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 * @(#)job.c 8.2 (Berkeley) 3/19/94
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 * handle the creation etc. of our child processes.
50 * Job_Make Start the creation of the given target.
53 * Check for and handle the termination of any children.
54 * This must be called reasonably frequently to keep the
55 * whole make going at a decent clip, since job table
56 * entries aren't removed until their process is caught
57 * this way. Its single argument is TRUE if the function
58 * should block waiting for a child to terminate.
60 * Job_CatchOutput Print any output our children have produced. Should
61 * also be called fairly frequently to keep the user
62 * informed of what's going on. If no output is waiting,
63 * it will block for a time given by the SEL_* constants,
64 * below, or until output is ready.
66 * Job_Init Called to intialize this module. in addition, any
67 * commands attached to the .BEGIN target are executed
68 * before this function returns. Hence, the makefile must
69 * have been parsed before this function is called.
71 * Job_Full Return TRUE if the job table is filled.
73 * Job_Empty Return TRUE if the job table is completely empty.
75 * Job_Finish Perform any final processing which needs doing. This
76 * includes the execution of any commands which have
77 * been/were attached to the .END target. It should only
78 * be called when the job table is empty.
80 * Job_AbortAll Abort all currently running jobs. It doesn't handle
81 * output or do anything for the jobs, just kills them.
82 * It should only be called in an emergency, as it were.
85 * Verify that the commands for a target are ok. Provide
86 * them if necessary and possible.
88 * Job_Touch Update a target without really updating it.
90 * Job_Wait Wait for all currently-running jobs to finish.
93 * The routines in this file implement the full-compatibility
94 * mode of PMake. Most of the special functionality of PMake
95 * is available in this mode. Things not supported:
97 * - friendly variable substitution.
100 * Compat_Run Initialize things for this module and recreate
101 * thems as need creatin'
104 #include <sys/queue.h>
105 #include <sys/types.h>
106 #include <sys/select.h>
107 #include <sys/stat.h>
109 #include <sys/event.h>
111 #include <sys/wait.h>
116 #include <inttypes.h>
142 #define TMPPAT "makeXXXXXXXXXX"
146 * The SEL_ constants determine the maximum amount of time spent in select
147 * before coming out to see if a child has finished. SEL_SEC is the number of
148 * seconds and SEL_USEC is the number of micro-seconds
152 #endif /* !USE_KQUEUE */
155 * Job Table definitions.
157 * The job "table" is kept as a linked Lst in 'jobs', with the number of
158 * active jobs maintained in the 'nJobs' variable. At no time will this
159 * exceed the value of 'maxJobs', initialized by the Job_Init function.
161 * When a job is finished, the Make_Update function is called on each of the
162 * parents of the node which was just remade. This takes care of the upward
163 * traversal of the dependency graph.
165 #define JOB_BUFSIZE 1024
167 pid_t pid; /* The child's process ID */
169 struct GNode *node; /* The target the child is making */
172 * A LstNode for the first command to be saved after the job completes.
173 * This is NULL if there was no "..." in the job's commands.
178 * An FILE* for writing out the commands. This is only
179 * used before the job is actually started.
184 * A word of flags which determine how the module handles errors,
185 * echoing, etc. for the job
187 short flags; /* Flags to control treatment of job */
188 #define JOB_IGNERR 0x001 /* Ignore non-zero exits */
189 #define JOB_SILENT 0x002 /* no output */
190 #define JOB_SPECIAL 0x004 /* Target is a special one. i.e. run it locally
191 * if we can't export it and maxLocal is 0 */
192 #define JOB_IGNDOTS 0x008 /* Ignore "..." lines when processing
194 #define JOB_FIRST 0x020 /* Job is first job for the node */
195 #define JOB_RESTART 0x080 /* Job needs to be completely restarted */
196 #define JOB_RESUME 0x100 /* Job needs to be resumed b/c it stopped,
198 #define JOB_CONTINUING 0x200 /* We are in the process of resuming this job.
199 * Used to avoid infinite recursion between
200 * JobFinish and JobRestart */
202 /* union for handling shell's output */
205 * This part is used when usePipes is true.
206 * The output is being caught via a pipe and the descriptors
207 * of our pipe, an array in which output is line buffered and
208 * the current position in that buffer are all maintained for
213 * Input side of pipe associated with
214 * job's output channel
219 * Output side of pipe associated with job's
225 * Buffer for storing the output of the
228 char op_outBuf[JOB_BUFSIZE + 1];
230 /* Current position in op_outBuf */
235 * If usePipes is false the output is routed to a temporary
236 * file and all that is kept is the name of the file and the
237 * descriptor open to the file.
240 /* Name of file to which shell output was rerouted */
241 char of_outFile[PATH_MAX];
244 * Stream open to the output file. Used to funnel all
245 * from a single job to one file while still allowing
246 * multiple shell invocations
251 } output; /* Data for tracking a shell's output */
253 TAILQ_ENTRY(Job) link; /* list link */
256 #define outPipe output.o_pipe.op_outPipe
257 #define inPipe output.o_pipe.op_inPipe
258 #define outBuf output.o_pipe.op_outBuf
259 #define curPos output.o_pipe.op_curPos
260 #define outFile output.o_file.of_outFile
261 #define outFd output.o_file.of_outFd
263 TAILQ_HEAD(JobList, Job);
266 * error handling variables
268 static int aborting = 0; /* why is the make aborting? */
269 #define ABORT_ERROR 1 /* Because of an error */
270 #define ABORT_INTERRUPT 2 /* Because it was interrupted */
271 #define ABORT_WAIT 3 /* Waiting for jobs to finish */
274 * XXX: Avoid SunOS bug... FILENO() is fp->_file, and file
275 * is a char! So when we go above 127 we turn negative!
277 #define FILENO(a) ((unsigned)fileno(a))
280 * post-make command processing. The node postCommands is really just the
281 * .END target but we keep it around to avoid having to search for it
284 static GNode *postCommands;
287 * The number of commands actually printed for a target. Should this
288 * number be 0, no shell will be executed.
290 static int numCommands;
293 * Return values from JobStart.
295 #define JOB_RUNNING 0 /* Job is running */
296 #define JOB_ERROR 1 /* Error in starting the job */
297 #define JOB_FINISHED 2 /* The job is already finished */
298 #define JOB_STOPPED 3 /* The job is stopped */
301 * The maximum number of jobs that may run. This is initialize from the
302 * -j argument for the leading make and from the FIFO for sub-makes.
306 static int nJobs; /* The number of children currently running */
308 /* The structures that describe them */
309 static struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs);
311 static Boolean jobFull; /* Flag to tell when the job table is full. It
312 * is set TRUE when (1) the total number of
313 * running jobs equals the maximum allowed */
315 static int kqfd; /* File descriptor obtained by kqueue() */
317 static fd_set outputs; /* Set of descriptors of pipes connected to
318 * the output channels of children */
321 static GNode *lastNode; /* The node for which output was most recently
323 static const char *targFmt; /* Format string to use to head output from a
324 * job when it's not the most-recent job heard
326 static char *targPrefix = NULL; /* What we print at the start of targFmt */
328 #define TARG_FMT "%s %s ---\n" /* Default format */
329 #define MESSAGE(fp, gn) \
330 fprintf(fp, targFmt, targPrefix, gn->name);
333 * When JobStart attempts to run a job but isn't allowed to
334 * or when Job_CatchChildren detects a job that has
335 * been stopped somehow, the job is placed on the stoppedJobs queue to be run
336 * when the next job finishes.
338 * Lst of Job structures describing jobs that were stopped due to
339 * concurrency limits or externally
341 static struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs);
343 static int fifoFd; /* Fd of our job fifo */
344 static char fifoName[] = "/tmp/make_fifo_XXXXXXXXX";
345 static int fifoMaster;
347 static volatile sig_atomic_t interrupted;
350 #if defined(USE_PGRP) && defined(SYSV)
351 # define KILL(pid, sig) killpg(-(pid), (sig))
353 # if defined(USE_PGRP)
354 # define KILL(pid, sig) killpg((pid), (sig))
356 # define KILL(pid, sig) kill((pid), (sig))
361 * Grmpf... There is no way to set bits of the wait structure
362 * anymore with the stupid W*() macros. I liked the union wait
363 * stuff much more. So, we devise our own macros... This is
364 * really ugly, use dramamine sparingly. You have been warned.
366 #define W_SETMASKED(st, val, fun) \
369 int mask = fun(sh); \
371 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \
373 *(st) = (*(st) & ~mask) | ((val) << sh); \
376 #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
377 #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
379 static void JobRestart(Job *);
380 static int JobStart(GNode *, int, Job *);
381 static void JobDoOutput(Job *, Boolean);
382 static void JobInterrupt(int, int);
383 static void JobRestartJobs(void);
384 static int Compat_RunCommand(LstNode *, struct GNode *);
386 static GNode *curTarg = NULL;
387 static GNode *ENDNode;
390 * Create a fifo file with a uniq filename, and returns a file
391 * descriptor to that fifo.
394 mkfifotemp(char *template)
399 const unsigned char padchar[] =
400 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
402 if (template[0] == '\0') {
403 errno = EINVAL; /* bad input string */
407 /* Find end of template string. */
408 pathend = strchr(template, '\0');
412 * Starting from the end of the template replace spaces with 'X' in
413 * them with random characters until there are no more 'X'.
415 while (ptr >= template && *ptr == 'X') {
417 #if __FreeBSD_version < 800041
418 arc4random() % (sizeof(padchar) - 1);
420 arc4random_uniform(sizeof(padchar) - 1);
422 *ptr-- = padchar[rand_num];
426 /* Check the target directory. */
427 for (; ptr > template; --ptr) {
432 if (stat(template, &sbuf) != 0)
435 if (!S_ISDIR(sbuf.st_mode)) {
445 if (mkfifo(template, 0600) == 0) {
448 if ((fd = open(template, O_RDWR, 0600)) < 0) {
455 if (errno != EEXIST) {
461 * If we have a collision, cycle through the space of
464 for (ptr = start;;) {
467 if (*ptr == '\0' || ptr == pathend)
470 pad = strchr(padchar, *ptr);
471 if (pad == NULL || *++pad == '\0') {
483 catch_child(int sig __unused)
493 * Catch SIGCHLD so that we get kicked out of select() when we
494 * need to look at a child. This is only known to matter for the
495 * -j case (perhaps without -P).
497 * XXX this is intentionally misplaced.
501 sigemptyset(&sa.sa_mask);
502 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
503 sa.sa_handler = catch_child;
504 sigaction(SIGCHLD, &sa, NULL);
508 * Wait for child process to terminate.
511 ProcWait(ProcStuff *ps)
517 * Wait for the process to exit.
521 if (pid == -1 && errno != EINTR) {
522 Fatal("error in wait: %d", pid);
525 if (pid == ps->child_pid) {
538 * Got a signal. Set global variables and hope that someone will
542 JobCatchSig(int signo)
550 * Pass a signal on to all local jobs if
551 * USE_PGRP is defined, then die ourselves.
554 * We die by the same signal.
557 JobPassSig(int signo)
560 sigset_t nmask, omask;
561 struct sigaction act;
564 sigaddset(&nmask, signo);
565 sigprocmask(SIG_SETMASK, &nmask, &omask);
567 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo));
568 TAILQ_FOREACH(job, &jobs, link) {
569 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n",
570 signo, (intmax_t)job->pid));
571 KILL(job->pid, signo);
575 * Deal with proper cleanup based on the signal received. We only run
576 * the .INTERRUPT target if the signal was in fact an interrupt.
577 * The other three termination signals are more of a "get out *now*"
580 if (signo == SIGINT) {
581 JobInterrupt(TRUE, signo);
582 } else if (signo == SIGHUP || signo == SIGTERM || signo == SIGQUIT) {
583 JobInterrupt(FALSE, signo);
587 * Leave gracefully if SIGQUIT, rather than core dumping.
589 if (signo == SIGQUIT) {
594 * Send ourselves the signal now we've given the message to everyone
595 * else. Note we block everything else possible while we're getting
596 * the signal. This ensures that all our jobs get continued when we
597 * wake up before we take any other signal.
598 * XXX this comment seems wrong.
600 act.sa_handler = SIG_DFL;
601 sigemptyset(&act.sa_mask);
603 sigaction(signo, &act, NULL);
605 DEBUGF(JOB, ("JobPassSig passing signal to self, mask = %x.\n",
606 ~0 & ~(1 << (signo - 1))));
607 signal(signo, SIG_DFL);
609 KILL(getpid(), signo);
612 TAILQ_FOREACH(job, &jobs, link) {
613 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n",
614 signo, (intmax_t)job->pid));
615 KILL(job->pid, signo);
618 sigprocmask(SIG_SETMASK, &omask, NULL);
619 sigprocmask(SIG_SETMASK, &omask, NULL);
620 act.sa_handler = JobPassSig;
621 sigaction(signo, &act, NULL);
626 * Put out another command for the given job. If the command starts
627 * with an @ or a - we process it specially. In the former case,
628 * so long as the -s and -n flags weren't given to make, we stick
629 * a shell-specific echoOff command in the script. In the latter,
630 * we ignore errors for the entire job, unless the shell has error
632 * If the command is just "..." we take all future commands for this
633 * job to be commands to be executed once the entire graph has been
634 * made and return non-zero to signal that the end of the commands
635 * was reached. These commands are later attached to the postCommands
636 * node and executed by Job_Finish when all things are done.
637 * This function is called from JobStart via LST_FOREACH.
640 * Always 0, unless the command was "..."
643 * If the command begins with a '-' and the shell has no error control,
644 * the JOB_IGNERR flag is set in the job descriptor.
645 * If the command is "..." and we're not ignoring such things,
646 * tailCmds is set to the successor node of the cmd.
647 * numCommands is incremented if the command is actually printed.
650 JobPrintCommand(LstNode *cmdNode, Job *job)
652 Boolean noSpecials; /* true if we shouldn't worry about
653 * inserting special commands into
654 * the input stream. */
655 Boolean shutUp = FALSE; /* true if we put a no echo command
656 * into the command file */
657 Boolean errOff = FALSE; /* true if we turned error checking
658 * off before printing the command
659 * and need to turn it back on */
660 const char *cmdTemplate;/* Template to use when printing the command */
661 char *cmd; /* Expanded command */
663 noSpecials = (noExecute && !(job->node->type & OP_MAKE));
665 #define DBPRINTF(fmt, arg) \
666 DEBUGF(JOB, (fmt, arg)); \
667 fprintf(job->cmdFILE, fmt, arg); \
668 fflush(job->cmdFILE);
671 * For debugging, we replace each command with the result of expanding
672 * the variables in the command.
674 cmd = Buf_Peel(Var_Subst(Lst_Datum(cmdNode), job->node, FALSE));
675 if (strcmp(cmd, "...") == 0) {
677 job->node->type |= OP_SAVE_CMDS;
678 if ((job->flags & JOB_IGNDOTS) == 0) {
679 job->tailCmds = Lst_Succ(cmdNode);
684 Lst_Replace(cmdNode, cmd);
687 * Check for leading @', -' or +'s to control echoing, error checking,
688 * and execution on -n.
690 while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
694 shutUp = DEBUG(LOUD) ? FALSE : TRUE;
704 * We're not actually exececuting anything...
705 * but this one needs to be - use compat mode
708 Compat_RunCommand(cmdNode, job->node);
716 while (isspace((unsigned char)*cmd))
720 * Ignore empty commands
726 cmdTemplate = "%s\n";
730 if (!(job->flags & JOB_SILENT) && !noSpecials &&
731 commandShell->hasEchoCtl) {
732 DBPRINTF("%s\n", commandShell->echoOff);
739 if (!(job->flags & JOB_IGNERR) && !noSpecials) {
740 if (commandShell->hasErrCtl) {
742 * We don't want the error-control commands
743 * showing up either, so we turn off echoing
744 * while executing them. We could put another
745 * field in the shell structure to tell
746 * JobDoOutput to look for this string too,
747 * but why make it any more complex than
750 if (!(job->flags & JOB_SILENT) && !shutUp &&
751 commandShell->hasEchoCtl) {
752 DBPRINTF("%s\n", commandShell->echoOff);
753 DBPRINTF("%s\n", commandShell->ignErr);
754 DBPRINTF("%s\n", commandShell->echoOn);
756 DBPRINTF("%s\n", commandShell->ignErr);
758 } else if (commandShell->ignErr &&
759 *commandShell->ignErr != '\0') {
761 * The shell has no error control, so we need to
762 * be weird to get it to ignore any errors from
763 * the command. If echoing is turned on, we turn
764 * it off and use the errCheck template to echo
765 * the command. Leave echoing off so the user
766 * doesn't see the weirdness we go through to
767 * ignore errors. Set cmdTemplate to use the
768 * weirdness instead of the simple "%s\n"
771 if (!(job->flags & JOB_SILENT) && !shutUp &&
772 commandShell->hasEchoCtl) {
773 DBPRINTF("%s\n", commandShell->echoOff);
774 DBPRINTF(commandShell->errCheck, cmd);
777 cmdTemplate = commandShell->ignErr;
779 * The error ignoration (hee hee) is already
780 * taken care of by the ignErr template, so
781 * pretend error checking is still on.
792 DBPRINTF(cmdTemplate, cmd);
796 * If echoing is already off, there's no point in issuing the
797 * echoOff command. Otherwise we issue it and pretend it was on
798 * for the whole command...
800 if (!shutUp && !(job->flags & JOB_SILENT) &&
801 commandShell->hasEchoCtl) {
802 DBPRINTF("%s\n", commandShell->echoOff);
805 DBPRINTF("%s\n", commandShell->errCheck);
808 DBPRINTF("%s\n", commandShell->echoOn);
815 * Called to close both input and output pipes when a job is finished.
818 * The file descriptors associated with the job are closed.
825 #if !defined(USE_KQUEUE)
826 FD_CLR(job->inPipe, &outputs);
828 if (job->outPipe != job->inPipe) {
831 JobDoOutput(job, TRUE);
835 JobDoOutput(job, TRUE);
841 * Do final processing for the given job including updating
842 * parents and starting new jobs as available/necessary. Note
843 * that we pay no attention to the JOB_IGNERR flag here.
844 * This is because when we're called because of a noexecute flag
845 * or something, jstat.w_status is 0 and when called from
846 * Job_CatchChildren, the status is zeroed if it s/b ignored.
849 * Some nodes may be put on the toBeMade queue.
850 * Final commands for the job are placed on postCommands.
852 * If we got an error and are aborting (aborting == ABORT_ERROR) and
853 * the job list is now empty, we are done for the day.
854 * If we recognized an error (makeErrors !=0), we set the aborting flag
855 * to ABORT_ERROR so no more jobs will be started.
858 JobFinish(Job *job, int *status)
863 if (WIFEXITED(*status)) {
864 int job_status = WEXITSTATUS(*status);
868 * Deal with ignored errors in -B mode. We need to
869 * print a message telling of the ignored error as
870 * well as setting status.w_status to 0 so the next
871 * command gets run. To do this, we set done to be
872 * TRUE if in -B mode and the job exited non-zero.
874 if (job_status == 0) {
877 if (job->flags & JOB_IGNERR) {
881 * If it exited non-zero and either we're
882 * doing things our way or we're not ignoring
883 * errors, the job is finished. Similarly, if
884 * the shell died because of a signal the job
885 * is also finished. In these cases, finish
886 * out the job's output before printing the
890 if (job->cmdFILE != NULL &&
891 job->cmdFILE != stdout) {
892 fclose(job->cmdFILE);
897 } else if (WIFSIGNALED(*status)) {
898 if (WTERMSIG(*status) == SIGCONT) {
900 * No need to close things down or anything.
905 * If it exited non-zero and either we're
906 * doing things our way or we're not ignoring
907 * errors, the job is finished. Similarly, if
908 * the shell died because of a signal the job
909 * is also finished. In these cases, finish
910 * out the job's output before printing the
914 if (job->cmdFILE != NULL &&
915 job->cmdFILE != stdout) {
916 fclose(job->cmdFILE);
922 * No need to close things down or anything.
927 if (WIFEXITED(*status)) {
928 if (done || DEBUG(JOB)) {
933 (job->flags & JOB_IGNERR)) {
935 * If output is going to a file and this job
936 * is ignoring errors, arrange to have the
937 * exit status sent to the output file as
940 out = fdopen(job->outFd, "w");
942 Punt("Cannot fdopen");
947 DEBUGF(JOB, ("Process %jd exited.\n",
948 (intmax_t)job->pid));
950 if (WEXITSTATUS(*status) == 0) {
952 if (usePipes && job->node != lastNode) {
953 MESSAGE(out, job->node);
954 lastNode = job->node;
957 "*** [%s] Completed successfully\n",
961 if (usePipes && job->node != lastNode) {
962 MESSAGE(out, job->node);
963 lastNode = job->node;
965 fprintf(out, "*** [%s] Error code %d%s\n",
967 WEXITSTATUS(*status),
968 (job->flags & JOB_IGNERR) ?
971 if (job->flags & JOB_IGNERR) {
978 } else if (WIFSIGNALED(*status)) {
979 if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) {
984 (job->flags & JOB_IGNERR)) {
986 * If output is going to a file and this job
987 * is ignoring errors, arrange to have the
988 * exit status sent to the output file as
991 out = fdopen(job->outFd, "w");
993 Punt("Cannot fdopen");
998 if (WTERMSIG(*status) == SIGCONT) {
1000 * If the beastie has continued, shift the
1001 * Job from the stopped list to the running
1002 * one (or re-stop it if concurrency is
1003 * exceeded) and go and get another child.
1005 if (job->flags & (JOB_RESUME | JOB_RESTART)) {
1006 if (usePipes && job->node != lastNode) {
1007 MESSAGE(out, job->node);
1008 lastNode = job->node;
1010 fprintf(out, "*** [%s] Continued\n",
1013 if (!(job->flags & JOB_CONTINUING)) {
1014 DEBUGF(JOB, ("Warning: process %jd was not "
1015 "continuing.\n", (intmax_t) job->pid));
1017 job->flags &= ~JOB_CONTINUING;
1018 TAILQ_INSERT_TAIL(&jobs, job, link);
1020 DEBUGF(JOB, ("Process %jd is continuing locally.\n",
1021 (intmax_t) job->pid));
1022 if (nJobs == maxJobs) {
1024 DEBUGF(JOB, ("Job queue is full.\n"));
1030 if (usePipes && job->node != lastNode) {
1031 MESSAGE(out, job->node);
1032 lastNode = job->node;
1035 "*** [%s] Signal %d\n", job->node->name,
1044 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
1046 * If output is going to a file and this job
1047 * is ignoring errors, arrange to have the
1048 * exit status sent to the output file as
1051 out = fdopen(job->outFd, "w");
1053 Punt("Cannot fdopen");
1058 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid));
1059 if (usePipes && job->node != lastNode) {
1060 MESSAGE(out, job->node);
1061 lastNode = job->node;
1063 fprintf(out, "*** [%s] Stopped -- signal %d\n",
1064 job->node->name, WSTOPSIG(*status));
1065 job->flags |= JOB_RESUME;
1066 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1072 * Now handle the -B-mode stuff. If the beast still isn't finished,
1073 * try and restart the job on the next command. If JobStart says it's
1074 * ok, it's ok. If there's an error, this puppy is done.
1076 if (compatMake && WIFEXITED(*status) &&
1077 Lst_Succ(job->node->compat_command) != NULL) {
1078 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
1084 W_SETEXITSTATUS(status, 1);
1088 * If we got back a JOB_FINISHED code, JobStart has
1089 * already called Make_Update and freed the job
1090 * descriptor. We set done to false here to avoid fake
1091 * cycles and double frees. JobStart needs to do the
1092 * update so we can proceed up the graph when given
1104 if (done && aborting != ABORT_ERROR &&
1105 aborting != ABORT_INTERRUPT && *status == 0) {
1107 * As long as we aren't aborting and the job didn't return a
1108 * non-zero status that we shouldn't ignore, we call
1109 * Make_Update to update the parents. In addition, any saved
1110 * commands for the node are placed on the .END target.
1112 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) {
1113 Lst_AtEnd(&postCommands->commands,
1115 Var_Subst(Lst_Datum(ln), job->node, FALSE)));
1118 job->node->made = MADE;
1119 Make_Update(job->node);
1122 } else if (*status != 0) {
1130 * Set aborting if any error.
1132 if (makeErrors && !keepgoing && aborting != ABORT_INTERRUPT) {
1134 * If we found any errors in this batch of children and the -k
1135 * flag wasn't given, we set the aborting flag so no more jobs
1138 aborting = ABORT_ERROR;
1141 if (aborting == ABORT_ERROR && Job_Empty()) {
1143 * If we are aborting and the job table is now empty, we finish.
1151 * Touch the given target. Called by JobStart when the -t flag was
1152 * given. Prints messages unless told to be silent.
1155 * The data modification of the file is changed. In addition, if the
1156 * file did not exist, it is created.
1159 Job_Touch(GNode *gn, Boolean silent)
1161 int streamID; /* ID of stream opened to do the touch */
1162 struct utimbuf times; /* Times for utime() call */
1164 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) {
1166 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual"
1167 * targets and, as such, shouldn't really be created.
1173 fprintf(stdout, "touch %s\n", gn->name);
1181 if (gn->type & OP_ARCHV) {
1183 } else if (gn->type & OP_LIB) {
1186 char *file = gn->path ? gn->path : gn->name;
1188 times.actime = times.modtime = now;
1189 if (utime(file, ×) < 0) {
1190 streamID = open(file, O_RDWR | O_CREAT, 0666);
1192 if (streamID >= 0) {
1196 * Read and write a byte to the file to change
1197 * the modification time, then close the file.
1199 if (read(streamID, &c, 1) == 1) {
1200 lseek(streamID, (off_t)0, SEEK_SET);
1201 write(streamID, &c, 1);
1206 fprintf(stdout, "*** couldn't touch %s: %s",
1207 file, strerror(errno));
1216 * Make sure the given node has all the commands it needs.
1219 * TRUE if the commands list is/was ok.
1222 * The node will have commands from the .DEFAULT rule added to it
1226 Job_CheckCommands(GNode *gn, void (*abortProc)(const char *, ...))
1229 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands) &&
1230 (gn->type & OP_LIB) == 0) {
1232 * No commands. Look for .DEFAULT rule from which we might infer
1235 if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) {
1237 * Make only looks for a .DEFAULT if the node was
1238 * never the target of an operator, so that's what we
1239 * do too. If a .DEFAULT was given, we substitute its
1240 * commands for gn's commands and set the IMPSRC
1241 * variable to be the target's name The DEFAULT node
1242 * acts like a transformation rule, in that gn also
1243 * inherits any attributes or sources attached to
1246 Make_HandleUse(DEFAULT, gn);
1247 Var_Set(IMPSRC, Var_Value(TARGET, gn), gn);
1249 } else if (Dir_MTime(gn) == 0) {
1251 * The node wasn't the target of an operator we have
1252 * no .DEFAULT rule to go on and the target doesn't
1253 * already exist. There's nothing more we can do for
1254 * this branch. If the -k flag wasn't given, we stop
1255 * in our tracks, otherwise we just don't update
1256 * this node's parents so they never get examined.
1258 static const char msg[] =
1259 "make: don't know how to make";
1261 if (gn->type & OP_OPTIONAL) {
1262 fprintf(stdout, "%s %s(ignored)\n",
1265 } else if (keepgoing) {
1266 fprintf(stdout, "%s %s(continuing)\n",
1271 #ifndef WITHOUT_OLD_JOKE
1272 if (strcmp(gn->name,"love") == 0)
1273 (*abortProc)("Not war.");
1276 (*abortProc)("%s %s. Stop",
1287 * Execute the shell for the given job. Called from JobStart and
1291 * A shell is executed, outputs is altered and the Job structure added
1295 JobExec(Job *job, char **argv)
1302 DEBUGF(JOB, ("Running %s\n", job->node->name));
1303 DEBUGF(JOB, ("\tCommand: "));
1304 for (i = 0; argv[i] != NULL; i++) {
1305 DEBUGF(JOB, ("%s ", argv[i]));
1307 DEBUGF(JOB, ("\n"));
1311 * Some jobs produce no output and it's disconcerting to have
1312 * no feedback of their running (since they produce no output, the
1313 * banner with their name in it never appears). This is an attempt to
1314 * provide that feedback, even if nothing follows it.
1316 if (lastNode != job->node && (job->flags & JOB_FIRST) &&
1317 !(job->flags & JOB_SILENT)) {
1318 MESSAGE(stdout, job->node);
1319 lastNode = job->node;
1322 ps.in = FILENO(job->cmdFILE);
1325 * Set up the child's output to be routed through the
1326 * pipe we've created for it.
1328 ps.out = job->outPipe;
1331 * We're capturing output in a file, so we duplicate
1332 * the descriptor to the temporary file into the
1335 ps.out = job->outFd;
1337 ps.err = STDERR_FILENO;
1339 ps.merge_errors = 1;
1347 * Fork. Warning since we are doing vfork() instead of fork(),
1348 * do not allocate memory in the child process!
1350 if ((ps.child_pid = vfork()) == -1) {
1351 Punt("Cannot fork");
1354 } else if (ps.child_pid == 0) {
1368 job->pid = ps.child_pid;
1370 if (usePipes && (job->flags & JOB_FIRST)) {
1372 * The first time a job is run for a node, we set the
1373 * current position in the buffer to the beginning and
1374 * mark another stream to watch in the outputs mask.
1377 struct kevent kev[2];
1381 #if defined(USE_KQUEUE)
1382 EV_SET(&kev[0], job->inPipe, EVFILT_READ, EV_ADD, 0, 0, job);
1383 EV_SET(&kev[1], job->pid, EVFILT_PROC,
1384 EV_ADD | EV_ONESHOT, NOTE_EXIT, 0, NULL);
1385 if (kevent(kqfd, kev, 2, NULL, 0, NULL) != 0) {
1387 * kevent() will fail if the job is already
1390 if (errno != EINTR && errno != EBADF && errno != ESRCH)
1391 Punt("kevent: %s", strerror(errno));
1394 FD_SET(job->inPipe, &outputs);
1395 #endif /* USE_KQUEUE */
1398 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1399 fclose(job->cmdFILE);
1400 job->cmdFILE = NULL;
1404 * Now the job is actually running, add it to the table.
1407 TAILQ_INSERT_TAIL(&jobs, job, link);
1408 if (nJobs == maxJobs) {
1415 * Create the argv needed to execute the shell for a given job.
1418 JobMakeArgv(Job *job, char **argv)
1421 static char args[10]; /* For merged arguments */
1423 argv[0] = commandShell->name;
1426 if ((commandShell->exit && *commandShell->exit != '-') ||
1427 (commandShell->echo && *commandShell->echo != '-')) {
1429 * At least one of the flags doesn't have a minus before it, so
1430 * merge them together. Have to do this because the *(&(@*#*&#$#
1431 * Bourne shell thinks its second argument is a file to source.
1432 * Grrrr. Note the ten-character limitation on the combined
1435 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" :
1436 commandShell->exit ? commandShell->exit : "",
1437 (job->flags & JOB_SILENT) ? "" :
1438 commandShell->echo ? commandShell->echo : "");
1445 if (!(job->flags & JOB_IGNERR) && commandShell->exit) {
1446 argv[argc] = commandShell->exit;
1449 if (!(job->flags & JOB_SILENT) && commandShell->echo) {
1450 argv[argc] = commandShell->echo;
1459 * Restart a job that stopped for some reason. The job must be neither
1460 * on the jobs nor on the stoppedJobs list.
1463 * jobFull will be set if the job couldn't be run.
1466 JobRestart(Job *job)
1469 if (job->flags & JOB_RESTART) {
1471 * Set up the control arguments to the shell. This is based on
1472 * the flags set earlier for this job. If the JOB_IGNERR flag
1473 * is clear, the 'exit' flag of the commandShell is used to
1474 * cause it to exit upon receiving an error. If the JOB_SILENT
1475 * flag is clear, the 'echo' flag of the commandShell is used
1476 * to get it to start echoing as soon as it starts
1477 * processing commands.
1481 JobMakeArgv(job, argv);
1483 DEBUGF(JOB, ("Restarting %s...", job->node->name));
1484 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1486 * Not allowed to run -- put it back on the hold
1487 * queue and mark the table full
1489 DEBUGF(JOB, ("holding\n"));
1490 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1492 DEBUGF(JOB, ("Job queue is full.\n"));
1496 * Job may be run locally.
1498 DEBUGF(JOB, ("running locally\n"));
1504 * The job has stopped and needs to be restarted.
1505 * Why it stopped, we don't know...
1507 DEBUGF(JOB, ("Resuming %s...", job->node->name));
1508 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1509 maxJobs == 0)) && nJobs != maxJobs) {
1511 * If we haven't reached the concurrency limit already
1512 * (or the job must be run and maxJobs is 0), it's ok
1518 error = (KILL(job->pid, SIGCONT) != 0);
1522 * Make sure the user knows we've continued
1523 * the beast and actually put the thing in the
1526 job->flags |= JOB_CONTINUING;
1528 W_SETTERMSIG(&status, SIGCONT);
1529 JobFinish(job, &status);
1531 job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1532 DEBUGF(JOB, ("done\n"));
1534 Error("couldn't resume %s: %s",
1535 job->node->name, strerror(errno));
1537 W_SETEXITSTATUS(&status, 1);
1538 JobFinish(job, &status);
1542 * Job cannot be restarted. Mark the table as full and
1543 * place the job back on the list of stopped jobs.
1545 DEBUGF(JOB, ("table full\n"));
1546 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1548 DEBUGF(JOB, ("Job queue is full.\n"));
1555 * Start a target-creation process going for the target described
1556 * by the graph node gn.
1559 * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1560 * if there isn't actually anything left to do for the job and
1561 * JOB_RUNNING if the job has been started.
1564 * A new Job node is created and added to the list of running
1565 * jobs. PMake is forked and a child shell created.
1568 JobStart(GNode *gn, int flags, Job *previous)
1570 Job *job; /* new job descriptor */
1571 char *argv[4]; /* Argument vector to shell */
1572 Boolean cmdsOK; /* true if the nodes commands were all right */
1573 Boolean noExec; /* Set true if we decide not to run the job */
1574 int tfd; /* File descriptor for temp file */
1576 char tfile[PATH_MAX];
1580 JobPassSig(interrupted);
1583 if (previous != NULL) {
1584 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT);
1587 job = emalloc(sizeof(Job));
1592 job->tailCmds = NULL;
1595 * Set the initial value of the flags for this job based on the global
1596 * ones and the node's attributes... Any flags supplied by the caller
1597 * are also added to the field.
1600 if (Targ_Ignore(gn)) {
1601 job->flags |= JOB_IGNERR;
1603 if (Targ_Silent(gn)) {
1604 job->flags |= JOB_SILENT;
1606 job->flags |= flags;
1609 * Check the commands now so any attributes from .DEFAULT have a chance
1610 * to migrate to the node.
1612 if (!compatMake && (job->flags & JOB_FIRST)) {
1613 cmdsOK = Job_CheckCommands(gn, Error);
1618 if ((tdir = getenv("TMPDIR")) == NULL)
1622 * If the -n flag wasn't given, we open up OUR (not the child's)
1623 * temporary file to stuff commands in it. The thing is rd/wr so we
1624 * don't need to reopen it to feed it to the shell. If the -n flag
1625 * *was* given, we just set the file to be stdout. Cute, huh?
1627 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1629 * We're serious here, but if the commands were bogus, we're
1636 snprintf(tfile, sizeof(tfile), "%s/%s", tdir, TMPPAT);
1637 if ((tfd = mkstemp(tfile)) == -1)
1638 Punt("Cannot create temp file: %s", strerror(errno));
1639 job->cmdFILE = fdopen(tfd, "w+");
1641 if (job->cmdFILE == NULL) {
1643 Punt("Could not open %s", tfile);
1645 fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1647 * Send the commands to the command file, flush all its
1648 * buffers then rewind and remove the thing.
1653 * Used to be backwards; replace when start doing multiple
1654 * commands per shell.
1658 * Be compatible: If this is the first time for this
1659 * node, verify its commands are ok and open the
1660 * commands list for sequential access by later
1661 * invocations of JobStart. Once that is done, we take
1662 * the next command off the list and print it to the
1663 * command file. If the command was an ellipsis, note
1664 * that there's nothing more to execute.
1666 if (job->flags & JOB_FIRST)
1667 gn->compat_command = Lst_First(&gn->commands);
1669 gn->compat_command =
1670 Lst_Succ(gn->compat_command);
1672 if (gn->compat_command == NULL ||
1673 JobPrintCommand(gn->compat_command, job))
1676 if (noExec && !(job->flags & JOB_FIRST)) {
1678 * If we're not going to execute anything, the
1679 * job is done and we need to close down the
1680 * various file descriptors we've opened for
1681 * output, then call JobDoOutput to catch the
1682 * final characters or send the file to the
1683 * screen... Note that the i/o streams are only
1684 * open if this isn't the first job. Note also
1685 * that this could not be done in
1686 * Job_CatchChildren b/c it wasn't clear if
1687 * there were more commands to execute or not...
1693 * We can do all the commands at once. hooray for sanity
1696 LST_FOREACH(ln, &gn->commands) {
1697 if (JobPrintCommand(ln, job))
1702 * If we didn't print out any commands to the shell
1703 * script, there's not much point in executing the
1706 if (numCommands == 0) {
1711 } else if (noExecute) {
1713 * Not executing anything -- just print all the commands to
1714 * stdout in one fell swoop. This will still set up
1715 * job->tailCmds correctly.
1717 if (lastNode != gn) {
1718 MESSAGE(stdout, gn);
1721 job->cmdFILE = stdout;
1724 * Only print the commands if they're ok, but don't die if
1725 * they're not -- just let the user know they're bad and keep
1726 * going. It doesn't do any harm in this case and may do
1730 LST_FOREACH(ln, &gn->commands) {
1731 if (JobPrintCommand(ln, job))
1736 * Don't execute the shell, thank you.
1742 * Just touch the target and note that no shell should be
1743 * executed. Set cmdFILE to stdout to make life easier. Check
1744 * the commands, too, but don't die if they're no good -- it
1745 * does no harm to keep working up the graph.
1747 job->cmdFILE = stdout;
1748 Job_Touch(gn, job->flags & JOB_SILENT);
1753 * If we're not supposed to execute a shell, don't.
1757 * Unlink and close the command file if we opened one
1759 if (job->cmdFILE != stdout) {
1760 if (job->cmdFILE != NULL)
1761 fclose(job->cmdFILE);
1767 * We only want to work our way up the graph if we aren't here
1768 * because the commands for the job were no good.
1771 if (aborting == 0) {
1772 for (ln = job->tailCmds; ln != NULL;
1773 ln = LST_NEXT(ln)) {
1774 Lst_AtEnd(&postCommands->commands,
1775 Buf_Peel(Var_Subst(Lst_Datum(ln),
1776 job->node, FALSE)));
1778 job->node->made = MADE;
1779 Make_Update(job->node);
1782 return(JOB_FINISHED);
1788 fflush(job->cmdFILE);
1792 * Set up the control arguments to the shell. This is based on the flags
1793 * set earlier for this job.
1795 JobMakeArgv(job, argv);
1798 * If we're using pipes to catch output, create the pipe by which we'll
1799 * get the shell's output. If we're using files, print out that we're
1800 * starting a job and then set up its temporary-file name.
1802 if (!compatMake || (job->flags & JOB_FIRST)) {
1807 Punt("Cannot create pipe: %s", strerror(errno));
1808 job->inPipe = fd[0];
1809 job->outPipe = fd[1];
1810 fcntl(job->inPipe, F_SETFD, 1);
1811 fcntl(job->outPipe, F_SETFD, 1);
1813 fprintf(stdout, "Remaking `%s'\n", gn->name);
1815 snprintf(job->outFile, sizeof(job->outFile), "%s/%s",
1817 if ((job->outFd = mkstemp(job->outFile)) == -1)
1818 Punt("cannot create temp file: %s",
1820 fcntl(job->outFd, F_SETFD, 1);
1824 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) {
1826 * We've hit the limit of concurrency, so put the job on hold
1827 * until some other job finishes. Note that the special jobs
1828 * (.BEGIN, .INTERRUPT and .END) may be run even when the
1829 * limit has been reached (e.g. when maxJobs == 0).
1833 DEBUGF(JOB, ("Can only run job locally.\n"));
1834 job->flags |= JOB_RESTART;
1835 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1837 if (nJobs >= maxJobs) {
1839 * If we're running this job as a special case
1840 * (see above), at least say the table is full.
1843 DEBUGF(JOB, ("Local job queue is full.\n"));
1847 return (JOB_RUNNING);
1851 JobOutput(Job *job, char *cp, char *endp, int msg)
1855 if (commandShell->noPrint) {
1856 ecp = strstr(cp, commandShell->noPrint);
1857 while (ecp != NULL) {
1860 if (msg && job->node != lastNode) {
1861 MESSAGE(stdout, job->node);
1862 lastNode = job->node;
1865 * The only way there wouldn't be a newline
1866 * after this line is if it were the last in
1867 * the buffer. However, since the non-printable
1868 * comes after it, there must be a newline, so
1869 * we don't print one.
1871 fprintf(stdout, "%s", cp);
1874 cp = ecp + strlen(commandShell->noPrint);
1877 * Still more to print, look again after
1878 * skipping the whitespace following the
1879 * non-printable command....
1882 while (*cp == ' ' || *cp == '\t' ||
1886 ecp = strstr(cp, commandShell->noPrint);
1897 * This function is called at different times depending on
1898 * whether the user has specified that output is to be collected
1899 * via pipes or temporary files. In the former case, we are called
1900 * whenever there is something to read on the pipe. We collect more
1901 * output from the given job and store it in the job's outBuf. If
1902 * this makes up a line, we print it tagged by the job's identifier,
1904 * If output has been collected in a temporary file, we open the
1905 * file and read it line by line, transferring it to our own
1906 * output channel until the file is empty. At which point we
1907 * remove the temporary file.
1908 * In both cases, however, we keep our figurative eye out for the
1909 * 'noPrint' line for the shell from which the output came. If
1910 * we recognize a line, we don't print it. If the command is not
1911 * alone on the line (the character after it is not \0 or \n), we
1912 * do print whatever follows it.
1915 * curPos may be shifted as may the contents of outBuf.
1918 JobDoOutput(Job *job, Boolean finish)
1920 Boolean gotNL = FALSE; /* true if got a newline */
1921 Boolean fbuf; /* true if our buffer filled up */
1922 int nr; /* number of bytes read */
1923 int i; /* auxiliary index into outBuf */
1924 int max; /* limit for i (end of current data) */
1925 int nRead; /* (Temporary) number of bytes read */
1926 FILE *oFILE; /* Stream pointer to shell's output file */
1931 * Read as many bytes as will fit in the buffer.
1937 nRead = read(job->inPipe, &job->outBuf[job->curPos],
1938 JOB_BUFSIZE - job->curPos);
1940 * Check for interrupt here too, because the above read may
1941 * block when the child process is stopped. In this case the
1942 * interrupt will unblock it (we don't use SA_RESTART).
1945 JobPassSig(interrupted);
1948 DEBUGF(JOB, ("JobDoOutput(piperead)"));
1955 * If we hit the end-of-file (the job is dead), we must flush
1956 * its remaining output, so pretend we read a newline if
1957 * there's any output remaining in the buffer.
1958 * Also clear the 'finish' flag so we stop looping.
1960 if (nr == 0 && job->curPos != 0) {
1961 job->outBuf[job->curPos] = '\n';
1964 } else if (nr == 0) {
1969 * Look for the last newline in the bytes we just got. If there
1970 * is one, break out of the loop with 'i' as its index and
1973 max = job->curPos + nr;
1974 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
1975 if (job->outBuf[i] == '\n') {
1978 } else if (job->outBuf[i] == '\0') {
1982 job->outBuf[i] = ' ';
1988 if (job->curPos == JOB_BUFSIZE) {
1990 * If we've run out of buffer space, we have
1991 * no choice but to print the stuff. sigh.
1997 if (gotNL || fbuf) {
1999 * Need to send the output to the screen. Null terminate
2000 * it first, overwriting the newline character if there
2001 * was one. So long as the line isn't one we should
2002 * filter (according to the shell description), we print
2003 * the line, preceded by a target banner if this target
2004 * isn't the same as the one for which we last printed
2005 * something. The rest of the data in the buffer are
2006 * then shifted down to the start of the buffer and
2007 * curPos is set accordingly.
2009 job->outBuf[i] = '\0';
2010 if (i >= job->curPos) {
2013 cp = JobOutput(job, job->outBuf,
2014 &job->outBuf[i], FALSE);
2017 * There's still more in that buffer. This time,
2018 * though, we know there's no newline at the
2019 * end, so we add one of our own free will.
2022 if (job->node != lastNode) {
2023 MESSAGE(stdout, job->node);
2024 lastNode = job->node;
2026 fprintf(stdout, "%s%s", cp,
2032 /* shift the remaining characters down */
2033 memcpy(job->outBuf, &job->outBuf[i + 1],
2035 job->curPos = max - (i + 1);
2039 * We have written everything out, so we just
2040 * start over from the start of the buffer.
2041 * No copying. No nothing.
2048 * If the finish flag is true, we must loop until we hit
2049 * end-of-file on the pipe. This is guaranteed to happen
2050 * eventually since the other end of the pipe is now
2051 * closed (we closed it explicitly and the child has
2052 * exited). When we do get an EOF, finish will be set
2053 * FALSE and we'll fall through and out.
2060 * We've been called to retrieve the output of the job from the
2061 * temporary file where it's been squirreled away. This consists
2062 * of opening the file, reading the output line by line, being
2063 * sure not to print the noPrint line for the shell we used,
2064 * then close and remove the temporary file. Very simple.
2066 * Change to read in blocks and do FindSubString type things
2067 * as for pipes? That would allow for "@echo -n..."
2069 oFILE = fopen(job->outFile, "r");
2070 if (oFILE != NULL) {
2071 fprintf(stdout, "Results of making %s:\n",
2075 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2076 char *cp, *endp, *oendp;
2079 oendp = endp = inLine + strlen(inLine);
2080 if (endp[-1] == '\n') {
2083 cp = JobOutput(job, inLine, endp, FALSE);
2086 * There's still more in that buffer. This time,
2087 * though, we know there's no newline at the
2088 * end, so we add one of our own free will.
2090 fprintf(stdout, "%s", cp);
2092 if (endp != oendp) {
2093 fprintf(stdout, "\n");
2098 eunlink(job->outFile);
2105 * Handle the exit of a child. Called from Make_Make.
2108 * The job descriptor is removed from the list of children.
2111 * We do waits, blocking or not, according to the wisdom of our
2112 * caller, until there are no more children to report. For each
2113 * job, call JobFinish to finish things off. This will take care of
2114 * putting jobs on the stoppedJobs queue.
2117 Job_CatchChildren(Boolean block)
2119 pid_t pid; /* pid of dead child */
2120 Job *job; /* job descriptor for dead child */
2121 int status; /* Exit/termination status */
2124 * Don't even bother if we know there's no one around.
2131 pid = waitpid(-1, &status,
2132 (block ? 0 : WNOHANG) | WUNTRACED);
2136 DEBUGF(JOB, ("Process %jd exited or stopped.\n",
2139 TAILQ_FOREACH(job, &jobs, link) {
2140 if (job->pid == pid)
2145 if (WIFSIGNALED(status) &&
2146 (WTERMSIG(status) == SIGCONT)) {
2147 TAILQ_FOREACH(job, &jobs, link) {
2148 if (job->pid == pid)
2152 Error("Resumed child (%jd) "
2153 "not in table", (intmax_t)pid);
2156 TAILQ_REMOVE(&stoppedJobs, job, link);
2158 Error("Child (%jd) not in table?",
2163 TAILQ_REMOVE(&jobs, job, link);
2165 if (fifoFd >= 0 && maxJobs > 1) {
2166 write(fifoFd, "+", 1);
2168 if (nJobs >= maxJobs)
2173 DEBUGF(JOB, ("Job queue is no longer full.\n"));
2178 JobFinish(job, &status);
2181 JobPassSig(interrupted);
2186 * Catch the output from our children, if we're using
2187 * pipes do so. Otherwise just block time until we get a
2188 * signal(most likely a SIGCHLD) since there's no point in
2189 * just spinning when there's nothing to do and the reaping
2190 * of a child can wait for a while.
2193 * Output is read from pipes if we're piping.
2194 * -----------------------------------------------------------------------
2198 Job_CatchOutput(int flag __unused)
2200 Job_CatchOutput(int flag)
2206 struct kevent kev[KEV_SIZE];
2209 struct timeval timeout;
2218 if ((nfds = kevent(kqfd, NULL, 0, kev, KEV_SIZE, NULL)) == -1) {
2220 Punt("kevent: %s", strerror(errno));
2222 JobPassSig(interrupted);
2224 for (i = 0; i < nfds; i++) {
2225 if (kev[i].flags & EV_ERROR) {
2226 warnc(kev[i].data, "kevent");
2229 switch (kev[i].filter) {
2231 JobDoOutput(kev[i].udata, FALSE);
2235 * Just wake up and let
2236 * Job_CatchChildren() collect the
2245 timeout.tv_sec = SEL_SEC;
2246 timeout.tv_usec = SEL_USEC;
2247 if (flag && jobFull && fifoFd >= 0)
2248 FD_SET(fifoFd, &readfds);
2250 nfds = select(FD_SETSIZE, &readfds, (fd_set *)NULL,
2251 (fd_set *)NULL, &timeout);
2254 JobPassSig(interrupted);
2257 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) {
2261 job = TAILQ_FIRST(&jobs);
2262 while (nfds != 0 && job != NULL) {
2263 if (FD_ISSET(job->inPipe, &readfds)) {
2264 JobDoOutput(job, FALSE);
2267 job = TAILQ_NEXT(job, link);
2269 #endif /* !USE_KQUEUE */
2275 * Start the creation of a target. Basically a front-end for
2276 * JobStart used by the Make module.
2279 * Another job is started.
2285 JobStart(gn, 0, NULL);
2294 } else if (!Var_Exists(MAKE_JOB_PREFIX, VAR_GLOBAL)) {
2295 Var_SetGlobal(MAKE_JOB_PREFIX, "---");
2297 targPrefix = Var_Subst("${" MAKE_JOB_PREFIX "}", VAR_GLOBAL, 0)->buf;
2302 * Initialize the process module, given a maximum number of jobs.
2305 * lists and counters are initialized
2308 Job_Init(int maxproc)
2310 GNode *begin; /* node for commands to do at the very start */
2312 struct sigaction sa;
2315 env = getenv("MAKE_JOBS_FIFO");
2317 if (env == NULL && maxproc > 1) {
2319 * We did not find the environment variable so we are the
2320 * leader. Create the fifo, open it, write one char per
2321 * allowed job into the pipe.
2323 fifoFd = mkfifotemp(fifoName);
2328 fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2330 setenv("MAKE_JOBS_FIFO", env, 1);
2331 while (maxproc-- > 0) {
2332 write(fifoFd, "+", 1);
2334 /* The master make does not get a magic token */
2339 } else if (env != NULL) {
2341 * We had the environment variable so we are a slave.
2342 * Open fifo and give ourselves a magic token which represents
2343 * the token our parent make has grabbed to start his make
2344 * process. Otherwise the sub-makes would gobble up tokens and
2345 * the proper number of tokens to specify to -j would depend
2346 * on the depth of the tree and the order of execution.
2348 fifoFd = open(env, O_RDWR, 0);
2350 fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2366 if ((maxJobs == 1 && fifoFd < 0) || !beVerbose || is_posix || beQuiet) {
2368 * If only one job can run at a time, there's no need for a
2369 * banner, no is there?
2377 * Catch the four signals that POSIX specifies if they aren't ignored.
2378 * JobCatchSignal will just set global variables and hope someone
2379 * else is going to handle the interrupt.
2381 sa.sa_handler = JobCatchSig;
2382 sigemptyset(&sa.sa_mask);
2385 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
2386 sigaction(SIGINT, &sa, NULL);
2388 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
2389 sigaction(SIGHUP, &sa, NULL);
2391 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
2392 sigaction(SIGQUIT, &sa, NULL);
2394 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
2395 sigaction(SIGTERM, &sa, NULL);
2398 * There are additional signals that need to be caught and passed if
2399 * either the export system wants to be told directly of signals or if
2400 * we're giving each job its own process group (since then it won't get
2401 * signals from the terminal driver as we own the terminal)
2403 #if defined(USE_PGRP)
2404 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
2405 sigaction(SIGTSTP, &sa, NULL);
2407 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
2408 sigaction(SIGTTOU, &sa, NULL);
2410 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
2411 sigaction(SIGTTIN, &sa, NULL);
2413 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
2414 sigaction(SIGWINCH, &sa, NULL);
2419 if ((kqfd = kqueue()) == -1) {
2420 Punt("kqueue: %s", strerror(errno));
2424 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2426 if (begin != NULL) {
2427 JobStart(begin, JOB_SPECIAL, (Job *)NULL);
2430 Job_CatchChildren(!usePipes);
2433 postCommands = Targ_FindNode(".END", TARG_CREATE);
2438 * See if the job table is full. It is considered full if it is OR
2439 * if we are in the process of aborting OR if we have
2440 * reached/exceeded our local quota. This prevents any more jobs
2444 * TRUE if the job table is full, FALSE otherwise
2454 if (fifoFd >= 0 && jobFull) {
2455 i = read(fifoFd, &c, 1);
2466 * See if the job table is empty. Because the local concurrency may
2467 * be set to 0, it is possible for the job table to become empty,
2468 * while the list of stoppedJobs remains non-empty. In such a case,
2469 * we want to restart as many jobs as we can.
2472 * TRUE if it is. FALSE if it ain't.
2478 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) {
2480 * The job table is obviously not full if it has no
2481 * jobs in it...Try and restart the stopped jobs.
2496 * Handle the receipt of an interrupt.
2499 * All children are killed. Another job will be started if the
2500 * .INTERRUPT target was given.
2503 JobInterrupt(int runINTERRUPT, int signo)
2505 Job *job; /* job descriptor in that element */
2506 GNode *interrupt; /* the node describing the .INTERRUPT target */
2508 aborting = ABORT_INTERRUPT;
2510 TAILQ_FOREACH(job, &jobs, link) {
2511 if (!Targ_Precious(job->node)) {
2512 char *file = (job->node->path == NULL ?
2513 job->node->name : job->node->path);
2515 if (!noExecute && eunlink(file) != -1) {
2516 Error("*** %s removed", file);
2520 DEBUGF(JOB, ("JobInterrupt passing signal to child "
2521 "%jd.\n", (intmax_t)job->pid));
2522 KILL(job->pid, signo);
2526 if (runINTERRUPT && !touchFlag) {
2528 * clear the interrupted flag because we would get an
2529 * infinite loop otherwise.
2533 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2534 if (interrupt != NULL) {
2535 ignoreErrors = FALSE;
2537 JobStart(interrupt, JOB_IGNDOTS, (Job *)NULL);
2540 Job_CatchChildren(!usePipes);
2550 * Do final processing such as the running of the commands
2551 * attached to the .END target.
2560 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) {
2562 Error("Errors reported so .END ignored");
2564 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
2568 Job_CatchChildren(!usePipes);
2582 * Waits for all running jobs to finish and returns. Sets 'aborting'
2583 * to ABORT_WAIT to prevent other jobs from starting.
2586 * Currently running jobs finish.
2592 aborting = ABORT_WAIT;
2593 while (nJobs != 0) {
2595 Job_CatchChildren(!usePipes);
2602 * Abort all currently running jobs without handling output or anything.
2603 * This function is to be called only in the event of a major
2604 * error. Most definitely NOT to be called from JobInterrupt.
2607 * All children are killed, not just the firstborn
2612 Job *job; /* the job descriptor in that element */
2615 aborting = ABORT_ERROR;
2618 TAILQ_FOREACH(job, &jobs, link) {
2620 * kill the child process with increasingly drastic
2621 * signals to make darn sure it's dead.
2623 KILL(job->pid, SIGINT);
2624 KILL(job->pid, SIGKILL);
2629 * Catch as many children as want to report in at first, then give up
2631 while (waitpid(-1, &foo, WNOHANG) > 0)
2637 * Tries to restart stopped jobs if there are slots available.
2638 * Note that this tries to restart them regardless of pending errors.
2639 * It's not good to leave stopped jobs lying around!
2642 * Resumes(and possibly migrates) jobs.
2645 JobRestartJobs(void)
2649 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) {
2650 DEBUGF(JOB, ("Job queue is not full. "
2651 "Restarting a stopped job.\n"));
2652 TAILQ_REMOVE(&stoppedJobs, job, link);
2659 * Execute the command in cmd, and return the output of that command
2663 * A string containing the output of the command, or the empty string
2664 * If error is not NULL, it contains the reason for the command failure
2665 * Any output sent to stderr in the child process is passed to stderr,
2666 * and not captured in the string.
2669 * The string must be freed by the caller.
2672 Cmd_Exec(const char *cmd, const char **error)
2674 int fds[2]; /* Pipe streams */
2675 int status; /* command exit status */
2676 Buffer *buf; /* buffer to store the result */
2684 * Open a pipe for fetching its output
2686 if (pipe(fds) == -1) {
2687 *error = "Couldn't create pipe for \"%s\"";
2691 /* Set close-on-exec on read side of pipe. */
2692 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC);
2694 ps.in = STDIN_FILENO;
2696 ps.err = STDERR_FILENO;
2698 ps.merge_errors = 0;
2702 /* Set up arguments for shell */
2703 ps.argv = emalloc(4 * sizeof(char *));
2704 ps.argv[0] = strdup(commandShell->name);
2705 ps.argv[1] = strdup("-c");
2706 ps.argv[2] = strdup(cmd);
2711 * Fork. Warning since we are doing vfork() instead of fork(),
2712 * do not allocate memory in the child process!
2714 if ((ps.child_pid = vfork()) == -1) {
2715 *error = "Couldn't exec \"%s\"";
2718 } else if (ps.child_pid == 0) {
2731 close(fds[1]); /* No need for the writing half of the pipe. */
2734 char result[BUFSIZ];
2736 rcnt = read(fds[0], result, sizeof(result));
2738 Buf_AddBytes(buf, (size_t)rcnt, (Byte *)result);
2739 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR));
2742 *error = "Error reading shell's output for \"%s\"";
2745 * Close the input side of the pipe.
2749 status = ProcWait(&ps);
2752 *error = "\"%s\" returned non-zero status";
2754 Buf_StripNewlines(buf);
2761 * Interrupt handler - set flag and defer handling to the main code
2764 CompatCatchSig(int signo)
2767 interrupted = signo;
2771 *-----------------------------------------------------------------------
2772 * CompatInterrupt --
2773 * Interrupt the creation of the current target and remove it if
2774 * it ain't precious.
2780 * The target is removed and the process exits. If .INTERRUPT exists,
2781 * its commands are run first WITH INTERRUPTS IGNORED..
2783 *-----------------------------------------------------------------------
2786 CompatInterrupt(int signo)
2789 sigset_t nmask, omask;
2792 sigemptyset(&nmask);
2793 sigaddset(&nmask, SIGINT);
2794 sigaddset(&nmask, SIGTERM);
2795 sigaddset(&nmask, SIGHUP);
2796 sigaddset(&nmask, SIGQUIT);
2797 sigprocmask(SIG_SETMASK, &nmask, &omask);
2799 /* prevent recursion in evaluation of .INTERRUPT */
2802 if (curTarg != NULL && !Targ_Precious(curTarg)) {
2803 const char *file = Var_Value(TARGET, curTarg);
2805 if (!noExecute && eunlink(file) != -1) {
2806 printf("*** %s removed\n", file);
2811 * Run .INTERRUPT only if hit with interrupt signal
2813 if (signo == SIGINT) {
2814 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2816 LST_FOREACH(ln, &gn->commands) {
2817 if (Compat_RunCommand(ln, gn))
2823 sigprocmask(SIG_SETMASK, &omask, NULL);
2825 if (signo == SIGQUIT)
2827 signal(signo, SIG_DFL);
2828 kill(getpid(), signo);
2835 * Returns NULL if a specified line must be executed by the shell,
2836 * and an argument vector if it can be run via execvp().
2839 * Uses brk_string so destroys the contents of argv.
2842 shellneed(ArgArray *aa, char *cmd)
2847 if (commandShell->meta == NULL || commandShell->builtins.argc <= 1)
2851 if (strpbrk(cmd, commandShell->meta) != NULL)
2855 * Break the command into words to form an argument
2856 * vector we can execute.
2858 brk_string(aa, cmd, TRUE);
2859 for (p = commandShell->builtins.argv + 1; *p != 0; p++) {
2860 if ((ret = strcmp(aa->argv[1], *p)) == 0) {
2861 /* found - use shell */
2870 return (aa->argv + 1);
2874 * Execute the next command for a target. If the command returns an
2875 * error, the node's made field is set to ERROR and creation stops.
2876 * The node from which the command came is also given. This is used
2877 * to execute the commands in compat mode and when executing commands
2878 * with the '+' flag in non-compat mode. In these modes each command
2879 * line should be executed by its own shell. We do some optimisation here:
2880 * if the shell description defines both a string of meta characters and
2881 * a list of builtins and the command line neither contains a meta character
2882 * nor starts with one of the builtins then we execute the command directly
2883 * without invoking a shell.
2886 * 0 if the command succeeded, 1 if an error occurred.
2889 * The node's 'made' field may be set to ERROR.
2892 Compat_RunCommand(LstNode *cmdNode, GNode *gn)
2895 char *cmd; /* Expanded command */
2896 Boolean silent; /* Don't print command */
2897 Boolean doit; /* Execute even in -n */
2898 Boolean errCheck; /* Check errors */
2899 int reason; /* Reason for child's death */
2900 int status; /* Description of child's death */
2901 char **av; /* Argument vector for thing to exec */
2904 silent = gn->type & OP_SILENT;
2905 errCheck = !(gn->type & OP_IGNORE);
2908 cmd = Buf_Peel(Var_Subst(Lst_Datum(cmdNode), gn, FALSE));
2909 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
2910 Lst_AtEnd(&ENDNode->commands, cmd);
2912 } else if (strcmp(cmd, "...") == 0) {
2914 gn->type |= OP_SAVE_CMDS;
2917 Lst_Replace(cmdNode, cmd);
2919 while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
2923 silent = DEBUG(LOUD) ? FALSE : TRUE;
2937 while (isspace((unsigned char)*cmd))
2941 * Ignore empty commands
2948 * Print the command before echoing if we're not supposed to be quiet
2949 * for this one. We also print the command if -n given, but not if '+'.
2951 if (!silent || (noExecute && !doit)) {
2952 printf("%s\n", cmd);
2957 * If we're not supposed to execute any commands, this is as far as
2960 if (!doit && noExecute) {
2964 ps.in = STDIN_FILENO;
2965 ps.out = STDOUT_FILENO;
2966 ps.err = STDERR_FILENO;
2968 ps.merge_errors = 0;
2972 if ((av = shellneed(&aa, cmd)) == NULL) {
2974 * Shell meta character or shell builtin found - pass
2975 * command to shell. We give the shell the -e flag as
2976 * well as -c if it is supposed to exit when it hits an error.
2978 ps.argv = emalloc(4 * sizeof(char *));
2979 ps.argv[0] = strdup(commandShell->path);
2980 ps.argv[1] = strdup(errCheck ? "-ec" : "-c");
2981 ps.argv[2] = strdup(cmd);
2988 ps.errCheck = errCheck;
2991 * Warning since we are doing vfork() instead of fork(),
2992 * do not allocate memory in the child process!
2994 if ((ps.child_pid = vfork()) == -1) {
2995 Fatal("Could not fork");
2997 } else if (ps.child_pid == 0) {
3015 * we need to print out the command associated with this
3016 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging
3017 * at level g2, in main(), Fatal() and DieHorribly(),
3018 * therefore do not free it when debugging.
3020 if (!DEBUG(GRAPH2)) {
3021 free(Lst_Datum(cmdNode));
3022 Lst_Replace(cmdNode, NULL);
3026 * The child is off and running. Now all we can do is wait...
3028 reason = ProcWait(&ps);
3031 CompatInterrupt(interrupted);
3034 * Decode and report the reason child exited, then
3035 * indicate how we handled it.
3037 if (WIFEXITED(reason)) {
3038 status = WEXITSTATUS(reason);
3042 printf("*** [%s] Error code %d",
3045 } else if (WIFSTOPPED(reason)) {
3046 status = WSTOPSIG(reason);
3048 status = WTERMSIG(reason);
3049 printf("*** [%s] Signal %d",
3061 printf(" (continuing)\n");
3066 * Continue executing
3067 * commands for this target.
3068 * If we return 0, this will
3071 printf(" (ignored)\n");
3078 *-----------------------------------------------------------------------
3080 * Make a target, given the parent, to abort if necessary.
3083 * If an error is detected and not being ignored, the process exits.
3085 *-----------------------------------------------------------------------
3088 Compat_Make(GNode *gn, GNode *pgn)
3092 if (gn->type & OP_USE) {
3093 Make_HandleUse(gn, pgn);
3095 } else if (gn->made == UNMADE) {
3097 * First mark ourselves to be made, then apply whatever
3098 * transformations the suffix module thinks are necessary.
3099 * Once that's done, we can descend and make all our children.
3100 * If any of them has an error but the -k flag was given, our
3101 * 'make' field will be set FALSE again. This is our signal to
3102 * not attempt to do anything but abort our parent as well.
3105 gn->made = BEINGMADE;
3107 LST_FOREACH(ln, &gn->children)
3108 Compat_Make(Lst_Datum(ln), gn);
3115 if (Lst_Member(&gn->iParents, pgn) != NULL) {
3116 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn);
3120 * All the children were made ok. Now cmtime contains the
3121 * modification time of the newest child, we need to find out
3122 * if we exist and when we were modified last. The criteria for
3123 * datedness are defined by the Make_OODate function.
3125 DEBUGF(MAKE, ("Examining %s...", gn->name));
3126 if (!Make_OODate(gn)) {
3127 gn->made = UPTODATE;
3128 DEBUGF(MAKE, ("up-to-date.\n"));
3131 DEBUGF(MAKE, ("out-of-date.\n"));
3135 * If the user is just seeing if something is out-of-date,
3136 * exit now to tell him/her "yes".
3143 * We need to be re-made. We also have to make sure we've got
3144 * a $? variable. To be nice, we also define the $> variable
3145 * using Make_DoAllVar().
3150 * Alter our type to tell if errors should be ignored or things
3151 * should not be printed so Compat_RunCommand knows what to do.
3153 if (Targ_Ignore(gn)) {
3154 gn->type |= OP_IGNORE;
3156 if (Targ_Silent(gn)) {
3157 gn->type |= OP_SILENT;
3160 if (Job_CheckCommands(gn, Fatal)) {
3162 * Our commands are ok, but we still have to worry
3163 * about the -t flag...
3167 LST_FOREACH(ln, &gn->commands) {
3168 if (Compat_RunCommand(ln, gn))
3173 Job_Touch(gn, gn->type & OP_SILENT);
3179 if (gn->made != ERROR) {
3181 * If the node was made successfully, mark it so, update
3182 * its modification time and timestamp all its parents.
3183 * Note that for .ZEROTIME targets, the timestamping
3184 * isn't done. This is to keep its state from affecting
3185 * that of its parent.
3190 * We can't re-stat the thing, but we can at least take
3191 * care of rules where a target depends on a source that
3192 * actually creates the target, but only if it has
3200 * mv y.tab.o parse.o
3201 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
3203 * In this case, if the definitions produced by yacc
3204 * haven't changed from before, parse.h won't have been
3205 * updated and gn->mtime will reflect the current
3206 * modification time for parse.h. This is something of a
3207 * kludge, I admit, but it's a useful one..
3209 * XXX: People like to use a rule like
3213 * To force things that depend on FRC to be made, so we
3214 * have to check for gn->children being empty as well...
3216 if (!Lst_IsEmpty(&gn->commands) ||
3217 Lst_IsEmpty(&gn->children)) {
3222 * This is what Make does and it's actually a good
3223 * thing, as it allows rules like
3225 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
3227 * to function as intended. Unfortunately, thanks to
3228 * the stateless nature of NFS (and the speed of this
3229 * program), there are times when the modification time
3230 * of a file created on a remote machine will not be
3231 * modified before the stat() implied by the Dir_MTime
3232 * occurs, thus leading us to believe that the file
3233 * is unchanged, wreaking havoc with files that depend
3236 * I have decided it is better to make too much than to
3237 * make too little, so this stuff is commented out
3238 * unless you're sure it's ok.
3241 if (noExecute || Dir_MTime(gn) == 0) {
3244 if (gn->cmtime > gn->mtime)
3245 gn->mtime = gn->cmtime;
3246 DEBUGF(MAKE, ("update time: %s\n",
3247 Targ_FmtTime(gn->mtime)));
3249 if (!(gn->type & OP_EXEC)) {
3250 pgn->childMade = TRUE;
3251 Make_TimeStamp(pgn, gn);
3254 } else if (keepgoing) {
3258 printf("\n\nStop in %s.\n", Var_Value(".CURDIR", gn));
3261 } else if (gn->made == ERROR) {
3263 * Already had an error when making this beastie. Tell the
3268 if (Lst_Member(&gn->iParents, pgn) != NULL) {
3269 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn);
3273 Error("Graph cycles through %s\n", gn->name);
3278 if ((gn->type & OP_EXEC) == 0) {
3279 pgn->childMade = TRUE;
3280 Make_TimeStamp(pgn, gn);
3284 if ((gn->type & OP_EXEC) == 0) {
3285 Make_TimeStamp(pgn, gn);
3297 * Install signal handlers for Compat_Run
3300 Compat_InstallSignalHandlers(void)
3303 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
3304 signal(SIGINT, CompatCatchSig);
3306 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
3307 signal(SIGTERM, CompatCatchSig);
3309 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
3310 signal(SIGHUP, CompatCatchSig);
3312 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
3313 signal(SIGQUIT, CompatCatchSig);
3318 *-----------------------------------------------------------------------
3320 * Start making again, given a list of target nodes.
3328 *-----------------------------------------------------------------------
3331 Compat_Run(Lst *targs)
3333 GNode *gn = NULL; /* Current root target */
3336 Compat_InstallSignalHandlers();
3337 ENDNode = Targ_FindNode(".END", TARG_CREATE);
3339 * If the user has defined a .BEGIN target, execute the commands
3343 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
3345 LST_FOREACH(ln, &gn->commands) {
3346 if (Compat_RunCommand(ln, gn))
3349 if (gn->made == ERROR) {
3350 printf("\n\nStop.\n");
3357 * For each entry in the list of targets to create, call Compat_Make on
3358 * it to create the thing. Compat_Make will leave the 'made' field of gn
3359 * in one of several states:
3360 * UPTODATE gn was already up-to-date
3361 * MADE gn was recreated successfully
3362 * ERROR An error occurred while gn was being created
3363 * ABORTED gn was not remade because one of its inferiors
3364 * could not be made due to errors.
3367 while (!Lst_IsEmpty(targs)) {
3368 gn = Lst_DeQueue(targs);
3369 Compat_Make(gn, gn);
3371 if (gn->made == UPTODATE) {
3372 printf("`%s' is up to date.\n", gn->name);
3373 } else if (gn->made == ABORTED) {
3374 printf("`%s' not remade because of errors.\n",
3377 } else if (gn->made == ERROR) {
3383 * If the user has defined a .END target, run its commands.
3385 if (makeErrors == 0) {
3386 LST_FOREACH(ln, &ENDNode->commands) {
3387 if (Compat_RunCommand(ln, ENDNode))