glob omits "." and ".."; removed built-in that will never be ported
[k8jam.git] / make1.c
blob022374491b904519ac05690e43487fbfe1fe2cec
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
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
12 * marked by make0().
14 * External routines:
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
52 #include <unistd.h>
54 # include "jam.h"
56 # include "lists.h"
57 # include "parse.h"
58 # include "variable.h"
59 # include "rules.h"
61 # include "search.h"
62 # include "newstr.h"
63 # include "make.h"
64 # include "command.h"
65 # include "execcmd.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. */
79 static struct {
80 int failed;
81 int skipped;
82 int total;
83 int made;
84 } counts[1] ;
87 * make1() - execute commands to update a TARGET and all its dependents
90 static int intr = 0;
92 int
93 make1( TARGET *t )
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. */
103 while( execwait() )
106 /* Talk about it */
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()
124 static void
125 make1a(
126 TARGET *t,
127 TARGET *parent )
129 TARGETS *c;
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. */
135 if( parent )
136 switch( t->progress )
138 case T_MAKE_INIT:
139 case T_MAKE_ACTIVE:
140 case T_MAKE_RUNNING:
141 t->parents = targetentry( t->parents, parent );
142 parent->asynccnt++;
145 if( t->progress != T_MAKE_INIT )
146 return;
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. */
154 t->asynccnt = 1;
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. */
169 make1b( t );
173 * make1b() - dependents of target built, now build target with make1c()
176 static void
177 make1b( TARGET *t )
179 TARGETS *c;
180 const char *failed = "dependents";
182 /* If any dependents are still outstanding, wait until they */
183 /* call make1b() to signal their completion. */
185 if( --t->asynccnt )
186 return;
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 )
204 ++counts->skipped;
205 printf( "...skipped %s for lack of %s...\n", t->name, failed );
208 if( t->status == EXEC_CMD_OK )
209 switch( t->fate )
211 case T_FATE_INIT:
212 case T_FATE_MAKING:
213 /* shouldn't happen */
215 case T_FATE_STABLE:
216 case T_FATE_NEWER:
217 break;
219 case T_FATE_CANTFIND:
220 case T_FATE_CANTMAKE:
221 t->status = EXEC_CMD_FAIL;
222 break;
224 case T_FATE_ISTMP:
225 if( DEBUG_MAKEQ )
226 printf( "...using %s...\n", t->name );
227 break;
229 case T_FATE_TOUCHED:
230 case T_FATE_MISSING:
231 case T_FATE_NEEDTMP:
232 case T_FATE_OUTDATED:
233 case T_FATE_UPDATE:
234 /* Set "on target" vars, build actions, unset vars */
235 /* Set "progress" so that make1c() counts this target among */
236 /* the successes/failures. */
238 if( t->actions )
240 ++counts->total;
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;
252 break;
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. */
261 make1c( t );
265 * make1c() - launch target's next command, call make1b() when done
268 static void
269 make1c( TARGET *t )
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 */
279 /* parents. */
281 if( cmd && t->status == EXEC_CMD_OK )
283 if( DEBUG_MAKE )
284 if( DEBUG_MAKEQ || ! ( cmd->rule->flags & RULE_QUIETLY ) )
286 printf( "%s ", cmd->rule->name );
287 list_print( lol_get( &cmd->args, 0 ) );
288 printf( "\n" );
291 if( DEBUG_EXEC )
292 printf( "%s\n", cmd->buf );
294 if( globs.cmdout )
295 fprintf( globs.cmdout, "%s", cmd->buf );
297 if( globs.noexec )
299 make1d( t, EXEC_CMD_OK );
301 else
303 fflush( stdout );
304 execcmd( cmd->buf, make1d, t, cmd->shell );
307 else
309 TARGETS *c;
310 ACTIONS *actions;
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 )
325 switch( t->status )
327 case EXEC_CMD_OK:
328 ++counts->made;
329 break;
330 case EXEC_CMD_FAIL:
331 ++counts->failed;
332 break;
335 /* Tell parents dependent has been built */
337 t->progress = T_MAKE_DONE;
339 for( c = t->parents; c; c = c->next )
340 make1b( c->target );
345 * make1d() - handle command execution completion and call back make1c()
348 static void
349 make1d(
350 void *closure,
351 int status )
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 )
366 ++intr;
368 if( status == EXEC_CMD_FAIL && DEBUG_MAKE )
370 /* Print command text on failure */
372 if( !DEBUG_EXEC )
373 printf( "%s\n", cmd->buf );
375 printf( "...failed %s ", cmd->rule->name );
376 list_print( lol_get( &cmd->args, 0 ) );
377 printf( "...\n" );
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. */
397 t->status = status;
398 t->cmds = (char *)cmd_next( cmd );
400 cmd_free( cmd );
402 make1c( t );
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
412 * execcmd().
415 static CMD *
416 make1cmds( ACTIONS *a0 )
418 CMD *cmds = 0;
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;
428 SETTINGS *boundvars;
429 LIST *nt, *ns;
430 ACTIONS *a1;
431 /*k8: CMD *cmd;*/
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 )
438 continue;
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 ) ) )
462 list_free( nt );
463 continue;
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
478 * commands.
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.
491 start = 0;
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,
501 list_copy( L0, nt ),
502 list_sublist( ns, start, chunk ),
503 list_copy( L0, shell ),
504 maxline );
506 if( cmd )
508 /* It fit: chain it up. */
510 if( !cmds ) cmds = cmd;
511 else cmds->tail->next = cmd;
512 cmds->tail = cmd;
513 start += chunk;
515 else if( ( rule->flags & RULE_PIECEMEAL ) && chunk > 1 )
517 /* Reduce chunk size slowly. */
519 chunk = chunk * 9 / 10;
521 else
523 /* Too long and not splittable. */
525 printf( "%s actions too long (max %d)!\n",
526 rule->name, maxline );
527 exit( EXITBAD );
530 while( start < length );
532 /* These were always copied when used. */
534 list_free( nt );
535 list_free( ns );
537 /* Free the variables whose values were bound by */
538 /* 'actions xxx bind vars' */
540 popsettings( boundvars );
541 freesettings( boundvars );
544 return cmds;
548 * make1list() - turn a list of targets into a LIST, for $(<) and $(>)
551 static LIST *
552 make1list(
553 LIST *l,
554 TARGETS *targets,
555 int flags )
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 */
564 /* targets. */
566 if( t->binding == T_BIND_UNBOUND )
567 make1bind( t, !( flags & RULE_EXISTING ) );
569 if( ( flags & RULE_EXISTING ) && t->binding != T_BIND_EXISTS )
570 continue;
572 if( ( flags & RULE_UPDATED ) && t->fate <= T_FATE_STABLE )
573 continue;
575 /* Prohibit duplicates for RULE_TOGETHER */
577 if( flags & RULE_TOGETHER )
579 LIST *m;
581 for( m = l; m; m = m->next )
582 if( !strcmp( m->string, t->boundname ) )
583 break;
585 if( m )
586 continue;
589 /* Build new list */
591 l = list_new( l, t->boundname, 1 );
594 return l;
598 * make1settings() - for vars that get bound values, build up replacement lists
601 static SETTINGS *
602 make1settings( LIST *vars )
604 SETTINGS *settings = 0;
606 for( ; vars; vars = list_next( vars ) )
608 LIST *l = var_get( vars->string );
609 LIST *nl = 0;
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 )
619 make1bind( t, 1 );
621 /* Build new list */
623 nl = list_new( nl, t->boundname, 1 );
626 /* Add to settings chain */
628 settings = addsettings( settings, 0, vars->string, nl );
631 return settings;
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.
641 static void
642 make1bind(
643 TARGET *t,
644 int warn )
646 if( t->flags & T_FLAG_NOTFILE )
647 return;
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 */
652 /* targets. */
654 if( warn )
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 );