2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * make1.c - execute command to bring targets up to date
10 * This module contains make1(), the entry point called by make() to
11 * recursively decend the dependency graph executing update actions as
16 * make1() - execute commands to update a TARGET and all its dependents
18 * Internal routines, the recursive/asynchronous command executors:
20 * make1a() - recursively traverse target tree, calling make1b()
21 * make1b() - dependents of target built, now build target with make1c()
22 * make1c() - launch target's next command, call make1b() when done
23 * make1d() - handle command execution completion and call back make1c()
25 * Internal support routines:
27 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
28 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
29 * make1settings() - for vars that get bound, build up replacement lists
30 * make1bind() - bind targets that weren't bound in dependency analysis
32 * 04/16/94 (seiwald) - Split from make.c.
33 * 04/21/94 (seiwald) - Handle empty "updated" actions.
34 * 05/04/94 (seiwald) - async multiprocess (-j) support
35 * 06/01/94 (seiwald) - new 'actions existing' does existing sources
36 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
37 * 01/19/95 (seiwald) - distinguish between CANTFIND/CANTMAKE targets.
38 * 01/22/94 (seiwald) - pass per-target JAMSHELL down to execcmd().
39 * 02/28/95 (seiwald) - Handle empty "existing" actions.
40 * 03/10/95 (seiwald) - Fancy counts.
41 * 02/07/01 (seiwald) - Fix jam -d0 return status.
42 * 01/21/02 (seiwald) - new -q to quit quickly on build failure
43 * 02/28/02 (seiwald) - don't delete 'actions updated' targets on failure
44 * 02/28/02 (seiwald) - merge EXEC_xxx flags in with RULE_xxx
45 * 07/17/02 (seiwald) - TEMPORARY sources for headers now get built
46 * 09/23/02 (seiwald) - "...using temp..." only displayed on -da now.
47 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
48 * 11/04/02 (seiwald) - const-ing for string literals
49 * 12/03/02 (seiwald) - fix odd includes support by grafting them onto depends
58 # include "variable.h"
67 static void make1a (TARGET
*t
, TARGET
*parent
);
68 static void make1b (TARGET
*t
);
69 static void make1c (TARGET
*t
);
70 static void make1d (void *closure
, int status
);
72 static CMD
*make1cmds (ACTIONS
*a0
);
73 static LIST
*make1list (LIST
*l
, TARGETS
*targets
, int flags
);
74 static SETTINGS
*make1settings (LIST
*vars
);
75 static void make1bind (TARGET
*t
, int warn
);
77 /* Ugly static - it's too hard to carry it through the callbacks. */
88 * make1() - execute commands to update a TARGET and all its dependents
91 int make1 (TARGET
*t
) {
92 memset((char *)counts
, 0, sizeof(*counts
));
93 /* Recursively make the target and its dependents */
94 make1a(t
, (TARGET
*)0);
95 /* Wait for any outstanding commands to finish running. */
98 if (DEBUG_MAKE
&& counts
->failed
) printf("...failed updating %d target(s)...\n", counts
->failed
);
99 if (DEBUG_MAKE
&& counts
->skipped
) printf("...skipped %d target(s)...\n", counts
->skipped
);
100 if (DEBUG_MAKE
&& counts
->made
) printf("...updated %d target(s)...\n", counts
->made
);
101 return counts
->total
!= counts
->made
;
106 * make1a() - recursively traverse target tree, calling make1b()
108 static void make1a (TARGET
*t
, TARGET
*parent
) {
110 /* If the parent is the first to try to build this target */
111 /* or this target is in the make1c() quagmire, arrange for the */
112 /* parent to be notified when this target is built. */
114 switch (t
->progress
) {
115 case T_MAKE_INIT
: case T_MAKE_ACTIVE
: case T_MAKE_RUNNING
:
116 t
->parents
= targetentry(t
->parents
, parent
);
121 if (t
->progress
!= T_MAKE_INIT
) return;
122 /* Asynccnt counts the dependents preventing this target from */
123 /* proceeding to make1b() for actual building. We start off with */
124 /* a count of 1 to prevent anything from happening until we can */
125 /* call all dependents. This 1 is accounted for when we call */
126 /* make1b() ourselves, below. */
128 /* Recurse on our dependents, manipulating progress to guard */
129 /* against circular dependency. */
130 t
->progress
= T_MAKE_ONSTACK
;
131 for (c
= t
->depends
; c
&& !intr
; c
= c
->next
) make1a(c
->target
, t
);
132 t
->progress
= T_MAKE_ACTIVE
;
133 /* Now that all dependents have bumped asynccnt, we now allow */
134 /* decrement our reference to asynccnt. */
140 * make1b() - dependents of target built, now build target with make1c()
142 static void make1b (TARGET
*t
) {
144 const char *failed
= "dependents";
146 /* If any dependents are still outstanding, wait until they */
147 /* call make1b() to signal their completion. */
148 if (--t
->asynccnt
) return;
149 /* Now ready to build target 't'... if dependents built ok. */
150 /* Collect status from dependents */
151 for (c
= t
->depends
; c
; c
= c
->next
) {
152 if (c
->target
->status
> t
->status
) {
153 failed
= c
->target
->name
;
154 t
->status
= c
->target
->status
;
157 /* If actions on deps have failed, bail. */
158 /* Otherwise, execute all actions to make target */
159 if (t
->status
== EXEC_CMD_FAIL
&& t
->actions
) {
161 printf("...skipped %s for lack of %s...\n", t
->name
, failed
);
163 if (t
->status
== EXEC_CMD_OK
) {
165 case T_FATE_INIT
: case T_FATE_MAKING
:
166 /* shouldn't happen */
167 case T_FATE_STABLE
: case T_FATE_NEWER
:
169 case T_FATE_CANTFIND
: case T_FATE_CANTMAKE
:
170 t
->status
= EXEC_CMD_FAIL
;
173 if (DEBUG_MAKEQ
) printf("...using %s...\n", t
->name
);
175 case T_FATE_TOUCHED
: case T_FATE_MISSING
: case T_FATE_NEEDTMP
:
176 case T_FATE_OUTDATED
: case T_FATE_UPDATE
:
177 /* Set "on target" vars, build actions, unset vars */
178 /* Set "progress" so that make1c() counts this target among */
179 /* the successes/failures. */
182 if (DEBUG_MAKE
&& !(counts
->total
% 100)) printf("...on %dth target...\n", counts
->total
);
183 pushsettings(t
->settings
);
184 t
->cmds
= (char *)make1cmds(t
->actions
);
185 popsettings(t
->settings
);
187 t
->progress
= T_MAKE_RUNNING
;
192 /* Call make1c() to begin the execution of the chain of commands */
193 /* needed to build target. If we're not going to build target */
194 /* (because of dependency failures or because no commands need to */
195 /* be run) the chain will be empty and make1c() will directly */
196 /* signal the completion of target. */
202 * make1c() - launch target's next command, call make1b() when done
204 static void make1c (TARGET
*t
) {
205 CMD
*cmd
= (CMD
*)t
->cmds
;
206 /* If there are (more) commands to run to build this target */
207 /* (and we haven't hit an error running earlier comands) we */
208 /* launch the command with execcmd(). */
210 /* If there are no more commands to run, we collect the status */
211 /* from all the actions then report our completion to all the */
213 if (cmd
&& t
->status
== EXEC_CMD_OK
) {
215 if (DEBUG_MAKEQ
|| ! (cmd
->rule
->flags
& RULE_QUIETLY
)) {
216 printf("%s ", cmd
->rule
->name
);
217 list_print(lol_get(&cmd
->args
, 0));
221 if (DEBUG_EXEC
) printf("%s\n", cmd
->buf
);
222 if (globs
.cmdout
) fprintf(globs
.cmdout
, "%s", cmd
->buf
);
223 if (globs
.noexec
) make1d(t
, EXEC_CMD_OK
);
226 execcmd(cmd
->buf
, make1d
, t
, cmd
->shell
);
231 /* Collect status from actions, and distribute it as well */
232 for (actions
= t
->actions
; actions
; actions
= actions
->next
) {
233 if (actions
->action
->status
> t
->status
) t
->status
= actions
->action
->status
;
235 for (actions
= t
->actions
; actions
; actions
= actions
->next
) {
236 if (t
->status
> actions
->action
->status
) actions
->action
->status
= t
->status
;
238 /* Tally success/failure for those we tried to update. */
239 if (t
->progress
== T_MAKE_RUNNING
) {
241 case EXEC_CMD_OK
: ++counts
->made
; break;
242 case EXEC_CMD_FAIL
: ++counts
->failed
; break;
245 /* Tell parents dependent has been built */
246 t
->progress
= T_MAKE_DONE
;
247 for (c
= t
->parents
; c
; c
= c
->next
) make1b(c
->target
);
253 * make1d() - handle command execution completion and call back make1c()
255 static void make1d (void *closure
, int status
) {
256 TARGET
*t
= (TARGET
*)closure
;
257 CMD
*cmd
= (CMD
*)t
->cmds
;
259 /* Execcmd() has completed. All we need to do is fiddle with the */
260 /* status and signal our completion so make1c() can run the next */
261 /* command. On interrupts, we bail heavily. */
262 if (status
== EXEC_CMD_FAIL
&& (cmd
->rule
->flags
& RULE_IGNORE
)) status
= EXEC_CMD_OK
;
263 /* On interrupt, set intr so _everything_ fails */
264 if (status
== EXEC_CMD_INTR
) ++intr
;
265 if (status
== EXEC_CMD_FAIL
&& DEBUG_MAKE
) {
266 /* Print command text on failure */
267 if (!DEBUG_EXEC
) printf("%s\n", cmd
->buf
);
268 printf("...failed %s ", cmd
->rule
->name
);
269 list_print(lol_get(&cmd
->args
, 0));
271 if (globs
.quitquick
) ++intr
;
273 /* If the command was interrupted or failed and the target */
274 /* is not "precious", remove the targets. */
275 /* Precious == 'actions updated' -- the target maintains state. */
276 if (status
!= EXEC_CMD_OK
&& !(cmd
->rule
->flags
& RULE_UPDATED
)) {
277 LIST
*targets
= lol_get(&cmd
->args
, 0);
278 for (; targets
; targets
= list_next(targets
)) {
279 if (!unlink(targets
->string
)) printf("...removing %s\n", targets
->string
);
282 /* Free this command and call make1c() to move onto next command. */
284 t
->cmds
= (char *)cmd_next(cmd
);
291 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
293 * Essentially copies a chain of ACTIONs to a chain of CMDs,
294 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions,
295 * and handling RULE_UPDATED actions. The result is a chain of
296 * CMDs which can be expanded by var_string() and executed with
299 static CMD
*make1cmds (ACTIONS
*a0
) {
301 LIST
*shell
= var_get("JAMSHELL"); /* shell is per-target */
303 /* Step through actions */
304 /* Actions may be shared with other targets or grouped with */
305 /* RULE_TOGETHER, so actions already seen are skipped. */
306 for (; a0
; a0
= a0
->next
) {
307 RULE
*rule
= a0
->action
->rule
;
311 int start
, chunk
, length
, maxline
;
312 /* Only do rules with commands to execute. */
313 /* If this action has already been executed, use saved status */
314 if (!rule
->actions
|| a0
->action
->running
) continue;
315 a0
->action
->running
= 1;
316 /* Make LISTS of targets and sources */
317 /* If `execute together` has been specified for this rule, tack */
318 /* on sources from each instance of this rule for this target. */
319 nt
= make1list(L0
, a0
->action
->targets
, 0);
320 ns
= make1list(L0
, a0
->action
->sources
, rule
->flags
);
321 if (rule
->flags
& RULE_TOGETHER
) {
322 for (a1
= a0
->next
; a1
; a1
= a1
->next
) {
323 if (a1
->action
->rule
== rule
&& !a1
->action
->running
) {
324 ns
= make1list(ns
, a1
->action
->sources
, rule
->flags
);
325 a1
->action
->running
= 1;
329 /* If doing only updated (or existing) sources, but none have */
330 /* been updated (or exist), skip this action. */
331 if (!ns
&& (rule
->flags
& (RULE_UPDATED
| RULE_EXISTING
))) {
335 /* If we had 'actions xxx bind vars' we bind the vars now */
336 boundvars
= make1settings(rule
->bindlist
);
337 pushsettings(boundvars
);
339 * Build command, starting with all source args.
341 * If cmd_new returns 0, it's because the resulting command
342 * length is > MAXLINE. In this case, we'll slowly reduce
343 * the number of source arguments presented until it does
344 * fit. This only applies to actions that allow PIECEMEAL
347 * While reducing slowly takes a bit of compute time to get
348 * things just right, it's worth it to get as close to MAXLINE
349 * as possible, because launching the commands we're executing
350 * is likely to be much more compute intensive!
352 * Note we loop through at least once, for sourceless actions.
354 * Max line length is the action specific maxline or, if not
355 * given or bigger than MAXLINE, MAXLINE.
358 chunk
= length
= list_length(ns
);
359 maxline
= rule
->flags
/ RULE_MAXLINE
;
360 maxline
= maxline
&& maxline
< MAXLINE
? maxline
: MAXLINE
;
362 /* Build cmd: cmd_new consumes its lists. */
363 CMD
*cmd
= cmd_new(rule
,
365 list_sublist(ns
, start
, chunk
),
366 list_copy(L0
, shell
),
369 /* It fit: chain it up. */
370 if (!cmds
) cmds
= cmd
; else cmds
->tail
->next
= cmd
;
373 } else if ((rule
->flags
& RULE_PIECEMEAL
) && chunk
> 1) {
374 /* Reduce chunk size slowly. */
377 /* Too long and not splittable. */
378 printf("%s actions too long (max %d)!\n", rule
->name
, maxline
);
381 } while (start
< length
);
382 /* These were always copied when used. */
385 /* Free the variables whose values were bound by */
386 /* 'actions xxx bind vars' */
387 popsettings(boundvars
);
388 freesettings(boundvars
);
395 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
397 static LIST
*make1list (LIST
*l
, TARGETS
*targets
, int flags
) {
398 for (; targets
; targets
= targets
->next
) {
399 TARGET
*t
= targets
->target
;
400 /* Sources to 'actions existing' are never in the dependency */
401 /* graph (if they were, they'd get built and 'existing' would */
402 /* be superfluous, so throttle warning message about independent */
404 if (t
->binding
== T_BIND_UNBOUND
) make1bind(t
, !(flags
& RULE_EXISTING
));
405 if ((flags
& RULE_EXISTING
) && t
->binding
!= T_BIND_EXISTS
) continue;
406 if ((flags
& RULE_UPDATED
) && t
->fate
<= T_FATE_STABLE
) continue;
407 /* Prohibit duplicates for RULE_TOGETHER */
408 if (flags
& RULE_TOGETHER
) {
411 for (m
= l
; m
; m
= m
->next
) {
412 if (!strcmp(m
->string
, t
->boundname
)) break;
417 l
= list_new(l
, t
->boundname
, 1);
424 * make1settings() - for vars that get bound values, build up replacement lists
426 static SETTINGS
*make1settings (LIST
*vars
) {
427 SETTINGS
*settings
= 0;
429 for (; vars
; vars
= list_next(vars
)) {
430 LIST
*l
= var_get(vars
->string
);
433 for (; l
; l
= list_next(l
)) {
434 TARGET
*t
= bindtarget(l
->string
);
435 /* Make sure the target is bound, warning if it is not in the */
436 /* dependency graph. */
437 if (t
->binding
== T_BIND_UNBOUND
) make1bind(t
, 1);
439 nl
= list_new(nl
, t
->boundname
, 1);
441 /* Add to settings chain */
442 settings
= addsettings(settings
, 0, vars
->string
, nl
);
449 * make1bind() - bind targets that weren't bound in dependency analysis
451 * Spot the kludge! If a target is not in the dependency tree, it didn't
452 * get bound by make0(), so we have to do it here. Ugly.
454 static void make1bind (TARGET
*t
, int warn
) {
455 if (t
->flags
& T_FLAG_NOTFILE
) return;
456 /* Sources to 'actions existing' are never in the dependency */
457 /* graph (if they were, they'd get built and 'existing' would */
458 /* be superfluous, so throttle warning message about independent */
460 if (warn
) printf("warning: using independent target %s\n", t
->name
);
461 pushsettings(t
->settings
);
462 t
->boundname
= search(t
->name
, &t
->time
);
463 t
->binding
= t
->time
? T_BIND_EXISTS
: T_BIND_MISSING
;
464 popsettings(t
->settings
);