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