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