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