1 /* $NetBSD: compat.c,v 1.173 2020/11/01 17:47:26 rillig 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.173 2020/11/01 17:47:26 rillig Exp $");
104 static GNode *curTarg = NULL;
105 static pid_t compatChild;
106 static int compatSigno;
109 * CompatDeleteTarget -- delete a failed, interrupted, or otherwise
110 * 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);
124 /* Interrupt the creation of the current target and remove it if it ain't
125 * precious. Then exit.
127 * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
129 * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
130 * left the logic alone for now. - dholland 20160826
133 CompatInterrupt(int signo)
137 CompatDeleteTarget(curTarg);
139 if (curTarg != NULL && !Targ_Precious(curTarg)) {
141 * Run .INTERRUPT only if hit with interrupt signal
143 if (signo == SIGINT) {
144 gn = Targ_FindNode(".INTERRUPT");
151 if (signo == SIGQUIT)
155 * If there is a child running, pass the signal on.
156 * We will exist after it has exited.
159 if (compatChild > 0) {
160 KILLPG(compatChild, signo);
162 bmake_signal(signo, SIG_DFL);
167 /* Execute the next command for a target. If the command returns an error,
168 * the node's made field is set to ERROR and creation stops.
171 * cmdp Command to execute
172 * gnp Node from which the command came
175 * 0 if the command succeeded, 1 if an error occurred.
178 Compat_RunCommand(const char *cmdp, GNode *gn)
180 char *cmdStart; /* Start of expanded command */
182 Boolean silent; /* Don't print command */
183 Boolean doIt; /* Execute even if -n */
184 volatile Boolean errCheck; /* Check errors */
185 WAIT_T reason; /* Reason for child's death */
186 int status; /* Description of child's death */
187 pid_t cpid; /* Child actually found */
188 pid_t retstat; /* Result of wait */
189 StringListNode *cmdNode; /* Node where current command is located */
190 const char **volatile av; /* Argument vector for thing to exec */
191 char **volatile mav; /* Copy of the argument vector for freeing */
192 Boolean useShell; /* TRUE if command should be executed
194 const char *volatile cmd = cmdp;
196 silent = (gn->type & OP_SILENT) != 0;
197 errCheck = !(gn->type & OP_IGNORE);
200 /* Luckily the commands don't end up in a string pool, otherwise
201 * this comparison could match too early, in a dependency using "..."
202 * for delayed commands, run in parallel mode, using the same shell
203 * command line more than once; see JobPrintCommand.
204 * TODO: write a unit-test to protect against this potential bug. */
205 cmdNode = Lst_FindDatum(gn->commands, cmd);
206 (void)Var_Subst(cmd, gn, VARE_WANTRES, &cmdStart);
207 /* TODO: handle errors */
209 if (*cmdStart == '\0') {
214 LstNode_Set(cmdNode, cmdStart);
216 if (gn->type & OP_SAVE_CMDS) {
217 GNode *endNode = Targ_GetEndNode();
219 Lst_Append(endNode->commands, cmdStart);
223 if (strcmp(cmdStart, "...") == 0) {
224 gn->type |= OP_SAVE_CMDS;
228 while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
231 silent = !DEBUG(LOUD);
238 if (!shellName) /* we came here from jobs */
245 while (ch_isspace(*cmd))
249 * If we did not end up with a command, just skip it.
254 #if !defined(MAKE_NATIVE)
256 * In a non-native build, the host environment might be weird enough
257 * that it's necessary to go through a shell to get the correct
258 * behaviour. Or perhaps the shell has been replaced with something
259 * that does extra logging, and that should not be bypassed.
264 * Search for meta characters in the command. If there are no meta
265 * characters, there's no need to execute a shell to execute the
268 * Additionally variable assignments and empty commands
269 * go to the shell. Therefore treat '=' and ':' like shell
270 * meta characters as documented in make(1).
273 useShell = needshell(cmd);
277 * Print the command before echoing if we're not supposed to be quiet for
278 * this one. We also print the command if -n given.
280 if (!silent || !GNode_ShouldExecute(gn)) {
286 * If we're not supposed to execute any commands, this is as far as
289 if (!doIt && !GNode_ShouldExecute(gn)) {
292 DEBUG1(JOB, "Execute: '%s'\n", cmd);
296 * We need to pass the command off to the shell, typically
297 * because the command contains a "meta" character.
299 static const char *shargv[5];
303 shargv[shargc++] = shellPath;
305 * The following work for any of the builtin shell specs.
307 if (errCheck && shellErrFlag) {
308 shargv[shargc++] = shellErrFlag;
311 shargv[shargc++] = "-xc";
313 shargv[shargc++] = "-c";
314 shargv[shargc++] = cmd;
315 shargv[shargc] = NULL;
321 * No meta-characters, so no need to exec a shell. Break the command
322 * into words to form an argument vector we can execute.
324 Words words = Str_Words(cmd, FALSE);
337 * Fork and execute the single command. If the fork fails, we abort.
339 compatChild = cpid = vFork();
341 Fatal("Could not fork");
350 (void)execvp(av[0], (char *const *)UNCONST(av));
351 execDie("exec", av[0]);
357 /* XXX: Memory management looks suspicious here. */
358 /* XXX: Setting a list item to NULL is unexpected. */
359 LstNode_SetNull(cmdNode);
363 meta_compat_parent(cpid);
368 * The child is off and running. Now all we can do is wait...
370 while ((retstat = wait(&reason)) != cpid) {
372 JobReapChild(retstat, reason, FALSE); /* not ours? */
373 if (retstat == -1 && errno != EINTR) {
379 Fatal("error in wait: %d: %s", retstat, strerror(errno));
381 if (WIFSTOPPED(reason)) {
382 status = WSTOPSIG(reason); /* stopped */
383 } else if (WIFEXITED(reason)) {
384 status = WEXITSTATUS(reason); /* exited */
385 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
387 meta_cmd_finish(NULL);
393 debug_printf("\n*** Failed target: %s\n*** Failed command: ",
395 for (cp = cmd; *cp; ) {
396 if (ch_isspace(*cp)) {
398 while (ch_isspace(*cp))
401 debug_printf("%c", *cp);
407 printf("*** Error code %d", status);
410 status = WTERMSIG(reason); /* signaled */
411 printf("*** Signal %d", status);
415 if (!WIFEXITED(reason) || status != 0) {
419 meta_job_error(NULL, gn, 0, status);
423 if (opts.keepgoing) {
424 /* Abort the current target, but let others continue. */
425 printf(" (continuing)\n");
430 CompatDeleteTarget(gn);
433 * Continue executing commands for this target.
434 * If we return 0, this will happen...
436 printf(" (ignored)\n");
444 bmake_signal(compatSigno, SIG_DFL);
445 kill(myPid, compatSigno);
452 RunCommands(GNode *gn)
455 for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
456 const char *cmd = ln->datum;
457 if (Compat_RunCommand(cmd, gn) != 0)
463 MakeNodes(GNodeList *gnodes, GNode *pgn)
466 for (ln = gnodes->first; ln != NULL; ln = ln->next) {
467 GNode *cohort = ln->datum;
468 Compat_Make(cohort, pgn);
474 * If an error is detected and not being ignored, the process exits.
477 * gn The node to make
478 * pgn Parent to abort if necessary
481 Compat_Make(GNode *gn, GNode *pgn)
483 if (!shellName) /* we came here from jobs */
485 if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
487 * First mark ourselves to be made, then apply whatever transformations
488 * the suffix module thinks are necessary. Once that's done, we can
489 * descend and make all our children. If any of them has an error
490 * but the -k flag was given, our 'make' field will be set FALSE again.
491 * This is our signal to not attempt to do anything but abort our
495 gn->made = BEINGMADE;
496 if (!(gn->type & OP_MADE))
498 MakeNodes(gn->children, gn);
499 if (!(gn->flags & REMAKE)) {
501 pgn->flags &= ~(unsigned)REMAKE;
505 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL)
506 Var_Set(IMPSRC, GNode_VarTarget(gn), pgn);
509 * All the children were made ok. Now youngestChild->mtime contains the
510 * modification time of the newest child, we need to find out if we
511 * exist and when we were modified last. The criteria for datedness
512 * are defined by the Make_OODate function.
514 DEBUG1(MAKE, "Examining %s...", gn->name);
515 if (!Make_OODate(gn)) {
517 DEBUG0(MAKE, "up-to-date.\n");
520 DEBUG0(MAKE, "out-of-date.\n");
523 * If the user is just seeing if something is out-of-date, exit now
524 * to tell him/her "yes".
526 if (opts.queryFlag) {
531 * We need to be re-made. We also have to make sure we've got a $?
532 * variable. To be nice, we also define the $> variable using
538 * Alter our type to tell if errors should be ignored or things
539 * should not be printed so CompatRunCommand knows what to do.
542 gn->type |= OP_IGNORE;
544 gn->type |= OP_SILENT;
546 if (Job_CheckCommands(gn, Fatal)) {
548 * Our commands are ok, but we still have to worry about the -t
551 if (!opts.touchFlag || (gn->type & OP_MAKE)) {
554 if (useMeta && GNode_ShouldExecute(gn)) {
555 meta_job_start(NULL, gn);
561 Job_Touch(gn, (gn->type & OP_SILENT) != 0);
567 if (useMeta && GNode_ShouldExecute(gn)) {
568 if (meta_job_finish(NULL) != 0)
573 if (gn->made != ERROR) {
575 * If the node was made successfully, mark it so, update
576 * its modification time and timestamp all its parents. Note
577 * that for .ZEROTIME targets, the timestamping isn't done.
578 * This is to keep its state from affecting that of its parent.
581 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
582 if (!(gn->type & OP_EXEC)) {
583 pgn->flags |= CHILDMADE;
584 Make_TimeStamp(pgn, gn);
586 } else if (opts.keepgoing) {
587 pgn->flags &= ~(unsigned)REMAKE;
589 PrintOnError(gn, "\nStop.");
592 } else if (gn->made == ERROR) {
593 /* Already had an error when making this. Tell the parent to abort. */
594 pgn->flags &= ~(unsigned)REMAKE;
596 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
597 const char *target = GNode_VarTarget(gn);
598 Var_Set(IMPSRC, target != NULL ? target : "", pgn);
602 Error("Graph cycles through %s", gn->name);
604 pgn->flags &= ~(unsigned)REMAKE;
607 if ((gn->type & OP_EXEC) == 0) {
608 pgn->flags |= CHILDMADE;
609 Make_TimeStamp(pgn, gn);
613 if ((gn->type & OP_EXEC) == 0) {
614 Make_TimeStamp(pgn, gn);
623 MakeNodes(gn->cohorts, pgn);
626 /* Initialize this module and start making.
629 * targs The target nodes to re-create
632 Compat_Run(GNodeList *targs)
634 GNode *gn = NULL; /* Current root target */
635 int errors; /* Number of targets not remade due to errors */
640 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
641 bmake_signal(SIGINT, CompatInterrupt);
642 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
643 bmake_signal(SIGTERM, CompatInterrupt);
644 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
645 bmake_signal(SIGHUP, CompatInterrupt);
646 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
647 bmake_signal(SIGQUIT, CompatInterrupt);
649 /* Create the .END node now, to keep the (debug) output of the
650 * counter.mk test the same as before 2020-09-23. This implementation
651 * detail probably doesn't matter though. */
652 (void)Targ_GetEndNode();
654 * If the user has defined a .BEGIN target, execute the commands attached
657 if (!opts.queryFlag) {
658 gn = Targ_FindNode(".BEGIN");
661 if (gn->made == ERROR) {
662 PrintOnError(gn, "\nStop.");
669 * Expand .USE nodes right now, because they can modify the structure
672 Make_ExpandUse(targs);
675 * For each entry in the list of targets to create, call Compat_Make on
676 * it to create the thing. Compat_Make will leave the 'made' field of gn
677 * in one of several states:
678 * UPTODATE gn was already up-to-date
679 * MADE gn was recreated successfully
680 * ERROR An error occurred while gn was being created
681 * ABORTED gn was not remade because one of its inferiors
682 * could not be made due to errors.
685 while (!Lst_IsEmpty(targs)) {
686 gn = Lst_Dequeue(targs);
689 if (gn->made == UPTODATE) {
690 printf("`%s' is up to date.\n", gn->name);
691 } else if (gn->made == ABORTED) {
692 printf("`%s' not remade because of errors.\n", gn->name);
698 * If the user has defined a .END target, run its commands.
701 GNode *endNode = Targ_GetEndNode();
702 Compat_Make(endNode, endNode);
703 /* XXX: Did you mean endNode->made instead of gn->made? */
704 if (gn->made == ERROR) {
705 PrintOnError(gn, "\nStop.");