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