1 /* $OpenBSD: make.c,v 1.63 2012/04/21 04:35:32 guenther Exp $ */ 2 /* $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1989, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * Copyright (c) 1989 by Berkeley Softworks 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Adam de Boor. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 /*- 39 * make.c -- 40 * The functions which perform the examination of targets and 41 * their suitability for creation 42 * 43 * Interface: 44 * Make_Run Initialize things for the module and recreate 45 * whatever needs recreating. Returns true if 46 * work was (or would have been) done and 47 * false 48 * otherwise. 49 * 50 * Make_Update Update all parents of a given child. Performs 51 * various bookkeeping chores like the updating 52 * of the cmtime field of the parent, filling 53 * of the IMPSRC context variable, etc. It will 54 * place the parent on the toBeMade queue if it 55 * should be. 56 * 57 */ 58 59 #include <limits.h> 60 #include <stdio.h> 61 #include <signal.h> 62 #include <stddef.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <ohash.h> 66 #include "config.h" 67 #include "defines.h" 68 #include "dir.h" 69 #include "job.h" 70 #include "suff.h" 71 #include "var.h" 72 #include "error.h" 73 #include "make.h" 74 #include "gnode.h" 75 #include "extern.h" 76 #include "timestamp.h" 77 #include "engine.h" 78 #include "lst.h" 79 #include "targ.h" 80 #include "targequiv.h" 81 #include "garray.h" 82 #include "memory.h" 83 84 /* what gets added each time. Kept as one static array so that it doesn't 85 * get resized every time. 86 */ 87 static struct growableArray examine; 88 /* The current fringe of the graph. These are nodes which await examination by 89 * MakeOODate. It is added to by Make_Update and subtracted from by 90 * MakeStartJobs */ 91 static struct growableArray toBeMade; 92 93 static struct ohash targets; /* stuff we must build */ 94 95 static void MakeAddChild(void *, void *); 96 static void MakeHandleUse(void *, void *); 97 static bool MakeStartJobs(void); 98 static void MakePrintStatus(void *, void *); 99 static bool try_to_make_node(GNode *); 100 static void add_targets_to_make(Lst); 101 102 static bool has_unmade_predecessor(GNode *); 103 static void requeue_successors(GNode *); 104 static void random_setup(void); 105 106 static bool randomize_queue; 107 long random_delay = 0; 108 109 bool 110 no_jobs_left() 111 { 112 return Array_IsEmpty(&toBeMade); 113 } 114 115 static void 116 random_setup() 117 { 118 randomize_queue = Var_Definedi("RANDOM_ORDER", NULL); 119 120 if (Var_Definedi("RANDOM_DELAY", NULL)) 121 random_delay = strtonum(Var_Value("RANDOM_DELAY"), 0, 1000, 122 NULL) * 1000000; 123 124 if (randomize_queue || random_delay) { 125 unsigned int random_seed; 126 char *t; 127 128 t = Var_Value("RANDOM_SEED"); 129 if (t != NULL) 130 random_seed = strtonum(t, 0, UINT_MAX, NULL); 131 else 132 random_seed = time(NULL); 133 fprintf(stderr, "RANDOM_SEED=%u\n", random_seed); 134 srandom(random_seed); 135 } 136 } 137 138 static void 139 randomize_garray(struct growableArray *g) 140 { 141 /* This is a fairly standard algorithm to randomize an array. */ 142 unsigned int i, v; 143 GNode *e; 144 145 for (i = g->n; i > 0; i--) { 146 v = random() % i; 147 if (v == i-1) 148 continue; 149 else { 150 e = g->a[i-1]; 151 g->a[i-1] = g->a[v]; 152 g->a[v] = e; 153 } 154 } 155 } 156 157 static bool 158 has_unmade_predecessor(GNode *gn) 159 { 160 LstNode ln; 161 162 if (Lst_IsEmpty(&gn->preds)) 163 return false; 164 165 166 for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)) { 167 GNode *pgn = (GNode *)Lst_Datum(ln); 168 169 if (pgn->must_make && pgn->built_status == UNKNOWN) { 170 if (DEBUG(MAKE)) 171 printf("predecessor %s not made yet.\n", 172 pgn->name); 173 return true; 174 } 175 } 176 return false; 177 } 178 179 static void 180 requeue_successors(GNode *gn) 181 { 182 LstNode ln; 183 /* Deal with successor nodes. If any is marked for making and has an 184 * unmade count of 0, has not been made and isn't in the examination 185 * queue, it means we need to place it in the queue as it restrained 186 * itself before. */ 187 for (ln = Lst_First(&gn->successors); ln != NULL; ln = Lst_Adv(ln)) { 188 GNode *succ = (GNode *)Lst_Datum(ln); 189 190 if (succ->must_make && succ->unmade == 0 191 && succ->built_status == UNKNOWN) 192 Array_PushNew(&toBeMade, succ); 193 } 194 } 195 196 /*- 197 *----------------------------------------------------------------------- 198 * Make_Update -- 199 * Perform update on the parents of a node. Used by JobFinish once 200 * a node has been dealt with and by MakeStartJobs if it finds an 201 * up-to-date node. 202 * 203 * Results: 204 * Always returns 0 205 * 206 * Side Effects: 207 * The unmade field of pgn is decremented and pgn may be placed on 208 * the toBeMade queue if this field becomes 0. 209 * 210 * If the child was made, the parent's childMade field will be set true 211 * and its cmtime set to now. 212 * 213 * If the child wasn't made, the cmtime field of the parent will be 214 * altered if the child's mtime is big enough. 215 * 216 *----------------------------------------------------------------------- 217 */ 218 void 219 Make_Update(GNode *cgn) /* the child node */ 220 { 221 GNode *pgn; /* the parent node */ 222 LstNode ln; /* Element in parents list */ 223 224 /* 225 * If the child was actually made, see what its modification time is 226 * now -- some rules won't actually update the file. If the file still 227 * doesn't exist, make its mtime now. 228 */ 229 if (cgn->built_status != UPTODATE) { 230 /* 231 * This is what Make does and it's actually a good thing, as it 232 * allows rules like 233 * 234 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h 235 * 236 * to function as intended. Unfortunately, thanks to the 237 * stateless nature of NFS, there are times when the 238 * modification time of a file created on a remote machine 239 * will not be modified before the local stat() implied by 240 * the Dir_MTime occurs, thus leading us to believe that the 241 * file is unchanged, wreaking havoc with files that depend 242 * on this one. 243 */ 244 if (noExecute || is_out_of_date(Dir_MTime(cgn))) 245 ts_set_from_now(cgn->mtime); 246 if (DEBUG(MAKE)) 247 printf("update time: %s\n", time_to_string(cgn->mtime)); 248 } 249 250 /* SIB: this is where I should mark the build as finished */ 251 cgn->build_lock = false; 252 for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) { 253 pgn = (GNode *)Lst_Datum(ln); 254 /* SIB: there should be a siblings loop there */ 255 pgn->unmade--; 256 if (pgn->must_make) { 257 if (DEBUG(MAKE)) 258 printf("%s--=%d ", 259 pgn->name, pgn->unmade); 260 261 if ( ! (cgn->type & (OP_EXEC|OP_USE))) { 262 if (cgn->built_status == MADE) 263 pgn->childMade = true; 264 (void)Make_TimeStamp(pgn, cgn); 265 } 266 if (pgn->unmade == 0) { 267 /* 268 * Queue the node up -- any unmade 269 * predecessors will be dealt with in 270 * MakeStartJobs. 271 */ 272 if (DEBUG(MAKE)) 273 printf("QUEUING "); 274 Array_Push(&toBeMade, pgn); 275 } else if (pgn->unmade < 0) { 276 Error("Child %s discovered graph cycles through %s", cgn->name, pgn->name); 277 } 278 } 279 } 280 if (DEBUG(MAKE)) 281 printf("\n"); 282 requeue_successors(cgn); 283 } 284 285 static bool 286 try_to_make_node(GNode *gn) 287 { 288 if (DEBUG(MAKE)) 289 printf("Examining %s...", gn->name); 290 291 if (gn->unmade != 0) { 292 if (DEBUG(MAKE)) 293 printf(" Requeuing (%d)\n", gn->unmade); 294 add_targets_to_make(&gn->children); 295 Array_Push(&toBeMade, gn); 296 return false; 297 } 298 if (has_been_built(gn)) { 299 if (DEBUG(MAKE)) 300 printf(" already made\n"); 301 return false; 302 } 303 if (has_unmade_predecessor(gn)) { 304 if (DEBUG(MAKE)) 305 printf(" Dropping for now\n"); 306 return false; 307 } 308 309 /* SIB: this is where there should be a siblings loop */ 310 Suff_FindDeps(gn); 311 if (gn->unmade != 0) { 312 if (DEBUG(MAKE)) 313 printf(" Requeuing (after deps: %d)\n", gn->unmade); 314 add_targets_to_make(&gn->children); 315 return false; 316 } 317 if (Make_OODate(gn)) { 318 /* SIB: if a sibling is getting built, I don't build it right now */ 319 if (DEBUG(MAKE)) 320 printf("out-of-date\n"); 321 if (queryFlag) 322 return true; 323 /* SIB: this is where commands should get prepared */ 324 Make_DoAllVar(gn); 325 /* SIB: this is where I should make the gn as `being built */ 326 gn->build_lock = true; 327 Job_Make(gn); 328 } else { 329 if (DEBUG(MAKE)) 330 printf("up-to-date\n"); 331 gn->built_status = UPTODATE; 332 if (gn->type & OP_JOIN) { 333 /* 334 * Even for an up-to-date .JOIN node, we need it 335 * to have its context variables so references 336 * to it get the correct value for .TARGET when 337 * building up the context variables of its 338 * parent(s)... 339 */ 340 Make_DoAllVar(gn); 341 } 342 343 Make_Update(gn); 344 } 345 return false; 346 } 347 348 /* 349 *----------------------------------------------------------------------- 350 * MakeStartJobs -- 351 * Start as many jobs as possible. 352 * 353 * Results: 354 * If the query flag was given to pmake, no job will be started, 355 * but as soon as an out-of-date target is found, this function 356 * returns true. At all other times, this function returns false. 357 * 358 * Side Effects: 359 * Nodes are removed from the toBeMade queue and job table slots 360 * are filled. 361 *----------------------------------------------------------------------- 362 */ 363 static bool 364 MakeStartJobs(void) 365 { 366 GNode *gn; 367 368 while (can_start_job() && (gn = Array_Pop(&toBeMade)) != NULL) { 369 if (try_to_make_node(gn)) 370 return true; 371 } 372 return false; 373 } 374 375 /*- 376 *----------------------------------------------------------------------- 377 * MakePrintStatus -- 378 * Print the status of a top-level node, viz. it being up-to-date 379 * already or not created due to an error in a lower level. 380 * Callback function for Make_Run via Lst_ForEach. 381 * 382 * Side Effects: 383 * A message may be printed. 384 *----------------------------------------------------------------------- 385 */ 386 static void 387 MakePrintStatus( 388 void *gnp, /* Node to examine */ 389 void *cyclep) /* True if gn->unmade being non-zero implies 390 * a cycle in the graph, not an error in an 391 * inferior */ 392 { 393 GNode *gn = (GNode *)gnp; 394 bool cycle = *(bool *)cyclep; 395 if (gn->built_status == UPTODATE) { 396 printf("`%s' is up to date.\n", gn->name); 397 } else if (gn->unmade != 0) { 398 if (cycle) { 399 bool t = true; 400 /* 401 * If printing cycles and came to one that has unmade 402 * children, print out the cycle by recursing on its 403 * children. Note a cycle like: 404 * a : b 405 * b : c 406 * c : b 407 * will cause this to erroneously complain about a 408 * being in the cycle, but this is a good approximation. 409 */ 410 if (gn->built_status == CYCLE) { 411 Error("Graph cycles through `%s'", gn->name); 412 gn->built_status = ENDCYCLE; 413 Lst_ForEach(&gn->children, MakePrintStatus, &t); 414 gn->built_status = UNKNOWN; 415 } else if (gn->built_status != ENDCYCLE) { 416 gn->built_status = CYCLE; 417 Lst_ForEach(&gn->children, MakePrintStatus, &t); 418 } 419 } else { 420 printf("`%s' not remade because of errors.\n", 421 gn->name); 422 } 423 } 424 } 425 426 427 static void 428 MakeAddChild(void *to_addp, void *ap) 429 { 430 GNode *gn = (GNode *)to_addp; 431 432 if (!gn->must_make && !(gn->type & OP_USE)) 433 Array_Push((struct growableArray *)ap, gn); 434 } 435 436 static void 437 MakeHandleUse(void *cgnp, void *pgnp) 438 { 439 GNode *cgn = (GNode *)cgnp; 440 GNode *pgn = (GNode *)pgnp; 441 442 if (cgn->type & OP_USE) 443 Make_HandleUse(cgn, pgn); 444 } 445 446 /* Add stuff to the toBeMade queue. we try to sort things so that stuff 447 * that can be done directly is done right away. This won't be perfect, 448 * since some dependencies are only discovered later (e.g., SuffFindDeps). 449 */ 450 static void 451 add_targets_to_make(Lst todo) 452 { 453 GNode *gn; 454 455 unsigned int slot; 456 457 AppendList2Array(todo, &examine); 458 459 while ((gn = Array_Pop(&examine)) != NULL) { 460 if (gn->must_make) /* already known */ 461 continue; 462 gn->must_make = true; 463 464 slot = ohash_qlookup(&targets, gn->name); 465 if (!ohash_find(&targets, slot)) 466 ohash_insert(&targets, slot, gn); 467 468 469 look_harder_for_target(gn); 470 kludge_look_harder_for_target(gn); 471 /* 472 * Apply any .USE rules before looking for implicit 473 * dependencies to make sure everything that should have 474 * commands has commands ... 475 */ 476 Lst_ForEach(&gn->children, MakeHandleUse, gn); 477 expand_all_children(gn); 478 479 if (gn->unmade != 0) { 480 if (DEBUG(MAKE)) 481 printf("%s: not queuing (%d unmade children)\n", 482 gn->name, gn->unmade); 483 Lst_ForEach(&gn->children, MakeAddChild, 484 &examine); 485 } else { 486 if (DEBUG(MAKE)) 487 printf("%s: queuing\n", gn->name); 488 Array_Push(&toBeMade, gn); 489 } 490 } 491 if (randomize_queue) 492 randomize_garray(&toBeMade); 493 } 494 495 /*- 496 *----------------------------------------------------------------------- 497 * Make_Run -- 498 * Initialize the nodes to remake and the list of nodes which are 499 * ready to be made by doing a breadth-first traversal of the graph 500 * starting from the nodes in the given list. Once this traversal 501 * is finished, all the 'leaves' of the graph are in the toBeMade 502 * queue. 503 * Using this queue and the Job module, work back up the graph, 504 * calling on MakeStartJobs to keep the job table as full as 505 * possible. 506 * 507 * Results: 508 * true if work was done. false otherwise. 509 * 510 * Side Effects: 511 * The must_make field of all nodes involved in the creation of the given 512 * targets is set to 1. The toBeMade list is set to contain all the 513 * 'leaves' of these subgraphs. 514 *----------------------------------------------------------------------- 515 */ 516 bool 517 Make_Run(Lst targs) /* the initial list of targets */ 518 { 519 int errors; /* Number of errors the Job module reports */ 520 GNode *gn; 521 unsigned int i; 522 bool cycle; 523 524 /* wild guess at initial sizes */ 525 Array_Init(&toBeMade, 500); 526 Array_Init(&examine, 150); 527 ohash_init(&targets, 10, &gnode_info); 528 if (DEBUG(PARALLEL)) 529 random_setup(); 530 531 add_targets_to_make(targs); 532 if (queryFlag) { 533 /* 534 * We wouldn't do any work unless we could start some jobs in 535 * the next loop... (we won't actually start any, of course, 536 * this is just to see if any of the targets was out of date) 537 */ 538 return MakeStartJobs(); 539 } else { 540 /* 541 * Initialization. At the moment, no jobs are running and until 542 * some get started, nothing will happen since the remaining 543 * upward traversal of the graph is performed by the routines 544 * in job.c upon the finishing of a job. So we fill the Job 545 * table as much as we can before going into our loop. 546 */ 547 (void)MakeStartJobs(); 548 } 549 550 /* 551 * Main Loop: The idea here is that the ending of jobs will take 552 * care of the maintenance of data structures and the waiting for output 553 * will cause us to be idle most of the time while our children run as 554 * much as possible. Because the job table is kept as full as possible, 555 * the only time when it will be empty is when all the jobs which need 556 * running have been run, so that is the end condition of this loop. 557 * Note that the Job module will exit if there were any errors unless 558 * the keepgoing flag was given. 559 */ 560 while (!Job_Empty()) { 561 handle_running_jobs(); 562 (void)MakeStartJobs(); 563 } 564 565 errors = Job_Finish(); 566 cycle = false; 567 568 for (gn = ohash_first(&targets, &i); gn != NULL; 569 gn = ohash_next(&targets, &i)) { 570 if (has_been_built(gn)) 571 continue; 572 cycle = true; 573 errors++; 574 printf("Error: target %s unaccounted for (%s)\n", 575 gn->name, status_to_string(gn)); 576 } 577 /* 578 * Print the final status of each target. E.g. if it wasn't made 579 * because some inferior reported an error. 580 */ 581 Lst_ForEach(targs, MakePrintStatus, &cycle); 582 if (errors) 583 Fatal("Errors while building"); 584 585 return true; 586 } 587