1 /* $NetBSD: compat.c,v 1.227 2021/04/27 15:19:25 christos Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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
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:
78 * - friendly variable substitution.
81 * Compat_Run Initialize things for this module and recreate
82 * thems as need creatin'
88 #include <sys/types.h>
99 #include "pathnames.h"
101 /* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */
102 MAKE_RCSID("$NetBSD: compat.c,v 1.227 2021/04/27 15:19:25 christos Exp $");
104 static GNode *curTarg = NULL;
105 static pid_t compatChild;
106 static int compatSigno;
109 * CompatDeleteTarget -- delete the file of a failed, interrupted, or
110 * otherwise duffed target if not inhibited by .PRECIOUS.
113 CompatDeleteTarget(GNode *gn)
115 if (gn != NULL && !Targ_Precious(gn)) {
116 const char *file = GNode_VarTarget(gn);
118 if (!opts.noExecute && eunlink(file) != -1) {
119 Error("*** %s removed", file);
125 * Interrupt the creation of the current target and remove it if it ain't
126 * precious. Then exit.
128 * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
130 * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
131 * left the logic alone for now. - dholland 20160826
134 CompatInterrupt(int signo)
136 CompatDeleteTarget(curTarg);
138 if (curTarg != NULL && !Targ_Precious(curTarg)) {
140 * Run .INTERRUPT only if hit with interrupt signal
142 if (signo == SIGINT) {
143 GNode *gn = Targ_FindNode(".INTERRUPT");
150 if (signo == SIGQUIT)
154 * If there is a child running, pass the signal on.
155 * We will exist after it has exited.
158 if (compatChild > 0) {
159 KILLPG(compatChild, signo);
161 bmake_signal(signo, SIG_DFL);
167 DebugFailedTarget(const char *cmd, const GNode *gn)
170 debug_printf("\n*** Failed target: %s\n*** Failed command: ",
173 /* Replace runs of whitespace with a single space, to reduce
174 * the amount of whitespace for multi-line command lines. */
176 if (ch_isspace(*p)) {
178 cpp_skip_whitespace(&p);
180 debug_printf("%c", *p);
188 UseShell(const char *cmd MAKE_ATTR_UNUSED)
190 #if !defined(MAKE_NATIVE)
192 * In a non-native build, the host environment might be weird enough
193 * that it's necessary to go through a shell to get the correct
194 * behaviour. Or perhaps the shell has been replaced with something
195 * that does extra logging, and that should not be bypassed.
200 * Search for meta characters in the command. If there are no meta
201 * characters, there's no need to execute a shell to execute the
204 * Additionally variable assignments and empty commands
205 * go to the shell. Therefore treat '=' and ':' like shell
206 * meta characters as documented in make(1).
209 return needshell(cmd);
214 * Execute the next command for a target. If the command returns an error,
215 * the node's made field is set to ERROR and creation stops.
218 * cmdp Command to execute
219 * gn Node from which the command came
220 * ln List node that contains the command
223 * 0 if the command succeeded, 1 if an error occurred.
226 Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
228 char *cmdStart; /* Start of expanded command */
230 bool silent; /* Don't print command */
231 bool doIt; /* Execute even if -n */
232 volatile bool errCheck; /* Check errors */
233 WAIT_T reason; /* Reason for child's death */
234 WAIT_T status; /* Description of child's death */
235 pid_t cpid; /* Child actually found */
236 pid_t retstat; /* Result of wait */
237 const char **volatile av; /* Argument vector for thing to exec */
238 char **volatile mav; /* Copy of the argument vector for freeing */
239 bool useShell; /* True if command should be executed
241 const char *volatile cmd = cmdp;
243 silent = (gn->type & OP_SILENT) != 0;
244 errCheck = !(gn->type & OP_IGNORE);
247 (void)Var_Subst(cmd, gn, VARE_WANTRES, &cmdStart);
248 /* TODO: handle errors */
250 if (cmdStart[0] == '\0') {
255 LstNode_Set(ln, cmdStart);
257 if (gn->type & OP_SAVE_CMDS) {
258 GNode *endNode = Targ_GetEndNode();
261 * Append the expanded command, to prevent the
262 * local variables from being interpreted in the
263 * scope of the .END node.
265 * A probably unintended side effect of this is that
266 * the expanded command will be expanded again in the
267 * .END node. Therefore, a literal '$' in these
268 * commands must be written as '$$$$' instead of the
271 Lst_Append(&endNode->commands, cmdStart);
275 if (strcmp(cmdStart, "...") == 0) {
276 gn->type |= OP_SAVE_CMDS;
282 silent = !DEBUG(LOUD);
283 else if (*cmd == '-')
285 else if (*cmd == '+') {
287 if (shellName == NULL) /* we came here from jobs */
294 while (ch_isspace(*cmd))
298 * If we did not end up with a command, just skip it.
303 useShell = UseShell(cmd);
305 * Print the command before echoing if we're not supposed to be quiet
306 * for this one. We also print the command if -n given.
308 if (!silent || !GNode_ShouldExecute(gn)) {
314 * If we're not supposed to execute any commands, this is as far as
317 if (!doIt && !GNode_ShouldExecute(gn))
320 DEBUG1(JOB, "Execute: '%s'\n", cmd);
324 * We need to pass the command off to the shell, typically
325 * because the command contains a "meta" character.
327 static const char *shargv[5];
329 /* The following work for any of the builtin shell specs. */
331 shargv[shargc++] = shellPath;
332 if (errCheck && shellErrFlag != NULL)
333 shargv[shargc++] = shellErrFlag;
334 shargv[shargc++] = DEBUG(SHELL) ? "-xc" : "-c";
335 shargv[shargc++] = cmd;
336 shargv[shargc] = NULL;
342 * No meta-characters, so no need to exec a shell. Break the
343 * command into words to form an argument vector we can
346 Words words = Str_Words(cmd, false);
361 * Fork and execute the single command. If the fork fails, we abort.
363 compatChild = cpid = vfork();
365 Fatal("Could not fork");
373 (void)execvp(av[0], (char *const *)UNCONST(av));
374 execDie("exec", av[0]);
380 /* XXX: Memory management looks suspicious here. */
381 /* XXX: Setting a list item to NULL is unexpected. */
386 meta_compat_parent(cpid);
391 * The child is off and running. Now all we can do is wait...
393 while ((retstat = wait(&reason)) != cpid) {
395 JobReapChild(retstat, reason, false); /* not ours? */
396 if (retstat == -1 && errno != EINTR) {
402 Fatal("error in wait: %d: %s", retstat, strerror(errno));
404 if (WIFSTOPPED(reason)) {
405 status = WSTOPSIG(reason); /* stopped */
406 } else if (WIFEXITED(reason)) {
407 status = WEXITSTATUS(reason); /* exited */
408 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
410 meta_cmd_finish(NULL);
415 DebugFailedTarget(cmd, gn);
416 printf("*** Error code %d", status);
419 status = WTERMSIG(reason); /* signaled */
420 printf("*** Signal %d", status);
424 if (!WIFEXITED(reason) || status != 0) {
428 meta_job_error(NULL, gn, false, status);
432 if (opts.keepgoing) {
434 * Abort the current target,
435 * but let others continue.
437 printf(" (continuing)\n");
442 CompatDeleteTarget(gn);
445 * Continue executing commands for this target.
446 * If we return 0, this will happen...
448 printf(" (ignored)\n");
455 if (compatSigno != 0) {
456 bmake_signal(compatSigno, SIG_DFL);
457 kill(myPid, compatSigno);
464 RunCommands(GNode *gn)
468 for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
469 const char *cmd = ln->datum;
470 if (Compat_RunCommand(cmd, gn, ln) != 0)
476 MakeNodes(GNodeList *gnodes, GNode *pgn)
480 for (ln = gnodes->first; ln != NULL; ln = ln->next) {
481 GNode *cohort = ln->datum;
482 Compat_Make(cohort, pgn);
487 MakeUnmade(GNode *gn, GNode *pgn)
490 assert(gn->made == UNMADE);
493 * First mark ourselves to be made, then apply whatever transformations
494 * the suffix module thinks are necessary. Once that's done, we can
495 * descend and make all our children. If any of them has an error
496 * but the -k flag was given, our 'make' field will be set to false
497 * again. This is our signal to not attempt to do anything but abort
498 * our parent as well.
501 gn->made = BEINGMADE;
503 if (!(gn->type & OP_MADE))
506 MakeNodes(&gn->children, gn);
508 if (!(gn->flags & REMAKE)) {
510 pgn->flags &= ~(unsigned)REMAKE;
514 if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
515 Var_Set(pgn, IMPSRC, GNode_VarTarget(gn));
518 * All the children were made ok. Now youngestChild->mtime contains the
519 * modification time of the newest child, we need to find out if we
520 * exist and when we were modified last. The criteria for datedness
521 * are defined by GNode_IsOODate.
523 DEBUG1(MAKE, "Examining %s...", gn->name);
524 if (!GNode_IsOODate(gn)) {
526 DEBUG0(MAKE, "up-to-date.\n");
531 * If the user is just seeing if something is out-of-date, exit now
532 * to tell him/her "yes".
534 DEBUG0(MAKE, "out-of-date.\n");
539 * We need to be re-made.
540 * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
542 GNode_SetLocalVars(gn);
545 * Alter our type to tell if errors should be ignored or things
546 * should not be printed so Compat_RunCommand knows what to do.
548 if (opts.ignoreErrors)
549 gn->type |= OP_IGNORE;
551 gn->type |= OP_SILENT;
553 if (Job_CheckCommands(gn, Fatal)) {
555 * Our commands are ok, but we still have to worry about
558 if (!opts.touchFlag || (gn->type & OP_MAKE)) {
561 if (useMeta && GNode_ShouldExecute(gn)) {
562 meta_job_start(NULL, gn);
568 Job_Touch(gn, (gn->type & OP_SILENT) != 0);
574 if (useMeta && GNode_ShouldExecute(gn)) {
575 if (meta_job_finish(NULL) != 0)
580 if (gn->made != ERROR) {
582 * If the node was made successfully, mark it so, update
583 * its modification time and timestamp all its parents.
584 * This is to keep its state from affecting that of its parent.
587 if (Make_Recheck(gn) == 0)
589 if (!(gn->type & OP_EXEC)) {
590 pgn->flags |= CHILDMADE;
591 GNode_UpdateYoungestChild(pgn, gn);
593 } else if (opts.keepgoing) {
594 pgn->flags &= ~(unsigned)REMAKE;
596 PrintOnError(gn, "\nStop.");
603 MakeOther(GNode *gn, GNode *pgn)
606 if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
607 const char *target = GNode_VarTarget(gn);
608 Var_Set(pgn, IMPSRC, target != NULL ? target : "");
613 Error("Graph cycles through %s", gn->name);
615 pgn->flags &= ~(unsigned)REMAKE;
618 if (!(gn->type & OP_EXEC)) {
619 pgn->flags |= CHILDMADE;
620 GNode_UpdateYoungestChild(pgn, gn);
624 if (!(gn->type & OP_EXEC))
625 GNode_UpdateYoungestChild(pgn, gn);
635 * If an error is detected and not being ignored, the process exits.
638 * gn The node to make
639 * pgn Parent to abort if necessary
643 * UPTODATE gn was already up-to-date.
644 * MADE gn was recreated successfully.
645 * ERROR An error occurred while gn was being created,
646 * either due to missing commands or in -k mode.
647 * ABORTED gn was not remade because one of its
648 * dependencies could not be made due to errors.
651 Compat_Make(GNode *gn, GNode *pgn)
653 if (shellName == NULL) /* we came here from jobs */
656 if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
657 if (!MakeUnmade(gn, pgn))
660 /* XXX: Replace with GNode_IsError(gn) */
661 } else if (gn->made == ERROR) {
663 * Already had an error when making this.
664 * Tell the parent to abort.
666 pgn->flags &= ~(unsigned)REMAKE;
672 MakeNodes(&gn->cohorts, pgn);
678 GNode *gn = Targ_FindNode(".BEGIN");
683 if (GNode_IsError(gn)) {
684 PrintOnError(gn, "\nStop.");
692 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
693 bmake_signal(SIGINT, CompatInterrupt);
694 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
695 bmake_signal(SIGTERM, CompatInterrupt);
696 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
697 bmake_signal(SIGHUP, CompatInterrupt);
698 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
699 bmake_signal(SIGQUIT, CompatInterrupt);
703 * Initialize this module and start making.
706 * targs The target nodes to re-create
709 Compat_Run(GNodeList *targs)
711 GNode *errorNode = NULL;
713 if (shellName == NULL)
718 /* Create the .END node now, to keep the (debug) output of the
719 * counter.mk test the same as before 2020-09-23. This implementation
720 * detail probably doesn't matter though. */
721 (void)Targ_GetEndNode();
727 * Expand .USE nodes right now, because they can modify the structure
730 Make_ExpandUse(targs);
732 while (!Lst_IsEmpty(targs)) {
733 GNode *gn = Lst_Dequeue(targs);
736 if (gn->made == UPTODATE) {
737 printf("`%s' is up to date.\n", gn->name);
738 } else if (gn->made == ABORTED) {
739 printf("`%s' not remade because of errors.\n",
742 if (GNode_IsError(gn) && errorNode == NULL)
746 /* If the user has defined a .END target, run its commands. */
747 if (errorNode == NULL) {
748 GNode *endNode = Targ_GetEndNode();
749 Compat_Make(endNode, endNode);
750 if (GNode_IsError(endNode))
754 if (errorNode != NULL) {
757 else if (DEBUG(GRAPH3))
759 PrintOnError(errorNode, "\nStop.");