Annotation of src/usr.bin/make/main.c, Revision 1.77
1.46 espie 1: /* $OpenPackages$ */
1.71 espie 2: /* $OpenBSD: main.c,v 1.70 2007/07/08 17:44:20 espie Exp $ */
1.12 millert 3: /* $NetBSD: main.c,v 1.34 1997/03/24 20:56:36 gwr Exp $ */
1.1 deraadt 4:
5: /*
1.9 millert 6: * Copyright (c) 1988, 1989, 1990, 1993
7: * The Regents of the University of California. All rights reserved.
1.1 deraadt 8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.62 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
1.49 espie 39: #include <sys/param.h>
1.1 deraadt 40: #include <sys/types.h>
41: #include <sys/stat.h>
1.12 millert 42: #ifndef MAKE_BOOTSTRAP
1.1 deraadt 43: #include <sys/utsname.h>
1.9 millert 44: #endif
1.1 deraadt 45: #include <errno.h>
46: #include <stdio.h>
1.49 espie 47: #include <stdlib.h>
1.48 espie 48: #include <string.h>
49: #include <unistd.h>
50: #include "config.h"
51: #include "defines.h"
52: #include "var.h"
53: #include "parse.h"
54: #include "parsevar.h"
1.1 deraadt 55: #include "dir.h"
1.48 espie 56: #include "error.h"
57: #include "pathnames.h"
58: #include "init.h"
1.1 deraadt 59: #include "job.h"
1.48 espie 60: #include "compat.h"
61: #include "targ.h"
62: #include "suff.h"
63: #include "str.h"
64: #include "main.h"
65: #include "lst.h"
66: #include "memory.h"
67: #include "make.h"
1.1 deraadt 68:
1.53 espie 69: #ifndef PATH_MAX
70: # ifdef MAXPATHLEN
71: # define PATH_MAX (MAXPATHLEN+1)
72: # else
73: # define PATH_MAX 1024
74: # endif
75: #endif
76:
1.46 espie 77: #ifndef DEFMAXLOCAL
78: #define DEFMAXLOCAL DEFMAXJOBS
1.4 niklas 79: #endif /* DEFMAXLOCAL */
1.1 deraadt 80:
1.46 espie 81: #define MAKEFLAGS ".MAKEFLAGS"
1.1 deraadt 82:
1.48 espie 83: static LIST to_create; /* Targets to be made */
84: Lst create = &to_create;
1.1 deraadt 85: GNode *DEFAULT; /* .DEFAULT node */
1.48 espie 86: bool allPrecious; /* .PRECIOUS given on line by itself */
1.1 deraadt 87:
1.48 espie 88: static bool noBuiltins; /* -r flag */
1.33 espie 89: static LIST makefiles; /* ordered list of makefiles to read */
1.48 espie 90: static LIST varstoprint; /* list of variables to print */
1.2 deraadt 91: int maxJobs; /* -j argument */
1.1 deraadt 92: static int maxLocal; /* -L argument */
1.48 espie 93: bool compatMake; /* -B argument */
1.63 espie 94: int debug; /* -d flag */
1.48 espie 95: bool noExecute; /* -n flag */
96: bool keepgoing; /* -k flag */
97: bool queryFlag; /* -q flag */
98: bool touchFlag; /* -t flag */
99: bool usePipes; /* !-P flag */
100: bool ignoreErrors; /* -i flag */
101: bool beSilent; /* -s flag */
1.1 deraadt 102:
1.75 espie 103: struct dirs {
104: char *current;
105: char *object;
106: };
107:
108: static void MainParseArgs(int, char **);
109: static void add_dirpath(Lst, const char *);
110: static void usage(void);
111: static void posixParseOptLetter(int);
112: static void record_option(int, const char *);
113:
114: static char *figure_out_MACHINE(void);
115: static char *figure_out_MACHINE_ARCH(void);
116: static void no_fd_limits(void);
117:
118: static char *chdir_verify_path(const char *, struct dirs *);
119: static char *concat_verify(const char *, const char *, char, struct dirs *);
120: static char *figure_out_CURDIR(void);
121: static void setup_CURDIR_OBJDIR(struct dirs *, const char *);
122:
123: static void setup_VPATH(void);
124:
125: static void read_all_make_rules(bool, Lst, struct dirs *);
126: static void read_makefile_list(Lst, struct dirs *);
127: static int ReadMakefile(void *, void *);
1.1 deraadt 128:
1.46 espie 129:
1.64 espie 130: static void record_option(int c, const char *arg)
1.48 espie 131: {
132: char opt[3];
133:
1.75 espie 134: opt[0] = '-';
135: opt[1] = c;
136: opt[2] = '\0';
1.76 espie 137: Var_Append(MAKEFLAGS, opt);
1.75 espie 138: if (arg != NULL)
1.76 espie 139: Var_Append(MAKEFLAGS, arg);
1.48 espie 140: }
141:
1.39 espie 142: static void
1.64 espie 143: posixParseOptLetter(int c)
1.39 espie 144: {
145: switch(c) {
146: case 'B':
1.48 espie 147: compatMake = true;
148: return; /* XXX don't pass to submakes. */
1.39 espie 149: case 'P':
1.48 espie 150: usePipes = false;
1.39 espie 151: break;
152: case 'S':
1.48 espie 153: keepgoing = false;
1.39 espie 154: break;
155: case 'e':
1.70 espie 156: Var_setCheckEnvFirst(true);
1.39 espie 157: break;
158: case 'i':
1.48 espie 159: ignoreErrors = true;
1.39 espie 160: break;
161: case 'k':
1.48 espie 162: keepgoing = true;
1.39 espie 163: break;
164: case 'n':
1.48 espie 165: noExecute = true;
1.39 espie 166: break;
167: case 'q':
1.48 espie 168: queryFlag = true;
1.39 espie 169: /* Kind of nonsensical, wot? */
170: break;
171: case 'r':
1.48 espie 172: noBuiltins = true;
1.39 espie 173: break;
174: case 's':
1.48 espie 175: beSilent = true;
1.39 espie 176: break;
177: case 't':
1.48 espie 178: touchFlag = true;
1.39 espie 179: break;
180: default:
181: case '?':
182: usage();
183: }
1.48 espie 184: record_option(c, NULL);
1.39 espie 185: }
186:
1.1 deraadt 187: /*-
188: * MainParseArgs --
189: * Parse a given argument vector. Called from main() and from
190: * Main_ParseArgLine() when the .MAKEFLAGS target is used.
191: *
192: * XXX: Deal with command line overriding .MAKEFLAGS in makefile
193: *
194: * Side Effects:
195: * Various global and local flags will be set depending on the flags
196: * given
197: */
198: static void
1.64 espie 199: MainParseArgs(int argc, char **argv)
1.1 deraadt 200: {
1.61 millert 201: int c, optend;
1.2 deraadt 202: int forceJobs = 0;
1.1 deraadt 203:
1.58 millert 204: #define OPTFLAGS "BD:I:PSV:d:ef:ij:km:nqrst"
205: #define OPTLETTERS "BPSiknqrst"
206:
1.1 deraadt 207: optind = 1; /* since we're called more than once */
1.58 millert 208: optreset = 1;
1.61 millert 209: optend = 0;
1.58 millert 210: while (optind < argc) {
1.61 millert 211: if (!optend && argv[optind][0] == '-') {
212: if (argv[optind][1] == '\0')
213: optind++; /* ignore "-" */
214: else if (argv[optind][1] == '-' &&
215: argv[optind][2] == '\0') {
216: optind++; /* ignore "--" */
217: optend++; /* "--" denotes end of flags */
218: }
219: }
220: c = optend ? -1 : getopt(argc, argv, OPTFLAGS);
221: switch (c) {
1.1 deraadt 222: case 'D':
1.76 espie 223: Var_Set(optarg, "1");
1.48 espie 224: record_option(c, optarg);
1.1 deraadt 225: break;
226: case 'I':
227: Parse_AddIncludeDir(optarg);
1.48 espie 228: record_option(c, optarg);
1.1 deraadt 229: break;
1.9 millert 230: case 'V':
1.48 espie 231: Lst_AtEnd(&varstoprint, optarg);
232: record_option(c, optarg);
1.9 millert 233: break;
1.1 deraadt 234: case 'd': {
235: char *modules = optarg;
236:
237: for (; *modules; ++modules)
238: switch (*modules) {
239: case 'A':
240: debug = ~0;
241: break;
242: case 'a':
243: debug |= DEBUG_ARCH;
244: break;
245: case 'c':
246: debug |= DEBUG_COND;
247: break;
248: case 'd':
249: debug |= DEBUG_DIR;
250: break;
251: case 'f':
252: debug |= DEBUG_FOR;
253: break;
254: case 'g':
255: if (modules[1] == '1') {
256: debug |= DEBUG_GRAPH1;
257: ++modules;
258: }
259: else if (modules[1] == '2') {
260: debug |= DEBUG_GRAPH2;
261: ++modules;
262: }
263: break;
264: case 'j':
265: debug |= DEBUG_JOB;
266: break;
1.46 espie 267: case 'l':
268: debug |= DEBUG_LOUD;
269: break;
1.1 deraadt 270: case 'm':
271: debug |= DEBUG_MAKE;
272: break;
273: case 's':
274: debug |= DEBUG_SUFF;
275: break;
276: case 't':
277: debug |= DEBUG_TARG;
278: break;
279: case 'v':
280: debug |= DEBUG_VAR;
281: break;
282: default:
283: (void)fprintf(stderr,
1.67 jmc 284: "make: illegal argument to -d option -- %c\n",
1.1 deraadt 285: *modules);
286: usage();
287: }
1.48 espie 288: record_option(c, optarg);
1.1 deraadt 289: break;
290: }
291: case 'f':
1.33 espie 292: Lst_AtEnd(&makefiles, optarg);
1.1 deraadt 293: break;
1.15 espie 294: case 'j': {
295: char *endptr;
296:
1.48 espie 297: forceJobs = true;
1.15 espie 298: maxJobs = strtol(optarg, &endptr, 0);
299: if (endptr == optarg) {
300: fprintf(stderr,
301: "make: illegal argument to -j option -- %s -- not a number\n",
302: optarg);
303: usage();
304: }
1.2 deraadt 305: maxLocal = maxJobs;
1.48 espie 306: record_option(c, optarg);
1.1 deraadt 307: break;
1.15 espie 308: }
1.5 niklas 309: case 'm':
1.48 espie 310: Dir_AddDir(sysIncPath, optarg);
311: record_option(c, optarg);
1.5 niklas 312: break;
1.58 millert 313: case -1:
314: /* Check for variable assignments and targets. */
1.59 millert 315: if (argv[optind] != NULL &&
1.70 espie 316: !Parse_CmdlineVar(argv[optind])) {
1.58 millert 317: if (!*argv[optind])
318: Punt("illegal (null) argument.");
1.61 millert 319: Lst_AtEnd(create, estrdup(argv[optind]));
1.58 millert 320: }
321: optind++; /* skip over non-option */
322: break;
1.1 deraadt 323: default:
1.39 espie 324: posixParseOptLetter(c);
1.1 deraadt 325: }
326: }
327:
1.2 deraadt 328: /*
329: * Be compatible if user did not specify -j and did not explicitly
1.48 espie 330: * turn compatibility on
1.2 deraadt 331: */
332: if (!compatMake && !forceJobs)
1.48 espie 333: compatMake = true;
1.1 deraadt 334: }
335:
336: /*-
337: * Main_ParseArgLine --
1.46 espie 338: * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
1.1 deraadt 339: * is encountered and by main() when reading the .MAKEFLAGS envariable.
340: * Takes a line of arguments and breaks it into its
1.46 espie 341: * component words and passes those words and the number of them to the
1.1 deraadt 342: * MainParseArgs function.
343: * The line should have all its leading whitespace removed.
344: *
345: * Side Effects:
346: * Only those that come from the various arguments.
347: */
348: void
1.64 espie 349: Main_ParseArgLine(const char *line) /* Line to fracture */
1.1 deraadt 350: {
351: char **argv; /* Manufactured argument vector */
352: int argc; /* Number of arguments in argv */
1.14 espie 353: char *args; /* Space used by the args */
1.19 espie 354: char *buf;
1.39 espie 355: char *argv0;
1.48 espie 356: const char *s;
1.60 espie 357: size_t len;
1.1 deraadt 358:
1.46 espie 359:
1.1 deraadt 360: if (line == NULL)
361: return;
362: for (; *line == ' '; ++line)
363: continue;
364: if (!*line)
365: return;
366:
1.39 espie 367: /* POSIX rule: MAKEFLAGS can hold a set of option letters without
368: * any blanks or dashes. */
369: for (s = line;; s++) {
370: if (*s == '\0') {
1.46 espie 371: while (line != s)
1.39 espie 372: posixParseOptLetter(*line++);
373: return;
1.46 espie 374: }
1.39 espie 375: if (strchr(OPTLETTERS, *s) == NULL)
376: break;
377: }
1.46 espie 378: argv0 = Var_Value(".MAKE");
1.60 espie 379: len = strlen(line) + strlen(argv0) + 2;
380: buf = emalloc(len);
381: (void)snprintf(buf, len, "%s %s", argv0, line);
1.14 espie 382:
1.46 espie 383: argv = brk_string(buf, &argc, &args);
1.14 espie 384: free(buf);
1.1 deraadt 385: MainParseArgs(argc, argv);
1.14 espie 386:
387: free(args);
388: free(argv);
1.1 deraadt 389: }
390:
1.75 espie 391: /* Add a :-separated path to a Lst of directories. */
392: static void
393: add_dirpath(Lst l, const char *n)
1.9 millert 394: {
1.75 espie 395: const char *start;
396: const char *cp;
1.9 millert 397:
1.75 espie 398: for (start = n;;) {
399: for (cp = start; *cp != '\0' && *cp != ':';)
400: cp++;
401: Dir_AddDiri(l, start, cp);
402: if (*cp == '\0')
403: break;
404: else
405: start= cp+1;
1.9 millert 406: }
407: }
408:
1.75 espie 409: /*
410: * Get the name of this type of MACHINE from utsname so we can share an
411: * executable for similar machines. (i.e. m68k: amiga hp300, mac68k, sun3, ...)
412: *
413: * Note that both MACHINE and MACHINE_ARCH are decided at
414: * run-time.
415: */
416: static char *
417: figure_out_MACHINE()
1.42 espie 418: {
1.75 espie 419: char *r = getenv("MACHINE");
420: if (r == NULL) {
421: #ifndef MAKE_BOOTSTRAP
422: static struct utsname utsname;
1.42 espie 423:
1.75 espie 424: if (uname(&utsname) == -1) {
425: perror("make: uname");
426: exit(2);
427: }
428: r = utsname.machine;
429: #else
430: r = MACHINE;
431: #endif
432: }
433: return r;
1.42 espie 434: }
435:
1.75 espie 436: static char *
437: figure_out_MACHINE_ARCH()
1.1 deraadt 438: {
1.75 espie 439: char *r = getenv("MACHINE_ARCH");
440: if (r == NULL) {
441: #ifndef MACHINE_ARCH
442: r = "unknown"; /* XXX: no uname -p yet */
443: #else
444: r = MACHINE_ARCH;
445: #endif
446: }
447: return r;
448: }
1.46 espie 449:
1.75 espie 450: /* get rid of resource limit on file descriptors */
451: static void
452: no_fd_limits()
453: {
1.2 deraadt 454: #ifdef RLIMIT_NOFILE
1.75 espie 455: struct rlimit rl;
456: if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
457: rl.rlim_cur != rl.rlim_max) {
458: rl.rlim_cur = rl.rlim_max;
459: (void)setrlimit(RLIMIT_NOFILE, &rl);
1.2 deraadt 460: }
461: #endif
1.75 espie 462: }
463:
464: static char *
465: figure_out_CURDIR()
466: {
467: char *dir, *cwd;
468: struct stat sa, sb;
469:
470: /* curdir is cwd... */
471: cwd = dogetcwd();
472: if (cwd == NULL) {
1.1 deraadt 473: (void)fprintf(stderr, "make: %s.\n", strerror(errno));
474: exit(2);
475: }
476:
1.75 espie 477: if (stat(cwd, &sa) == -1) {
478: (void)fprintf(stderr, "make: %s: %s.\n", cwd, strerror(errno));
479: exit(2);
1.1 deraadt 480: }
481:
1.75 espie 482: /* ...but we can use the alias $PWD if we can prove it is the same
483: * directory */
484: if ((dir = getenv("PWD")) != NULL) {
485: if (stat(dir, &sb) == 0 && sa.st_ino == sb.st_ino &&
486: sa.st_dev == sb.st_dev)
487: free(cwd);
488: return estrdup(dir);
1.1 deraadt 489: }
490:
1.75 espie 491: return cwd;
492: }
493:
494: static char *
495: chdir_verify_path(const char *path, struct dirs *d)
496: {
497: struct stat sb;
1.9 millert 498:
1.75 espie 499: if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
500: if (chdir(path)) {
501: (void)fprintf(stderr, "make warning: %s: %s.\n",
502: path, strerror(errno));
503: return NULL;
504: } else {
505: if (path[0] != '/')
506: return Str_concat(d->current, path, '/');
507: else
508: return estrdup(path);
509: }
1.1 deraadt 510: }
511:
1.75 espie 512: return NULL;
513: }
514:
515: static char *
516: concat_verify(const char *p1, const char *p2, char c, struct dirs *d)
517: {
518: char *tmp = Str_concat(p1, p2, c);
519: char *result = chdir_verify_path(tmp, d);
520: free(tmp);
521: return result;
522: }
523:
524: static void
525: setup_CURDIR_OBJDIR(struct dirs *d, const char *machine)
526: {
527: char *path, *prefix;
1.12 millert 528:
1.75 espie 529: d->current = figure_out_CURDIR();
530: d->object = NULL;
1.1 deraadt 531: /*
1.9 millert 532: * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
533: * exists, change into it and build there. (If a .${MACHINE} suffix
534: * exists, use that directory instead).
535: * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
536: * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
537: * If all fails, use the current directory to build.
538: *
539: * Once things are initted,
540: * have to add the original directory to the search path,
1.66 jolan 541: * and modify the paths for the Makefiles appropriately. The
1.1 deraadt 542: * current directory is also placed as a variable for make scripts.
543: */
1.75 espie 544: if ((prefix = getenv("MAKEOBJDIRPREFIX")) != NULL) {
545: d->object = concat_verify(prefix, d->current, 0, d);
546: } else if ((path = getenv("MAKEOBJDIR")) != NULL) {
547: d->object = chdir_verify_path(path, d);
548: } else {
549: path = _PATH_OBJDIR;
550: prefix = _PATH_OBJDIRPREFIX;
551: d->object = concat_verify(path, machine, '.', d);
552: if (!d->object)
553: d->object=chdir_verify_path(path, d);
554: if (!d->object)
555: d->object = concat_verify(prefix, d->current, 0, d);
556: }
557: if (d->object == NULL)
558: d->object = d->current;
559: }
560:
561: #ifdef CLEANUP
562: static void
563: free_CURDIR_OBJDIR(struct dirs *d)
564: {
565: if (d->object != d->current)
566: free(d->object);
567: free(d->current);
568: }
569: #endif
570:
571:
572: /*
573: * if the VPATH variable is defined, add its contents to the search path.
574: * Uses the same format as the PATH env variable, i.e.,
575: * <directory>:<directory>:<directory>...
576: */
577: static void
578: setup_VPATH()
579: {
580: if (Var_Value("VPATH") != NULL) {
581: char *vpath;
582:
583: vpath = Var_Subst("${VPATH}", NULL, false);
584: add_dirpath(dirSearchPath, vpath);
585: (void)free(vpath);
1.1 deraadt 586: }
1.75 espie 587: }
588:
589: static void
590: read_makefile_list(Lst mk, struct dirs *d)
591: {
592: LstNode ln;
593: ln = Lst_Find(mk, ReadMakefile, d);
594: if (ln != NULL)
595: Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
596: }
597:
598: static void
599: read_all_make_rules(bool noBuiltins, Lst makefiles, struct dirs *d)
600: {
601: /*
602: * Read in the built-in rules first, followed by the specified
603: * makefile(s), or the default BSDmakefile, Makefile or
604: * makefile, in that order.
605: */
606: if (!noBuiltins) {
607: LIST sysMkPath; /* Path of sys.mk */
608:
609: Lst_Init(&sysMkPath);
610: Dir_Expand(_PATH_DEFSYSMK, sysIncPath, &sysMkPath);
611: if (Lst_IsEmpty(&sysMkPath))
612: Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
613:
614: read_makefile_list(&sysMkPath, d);
615: #ifdef CLEANUP
616: Lst_Destroy(&sysMkPath, (SimpleProc)free);
617: #endif
1.1 deraadt 618: }
619:
1.75 espie 620: if (!Lst_IsEmpty(makefiles)) {
621: read_makefile_list(makefiles, d);
622: } else if (!ReadMakefile("BSDmakefile", d))
623: if (!ReadMakefile("makefile", d))
624: (void)ReadMakefile("Makefile", d);
625:
626: /* Always read a .depend file, if it exists. */
627: (void)ReadMakefile(".depend", d);
628: }
629:
630:
631: int main(int, char **);
632: /*-
633: * main --
634: * The main function, for obvious reasons. Initializes variables
635: * and a few modules, then parses the arguments give it in the
636: * environment and on the command line. Reads the system makefile
637: * followed by either Makefile, makefile or the file given by the
638: * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
639: * flags it has received by then uses either the Make or the Compat
640: * module to create the initial list of targets.
641: *
642: * Results:
643: * If -q was given, exits -1 if anything was out-of-date. Else it exits
644: * 0.
645: *
646: * Side Effects:
647: * The program exits when done. Targets are created. etc. etc. etc.
648: */
649: int
650: main(int argc, char **argv)
651: {
652: static LIST targs; /* target nodes to create */
653: bool outOfDate = true; /* false if all targets up to date */
654: char *machine = figure_out_MACHINE();
655: char *machine_arch = figure_out_MACHINE_ARCH();
656: const char *syspath = _PATH_DEFSYSPATH;
657: char *p;
658: static struct dirs d;
659:
660: no_fd_limits();
661: setup_CURDIR_OBJDIR(&d, machine);
662:
663: esetenv("PWD", d.object);
1.30 espie 664: unsetenv("CDPATH");
1.1 deraadt 665:
1.56 espie 666: Static_Lst_Init(create);
667: Static_Lst_Init(&makefiles);
668: Static_Lst_Init(&varstoprint);
669: Static_Lst_Init(&targs);
1.52 espie 670:
1.48 espie 671: beSilent = false; /* Print commands as executed */
672: ignoreErrors = false; /* Pay attention to non-zero returns */
673: noExecute = false; /* Execute all commands */
674: keepgoing = false; /* Stop on error */
675: allPrecious = false; /* Remove targets when interrupted */
676: queryFlag = false; /* This is not just a check-run */
677: noBuiltins = false; /* Read the built-in rules */
678: touchFlag = false; /* Actually update targets */
679: usePipes = true; /* Catch child output in pipes */
1.1 deraadt 680: debug = 0; /* No debug verbosity, please. */
681:
1.46 espie 682: maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */
1.2 deraadt 683: maxJobs = maxLocal;
1.48 espie 684: compatMake = false; /* No compat mode */
1.9 millert 685:
1.1 deraadt 686:
687: /*
1.48 espie 688: * Initialize all external modules.
1.1 deraadt 689: */
1.48 espie 690: Init();
691:
1.75 espie 692: if (d.object != d.current)
693: Dir_AddDir(dirSearchPath, d.current);
1.76 espie 694: Var_Set(".CURDIR", d.current);
695: Var_Set(".OBJDIR", d.object);
1.1 deraadt 696:
697: /*
698: * Initialize various variables.
699: * MAKE also gets this name, for compatibility
700: * .MAKEFLAGS gets set to the empty string just in case.
701: * MFLAGS also gets initialized empty, for compatibility.
702: */
1.76 espie 703: Var_Set("MAKE", argv[0]);
704: Var_Set(".MAKE", argv[0]);
705: Var_Set(MAKEFLAGS, "");
706: Var_Set("MFLAGS", "");
707: Var_Set("MACHINE", machine);
708: Var_Set("MACHINE_ARCH", machine_arch);
1.1 deraadt 709:
710: /*
1.46 espie 711: * First snag any flags out of the MAKEFLAGS environment variable.
1.1 deraadt 712: */
713: Main_ParseArgLine(getenv("MAKEFLAGS"));
1.9 millert 714:
1.1 deraadt 715: MainParseArgs(argc, argv);
1.30 espie 716:
1.46 espie 717: /* And set up everything for sub-makes */
1.39 espie 718: Var_AddCmdline(MAKEFLAGS);
1.46 espie 719:
1.1 deraadt 720:
1.25 espie 721: DEFAULT = NULL;
1.1 deraadt 722:
723: /*
724: * Set up the .TARGETS variable to contain the list of targets to be
725: * created. If none specified, make the variable empty -- the parser
726: * will fill the thing in with the default or .MAIN target.
727: */
1.48 espie 728: if (!Lst_IsEmpty(create)) {
1.1 deraadt 729: LstNode ln;
730:
1.48 espie 731: for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) {
1.1 deraadt 732: char *name = (char *)Lst_Datum(ln);
733:
1.76 espie 734: Var_Append(".TARGETS", name);
1.1 deraadt 735: }
736: } else
1.76 espie 737: Var_Set(".TARGETS", "");
1.1 deraadt 738:
1.5 niklas 739:
740: /*
741: * If no user-supplied system path was given (through the -m option)
742: * add the directories from the DEFSYSPATH (more than one may be given
743: * as dir1:...:dirn) to the system include path.
744: */
1.48 espie 745: if (Lst_IsEmpty(sysIncPath))
746: add_dirpath(sysIncPath, syspath);
1.5 niklas 747:
1.75 espie 748: read_all_make_rules(noBuiltins, &makefiles, &d);
1.1 deraadt 749:
1.76 espie 750: Var_Append("MFLAGS", Var_Value(MAKEFLAGS));
1.1 deraadt 751:
1.46 espie 752: /* Install all the flags into the MAKEFLAGS env variable. */
753: if (((p = Var_Value(MAKEFLAGS)) != NULL) && *p)
1.44 espie 754: esetenv("MAKEFLAGS", p);
1.1 deraadt 755:
1.75 espie 756: setup_VPATH();
1.1 deraadt 757:
1.46 espie 758: /* Now that all search paths have been read for suffixes et al, it's
759: * time to add the default search path to their lists... */
1.1 deraadt 760: Suff_DoPaths();
761:
1.46 espie 762: /* Print the initial graph, if the user requested it. */
1.1 deraadt 763: if (DEBUG(GRAPH1))
764: Targ_PrintGraph(1);
765:
1.46 espie 766: /* Print the values of any variables requested by the user. */
1.48 espie 767: if (!Lst_IsEmpty(&varstoprint)) {
1.75 espie 768: LstNode ln;
1.9 millert 769:
1.75 espie 770: for (ln = Lst_First(&varstoprint); ln != NULL;
771: ln = Lst_Adv(ln)) {
772: char *value = Var_Value((char *)Lst_Datum(ln));
1.9 millert 773:
1.75 espie 774: printf("%s\n", value ? value : "");
775: }
1.48 espie 776: } else {
1.75 espie 777: /* Have now read the entire graph and need to make a list
778: * of targets to create. If none was given on the command
779: * line, we consult the parsing module to find the main
780: * target(s) to create. */
781: if (Lst_IsEmpty(create))
782: Parse_MainName(&targs);
783: else
784: Targ_FindList(&targs, create);
785:
786: if (compatMake)
787: /* Compat_Init will take care of creating all the
788: * targets as well as initializing the module. */
789: Compat_Run(&targs);
790: else {
791: /* Initialize job module before traversing the graph,
792: * now that any .BEGIN and .END targets have been
793: * read. This is done only if the -q flag wasn't given
794: * (to prevent the .BEGIN from being executed should
795: * it exist). */
796: if (!queryFlag) {
797: if (maxLocal == -1)
798: maxLocal = maxJobs;
799: Job_Init(maxJobs, maxLocal);
800: }
801:
802: /* Traverse the graph, checking on all the targets. */
803: outOfDate = Make_Run(&targs);
1.1 deraadt 804: }
1.9 millert 805: }
806:
1.52 espie 807: #ifdef CLEANUP
1.34 espie 808: Lst_Destroy(&targs, NOFREE);
1.48 espie 809: Lst_Destroy(&varstoprint, NOFREE);
1.33 espie 810: Lst_Destroy(&makefiles, NOFREE);
1.48 espie 811: Lst_Destroy(create, (SimpleProc)free);
1.52 espie 812: #endif
1.1 deraadt 813:
814: /* print the graph now it's been processed if the user requested it */
815: if (DEBUG(GRAPH2))
816: Targ_PrintGraph(2);
817:
1.53 espie 818: #ifdef CLEANUP
1.75 espie 819: free_CURDIR_OBJDIR(&d);
1.68 espie 820: End();
1.53 espie 821: #endif
1.1 deraadt 822: if (queryFlag && outOfDate)
1.46 espie 823: return 1;
1.1 deraadt 824: else
1.46 espie 825: return 0;
1.1 deraadt 826: }
827:
828: /*-
829: * ReadMakefile --
830: * Open and parse the given makefile.
831: *
832: * Results:
1.48 espie 833: * true if ok. false if couldn't open file.
1.1 deraadt 834: *
835: * Side Effects:
836: * lots
837: */
1.48 espie 838: static bool
1.75 espie 839: ReadMakefile(void *p, void *q)
1.1 deraadt 840: {
1.64 espie 841: char *fname = (char *)p; /* makefile to read */
1.75 espie 842: struct dirs *d = (struct dirs *)q;
1.1 deraadt 843: FILE *stream;
1.53 espie 844: char *name;
1.1 deraadt 845:
846: if (!strcmp(fname, "-")) {
1.76 espie 847: Var_Set("MAKEFILE", "");
1.37 espie 848: Parse_File(estrdup("(stdin)"), stdin);
1.1 deraadt 849: } else {
850: if ((stream = fopen(fname, "r")) != NULL)
851: goto found;
852: /* if we've chdir'd, rebuild the path name */
1.75 espie 853: if (d->current != d->object && *fname != '/') {
1.53 espie 854: char *path;
855:
1.75 espie 856: path = Str_concat(d->current, fname, '/');
1.53 espie 857: if ((stream = fopen(path, "r")) == NULL)
1.75 espie 858: free(path);
1.53 espie 859: else {
1.75 espie 860: fname = path;
861: goto found;
1.1 deraadt 862: }
863: }
864: /* look in -I and system include directories. */
1.48 espie 865: name = Dir_FindFile(fname, parseIncPath);
1.1 deraadt 866: if (!name)
1.48 espie 867: name = Dir_FindFile(fname, sysIncPath);
1.1 deraadt 868: if (!name || !(stream = fopen(name, "r")))
1.48 espie 869: return false;
1.1 deraadt 870: fname = name;
871: /*
872: * set the MAKEFILE variable desired by System V fans -- the
873: * placement of the setting here means it gets set to the last
874: * makefile specified, as it is set by SysV make.
875: */
1.76 espie 876: found: Var_Set("MAKEFILE", fname);
1.1 deraadt 877: Parse_File(fname, stream);
878: }
1.48 espie 879: return true;
1.1 deraadt 880: }
881:
1.46 espie 882:
1.1 deraadt 883: /*
884: * usage --
885: * exit with usage message
886: */
887: static void
888: usage()
889: {
890: (void)fprintf(stderr,
1.65 jmc 891: "usage: make [-BeiknPqrSst] [-D variable] [-d flags] [-f makefile]\n\
1.46 espie 892: [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\
1.65 jmc 893: [NAME=value] [target ...]\n");
1.1 deraadt 894: exit(2);
895: }
896:
897: