Module Name: src Committed By: rillig Date: Mon Oct 5 17:33:21 UTC 2020
Modified Files: src/usr.bin/make: main.c Log Message: make(1): extract init_machine and init_machine_arch from main To generate a diff of this commit: cvs rdiff -u -r1.366 -r1.367 src/usr.bin/make/main.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/main.c diff -u src/usr.bin/make/main.c:1.366 src/usr.bin/make/main.c:1.367 --- src/usr.bin/make/main.c:1.366 Sun Oct 4 19:36:32 2020 +++ src/usr.bin/make/main.c Mon Oct 5 17:33:21 2020 @@ -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() */