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