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