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