Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): extract init_machine and init_machine_...



details:   https://anonhg.NetBSD.org/src/rev/acc7f11c1fde
branches:  trunk
changeset: 940195:acc7f11c1fde
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Oct 05 17:33:21 2020 +0000

description:
make(1): extract init_machine and init_machine_arch from main

diffstat:

 usr.bin/make/main.c |  102 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 60 insertions(+), 42 deletions(-)

diffs (137 lines):

diff -r 4e2d38e55a92 -r acc7f11c1fde usr.bin/make/main.c
--- a/usr.bin/make/main.c       Mon Oct 05 17:29:22 2020 +0000
+++ b/usr.bin/make/main.c       Mon Oct 05 17:33:21 2020 +0000
@@ -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 @@
        }
 }
 
+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 @@
        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 @@
         * 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() */
 



Home | Main Index | Thread Index | Old Index