Home | History | Annotate | Line # | Download | only in make
compat.c revision 1.210
      1 /*	$NetBSD: compat.c,v 1.210 2020/12/13 16:30:08 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  * compat.c --
     74  *	The routines in this file implement the full-compatibility
     75  *	mode of PMake. Most of the special functionality of PMake
     76  *	is available in this mode. Things not supported:
     77  *	    - different shells.
     78  *	    - friendly variable substitution.
     79  *
     80  * Interface:
     81  *	Compat_Run	Initialize things for this module and recreate
     82  *			thems as need creatin'
     83  */
     84 
     85 #include <sys/types.h>
     86 #include <sys/stat.h>
     87 #include <sys/wait.h>
     88 
     89 #include <errno.h>
     90 #include <signal.h>
     91 
     92 #include "make.h"
     93 #include "dir.h"
     94 #include "job.h"
     95 #include "metachar.h"
     96 #include "pathnames.h"
     97 
     98 /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
     99 MAKE_RCSID("$NetBSD: compat.c,v 1.210 2020/12/13 16:30:08 rillig Exp $");
    100 
    101 static GNode *curTarg = NULL;
    102 static pid_t compatChild;
    103 static int compatSigno;
    104 
    105 /*
    106  * CompatDeleteTarget -- delete the file of a failed, interrupted, or
    107  * otherwise duffed target if not inhibited by .PRECIOUS.
    108  */
    109 static void
    110 CompatDeleteTarget(GNode *gn)
    111 {
    112 	if (gn != NULL && !Targ_Precious(gn)) {
    113 		const char *file = GNode_VarTarget(gn);
    114 
    115 		if (!opts.noExecute && eunlink(file) != -1) {
    116 			Error("*** %s removed", file);
    117 		}
    118 	}
    119 }
    120 
    121 /* Interrupt the creation of the current target and remove it if it ain't
    122  * precious. Then exit.
    123  *
    124  * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
    125  *
    126  * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
    127  * left the logic alone for now. - dholland 20160826
    128  */
    129 static void
    130 CompatInterrupt(int signo)
    131 {
    132 	CompatDeleteTarget(curTarg);
    133 
    134 	if (curTarg != NULL && !Targ_Precious(curTarg)) {
    135 		/*
    136 		 * Run .INTERRUPT only if hit with interrupt signal
    137 		 */
    138 		if (signo == SIGINT) {
    139 			GNode *gn = Targ_FindNode(".INTERRUPT");
    140 			if (gn != NULL) {
    141 				Compat_Make(gn, gn);
    142 			}
    143 		}
    144 	}
    145 
    146 	if (signo == SIGQUIT)
    147 		_exit(signo);
    148 
    149 	/*
    150 	 * If there is a child running, pass the signal on.
    151 	 * We will exist after it has exited.
    152 	 */
    153 	compatSigno = signo;
    154 	if (compatChild > 0) {
    155 		KILLPG(compatChild, signo);
    156 	} else {
    157 		bmake_signal(signo, SIG_DFL);
    158 		kill(myPid, signo);
    159 	}
    160 }
    161 
    162 static void
    163 DebugFailedTarget(const char *cmd, GNode *gn)
    164 {
    165 	const char *p = cmd;
    166 	debug_printf("\n*** Failed target:  %s\n*** Failed command: ",
    167 		     gn->name);
    168 
    169 	/* Replace runs of whitespace with a single space, to reduce
    170 	 * the amount of whitespace for multi-line command lines. */
    171 	while (*p != '\0') {
    172 		if (ch_isspace(*p)) {
    173 			debug_printf(" ");
    174 			cpp_skip_whitespace(&p);
    175 		} else {
    176 			debug_printf("%c", *p);
    177 			p++;
    178 		}
    179 	}
    180 	debug_printf("\n");
    181 }
    182 
    183 static Boolean
    184 UseShell(const char *cmd MAKE_ATTR_UNUSED)
    185 {
    186 #if !defined(MAKE_NATIVE)
    187 	/*
    188 	 * In a non-native build, the host environment might be weird enough
    189 	 * that it's necessary to go through a shell to get the correct
    190 	 * behaviour.  Or perhaps the shell has been replaced with something
    191 	 * that does extra logging, and that should not be bypassed.
    192 	 */
    193 	return TRUE;
    194 #else
    195 	/*
    196 	 * Search for meta characters in the command. If there are no meta
    197 	 * characters, there's no need to execute a shell to execute the
    198 	 * command.
    199 	 *
    200 	 * Additionally variable assignments and empty commands
    201 	 * go to the shell. Therefore treat '=' and ':' like shell
    202 	 * meta characters as documented in make(1).
    203 	 */
    204 
    205 	return needshell(cmd);
    206 #endif
    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  *	gnp		Node from which the command came
    215  *
    216  * Results:
    217  *	0 if the command succeeded, 1 if an error occurred.
    218  */
    219 int
    220 Compat_RunCommand(const char *cmdp, GNode *gn)
    221 {
    222 	char *cmdStart;		/* Start of expanded command */
    223 	char *bp;
    224 	Boolean silent;		/* Don't print command */
    225 	Boolean doIt;		/* Execute even if -n */
    226 	volatile Boolean 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 	StringListNode *cmdNode; /* Node where current command is located */
    232 	const char **volatile av; /* Argument vector for thing to exec */
    233 	char **volatile mav;	/* Copy of the argument vector for freeing */
    234 	Boolean useShell;	/* TRUE if command should be executed
    235 				 * using a shell */
    236 	const char *volatile cmd = cmdp;
    237 
    238 	silent = (gn->type & OP_SILENT) != 0;
    239 	errCheck = !(gn->type & OP_IGNORE);
    240 	doIt = FALSE;
    241 
    242 	/* Luckily the commands don't end up in a string pool, otherwise
    243 	 * this comparison could match too early, in a dependency using "..."
    244 	 * for delayed commands, run in parallel mode, using the same shell
    245 	 * command line more than once; see JobPrintCommand.
    246 	 * TODO: write a unit-test to protect against this potential bug. */
    247 	cmdNode = Lst_FindDatum(&gn->commands, cmd);
    248 	(void)Var_Subst(cmd, gn, VARE_WANTRES, &cmdStart);
    249 	/* TODO: handle errors */
    250 
    251 	if (cmdStart[0] == '\0') {
    252 		free(cmdStart);
    253 		return 0;
    254 	}
    255 	cmd = cmdStart;
    256 	LstNode_Set(cmdNode, cmdStart);
    257 
    258 	if (gn->type & OP_SAVE_CMDS) {
    259 		GNode *endNode = Targ_GetEndNode();
    260 		if (gn != endNode) {
    261 			Lst_Append(&endNode->commands, cmdStart);
    262 			return 0;
    263 		}
    264 	}
    265 	if (strcmp(cmdStart, "...") == 0) {
    266 		gn->type |= OP_SAVE_CMDS;
    267 		return 0;
    268 	}
    269 
    270 	for (;;) {
    271 		if (*cmd == '@')
    272 			silent = !DEBUG(LOUD);
    273 		else if (*cmd == '-')
    274 			errCheck = FALSE;
    275 		else if (*cmd == '+') {
    276 			doIt = TRUE;
    277 			if (!shellName)	/* we came here from jobs */
    278 				Shell_Init();
    279 		} else
    280 			break;
    281 		cmd++;
    282 	}
    283 
    284 	while (ch_isspace(*cmd))
    285 		cmd++;
    286 
    287 	/*
    288 	 * If we did not end up with a command, just skip it.
    289 	 */
    290 	if (cmd[0] == '\0')
    291 		return 0;
    292 
    293 	useShell = UseShell(cmd);
    294 	/*
    295 	 * Print the command before echoing if we're not supposed to be quiet
    296 	 * for this one. We also print the command if -n given.
    297 	 */
    298 	if (!silent || !GNode_ShouldExecute(gn)) {
    299 		printf("%s\n", cmd);
    300 		fflush(stdout);
    301 	}
    302 
    303 	/*
    304 	 * If we're not supposed to execute any commands, this is as far as
    305 	 * we go...
    306 	 */
    307 	if (!doIt && !GNode_ShouldExecute(gn))
    308 		return 0;
    309 
    310 	DEBUG1(JOB, "Execute: '%s'\n", cmd);
    311 
    312 	if (useShell) {
    313 		/*
    314 		 * We need to pass the command off to the shell, typically
    315 		 * because the command contains a "meta" character.
    316 		 */
    317 		static const char *shargv[5];
    318 
    319 		/* The following work for any of the builtin shell specs. */
    320 		int shargc = 0;
    321 		shargv[shargc++] = shellPath;
    322 		if (errCheck && shellErrFlag)
    323 			shargv[shargc++] = shellErrFlag;
    324 		shargv[shargc++] = DEBUG(SHELL) ? "-xc" : "-c";
    325 		shargv[shargc++] = cmd;
    326 		shargv[shargc] = NULL;
    327 		av = shargv;
    328 		bp = NULL;
    329 		mav = NULL;
    330 	} else {
    331 		/*
    332 		 * No meta-characters, so no need to exec a shell. Break the
    333 		 * command into words to form an argument vector we can
    334 		 * execute.
    335 		 */
    336 		Words words = Str_Words(cmd, FALSE);
    337 		mav = words.words;
    338 		bp = words.freeIt;
    339 		av = (void *)mav;
    340 	}
    341 
    342 #ifdef USE_META
    343 	if (useMeta) {
    344 		meta_compat_start();
    345 	}
    346 #endif
    347 
    348 	/*
    349 	 * Fork and execute the single command. If the fork fails, we abort.
    350 	 */
    351 	compatChild = cpid = vFork();
    352 	if (cpid < 0) {
    353 		Fatal("Could not fork");
    354 	}
    355 	if (cpid == 0) {
    356 		Var_ReexportVars();
    357 #ifdef USE_META
    358 		if (useMeta) {
    359 			meta_compat_child();
    360 		}
    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(cmdNode);
    372 
    373 #ifdef USE_META
    374 	if (useMeta) {
    375 		meta_compat_parent(cpid);
    376 	}
    377 #endif
    378 
    379 	/*
    380 	 * The child is off and running. Now all we can do is wait...
    381 	 */
    382 	while ((retstat = wait(&reason)) != cpid) {
    383 		if (retstat > 0)
    384 			JobReapChild(retstat, reason, FALSE); /* not ours? */
    385 		if (retstat == -1 && errno != EINTR) {
    386 			break;
    387 		}
    388 	}
    389 
    390 	if (retstat < 0)
    391 		Fatal("error in wait: %d: %s", retstat, strerror(errno));
    392 
    393 	if (WIFSTOPPED(reason)) {
    394 		status = WSTOPSIG(reason);	/* stopped */
    395 	} else if (WIFEXITED(reason)) {
    396 		status = WEXITSTATUS(reason);	/* exited */
    397 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
    398 		if (useMeta) {
    399 		    meta_cmd_finish(NULL);
    400 		}
    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 			}
    419 #endif
    420 			gn->made = ERROR;
    421 			if (opts.keepgoing) {
    422 				/*
    423 				 * Abort the current target,
    424 				 * but let others continue.
    425 				 */
    426 				printf(" (continuing)\n");
    427 			} else {
    428 				printf("\n");
    429 			}
    430 			if (deleteOnError)
    431 				CompatDeleteTarget(gn);
    432 		} else {
    433 			/*
    434 			 * Continue executing commands for this target.
    435 			 * If we return 0, this will happen...
    436 			 */
    437 			printf(" (ignored)\n");
    438 			status = 0;
    439 		}
    440 	}
    441 
    442 	free(cmdStart);
    443 	compatChild = 0;
    444 	if (compatSigno != 0) {
    445 		bmake_signal(compatSigno, SIG_DFL);
    446 		kill(myPid, compatSigno);
    447 	}
    448 
    449 	return status;
    450 }
    451 
    452 static void
    453 RunCommands(GNode *gn)
    454 {
    455 	StringListNode *ln;
    456 
    457 	for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
    458 		const char *cmd = ln->datum;
    459 		if (Compat_RunCommand(cmd, gn) != 0)
    460 			break;
    461 	}
    462 }
    463 
    464 static void
    465 MakeNodes(GNodeList *gnodes, GNode *pgn)
    466 {
    467 	GNodeListNode *ln;
    468 
    469 	for (ln = gnodes->first; ln != NULL; ln = ln->next) {
    470 		GNode *cohort = ln->datum;
    471 		Compat_Make(cohort, pgn);
    472 	}
    473 }
    474 
    475 static Boolean
    476 MakeUnmade(GNode *gn, GNode *pgn)
    477 {
    478 
    479 	assert(gn->made == UNMADE);
    480 
    481 	/*
    482 	 * First mark ourselves to be made, then apply whatever transformations
    483 	 * the suffix module thinks are necessary. Once that's done, we can
    484 	 * descend and make all our children. If any of them has an error
    485 	 * but the -k flag was given, our 'make' field will be set to FALSE
    486 	 * again. This is our signal to not attempt to do anything but abort
    487 	 * our parent as well.
    488 	 */
    489 	gn->flags |= REMAKE;
    490 	gn->made = BEINGMADE;
    491 
    492 	if (!(gn->type & OP_MADE))
    493 		Suff_FindDeps(gn);
    494 
    495 	MakeNodes(&gn->children, gn);
    496 
    497 	if (!(gn->flags & REMAKE)) {
    498 		gn->made = ABORTED;
    499 		pgn->flags &= ~(unsigned)REMAKE;
    500 		return FALSE;
    501 	}
    502 
    503 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
    504 		Var_Set(IMPSRC, GNode_VarTarget(gn), pgn);
    505 
    506 	/*
    507 	 * All the children were made ok. Now youngestChild->mtime contains the
    508 	 * modification time of the newest child, we need to find out if we
    509 	 * exist and when we were modified last. The criteria for datedness
    510 	 * are defined by GNode_IsOODate.
    511 	 */
    512 	DEBUG1(MAKE, "Examining %s...", gn->name);
    513 	if (!GNode_IsOODate(gn)) {
    514 		gn->made = UPTODATE;
    515 		DEBUG0(MAKE, "up-to-date.\n");
    516 		return FALSE;
    517 	}
    518 
    519 	/*
    520 	 * If the user is just seeing if something is out-of-date, exit now
    521 	 * to tell him/her "yes".
    522 	 */
    523 	DEBUG0(MAKE, "out-of-date.\n");
    524 	if (opts.queryFlag)
    525 		exit(1);
    526 
    527 	/*
    528 	 * We need to be re-made.
    529 	 * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
    530 	 */
    531 	Make_DoAllVar(gn);
    532 
    533 	/*
    534 	 * Alter our type to tell if errors should be ignored or things
    535 	 * should not be printed so CompatRunCommand knows what to do.
    536 	 */
    537 	if (opts.ignoreErrors)
    538 		gn->type |= OP_IGNORE;
    539 	if (opts.beSilent)
    540 		gn->type |= OP_SILENT;
    541 
    542 	if (Job_CheckCommands(gn, Fatal)) {
    543 		/*
    544 		 * Our commands are ok, but we still have to worry about
    545 		 * the -t flag.
    546 		 */
    547 		if (!opts.touchFlag || (gn->type & OP_MAKE)) {
    548 			curTarg = gn;
    549 #ifdef USE_META
    550 			if (useMeta && GNode_ShouldExecute(gn)) {
    551 				meta_job_start(NULL, gn);
    552 			}
    553 #endif
    554 			RunCommands(gn);
    555 			curTarg = NULL;
    556 		} else {
    557 			Job_Touch(gn, (gn->type & OP_SILENT) != 0);
    558 		}
    559 	} else {
    560 		gn->made = ERROR;
    561 	}
    562 #ifdef USE_META
    563 	if (useMeta && GNode_ShouldExecute(gn)) {
    564 		if (meta_job_finish(NULL) != 0)
    565 			gn->made = ERROR;
    566 	}
    567 #endif
    568 
    569 	if (gn->made != ERROR) {
    570 		/*
    571 		 * If the node was made successfully, mark it so, update
    572 		 * its modification time and timestamp all its parents.
    573 		 * This is to keep its state from affecting that of its parent.
    574 		 */
    575 		gn->made = MADE;
    576 		if (Make_Recheck(gn) == 0)
    577 			pgn->flags |= FORCE;
    578 		if (!(gn->type & OP_EXEC)) {
    579 			pgn->flags |= CHILDMADE;
    580 			GNode_UpdateYoungestChild(pgn, gn);
    581 		}
    582 	} else if (opts.keepgoing) {
    583 		pgn->flags &= ~(unsigned)REMAKE;
    584 	} else {
    585 		PrintOnError(gn, "\nStop.");
    586 		exit(1);
    587 	}
    588 	return TRUE;
    589 }
    590 
    591 static void
    592 MakeOther(GNode *gn, GNode *pgn)
    593 {
    594 
    595 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
    596 		const char *target = GNode_VarTarget(gn);
    597 		Var_Set(IMPSRC, target != NULL ? target : "", pgn);
    598 	}
    599 
    600 	switch (gn->made) {
    601 	case BEINGMADE:
    602 		Error("Graph cycles through %s", gn->name);
    603 		gn->made = ERROR;
    604 		pgn->flags &= ~(unsigned)REMAKE;
    605 		break;
    606 	case MADE:
    607 		if (!(gn->type & OP_EXEC)) {
    608 			pgn->flags |= CHILDMADE;
    609 			GNode_UpdateYoungestChild(pgn, gn);
    610 		}
    611 		break;
    612 	case UPTODATE:
    613 		if (!(gn->type & OP_EXEC))
    614 			GNode_UpdateYoungestChild(pgn, gn);
    615 		break;
    616 	default:
    617 		break;
    618 	}
    619 }
    620 
    621 /* Make a target.
    622  *
    623  * If an error is detected and not being ignored, the process exits.
    624  *
    625  * Input:
    626  *	gn		The node to make
    627  *	pgn		Parent to abort if necessary
    628  *
    629  * Output:
    630  *	gn->made
    631  *		UPTODATE	gn was already up-to-date.
    632  *		MADE		gn was recreated successfully.
    633  *		ERROR		An error occurred while gn was being created,
    634  *				either due to missing commands or in -k mode.
    635  *		ABORTED		gn was not remade because one of its
    636  *				dependencies could not be made due to errors.
    637  */
    638 void
    639 Compat_Make(GNode *gn, GNode *pgn)
    640 {
    641 	if (shellName == NULL)	/* we came here from jobs */
    642 		Shell_Init();
    643 
    644 	if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
    645 		if (!MakeUnmade(gn, pgn))
    646 			goto cohorts;
    647 
    648 		/* XXX: Replace with GNode_IsError(gn) */
    649 	} else if (gn->made == ERROR) {
    650 		/*
    651 		 * Already had an error when making this.
    652 		 * Tell the parent to abort.
    653 		 */
    654 		pgn->flags &= ~(unsigned)REMAKE;
    655 	} else {
    656 		MakeOther(gn, pgn);
    657 	}
    658 
    659 cohorts:
    660 	MakeNodes(&gn->cohorts, pgn);
    661 }
    662 
    663 static void
    664 MakeBeginNode(void)
    665 {
    666 	GNode *gn = Targ_FindNode(".BEGIN");
    667 	if (gn == NULL)
    668 		return;
    669 
    670 	Compat_Make(gn, gn);
    671 	if (GNode_IsError(gn)) {
    672 		PrintOnError(gn, "\nStop.");
    673 		exit(1);
    674 	}
    675 }
    676 
    677 /* Initialize this module and start making.
    678  *
    679  * Input:
    680  *	targs		The target nodes to re-create
    681  */
    682 void
    683 Compat_Run(GNodeList *targs)
    684 {
    685 	GNode *gn = NULL;	/* Current root target */
    686 	Boolean seenError;
    687 
    688 	if (!shellName)
    689 		Shell_Init();
    690 
    691 	if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
    692 		bmake_signal(SIGINT, CompatInterrupt);
    693 	if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
    694 		bmake_signal(SIGTERM, CompatInterrupt);
    695 	if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
    696 		bmake_signal(SIGHUP, CompatInterrupt);
    697 	if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
    698 		bmake_signal(SIGQUIT, CompatInterrupt);
    699 
    700 	/* Create the .END node now, to keep the (debug) output of the
    701 	 * counter.mk test the same as before 2020-09-23.  This implementation
    702 	 * detail probably doesn't matter though. */
    703 	(void)Targ_GetEndNode();
    704 
    705 	if (!opts.queryFlag)
    706 		MakeBeginNode();
    707 
    708 	/*
    709 	 * Expand .USE nodes right now, because they can modify the structure
    710 	 * of the tree.
    711 	 */
    712 	Make_ExpandUse(targs);
    713 
    714 	seenError = FALSE;
    715 	while (!Lst_IsEmpty(targs)) {
    716 		gn = Lst_Dequeue(targs);
    717 		Compat_Make(gn, gn);
    718 
    719 		if (gn->made == UPTODATE) {
    720 			printf("`%s' is up to date.\n", gn->name);
    721 		} else if (gn->made == ABORTED) {
    722 			printf("`%s' not remade because of errors.\n",
    723 			       gn->name);
    724 		}
    725 		if (GNode_IsError(gn))
    726 			seenError = TRUE;
    727 	}
    728 
    729 	/* If the user has defined a .END target, run its commands. */
    730 	if (!seenError) {
    731 		GNode *endNode = Targ_GetEndNode();
    732 		Compat_Make(endNode, endNode);
    733 		seenError = GNode_IsError(endNode);
    734 	}
    735 
    736 	if (seenError) {
    737 		PrintOnError(gn, "\nStop.");
    738 		exit(1);
    739 	}
    740 }
    741