Home | History | Annotate | Line # | Download | only in make
compat.c revision 1.267
      1 /*	$NetBSD: compat.c,v 1.267 2025/06/13 03:51:18 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1988, 1989 by Adam de Boor
     37  * Copyright (c) 1989 by Berkeley Softworks
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Adam de Boor.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 /*
     73  * This file implements the full-compatibility mode of make, which makes the
     74  * targets without parallelism and without a custom shell.
     75  *
     76  * Interface:
     77  *	Compat_MakeAll	Initialize this module and make the given targets.
     78  */
     79 
     80 #include <sys/types.h>
     81 #include <sys/stat.h>
     82 #include <sys/wait.h>
     83 
     84 #include <errno.h>
     85 #include <signal.h>
     86 
     87 #include "make.h"
     88 #include "dir.h"
     89 #include "job.h"
     90 #ifdef USE_META
     91 # include "meta.h"
     92 #endif
     93 #include "metachar.h"
     94 #include "pathnames.h"
     95 
     96 /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
     97 MAKE_RCSID("$NetBSD: compat.c,v 1.267 2025/06/13 03:51:18 rillig Exp $");
     98 
     99 static GNode *curTarg;
    100 static pid_t compatChild;
    101 static int compatSigno;
    102 
    103 /*
    104  * Delete the file of a failed, interrupted, or otherwise duffed target,
    105  * unless inhibited by .PRECIOUS.
    106  */
    107 static void
    108 CompatDeleteTarget(GNode *gn)
    109 {
    110 	if (!GNode_IsPrecious(gn) &&
    111 	    (gn->type & OP_PHONY) == 0) {
    112 		const char *file = GNode_VarTarget(gn);
    113 		if (!opts.noExecute && unlink_file(file) == 0)
    114 			Error("*** %s removed", file);
    115 	}
    116 }
    117 
    118 /*
    119  * Interrupt the creation of the current target and remove it if it ain't
    120  * precious. Then exit.
    121  *
    122  * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
    123  *
    124  * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
    125  * left the logic alone for now. - dholland 20160826
    126  */
    127 static void
    128 CompatInterrupt(int signo)
    129 {
    130 	if (curTarg != NULL) {
    131 		CompatDeleteTarget(curTarg);
    132 		if (signo == SIGINT && !GNode_IsPrecious(curTarg)) {
    133 			GNode *gn = Targ_FindNode(".INTERRUPT");
    134 			if (gn != NULL)
    135 				Compat_Make(gn, gn);
    136 		}
    137 	}
    138 
    139 	if (signo == SIGQUIT)
    140 		_exit(signo);
    141 
    142 	/*
    143 	 * If there is a child running, pass the signal on.
    144 	 * We will exist after it has exited.
    145 	 */
    146 	compatSigno = signo;
    147 	if (compatChild > 0) {
    148 		KILLPG(compatChild, signo);
    149 	} else {
    150 		bmake_signal(signo, SIG_DFL);
    151 		kill(myPid, signo);
    152 	}
    153 }
    154 
    155 static void
    156 DebugFailedTarget(const char *cmd, const GNode *gn)
    157 {
    158 	const char *p = cmd;
    159 	debug_printf("\n*** Failed target:  %s\n*** Failed command: ",
    160 	    gn->name);
    161 
    162 	/*
    163 	 * Replace runs of whitespace with a single space, to reduce the
    164 	 * amount of whitespace for multi-line command lines.
    165 	 */
    166 	while (*p != '\0') {
    167 		if (ch_isspace(*p)) {
    168 			debug_printf(" ");
    169 			cpp_skip_whitespace(&p);
    170 		} else {
    171 			debug_printf("%c", *p);
    172 			p++;
    173 		}
    174 	}
    175 	debug_printf("\n");
    176 }
    177 
    178 static bool
    179 UseShell(const char *cmd MAKE_ATTR_UNUSED)
    180 {
    181 #if !defined(MAKE_NATIVE)
    182 	/*
    183 	 * In a non-native build, the host environment might be weird enough
    184 	 * that it's necessary to go through a shell to get the correct
    185 	 * behaviour.  Or perhaps the shell has been replaced with something
    186 	 * that does extra logging, and that should not be bypassed.
    187 	 */
    188 	return true;
    189 #else
    190 	/*
    191 	 * Search for meta characters in the command. If there are no meta
    192 	 * characters, there's no need to execute a shell to execute the
    193 	 * command.
    194 	 *
    195 	 * Additionally variable assignments and empty commands
    196 	 * go to the shell. Therefore treat '=' and ':' like shell
    197 	 * meta characters as documented in make(1).
    198 	 */
    199 
    200 	return needshell(cmd);
    201 #endif
    202 }
    203 
    204 static int
    205 Compat_Spawn(const char **av)
    206 {
    207 	int pid = FORK_FUNCTION();
    208 	if (pid < 0)
    209 		Fatal("Could not fork");
    210 
    211 	if (pid == 0) {
    212 #ifdef USE_META
    213 		if (useMeta)
    214 			meta_compat_child();
    215 #endif
    216 		(void)execvp(av[0], (char *const *)UNCONST(av));
    217 		execDie("exec", av[0]);
    218 	}
    219 	return pid;
    220 }
    221 
    222 /*
    223  * Execute the next command for a target. If the command returns an error,
    224  * the node's made field is set to ERROR and creation stops.
    225  *
    226  * Input:
    227  *	cmdp		Command to execute
    228  *	gn		Node from which the command came
    229  *	ln		List node that contains the command
    230  *
    231  * Results:
    232  *	true if the command succeeded.
    233  */
    234 bool
    235 Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
    236 {
    237 	char *cmdStart;		/* Start of expanded command */
    238 	char *volatile bp;
    239 	bool silent;		/* Don't print command */
    240 	bool doIt;		/* Execute even if -n */
    241 	volatile bool errCheck;	/* Check errors */
    242 	int reason;		/* Reason for child's death */
    243 	int status;		/* Description of child's death */
    244 	pid_t retstat;		/* Result of wait */
    245 	const char **av;	/* Arguments for the child process */
    246 	char **volatile mav;	/* Copy of the argument vector for freeing */
    247 	bool useShell;		/* True if command should be executed using a
    248 				 * shell */
    249 	const char *cmd = cmdp;
    250 	char cmd_file[MAXPATHLEN];
    251 	size_t cmd_len;
    252 	int parseErrorsBefore;
    253 
    254 	silent = (gn->type & OP_SILENT) != OP_NONE;
    255 	errCheck = !(gn->type & OP_IGNORE);
    256 	doIt = false;
    257 
    258 	parseErrorsBefore = parseErrors;
    259 	cmdStart = Var_SubstInTarget(cmd, gn);
    260 	if (parseErrors != parseErrorsBefore) {
    261 		free(cmdStart);
    262 		return false;
    263 	}
    264 
    265 	if (cmdStart[0] == '\0') {
    266 		free(cmdStart);
    267 		return true;
    268 	}
    269 	cmd = cmdStart;
    270 	LstNode_Set(ln, cmdStart);
    271 
    272 	if (gn->type & OP_SAVE_CMDS) {
    273 		GNode *endNode = Targ_GetEndNode();
    274 		if (gn != endNode) {
    275 			/*
    276 			 * Append the expanded command, to prevent the
    277 			 * local variables from being interpreted in the
    278 			 * scope of the .END node.
    279 			 *
    280 			 * A probably unintended side effect of this is that
    281 			 * the expanded command will be expanded again in the
    282 			 * .END node.  Therefore, a literal '$' in these
    283 			 * commands must be written as '$$$$' instead of the
    284 			 * usual '$$'.
    285 			 */
    286 			Lst_Append(&endNode->commands, cmdStart);
    287 			goto register_command;
    288 		}
    289 	}
    290 	if (strcmp(cmdStart, "...") == 0) {
    291 		gn->type |= OP_SAVE_CMDS;
    292 	register_command:
    293 		Parse_RegisterCommand(cmdStart);
    294 		return true;
    295 	}
    296 
    297 	for (;;) {
    298 		if (*cmd == '@')
    299 			silent = !DEBUG(LOUD);
    300 		else if (*cmd == '-')
    301 			errCheck = false;
    302 		else if (*cmd == '+')
    303 			doIt = true;
    304 		else if (!ch_isspace(*cmd))
    305 			/* Ignore whitespace for compatibility with gnu make */
    306 			break;
    307 		cmd++;
    308 	}
    309 
    310 	if (cmd[0] == '\0')
    311 		goto register_command;
    312 
    313 	useShell = UseShell(cmd);
    314 
    315 	if (!silent || !GNode_ShouldExecute(gn)) {
    316 		printf("%s\n", cmd);
    317 		fflush(stdout);
    318 	}
    319 
    320 	if (!doIt && !GNode_ShouldExecute(gn))
    321 		goto register_command;
    322 
    323 	DEBUG1(JOB, "Execute: '%s'\n", cmd);
    324 
    325 	cmd_len = strlen(cmd);
    326 	if (cmd_len > MAKE_CMDLEN_LIMIT)
    327 		useShell = true;
    328 	else
    329 		cmd_file[0] = '\0';
    330 
    331 	if (useShell) {
    332 		static const char *shargv[5];
    333 
    334 		if (Cmd_Argv(cmd, cmd_len, shargv, 5,
    335 			cmd_file, sizeof(cmd_file),
    336 			errCheck && shellErrFlag != NULL,
    337 			DEBUG(SHELL)) < 0)
    338 			Fatal("cannot run \"%s\"", cmd);
    339 		av = shargv;
    340 		bp = NULL;
    341 		mav = NULL;
    342 	} else {
    343 		Words words = Str_Words(cmd, false);
    344 		mav = words.words;
    345 		bp = words.freeIt;
    346 		av = (void *)mav;
    347 	}
    348 
    349 #ifdef USE_META
    350 	if (useMeta)
    351 		meta_compat_start();
    352 #endif
    353 
    354 	Var_ReexportVars(gn);
    355 	Var_ExportStackTrace(gn->name, cmd);
    356 
    357 	compatChild = Compat_Spawn(av);
    358 	free(mav);
    359 	free(bp);
    360 
    361 	/* XXX: Memory management looks suspicious here. */
    362 	/* XXX: Setting a list item to NULL is unexpected. */
    363 	LstNode_SetNull(ln);
    364 
    365 #ifdef USE_META
    366 	if (useMeta)
    367 		meta_compat_parent(compatChild);
    368 #endif
    369 
    370 	/* The child is off and running. Now all we can do is wait... */
    371 	while ((retstat = wait(&reason)) != compatChild) {
    372 		if (retstat > 0)
    373 			JobReapChild(retstat, reason, false); /* not ours? */
    374 		if (retstat == -1 && errno != EINTR)
    375 			break;
    376 	}
    377 
    378 	if (retstat < 0)
    379 		Fatal("error in wait: %d: %s", retstat, strerror(errno));
    380 
    381 	if (WIFSTOPPED(reason)) {
    382 		status = WSTOPSIG(reason);
    383 	} else if (WIFEXITED(reason)) {
    384 		status = WEXITSTATUS(reason);
    385 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
    386 		if (useMeta)
    387 			meta_cmd_finish(NULL);
    388 #endif
    389 		if (status != 0) {
    390 			if (DEBUG(ERROR))
    391 				DebugFailedTarget(cmd, gn);
    392 			printf("*** Error code %d", status);
    393 		}
    394 	} else {
    395 		status = WTERMSIG(reason);
    396 		printf("*** Signal %d", status);
    397 	}
    398 
    399 
    400 	if (!WIFEXITED(reason) || status != 0) {
    401 		if (errCheck) {
    402 #ifdef USE_META
    403 			if (useMeta)
    404 				meta_job_error(NULL, gn, false, status);
    405 #endif
    406 			gn->made = ERROR;
    407 			if (WIFEXITED(reason))
    408 				gn->exit_status = status;
    409 			if (opts.keepgoing) {
    410 				/*
    411 				 * Abort the current target,
    412 				 * but let others continue.
    413 				 */
    414 				printf(" (continuing)\n");
    415 			} else {
    416 				printf("\n");
    417 			}
    418 			if (deleteOnError)
    419 				CompatDeleteTarget(gn);
    420 		} else {
    421 			/*
    422 			 * Continue executing commands for this target.
    423 			 * If we return 0, this will happen...
    424 			 */
    425 			printf(" (ignored)\n");
    426 			status = 0;
    427 		}
    428 		fflush(stdout);
    429 	}
    430 
    431 	free(cmdStart);
    432 	if (cmd_file[0] != '\0')
    433 		unlink(cmd_file);
    434 	compatChild = 0;
    435 	if (compatSigno != 0) {
    436 		bmake_signal(compatSigno, SIG_DFL);
    437 		kill(myPid, compatSigno);
    438 	}
    439 
    440 	return status == 0;
    441 }
    442 
    443 static void
    444 RunCommands(GNode *gn)
    445 {
    446 	StringListNode *ln;
    447 
    448 	for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
    449 		const char *cmd = ln->datum;
    450 		if (!Compat_RunCommand(cmd, gn, ln))
    451 			break;
    452 	}
    453 }
    454 
    455 static void
    456 MakeInRandomOrder(GNode **gnodes, GNode **end, GNode *pgn)
    457 {
    458 	GNode **it;
    459 	size_t r;
    460 
    461 	for (r = (size_t)(end - gnodes); r >= 2; r--) {
    462 		/* Biased, but irrelevant in practice. */
    463 		size_t i = (size_t)random() % r;
    464 		GNode *t = gnodes[r - 1];
    465 		gnodes[r - 1] = gnodes[i];
    466 		gnodes[i] = t;
    467 	}
    468 
    469 	for (it = gnodes; it != end; it++)
    470 		Compat_Make(*it, pgn);
    471 }
    472 
    473 static void
    474 MakeWaitGroupsInRandomOrder(GNodeList *gnodes, GNode *pgn)
    475 {
    476 	Vector vec;
    477 	GNodeListNode *ln;
    478 	GNode **nodes;
    479 	size_t i, n, start;
    480 
    481 	Vector_Init(&vec, sizeof(GNode *));
    482 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
    483 		*(GNode **)Vector_Push(&vec) = ln->datum;
    484 	nodes = vec.items;
    485 	n = vec.len;
    486 
    487 	start = 0;
    488 	for (i = 0; i < n; i++) {
    489 		if (nodes[i]->type & OP_WAIT) {
    490 			MakeInRandomOrder(nodes + start, nodes + i, pgn);
    491 			Compat_Make(nodes[i], pgn);
    492 			start = i + 1;
    493 		}
    494 	}
    495 	MakeInRandomOrder(nodes + start, nodes + i, pgn);
    496 
    497 	Vector_Done(&vec);
    498 }
    499 
    500 static void
    501 MakeNodes(GNodeList *gnodes, GNode *pgn)
    502 {
    503 	GNodeListNode *ln;
    504 
    505 	if (Lst_IsEmpty(gnodes))
    506 		return;
    507 	if (opts.randomizeTargets) {
    508 		MakeWaitGroupsInRandomOrder(gnodes, pgn);
    509 		return;
    510 	}
    511 
    512 	for (ln = gnodes->first; ln != NULL; ln = ln->next) {
    513 		GNode *cgn = ln->datum;
    514 		Compat_Make(cgn, pgn);
    515 	}
    516 }
    517 
    518 static bool
    519 MakeUnmade(GNode *gn, GNode *pgn)
    520 {
    521 
    522 	assert(gn->made == UNMADE);
    523 
    524 	/*
    525 	 * First mark ourselves to be made, then apply whatever transformations
    526 	 * the suffix module thinks are necessary. Once that's done, we can
    527 	 * descend and make all our children. If any of them has an error
    528 	 * but the -k flag was given, our 'make' field will be set to false
    529 	 * again. This is our signal to not attempt to do anything but abort
    530 	 * our parent as well.
    531 	 */
    532 	gn->flags.remake = true;
    533 	gn->made = BEINGMADE;
    534 
    535 	if (!(gn->type & OP_MADE))
    536 		Suff_FindDeps(gn);
    537 
    538 	MakeNodes(&gn->children, gn);
    539 
    540 	if (!gn->flags.remake) {
    541 		gn->made = ABORTED;
    542 		pgn->flags.remake = false;
    543 		return false;
    544 	}
    545 
    546 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
    547 		Var_Set(pgn, IMPSRC, GNode_VarTarget(gn));
    548 
    549 	/*
    550 	 * All the children were made ok. Now youngestChild->mtime contains the
    551 	 * modification time of the newest child, we need to find out if we
    552 	 * exist and when we were modified last. The criteria for datedness
    553 	 * are defined by GNode_IsOODate.
    554 	 */
    555 	DEBUG1(MAKE, "Examining %s...", gn->name);
    556 	if (!GNode_IsOODate(gn)) {
    557 		gn->made = UPTODATE;
    558 		DEBUG0(MAKE, "up-to-date.\n");
    559 		return false;
    560 	}
    561 
    562 	/*
    563 	 * If the user is just seeing if something is out-of-date, exit now
    564 	 * to tell him/her "yes".
    565 	 */
    566 	DEBUG0(MAKE, "out-of-date.\n");
    567 	if (opts.query && gn != Targ_GetEndNode())
    568 		exit(1);
    569 
    570 	/*
    571 	 * We need to be re-made.
    572 	 * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
    573 	 */
    574 	GNode_SetLocalVars(gn);
    575 
    576 	/*
    577 	 * Alter our type to tell if errors should be ignored or things
    578 	 * should not be printed so Compat_RunCommand knows what to do.
    579 	 */
    580 	if (opts.ignoreErrors)
    581 		gn->type |= OP_IGNORE;
    582 	if (opts.silent)
    583 		gn->type |= OP_SILENT;
    584 
    585 	if (Job_CheckCommands(gn, Fatal)) {
    586 		if (!opts.touch || (gn->type & OP_MAKE)) {
    587 			curTarg = gn;
    588 #ifdef USE_META
    589 			if (useMeta && GNode_ShouldExecute(gn))
    590 				meta_job_start(NULL, gn);
    591 #endif
    592 			RunCommands(gn);
    593 			curTarg = NULL;
    594 		} else {
    595 			Job_Touch(gn, (gn->type & OP_SILENT) != OP_NONE);
    596 		}
    597 	} else {
    598 		gn->made = ERROR;
    599 	}
    600 #ifdef USE_META
    601 	if (useMeta && GNode_ShouldExecute(gn)) {
    602 		if (meta_job_finish(NULL) != 0)
    603 			gn->made = ERROR;
    604 	}
    605 #endif
    606 
    607 	if (gn->made != ERROR) {
    608 		/*
    609 		 * If the node was made successfully, mark it so, update
    610 		 * its modification time and timestamp all its parents.
    611 		 * This is to keep its state from affecting that of its parent.
    612 		 */
    613 		gn->made = MADE;
    614 		if (Make_Recheck(gn) == 0)
    615 			pgn->flags.force = true;
    616 		if (!(gn->type & OP_EXEC)) {
    617 			pgn->flags.childMade = true;
    618 			GNode_UpdateYoungestChild(pgn, gn);
    619 		}
    620 	} else if (opts.keepgoing) {
    621 		pgn->flags.remake = false;
    622 	} else {
    623 		PrintOnError(gn, "\nStop.\n");
    624 		exit(1);
    625 	}
    626 	return true;
    627 }
    628 
    629 static void
    630 MakeOther(GNode *gn, GNode *pgn)
    631 {
    632 
    633 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
    634 		const char *target = GNode_VarTarget(gn);
    635 		Var_Set(pgn, IMPSRC, target != NULL ? target : "");
    636 	}
    637 
    638 	switch (gn->made) {
    639 	case BEINGMADE:
    640 		Error("Graph cycles through %s", gn->name);
    641 		gn->made = ERROR;
    642 		pgn->flags.remake = false;
    643 		break;
    644 	case MADE:
    645 		if (!(gn->type & OP_EXEC)) {
    646 			pgn->flags.childMade = true;
    647 			GNode_UpdateYoungestChild(pgn, gn);
    648 		}
    649 		break;
    650 	case UPTODATE:
    651 		if (!(gn->type & OP_EXEC))
    652 			GNode_UpdateYoungestChild(pgn, gn);
    653 		break;
    654 	default:
    655 		break;
    656 	}
    657 }
    658 
    659 /*
    660  * Make a target.
    661  *
    662  * If an error is detected and not being ignored, the process exits.
    663  *
    664  * Input:
    665  *	gn		The node to make
    666  *	pgn		Parent to abort if necessary
    667  *
    668  * Output:
    669  *	gn->made
    670  *		UPTODATE	gn was already up-to-date.
    671  *		MADE		gn was recreated successfully.
    672  *		ERROR		An error occurred while gn was being created,
    673  *				either due to missing commands or in -k mode.
    674  *		ABORTED		gn was not remade because one of its
    675  *				dependencies could not be made due to errors.
    676  */
    677 void
    678 Compat_Make(GNode *gn, GNode *pgn)
    679 {
    680 	if (shellName == NULL)	/* we came here from jobs */
    681 		Shell_Init();
    682 
    683 	if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
    684 		if (!MakeUnmade(gn, pgn))
    685 			goto cohorts;
    686 
    687 		/* XXX: Replace with GNode_IsError(gn) */
    688 	} else if (gn->made == ERROR) {
    689 		/*
    690 		 * Already had an error when making this.
    691 		 * Tell the parent to abort.
    692 		 */
    693 		pgn->flags.remake = false;
    694 	} else {
    695 		MakeOther(gn, pgn);
    696 	}
    697 
    698 cohorts:
    699 	MakeNodes(&gn->cohorts, pgn);
    700 }
    701 
    702 static void
    703 MakeBeginNode(void)
    704 {
    705 	GNode *gn = Targ_FindNode(".BEGIN");
    706 	if (gn == NULL)
    707 		return;
    708 
    709 	Compat_Make(gn, gn);
    710 	if (GNode_IsError(gn)) {
    711 		PrintOnError(gn, "\nStop.\n");
    712 		exit(1);
    713 	}
    714 }
    715 
    716 static void
    717 InitSignals(void)
    718 {
    719 	if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
    720 		bmake_signal(SIGINT, CompatInterrupt);
    721 	if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
    722 		bmake_signal(SIGTERM, CompatInterrupt);
    723 	if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
    724 		bmake_signal(SIGHUP, CompatInterrupt);
    725 	if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
    726 		bmake_signal(SIGQUIT, CompatInterrupt);
    727 }
    728 
    729 void
    730 Compat_MakeAll(GNodeList *targets)
    731 {
    732 	GNode *errorNode = NULL;
    733 
    734 	if (shellName == NULL)
    735 		Shell_Init();
    736 
    737 	InitSignals();
    738 
    739 	/*
    740 	 * Create the .END node now, to keep the (debug) output of the
    741 	 * counter.mk test the same as before 2020-09-23.  This
    742 	 * implementation detail probably doesn't matter though.
    743 	 */
    744 	(void)Targ_GetEndNode();
    745 
    746 	if (!opts.query)
    747 		MakeBeginNode();
    748 
    749 	/*
    750 	 * Expand .USE nodes right now, because they can modify the structure
    751 	 * of the tree.
    752 	 */
    753 	Make_ExpandUse(targets);
    754 
    755 	while (!Lst_IsEmpty(targets)) {
    756 		GNode *gn = Lst_Dequeue(targets);
    757 		Compat_Make(gn, gn);
    758 
    759 		if (gn->made == UPTODATE) {
    760 			printf("`%s' is up to date.\n", gn->name);
    761 		} else if (gn->made == ABORTED) {
    762 			printf("`%s' not remade because of errors.\n",
    763 			    gn->name);
    764 		}
    765 		if (GNode_IsError(gn) && errorNode == NULL)
    766 			errorNode = gn;
    767 	}
    768 
    769 	if (errorNode == NULL) {
    770 		GNode *endNode = Targ_GetEndNode();
    771 		Compat_Make(endNode, endNode);
    772 		if (GNode_IsError(endNode))
    773 			errorNode = endNode;
    774 	}
    775 
    776 	if (errorNode != NULL) {
    777 		if (DEBUG(GRAPH2))
    778 			Targ_PrintGraph(2);
    779 		else if (DEBUG(GRAPH3))
    780 			Targ_PrintGraph(3);
    781 		PrintOnError(errorNode, "\nStop.\n");
    782 		exit(1);
    783 	}
    784 }
    785