1 /* $NetBSD: compat.c,v 1.93 2013/09/02 19:26:42 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.93 2013/09/02 19:26:42 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.93 2013/09/02 19:26:42 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);
186 if (signo == SIGQUIT)
188 bmake_signal(signo, SIG_DFL);
193 *-----------------------------------------------------------------------
194 * CompatRunCommand --
195 * Execute the next command for a target. If the command returns an
196 * error, the node's made field is set to ERROR and creation stops.
199 * cmdp Command to execute
200 * gnp Node from which the command came
203 * 0 if the command succeeded, 1 if an error occurred.
206 * The node's 'made' field may be set to ERROR.
208 *-----------------------------------------------------------------------
211 CompatRunCommand(void *cmdp, void *gnp)
213 char *cmdStart; /* Start of expanded command */
215 Boolean silent, /* Don't print command */
216 doIt; /* Execute even if -n */
217 volatile Boolean errCheck; /* Check errors */
218 WAIT_T reason; /* Reason for child's death */
219 int status; /* Description of child's death */
220 pid_t cpid; /* Child actually found */
221 pid_t retstat; /* Result of wait */
222 LstNode cmdNode; /* Node where current command is located */
223 const char ** volatile av; /* Argument vector for thing to exec */
224 char ** volatile mav;/* Copy of the argument vector for freeing */
225 int argc; /* Number of arguments in av or 0 if not
226 * dynamically allocated */
227 Boolean local; /* TRUE if command should be executed
229 Boolean useShell; /* TRUE if command should be executed
231 char * volatile cmd = (char *)cmdp;
232 GNode *gn = (GNode *)gnp;
234 silent = gn->type & OP_SILENT;
235 errCheck = !(gn->type & OP_IGNORE);
238 cmdNode = Lst_Member(gn->commands, cmd);
239 cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
242 * brk_string will return an argv with a NULL in av[0], thus causing
243 * execvp to choke and die horribly. Besides, how can we execute a null
244 * command? In any case, we warn the user that the command expanded to
245 * nothing (is this the right thing to do?).
248 if (*cmdStart == '\0') {
253 Lst_Replace(cmdNode, cmdStart);
255 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
256 (void)Lst_AtEnd(ENDNode->commands, cmdStart);
259 if (strcmp(cmdStart, "...") == 0) {
260 gn->type |= OP_SAVE_CMDS;
264 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
267 silent = DEBUG(LOUD) ? FALSE : TRUE;
274 if (!meta[0]) /* we came here from jobs */
281 while (isspace((unsigned char)*cmd))
285 * If we did not end up with a command, just skip it.
290 #if !defined(MAKE_NATIVE)
292 * In a non-native build, the host environment might be weird enough
293 * that it's necessary to go through a shell to get the correct
294 * behaviour. Or perhaps the shell has been replaced with something
295 * that does extra logging, and that should not be bypassed.
300 * Search for meta characters in the command. If there are no meta
301 * characters, there's no need to execute a shell to execute the
304 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
307 useShell = (*cp != '\0');
311 * Print the command before echoing if we're not supposed to be quiet for
312 * this one. We also print the command if -n given.
314 if (!silent || NoExecute(gn)) {
320 * If we're not supposed to execute any commands, this is as far as
323 if (!doIt && NoExecute(gn)) {
327 fprintf(debug_file, "Execute: '%s'\n", cmd);
332 * We need to pass the command off to the shell, typically
333 * because the command contains a "meta" character.
335 static const char *shargv[5];
339 shargv[shargc++] = shellPath;
341 * The following work for any of the builtin shell specs.
343 if (errCheck && shellErrFlag) {
344 shargv[shargc++] = shellErrFlag;
347 shargv[shargc++] = "-xc";
349 shargv[shargc++] = "-c";
350 shargv[shargc++] = cmd;
351 shargv[shargc++] = NULL;
358 * No meta-characters, so no need to exec a shell. Break the command
359 * into words to form an argument vector we can execute.
361 mav = brk_string(cmd, &argc, TRUE, &bp);
378 * Fork and execute the single command. If the fork fails, we abort.
382 Fatal("Could not fork");
392 (void)execvp(av[0], (char *const *)UNCONST(av));
394 (void)execv(av[0], (char *const *)UNCONST(av));
395 execError("exec", av[0]);
402 Lst_Replace(cmdNode, NULL);
406 meta_compat_parent();
411 * The child is off and running. Now all we can do is wait...
415 while ((retstat = wait(&reason)) != cpid) {
417 JobReapChild(retstat, reason, FALSE); /* not ours? */
418 if (retstat == -1 && errno != EINTR) {
424 if (WIFSTOPPED(reason)) {
425 status = WSTOPSIG(reason); /* stopped */
426 } else if (WIFEXITED(reason)) {
427 status = WEXITSTATUS(reason); /* exited */
428 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
430 meta_cmd_finish(NULL);
435 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
437 for (cp = cmd; *cp; ) {
438 if (isspace((unsigned char)*cp)) {
439 fprintf(debug_file, " ");
440 while (isspace((unsigned char)*cp))
443 fprintf(debug_file, "%c", *cp);
447 fprintf(debug_file, "\n");
449 printf("*** Error code %d", status);
452 status = WTERMSIG(reason); /* signaled */
453 printf("*** Signal %d", status);
457 if (!WIFEXITED(reason) || (status != 0)) {
461 meta_job_error(NULL, gn, 0, status);
467 * Abort the current target, but let others
470 printf(" (continuing)\n");
474 * Continue executing commands for this target.
475 * If we return 0, this will happen...
477 printf(" (ignored)\n");
483 Fatal("error in wait: %d: %s", retstat, strerror(errno));
493 *-----------------------------------------------------------------------
498 * gnp The node to make
499 * pgnp Parent to abort if necessary
505 * If an error is detected and not being ignored, the process exits.
507 *-----------------------------------------------------------------------
510 Compat_Make(void *gnp, void *pgnp)
512 GNode *gn = (GNode *)gnp;
513 GNode *pgn = (GNode *)pgnp;
515 if (!meta[0]) /* we came here from jobs */
517 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
519 * First mark ourselves to be made, then apply whatever transformations
520 * the suffix module thinks are necessary. Once that's done, we can
521 * descend and make all our children. If any of them has an error
522 * but the -k flag was given, our 'make' field will be set FALSE again.
523 * This is our signal to not attempt to do anything but abort our
527 gn->made = BEINGMADE;
528 if ((gn->type & OP_MADE) == 0)
530 Lst_ForEach(gn->children, Compat_Make, gn);
531 if ((gn->flags & REMAKE) == 0) {
533 pgn->flags &= ~REMAKE;
537 if (Lst_Member(gn->iParents, pgn) != NULL) {
539 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
545 * All the children were made ok. Now cmgn->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 the Make_OODate function.
551 fprintf(debug_file, "Examining %s...", gn->name);
553 if (! Make_OODate(gn)) {
556 fprintf(debug_file, "up-to-date.\n");
559 } else if (DEBUG(MAKE)) {
560 fprintf(debug_file, "out-of-date.\n");
564 * If the user is just seeing if something is out-of-date, exit now
565 * to tell him/her "yes".
572 * We need to be re-made. We also have to make sure we've got a $?
573 * variable. To be nice, we also define the $> variable using
579 * Alter our type to tell if errors should be ignored or things
580 * should not be printed so CompatRunCommand knows what to do.
582 if (Targ_Ignore(gn)) {
583 gn->type |= OP_IGNORE;
585 if (Targ_Silent(gn)) {
586 gn->type |= OP_SILENT;
589 if (Job_CheckCommands(gn, Fatal)) {
591 * Our commands are ok, but we still have to worry about the -t
594 if (!touchFlag || (gn->type & OP_MAKE)) {
597 if (useMeta && !NoExecute(gn)) {
598 meta_job_start(NULL, gn);
601 Lst_ForEach(gn->commands, CompatRunCommand, gn);
604 Job_Touch(gn, gn->type & OP_SILENT);
610 if (useMeta && !NoExecute(gn)) {
611 meta_job_finish(NULL);
615 if (gn->made != ERROR) {
617 * If the node was made successfully, mark it so, update
618 * its modification time and timestamp all its parents. Note
619 * that for .ZEROTIME targets, the timestamping isn't done.
620 * This is to keep its state from affecting that of its parent.
623 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
624 if (!(gn->type & OP_EXEC)) {
625 pgn->flags |= CHILDMADE;
626 Make_TimeStamp(pgn, gn);
628 } else if (keepgoing) {
629 pgn->flags &= ~REMAKE;
631 PrintOnError(gn, "\n\nStop.");
634 } else if (gn->made == ERROR) {
636 * Already had an error when making this beastie. Tell the parent
639 pgn->flags &= ~REMAKE;
641 if (Lst_Member(gn->iParents, pgn) != NULL) {
643 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
649 Error("Graph cycles through %s", gn->name);
651 pgn->flags &= ~REMAKE;
654 if ((gn->type & OP_EXEC) == 0) {
655 pgn->flags |= CHILDMADE;
656 Make_TimeStamp(pgn, gn);
660 if ((gn->type & OP_EXEC) == 0) {
661 Make_TimeStamp(pgn, gn);
670 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
675 *-----------------------------------------------------------------------
677 * Initialize this mode and start making.
680 * targs List of target nodes to re-create
688 *-----------------------------------------------------------------------
691 Compat_Run(Lst targs)
693 GNode *gn = NULL;/* Current root target */
694 int errors; /* Number of targets not remade due to errors */
698 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
699 bmake_signal(SIGINT, CompatInterrupt);
701 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
702 bmake_signal(SIGTERM, CompatInterrupt);
704 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
705 bmake_signal(SIGHUP, CompatInterrupt);
707 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
708 bmake_signal(SIGQUIT, CompatInterrupt);
711 ENDNode = Targ_FindNode(".END", TARG_CREATE);
712 ENDNode->type = OP_SPECIAL;
714 * If the user has defined a .BEGIN target, execute the commands attached
718 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
721 if (gn->made == ERROR) {
722 PrintOnError(gn, "\n\nStop.");
729 * Expand .USE nodes right now, because they can modify the structure
732 Make_ExpandUse(targs);
735 * For each entry in the list of targets to create, call Compat_Make on
736 * it to create the thing. Compat_Make will leave the 'made' field of gn
737 * in one of several states:
738 * UPTODATE gn was already up-to-date
739 * MADE gn was recreated successfully
740 * ERROR An error occurred while gn was being created
741 * ABORTED gn was not remade because one of its inferiors
742 * could not be made due to errors.
745 while (!Lst_IsEmpty (targs)) {
746 gn = (GNode *)Lst_DeQueue(targs);
749 if (gn->made == UPTODATE) {
750 printf("`%s' is up to date.\n", gn->name);
751 } else if (gn->made == ABORTED) {
752 printf("`%s' not remade because of errors.\n", gn->name);
758 * If the user has defined a .END target, run its commands.
761 Compat_Make(ENDNode, ENDNode);
762 if (gn->made == ERROR) {
763 PrintOnError(gn, "\n\nStop.");