2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)compat.c 8.2 (Berkeley) 3/19/94
40 * $FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $
41 * $DragonFly: src/usr.bin/make/Attic/compat.c,v 1.9 2004/11/12 22:57:04 dillon Exp $
46 * The routines in this file implement the full-compatibility
47 * mode of PMake. Most of the special functionality of PMake
48 * is available in this mode. Things not supported:
50 * - friendly variable substitution.
53 * Compat_Run Initialize things for this module and recreate
54 * thems as need creatin'
58 #include <sys/types.h>
70 * The following array is used to make a fast determination of which
71 * characters are interpreted specially by the shell. If a command
72 * contains any of these characters, it is executed by the shell, not
76 static char meta[256];
78 static GNode *curTarg = NULL;
79 static GNode *ENDNode;
80 static void CompatInterrupt(int);
81 static int CompatRunCommand(void *, void *);
82 static int CompatMake(void *, void *);
83 static int shellneed(char *);
85 static char *sh_builtin[] = {
86 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
87 "unalias", "umask", "unset", "wait", ":", 0};
90 *-----------------------------------------------------------------------
92 * Interrupt the creation of the current target and remove it if
99 * The target is removed and the process exits. If .INTERRUPT exists,
100 * its commands are run first WITH INTERRUPTS IGNORED..
102 *-----------------------------------------------------------------------
105 CompatInterrupt (int signo)
109 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
111 char *file = Var_Value (TARGET, curTarg, &p1);
113 if (!noExecute && eunlink(file) != -1) {
114 printf ("*** %s removed\n", file);
119 * Run .INTERRUPT only if hit with interrupt signal
121 if (signo == SIGINT) {
122 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
124 Lst_ForEach(gn->commands, CompatRunCommand, (void *)gn);
129 if (signo == SIGQUIT)
131 (void) signal(signo, SIG_DFL);
132 (void) kill(getpid(), signo);
136 *-----------------------------------------------------------------------
140 * Returns 1 if a specified line must be executed by the shell,
141 * 0 if it can be run via execve, and -1 if the command is a no-op.
146 *-----------------------------------------------------------------------
149 shellneed (char *cmd)
154 av = brk_string(cmd, &ac, TRUE);
155 for(p = sh_builtin; *p != 0; p++)
156 if (strcmp(av[1], *p) == 0)
162 *-----------------------------------------------------------------------
163 * CompatRunCommand --
164 * Execute the next command for a target. If the command returns an
165 * error, the node's made field is set to ERROR and creation stops.
166 * The node from which the command came is also given.
169 * 0 if the command succeeded, 1 if an error occurred.
172 * The node's 'made' field may be set to ERROR.
174 *-----------------------------------------------------------------------
177 CompatRunCommand (void *cmdp, void *gnp)
179 char *cmdStart; /* Start of expanded command */
181 Boolean silent, /* Don't print command */
182 errCheck; /* Check errors */
183 int reason; /* Reason for child's death */
184 int status; /* Description of child's death */
185 int cpid; /* Child actually found */
186 ReturnStatus stat; /* Status of fork */
187 LstNode cmdNode; /* Node where current command is located */
188 char **av; /* Argument vector for thing to exec */
189 int argc; /* Number of arguments in av or 0 if not
190 * dynamically allocated */
191 Boolean local; /* TRUE if command should be executed
193 int internal; /* Various values.. */
194 char *cmd = (char *) cmdp;
195 GNode *gn = (GNode *) gnp;
198 * Avoid clobbered variable warnings by forcing the compiler
199 * to ``unregister'' variables
205 silent = gn->type & OP_SILENT;
206 errCheck = !(gn->type & OP_IGNORE);
208 cmdNode = Lst_Member (gn->commands, (void *)cmd);
209 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
212 * brk_string will return an argv with a NULL in av[0], thus causing
213 * execvp to choke and die horribly. Besides, how can we execute a null
214 * command? In any case, we warn the user that the command expanded to
215 * nothing (is this the right thing to do?).
218 if (*cmdStart == '\0') {
220 Error("%s expands to empty string", cmd);
225 Lst_Replace (cmdNode, (void *)cmdStart);
227 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
228 (void)Lst_AtEnd(ENDNode->commands, (void *)cmdStart);
230 } else if (strcmp(cmdStart, "...") == 0) {
231 gn->type |= OP_SAVE_CMDS;
235 while ((*cmd == '@') || (*cmd == '-')) {
237 silent = DEBUG(LOUD) ? FALSE : TRUE;
244 while (isspace((unsigned char)*cmd))
248 * Search for meta characters in the command. If there are no meta
249 * characters, there's no need to execute a shell to execute the
252 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
257 * Print the command before echoing if we're not supposed to be quiet for
258 * this one. We also print the command if -n given.
260 if (!silent || noExecute) {
261 printf ("%s\n", cmd);
266 * If we're not supposed to execute any commands, this is as far as
275 * If *cp isn't the null character, we hit a "meta" character and
276 * need to pass the command off to the shell. We give the shell the
277 * -e flag as well as -c if it's supposed to exit when it hits an
280 static char *shargv[4] = { "/bin/sh" };
282 shargv[1] = (errCheck ? "-ec" : "-c");
284 shargv[3] = (char *)NULL;
287 } else if ((internal = shellneed(cmd))) {
289 * This command must be passed by the shell for other reasons..
290 * or.. possibly not at all.
292 static char *shargv[4] = { "/bin/sh" };
294 if (internal == -1) {
295 /* Command does not need to be executed */
299 shargv[1] = (errCheck ? "-ec" : "-c");
301 shargv[3] = (char *)NULL;
306 * No meta-characters, so no need to exec a shell. Break the command
307 * into words to form an argument vector we can execute.
308 * brk_string sticks our name in av[0], so we have to
311 av = brk_string(cmd, &argc, TRUE);
318 * Fork and execute the single command. If the fork fails, we abort.
322 Fatal("Could not fork");
327 (void) write (2, av[0], strlen (av[0]));
328 (void) write (2, ":", 1);
329 (void) write (2, strerror(errno), strlen(strerror(errno)));
330 (void) write (2, "\n", 1);
332 (void)execv(av[0], av);
338 * we need to print out the command associated with this Gnode in
339 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
340 * in main(), Fatal() and DieHorribly(), therefore do not free it
343 if (!DEBUG(GRAPH2)) {
345 Lst_Replace (cmdNode, cmdp);
349 * The child is off and running. Now all we can do is wait...
353 while ((stat = wait(&reason)) != cpid) {
354 if (stat == -1 && errno != EINTR) {
360 if (WIFSTOPPED(reason)) {
361 status = WSTOPSIG(reason); /* stopped */
362 } else if (WIFEXITED(reason)) {
363 status = WEXITSTATUS(reason); /* exited */
365 printf ("*** Error code %d", status);
368 status = WTERMSIG(reason); /* signaled */
369 printf ("*** Signal %d", status);
373 if (!WIFEXITED(reason) || (status != 0)) {
378 * Abort the current target, but let others
381 printf (" (continuing)\n");
385 * Continue executing commands for this target.
386 * If we return 0, this will happen...
388 printf (" (ignored)\n");
394 Fatal ("error in wait: %d", stat);
403 *-----------------------------------------------------------------------
405 * Make a target, given the parent, to abort if necessary.
411 * If an error is detected and not being ignored, the process exits.
413 *-----------------------------------------------------------------------
416 CompatMake (void *gnp, void *pgnp)
418 GNode *gn = (GNode *) gnp;
419 GNode *pgn = (GNode *) pgnp;
420 if (gn->type & OP_USE) {
421 Make_HandleUse(gn, pgn);
422 } else if (gn->made == UNMADE) {
424 * First mark ourselves to be made, then apply whatever transformations
425 * the suffix module thinks are necessary. Once that's done, we can
426 * descend and make all our children. If any of them has an error
427 * but the -k flag was given, our 'make' field will be set FALSE again.
428 * This is our signal to not attempt to do anything but abort our
432 gn->made = BEINGMADE;
434 Lst_ForEach (gn->children, CompatMake, (void *)gn);
441 if (Lst_Member (gn->iParents, pgn) != NULL) {
443 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
448 * All the children were made ok. Now cmtime contains the modification
449 * time of the newest child, we need to find out if we exist and when
450 * we were modified last. The criteria for datedness are defined by the
451 * Make_OODate function.
453 DEBUGF(MAKE, ("Examining %s...", gn->name));
454 if (! Make_OODate(gn)) {
456 DEBUGF(MAKE, ("up-to-date.\n"));
459 DEBUGF(MAKE, ("out-of-date.\n"));
463 * If the user is just seeing if something is out-of-date, exit now
464 * to tell him/her "yes".
471 * We need to be re-made. We also have to make sure we've got a $?
472 * variable. To be nice, we also define the $> variable using
478 * Alter our type to tell if errors should be ignored or things
479 * should not be printed so CompatRunCommand knows what to do.
481 if (Targ_Ignore (gn)) {
482 gn->type |= OP_IGNORE;
484 if (Targ_Silent (gn)) {
485 gn->type |= OP_SILENT;
488 if (Job_CheckCommands (gn, Fatal)) {
490 * Our commands are ok, but we still have to worry about the -t
495 Lst_ForEach (gn->commands, CompatRunCommand, (void *)gn);
498 Job_Touch (gn, gn->type & OP_SILENT);
504 if (gn->made != ERROR) {
506 * If the node was made successfully, mark it so, update
507 * its modification time and timestamp all its parents. Note
508 * that for .ZEROTIME targets, the timestamping isn't done.
509 * This is to keep its state from affecting that of its parent.
514 * We can't re-stat the thing, but we can at least take care of
515 * rules where a target depends on a source that actually creates
516 * the target, but only if it has changed, e.g.
524 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
526 * In this case, if the definitions produced by yacc haven't
527 * changed from before, parse.h won't have been updated and
528 * gn->mtime will reflect the current modification time for
529 * parse.h. This is something of a kludge, I admit, but it's a
532 * XXX: People like to use a rule like
536 * To force things that depend on FRC to be made, so we have to
537 * check for gn->children being empty as well...
539 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
544 * This is what Make does and it's actually a good thing, as it
547 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
549 * to function as intended. Unfortunately, thanks to the stateless
550 * nature of NFS (and the speed of this program), there are times
551 * when the modification time of a file created on a remote
552 * machine will not be modified before the stat() implied by
553 * the Dir_MTime occurs, thus leading us to believe that the file
554 * is unchanged, wreaking havoc with files that depend on this one.
556 * I have decided it is better to make too much than to make too
557 * little, so this stuff is commented out unless you're sure it's
561 if (noExecute || Dir_MTime(gn) == 0) {
564 if (gn->cmtime > gn->mtime)
565 gn->mtime = gn->cmtime;
566 DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(gn->mtime)));
568 if (!(gn->type & OP_EXEC)) {
569 pgn->childMade = TRUE;
570 Make_TimeStamp(pgn, gn);
572 } else if (keepgoing) {
577 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
581 } else if (gn->made == ERROR) {
583 * Already had an error when making this beastie. Tell the parent
588 if (Lst_Member (gn->iParents, pgn) != NULL) {
590 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
595 Error("Graph cycles through %s\n", gn->name);
600 if ((gn->type & OP_EXEC) == 0) {
601 pgn->childMade = TRUE;
602 Make_TimeStamp(pgn, gn);
606 if ((gn->type & OP_EXEC) == 0) {
607 Make_TimeStamp(pgn, gn);
619 *-----------------------------------------------------------------------
621 * Start making again, given a list of target nodes.
629 *-----------------------------------------------------------------------
632 Compat_Run(Lst targs)
634 char *cp; /* Pointer to string of shell meta-characters */
635 GNode *gn = NULL;/* Current root target */
636 int errors; /* Number of targets not remade due to errors */
638 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
639 signal(SIGINT, CompatInterrupt);
641 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
642 signal(SIGTERM, CompatInterrupt);
644 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
645 signal(SIGHUP, CompatInterrupt);
647 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
648 signal(SIGQUIT, CompatInterrupt);
651 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
652 meta[(unsigned char) *cp] = 1;
655 * The null character serves as a sentinel in the string.
659 ENDNode = Targ_FindNode(".END", TARG_CREATE);
661 * If the user has defined a .BEGIN target, execute the commands attached
665 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
667 Lst_ForEach(gn->commands, CompatRunCommand, (void *)gn);
668 if (gn->made == ERROR) {
669 printf("\n\nStop.\n");
676 * For each entry in the list of targets to create, call CompatMake on
677 * it to create the thing. CompatMake will leave the 'made' field of gn
678 * in one of several states:
679 * UPTODATE gn was already up-to-date
680 * MADE gn was recreated successfully
681 * ERROR An error occurred while gn was being created
682 * ABORTED gn was not remade because one of its inferiors
683 * could not be made due to errors.
686 while (!Lst_IsEmpty (targs)) {
687 gn = (GNode *) Lst_DeQueue (targs);
690 if (gn->made == UPTODATE) {
691 printf ("`%s' is up to date.\n", gn->name);
692 } else if (gn->made == ABORTED) {
693 printf ("`%s' not remade because of errors.\n", gn->name);
699 * If the user has defined a .END target, run its commands.
702 Lst_ForEach(ENDNode->commands, CompatRunCommand, (void *)gn);