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.50 2005/02/10 14:39:05 harti Exp $
41 * $DragonFly: src/usr.bin/make/Attic/compat.c,v 1.36 2005/04/16 10:34:26 okumoto 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'
57 #include <sys/types.h>
81 * The following array is used to make a fast determination of which
82 * characters are interpreted specially by the shell. If a command
83 * contains any of these characters, it is executed by the shell, not
86 static char meta[256];
88 static GNode *curTarg = NULL;
89 static GNode *ENDNode;
90 static sig_atomic_t interrupted;
95 const char *cp; /* Pointer to string of shell meta-characters */
97 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
98 meta[(unsigned char)*cp] = 1;
101 * The null character serves as a sentinel in the string.
107 * Interrupt handler - set flag and defer handling to the main code
110 CompatCatchSig(int signo)
117 *-----------------------------------------------------------------------
119 * Interrupt the creation of the current target and remove it if
126 * The target is removed and the process exits. If .INTERRUPT exists,
127 * its commands are run first WITH INTERRUPTS IGNORED..
129 *-----------------------------------------------------------------------
132 CompatInterrupt(int signo)
135 sigset_t nmask, omask;
139 sigaddset(&nmask, SIGINT);
140 sigaddset(&nmask, SIGTERM);
141 sigaddset(&nmask, SIGHUP);
142 sigaddset(&nmask, SIGQUIT);
143 sigprocmask(SIG_SETMASK, &nmask, &omask);
145 /* prevent recursion in evaluation of .INTERRUPT */
148 if (curTarg != NULL && !Targ_Precious(curTarg)) {
150 char *file = Var_Value(TARGET, curTarg, &p1);
152 if (!noExecute && eunlink(file) != -1) {
153 printf("*** %s removed\n", file);
159 * Run .INTERRUPT only if hit with interrupt signal
161 if (signo == SIGINT) {
162 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
164 LST_FOREACH(ln, &gn->commands) {
165 if (Compat_RunCommand(Lst_Datum(ln), gn))
171 sigprocmask(SIG_SETMASK, &omask, NULL);
173 if (signo == SIGQUIT)
175 signal(signo, SIG_DFL);
176 kill(getpid(), signo);
180 *-----------------------------------------------------------------------
184 * Returns 1 if a specified line must be executed by the shell,
185 * and 0 if it can be run via execve.
188 * Uses brk_string so destroys the contents of argv.
190 *-----------------------------------------------------------------------
195 static const char *sh_builtin[] = {
196 "alias", "cd", "eval", "exec",
197 "exit", "read", "set", "ulimit",
198 "unalias", "umask", "unset", "wait",
204 av = brk_string(cmd, NULL, TRUE);
205 for (p = sh_builtin; *p != 0; p++)
206 if (strcmp(av[1], *p) == 0)
212 *-----------------------------------------------------------------------
213 * Compat_RunCommand --
214 * Execute the next command for a target. If the command returns an
215 * error, the node's made field is set to ERROR and creation stops.
216 * The node from which the command came is also given.
219 * 0 if the command succeeded, 1 if an error occurred.
222 * The node's 'made' field may be set to ERROR.
224 *-----------------------------------------------------------------------
227 Compat_RunCommand(char *cmd, GNode *gn)
229 char *cmdStart; /* Start of expanded command */
231 Boolean silent; /* Don't print command */
232 Boolean doit; /* Execute even in -n */
233 Boolean errCheck; /* Check errors */
234 int reason; /* Reason for child's death */
235 int status; /* Description of child's death */
236 int cpid; /* Child actually found */
237 ReturnStatus rstat; /* Status of fork */
238 LstNode *cmdNode; /* Node where current command is located */
239 char **av; /* Argument vector for thing to exec */
240 char *cmd_save; /* saved cmd */
243 * Avoid clobbered variable warnings by forcing the compiler
244 * to ``unregister'' variables
250 silent = gn->type & OP_SILENT;
251 errCheck = !(gn->type & OP_IGNORE);
254 cmdNode = Lst_Member(&gn->commands, cmd);
255 cmdStart = Buf_Peel(Var_Subst(cmd, gn, FALSE));
258 * brk_string will return an argv with a NULL in av[0], thus causing
259 * execvp to choke and die horribly. Besides, how can we execute a null
260 * command? In any case, we warn the user that the command expanded to
261 * nothing (is this the right thing to do?).
263 if (*cmdStart == '\0') {
265 Error("%s expands to empty string", cmd);
270 Lst_Replace(cmdNode, cmdStart);
272 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
273 Lst_AtEnd(&ENDNode->commands, cmdStart);
275 } else if (strcmp(cmdStart, "...") == 0) {
276 gn->type |= OP_SAVE_CMDS;
280 while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
284 silent = DEBUG(LOUD) ? FALSE : TRUE;
293 if (!meta[0]) /* we came here from jobs */
300 while (isspace((unsigned char)*cmd))
304 * Search for meta characters in the command. If there are no meta
305 * characters, there's no need to execute a shell to execute the
308 for (cp = cmd; !meta[(unsigned char)*cp]; cp++)
312 * Print the command before echoing if we're not supposed to be quiet
313 * for this one. We also print the command if -n given, but not if '+'.
315 if (!silent || (noExecute && !doit)) {
321 * If we're not supposed to execute any commands, this is as far as
324 if (!doit && noExecute) {
330 * If *cp isn't the null character, we hit a "meta" character
331 * and need to pass the command off to the shell. We give the
332 * shell the -e flag as well as -c if it's supposed to exit
333 * when it hits an error.
335 static char *shargv[4];
337 shargv[0] = shellPath;
338 shargv[1] = (errCheck ? "-ec" : "-c");
343 } else if (shellneed(cmd)) {
345 * This command must be passed by the shell for other reasons..
346 * or.. possibly not at all.
348 static char *shargv[4];
350 shargv[0] = shellPath;
351 shargv[1] = (errCheck ? "-ec" : "-c");
358 * No meta-characters, so no need to exec a shell. Break the
359 * command into words to form an argument vector we can execute.
360 * brk_string sticks our name in av[0], so we have to
363 av = brk_string(cmd, NULL, TRUE);
368 * Fork and execute the single command. If the fork fails, we abort.
372 Fatal("Could not fork");
376 write(STDERR_FILENO, av[0], strlen(av[0]));
377 write(STDERR_FILENO, ":", 1);
378 write(STDERR_FILENO, strerror(errno), strlen(strerror(errno)));
379 write(STDERR_FILENO, "\n", 1);
384 * we need to print out the command associated with this Gnode in
385 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
386 * in main(), Fatal() and DieHorribly(), therefore do not free it
389 if (!DEBUG(GRAPH2)) {
391 Lst_Replace(cmdNode, cmd_save);
395 * The child is off and running. Now all we can do is wait...
398 while ((rstat = wait(&reason)) != cpid) {
399 if (interrupted || (rstat == -1 && errno != EINTR)) {
404 CompatInterrupt(interrupted);
407 if (WIFSTOPPED(reason)) {
408 status = WSTOPSIG(reason); /* stopped */
409 } else if (WIFEXITED(reason)) {
410 status = WEXITSTATUS(reason); /* exited */
412 printf("*** Error code %d", status);
415 status = WTERMSIG(reason); /* signaled */
416 printf("*** Signal %d", status);
419 if (!WIFEXITED(reason) || status != 0) {
424 * Abort the current target,
425 * but let others continue.
427 printf(" (continuing)\n");
431 * Continue executing commands for this
432 * target. If we return 0, this will
435 printf(" (ignored)\n");
441 Fatal("error in wait: %d", rstat);
450 *-----------------------------------------------------------------------
452 * Make a target, given the parent, to abort if necessary.
455 * If an error is detected and not being ignored, the process exits.
457 *-----------------------------------------------------------------------
460 CompatMake(GNode *gn, GNode *pgn)
464 if (gn->type & OP_USE) {
465 Make_HandleUse(gn, pgn);
467 } else if (gn->made == UNMADE) {
469 * First mark ourselves to be made, then apply whatever
470 * transformations the suffix module thinks are necessary.
471 * Once that's done, we can descend and make all our children.
472 * If any of them has an error but the -k flag was given, our
473 * 'make' field will be set FALSE again. This is our signal to
474 * not attempt to do anything but abort our parent as well.
477 gn->made = BEINGMADE;
479 LST_FOREACH(ln, &gn->children)
480 CompatMake(Lst_Datum(ln), gn);
487 if (Lst_Member(&gn->iParents, pgn) != NULL) {
489 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
494 * All the children were made ok. Now cmtime contains the
495 * modification time of the newest child, we need to find out
496 * if we exist and when we were modified last. The criteria for
497 * datedness are defined by the Make_OODate function.
499 DEBUGF(MAKE, ("Examining %s...", gn->name));
500 if (!Make_OODate(gn)) {
502 DEBUGF(MAKE, ("up-to-date.\n"));
505 DEBUGF(MAKE, ("out-of-date.\n"));
509 * If the user is just seeing if something is out-of-date,
510 * exit now to tell him/her "yes".
517 * We need to be re-made. We also have to make sure we've got
518 * a $? variable. To be nice, we also define the $> variable
519 * using Make_DoAllVar().
524 * Alter our type to tell if errors should be ignored or things
525 * should not be printed so Compat_RunCommand knows what to do.
527 if (Targ_Ignore(gn)) {
528 gn->type |= OP_IGNORE;
530 if (Targ_Silent(gn)) {
531 gn->type |= OP_SILENT;
534 if (Job_CheckCommands(gn, Fatal)) {
536 * Our commands are ok, but we still have to worry
537 * about the -t flag...
541 LST_FOREACH(ln, &gn->commands) {
542 if (Compat_RunCommand(Lst_Datum(ln),
548 Job_Touch(gn, gn->type & OP_SILENT);
554 if (gn->made != ERROR) {
556 * If the node was made successfully, mark it so, update
557 * its modification time and timestamp all its parents.
558 * Note that for .ZEROTIME targets, the timestamping
559 * isn't done. This is to keep its state from affecting
560 * that of its parent.
565 * We can't re-stat the thing, but we can at least take
566 * care of rules where a target depends on a source that
567 * actually creates the target, but only if it has
576 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
578 * In this case, if the definitions produced by yacc
579 * haven't changed from before, parse.h won't have been
580 * updated and gn->mtime will reflect the current
581 * modification time for parse.h. This is something of a
582 * kludge, I admit, but it's a useful one..
584 * XXX: People like to use a rule like
588 * To force things that depend on FRC to be made, so we
589 * have to check for gn->children being empty as well...
591 if (!Lst_IsEmpty(&gn->commands) ||
592 Lst_IsEmpty(&gn->children)) {
597 * This is what Make does and it's actually a good
598 * thing, as it allows rules like
600 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
602 * to function as intended. Unfortunately, thanks to
603 * the stateless nature of NFS (and the speed of this
604 * program), there are times when the modification time
605 * of a file created on a remote machine will not be
606 * modified before the stat() implied by the Dir_MTime
607 * occurs, thus leading us to believe that the file
608 * is unchanged, wreaking havoc with files that depend
611 * I have decided it is better to make too much than to
612 * make too little, so this stuff is commented out
613 * unless you're sure it's ok.
616 if (noExecute || Dir_MTime(gn) == 0) {
619 if (gn->cmtime > gn->mtime)
620 gn->mtime = gn->cmtime;
621 DEBUGF(MAKE, ("update time: %s\n",
622 Targ_FmtTime(gn->mtime)));
624 if (!(gn->type & OP_EXEC)) {
625 pgn->childMade = TRUE;
626 Make_TimeStamp(pgn, gn);
629 } else if (keepgoing) {
635 printf("\n\nStop in %s.\n",
636 Var_Value(".CURDIR", gn, &p1));
640 } else if (gn->made == ERROR) {
642 * Already had an error when making this beastie. Tell the
647 if (Lst_Member(&gn->iParents, pgn) != NULL) {
649 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
654 Error("Graph cycles through %s\n", gn->name);
659 if ((gn->type & OP_EXEC) == 0) {
660 pgn->childMade = TRUE;
661 Make_TimeStamp(pgn, gn);
665 if ((gn->type & OP_EXEC) == 0) {
666 Make_TimeStamp(pgn, gn);
678 *-----------------------------------------------------------------------
680 * Start making again, given a list of target nodes.
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 Shell_Init(); /* Set up shell. */
700 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
701 signal(SIGINT, CompatCatchSig);
703 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
704 signal(SIGTERM, CompatCatchSig);
706 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
707 signal(SIGHUP, CompatCatchSig);
709 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
710 signal(SIGQUIT, CompatCatchSig);
713 ENDNode = Targ_FindNode(".END", TARG_CREATE);
715 * If the user has defined a .BEGIN target, execute the commands
719 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
721 LST_FOREACH(ln, &gn->commands) {
722 if (Compat_RunCommand(Lst_Datum(ln), gn))
725 if (gn->made == ERROR) {
726 printf("\n\nStop.\n");
733 * For each entry in the list of targets to create, call CompatMake on
734 * it to create the thing. CompatMake will leave the 'made' field of gn
735 * in one of several states:
736 * UPTODATE gn was already up-to-date
737 * MADE gn was recreated successfully
738 * ERROR An error occurred while gn was being created
739 * ABORTED gn was not remade because one of its inferiors
740 * could not be made due to errors.
743 while (!Lst_IsEmpty(targs)) {
744 gn = Lst_DeQueue(targs);
747 if (gn->made == UPTODATE) {
748 printf("`%s' is up to date.\n", gn->name);
749 } else if (gn->made == ABORTED) {
750 printf("`%s' not remade because of errors.\n",
757 * If the user has defined a .END target, run its commands.
760 LST_FOREACH(ln, &ENDNode->commands) {
761 if (Compat_RunCommand(Lst_Datum(ln), gn))