main.c revision 1.658 1 /* $NetBSD: main.c,v 1.658 2025/06/13 03:51:18 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * Adam de Boor.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 */
70
71 /*
72 * The main file for this entire program. Exit routines etc. reside here.
73 *
74 * Utility functions defined in this file:
75 *
76 * Main_ParseArgLine
77 * Parse and process command line arguments from a
78 * single string. Used to implement the special targets
79 * .MFLAGS and .MAKEFLAGS.
80 *
81 * Error Print a tagged error message.
82 *
83 * Fatal Print an error message and exit.
84 *
85 * Punt Abort all jobs and exit with a message.
86 */
87
88 #include <sys/types.h>
89 #include <sys/time.h>
90 #include <sys/param.h>
91 #include <sys/resource.h>
92 #include <sys/stat.h>
93 #ifdef MAKE_NATIVE
94 #include <sys/sysctl.h>
95 #endif
96 #include <sys/utsname.h>
97 #include <sys/wait.h>
98
99 #include <errno.h>
100 #include <signal.h>
101 #include <stdarg.h>
102 #include <time.h>
103
104 #include "make.h"
105 #include "dir.h"
106 #include "job.h"
107 #ifdef USE_META
108 # include "meta.h"
109 #endif
110 #include "pathnames.h"
111 #include "trace.h"
112
113 /* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
114 MAKE_RCSID("$NetBSD: main.c,v 1.658 2025/06/13 03:51:18 rillig Exp $");
115 #if defined(MAKE_NATIVE)
116 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
117 "The Regents of the University of California. "
118 "All rights reserved.");
119 #endif
120
121 CmdOpts opts;
122 time_t now; /* Time at start of make */
123 GNode *defaultNode; /* .DEFAULT node */
124 bool allPrecious; /* .PRECIOUS given on a line by itself */
125 bool deleteOnError; /* .DELETE_ON_ERROR: set */
126
127 static int maxJobTokens; /* -j argument */
128 static bool enterFlagObj; /* -w and objdir != srcdir */
129 static bool bogusJflag; /* -J invalid */
130
131 static int tokenPoolReader = -1, tokenPoolWriter = -1;
132 bool doing_depend; /* Set while reading .depend */
133 static bool jobsRunning; /* true if the jobs might be running */
134 static const char *tracefile;
135 static bool ReadMakefile(const char *);
136 static void purge_relative_cached_realpaths(void);
137
138 static bool ignorePWD; /* if we use -C, PWD is meaningless */
139 static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
140 char curdir[MAXPATHLEN + 1]; /* Startup directory */
141 const char *progname;
142 char *makeDependfile;
143 pid_t myPid;
144 int makelevel;
145
146 bool forceJobs = false;
147 static int main_errors = 0;
148 static HashTable cached_realpaths;
149
150 /*
151 * For compatibility with the POSIX version of MAKEFLAGS that includes
152 * all the options without '-', convert 'flags' to '-f -l -a -g -s '.
153 */
154 static char *
155 explode(const char *flags)
156 {
157 char *exploded, *ep;
158 const char *p;
159
160 if (flags == NULL)
161 return NULL;
162
163 for (p = flags; *p != '\0'; p++)
164 if (!ch_isalpha(*p))
165 return bmake_strdup(flags);
166
167 exploded = bmake_malloc((size_t)(p - flags) * 3 + 1);
168 for (p = flags, ep = exploded; *p != '\0'; p++) {
169 *ep++ = '-';
170 *ep++ = *p;
171 *ep++ = ' ';
172 }
173 *ep = '\0';
174 return exploded;
175 }
176
177 MAKE_ATTR_DEAD static void
178 usage(void)
179 {
180 size_t prognameLen = strcspn(progname, "[");
181
182 (void)fprintf(stderr,
183 "usage: %.*s [-BeikNnqrSstWwX]\n"
184 " [-C directory] [-D variable] [-d flags] [-f makefile]\n"
185 " [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
186 " [-V variable] [-v variable] [variable=value] [target ...]\n",
187 (int)prognameLen, progname);
188 exit(2);
189 }
190
191 static void
192 MainParseArgDebugFile(const char *arg)
193 {
194 const char *mode;
195 size_t len;
196 char *fname;
197
198 if (opts.debug_file != stdout && opts.debug_file != stderr)
199 fclose(opts.debug_file);
200
201 if (*arg == '+') {
202 arg++;
203 mode = "a";
204 } else
205 mode = "w";
206
207 if (strcmp(arg, "stdout") == 0) {
208 opts.debug_file = stdout;
209 return;
210 }
211 if (strcmp(arg, "stderr") == 0) {
212 opts.debug_file = stderr;
213 return;
214 }
215
216 len = strlen(arg);
217 fname = bmake_malloc(len + 20);
218 memcpy(fname, arg, len + 1);
219
220 /* Replace the trailing '%d' after '.%d' with the pid. */
221 if (len >= 3 && memcmp(fname + len - 3, ".%d", 3) == 0)
222 snprintf(fname + len - 2, 20, "%d", getpid());
223
224 opts.debug_file = fopen(fname, mode);
225 if (opts.debug_file == NULL) {
226 fprintf(stderr, "Cannot open debug file \"%s\"\n", fname);
227 exit(2);
228 }
229 free(fname);
230 }
231
232 static void
233 MainParseArgDebug(const char *argvalue)
234 {
235 const char *modules;
236 DebugFlags debug = opts.debug;
237
238 for (modules = argvalue; *modules != '\0'; modules++) {
239 switch (*modules) {
240 case '0': /* undocumented, only intended for tests */
241 memset(&debug, 0, sizeof(debug));
242 break;
243 case 'A':
244 memset(&debug, ~0, sizeof(debug));
245 break;
246 case 'a':
247 debug.DEBUG_ARCH = true;
248 break;
249 case 'C':
250 debug.DEBUG_CWD = true;
251 break;
252 case 'c':
253 debug.DEBUG_COND = true;
254 break;
255 case 'd':
256 debug.DEBUG_DIR = true;
257 break;
258 case 'e':
259 debug.DEBUG_ERROR = true;
260 break;
261 case 'f':
262 debug.DEBUG_FOR = true;
263 break;
264 case 'g':
265 if (modules[1] == '1') {
266 debug.DEBUG_GRAPH1 = true;
267 modules++;
268 } else if (modules[1] == '2') {
269 debug.DEBUG_GRAPH2 = true;
270 modules++;
271 } else if (modules[1] == '3') {
272 debug.DEBUG_GRAPH3 = true;
273 modules++;
274 }
275 break;
276 case 'h':
277 debug.DEBUG_HASH = true;
278 break;
279 case 'j':
280 debug.DEBUG_JOB = true;
281 break;
282 case 'L':
283 opts.strict = true;
284 break;
285 case 'l':
286 debug.DEBUG_LOUD = true;
287 break;
288 case 'M':
289 debug.DEBUG_META = true;
290 break;
291 case 'm':
292 debug.DEBUG_MAKE = true;
293 break;
294 case 'n':
295 debug.DEBUG_SCRIPT = true;
296 break;
297 case 'p':
298 debug.DEBUG_PARSE = true;
299 break;
300 case 's':
301 debug.DEBUG_SUFF = true;
302 break;
303 case 't':
304 debug.DEBUG_TARG = true;
305 break;
306 case 'V':
307 opts.debugVflag = true;
308 break;
309 case 'v':
310 debug.DEBUG_VAR = true;
311 break;
312 case 'x':
313 debug.DEBUG_SHELL = true;
314 break;
315 case 'F':
316 MainParseArgDebugFile(modules + 1);
317 goto finish;
318 default:
319 (void)fprintf(stderr,
320 "%s: illegal argument to d option -- %c\n",
321 progname, *modules);
322 usage();
323 }
324 }
325
326 finish:
327 opts.debug = debug;
328
329 setvbuf(opts.debug_file, NULL, _IONBF, 0);
330 if (opts.debug_file != stdout)
331 setvbuf(stdout, NULL, _IOLBF, 0);
332 }
333
334 /* Is path relative or does it contain any relative component "." or ".."? */
335 static bool
336 IsRelativePath(const char *path)
337 {
338 const char *p;
339
340 if (path[0] != '/')
341 return true;
342 p = path;
343 while ((p = strstr(p, "/.")) != NULL) {
344 p += 2;
345 if (*p == '.')
346 p++;
347 if (*p == '/' || *p == '\0')
348 return true;
349 }
350 return false;
351 }
352
353 static void
354 MainParseArgChdir(const char *argvalue)
355 {
356 struct stat sa, sb;
357
358 if (chdir(argvalue) == -1) {
359 (void)fprintf(stderr, "%s: chdir %s: %s\n",
360 progname, argvalue, strerror(errno));
361 exit(2); /* Not 1 so -q can distinguish error */
362 }
363 if (getcwd(curdir, MAXPATHLEN) == NULL) {
364 (void)fprintf(stderr, "%s: getcwd: %s\n",
365 progname, strerror(errno));
366 exit(2);
367 }
368 if (!IsRelativePath(argvalue) &&
369 stat(argvalue, &sa) != -1 &&
370 stat(curdir, &sb) != -1 &&
371 sa.st_ino == sb.st_ino &&
372 sa.st_dev == sb.st_dev)
373 snprintf(curdir, MAXPATHLEN, "%s", argvalue);
374 ignorePWD = true;
375 }
376
377 static void
378 MainParseArgJobsInternal(const char *argvalue)
379 {
380 char end;
381 if (sscanf(argvalue, "%d,%d%c",
382 &tokenPoolReader, &tokenPoolWriter, &end) != 2) {
383 (void)fprintf(stderr,
384 "%s: error: invalid internal option "
385 "\"-J %s\" in \"%s\"\n",
386 progname, argvalue, curdir);
387 exit(2);
388 }
389 if ((fcntl(tokenPoolReader, F_GETFD, 0) < 0) ||
390 (fcntl(tokenPoolWriter, F_GETFD, 0) < 0)) {
391 tokenPoolReader = -1;
392 tokenPoolWriter = -1;
393 bogusJflag = true;
394 } else {
395 Global_Append(MAKEFLAGS, "-J");
396 Global_Append(MAKEFLAGS, argvalue);
397 }
398 }
399
400 static void
401 MainParseArgJobs(const char *arg)
402 {
403 const char *p;
404 char *end;
405 char v[12];
406
407 forceJobs = true;
408 opts.maxJobs = (int)strtol(arg, &end, 0);
409 p = end;
410 #ifdef _SC_NPROCESSORS_ONLN
411 if (*p != '\0') {
412 double d;
413
414 if (*p == 'C')
415 d = (opts.maxJobs > 0) ? opts.maxJobs : 1;
416 else if (*p == '.') {
417 d = strtod(arg, &end);
418 p = end;
419 } else
420 d = 0.0;
421 if (d > 0.0) {
422 p = "";
423 opts.maxJobs = (int)sysconf(_SC_NPROCESSORS_ONLN);
424 opts.maxJobs = (int)(d * (double)opts.maxJobs);
425 }
426 }
427 #endif
428 if (*p != '\0' || opts.maxJobs < 1) {
429 (void)fprintf(stderr,
430 "%s: argument '%s' to option '-j' "
431 "must be a positive number\n",
432 progname, arg);
433 exit(2); /* Not 1 so -q can distinguish error */
434 }
435 snprintf(v, sizeof(v), "%d", opts.maxJobs);
436 Global_Append(MAKEFLAGS, "-j");
437 Global_Append(MAKEFLAGS, v);
438 Global_Set(".MAKE.JOBS", v);
439 maxJobTokens = opts.maxJobs;
440 }
441
442 static void
443 MainParseArgSysInc(const char *argvalue)
444 {
445 if (strncmp(argvalue, ".../", 4) == 0) {
446 char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
447 if (found_path == NULL)
448 return;
449 (void)SearchPath_Add(sysIncPath, found_path);
450 free(found_path);
451 } else {
452 (void)SearchPath_Add(sysIncPath, argvalue);
453 }
454 Global_Append(MAKEFLAGS, "-m");
455 Global_Append(MAKEFLAGS, argvalue);
456 Dir_SetSYSPATH();
457 }
458
459 static bool
460 MainParseOption(char c, const char *argvalue)
461 {
462 switch (c) {
463 case '\0':
464 break;
465 case 'B':
466 opts.compatMake = true;
467 Global_Append(MAKEFLAGS, "-B");
468 Global_Set(".MAKE.MODE", "compat");
469 break;
470 case 'C':
471 MainParseArgChdir(argvalue);
472 break;
473 case 'D':
474 if (argvalue[0] == '\0')
475 return false;
476 Var_SetExpand(SCOPE_GLOBAL, argvalue, "1");
477 Global_Append(MAKEFLAGS, "-D");
478 Global_Append(MAKEFLAGS, argvalue);
479 break;
480 case 'I':
481 SearchPath_Add(parseIncPath, argvalue);
482 Global_Append(MAKEFLAGS, "-I");
483 Global_Append(MAKEFLAGS, argvalue);
484 break;
485 case 'J':
486 MainParseArgJobsInternal(argvalue);
487 break;
488 case 'N':
489 opts.noExecute = true;
490 opts.noRecursiveExecute = true;
491 Global_Append(MAKEFLAGS, "-N");
492 break;
493 case 'S':
494 opts.keepgoing = false;
495 Global_Append(MAKEFLAGS, "-S");
496 break;
497 case 'T':
498 tracefile = bmake_strdup(argvalue);
499 Global_Append(MAKEFLAGS, "-T");
500 Global_Append(MAKEFLAGS, argvalue);
501 break;
502 case 'V':
503 case 'v':
504 opts.printVars = c == 'v' ? PVM_EXPANDED : PVM_UNEXPANDED;
505 Lst_Append(&opts.variables, bmake_strdup(argvalue));
506 /* XXX: Why always -V? */
507 Global_Append(MAKEFLAGS, "-V");
508 Global_Append(MAKEFLAGS, argvalue);
509 break;
510 case 'W':
511 opts.parseWarnFatal = true;
512 /* XXX: why no Global_Append? */
513 break;
514 case 'X':
515 opts.varNoExportEnv = true;
516 Global_Append(MAKEFLAGS, "-X");
517 break;
518 case 'd':
519 /* If '-d-opts' don't pass to children */
520 if (argvalue[0] == '-')
521 argvalue++;
522 else {
523 Global_Append(MAKEFLAGS, "-d");
524 Global_Append(MAKEFLAGS, argvalue);
525 }
526 MainParseArgDebug(argvalue);
527 break;
528 case 'e':
529 opts.checkEnvFirst = true;
530 Global_Append(MAKEFLAGS, "-e");
531 break;
532 case 'f':
533 Lst_Append(&opts.makefiles, bmake_strdup(argvalue));
534 break;
535 case 'i':
536 opts.ignoreErrors = true;
537 Global_Append(MAKEFLAGS, "-i");
538 break;
539 case 'j':
540 MainParseArgJobs(argvalue);
541 break;
542 case 'k':
543 opts.keepgoing = true;
544 Global_Append(MAKEFLAGS, "-k");
545 break;
546 case 'm':
547 MainParseArgSysInc(argvalue);
548 /* XXX: why no Var_Append? */
549 break;
550 case 'n':
551 opts.noExecute = true;
552 Global_Append(MAKEFLAGS, "-n");
553 break;
554 case 'q':
555 opts.query = true;
556 /* Kind of nonsensical, wot? */
557 Global_Append(MAKEFLAGS, "-q");
558 break;
559 case 'r':
560 opts.noBuiltins = true;
561 Global_Append(MAKEFLAGS, "-r");
562 break;
563 case 's':
564 opts.silent = true;
565 Global_Append(MAKEFLAGS, "-s");
566 break;
567 case 't':
568 opts.touch = true;
569 Global_Append(MAKEFLAGS, "-t");
570 break;
571 case 'w':
572 opts.enterFlag = true;
573 Global_Append(MAKEFLAGS, "-w");
574 break;
575 default:
576 usage();
577 }
578 return true;
579 }
580
581 /*
582 * Parse the given arguments. Called from main() and from
583 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
584 *
585 * The arguments must be treated as read-only and will be freed after the
586 * call.
587 *
588 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
589 */
590 static void
591 MainParseArgs(int argc, char **argv)
592 {
593 char c;
594 int arginc;
595 char *argvalue;
596 char *optscan;
597 bool inOption, dashDash = false;
598
599 const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
600 /* Can't actually use getopt(3) because rescanning is not portable */
601
602 rearg:
603 inOption = false;
604 optscan = NULL;
605 while (argc > 1) {
606 const char *optspec;
607 if (!inOption)
608 optscan = argv[1];
609 c = *optscan++;
610 arginc = 0;
611 if (inOption) {
612 if (c == '\0') {
613 argv++;
614 argc--;
615 inOption = false;
616 continue;
617 }
618 } else {
619 if (c != '-' || dashDash)
620 break;
621 inOption = true;
622 c = *optscan++;
623 }
624 /* '-' found at some earlier point */
625 optspec = strchr(optspecs, c);
626 if (c != '\0' && optspec != NULL && optspec[1] == ':') {
627 /*
628 * -<something> found, and <something> should have an
629 * argument
630 */
631 inOption = false;
632 arginc = 1;
633 argvalue = optscan;
634 if (*argvalue == '\0') {
635 if (argc < 3)
636 goto noarg;
637 argvalue = argv[2];
638 arginc = 2;
639 }
640 } else {
641 argvalue = NULL;
642 }
643 switch (c) {
644 case '\0':
645 arginc = 1;
646 inOption = false;
647 break;
648 case '-':
649 dashDash = true;
650 break;
651 default:
652 if (!MainParseOption(c, argvalue))
653 goto noarg;
654 }
655 argv += arginc;
656 argc -= arginc;
657 }
658
659 /*
660 * See if the rest of the arguments are variable assignments and
661 * perform them if so. Else take them to be targets and stuff them
662 * on the end of the "create" list.
663 */
664 for (; argc > 1; argv++, argc--) {
665 if (!Parse_VarAssign(argv[1], false, SCOPE_CMDLINE)) {
666 if (argv[1][0] == '\0')
667 Punt("illegal (null) argument.");
668 if (argv[1][0] == '-' && !dashDash)
669 goto rearg;
670 Lst_Append(&opts.create, bmake_strdup(argv[1]));
671 }
672 }
673
674 return;
675 noarg:
676 (void)fprintf(stderr, "%s: option requires an argument -- %c\n",
677 progname, c);
678 usage();
679 }
680
681 /*
682 * Break a line of arguments into words and parse them.
683 *
684 * Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
685 * by main() when reading the MAKEFLAGS environment variable.
686 */
687 void
688 Main_ParseArgLine(const char *line)
689 {
690 Words words;
691 char *buf;
692 const char *p;
693
694 if (line == NULL)
695 return;
696 for (p = line; *p == ' '; p++)
697 continue;
698 if (p[0] == '\0')
699 return;
700
701 {
702 FStr argv0 = Var_Value(SCOPE_GLOBAL, ".MAKE");
703 buf = str_concat3(argv0.str, " ", p);
704 FStr_Done(&argv0);
705 }
706
707 words = Str_Words(buf, true);
708 if (words.words == NULL) {
709 Error("Unterminated quoted string [%s]", buf);
710 free(buf);
711 return;
712 }
713 free(buf);
714 EvalStack_PushMakeflags(line);
715 MainParseArgs((int)words.len, words.words);
716 EvalStack_Pop();
717
718 Words_Free(words);
719 }
720
721 bool
722 Main_SetObjdir(bool writable, const char *fmt, ...)
723 {
724 struct stat sb;
725 char *path;
726 char buf[MAXPATHLEN + 1];
727 char buf2[MAXPATHLEN + 1];
728 va_list ap;
729
730 va_start(ap, fmt);
731 vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
732 va_end(ap);
733
734 if (path[0] != '/') {
735 if (snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path) <= MAXPATHLEN)
736 path = buf2;
737 else
738 return false;
739 }
740
741 /* look for the directory and try to chdir there */
742 if (stat(path, &sb) != 0 || !S_ISDIR(sb.st_mode))
743 return false;
744
745 if ((writable && access(path, W_OK) != 0) || chdir(path) != 0) {
746 (void)fprintf(stderr, "%s: warning: %s: %s\n",
747 progname, path, strerror(errno));
748 /* Allow debugging how we got here - not always obvious */
749 if (GetBooleanExpr("${MAKE_DEBUG_OBJDIR_CHECK_WRITABLE}",
750 false))
751 PrintOnError(NULL, "");
752 return false;
753 }
754
755 snprintf(objdir, sizeof objdir, "%s", path);
756 Global_Set(".OBJDIR", objdir);
757 setenv("PWD", objdir, 1);
758 Dir_InitDot();
759 purge_relative_cached_realpaths();
760 if (opts.enterFlag && strcmp(objdir, curdir) != 0)
761 enterFlagObj = true;
762 return true;
763 }
764
765 static bool
766 SetVarObjdir(bool writable, const char *var, const char *suffix)
767 {
768 FStr path = Var_Value(SCOPE_CMDLINE, var);
769
770 if (path.str == NULL || path.str[0] == '\0') {
771 FStr_Done(&path);
772 return false;
773 }
774
775 Var_Expand(&path, SCOPE_GLOBAL, VARE_EVAL);
776
777 (void)Main_SetObjdir(writable, "%s%s", path.str, suffix);
778
779 FStr_Done(&path);
780 return true;
781 }
782
783 /*
784 * Splits str into words (in-place, modifying it), adding them to the list.
785 * The string must be kept alive as long as the list.
786 */
787 void
788 AppendWords(StringList *lp, char *str)
789 {
790 char *p;
791 const char *sep = " \t";
792
793 for (p = strtok(str, sep); p != NULL; p = strtok(NULL, sep))
794 Lst_Append(lp, p);
795 }
796
797 #ifdef SIGINFO
798 static void
799 siginfo(int signo MAKE_ATTR_UNUSED)
800 {
801 char dir[MAXPATHLEN];
802 char str[2 * MAXPATHLEN];
803 int len;
804 if (getcwd(dir, sizeof dir) == NULL)
805 return;
806 len = snprintf(str, sizeof str, "%s: Working in: %s\n", progname, dir);
807 if (len > 0)
808 (void)write(STDERR_FILENO, str, (size_t)len);
809 }
810 #endif
811
812 /* Allow makefiles some control over the mode we run in. */
813 static void
814 MakeMode(void)
815 {
816 char *mode = Var_Subst("${.MAKE.MODE:tl}", SCOPE_GLOBAL, VARE_EVAL);
817 /* TODO: handle errors */
818
819 if (mode[0] != '\0') {
820 if (strstr(mode, "compat") != NULL) {
821 opts.compatMake = true;
822 forceJobs = false;
823 }
824 #if USE_META
825 if (strstr(mode, "meta") != NULL)
826 meta_mode_init(mode);
827 #endif
828 if (strstr(mode, "randomize-targets") != NULL)
829 opts.randomizeTargets = true;
830 }
831
832 free(mode);
833 }
834
835 static void
836 PrintVariable(const char *varname, bool expandVars)
837 {
838 if (strchr(varname, '$') != NULL) {
839 char *evalue = Var_Subst(varname, SCOPE_GLOBAL, VARE_EVAL);
840 /* TODO: handle errors */
841 printf("%s\n", evalue);
842 free(evalue);
843
844 } else if (expandVars) {
845 char *expr = str_concat3("${", varname, "}");
846 char *evalue = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
847 /* TODO: handle errors */
848 free(expr);
849 printf("%s\n", evalue);
850 free(evalue);
851
852 } else {
853 FStr value = Var_Value(SCOPE_GLOBAL, varname);
854 printf("%s\n", value.str != NULL ? value.str : "");
855 FStr_Done(&value);
856 }
857 }
858
859 /*
860 * Return a bool based on a variable.
861 *
862 * If the knob is not set, return the fallback.
863 * If set, anything that looks or smells like "No", "False", "Off", "0", etc.
864 * is false, otherwise true.
865 */
866 bool
867 GetBooleanExpr(const char *expr, bool fallback)
868 {
869 char *value;
870 bool res;
871
872 value = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
873 /* TODO: handle errors */
874 res = ParseBoolean(value, fallback);
875 free(value);
876 return res;
877 }
878
879 static void
880 PrintVariables(void)
881 {
882 StringListNode *ln;
883 bool expandVars;
884
885 if (opts.printVars == PVM_EXPANDED)
886 expandVars = true;
887 else if (opts.debugVflag)
888 expandVars = false;
889 else
890 expandVars = GetBooleanExpr("${.MAKE.EXPAND_VARIABLES}",
891 false);
892
893 for (ln = opts.variables.first; ln != NULL; ln = ln->next)
894 PrintVariable(ln->datum, expandVars);
895 }
896
897 static bool
898 MakeTargets(void)
899 {
900 GNodeList targets = LST_INIT;
901 bool outOfDate; /* false if all targets up to date */
902
903 if (Lst_IsEmpty(&opts.create))
904 Parse_MainName(&targets);
905 else
906 Targ_FindList(&targets, &opts.create);
907
908 if (!opts.compatMake) {
909 if (!opts.query) {
910 Job_Init();
911 jobsRunning = true;
912 }
913
914 outOfDate = Make_MakeParallel(&targets);
915 } else {
916 Compat_MakeAll(&targets);
917 outOfDate = false;
918 }
919 Lst_Done(&targets); /* Don't free the targets themselves. */
920 return outOfDate;
921 }
922
923 /*
924 * Set up the .TARGETS variable to contain the list of targets to be created.
925 * If none specified, make the variable empty for now, the parser will fill
926 * in the default or .MAIN target later.
927 */
928 static void
929 InitVarTargets(void)
930 {
931 StringListNode *ln;
932
933 if (Lst_IsEmpty(&opts.create)) {
934 Global_Set(".TARGETS", "");
935 return;
936 }
937
938 for (ln = opts.create.first; ln != NULL; ln = ln->next) {
939 const char *name = ln->datum;
940 Global_Append(".TARGETS", name);
941 }
942 }
943
944 static void
945 InitRandom(void)
946 {
947 struct timeval tv;
948
949 gettimeofday(&tv, NULL);
950 srandom((unsigned)(tv.tv_sec + tv.tv_usec));
951 }
952
953 static const char *
954 InitVarMachine(const struct utsname *utsname MAKE_ATTR_UNUSED)
955 {
956 const char *machine = getenv("MACHINE");
957 if (machine != NULL)
958 return machine;
959
960 #if defined(MAKE_NATIVE)
961 return utsname->machine;
962 #elif defined(MAKE_MACHINE)
963 return MAKE_MACHINE;
964 #else
965 return "unknown";
966 #endif
967 }
968
969 static const char *
970 InitVarMachineArch(void)
971 {
972 const char *env = getenv("MACHINE_ARCH");
973 if (env != NULL)
974 return env;
975
976 #ifdef MAKE_NATIVE
977 {
978 struct utsname utsname;
979 static char machine_arch_buf[sizeof utsname.machine];
980 const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
981 size_t len = sizeof machine_arch_buf;
982
983 if (sysctl(mib, (unsigned)__arraycount(mib),
984 machine_arch_buf, &len, NULL, 0) < 0) {
985 (void)fprintf(stderr, "%s: sysctl: %s\n",
986 progname, strerror(errno));
987 exit(2);
988 }
989
990 return machine_arch_buf;
991 }
992 #elif defined(MACHINE_ARCH)
993 return MACHINE_ARCH;
994 #elif defined(MAKE_MACHINE_ARCH)
995 return MAKE_MACHINE_ARCH;
996 #else
997 return "unknown";
998 #endif
999 }
1000
1001 #ifndef NO_PWD_OVERRIDE
1002 /*
1003 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
1004 * since the value of curdir can vary depending on how we got
1005 * here. That is, sitting at a shell prompt (shell that provides $PWD)
1006 * or via subdir.mk, in which case it's likely a shell which does
1007 * not provide it.
1008 *
1009 * So, to stop it breaking this case only, we ignore PWD if
1010 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains an expression.
1011 */
1012 static void
1013 HandlePWD(const struct stat *curdir_st)
1014 {
1015 char *pwd;
1016 FStr makeobjdir;
1017 struct stat pwd_st;
1018
1019 if (ignorePWD || (pwd = getenv("PWD")) == NULL)
1020 return;
1021
1022 if (Var_Exists(SCOPE_CMDLINE, "MAKEOBJDIRPREFIX"))
1023 return;
1024
1025 makeobjdir = Var_Value(SCOPE_CMDLINE, "MAKEOBJDIR");
1026 if (makeobjdir.str != NULL && strchr(makeobjdir.str, '$') != NULL)
1027 goto ignore_pwd;
1028
1029 if (stat(pwd, &pwd_st) == 0 &&
1030 curdir_st->st_ino == pwd_st.st_ino &&
1031 curdir_st->st_dev == pwd_st.st_dev)
1032 snprintf(curdir, MAXPATHLEN, "%s", pwd);
1033
1034 ignore_pwd:
1035 FStr_Done(&makeobjdir);
1036 }
1037 #endif
1038
1039 /*
1040 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, MAKEOBJDIR is set
1041 * in the environment, try only that value and fall back to .CURDIR if it
1042 * does not exist.
1043 *
1044 * Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
1045 * and finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none of these
1046 * paths exist, just use .CURDIR.
1047 */
1048 static void
1049 InitObjdir(const char *machine, const char *machine_arch)
1050 {
1051 bool writable;
1052
1053 Dir_InitCur(curdir);
1054 writable = GetBooleanExpr("${MAKE_OBJDIR_CHECK_WRITABLE}", true);
1055 (void)Main_SetObjdir(false, "%s", curdir);
1056
1057 if (!SetVarObjdir(writable, "MAKEOBJDIRPREFIX", curdir) &&
1058 !SetVarObjdir(writable, "MAKEOBJDIR", "") &&
1059 !Main_SetObjdir(writable, "%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
1060 !Main_SetObjdir(writable, "%s.%s", _PATH_OBJDIR, machine) &&
1061 !Main_SetObjdir(writable, "%s", _PATH_OBJDIR))
1062 (void)Main_SetObjdir(writable, "%s%s", _PATH_OBJDIRPREFIX, curdir);
1063 }
1064
1065 /* get rid of resource limit on file descriptors */
1066 static void
1067 UnlimitFiles(void)
1068 {
1069 #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
1070 struct rlimit rl;
1071 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
1072 rl.rlim_cur != rl.rlim_max) {
1073 rl.rlim_cur = rl.rlim_max;
1074 (void)setrlimit(RLIMIT_NOFILE, &rl);
1075 }
1076 #endif
1077 }
1078
1079 static void
1080 CmdOpts_Init(void)
1081 {
1082 opts.compatMake = false;
1083 memset(&opts.debug, 0, sizeof(opts.debug));
1084 /* opts.debug_file has already been initialized earlier */
1085 opts.strict = false;
1086 opts.debugVflag = false;
1087 opts.checkEnvFirst = false;
1088 Lst_Init(&opts.makefiles);
1089 opts.ignoreErrors = false; /* Pay attention to non-zero returns */
1090 opts.maxJobs = 1;
1091 opts.keepgoing = false; /* Stop on error */
1092 opts.noRecursiveExecute = false; /* Execute all .MAKE targets */
1093 opts.noExecute = false; /* Execute all commands */
1094 opts.query = false;
1095 opts.noBuiltins = false; /* Read the built-in rules */
1096 opts.silent = false; /* Print commands as executed */
1097 opts.touch = false;
1098 opts.printVars = PVM_NONE;
1099 Lst_Init(&opts.variables);
1100 opts.parseWarnFatal = false;
1101 opts.enterFlag = false;
1102 opts.varNoExportEnv = false;
1103 Lst_Init(&opts.create);
1104 }
1105
1106 /*
1107 * Initialize MAKE and .MAKE to the path of the executable, so that it can be
1108 * found by execvp(3) and the shells, even after a chdir.
1109 *
1110 * If it's a relative path and contains a '/', resolve it to an absolute path.
1111 * Otherwise keep it as is, assuming it will be found in the PATH.
1112 */
1113 static void
1114 InitVarMake(const char *argv0)
1115 {
1116 const char *make = argv0;
1117 char pathbuf[MAXPATHLEN];
1118
1119 if (argv0[0] != '/' && strchr(argv0, '/') != NULL) {
1120 const char *abspath = cached_realpath(argv0, pathbuf);
1121 struct stat st;
1122 if (abspath != NULL && abspath[0] == '/' &&
1123 stat(make, &st) == 0)
1124 make = abspath;
1125 }
1126
1127 Global_Set("MAKE", make);
1128 Global_Set(".MAKE", make);
1129 }
1130
1131 /*
1132 * Add the directories from the colon-separated syspath to defSysIncPath.
1133 * After returning, the contents of syspath is unspecified.
1134 */
1135 static void
1136 InitDefSysIncPath(char *syspath)
1137 {
1138 static char defsyspath[] = _PATH_DEFSYSPATH;
1139 char *start, *p;
1140
1141 /*
1142 * If no user-supplied system path was given (through the -m option)
1143 * add the directories from the DEFSYSPATH (more than one may be given
1144 * as dir1:...:dirn) to the system include path.
1145 */
1146 if (syspath == NULL || syspath[0] == '\0')
1147 syspath = defsyspath;
1148 else
1149 syspath = bmake_strdup(syspath);
1150
1151 for (start = syspath; *start != '\0'; start = p) {
1152 for (p = start; *p != '\0' && *p != ':'; p++)
1153 continue;
1154 if (*p == ':')
1155 *p++ = '\0';
1156
1157 /* look for magic parent directory search string */
1158 if (strncmp(start, ".../", 4) == 0) {
1159 char *dir = Dir_FindHereOrAbove(curdir, start + 4);
1160 if (dir != NULL) {
1161 (void)SearchPath_Add(defSysIncPath, dir);
1162 free(dir);
1163 }
1164 } else {
1165 (void)SearchPath_Add(defSysIncPath, start);
1166 }
1167 }
1168
1169 if (syspath != defsyspath)
1170 free(syspath);
1171 }
1172
1173 static void
1174 ReadBuiltinRules(void)
1175 {
1176 StringListNode *ln;
1177 StringList sysMkFiles = LST_INIT;
1178
1179 SearchPath_Expand(
1180 Lst_IsEmpty(&sysIncPath->dirs) ? defSysIncPath : sysIncPath,
1181 _PATH_DEFSYSMK,
1182 &sysMkFiles);
1183 if (Lst_IsEmpty(&sysMkFiles))
1184 Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK);
1185
1186 for (ln = sysMkFiles.first; ln != NULL; ln = ln->next)
1187 if (ReadMakefile(ln->datum))
1188 break;
1189
1190 if (ln == NULL)
1191 Fatal("%s: cannot open %s.",
1192 progname, (const char *)sysMkFiles.first->datum);
1193
1194 Lst_DoneFree(&sysMkFiles);
1195 }
1196
1197 static void
1198 InitMaxJobs(void)
1199 {
1200 char *value;
1201 int n;
1202
1203 if (bogusJflag && !opts.compatMake) {
1204 opts.compatMake = true;
1205 Parse_Error(PARSE_WARNING,
1206 "internal option \"-J\" in \"%s\" "
1207 "refers to unopened file descriptors; "
1208 "falling back to compat mode.\n"
1209 "\t"
1210 "To run the target even in -n mode, "
1211 "add the .MAKE pseudo-source to the target.\n"
1212 "\t"
1213 "To run the target in default mode only, "
1214 "add a ${:D make} marker to a target's command. "
1215 "(This marker expression expands to an empty string.)\n"
1216 "\t"
1217 "To make the sub-make run in compat mode, add -B to "
1218 "its invocation."
1219 "To make the sub-make independent from the parent make, "
1220 "unset the MAKEFLAGS environment variable in the "
1221 "target's commands.",
1222 curdir);
1223 PrintStackTrace(true);
1224 return;
1225 }
1226 if (forceJobs || opts.compatMake ||
1227 !Var_Exists(SCOPE_GLOBAL, ".MAKE.JOBS"))
1228 return;
1229
1230 value = Var_Subst("${.MAKE.JOBS}", SCOPE_GLOBAL, VARE_EVAL);
1231 /* TODO: handle errors */
1232 n = (int)strtol(value, NULL, 0);
1233 if (n < 1) {
1234 (void)fprintf(stderr,
1235 "%s: illegal value for .MAKE.JOBS "
1236 "-- must be positive integer!\n",
1237 progname);
1238 exit(2); /* Not 1 so -q can distinguish error */
1239 }
1240
1241 if (n != opts.maxJobs) {
1242 Global_Append(MAKEFLAGS, "-j");
1243 Global_Append(MAKEFLAGS, value);
1244 }
1245
1246 opts.maxJobs = n;
1247 maxJobTokens = opts.maxJobs;
1248 forceJobs = true;
1249 free(value);
1250 }
1251
1252 /*
1253 * For compatibility, look at the directories in the VPATH variable
1254 * and add them to the search path, if the variable is defined. The
1255 * variable's value is in the same format as the PATH environment
1256 * variable, i.e. <directory>:<directory>:<directory>...
1257 */
1258 static void
1259 InitVpath(void)
1260 {
1261 char *vpath, savec, *path;
1262 if (!Var_Exists(SCOPE_CMDLINE, "VPATH"))
1263 return;
1264
1265 vpath = Var_Subst("${VPATH}", SCOPE_CMDLINE, VARE_EVAL);
1266 /* TODO: handle errors */
1267 path = vpath;
1268 do {
1269 char *p;
1270 /* skip to end of directory */
1271 for (p = path; *p != ':' && *p != '\0'; p++)
1272 continue;
1273 /* Save terminator character so know when to stop */
1274 savec = *p;
1275 *p = '\0';
1276 /* Add directory to search path */
1277 (void)SearchPath_Add(&dirSearchPath, path);
1278 *p = savec;
1279 path = p + 1;
1280 } while (savec == ':');
1281 free(vpath);
1282 }
1283
1284 static void
1285 ReadAllMakefiles(const StringList *makefiles)
1286 {
1287 StringListNode *ln;
1288
1289 for (ln = makefiles->first; ln != NULL; ln = ln->next) {
1290 const char *fname = ln->datum;
1291 if (!ReadMakefile(fname))
1292 Fatal("%s: cannot open %s.", progname, fname);
1293 }
1294 }
1295
1296 static void
1297 ReadFirstDefaultMakefile(void)
1298 {
1299 StringList makefiles = LST_INIT;
1300 StringListNode *ln;
1301 char *prefs = Var_Subst("${.MAKE.MAKEFILE_PREFERENCE}",
1302 SCOPE_CMDLINE, VARE_EVAL);
1303 /* TODO: handle errors */
1304
1305 AppendWords(&makefiles, prefs);
1306
1307 for (ln = makefiles.first; ln != NULL; ln = ln->next)
1308 if (ReadMakefile(ln->datum))
1309 break;
1310
1311 Lst_Done(&makefiles);
1312 free(prefs);
1313 }
1314
1315 /*
1316 * Initialize variables such as MAKE, MACHINE, .MAKEFLAGS.
1317 * Initialize a few modules.
1318 * Parse the arguments from MAKEFLAGS and the command line.
1319 */
1320 static void
1321 main_Init(int argc, char **argv)
1322 {
1323 struct stat sa;
1324 const char *machine;
1325 const char *machine_arch;
1326 char *syspath = getenv("MAKESYSPATH");
1327 struct utsname utsname;
1328
1329 /* default to writing debug to stderr */
1330 opts.debug_file = stderr;
1331
1332 Str_Intern_Init();
1333 HashTable_Init(&cached_realpaths);
1334
1335 #ifdef SIGINFO
1336 (void)bmake_signal(SIGINFO, siginfo);
1337 #endif
1338
1339 InitRandom();
1340
1341 progname = str_basename(argv[0]);
1342
1343 UnlimitFiles();
1344
1345 if (uname(&utsname) == -1) {
1346 (void)fprintf(stderr, "%s: uname: %s\n", progname,
1347 strerror(errno));
1348 exit(2);
1349 }
1350
1351 machine = InitVarMachine(&utsname);
1352 machine_arch = InitVarMachineArch();
1353
1354 myPid = getpid();
1355
1356 /* Just in case MAKEOBJDIR wants us to do something tricky. */
1357 Targ_Init();
1358 Global_Set_ReadOnly(".MAKE.OS", utsname.sysname);
1359 Global_Set("MACHINE", machine);
1360 Global_Set("MACHINE_ARCH", machine_arch);
1361 #ifdef MAKE_VERSION
1362 Global_Set("MAKE_VERSION", MAKE_VERSION);
1363 #endif
1364 Global_Set_ReadOnly(".newline", "\n");
1365 #ifndef MAKEFILE_PREFERENCE_LIST
1366 /* This is the traditional preference for makefiles. */
1367 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
1368 #endif
1369 Global_Set(".MAKE.MAKEFILE_PREFERENCE", MAKEFILE_PREFERENCE_LIST);
1370 Global_Set(".MAKE.DEPENDFILE", ".depend");
1371 /* Tell makefiles like jobs.mk whether we support -jC */
1372 #ifdef _SC_NPROCESSORS_ONLN
1373 Global_Set_ReadOnly(".MAKE.JOBS.C", "yes");
1374 #else
1375 Global_Set_ReadOnly(".MAKE.JOBS.C", "no");
1376 #endif
1377
1378 CmdOpts_Init();
1379 allPrecious = false; /* Remove targets when interrupted */
1380 deleteOnError = false; /* Historical default behavior */
1381 jobsRunning = false;
1382
1383 maxJobTokens = opts.maxJobs;
1384 ignorePWD = false;
1385
1386 /*
1387 * Initialize the parsing, directory and variable modules to prepare
1388 * for the reading of inclusion paths and variable settings on the
1389 * command line
1390 */
1391
1392 /*
1393 * Initialize various variables.
1394 * MAKE also gets this name, for compatibility
1395 * .MAKEFLAGS gets set to the empty string just in case.
1396 * MFLAGS also gets initialized empty, for compatibility.
1397 */
1398 Parse_Init();
1399 InitVarMake(argv[0]);
1400 Global_Set(MAKEFLAGS, "");
1401 Global_Set(".MAKEOVERRIDES", "");
1402 Global_Set("MFLAGS", "");
1403 Global_Set(".ALLTARGETS", "");
1404 Global_Set_ReadOnly(".MAKE.LEVEL.ENV", MAKE_LEVEL_ENV);
1405
1406 /* Set some other useful variables. */
1407 {
1408 char buf[64];
1409 const char *ep = getenv(MAKE_LEVEL_ENV);
1410
1411 makelevel = ep != NULL && ep[0] != '\0' ? atoi(ep) : 0;
1412 if (makelevel < 0)
1413 makelevel = 0;
1414 snprintf(buf, sizeof buf, "%d", makelevel);
1415 Global_Set(".MAKE.LEVEL", buf);
1416 snprintf(buf, sizeof buf, "%u", myPid);
1417 Global_Set_ReadOnly(".MAKE.PID", buf);
1418 snprintf(buf, sizeof buf, "%u", getppid());
1419 Global_Set_ReadOnly(".MAKE.PPID", buf);
1420 snprintf(buf, sizeof buf, "%u", getuid());
1421 Global_Set_ReadOnly(".MAKE.UID", buf);
1422 snprintf(buf, sizeof buf, "%u", getgid());
1423 Global_Set_ReadOnly(".MAKE.GID", buf);
1424 }
1425 if (makelevel > 0) {
1426 char pn[1024];
1427 snprintf(pn, sizeof pn, "%s[%d]", progname, makelevel);
1428 progname = bmake_strdup(pn);
1429 }
1430
1431 #ifdef USE_META
1432 meta_init();
1433 #endif
1434 Dir_Init();
1435
1436 if (getcwd(curdir, MAXPATHLEN) == NULL) {
1437 (void)fprintf(stderr, "%s: getcwd: %s\n",
1438 progname, strerror(errno));
1439 exit(2);
1440 }
1441
1442 {
1443 char *makeflags = explode(getenv("MAKEFLAGS"));
1444 Main_ParseArgLine(makeflags);
1445 free(makeflags);
1446 }
1447
1448 MainParseArgs(argc, argv);
1449
1450 if (opts.enterFlag)
1451 printf("%s: Entering directory `%s'\n", progname, curdir);
1452
1453 if (stat(curdir, &sa) == -1) {
1454 (void)fprintf(stderr, "%s: stat %s: %s\n",
1455 progname, curdir, strerror(errno));
1456 exit(2);
1457 }
1458
1459 #ifndef NO_PWD_OVERRIDE
1460 HandlePWD(&sa);
1461 #endif
1462 Global_Set(".CURDIR", curdir);
1463
1464 InitObjdir(machine, machine_arch);
1465
1466 Arch_Init();
1467 Suff_Init();
1468 Trace_Init(tracefile);
1469
1470 defaultNode = NULL;
1471 (void)time(&now);
1472
1473 Trace_Log(MAKESTART, NULL);
1474
1475 InitVarTargets();
1476
1477 InitDefSysIncPath(syspath);
1478 }
1479
1480 /*
1481 * Read the system makefile followed by either makefile, Makefile or the
1482 * files given by the -f option. Exit on parse errors.
1483 */
1484 static void
1485 main_ReadFiles(void)
1486 {
1487
1488 if (Lst_IsEmpty(&sysIncPath->dirs))
1489 SearchPath_AddAll(sysIncPath, defSysIncPath);
1490
1491 Dir_SetSYSPATH();
1492 if (!opts.noBuiltins)
1493 ReadBuiltinRules();
1494
1495 posix_state = PS_MAYBE_NEXT_LINE;
1496 if (!Lst_IsEmpty(&opts.makefiles))
1497 ReadAllMakefiles(&opts.makefiles);
1498 else
1499 ReadFirstDefaultMakefile();
1500 }
1501
1502 /* Compute the dependency graph. */
1503 static void
1504 main_PrepareMaking(void)
1505 {
1506 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
1507 if (!opts.noBuiltins || opts.printVars == PVM_NONE) {
1508 makeDependfile = Var_Subst("${.MAKE.DEPENDFILE}",
1509 SCOPE_CMDLINE, VARE_EVAL);
1510 if (makeDependfile[0] != '\0') {
1511 /* TODO: handle errors */
1512 doing_depend = true;
1513 (void)ReadMakefile(makeDependfile);
1514 doing_depend = false;
1515 }
1516 }
1517
1518 if (enterFlagObj)
1519 printf("%s: Entering directory `%s'\n", progname, objdir);
1520
1521 MakeMode();
1522
1523 {
1524 FStr makeflags = Var_Value(SCOPE_GLOBAL, MAKEFLAGS);
1525 Global_Append("MFLAGS", makeflags.str);
1526 FStr_Done(&makeflags);
1527 }
1528
1529 InitMaxJobs();
1530
1531 if (!opts.compatMake && !forceJobs)
1532 opts.compatMake = true;
1533
1534 if (!opts.compatMake)
1535 TokenPool_Init(maxJobTokens, tokenPoolReader, tokenPoolWriter);
1536 DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1537 tokenPoolReader, tokenPoolWriter, opts.maxJobs, maxJobTokens,
1538 opts.compatMake ? 1 : 0);
1539
1540 if (opts.printVars == PVM_NONE)
1541 Main_ExportMAKEFLAGS(true); /* initial export */
1542
1543 InitVpath();
1544
1545 /*
1546 * Now that all search paths have been read for suffixes et al, it's
1547 * time to add the default search path to their lists...
1548 */
1549 Suff_ExtendPaths();
1550
1551 /*
1552 * Propagate attributes through :: dependency lists.
1553 */
1554 Targ_Propagate();
1555
1556 /* print the initial graph, if the user requested it */
1557 if (DEBUG(GRAPH1))
1558 Targ_PrintGraph(1);
1559 }
1560
1561 /*
1562 * Make the targets, or print variables.
1563 * Return whether any of the targets is out-of-date.
1564 */
1565 static bool
1566 main_Run(void)
1567 {
1568 if (opts.printVars != PVM_NONE) {
1569 PrintVariables();
1570 return false;
1571 } else
1572 return MakeTargets();
1573 }
1574
1575 /* Clean up after making the targets. */
1576 static void
1577 main_CleanUp(void)
1578 {
1579 #ifdef CLEANUP
1580 Lst_DoneFree(&opts.variables);
1581 Lst_DoneFree(&opts.makefiles);
1582 Lst_DoneFree(&opts.create);
1583 #endif
1584
1585 if (DEBUG(GRAPH2))
1586 Targ_PrintGraph(2);
1587
1588 Trace_Log(MAKEEND, NULL);
1589
1590 if (enterFlagObj)
1591 printf("%s: Leaving directory `%s'\n", progname, objdir);
1592 if (opts.enterFlag)
1593 printf("%s: Leaving directory `%s'\n", progname, curdir);
1594
1595 Var_Stats();
1596 Targ_Stats();
1597
1598 #ifdef USE_META
1599 meta_finish();
1600 #endif
1601 #ifdef CLEANUP
1602 Suff_End();
1603 Targ_End();
1604 Arch_End();
1605 Parse_End();
1606 Dir_End();
1607 Job_End();
1608 #endif
1609 Trace_End();
1610 #ifdef CLEANUP
1611 Str_Intern_End();
1612 #endif
1613 }
1614
1615 static int
1616 main_ExitCode(bool outOfDate)
1617 {
1618 if ((opts.strict && main_errors > 0) || parseErrors > 0)
1619 return 2; /* Not 1 so -q can distinguish error */
1620 return outOfDate ? 1 : 0;
1621 }
1622
1623 int
1624 main(int argc, char **argv)
1625 {
1626 bool outOfDate;
1627
1628 main_Init(argc, argv);
1629 main_ReadFiles();
1630 main_PrepareMaking();
1631 outOfDate = main_Run();
1632 main_CleanUp();
1633 return main_ExitCode(outOfDate);
1634 }
1635
1636 /*
1637 * Open and parse the given makefile, with all its side effects.
1638 * Return false if the file could not be opened.
1639 */
1640 static bool
1641 ReadMakefile(const char *fname)
1642 {
1643 int fd;
1644 char *name, *path = NULL;
1645
1646 if (strcmp(fname, "-") == 0) {
1647 Parse_File("(stdin)", -1);
1648 Var_Set(SCOPE_INTERNAL, "MAKEFILE", "");
1649 } else {
1650 if (strncmp(fname, ".../", 4) == 0) {
1651 name = Dir_FindHereOrAbove(curdir, fname + 4);
1652 if (name != NULL) {
1653 /* Dir_FindHereOrAbove returns dirname */
1654 path = str_concat3(name, "/",
1655 str_basename(fname));
1656 free(name);
1657 fd = open(path, O_RDONLY);
1658 if (fd != -1) {
1659 fname = path;
1660 goto found;
1661 }
1662 }
1663 }
1664 /* if we've chdir'd, rebuild the path name */
1665 if (strcmp(curdir, objdir) != 0 && *fname != '/') {
1666 path = str_concat3(curdir, "/", fname);
1667 fd = open(path, O_RDONLY);
1668 if (fd != -1) {
1669 fname = path;
1670 goto found;
1671 }
1672 free(path);
1673
1674 /* If curdir failed, try objdir (ala .depend) */
1675 path = str_concat3(objdir, "/", fname);
1676 fd = open(path, O_RDONLY);
1677 if (fd != -1) {
1678 fname = path;
1679 goto found;
1680 }
1681 } else {
1682 fd = open(fname, O_RDONLY);
1683 if (fd != -1)
1684 goto found;
1685 }
1686 /* look in -I and system include directories. */
1687 name = Dir_FindFile(fname, parseIncPath);
1688 if (name == NULL) {
1689 SearchPath *sysInc = Lst_IsEmpty(&sysIncPath->dirs)
1690 ? defSysIncPath : sysIncPath;
1691 name = Dir_FindFile(fname, sysInc);
1692 }
1693 if (name == NULL || (fd = open(name, O_RDONLY)) == -1) {
1694 free(name);
1695 free(path);
1696 return false;
1697 }
1698 fname = name;
1699 /*
1700 * set the MAKEFILE variable desired by System V fans -- the
1701 * placement of the setting here means it gets set to the last
1702 * makefile specified, as it is set by SysV make.
1703 */
1704 found:
1705 if (!doing_depend)
1706 Var_Set(SCOPE_INTERNAL, "MAKEFILE", fname);
1707 Parse_File(fname, fd);
1708 }
1709 free(path);
1710 return true;
1711 }
1712
1713 /* populate av for Cmd_Exec and Compat_RunCommand */
1714 int
1715 Cmd_Argv(const char *cmd, size_t cmd_len, const char **av, size_t avsz,
1716 char *cmd_file, size_t cmd_filesz, bool eflag, bool xflag)
1717 {
1718 int ac = 0;
1719 int cmd_fd = -1;
1720
1721 if (shellPath == NULL)
1722 Shell_Init();
1723
1724 if (cmd_file != NULL) {
1725 if (cmd_len == 0)
1726 cmd_len = strlen(cmd);
1727
1728 if (cmd_len > MAKE_CMDLEN_LIMIT) {
1729 cmd_fd = mkTempFile(NULL, cmd_file, cmd_filesz);
1730 if (cmd_fd >= 0) {
1731 ssize_t n;
1732
1733 n = write(cmd_fd, cmd, cmd_len);
1734 close(cmd_fd);
1735 if (n < (ssize_t)cmd_len) {
1736 unlink(cmd_file);
1737 cmd_fd = -1;
1738 }
1739 }
1740 } else
1741 cmd_file[0] = '\0';
1742 }
1743
1744 if (avsz < 4 || (eflag && avsz < 5))
1745 return -1;
1746
1747 /* The following works for any of the builtin shell specs. */
1748 av[ac++] = shellPath;
1749 if (eflag)
1750 av[ac++] = shellErrFlag;
1751 if (cmd_fd >= 0) {
1752 if (xflag)
1753 av[ac++] = "-x";
1754 av[ac++] = cmd_file;
1755 } else {
1756 av[ac++] = xflag ? "-xc" : "-c";
1757 av[ac++] = cmd;
1758 }
1759 av[ac] = NULL;
1760 return ac;
1761 }
1762
1763 /*
1764 * Execute the command in cmd, and return its output (only stdout, not
1765 * stderr, possibly empty). In the output, replace newlines with spaces.
1766 */
1767 char *
1768 Cmd_Exec(const char *cmd, char **error)
1769 {
1770 const char *args[4]; /* Arguments for invoking the shell */
1771 int pipefds[2];
1772 int cpid; /* Child PID */
1773 int pid; /* PID from wait() */
1774 int status; /* command exit status */
1775 Buffer buf; /* buffer to store the result */
1776 ssize_t bytes_read;
1777 char *output;
1778 char *p;
1779 int saved_errno;
1780 char cmd_file[MAXPATHLEN];
1781
1782 DEBUG1(VAR, "Capturing the output of command \"%s\"\n", cmd);
1783
1784 if (Cmd_Argv(cmd, 0, args, 4, cmd_file, sizeof(cmd_file), false, false) < 0
1785 || pipe(pipefds) == -1) {
1786 *error = str_concat3(
1787 "Couldn't create pipe for \"", cmd, "\"");
1788 return bmake_strdup("");
1789 }
1790
1791 Var_ReexportVars(SCOPE_GLOBAL);
1792 Var_ExportStackTrace(NULL, cmd);
1793
1794 switch (cpid = FORK_FUNCTION()) {
1795 case 0:
1796 (void)close(pipefds[0]);
1797 (void)dup2(pipefds[1], STDOUT_FILENO);
1798 (void)close(pipefds[1]);
1799
1800 (void)execv(shellPath, UNCONST(args));
1801 _exit(1);
1802 /* NOTREACHED */
1803
1804 case -1:
1805 *error = str_concat3("Couldn't exec \"", cmd, "\"");
1806 return bmake_strdup("");
1807 }
1808
1809 (void)close(pipefds[1]); /* No need for the writing half */
1810
1811 saved_errno = 0;
1812 Buf_Init(&buf);
1813
1814 do {
1815 char result[BUFSIZ];
1816 bytes_read = read(pipefds[0], result, sizeof result);
1817 if (bytes_read > 0)
1818 Buf_AddBytes(&buf, result, (size_t)bytes_read);
1819 } while (bytes_read > 0 || (bytes_read == -1 && errno == EINTR));
1820 if (bytes_read == -1)
1821 saved_errno = errno;
1822
1823 (void)close(pipefds[0]); /* Close the input side of the pipe. */
1824
1825 while ((pid = waitpid(cpid, &status, 0)) != cpid && pid >= 0)
1826 JobReapChild(pid, status, false);
1827
1828 if (Buf_EndsWith(&buf, '\n'))
1829 buf.data[buf.len - 1] = '\0';
1830
1831 output = Buf_DoneData(&buf);
1832 for (p = output; *p != '\0'; p++)
1833 if (*p == '\n')
1834 *p = ' ';
1835
1836 if (WIFSIGNALED(status))
1837 *error = str_concat3("\"", cmd, "\" exited on a signal");
1838 else if (WEXITSTATUS(status) != 0) {
1839 Buffer errBuf;
1840 Buf_Init(&errBuf);
1841 Buf_AddStr(&errBuf, "Command \"");
1842 Buf_AddStr(&errBuf, cmd);
1843 Buf_AddStr(&errBuf, "\" exited with status ");
1844 Buf_AddInt(&errBuf, WEXITSTATUS(status));
1845 *error = Buf_DoneData(&errBuf);
1846 } else if (saved_errno != 0)
1847 *error = str_concat3(
1848 "Couldn't read shell's output for \"", cmd, "\"");
1849 else
1850 *error = NULL;
1851 if (cmd_file[0] != '\0')
1852 unlink(cmd_file);
1853 return output;
1854 }
1855
1856 /*
1857 * Print a printf-style error message.
1858 *
1859 * In default mode, this error message has no consequences, for compatibility
1860 * reasons, in particular it does not affect the exit status. Only in lint
1861 * mode (-dL) it does.
1862 */
1863 void
1864 Error(const char *fmt, ...)
1865 {
1866 va_list ap;
1867 FILE *f;
1868
1869 f = opts.debug_file;
1870 if (f == stdout)
1871 f = stderr;
1872 (void)fflush(stdout);
1873
1874 for (;;) {
1875 fprintf(f, "%s: ", progname);
1876 va_start(ap, fmt);
1877 (void)vfprintf(f, fmt, ap);
1878 va_end(ap);
1879 (void)fprintf(f, "\n");
1880 (void)fflush(f);
1881 if (f == stderr)
1882 break;
1883 f = stderr;
1884 }
1885 main_errors++;
1886 }
1887
1888 /*
1889 * Wait for any running jobs to finish, then produce an error message,
1890 * finally exit immediately.
1891 *
1892 * Exiting immediately differs from Parse_Error, which exits only after the
1893 * current top-level makefile has been parsed completely.
1894 */
1895 void
1896 Fatal(const char *fmt, ...)
1897 {
1898 va_list ap;
1899
1900 if (jobsRunning)
1901 Job_Wait();
1902
1903 (void)fflush(stdout);
1904 fprintf(stderr, "%s: ", progname);
1905 va_start(ap, fmt);
1906 (void)vfprintf(stderr, fmt, ap);
1907 va_end(ap);
1908 (void)fprintf(stderr, "\n");
1909 (void)fflush(stderr);
1910 PrintStackTrace(true);
1911
1912 PrintOnError(NULL, "\n");
1913
1914 if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
1915 Targ_PrintGraph(2);
1916 Trace_Log(MAKEERROR, NULL);
1917 exit(2); /* Not 1 so -q can distinguish error */
1918 }
1919
1920 /*
1921 * Major exception once jobs are being created.
1922 * Kills all jobs, prints a message and exits.
1923 */
1924 void
1925 Punt(const char *fmt, ...)
1926 {
1927 va_list ap;
1928
1929 (void)fflush(stdout);
1930 (void)fprintf(stderr, "%s: ", progname);
1931 va_start(ap, fmt);
1932 (void)vfprintf(stderr, fmt, ap);
1933 va_end(ap);
1934 (void)fprintf(stderr, "\n");
1935 (void)fflush(stderr);
1936
1937 PrintOnError(NULL, "\n");
1938
1939 DieHorribly();
1940 }
1941
1942 /* Exit without giving a message. */
1943 void
1944 DieHorribly(void)
1945 {
1946 if (jobsRunning)
1947 Job_AbortAll();
1948 if (DEBUG(GRAPH2))
1949 Targ_PrintGraph(2);
1950 Trace_Log(MAKEERROR, NULL);
1951 exit(2); /* Not 1 so -q can distinguish error */
1952 }
1953
1954 int
1955 unlink_file(const char *file)
1956 {
1957 struct stat st;
1958
1959 if (lstat(file, &st) == -1)
1960 return -1;
1961
1962 if (S_ISDIR(st.st_mode)) {
1963 /*
1964 * POSIX says for unlink: "The path argument shall not name
1965 * a directory unless [...]".
1966 */
1967 errno = EISDIR;
1968 return -1;
1969 }
1970 return unlink(file);
1971 }
1972
1973 static void
1974 write_all(int fd, const void *data, size_t n)
1975 {
1976 const char *mem = data;
1977
1978 while (n > 0) {
1979 ssize_t written = write(fd, mem, n);
1980 /* XXX: Should this EAGAIN be EINTR? */
1981 if (written == -1 && errno == EAGAIN)
1982 continue;
1983 if (written == -1)
1984 break;
1985 mem += written;
1986 n -= (size_t)written;
1987 }
1988 }
1989
1990 /* Print why exec failed, avoiding stdio. */
1991 void MAKE_ATTR_DEAD
1992 execDie(const char *func, const char *arg)
1993 {
1994 char msg[1024];
1995 int len;
1996
1997 len = snprintf(msg, sizeof(msg), "%s: %s(%s): %s\n",
1998 progname, func, arg, strerror(errno));
1999 write_all(STDERR_FILENO, msg, (size_t)len);
2000 _exit(1);
2001 }
2002
2003 static void
2004 purge_relative_cached_realpaths(void)
2005 {
2006 HashIter hi;
2007 bool more;
2008
2009 HashIter_Init(&hi, &cached_realpaths);
2010 more = HashIter_Next(&hi);
2011 while (more) {
2012 HashEntry *he = hi.entry;
2013 more = HashIter_Next(&hi);
2014 if (he->key[0] != '/') {
2015 DEBUG1(DIR, "cached_realpath: purging %s\n", he->key);
2016 free(he->value);
2017 HashTable_DeleteEntry(&cached_realpaths, he);
2018 }
2019 }
2020 }
2021
2022 const char *
2023 cached_realpath(const char *pathname, char *resolved)
2024 {
2025 const char *rp;
2026
2027 if (pathname == NULL || pathname[0] == '\0')
2028 return NULL;
2029
2030 rp = HashTable_FindValue(&cached_realpaths, pathname);
2031 if (rp != NULL) {
2032 snprintf(resolved, MAXPATHLEN, "%s", rp);
2033 return resolved;
2034 }
2035
2036 rp = realpath(pathname, resolved);
2037 if (rp != NULL) {
2038 HashTable_Set(&cached_realpaths, pathname, bmake_strdup(rp));
2039 DEBUG2(DIR, "cached_realpath: %s -> %s\n", pathname, rp);
2040 return resolved;
2041 }
2042
2043 /* should we negative-cache? */
2044 return NULL;
2045 }
2046
2047 /*
2048 * Return true if we should die without noise.
2049 * For example our failing child was a sub-make or failure happened elsewhere.
2050 */
2051 bool
2052 shouldDieQuietly(GNode *gn, int bf)
2053 {
2054 static int quietly = -1;
2055
2056 if (quietly < 0) {
2057 if (DEBUG(JOB) ||
2058 !GetBooleanExpr("${.MAKE.DIE_QUIETLY}", true))
2059 quietly = 0;
2060 else if (bf >= 0)
2061 quietly = bf;
2062 else
2063 quietly = (gn != NULL && (gn->type & OP_MAKE)) ? 1 : 0;
2064 }
2065 return quietly != 0;
2066 }
2067
2068 static void
2069 SetErrorVars(GNode *gn)
2070 {
2071 StringListNode *ln;
2072 char sts[16];
2073
2074 /*
2075 * We can print this even if there is no .ERROR target.
2076 */
2077 snprintf(sts, sizeof(sts), "%d", gn->exit_status);
2078 Global_Set(".ERROR_EXIT", sts);
2079 Global_Set(".ERROR_TARGET", gn->name);
2080 Global_Delete(".ERROR_CMD");
2081
2082 for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
2083 const char *cmd = ln->datum;
2084
2085 if (cmd == NULL)
2086 break;
2087 Global_Append(".ERROR_CMD", cmd);
2088 }
2089 }
2090
2091 /*
2092 * Print some helpful information in case of an error.
2093 * The caller should exit soon after calling this function.
2094 */
2095 void
2096 PrintOnError(GNode *gn, const char *msg)
2097 {
2098 static GNode *errorNode = NULL;
2099 StringListNode *ln;
2100
2101 if (DEBUG(HASH)) {
2102 Targ_Stats();
2103 Var_Stats();
2104 }
2105
2106 if (errorNode != NULL)
2107 return; /* we've been here! */
2108
2109 printf("%s%s: stopped", msg, progname);
2110 ln = opts.create.first;
2111 if (ln != NULL || mainNode != NULL) {
2112 printf(" making \"");
2113 if (ln != NULL) {
2114 printf("%s", (const char *)ln->datum);
2115 for (ln = ln->next; ln != NULL; ln = ln->next)
2116 printf(" %s", (const char *)ln->datum);
2117 } else
2118 printf("%s", mainNode->name);
2119 printf("\"");
2120 }
2121 printf(" in %s\n", curdir);
2122
2123 /* we generally want to keep quiet if a sub-make died */
2124 if (shouldDieQuietly(gn, -1))
2125 return;
2126
2127 if (gn != NULL)
2128 SetErrorVars(gn);
2129
2130 {
2131 char *errorVarsValues;
2132 enum PosixState p_s = posix_state;
2133
2134 posix_state = PS_TOO_LATE;
2135 errorVarsValues = Var_Subst(
2136 "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
2137 SCOPE_GLOBAL, VARE_EVAL);
2138 /* TODO: handle errors */
2139 printf("%s", errorVarsValues);
2140 free(errorVarsValues);
2141 posix_state = p_s;
2142 }
2143
2144 fflush(stdout);
2145
2146 /*
2147 * Finally, see if there is a .ERROR target, and run it if so.
2148 */
2149 errorNode = Targ_FindNode(".ERROR");
2150 if (errorNode != NULL) {
2151 errorNode->type |= OP_SPECIAL;
2152 Compat_Make(errorNode, errorNode);
2153 }
2154 }
2155
2156 void
2157 Main_ExportMAKEFLAGS(bool first)
2158 {
2159 static bool once = true;
2160 enum PosixState p_s;
2161 char *flags;
2162
2163 if (once != first)
2164 return;
2165 once = false;
2166
2167 p_s = posix_state;
2168 posix_state = PS_TOO_LATE;
2169 flags = Var_Subst(
2170 "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
2171 SCOPE_CMDLINE, VARE_EVAL);
2172 /* TODO: handle errors */
2173 if (flags[0] != '\0')
2174 setenv("MAKEFLAGS", flags, 1);
2175 free(flags);
2176 posix_state = p_s;
2177 }
2178
2179 char *
2180 getTmpdir(void)
2181 {
2182 static char *tmpdir = NULL;
2183 struct stat st;
2184
2185 if (tmpdir != NULL)
2186 return tmpdir;
2187
2188 /* Honor $TMPDIR if it is valid, strip a trailing '/'. */
2189 tmpdir = Var_Subst("${TMPDIR:tA:U" _PATH_TMP ":S,/$,,W}/",
2190 SCOPE_GLOBAL, VARE_EVAL);
2191 /* TODO: handle errors */
2192
2193 if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
2194 free(tmpdir);
2195 tmpdir = bmake_strdup(_PATH_TMP);
2196 }
2197 return tmpdir;
2198 }
2199
2200 /*
2201 * Create and open a temp file using "pattern".
2202 * If tfile is provided, set it to a copy of the filename created.
2203 * Otherwise unlink the file once open.
2204 */
2205 int
2206 mkTempFile(const char *pattern, char *tfile, size_t tfile_sz)
2207 {
2208 static char *tmpdir = NULL;
2209 char tbuf[MAXPATHLEN];
2210 int fd;
2211
2212 if (pattern == NULL)
2213 pattern = "makeXXXXXX";
2214 if (tmpdir == NULL)
2215 tmpdir = getTmpdir();
2216 if (tfile == NULL) {
2217 tfile = tbuf;
2218 tfile_sz = sizeof tbuf;
2219 }
2220
2221 if (pattern[0] == '/')
2222 snprintf(tfile, tfile_sz, "%s", pattern);
2223 else
2224 snprintf(tfile, tfile_sz, "%s%s", tmpdir, pattern);
2225
2226 if ((fd = mkstemp(tfile)) < 0)
2227 Punt("mkstemp %s: %s", tfile, strerror(errno));
2228 if (tfile == tbuf)
2229 unlink(tfile); /* we just want the descriptor */
2230
2231 return fd;
2232 }
2233
2234 /*
2235 * Convert a string representation of a boolean into a boolean value.
2236 * Anything that looks like "No", "False", "Off", "0" etc. is false,
2237 * the empty string is the fallback, everything else is true.
2238 */
2239 bool
2240 ParseBoolean(const char *s, bool fallback)
2241 {
2242 char ch = ch_tolower(s[0]);
2243 if (ch == '\0')
2244 return fallback;
2245 if (ch == '0' || ch == 'f' || ch == 'n')
2246 return false;
2247 if (ch == 'o')
2248 return ch_tolower(s[1]) != 'f';
2249 return true;
2250 }
2251