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