main.c revision 1.586 1 /* $NetBSD: main.c,v 1.586 2023/01/01 16:46:26 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 * Finish Finish things up by printing the number of errors
88 * that occurred, and exit.
89 */
90
91 #include <sys/types.h>
92 #include <sys/time.h>
93 #include <sys/param.h>
94 #include <sys/resource.h>
95 #include <sys/stat.h>
96 #ifdef MAKE_NATIVE
97 #include <sys/sysctl.h>
98 #endif
99 #include <sys/utsname.h>
100 #include <sys/wait.h>
101
102 #include <errno.h>
103 #include <signal.h>
104 #include <stdarg.h>
105 #include <time.h>
106
107 #include "make.h"
108 #include "dir.h"
109 #include "job.h"
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.586 2023/01/01 16:46:26 rillig Exp $");
115 #if defined(MAKE_NATIVE) && !defined(lint)
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 line by itself */
125 bool deleteOnError; /* .DELETE_ON_ERROR: set */
126
127 static int maxJobTokens; /* -j argument */
128 bool enterFlagObj; /* -w and objdir != srcdir */
129
130 static int jp_0 = -1, jp_1 = -1; /* ends of parent job pipe */
131 bool doing_depend; /* Set while reading .depend */
132 static bool jobsRunning; /* true if the jobs might be running */
133 static const char *tracefile;
134 static bool ReadMakefile(const char *);
135 static void purge_relative_cached_realpaths(void);
136
137 static bool ignorePWD; /* if we use -C, PWD is meaningless */
138 static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
139 char curdir[MAXPATHLEN + 1]; /* Startup directory */
140 const char *progname;
141 char *makeDependfile;
142 pid_t myPid;
143 int makelevel;
144
145 bool forceJobs = false;
146 static int main_errors = 0;
147 static HashTable cached_realpaths;
148
149 /*
150 * For compatibility with the POSIX version of MAKEFLAGS that includes
151 * all the options without '-', convert 'flags' to '-f -l -a -g -s'.
152 */
153 static char *
154 explode(const char *flags)
155 {
156 char *exploded, *ep;
157 const char *p;
158
159 if (flags == NULL)
160 return NULL;
161
162 for (p = flags; *p != '\0'; p++)
163 if (!ch_isalpha(*p))
164 return bmake_strdup(flags);
165
166 exploded = bmake_malloc((size_t)(p - flags) * 3 + 1);
167 for (p = flags, ep = exploded; *p != '\0'; p++) {
168 *ep++ = '-';
169 *ep++ = *p;
170 *ep++ = ' ';
171 }
172 *ep = '\0';
173 return exploded;
174 }
175
176 MAKE_ATTR_DEAD static void
177 usage(void)
178 {
179 size_t prognameLen = strcspn(progname, "[");
180
181 (void)fprintf(stderr,
182 "usage: %.*s [-BeikNnqrSstWwX]\n"
183 " [-C directory] [-D variable] [-d flags] [-f makefile]\n"
184 " [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
185 " [-V variable] [-v variable] [variable=value] [target ...]\n",
186 (int)prognameLen, progname);
187 exit(2);
188 }
189
190 static void
191 MainParseArgDebugFile(const char *arg)
192 {
193 const char *mode;
194 size_t len;
195 char *fname;
196
197 if (opts.debug_file != stdout && opts.debug_file != stderr)
198 fclose(opts.debug_file);
199
200 if (*arg == '+') {
201 arg++;
202 mode = "a";
203 } else
204 mode = "w";
205
206 if (strcmp(arg, "stdout") == 0) {
207 opts.debug_file = stdout;
208 return;
209 }
210 if (strcmp(arg, "stderr") == 0) {
211 opts.debug_file = stderr;
212 return;
213 }
214
215 len = strlen(arg);
216 fname = bmake_malloc(len + 20);
217 memcpy(fname, arg, len + 1);
218
219 /* Replace the trailing '%d' after '.%d' with the pid. */
220 if (len >= 3 && memcmp(fname + len - 3, ".%d", 3) == 0)
221 snprintf(fname + len - 2, 20, "%d", getpid());
222
223 opts.debug_file = fopen(fname, mode);
224 if (opts.debug_file == NULL) {
225 fprintf(stderr, "Cannot open debug file \"%s\"\n",
226 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: %s.\n", progname, strerror(errno));
365 exit(2);
366 }
367 if (!IsRelativePath(argvalue) &&
368 stat(argvalue, &sa) != -1 &&
369 stat(curdir, &sb) != -1 &&
370 sa.st_ino == sb.st_ino &&
371 sa.st_dev == sb.st_dev)
372 strncpy(curdir, argvalue, MAXPATHLEN);
373 ignorePWD = true;
374 }
375
376 static void
377 MainParseArgJobsInternal(const char *argvalue)
378 {
379 char end;
380 if (sscanf(argvalue, "%d,%d%c", &jp_0, &jp_1, &end) != 2) {
381 (void)fprintf(stderr,
382 "%s: internal error -- J option malformed (%s)\n",
383 progname, argvalue);
384 usage();
385 }
386 if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
387 (fcntl(jp_1, F_GETFD, 0) < 0)) {
388 jp_0 = -1;
389 jp_1 = -1;
390 opts.compatMake = true;
391 } else {
392 Global_Append(MAKEFLAGS, "-J");
393 Global_Append(MAKEFLAGS, argvalue);
394 }
395 }
396
397 static void
398 MainParseArgJobs(const char *argvalue)
399 {
400 char *p;
401
402 forceJobs = true;
403 opts.maxJobs = (int)strtol(argvalue, &p, 0);
404 if (*p != '\0' || opts.maxJobs < 1) {
405 (void)fprintf(stderr,
406 "%s: illegal argument to -j -- must be positive integer!\n",
407 progname);
408 exit(2); /* Not 1 so -q can distinguish error */
409 }
410 Global_Append(MAKEFLAGS, "-j");
411 Global_Append(MAKEFLAGS, argvalue);
412 Global_Set(".MAKE.JOBS", argvalue);
413 maxJobTokens = opts.maxJobs;
414 }
415
416 static void
417 MainParseArgSysInc(const char *argvalue)
418 {
419 /* look for magic parent directory search string */
420 if (strncmp(".../", argvalue, 4) == 0) {
421 char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
422 if (found_path == NULL)
423 return;
424 (void)SearchPath_Add(sysIncPath, found_path);
425 free(found_path);
426 } else {
427 (void)SearchPath_Add(sysIncPath, argvalue);
428 }
429 Global_Append(MAKEFLAGS, "-m");
430 Global_Append(MAKEFLAGS, argvalue);
431 }
432
433 static bool
434 MainParseArg(char c, const char *argvalue)
435 {
436 switch (c) {
437 case '\0':
438 break;
439 case 'B':
440 opts.compatMake = true;
441 Global_Append(MAKEFLAGS, "-B");
442 Global_Set(MAKE_MODE, "compat");
443 break;
444 case 'C':
445 MainParseArgChdir(argvalue);
446 break;
447 case 'D':
448 if (argvalue[0] == '\0')
449 return false;
450 Var_SetExpand(SCOPE_GLOBAL, argvalue, "1");
451 Global_Append(MAKEFLAGS, "-D");
452 Global_Append(MAKEFLAGS, argvalue);
453 break;
454 case 'I':
455 Parse_AddIncludeDir(argvalue);
456 Global_Append(MAKEFLAGS, "-I");
457 Global_Append(MAKEFLAGS, argvalue);
458 break;
459 case 'J':
460 MainParseArgJobsInternal(argvalue);
461 break;
462 case 'N':
463 opts.noExecute = true;
464 opts.noRecursiveExecute = true;
465 Global_Append(MAKEFLAGS, "-N");
466 break;
467 case 'S':
468 opts.keepgoing = false;
469 Global_Append(MAKEFLAGS, "-S");
470 break;
471 case 'T':
472 tracefile = bmake_strdup(argvalue);
473 Global_Append(MAKEFLAGS, "-T");
474 Global_Append(MAKEFLAGS, argvalue);
475 break;
476 case 'V':
477 case 'v':
478 opts.printVars = c == 'v' ? PVM_EXPANDED : PVM_UNEXPANDED;
479 Lst_Append(&opts.variables, bmake_strdup(argvalue));
480 /* XXX: Why always -V? */
481 Global_Append(MAKEFLAGS, "-V");
482 Global_Append(MAKEFLAGS, argvalue);
483 break;
484 case 'W':
485 opts.parseWarnFatal = true;
486 /* XXX: why no Global_Append? */
487 break;
488 case 'X':
489 opts.varNoExportEnv = true;
490 Global_Append(MAKEFLAGS, "-X");
491 break;
492 case 'd':
493 /* If '-d-opts' don't pass to children */
494 if (argvalue[0] == '-')
495 argvalue++;
496 else {
497 Global_Append(MAKEFLAGS, "-d");
498 Global_Append(MAKEFLAGS, argvalue);
499 }
500 MainParseArgDebug(argvalue);
501 break;
502 case 'e':
503 opts.checkEnvFirst = true;
504 Global_Append(MAKEFLAGS, "-e");
505 break;
506 case 'f':
507 Lst_Append(&opts.makefiles, bmake_strdup(argvalue));
508 break;
509 case 'i':
510 opts.ignoreErrors = true;
511 Global_Append(MAKEFLAGS, "-i");
512 break;
513 case 'j':
514 MainParseArgJobs(argvalue);
515 break;
516 case 'k':
517 opts.keepgoing = true;
518 Global_Append(MAKEFLAGS, "-k");
519 break;
520 case 'm':
521 MainParseArgSysInc(argvalue);
522 /* XXX: why no Var_Append? */
523 break;
524 case 'n':
525 opts.noExecute = true;
526 Global_Append(MAKEFLAGS, "-n");
527 break;
528 case 'q':
529 opts.query = true;
530 /* Kind of nonsensical, wot? */
531 Global_Append(MAKEFLAGS, "-q");
532 break;
533 case 'r':
534 opts.noBuiltins = true;
535 Global_Append(MAKEFLAGS, "-r");
536 break;
537 case 's':
538 opts.silent = true;
539 Global_Append(MAKEFLAGS, "-s");
540 break;
541 case 't':
542 opts.touch = true;
543 Global_Append(MAKEFLAGS, "-t");
544 break;
545 case 'w':
546 opts.enterFlag = true;
547 Global_Append(MAKEFLAGS, "-w");
548 break;
549 default:
550 usage();
551 }
552 return true;
553 }
554
555 /*
556 * Parse the given arguments. Called from main() and from
557 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
558 *
559 * The arguments must be treated as read-only and will be freed after the
560 * call.
561 *
562 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
563 */
564 static void
565 MainParseArgs(int argc, char **argv)
566 {
567 char c;
568 int arginc;
569 char *argvalue;
570 char *optscan;
571 bool inOption, dashDash = false;
572
573 const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
574 /* Can't actually use getopt(3) because rescanning is not portable */
575
576 rearg:
577 inOption = false;
578 optscan = NULL;
579 while (argc > 1) {
580 const char *optspec;
581 if (!inOption)
582 optscan = argv[1];
583 c = *optscan++;
584 arginc = 0;
585 if (inOption) {
586 if (c == '\0') {
587 argv++;
588 argc--;
589 inOption = false;
590 continue;
591 }
592 } else {
593 if (c != '-' || dashDash)
594 break;
595 inOption = true;
596 c = *optscan++;
597 }
598 /* '-' found at some earlier point */
599 optspec = strchr(optspecs, c);
600 if (c != '\0' && optspec != NULL && optspec[1] == ':') {
601 /*
602 * -<something> found, and <something> should have an
603 * argument
604 */
605 inOption = false;
606 arginc = 1;
607 argvalue = optscan;
608 if (*argvalue == '\0') {
609 if (argc < 3)
610 goto noarg;
611 argvalue = argv[2];
612 arginc = 2;
613 }
614 } else {
615 argvalue = NULL;
616 }
617 switch (c) {
618 case '\0':
619 arginc = 1;
620 inOption = false;
621 break;
622 case '-':
623 dashDash = true;
624 break;
625 default:
626 if (!MainParseArg(c, argvalue))
627 goto noarg;
628 }
629 argv += arginc;
630 argc -= arginc;
631 }
632
633 /*
634 * See if the rest of the arguments are variable assignments and
635 * perform them if so. Else take them to be targets and stuff them
636 * on the end of the "create" list.
637 */
638 for (; argc > 1; argv++, argc--) {
639 if (!Parse_VarAssign(argv[1], false, SCOPE_CMDLINE)) {
640 if (argv[1][0] == '\0')
641 Punt("illegal (null) argument.");
642 if (argv[1][0] == '-' && !dashDash)
643 goto rearg;
644 Lst_Append(&opts.create, bmake_strdup(argv[1]));
645 }
646 }
647
648 return;
649 noarg:
650 (void)fprintf(stderr, "%s: option requires an argument -- %c\n",
651 progname, c);
652 usage();
653 }
654
655 /*
656 * Break a line of arguments into words and parse them.
657 *
658 * Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
659 * by main() when reading the MAKEFLAGS environment variable.
660 */
661 void
662 Main_ParseArgLine(const char *line)
663 {
664 Words words;
665 char *buf;
666
667 if (line == NULL)
668 return;
669 /* XXX: don't use line as an iterator variable */
670 for (; *line == ' '; line++)
671 continue;
672 if (line[0] == '\0')
673 return;
674
675 {
676 FStr argv0 = Var_Value(SCOPE_GLOBAL, ".MAKE");
677 buf = str_concat3(argv0.str, " ", line);
678 FStr_Done(&argv0);
679 }
680
681 words = Str_Words(buf, true);
682 if (words.words == NULL) {
683 Error("Unterminated quoted string [%s]", buf);
684 free(buf);
685 return;
686 }
687 free(buf);
688 MainParseArgs((int)words.len, words.words);
689
690 Words_Free(words);
691 }
692
693 bool
694 Main_SetObjdir(bool writable, const char *fmt, ...)
695 {
696 struct stat sb;
697 char *path;
698 char buf[MAXPATHLEN + 1];
699 char buf2[MAXPATHLEN + 1];
700 va_list ap;
701
702 va_start(ap, fmt);
703 vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
704 va_end(ap);
705
706 if (path[0] != '/') {
707 if (snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path) <= MAXPATHLEN)
708 path = buf2;
709 else
710 return false;
711 }
712
713 /* look for the directory and try to chdir there */
714 if (stat(path, &sb) != 0 || !S_ISDIR(sb.st_mode))
715 return false;
716
717 if ((writable && access(path, W_OK) != 0) || chdir(path) != 0) {
718 (void)fprintf(stderr, "%s: warning: %s: %s.\n",
719 progname, path, strerror(errno));
720 return false;
721 }
722
723 snprintf(objdir, sizeof objdir, "%s", path);
724 Global_Set(".OBJDIR", objdir);
725 setenv("PWD", objdir, 1);
726 Dir_InitDot();
727 purge_relative_cached_realpaths();
728 if (opts.enterFlag && strcmp(objdir, curdir) != 0)
729 enterFlagObj = true;
730 return true;
731 }
732
733 static bool
734 SetVarObjdir(bool writable, const char *var, const char *suffix)
735 {
736 FStr path = Var_Value(SCOPE_CMDLINE, var);
737
738 if (path.str == NULL || path.str[0] == '\0') {
739 FStr_Done(&path);
740 return false;
741 }
742
743 Var_Expand(&path, SCOPE_GLOBAL, VARE_WANTRES);
744
745 (void)Main_SetObjdir(writable, "%s%s", path.str, suffix);
746
747 FStr_Done(&path);
748 return true;
749 }
750
751 /*
752 * Splits str into words, adding them to the list.
753 * The string must be kept alive as long as the list.
754 */
755 int
756 str2Lst_Append(StringList *lp, char *str)
757 {
758 char *cp;
759 int n;
760
761 const char *sep = " \t";
762
763 for (n = 0, cp = strtok(str, sep); cp != NULL; cp = strtok(NULL, sep)) {
764 Lst_Append(lp, cp);
765 n++;
766 }
767 return n;
768 }
769
770 #ifdef SIGINFO
771 /*ARGSUSED*/
772 static void
773 siginfo(int signo MAKE_ATTR_UNUSED)
774 {
775 char dir[MAXPATHLEN];
776 char str[2 * MAXPATHLEN];
777 int len;
778 if (getcwd(dir, sizeof dir) == NULL)
779 return;
780 len = snprintf(str, sizeof str, "%s: Working in: %s\n", progname, dir);
781 if (len > 0)
782 (void)write(STDERR_FILENO, str, (size_t)len);
783 }
784 #endif
785
786 /* Allow makefiles some control over the mode we run in. */
787 static void
788 MakeMode(void)
789 {
790 char *mode;
791
792 (void)Var_Subst("${" MAKE_MODE ":tl}", SCOPE_GLOBAL, VARE_WANTRES, &mode);
793 /* TODO: handle errors */
794
795 if (mode[0] != '\0') {
796 if (strstr(mode, "compat") != NULL) {
797 opts.compatMake = true;
798 forceJobs = false;
799 }
800 #if USE_META
801 if (strstr(mode, "meta") != NULL)
802 meta_mode_init(mode);
803 #endif
804 if (strstr(mode, "randomize-targets") != NULL)
805 opts.randomizeTargets = true;
806 }
807
808 free(mode);
809 }
810
811 static void
812 PrintVar(const char *varname, bool expandVars)
813 {
814 if (strchr(varname, '$') != NULL) {
815 char *evalue;
816 (void)Var_Subst(varname, SCOPE_GLOBAL, VARE_WANTRES, &evalue);
817 /* TODO: handle errors */
818 printf("%s\n", evalue);
819 free(evalue);
820
821 } else if (expandVars) {
822 char *expr = str_concat3("${", varname, "}");
823 char *evalue;
824 (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &evalue);
825 /* TODO: handle errors */
826 free(expr);
827 printf("%s\n", evalue);
828 free(evalue);
829
830 } else {
831 FStr value = Var_Value(SCOPE_GLOBAL, varname);
832 printf("%s\n", value.str != NULL ? value.str : "");
833 FStr_Done(&value);
834 }
835 }
836
837 /*
838 * Return a bool based on a variable.
839 *
840 * If the knob is not set, return the fallback.
841 * If set, anything that looks or smells like "No", "False", "Off", "0", etc.
842 * is false, otherwise true.
843 */
844 bool
845 GetBooleanExpr(const char *expr, bool fallback)
846 {
847 char *value;
848 bool res;
849
850 (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &value);
851 /* TODO: handle errors */
852 res = ParseBoolean(value, fallback);
853 free(value);
854 return res;
855 }
856
857 static void
858 doPrintVars(void)
859 {
860 StringListNode *ln;
861 bool expandVars;
862
863 if (opts.printVars == PVM_EXPANDED)
864 expandVars = true;
865 else if (opts.debugVflag)
866 expandVars = false;
867 else
868 expandVars = GetBooleanExpr("${.MAKE.EXPAND_VARIABLES}",
869 false);
870
871 for (ln = opts.variables.first; ln != NULL; ln = ln->next) {
872 const char *varname = ln->datum;
873 PrintVar(varname, expandVars);
874 }
875 }
876
877 static bool
878 runTargets(void)
879 {
880 GNodeList targs = LST_INIT; /* target nodes to create */
881 bool outOfDate; /* false if all targets up to date */
882
883 /*
884 * Have now read the entire graph and need to make a list of
885 * targets to create. If none was given on the command line,
886 * we consult the parsing module to find the main target(s)
887 * to create.
888 */
889 if (Lst_IsEmpty(&opts.create))
890 Parse_MainName(&targs);
891 else
892 Targ_FindList(&targs, &opts.create);
893
894 if (!opts.compatMake) {
895 /*
896 * Initialize job module before traversing the graph
897 * now that any .BEGIN and .END targets have been read.
898 * This is done only if the -q flag wasn't given
899 * (to prevent the .BEGIN from being executed should
900 * it exist).
901 */
902 if (!opts.query) {
903 Job_Init();
904 jobsRunning = true;
905 }
906
907 /* Traverse the graph, checking on all the targets */
908 outOfDate = Make_Run(&targs);
909 } else {
910 Compat_MakeAll(&targs);
911 outOfDate = false;
912 }
913 Lst_Done(&targs); /* Don't free the targets themselves. */
914 return outOfDate;
915 }
916
917 /*
918 * Set up the .TARGETS variable to contain the list of targets to be created.
919 * If none specified, make the variable empty for now, the parser will fill
920 * in the default or .MAIN target later.
921 */
922 static void
923 InitVarTargets(void)
924 {
925 StringListNode *ln;
926
927 if (Lst_IsEmpty(&opts.create)) {
928 Global_Set(".TARGETS", "");
929 return;
930 }
931
932 for (ln = opts.create.first; ln != NULL; ln = ln->next) {
933 const char *name = ln->datum;
934 Global_Append(".TARGETS", name);
935 }
936 }
937
938 static void
939 InitRandom(void)
940 {
941 struct timeval tv;
942
943 gettimeofday(&tv, NULL);
944 srandom((unsigned int)(tv.tv_sec + tv.tv_usec));
945 }
946
947 static const char *
948 InitVarMachine(const struct utsname *utsname MAKE_ATTR_UNUSED)
949 {
950 const char *machine = getenv("MACHINE");
951 if (machine != NULL)
952 return machine;
953
954 #if defined(MAKE_NATIVE)
955 return utsname->machine;
956 #elif defined(MAKE_MACHINE)
957 return MAKE_MACHINE;
958 #else
959 return "unknown";
960 #endif
961 }
962
963 static const char *
964 InitVarMachineArch(void)
965 {
966 const char *env = getenv("MACHINE_ARCH");
967 if (env != NULL)
968 return env;
969
970 #ifdef MAKE_NATIVE
971 {
972 struct utsname utsname;
973 static char machine_arch_buf[sizeof utsname.machine];
974 const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
975 size_t len = sizeof machine_arch_buf;
976
977 if (sysctl(mib, (unsigned int)__arraycount(mib),
978 machine_arch_buf, &len, NULL, 0) < 0) {
979 (void)fprintf(stderr, "%s: sysctl failed (%s).\n",
980 progname, strerror(errno));
981 exit(2);
982 }
983
984 return machine_arch_buf;
985 }
986 #elif defined(MACHINE_ARCH)
987 return MACHINE_ARCH;
988 #elif defined(MAKE_MACHINE_ARCH)
989 return MAKE_MACHINE_ARCH;
990 #else
991 return "unknown";
992 #endif
993 }
994
995 #ifndef NO_PWD_OVERRIDE
996 /*
997 * All this code is so that we know where we are when we start up
998 * on a different machine with pmake.
999 *
1000 * XXX: Make no longer has "local" and "remote" mode. Is this code still
1001 * necessary?
1002 *
1003 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
1004 * since the value of curdir can vary depending on how we got
1005 * here. Ie sitting at a shell prompt (shell that provides $PWD)
1006 * or via subdir.mk in which case its 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 a variable 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 (void)strncpy(curdir, pwd, MAXPATHLEN);
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
1118 if (argv0[0] != '/' && strchr(argv0, '/') != NULL) {
1119 char pathbuf[MAXPATHLEN];
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, *cp;
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 = cp) {
1152 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1153 continue;
1154 if (*cp == ':')
1155 *cp++ = '\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_DoneCall(&sysMkFiles, free);
1195 }
1196
1197 static void
1198 InitMaxJobs(void)
1199 {
1200 char *value;
1201 int n;
1202
1203 if (forceJobs || opts.compatMake ||
1204 !Var_Exists(SCOPE_GLOBAL, ".MAKE.JOBS"))
1205 return;
1206
1207 (void)Var_Subst("${.MAKE.JOBS}", SCOPE_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(2); /* Not 1 so -q can distinguish error */
1216 }
1217
1218 if (n != opts.maxJobs) {
1219 Global_Append(MAKEFLAGS, "-j");
1220 Global_Append(MAKEFLAGS, value);
1221 }
1222
1223 opts.maxJobs = n;
1224 maxJobTokens = opts.maxJobs;
1225 forceJobs = true;
1226 free(value);
1227 }
1228
1229 /*
1230 * For compatibility, look at the directories in the VPATH variable
1231 * and add them to the search path, if the variable is defined. The
1232 * variable's value is in the same format as the PATH environment
1233 * variable, i.e. <directory>:<directory>:<directory>...
1234 */
1235 static void
1236 InitVpath(void)
1237 {
1238 char *vpath, savec, *path;
1239 if (!Var_Exists(SCOPE_CMDLINE, "VPATH"))
1240 return;
1241
1242 (void)Var_Subst("${VPATH}", SCOPE_CMDLINE, VARE_WANTRES, &vpath);
1243 /* TODO: handle errors */
1244 path = vpath;
1245 do {
1246 char *cp;
1247 /* skip to end of directory */
1248 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
1249 continue;
1250 /* Save terminator character so know when to stop */
1251 savec = *cp;
1252 *cp = '\0';
1253 /* Add directory to search path */
1254 (void)SearchPath_Add(&dirSearchPath, path);
1255 *cp = savec;
1256 path = cp + 1;
1257 } while (savec == ':');
1258 free(vpath);
1259 }
1260
1261 static void
1262 ReadAllMakefiles(const StringList *makefiles)
1263 {
1264 StringListNode *ln;
1265
1266 for (ln = makefiles->first; ln != NULL; ln = ln->next) {
1267 const char *fname = ln->datum;
1268 if (!ReadMakefile(fname))
1269 Fatal("%s: cannot open %s.", progname, fname);
1270 }
1271 }
1272
1273 static void
1274 ReadFirstDefaultMakefile(void)
1275 {
1276 StringList makefiles = LST_INIT;
1277 StringListNode *ln;
1278 char *prefs;
1279
1280 (void)Var_Subst("${" MAKE_MAKEFILE_PREFERENCE "}",
1281 SCOPE_CMDLINE, VARE_WANTRES, &prefs);
1282 /* TODO: handle errors */
1283
1284 (void)str2Lst_Append(&makefiles, prefs);
1285
1286 for (ln = makefiles.first; ln != NULL; ln = ln->next)
1287 if (ReadMakefile(ln->datum))
1288 break;
1289
1290 Lst_Done(&makefiles);
1291 free(prefs);
1292 }
1293
1294 /*
1295 * Initialize variables such as MAKE, MACHINE, .MAKEFLAGS.
1296 * Initialize a few modules.
1297 * Parse the arguments from MAKEFLAGS and the command line.
1298 */
1299 static void
1300 main_Init(int argc, char **argv)
1301 {
1302 struct stat sa;
1303 const char *machine;
1304 const char *machine_arch;
1305 char *syspath = getenv("MAKESYSPATH");
1306 struct utsname utsname;
1307
1308 /* default to writing debug to stderr */
1309 opts.debug_file = stderr;
1310
1311 Str_Intern_Init();
1312 HashTable_Init(&cached_realpaths);
1313
1314 #ifdef SIGINFO
1315 (void)bmake_signal(SIGINFO, siginfo);
1316 #endif
1317
1318 InitRandom();
1319
1320 progname = str_basename(argv[0]);
1321
1322 UnlimitFiles();
1323
1324 if (uname(&utsname) == -1) {
1325 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
1326 strerror(errno));
1327 exit(2);
1328 }
1329
1330 /*
1331 * Get the name of this type of MACHINE from utsname
1332 * so we can share an executable for similar machines.
1333 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
1334 *
1335 * Note that both MACHINE and MACHINE_ARCH are decided at
1336 * run-time.
1337 */
1338 machine = InitVarMachine(&utsname);
1339 machine_arch = InitVarMachineArch();
1340
1341 myPid = getpid(); /* remember this for vFork() */
1342
1343 /*
1344 * Just in case MAKEOBJDIR wants us to do something tricky.
1345 */
1346 Targ_Init();
1347 Var_Init();
1348 Global_Set(".MAKE.OS", utsname.sysname);
1349 Global_Set("MACHINE", machine);
1350 Global_Set("MACHINE_ARCH", machine_arch);
1351 #ifdef MAKE_VERSION
1352 Global_Set("MAKE_VERSION", MAKE_VERSION);
1353 #endif
1354 Global_Set(".newline", "\n"); /* handy for :@ loops */
1355 #ifndef MAKEFILE_PREFERENCE_LIST
1356 /* This is the traditional preference for makefiles. */
1357 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
1358 #endif
1359 Global_Set(MAKE_MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST);
1360 Global_Set(MAKE_DEPENDFILE, ".depend");
1361
1362 CmdOpts_Init();
1363 allPrecious = false; /* Remove targets when interrupted */
1364 deleteOnError = false; /* Historical default behavior */
1365 jobsRunning = false;
1366
1367 maxJobTokens = opts.maxJobs;
1368 ignorePWD = false;
1369
1370 /*
1371 * Initialize the parsing, directory and variable modules to prepare
1372 * for the reading of inclusion paths and variable settings on the
1373 * command line
1374 */
1375
1376 /*
1377 * Initialize various variables.
1378 * MAKE also gets this name, for compatibility
1379 * .MAKEFLAGS gets set to the empty string just in case.
1380 * MFLAGS also gets initialized empty, for compatibility.
1381 */
1382 Parse_Init();
1383 InitVarMake(argv[0]);
1384 Global_Set(MAKEFLAGS, "");
1385 Global_Set(MAKEOVERRIDES, "");
1386 Global_Set("MFLAGS", "");
1387 Global_Set(".ALLTARGETS", "");
1388 Var_Set(SCOPE_CMDLINE, ".MAKE.LEVEL.ENV", MAKE_LEVEL_ENV);
1389
1390 /* Set some other useful variables. */
1391 {
1392 char buf[64], *ep = getenv(MAKE_LEVEL_ENV);
1393
1394 makelevel = ep != NULL && ep[0] != '\0' ? atoi(ep) : 0;
1395 if (makelevel < 0)
1396 makelevel = 0;
1397 snprintf(buf, sizeof buf, "%d", makelevel);
1398 Global_Set(MAKE_LEVEL, buf);
1399 snprintf(buf, sizeof buf, "%u", myPid);
1400 Global_Set(".MAKE.PID", buf);
1401 snprintf(buf, sizeof buf, "%u", getppid());
1402 Global_Set(".MAKE.PPID", buf);
1403 snprintf(buf, sizeof buf, "%u", getuid());
1404 Global_Set(".MAKE.UID", buf);
1405 snprintf(buf, sizeof buf, "%u", getgid());
1406 Global_Set(".MAKE.GID", buf);
1407 }
1408 if (makelevel > 0) {
1409 char pn[1024];
1410 snprintf(pn, sizeof pn, "%s[%d]", progname, makelevel);
1411 progname = bmake_strdup(pn);
1412 }
1413
1414 #ifdef USE_META
1415 meta_init();
1416 #endif
1417 Dir_Init();
1418
1419 #ifdef POSIX
1420 {
1421 char *makeflags = explode(getenv("MAKEFLAGS"));
1422 Main_ParseArgLine(makeflags);
1423 free(makeflags);
1424 }
1425 #else
1426 /*
1427 * First snag any flags out of the MAKE environment variable.
1428 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
1429 * in a different format).
1430 */
1431 Main_ParseArgLine(getenv("MAKE"));
1432 #endif
1433
1434 if (getcwd(curdir, MAXPATHLEN) == NULL) {
1435 (void)fprintf(stderr, "%s: getcwd: %s.\n",
1436 progname, strerror(errno));
1437 exit(2);
1438 }
1439
1440 MainParseArgs(argc, argv);
1441
1442 if (opts.enterFlag)
1443 printf("%s: Entering directory `%s'\n", progname, curdir);
1444
1445 if (stat(curdir, &sa) == -1) {
1446 (void)fprintf(stderr, "%s: %s: %s.\n",
1447 progname, curdir, strerror(errno));
1448 exit(2);
1449 }
1450
1451 #ifndef NO_PWD_OVERRIDE
1452 HandlePWD(&sa);
1453 #endif
1454 Global_Set(".CURDIR", curdir);
1455
1456 InitObjdir(machine, machine_arch);
1457
1458 Arch_Init();
1459 Suff_Init();
1460 Trace_Init(tracefile);
1461
1462 defaultNode = NULL;
1463 (void)time(&now);
1464
1465 Trace_Log(MAKESTART, NULL);
1466
1467 InitVarTargets();
1468
1469 InitDefSysIncPath(syspath);
1470 }
1471
1472 /*
1473 * Read the system makefile followed by either makefile, Makefile or the
1474 * files given by the -f option. Exit on parse errors.
1475 */
1476 static void
1477 main_ReadFiles(void)
1478 {
1479
1480 if (!opts.noBuiltins)
1481 ReadBuiltinRules();
1482
1483 posix_state = PS_MAYBE_NEXT_LINE;
1484 if (!Lst_IsEmpty(&opts.makefiles))
1485 ReadAllMakefiles(&opts.makefiles);
1486 else
1487 ReadFirstDefaultMakefile();
1488 }
1489
1490 /* Compute the dependency graph. */
1491 static void
1492 main_PrepareMaking(void)
1493 {
1494 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
1495 if (!opts.noBuiltins || opts.printVars == PVM_NONE) {
1496 (void)Var_Subst("${.MAKE.DEPENDFILE}",
1497 SCOPE_CMDLINE, VARE_WANTRES, &makeDependfile);
1498 if (makeDependfile[0] != '\0') {
1499 /* TODO: handle errors */
1500 doing_depend = true;
1501 (void)ReadMakefile(makeDependfile);
1502 doing_depend = false;
1503 }
1504 }
1505
1506 if (enterFlagObj)
1507 printf("%s: Entering directory `%s'\n", progname, objdir);
1508
1509 MakeMode();
1510
1511 {
1512 FStr makeflags = Var_Value(SCOPE_GLOBAL, MAKEFLAGS);
1513 Global_Append("MFLAGS", makeflags.str);
1514 FStr_Done(&makeflags);
1515 }
1516
1517 InitMaxJobs();
1518
1519 if (!opts.compatMake && !forceJobs)
1520 opts.compatMake = true;
1521
1522 if (!opts.compatMake)
1523 Job_ServerStart(maxJobTokens, jp_0, jp_1);
1524 DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1525 jp_0, jp_1, opts.maxJobs, maxJobTokens, opts.compatMake ? 1 : 0);
1526
1527 if (opts.printVars == PVM_NONE)
1528 Main_ExportMAKEFLAGS(true); /* initial export */
1529
1530 InitVpath();
1531
1532 /*
1533 * Now that all search paths have been read for suffixes et al, it's
1534 * time to add the default search path to their lists...
1535 */
1536 Suff_ExtendPaths();
1537
1538 /*
1539 * Propagate attributes through :: dependency lists.
1540 */
1541 Targ_Propagate();
1542
1543 /* print the initial graph, if the user requested it */
1544 if (DEBUG(GRAPH1))
1545 Targ_PrintGraph(1);
1546 }
1547
1548 /*
1549 * Make the targets.
1550 * If the -v or -V options are given, print variables instead.
1551 * Return whether any of the targets is out-of-date.
1552 */
1553 static bool
1554 main_Run(void)
1555 {
1556 if (opts.printVars != PVM_NONE) {
1557 /* print the values of any variables requested by the user */
1558 doPrintVars();
1559 return false;
1560 } else {
1561 return runTargets();
1562 }
1563 }
1564
1565 /* Clean up after making the targets. */
1566 static void
1567 main_CleanUp(void)
1568 {
1569 #ifdef CLEANUP
1570 Lst_DoneCall(&opts.variables, free);
1571 Lst_DoneCall(&opts.makefiles, free);
1572 Lst_DoneCall(&opts.create, free);
1573 #endif
1574
1575 if (DEBUG(GRAPH2))
1576 Targ_PrintGraph(2);
1577
1578 Trace_Log(MAKEEND, NULL);
1579
1580 if (enterFlagObj)
1581 printf("%s: Leaving directory `%s'\n", progname, objdir);
1582 if (opts.enterFlag)
1583 printf("%s: Leaving directory `%s'\n", progname, curdir);
1584
1585 #ifdef USE_META
1586 meta_finish();
1587 #endif
1588 Suff_End();
1589 Targ_End();
1590 Arch_End();
1591 Var_End();
1592 Parse_End();
1593 Dir_End();
1594 Job_End();
1595 Trace_End();
1596 Str_Intern_End();
1597 }
1598
1599 /* Determine the exit code. */
1600 static int
1601 main_Exit(bool outOfDate)
1602 {
1603 if (opts.strict && (main_errors > 0 || Parse_NumErrors() > 0))
1604 return 2; /* Not 1 so -q can distinguish error */
1605 return outOfDate ? 1 : 0;
1606 }
1607
1608 int
1609 main(int argc, char **argv)
1610 {
1611 bool outOfDate;
1612
1613 main_Init(argc, argv);
1614 main_ReadFiles();
1615 main_PrepareMaking();
1616 outOfDate = main_Run();
1617 main_CleanUp();
1618 return main_Exit(outOfDate);
1619 }
1620
1621 /*
1622 * Open and parse the given makefile, with all its side effects.
1623 * Return false if the file could not be opened.
1624 */
1625 static bool
1626 ReadMakefile(const char *fname)
1627 {
1628 int fd;
1629 char *name, *path = NULL;
1630
1631 if (strcmp(fname, "-") == 0) {
1632 Parse_File("(stdin)", -1);
1633 Var_Set(SCOPE_INTERNAL, "MAKEFILE", "");
1634 } else {
1635 /* if we've chdir'd, rebuild the path name */
1636 if (strcmp(curdir, objdir) != 0 && *fname != '/') {
1637 path = str_concat3(curdir, "/", fname);
1638 fd = open(path, O_RDONLY);
1639 if (fd != -1) {
1640 fname = path;
1641 goto found;
1642 }
1643 free(path);
1644
1645 /* If curdir failed, try objdir (ala .depend) */
1646 path = str_concat3(objdir, "/", fname);
1647 fd = open(path, O_RDONLY);
1648 if (fd != -1) {
1649 fname = path;
1650 goto found;
1651 }
1652 } else {
1653 fd = open(fname, O_RDONLY);
1654 if (fd != -1)
1655 goto found;
1656 }
1657 /* look in -I and system include directories. */
1658 name = Dir_FindFile(fname, parseIncPath);
1659 if (name == NULL) {
1660 SearchPath *sysInc = Lst_IsEmpty(&sysIncPath->dirs)
1661 ? defSysIncPath : sysIncPath;
1662 name = Dir_FindFile(fname, sysInc);
1663 }
1664 if (name == NULL || (fd = open(name, O_RDONLY)) == -1) {
1665 free(name);
1666 free(path);
1667 return false;
1668 }
1669 fname = name;
1670 /*
1671 * set the MAKEFILE variable desired by System V fans -- the
1672 * placement of the setting here means it gets set to the last
1673 * makefile specified, as it is set by SysV make.
1674 */
1675 found:
1676 if (!doing_depend)
1677 Var_Set(SCOPE_INTERNAL, "MAKEFILE", fname);
1678 Parse_File(fname, fd);
1679 }
1680 free(path);
1681 return true;
1682 }
1683
1684 /*
1685 * Execute the command in cmd, and return its output (only stdout, not
1686 * stderr, possibly empty). In the output, replace newlines with spaces.
1687 */
1688 char *
1689 Cmd_Exec(const char *cmd, char **error)
1690 {
1691 const char *args[4]; /* Arguments for invoking the shell */
1692 int pipefds[2];
1693 int cpid; /* Child PID */
1694 int pid; /* PID from wait() */
1695 int status; /* command exit status */
1696 Buffer buf; /* buffer to store the result */
1697 ssize_t bytes_read;
1698 char *output;
1699 char *cp;
1700 int saved_errno;
1701
1702 if (shellName == NULL)
1703 Shell_Init();
1704
1705 args[0] = shellName;
1706 args[1] = "-c";
1707 args[2] = cmd;
1708 args[3] = NULL;
1709 DEBUG1(VAR, "Capturing the output of command \"%s\"\n", cmd);
1710
1711 if (pipe(pipefds) == -1) {
1712 *error = str_concat3(
1713 "Couldn't create pipe for \"", cmd, "\"");
1714 return bmake_strdup("");
1715 }
1716
1717 Var_ReexportVars();
1718
1719 switch (cpid = vfork()) {
1720 case 0:
1721 (void)close(pipefds[0]);
1722 (void)dup2(pipefds[1], STDOUT_FILENO);
1723 (void)close(pipefds[1]);
1724
1725 (void)execv(shellPath, UNCONST(args));
1726 _exit(1);
1727 /* NOTREACHED */
1728
1729 case -1:
1730 *error = str_concat3("Couldn't exec \"", cmd, "\"");
1731 return bmake_strdup("");
1732 }
1733
1734 (void)close(pipefds[1]); /* No need for the writing half */
1735
1736 saved_errno = 0;
1737 Buf_Init(&buf);
1738
1739 do {
1740 char result[BUFSIZ];
1741 bytes_read = read(pipefds[0], result, sizeof result);
1742 if (bytes_read > 0)
1743 Buf_AddBytes(&buf, result, (size_t)bytes_read);
1744 } while (bytes_read > 0 || (bytes_read == -1 && errno == EINTR));
1745 if (bytes_read == -1)
1746 saved_errno = errno;
1747
1748 (void)close(pipefds[0]); /* Close the input side of the pipe. */
1749
1750 while ((pid = waitpid(cpid, &status, 0)) != cpid && pid >= 0)
1751 JobReapChild(pid, status, false);
1752
1753 if (Buf_EndsWith(&buf, '\n'))
1754 buf.data[buf.len - 1] = '\0';
1755
1756 output = Buf_DoneData(&buf);
1757 for (cp = output; *cp != '\0'; cp++)
1758 if (*cp == '\n')
1759 *cp = ' ';
1760
1761 if (WIFSIGNALED(status))
1762 *error = str_concat3("\"", cmd, "\" exited on a signal");
1763 else if (WEXITSTATUS(status) != 0)
1764 *error = str_concat3(
1765 "\"", cmd, "\" returned non-zero status");
1766 else if (saved_errno != 0)
1767 *error = str_concat3(
1768 "Couldn't read shell's output for \"", cmd, "\"");
1769 else
1770 *error = NULL;
1771 return output;
1772 }
1773
1774 /*
1775 * Print a printf-style error message.
1776 *
1777 * In default mode, this error message has no consequences, for compatibility
1778 * reasons, in particular it does not affect the exit status. Only in lint
1779 * mode (-dL) it does.
1780 */
1781 void
1782 Error(const char *fmt, ...)
1783 {
1784 va_list ap;
1785 FILE *f;
1786
1787 f = opts.debug_file;
1788 if (f == stdout)
1789 f = stderr;
1790 (void)fflush(stdout);
1791
1792 for (;;) {
1793 fprintf(f, "%s: ", progname);
1794 va_start(ap, fmt);
1795 (void)vfprintf(f, fmt, ap);
1796 va_end(ap);
1797 (void)fprintf(f, "\n");
1798 (void)fflush(f);
1799 if (f == stderr)
1800 break;
1801 f = stderr;
1802 }
1803 main_errors++;
1804 }
1805
1806 /*
1807 * Wait for any running jobs to finish, then produce an error message,
1808 * finally exit immediately.
1809 *
1810 * Exiting immediately differs from Parse_Error, which exits only after the
1811 * current top-level makefile has been parsed completely.
1812 */
1813 void
1814 Fatal(const char *fmt, ...)
1815 {
1816 va_list ap;
1817
1818 if (jobsRunning)
1819 Job_Wait();
1820
1821 (void)fflush(stdout);
1822 va_start(ap, fmt);
1823 (void)vfprintf(stderr, fmt, ap);
1824 va_end(ap);
1825 (void)fprintf(stderr, "\n");
1826 (void)fflush(stderr);
1827 PrintStackTrace(true);
1828
1829 PrintOnError(NULL, "\n");
1830
1831 if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
1832 Targ_PrintGraph(2);
1833 Trace_Log(MAKEERROR, NULL);
1834 exit(2); /* Not 1 so -q can distinguish error */
1835 }
1836
1837 /*
1838 * Major exception once jobs are being created.
1839 * Kills all jobs, prints a message and exits.
1840 */
1841 void
1842 Punt(const char *fmt, ...)
1843 {
1844 va_list ap;
1845
1846 (void)fflush(stdout);
1847 (void)fprintf(stderr, "%s: ", progname);
1848 va_start(ap, fmt);
1849 (void)vfprintf(stderr, fmt, ap);
1850 va_end(ap);
1851 (void)fprintf(stderr, "\n");
1852 (void)fflush(stderr);
1853
1854 PrintOnError(NULL, "\n");
1855
1856 DieHorribly();
1857 }
1858
1859 /* Exit without giving a message. */
1860 void
1861 DieHorribly(void)
1862 {
1863 if (jobsRunning)
1864 Job_AbortAll();
1865 if (DEBUG(GRAPH2))
1866 Targ_PrintGraph(2);
1867 Trace_Log(MAKEERROR, NULL);
1868 exit(2); /* Not 1 so -q can distinguish error */
1869 }
1870
1871 /*
1872 * Called when aborting due to errors in child shell to signal abnormal exit.
1873 * The program exits.
1874 * Errors is the number of errors encountered in Make_Make.
1875 */
1876 void
1877 Finish(int errs)
1878 {
1879 if (shouldDieQuietly(NULL, -1))
1880 exit(2);
1881 Fatal("%d error%s", errs, errs == 1 ? "" : "s");
1882 }
1883
1884 int
1885 unlink_file(const char *file)
1886 {
1887 struct stat st;
1888
1889 if (lstat(file, &st) == -1)
1890 return -1;
1891
1892 if (S_ISDIR(st.st_mode)) {
1893 /*
1894 * POSIX says for unlink: "The path argument shall not name
1895 * a directory unless [...]".
1896 */
1897 errno = EISDIR;
1898 return -1;
1899 }
1900 return unlink(file);
1901 }
1902
1903 static void
1904 write_all(int fd, const void *data, size_t n)
1905 {
1906 const char *mem = data;
1907
1908 while (n > 0) {
1909 ssize_t written = write(fd, mem, n);
1910 /* XXX: Should this EAGAIN be EINTR? */
1911 if (written == -1 && errno == EAGAIN)
1912 continue;
1913 if (written == -1)
1914 break;
1915 mem += written;
1916 n -= (size_t)written;
1917 }
1918 }
1919
1920 /* Print why exec failed, avoiding stdio. */
1921 void MAKE_ATTR_DEAD
1922 execDie(const char *af, const char *av)
1923 {
1924 Buffer buf;
1925
1926 Buf_Init(&buf);
1927 Buf_AddStr(&buf, progname);
1928 Buf_AddStr(&buf, ": ");
1929 Buf_AddStr(&buf, af);
1930 Buf_AddStr(&buf, "(");
1931 Buf_AddStr(&buf, av);
1932 Buf_AddStr(&buf, ") failed (");
1933 Buf_AddStr(&buf, strerror(errno));
1934 Buf_AddStr(&buf, ")\n");
1935
1936 write_all(STDERR_FILENO, buf.data, buf.len);
1937
1938 Buf_Done(&buf);
1939 _exit(1);
1940 }
1941
1942 static void
1943 purge_relative_cached_realpaths(void)
1944 {
1945 HashEntry *he, *nhe;
1946 HashIter hi;
1947
1948 HashIter_Init(&hi, &cached_realpaths);
1949 he = HashIter_Next(&hi);
1950 while (he != NULL) {
1951 nhe = HashIter_Next(&hi);
1952 if (he->key[0] != '/') {
1953 DEBUG1(DIR, "cached_realpath: purging %s\n", he->key);
1954 HashTable_DeleteEntry(&cached_realpaths, he);
1955 /*
1956 * XXX: What about the allocated he->value? Either
1957 * free them or document why they cannot be freed.
1958 */
1959 }
1960 he = nhe;
1961 }
1962 }
1963
1964 const char *
1965 cached_realpath(const char *pathname, char *resolved)
1966 {
1967 const char *rp;
1968
1969 if (pathname == NULL || pathname[0] == '\0')
1970 return NULL;
1971
1972 rp = HashTable_FindValue(&cached_realpaths, pathname);
1973 if (rp != NULL) {
1974 /* a hit */
1975 strncpy(resolved, rp, MAXPATHLEN);
1976 resolved[MAXPATHLEN - 1] = '\0';
1977 return resolved;
1978 }
1979
1980 rp = realpath(pathname, resolved);
1981 if (rp != NULL) {
1982 HashTable_Set(&cached_realpaths, pathname, bmake_strdup(rp));
1983 DEBUG2(DIR, "cached_realpath: %s -> %s\n", pathname, rp);
1984 return resolved;
1985 }
1986
1987 /* should we negative-cache? */
1988 return NULL;
1989 }
1990
1991 /*
1992 * Return true if we should die without noise.
1993 * For example our failing child was a sub-make or failure happened elsewhere.
1994 */
1995 bool
1996 shouldDieQuietly(GNode *gn, int bf)
1997 {
1998 static int quietly = -1;
1999
2000 if (quietly < 0) {
2001 if (DEBUG(JOB) ||
2002 !GetBooleanExpr("${.MAKE.DIE_QUIETLY}", true))
2003 quietly = 0;
2004 else if (bf >= 0)
2005 quietly = bf;
2006 else
2007 quietly = (gn != NULL && (gn->type & OP_MAKE)) ? 1 : 0;
2008 }
2009 return quietly != 0;
2010 }
2011
2012 static void
2013 SetErrorVars(GNode *gn)
2014 {
2015 StringListNode *ln;
2016
2017 /*
2018 * We can print this even if there is no .ERROR target.
2019 */
2020 Global_Set(".ERROR_TARGET", gn->name);
2021 Global_Delete(".ERROR_CMD");
2022
2023 for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
2024 const char *cmd = ln->datum;
2025
2026 if (cmd == NULL)
2027 break;
2028 Global_Append(".ERROR_CMD", cmd);
2029 }
2030 }
2031
2032 /*
2033 * Print some helpful information in case of an error.
2034 * The caller should exit soon after calling this function.
2035 */
2036 void
2037 PrintOnError(GNode *gn, const char *msg)
2038 {
2039 static GNode *errorNode = NULL;
2040
2041 if (DEBUG(HASH)) {
2042 Targ_Stats();
2043 Var_Stats();
2044 }
2045
2046 if (errorNode != NULL)
2047 return; /* we've been here! */
2048
2049 printf("%s%s: stopped in %s\n", msg, progname, curdir);
2050
2051 /* we generally want to keep quiet if a sub-make died */
2052 if (shouldDieQuietly(gn, -1))
2053 return;
2054
2055 if (gn != NULL)
2056 SetErrorVars(gn);
2057
2058 {
2059 char *errorVarsValues;
2060 (void)Var_Subst("${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
2061 SCOPE_GLOBAL, VARE_WANTRES, &errorVarsValues);
2062 /* TODO: handle errors */
2063 printf("%s", errorVarsValues);
2064 free(errorVarsValues);
2065 }
2066
2067 fflush(stdout);
2068
2069 /*
2070 * Finally, see if there is a .ERROR target, and run it if so.
2071 */
2072 errorNode = Targ_FindNode(".ERROR");
2073 if (errorNode != NULL) {
2074 errorNode->type |= OP_SPECIAL;
2075 Compat_Make(errorNode, errorNode);
2076 }
2077 }
2078
2079 void
2080 Main_ExportMAKEFLAGS(bool first)
2081 {
2082 static bool once = true;
2083 char *flags;
2084
2085 if (once != first)
2086 return;
2087 once = false;
2088
2089 (void)Var_Subst(
2090 "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
2091 SCOPE_CMDLINE, VARE_WANTRES, &flags);
2092 /* TODO: handle errors */
2093 if (flags[0] != '\0') {
2094 #ifdef POSIX
2095 setenv("MAKEFLAGS", flags, 1);
2096 #else
2097 setenv("MAKE", flags, 1);
2098 #endif
2099 }
2100 }
2101
2102 char *
2103 getTmpdir(void)
2104 {
2105 static char *tmpdir = NULL;
2106 struct stat st;
2107
2108 if (tmpdir != NULL)
2109 return tmpdir;
2110
2111 /* Honor $TMPDIR if it is valid, strip a trailing '/'. */
2112 (void)Var_Subst("${TMPDIR:tA:U" _PATH_TMP ":S,/$,,W}/",
2113 SCOPE_GLOBAL, VARE_WANTRES, &tmpdir);
2114 /* TODO: handle errors */
2115
2116 if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
2117 free(tmpdir);
2118 tmpdir = bmake_strdup(_PATH_TMP);
2119 }
2120 return tmpdir;
2121 }
2122
2123 /*
2124 * Create and open a temp file using "pattern".
2125 * If out_fname is provided, set it to a copy of the filename created.
2126 * Otherwise unlink the file once open.
2127 */
2128 int
2129 mkTempFile(const char *pattern, char *tfile, size_t tfile_sz)
2130 {
2131 static char *tmpdir = NULL;
2132 char tbuf[MAXPATHLEN];
2133 int fd;
2134
2135 if (pattern == NULL)
2136 pattern = TMPPAT;
2137 if (tmpdir == NULL)
2138 tmpdir = getTmpdir();
2139 if (tfile == NULL) {
2140 tfile = tbuf;
2141 tfile_sz = sizeof tbuf;
2142 }
2143
2144 if (pattern[0] == '/')
2145 snprintf(tfile, tfile_sz, "%s", pattern);
2146 else
2147 snprintf(tfile, tfile_sz, "%s%s", tmpdir, pattern);
2148
2149 if ((fd = mkstemp(tfile)) < 0)
2150 Punt("Could not create temporary file %s: %s", tfile,
2151 strerror(errno));
2152 if (tfile == tbuf)
2153 unlink(tfile); /* we just want the descriptor */
2154
2155 return fd;
2156 }
2157
2158 /*
2159 * Convert a string representation of a boolean into a boolean value.
2160 * Anything that looks like "No", "False", "Off", "0" etc. is false,
2161 * the empty string is the fallback, everything else is true.
2162 */
2163 bool
2164 ParseBoolean(const char *s, bool fallback)
2165 {
2166 char ch = ch_tolower(s[0]);
2167 if (ch == '\0')
2168 return fallback;
2169 if (ch == '0' || ch == 'f' || ch == 'n')
2170 return false;
2171 if (ch == 'o')
2172 return ch_tolower(s[1]) != 'f';
2173 return true;
2174 }
2175