Annotation of src/usr.bin/make/main.c, Revision 1.36
1.36 ! espie 1: /* $OpenBSD: main.c,v 1.35 2000/06/23 16:15:49 espie Exp $ */
1.12 millert 2: /* $NetBSD: main.c,v 1.34 1997/03/24 20:56:36 gwr Exp $ */
1.1 deraadt 3:
4: /*
1.9 millert 5: * Copyright (c) 1988, 1989, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: * Copyright (c) 1989 by Berkeley Softworks
8: * All rights reserved.
9: *
10: * This code is derived from software contributed to Berkeley by
11: * Adam de Boor.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
21: * 3. All advertising materials mentioning features or use of this software
22: * must display the following acknowledgement:
23: * This product includes software developed by the University of
24: * California, Berkeley and its contributors.
25: * 4. Neither the name of the University nor the names of its contributors
26: * may be used to endorse or promote products derived from this software
27: * without specific prior written permission.
28: *
29: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39: * SUCH DAMAGE.
40: */
41:
42: #ifndef lint
1.9 millert 43: static char copyright[] =
44: "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
45: The Regents of the University of California. All rights reserved.\n";
1.1 deraadt 46: #endif /* not lint */
47:
48: #ifndef lint
49: #if 0
1.9 millert 50: static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
1.1 deraadt 51: #else
1.36 ! espie 52: static char rcsid[] = "$OpenBSD: main.c,v 1.35 2000/06/23 16:15:49 espie Exp $";
1.1 deraadt 53: #endif
54: #endif /* not lint */
55:
56: /*-
57: * main.c --
58: * The main file for this entire program. Exit routines etc
59: * reside here.
60: *
61: * Utility functions defined in this file:
62: * Main_ParseArgLine Takes a line of arguments, breaks them and
63: * treats them as if they were given when first
64: * invoked. Used by the parse module to implement
65: * the .MFLAGS target.
66: *
67: * Error Print a tagged error message. The global
68: * MAKE variable must have been defined. This
69: * takes a format string and two optional
70: * arguments for it.
71: *
72: * Fatal Print an error message and exit. Also takes
73: * a format string and two arguments.
74: *
75: * Punt Aborts all jobs and exits with a message. Also
76: * takes a format string and two arguments.
77: *
78: * Finish Finish things up by printing the number of
79: * errors which occured, as passed to it, and
80: * exiting.
81: */
82:
83: #include <sys/types.h>
84: #include <sys/time.h>
85: #include <sys/param.h>
86: #include <sys/resource.h>
1.2 deraadt 87: #include <sys/signal.h>
1.1 deraadt 88: #include <sys/stat.h>
1.12 millert 89: #ifndef MAKE_BOOTSTRAP
1.1 deraadt 90: #include <sys/utsname.h>
1.9 millert 91: #endif
1.8 briggs 92: #include <sys/wait.h>
1.1 deraadt 93: #include <errno.h>
94: #include <fcntl.h>
95: #include <stdio.h>
1.12 millert 96: #include <stdlib.h>
1.14 espie 97: #include <time.h>
1.12 millert 98: #ifdef __STDC__
1.1 deraadt 99: #include <stdarg.h>
100: #else
101: #include <varargs.h>
102: #endif
103: #include "make.h"
104: #include "hash.h"
105: #include "dir.h"
106: #include "job.h"
107: #include "pathnames.h"
108:
109: #ifndef DEFMAXLOCAL
110: #define DEFMAXLOCAL DEFMAXJOBS
1.4 niklas 111: #endif /* DEFMAXLOCAL */
1.1 deraadt 112:
113: #define MAKEFLAGS ".MAKEFLAGS"
114:
1.33 espie 115: LIST create; /* Targets to be made */
1.28 espie 116: time_t now = OUT_OF_DATE;/* Time at start of make */
1.1 deraadt 117: GNode *DEFAULT; /* .DEFAULT node */
118: Boolean allPrecious; /* .PRECIOUS given on line by itself */
119:
120: static Boolean noBuiltins; /* -r flag */
1.33 espie 121: static LIST makefiles; /* ordered list of makefiles to read */
1.9 millert 122: static Boolean printVars; /* print value of one or more vars */
1.33 espie 123: static LIST variables; /* list of variables to print */
1.2 deraadt 124: int maxJobs; /* -j argument */
1.1 deraadt 125: static int maxLocal; /* -L argument */
126: Boolean compatMake; /* -B argument */
127: Boolean debug; /* -d flag */
128: Boolean noExecute; /* -n flag */
129: Boolean keepgoing; /* -k flag */
130: Boolean queryFlag; /* -q flag */
131: Boolean touchFlag; /* -t flag */
132: Boolean usePipes; /* !-P flag */
133: Boolean ignoreErrors; /* -i flag */
134: Boolean beSilent; /* -s flag */
135: Boolean oldVars; /* variable substitution style */
136: Boolean checkEnvFirst; /* -e flag */
137: static Boolean jobsRunning; /* TRUE if the jobs might be running */
138:
1.9 millert 139: static void MainParseArgs __P((int, char **));
140: char * chdir_verify_path __P((char *, char *));
1.32 espie 141: static int ReadMakefile __P((void *, void *));
1.9 millert 142: static void usage __P((void));
1.16 espie 143: int main __P((int, char **));
1.1 deraadt 144:
145: static char *curdir; /* startup directory */
146: static char *objdir; /* where we chdir'ed to */
147:
148: /*-
149: * MainParseArgs --
150: * Parse a given argument vector. Called from main() and from
151: * Main_ParseArgLine() when the .MAKEFLAGS target is used.
152: *
153: * XXX: Deal with command line overriding .MAKEFLAGS in makefile
154: *
155: * Results:
156: * None
157: *
158: * Side Effects:
159: * Various global and local flags will be set depending on the flags
160: * given
161: */
162: static void
163: MainParseArgs(argc, argv)
164: int argc;
165: char **argv;
166: {
167: extern int optind;
168: extern char *optarg;
169: int c;
1.2 deraadt 170: int forceJobs = 0;
1.1 deraadt 171:
172: optind = 1; /* since we're called more than once */
1.2 deraadt 173: #ifdef REMOTE
1.9 millert 174: # define OPTFLAGS "BD:I:L:PSV:d:ef:ij:km:nqrst"
1.1 deraadt 175: #else
1.9 millert 176: # define OPTFLAGS "BD:I:PSV:d:ef:ij:km:nqrst"
1.1 deraadt 177: #endif
1.10 millert 178: rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
1.1 deraadt 179: switch(c) {
180: case 'D':
181: Var_Set(optarg, "1", VAR_GLOBAL);
182: Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
183: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
184: break;
185: case 'I':
186: Parse_AddIncludeDir(optarg);
187: Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
188: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
189: break;
1.9 millert 190: case 'V':
191: printVars = TRUE;
1.33 espie 192: Lst_AtEnd(&variables, optarg);
1.9 millert 193: Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
194: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
195: break;
1.1 deraadt 196: case 'B':
197: compatMake = TRUE;
198: break;
1.2 deraadt 199: #ifdef REMOTE
1.15 espie 200: case 'L': {
201: char *endptr;
202:
203: maxLocal = strtol(optarg, &endptr, 0);
204: if (endptr == optarg) {
205: fprintf(stderr,
206: "make: illegal argument to -L option -- %s -- not a number\n",
207: optarg);
208: usage();
209: }
1.1 deraadt 210: Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
211: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
212: break;
1.15 espie 213: }
1.2 deraadt 214: #endif
1.1 deraadt 215: case 'P':
216: usePipes = FALSE;
217: Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
218: break;
219: case 'S':
220: keepgoing = FALSE;
221: Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
222: break;
223: case 'd': {
224: char *modules = optarg;
225:
226: for (; *modules; ++modules)
227: switch (*modules) {
228: case 'A':
229: debug = ~0;
230: break;
231: case 'a':
232: debug |= DEBUG_ARCH;
233: break;
234: case 'c':
235: debug |= DEBUG_COND;
236: break;
237: case 'd':
238: debug |= DEBUG_DIR;
239: break;
240: case 'f':
241: debug |= DEBUG_FOR;
242: break;
243: case 'g':
244: if (modules[1] == '1') {
245: debug |= DEBUG_GRAPH1;
246: ++modules;
247: }
248: else if (modules[1] == '2') {
249: debug |= DEBUG_GRAPH2;
250: ++modules;
251: }
252: break;
253: case 'j':
254: debug |= DEBUG_JOB;
255: break;
256: case 'm':
257: debug |= DEBUG_MAKE;
258: break;
259: case 's':
260: debug |= DEBUG_SUFF;
261: break;
262: case 't':
263: debug |= DEBUG_TARG;
264: break;
265: case 'v':
266: debug |= DEBUG_VAR;
267: break;
268: default:
269: (void)fprintf(stderr,
270: "make: illegal argument to d option -- %c\n",
271: *modules);
272: usage();
273: }
274: Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
275: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
276: break;
277: }
278: case 'e':
279: checkEnvFirst = TRUE;
280: Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
281: break;
282: case 'f':
1.33 espie 283: Lst_AtEnd(&makefiles, optarg);
1.1 deraadt 284: break;
285: case 'i':
286: ignoreErrors = TRUE;
287: Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
288: break;
1.15 espie 289: case 'j': {
290: char *endptr;
291:
1.2 deraadt 292: forceJobs = TRUE;
1.15 espie 293: maxJobs = strtol(optarg, &endptr, 0);
294: if (endptr == optarg) {
295: fprintf(stderr,
296: "make: illegal argument to -j option -- %s -- not a number\n",
297: optarg);
298: usage();
299: }
1.1 deraadt 300: maxJobs = atoi(optarg);
1.2 deraadt 301: #ifndef REMOTE
302: maxLocal = maxJobs;
303: #endif
1.1 deraadt 304: Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
305: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
306: break;
1.15 espie 307: }
1.1 deraadt 308: case 'k':
309: keepgoing = TRUE;
310: Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
311: break;
1.5 niklas 312: case 'm':
1.33 espie 313: Dir_AddDir(&sysIncPath, optarg);
1.5 niklas 314: Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
315: Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
316: break;
1.1 deraadt 317: case 'n':
318: noExecute = TRUE;
319: Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
320: break;
321: case 'q':
322: queryFlag = TRUE;
323: /* Kind of nonsensical, wot? */
324: Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
325: break;
326: case 'r':
327: noBuiltins = TRUE;
328: Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
329: break;
330: case 's':
331: beSilent = TRUE;
332: Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
333: break;
334: case 't':
335: touchFlag = TRUE;
336: Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
337: break;
338: default:
339: case '?':
340: usage();
341: }
342: }
343:
1.2 deraadt 344: /*
345: * Be compatible if user did not specify -j and did not explicitly
346: * turned compatibility on
347: */
348: if (!compatMake && !forceJobs)
349: compatMake = TRUE;
350:
1.1 deraadt 351: oldVars = TRUE;
352:
353: /*
354: * See if the rest of the arguments are variable assignments and
355: * perform them if so. Else take them to be targets and stuff them
356: * on the end of the "create" list.
357: */
358: for (argv += optind, argc -= optind; *argv; ++argv, --argc)
1.3 deraadt 359: if (Parse_IsVar(*argv)) {
1.9 millert 360: char *var = estrdup(*argv);
1.3 deraadt 361:
362: Parse_DoVar(var, VAR_CMD);
363: free(var);
364: } else {
1.1 deraadt 365: if (!**argv)
366: Punt("illegal (null) argument.");
367: if (**argv == '-') {
368: if ((*argv)[1])
369: optind = 0; /* -flag... */
370: else
371: optind = 1; /* - */
372: goto rearg;
373: }
1.33 espie 374: Lst_AtEnd(&create, estrdup(*argv));
1.1 deraadt 375: }
376: }
377:
378: /*-
379: * Main_ParseArgLine --
380: * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
381: * is encountered and by main() when reading the .MAKEFLAGS envariable.
382: * Takes a line of arguments and breaks it into its
383: * component words and passes those words and the number of them to the
384: * MainParseArgs function.
385: * The line should have all its leading whitespace removed.
386: *
387: * Results:
388: * None
389: *
390: * Side Effects:
391: * Only those that come from the various arguments.
392: */
393: void
394: Main_ParseArgLine(line)
395: char *line; /* Line to fracture */
396: {
397: char **argv; /* Manufactured argument vector */
398: int argc; /* Number of arguments in argv */
1.14 espie 399: char *args; /* Space used by the args */
1.19 espie 400: char *buf;
401: char *argv0 = Var_Value(".MAKE", VAR_GLOBAL);
1.1 deraadt 402:
403: if (line == NULL)
404: return;
405: for (; *line == ' '; ++line)
406: continue;
407: if (!*line)
408: return;
409:
1.14 espie 410: buf = emalloc(strlen(line) + strlen(argv0) + 2);
411: (void)sprintf(buf, "%s %s", argv0, line);
412:
413: argv = brk_string(buf, &argc, TRUE, &args);
414: free(buf);
1.1 deraadt 415: MainParseArgs(argc, argv);
1.14 espie 416:
417: free(args);
418: free(argv);
1.1 deraadt 419: }
420:
1.9 millert 421: char *
422: chdir_verify_path(path, obpath)
423: char *path;
424: char *obpath;
425: {
426: struct stat sb;
427:
428: if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
429: if (chdir(path)) {
430: (void)fprintf(stderr, "make warning: %s: %s.\n",
431: path, strerror(errno));
432: return 0;
433: }
434: else {
435: if (path[0] != '/') {
436: (void) snprintf(obpath, MAXPATHLEN, "%s/%s",
437: curdir, path);
438: return obpath;
439: }
440: else
441: return path;
442: }
443: }
444:
445: return 0;
446: }
447:
448:
1.1 deraadt 449: /*-
450: * main --
451: * The main function, for obvious reasons. Initializes variables
452: * and a few modules, then parses the arguments give it in the
453: * environment and on the command line. Reads the system makefile
454: * followed by either Makefile, makefile or the file given by the
455: * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
456: * flags it has received by then uses either the Make or the Compat
457: * module to create the initial list of targets.
458: *
459: * Results:
460: * If -q was given, exits -1 if anything was out-of-date. Else it exits
461: * 0.
462: *
463: * Side Effects:
464: * The program exits when done. Targets are created. etc. etc. etc.
465: */
466: int
467: main(argc, argv)
468: int argc;
469: char **argv;
470: {
1.34 espie 471: LIST targs; /* target nodes to create -- passed to Make_Init */
1.1 deraadt 472: Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
473: struct stat sb, sa;
1.19 espie 474: char *p, *path, *pathp, *pwd;
1.1 deraadt 475: char mdpath[MAXPATHLEN + 1];
476: char obpath[MAXPATHLEN + 1];
477: char cdpath[MAXPATHLEN + 1];
478: char *machine = getenv("MACHINE");
1.12 millert 479: char *machine_arch = getenv("MACHINE_ARCH");
1.5 niklas 480: char *cp = NULL, *start;
481: /* avoid faults on read-only strings */
482: static char syspath[] = _PATH_DEFSYSPATH;
1.1 deraadt 483:
1.2 deraadt 484: #ifdef RLIMIT_NOFILE
485: /*
486: * get rid of resource limit on file descriptors
487: */
488: {
489: struct rlimit rl;
490: if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
491: rl.rlim_cur != rl.rlim_max) {
492: rl.rlim_cur = rl.rlim_max;
493: (void) setrlimit(RLIMIT_NOFILE, &rl);
494: }
495: }
496: #endif
1.1 deraadt 497: /*
498: * Find where we are and take care of PWD for the automounter...
499: * All this code is so that we know where we are when we start up
500: * on a different machine with pmake.
501: */
502: curdir = cdpath;
503: if (getcwd(curdir, MAXPATHLEN) == NULL) {
504: (void)fprintf(stderr, "make: %s.\n", strerror(errno));
505: exit(2);
506: }
507:
508: if (stat(curdir, &sa) == -1) {
509: (void)fprintf(stderr, "make: %s: %s.\n",
510: curdir, strerror(errno));
511: exit(2);
512: }
513:
514: if ((pwd = getenv("PWD")) != NULL) {
515: if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
1.9 millert 516: sa.st_dev == sb.st_dev)
1.1 deraadt 517: (void) strcpy(curdir, pwd);
518: }
519:
520: /*
521: * Get the name of this type of MACHINE from utsname
522: * so we can share an executable for similar machines.
523: * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
524: *
1.12 millert 525: * Note that both MACHINE and MACHINE_ARCH are decided at
526: * run-time.
1.1 deraadt 527: */
1.12 millert 528: if (!machine) {
529: #ifndef MAKE_BOOTSTRAP
1.9 millert 530: struct utsname utsname;
531:
1.2 deraadt 532: if (uname(&utsname) == -1) {
1.1 deraadt 533: perror("make: uname");
534: exit(2);
535: }
536: machine = utsname.machine;
1.6 niklas 537: #else
538: machine = MACHINE;
539: #endif
1.1 deraadt 540: }
541:
1.12 millert 542: if (!machine_arch) {
543: #ifndef MACHINE_ARCH
544: machine_arch = "unknown"; /* XXX: no uname -p yet */
545: #else
546: machine_arch = MACHINE_ARCH;
547: #endif
548: }
549:
1.1 deraadt 550: /*
1.9 millert 551: * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
552: * exists, change into it and build there. (If a .${MACHINE} suffix
553: * exists, use that directory instead).
554: * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
555: * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
556: * If all fails, use the current directory to build.
557: *
558: * Once things are initted,
559: * have to add the original directory to the search path,
1.1 deraadt 560: * and modify the paths for the Makefiles apropriately. The
561: * current directory is also placed as a variable for make scripts.
562: */
1.9 millert 563: if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
564: if (!(path = getenv("MAKEOBJDIR"))) {
565: path = _PATH_OBJDIR;
566: pathp = _PATH_OBJDIRPREFIX;
567: (void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
568: path, machine);
569: if (!(objdir = chdir_verify_path(mdpath, obpath)))
570: if (!(objdir=chdir_verify_path(path, obpath))) {
571: (void) snprintf(mdpath, MAXPATHLEN,
572: "%s%s", pathp, curdir);
573: if (!(objdir=chdir_verify_path(mdpath,
574: obpath)))
575: objdir = curdir;
576: }
577: }
578: else if (!(objdir = chdir_verify_path(path, obpath)))
1.1 deraadt 579: objdir = curdir;
580: }
581: else {
1.9 millert 582: (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
583: if (!(objdir = chdir_verify_path(mdpath, obpath)))
1.1 deraadt 584: objdir = curdir;
585: }
586:
587: setenv("PWD", objdir, 1);
1.30 espie 588: unsetenv("CDPATH");
1.1 deraadt 589:
1.33 espie 590: Lst_Init(&create);
591: Lst_Init(&makefiles);
1.9 millert 592: printVars = FALSE;
1.33 espie 593: Lst_Init(&variables);
1.1 deraadt 594: beSilent = FALSE; /* Print commands as executed */
595: ignoreErrors = FALSE; /* Pay attention to non-zero returns */
596: noExecute = FALSE; /* Execute all commands */
597: keepgoing = FALSE; /* Stop on error */
598: allPrecious = FALSE; /* Remove targets when interrupted */
599: queryFlag = FALSE; /* This is not just a check-run */
600: noBuiltins = FALSE; /* Read the built-in rules */
601: touchFlag = FALSE; /* Actually update targets */
602: usePipes = TRUE; /* Catch child output in pipes */
603: debug = 0; /* No debug verbosity, please. */
604: jobsRunning = FALSE;
605:
1.2 deraadt 606: maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */
607: #ifdef REMOTE
1.1 deraadt 608: maxJobs = DEFMAXJOBS; /* Set default max concurrency */
609: #else
1.2 deraadt 610: maxJobs = maxLocal;
1.1 deraadt 611: #endif
1.2 deraadt 612: compatMake = FALSE; /* No compat mode */
1.9 millert 613:
1.1 deraadt 614:
615: /*
616: * Initialize the parsing, directory and variable modules to prepare
617: * for the reading of inclusion paths and variable settings on the
618: * command line
619: */
620: Dir_Init(); /* Initialize directory structures so -I flags
621: * can be processed correctly */
622: Parse_Init(); /* Need to initialize the paths of #include
623: * directories */
624: Var_Init(); /* As well as the lists of variables for
625: * parsing arguments */
626: if (objdir != curdir)
1.33 espie 627: Dir_AddDir(&dirSearchPath, curdir);
1.1 deraadt 628: Var_Set(".CURDIR", curdir, VAR_GLOBAL);
629: Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
630:
631: /*
632: * Initialize various variables.
633: * MAKE also gets this name, for compatibility
634: * .MAKEFLAGS gets set to the empty string just in case.
635: * MFLAGS also gets initialized empty, for compatibility.
636: */
637: Var_Set("MAKE", argv[0], VAR_GLOBAL);
1.14 espie 638: Var_Set(".MAKE", argv[0], VAR_GLOBAL);
1.1 deraadt 639: Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
640: Var_Set("MFLAGS", "", VAR_GLOBAL);
641: Var_Set("MACHINE", machine, VAR_GLOBAL);
1.12 millert 642: Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
1.1 deraadt 643:
644: /*
645: * First snag any flags out of the MAKE environment variable.
646: * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
647: * in a different format).
648: */
649: #ifdef POSIX
650: Main_ParseArgLine(getenv("MAKEFLAGS"));
651: #else
652: Main_ParseArgLine(getenv("MAKE"));
653: #endif
1.9 millert 654:
1.1 deraadt 655: MainParseArgs(argc, argv);
1.30 espie 656:
1.1 deraadt 657:
658: /*
659: * Initialize archive, target and suffix modules in preparation for
660: * parsing the makefile(s)
661: */
662: Arch_Init();
663: Targ_Init();
664: Suff_Init();
665:
1.25 espie 666: DEFAULT = NULL;
1.1 deraadt 667: (void)time(&now);
668:
669: /*
670: * Set up the .TARGETS variable to contain the list of targets to be
671: * created. If none specified, make the variable empty -- the parser
672: * will fill the thing in with the default or .MAIN target.
673: */
1.33 espie 674: if (!Lst_IsEmpty(&create)) {
1.1 deraadt 675: LstNode ln;
676:
1.35 espie 677: for (ln = Lst_First(&create); ln != NULL; ln = Lst_Adv(ln)) {
1.1 deraadt 678: char *name = (char *)Lst_Datum(ln);
679:
680: Var_Append(".TARGETS", name, VAR_GLOBAL);
681: }
682: } else
683: Var_Set(".TARGETS", "", VAR_GLOBAL);
684:
1.5 niklas 685:
686: /*
687: * If no user-supplied system path was given (through the -m option)
688: * add the directories from the DEFSYSPATH (more than one may be given
689: * as dir1:...:dirn) to the system include path.
690: */
1.33 espie 691: if (Lst_IsEmpty(&sysIncPath)) {
1.5 niklas 692: for (start = syspath; *start != '\0'; start = cp) {
1.9 millert 693: for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1.5 niklas 694: continue;
695: if (*cp == '\0') {
1.33 espie 696: Dir_AddDir(&sysIncPath, start);
1.5 niklas 697: } else {
698: *cp++ = '\0';
1.33 espie 699: Dir_AddDir(&sysIncPath, start);
1.5 niklas 700: }
701: }
702: }
703:
1.1 deraadt 704: /*
1.5 niklas 705: * Read in the built-in rules first, followed by the specified
706: * makefile, if it was (makefile != (char *) NULL), or the default
707: * Makefile and makefile, in that order, if it wasn't.
1.1 deraadt 708: */
1.5 niklas 709: if (!noBuiltins) {
710: LstNode ln;
1.33 espie 711: LIST sysMkPath; /* Path of sys.mk */
1.5 niklas 712:
1.33 espie 713: Lst_Init(&sysMkPath);
714: Dir_Expand(_PATH_DEFSYSMK, &sysIncPath, &sysMkPath);
715: if (Lst_IsEmpty(&sysMkPath))
1.5 niklas 716: Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
1.33 espie 717: ln = Lst_Find(&sysMkPath, ReadMakefile, NULL);
1.25 espie 718: if (ln != NULL)
1.5 niklas 719: Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1.34 espie 720: #ifdef CLEANUP
721: Lst_Destroy(&sysMkPath, (SimpleProc)free);
722: #endif
1.5 niklas 723: }
1.1 deraadt 724:
1.33 espie 725: if (!Lst_IsEmpty(&makefiles)) {
1.1 deraadt 726: LstNode ln;
727:
1.33 espie 728: ln = Lst_Find(&makefiles, ReadMakefile, NULL);
1.25 espie 729: if (ln != NULL)
1.1 deraadt 730: Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1.13 niklas 731: } else if (!ReadMakefile("BSDmakefile", NULL))
732: if (!ReadMakefile("makefile", NULL))
733: (void)ReadMakefile("Makefile", NULL);
1.1 deraadt 734:
1.9 millert 735: (void)ReadMakefile(".depend", NULL);
1.1 deraadt 736:
1.19 espie 737: Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
1.1 deraadt 738:
739: /* Install all the flags into the MAKE envariable. */
1.19 espie 740: if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) != NULL) && *p)
1.1 deraadt 741: #ifdef POSIX
742: setenv("MAKEFLAGS", p, 1);
743: #else
744: setenv("MAKE", p, 1);
745: #endif
746:
747: /*
748: * For compatibility, look at the directories in the VPATH variable
749: * and add them to the search path, if the variable is defined. The
750: * variable's value is in the same format as the PATH envariable, i.e.
751: * <directory>:<directory>:<directory>...
752: */
753: if (Var_Exists("VPATH", VAR_CMD)) {
754: char *vpath, *path, *cp, savec;
755: /*
756: * GCC stores string constants in read-only memory, but
757: * Var_Subst will want to write this thing, so store it
758: * in an array
759: */
760: static char VPATH[] = "${VPATH}";
761:
1.36 ! espie 762: vpath = Var_Subst(VPATH, (SymTable *)VAR_CMD, FALSE);
1.1 deraadt 763: path = vpath;
764: do {
765: /* skip to end of directory */
766: for (cp = path; *cp != ':' && *cp != '\0'; cp++)
767: continue;
768: /* Save terminator character so know when to stop */
769: savec = *cp;
770: *cp = '\0';
771: /* Add directory to search path */
1.33 espie 772: Dir_AddDir(&dirSearchPath, path);
1.1 deraadt 773: *cp = savec;
774: path = cp + 1;
775: } while (savec == ':');
1.32 espie 776: free(vpath);
1.1 deraadt 777: }
778:
779: /*
780: * Now that all search paths have been read for suffixes et al, it's
781: * time to add the default search path to their lists...
782: */
783: Suff_DoPaths();
784:
785: /* print the initial graph, if the user requested it */
786: if (DEBUG(GRAPH1))
787: Targ_PrintGraph(1);
788:
1.9 millert 789: /* print the values of any variables requested by the user */
790: if (printVars) {
791: LstNode ln;
792:
1.35 espie 793: for (ln = Lst_First(&variables); ln != NULL; ln = Lst_Adv(ln)) {
1.9 millert 794: char *value = Var_Value((char *)Lst_Datum(ln),
1.19 espie 795: VAR_GLOBAL);
1.9 millert 796:
797: printf("%s\n", value ? value : "");
798: }
799: }
800:
1.1 deraadt 801: /*
802: * Have now read the entire graph and need to make a list of targets
803: * to create. If none was given on the command line, we consult the
804: * parsing module to find the main target(s) to create.
805: */
1.34 espie 806: Lst_Init(&targs);
807: if (!Lst_IsEmpty(&create))
808: Targ_FindList(&targs, &create);
1.1 deraadt 809: else
1.34 espie 810: Parse_MainName(&targs);
1.1 deraadt 811:
1.9 millert 812: if (!compatMake && !printVars) {
1.1 deraadt 813: /*
814: * Initialize job module before traversing the graph, now that
815: * any .BEGIN and .END targets have been read. This is done
816: * only if the -q flag wasn't given (to prevent the .BEGIN from
817: * being executed should it exist).
818: */
819: if (!queryFlag) {
820: if (maxLocal == -1)
821: maxLocal = maxJobs;
822: Job_Init(maxJobs, maxLocal);
823: jobsRunning = TRUE;
824: }
825:
826: /* Traverse the graph, checking on all the targets */
1.34 espie 827: outOfDate = Make_Run(&targs);
1.9 millert 828: } else if (!printVars) {
1.1 deraadt 829: /*
830: * Compat_Init will take care of creating all the targets as
831: * well as initializing the module.
832: */
1.34 espie 833: Compat_Run(&targs);
1.9 millert 834: }
835:
1.34 espie 836: Lst_Destroy(&targs, NOFREE);
1.33 espie 837: Lst_Destroy(&variables, NOFREE);
838: Lst_Destroy(&makefiles, NOFREE);
839: Lst_Destroy(&create, (SimpleProc)free);
1.1 deraadt 840:
841: /* print the graph now it's been processed if the user requested it */
842: if (DEBUG(GRAPH2))
843: Targ_PrintGraph(2);
844:
845: Suff_End();
846: Targ_End();
847: Arch_End();
848: Var_End();
849: Parse_End();
850: Dir_End();
1.14 espie 851: Job_End();
1.1 deraadt 852:
853: if (queryFlag && outOfDate)
854: return(1);
855: else
856: return(0);
857: }
858:
859: /*-
860: * ReadMakefile --
861: * Open and parse the given makefile.
862: *
863: * Results:
864: * TRUE if ok. FALSE if couldn't open file.
865: *
866: * Side Effects:
867: * lots
868: */
869: static Boolean
1.9 millert 870: ReadMakefile(p, q)
1.32 espie 871: void *p;
872: void *q;
1.1 deraadt 873: {
1.9 millert 874: char *fname = p; /* makefile to read */
1.33 espie 875: extern LIST parseIncPath;
1.1 deraadt 876: FILE *stream;
877: char *name, path[MAXPATHLEN + 1];
878:
879: if (!strcmp(fname, "-")) {
880: Parse_File("(stdin)", stdin);
881: Var_Set("MAKEFILE", "", VAR_GLOBAL);
882: } else {
883: if ((stream = fopen(fname, "r")) != NULL)
884: goto found;
885: /* if we've chdir'd, rebuild the path name */
886: if (curdir != objdir && *fname != '/') {
887: (void)sprintf(path, "%s/%s", curdir, fname);
888: if ((stream = fopen(path, "r")) != NULL) {
889: fname = path;
890: goto found;
891: }
892: }
893: /* look in -I and system include directories. */
1.33 espie 894: name = Dir_FindFile(fname, &parseIncPath);
1.1 deraadt 895: if (!name)
1.33 espie 896: name = Dir_FindFile(fname, &sysIncPath);
1.1 deraadt 897: if (!name || !(stream = fopen(name, "r")))
898: return(FALSE);
899: fname = name;
900: /*
901: * set the MAKEFILE variable desired by System V fans -- the
902: * placement of the setting here means it gets set to the last
903: * makefile specified, as it is set by SysV make.
904: */
905: found: Var_Set("MAKEFILE", fname, VAR_GLOBAL);
906: Parse_File(fname, stream);
907: (void)fclose(stream);
908: }
909: return(TRUE);
910: }
911:
912: /*-
1.8 briggs 913: * Cmd_Exec --
914: * Execute the command in cmd, and return the output of that command
915: * in a string.
916: *
917: * Results:
918: * A string containing the output of the command, or the empty string
919: * If err is not NULL, it contains the reason for the command failure
920: *
921: * Side Effects:
922: * The string must be freed by the caller.
923: */
924: char *
925: Cmd_Exec(cmd, err)
926: char *cmd;
927: char **err;
928: {
929: char *args[4]; /* Args for invoking the shell */
930: int fds[2]; /* Pipe streams */
931: int cpid; /* Child PID */
932: int pid; /* PID from wait() */
933: char *res; /* result */
934: int status; /* command exit status */
1.22 espie 935: BUFFER buf; /* buffer to store the result */
1.8 briggs 936: char *cp;
1.17 espie 937: ssize_t cc;
1.18 espie 938: size_t length;
1.8 briggs 939:
940:
941: *err = NULL;
942:
943: /*
944: * Set up arguments for shell
945: */
946: args[0] = "sh";
947: args[1] = "-c";
948: args[2] = cmd;
949: args[3] = NULL;
950:
951: /*
952: * Open a pipe for fetching its output
953: */
954: if (pipe(fds) == -1) {
955: *err = "Couldn't create pipe for \"%s\"";
956: goto bad;
957: }
958:
959: /*
960: * Fork
961: */
962: switch (cpid = vfork()) {
963: case 0:
964: /*
965: * Close input side of pipe
966: */
967: (void) close(fds[0]);
968:
969: /*
970: * Duplicate the output stream to the shell's output, then
971: * shut the extra thing down. Note we don't fetch the error
972: * stream...why not? Why?
973: */
974: (void) dup2(fds[1], 1);
975: (void) close(fds[1]);
1.9 millert 976:
1.8 briggs 977: (void) execv("/bin/sh", args);
978: _exit(1);
979: /*NOTREACHED*/
980:
981: case -1:
982: *err = "Couldn't exec \"%s\"";
983: goto bad;
984:
985: default:
986: /*
987: * No need for the writing half
988: */
989: (void) close(fds[1]);
1.9 millert 990:
1.22 espie 991: Buf_Init(&buf, MAKE_BSIZE);
1.8 briggs 992:
993: do {
994: char result[BUFSIZ];
995: cc = read(fds[0], result, sizeof(result));
1.9 millert 996: if (cc > 0)
1.22 espie 997: Buf_AddChars(&buf, cc, result);
1.8 briggs 998: }
999: while (cc > 0 || (cc == -1 && errno == EINTR));
1000:
1001: /*
1002: * Close the input side of the pipe.
1003: */
1004: (void) close(fds[0]);
1005:
1006: /*
1007: * Wait for the process to exit.
1008: */
1009: while(((pid = wait(&status)) != cpid) && (pid >= 0))
1010: continue;
1011:
1.22 espie 1012: res = Buf_Retrieve(&buf);
1013: length = Buf_Size(&buf);
1.8 briggs 1014:
1.17 espie 1015: if (cc == -1)
1.8 briggs 1016: *err = "Couldn't read shell's output for \"%s\"";
1017:
1018: if (status)
1019: *err = "\"%s\" returned non-zero status";
1020:
1021: /*
1022: * Null-terminate the result, convert newlines to spaces and
1023: * install it in the variable.
1024: */
1.17 espie 1025: res[length] = '\0';
1026: cp = res + length - 1;
1.8 briggs 1027:
1028: if (*cp == '\n') {
1029: /*
1030: * A final newline is just stripped
1031: */
1032: *cp-- = '\0';
1033: }
1034: while (cp >= res) {
1035: if (*cp == '\n') {
1036: *cp = ' ';
1037: }
1038: cp--;
1039: }
1040: break;
1041: }
1042: return res;
1043: bad:
1044: res = emalloc(1);
1045: *res = '\0';
1046: return res;
1047: }
1048:
1049: /*-
1.1 deraadt 1050: * Error --
1051: * Print an error message given its format.
1052: *
1053: * Results:
1054: * None.
1055: *
1056: * Side Effects:
1057: * The message is printed.
1058: */
1059: /* VARARGS */
1060: void
1.12 millert 1061: #ifdef __STDC__
1.1 deraadt 1062: Error(char *fmt, ...)
1063: #else
1064: Error(va_alist)
1065: va_dcl
1066: #endif
1067: {
1068: va_list ap;
1.12 millert 1069: #ifdef __STDC__
1.1 deraadt 1070: va_start(ap, fmt);
1071: #else
1072: char *fmt;
1073:
1074: va_start(ap);
1075: fmt = va_arg(ap, char *);
1076: #endif
1077: (void)vfprintf(stderr, fmt, ap);
1078: va_end(ap);
1079: (void)fprintf(stderr, "\n");
1080: (void)fflush(stderr);
1081: }
1082:
1083: /*-
1084: * Fatal --
1085: * Produce a Fatal error message. If jobs are running, waits for them
1086: * to finish.
1087: *
1088: * Results:
1089: * None
1090: *
1091: * Side Effects:
1092: * The program exits
1093: */
1094: /* VARARGS */
1095: void
1.12 millert 1096: #ifdef __STDC__
1.1 deraadt 1097: Fatal(char *fmt, ...)
1098: #else
1099: Fatal(va_alist)
1100: va_dcl
1101: #endif
1102: {
1103: va_list ap;
1.12 millert 1104: #ifdef __STDC__
1.1 deraadt 1105: va_start(ap, fmt);
1106: #else
1107: char *fmt;
1108:
1109: va_start(ap);
1110: fmt = va_arg(ap, char *);
1111: #endif
1112: if (jobsRunning)
1113: Job_Wait();
1114:
1115: (void)vfprintf(stderr, fmt, ap);
1116: va_end(ap);
1117: (void)fprintf(stderr, "\n");
1118: (void)fflush(stderr);
1119:
1120: if (DEBUG(GRAPH2))
1121: Targ_PrintGraph(2);
1122: exit(2); /* Not 1 so -q can distinguish error */
1123: }
1124:
1125: /*
1126: * Punt --
1127: * Major exception once jobs are being created. Kills all jobs, prints
1128: * a message and exits.
1129: *
1130: * Results:
1.9 millert 1131: * None
1.1 deraadt 1132: *
1133: * Side Effects:
1134: * All children are killed indiscriminately and the program Lib_Exits
1135: */
1136: /* VARARGS */
1137: void
1.12 millert 1138: #ifdef __STDC__
1.1 deraadt 1139: Punt(char *fmt, ...)
1140: #else
1141: Punt(va_alist)
1142: va_dcl
1143: #endif
1144: {
1145: va_list ap;
1.12 millert 1146: #ifdef __STDC__
1.1 deraadt 1147: va_start(ap, fmt);
1148: #else
1149: char *fmt;
1150:
1151: va_start(ap);
1152: fmt = va_arg(ap, char *);
1153: #endif
1154:
1155: (void)fprintf(stderr, "make: ");
1156: (void)vfprintf(stderr, fmt, ap);
1157: va_end(ap);
1158: (void)fprintf(stderr, "\n");
1159: (void)fflush(stderr);
1160:
1161: DieHorribly();
1162: }
1163:
1164: /*-
1165: * DieHorribly --
1166: * Exit without giving a message.
1167: *
1168: * Results:
1169: * None
1170: *
1171: * Side Effects:
1172: * A big one...
1173: */
1174: void
1175: DieHorribly()
1176: {
1177: if (jobsRunning)
1178: Job_AbortAll();
1179: if (DEBUG(GRAPH2))
1180: Targ_PrintGraph(2);
1181: exit(2); /* Not 1, so -q can distinguish error */
1182: }
1183:
1184: /*
1185: * Finish --
1186: * Called when aborting due to errors in child shell to signal
1.9 millert 1187: * abnormal exit.
1.1 deraadt 1188: *
1189: * Results:
1.9 millert 1190: * None
1.1 deraadt 1191: *
1192: * Side Effects:
1193: * The program exits
1194: */
1195: void
1196: Finish(errors)
1197: int errors; /* number of errors encountered in Make_Make */
1198: {
1199: Fatal("%d error%s", errors, errors == 1 ? "" : "s");
1200: }
1201:
1202: /*
1203: * usage --
1204: * exit with usage message
1205: */
1206: static void
1207: usage()
1208: {
1209: (void)fprintf(stderr,
1.9 millert 1210: "usage: make [-Beiknqrst] [-D variable] [-d flags] [-f makefile ]\n\
1211: [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\
1212: [variable=value] [target ...]\n");
1.1 deraadt 1213: exit(2);
1214: }
1215:
1216:
1.31 espie 1217: void
1218: PrintAddr(a)
1.32 espie 1219: void *a;
1.1 deraadt 1220: {
1221: printf("%lx ", (unsigned long) a);
1222: }