xref: /original-bsd/usr.bin/make/main.c (revision a516d9f1)
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.2 (Berkeley) 01/02/94";
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 		path = ".";
395 	}
396 
397 	create = Lst_Init(FALSE);
398 	makefiles = Lst_Init(FALSE);
399 	beSilent = FALSE;		/* Print commands as executed */
400 	ignoreErrors = FALSE;		/* Pay attention to non-zero returns */
401 	noExecute = FALSE;		/* Execute all commands */
402 	keepgoing = FALSE;		/* Stop on error */
403 	allPrecious = FALSE;		/* Remove targets when interrupted */
404 	queryFlag = FALSE;		/* This is not just a check-run */
405 	noBuiltins = FALSE;		/* Read the built-in rules */
406 	touchFlag = FALSE;		/* Actually update targets */
407 	usePipes = TRUE;		/* Catch child output in pipes */
408 	debug = 0;			/* No debug verbosity, please. */
409 	jobsRunning = FALSE;
410 
411 	maxJobs = DEFMAXJOBS;		/* Set default max concurrency */
412 	maxLocal = DEFMAXLOCAL;		/* Set default local max concurrency */
413 #ifdef notyet
414 	compatMake = FALSE;		/* No compat mode */
415 #else
416 	compatMake = TRUE;		/* No compat mode */
417 #endif
418 
419 
420 	/*
421 	 * Initialize the parsing, directory and variable modules to prepare
422 	 * for the reading of inclusion paths and variable settings on the
423 	 * command line
424 	 */
425 	Dir_Init();		/* Initialize directory structures so -I flags
426 				 * can be processed correctly */
427 	Parse_Init();		/* Need to initialize the paths of #include
428 				 * directories */
429 	Var_Init();		/* As well as the lists of variables for
430 				 * parsing arguments */
431 	if (curdir) {
432 		Dir_AddDir(dirSearchPath, curdir);
433 		Var_Set(".CURDIR", curdir, VAR_GLOBAL);
434 	}
435 	Var_Set(".OBJDIR", path, VAR_GLOBAL);
436 
437 	/*
438 	 * Initialize various variables.
439 	 *	MAKE also gets this name, for compatibility
440 	 *	.MAKEFLAGS gets set to the empty string just in case.
441 	 *	MFLAGS also gets initialized empty, for compatibility.
442 	 */
443 	Var_Set("MAKE", argv[0], VAR_GLOBAL);
444 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
445 	Var_Set("MFLAGS", "", VAR_GLOBAL);
446 	Var_Set("MACHINE", MACHINE, VAR_GLOBAL);
447 
448 	/*
449 	 * First snag any flags out of the MAKE environment variable.
450 	 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
451 	 * in a different format).
452 	 */
453 #ifdef POSIX
454 	Main_ParseArgLine(getenv("MAKEFLAGS"));
455 #else
456 	Main_ParseArgLine(getenv("MAKE"));
457 #endif
458 
459 	MainParseArgs(argc, argv);
460 
461 	/*
462 	 * Initialize archive, target and suffix modules in preparation for
463 	 * parsing the makefile(s)
464 	 */
465 	Arch_Init();
466 	Targ_Init();
467 	Suff_Init();
468 
469 	DEFAULT = NILGNODE;
470 	(void)time(&now);
471 
472 	/*
473 	 * Set up the .TARGETS variable to contain the list of targets to be
474 	 * created. If none specified, make the variable empty -- the parser
475 	 * will fill the thing in with the default or .MAIN target.
476 	 */
477 	if (!Lst_IsEmpty(create)) {
478 		LstNode ln;
479 
480 		for (ln = Lst_First(create); ln != NILLNODE;
481 		    ln = Lst_Succ(ln)) {
482 			char *name = (char *)Lst_Datum(ln);
483 
484 			Var_Append(".TARGETS", name, VAR_GLOBAL);
485 		}
486 	} else
487 		Var_Set(".TARGETS", "", VAR_GLOBAL);
488 
489 	/*
490 	 * Read in the built-in rules first, followed by the specified makefile,
491 	 * if it was (makefile != (char *) NULL), or the default Makefile and
492 	 * makefile, in that order, if it wasn't.
493 	 */
494 	 if (!noBuiltins && !ReadMakefile(_PATH_DEFSYSMK))
495 		Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
496 
497 	if (!Lst_IsEmpty(makefiles)) {
498 		LstNode ln;
499 
500 		ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile);
501 		if (ln != NILLNODE)
502 			Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
503 	} else if (!ReadMakefile("makefile"))
504 		(void)ReadMakefile("Makefile");
505 
506 	(void)ReadMakefile(".depend");
507 
508 	Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
509 
510 	/* Install all the flags into the MAKE envariable. */
511 	if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) != NULL) && *p)
512 #ifdef POSIX
513 		setenv("MAKEFLAGS", p, 1);
514 #else
515 		setenv("MAKE", p, 1);
516 #endif
517 
518 	/*
519 	 * For compatibility, look at the directories in the VPATH variable
520 	 * and add them to the search path, if the variable is defined. The
521 	 * variable's value is in the same format as the PATH envariable, i.e.
522 	 * <directory>:<directory>:<directory>...
523 	 */
524 	if (Var_Exists("VPATH", VAR_CMD)) {
525 		char *vpath, *path, *cp, savec;
526 		/*
527 		 * GCC stores string constants in read-only memory, but
528 		 * Var_Subst will want to write this thing, so store it
529 		 * in an array
530 		 */
531 		static char VPATH[] = "${VPATH}";
532 
533 		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
534 		path = vpath;
535 		do {
536 			/* skip to end of directory */
537 			for (cp = path; *cp != ':' && *cp != '\0'; cp++)
538 				continue;
539 			/* Save terminator character so know when to stop */
540 			savec = *cp;
541 			*cp = '\0';
542 			/* Add directory to search path */
543 			Dir_AddDir(dirSearchPath, path);
544 			*cp = savec;
545 			path = cp + 1;
546 		} while (savec == ':');
547 		(void)free((Address)vpath);
548 	}
549 
550 	/*
551 	 * Now that all search paths have been read for suffixes et al, it's
552 	 * time to add the default search path to their lists...
553 	 */
554 	Suff_DoPaths();
555 
556 	/* print the initial graph, if the user requested it */
557 	if (DEBUG(GRAPH1))
558 		Targ_PrintGraph(1);
559 
560 	/*
561 	 * Have now read the entire graph and need to make a list of targets
562 	 * to create. If none was given on the command line, we consult the
563 	 * parsing module to find the main target(s) to create.
564 	 */
565 	if (Lst_IsEmpty(create))
566 		targs = Parse_MainName();
567 	else
568 		targs = Targ_FindList(create, TARG_CREATE);
569 
570 /*
571  * this was original amMake -- want to allow parallelism, so put this
572  * back in, eventually.
573  */
574 	if (!compatMake) {
575 		/*
576 		 * Initialize job module before traversing the graph, now that
577 		 * any .BEGIN and .END targets have been read.  This is done
578 		 * only if the -q flag wasn't given (to prevent the .BEGIN from
579 		 * being executed should it exist).
580 		 */
581 		if (!queryFlag) {
582 			if (maxLocal == -1)
583 				maxLocal = maxJobs;
584 			Job_Init(maxJobs, maxLocal);
585 			jobsRunning = TRUE;
586 		}
587 
588 		/* Traverse the graph, checking on all the targets */
589 		outOfDate = Make_Run(targs);
590 	} else
591 		/*
592 		 * Compat_Init will take care of creating all the targets as
593 		 * well as initializing the module.
594 		 */
595 		Compat_Run(targs);
596 
597 	/* print the graph now it's been processed if the user requested it */
598 	if (DEBUG(GRAPH2))
599 		Targ_PrintGraph(2);
600 
601 	if (queryFlag && outOfDate)
602 		return(1);
603 	else
604 		return(0);
605 }
606 
607 /*-
608  * ReadMakefile  --
609  *	Open and parse the given makefile.
610  *
611  * Results:
612  *	TRUE if ok. FALSE if couldn't open file.
613  *
614  * Side Effects:
615  *	lots
616  */
617 static Boolean
618 ReadMakefile(fname)
619 	char *fname;		/* makefile to read */
620 {
621 	extern Lst parseIncPath, sysIncPath;
622 	FILE *stream;
623 	char *name, path[MAXPATHLEN + 1];
624 
625 	if (!strcmp(fname, "-")) {
626 		Parse_File("(stdin)", stdin);
627 		Var_Set("MAKEFILE", "", VAR_GLOBAL);
628 	} else {
629 		if ((stream = fopen(fname, "r")) != NULL)
630 			goto found;
631 		/* if we've chdir'd, rebuild the path name */
632 		if (curdir && *fname != '/') {
633 			(void)sprintf(path, "%s/%s", curdir, fname);
634 			if ((stream = fopen(path, "r")) != NULL) {
635 				fname = path;
636 				goto found;
637 			}
638 		}
639 		/* look in -I and system include directories. */
640 		name = Dir_FindFile(fname, parseIncPath);
641 		if (!name)
642 			name = Dir_FindFile(fname, sysIncPath);
643 		if (!name || !(stream = fopen(name, "r")))
644 			return(FALSE);
645 		fname = name;
646 		/*
647 		 * set the MAKEFILE variable desired by System V fans -- the
648 		 * placement of the setting here means it gets set to the last
649 		 * makefile specified, as it is set by SysV make.
650 		 */
651 found:		Var_Set("MAKEFILE", fname, VAR_GLOBAL);
652 		Parse_File(fname, stream);
653 		(void)fclose(stream);
654 	}
655 	return(TRUE);
656 }
657 
658 /*-
659  * Error --
660  *	Print an error message given its format.
661  *
662  * Results:
663  *	None.
664  *
665  * Side Effects:
666  *	The message is printed.
667  */
668 /* VARARGS */
669 void
670 #if __STDC__
671 Error(const char *fmt, ...)
672 #else
673 Error(fmt, va_alist)
674 	char *fmt;
675 	va_dcl
676 #endif
677 {
678 	va_list ap;
679 #if __STDC__
680 	va_start(ap, fmt);
681 #else
682 	va_start(ap);
683 #endif
684 	(void)vfprintf(stderr, fmt, ap);
685 	va_end(ap);
686 	(void)fprintf(stderr, "\n");
687 	(void)fflush(stderr);
688 }
689 
690 /*-
691  * Fatal --
692  *	Produce a Fatal error message. If jobs are running, waits for them
693  *	to finish.
694  *
695  * Results:
696  *	None
697  *
698  * Side Effects:
699  *	The program exits
700  */
701 /* VARARGS */
702 void
703 #if __STDC__
704 Fatal(const char *fmt, ...)
705 #else
706 Fatal(fmt, va_alist)
707 	char *fmt;
708 	va_dcl
709 #endif
710 {
711 	va_list ap;
712 #if __STDC__
713 	va_start(ap, fmt);
714 #else
715 	va_start(ap);
716 #endif
717 	if (jobsRunning)
718 		Job_Wait();
719 
720 	(void)vfprintf(stderr, fmt, ap);
721 	va_end(ap);
722 	(void)fprintf(stderr, "\n");
723 	(void)fflush(stderr);
724 
725 	if (DEBUG(GRAPH2))
726 		Targ_PrintGraph(2);
727 	exit(2);		/* Not 1 so -q can distinguish error */
728 }
729 
730 /*
731  * Punt --
732  *	Major exception once jobs are being created. Kills all jobs, prints
733  *	a message and exits.
734  *
735  * Results:
736  *	None
737  *
738  * Side Effects:
739  *	All children are killed indiscriminately and the program Lib_Exits
740  */
741 /* VARARGS */
742 void
743 #if __STDC__
744 Punt(const char *fmt, ...)
745 #else
746 Punt(fmt, va_alist)
747 	char *fmt;
748 	va_dcl
749 #endif
750 {
751 	va_list ap;
752 #if __STDC__
753 	va_start(ap, fmt);
754 #else
755 	va_start(ap);
756 #endif
757 	(void)fprintf(stderr, "make: ");
758 	(void)vfprintf(stderr, fmt, ap);
759 	va_end(ap);
760 	(void)fprintf(stderr, "\n");
761 	(void)fflush(stderr);
762 
763 	DieHorribly();
764 }
765 
766 /*-
767  * DieHorribly --
768  *	Exit without giving a message.
769  *
770  * Results:
771  *	None
772  *
773  * Side Effects:
774  *	A big one...
775  */
776 void
777 DieHorribly()
778 {
779 	if (jobsRunning)
780 		Job_AbortAll();
781 	if (DEBUG(GRAPH2))
782 		Targ_PrintGraph(2);
783 	exit(2);		/* Not 1, so -q can distinguish error */
784 }
785 
786 /*
787  * Finish --
788  *	Called when aborting due to errors in child shell to signal
789  *	abnormal exit.
790  *
791  * Results:
792  *	None
793  *
794  * Side Effects:
795  *	The program exits
796  */
797 void
798 Finish(errors)
799 	int errors;	/* number of errors encountered in Make_Make */
800 {
801 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
802 }
803 
804 /*
805  * emalloc --
806  *	malloc, but die on error.
807  */
808 char *
809 emalloc(len)
810 	u_int len;
811 {
812 	char *p;
813 
814 	if (!(p = malloc(len)))
815 		enomem();
816 	return(p);
817 }
818 
819 /*
820  * enomem --
821  *	die when out of memory.
822  */
823 void
824 enomem()
825 {
826 	(void)fprintf(stderr, "make: %s.\n", strerror(errno));
827 	exit(2);
828 }
829 
830 /*
831  * usage --
832  *	exit with usage message
833  */
834 static void
835 usage()
836 {
837 	(void)fprintf(stderr,
838 "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\
839             [-I directory] [-j max_jobs] [variable=value]\n");
840 	exit(2);
841 }
842