1/* 2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc. 3 * 4 * This file is part of Jam - see jam.c for Copyright information. 5 */ 6 7/* 8 * make1.c - execute command to bring targets up to date 9 * 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(). 13 * 14 * External routines: 15 * 16 * make1() - execute commands to update a TARGET and all its dependents 17 * 18 * Internal routines, the recursive/asynchronous command executors: 19 * 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() 24 * 25 * Internal support routines: 26 * 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 31 * 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 50 */ 51 52# include "jam.h" 53 54# include "lists.h" 55# include "parse.h" 56# include "variable.h" 57# include "rules.h" 58 59# include "search.h" 60# include "newstr.h" 61# include "make.h" 62# include "command.h" 63# include "execcmd.h" 64 65static void make1a( TARGET *t, TARGET *parent ); 66static void make1b( TARGET *t ); 67static void make1c( TARGET *t ); 68static void make1d( void *closure, int status ); 69 70static CMD *make1cmds( ACTIONS *a0 ); 71static LIST *make1list( LIST *l, TARGETS *targets, int flags, 72 int *missingTargets ); 73static SETTINGS *make1settings( LIST *vars ); 74static void make1bind( TARGET *t, int warn ); 75 76/* Ugly static - it's too hard to carry it through the callbacks. */ 77 78static struct { 79 int failed; 80 int skipped; 81 int total; 82 int made; 83} counts[1] ; 84 85/* 86 * make1() - execute commands to update a TARGET and all its dependents 87 */ 88 89static int intr = 0; 90 91int 92make1( TARGET *t ) 93{ 94 memset( (char *)counts, 0, sizeof( *counts ) ); 95 96 /* Recursively make the target and its dependents */ 97 98 make1a( t, (TARGET *)0 ); 99 100 /* Wait for any outstanding commands to finish running. */ 101 102 while( execwait() ) 103 ; 104 105 /* Talk about it */ 106 107 if( counts->failed ) { 108 printf("\nBUILD FAILURE:\n"); 109 if( DEBUG_MAKE ) printf( "...failed updating %d target(s)...\n", counts->failed ); 110 } 111 112 if( DEBUG_MAKE && counts->skipped ) 113 printf( "...skipped %d target(s)...\n", counts->skipped ); 114 115 if( DEBUG_MAKE && counts->made ) 116 printf( "...updated %d target(s)...\n", counts->made ); 117 118 return counts->total != counts->made; 119} 120 121/* 122 * make1a() - recursively traverse target tree, calling make1b() 123 */ 124 125static void 126make1a( 127 TARGET *t, 128 TARGET *parent ) 129{ 130 TARGETS *c; 131 132 /* If the parent is the first to try to build this target */ 133 /* or this target is in the make1c() quagmire, arrange for the */ 134 /* parent to be notified when this target is built. */ 135 136 if( parent ) 137 switch( t->progress ) 138 { 139 case T_MAKE_INIT: 140 case T_MAKE_ACTIVE: 141 case T_MAKE_RUNNING: 142 t->parents = targetentry( t->parents, parent ); 143 parent->asynccnt++; 144 } 145 146 if( t->progress != T_MAKE_INIT ) 147 return; 148 149 /* Asynccnt counts the dependents preventing this target from */ 150 /* proceeding to make1b() for actual building. We start off with */ 151 /* a count of 1 to prevent anything from happening until we can */ 152 /* call all dependents. This 1 is accounted for when we call */ 153 /* make1b() ourselves, below. */ 154 155 t->asynccnt = 1; 156 157 /* Recurse on our dependents, manipulating progress to guard */ 158 /* against circular dependency. */ 159 160 t->progress = T_MAKE_ONSTACK; 161 162 for( c = t->depends; c && !intr; c = c->next ) 163 make1a( c->target, t ); 164 165 t->progress = T_MAKE_ACTIVE; 166 167 /* Now that all dependents have bumped asynccnt, we now allow */ 168 /* decrement our reference to asynccnt. */ 169 170 make1b( t ); 171} 172 173/* 174 * make1b() - dependents of target built, now build target with make1c() 175 */ 176 177static void 178make1b( TARGET *t ) 179{ 180 TARGETS *c; 181 const char *failed = "dependents"; 182 183 /* If any dependents are still outstanding, wait until they */ 184 /* call make1b() to signal their completion. */ 185 186 if( --t->asynccnt ) 187 return; 188 189 /* Now ready to build target 't'... if dependents built ok. */ 190 191 /* Collect status from dependents */ 192 193 for( c = t->depends; c; c = c->next ) 194 if( c->target->status > t->status ) 195 { 196 failed = c->target->name; 197 t->status = c->target->status; 198 } 199 200 /* If actions on deps have failed, bail. */ 201 /* Otherwise, execute all actions to make target */ 202 203 if( t->status == EXEC_CMD_FAIL && t->actions ) 204 { 205 ++counts->skipped; 206// printf( "...skipped %s for lack of %s...\n", t->name, failed ); 207 } 208 209 if( t->status == EXEC_CMD_OK ) 210 switch( t->fate ) 211 { 212 case T_FATE_INIT: 213 case T_FATE_MAKING: 214 /* shouldn't happen */ 215 216 case T_FATE_STABLE: 217 case T_FATE_NEWER: 218 break; 219 220 case T_FATE_CANTFIND: 221 case T_FATE_CANTMAKE: 222 t->status = EXEC_CMD_FAIL; 223 break; 224 225 case T_FATE_ISTMP: 226 if( DEBUG_MAKEQ ) 227 printf( "...using %s...\n", t->name ); 228 break; 229 230 case T_FATE_TOUCHED: 231 case T_FATE_MISSING: 232 case T_FATE_NEEDTMP: 233 case T_FATE_OUTDATED: 234 case T_FATE_UPDATE: 235 /* Set "on target" vars, build actions, unset vars */ 236 /* Set "progress" so that make1c() counts this target among */ 237 /* the successes/failures. */ 238 239 if( t->actions ) 240 { 241 ++counts->total; 242 243 if( DEBUG_MAKE && !( counts->total % 100 ) ) 244 printf( "...on %dth target...\n", counts->total ); 245 246 pushsettings( t->settings ); 247 t->cmds = (char *)make1cmds( t->actions ); 248 popsettings( t->settings ); 249 250 t->progress = T_MAKE_RUNNING; 251 } 252 253 break; 254 } 255 256 /* Call make1c() to begin the execution of the chain of commands */ 257 /* needed to build target. If we're not going to build target */ 258 /* (because of dependency failures or because no commands need to */ 259 /* be run) the chain will be empty and make1c() will directly */ 260 /* signal the completion of target. */ 261 262 make1c( t ); 263} 264 265/* 266 * make1c() - launch target's next command, call make1b() when done 267 */ 268 269static void 270make1c( TARGET *t ) 271{ 272 CMD *cmd = (CMD *)t->cmds; 273 274 /* If there are (more) commands to run to build this target */ 275 /* (and we haven't hit an error running earlier comands) we */ 276 /* launch the command with execcmd(). */ 277 278 /* If there are no more commands to run, we collect the status */ 279 /* from all the actions then report our completion to all the */ 280 /* parents. */ 281 282 if( cmd && t->status == EXEC_CMD_OK ) 283 { 284 if( DEBUG_MAKE ) 285 if( DEBUG_MAKEQ || ! ( cmd->rule->flags & RULE_QUIETLY ) ) 286 { 287 printf( "%s ", cmd->rule->name ); 288 list_print( lol_get( &cmd->args, 0 ) ); 289 printf( "\n" ); 290 } 291 292 if( DEBUG_EXEC ) 293 printf( "%s\n", cmd->buf ); 294 295 if( globs.cmdout ) 296 fprintf( globs.cmdout, "%s", cmd->buf ); 297 298 if( globs.noexec ) 299 { 300 make1d( t, EXEC_CMD_OK ); 301 } 302 else 303 { 304 fflush( stdout ); 305 execcmd( cmd->buf, make1d, t, cmd->shell ); 306 } 307 } 308 else 309 { 310 TARGETS *c; 311 ACTIONS *actions; 312 313 /* Collect status from actions, and distribute it as well */ 314 315 for( actions = t->actions; actions; actions = actions->next ) 316 if( actions->action->status > t->status ) 317 t->status = actions->action->status; 318 319 for( actions = t->actions; actions; actions = actions->next ) 320 if( t->status > actions->action->status ) 321 actions->action->status = t->status; 322 323 /* Tally success/failure for those we tried to update. */ 324 325 if( t->progress == T_MAKE_RUNNING ) 326 switch( t->status ) 327 { 328 case EXEC_CMD_OK: 329 ++counts->made; 330 break; 331 case EXEC_CMD_FAIL: 332 ++counts->failed; 333 break; 334 } 335 336 /* Tell parents dependent has been built */ 337 338 t->progress = T_MAKE_DONE; 339 340 for( c = t->parents; c; c = c->next ) 341 make1b( c->target ); 342 } 343} 344 345/* 346 * make1d() - handle command execution completion and call back make1c() 347 */ 348 349static void 350make1d( 351 void *closure, 352 int status ) 353{ 354 TARGET *t = (TARGET *)closure; 355 CMD *cmd = (CMD *)t->cmds; 356 357 /* Execcmd() has completed. All we need to do is fiddle with the */ 358 /* status and signal our completion so make1c() can run the next */ 359 /* command. On interrupts, we bail heavily. */ 360 361 if( status == EXEC_CMD_FAIL && ( cmd->rule->flags & RULE_IGNORE ) ) 362 status = EXEC_CMD_OK; 363 364 /* On interrupt, set intr so _everything_ fails */ 365 366 if( status == EXEC_CMD_INTR ) 367 ++intr; 368 369 if( status == EXEC_CMD_FAIL && DEBUG_MAKE ) 370 { 371 /* Print command text on failure */ 372 373 if( !DEBUG_EXEC ) 374 printf( "%s\n", cmd->buf ); 375 376 printf( "...failed %s ", cmd->rule->name ); 377 list_print( lol_get( &cmd->args, 0 ) ); 378 printf( "...\n" ); 379 380 if( globs.quitquick ) ++intr; 381 } 382 383 /* If the command was interrupted or failed and the target */ 384 /* is not "precious", remove the targets. */ 385 /* Precious == 'actions updated' -- the target maintains state. */ 386 387 if( status != EXEC_CMD_OK && !( cmd->rule->flags & RULE_UPDATED ) ) 388 { 389 LIST *targets = lol_get( &cmd->args, 0 ); 390 391 for( ; targets; targets = list_next( targets ) ) 392 if( !unlink( targets->string ) ) 393 printf( "...removing %s\n", targets->string ); 394 } 395 396 /* Free this command and call make1c() to move onto next command. */ 397 398 t->status = status; 399 t->cmds = (char *)cmd_next( cmd ); 400 401 cmd_free( cmd ); 402 403 make1c( t ); 404} 405 406/* 407 * make1cmds() - turn ACTIONS into CMDs, grouping, splitting, etc 408 * 409 * Essentially copies a chain of ACTIONs to a chain of CMDs, 410 * grouping RULE_TOGETHER actions, splitting RULE_PIECEMEAL actions, 411 * and handling RULE_UPDATED actions. The result is a chain of 412 * CMDs which can be expanded by var_string() and executed with 413 * execcmd(). 414 */ 415 416static CMD * 417make1cmds( ACTIONS *a0 ) 418{ 419 CMD *cmds = 0; 420 LIST *shell = var_get( "JAMSHELL" ); /* shell is per-target */ 421 422 /* Step through actions */ 423 /* Actions may be shared with other targets or grouped with */ 424 /* RULE_TOGETHER, so actions already seen are skipped. */ 425 426 for( ; a0; a0 = a0->next ) 427 { 428 RULE *rule = a0->action->rule; 429 SETTINGS *boundvars; 430 LIST *nt, *ns; 431 ACTIONS *a1; 432 CMD *cmd; 433 int start, chunk, length, maxline; 434 int missingTargets = 0; 435 int ruleFlags = rule->flags; 436 437 /* Only do rules with commands to execute. */ 438 /* If this action has already been executed, use saved status */ 439 440 if( !rule->actions || a0->action->running ) 441 continue; 442 443 a0->action->running = 1; 444 445 /* Make LISTS of targets and sources */ 446 /* If `execute together` has been specified for this rule, tack */ 447 /* on sources from each instance of this rule for this target. */ 448 449 nt = make1list( L0, a0->action->targets, 0 , &missingTargets ); 450 451 /* If a target is missing use all sources. */ 452 if (missingTargets) 453 ruleFlags &= ~RULE_UPDATED; 454 455 ns = make1list( L0, a0->action->sources, ruleFlags, NULL ); 456 457 if( ruleFlags & RULE_TOGETHER ) 458 for( a1 = a0->next; a1; a1 = a1->next ) 459 if( a1->action->rule == rule && !a1->action->running ) 460 { 461 ns = make1list( ns, a1->action->sources, ruleFlags, NULL ); 462 a1->action->running = 1; 463 } 464 465 /* If doing only updated (or existing) sources, but none have */ 466 /* been updated (or exist), skip this action. */ 467 468 if( !ns && ( ruleFlags & ( RULE_UPDATED | RULE_EXISTING ) ) ) 469 { 470 list_free( nt ); 471 continue; 472 } 473 474 /* If we had 'actions xxx bind vars' we bind the vars now */ 475 476 boundvars = make1settings( rule->bindlist ); 477 pushsettings( boundvars ); 478 479 /* 480 * Build command, starting with all source args. 481 * 482 * If cmd_new returns 0, it's because the resulting command 483 * length is > MAXLINE. In this case, we'll slowly reduce 484 * the number of source arguments presented until it does 485 * fit. This only applies to actions that allow PIECEMEAL 486 * commands. 487 * 488 * While reducing slowly takes a bit of compute time to get 489 * things just right, it's worth it to get as close to MAXLINE 490 * as possible, because launching the commands we're executing 491 * is likely to be much more compute intensive! 492 * 493 * Note we loop through at least once, for sourceless actions. 494 * 495 * Max line length is the action specific maxline or, if not 496 * given or bigger than MAXLINE, MAXLINE. 497 */ 498 499 start = 0; 500 chunk = length = list_length( ns ); 501 maxline = ruleFlags / RULE_MAXLINE; 502 maxline = maxline && maxline < MAXLINE ? maxline : MAXLINE; 503 504 do 505 { 506 /* Build cmd: cmd_new consumes its lists. */ 507 508 CMD *cmd = cmd_new( rule, 509 list_copy( L0, nt ), 510 list_sublist( ns, start, chunk ), 511 list_copy( L0, shell ), 512 maxline ); 513 514 if( cmd ) 515 { 516 /* It fit: chain it up. */ 517 518 if( !cmds ) cmds = cmd; 519 else cmds->tail->next = cmd; 520 cmds->tail = cmd; 521 start += chunk; 522 } 523 else if( ( ruleFlags & RULE_PIECEMEAL ) && chunk > 1 ) 524 { 525 /* Reduce chunk size slowly. */ 526 527 chunk = chunk * 9 / 10; 528 } 529 else 530 { 531 /* Too long and not splittable. */ 532 533 printf( "%s actions too long (max %d)!\n", 534 rule->name, maxline ); 535 exit( EXITBAD ); 536 } 537 } 538 while( start < length ); 539 540 /* These were always copied when used. */ 541 542 list_free( nt ); 543 list_free( ns ); 544 545 /* Free the variables whose values were bound by */ 546 /* 'actions xxx bind vars' */ 547 548 popsettings( boundvars ); 549 freesettings( boundvars ); 550 } 551 552 return cmds; 553} 554 555/* 556 * make1list() - turn a list of targets into a LIST, for $(<) and $(>) 557 */ 558 559static LIST * 560make1list( 561 LIST *l, 562 TARGETS *targets, 563 int flags, 564 int *missingTargets ) 565{ 566 for( ; targets; targets = targets->next ) 567 { 568 TARGET *t = targets->target; 569 570 /* Sources to 'actions existing' are never in the dependency */ 571 /* graph (if they were, they'd get built and 'existing' would */ 572 /* be superfluous, so throttle warning message about independent */ 573 /* targets. */ 574 575 if( t->binding == T_BIND_UNBOUND ) 576 make1bind( t, !( flags & RULE_EXISTING ) ); 577 578 if( ( flags & RULE_EXISTING ) && t->binding != T_BIND_EXISTS ) 579 continue; 580 581 if ( t->binding != T_BIND_EXISTS && missingTargets) 582 *missingTargets = 1; 583 584 if( ( flags & RULE_UPDATED ) && t->fate <= T_FATE_STABLE ) 585 continue; 586 587 /* Prohibit duplicates for RULE_TOGETHER */ 588 589 if( flags & RULE_TOGETHER ) 590 { 591 LIST *m; 592 593 for( m = l; m; m = m->next ) 594 if( !strcmp( m->string, t->boundname ) ) 595 break; 596 597 if( m ) 598 continue; 599 } 600 601 /* Build new list */ 602 603 l = list_new( l, t->boundname, 1 ); 604 } 605 606 return l; 607} 608 609/* 610 * make1settings() - for vars that get bound values, build up replacement lists 611 */ 612 613static SETTINGS * 614make1settings( LIST *vars ) 615{ 616 SETTINGS *settings = 0; 617 618 for( ; vars; vars = list_next( vars ) ) 619 { 620 LIST *l = var_get( vars->string ); 621 LIST *nl = 0; 622 623 for( ; l; l = list_next( l ) ) 624 { 625 TARGET *t = bindtarget( l->string ); 626 627 /* Make sure the target is bound, warning if it is not in the */ 628 /* dependency graph. */ 629 630 if( t->binding == T_BIND_UNBOUND ) 631 make1bind( t, 1 ); 632 633 /* Build new list */ 634 635 nl = list_new( nl, t->boundname, 1 ); 636 } 637 638 /* Add to settings chain */ 639 640 settings = addsettings( settings, 0, vars->string, nl ); 641 } 642 643 return settings; 644} 645 646/* 647 * make1bind() - bind targets that weren't bound in dependency analysis 648 * 649 * Spot the kludge! If a target is not in the dependency tree, it didn't 650 * get bound by make0(), so we have to do it here. Ugly. 651 */ 652 653static void 654make1bind( 655 TARGET *t, 656 int warn ) 657{ 658 if( t->flags & T_FLAG_NOTFILE ) 659 return; 660 661 /* Sources to 'actions existing' are never in the dependency */ 662 /* graph (if they were, they'd get built and 'existing' would */ 663 /* be superfluous, so throttle warning message about independent */ 664 /* targets. */ 665 666 if( warn ) 667 printf( "warning: using independent target %s\n", t->name ); 668 669 pushsettings( t->settings ); 670 t->boundname = search( t->name, &t->time ); 671 t->binding = t->time ? T_BIND_EXISTS : T_BIND_MISSING; 672 popsettings( t->settings ); 673} 674