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
40 * $FreeBSD: src/usr.bin/make/job.c,v 1.75 2005/02/10 14:32:14 harti Exp $
41 * $DragonFly: src/usr.bin/make/job.c,v 1.143 2005/08/24 00:09:29 okumoto Exp $
50 * handle the creation etc. of our child processes.
53 * Job_Make Start the creation of the given target.
56 * Check for and handle the termination of any children.
57 * This must be called reasonably frequently to keep the
58 * whole make going at a decent clip, since job table
59 * entries aren't removed until their process is caught
60 * this way. Its single argument is true if the function
61 * should block waiting for a child to terminate.
63 * Job_CatchOutput Print any output our children have produced. Should
64 * also be called fairly frequently to keep the user
65 * informed of what's going on. If no output is waiting,
66 * it will block for a time given by the SEL_* constants,
67 * below, or until output is ready.
69 * Job_Init Called to initialize this module. in addition, any
70 * commands attached to the .BEGIN target are executed
71 * before this function returns. Hence, the makefile must
72 * have been parsed before this function is called.
74 * Job_Full Return true if the job table is filled.
76 * Job_Empty Return true if the job table is completely empty.
78 * Job_Finish Perform any final processing which needs doing. This
79 * includes the execution of any commands which have
80 * been/were attached to the .END target. It should only
81 * be called when the job table is empty.
83 * Job_AbortAll Abort all currently running jobs. It doesn't handle
84 * output or do anything for the jobs, just kills them.
85 * It should only be called in an emergency, as it were.
88 * Verify that the commands for a target are ok. Provide
89 * them if necessary and possible.
91 * JobTouch Update a target without really updating it.
93 * Job_Wait Wait for all currently-running jobs to finish.
96 * The routines in this file implement the full-compatibility
97 * mode of PMake. Most of the special functionality of PMake
98 * is available in this mode. Things not supported:
100 * - friendly variable substitution.
103 * Compat_Run Initialize things for this module and recreate
104 * thems as need creatin'
107 #include <sys/queue.h>
108 #include <sys/types.h>
109 #include <sys/select.h>
110 #include <sys/stat.h>
111 #include <sys/wait.h>
116 #include <inttypes.h>
132 #include "pathnames.h"
141 #define TMPPAT "/tmp/makeXXXXXXXXXX"
142 #define MKLVL_MAXVAL 500
143 #define MKLVL_ENVVAR "__MKLVL__"
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
154 * Job Table definitions.
156 * The job "table" is kept as a linked Lst in 'jobs', with the number of
157 * active jobs maintained in the 'nJobs' variable. At no time will this
158 * exceed the value of 'maxJobs', initialized by the Job_Init function.
160 * When a job is finished, the Make_Update function is called on each of the
161 * parents of the node which was just remade. This takes care of the upward
162 * traversal of the dependency graph.
164 #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[sizeof(TMPPAT)];
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
261 #define outFile output.o_file.of_outFile
262 #define outFd output.o_file.of_outFd
264 TAILQ_HEAD(JobList, Job);
267 * error handling variables
269 static int errors = 0; /* number of errors reported */
270 static int aborting = 0; /* why is the make aborting? */
271 #define ABORT_ERROR 1 /* Because of an error */
272 #define ABORT_INTERRUPT 2 /* Because it was interrupted */
273 #define ABORT_WAIT 3 /* Waiting for jobs to finish */
276 * post-make command processing. The node postCommands is really just the
277 * .END target but we keep it around to avoid having to search for it
280 static GNode *postCommands;
283 * The number of commands actually printed for a target. Should this
284 * number be 0, no shell will be executed.
286 static int numCommands;
289 * Return values from JobStart.
291 #define JOB_RUNNING 0 /* Job is running */
292 #define JOB_ERROR 1 /* Error in starting the job */
293 #define JOB_FINISHED 2 /* The job is already finished */
294 #define JOB_STOPPED 3 /* The job is stopped */
297 * The maximum number of jobs that may run. This is initialize from the
298 * -j argument for the leading make and from the FIFO for sub-makes.
301 static int nJobs; /* The number of children currently running */
303 /* The structures that describe them */
304 static struct JobList jobs = TAILQ_HEAD_INITIALIZER(jobs);
306 static bool jobFull; /* Flag to tell when the job table is full. It
307 * is set true when (1) the total number of
308 * running jobs equals the maximum allowed */
309 static fd_set outputs; /* Set of descriptors of pipes connected to
310 * the output channels of children */
311 static GNode *lastNode; /* The node for which output was most recently
313 static const char *targFmt; /* Format string to use to head output from a
314 * job when it's not the most-recent job heard
317 #define TARG_FMT "--- %s ---\n" /* Default format */
318 #define MESSAGE(fp, gn) \
319 fprintf(fp, targFmt, gn->name);
322 * When JobStart attempts to run a job but isn't allowed to
323 * or when Job_CatchChildren detects a job that has
324 * been stopped somehow, the job is placed on the stoppedJobs queue to be run
325 * when the next job finishes.
327 * Lst of Job structures describing jobs that were stopped due to
328 * concurrency limits or externally
330 static struct JobList stoppedJobs = TAILQ_HEAD_INITIALIZER(stoppedJobs);
332 static int fifoFd; /* Fd of our job fifo */
333 static char fifoName[] = "/tmp/make_fifo_XXXXXXXXX";
334 static int fifoMaster;
336 #if defined(USE_PGRP)
338 # define KILL(pid, sig) killpg(-(pid), (sig))
340 # define KILL(pid, sig) killpg((pid), (sig))
343 # define KILL(pid, sig) kill((pid), (sig))
347 * Grmpf... There is no way to set bits of the wait structure
348 * anymore with the stupid W*() macros. I liked the union wait
349 * stuff much more. So, we devise our own macros... This is
350 * really ugly, use dramamine sparingly. You have been warned.
352 #define W_SETMASKED(st, val, fun) \
355 int mask = fun(sh); \
357 for (sh = 0; ((mask >> sh) & 1) == 0; sh++) \
359 *(st) = (*(st) & ~mask) | ((val) << sh); \
362 #define W_SETTERMSIG(st, val) W_SETMASKED(st, val, WTERMSIG)
363 #define W_SETEXITSTATUS(st, val) W_SETMASKED(st, val, WEXITSTATUS)
365 typedef void AbortProc(const char [], ...);
367 static void JobRestart(Job *);
368 static int JobStart(GNode *, int, Job *);
369 static void JobDoOutput(Job *, bool);
370 static void JobRestartJobs(void);
371 static int Compat_RunCommand(GNode *, const char [], GNode *);
372 static void JobPassSig(int);
373 static void JobTouch(GNode *, bool);
374 static bool JobCheckCommands(GNode *, AbortProc *);
376 static GNode *curTarg = NULL;
378 static volatile sig_atomic_t got_SIGINT;
379 static volatile sig_atomic_t got_SIGHUP;
380 static volatile sig_atomic_t got_SIGQUIT;
381 static volatile sig_atomic_t got_SIGTERM;
382 #if defined(USE_PGRP)
383 static volatile sig_atomic_t got_SIGTSTP;
384 static volatile sig_atomic_t got_SIGTTOU;
385 static volatile sig_atomic_t got_SIGTTIN;
386 static volatile sig_atomic_t got_SIGWINCH;
391 * In lieu of a good way to prevent every possible looping in make(1), stop
392 * there from being more than MKLVL_MAXVAL processes forked by make(1), to
393 * prevent a forkbomb from happening, in a dumb and mechanical way.
396 * Creates or modifies environment variable MKLVL_ENVVAR via setenv().
399 check_make_level(void)
401 char *value = getenv(MKLVL_ENVVAR);
402 int level = (value == NULL) ? 0 : atoi(value);
405 errc(2, EAGAIN, "Invalid value for recursion level (%d).",
407 } else if (level > MKLVL_MAXVAL) {
408 errc(2, EAGAIN, "Max recursion level (%d) exceeded.",
412 sprintf(new_value, "%d", level + 1);
413 if (setenv(MKLVL_ENVVAR, new_value, 1) == -1)
414 Punt("setenv: %s: can't allocate memory", MKLVL_ENVVAR);
419 * Handle recept of a signal by setting a variable. The handling action is
420 * defered until the mainline code can safely handle it.
441 #if defined(USE_PGRP)
456 /* unexpected signal */
483 #if defined(USE_PGRP)
498 JobPassSig(SIGWINCH);
504 Sig_Init(bool compat)
512 #if defined(USE_PGRP)
519 if (compat == false) {
521 * Setup handler to catch SIGCHLD so that we get kicked out
522 * of select() when we need to look at a child. This is only
523 * known to matter for the -j case (perhaps without -P).
525 sigemptyset(&sa.sa_mask);
526 sa.sa_handler = SigCatcher;
527 sa.sa_flags = SA_NOCLDSTOP;
528 sigaction(SIGCHLD, &sa, NULL);
532 * Catch the four signals that POSIX specifies if they aren't
535 sigemptyset(&sa.sa_mask);
536 sa.sa_handler = SigCatcher;
539 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
540 sigaction(SIGINT, &sa, NULL);
542 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
543 sigaction(SIGHUP, &sa, NULL);
545 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
546 sigaction(SIGQUIT, &sa, NULL);
548 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
549 sigaction(SIGTERM, &sa, NULL);
552 #if defined(USE_PGRP)
553 if (compat == false) {
555 * There are additional signals that need to be caught and
556 * passed if either the export system wants to be told
557 * directly of signals or if we're giving each job its own
558 * process group (since then it won't get signals from the
559 * terminal driver as we own the terminal)
561 if (signal(SIGTSTP, SIG_IGN) != SIG_IGN) {
562 sigaction(SIGTSTP, &sa, NULL);
564 if (signal(SIGTTOU, SIG_IGN) != SIG_IGN) {
565 sigaction(SIGTTOU, &sa, NULL);
567 if (signal(SIGTTIN, SIG_IGN) != SIG_IGN) {
568 sigaction(SIGTTIN, &sa, NULL);
570 if (signal(SIGWINCH, SIG_IGN) != SIG_IGN) {
571 sigaction(SIGWINCH, &sa, NULL);
589 * get rid of resource limit on file descriptors
591 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) {
594 rl.rlim_cur = rl.rlim_max;
595 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
603 * Wait for child process to terminate.
606 ProcWait(ProcStuff *ps)
609 * Wait for the process to exit.
614 pid = waitpid(ps->child_pid, &ps->child_status, 0);
615 if (pid == ps->child_pid) {
617 * We finished waiting for the child.
621 if (errno == EINTR) {
623 * Return so we can handle the signal that
628 Fatal("error in wait: %d", pid);
636 * Execute the list of command associated with the node.
638 * @param save Commands preceeded by "..." are save in this node to be
639 * executed after the other rules are executed.
642 Compat_RunCmds(GNode *gn, GNode *save)
644 Lst *cmds = &gn->commands;
647 LST_FOREACH(ln, cmds) {
648 char *cmd = Lst_Datum(ln);
650 if (Compat_RunCommand(gn, cmd, save))
656 * Pass a signal on to all jobs.
659 * We die by the same signal.
662 JobPassSig(int signo)
666 DEBUGF(JOB, ("JobPassSig(%d) called.\n", signo));
669 * Propagate signal to children and in addition, send SIGCONT
670 * in case any of the children where suspended, so the the
671 * signal will get delivered.
673 TAILQ_FOREACH(job, &jobs, link) {
674 DEBUGF(JOB, ("JobPassSig passing signal %d to child %jd.\n",
675 signo, (intmax_t)job->pid));
676 KILL(job->pid, signo);
677 KILL(job->pid, SIGCONT);
680 #if defined(USE_PGRP)
682 * Why are we even catching these signals?
683 * SIGTSTP, SIGTTOU, SIGTTIN, and SIGWINCH
697 * Deal with proper cleanup based on the signal received. We only run
698 * the .INTERRUPT target if the signal was in fact an interrupt.
699 * The other three termination signals are more of a "get out *now*"
703 aborting = ABORT_INTERRUPT;
705 TAILQ_FOREACH(job, &jobs, link) {
706 if (!Targ_Precious(job->node)) {
707 char *file = (job->node->path == NULL ?
708 job->node->name : job->node->path);
710 if (!noExecute && eunlink(file) != -1) {
711 Error("*** %s removed", file);
716 if ((signo == SIGINT) && !touchFlag) {
719 interrupt = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
720 if (interrupt != NULL) {
721 ignoreErrors = false;
723 JobStart(interrupt, JOB_IGNDOTS, NULL);
726 Job_CatchChildren(!usePipes);
732 * Leave gracefully if SIGQUIT, rather than core dumping.
734 if (signo == SIGQUIT) {
738 DEBUGF(JOB, ("JobPassSig passing signal %d to self.\n", signo));
740 signal(signo, SIG_DFL);
741 KILL(getpid(), signo);
746 * Put out another command for the given job. If the command starts
747 * with an @ or a - we process it specially. In the former case,
748 * so long as the -s and -n flags weren't given to make, we stick
749 * a shell-specific echoOff command in the script. In the latter,
750 * we ignore errors for the entire job, unless the shell has error
752 * If the command is just "..." we take all future commands for this
753 * job to be commands to be executed once the entire graph has been
754 * made and return non-zero to signal that the end of the commands
755 * was reached. These commands are later attached to the postCommands
756 * node and executed by Job_Finish when all things are done.
757 * This function is called from JobStart via LST_FOREACH.
760 * Always 0, unless the command was "..."
763 * If the command begins with a '-' and the shell has no error control,
764 * the JOB_IGNERR flag is set in the job descriptor.
765 * If the command is "..." and we're not ignoring such things,
766 * tailCmds is set to the successor node of the cmd.
767 * numCommands is incremented if the command is actually printed.
770 JobPrintCommand(char *cmd, Job *job)
772 struct Shell *shell = job->shell;
773 bool noSpecials; /* true if we shouldn't worry about
774 * inserting special commands into
775 * the input stream. */
776 bool shutUp = false; /* true if we put a no echo command
777 * into the command file */
778 bool errOff = false; /* true if we turned error checking
779 * off before printing the command
780 * and need to turn it back on */
781 const char *cmdTemplate;/* Template to use when printing the command */
782 char *cmdStart; /* Start of expanded command */
783 LstNode *cmdNode; /* Node for replacing the command */
785 noSpecials = (noExecute && !(job->node->type & OP_MAKE));
787 if (strcmp(cmd, "...") == 0) {
788 job->node->type |= OP_SAVE_CMDS;
789 if ((job->flags & JOB_IGNDOTS) == 0) {
791 Lst_Succ(Lst_Member(&job->node->commands, cmd));
797 #define DBPRINTF(fmt, arg) \
798 DEBUGF(JOB, (fmt, arg)); \
799 fprintf(job->cmdFILE, fmt, arg); \
800 fflush(job->cmdFILE);
805 * For debugging, we replace each command with the result of expanding
806 * the variables in the command.
808 cmdNode = Lst_Member(&job->node->commands, cmd);
810 cmd = Buf_Peel(Var_Subst(cmd, job->node, false));
813 Lst_Replace(cmdNode, cmdStart);
815 cmdTemplate = "%s\n";
818 * Check for leading @', -' or +'s to control echoing, error checking,
819 * and execution on -n.
821 while (*cmd == '@' || *cmd == '-' || *cmd == '+') {
825 shutUp = DEBUG(LOUD) ? false : true;
835 * We're not actually executing anything...
836 * but this one needs to be - use compat mode
839 Compat_RunCommand(job->node, cmd, NULL);
847 while (isspace((unsigned char)*cmd))
851 if (!(job->flags & JOB_SILENT) && !noSpecials &&
853 DBPRINTF("%s\n", shell->echoOff);
860 if (!(job->flags & JOB_IGNERR) && !noSpecials) {
861 if (shell->hasErrCtl) {
863 * We don't want the error-control commands
864 * showing up either, so we turn off echoing
865 * while executing them. We could put another
866 * field in the shell structure to tell
867 * JobDoOutput to look for this string too,
868 * but why make it any more complex than
871 if (!(job->flags & JOB_SILENT) && !shutUp &&
873 DBPRINTF("%s\n", shell->echoOff);
874 DBPRINTF("%s\n", shell->ignErr);
875 DBPRINTF("%s\n", shell->echoOn);
877 DBPRINTF("%s\n", shell->ignErr);
879 } else if (shell->ignErr &&
880 *shell->ignErr != '\0') {
882 * The shell has no error control, so we need to
883 * be weird to get it to ignore any errors from
884 * the command. If echoing is turned on, we turn
885 * it off and use the errCheck template to echo
886 * the command. Leave echoing off so the user
887 * doesn't see the weirdness we go through to
888 * ignore errors. Set cmdTemplate to use the
889 * weirdness instead of the simple "%s\n"
892 if (!(job->flags & JOB_SILENT) && !shutUp &&
894 DBPRINTF("%s\n", shell->echoOff);
895 DBPRINTF(shell->errCheck, cmd);
898 cmdTemplate = shell->ignErr;
900 * The error ignoration (hee hee) is already
901 * taken care of by the ignErr template, so
902 * pretend error checking is still on.
913 DBPRINTF(cmdTemplate, cmd);
917 * If echoing is already off, there's no point in issuing the
918 * echoOff command. Otherwise we issue it and pretend it was on
919 * for the whole command...
921 if (!shutUp && !(job->flags & JOB_SILENT) &&
923 DBPRINTF("%s\n", shell->echoOff);
926 DBPRINTF("%s\n", shell->errCheck);
929 DBPRINTF("%s\n", shell->echoOn);
936 * Called to close both input and output pipes when a job is finished.
939 * The file descriptors associated with the job are closed.
946 FD_CLR(job->inPipe, &outputs);
947 if (job->outPipe != job->inPipe) {
950 JobDoOutput(job, true);
954 JobDoOutput(job, true);
960 * Do final processing for the given job including updating
961 * parents and starting new jobs as available/necessary. Note
962 * that we pay no attention to the JOB_IGNERR flag here.
963 * This is because when we're called because of a noexecute flag
964 * or something, jstat.w_status is 0 and when called from
965 * Job_CatchChildren, the status is zeroed if it s/b ignored.
968 * Some nodes may be put on the toBeMade queue.
969 * Final commands for the job are placed on postCommands.
971 * If we got an error and are aborting (aborting == ABORT_ERROR) and
972 * the job list is now empty, we are done for the day.
973 * If we recognized an error (errors !=0), we set the aborting flag
974 * to ABORT_ERROR so no more jobs will be started.
977 JobFinish(Job *job, int *status)
982 if (WIFEXITED(*status)) {
983 int job_status = WEXITSTATUS(*status);
987 * Deal with ignored errors in -B mode. We need to
988 * print a message telling of the ignored error as
989 * well as setting status.w_status to 0 so the next
990 * command gets run. To do this, we set done to be
991 * true if in -B mode and the job exited non-zero.
993 if (job_status == 0) {
996 if (job->flags & JOB_IGNERR) {
1000 * If it exited non-zero and either we're
1001 * doing things our way or we're not ignoring
1002 * errors, the job is finished. Similarly, if
1003 * the shell died because of a signal the job
1004 * is also finished. In these cases, finish
1005 * out the job's output before printing the
1009 if (job->cmdFILE != NULL &&
1010 job->cmdFILE != stdout) {
1011 fclose(job->cmdFILE);
1016 } else if (WIFSIGNALED(*status)) {
1017 if (WTERMSIG(*status) == SIGCONT) {
1019 * No need to close things down or anything.
1024 * If it exited non-zero and either we're
1025 * doing things our way or we're not ignoring
1026 * errors, the job is finished. Similarly, if
1027 * the shell died because of a signal the job
1028 * is also finished. In these cases, finish
1029 * out the job's output before printing the
1033 if (job->cmdFILE != NULL &&
1034 job->cmdFILE != stdout) {
1035 fclose(job->cmdFILE);
1041 * No need to close things down or anything.
1046 if (WIFEXITED(*status)) {
1047 if (done || DEBUG(JOB)) {
1052 (job->flags & JOB_IGNERR)) {
1054 * If output is going to a file and this job
1055 * is ignoring errors, arrange to have the
1056 * exit status sent to the output file as
1059 out = fdopen(job->outFd, "w");
1061 Punt("Cannot fdopen");
1066 DEBUGF(JOB, ("Process %jd exited.\n",
1067 (intmax_t)job->pid));
1069 if (WEXITSTATUS(*status) == 0) {
1071 if (usePipes && job->node != lastNode) {
1072 MESSAGE(out, job->node);
1073 lastNode = job->node;
1076 "*** Completed successfully\n");
1079 if (usePipes && job->node != lastNode) {
1080 MESSAGE(out, job->node);
1081 lastNode = job->node;
1083 fprintf(out, "*** Error code %d%s\n",
1084 WEXITSTATUS(*status),
1085 (job->flags & JOB_IGNERR) ?
1088 if (job->flags & JOB_IGNERR) {
1095 } else if (WIFSIGNALED(*status)) {
1096 if (done || DEBUG(JOB) || (WTERMSIG(*status) == SIGCONT)) {
1101 (job->flags & JOB_IGNERR)) {
1103 * If output is going to a file and this job
1104 * is ignoring errors, arrange to have the
1105 * exit status sent to the output file as
1108 out = fdopen(job->outFd, "w");
1110 Punt("Cannot fdopen");
1115 if (WTERMSIG(*status) == SIGCONT) {
1117 * If the beastie has continued, shift the
1118 * Job from the stopped list to the running
1119 * one (or re-stop it if concurrency is
1120 * exceeded) and go and get another child.
1122 if (job->flags & (JOB_RESUME | JOB_RESTART)) {
1123 if (usePipes && job->node != lastNode) {
1124 MESSAGE(out, job->node);
1125 lastNode = job->node;
1127 fprintf(out, "*** Continued\n");
1129 if (!(job->flags & JOB_CONTINUING)) {
1130 DEBUGF(JOB, ("Warning: process %jd was not "
1131 "continuing.\n", (intmax_t) job->pid));
1134 * We don't really want to restart a
1135 * job from scratch just because it
1136 * continued, especially not without
1137 * killing the continuing process!
1138 * That's why this is ifdef'ed out.
1144 job->flags &= ~JOB_CONTINUING;
1145 TAILQ_INSERT_TAIL(&jobs, job, link);
1147 DEBUGF(JOB, ("Process %jd is continuing locally.\n",
1148 (intmax_t) job->pid));
1149 if (nJobs == maxJobs) {
1151 DEBUGF(JOB, ("Job queue is full.\n"));
1157 if (usePipes && job->node != lastNode) {
1158 MESSAGE(out, job->node);
1159 lastNode = job->node;
1162 "*** Signal %d\n", WTERMSIG(*status));
1170 if (compatMake && !usePipes && (job->flags & JOB_IGNERR)) {
1172 * If output is going to a file and this job
1173 * is ignoring errors, arrange to have the
1174 * exit status sent to the output file as
1177 out = fdopen(job->outFd, "w");
1179 Punt("Cannot fdopen");
1184 DEBUGF(JOB, ("Process %jd stopped.\n", (intmax_t) job->pid));
1185 if (usePipes && job->node != lastNode) {
1186 MESSAGE(out, job->node);
1187 lastNode = job->node;
1189 fprintf(out, "*** Stopped -- signal %d\n", WSTOPSIG(*status));
1190 job->flags |= JOB_RESUME;
1191 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1197 * Now handle the -B-mode stuff. If the beast still isn't finished,
1198 * try and restart the job on the next command. If JobStart says it's
1199 * ok, it's ok. If there's an error, this puppy is done.
1201 if (compatMake && WIFEXITED(*status) &&
1202 Lst_Succ(job->node->compat_command) != NULL) {
1203 switch (JobStart(job->node, job->flags & JOB_IGNDOTS, job)) {
1209 W_SETEXITSTATUS(status, 1);
1213 * If we got back a JOB_FINISHED code, JobStart has
1214 * already called Make_Update and freed the job
1215 * descriptor. We set done to false here to avoid fake
1216 * cycles and double frees. JobStart needs to do the
1217 * update so we can proceed up the graph when given
1229 if (done && aborting != ABORT_ERROR &&
1230 aborting != ABORT_INTERRUPT && *status == 0) {
1232 * As long as we aren't aborting and the job didn't return a
1233 * non-zero status that we shouldn't ignore, we call
1234 * Make_Update to update the parents. In addition, any saved
1235 * commands for the node are placed on the .END target.
1237 for (ln = job->tailCmds; ln != NULL; ln = LST_NEXT(ln)) {
1238 Lst_AtEnd(&postCommands->commands,
1240 Var_Subst(Lst_Datum(ln), job->node, false)));
1243 job->node->made = MADE;
1244 Make_Update(job->node);
1247 } else if (*status != 0) {
1255 * Set aborting if any error.
1257 if (errors && !keepgoing && aborting != ABORT_INTERRUPT) {
1259 * If we found any errors in this batch of children and the -k
1260 * flag wasn't given, we set the aborting flag so no more jobs
1263 aborting = ABORT_ERROR;
1266 if (aborting == ABORT_ERROR && Job_Empty()) {
1268 * If we are aborting and the job table is now empty, we finish.
1276 * Touch the given target. Called by JobStart when the -t flag was
1277 * given. Prints messages unless told to be silent.
1280 * The data modification of the file is changed. In addition, if the
1281 * file did not exist, it is created.
1284 JobTouch(GNode *gn, bool silent)
1286 int streamID; /* ID of stream opened to do the touch */
1287 struct utimbuf times; /* Times for utime() call */
1289 if (gn->type & (OP_JOIN | OP_USE | OP_EXEC | OP_OPTIONAL)) {
1291 * .JOIN, .USE, .ZEROTIME and .OPTIONAL targets are "virtual"
1292 * targets and, as such, shouldn't really be created.
1298 fprintf(stdout, "touch %s\n", gn->name);
1306 if (gn->type & OP_ARCHV) {
1308 } else if (gn->type & OP_LIB) {
1311 char *file = gn->path ? gn->path : gn->name;
1313 times.actime = times.modtime = now;
1314 if (utime(file, ×) < 0) {
1315 streamID = open(file, O_RDWR | O_CREAT, 0666);
1317 if (streamID >= 0) {
1321 * Read and write a byte to the file to change
1322 * the modification time, then close the file.
1324 if (read(streamID, &c, 1) == 1) {
1325 lseek(streamID, (off_t)0, SEEK_SET);
1326 write(streamID, &c, 1);
1331 fprintf(stdout, "*** couldn't touch %s: %s",
1332 file, strerror(errno));
1341 * Make sure the given node has all the commands it needs.
1344 * true if the commands list is/was ok.
1347 * The node will have commands from the .DEFAULT rule added to it
1351 JobCheckCommands(GNode *gn, AbortProc *abortProc)
1353 const char *msg = "make: don't know how to make";
1355 if (!OP_NOP(gn->type)) {
1356 return (true); /* this node does nothing */
1359 if (!Lst_IsEmpty(&gn->commands)) {
1360 return (true); /* this node has no commands */
1363 if (gn->type & OP_LIB) {
1368 * No commands. Look for .DEFAULT rule from which we might infer
1371 if (DEFAULT != NULL && !Lst_IsEmpty(&DEFAULT->commands)) {
1373 * Make only looks for a .DEFAULT if the node was
1374 * never the target of an operator, so that's what we
1375 * do too. If a .DEFAULT was given, we substitute its
1376 * commands for gn's commands and set the IMPSRC
1377 * variable to be the target's name The DEFAULT node
1378 * acts like a transformation rule, in that gn also
1379 * inherits any attributes or sources attached to
1382 Make_HandleUse(DEFAULT, gn);
1383 Var_Set(IMPSRC, Var_Value(TARGET, gn), gn);
1387 if (Dir_MTime(gn) != 0) {
1392 * The node wasn't the target of an operator we have
1393 * no .DEFAULT rule to go on and the target doesn't
1394 * already exist. There's nothing more we can do for
1395 * this branch. If the -k flag wasn't given, we stop
1396 * in our tracks, otherwise we just don't update
1397 * this node's parents so they never get examined.
1399 if (gn->type & OP_OPTIONAL) {
1400 fprintf(stdout, "%s %s(ignored)\n", msg, gn->name);
1406 fprintf(stdout, "%s %s(continuing)\n", msg, gn->name);
1412 if (strcmp(gn->name,"love") == 0)
1413 abortProc("Not war.");
1416 abortProc("%s %s. Stop", msg, gn->name);
1423 * Execute the shell for the given job. Called from JobStart and
1427 * A shell is executed, outputs is altered and the Job structure added
1431 JobExec(Job *job, char **argv)
1433 struct Shell *shell = job->shell;
1439 DEBUGF(JOB, ("Running %s\n", job->node->name));
1440 DEBUGF(JOB, ("\tCommand: "));
1441 for (i = 0; argv[i] != NULL; i++) {
1442 DEBUGF(JOB, ("%s ", argv[i]));
1444 DEBUGF(JOB, ("\n"));
1448 * Some jobs produce no output and it's disconcerting to have
1449 * no feedback of their running (since they produce no output, the
1450 * banner with their name in it never appears). This is an attempt to
1451 * provide that feedback, even if nothing follows it.
1453 if (lastNode != job->node && (job->flags & JOB_FIRST) &&
1454 !(job->flags & JOB_SILENT)) {
1455 MESSAGE(stdout, job->node);
1456 lastNode = job->node;
1459 ps.in = FILENO(job->cmdFILE);
1462 * Set up the child's output to be routed through the
1463 * pipe we've created for it.
1465 ps.out = job->outPipe;
1468 * We're capturing output in a file, so we duplicate
1469 * the descriptor to the temporary file into the
1472 ps.out = job->outFd;
1474 ps.err = STDERR_FILENO;
1476 ps.merge_errors = 1;
1484 * Fork. Warning since we are doing vfork() instead of fork(),
1485 * do not allocate memory in the child process!
1487 if ((ps.child_pid = vfork()) == -1) {
1488 Punt("Cannot fork");
1490 } else if (ps.child_pid == 0) {
1497 Proc_Exec(&ps, shell);
1504 job->pid = ps.child_pid;
1506 if (usePipes && (job->flags & JOB_FIRST)) {
1508 * The first time a job is run for a node, we set the
1509 * current position in the buffer to the beginning and
1510 * mark another stream to watch in the outputs mask.
1513 FD_SET(job->inPipe, &outputs);
1516 if (job->cmdFILE != NULL && job->cmdFILE != stdout) {
1517 fclose(job->cmdFILE);
1518 job->cmdFILE = NULL;
1522 * Now the job is actually running, add it to the table.
1525 TAILQ_INSERT_TAIL(&jobs, job, link);
1526 if (nJobs == maxJobs) {
1534 * Create the argv needed to execute the shell for a given job.
1537 JobMakeArgv(Job *job, char **argv)
1539 struct Shell *shell = job->shell;
1541 static char args[10]; /* For merged arguments */
1543 argv[0] = shell->name;
1546 if ((shell->exit && *shell->exit != '-') ||
1547 (shell->echo && *shell->echo != '-')) {
1549 * At least one of the flags doesn't have a minus before it, so
1550 * merge them together. Have to do this because the *(&(@*#*&#$#
1551 * Bourne shell thinks its second argument is a file to source.
1552 * Grrrr. Note the ten-character limitation on the combined
1555 sprintf(args, "-%s%s", (job->flags & JOB_IGNERR) ? "" :
1556 shell->exit ? shell->exit : "",
1557 (job->flags & JOB_SILENT) ? "" :
1558 shell->echo ? shell->echo : "");
1565 if (!(job->flags & JOB_IGNERR) && shell->exit) {
1566 argv[argc] = shell->exit;
1569 if (!(job->flags & JOB_SILENT) && shell->echo) {
1570 argv[argc] = shell->echo;
1579 * Restart a job that stopped for some reason. The job must be neither
1580 * on the jobs nor on the stoppedJobs list.
1583 * jobFull will be set if the job couldn't be run.
1586 JobRestart(Job *job)
1589 if (job->flags & JOB_RESTART) {
1591 * Set up the control arguments to the shell. This is based on
1592 * the flags set earlier for this job. If the JOB_IGNERR flag
1593 * is clear, the 'exit' flag of the shell is used to
1594 * cause it to exit upon receiving an error. If the JOB_SILENT
1595 * flag is clear, the 'echo' flag of the shell is used
1596 * to get it to start echoing as soon as it starts
1597 * processing commands.
1601 JobMakeArgv(job, argv);
1603 DEBUGF(JOB, ("Restarting %s...", job->node->name));
1604 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL)) {
1606 * Not allowed to run -- put it back on the hold
1607 * queue and mark the table full
1609 DEBUGF(JOB, ("holding\n"));
1610 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1612 DEBUGF(JOB, ("Job queue is full.\n"));
1616 * Job may be run locally.
1618 DEBUGF(JOB, ("running locally\n"));
1624 * The job has stopped and needs to be restarted.
1625 * Why it stopped, we don't know...
1627 DEBUGF(JOB, ("Resuming %s...", job->node->name));
1628 if ((nJobs < maxJobs || ((job->flags & JOB_SPECIAL) &&
1629 maxJobs == 0)) && nJobs != maxJobs) {
1631 * If we haven't reached the concurrency limit already
1632 * (or the job must be run and maxJobs is 0), it's ok
1638 error = (KILL(job->pid, SIGCONT) != 0);
1642 * Make sure the user knows we've continued
1643 * the beast and actually put the thing in the
1646 job->flags |= JOB_CONTINUING;
1648 W_SETTERMSIG(&status, SIGCONT);
1649 JobFinish(job, &status);
1651 job->flags &= ~(JOB_RESUME|JOB_CONTINUING);
1652 DEBUGF(JOB, ("done\n"));
1654 Error("couldn't resume %s: %s",
1655 job->node->name, strerror(errno));
1657 W_SETEXITSTATUS(&status, 1);
1658 JobFinish(job, &status);
1662 * Job cannot be restarted. Mark the table as full and
1663 * place the job back on the list of stopped jobs.
1665 DEBUGF(JOB, ("table full\n"));
1666 TAILQ_INSERT_HEAD(&stoppedJobs, job, link);
1668 DEBUGF(JOB, ("Job queue is full.\n"));
1675 * Start a target-creation process going for the target described
1676 * by the graph node gn.
1679 * JOB_ERROR if there was an error in the commands, JOB_FINISHED
1680 * if there isn't actually anything left to do for the job and
1681 * JOB_RUNNING if the job has been started.
1684 * A new Job node is created and added to the list of running
1685 * jobs. PMake is forked and a child shell created.
1688 JobStart(GNode *gn, int flags, Job *previous)
1690 Job *job; /* new job descriptor */
1691 char *argv[4]; /* Argument vector to shell */
1692 bool cmdsOK; /* true if the nodes commands were all right */
1693 bool noExec; /* Set true if we decide not to run the job */
1694 int tfd; /* File descriptor for temp file */
1696 char tfile[sizeof(TMPPAT)];
1698 if (previous == NULL) {
1699 job = emalloc(sizeof(Job));
1702 previous->flags &= ~(JOB_FIRST | JOB_IGNERR | JOB_SILENT);
1706 job->shell = commandShell;
1708 job->tailCmds = NULL;
1711 * Set the initial value of the flags for this job based on the global
1712 * ones and the node's attributes... Any flags supplied by the caller
1713 * are also added to the field.
1716 if (Targ_Ignore(gn)) {
1717 job->flags |= JOB_IGNERR;
1719 if (Targ_Silent(gn)) {
1720 job->flags |= JOB_SILENT;
1722 job->flags |= flags;
1725 * Check the commands now so any attributes from .DEFAULT have a chance
1726 * to migrate to the node.
1728 if (!compatMake && (job->flags & JOB_FIRST)) {
1729 cmdsOK = JobCheckCommands(gn, Error);
1735 * If the -n flag wasn't given, we open up OUR (not the child's)
1736 * temporary file to stuff commands in it. The thing is rd/wr so we
1737 * don't need to reopen it to feed it to the shell. If the -n flag
1738 * *was* given, we just set the file to be stdout. Cute, huh?
1740 if ((gn->type & OP_MAKE) || (!noExecute && !touchFlag)) {
1742 * We're serious here, but if the commands were bogus, we're
1749 strcpy(tfile, TMPPAT);
1750 if ((tfd = mkstemp(tfile)) == -1)
1751 Punt("Cannot create temp file: %s", strerror(errno));
1752 job->cmdFILE = fdopen(tfd, "w+");
1754 if (job->cmdFILE == NULL) {
1756 Punt("Could not open %s", tfile);
1758 fcntl(FILENO(job->cmdFILE), F_SETFD, 1);
1760 * Send the commands to the command file, flush all its
1761 * buffers then rewind and remove the thing.
1766 * Used to be backwards; replace when start doing multiple
1767 * commands per shell.
1771 * Be compatible: If this is the first time for this
1772 * node, verify its commands are ok and open the
1773 * commands list for sequential access by later
1774 * invocations of JobStart. Once that is done, we take
1775 * the next command off the list and print it to the
1776 * command file. If the command was an ellipsis, note
1777 * that there's nothing more to execute.
1779 if (job->flags & JOB_FIRST)
1780 gn->compat_command = Lst_First(&gn->commands);
1782 gn->compat_command =
1783 Lst_Succ(gn->compat_command);
1785 if (gn->compat_command == NULL ||
1786 JobPrintCommand(Lst_Datum(gn->compat_command), job))
1789 if (noExec && !(job->flags & JOB_FIRST)) {
1791 * If we're not going to execute anything, the
1792 * job is done and we need to close down the
1793 * various file descriptors we've opened for
1794 * output, then call JobDoOutput to catch the
1795 * final characters or send the file to the
1796 * screen... Note that the i/o streams are only
1797 * open if this isn't the first job. Note also
1798 * that this could not be done in
1799 * Job_CatchChildren b/c it wasn't clear if
1800 * there were more commands to execute or not...
1806 * We can do all the commands at once. hooray for sanity
1809 LST_FOREACH(ln, &gn->commands) {
1810 if (JobPrintCommand(Lst_Datum(ln), job))
1815 * If we didn't print out any commands to the shell
1816 * script, there's not much point in executing the
1819 if (numCommands == 0) {
1824 } else if (noExecute) {
1826 * Not executing anything -- just print all the commands to
1827 * stdout in one fell swoop. This will still set up
1828 * job->tailCmds correctly.
1830 if (lastNode != gn) {
1831 MESSAGE(stdout, gn);
1834 job->cmdFILE = stdout;
1837 * Only print the commands if they're ok, but don't die if
1838 * they're not -- just let the user know they're bad and keep
1839 * going. It doesn't do any harm in this case and may do
1843 LST_FOREACH(ln, &gn->commands) {
1844 if (JobPrintCommand(Lst_Datum(ln), job))
1849 * Don't execute the shell, thank you.
1855 * Just touch the target and note that no shell should be
1856 * executed. Set cmdFILE to stdout to make life easier. Check
1857 * the commands, too, but don't die if they're no good -- it
1858 * does no harm to keep working up the graph.
1860 job->cmdFILE = stdout;
1861 JobTouch(gn, job->flags & JOB_SILENT);
1866 * If we're not supposed to execute a shell, don't.
1870 * Unlink and close the command file if we opened one
1872 if (job->cmdFILE != stdout) {
1873 if (job->cmdFILE != NULL)
1874 fclose(job->cmdFILE);
1880 * We only want to work our way up the graph if we aren't here
1881 * because the commands for the job were no good.
1884 if (aborting == 0) {
1885 for (ln = job->tailCmds; ln != NULL;
1886 ln = LST_NEXT(ln)) {
1887 Lst_AtEnd(&postCommands->commands,
1888 Buf_Peel(Var_Subst(Lst_Datum(ln),
1889 job->node, false)));
1891 job->node->made = MADE;
1892 Make_Update(job->node);
1895 return(JOB_FINISHED);
1901 fflush(job->cmdFILE);
1905 * Set up the control arguments to the shell. This is based on the flags
1906 * set earlier for this job.
1908 JobMakeArgv(job, argv);
1911 * If we're using pipes to catch output, create the pipe by which we'll
1912 * get the shell's output. If we're using files, print out that we're
1913 * starting a job and then set up its temporary-file name.
1915 if (!compatMake || (job->flags & JOB_FIRST)) {
1920 Punt("Cannot create pipe: %s", strerror(errno));
1921 job->inPipe = fd[0];
1922 job->outPipe = fd[1];
1923 fcntl(job->inPipe, F_SETFD, 1);
1924 fcntl(job->outPipe, F_SETFD, 1);
1926 fprintf(stdout, "Remaking `%s'\n", gn->name);
1928 strcpy(job->outFile, TMPPAT);
1929 if ((job->outFd = mkstemp(job->outFile)) == -1)
1930 Punt("cannot create temp file: %s",
1932 fcntl(job->outFd, F_SETFD, 1);
1936 if (nJobs >= maxJobs && !(job->flags & JOB_SPECIAL) && maxJobs != 0) {
1938 * We've hit the limit of concurrency, so put the job on hold
1939 * until some other job finishes. Note that the special jobs
1940 * (.BEGIN, .INTERRUPT and .END) may be run even when the
1941 * limit has been reached (e.g. when maxJobs == 0).
1945 DEBUGF(JOB, ("Can only run job locally.\n"));
1946 job->flags |= JOB_RESTART;
1947 TAILQ_INSERT_TAIL(&stoppedJobs, job, link);
1949 if (nJobs >= maxJobs) {
1951 * If we're running this job as a special case
1952 * (see above), at least say the table is full.
1955 DEBUGF(JOB, ("Local job queue is full.\n"));
1959 return (JOB_RUNNING);
1963 JobOutput(Job *job, char *cp, char *endp, int msg)
1965 struct Shell *shell = job->shell;
1968 if (shell->noPrint) {
1969 ecp = strstr(cp, shell->noPrint);
1970 while (ecp != NULL) {
1973 if (msg && job->node != lastNode) {
1974 MESSAGE(stdout, job->node);
1975 lastNode = job->node;
1978 * The only way there wouldn't be a newline
1979 * after this line is if it were the last in
1980 * the buffer. However, since the non-printable
1981 * comes after it, there must be a newline, so
1982 * we don't print one.
1984 fprintf(stdout, "%s", cp);
1987 cp = ecp + strlen(shell->noPrint);
1990 * Still more to print, look again after
1991 * skipping the whitespace following the
1992 * non-printable command....
1995 while (*cp == ' ' || *cp == '\t' ||
1999 ecp = strstr(cp, shell->noPrint);
2010 * This function is called at different times depending on
2011 * whether the user has specified that output is to be collected
2012 * via pipes or temporary files. In the former case, we are called
2013 * whenever there is something to read on the pipe. We collect more
2014 * output from the given job and store it in the job's outBuf. If
2015 * this makes up a line, we print it tagged by the job's identifier,
2017 * If output has been collected in a temporary file, we open the
2018 * file and read it line by line, transferring it to our own
2019 * output channel until the file is empty. At which point we
2020 * remove the temporary file.
2021 * In both cases, however, we keep our figurative eye out for the
2022 * 'noPrint' line for the shell from which the output came. If
2023 * we recognize a line, we don't print it. If the command is not
2024 * alone on the line (the character after it is not \0 or \n), we
2025 * do print whatever follows it.
2028 * curPos may be shifted as may the contents of outBuf.
2031 JobDoOutput(Job *job, bool finish)
2033 bool gotNL = false; /* true if got a newline */
2034 bool fbuf; /* true if our buffer filled up */
2035 int nr; /* number of bytes read */
2036 int i; /* auxiliary index into outBuf */
2037 int max; /* limit for i (end of current data) */
2038 int nRead; /* (Temporary) number of bytes read */
2039 FILE *oFILE; /* Stream pointer to shell's output file */
2044 * Read as many bytes as will fit in the buffer.
2050 nRead = read(job->inPipe, &job->outBuf[job->curPos],
2051 JOB_BUFSIZE - job->curPos);
2053 * Check for interrupt here too, because the above read may
2054 * block when the child process is stopped. In this case the
2055 * interrupt will unblock it (we don't use SA_RESTART).
2060 DEBUGF(JOB, ("JobDoOutput(piperead)"));
2067 * If we hit the end-of-file (the job is dead), we must flush
2068 * its remaining output, so pretend we read a newline if
2069 * there's any output remaining in the buffer.
2070 * Also clear the 'finish' flag so we stop looping.
2072 if (nr == 0 && job->curPos != 0) {
2073 job->outBuf[job->curPos] = '\n';
2076 } else if (nr == 0) {
2081 * Look for the last newline in the bytes we just got. If there
2082 * is one, break out of the loop with 'i' as its index and
2085 max = job->curPos + nr;
2086 for (i = job->curPos + nr - 1; i >= job->curPos; i--) {
2087 if (job->outBuf[i] == '\n') {
2090 } else if (job->outBuf[i] == '\0') {
2094 job->outBuf[i] = ' ';
2100 if (job->curPos == JOB_BUFSIZE) {
2102 * If we've run out of buffer space, we have
2103 * no choice but to print the stuff. sigh.
2109 if (gotNL || fbuf) {
2111 * Need to send the output to the screen. Null terminate
2112 * it first, overwriting the newline character if there
2113 * was one. So long as the line isn't one we should
2114 * filter (according to the shell description), we print
2115 * the line, preceded by a target banner if this target
2116 * isn't the same as the one for which we last printed
2117 * something. The rest of the data in the buffer are
2118 * then shifted down to the start of the buffer and
2119 * curPos is set accordingly.
2121 job->outBuf[i] = '\0';
2122 if (i >= job->curPos) {
2125 cp = JobOutput(job, job->outBuf,
2126 &job->outBuf[i], false);
2129 * There's still more in that buffer. This time,
2130 * though, we know there's no newline at the
2131 * end, so we add one of our own free will.
2134 if (job->node != lastNode) {
2135 MESSAGE(stdout, job->node);
2136 lastNode = job->node;
2138 fprintf(stdout, "%s%s", cp,
2144 /* shift the remaining characters down */
2145 memcpy(job->outBuf, &job->outBuf[i + 1],
2147 job->curPos = max - (i + 1);
2151 * We have written everything out, so we just
2152 * start over from the start of the buffer.
2153 * No copying. No nothing.
2160 * If the finish flag is true, we must loop until we hit
2161 * end-of-file on the pipe. This is guaranteed to happen
2162 * eventually since the other end of the pipe is now
2163 * closed (we closed it explicitly and the child has
2164 * exited). When we do get an EOF, finish will be set
2165 * false and we'll fall through and out.
2172 * We've been called to retrieve the output of the job from the
2173 * temporary file where it's been squirreled away. This consists
2174 * of opening the file, reading the output line by line, being
2175 * sure not to print the noPrint line for the shell we used,
2176 * then close and remove the temporary file. Very simple.
2178 * Change to read in blocks and do FindSubString type things
2179 * as for pipes? That would allow for "@echo -n..."
2181 oFILE = fopen(job->outFile, "r");
2182 if (oFILE != NULL) {
2183 fprintf(stdout, "Results of making %s:\n",
2187 while (fgets(inLine, sizeof(inLine), oFILE) != NULL) {
2188 char *cp, *endp, *oendp;
2191 oendp = endp = inLine + strlen(inLine);
2192 if (endp[-1] == '\n') {
2195 cp = JobOutput(job, inLine, endp, false);
2198 * There's still more in that buffer. This time,
2199 * though, we know there's no newline at the
2200 * end, so we add one of our own free will.
2202 fprintf(stdout, "%s", cp);
2204 if (endp != oendp) {
2205 fprintf(stdout, "\n");
2210 eunlink(job->outFile);
2217 * Handle the exit of a child. Called from Make_Make.
2220 * The job descriptor is removed from the list of children.
2223 * We do waits, blocking or not, according to the wisdom of our
2224 * caller, until there are no more children to report. For each
2225 * job, call JobFinish to finish things off. This will take care of
2226 * putting jobs on the stoppedJobs queue.
2229 Job_CatchChildren(bool block)
2231 pid_t pid; /* pid of dead child */
2232 Job *job; /* job descriptor for dead child */
2233 int status; /* Exit/termination status */
2236 * Don't even bother if we know there's no one around.
2243 pid = waitpid((pid_t)-1, &status,
2244 (block ? 0 : WNOHANG) | WUNTRACED);
2248 DEBUGF(JOB, ("Process %jd exited or stopped.\n",
2251 TAILQ_FOREACH(job, &jobs, link) {
2252 if (job->pid == pid)
2257 if (WIFSIGNALED(status) &&
2258 (WTERMSIG(status) == SIGCONT)) {
2259 TAILQ_FOREACH(job, &jobs, link) {
2260 if (job->pid == pid)
2264 Error("Resumed child (%jd) "
2265 "not in table", (intmax_t)pid);
2268 TAILQ_REMOVE(&stoppedJobs, job, link);
2270 Error("Child (%jd) not in table?",
2275 TAILQ_REMOVE(&jobs, job, link);
2277 if (fifoFd >= 0 && maxJobs > 1) {
2278 write(fifoFd, "+", 1);
2280 if (nJobs >= maxJobs)
2285 DEBUGF(JOB, ("Job queue is no longer full.\n"));
2290 JobFinish(job, &status);
2297 * Catch the output from our children, if we're using
2298 * pipes do so. Otherwise just block time until we get a
2299 * signal(most likely a SIGCHLD) since there's no point in
2300 * just spinning when there's nothing to do and the reaping
2301 * of a child can wait for a while.
2304 * Output is read from pipes if we're piping.
2307 Job_CatchOutput(int flag)
2310 struct timeval timeout;
2318 timeout.tv_sec = SEL_SEC;
2319 timeout.tv_usec = SEL_USEC;
2320 if (flag && jobFull && fifoFd >= 0)
2321 FD_SET(fifoFd, &readfds);
2323 nfds = select(FD_SETSIZE, &readfds, NULL, NULL, &timeout);
2325 /* timeout expired */
2328 } else if (nfds < 0) {
2333 if (fifoFd >= 0 && FD_ISSET(fifoFd, &readfds)) {
2337 job = TAILQ_FIRST(&jobs);
2338 while (nfds != 0 && job != NULL) {
2339 if (FD_ISSET(job->inPipe, &readfds)) {
2340 JobDoOutput(job, false);
2343 job = TAILQ_NEXT(job, link);
2351 * Start the creation of a target. Basically a front-end for
2352 * JobStart used by the Make module.
2355 * Another job is started.
2361 JobStart(gn, 0, NULL);
2366 * Initialize the process module, given a maximum number of jobs.
2369 * lists and counters are initialized
2372 Job_Init(int maxproc)
2374 GNode *begin; /* node for commands to do at the very start */
2378 env = getenv("MAKE_JOBS_FIFO");
2380 if (env == NULL && maxproc > 1) {
2382 * We did not find the environment variable so we are the
2383 * leader. Create the fifo, open it, write one char per
2384 * allowed job into the pipe.
2386 fifoFd = mkfifotemp(fifoName);
2391 fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2393 if (setenv("MAKE_JOBS_FIFO", env, 1) == -1)
2394 Punt("setenv: MAKE_JOBS_FIFO: can't allocate memory");
2395 while (maxproc-- > 0) {
2396 write(fifoFd, "+", 1);
2398 /* The master make does not get a magic token */
2403 } else if (env != NULL) {
2405 * We had the environment variable so we are a slave.
2406 * Open fifo and give ourselves a magic token which represents
2407 * the token our parent make has grabbed to start his make
2408 * process. Otherwise the sub-makes would gobble up tokens and
2409 * the proper number of tokens to specify to -j would depend
2410 * on the depth of the tree and the order of execution.
2412 fifoFd = open(env, O_RDWR, 0);
2414 fcntl(fifoFd, F_SETFL, O_NONBLOCK);
2431 if ((maxJobs == 1 && fifoFd < 0) || beVerbose == 0) {
2433 * If only one job can run at a time, there's no need for a
2434 * banner, no is there?
2441 begin = Targ_FindNode(".BEGIN", TARG_NOCREATE);
2443 if (begin != NULL) {
2444 JobStart(begin, JOB_SPECIAL, NULL);
2447 Job_CatchChildren(!usePipes);
2450 postCommands = Targ_FindNode(".END", TARG_CREATE);
2455 * See if the job table is full. It is considered full if it is OR
2456 * if we are in the process of aborting OR if we have
2457 * reached/exceeded our local quota. This prevents any more jobs
2461 * true if the job table is full, false otherwise
2471 if (fifoFd >= 0 && jobFull) {
2472 i = read(fifoFd, &c, 1);
2483 * See if the job table is empty. Because the local concurrency may
2484 * be set to 0, it is possible for the job table to become empty,
2485 * while the list of stoppedJobs remains non-empty. In such a case,
2486 * we want to restart as many jobs as we can.
2489 * true if it is. false if it ain't.
2496 if (!TAILQ_EMPTY(&stoppedJobs) && !aborting) {
2498 * The job table is obviously not full if it has no
2499 * jobs in it...Try and restart the stopped jobs.
2514 * Do final processing such as the running of the commands
2515 * attached to the .END target.
2518 * Number of errors reported.
2524 if (postCommands != NULL && !Lst_IsEmpty(&postCommands->commands)) {
2526 Error("Errors reported so .END ignored");
2528 JobStart(postCommands, JOB_SPECIAL | JOB_IGNDOTS, NULL);
2532 Job_CatchChildren(!usePipes);
2547 * Waits for all running jobs to finish and returns. Sets 'aborting'
2548 * to ABORT_WAIT to prevent other jobs from starting.
2551 * Currently running jobs finish.
2557 aborting = ABORT_WAIT;
2558 while (nJobs != 0) {
2560 Job_CatchChildren(!usePipes);
2567 * Abort all currently running jobs without handling output or anything.
2568 * This function is to be called only in the event of a major
2572 * All children are killed, not just the firstborn
2577 Job *job; /* the job descriptor in that element */
2580 aborting = ABORT_ERROR;
2583 TAILQ_FOREACH(job, &jobs, link) {
2585 * kill the child process with increasingly drastic
2586 * signals to make darn sure it's dead.
2588 KILL(job->pid, SIGINT);
2589 KILL(job->pid, SIGKILL);
2594 * Catch as many children as want to report in at first, then give up
2596 while (waitpid((pid_t)-1, &status, WNOHANG) > 0)
2602 * Tries to restart stopped jobs if there are slots available.
2603 * Note that this tries to restart them regardless of pending errors.
2604 * It's not good to leave stopped jobs lying around!
2607 * Resumes(and possibly migrates) jobs.
2610 JobRestartJobs(void)
2614 while (!jobFull && (job = TAILQ_FIRST(&stoppedJobs)) != NULL) {
2615 DEBUGF(JOB, ("Job queue is not full. "
2616 "Restarting a stopped job.\n"));
2617 TAILQ_REMOVE(&stoppedJobs, job, link);
2624 * Execute the command in cmd, and return the output of that command
2628 * A string containing the output of the command, or the empty string
2629 * If error is not NULL, it contains the reason for the command failure
2630 * Any output sent to stderr in the child process is passed to stderr,
2631 * and not captured in the string.
2634 * The string must be freed by the caller.
2637 Cmd_Exec(const char *cmd, const char **error)
2639 struct Shell *shell = commandShell;
2640 int fds[2]; /* Pipe streams */
2641 Buffer *buf; /* buffer to store the result */
2649 * Open a pipe for fetching its output
2651 if (pipe(fds) == -1) {
2652 *error = "Couldn't create pipe for \"%s\"";
2656 /* Set close-on-exec on read side of pipe. */
2657 fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC);
2659 ps.in = STDIN_FILENO;
2661 ps.err = STDERR_FILENO;
2663 ps.merge_errors = 0;
2667 /* Set up arguments for shell */
2668 ps.argv = emalloc(4 * sizeof(char *));
2669 ps.argv[0] = strdup(shell->name);
2670 ps.argv[1] = strdup("-c");
2671 ps.argv[2] = strdup(cmd);
2676 * Fork. Warning since we are doing vfork() instead of fork(),
2677 * do not allocate memory in the child process!
2679 if ((ps.child_pid = vfork()) == -1) {
2680 *error = "Couldn't exec \"%s\"";
2682 } else if (ps.child_pid == 0) {
2686 Proc_Exec(&ps, shell);
2695 close(fds[1]); /* No need for the writing half of the pipe. */
2698 char result[BUFSIZ];
2700 rcnt = read(fds[0], result, sizeof(result));
2702 Buf_AddBytes(buf, (size_t)rcnt, result);
2703 } while (rcnt > 0 || (rcnt == -1 && errno == EINTR));
2706 *error = "Error reading shell's output for \"%s\"";
2709 * Close the input side of the pipe.
2714 if (ps.child_status)
2715 *error = "\"%s\" returned non-zero status";
2717 Buf_StripNewlines(buf);
2724 * Handle interrupts during the creation of the target and remove
2725 * it if it ain't precious. The default handler for the signal is
2726 * reinstalled, and the signal is raised again.
2729 * The target is removed and the process exits. If the cause was SIGINT
2730 * and .INTERRUPT: exists its commands are run.
2733 CompatInterrupt(void)
2742 } else if (got_SIGHUP) {
2746 } else if (got_SIGQUIT) {
2750 } else if (got_SIGTERM) {
2755 return; /* no signal delivered */
2758 if (curTarg != NULL && !Targ_Precious(curTarg)) {
2759 const char *file = Var_Value(TARGET, curTarg);
2761 if (!noExecute && eunlink(file) != -1) {
2762 printf("*** %s removed\n", file);
2767 * Run .INTERRUPT only if hit with interrupt signal
2769 if (signo == SIGINT) {
2770 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
2772 Compat_RunCmds(gn, NULL);
2776 if (signo == SIGQUIT) {
2778 * We do not raise SIGQUIT, since on systems that create core
2779 * files upon receipt of SIGQUIT, the core from make would
2780 * conflict with a core file from the command that was
2781 * running when the SIGQUIT arrived.
2783 * This is true even on BSD systems that name the core file
2784 * after the program, since we might be calling make
2790 signal(signo, SIG_DFL);
2791 kill(getpid(), signo);
2797 * Execute the next command for a target. If the command returns an
2798 * error, the node's made field is set to ERROR and creation stops.
2799 * The node from which the command came is also given. This is used
2800 * to execute the commands in compat mode and when executing commands
2801 * with the '+' flag in non-compat mode. In these modes each command
2802 * line should be executed by its own shell. We do some optimization here:
2803 * if the shell description defines both a string of meta characters and
2804 * a list of builtins and the command line neither contains a meta character
2805 * nor starts with one of the builtins then we execute the command directly
2806 * without invoking a shell.
2809 * 0 if the command succeeded, 1 if an error occurred.
2812 * The node's 'made' field may be set to ERROR.
2815 Compat_RunCommand(GNode *gn, const char cmd[], GNode *ENDNode)
2817 struct Shell *shell = commandShell;
2819 char *cmdStart; /* Start of expanded command */
2820 bool silent; /* Don't print command */
2821 bool doit; /* Execute even in -n */
2822 bool errCheck; /* Check errors */
2823 int status; /* Description of child's death */
2824 LstNode *cmdNode; /* Node where current cmd is located */
2825 char **av; /* Argument vector for thing to exec */
2829 cmdNode = Lst_Member(&gn->commands, cmd);
2831 cmdStart = Buf_Peel(Var_Subst(cmd, gn, false));
2832 if (cmdStart[0] == '\0') {
2834 Error("%s expands to empty string", cmd);
2838 Lst_Replace(cmdNode, cmdStart);
2840 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
2841 if (ENDNode != NULL) {
2842 Lst_AtEnd(&ENDNode->commands, cmdStart);
2847 if (strcmp(cmdStart, "...") == 0) {
2848 gn->type |= OP_SAVE_CMDS;
2849 return (0); /* any further commands are deferred */
2853 silent = gn->type & OP_SILENT;
2855 errCheck = !(gn->type & OP_IGNORE);
2857 while (*line == '@' || *line == '-' || *line == '+') {
2861 silent = DEBUG(LOUD) ? false : true;
2875 while (isspace((unsigned char)*line))
2878 if (noExecute && doit == false) {
2879 /* Just print out the command */
2880 printf("%s\n", line);
2885 if (silent == false) {
2887 * Print the command before echoing if we're not supposed to
2888 * be quiet for this one.
2890 printf("%s\n", line);
2894 if (shell->meta == NULL || strpbrk(line, shell->meta) == NULL) {
2896 char **sh_builtin = shell->builtins.argv + 1;
2899 * Break the command into words to form an argument
2900 * vector we can execute.
2902 brk_string(&aa, line, true);
2905 for (p = sh_builtin; *p != 0; p++) {
2906 if (strcmp(av[0], *p) == 0) {
2908 * This command must be passed by the shell
2909 * for other reasons.. or.. possibly not at
2918 * We found a "meta" character and need to pass the command
2924 ps.in = STDIN_FILENO;
2925 ps.out = STDOUT_FILENO;
2926 ps.err = STDERR_FILENO;
2928 ps.merge_errors = 0;
2934 * We give the shell the -e flag as well as -c if it's
2935 * supposed to exit when it hits an error.
2937 ps.argv = emalloc(4 * sizeof(char *));
2938 ps.argv[0] = strdup(shell->path);
2939 ps.argv[1] = strdup(errCheck ? "-ec" : "-c");
2940 ps.argv[2] = strdup(line);
2947 ps.errCheck = errCheck;
2950 * Fork and execute the single command. If the fork fails, we abort.
2951 * Warning since we are doing vfork() instead of fork(),
2952 * do not allocate memory in the child process!
2954 if ((ps.child_pid = vfork()) == -1) {
2955 Fatal("Could not fork");
2957 } else if (ps.child_pid == 0) {
2961 Proc_Exec(&ps, shell);
2975 * we need to print out the command associated with this
2976 * Gnode in Targ_PrintCmd from Targ_PrintGraph when debugging
2977 * at level g2, in main(), Fatal() and DieHorribly(),
2978 * therefore do not free it when debugging.
2980 if (!DEBUG(GRAPH2)) {
2985 * The child is off and running. Now all we can do is wait...
2986 * Block until child has terminated, or a signal is received.
2988 while (ProcWait(&ps) < 0) {
2989 CompatInterrupt(); /* check on the signal */
2993 * Decode and report the reason child exited, then
2994 * indicate how we handled it.
2996 if (WIFEXITED(ps.child_status)) {
2997 status = WEXITSTATUS(ps.child_status);
3001 printf("*** Error code %d", status);
3003 } else if (WIFSTOPPED(ps.child_status)) {
3004 /* can't happen since WUNTRACED isn't set */
3005 status = WSTOPSIG(ps.child_status);
3007 status = WTERMSIG(ps.child_status);
3008 printf("*** Signal %d", status);
3019 printf(" (continuing)\n");
3024 * Continue executing
3025 * commands for this target.
3026 * If we return 0, this will
3029 printf(" (ignored)\n");
3037 * Make a target, given the parent, to abort if necessary.
3040 * If an error is detected and not being ignored, the process exits.
3043 CompatMake(GNode *gn, GNode *pgn, GNode *ENDNode, bool queryFlag)
3047 if (gn->type & OP_USE) {
3048 Make_HandleUse(gn, pgn);
3050 } else if (gn->made == UNMADE) {
3052 * First mark ourselves to be made, then apply whatever
3053 * transformations the suffix module thinks are necessary.
3054 * Once that's done, we can descend and make all our children.
3055 * If any of them has an error but the -k flag was given, our
3056 * 'make' field will be set false again. This is our signal to
3057 * not attempt to do anything but abort our parent as well.
3060 gn->made = BEINGMADE;
3062 LST_FOREACH(ln, &gn->children)
3063 CompatMake(Lst_Datum(ln), gn, ENDNode, queryFlag);
3070 if (Lst_Member(&gn->iParents, pgn) != NULL) {
3071 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn);
3075 * All the children were made ok. Now cmtime contains the
3076 * modification time of the newest child, we need to find out
3077 * if we exist and when we were modified last. The criteria for
3078 * datedness are defined by the Make_OODate function.
3080 DEBUGF(MAKE, ("Examining %s...", gn->name));
3081 if (!Make_OODate(gn)) {
3082 gn->made = UPTODATE;
3083 DEBUGF(MAKE, ("up-to-date.\n"));
3086 DEBUGF(MAKE, ("out-of-date.\n"));
3090 * If the user is just seeing if something is out-of-date,
3091 * exit now to tell him/her "yes".
3098 * We need to be re-made. We also have to make sure we've got
3099 * a $? variable. To be nice, we also define the $> variable
3100 * using Make_DoAllVar().
3105 * Alter our type to tell if errors should be ignored or things
3106 * should not be printed so Compat_RunCommand knows what to do.
3108 if (Targ_Ignore(gn)) {
3109 gn->type |= OP_IGNORE;
3111 if (Targ_Silent(gn)) {
3112 gn->type |= OP_SILENT;
3115 if (JobCheckCommands(gn, Fatal)) {
3117 * Our commands are ok, but we still have to worry
3118 * about the -t flag...
3121 JobTouch(gn, gn->type & OP_SILENT);
3124 Compat_RunCmds(gn, ENDNode);
3131 if (gn->made != ERROR) {
3133 * If the node was made successfully, mark it so, update
3134 * its modification time and timestamp all its parents.
3135 * Note that for .ZEROTIME targets, the timestamping
3136 * isn't done. This is to keep its state from affecting
3137 * that of its parent.
3142 * We can't re-stat the thing, but we can at least take
3143 * care of rules where a target depends on a source that
3144 * actually creates the target, but only if it has
3152 * mv y.tab.o parse.o
3153 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
3155 * In this case, if the definitions produced by yacc
3156 * haven't changed from before, parse.h won't have been
3157 * updated and gn->mtime will reflect the current
3158 * modification time for parse.h. This is something of a
3159 * kludge, I admit, but it's a useful one..
3161 * XXX: People like to use a rule like
3165 * To force things that depend on FRC to be made, so we
3166 * have to check for gn->children being empty as well...
3168 if (!Lst_IsEmpty(&gn->commands) ||
3169 Lst_IsEmpty(&gn->children)) {
3174 * This is what Make does and it's actually a good
3175 * thing, as it allows rules like
3177 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
3179 * to function as intended. Unfortunately, thanks to
3180 * the stateless nature of NFS (and the speed of this
3181 * program), there are times when the modification time
3182 * of a file created on a remote machine will not be
3183 * modified before the stat() implied by the Dir_MTime
3184 * occurs, thus leading us to believe that the file
3185 * is unchanged, wrecking havoc with files that depend
3188 * I have decided it is better to make too much than to
3189 * make too little, so this stuff is commented out
3190 * unless you're sure it's ok.
3193 if (noExecute || Dir_MTime(gn) == 0) {
3196 if (gn->cmtime > gn->mtime)
3197 gn->mtime = gn->cmtime;
3198 DEBUGF(MAKE, ("update time: %s\n",
3199 Targ_FmtTime(gn->mtime)));
3201 if (!(gn->type & OP_EXEC)) {
3202 pgn->childMade = true;
3203 Make_TimeStamp(pgn, gn);
3206 } else if (keepgoing) {
3210 printf("\n\nStop in %s.\n", Var_Value(".CURDIR", gn));
3213 } else if (gn->made == ERROR) {
3215 * Already had an error when making this beastie. Tell the
3220 if (Lst_Member(&gn->iParents, pgn) != NULL) {
3221 Var_Set(IMPSRC, Var_Value(TARGET, gn), pgn);
3225 Error("Graph cycles through %s\n", gn->name);
3230 if ((gn->type & OP_EXEC) == 0) {
3231 pgn->childMade = true;
3232 Make_TimeStamp(pgn, gn);
3236 if ((gn->type & OP_EXEC) == 0) {
3237 Make_TimeStamp(pgn, gn);
3247 * Start making given a list of target nodes. Returns what the exit
3248 * status of make should be.
3250 * @note Obviously some function we call is exiting since the code only
3251 * returns 0. We will fix that bug eventually.
3254 Compat_Run(Lst *targs, bool queryFlag)
3256 int error_cnt; /* Number of targets not remade due to errors */
3259 deferred = Targ_NewGN("Deferred");
3262 * If the user has defined a .BEGIN target, execute the commands
3265 if (queryFlag == false) {
3266 GNode *gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
3268 Compat_RunCmds(gn, deferred);
3269 if (gn->made == ERROR) {
3270 printf("\n\nStop.\n");
3271 return (1); /* Failed .BEGIN target */
3277 * For each entry in the list of targets to create, call CompatMake on
3278 * it to create the thing. CompatMake will leave the 'made' field of gn
3279 * in one of several states:
3280 * UPTODATE gn was already up-to-date
3281 * MADE gn was recreated successfully
3282 * ERROR An error occurred while gn was being created
3283 * ABORTED gn was not remade because one of its inferiors
3284 * could not be made due to errors.
3287 while (!Lst_IsEmpty(targs)) {
3288 GNode *gn = Lst_DeQueue(targs);
3290 CompatMake(gn, gn, deferred, queryFlag);
3291 if (gn->made == UPTODATE) {
3292 printf("`%s' is up to date.\n", gn->name);
3293 } else if (gn->made == ABORTED) {
3294 printf("`%s' not remade because of errors.\n",
3300 if ((error_cnt == 0) && (queryFlag == false)) {
3304 * If the user has deferred commands using "..." run them.
3306 Compat_RunCmds(deferred, NULL);
3307 if (deferred->made == ERROR) {
3308 printf("\n\nStop.\n");
3309 return (1); /* Failed "deferred" target */
3313 * If the user has defined a .END target, run its commands.
3315 gn = Targ_FindNode(".END", TARG_NOCREATE);
3317 Compat_RunCmds(gn, NULL);
3318 if (gn->made == ERROR) {
3319 printf("\n\nStop.\n");
3320 return (1); /* Failed .END target */
3325 return (0); /* Successful completion */