Mon Oct 5 17:33:21 2020 UTC ()
make(1): extract init_machine and init_machine_arch from main


(rillig)
diff -r1.366 -r1.367 src/usr.bin/make/main.c

cvs diff -r1.366 -r1.367 src/usr.bin/make/main.c (expand / switch to unified diff)

--- src/usr.bin/make/main.c 2020/10/04 19:36:32 1.366
+++ src/usr.bin/make/main.c 2020/10/05 17:33:21 1.367
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: main.c,v 1.366 2020/10/04 19:36:32 rillig Exp $ */ 1/* $NetBSD: main.c,v 1.367 2020/10/05 17:33:21 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -112,27 +112,27 @@ @@ -112,27 +112,27 @@
112#include <time.h> 112#include <time.h>
113 113
114#include "make.h" 114#include "make.h"
115#include "dir.h" 115#include "dir.h"
116#include "job.h" 116#include "job.h"
117#include "pathnames.h" 117#include "pathnames.h"
118#include "trace.h" 118#include "trace.h"
119 119
120#ifdef USE_IOVEC 120#ifdef USE_IOVEC
121#include <sys/uio.h> 121#include <sys/uio.h>
122#endif 122#endif
123 123
124/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */ 124/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
125MAKE_RCSID("$NetBSD: main.c,v 1.366 2020/10/04 19:36:32 rillig Exp $"); 125MAKE_RCSID("$NetBSD: main.c,v 1.367 2020/10/05 17:33:21 rillig Exp $");
126#if defined(MAKE_NATIVE) && !defined(lint) 126#if defined(MAKE_NATIVE) && !defined(lint)
127__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 " 127__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
128 "The Regents of the University of California. " 128 "The Regents of the University of California. "
129 "All rights reserved."); 129 "All rights reserved.");
130#endif 130#endif
131 131
132#ifndef DEFMAXLOCAL 132#ifndef DEFMAXLOCAL
133#define DEFMAXLOCAL DEFMAXJOBS 133#define DEFMAXLOCAL DEFMAXJOBS
134#endif 134#endif
135 135
136StringList * create; /* Targets to be made */ 136StringList * create; /* Targets to be made */
137time_t now; /* Time at start of make */ 137time_t now; /* Time at start of make */
138GNode *DEFAULT; /* .DEFAULT node */ 138GNode *DEFAULT; /* .DEFAULT node */
@@ -963,52 +963,106 @@ InitVarTargets(void) @@ -963,52 +963,106 @@ InitVarTargets(void)
963 StringListNode *ln; 963 StringListNode *ln;
964 964
965 if (Lst_IsEmpty(create)) { 965 if (Lst_IsEmpty(create)) {
966 Var_Set(".TARGETS", "", VAR_GLOBAL); 966 Var_Set(".TARGETS", "", VAR_GLOBAL);
967 return; 967 return;
968 } 968 }
969 969
970 for (ln = create->first; ln != NULL; ln = ln->next) { 970 for (ln = create->first; ln != NULL; ln = ln->next) {
971 char *name = ln->datum; 971 char *name = ln->datum;
972 Var_Append(".TARGETS", name, VAR_GLOBAL); 972 Var_Append(".TARGETS", name, VAR_GLOBAL);
973 } 973 }
974} 974}
975 975
 976static const char *
 977init_machine(const struct utsname *utsname)
 978{
 979 const char *machine = getenv("MACHINE");
 980 if (machine != NULL)
 981 return machine;
 982
 983#ifdef MAKE_NATIVE
 984 return utsname->machine;
 985#else
 986#ifdef MAKE_MACHINE
 987 return MAKE_MACHINE;
 988#else
 989 return "unknown";
 990#endif
 991#endif
 992}
 993
 994static const char *
 995init_machine_arch(void)
 996{
 997 const char *env = getenv("MACHINE_ARCH");
 998 if (env != NULL)
 999 return env;
 1000
 1001#ifdef MAKE_NATIVE
 1002 {
 1003 struct utsname utsname;
 1004 static char machine_arch_buf[sizeof(utsname.machine)];
 1005 const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
 1006 size_t len = sizeof(machine_arch_buf);
 1007
 1008 if (sysctl(mib, __arraycount(mib), machine_arch_buf,
 1009 &len, NULL, 0) < 0) {
 1010 (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
 1011 strerror(errno));
 1012 exit(2);
 1013 }
 1014
 1015 return machine_arch_buf;
 1016 }
 1017#else
 1018#ifndef MACHINE_ARCH
 1019#ifdef MAKE_MACHINE_ARCH
 1020 return MAKE_MACHINE_ARCH;
 1021#else
 1022 return "unknown";
 1023#endif
 1024#else
 1025 return MACHINE_ARCH;
 1026#endif
 1027#endif
 1028}
 1029
976/*- 1030/*-
977 * main -- 1031 * main --
978 * The main function, for obvious reasons. Initializes variables 1032 * The main function, for obvious reasons. Initializes variables
979 * and a few modules, then parses the arguments give it in the 1033 * and a few modules, then parses the arguments give it in the
980 * environment and on the command line. Reads the system makefile 1034 * environment and on the command line. Reads the system makefile
981 * followed by either Makefile, makefile or the file given by the 1035 * followed by either Makefile, makefile or the file given by the
982 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 1036 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
983 * flags it has received by then uses either the Make or the Compat 1037 * flags it has received by then uses either the Make or the Compat
984 * module to create the initial list of targets. 1038 * module to create the initial list of targets.
985 * 1039 *
986 * Results: 1040 * Results:
987 * If -q was given, exits -1 if anything was out-of-date. Else it exits 1041 * If -q was given, exits -1 if anything was out-of-date. Else it exits
988 * 0. 1042 * 0.
989 * 1043 *
990 * Side Effects: 1044 * Side Effects:
991 * The program exits when done. Targets are created. etc. etc. etc. 1045 * The program exits when done. Targets are created. etc. etc. etc.
992 */ 1046 */
993int 1047int
994main(int argc, char **argv) 1048main(int argc, char **argv)
995{ 1049{
996 Boolean outOfDate; /* FALSE if all targets up to date */ 1050 Boolean outOfDate; /* FALSE if all targets up to date */
997 struct stat sb, sa; 1051 struct stat sb, sa;
998 char *p1, *path; 1052 char *p1, *path;
999 char mdpath[MAXPATHLEN]; 1053 char mdpath[MAXPATHLEN];
1000 const char *machine = getenv("MACHINE"); 1054 const char *machine;
1001 const char *machine_arch = getenv("MACHINE_ARCH"); 1055 const char *machine_arch;
1002 char *syspath = getenv("MAKESYSPATH"); 1056 char *syspath = getenv("MAKESYSPATH");
1003 StringList *sysMkPath; /* Path of sys.mk */ 1057 StringList *sysMkPath; /* Path of sys.mk */
1004 char *cp = NULL, *start; 1058 char *cp = NULL, *start;
1005 /* avoid faults on read-only strings */ 1059 /* avoid faults on read-only strings */
1006 static char defsyspath[] = _PATH_DEFSYSPATH; 1060 static char defsyspath[] = _PATH_DEFSYSPATH;
1007 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */ 1061 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
1008 struct timeval rightnow; /* to initialize random seed */ 1062 struct timeval rightnow; /* to initialize random seed */
1009 struct utsname utsname; 1063 struct utsname utsname;
1010 1064
1011 /* default to writing debug to stderr */ 1065 /* default to writing debug to stderr */
1012 debug_file = stderr; 1066 debug_file = stderr;
1013 1067
1014#ifdef SIGINFO 1068#ifdef SIGINFO
@@ -1043,64 +1097,28 @@ main(int argc, char **argv) @@ -1043,64 +1097,28 @@ main(int argc, char **argv)
1043 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname, 1097 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
1044 strerror(errno)); 1098 strerror(errno));
1045 exit(2); 1099 exit(2);
1046 } 1100 }
1047 1101
1048 /* 1102 /*
1049 * Get the name of this type of MACHINE from utsname 1103 * Get the name of this type of MACHINE from utsname
1050 * so we can share an executable for similar machines. 1104 * so we can share an executable for similar machines.
1051 * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 1105 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
1052 * 1106 *
1053 * Note that both MACHINE and MACHINE_ARCH are decided at 1107 * Note that both MACHINE and MACHINE_ARCH are decided at
1054 * run-time. 1108 * run-time.
1055 */ 1109 */
1056 if (!machine) { 1110 machine = init_machine(&utsname);
1057#ifdef MAKE_NATIVE 1111 machine_arch = init_machine_arch();
1058 machine = utsname.machine; 
1059#else 
1060#ifdef MAKE_MACHINE 
1061 machine = MAKE_MACHINE; 
1062#else 
1063 machine = "unknown"; 
1064#endif 
1065#endif 
1066 } 
1067 
1068 if (!machine_arch) { 
1069#ifdef MAKE_NATIVE 
1070 static char machine_arch_buf[sizeof(utsname.machine)]; 
1071 const int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; 
1072 size_t len = sizeof(machine_arch_buf); 
1073 
1074 if (sysctl(mib, __arraycount(mib), machine_arch_buf, 
1075 &len, NULL, 0) < 0) { 
1076 (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname, 
1077 strerror(errno)); 
1078 exit(2); 
1079 } 
1080 
1081 machine_arch = machine_arch_buf; 
1082#else 
1083#ifndef MACHINE_ARCH 
1084#ifdef MAKE_MACHINE_ARCH 
1085 machine_arch = MAKE_MACHINE_ARCH; 
1086#else 
1087 machine_arch = "unknown"; 
1088#endif 
1089#else 
1090 machine_arch = MACHINE_ARCH; 
1091#endif 
1092#endif 
1093 } 
1094 1112
1095 myPid = getpid(); /* remember this for vFork() */ 1113 myPid = getpid(); /* remember this for vFork() */
1096 1114
1097 /* 1115 /*
1098 * Just in case MAKEOBJDIR wants us to do something tricky. 1116 * Just in case MAKEOBJDIR wants us to do something tricky.
1099 */ 1117 */
1100 Var_Init(); /* Initialize the lists of variables for 1118 Var_Init(); /* Initialize the lists of variables for
1101 * parsing arguments */ 1119 * parsing arguments */
1102 Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL); 1120 Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL);
1103 Var_Set("MACHINE", machine, VAR_GLOBAL); 1121 Var_Set("MACHINE", machine, VAR_GLOBAL);
1104 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL); 1122 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
1105#ifdef MAKE_VERSION 1123#ifdef MAKE_VERSION
1106 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL); 1124 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);