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