xref: /original-bsd/usr.bin/make/main.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char copyright[] =
15 "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
16 	The Regents of the University of California.  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 06/06/93";
21 #endif /* not lint */
22 
23 /*-
24  * main.c --
25  *	The main file for this entire program. Exit routines etc
26  *	reside here.
27  *
28  * Utility functions defined in this file:
29  *	Main_ParseArgLine	Takes a line of arguments, breaks them and
30  *				treats them as if they were given when first
31  *				invoked. Used by the parse module to implement
32  *				the .MFLAGS target.
33  *
34  *	Error			Print a tagged error message. The global
35  *				MAKE variable must have been defined. This
36  *				takes a format string and two optional
37  *				arguments for it.
38  *
39  *	Fatal			Print an error message and exit. Also takes
40  *				a format string and two arguments.
41  *
42  *	Punt			Aborts all jobs and exits with a message. Also
43  *				takes a format string and two arguments.
44  *
45  *	Finish			Finish things up by printing the number of
46  *				errors which occured, as passed to it, and
47  *				exiting.
48  */
49 
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <sys/param.h>
53 #include <sys/resource.h>
54 #include <sys/signal.h>
55 #include <sys/stat.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <stdio.h>
59 #if __STDC__
60 #include <stdarg.h>
61 #else
62 #include <varargs.h>
63 #endif
64 #include "make.h"
65 #include "hash.h"
66 #include "dir.h"
67 #include "job.h"
68 #include "pathnames.h"
69 
70 #ifndef	DEFMAXLOCAL
71 #define	DEFMAXLOCAL DEFMAXJOBS
72 #endif	DEFMAXLOCAL
73 
74 #define	MAKEFLAGS	".MAKEFLAGS"
75 
76 Lst			create;		/* Targets to be made */
77 time_t			now;		/* Time at start of make */
78 GNode			*DEFAULT;	/* .DEFAULT node */
79 Boolean			allPrecious;	/* .PRECIOUS given on line by itself */
80 
81 static Boolean		noBuiltins;	/* -r flag */
82 static Lst		makefiles;	/* ordered list of makefiles to read */
83 int			maxJobs;	/* -J argument */
84 static int		maxLocal;	/* -L argument */
85 Boolean			compatMake;	/* -B argument */
86 Boolean			debug;		/* -d flag */
87 Boolean			noExecute;	/* -n flag */
88 Boolean			keepgoing;	/* -k flag */
89 Boolean			queryFlag;	/* -q flag */
90 Boolean			touchFlag;	/* -t flag */
91 Boolean			usePipes;	/* !-P flag */
92 Boolean			ignoreErrors;	/* -i flag */
93 Boolean			beSilent;	/* -s flag */
94 Boolean			oldVars;	/* variable substitution style */
95 Boolean			checkEnvFirst;	/* -e flag */
96 static Boolean		jobsRunning;	/* TRUE if the jobs might be running */
97 
98 static Boolean		ReadMakefile();
99 static void		usage();
100 
101 static char *curdir;			/* if chdir'd for an architecture */
102 
103 /*-
104  * MainParseArgs --
105  *	Parse a given argument vector. Called from main() and from
106  *	Main_ParseArgLine() when the .MAKEFLAGS target is used.
107  *
108  *	XXX: Deal with command line overriding .MAKEFLAGS in makefile
109  *
110  * Results:
111  *	None
112  *
113  * Side Effects:
114  *	Various global and local flags will be set depending on the flags
115  *	given
116  */
117 static void
118 MainParseArgs(argc, argv)
119 	int argc;
120 	char **argv;
121 {
122 	extern int optind;
123 	extern char *optarg;
124 	int c;
125 
126 	optind = 1;	/* since we're called more than once */
127 #ifdef notyet
128 # define OPTFLAGS "BD:I:L:PSd:ef:ij:knqrst"
129 #else
130 # define OPTFLAGS "D:I:d:ef:ij:knqrst"
131 #endif
132 rearg:	while ((c = getopt(argc, argv, OPTFLAGS)) != EOF) {
133 		switch(c) {
134 		case 'D':
135 			Var_Set(optarg, "1", VAR_GLOBAL);
136 			Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
137 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
138 			break;
139 		case 'I':
140 			Parse_AddIncludeDir(optarg);
141 			Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
142 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
143 			break;
144 #ifdef notyet
145 		case 'B':
146 			compatMake = TRUE;
147 			break;
148 		case 'L':
149 			maxLocal = atoi(optarg);
150 			Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
151 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
152 			break;
153 		case 'P':
154 			usePipes = FALSE;
155 			Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
156 			break;
157 		case 'S':
158 			keepgoing = FALSE;
159 			Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
160 			break;
161 #endif
162 		case 'd': {
163 			char *modules = optarg;
164 
165 			for (; *modules; ++modules)
166 				switch (*modules) {
167 				case 'A':
168 					debug = ~0;
169 					break;
170 				case 'a':
171 					debug |= DEBUG_ARCH;
172 					break;
173 				case 'c':
174 					debug |= DEBUG_COND;
175 					break;
176 				case 'd':
177 					debug |= DEBUG_DIR;
178 					break;
179 				case 'f':
180 					debug |= DEBUG_FOR;
181 					break;
182 				case 'g':
183 					if (modules[1] == '1') {
184 						debug |= DEBUG_GRAPH1;
185 						++modules;
186 					}
187 					else if (modules[1] == '2') {
188 						debug |= DEBUG_GRAPH2;
189 						++modules;
190 					}
191 					break;
192 				case 'j':
193 					debug |= DEBUG_JOB;
194 					break;
195 				case 'm':
196 					debug |= DEBUG_MAKE;
197 					break;
198 				case 's':
199 					debug |= DEBUG_SUFF;
200 					break;
201 				case 't':
202 					debug |= DEBUG_TARG;
203 					break;
204 				case 'v':
205 					debug |= DEBUG_VAR;
206 					break;
207 				default:
208 					(void)fprintf(stderr,
209 				"make: illegal argument to d option -- %c\n",
210 					    *modules);
211 					usage();
212 				}
213 			Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
214 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
215 			break;
216 		}
217 		case 'e':
218 			checkEnvFirst = TRUE;
219 			Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
220 			break;
221 		case 'f':
222 			(void)Lst_AtEnd(makefiles, (ClientData)optarg);
223 			break;
224 		case 'i':
225 			ignoreErrors = TRUE;
226 			Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
227 			break;
228 		case 'j':
229 			maxJobs = atoi(optarg);
230 			Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
231 			Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
232 			break;
233 		case 'k':
234 			keepgoing = TRUE;
235 			Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
236 			break;
237 		case 'n':
238 			noExecute = TRUE;
239 			Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
240 			break;
241 		case 'q':
242 			queryFlag = TRUE;
243 			/* Kind of nonsensical, wot? */
244 			Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
245 			break;
246 		case 'r':
247 			noBuiltins = TRUE;
248 			Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
249 			break;
250 		case 's':
251 			beSilent = TRUE;
252 			Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
253 			break;
254 		case 't':
255 			touchFlag = TRUE;
256 			Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
257 			break;
258 		default:
259 		case '?':
260 			usage();
261 		}
262 	}
263 
264 	oldVars = TRUE;
265 
266 	/*
267 	 * See if the rest of the arguments are variable assignments and
268 	 * perform them if so. Else take them to be targets and stuff them
269 	 * on the end of the "create" list.
270 	 */
271 	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
272 		if (Parse_IsVar(*argv))
273 			Parse_DoVar(*argv, VAR_CMD);
274 		else {
275 			if (!*argv[0] || *argv[0] == '-' && !(*argv)[1])
276 				Punt("illegal (null) argument.");
277 			if (**argv == '-') {
278 				optind = 0;
279 				goto rearg;
280 			}
281 			(void)Lst_AtEnd(create, (ClientData)*argv);
282 		}
283 }
284 
285 /*-
286  * Main_ParseArgLine --
287  *  	Used by the parse module when a .MFLAGS or .MAKEFLAGS target
288  *	is encountered and by main() when reading the .MAKEFLAGS envariable.
289  *	Takes a line of arguments and breaks it into its
290  * 	component words and passes those words and the number of them to the
291  *	MainParseArgs function.
292  *	The line should have all its leading whitespace removed.
293  *
294  * Results:
295  *	None
296  *
297  * Side Effects:
298  *	Only those that come from the various arguments.
299  */
300 void
301 Main_ParseArgLine(line)
302 	char *line;			/* Line to fracture */
303 {
304 	char **argv;			/* Manufactured argument vector */
305 	int argc;			/* Number of arguments in argv */
306 
307 	if (line == NULL)
308 		return;
309 	for (; *line == ' '; ++line)
310 		continue;
311 	if (!*line)
312 		return;
313 
314 	argv = brk_string(line, &argc);
315 	MainParseArgs(argc, argv);
316 }
317 
318 /*-
319  * main --
320  *	The main function, for obvious reasons. Initializes variables
321  *	and a few modules, then parses the arguments give it in the
322  *	environment and on the command line. Reads the system makefile
323  *	followed by either Makefile, makefile or the file given by the
324  *	-f argument. Sets the .MAKEFLAGS PMake variable based on all the
325  *	flags it has received by then uses either the Make or the Compat
326  *	module to create the initial list of targets.
327  *
328  * Results:
329  *	If -q was given, exits -1 if anything was out-of-date. Else it exits
330  *	0.
331  *
332  * Side Effects:
333  *	The program exits when done. Targets are created. etc. etc. etc.
334  */
335 int
336 main(argc, argv)
337 	int argc;
338 	char **argv;
339 {
340 	Lst targs;	/* target nodes to create -- passed to Make_Init */
341 	Boolean outOfDate = TRUE; 	/* FALSE if all targets up to date */
342 	struct stat sb, sa;
343 	char *p, *path, *pwd, *getenv();
344 
345 	/*
346 	 * Find where we are and take care of PWD for the automounter...
347 	 */
348 	curdir = emalloc((u_int)MAXPATHLEN + 1);
349 	if (!getwd(curdir)) {
350 		(void)fprintf(stderr, "make: %s.\n", curdir);
351 		exit(2);
352 	}
353 
354 	if (stat(curdir, &sa) == -1) {
355 	    (void)fprintf(stderr, "make: %s: %s.\n",
356 			  curdir, strerror(errno));
357 	    exit(2);
358 	}
359 
360 	if ((pwd = getenv("PWD")) != NULL) {
361 	    if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
362 		sa.st_dev == sb.st_dev)
363 		(void) strcpy(curdir, pwd);
364 	}
365 
366 
367 	/*
368 	 * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
369 	 * exists, change into it and build there.  Once things are
370 	 * initted, have to add the original directory to the search path,
371 	 * and modify the paths for the Makefiles apropriately.  The
372 	 * current directory is also placed as a variable for make scripts.
373 	 */
374 	if (!(path = getenv("MAKEOBJDIR")))
375 		path = _PATH_OBJDIR;
376 
377 	if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode) &&
378 	    lstat(path, &sb) == 0) {
379 		if (chdir(path)) {
380 			(void)fprintf(stderr, "make: %s: %s.\n",
381 			    path, strerror(errno));
382 			exit(2);
383 		}
384 		if (path[0] != '/') {
385 			char cwd[MAXPATHLEN];
386 			(void) sprintf(cwd, "%s/%s", curdir, path);
387 			setenv("PWD", cwd, 1);
388 		}
389 		else
390 			setenv("PWD", path, 1);
391 	}
392 	else
393 		setenv("PWD", curdir, 1);
394 
395 	create = Lst_Init(FALSE);
396 	makefiles = Lst_Init(FALSE);
397 	beSilent = FALSE;		/* Print commands as executed */
398 	ignoreErrors = FALSE;		/* Pay attention to non-zero returns */
399 	noExecute = FALSE;		/* Execute all commands */
400 	keepgoing = FALSE;		/* Stop on error */
401 	allPrecious = FALSE;		/* Remove targets when interrupted */
402 	queryFlag = FALSE;		/* This is not just a check-run */
403 	noBuiltins = FALSE;		/* Read the built-in rules */
404 	touchFlag = FALSE;		/* Actually update targets */
405 	usePipes = TRUE;		/* Catch child output in pipes */
406 	debug = 0;			/* No debug verbosity, please. */
407 	jobsRunning = FALSE;
408 
409 	maxJobs = DEFMAXJOBS;		/* Set default max concurrency */
410 	maxLocal = DEFMAXLOCAL;		/* Set default local max concurrency */
411 #ifdef notyet
412 	compatMake = FALSE;		/* No compat mode */
413 #else
414 	compatMake = TRUE;		/* No compat mode */
415 #endif
416 
417 
418 	/*
419 	 * Initialize the parsing, directory and variable modules to prepare
420 	 * for the reading of inclusion paths and variable settings on the
421 	 * command line
422 	 */
423 	Dir_Init();		/* Initialize directory structures so -I flags
424 				 * can be processed correctly */
425 	Parse_Init();		/* Need to initialize the paths of #include
426 				 * directories */
427 	Var_Init();		/* As well as the lists of variables for
428 				 * parsing arguments */
429 	if (curdir) {
430 		Dir_AddDir(dirSearchPath, curdir);
431 		Var_Set(".CURDIR", curdir, VAR_GLOBAL);
432 	}
433 	Var_Set(".OBJDIR", path, VAR_GLOBAL);
434 
435 	/*
436 	 * Initialize various variables.
437 	 *	MAKE also gets this name, for compatibility
438 	 *	.MAKEFLAGS gets set to the empty string just in case.
439 	 *	MFLAGS also gets initialized empty, for compatibility.
440 	 */
441 	Var_Set("MAKE", argv[0], VAR_GLOBAL);
442 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
443 	Var_Set("MFLAGS", "", VAR_GLOBAL);
444 	Var_Set("MACHINE", MACHINE, VAR_GLOBAL);
445 
446 	/*
447 	 * First snag any flags out of the MAKE environment variable.
448 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
449 	 * in a different format).
450 	 */
451 #ifdef POSIX
452 	Main_ParseArgLine(getenv("MAKEFLAGS"));
453 #else
454 	Main_ParseArgLine(getenv("MAKE"));
455 #endif
456 
457 	MainParseArgs(argc, argv);
458 
459 	/*
460 	 * Initialize archive, target and suffix modules in preparation for
461 	 * parsing the makefile(s)
462 	 */
463 	Arch_Init();
464 	Targ_Init();
465 	Suff_Init();
466 
467 	DEFAULT = NILGNODE;
468 	(void)time(&now);
469 
470 	/*
471 	 * Set up the .TARGETS variable to contain the list of targets to be
472 	 * created. If none specified, make the variable empty -- the parser
473 	 * will fill the thing in with the default or .MAIN target.
474 	 */
475 	if (!Lst_IsEmpty(create)) {
476 		LstNode ln;
477 
478 		for (ln = Lst_First(create); ln != NILLNODE;
479 		    ln = Lst_Succ(ln)) {
480 			char *name = (char *)Lst_Datum(ln);
481 
482 			Var_Append(".TARGETS", name, VAR_GLOBAL);
483 		}
484 	} else
485 		Var_Set(".TARGETS", "", VAR_GLOBAL);
486 
487 	/*
488 	 * Read in the built-in rules first, followed by the specified makefile,
489 	 * if it was (makefile != (char *) NULL), or the default Makefile and
490 	 * makefile, in that order, if it wasn't.
491 	 */
492 	 if (!noBuiltins && !ReadMakefile(_PATH_DEFSYSMK))
493 		Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
494 
495 	if (!Lst_IsEmpty(makefiles)) {
496 		LstNode ln;
497 
498 		ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile);
499 		if (ln != NILLNODE)
500 			Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
501 	} else if (!ReadMakefile("makefile"))
502 		(void)ReadMakefile("Makefile");
503 
504 	(void)ReadMakefile(".depend");
505 
506 	Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
507 
508 	/* Install all the flags into the MAKE envariable. */
509 	if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) != NULL) && *p)
510 #ifdef POSIX
511 		setenv("MAKEFLAGS", p, 1);
512 #else
513 		setenv("MAKE", p, 1);
514 #endif
515 
516 	/*
517 	 * For compatibility, look at the directories in the VPATH variable
518 	 * and add them to the search path, if the variable is defined. The
519 	 * variable's value is in the same format as the PATH envariable, i.e.
520 	 * <directory>:<directory>:<directory>...
521 	 */
522 	if (Var_Exists("VPATH", VAR_CMD)) {
523 		char *vpath, *path, *cp, savec;
524 		/*
525 		 * GCC stores string constants in read-only memory, but
526 		 * Var_Subst will want to write this thing, so store it
527 		 * in an array
528 		 */
529 		static char VPATH[] = "${VPATH}";
530 
531 		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
532 		path = vpath;
533 		do {
534 			/* skip to end of directory */
535 			for (cp = path; *cp != ':' && *cp != '\0'; cp++)
536 				continue;
537 			/* Save terminator character so know when to stop */
538 			savec = *cp;
539 			*cp = '\0';
540 			/* Add directory to search path */
541 			Dir_AddDir(dirSearchPath, path);
542 			*cp = savec;
543 			path = cp + 1;
544 		} while (savec == ':');
545 		(void)free((Address)vpath);
546 	}
547 
548 	/*
549 	 * Now that all search paths have been read for suffixes et al, it's
550 	 * time to add the default search path to their lists...
551 	 */
552 	Suff_DoPaths();
553 
554 	/* print the initial graph, if the user requested it */
555 	if (DEBUG(GRAPH1))
556 		Targ_PrintGraph(1);
557 
558 	/*
559 	 * Have now read the entire graph and need to make a list of targets
560 	 * to create. If none was given on the command line, we consult the
561 	 * parsing module to find the main target(s) to create.
562 	 */
563 	if (Lst_IsEmpty(create))
564 		targs = Parse_MainName();
565 	else
566 		targs = Targ_FindList(create, TARG_CREATE);
567 
568 /*
569  * this was original amMake -- want to allow parallelism, so put this
570  * back in, eventually.
571  */
572 	if (!compatMake) {
573 		/*
574 		 * Initialize job module before traversing the graph, now that
575 		 * any .BEGIN and .END targets have been read.  This is done
576 		 * only if the -q flag wasn't given (to prevent the .BEGIN from
577 		 * being executed should it exist).
578 		 */
579 		if (!queryFlag) {
580 			if (maxLocal == -1)
581 				maxLocal = maxJobs;
582 			Job_Init(maxJobs, maxLocal);
583 			jobsRunning = TRUE;
584 		}
585 
586 		/* Traverse the graph, checking on all the targets */
587 		outOfDate = Make_Run(targs);
588 	} else
589 		/*
590 		 * Compat_Init will take care of creating all the targets as
591 		 * well as initializing the module.
592 		 */
593 		Compat_Run(targs);
594 
595 	/* print the graph now it's been processed if the user requested it */
596 	if (DEBUG(GRAPH2))
597 		Targ_PrintGraph(2);
598 
599 	if (queryFlag && outOfDate)
600 		return(1);
601 	else
602 		return(0);
603 }
604 
605 /*-
606  * ReadMakefile  --
607  *	Open and parse the given makefile.
608  *
609  * Results:
610  *	TRUE if ok. FALSE if couldn't open file.
611  *
612  * Side Effects:
613  *	lots
614  */
615 static Boolean
616 ReadMakefile(fname)
617 	char *fname;		/* makefile to read */
618 {
619 	extern Lst parseIncPath, sysIncPath;
620 	FILE *stream;
621 	char *name, path[MAXPATHLEN + 1];
622 
623 	if (!strcmp(fname, "-")) {
624 		Parse_File("(stdin)", stdin);
625 		Var_Set("MAKEFILE", "", VAR_GLOBAL);
626 	} else {
627 		if ((stream = fopen(fname, "r")) != NULL)
628 			goto found;
629 		/* if we've chdir'd, rebuild the path name */
630 		if (curdir && *fname != '/') {
631 			(void)sprintf(path, "%s/%s", curdir, fname);
632 			if ((stream = fopen(path, "r")) != NULL) {
633 				fname = path;
634 				goto found;
635 			}
636 		}
637 		/* look in -I and system include directories. */
638 		name = Dir_FindFile(fname, parseIncPath);
639 		if (!name)
640 			name = Dir_FindFile(fname, sysIncPath);
641 		if (!name || !(stream = fopen(name, "r")))
642 			return(FALSE);
643 		fname = name;
644 		/*
645 		 * set the MAKEFILE variable desired by System V fans -- the
646 		 * placement of the setting here means it gets set to the last
647 		 * makefile specified, as it is set by SysV make.
648 		 */
649 found:		Var_Set("MAKEFILE", fname, VAR_GLOBAL);
650 		Parse_File(fname, stream);
651 		(void)fclose(stream);
652 	}
653 	return(TRUE);
654 }
655 
656 /*-
657  * Error --
658  *	Print an error message given its format.
659  *
660  * Results:
661  *	None.
662  *
663  * Side Effects:
664  *	The message is printed.
665  */
666 /* VARARGS */
667 void
668 #if __STDC__
669 Error(const char *fmt, ...)
670 #else
671 Error(fmt, va_alist)
672 	char *fmt;
673 	va_dcl
674 #endif
675 {
676 	va_list ap;
677 #if __STDC__
678 	va_start(ap, fmt);
679 #else
680 	va_start(ap);
681 #endif
682 	(void)vfprintf(stderr, fmt, ap);
683 	va_end(ap);
684 	(void)fprintf(stderr, "\n");
685 	(void)fflush(stderr);
686 }
687 
688 /*-
689  * Fatal --
690  *	Produce a Fatal error message. If jobs are running, waits for them
691  *	to finish.
692  *
693  * Results:
694  *	None
695  *
696  * Side Effects:
697  *	The program exits
698  */
699 /* VARARGS */
700 void
701 #if __STDC__
702 Fatal(const char *fmt, ...)
703 #else
704 Fatal(fmt, va_alist)
705 	char *fmt;
706 	va_dcl
707 #endif
708 {
709 	va_list ap;
710 #if __STDC__
711 	va_start(ap, fmt);
712 #else
713 	va_start(ap);
714 #endif
715 	if (jobsRunning)
716 		Job_Wait();
717 
718 	(void)vfprintf(stderr, fmt, ap);
719 	va_end(ap);
720 	(void)fprintf(stderr, "\n");
721 	(void)fflush(stderr);
722 
723 	if (DEBUG(GRAPH2))
724 		Targ_PrintGraph(2);
725 	exit(2);		/* Not 1 so -q can distinguish error */
726 }
727 
728 /*
729  * Punt --
730  *	Major exception once jobs are being created. Kills all jobs, prints
731  *	a message and exits.
732  *
733  * Results:
734  *	None
735  *
736  * Side Effects:
737  *	All children are killed indiscriminately and the program Lib_Exits
738  */
739 /* VARARGS */
740 void
741 #if __STDC__
742 Punt(const char *fmt, ...)
743 #else
744 Punt(fmt, va_alist)
745 	char *fmt;
746 	va_dcl
747 #endif
748 {
749 	va_list ap;
750 #if __STDC__
751 	va_start(ap, fmt);
752 #else
753 	va_start(ap);
754 #endif
755 	(void)fprintf(stderr, "make: ");
756 	(void)vfprintf(stderr, fmt, ap);
757 	va_end(ap);
758 	(void)fprintf(stderr, "\n");
759 	(void)fflush(stderr);
760 
761 	DieHorribly();
762 }
763 
764 /*-
765  * DieHorribly --
766  *	Exit without giving a message.
767  *
768  * Results:
769  *	None
770  *
771  * Side Effects:
772  *	A big one...
773  */
774 void
775 DieHorribly()
776 {
777 	if (jobsRunning)
778 		Job_AbortAll();
779 	if (DEBUG(GRAPH2))
780 		Targ_PrintGraph(2);
781 	exit(2);		/* Not 1, so -q can distinguish error */
782 }
783 
784 /*
785  * Finish --
786  *	Called when aborting due to errors in child shell to signal
787  *	abnormal exit.
788  *
789  * Results:
790  *	None
791  *
792  * Side Effects:
793  *	The program exits
794  */
795 void
796 Finish(errors)
797 	int errors;	/* number of errors encountered in Make_Make */
798 {
799 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
800 }
801 
802 /*
803  * emalloc --
804  *	malloc, but die on error.
805  */
806 char *
807 emalloc(len)
808 	u_int len;
809 {
810 	char *p;
811 
812 	if (!(p = malloc(len)))
813 		enomem();
814 	return(p);
815 }
816 
817 /*
818  * enomem --
819  *	die when out of memory.
820  */
821 void
822 enomem()
823 {
824 	(void)fprintf(stderr, "make: %s.\n", strerror(errno));
825 	exit(2);
826 }
827 
828 /*
829  * usage --
830  *	exit with usage message
831  */
832 static void
833 usage()
834 {
835 	(void)fprintf(stderr,
836 "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\
837             [-I directory] [-j max_jobs] [variable=value]\n");
838 	exit(2);
839 }
840