1 /* $NetBSD: compat.c,v 1.105 2016/05/12 20:28:34 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.105 2016/05/12 20:28:34 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.105 2016/05/12 20:28:34 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 "metachar.h"
115 #include "pathnames.h"
118 static GNode
*curTarg
= NULL
;
119 static GNode
*ENDNode
;
120 static void CompatInterrupt(int);
123 *-----------------------------------------------------------------------
125 * Interrupt the creation of the current target and remove it if
132 * The target is removed and the process exits. If .INTERRUPT exists,
133 * its commands are run first WITH INTERRUPTS IGNORED..
135 *-----------------------------------------------------------------------
138 CompatInterrupt(int signo
)
142 if ((curTarg
!= NULL
) && !Targ_Precious (curTarg
)) {
144 char *file
= Var_Value(TARGET
, curTarg
, &p1
);
146 if (!noExecute
&& eunlink(file
) != -1) {
147 Error("*** %s removed", file
);
153 * Run .INTERRUPT only if hit with interrupt signal
155 if (signo
== SIGINT
) {
156 gn
= Targ_FindNode(".INTERRUPT", TARG_NOCREATE
);
163 if (signo
== SIGQUIT
)
165 bmake_signal(signo
, SIG_DFL
);
170 *-----------------------------------------------------------------------
171 * CompatRunCommand --
172 * Execute the next command for a target. If the command returns an
173 * error, the node's made field is set to ERROR and creation stops.
176 * cmdp Command to execute
177 * gnp Node from which the command came
180 * 0 if the command succeeded, 1 if an error occurred.
183 * The node's 'made' field may be set to ERROR.
185 *-----------------------------------------------------------------------
188 CompatRunCommand(void *cmdp
, void *gnp
)
190 char *cmdStart
; /* Start of expanded command */
192 Boolean silent
, /* Don't print command */
193 doIt
; /* Execute even if -n */
194 volatile Boolean errCheck
; /* Check errors */
195 WAIT_T reason
; /* Reason for child's death */
196 int status
; /* Description of child's death */
197 pid_t cpid
; /* Child actually found */
198 pid_t retstat
; /* Result of wait */
199 LstNode cmdNode
; /* Node where current command is located */
200 const char ** volatile av
; /* Argument vector for thing to exec */
201 char ** volatile mav
;/* Copy of the argument vector for freeing */
202 int argc
; /* Number of arguments in av or 0 if not
203 * dynamically allocated */
204 Boolean local
; /* TRUE if command should be executed
206 Boolean useShell
; /* TRUE if command should be executed
208 char * volatile cmd
= (char *)cmdp
;
209 GNode
*gn
= (GNode
*)gnp
;
211 silent
= gn
->type
& OP_SILENT
;
212 errCheck
= !(gn
->type
& OP_IGNORE
);
215 cmdNode
= Lst_Member(gn
->commands
, cmd
);
216 cmdStart
= Var_Subst(NULL
, cmd
, gn
, VARF_WANTRES
);
219 * brk_string will return an argv with a NULL in av[0], thus causing
220 * execvp to choke and die horribly. Besides, how can we execute a null
221 * command? In any case, we warn the user that the command expanded to
222 * nothing (is this the right thing to do?).
225 if (*cmdStart
== '\0') {
230 Lst_Replace(cmdNode
, cmdStart
);
232 if ((gn
->type
& OP_SAVE_CMDS
) && (gn
!= ENDNode
)) {
233 (void)Lst_AtEnd(ENDNode
->commands
, cmdStart
);
236 if (strcmp(cmdStart
, "...") == 0) {
237 gn
->type
|= OP_SAVE_CMDS
;
241 while ((*cmd
== '@') || (*cmd
== '-') || (*cmd
== '+')) {
244 silent
= DEBUG(LOUD
) ? FALSE
: TRUE
;
251 if (!shellName
) /* we came here from jobs */
258 while (isspace((unsigned char)*cmd
))
262 * If we did not end up with a command, just skip it.
267 #if !defined(MAKE_NATIVE)
269 * In a non-native build, the host environment might be weird enough
270 * that it's necessary to go through a shell to get the correct
271 * behaviour. Or perhaps the shell has been replaced with something
272 * that does extra logging, and that should not be bypassed.
277 * Search for meta characters in the command. If there are no meta
278 * characters, there's no need to execute a shell to execute the
281 * Additionally variable assignments and empty commands
282 * go to the shell. Therefore treat '=' and ':' like shell
283 * meta characters as documented in make(1).
286 useShell
= needshell(cmd
, FALSE
);
290 * Print the command before echoing if we're not supposed to be quiet for
291 * this one. We also print the command if -n given.
293 if (!silent
|| NoExecute(gn
)) {
299 * If we're not supposed to execute any commands, this is as far as
302 if (!doIt
&& NoExecute(gn
)) {
306 fprintf(debug_file
, "Execute: '%s'\n", cmd
);
311 * We need to pass the command off to the shell, typically
312 * because the command contains a "meta" character.
314 static const char *shargv
[5];
318 shargv
[shargc
++] = shellPath
;
320 * The following work for any of the builtin shell specs.
322 if (errCheck
&& shellErrFlag
) {
323 shargv
[shargc
++] = shellErrFlag
;
326 shargv
[shargc
++] = "-xc";
328 shargv
[shargc
++] = "-c";
329 shargv
[shargc
++] = cmd
;
330 shargv
[shargc
++] = NULL
;
337 * No meta-characters, so no need to exec a shell. Break the command
338 * into words to form an argument vector we can execute.
340 mav
= brk_string(cmd
, &argc
, TRUE
, &bp
);
357 * Fork and execute the single command. If the fork fails, we abort.
361 Fatal("Could not fork");
371 (void)execvp(av
[0], (char *const *)UNCONST(av
));
373 (void)execv(av
[0], (char *const *)UNCONST(av
));
374 execError("exec", av
[0]);
381 Lst_Replace(cmdNode
, NULL
);
385 meta_compat_parent();
390 * The child is off and running. Now all we can do is wait...
394 while ((retstat
= wait(&reason
)) != cpid
) {
396 JobReapChild(retstat
, reason
, FALSE
); /* not ours? */
397 if (retstat
== -1 && errno
!= EINTR
) {
403 if (WIFSTOPPED(reason
)) {
404 status
= WSTOPSIG(reason
); /* stopped */
405 } else if (WIFEXITED(reason
)) {
406 status
= WEXITSTATUS(reason
); /* exited */
407 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
409 meta_cmd_finish(NULL
);
414 fprintf(debug_file
, "\n*** Failed target: %s\n*** Failed command: ",
416 for (cp
= cmd
; *cp
; ) {
417 if (isspace((unsigned char)*cp
)) {
418 fprintf(debug_file
, " ");
419 while (isspace((unsigned char)*cp
))
422 fprintf(debug_file
, "%c", *cp
);
426 fprintf(debug_file
, "\n");
428 printf("*** Error code %d", status
);
431 status
= WTERMSIG(reason
); /* signaled */
432 printf("*** Signal %d", status
);
436 if (!WIFEXITED(reason
) || (status
!= 0)) {
440 meta_job_error(NULL
, gn
, 0, status
);
446 * Abort the current target, but let others
449 printf(" (continuing)\n");
453 * Continue executing commands for this target.
454 * If we return 0, this will happen...
456 printf(" (ignored)\n");
462 Fatal("error in wait: %d: %s", retstat
, strerror(errno
));
472 *-----------------------------------------------------------------------
477 * gnp The node to make
478 * pgnp Parent to abort if necessary
484 * If an error is detected and not being ignored, the process exits.
486 *-----------------------------------------------------------------------
489 Compat_Make(void *gnp
, void *pgnp
)
491 GNode
*gn
= (GNode
*)gnp
;
492 GNode
*pgn
= (GNode
*)pgnp
;
494 if (!shellName
) /* we came here from jobs */
496 if (gn
->made
== UNMADE
&& (gn
== pgn
|| (pgn
->type
& OP_MADE
) == 0)) {
498 * First mark ourselves to be made, then apply whatever transformations
499 * the suffix module thinks are necessary. Once that's done, we can
500 * descend and make all our children. If any of them has an error
501 * but the -k flag was given, our 'make' field will be set FALSE again.
502 * This is our signal to not attempt to do anything but abort our
506 gn
->made
= BEINGMADE
;
507 if ((gn
->type
& OP_MADE
) == 0)
509 Lst_ForEach(gn
->children
, Compat_Make
, gn
);
510 if ((gn
->flags
& REMAKE
) == 0) {
512 pgn
->flags
&= ~REMAKE
;
516 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
518 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
523 * All the children were made ok. Now cmgn->mtime contains the
524 * modification time of the newest child, we need to find out if we
525 * exist and when we were modified last. The criteria for datedness
526 * are defined by the Make_OODate function.
529 fprintf(debug_file
, "Examining %s...", gn
->name
);
531 if (! Make_OODate(gn
)) {
534 fprintf(debug_file
, "up-to-date.\n");
537 } else if (DEBUG(MAKE
)) {
538 fprintf(debug_file
, "out-of-date.\n");
542 * If the user is just seeing if something is out-of-date, exit now
543 * to tell him/her "yes".
550 * We need to be re-made. We also have to make sure we've got a $?
551 * variable. To be nice, we also define the $> variable using
557 * Alter our type to tell if errors should be ignored or things
558 * should not be printed so CompatRunCommand knows what to do.
560 if (Targ_Ignore(gn
)) {
561 gn
->type
|= OP_IGNORE
;
563 if (Targ_Silent(gn
)) {
564 gn
->type
|= OP_SILENT
;
567 if (Job_CheckCommands(gn
, Fatal
)) {
569 * Our commands are ok, but we still have to worry about the -t
572 if (!touchFlag
|| (gn
->type
& OP_MAKE
)) {
575 if (useMeta
&& !NoExecute(gn
)) {
576 meta_job_start(NULL
, gn
);
579 Lst_ForEach(gn
->commands
, CompatRunCommand
, gn
);
582 Job_Touch(gn
, gn
->type
& OP_SILENT
);
588 if (useMeta
&& !NoExecute(gn
)) {
589 if (meta_job_finish(NULL
) != 0)
594 if (gn
->made
!= ERROR
) {
596 * If the node was made successfully, mark it so, update
597 * its modification time and timestamp all its parents. Note
598 * that for .ZEROTIME targets, the timestamping isn't done.
599 * This is to keep its state from affecting that of its parent.
602 pgn
->flags
|= Make_Recheck(gn
) == 0 ? FORCE
: 0;
603 if (!(gn
->type
& OP_EXEC
)) {
604 pgn
->flags
|= CHILDMADE
;
605 Make_TimeStamp(pgn
, gn
);
607 } else if (keepgoing
) {
608 pgn
->flags
&= ~REMAKE
;
610 PrintOnError(gn
, "\n\nStop.");
613 } else if (gn
->made
== ERROR
) {
615 * Already had an error when making this beastie. Tell the parent
618 pgn
->flags
&= ~REMAKE
;
620 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
622 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
627 Error("Graph cycles through %s", gn
->name
);
629 pgn
->flags
&= ~REMAKE
;
632 if ((gn
->type
& OP_EXEC
) == 0) {
633 pgn
->flags
|= CHILDMADE
;
634 Make_TimeStamp(pgn
, gn
);
638 if ((gn
->type
& OP_EXEC
) == 0) {
639 Make_TimeStamp(pgn
, gn
);
648 Lst_ForEach(gn
->cohorts
, Compat_Make
, pgnp
);
653 *-----------------------------------------------------------------------
655 * Initialize this mode and start making.
658 * targs List of target nodes to re-create
666 *-----------------------------------------------------------------------
669 Compat_Run(Lst targs
)
671 GNode
*gn
= NULL
;/* Current root target */
672 int errors
; /* Number of targets not remade due to errors */
677 if (bmake_signal(SIGINT
, SIG_IGN
) != SIG_IGN
) {
678 bmake_signal(SIGINT
, CompatInterrupt
);
680 if (bmake_signal(SIGTERM
, SIG_IGN
) != SIG_IGN
) {
681 bmake_signal(SIGTERM
, CompatInterrupt
);
683 if (bmake_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
) {
684 bmake_signal(SIGHUP
, CompatInterrupt
);
686 if (bmake_signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
) {
687 bmake_signal(SIGQUIT
, CompatInterrupt
);
690 ENDNode
= Targ_FindNode(".END", TARG_CREATE
);
691 ENDNode
->type
= OP_SPECIAL
;
693 * If the user has defined a .BEGIN target, execute the commands attached
697 gn
= Targ_FindNode(".BEGIN", TARG_NOCREATE
);
700 if (gn
->made
== ERROR
) {
701 PrintOnError(gn
, "\n\nStop.");
708 * Expand .USE nodes right now, because they can modify the structure
711 Make_ExpandUse(targs
);
714 * For each entry in the list of targets to create, call Compat_Make on
715 * it to create the thing. Compat_Make will leave the 'made' field of gn
716 * in one of several states:
717 * UPTODATE gn was already up-to-date
718 * MADE gn was recreated successfully
719 * ERROR An error occurred while gn was being created
720 * ABORTED gn was not remade because one of its inferiors
721 * could not be made due to errors.
724 while (!Lst_IsEmpty (targs
)) {
725 gn
= (GNode
*)Lst_DeQueue(targs
);
728 if (gn
->made
== UPTODATE
) {
729 printf("`%s' is up to date.\n", gn
->name
);
730 } else if (gn
->made
== ABORTED
) {
731 printf("`%s' not remade because of errors.\n", gn
->name
);
737 * If the user has defined a .END target, run its commands.
740 Compat_Make(ENDNode
, ENDNode
);
741 if (gn
->made
== ERROR
) {
742 PrintOnError(gn
, "\n\nStop.");