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