make(1): extract init_machine and init_machine_arch from main

This commit is contained in:
rillig 2020-10-05 17:33:21 +00:00
parent ec2482217d
commit 37b6a2b531
1 changed files with 60 additions and 42 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.366 2020/10/04 19:36:32 rillig Exp $ */
/* $NetBSD: main.c,v 1.367 2020/10/05 17:33:21 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -122,7 +122,7 @@
#endif
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: main.c,v 1.366 2020/10/04 19:36:32 rillig Exp $");
MAKE_RCSID("$NetBSD: main.c,v 1.367 2020/10/05 17:33:21 rillig Exp $");
#if defined(MAKE_NATIVE) && !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
"The Regents of the University of California. "
@ -973,6 +973,60 @@ InitVarTargets(void)
}
}
static const char *
init_machine(const struct utsname *utsname)
{
const char *machine = getenv("MACHINE");
if (machine != NULL)
return machine;
#ifdef MAKE_NATIVE
return utsname->machine;
#else
#ifdef MAKE_MACHINE
return MAKE_MACHINE;
#else
return "unknown";
#endif
#endif
}
static const char *
init_machine_arch(void)
{
const char *env = getenv("MACHINE_ARCH");
if (env != NULL)
return env;
#ifdef MAKE_NATIVE
{
struct utsname utsname;
static char machine_arch_buf[sizeof(utsname.machine)];
const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
size_t len = sizeof(machine_arch_buf);
if (sysctl(mib, __arraycount(mib), machine_arch_buf,
&len, NULL, 0) < 0) {
(void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
strerror(errno));
exit(2);
}
return machine_arch_buf;
}
#else
#ifndef MACHINE_ARCH
#ifdef MAKE_MACHINE_ARCH
return MAKE_MACHINE_ARCH;
#else
return "unknown";
#endif
#else
return MACHINE_ARCH;
#endif
#endif
}
/*-
* main --
* The main function, for obvious reasons. Initializes variables
@ -997,8 +1051,8 @@ main(int argc, char **argv)
struct stat sb, sa;
char *p1, *path;
char mdpath[MAXPATHLEN];
const char *machine = getenv("MACHINE");
const char *machine_arch = getenv("MACHINE_ARCH");
const char *machine;
const char *machine_arch;
char *syspath = getenv("MAKESYSPATH");
StringList *sysMkPath; /* Path of sys.mk */
char *cp = NULL, *start;
@ -1053,44 +1107,8 @@ main(int argc, char **argv)
* Note that both MACHINE and MACHINE_ARCH are decided at
* run-time.
*/
if (!machine) {
#ifdef MAKE_NATIVE
machine = utsname.machine;
#else
#ifdef MAKE_MACHINE
machine = MAKE_MACHINE;
#else
machine = "unknown";
#endif
#endif
}
if (!machine_arch) {
#ifdef MAKE_NATIVE
static char machine_arch_buf[sizeof(utsname.machine)];
const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
size_t len = sizeof(machine_arch_buf);
if (sysctl(mib, __arraycount(mib), machine_arch_buf,
&len, NULL, 0) < 0) {
(void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
strerror(errno));
exit(2);
}
machine_arch = machine_arch_buf;
#else
#ifndef MACHINE_ARCH
#ifdef MAKE_MACHINE_ARCH
machine_arch = MAKE_MACHINE_ARCH;
#else
machine_arch = "unknown";
#endif
#else
machine_arch = MACHINE_ARCH;
#endif
#endif
}
machine = init_machine(&utsname);
machine_arch = init_machine_arch();
myPid = getpid(); /* remember this for vFork() */