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