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