Annotation of src/usr.bin/make/main.c, Revision 1.25
1.25 ! espie 1: /* $OpenBSD: main.c,v 1.24 1999/12/18 02:11:27 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.25 ! espie 52: static char rcsid[] = "$OpenBSD: main.c,v 1.24 1999/12/18 02:11:27 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:
115: Lst create; /* Targets to be made */
116: time_t now; /* Time at start of make */
117: GNode *DEFAULT; /* .DEFAULT node */
118: Boolean allPrecious; /* .PRECIOUS given on line by itself */
119:
120: static Boolean noBuiltins; /* -r flag */
121: static Lst makefiles; /* ordered list of makefiles to read */
1.9 millert 122: static Boolean printVars; /* print value of one or more vars */
123: static Lst 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 *));
141: static int ReadMakefile __P((ClientData, ClientData));
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;
192: (void)Lst_AtEnd(variables, (ClientData)optarg);
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':
283: (void)Lst_AtEnd(makefiles, (ClientData)optarg);
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':
313: Dir_AddDir(sysIncPath, optarg);
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.8 briggs 374: (void)Lst_AtEnd(create, (ClientData)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: {
471: Lst targs; /* target nodes to create -- passed to Make_Init */
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: Lst sysMkPath; /* Path of sys.mk */
481: char *cp = NULL, *start;
482: /* avoid faults on read-only strings */
483: static char syspath[] = _PATH_DEFSYSPATH;
1.1 deraadt 484:
1.2 deraadt 485: #ifdef RLIMIT_NOFILE
486: /*
487: * get rid of resource limit on file descriptors
488: */
489: {
490: struct rlimit rl;
491: if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
492: rl.rlim_cur != rl.rlim_max) {
493: rl.rlim_cur = rl.rlim_max;
494: (void) setrlimit(RLIMIT_NOFILE, &rl);
495: }
496: }
497: #endif
1.1 deraadt 498: /*
499: * Find where we are and take care of PWD for the automounter...
500: * All this code is so that we know where we are when we start up
501: * on a different machine with pmake.
502: */
503: curdir = cdpath;
504: if (getcwd(curdir, MAXPATHLEN) == NULL) {
505: (void)fprintf(stderr, "make: %s.\n", strerror(errno));
506: exit(2);
507: }
508:
509: if (stat(curdir, &sa) == -1) {
510: (void)fprintf(stderr, "make: %s: %s.\n",
511: curdir, strerror(errno));
512: exit(2);
513: }
514:
515: if ((pwd = getenv("PWD")) != NULL) {
516: if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
1.9 millert 517: sa.st_dev == sb.st_dev)
1.1 deraadt 518: (void) strcpy(curdir, pwd);
519: }
520:
521: /*
522: * Get the name of this type of MACHINE from utsname
523: * so we can share an executable for similar machines.
524: * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
525: *
1.12 millert 526: * Note that both MACHINE and MACHINE_ARCH are decided at
527: * run-time.
1.1 deraadt 528: */
1.12 millert 529: if (!machine) {
530: #ifndef MAKE_BOOTSTRAP
1.9 millert 531: struct utsname utsname;
532:
1.2 deraadt 533: if (uname(&utsname) == -1) {
1.1 deraadt 534: perror("make: uname");
535: exit(2);
536: }
537: machine = utsname.machine;
1.6 niklas 538: #else
539: machine = MACHINE;
540: #endif
1.1 deraadt 541: }
542:
1.12 millert 543: if (!machine_arch) {
544: #ifndef MACHINE_ARCH
545: machine_arch = "unknown"; /* XXX: no uname -p yet */
546: #else
547: machine_arch = MACHINE_ARCH;
548: #endif
549: }
550:
1.1 deraadt 551: /*
1.9 millert 552: * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
553: * exists, change into it and build there. (If a .${MACHINE} suffix
554: * exists, use that directory instead).
555: * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
556: * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
557: * If all fails, use the current directory to build.
558: *
559: * Once things are initted,
560: * have to add the original directory to the search path,
1.1 deraadt 561: * and modify the paths for the Makefiles apropriately. The
562: * current directory is also placed as a variable for make scripts.
563: */
1.9 millert 564: if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
565: if (!(path = getenv("MAKEOBJDIR"))) {
566: path = _PATH_OBJDIR;
567: pathp = _PATH_OBJDIRPREFIX;
568: (void) snprintf(mdpath, MAXPATHLEN, "%s.%s",
569: path, machine);
570: if (!(objdir = chdir_verify_path(mdpath, obpath)))
571: if (!(objdir=chdir_verify_path(path, obpath))) {
572: (void) snprintf(mdpath, MAXPATHLEN,
573: "%s%s", pathp, curdir);
574: if (!(objdir=chdir_verify_path(mdpath,
575: obpath)))
576: objdir = curdir;
577: }
578: }
579: else if (!(objdir = chdir_verify_path(path, obpath)))
1.1 deraadt 580: objdir = curdir;
581: }
582: else {
1.9 millert 583: (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir);
584: if (!(objdir = chdir_verify_path(mdpath, obpath)))
1.1 deraadt 585: objdir = curdir;
586: }
587:
588: setenv("PWD", objdir, 1);
589:
1.24 espie 590: create = Lst_Init();
591: makefiles = Lst_Init();
1.9 millert 592: printVars = FALSE;
1.24 espie 593: variables = Lst_Init();
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)
627: Dir_AddDir(dirSearchPath, curdir);
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);
656:
657: /*
658: * Initialize archive, target and suffix modules in preparation for
659: * parsing the makefile(s)
660: */
661: Arch_Init();
662: Targ_Init();
663: Suff_Init();
664:
1.25 ! espie 665: DEFAULT = NULL;
1.1 deraadt 666: (void)time(&now);
667:
668: /*
669: * Set up the .TARGETS variable to contain the list of targets to be
670: * created. If none specified, make the variable empty -- the parser
671: * will fill the thing in with the default or .MAIN target.
672: */
673: if (!Lst_IsEmpty(create)) {
674: LstNode ln;
675:
1.25 ! espie 676: for (ln = Lst_First(create); ln != NULL;
1.1 deraadt 677: ln = Lst_Succ(ln)) {
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: */
691: if (Lst_IsEmpty(sysIncPath)) {
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') {
696: Dir_AddDir(sysIncPath, start);
697: } else {
698: *cp++ = '\0';
699: Dir_AddDir(sysIncPath, start);
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;
711:
1.24 espie 712: sysMkPath = Lst_Init();
1.5 niklas 713: Dir_Expand (_PATH_DEFSYSMK, sysIncPath, sysMkPath);
714: if (Lst_IsEmpty(sysMkPath))
715: Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
716: ln = Lst_Find(sysMkPath, (ClientData)NULL, ReadMakefile);
1.25 ! espie 717: if (ln != NULL)
1.5 niklas 718: Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
719: }
1.1 deraadt 720:
721: if (!Lst_IsEmpty(makefiles)) {
722: LstNode ln;
723:
724: ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile);
1.25 ! espie 725: if (ln != NULL)
1.1 deraadt 726: Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1.13 niklas 727: } else if (!ReadMakefile("BSDmakefile", NULL))
728: if (!ReadMakefile("makefile", NULL))
729: (void)ReadMakefile("Makefile", NULL);
1.1 deraadt 730:
1.9 millert 731: (void)ReadMakefile(".depend", NULL);
1.1 deraadt 732:
1.19 espie 733: Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
1.1 deraadt 734:
735: /* Install all the flags into the MAKE envariable. */
1.19 espie 736: if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) != NULL) && *p)
1.1 deraadt 737: #ifdef POSIX
738: setenv("MAKEFLAGS", p, 1);
739: #else
740: setenv("MAKE", p, 1);
741: #endif
742:
743: /*
744: * For compatibility, look at the directories in the VPATH variable
745: * and add them to the search path, if the variable is defined. The
746: * variable's value is in the same format as the PATH envariable, i.e.
747: * <directory>:<directory>:<directory>...
748: */
749: if (Var_Exists("VPATH", VAR_CMD)) {
750: char *vpath, *path, *cp, savec;
751: /*
752: * GCC stores string constants in read-only memory, but
753: * Var_Subst will want to write this thing, so store it
754: * in an array
755: */
756: static char VPATH[] = "${VPATH}";
757:
1.23 espie 758: vpath = Var_Subst(VPATH, VAR_CMD, FALSE);
1.1 deraadt 759: path = vpath;
760: do {
761: /* skip to end of directory */
762: for (cp = path; *cp != ':' && *cp != '\0'; cp++)
763: continue;
764: /* Save terminator character so know when to stop */
765: savec = *cp;
766: *cp = '\0';
767: /* Add directory to search path */
768: Dir_AddDir(dirSearchPath, path);
769: *cp = savec;
770: path = cp + 1;
771: } while (savec == ':');
772: (void)free((Address)vpath);
773: }
774:
775: /*
776: * Now that all search paths have been read for suffixes et al, it's
777: * time to add the default search path to their lists...
778: */
779: Suff_DoPaths();
780:
781: /* print the initial graph, if the user requested it */
782: if (DEBUG(GRAPH1))
783: Targ_PrintGraph(1);
784:
1.9 millert 785: /* print the values of any variables requested by the user */
786: if (printVars) {
787: LstNode ln;
788:
1.25 ! espie 789: for (ln = Lst_First(variables); ln != NULL;
1.9 millert 790: ln = Lst_Succ(ln)) {
791: char *value = Var_Value((char *)Lst_Datum(ln),
1.19 espie 792: VAR_GLOBAL);
1.9 millert 793:
794: printf("%s\n", value ? value : "");
795: }
796: }
797:
1.1 deraadt 798: /*
799: * Have now read the entire graph and need to make a list of targets
800: * to create. If none was given on the command line, we consult the
801: * parsing module to find the main target(s) to create.
802: */
803: if (Lst_IsEmpty(create))
804: targs = Parse_MainName();
805: else
806: targs = Targ_FindList(create, TARG_CREATE);
807:
1.9 millert 808: if (!compatMake && !printVars) {
1.1 deraadt 809: /*
810: * Initialize job module before traversing the graph, now that
811: * any .BEGIN and .END targets have been read. This is done
812: * only if the -q flag wasn't given (to prevent the .BEGIN from
813: * being executed should it exist).
814: */
815: if (!queryFlag) {
816: if (maxLocal == -1)
817: maxLocal = maxJobs;
818: Job_Init(maxJobs, maxLocal);
819: jobsRunning = TRUE;
820: }
821:
822: /* Traverse the graph, checking on all the targets */
823: outOfDate = Make_Run(targs);
1.9 millert 824: } else if (!printVars) {
1.1 deraadt 825: /*
826: * Compat_Init will take care of creating all the targets as
827: * well as initializing the module.
828: */
829: Compat_Run(targs);
1.9 millert 830: }
831:
1.1 deraadt 832: Lst_Destroy(targs, NOFREE);
1.9 millert 833: Lst_Destroy(variables, NOFREE);
1.1 deraadt 834: Lst_Destroy(makefiles, NOFREE);
835: Lst_Destroy(create, (void (*) __P((ClientData))) free);
836:
837: /* print the graph now it's been processed if the user requested it */
838: if (DEBUG(GRAPH2))
839: Targ_PrintGraph(2);
840:
841: Suff_End();
842: Targ_End();
843: Arch_End();
844: Var_End();
845: Parse_End();
846: Dir_End();
1.14 espie 847: Job_End();
1.1 deraadt 848:
849: if (queryFlag && outOfDate)
850: return(1);
851: else
852: return(0);
853: }
854:
855: /*-
856: * ReadMakefile --
857: * Open and parse the given makefile.
858: *
859: * Results:
860: * TRUE if ok. FALSE if couldn't open file.
861: *
862: * Side Effects:
863: * lots
864: */
865: static Boolean
1.9 millert 866: ReadMakefile(p, q)
867: ClientData p, q;
1.1 deraadt 868: {
1.9 millert 869: char *fname = p; /* makefile to read */
1.5 niklas 870: extern Lst parseIncPath;
1.1 deraadt 871: FILE *stream;
872: char *name, path[MAXPATHLEN + 1];
873:
874: if (!strcmp(fname, "-")) {
875: Parse_File("(stdin)", stdin);
876: Var_Set("MAKEFILE", "", VAR_GLOBAL);
877: } else {
878: if ((stream = fopen(fname, "r")) != NULL)
879: goto found;
880: /* if we've chdir'd, rebuild the path name */
881: if (curdir != objdir && *fname != '/') {
882: (void)sprintf(path, "%s/%s", curdir, fname);
883: if ((stream = fopen(path, "r")) != NULL) {
884: fname = path;
885: goto found;
886: }
887: }
888: /* look in -I and system include directories. */
889: name = Dir_FindFile(fname, parseIncPath);
890: if (!name)
891: name = Dir_FindFile(fname, sysIncPath);
892: if (!name || !(stream = fopen(name, "r")))
893: return(FALSE);
894: fname = name;
895: /*
896: * set the MAKEFILE variable desired by System V fans -- the
897: * placement of the setting here means it gets set to the last
898: * makefile specified, as it is set by SysV make.
899: */
900: found: Var_Set("MAKEFILE", fname, VAR_GLOBAL);
901: Parse_File(fname, stream);
902: (void)fclose(stream);
903: }
904: return(TRUE);
905: }
906:
907: /*-
1.8 briggs 908: * Cmd_Exec --
909: * Execute the command in cmd, and return the output of that command
910: * in a string.
911: *
912: * Results:
913: * A string containing the output of the command, or the empty string
914: * If err is not NULL, it contains the reason for the command failure
915: *
916: * Side Effects:
917: * The string must be freed by the caller.
918: */
919: char *
920: Cmd_Exec(cmd, err)
921: char *cmd;
922: char **err;
923: {
924: char *args[4]; /* Args for invoking the shell */
925: int fds[2]; /* Pipe streams */
926: int cpid; /* Child PID */
927: int pid; /* PID from wait() */
928: char *res; /* result */
929: int status; /* command exit status */
1.22 espie 930: BUFFER buf; /* buffer to store the result */
1.8 briggs 931: char *cp;
1.17 espie 932: ssize_t cc;
1.18 espie 933: size_t length;
1.8 briggs 934:
935:
936: *err = NULL;
937:
938: /*
939: * Set up arguments for shell
940: */
941: args[0] = "sh";
942: args[1] = "-c";
943: args[2] = cmd;
944: args[3] = NULL;
945:
946: /*
947: * Open a pipe for fetching its output
948: */
949: if (pipe(fds) == -1) {
950: *err = "Couldn't create pipe for \"%s\"";
951: goto bad;
952: }
953:
954: /*
955: * Fork
956: */
957: switch (cpid = vfork()) {
958: case 0:
959: /*
960: * Close input side of pipe
961: */
962: (void) close(fds[0]);
963:
964: /*
965: * Duplicate the output stream to the shell's output, then
966: * shut the extra thing down. Note we don't fetch the error
967: * stream...why not? Why?
968: */
969: (void) dup2(fds[1], 1);
970: (void) close(fds[1]);
1.9 millert 971:
1.8 briggs 972: (void) execv("/bin/sh", args);
973: _exit(1);
974: /*NOTREACHED*/
975:
976: case -1:
977: *err = "Couldn't exec \"%s\"";
978: goto bad;
979:
980: default:
981: /*
982: * No need for the writing half
983: */
984: (void) close(fds[1]);
1.9 millert 985:
1.22 espie 986: Buf_Init(&buf, MAKE_BSIZE);
1.8 briggs 987:
988: do {
989: char result[BUFSIZ];
990: cc = read(fds[0], result, sizeof(result));
1.9 millert 991: if (cc > 0)
1.22 espie 992: Buf_AddChars(&buf, cc, result);
1.8 briggs 993: }
994: while (cc > 0 || (cc == -1 && errno == EINTR));
995:
996: /*
997: * Close the input side of the pipe.
998: */
999: (void) close(fds[0]);
1000:
1001: /*
1002: * Wait for the process to exit.
1003: */
1004: while(((pid = wait(&status)) != cpid) && (pid >= 0))
1005: continue;
1006:
1.22 espie 1007: res = Buf_Retrieve(&buf);
1008: length = Buf_Size(&buf);
1.8 briggs 1009:
1.17 espie 1010: if (cc == -1)
1.8 briggs 1011: *err = "Couldn't read shell's output for \"%s\"";
1012:
1013: if (status)
1014: *err = "\"%s\" returned non-zero status";
1015:
1016: /*
1017: * Null-terminate the result, convert newlines to spaces and
1018: * install it in the variable.
1019: */
1.17 espie 1020: res[length] = '\0';
1021: cp = res + length - 1;
1.8 briggs 1022:
1023: if (*cp == '\n') {
1024: /*
1025: * A final newline is just stripped
1026: */
1027: *cp-- = '\0';
1028: }
1029: while (cp >= res) {
1030: if (*cp == '\n') {
1031: *cp = ' ';
1032: }
1033: cp--;
1034: }
1035: break;
1036: }
1037: return res;
1038: bad:
1039: res = emalloc(1);
1040: *res = '\0';
1041: return res;
1042: }
1043:
1044: /*-
1.1 deraadt 1045: * Error --
1046: * Print an error message given its format.
1047: *
1048: * Results:
1049: * None.
1050: *
1051: * Side Effects:
1052: * The message is printed.
1053: */
1054: /* VARARGS */
1055: void
1.12 millert 1056: #ifdef __STDC__
1.1 deraadt 1057: Error(char *fmt, ...)
1058: #else
1059: Error(va_alist)
1060: va_dcl
1061: #endif
1062: {
1063: va_list ap;
1.12 millert 1064: #ifdef __STDC__
1.1 deraadt 1065: va_start(ap, fmt);
1066: #else
1067: char *fmt;
1068:
1069: va_start(ap);
1070: fmt = va_arg(ap, char *);
1071: #endif
1072: (void)vfprintf(stderr, fmt, ap);
1073: va_end(ap);
1074: (void)fprintf(stderr, "\n");
1075: (void)fflush(stderr);
1076: }
1077:
1078: /*-
1079: * Fatal --
1080: * Produce a Fatal error message. If jobs are running, waits for them
1081: * to finish.
1082: *
1083: * Results:
1084: * None
1085: *
1086: * Side Effects:
1087: * The program exits
1088: */
1089: /* VARARGS */
1090: void
1.12 millert 1091: #ifdef __STDC__
1.1 deraadt 1092: Fatal(char *fmt, ...)
1093: #else
1094: Fatal(va_alist)
1095: va_dcl
1096: #endif
1097: {
1098: va_list ap;
1.12 millert 1099: #ifdef __STDC__
1.1 deraadt 1100: va_start(ap, fmt);
1101: #else
1102: char *fmt;
1103:
1104: va_start(ap);
1105: fmt = va_arg(ap, char *);
1106: #endif
1107: if (jobsRunning)
1108: Job_Wait();
1109:
1110: (void)vfprintf(stderr, fmt, ap);
1111: va_end(ap);
1112: (void)fprintf(stderr, "\n");
1113: (void)fflush(stderr);
1114:
1115: if (DEBUG(GRAPH2))
1116: Targ_PrintGraph(2);
1117: exit(2); /* Not 1 so -q can distinguish error */
1118: }
1119:
1120: /*
1121: * Punt --
1122: * Major exception once jobs are being created. Kills all jobs, prints
1123: * a message and exits.
1124: *
1125: * Results:
1.9 millert 1126: * None
1.1 deraadt 1127: *
1128: * Side Effects:
1129: * All children are killed indiscriminately and the program Lib_Exits
1130: */
1131: /* VARARGS */
1132: void
1.12 millert 1133: #ifdef __STDC__
1.1 deraadt 1134: Punt(char *fmt, ...)
1135: #else
1136: Punt(va_alist)
1137: va_dcl
1138: #endif
1139: {
1140: va_list ap;
1.12 millert 1141: #ifdef __STDC__
1.1 deraadt 1142: va_start(ap, fmt);
1143: #else
1144: char *fmt;
1145:
1146: va_start(ap);
1147: fmt = va_arg(ap, char *);
1148: #endif
1149:
1150: (void)fprintf(stderr, "make: ");
1151: (void)vfprintf(stderr, fmt, ap);
1152: va_end(ap);
1153: (void)fprintf(stderr, "\n");
1154: (void)fflush(stderr);
1155:
1156: DieHorribly();
1157: }
1158:
1159: /*-
1160: * DieHorribly --
1161: * Exit without giving a message.
1162: *
1163: * Results:
1164: * None
1165: *
1166: * Side Effects:
1167: * A big one...
1168: */
1169: void
1170: DieHorribly()
1171: {
1172: if (jobsRunning)
1173: Job_AbortAll();
1174: if (DEBUG(GRAPH2))
1175: Targ_PrintGraph(2);
1176: exit(2); /* Not 1, so -q can distinguish error */
1177: }
1178:
1179: /*
1180: * Finish --
1181: * Called when aborting due to errors in child shell to signal
1.9 millert 1182: * abnormal exit.
1.1 deraadt 1183: *
1184: * Results:
1.9 millert 1185: * None
1.1 deraadt 1186: *
1187: * Side Effects:
1188: * The program exits
1189: */
1190: void
1191: Finish(errors)
1192: int errors; /* number of errors encountered in Make_Make */
1193: {
1194: Fatal("%d error%s", errors, errors == 1 ? "" : "s");
1195: }
1196:
1197: /*
1198: * usage --
1199: * exit with usage message
1200: */
1201: static void
1202: usage()
1203: {
1204: (void)fprintf(stderr,
1.9 millert 1205: "usage: make [-Beiknqrst] [-D variable] [-d flags] [-f makefile ]\n\
1206: [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\
1207: [variable=value] [target ...]\n");
1.1 deraadt 1208: exit(2);
1209: }
1210:
1211:
1212: int
1213: PrintAddr(a, b)
1214: ClientData a;
1215: ClientData b;
1216: {
1217: printf("%lx ", (unsigned long) a);
1218: return b ? 0 : 0;
1219: }