2 * Copyright 1993, 1995 Christopher Seiwald.
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 * make.c - bring a target up to date, once rules are in place.
16 * This modules controls the execution of rules to bring a target and its
17 * dependencies up to date. It is invoked after the targets, rules, et. al.
18 * described in rules.h are created by the interpreting jam files.
20 * This file contains the main make() entry point and the first pass make0().
21 * The second pass, make1(), which actually does the command execution, is in
25 * make() - make a target, given its name
28 * make0() - bind and scan everything to make a TARGET
29 * make0sort() - reorder TARGETS chain by their time (newest to oldest)
31 * 12/26/93 (seiwald) - allow NOTIME targets to be expanded via $(<), $(>).
32 * 01/04/94 (seiwald) - print all targets, bounded, when tracing commands.
33 * 04/08/94 (seiwald) - progress report now reflects only targets with actions.
34 * 04/11/94 (seiwald) - Combined deps & headers into deps[2] in TARGET.
35 * 12/20/94 (seiwald) - NOTIME renamed NOTFILE.
36 * 12/20/94 (seiwald) - make0() headers after determining fate of target, so
37 * that headers are not seen as being dependent on
39 * 01/19/95 (seiwald) - distinguish between CANTFIND/CANTMAKE targets.
40 * 02/02/95 (seiwald) - propagate leaf source time for new LEAVES rule.
41 * 02/14/95 (seiwald) - NOUPDATE rule means don't update existing target.
42 * 08/22/95 (seiwald) - NOUPDATE targets immune to anyhow (-a) flag.
43 * 09/06/00 (seiwald) - NOCARE affects targets with sources/actions.
44 * 03/02/01 (seiwald) - reverse NOCARE change.
45 * 03/14/02 (seiwald) - TEMPORARY targets no longer take on parents age.
46 * 03/16/02 (seiwald) - support for -g (reorder builds by source time).
56 #ifdef OPT_HEADER_CACHE_EXT
68 #define max( a,b ) ((a)>(b)?(a):(b))
71 static TARGETS * make0sort( TARGETS * c );
73 #ifdef OPT_GRAPH_DEBUG_EXT
74 static void dependGraphOutput( TARGET * t, int depth );
77 static const char * target_fate[] =
79 "init", /* T_FATE_INIT */
80 "making", /* T_FATE_MAKING */
81 "stable", /* T_FATE_STABLE */
82 "newer", /* T_FATE_NEWER */
83 "temp", /* T_FATE_ISTMP */
84 "touched", /* T_FATE_TOUCHED */
85 "rebuild", /* T_FATE_REBUILD */
86 "missing", /* T_FATE_MISSING */
87 "needtmp", /* T_FATE_NEEDTMP */
88 "old", /* T_FATE_OUTDATED */
89 "update", /* T_FATE_UPDATE */
90 "nofind", /* T_FATE_CANTFIND */
91 "nomake" /* T_FATE_CANTMAKE */
94 static const char * target_bind[] =
102 # define spaces(x) ( " " + ( x > 20 ? 0 : 20-x ) )
106 * make() - make a target, given its name.
109 int make( LIST * targets, int anyhow )
112 int status = 0; /* 1 if anything fails */
114 #ifdef OPT_HEADER_CACHE_EXT
118 memset( (char *)counts, 0, sizeof( *counts ) );
120 /* First bind all targets with LOCATE_TARGET setting. This is needed to
121 * correctly handle dependencies to generated headers.
123 bind_explicitly_located_targets();
127 PROFILE_ENTER( MAKE_MAKE0 );
128 for ( iter = list_begin( targets ), end = list_end( targets ); iter != end; iter = list_next( iter ) )
130 TARGET * t = bindtarget( list_item( iter ) );
131 if ( t->fate == T_FATE_INIT )
132 make0( t, 0, 0, counts, anyhow );
134 PROFILE_EXIT( MAKE_MAKE0 );
137 #ifdef OPT_GRAPH_DEBUG_EXT
141 for ( iter = list_begin( targets ), end = list_end( targets ); iter != end; iter = list_next( iter ) )
142 dependGraphOutput( bindtarget( list_item( iter ) ), 0 );
148 if ( counts->targets )
149 printf( "...found %d target%s...\n", counts->targets,
150 counts->targets > 1 ? "s" : "" );
152 printf( "...using %d temp target%s...\n", counts->temp,
153 counts->temp > 1 ? "s" : "" );
154 if ( counts->updating )
155 printf( "...updating %d target%s...\n", counts->updating,
156 counts->updating > 1 ? "s" : "" );
157 if ( counts->cantfind )
158 printf( "...can't find %d target%s...\n", counts->cantfind,
159 counts->cantfind > 1 ? "s" : "" );
160 if ( counts->cantmake )
161 printf( "...can't make %d target%s...\n", counts->cantmake,
162 counts->cantmake > 1 ? "s" : "" );
165 status = counts->cantfind || counts->cantmake;
169 PROFILE_ENTER( MAKE_MAKE1 );
170 for ( iter = list_begin( targets ), end = list_end( targets ); iter != end; iter = list_next( iter ) )
171 status |= make1( bindtarget( list_item( iter ) ) );
172 PROFILE_EXIT( MAKE_MAKE1 );
179 /* Force any dependants of t that have already at least begun being visited by
180 * make0() to be updated.
183 static void update_dependants( TARGET * t )
187 for ( q = t->dependants; q; q = q->next )
189 TARGET * p = q->target;
190 char fate0 = p->fate;
192 /* If we have already at least begun visiting it and we are not already
193 * rebuilding it for other reasons.
195 if ( ( fate0 != T_FATE_INIT ) && ( fate0 < T_FATE_BUILD ) )
197 p->fate = T_FATE_UPDATE;
201 printf( "fate change %s from %s to %s (as dependant of %s)\n",
202 object_str( p->name ), target_fate[ (int) fate0 ], target_fate[ (int) p->fate ], object_str( t->name ) );
205 /* If we are done visiting it, go back and make sure its dependants
208 if ( fate0 > T_FATE_MAKING )
209 update_dependants( p );
216 * Make sure that all of t's rebuilds get rebuilt.
219 static void force_rebuilds( TARGET * t )
222 for ( d = t->rebuilds; d; d = d->next )
224 TARGET * r = d->target;
226 /* If it is not already being rebuilt for other reasons. */
227 if ( r->fate < T_FATE_BUILD )
230 printf( "fate change %s from %s to %s (by rebuild)\n",
231 object_str( r->name ), target_fate[ (int) r->fate ], target_fate[ T_FATE_REBUILD ] );
233 /* Force rebuild it. */
234 r->fate = T_FATE_REBUILD;
236 /* And make sure its dependants are updated too. */
237 update_dependants( r );
244 * make0() - bind and scan everything to make a TARGET.
246 * Recursively binds a target, searches for #included headers, calls itself on
247 * those headers and any dependencies.
253 TARGET * p, /* parent */
254 int depth, /* for display purposes */
255 COUNTS * counts, /* for reporting */
257 ) /* forcibly touch all (real) targets */
265 char const * flag = "";
268 #ifdef OPT_GRAPH_DEBUG_EXT
269 int savedFate, oldTimeStamp;
272 if ( DEBUG_MAKEPROG )
273 printf( "make\t--\t%s%s\n", spaces( depth ), object_str( t->name ) );
279 if ( DEBUG_MAKEPROG )
280 printf( "make\t--\t%s%s\n", spaces( depth ), object_str( t->name ) );
282 t->fate = T_FATE_MAKING;
285 * Step 2: under the influence of "on target" variables,
286 * bind the target and search for headers.
289 /* Step 2a: set "on target" variables. */
290 s = copysettings( t->settings );
291 pushsettings( root_module(), s );
293 /* Step 2b: find and timestamp the target file (if it is a file). */
294 if ( ( t->binding == T_BIND_UNBOUND ) && !( t->flags & T_FLAG_NOTFILE ) )
296 OBJECT * another_target;
297 object_free( t->boundname );
298 t->boundname = search( t->name, &t->time, &another_target,
299 t->flags & T_FLAG_ISFILE );
300 /* If it was detected that this target refers to an already existing and
301 * bound one, we add an include dependency, so that every target
302 * depending on us will depend on that other target as well.
304 if ( another_target )
305 target_include( t, bindtarget( another_target ) );
307 t->binding = t->time ? T_BIND_EXISTS : T_BIND_MISSING;
310 /* INTERNAL, NOTFILE header nodes have the time of their parents. */
311 if ( p && ( t->flags & T_FLAG_INTERNAL ) )
314 /* If temp file does not exist but parent does, use parent. */
315 if ( p && ( t->flags & T_FLAG_TEMP ) &&
316 ( t->binding == T_BIND_MISSING ) &&
317 ( p->binding != T_BIND_MISSING ) )
319 t->binding = T_BIND_PARENTS;
325 LIST * var = var_get( root_module(), constant_JAM_SEMAPHORE );
326 if ( !list_empty( var ) )
328 TARGET * semaphore = bindtarget( list_front( var ) );
329 semaphore->progress = T_MAKE_SEMAPHORE;
330 t->semaphore = semaphore;
335 /* Step 2c: If its a file, search for headers. */
336 if ( t->binding == T_BIND_EXISTS )
339 /* Step 2d: reset "on target" variables. */
340 popsettings( root_module(), s );
344 * Pause for a little progress reporting .
349 if ( ! object_equal( t->name, t->boundname ) )
350 printf( "bind\t--\t%s%s: %s\n",
351 spaces( depth ), object_str( t->name ), object_str( t->boundname ) );
353 switch ( t->binding )
358 printf( "time\t--\t%s%s: %s\n",
359 spaces( depth ), object_str( t->name ), target_bind[ (int) t->binding ] );
363 printf( "time\t--\t%s%s: %s",
364 spaces( depth ), object_str( t->name ), ctime( &t->time ) );
370 * Step 3: recursively make0() dependencies & headers.
373 /* Step 3a: recursively make0() dependencies. */
374 for ( c = t->depends; c; c = c->next )
376 int internal = t->flags & T_FLAG_INTERNAL;
378 /* Warn about circular deps, except for includes, which include each
381 if ( c->target->fate == T_FATE_INIT )
382 make0( c->target, ptime, depth + 1, counts, anyhow );
383 else if ( c->target->fate == T_FATE_MAKING && !internal )
384 printf( "warning: %s depends on itself\n", object_str( c->target->name ) );
387 /* Step 3b: recursively make0() internal includes node. */
389 make0( t->includes, p, depth + 1, counts, anyhow );
391 /* Step 3c: add dependencies' includes to our direct dependencies. */
394 for ( c = t->depends; c; c = c->next )
395 if ( c->target->includes )
396 incs = targetentry( incs, c->target->includes );
397 t->depends = targetchain( t->depends, incs );
401 * Step 4: compute time & fate
404 /* Step 4a: pick up dependencies' time and fate */
407 fate = T_FATE_STABLE;
408 for ( c = t->depends; c; c = c->next )
410 /* If LEAVES has been applied, we only heed the timestamps of the leaf
413 leaf = max( leaf, c->target->leaf );
415 if ( t->flags & T_FLAG_LEAVES )
421 last = max( last, c->target->time );
422 fate = max( fate, c->target->fate );
424 #ifdef OPT_GRAPH_DEBUG_EXT
426 if ( fate < c->target->fate )
427 printf( "fate change %s from %s to %s by dependency %s\n",
428 object_str( t->name ), target_fate[(int) fate], target_fate[(int) c->target->fate],
429 object_str( c->target->name ) );
433 /* Step 4b: pick up included headers time */
436 * If a header is newer than a temp source that includes it,
437 * the temp source will need building.
440 hlast = t->includes ? t->includes->time : 0;
442 /* Step 4c: handle NOUPDATE oddity.
444 * If a NOUPDATE file exists, mark it as having eternally old dependencies.
445 * Do not inherit our fate from our dependencies. Decide fate based only on
446 * other flags and our binding (done later).
448 if ( t->flags & T_FLAG_NOUPDATE )
450 #ifdef OPT_GRAPH_DEBUG_EXT
452 if ( fate != T_FATE_STABLE )
453 printf( "fate change %s back to stable, NOUPDATE.\n",
454 object_str( t->name ) );
460 /* Do not inherit our fate from our dependencies. Decide fate based only
461 * upon other flags and our binding (done later).
463 fate = T_FATE_STABLE;
466 /* Step 4d: determine fate: rebuild target or what? */
470 If can not find or make child, can not make target.
471 If children changed, make target.
472 If target missing, make it.
473 If children newer, make target.
474 If temp's children newer than parent, make temp.
475 If temp's headers newer than parent, make temp.
476 If deliberately touched, make it.
477 If up-to-date temp file present, use it.
478 If target newer than non-notfile parent, mark target newer.
481 Note this block runs from least to most stable:
482 as we make it further down the list, the target's
483 fate is getting stabler.
486 #ifdef OPT_GRAPH_DEBUG_EXT
491 if ( fate >= T_FATE_BROKEN )
493 fate = T_FATE_CANTMAKE;
495 else if ( fate >= T_FATE_SPOIL )
497 fate = T_FATE_UPDATE;
499 else if ( t->binding == T_BIND_MISSING )
501 fate = T_FATE_MISSING;
503 else if ( ( t->binding == T_BIND_EXISTS ) && ( last > t->time ) )
505 #ifdef OPT_GRAPH_DEBUG_EXT
508 fate = T_FATE_OUTDATED;
510 else if ( ( t->binding == T_BIND_PARENTS ) && ( last > p->time ) )
512 #ifdef OPT_GRAPH_DEBUG_EXT
515 fate = T_FATE_NEEDTMP;
517 else if ( ( t->binding == T_BIND_PARENTS ) && ( hlast > p->time ) )
519 fate = T_FATE_NEEDTMP;
521 else if ( t->flags & T_FLAG_TOUCHED )
523 fate = T_FATE_TOUCHED;
525 else if ( anyhow && !( t->flags & T_FLAG_NOUPDATE ) )
527 fate = T_FATE_TOUCHED;
529 else if ( ( t->binding == T_BIND_EXISTS ) && ( t->flags & T_FLAG_TEMP ) )
533 else if ( ( t->binding == T_BIND_EXISTS ) && p &&
534 ( p->binding != T_BIND_UNBOUND ) && ( t->time > p->time ) )
536 #ifdef OPT_GRAPH_DEBUG_EXT
543 fate = T_FATE_STABLE;
545 #ifdef OPT_GRAPH_DEBUG_EXT
546 if ( DEBUG_FATE && ( fate != savedFate ) )
548 if ( savedFate == T_FATE_STABLE )
549 printf( "fate change %s set to %s%s\n", object_str( t->name ),
550 target_fate[ fate ], oldTimeStamp ? " (by timestamp)" : "" );
552 printf( "fate change %s from %s to %s%s\n", object_str( t->name ),
553 target_fate[ savedFate ], target_fate[ fate ],
554 oldTimeStamp ? " (by timestamp)" : "" );
558 /* Step 4e: handle missing files */
559 /* If it is missing and there are no actions to create it, boom. */
560 /* If we can not make a target we do not care about it, okay. */
561 /* We could insist that there are updating actions for all missing */
562 /* files, but if they have dependencies we just pretend it is a NOTFILE. */
564 if ( ( fate == T_FATE_MISSING ) && !t->actions && !t->depends )
566 if ( t->flags & T_FLAG_NOCARE )
568 #ifdef OPT_GRAPH_DEBUG_EXT
570 printf( "fate change %s to STABLE from %s, "
571 "no actions, no dependencies and do not care\n",
572 object_str( t->name ), target_fate[ fate ] );
574 fate = T_FATE_STABLE;
578 printf( "don't know how to make %s\n", object_str( t->name ) );
579 fate = T_FATE_CANTFIND;
583 /* Step 4f: propagate dependencies' time & fate. */
584 /* Set leaf time to be our time only if this is a leaf. */
586 t->time = max( t->time, last );
587 t->leaf = leaf ? leaf : t->time ;
588 /* This target's fate may have been updated by virtue of following some
589 * target's rebuilds list, so only allow it to be increased to the fate we
590 * have calculated. Otherwise, grab its new fate.
592 if ( fate > t->fate )
597 /* Step 4g: if this target needs to be built, force rebuild everything in
598 * this target's rebuilds list.
600 if ( ( fate >= T_FATE_BUILD ) && ( fate < T_FATE_BROKEN ) )
604 * Step 5: sort dependencies by their update time.
607 if ( globs.newestfirst )
608 t->depends = make0sort( t->depends );
611 * Step 6: a little harmless tabulating for tracing purposes
614 /* Do not count or report interal includes nodes. */
615 if ( t->flags & T_FLAG_INTERNAL )
620 #ifdef OPT_IMPROVED_PATIENCE_EXT
623 if ( !( ++counts->targets % 1000 ) && DEBUG_MAKE )
624 printf( "...patience...\n" );
627 if ( fate == T_FATE_ISTMP )
629 else if ( fate == T_FATE_CANTFIND )
631 else if ( ( fate == T_FATE_CANTMAKE ) && t->actions )
633 else if ( ( fate >= T_FATE_BUILD ) && ( fate < T_FATE_BROKEN ) &&
638 if ( !( t->flags & T_FLAG_NOTFILE ) && ( fate >= T_FATE_SPOIL ) )
640 else if ( ( t->binding == T_BIND_EXISTS ) && p && ( t->time > p->time ) )
643 if ( DEBUG_MAKEPROG )
644 printf( "made%s\t%s\t%s%s\n", flag, target_fate[ (int) t->fate ],
645 spaces( depth ), object_str( t->name ) );
649 #ifdef OPT_GRAPH_DEBUG_EXT
651 static const char * target_name( TARGET * t )
653 static char buf[ 1000 ];
654 if ( t->flags & T_FLAG_INTERNAL )
656 sprintf( buf, "%s (internal node)", object_str( t->name ) );
659 return object_str( t->name );
664 * dependGraphOutput() - output the DG after make0 has run.
667 static void dependGraphOutput( TARGET * t, int depth )
671 if ( ( t->flags & T_FLAG_VISITED ) || !t->name || !t->boundname )
674 t->flags |= T_FLAG_VISITED;
680 case T_FATE_OUTDATED:
682 printf( "->%s%2d Name: %s\n", spaces( depth ), depth, target_name( t ) );
685 printf( " %s%2d Name: %s\n", spaces( depth ), depth, target_name( t ) );
689 if ( ! object_equal( t->name, t->boundname ) )
690 printf( " %s Loc: %s\n", spaces( depth ), object_str( t->boundname ) );
695 printf( " %s : Stable\n", spaces( depth ) );
698 printf( " %s : Newer\n", spaces( depth ) );
701 printf( " %s : Up to date temp file\n", spaces( depth ) );
704 printf( " %s : Temporary file, to be updated\n", spaces( depth ) );
707 printf( " %s : Been touched, updating it\n", spaces( depth ) );
710 printf( " %s : Missing, creating it\n", spaces( depth ) );
712 case T_FATE_OUTDATED:
713 printf( " %s : Outdated, updating it\n", spaces( depth ) );
716 printf( " %s : Rebuild, updating it\n", spaces( depth ) );
719 printf( " %s : Updating it\n", spaces( depth ) );
721 case T_FATE_CANTFIND:
722 printf( " %s : Can not find it\n", spaces( depth ) );
724 case T_FATE_CANTMAKE:
725 printf( " %s : Can make it\n", spaces( depth ) );
729 if ( t->flags & ~T_FLAG_VISITED )
731 printf( " %s : ", spaces( depth ) );
732 if ( t->flags & T_FLAG_TEMP ) printf( "TEMPORARY " );
733 if ( t->flags & T_FLAG_NOCARE ) printf( "NOCARE " );
734 if ( t->flags & T_FLAG_NOTFILE ) printf( "NOTFILE " );
735 if ( t->flags & T_FLAG_TOUCHED ) printf( "TOUCHED " );
736 if ( t->flags & T_FLAG_LEAVES ) printf( "LEAVES " );
737 if ( t->flags & T_FLAG_NOUPDATE ) printf( "NOUPDATE " );
741 for ( c = t->depends; c; c = c->next )
743 printf( " %s : Depends on %s (%s)", spaces( depth ),
744 target_name( c->target ), target_fate[ (int) c->target->fate ] );
745 if ( c->target->time == t->time )
746 printf( " (max time)");
750 for ( c = t->depends; c; c = c->next )
751 dependGraphOutput( c->target, depth + 1 );
757 * make0sort() - reorder TARGETS chain by their time (newest to oldest).
759 * We walk chain, taking each item and inserting it on the sorted result, with
760 * newest items at the front. This involves updating each of the TARGETS'
761 * c->next and c->tail. Note that we make c->tail a valid prev pointer for every
762 * entry. Normally, it is only valid at the head, where prev == tail. Note also
763 * that while tail is a loop, next ends at the end of the chain.
766 static TARGETS * make0sort( TARGETS * chain )
768 PROFILE_ENTER( MAKE_MAKE0SORT );
770 TARGETS * result = 0;
772 /* Walk the current target list. */
776 TARGETS * s = result;
780 /* Find point s in result for c. */
781 while ( s && ( s->target->time > c->target->time ) )
784 /* Insert c in front of s (might be 0). Do not even think of deciphering
787 c->next = s; /* good even if s = 0 */
788 if ( result == s ) result = c; /* new head of chain? */
789 if ( !s ) s = result; /* wrap to ensure a next */
790 if ( result != c ) s->tail->next = c; /* not head? be prev's next */
791 c->tail = s->tail; /* take on next's prev */
792 s->tail = c; /* make next's prev us */
795 PROFILE_EXIT( MAKE_MAKE0SORT );
800 static LIST * targets_to_update_ = L0;
803 void mark_target_for_updating( OBJECT * target )
805 targets_to_update_ = list_push_back( targets_to_update_, object_copy( target ) );
809 LIST * targets_to_update()
811 return targets_to_update_;
815 void clear_targets_to_update()
817 list_free( targets_to_update_ );
818 targets_to_update_ = L0;