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