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