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