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