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