Annotation of src/usr.bin/make/make.c, Revision 1.43
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.26 espie 63: #include "config.h"
64: #include "defines.h"
65: #include "dir.h"
66: #include "job.h"
67: #include "suff.h"
68: #include "var.h"
69: #include "error.h"
70: #include "make.h"
71: #include "gnode.h"
72: #include "extern.h"
73: #include "timestamp.h"
1.37 espie 74: #include "engine.h"
1.26 espie 75: #include "lst.h"
1.43 ! espie 76: #include "targ.h"
1.25 espie 77:
78: static LIST toBeMade; /* The current fringe of the graph. These
1.1 deraadt 79: * are nodes which await examination by
80: * MakeOODate. It is added to by
81: * Make_Update and subtracted from by
82: * MakeStartJobs */
1.25 espie 83: static int numNodes; /* Number of nodes to be processed. If this
1.1 deraadt 84: * is non-zero when Job_Empty() returns
1.26 espie 85: * true, there's a cycle in the graph */
1.1 deraadt 86:
1.25 espie 87: static void MakeAddChild(void *, void *);
88: static void MakeHandleUse(void *, void *);
1.26 espie 89: static bool MakeStartJobs(void);
1.25 espie 90: static void MakePrintStatus(void *, void *);
1.40 espie 91:
1.1 deraadt 92: /*-
93: *-----------------------------------------------------------------------
94: * MakeAddChild --
95: * Function used by Make_Run to add a child to the list l.
1.26 espie 96: * It will only add the child if its make field is false.
1.1 deraadt 97: *
98: * Side Effects:
99: * The given list is extended
100: *-----------------------------------------------------------------------
101: */
1.15 espie 102: static void
1.40 espie 103: MakeAddChild(void *to_addp, void *lp)
1.1 deraadt 104: {
1.40 espie 105: GNode *to_add = (GNode *)to_addp;
106: Lst l = (Lst)lp;
1.5 millert 107:
1.40 espie 108: if (!to_add->make && !(to_add->type & OP_USE))
109: Lst_EnQueue(l, to_add);
1.1 deraadt 110: }
1.25 espie 111:
1.15 espie 112: static void
1.40 espie 113: MakeHandleUse(void *pgn, void *cgn)
1.1 deraadt 114: {
1.15 espie 115: Make_HandleUse((GNode *)pgn, (GNode *)cgn);
1.1 deraadt 116: }
1.25 espie 117:
1.1 deraadt 118: /*-
119: *-----------------------------------------------------------------------
1.25 espie 120: * Make_Update --
1.1 deraadt 121: * Perform update on the parents of a node. Used by JobFinish once
122: * a node has been dealt with and by MakeStartJobs if it finds an
1.4 millert 123: * up-to-date node.
1.1 deraadt 124: *
125: * Results:
126: * Always returns 0
127: *
128: * Side Effects:
129: * The unmade field of pgn is decremented and pgn may be placed on
130: * the toBeMade queue if this field becomes 0.
131: *
1.25 espie 132: * If the child was made, the parent's childMade field will be set true
1.1 deraadt 133: * and its cmtime set to now.
134: *
135: * If the child wasn't made, the cmtime field of the parent will be
136: * altered if the child's mtime is big enough.
137: *
138: * Finally, if the child is the implied source for the parent, the
139: * parent's IMPSRC variable is set appropriately.
140: *
141: *-----------------------------------------------------------------------
142: */
143: void
1.35 espie 144: Make_Update(GNode *cgn) /* the child node */
1.1 deraadt 145: {
1.40 espie 146: GNode *pgn; /* the parent node */
147: char *cname; /* the child's name */
148: LstNode ln; /* Element in parents and iParents lists */
149:
150: cname = Varq_Value(TARGET_INDEX, cgn);
151:
1.1 deraadt 152: /*
1.40 espie 153: * If the child was actually made, see what its modification time is
154: * now -- some rules won't actually update the file. If the file still
155: * doesn't exist, make its mtime now.
1.1 deraadt 156: */
1.40 espie 157: if (cgn->made != UPTODATE) {
158: /*
159: * This is what Make does and it's actually a good thing, as it
160: * allows rules like
161: *
162: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
163: *
1.41 espie 164: * to function as intended. Unfortunately, thanks to the
165: * stateless nature of NFS, there are times when the
166: * modification time of a file created on a remote machine
1.40 espie 167: * will not be modified before the local stat() implied by
1.41 espie 168: * the Dir_MTime occurs, thus leading us to believe that the
169: * file is unchanged, wreaking havoc with files that depend
1.40 espie 170: * on this one.
171: */
1.43 ! espie 172: if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.40 espie 173: cgn->mtime = now;
174: if (DEBUG(MAKE))
175: printf("update time: %s\n", time_to_string(cgn->mtime));
176: }
177:
178: for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
179: pgn = (GNode *)Lst_Datum(ln);
180: if (pgn->make) {
181: pgn->unmade--;
182:
183: if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
184: if (cgn->made == MADE) {
185: pgn->childMade = true;
1.41 espie 186: if (is_strictly_before(pgn->cmtime,
1.40 espie 187: cgn->mtime))
188: pgn->cmtime = cgn->mtime;
189: } else {
190: (void)Make_TimeStamp(pgn, cgn);
191: }
192: }
193: if (pgn->unmade == 0) {
194: /*
1.41 espie 195: * Queue the node up -- any unmade
196: * predecessors will be dealt with in
1.40 espie 197: * MakeStartJobs.
198: */
199: Lst_EnQueue(&toBeMade, pgn);
200: } else if (pgn->unmade < 0) {
201: Error("Graph cycles through %s", pgn->name);
202: }
203: }
1.1 deraadt 204: }
1.41 espie 205: /* Deal with successor nodes. If any is marked for making and has an
206: * unmade count of 0, has not been made and isn't in the examination
207: * queue, it means we need to place it in the queue as it restrained
1.40 espie 208: * itself before. */
209: for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
210: GNode *succ = (GNode *)Lst_Datum(ln);
211:
212: if (succ->make && succ->unmade == 0 && succ->made == UNMADE)
213: (void)Lst_QueueNew(&toBeMade, succ);
1.1 deraadt 214: }
1.4 millert 215:
1.40 espie 216: /* Set the .PREFIX and .IMPSRC variables for all the implied parents
217: * of this node. */
218: {
219: char *cpref = Varq_Value(PREFIX_INDEX, cgn);
220:
221: for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Adv(ln)) {
222: pgn = (GNode *)Lst_Datum(ln);
223: if (pgn->make) {
224: Varq_Set(IMPSRC_INDEX, cname, pgn);
225: Varq_Set(PREFIX_INDEX, cpref, pgn);
1.1 deraadt 226: }
227: }
228: }
1.40 espie 229:
1.1 deraadt 230: }
1.25 espie 231:
1.40 espie 232: /*
1.1 deraadt 233: *-----------------------------------------------------------------------
234: * MakeStartJobs --
235: * Start as many jobs as possible.
236: *
237: * Results:
238: * If the query flag was given to pmake, no job will be started,
239: * but as soon as an out-of-date target is found, this function
1.26 espie 240: * returns true. At all other times, this function returns false.
1.1 deraadt 241: *
242: * Side Effects:
243: * Nodes are removed from the toBeMade queue and job table slots
244: * are filled.
245: *-----------------------------------------------------------------------
246: */
1.26 espie 247: static bool
1.35 espie 248: MakeStartJobs(void)
1.1 deraadt 249: {
1.40 espie 250: GNode *gn;
1.4 millert 251:
1.40 espie 252: while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
253: if (DEBUG(MAKE))
254: printf("Examining %s...", gn->name);
255: /*
256: * Make sure any and all predecessors that are going to be made,
257: * have been.
258: */
259: if (!Lst_IsEmpty(&gn->preds)) {
260: LstNode ln;
1.1 deraadt 261:
1.41 espie 262: for (ln = Lst_First(&gn->preds); ln != NULL;
1.40 espie 263: ln = Lst_Adv(ln)){
264: GNode *pgn = (GNode *)Lst_Datum(ln);
265:
266: if (pgn->make && pgn->made == UNMADE) {
267: if (DEBUG(MAKE))
268: printf("predecessor %s not made yet.\n", pgn->name);
269: break;
270: }
271: }
272: /*
1.41 espie 273: * If ln isn't NULL, there's a predecessor as yet
1.40 espie 274: * unmade, so we just drop this node on the floor. When
1.41 espie 275: * the node in question has been made, it will notice
276: * this node as being ready to make but as yet unmade
1.40 espie 277: * and will place the node on the queue.
278: */
279: if (ln != NULL)
280: continue;
1.1 deraadt 281: }
1.4 millert 282:
1.40 espie 283: numNodes--;
284: if (Make_OODate(gn)) {
285: if (DEBUG(MAKE))
286: printf("out-of-date\n");
287: if (queryFlag)
288: return true;
289: Make_DoAllVar(gn);
290: Job_Make(gn);
291: } else {
292: if (DEBUG(MAKE))
293: printf("up-to-date\n");
294: gn->made = UPTODATE;
295: if (gn->type & OP_JOIN) {
296: /*
1.41 espie 297: * Even for an up-to-date .JOIN node, we need it
298: * to have its context variables so references
299: * to it get the correct value for .TARGET when
300: * building up the context variables of its
1.40 espie 301: * parent(s)...
302: */
303: Make_DoAllVar(gn);
304: }
1.4 millert 305:
1.40 espie 306: Make_Update(gn);
307: }
1.1 deraadt 308: }
1.40 espie 309: return false;
1.1 deraadt 310: }
1.25 espie 311:
1.1 deraadt 312: /*-
313: *-----------------------------------------------------------------------
314: * MakePrintStatus --
315: * Print the status of a top-level node, viz. it being up-to-date
316: * already or not created due to an error in a lower level.
317: * Callback function for Make_Run via Lst_ForEach.
1.25 espie 318: *
319: * Side Effects:
320: * A message may be printed.
1.1 deraadt 321: *-----------------------------------------------------------------------
322: */
1.15 espie 323: static void
1.35 espie 324: MakePrintStatus(
325: void *gnp, /* Node to examine */
326: void *cyclep) /* True if gn->unmade being non-zero implies
1.1 deraadt 327: * a cycle in the graph, not an error in an
328: * inferior */
329: {
1.40 espie 330: GNode *gn = (GNode *)gnp;
331: bool cycle = *(bool *)cyclep;
332: if (gn->made == UPTODATE) {
333: printf("`%s' is up to date.\n", gn->name);
334: } else if (gn->unmade != 0) {
335: if (cycle) {
336: bool t = true;
337: /*
1.41 espie 338: * If printing cycles and came to one that has unmade
339: * children, print out the cycle by recursing on its
1.40 espie 340: * children. Note a cycle like:
341: * a : b
342: * b : c
343: * c : b
1.41 espie 344: * will cause this to erroneously complain about a
1.40 espie 345: * being in the cycle, but this is a good approximation.
346: */
347: if (gn->made == CYCLE) {
348: Error("Graph cycles through `%s'", gn->name);
349: gn->made = ENDCYCLE;
350: Lst_ForEach(&gn->children, MakePrintStatus, &t);
351: gn->made = UNMADE;
352: } else if (gn->made != ENDCYCLE) {
353: gn->made = CYCLE;
354: Lst_ForEach(&gn->children, MakePrintStatus, &t);
355: }
356: } else {
1.41 espie 357: printf("`%s' not remade because of errors.\n",
1.40 espie 358: gn->name);
359: }
1.1 deraadt 360: }
361: }
1.25 espie 362:
1.5 millert 363:
1.1 deraadt 364: /*-
365: *-----------------------------------------------------------------------
1.6 millert 366: * Make_Run --
367: * Initialize the nodes to remake and the list of nodes which are
368: * ready to be made by doing a breadth-first traversal of the graph
369: * starting from the nodes in the given list. Once this traversal
370: * is finished, all the 'leaves' of the graph are in the toBeMade
371: * queue.
372: * Using this queue and the Job module, work back up the graph,
373: * calling on MakeStartJobs to keep the job table as full as
374: * possible.
375: *
1.1 deraadt 376: * Results:
1.26 espie 377: * true if work was done. false otherwise.
1.1 deraadt 378: *
379: * Side Effects:
1.6 millert 380: * The make field of all nodes involved in the creation of the given
381: * targets is set to 1. The toBeMade list is set to contain all the
382: * 'leaves' of these subgraphs.
1.1 deraadt 383: *-----------------------------------------------------------------------
384: */
1.26 espie 385: bool
1.35 espie 386: Make_Run(Lst targs) /* the initial list of targets */
1.1 deraadt 387: {
1.40 espie 388: GNode *gn; /* a temporary pointer */
389: LIST examine; /* List of targets to examine */
390: int errors; /* Number of errors the Job module reports */
391:
392: Static_Lst_Init(&toBeMade);
393:
394: Lst_Clone(&examine, targs, NOCOPY);
395: numNodes = 0;
396:
397: /*
1.41 espie 398: * Make an initial downward pass over the graph, marking nodes to be
1.40 espie 399: * made as we go down. We call Suff_FindDeps to find where a node is and
400: * to get some children for it if it has none and also has no commands.
401: * If the node is a leaf, we stick it on the toBeMade queue to
402: * be looked at in a minute, otherwise we add its children to our queue
403: * and go on about our business.
404: */
405: while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
406: if (!gn->make) {
407: gn->make = true;
408: numNodes++;
409:
1.43 ! espie 410: look_harder_for_target(gn);
1.40 espie 411: /*
1.41 espie 412: * Apply any .USE rules before looking for implicit
1.40 espie 413: * dependencies to make sure everything that should have
414: * commands has commands ...
415: */
416: Lst_ForEach(&gn->children, MakeHandleUse, gn);
417: Suff_FindDeps(gn);
418:
419: if (gn->unmade != 0)
1.41 espie 420: Lst_ForEach(&gn->children, MakeAddChild,
1.40 espie 421: &examine);
422: else
423: Lst_EnQueue(&toBeMade, gn);
424: }
425: }
426:
427: if (queryFlag) {
428: /*
1.41 espie 429: * We wouldn't do any work unless we could start some jobs in
430: * the next loop... (we won't actually start any, of course,
1.40 espie 431: * this is just to see if any of the targets was out of date)
432: */
433: return MakeStartJobs();
434: } else {
435: /*
436: * Initialization. At the moment, no jobs are running and until
437: * some get started, nothing will happen since the remaining
1.41 espie 438: * upward traversal of the graph is performed by the routines
439: * in job.c upon the finishing of a job. So we fill the Job
1.40 espie 440: * table as much as we can before going into our loop.
441: */
442: (void)MakeStartJobs();
1.1 deraadt 443: }
1.4 millert 444:
1.1 deraadt 445: /*
1.40 espie 446: * Main Loop: The idea here is that the ending of jobs will take
447: * care of the maintenance of data structures and the waiting for output
448: * will cause us to be idle most of the time while our children run as
449: * much as possible. Because the job table is kept as full as possible,
450: * the only time when it will be empty is when all the jobs which need
451: * running have been run, so that is the end condition of this loop.
1.41 espie 452: * Note that the Job module will exit if there were any errors unless
1.40 espie 453: * the keepgoing flag was given.
1.1 deraadt 454: */
1.40 espie 455: while (!Job_Empty()) {
456: Job_CatchOutput();
1.42 espie 457: Job_CatchChildren();
1.40 espie 458: (void)MakeStartJobs();
459: }
460:
461: errors = Job_Finish();
462:
1.1 deraadt 463: /*
1.40 espie 464: * Print the final status of each target. E.g. if it wasn't made
465: * because some inferior reported an error.
1.1 deraadt 466: */
1.40 espie 467: errors = errors == 0 && numNodes != 0;
468: Lst_ForEach(targs, MakePrintStatus, &errors);
1.4 millert 469:
1.40 espie 470: return true;
1.1 deraadt 471: }