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