Annotation of src/usr.bin/make/make.c, Revision 1.57
1.25 espie 1: /* $OpenPackages$ */
1.43 espie 2: /* $OpenBSD$ */
1.6 millert 3: /* $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $ */
1.1 deraadt 4:
5: /*
1.4 millert 6: * Copyright (c) 1988, 1989, 1990, 1993
7: * The Regents of the University of California. All rights reserved.
1.1 deraadt 8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.33 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: /*-
40: * make.c --
41: * The functions which perform the examination of targets and
42: * their suitability for creation
43: *
44: * Interface:
1.25 espie 45: * Make_Run Initialize things for the module and recreate
1.26 espie 46: * whatever needs recreating. Returns true if
47: * work was (or would have been) done and
48: * false
1.25 espie 49: * otherwise.
50: *
51: * Make_Update Update all parents of a given child. Performs
52: * various bookkeeping chores like the updating
53: * of the cmtime field of the parent, filling
54: * of the IMPSRC context variable, etc. It will
55: * place the parent on the toBeMade queue if it
56: * should be.
57: *
1.1 deraadt 58: */
59:
1.27 espie 60: #include <limits.h>
1.26 espie 61: #include <stdio.h>
1.43 espie 62: #include <signal.h>
1.52 espie 63: #include <stddef.h>
1.54 espie 64: #include <stdlib.h>
1.52 espie 65: #include <string.h>
66: #include <ohash.h>
1.26 espie 67: #include "config.h"
68: #include "defines.h"
69: #include "dir.h"
70: #include "job.h"
71: #include "suff.h"
72: #include "var.h"
73: #include "error.h"
74: #include "make.h"
75: #include "gnode.h"
76: #include "extern.h"
77: #include "timestamp.h"
1.37 espie 78: #include "engine.h"
1.26 espie 79: #include "lst.h"
1.43 espie 80: #include "targ.h"
1.56 espie 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;
1.25 espie 92:
1.52 espie 93: static struct ohash targets; /* stuff we must build */
1.1 deraadt 94:
1.25 espie 95: static void MakeAddChild(void *, void *);
96: static void MakeHandleUse(void *, void *);
1.26 espie 97: static bool MakeStartJobs(void);
1.25 espie 98: static void MakePrintStatus(void *, void *);
1.46 espie 99: static bool try_to_make_node(GNode *);
100: static void add_targets_to_make(Lst);
1.40 espie 101:
1.52 espie 102: static bool has_unmade_predecessor(GNode *);
103: static void requeue_successors(GNode *);
1.54 espie 104: static void random_setup(void);
105:
106: static bool randomize_queue;
107: long random_delay = 0;
108:
1.56 espie 109: bool
110: no_jobs_left()
111: {
112: return Array_IsEmpty(&toBeMade);
113: }
114:
1.54 espie 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: }
1.52 espie 137:
1.56 espie 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:
1.52 espie 157: static bool
158: has_unmade_predecessor(GNode *gn)
1.1 deraadt 159: {
1.52 espie 160: LstNode ln;
161:
162: if (Lst_IsEmpty(&gn->preds))
163: return false;
1.5 millert 164:
1.52 espie 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;
1.1 deraadt 177: }
1.25 espie 178:
1.15 espie 179: static void
1.52 espie 180: requeue_successors(GNode *gn)
1.1 deraadt 181: {
1.52 espie 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)
1.56 espie 192: Array_PushNew(&toBeMade, succ);
1.52 espie 193: }
1.1 deraadt 194: }
1.25 espie 195:
1.1 deraadt 196: /*-
197: *-----------------------------------------------------------------------
1.25 espie 198: * Make_Update --
1.1 deraadt 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
1.4 millert 201: * up-to-date node.
1.1 deraadt 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: *
1.25 espie 210: * If the child was made, the parent's childMade field will be set true
1.1 deraadt 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
1.35 espie 219: Make_Update(GNode *cgn) /* the child node */
1.1 deraadt 220: {
1.40 espie 221: GNode *pgn; /* the parent node */
222: char *cname; /* the child's name */
1.47 espie 223: LstNode ln; /* Element in parents list */
1.40 espie 224:
1.50 espie 225: cname = Var(TARGET_INDEX, cgn);
1.40 espie 226:
1.1 deraadt 227: /*
1.40 espie 228: * If the child was actually made, see what its modification time is
229: * now -- some rules won't actually update the file. If the file still
230: * doesn't exist, make its mtime now.
1.1 deraadt 231: */
1.48 espie 232: if (cgn->built_status != UPTODATE) {
1.40 espie 233: /*
234: * This is what Make does and it's actually a good thing, as it
235: * allows rules like
236: *
237: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
238: *
1.41 espie 239: * to function as intended. Unfortunately, thanks to the
240: * stateless nature of NFS, there are times when the
241: * modification time of a file created on a remote machine
1.40 espie 242: * will not be modified before the local stat() implied by
1.41 espie 243: * the Dir_MTime occurs, thus leading us to believe that the
244: * file is unchanged, wreaking havoc with files that depend
1.40 espie 245: * on this one.
246: */
1.43 espie 247: if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.40 espie 248: cgn->mtime = now;
249: if (DEBUG(MAKE))
250: printf("update time: %s\n", time_to_string(cgn->mtime));
251: }
252:
253: for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
254: pgn = (GNode *)Lst_Datum(ln);
1.55 espie 255: pgn->unmade--;
1.48 espie 256: if (pgn->must_make) {
1.52 espie 257: if (DEBUG(MAKE))
258: printf("%s--=%d ",
259: pgn->name, pgn->unmade);
1.40 espie 260:
261: if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
1.48 espie 262: if (cgn->built_status == MADE) {
1.40 espie 263: pgn->childMade = true;
1.41 espie 264: if (is_strictly_before(pgn->cmtime,
1.40 espie 265: cgn->mtime))
266: pgn->cmtime = cgn->mtime;
267: } else {
268: (void)Make_TimeStamp(pgn, cgn);
269: }
270: }
271: if (pgn->unmade == 0) {
272: /*
1.41 espie 273: * Queue the node up -- any unmade
274: * predecessors will be dealt with in
1.40 espie 275: * MakeStartJobs.
276: */
1.52 espie 277: if (DEBUG(MAKE))
278: printf("QUEUING ");
1.56 espie 279: Array_Push(&toBeMade, pgn);
1.40 espie 280: } else if (pgn->unmade < 0) {
1.52 espie 281: Error("Child %s discovered graph cycles through %s", cgn->name, pgn->name);
1.40 espie 282: }
283: }
1.1 deraadt 284: }
1.52 espie 285: if (DEBUG(MAKE))
286: printf("\n");
287: requeue_successors(cgn);
1.1 deraadt 288: }
1.25 espie 289:
1.46 espie 290: static bool
291: try_to_make_node(GNode *gn)
292: {
293: if (DEBUG(MAKE))
294: printf("Examining %s...", gn->name);
1.52 espie 295:
296: if (gn->unmade != 0) {
297: if (DEBUG(MAKE))
298: printf(" Requeuing (%d)\n", gn->unmade);
299: add_targets_to_make(&gn->children);
1.56 espie 300: Array_Push(&toBeMade, gn);
1.52 espie 301: return false;
302: }
303: if (has_been_built(gn)) {
304: if (DEBUG(MAKE))
305: printf(" already made\n");
306: return false;
307: }
308: if (has_unmade_predecessor(gn)) {
309: if (DEBUG(MAKE))
310: printf(" Dropping for now\n");
311: return false;
312: }
1.46 espie 313:
1.52 espie 314: Suff_FindDeps(gn);
315: if (gn->unmade != 0) {
316: if (DEBUG(MAKE))
317: printf(" Requeuing (after deps: %d)\n", gn->unmade);
318: add_targets_to_make(&gn->children);
319: return false;
1.46 espie 320: }
321: if (Make_OODate(gn)) {
322: if (DEBUG(MAKE))
323: printf("out-of-date\n");
324: if (queryFlag)
325: return true;
326: Make_DoAllVar(gn);
327: Job_Make(gn);
328: } else {
329: if (DEBUG(MAKE))
330: printf("up-to-date\n");
1.48 espie 331: gn->built_status = UPTODATE;
1.46 espie 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:
1.40 espie 348: /*
1.1 deraadt 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
1.26 espie 356: * returns true. At all other times, this function returns false.
1.1 deraadt 357: *
358: * Side Effects:
359: * Nodes are removed from the toBeMade queue and job table slots
360: * are filled.
361: *-----------------------------------------------------------------------
362: */
1.26 espie 363: static bool
1.35 espie 364: MakeStartJobs(void)
1.1 deraadt 365: {
1.40 espie 366: GNode *gn;
1.4 millert 367:
1.56 espie 368: while (!Job_Full() && (gn = Array_Pop(&toBeMade)) != NULL) {
1.46 espie 369: if (try_to_make_node(gn))
370: return true;
1.1 deraadt 371: }
1.40 espie 372: return false;
1.1 deraadt 373: }
1.25 espie 374:
1.1 deraadt 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.
1.25 espie 381: *
382: * Side Effects:
383: * A message may be printed.
1.1 deraadt 384: *-----------------------------------------------------------------------
385: */
1.15 espie 386: static void
1.35 espie 387: MakePrintStatus(
388: void *gnp, /* Node to examine */
389: void *cyclep) /* True if gn->unmade being non-zero implies
1.1 deraadt 390: * a cycle in the graph, not an error in an
391: * inferior */
392: {
1.40 espie 393: GNode *gn = (GNode *)gnp;
394: bool cycle = *(bool *)cyclep;
1.48 espie 395: if (gn->built_status == UPTODATE) {
1.40 espie 396: printf("`%s' is up to date.\n", gn->name);
397: } else if (gn->unmade != 0) {
398: if (cycle) {
399: bool t = true;
400: /*
1.41 espie 401: * If printing cycles and came to one that has unmade
402: * children, print out the cycle by recursing on its
1.40 espie 403: * children. Note a cycle like:
404: * a : b
405: * b : c
406: * c : b
1.41 espie 407: * will cause this to erroneously complain about a
1.40 espie 408: * being in the cycle, but this is a good approximation.
409: */
1.48 espie 410: if (gn->built_status == CYCLE) {
1.40 espie 411: Error("Graph cycles through `%s'", gn->name);
1.48 espie 412: gn->built_status = ENDCYCLE;
1.40 espie 413: Lst_ForEach(&gn->children, MakePrintStatus, &t);
1.49 espie 414: gn->built_status = UNKNOWN;
1.48 espie 415: } else if (gn->built_status != ENDCYCLE) {
416: gn->built_status = CYCLE;
1.40 espie 417: Lst_ForEach(&gn->children, MakePrintStatus, &t);
418: }
419: } else {
1.41 espie 420: printf("`%s' not remade because of errors.\n",
1.40 espie 421: gn->name);
422: }
1.1 deraadt 423: }
424: }
1.25 espie 425:
1.5 millert 426:
1.52 espie 427: static void
1.56 espie 428: MakeAddChild(void *to_addp, void *ap)
1.52 espie 429: {
430: GNode *gn = (GNode *)to_addp;
431:
432: if (!gn->must_make && !(gn->type & OP_USE))
1.56 espie 433: Array_Push((struct growableArray *)ap, gn);
1.52 espie 434: }
435:
436: static void
437: MakeHandleUse(void *pgn, void *cgn)
438: {
439: Make_HandleUse((GNode *)pgn, (GNode *)cgn);
440: }
441:
442: /* Add stuff to the toBeMade queue. we try to sort things so that stuff
443: * that can be done directly is done right away. This won't be perfect,
444: * since some dependencies are only discovered later (e.g., SuffFindDeps).
1.46 espie 445: */
446: static void
1.52 espie 447: add_targets_to_make(Lst todo)
1.46 espie 448: {
449: GNode *gn;
1.56 espie 450:
1.52 espie 451: unsigned int slot;
452:
1.56 espie 453: AppendList2Array(todo, &examine);
1.46 espie 454:
1.56 espie 455: while ((gn = Array_Pop(&examine)) != NULL) {
1.52 espie 456: if (gn->must_make) /* already known */
457: continue;
458: gn->must_make = true;
1.46 espie 459:
1.52 espie 460: slot = ohash_qlookup(&targets, gn->name);
461: if (!ohash_find(&targets, slot))
462: ohash_insert(&targets, slot, gn);
463:
464:
465: look_harder_for_target(gn);
466: /*
467: * Apply any .USE rules before looking for implicit
468: * dependencies to make sure everything that should have
469: * commands has commands ...
470: */
471: Lst_ForEach(&gn->children, MakeHandleUse, gn);
472: expand_all_children(gn);
1.46 espie 473:
1.52 espie 474: if (gn->unmade != 0) {
475: if (DEBUG(MAKE))
476: printf("%s: not queuing (%d unmade children)\n",
477: gn->name, gn->unmade);
478: Lst_ForEach(&gn->children, MakeAddChild,
479: &examine);
480: } else {
481: if (DEBUG(MAKE))
482: printf("%s: queuing\n", gn->name);
1.56 espie 483: Array_Push(&toBeMade, gn);
1.46 espie 484: }
485: }
1.56 espie 486: if (randomize_queue)
487: randomize_garray(&toBeMade);
1.46 espie 488: }
489:
1.1 deraadt 490: /*-
491: *-----------------------------------------------------------------------
1.6 millert 492: * Make_Run --
493: * Initialize the nodes to remake and the list of nodes which are
494: * ready to be made by doing a breadth-first traversal of the graph
495: * starting from the nodes in the given list. Once this traversal
496: * is finished, all the 'leaves' of the graph are in the toBeMade
497: * queue.
498: * Using this queue and the Job module, work back up the graph,
499: * calling on MakeStartJobs to keep the job table as full as
500: * possible.
501: *
1.1 deraadt 502: * Results:
1.26 espie 503: * true if work was done. false otherwise.
1.1 deraadt 504: *
505: * Side Effects:
1.48 espie 506: * The must_make field of all nodes involved in the creation of the given
1.6 millert 507: * targets is set to 1. The toBeMade list is set to contain all the
508: * 'leaves' of these subgraphs.
1.1 deraadt 509: *-----------------------------------------------------------------------
510: */
1.26 espie 511: bool
1.35 espie 512: Make_Run(Lst targs) /* the initial list of targets */
1.1 deraadt 513: {
1.52 espie 514: int errors; /* Number of errors the Job module reports */
515: GNode *gn;
516: unsigned int i;
517: bool cycle;
1.40 espie 518:
1.54 espie 519: /* wild guess at initial sizes */
1.56 espie 520: Array_Init(&toBeMade, 500);
521: Array_Init(&examine, 150);
1.52 espie 522: ohash_init(&targets, 10, &gnode_info);
1.54 espie 523: if (DEBUG(PARALLEL))
524: random_setup();
1.40 espie 525:
1.46 espie 526: add_targets_to_make(targs);
1.40 espie 527: if (queryFlag) {
528: /*
1.41 espie 529: * We wouldn't do any work unless we could start some jobs in
530: * the next loop... (we won't actually start any, of course,
1.40 espie 531: * this is just to see if any of the targets was out of date)
532: */
533: return MakeStartJobs();
534: } else {
535: /*
536: * Initialization. At the moment, no jobs are running and until
537: * some get started, nothing will happen since the remaining
1.41 espie 538: * upward traversal of the graph is performed by the routines
539: * in job.c upon the finishing of a job. So we fill the Job
1.40 espie 540: * table as much as we can before going into our loop.
541: */
542: (void)MakeStartJobs();
1.1 deraadt 543: }
1.4 millert 544:
1.1 deraadt 545: /*
1.40 espie 546: * Main Loop: The idea here is that the ending of jobs will take
547: * care of the maintenance of data structures and the waiting for output
548: * will cause us to be idle most of the time while our children run as
549: * much as possible. Because the job table is kept as full as possible,
550: * the only time when it will be empty is when all the jobs which need
551: * running have been run, so that is the end condition of this loop.
1.41 espie 552: * Note that the Job module will exit if there were any errors unless
1.40 espie 553: * the keepgoing flag was given.
1.1 deraadt 554: */
1.40 espie 555: while (!Job_Empty()) {
1.57 ! espie 556: handle_running_jobs();
1.40 espie 557: (void)MakeStartJobs();
558: }
559:
560: errors = Job_Finish();
1.52 espie 561: cycle = false;
1.40 espie 562:
1.52 espie 563: for (gn = ohash_first(&targets, &i); gn != NULL;
564: gn = ohash_next(&targets, &i)) {
565: if (has_been_built(gn))
566: continue;
567: cycle = true;
568: errors++;
569: printf("Error: target %s unaccounted for (%s)\n",
570: gn->name, status_to_string(gn));
571: }
1.1 deraadt 572: /*
1.40 espie 573: * Print the final status of each target. E.g. if it wasn't made
574: * because some inferior reported an error.
1.1 deraadt 575: */
1.52 espie 576: Lst_ForEach(targs, MakePrintStatus, &cycle);
577: if (errors)
578: Fatal("Errors while building");
1.4 millert 579:
1.40 espie 580: return true;
1.1 deraadt 581: }