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.13 2004/11/30 19:12:57 joerg 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>
71 * The following array is used to make a fast determination of which
72 * characters are interpreted specially by the shell. If a command
73 * contains any of these characters, it is executed by the shell, not
77 static char meta[256];
79 static GNode *curTarg = NULL;
80 static GNode *ENDNode;
81 static void CompatInterrupt(int);
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};
92 char *cp; /* Pointer to string of shell meta-characters */
94 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
95 meta[(unsigned char) *cp] = 1;
98 * The null character serves as a sentinel in the string.
104 *-----------------------------------------------------------------------
106 * Interrupt the creation of the current target and remove it if
113 * The target is removed and the process exits. If .INTERRUPT exists,
114 * its commands are run first WITH INTERRUPTS IGNORED..
116 *-----------------------------------------------------------------------
119 CompatInterrupt (int signo)
123 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
125 char *file = Var_Value (TARGET, curTarg, &p1);
127 if (!noExecute && eunlink(file) != -1) {
128 printf ("*** %s removed\n", file);
133 * Run .INTERRUPT only if hit with interrupt signal
135 if (signo == SIGINT) {
136 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
138 Lst_ForEach(gn->commands, Compat_RunCommand, (void *)gn);
143 if (signo == SIGQUIT)
145 (void) signal(signo, SIG_DFL);
146 (void) kill(getpid(), signo);
150 *-----------------------------------------------------------------------
154 * Returns 1 if a specified line must be executed by the shell,
155 * 0 if it can be run via execve, and -1 if the command is a no-op.
160 *-----------------------------------------------------------------------
163 shellneed (char *cmd)
168 av = brk_string(cmd, &ac, TRUE);
169 for(p = sh_builtin; *p != 0; p++)
170 if (strcmp(av[1], *p) == 0)
176 *-----------------------------------------------------------------------
177 * Compat_RunCommand --
178 * Execute the next command for a target. If the command returns an
179 * error, the node's made field is set to ERROR and creation stops.
180 * The node from which the command came is also given.
183 * 0 if the command succeeded, 1 if an error occurred.
186 * The node's 'made' field may be set to ERROR.
188 *-----------------------------------------------------------------------
191 Compat_RunCommand (void *cmdp, void *gnp)
193 char *cmdStart; /* Start of expanded command */
195 Boolean silent, /* Don't print command */
196 doit, /* Execute even in -n */
197 errCheck; /* Check errors */
198 int reason; /* Reason for child's death */
199 int status; /* Description of child's death */
200 int cpid; /* Child actually found */
201 ReturnStatus rstat; /* Status of fork */
202 LstNode cmdNode; /* Node where current command is located */
203 char **av; /* Argument vector for thing to exec */
204 int argc; /* Number of arguments in av or 0 if not
205 * dynamically allocated */
206 int internal; /* Various values.. */
207 char *cmd = (char *) cmdp;
208 GNode *gn = (GNode *) gnp;
211 * Avoid clobbered variable warnings by forcing the compiler
212 * to ``unregister'' variables
218 silent = gn->type & OP_SILENT;
219 errCheck = !(gn->type & OP_IGNORE);
222 cmdNode = Lst_Member (gn->commands, (void *)cmd);
223 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
226 * brk_string will return an argv with a NULL in av[0], thus causing
227 * execvp to choke and die horribly. Besides, how can we execute a null
228 * command? In any case, we warn the user that the command expanded to
229 * nothing (is this the right thing to do?).
232 if (*cmdStart == '\0') {
234 Error("%s expands to empty string", cmd);
239 Lst_Replace (cmdNode, (void *)cmdStart);
241 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
242 (void)Lst_AtEnd(ENDNode->commands, (void *)cmdStart);
244 } else if (strcmp(cmdStart, "...") == 0) {
245 gn->type |= OP_SAVE_CMDS;
249 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
253 silent = DEBUG(LOUD) ? FALSE : TRUE;
262 if (!meta[0]) /* we came here from jobs */
269 while (isspace((unsigned char)*cmd))
273 * Search for meta characters in the command. If there are no meta
274 * characters, there's no need to execute a shell to execute the
277 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
282 * Print the command before echoing if we're not supposed to be quiet for
283 * this one. We also print the command if -n given, but not if '+'.
285 if (!silent || (noExecute && !doit)) {
286 printf ("%s\n", cmd);
291 * If we're not supposed to execute any commands, this is as far as
294 if (!doit && noExecute) {
300 * If *cp isn't the null character, we hit a "meta" character and
301 * need to pass the command off to the shell. We give the shell the
302 * -e flag as well as -c if it's supposed to exit when it hits an
305 static char *shargv[4] = { "/bin/sh" };
307 shargv[1] = (errCheck ? "-ec" : "-c");
309 shargv[3] = (char *)NULL;
312 } else if ((internal = shellneed(cmd))) {
314 * This command must be passed by the shell for other reasons..
315 * or.. possibly not at all.
317 static char *shargv[4] = { "/bin/sh" };
319 if (internal == -1) {
320 /* Command does not need to be executed */
324 shargv[1] = (errCheck ? "-ec" : "-c");
326 shargv[3] = (char *)NULL;
331 * No meta-characters, so no need to exec a shell. Break the command
332 * into words to form an argument vector we can execute.
333 * brk_string sticks our name in av[0], so we have to
336 av = brk_string(cmd, &argc, TRUE);
341 * Fork and execute the single command. If the fork fails, we abort.
345 Fatal("Could not fork");
349 (void) write (STDERR_FILENO, av[0], strlen (av[0]));
350 (void) write (STDERR_FILENO, ":", 1);
351 (void) write (STDERR_FILENO, strerror(errno), strlen(strerror(errno)));
352 (void) write (STDERR_FILENO, "\n", 1);
357 * we need to print out the command associated with this Gnode in
358 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
359 * in main(), Fatal() and DieHorribly(), therefore do not free it
362 if (!DEBUG(GRAPH2)) {
364 Lst_Replace (cmdNode, cmdp);
368 * The child is off and running. Now all we can do is wait...
372 while ((rstat = wait(&reason)) != cpid) {
373 if (rstat == -1 && errno != EINTR) {
379 if (WIFSTOPPED(reason)) {
380 status = WSTOPSIG(reason); /* stopped */
381 } else if (WIFEXITED(reason)) {
382 status = WEXITSTATUS(reason); /* exited */
384 printf ("*** Error code %d", status);
387 status = WTERMSIG(reason); /* signaled */
388 printf ("*** Signal %d", status);
392 if (!WIFEXITED(reason) || (status != 0)) {
397 * Abort the current target, but let others
400 printf (" (continuing)\n");
404 * Continue executing commands for this target.
405 * If we return 0, this will happen...
407 printf (" (ignored)\n");
413 Fatal ("error in wait: %d", rstat);
422 *-----------------------------------------------------------------------
424 * Make a target, given the parent, to abort if necessary.
430 * If an error is detected and not being ignored, the process exits.
432 *-----------------------------------------------------------------------
435 CompatMake (void *gnp, void *pgnp)
437 GNode *gn = (GNode *) gnp;
438 GNode *pgn = (GNode *) pgnp;
439 if (gn->type & OP_USE) {
440 Make_HandleUse(gn, pgn);
441 } else if (gn->made == UNMADE) {
443 * First mark ourselves to be made, then apply whatever transformations
444 * the suffix module thinks are necessary. Once that's done, we can
445 * descend and make all our children. If any of them has an error
446 * but the -k flag was given, our 'make' field will be set FALSE again.
447 * This is our signal to not attempt to do anything but abort our
451 gn->made = BEINGMADE;
453 Lst_ForEach (gn->children, CompatMake, (void *)gn);
460 if (Lst_Member (gn->iParents, pgn) != NULL) {
462 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
467 * All the children were made ok. Now cmtime contains the modification
468 * time of the newest child, we need to find out if we exist and when
469 * we were modified last. The criteria for datedness are defined by the
470 * Make_OODate function.
472 DEBUGF(MAKE, ("Examining %s...", gn->name));
473 if (! Make_OODate(gn)) {
475 DEBUGF(MAKE, ("up-to-date.\n"));
478 DEBUGF(MAKE, ("out-of-date.\n"));
482 * If the user is just seeing if something is out-of-date, exit now
483 * to tell him/her "yes".
490 * We need to be re-made. We also have to make sure we've got a $?
491 * variable. To be nice, we also define the $> variable using
497 * Alter our type to tell if errors should be ignored or things
498 * should not be printed so Compat_RunCommand knows what to do.
500 if (Targ_Ignore (gn)) {
501 gn->type |= OP_IGNORE;
503 if (Targ_Silent (gn)) {
504 gn->type |= OP_SILENT;
507 if (Job_CheckCommands (gn, Fatal)) {
509 * Our commands are ok, but we still have to worry about the -t
514 Lst_ForEach (gn->commands, Compat_RunCommand, (void *)gn);
517 Job_Touch (gn, gn->type & OP_SILENT);
523 if (gn->made != ERROR) {
525 * If the node was made successfully, mark it so, update
526 * its modification time and timestamp all its parents. Note
527 * that for .ZEROTIME targets, the timestamping isn't done.
528 * This is to keep its state from affecting that of its parent.
533 * We can't re-stat the thing, but we can at least take care of
534 * rules where a target depends on a source that actually creates
535 * the target, but only if it has changed, e.g.
543 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
545 * In this case, if the definitions produced by yacc haven't
546 * changed from before, parse.h won't have been updated and
547 * gn->mtime will reflect the current modification time for
548 * parse.h. This is something of a kludge, I admit, but it's a
551 * XXX: People like to use a rule like
555 * To force things that depend on FRC to be made, so we have to
556 * check for gn->children being empty as well...
558 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
563 * This is what Make does and it's actually a good thing, as it
566 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
568 * to function as intended. Unfortunately, thanks to the stateless
569 * nature of NFS (and the speed of this program), there are times
570 * when the modification time of a file created on a remote
571 * machine will not be modified before the stat() implied by
572 * the Dir_MTime occurs, thus leading us to believe that the file
573 * is unchanged, wreaking havoc with files that depend on this one.
575 * I have decided it is better to make too much than to make too
576 * little, so this stuff is commented out unless you're sure it's
580 if (noExecute || Dir_MTime(gn) == 0) {
583 if (gn->cmtime > gn->mtime)
584 gn->mtime = gn->cmtime;
585 DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(gn->mtime)));
587 if (!(gn->type & OP_EXEC)) {
588 pgn->childMade = TRUE;
589 Make_TimeStamp(pgn, gn);
591 } else if (keepgoing) {
596 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
600 } else if (gn->made == ERROR) {
602 * Already had an error when making this beastie. Tell the parent
607 if (Lst_Member (gn->iParents, pgn) != NULL) {
609 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
614 Error("Graph cycles through %s\n", gn->name);
619 if ((gn->type & OP_EXEC) == 0) {
620 pgn->childMade = TRUE;
621 Make_TimeStamp(pgn, gn);
625 if ((gn->type & OP_EXEC) == 0) {
626 Make_TimeStamp(pgn, gn);
638 *-----------------------------------------------------------------------
640 * Start making again, given a list of target nodes.
648 *-----------------------------------------------------------------------
651 Compat_Run(Lst targs)
653 GNode *gn = NULL;/* Current root target */
654 int errors; /* Number of targets not remade due to errors */
658 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
659 signal(SIGINT, CompatInterrupt);
661 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
662 signal(SIGTERM, CompatInterrupt);
664 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
665 signal(SIGHUP, CompatInterrupt);
667 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
668 signal(SIGQUIT, CompatInterrupt);
671 ENDNode = Targ_FindNode(".END", TARG_CREATE);
673 * If the user has defined a .BEGIN target, execute the commands attached
677 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
679 Lst_ForEach(gn->commands, Compat_RunCommand, (void *)gn);
680 if (gn->made == ERROR) {
681 printf("\n\nStop.\n");
688 * For each entry in the list of targets to create, call CompatMake on
689 * it to create the thing. CompatMake will leave the 'made' field of gn
690 * in one of several states:
691 * UPTODATE gn was already up-to-date
692 * MADE gn was recreated successfully
693 * ERROR An error occurred while gn was being created
694 * ABORTED gn was not remade because one of its inferiors
695 * could not be made due to errors.
698 while (!Lst_IsEmpty (targs)) {
699 gn = (GNode *) Lst_DeQueue (targs);
702 if (gn->made == UPTODATE) {
703 printf ("`%s' is up to date.\n", gn->name);
704 } else if (gn->made == ABORTED) {
705 printf ("`%s' not remade because of errors.\n", gn->name);
711 * If the user has defined a .END target, run its commands.
714 Lst_ForEach(ENDNode->commands, Compat_RunCommand, (void *)gn);