1 /* $NetBSD: compat.c,v 1.80 2010/08/07 06:44:08 sjg 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
73 static char rcsid[] = "$NetBSD: compat.c,v 1.80 2010/08/07 06:44:08 sjg Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
80 __RCSID("$NetBSD: compat.c,v 1.80 2010/08/07 06:44:08 sjg Exp $");
87 * The routines in this file implement the full-compatibility
88 * mode of PMake. Most of the special functionality of PMake
89 * is available in this mode. Things not supported:
91 * - friendly variable substitution.
94 * Compat_Run Initialize things for this module and recreate
95 * thems as need creatin'
101 #include <sys/types.h>
102 #include <sys/stat.h>
114 #include "pathnames.h"
117 * The following array is used to make a fast determination of which
118 * characters are interpreted specially by the shell. If a command
119 * contains any of these characters, it is executed by the shell, not
123 static char meta[256];
125 static GNode *curTarg = NULL;
126 static GNode *ENDNode;
127 static void CompatInterrupt(int);
134 Shell_Init(); /* setup default shell */
136 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
137 meta[(unsigned char) *cp] = 1;
140 * The null character serves as a sentinel in the string.
146 *-----------------------------------------------------------------------
148 * Interrupt the creation of the current target and remove it if
155 * The target is removed and the process exits. If .INTERRUPT exists,
156 * its commands are run first WITH INTERRUPTS IGNORED..
158 *-----------------------------------------------------------------------
161 CompatInterrupt(int signo)
165 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
167 char *file = Var_Value(TARGET, curTarg, &p1);
169 if (!noExecute && eunlink(file) != -1) {
170 Error("*** %s removed", file);
176 * Run .INTERRUPT only if hit with interrupt signal
178 if (signo == SIGINT) {
179 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
190 *-----------------------------------------------------------------------
191 * CompatRunCommand --
192 * Execute the next command for a target. If the command returns an
193 * error, the node's made field is set to ERROR and creation stops.
196 * cmdp Command to execute
197 * gnp Node from which the command came
200 * 0 if the command succeeded, 1 if an error occurred.
203 * The node's 'made' field may be set to ERROR.
205 *-----------------------------------------------------------------------
208 CompatRunCommand(void *cmdp, void *gnp)
210 char *cmdStart; /* Start of expanded command */
212 Boolean silent, /* Don't print command */
213 doIt; /* Execute even if -n */
214 volatile Boolean errCheck; /* Check errors */
215 WAIT_T reason; /* Reason for child's death */
216 int status; /* Description of child's death */
217 pid_t cpid; /* Child actually found */
218 pid_t retstat; /* Result of wait */
219 LstNode cmdNode; /* Node where current command is located */
220 const char ** volatile av; /* Argument vector for thing to exec */
221 char ** volatile mav;/* Copy of the argument vector for freeing */
222 int argc; /* Number of arguments in av or 0 if not
223 * dynamically allocated */
224 Boolean local; /* TRUE if command should be executed
226 Boolean useShell; /* TRUE if command should be executed
228 char * volatile cmd = (char *)cmdp;
229 GNode *gn = (GNode *)gnp;
231 silent = gn->type & OP_SILENT;
232 errCheck = !(gn->type & OP_IGNORE);
235 cmdNode = Lst_Member(gn->commands, cmd);
236 cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
239 * brk_string will return an argv with a NULL in av[0], thus causing
240 * execvp to choke and die horribly. Besides, how can we execute a null
241 * command? In any case, we warn the user that the command expanded to
242 * nothing (is this the right thing to do?).
245 if (*cmdStart == '\0') {
247 Error("%s expands to empty string", cmd);
251 Lst_Replace(cmdNode, cmdStart);
253 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
254 (void)Lst_AtEnd(ENDNode->commands, cmdStart);
257 if (strcmp(cmdStart, "...") == 0) {
258 gn->type |= OP_SAVE_CMDS;
262 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
265 silent = DEBUG(LOUD) ? FALSE : TRUE;
272 if (!meta[0]) /* we came here from jobs */
279 while (isspace((unsigned char)*cmd))
282 #if !defined(MAKE_NATIVE)
284 * In a non-native build, the host environment might be weird enough
285 * that it's necessary to go through a shell to get the correct
286 * behaviour. Or perhaps the shell has been replaced with something
287 * that does extra logging, and that should not be bypassed.
292 * Search for meta characters in the command. If there are no meta
293 * characters, there's no need to execute a shell to execute the
296 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
299 useShell = (*cp != '\0');
303 * Print the command before echoing if we're not supposed to be quiet for
304 * this one. We also print the command if -n given.
306 if (!silent || NoExecute(gn)) {
312 * If we're not supposed to execute any commands, this is as far as
315 if (!doIt && NoExecute(gn)) {
319 fprintf(debug_file, "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[4];
329 shargv[0] = shellPath;
331 * The following work for any of the builtin shell specs.
345 * No meta-characters, so no need to exec a shell. Break the command
346 * into words to form an argument vector we can execute.
348 mav = brk_string(cmd, &argc, TRUE, &bp);
353 av = (const char **)mav;
359 * Fork and execute the single command. If the fork fails, we abort.
363 Fatal("Could not fork");
369 (void)execvp(av[0], (char *const *)UNCONST(av));
371 (void)execv(av[0], (char *const *)UNCONST(av));
372 execError("exec", av[0]);
379 Lst_Replace(cmdNode, NULL);
382 * The child is off and running. Now all we can do is wait...
386 while ((retstat = wait(&reason)) != cpid) {
388 JobReapChild(retstat, reason, FALSE); /* not ours? */
389 if (retstat == -1 && errno != EINTR) {
395 if (WIFSTOPPED(reason)) {
396 status = WSTOPSIG(reason); /* stopped */
397 } else if (WIFEXITED(reason)) {
398 status = WEXITSTATUS(reason); /* exited */
401 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
403 for (cp = cmd; *cp; ) {
404 if (isspace((unsigned char)*cp)) {
405 fprintf(debug_file, " ");
406 while (isspace((unsigned char)*cp))
409 fprintf(debug_file, "%c", *cp);
413 fprintf(debug_file, "\n");
415 printf("*** Error code %d", status);
418 status = WTERMSIG(reason); /* signaled */
419 printf("*** Signal %d", status);
423 if (!WIFEXITED(reason) || (status != 0)) {
428 * Abort the current target, but let others
431 printf(" (continuing)\n");
435 * Continue executing commands for this target.
436 * If we return 0, this will happen...
438 printf(" (ignored)\n");
444 Fatal("error in wait: %d: %s", retstat, strerror(errno));
454 *-----------------------------------------------------------------------
459 * gnp The node to make
460 * pgnp Parent to abort if necessary
466 * If an error is detected and not being ignored, the process exits.
468 *-----------------------------------------------------------------------
471 Compat_Make(void *gnp, void *pgnp)
473 GNode *gn = (GNode *)gnp;
474 GNode *pgn = (GNode *)pgnp;
476 if (!meta[0]) /* we came here from jobs */
478 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
480 * First mark ourselves to be made, then apply whatever transformations
481 * the suffix module thinks are necessary. Once that's done, we can
482 * descend and make all our children. If any of them has an error
483 * but the -k flag was given, our 'make' field will be set FALSE again.
484 * This is our signal to not attempt to do anything but abort our
488 gn->made = BEINGMADE;
489 if ((gn->type & OP_MADE) == 0)
491 Lst_ForEach(gn->children, Compat_Make, gn);
492 if ((gn->flags & REMAKE) == 0) {
494 pgn->flags &= ~REMAKE;
498 if (Lst_Member(gn->iParents, pgn) != NULL) {
500 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
506 * All the children were made ok. Now cmtime contains the modification
507 * time of the newest child, we need to find out if we exist and when
508 * we were modified last. The criteria for datedness are defined by the
509 * Make_OODate function.
512 fprintf(debug_file, "Examining %s...", gn->name);
514 if (! Make_OODate(gn)) {
517 fprintf(debug_file, "up-to-date.\n");
520 } else if (DEBUG(MAKE)) {
521 fprintf(debug_file, "out-of-date.\n");
525 * If the user is just seeing if something is out-of-date, exit now
526 * to tell him/her "yes".
533 * We need to be re-made. We also have to make sure we've got a $?
534 * variable. To be nice, we also define the $> variable using
540 * Alter our type to tell if errors should be ignored or things
541 * should not be printed so CompatRunCommand knows what to do.
543 if (Targ_Ignore(gn)) {
544 gn->type |= OP_IGNORE;
546 if (Targ_Silent(gn)) {
547 gn->type |= OP_SILENT;
550 if (Job_CheckCommands(gn, Fatal)) {
552 * Our commands are ok, but we still have to worry about the -t
555 if (!touchFlag || (gn->type & OP_MAKE)) {
557 Lst_ForEach(gn->commands, CompatRunCommand, gn);
560 Job_Touch(gn, gn->type & OP_SILENT);
566 if (gn->made != ERROR) {
568 * If the node was made successfully, mark it so, update
569 * its modification time and timestamp all its parents. Note
570 * that for .ZEROTIME targets, the timestamping isn't done.
571 * This is to keep its state from affecting that of its parent.
574 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
575 if (!(gn->type & OP_EXEC)) {
576 pgn->flags |= CHILDMADE;
577 Make_TimeStamp(pgn, gn);
579 } else if (keepgoing) {
580 pgn->flags &= ~REMAKE;
582 PrintOnError(gn, "\n\nStop.");
585 } else if (gn->made == ERROR) {
587 * Already had an error when making this beastie. Tell the parent
590 pgn->flags &= ~REMAKE;
592 if (Lst_Member(gn->iParents, pgn) != NULL) {
594 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
600 Error("Graph cycles through %s", gn->name);
602 pgn->flags &= ~REMAKE;
605 if ((gn->type & OP_EXEC) == 0) {
606 pgn->flags |= CHILDMADE;
607 Make_TimeStamp(pgn, gn);
611 if ((gn->type & OP_EXEC) == 0) {
612 Make_TimeStamp(pgn, gn);
621 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
626 *-----------------------------------------------------------------------
628 * Initialize this mode and start making.
631 * targs List of target nodes to re-create
639 *-----------------------------------------------------------------------
642 Compat_Run(Lst targs)
644 GNode *gn = NULL;/* Current root target */
645 int errors; /* Number of targets not remade due to errors */
649 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
650 bmake_signal(SIGINT, CompatInterrupt);
652 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
653 bmake_signal(SIGTERM, CompatInterrupt);
655 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
656 bmake_signal(SIGHUP, CompatInterrupt);
658 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
659 bmake_signal(SIGQUIT, CompatInterrupt);
662 ENDNode = Targ_FindNode(".END", TARG_CREATE);
663 ENDNode->type = OP_SPECIAL;
665 * If the user has defined a .BEGIN target, execute the commands attached
669 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
672 if (gn->made == ERROR) {
673 PrintOnError(gn, "\n\nStop.");
680 * Expand .USE nodes right now, because they can modify the structure
683 Make_ExpandUse(targs);
686 * For each entry in the list of targets to create, call Compat_Make on
687 * it to create the thing. Compat_Make will leave the 'made' field of gn
688 * in one of several states:
689 * UPTODATE gn was already up-to-date
690 * MADE gn was recreated successfully
691 * ERROR An error occurred while gn was being created
692 * ABORTED gn was not remade because one of its inferiors
693 * could not be made due to errors.
696 while (!Lst_IsEmpty (targs)) {
697 gn = (GNode *)Lst_DeQueue(targs);
700 if (gn->made == UPTODATE) {
701 printf("`%s' is up to date.\n", gn->name);
702 } else if (gn->made == ABORTED) {
703 printf("`%s' not remade because of errors.\n", gn->name);
709 * If the user has defined a .END target, run its commands.
712 Compat_Make(ENDNode, ENDNode);
713 if (gn->made == ERROR) {
714 PrintOnError(gn, "\n\nStop.");