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. */
87 * make1() - execute commands to update a TARGET and all its dependents
95 memset( (char *)counts
, 0, sizeof( *counts
) );
97 /* Recursively make the target and its dependents */
99 make1a( t
, (TARGET
*)0 );
101 /* Wait for any outstanding commands to finish running. */
108 if( DEBUG_MAKE
&& counts
->failed
)
109 printf( "...failed updating %d target(s)...\n", counts
->failed
);
111 if( DEBUG_MAKE
&& counts
->skipped
)
112 printf( "...skipped %d target(s)...\n", counts
->skipped
);
114 if( DEBUG_MAKE
&& counts
->made
)
115 printf( "...updated %d target(s)...\n", counts
->made
);
117 return counts
->total
!= counts
->made
;
121 * make1a() - recursively traverse target tree, calling make1b()
131 /* If the parent is the first to try to build this target */
132 /* or this target is in the make1c() quagmire, arrange for the */
133 /* parent to be notified when this target is built. */
136 switch( t
->progress
)
141 t
->parents
= targetentry( t
->parents
, parent
);
145 if( t
->progress
!= T_MAKE_INIT
)
148 /* Asynccnt counts the dependents preventing this target from */
149 /* proceeding to make1b() for actual building. We start off with */
150 /* a count of 1 to prevent anything from happening until we can */
151 /* call all dependents. This 1 is accounted for when we call */
152 /* make1b() ourselves, below. */
156 /* Recurse on our dependents, manipulating progress to guard */
157 /* against circular dependency. */
159 t
->progress
= T_MAKE_ONSTACK
;
161 for( c
= t
->depends
; c
&& !intr
; c
= c
->next
)
162 make1a( c
->target
, t
);
164 t
->progress
= T_MAKE_ACTIVE
;
166 /* Now that all dependents have bumped asynccnt, we now allow */
167 /* decrement our reference to asynccnt. */
173 * make1b() - dependents of target built, now build target with make1c()
180 const char *failed
= "dependents";
182 /* If any dependents are still outstanding, wait until they */
183 /* call make1b() to signal their completion. */
188 /* Now ready to build target 't'... if dependents built ok. */
190 /* Collect status from dependents */
192 for( c
= t
->depends
; c
; c
= c
->next
)
193 if( c
->target
->status
> t
->status
)
195 failed
= c
->target
->name
;
196 t
->status
= c
->target
->status
;
199 /* If actions on deps have failed, bail. */
200 /* Otherwise, execute all actions to make target */
202 if( t
->status
== EXEC_CMD_FAIL
&& t
->actions
)
205 printf( "...skipped %s for lack of %s...\n", t
->name
, failed
);
208 if( t
->status
== EXEC_CMD_OK
)
213 /* shouldn't happen */
219 case T_FATE_CANTFIND
:
220 case T_FATE_CANTMAKE
:
221 t
->status
= EXEC_CMD_FAIL
;
226 printf( "...using %s...\n", t
->name
);
232 case T_FATE_OUTDATED
:
234 /* Set "on target" vars, build actions, unset vars */
235 /* Set "progress" so that make1c() counts this target among */
236 /* the successes/failures. */
242 if( DEBUG_MAKE
&& !( counts
->total
% 100 ) )
243 printf( "...on %dth target...\n", counts
->total
);
245 pushsettings( t
->settings
);
246 t
->cmds
= (char *)make1cmds( t
->actions
);
247 popsettings( t
->settings
);
249 t
->progress
= T_MAKE_RUNNING
;
255 /* Call make1c() to begin the execution of the chain of commands */
256 /* needed to build target. If we're not going to build target */
257 /* (because of dependency failures or because no commands need to */
258 /* be run) the chain will be empty and make1c() will directly */
259 /* signal the completion of target. */
265 * make1c() - launch target's next command, call make1b() when done
271 CMD
*cmd
= (CMD
*)t
->cmds
;
273 /* If there are (more) commands to run to build this target */
274 /* (and we haven't hit an error running earlier comands) we */
275 /* launch the command with execcmd(). */
277 /* If there are no more commands to run, we collect the status */
278 /* from all the actions then report our completion to all the */
281 if( cmd
&& t
->status
== EXEC_CMD_OK
)
284 if( DEBUG_MAKEQ
|| ! ( cmd
->rule
->flags
& RULE_QUIETLY
) )
286 printf( "%s ", cmd
->rule
->name
);
287 list_print( lol_get( &cmd
->args
, 0 ) );
292 printf( "%s\n", cmd
->buf
);
295 fprintf( globs
.cmdout
, "%s", cmd
->buf
);
299 make1d( t
, EXEC_CMD_OK
);
304 execcmd( cmd
->buf
, make1d
, t
, cmd
->shell
);
312 /* Collect status from actions, and distribute it as well */
314 for( actions
= t
->actions
; actions
; actions
= actions
->next
)
315 if( actions
->action
->status
> t
->status
)
316 t
->status
= actions
->action
->status
;
318 for( actions
= t
->actions
; actions
; actions
= actions
->next
)
319 if( t
->status
> actions
->action
->status
)
320 actions
->action
->status
= t
->status
;
322 /* Tally success/failure for those we tried to update. */
324 if( t
->progress
== T_MAKE_RUNNING
)
335 /* Tell parents dependent has been built */
337 t
->progress
= T_MAKE_DONE
;
339 for( c
= t
->parents
; c
; c
= c
->next
)
345 * make1d() - handle command execution completion and call back make1c()
353 TARGET
*t
= (TARGET
*)closure
;
354 CMD
*cmd
= (CMD
*)t
->cmds
;
356 /* Execcmd() has completed. All we need to do is fiddle with the */
357 /* status and signal our completion so make1c() can run the next */
358 /* command. On interrupts, we bail heavily. */
360 if( status
== EXEC_CMD_FAIL
&& ( cmd
->rule
->flags
& RULE_IGNORE
) )
361 status
= EXEC_CMD_OK
;
363 /* On interrupt, set intr so _everything_ fails */
365 if( status
== EXEC_CMD_INTR
)
368 if( status
== EXEC_CMD_FAIL
&& DEBUG_MAKE
)
370 /* Print command text on failure */
373 printf( "%s\n", cmd
->buf
);
375 printf( "...failed %s ", cmd
->rule
->name
);
376 list_print( lol_get( &cmd
->args
, 0 ) );
379 if( globs
.quitquick
) ++intr
;
382 /* If the command was interrupted or failed and the target */
383 /* is not "precious", remove the targets. */
384 /* Precious == 'actions updated' -- the target maintains state. */
386 if( status
!= EXEC_CMD_OK
&& !( cmd
->rule
->flags
& RULE_UPDATED
) )
388 LIST
*targets
= lol_get( &cmd
->args
, 0 );
390 for( ; targets
; targets
= list_next( targets
) )
391 if( !unlink( targets
->string
) )
392 printf( "...removing %s\n", targets
->string
);
395 /* Free this command and call make1c() to move onto next command. */
398 t
->cmds
= (char *)cmd_next( cmd
);
406 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc
408 * Essentially copies a chain of ACTIONs to a chain of CMDs,
409 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions,
410 * and handling RULE_UPDATED actions. The result is a chain of
411 * CMDs which can be expanded by var_string() and executed with
416 make1cmds( ACTIONS
*a0
)
419 LIST
*shell
= var_get( "JAMSHELL" ); /* shell is per-target */
421 /* Step through actions */
422 /* Actions may be shared with other targets or grouped with */
423 /* RULE_TOGETHER, so actions already seen are skipped. */
425 for( ; a0
; a0
= a0
->next
)
427 RULE
*rule
= a0
->action
->rule
;
432 int start
, chunk
, length
, maxline
;
434 /* Only do rules with commands to execute. */
435 /* If this action has already been executed, use saved status */
437 if( !rule
->actions
|| a0
->action
->running
)
440 a0
->action
->running
= 1;
442 /* Make LISTS of targets and sources */
443 /* If `execute together` has been specified for this rule, tack */
444 /* on sources from each instance of this rule for this target. */
446 nt
= make1list( L0
, a0
->action
->targets
, 0 );
447 ns
= make1list( L0
, a0
->action
->sources
, rule
->flags
);
449 if( rule
->flags
& RULE_TOGETHER
)
450 for( a1
= a0
->next
; a1
; a1
= a1
->next
)
451 if( a1
->action
->rule
== rule
&& !a1
->action
->running
)
453 ns
= make1list( ns
, a1
->action
->sources
, rule
->flags
);
454 a1
->action
->running
= 1;
457 /* If doing only updated (or existing) sources, but none have */
458 /* been updated (or exist), skip this action. */
460 if( !ns
&& ( rule
->flags
& ( RULE_UPDATED
| RULE_EXISTING
) ) )
466 /* If we had 'actions xxx bind vars' we bind the vars now */
468 boundvars
= make1settings( rule
->bindlist
);
469 pushsettings( boundvars
);
472 * Build command, starting with all source args.
474 * If cmd_new returns 0, it's because the resulting command
475 * length is > MAXLINE. In this case, we'll slowly reduce
476 * the number of source arguments presented until it does
477 * fit. This only applies to actions that allow PIECEMEAL
480 * While reducing slowly takes a bit of compute time to get
481 * things just right, it's worth it to get as close to MAXLINE
482 * as possible, because launching the commands we're executing
483 * is likely to be much more compute intensive!
485 * Note we loop through at least once, for sourceless actions.
487 * Max line length is the action specific maxline or, if not
488 * given or bigger than MAXLINE, MAXLINE.
492 chunk
= length
= list_length( ns
);
493 maxline
= rule
->flags
/ RULE_MAXLINE
;
494 maxline
= maxline
&& maxline
< MAXLINE
? maxline
: MAXLINE
;
498 /* Build cmd: cmd_new consumes its lists. */
500 CMD
*cmd
= cmd_new( rule
,
502 list_sublist( ns
, start
, chunk
),
503 list_copy( L0
, shell
),
508 /* It fit: chain it up. */
510 if( !cmds
) cmds
= cmd
;
511 else cmds
->tail
->next
= cmd
;
515 else if( ( rule
->flags
& RULE_PIECEMEAL
) && chunk
> 1 )
517 /* Reduce chunk size slowly. */
519 chunk
= chunk
* 9 / 10;
523 /* Too long and not splittable. */
525 printf( "%s actions too long (max %d)!\n",
526 rule
->name
, maxline
);
530 while( start
< length
);
532 /* These were always copied when used. */
537 /* Free the variables whose values were bound by */
538 /* 'actions xxx bind vars' */
540 popsettings( boundvars
);
541 freesettings( boundvars
);
548 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
557 for( ; targets
; targets
= targets
->next
)
559 TARGET
*t
= targets
->target
;
561 /* Sources to 'actions existing' are never in the dependency */
562 /* graph (if they were, they'd get built and 'existing' would */
563 /* be superfluous, so throttle warning message about independent */
566 if( t
->binding
== T_BIND_UNBOUND
)
567 make1bind( t
, !( flags
& RULE_EXISTING
) );
569 if( ( flags
& RULE_EXISTING
) && t
->binding
!= T_BIND_EXISTS
)
572 if( ( flags
& RULE_UPDATED
) && t
->fate
<= T_FATE_STABLE
)
575 /* Prohibit duplicates for RULE_TOGETHER */
577 if( flags
& RULE_TOGETHER
)
581 for( m
= l
; m
; m
= m
->next
)
582 if( !strcmp( m
->string
, t
->boundname
) )
591 l
= list_new( l
, t
->boundname
, 1 );
598 * make1settings() - for vars that get bound values, build up replacement lists
602 make1settings( LIST
*vars
)
604 SETTINGS
*settings
= 0;
606 for( ; vars
; vars
= list_next( vars
) )
608 LIST
*l
= var_get( vars
->string
);
611 for( ; l
; l
= list_next( l
) )
613 TARGET
*t
= bindtarget( l
->string
);
615 /* Make sure the target is bound, warning if it is not in the */
616 /* dependency graph. */
618 if( t
->binding
== T_BIND_UNBOUND
)
623 nl
= list_new( nl
, t
->boundname
, 1 );
626 /* Add to settings chain */
628 settings
= addsettings( settings
, 0, vars
->string
, nl
);
635 * make1bind() - bind targets that weren't bound in dependency analysis
637 * Spot the kludge! If a target is not in the dependency tree, it didn't
638 * get bound by make0(), so we have to do it here. Ugly.
646 if( t
->flags
& T_FLAG_NOTFILE
)
649 /* Sources to 'actions existing' are never in the dependency */
650 /* graph (if they were, they'd get built and 'existing' would */
651 /* be superfluous, so throttle warning message about independent */
655 printf( "warning: using independent target %s\n", t
->name
);
657 pushsettings( t
->settings
);
658 t
->boundname
= search( t
->name
, &t
->time
);
659 t
->binding
= t
->time
? T_BIND_EXISTS
: T_BIND_MISSING
;
660 popsettings( t
->settings
);