]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/make1.c
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * Copyright 2001-2004 David Abrahams.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
14 * make1.c - execute commands to bring targets up to date
16 * This module contains make1(), the entry point called by make() to recursively
17 * descend the dependency graph executing update actions as marked by make0().
20 * make1() - execute commands to update a TARGET and all of its dependencies
22 * Internal routines, the recursive/asynchronous command executors:
23 * make1a() - recursively schedules dependency builds and then goes to
25 * make1b() - if nothing is blocking this target's build, proceed to
27 * make1c() - launch target's next command, or go to parents' MAKE1B
29 * make1c_closure() - handle command execution completion and go to MAKE1C
31 * Internal support routines:
32 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc.
33 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
34 * make1settings() - for vars with bound values, build up replacement lists
35 * make1bind() - bind targets that weren't bound in dependency analysis
57 #if !defined( NT ) || defined( __GNUC__ )
58 #include <unistd.h> /* for unlink */
61 static CMD
* make1cmds ( TARGET
* );
62 static LIST
* make1list ( LIST
*, TARGETS
*, int flags
);
63 static SETTINGS
* make1settings ( struct module_t
*, LIST
* vars
);
64 static void make1bind ( TARGET
* );
65 static TARGET
* make1findcycle ( TARGET
* );
66 static void make1breakcycle( TARGET
*, TARGET
* cycle_root
);
67 static void push_cmds( CMDLIST
* cmds
, int status
);
68 static int cmd_sem_lock( TARGET
* t
);
69 static void cmd_sem_unlock( TARGET
* t
);
71 static int targets_contains( TARGETS
* l
, TARGET
* t
);
72 static int targets_equal( TARGETS
* l1
, TARGETS
* l2
);
74 /* Ugly static - it is too hard to carry it through the callbacks. */
85 #define T_STATE_MAKE1A 0 /* make1a() should be called */
86 #define T_STATE_MAKE1B 1 /* make1b() should be called */
87 #define T_STATE_MAKE1C 2 /* make1c() should be called */
89 typedef struct _state state
;
92 state
* prev
; /* previous state on stack */
93 TARGET
* t
; /* current target */
94 TARGET
* parent
; /* parent argument necessary for MAKE1A */
95 int curstate
; /* current state */
98 static void make1a( state
* const );
99 static void make1b( state
* const );
100 static void make1c( state
const * const );
102 static void make1c_closure( void * const closure
, int status
,
103 timing_info
const * const, char const * const cmd_stdout
,
104 char const * const cmd_stderr
, int const cmd_exit_reason
);
106 typedef struct _stack
111 static stack state_stack
= { NULL
};
113 static state
* state_freelist
= NULL
;
115 /* Currently running command counter. */
116 static int cmdsrunning
;
119 static state
* alloc_state()
121 if ( state_freelist
)
123 state
* const pState
= state_freelist
;
124 state_freelist
= pState
->prev
;
125 memset( pState
, 0, sizeof( state
) );
128 return (state
*)BJAM_MALLOC( sizeof( state
) );
132 static void free_state( state
* const pState
)
134 pState
->prev
= state_freelist
;
135 state_freelist
= pState
;
139 static void clear_state_freelist()
141 while ( state_freelist
)
143 state
* const pState
= state_freelist
;
144 state_freelist
= state_freelist
->prev
;
150 static state
* current_state( stack
* const pStack
)
152 return pStack
->stack
;
156 static void pop_state( stack
* const pStack
)
160 state
* const pState
= pStack
->stack
->prev
;
161 free_state( pStack
->stack
);
162 pStack
->stack
= pState
;
167 static state
* push_state( stack
* const pStack
, TARGET
* const t
,
168 TARGET
* const parent
, int const curstate
)
170 state
* const pState
= alloc_state();
172 pState
->parent
= parent
;
173 pState
->prev
= pStack
->stack
;
174 pState
->curstate
= curstate
;
175 return pStack
->stack
= pState
;
180 * Pushes a stack onto another stack, effectively reversing the order.
183 static void push_stack_on_stack( stack
* const pDest
, stack
* const pSrc
)
185 while ( pSrc
->stack
)
187 state
* const pState
= pSrc
->stack
;
188 pSrc
->stack
= pState
->prev
;
189 pState
->prev
= pDest
->stack
;
190 pDest
->stack
= pState
;
196 * make1() - execute commands to update a list of targets and all of their dependencies
202 int make1( LIST
* targets
)
207 memset( (char *)counts
, 0, sizeof( *counts
) );
211 stack temp_stack
= { NULL
};
212 for ( iter
= list_begin( targets
), end
= list_end( targets
);
213 iter
!= end
; iter
= list_next( iter
) )
214 push_state( &temp_stack
, bindtarget( list_item( iter
) ), NULL
, T_STATE_MAKE1A
);
215 push_stack_on_stack( &state_stack
, &temp_stack
);
218 /* Clear any state left over from the past */
221 /* Recursively make the target and its dependencies. */
225 while ( ( pState
= current_state( &state_stack
) ) )
228 pop_state( &state_stack
);
230 switch ( pState
->curstate
)
232 case T_STATE_MAKE1A
: make1a( pState
); break;
233 case T_STATE_MAKE1B
: make1b( pState
); break;
234 case T_STATE_MAKE1C
: make1c( pState
); break;
236 assert( !"make1(): Invalid state detected." );
241 /* Wait for outstanding commands to finish running. */
245 clear_state_freelist();
248 if ( counts
->failed
)
249 out_printf( "...failed updating %d target%s...\n", counts
->failed
,
250 counts
->failed
> 1 ? "s" : "" );
251 if ( DEBUG_MAKE
&& counts
->skipped
)
252 out_printf( "...skipped %d target%s...\n", counts
->skipped
,
253 counts
->skipped
> 1 ? "s" : "" );
254 if ( DEBUG_MAKE
&& counts
->made
)
255 out_printf( "...updated %d target%s...\n", counts
->made
,
256 counts
->made
> 1 ? "s" : "" );
258 /* If we were interrupted, exit now that all child processes
265 for ( iter
= list_begin( targets
), end
= list_end( targets
);
266 iter
!= end
; iter
= list_next( iter
) )
268 /* Check that the target was updated and that the
270 TARGET
* t
= bindtarget( list_item( iter
) );
271 if (t
->progress
== T_MAKE_DONE
)
273 if (t
->status
!= EXEC_CMD_OK
)
276 else if ( ! ( t
->progress
== T_MAKE_NOEXEC_DONE
&& globs
.noexec
) )
287 * make1a() - recursively schedules dependency builds and then goes to MAKE1B
289 * Called to start processing a specified target. Does nothing if the target is
290 * already being processed or otherwise starts processing all of its
294 static void make1a( state
* const pState
)
296 TARGET
* t
= pState
->t
;
297 TARGET
* const scc_root
= target_scc( t
);
299 if ( !pState
->parent
|| target_scc( pState
->parent
) != scc_root
)
300 pState
->t
= t
= scc_root
;
302 /* If the parent is the first to try to build this target or this target is
303 * in the MAKE1C quagmire, arrange for the parent to be notified when this
304 * target has been built.
306 if ( pState
->parent
&& t
->progress
<= T_MAKE_RUNNING
)
308 TARGET
* const parent_scc
= target_scc( pState
->parent
);
309 if ( t
!= parent_scc
)
311 t
->parents
= targetentry( t
->parents
, parent_scc
);
312 ++parent_scc
->asynccnt
;
316 /* If the target has been previously updated with -n in effect, and we are
317 * now ignoring -n, update it for real. E.g. if the UPDATE_NOW rule was
318 * called for it twice - first with the -n option and then without.
320 if ( !globs
.noexec
&& t
->progress
== T_MAKE_NOEXEC_DONE
)
321 t
->progress
= T_MAKE_INIT
;
323 /* If this target is already being processed then do nothing. There is no
324 * need to start processing the same target all over again.
326 if ( t
->progress
!= T_MAKE_INIT
)
328 pop_state( &state_stack
);
332 /* Guard against circular dependencies. */
333 t
->progress
= T_MAKE_ONSTACK
;
335 /* 'asynccnt' counts the dependencies preventing this target from proceeding
336 * to MAKE1C for actual building. We start off with a count of 1 to prevent
337 * anything from happening until we can notify all dependencies that they
338 * are needed. This 1 is then accounted for when we enter MAKE1B ourselves,
339 * below. Without this if a dependency gets built before we finish
340 * processing all of our other dependencies our build might be triggerred
345 /* Push dependency build requests (to be executed in the natural order). */
347 stack temp_stack
= { NULL
};
349 for ( c
= t
->depends
; c
&& !quit
; c
= c
->next
)
350 push_state( &temp_stack
, c
->target
, t
, T_STATE_MAKE1A
);
351 push_stack_on_stack( &state_stack
, &temp_stack
);
354 t
->progress
= T_MAKE_ACTIVE
;
356 /* Once all of our dependencies have started getting processed we can move
359 /* Implementation note:
360 * In theory this would be done by popping this state before pushing
361 * dependency target build requests but as a slight optimization we simply
362 * modify our current state and leave it on the stack instead.
364 pState
->curstate
= T_STATE_MAKE1B
;
369 * make1b() - if nothing is blocking this target's build, proceed to MAKE1C
371 * Called after something stops blocking this target's build, e.g. that all of
372 * its dependencies have started being processed, one of its dependencies has
373 * been built or a semaphore this target has been waiting for is free again.
376 static void make1b( state
* const pState
)
378 TARGET
* const t
= pState
->t
;
380 char const * failed_name
= "dependencies";
382 pop_state( &state_stack
);
384 /* If any dependencies are still outstanding, wait until they signal their
385 * completion by pushing this same state for their parent targets.
392 /* Now ready to build target 't', if dependencies built OK. */
394 /* Collect status from dependencies. If -n was passed then act as though all
395 * dependencies built correctly (the only way they can fail is if UPDATE_NOW
396 * was called). If the dependencies can not be found or we got an interrupt,
397 * we can not get here.
402 for ( c
= t
->depends
; c
; c
= c
->next
)
403 if ( c
->target
->status
> t
->status
&& !( c
->target
->flags
&
407 t
->status
= c
->target
->status
;
411 /* If an internal header node failed to build, we want to output the target
415 failed_name
= failed
->flags
& T_FLAG_INTERNAL
417 : object_str( failed
->name
);
418 t
->failed
= failed_name
;
420 /* If actions for building any of the dependencies have failed, bail.
421 * Otherwise, execute all actions to make the current target.
423 if ( ( t
->status
== EXEC_CMD_FAIL
) && t
->actions
)
426 if ( ( t
->flags
& ( T_FLAG_RMOLD
| T_FLAG_NOTFILE
) ) == T_FLAG_RMOLD
)
428 if ( !unlink( object_str( t
->boundname
) ) )
429 out_printf( "...removing outdated %s\n", object_str( t
->boundname
)
433 out_printf( "...skipped %s for lack of %s...\n", object_str( t
->name
),
437 if ( t
->status
== EXEC_CMD_OK
)
444 case T_FATE_CANTFIND
:
445 case T_FATE_CANTMAKE
:
446 t
->status
= EXEC_CMD_FAIL
;
451 out_printf( "...using %s...\n", object_str( t
->name
) );
457 case T_FATE_OUTDATED
:
460 /* Prepare commands for executing actions scheduled for this target.
461 * Commands have their embedded variables automatically expanded,
462 * including making use of any "on target" variables.
467 if ( DEBUG_MAKE
&& !( counts
->total
% 100 ) )
468 out_printf( "...on %dth target...\n", counts
->total
);
470 t
->cmds
= (char *)make1cmds( t
);
471 /* Update the target's "progress" so MAKE1C processing counts it
472 * among its successes/failures.
474 t
->progress
= T_MAKE_RUNNING
;
478 /* All valid fates should have been accounted for by now. */
480 err_printf( "ERROR: %s has bad fate %d", object_str( t
->name
),
485 /* Proceed to MAKE1C to begin executing the chain of commands prepared for
486 * building the target. If we are not going to build the target (e.g. due to
487 * dependency failures or no commands needing to be run) the chain will be
488 * empty and MAKE1C processing will directly signal the target's completion.
491 if ( t
->cmds
== NULL
|| --( ( CMD
* )t
->cmds
)->asynccnt
== 0 )
492 push_state( &state_stack
, t
, NULL
, T_STATE_MAKE1C
);
493 else if ( DEBUG_EXECCMD
)
495 CMD
* cmd
= ( CMD
* )t
->cmds
;
496 out_printf( "Delaying %s %s: %d targets not ready\n", object_str( cmd
->rule
->name
), object_str( t
->boundname
), cmd
->asynccnt
);
502 * make1c() - launch target's next command, or go to parents' MAKE1B if none
504 * If there are (more) commands to run to build this target (and we have not hit
505 * an error running earlier comands) we launch the command using exec_cmd().
506 * Command execution signals its completion in exec_wait() by calling our
507 * make1c_closure() callback.
509 * If there are no more commands to run, we collect the status from all the
510 * actions and report our completion to all the parents.
513 static void make1c( state
const * const pState
)
515 TARGET
* const t
= pState
->t
;
516 CMD
* const cmd
= (CMD
*)t
->cmds
;
520 /* Pop state first in case something below (e.g. exec_cmd(), exec_wait()
521 * or make1c_closure()) pushes a new state. Note that we must not access
522 * the popped state data after this as the same stack node might have
523 * been reused internally for some newly pushed state.
525 pop_state( &state_stack
);
527 if ( cmd
->status
!= EXEC_CMD_OK
)
530 push_cmds( cmd
->next
, cmd
->status
);
536 if ( ! cmd_sem_lock( t
) )
542 /* Increment the jobs running counter. */
545 /* Execute the actual build command or fake it if no-op. */
546 if ( globs
.noexec
|| cmd
->noop
)
548 timing_info time_info
= { 0 };
549 timestamp_current( &time_info
.start
);
550 timestamp_copy( &time_info
.end
, &time_info
.start
);
551 make1c_closure( t
, EXEC_CMD_OK
, &time_info
, "", "", EXIT_OK
);
555 exec_cmd( cmd
->buf
, make1c_closure
, t
, cmd
->shell
);
557 /* Wait until under the concurrent command count limit. */
558 /* FIXME: This wait could be skipped here and moved to just before
559 * trying to execute a command that would cross the command count
560 * limit. Note though that this might affect the order in which
561 * unrelated targets get built and would thus require that all
562 * affected Boost Build tests be updated.
564 assert( 0 < globs
.jobs
);
565 assert( globs
.jobs
<= MAXJOBS
);
566 while ( cmdsrunning
>= globs
.jobs
)
574 /* Tally success/failure for those we tried to update. */
575 if ( t
->progress
== T_MAKE_RUNNING
)
578 case EXEC_CMD_OK
: ++counts
->made
; break;
579 case EXEC_CMD_FAIL
: ++counts
->failed
; break;
582 /* Tell parents their dependency has been built. */
585 stack temp_stack
= { NULL
};
586 TARGET
* additional_includes
= NULL
;
588 t
->progress
= globs
.noexec
? T_MAKE_NOEXEC_DONE
: T_MAKE_DONE
;
590 /* Target has been updated so rescan it for dependencies. */
591 if ( t
->fate
>= T_FATE_MISSING
&& t
->status
== EXEC_CMD_OK
&&
592 !( t
->flags
& T_FLAG_INTERNAL
) )
594 TARGET
* saved_includes
;
597 /* Clean current includes. */
598 saved_includes
= t
->includes
;
601 s
= copysettings( t
->settings
);
602 pushsettings( root_module(), s
);
604 popsettings( root_module(), s
);
609 /* Tricky. The parents have already been processed, but they
610 * have not seen the internal node, because it was just
611 * created. We need to:
612 * - push MAKE1A states that would have been pushed by the
614 * - make sure all unprocessed parents will pick up the
616 * - make sure processing the additional MAKE1A states is
617 * done before processing the MAKE1B state for our
618 * current target (which would mean this target has
619 * already been built), otherwise the parent would be
620 * considered built before the additional MAKE1A state
621 * processing even got a chance to start.
623 make0( t
->includes
, t
->parents
->target
, 0, 0, 0, t
->includes
625 /* Link the old includes on to make sure that it gets
626 * cleaned up correctly.
628 t
->includes
->includes
= saved_includes
;
629 for ( c
= t
->dependants
; c
; c
= c
->next
)
630 c
->target
->depends
= targetentry( c
->target
->depends
,
632 /* Will be processed below. */
633 additional_includes
= t
->includes
;
637 t
->includes
= saved_includes
;
641 if ( additional_includes
)
642 for ( c
= t
->parents
; c
; c
= c
->next
)
643 push_state( &temp_stack
, additional_includes
, c
->target
,
648 TARGET
* const scc_root
= target_scc( t
);
649 assert( scc_root
->progress
< T_MAKE_DONE
);
650 for ( c
= t
->parents
; c
; c
= c
->next
)
652 if ( target_scc( c
->target
) == scc_root
)
653 push_state( &temp_stack
, c
->target
, NULL
, T_STATE_MAKE1B
656 scc_root
->parents
= targetentry( scc_root
->parents
,
662 for ( c
= t
->parents
; c
; c
= c
->next
)
663 push_state( &temp_stack
, c
->target
, NULL
, T_STATE_MAKE1B
);
666 /* Must pop state before pushing any more. */
667 pop_state( &state_stack
);
669 /* Using stacks reverses the order of execution. Reverse it back. */
670 push_stack_on_stack( &state_stack
, &temp_stack
);
677 * call_timing_rule() - Look up the __TIMING_RULE__ variable on the given
678 * target, and if non-empty, invoke the rule it names, passing the given
682 static void call_timing_rule( TARGET
* target
, timing_info
const * const time
)
686 pushsettings( root_module(), target
->settings
);
687 timing_rule
= var_get( root_module(), constant_TIMING_RULE
);
688 popsettings( root_module(), target
->settings
);
690 if ( !list_empty( timing_rule
) )
692 /* rule timing-rule ( args * : target : start end user system clock ) */
694 /* Prepare the argument list. */
696 OBJECT
* rulename
= list_front( timing_rule
);
699 /* args * :: $(__TIMING_RULE__[2-]) */
700 lol_add( frame
->args
, list_copy_range( timing_rule
, list_next(
701 list_begin( timing_rule
) ), list_end( timing_rule
) ) );
703 /* target :: the name of the target */
704 lol_add( frame
->args
, list_new( object_copy( target
->name
) ) );
706 /* start end user system clock :: info about the action command */
707 lol_add( frame
->args
, list_push_back( list_push_back( list_push_back( list_push_back( list_new(
708 outf_time( &time
->start
) ),
709 outf_time( &time
->end
) ),
710 outf_double( time
->user
) ),
711 outf_double( time
->system
) ),
712 outf_double( timestamp_delta_seconds(&time
->start
, &time
->end
) ) )
716 evaluate_rule( bindrule( rulename
, root_module() ), rulename
, frame
);
725 * call_action_rule() - Look up the __ACTION_RULE__ variable on the given
726 * target, and if non-empty, invoke the rule it names, passing the given info,
727 * timing_info, executed command and command output.
730 static void call_action_rule
734 timing_info
const * time
,
735 char const * executed_command
,
736 char const * command_output
741 pushsettings( root_module(), target
->settings
);
742 action_rule
= var_get( root_module(), constant_ACTION_RULE
);
743 popsettings( root_module(), target
->settings
);
745 if ( !list_empty( action_rule
) )
747 /* rule action-rule (
750 command status start end user system :
753 /* Prepare the argument list. */
755 OBJECT
* rulename
= list_front( action_rule
);
758 /* args * :: $(__ACTION_RULE__[2-]) */
759 lol_add( frame
->args
, list_copy_range( action_rule
, list_next(
760 list_begin( action_rule
) ), list_end( action_rule
) ) );
762 /* target :: the name of the target */
763 lol_add( frame
->args
, list_new( object_copy( target
->name
) ) );
765 /* command status start end user system :: info about the action command
767 lol_add( frame
->args
,
768 list_push_back( list_push_back( list_push_back( list_push_back( list_push_back( list_new(
769 object_new( executed_command
) ),
770 outf_int( status
) ),
771 outf_time( &time
->start
) ),
772 outf_time( &time
->end
) ),
773 outf_double( time
->user
) ),
774 outf_double( time
->system
) ) );
776 /* output ? :: the output of the action command */
777 if ( command_output
)
778 lol_add( frame
->args
, list_new( object_new( command_output
) ) );
780 lol_add( frame
->args
, L0
);
783 evaluate_rule( bindrule( rulename
, root_module() ), rulename
, frame
);
792 * make1c_closure() - handle command execution completion and go to MAKE1C.
794 * Internal function passed as a notification callback for when a command
795 * finishes getting executed by the OS or called directly when faking that a
796 * command had been executed by the OS.
798 * Now all we need to do is fiddle with the command exit status and push a new
799 * MAKE1C state to execute the next command scheduled for building this target
800 * or close up the target's build process in case there are no more commands
801 * scheduled for it. On interrupts, we bail heavily.
804 static void make1c_closure
806 void * const closure
,
808 timing_info
const * const time
,
809 char const * const cmd_stdout
,
810 char const * const cmd_stderr
,
811 int const cmd_exit_reason
814 TARGET
* const t
= (TARGET
*)closure
;
815 CMD
* const cmd
= (CMD
*)t
->cmds
;
816 char const * rule_name
= 0;
817 char const * target_name
= 0;
823 /* Calculate the target's status from the cmd execution result. */
825 /* Store the target's status. */
826 t
->status
= status_orig
;
828 /* Invert OK/FAIL target status when FAIL_EXPECTED has been applied. */
829 if ( t
->flags
& T_FLAG_FAIL_EXPECTED
&& !globs
.noexec
)
833 case EXEC_CMD_FAIL
: t
->status
= EXEC_CMD_OK
; break;
834 case EXEC_CMD_OK
: t
->status
= EXEC_CMD_FAIL
; break;
838 /* Ignore failures for actions marked as 'ignore'. */
839 if ( t
->status
== EXEC_CMD_FAIL
&& cmd
->rule
->actions
->flags
&
841 t
->status
= EXEC_CMD_OK
;
845 ( DEBUG_MAKE
&& !( cmd
->rule
->actions
->flags
& RULE_QUIETLY
) ) )
847 rule_name
= object_str( cmd
->rule
->name
);
848 target_name
= object_str( list_front( lol_get( (LOL
*)&cmd
->args
, 0 ) )
852 out_action( rule_name
, target_name
, cmd
->buf
->value
, cmd_stdout
, cmd_stderr
,
857 call_timing_rule( t
, time
);
859 out_printf( "%f sec system; %f sec user; %f sec clock\n",
860 time
->system
, time
->user
,
861 timestamp_delta_seconds(&time
->start
, &time
->end
) );
863 /* Assume -p0 is in effect, i.e. cmd_stdout contains merged output. */
864 call_action_rule( t
, status_orig
, time
, cmd
->buf
->value
, cmd_stdout
);
867 /* Print command text on failure. */
868 if ( t
->status
== EXEC_CMD_FAIL
&& DEBUG_MAKE
)
871 out_printf( "%s\n", cmd
->buf
->value
);
873 out_printf( "...failed %s ", object_str( cmd
->rule
->name
) );
874 list_print( lol_get( (LOL
*)&cmd
->args
, 0 ) );
875 out_printf( "...\n" );
878 /* On interrupt, set quit so _everything_ fails. Do the same for failed
879 * commands if we were asked to stop the build in case of any errors.
881 if ( t
->status
== EXEC_CMD_INTR
)
886 if ( t
->status
== EXEC_CMD_FAIL
&& globs
.quitquick
)
889 /* If the command was not successful remove all of its targets not marked as
892 if ( t
->status
!= EXEC_CMD_OK
)
894 LIST
* const targets
= lol_get( (LOL
*)&cmd
->args
, 0 );
895 LISTITER iter
= list_begin( targets
);
896 LISTITER
const end
= list_end( targets
);
897 for ( ; iter
!= end
; iter
= list_next( iter
) )
899 char const * const filename
= object_str( list_item( iter
) );
900 TARGET
const * const t
= bindtarget( list_item( iter
) );
901 if ( !( t
->flags
& T_FLAG_PRECIOUS
) && !unlink( filename
) )
902 out_printf( "...removing %s\n", filename
);
907 /* Release any semaphores used by this action. */
911 /* Free this command and push the MAKE1C state to execute the next one
912 * scheduled for building this same target.
915 push_cmds( cmd
->next
, t
->status
);
919 /* push the next MAKE1C state after a command is run. */
920 static void push_cmds( CMDLIST
* cmds
, int status
)
923 for( cmd_iter
= cmds
; cmd_iter
; cmd_iter
= cmd_iter
->next
)
925 if ( cmd_iter
->iscmd
)
927 CMD
* next_cmd
= cmd_iter
->impl
.cmd
;
928 /* Propagate the command status. */
929 if ( next_cmd
->status
< status
)
930 next_cmd
->status
= status
;
931 if ( --next_cmd
->asynccnt
== 0 )
933 /* Select the first target associated with the action.
934 * This is safe because sibling CMDs cannot have targets
937 TARGET
* first_target
= bindtarget( list_front( lol_get( &next_cmd
->args
, 0 ) ) );
938 first_target
->cmds
= (char *)next_cmd
;
939 push_state( &state_stack
, first_target
, NULL
, T_STATE_MAKE1C
);
941 else if ( DEBUG_EXECCMD
)
943 TARGET
* first_target
= bindtarget( list_front( lol_get( &next_cmd
->args
, 0 ) ) );
944 out_printf( "Delaying %s %s: %d targets not ready\n", object_str( next_cmd
->rule
->name
), object_str( first_target
->boundname
), next_cmd
->asynccnt
);
949 /* This is a target that we're finished updating */
950 TARGET
* updated_target
= cmd_iter
->impl
.t
;
951 if ( updated_target
->status
< status
)
952 updated_target
->status
= status
;
953 updated_target
->cmds
= NULL
;
954 push_state( &state_stack
, updated_target
, NULL
, T_STATE_MAKE1C
);
961 * swap_settings() - replace the settings from the current module and target
962 * with those from the new module and target
965 static void swap_settings
967 module_t
* * current_module
,
968 TARGET
* * current_target
,
969 module_t
* new_module
,
973 if ( ( new_target
== *current_target
) &&
974 ( new_module
== *current_module
) )
977 if ( *current_target
)
978 popsettings( *current_module
, (*current_target
)->settings
);
981 pushsettings( new_module
, new_target
->settings
);
983 *current_module
= new_module
;
984 *current_target
= new_target
;
989 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc.
991 * Essentially copies a chain of ACTIONs to a chain of CMDs, grouping
992 * RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions, and handling
993 * RULE_NEWSRCS actions. The result is a chain of CMDs which has already had all
994 * of its embedded variable references expanded and can now be executed using
998 static CMD
* make1cmds( TARGET
* t
)
1003 module_t
* settings_module
= 0;
1004 TARGET
* settings_target
= 0;
1006 int const running_flag
= globs
.noexec
? A_RUNNING_NOEXEC
: A_RUNNING
;
1008 /* Step through actions.
1010 for ( a0
= t
->actions
; a0
; a0
= a0
->next
)
1012 RULE
* rule
= a0
->action
->rule
;
1013 rule_actions
* actions
= rule
->actions
;
1014 SETTINGS
* boundvars
;
1019 /* Only do rules with commands to execute.
1024 if ( a0
->action
->running
>= running_flag
)
1027 /* If this action was skipped either because it was
1028 * combined with another action by RULE_TOGETHER, or
1029 * because all of its sources were filtered out,
1030 * then we don't have anything to do here.
1032 if ( a0
->action
->first_cmd
== NULL
)
1034 /* This action has already been processed for another target.
1035 * Just set up the dependency graph correctly and move on.
1037 first
= a0
->action
->first_cmd
;
1040 last_cmd
->next
= cmdlist_append_cmd( last_cmd
->next
, first
);
1046 last_cmd
= a0
->action
->last_cmd
;
1050 a0
->action
->running
= running_flag
;
1052 /* Make LISTS of targets and sources. If `execute together` has been
1053 * specified for this rule, tack on sources from each instance of this
1054 * rule for this target.
1056 nt
= make1list( L0
, a0
->action
->targets
, 0 );
1057 ns
= make1list( L0
, a0
->action
->sources
, actions
->flags
);
1058 if ( actions
->flags
& RULE_TOGETHER
)
1059 for ( a1
= a0
->next
; a1
; a1
= a1
->next
)
1060 if ( a1
->action
->rule
== rule
&&
1061 a1
->action
->running
< running_flag
&&
1062 targets_equal( a0
->action
->targets
, a1
->action
->targets
) )
1064 ns
= make1list( ns
, a1
->action
->sources
, actions
->flags
);
1065 a1
->action
->running
= running_flag
;
1068 /* If doing only updated (or existing) sources, but none have been
1069 * updated (or exist), skip this action.
1071 if ( list_empty( ns
) &&
1072 ( actions
->flags
& ( RULE_NEWSRCS
| RULE_EXISTING
) ) )
1078 swap_settings( &settings_module
, &settings_target
, rule
->module
, t
);
1079 if ( list_empty( shell
) )
1081 /* shell is per-target */
1082 shell
= var_get( rule
->module
, constant_JAMSHELL
);
1085 /* If we had 'actions xxx bind vars' we bind the vars now. */
1086 boundvars
= make1settings( rule
->module
, actions
->bindlist
);
1087 pushsettings( rule
->module
, boundvars
);
1090 * Build command, starting with all source args.
1092 * For actions that allow PIECEMEAL commands, if the constructed command
1093 * string is too long, we retry constructing it with a reduced number of
1094 * source arguments presented.
1096 * While reducing slowly takes a bit of compute time to get things just
1097 * right, it is worth it to get as close to maximum allowed command
1098 * string length as possible, because launching the commands we are
1099 * executing is likely to be much more compute intensive.
1101 * Note that we loop through at least once, for sourceless actions.
1104 int const length
= list_length( ns
);
1108 LIST
* cmd_targets
= L0
;
1109 LIST
* cmd_shell
= L0
;
1110 TARGETS
* semaphores
= NULL
;
1111 TARGETS
* targets_iter
;
1116 int cmd_check_result
;
1117 int cmd_error_length
;
1118 int cmd_error_max_length
;
1120 int accept_command
= 0;
1122 /* Build cmd: cmd_new() takes ownership of its lists. */
1123 if ( list_empty( cmd_targets
) ) cmd_targets
= list_copy( nt
);
1124 if ( list_empty( cmd_shell
) ) cmd_shell
= list_copy( shell
);
1125 cmd
= cmd_new( rule
, cmd_targets
, list_sublist( ns
, start
,
1126 chunk
), cmd_shell
);
1128 cmd_check_result
= exec_check( cmd
->buf
, &cmd
->shell
,
1129 &cmd_error_length
, &cmd_error_max_length
);
1131 if ( cmd_check_result
== EXEC_CHECK_OK
)
1135 else if ( cmd_check_result
== EXEC_CHECK_NOOP
)
1140 else if ( ( actions
->flags
& RULE_PIECEMEAL
) && ( chunk
> 1 ) )
1142 /* Too long but splittable. Reduce chunk size slowly and
1145 assert( cmd_check_result
== EXEC_CHECK_TOO_LONG
||
1146 cmd_check_result
== EXEC_CHECK_LINE_TOO_LONG
);
1147 chunk
= chunk
* 9 / 10;
1152 /* Too long and not splittable. */
1153 char const * const error_message
= cmd_check_result
==
1156 : "contains a line that is too long";
1157 assert( cmd_check_result
== EXEC_CHECK_TOO_LONG
||
1158 cmd_check_result
== EXEC_CHECK_LINE_TOO_LONG
);
1159 out_printf( "%s action %s (%d, max %d):\n", object_str(
1160 rule
->name
), error_message
, cmd_error_length
,
1161 cmd_error_max_length
);
1163 /* Tell the user what did not fit. */
1164 out_puts( cmd
->buf
->value
);
1168 assert( !retry
|| !accept_command
);
1170 if ( accept_command
)
1175 last_cmd
->next
= cmdlist_append_cmd( last_cmd
->next
, cmd
);
1180 cmds
= last_cmd
= cmd
;
1183 if ( cmd_count
++ == 0 )
1185 a0
->action
->first_cmd
= cmd
;
1188 /* Mark lists we need recreated for the next command since
1189 * they got consumed by the cmd object.
1196 /* We can reuse targets & shell lists for the next command
1197 * if we do not let them die with this cmd object.
1199 cmd_release_targets_and_shell( cmd
);
1206 while ( start
< length
);
1208 /* Record the end of the actions cmds */
1209 a0
->action
->last_cmd
= last_cmd
;
1212 for ( targets_iter
= a0
->action
->targets
; targets_iter
; targets_iter
= targets_iter
->next
)
1214 if ( targets_contains( targets_iter
->next
, targets_iter
->target
) )
1216 /* Add all targets produced by the action to the update list. */
1217 push_state( &state_stack
, targets_iter
->target
, NULL
, T_STATE_MAKE1A
);
1220 /* We need to wait until all the targets agree that
1221 * it's okay to run this action.
1223 ( ( CMD
* )a0
->action
->first_cmd
)->asynccnt
= unique_targets
;
1226 /* Collect semaphores */
1227 for ( targets_iter
= a0
->action
->targets
; targets_iter
; targets_iter
= targets_iter
->next
)
1229 TARGET
* sem
= targets_iter
->target
->semaphore
;
1233 if ( ! targets_contains( semaphores
, sem
) )
1234 semaphores
= targetentry( semaphores
, sem
);
1237 ( ( CMD
* )a0
->action
->first_cmd
)->lock
= semaphores
;
1238 ( ( CMD
* )a0
->action
->last_cmd
)->unlock
= semaphores
;
1242 /* These were always copied when used. */
1246 /* Free variables with values bound by 'actions xxx bind vars'. */
1247 popsettings( rule
->module
, boundvars
);
1248 freesettings( boundvars
);
1253 last_cmd
->next
= cmdlist_append_target( last_cmd
->next
, t
);
1256 swap_settings( &settings_module
, &settings_target
, 0, 0 );
1262 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
1265 static LIST
* make1list( LIST
* l
, TARGETS
* targets
, int flags
)
1267 for ( ; targets
; targets
= targets
->next
)
1269 TARGET
* t
= targets
->target
;
1271 if ( t
->binding
== T_BIND_UNBOUND
)
1274 if ( ( flags
& RULE_EXISTING
) && ( flags
& RULE_NEWSRCS
) )
1276 if ( ( t
->binding
!= T_BIND_EXISTS
) &&
1277 ( t
->fate
<= T_FATE_STABLE
) )
1280 else if ( flags
& RULE_EXISTING
)
1282 if ( t
->binding
!= T_BIND_EXISTS
)
1285 else if ( flags
& RULE_NEWSRCS
)
1287 if ( t
->fate
<= T_FATE_STABLE
)
1291 /* Prohibit duplicates for RULE_TOGETHER. */
1292 if ( flags
& RULE_TOGETHER
)
1294 LISTITER iter
= list_begin( l
);
1295 LISTITER
const end
= list_end( l
);
1296 for ( ; iter
!= end
; iter
= list_next( iter
) )
1297 if ( object_equal( list_item( iter
), t
->boundname
) )
1303 /* Build new list. */
1304 l
= list_push_back( l
, object_copy( t
->boundname
) );
1312 * make1settings() - for vars with bound values, build up replacement lists
1315 static SETTINGS
* make1settings( struct module_t
* module
, LIST
* vars
)
1317 SETTINGS
* settings
= 0;
1319 LISTITER vars_iter
= list_begin( vars
);
1320 LISTITER
const vars_end
= list_end( vars
);
1321 for ( ; vars_iter
!= vars_end
; vars_iter
= list_next( vars_iter
) )
1323 LIST
* const l
= var_get( module
, list_item( vars_iter
) );
1325 LISTITER iter
= list_begin( l
);
1326 LISTITER
const end
= list_end( l
);
1328 for ( ; iter
!= end
; iter
= list_next( iter
) )
1330 TARGET
* const t
= bindtarget( list_item( iter
) );
1332 /* Make sure the target is bound. */
1333 if ( t
->binding
== T_BIND_UNBOUND
)
1336 /* Build a new list. */
1337 nl
= list_push_back( nl
, object_copy( t
->boundname
) );
1340 /* Add to settings chain. */
1341 settings
= addsettings( settings
, VAR_SET
, list_item( vars_iter
), nl
);
1349 * make1bind() - bind targets that were not bound during dependency analysis
1351 * Spot the kludge! If a target is not in the dependency tree, it did not get
1352 * bound by make0(), so we have to do it here. Ugly.
1355 static void make1bind( TARGET
* t
)
1357 if ( t
->flags
& T_FLAG_NOTFILE
)
1360 pushsettings( root_module(), t
->settings
);
1361 object_free( t
->boundname
);
1362 t
->boundname
= search( t
->name
, &t
->time
, 0, t
->flags
& T_FLAG_ISFILE
);
1363 t
->binding
= timestamp_empty( &t
->time
) ? T_BIND_MISSING
: T_BIND_EXISTS
;
1364 popsettings( root_module(), t
->settings
);
1368 static int targets_contains( TARGETS
* l
, TARGET
* t
)
1370 for ( ; l
; l
= l
->next
)
1372 if ( t
== l
->target
)
1380 static int targets_equal( TARGETS
* l1
, TARGETS
* l2
)
1382 for ( ; l1
&& l2
; l1
= l1
->next
, l2
= l2
->next
)
1384 if ( l1
->target
!= l2
->target
)
1391 #ifdef OPT_SEMAPHORE
1393 static int cmd_sem_lock( TARGET
* t
)
1395 CMD
* cmd
= (CMD
*)t
->cmds
;
1397 /* Check whether all the semaphores required for updating
1398 * this target are free.
1400 for ( iter
= cmd
->lock
; iter
; iter
= iter
->next
)
1402 if ( iter
->target
->asynccnt
> 0 )
1404 if ( DEBUG_EXECCMD
)
1405 out_printf( "SEM: %s is busy, delaying launch of %s\n",
1406 object_str( iter
->target
->name
), object_str( t
->name
) );
1407 iter
->target
->parents
= targetentry( iter
->target
->parents
, t
);
1411 /* Lock the semaphores. */
1412 for ( iter
= cmd
->lock
; iter
; iter
= iter
->next
)
1414 ++iter
->target
->asynccnt
;
1415 if ( DEBUG_EXECCMD
)
1416 out_printf( "SEM: %s now used by %s\n", object_str( iter
->target
->name
1417 ), object_str( t
->name
) );
1419 /* A cmd only needs to be locked around its execution.
1420 * clearing cmd->lock here makes it safe to call cmd_sem_lock
1427 static void cmd_sem_unlock( TARGET
* t
)
1429 CMD
* cmd
= ( CMD
* )t
->cmds
;
1431 /* Release the semaphores. */
1432 for ( iter
= cmd
->unlock
; iter
; iter
= iter
->next
)
1434 if ( DEBUG_EXECCMD
)
1435 out_printf( "SEM: %s is now free\n", object_str(
1436 iter
->target
->name
) );
1437 --iter
->target
->asynccnt
;
1438 assert( iter
->target
->asynccnt
<= 0 );
1440 for ( iter
= cmd
->unlock
; iter
; iter
= iter
->next
)
1442 /* Find a waiting target that's ready */
1443 while ( iter
->target
->parents
)
1445 TARGETS
* first
= iter
->target
->parents
;
1446 TARGET
* t1
= first
->target
;
1448 /* Pop the first waiting CMD */
1450 first
->next
->tail
= first
->tail
;
1451 iter
->target
->parents
= first
->next
;
1454 if ( cmd_sem_lock( t1
) )
1456 push_state( &state_stack
, t1
, NULL
, T_STATE_MAKE1C
);