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