Tue Oct 27 19:16:46 2020 UTC ()
make(1): extract InitMaxJobs from main


(rillig)
diff -r1.407 -r1.408 src/usr.bin/make/main.c

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

--- src/usr.bin/make/main.c 2020/10/27 18:16:19 1.407
+++ src/usr.bin/make/main.c 2020/10/27 19:16:46 1.408
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: main.c,v 1.407 2020/10/27 18:16:19 rillig Exp $ */ 1/* $NetBSD: main.c,v 1.408 2020/10/27 19:16:46 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.407 2020/10/27 18:16:19 rillig Exp $"); 121MAKE_RCSID("$NetBSD: main.c,v 1.408 2020/10/27 19:16:46 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 */
@@ -1184,26 +1184,58 @@ ReadBuiltinRules(void) @@ -1184,26 +1184,58 @@ ReadBuiltinRules(void)
1184{ 1184{
1185 StringList *sysMkPath = Lst_New(); 1185 StringList *sysMkPath = Lst_New();
1186 Dir_Expand(_PATH_DEFSYSMK, 1186 Dir_Expand(_PATH_DEFSYSMK,
1187 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath, 1187 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
1188 sysMkPath); 1188 sysMkPath);
1189 if (Lst_IsEmpty(sysMkPath)) 1189 if (Lst_IsEmpty(sysMkPath))
1190 Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK); 1190 Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK);
1191 if (!Lst_ForEachUntil(sysMkPath, ReadMakefileSucceeded, NULL)) 1191 if (!Lst_ForEachUntil(sysMkPath, ReadMakefileSucceeded, NULL))
1192 Fatal("%s: cannot open %s.", progname, 1192 Fatal("%s: cannot open %s.", progname,
1193 (char *)sysMkPath->first->datum); 1193 (char *)sysMkPath->first->datum);
1194 /* XXX: sysMkPath is not freed */ 1194 /* XXX: sysMkPath is not freed */
1195} 1195}
1196 1196
 1197static void
 1198InitMaxJobs(void)
 1199{
 1200 char *value;
 1201 int n;
 1202
 1203 if (forceJobs || opts.compatMake ||
 1204 !Var_Exists(".MAKE.JOBS", VAR_GLOBAL))
 1205 return;
 1206
 1207 (void)Var_Subst("${.MAKE.JOBS}", VAR_GLOBAL, VARE_WANTRES, &value);
 1208 /* TODO: handle errors */
 1209 n = (int)strtol(value, NULL, 0);
 1210 if (n < 1) {
 1211 (void)fprintf(stderr,
 1212 "%s: illegal value for .MAKE.JOBS "
 1213 "-- must be positive integer!\n",
 1214 progname);
 1215 exit(1);
 1216 }
 1217
 1218 if (n != opts.maxJobs) {
 1219 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
 1220 Var_Append(MAKEFLAGS, value, VAR_GLOBAL);
 1221 }
 1222
 1223 opts.maxJobs = n;
 1224 maxJobTokens = opts.maxJobs;
 1225 forceJobs = TRUE;
 1226 free(value);
 1227}
 1228
1197/* 1229/*
1198 * For compatibility, look at the directories in the VPATH variable 1230 * For compatibility, look at the directories in the VPATH variable
1199 * and add them to the search path, if the variable is defined. The 1231 * and add them to the search path, if the variable is defined. The
1200 * variable's value is in the same format as the PATH environment 1232 * variable's value is in the same format as the PATH environment
1201 * variable, i.e. <directory>:<directory>:<directory>... 1233 * variable, i.e. <directory>:<directory>:<directory>...
1202 */ 1234 */
1203static void 1235static void
1204InitVpath(void) 1236InitVpath(void)
1205{ 1237{
1206 char *vpath, savec, *path; 1238 char *vpath, savec, *path;
1207 if (!Var_Exists("VPATH", VAR_CMD)) 1239 if (!Var_Exists("VPATH", VAR_CMD))
1208 return; 1240 return;
1209 1241
@@ -1505,48 +1537,27 @@ main(int argc, char **argv) @@ -1505,48 +1537,27 @@ main(int argc, char **argv)
1505 (void)ReadMakefile(makeDependfile); 1537 (void)ReadMakefile(makeDependfile);
1506 doing_depend = FALSE; 1538 doing_depend = FALSE;
1507 } 1539 }
1508 } 1540 }
1509 1541
1510 if (enterFlagObj) 1542 if (enterFlagObj)
1511 printf("%s: Entering directory `%s'\n", progname, objdir); 1543 printf("%s: Entering directory `%s'\n", progname, objdir);
1512 1544
1513 MakeMode(NULL); 1545 MakeMode(NULL);
1514 1546
1515 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 1547 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
1516 bmake_free(p1); 1548 bmake_free(p1);
1517 1549
1518 if (!forceJobs && !opts.compatMake && 1550 InitMaxJobs();
1519 Var_Exists(".MAKE.JOBS", VAR_GLOBAL)) { 
1520 char *value; 
1521 int n; 
1522 
1523 (void)Var_Subst("${.MAKE.JOBS}", VAR_GLOBAL, VARE_WANTRES, &value); 
1524 /* TODO: handle errors */ 
1525 n = (int)strtol(value, NULL, 0); 
1526 if (n < 1) { 
1527 (void)fprintf(stderr, "%s: illegal value for .MAKE.JOBS -- must be positive integer!\n", 
1528 progname); 
1529 exit(1); 
1530 } 
1531 if (n != opts.maxJobs) { 
1532 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 
1533 Var_Append(MAKEFLAGS, value, VAR_GLOBAL); 
1534 } 
1535 opts.maxJobs = n; 
1536 maxJobTokens = opts.maxJobs; 
1537 forceJobs = TRUE; 
1538 free(value); 
1539 } 
1540 1551
1541 /* 1552 /*
1542 * Be compatible if user did not specify -j and did not explicitly 1553 * Be compatible if user did not specify -j and did not explicitly
1543 * turned compatibility on 1554 * turned compatibility on
1544 */ 1555 */
1545 if (!opts.compatMake && !forceJobs) { 1556 if (!opts.compatMake && !forceJobs) {
1546 opts.compatMake = TRUE; 1557 opts.compatMake = TRUE;
1547 } 1558 }
1548 1559
1549 if (!opts.compatMake) 1560 if (!opts.compatMake)
1550 Job_ServerStart(maxJobTokens, jp_0, jp_1); 1561 Job_ServerStart(maxJobTokens, jp_0, jp_1);
1551 DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n", 1562 DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1552 jp_0, jp_1, opts.maxJobs, maxJobTokens, opts.compatMake ? 1 : 0); 1563 jp_0, jp_1, opts.maxJobs, maxJobTokens, opts.compatMake ? 1 : 0);