Tue Oct 27 17:36:17 2020 UTC ()
make(1): extract InitVarMake from main


(rillig)
diff -r1.404 -r1.405 src/usr.bin/make/main.c

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

--- src/usr.bin/make/main.c 2020/10/27 17:09:09 1.404
+++ src/usr.bin/make/main.c 2020/10/27 17:36:17 1.405
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: main.c,v 1.404 2020/10/27 17:09:09 rillig Exp $ */ 1/* $NetBSD: main.c,v 1.405 2020/10/27 17:36:17 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.
@@ -108,27 +108,27 @@ @@ -108,27 +108,27 @@
108 108
109#include <errno.h> 109#include <errno.h>
110#include <signal.h> 110#include <signal.h>
111#include <stdarg.h> 111#include <stdarg.h>
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/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */ 120/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
121MAKE_RCSID("$NetBSD: main.c,v 1.404 2020/10/27 17:09:09 rillig Exp $"); 121MAKE_RCSID("$NetBSD: main.c,v 1.405 2020/10/27 17:36:17 rillig Exp $");
122#if defined(MAKE_NATIVE) && !defined(lint) 122#if defined(MAKE_NATIVE) && !defined(lint)
123__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 " 123__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
124 "The Regents of the University of California. " 124 "The Regents of the University of California. "
125 "All rights reserved."); 125 "All rights reserved.");
126#endif 126#endif
127 127
128#ifndef DEFMAXLOCAL 128#ifndef DEFMAXLOCAL
129#define DEFMAXLOCAL DEFMAXJOBS 129#define DEFMAXLOCAL DEFMAXJOBS
130#endif 130#endif
131 131
132CmdOpts opts; 132CmdOpts opts;
133time_t now; /* Time at start of make */ 133time_t now; /* Time at start of make */
134GNode *DEFAULT; /* .DEFAULT node */ 134GNode *DEFAULT; /* .DEFAULT node */
@@ -1086,26 +1086,53 @@ CmdOpts_Init(void) @@ -1086,26 +1086,53 @@ CmdOpts_Init(void)
1086 opts.queryFlag = FALSE; /* This is not just a check-run */ 1086 opts.queryFlag = FALSE; /* This is not just a check-run */
1087 opts.noBuiltins = FALSE; /* Read the built-in rules */ 1087 opts.noBuiltins = FALSE; /* Read the built-in rules */
1088 opts.beSilent = FALSE; /* Print commands as executed */ 1088 opts.beSilent = FALSE; /* Print commands as executed */
1089 opts.touchFlag = FALSE; /* Actually update targets */ 1089 opts.touchFlag = FALSE; /* Actually update targets */
1090 opts.printVars = 0; 1090 opts.printVars = 0;
1091 opts.variables = Lst_New(); 1091 opts.variables = Lst_New();
1092 opts.parseWarnFatal = FALSE; 1092 opts.parseWarnFatal = FALSE;
1093 opts.enterFlag = FALSE; 1093 opts.enterFlag = FALSE;
1094 opts.varNoExportEnv = FALSE; 1094 opts.varNoExportEnv = FALSE;
1095 opts.create = Lst_New(); 1095 opts.create = Lst_New();
1096} 1096}
1097 1097
1098static void 1098static void
 1099InitVarMake(char **argv)
 1100{
 1101 char mdpath[MAXPATHLEN];
 1102 const char *p1;
 1103
 1104 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
 1105 /*
 1106 * Leave alone if it is an absolute path, or if it does
 1107 * not contain a '/' in which case we need to find it in
 1108 * the path, like execvp(3) and the shells do.
 1109 */
 1110 p1 = argv[0];
 1111 } else {
 1112 struct stat sb;
 1113 /*
 1114 * A relative path, canonicalize it.
 1115 */
 1116 p1 = cached_realpath(argv[0], mdpath);
 1117 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
 1118 p1 = argv[0]; /* realpath failed */
 1119 }
 1120 }
 1121 Var_Set("MAKE", p1, VAR_GLOBAL);
 1122 Var_Set(".MAKE", p1, VAR_GLOBAL);
 1123}
 1124
 1125static void
1099InitDefIncPath(char *syspath) 1126InitDefIncPath(char *syspath)
1100{ 1127{
1101 static char defsyspath[] = _PATH_DEFSYSPATH; 1128 static char defsyspath[] = _PATH_DEFSYSPATH;
1102 char *start, *cp; 1129 char *start, *cp;
1103 1130
1104 /* 1131 /*
1105 * If no user-supplied system path was given (through the -m option) 1132 * If no user-supplied system path was given (through the -m option)
1106 * add the directories from the DEFSYSPATH (more than one may be given 1133 * add the directories from the DEFSYSPATH (more than one may be given
1107 * as dir1:...:dirn) to the system include path. 1134 * as dir1:...:dirn) to the system include path.
1108 */ 1135 */
1109 /* XXX: mismatch: the -m option sets sysIncPath, not syspath */ 1136 /* XXX: mismatch: the -m option sets sysIncPath, not syspath */
1110 if (syspath == NULL || syspath[0] == '\0') 1137 if (syspath == NULL || syspath[0] == '\0')
1111 syspath = defsyspath; 1138 syspath = defsyspath;
@@ -1250,27 +1277,26 @@ CleanUp(void) @@ -1250,27 +1277,26 @@ CleanUp(void)
1250 * Results: 1277 * Results:
1251 * If -q was given, exits -1 if anything was out-of-date. Else it exits 1278 * If -q was given, exits -1 if anything was out-of-date. Else it exits
1252 * 0. 1279 * 0.
1253 * 1280 *
1254 * Side Effects: 1281 * Side Effects:
1255 * The program exits when done. Targets are created. etc. etc. etc. 1282 * The program exits when done. Targets are created. etc. etc. etc.
1256 */ 1283 */
1257int 1284int
1258main(int argc, char **argv) 1285main(int argc, char **argv)
1259{ 1286{
1260 Boolean outOfDate; /* FALSE if all targets up to date */ 1287 Boolean outOfDate; /* FALSE if all targets up to date */
1261 struct stat sa; 1288 struct stat sa;
1262 char *p1; 1289 char *p1;
1263 char mdpath[MAXPATHLEN]; 
1264 const char *machine; 1290 const char *machine;
1265 const char *machine_arch; 1291 const char *machine_arch;
1266 char *syspath = getenv("MAKESYSPATH"); 1292 char *syspath = getenv("MAKESYSPATH");
1267 struct utsname utsname; 1293 struct utsname utsname;
1268 1294
1269 /* default to writing debug to stderr */ 1295 /* default to writing debug to stderr */
1270 opts.debug_file = stderr; 1296 opts.debug_file = stderr;
1271 1297
1272#ifdef SIGINFO 1298#ifdef SIGINFO
1273 (void)bmake_signal(SIGINFO, siginfo); 1299 (void)bmake_signal(SIGINFO, siginfo);
1274#endif 1300#endif
1275 1301
1276 InitRandom(); 1302 InitRandom();
@@ -1334,45 +1360,27 @@ main(int argc, char **argv) @@ -1334,45 +1360,27 @@ main(int argc, char **argv)
1334 /* 1360 /*
1335 * Initialize the parsing, directory and variable modules to prepare 1361 * Initialize the parsing, directory and variable modules to prepare
1336 * for the reading of inclusion paths and variable settings on the 1362 * for the reading of inclusion paths and variable settings on the
1337 * command line 1363 * command line
1338 */ 1364 */
1339 1365
1340 /* 1366 /*
1341 * Initialize various variables. 1367 * Initialize various variables.
1342 * MAKE also gets this name, for compatibility 1368 * MAKE also gets this name, for compatibility
1343 * .MAKEFLAGS gets set to the empty string just in case. 1369 * .MAKEFLAGS gets set to the empty string just in case.
1344 * MFLAGS also gets initialized empty, for compatibility. 1370 * MFLAGS also gets initialized empty, for compatibility.
1345 */ 1371 */
1346 Parse_Init(); 1372 Parse_Init();
1347 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) { 1373 InitVarMake(argv);
1348 /* 
1349 * Leave alone if it is an absolute path, or if it does 
1350 * not contain a '/' in which case we need to find it in 
1351 * the path, like execvp(3) and the shells do. 
1352 */ 
1353 p1 = argv[0]; 
1354 } else { 
1355 struct stat sb; 
1356 /* 
1357 * A relative path, canonicalize it. 
1358 */ 
1359 p1 = cached_realpath(argv[0], mdpath); 
1360 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) { 
1361 p1 = argv[0]; /* realpath failed */ 
1362 } 
1363 } 
1364 Var_Set("MAKE", p1, VAR_GLOBAL); 
1365 Var_Set(".MAKE", p1, VAR_GLOBAL); 
1366 Var_Set(MAKEFLAGS, "", VAR_GLOBAL); 1374 Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
1367 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL); 1375 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL);
1368 Var_Set("MFLAGS", "", VAR_GLOBAL); 1376 Var_Set("MFLAGS", "", VAR_GLOBAL);
1369 Var_Set(".ALLTARGETS", "", VAR_GLOBAL); 1377 Var_Set(".ALLTARGETS", "", VAR_GLOBAL);
1370 /* some makefiles need to know this */ 1378 /* some makefiles need to know this */
1371 Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD); 1379 Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD);
1372 1380
1373 /* 1381 /*
1374 * Set some other useful macros 1382 * Set some other useful macros
1375 */ 1383 */
1376 { 1384 {
1377 char tmp[64], *ep; 1385 char tmp[64], *ep;
1378 1386