xref: /original-bsd/usr.bin/make/make.c (revision 0842ddeb)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)make.c	8.2 (Berkeley) 04/28/95";
15 #endif /* not lint */
16 
17 /*-
18  * make.c --
19  *	The functions which perform the examination of targets and
20  *	their suitability for creation
21  *
22  * Interface:
23  *	Make_Run 	    	Initialize things for the module and recreate
24  *	    	  	    	whatever needs recreating. Returns TRUE if
25  *	    	    	    	work was (or would have been) done and FALSE
26  *	    	  	    	otherwise.
27  *
28  *	Make_Update	    	Update all parents of a given child. Performs
29  *	    	  	    	various bookkeeping chores like the updating
30  *	    	  	    	of the cmtime field of the parent, filling
31  *	    	  	    	of the IMPSRC context variable, etc. It will
32  *	    	  	    	place the parent on the toBeMade queue if it
33  *	    	  	    	should be.
34  *
35  *	Make_TimeStamp	    	Function to set the parent's cmtime field
36  *	    	  	    	based on a child's modification time.
37  *
38  *	Make_DoAllVar	    	Set up the various local variables for a
39  *	    	  	    	target, including the .ALLSRC variable, making
40  *	    	  	    	sure that any variable that needs to exist
41  *	    	  	    	at the very least has the empty value.
42  *
43  *	Make_OODate 	    	Determine if a target is out-of-date.
44  *
45  *	Make_HandleUse	    	See if a child is a .USE node for a parent
46  *				and perform the .USE actions if so.
47  */
48 
49 #include    "make.h"
50 #include    "hash.h"
51 #include    "dir.h"
52 #include    "job.h"
53 
54 static Lst     	toBeMade;	/* The current fringe of the graph. These
55 				 * are nodes which await examination by
56 				 * MakeOODate. It is added to by
57 				 * Make_Update and subtracted from by
58 				 * MakeStartJobs */
59 static int  	numNodes;   	/* Number of nodes to be processed. If this
60 				 * is non-zero when Job_Empty() returns
61 				 * TRUE, there's a cycle in the graph */
62 
63 static int MakeAddChild __P((ClientData, ClientData));
64 static int MakeAddAllSrc __P((ClientData, ClientData));
65 static int MakeTimeStamp __P((ClientData, ClientData));
66 static int MakeHandleUse __P((ClientData, ClientData));
67 static Boolean MakeStartJobs __P((void));
68 static int MakePrintStatus __P((ClientData, ClientData));
69 /*-
70  *-----------------------------------------------------------------------
71  * Make_TimeStamp --
72  *	Set the cmtime field of a parent node based on the mtime stamp in its
73  *	child. Called from MakeOODate via Lst_ForEach.
74  *
75  * Results:
76  *	Always returns 0.
77  *
78  * Side Effects:
79  *	The cmtime of the parent node will be changed if the mtime
80  *	field of the child is greater than it.
81  *-----------------------------------------------------------------------
82  */
83 int
84 Make_TimeStamp (pgn, cgn)
85     GNode *pgn;	/* the current parent */
86     GNode *cgn;	/* the child we've just examined */
87 {
88     if (cgn->mtime > pgn->cmtime) {
89 	pgn->cmtime = cgn->mtime;
90     }
91     return (0);
92 }
93 
94 static int
95 MakeTimeStamp (pgn, cgn)
96     ClientData pgn;	/* the current parent */
97     ClientData cgn;	/* the child we've just examined */
98 {
99     return Make_TimeStamp((GNode *) pgn, (GNode *) cgn);
100 }
101 
102 /*-
103  *-----------------------------------------------------------------------
104  * Make_OODate --
105  *	See if a given node is out of date with respect to its sources.
106  *	Used by Make_Run when deciding which nodes to place on the
107  *	toBeMade queue initially and by Make_Update to screen out USE and
108  *	EXEC nodes. In the latter case, however, any other sort of node
109  *	must be considered out-of-date since at least one of its children
110  *	will have been recreated.
111  *
112  * Results:
113  *	TRUE if the node is out of date. FALSE otherwise.
114  *
115  * Side Effects:
116  *	The mtime field of the node and the cmtime field of its parents
117  *	will/may be changed.
118  *-----------------------------------------------------------------------
119  */
120 Boolean
121 Make_OODate (gn)
122     register GNode *gn;	      /* the node to check */
123 {
124     Boolean         oodate;
125 
126     /*
127      * Certain types of targets needn't even be sought as their datedness
128      * doesn't depend on their modification time...
129      */
130     if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
131 	(void) Dir_MTime (gn);
132 	if (DEBUG(MAKE)) {
133 	    if (gn->mtime != 0) {
134 		printf ("modified %s...", Targ_FmtTime(gn->mtime));
135 	    } else {
136 		printf ("non-existent...");
137 	    }
138 	}
139     }
140 
141     /*
142      * A target is remade in one of the following circumstances:
143      *	its modification time is smaller than that of its youngest child
144      *	    and it would actually be run (has commands or type OP_NOP)
145      *	it's the object of a force operator
146      *	it has no children, was on the lhs of an operator and doesn't exist
147      *	    already.
148      *
149      * Libraries are only considered out-of-date if the archive module says
150      * they are.
151      *
152      * These weird rules are brought to you by Backward-Compatability and
153      * the strange people who wrote 'Make'.
154      */
155     if (gn->type & OP_USE) {
156 	/*
157 	 * If the node is a USE node it is *never* out of date
158 	 * no matter *what*.
159 	 */
160 	if (DEBUG(MAKE)) {
161 	    printf(".USE node...");
162 	}
163 	oodate = FALSE;
164     } else if (gn->type & OP_LIB) {
165 	if (DEBUG(MAKE)) {
166 	    printf("library...");
167 	}
168 
169 	/*
170 	 * always out of date if no children and :: target
171 	 */
172 
173 	oodate = Arch_LibOODate (gn) ||
174 	    ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
175     } else if (gn->type & OP_JOIN) {
176 	/*
177 	 * A target with the .JOIN attribute is only considered
178 	 * out-of-date if any of its children was out-of-date.
179 	 */
180 	if (DEBUG(MAKE)) {
181 	    printf(".JOIN node...");
182 	}
183 	oodate = gn->childMade;
184     } else if (gn->type & (OP_FORCE|OP_EXEC)) {
185 	/*
186 	 * A node which is the object of the force (!) operator or which has
187 	 * the .EXEC attribute is always considered out-of-date.
188 	 */
189 	if (DEBUG(MAKE)) {
190 	    if (gn->type & OP_FORCE) {
191 		printf("! operator...");
192 	    } else {
193 		printf(".EXEC node...");
194 	    }
195 	}
196 	oodate = TRUE;
197     } else if ((gn->mtime < gn->cmtime) ||
198 	       ((gn->cmtime == 0) &&
199 		((gn->mtime==0) || (gn->type & OP_DOUBLEDEP))))
200     {
201 	/*
202 	 * A node whose modification time is less than that of its
203 	 * youngest child or that has no children (cmtime == 0) and
204 	 * either doesn't exist (mtime == 0) or was the object of a
205 	 * :: operator is out-of-date. Why? Because that's the way Make does
206 	 * it.
207 	 */
208 	if (DEBUG(MAKE)) {
209 	    if (gn->mtime < gn->cmtime) {
210 		printf("modified before source...");
211 	    } else if (gn->mtime == 0) {
212 		printf("non-existent and no sources...");
213 	    } else {
214 		printf(":: operator and no sources...");
215 	    }
216 	}
217 	oodate = TRUE;
218     } else {
219 #if 0
220 	/* WHY? */
221 	if (DEBUG(MAKE)) {
222 	    printf("source %smade...", gn->childMade ? "" : "not ");
223 	}
224 	oodate = gn->childMade;
225 #else
226 	oodate = FALSE;
227 #endif /* 0 */
228     }
229 
230     /*
231      * If the target isn't out-of-date, the parents need to know its
232      * modification time. Note that targets that appear to be out-of-date
233      * but aren't, because they have no commands and aren't of type OP_NOP,
234      * have their mtime stay below their children's mtime to keep parents from
235      * thinking they're out-of-date.
236      */
237     if (!oodate) {
238 	Lst_ForEach (gn->parents, MakeTimeStamp, (ClientData)gn);
239     }
240 
241     return (oodate);
242 }
243 
244 /*-
245  *-----------------------------------------------------------------------
246  * MakeAddChild  --
247  *	Function used by Make_Run to add a child to the list l.
248  *	It will only add the child if its make field is FALSE.
249  *
250  * Results:
251  *	Always returns 0
252  *
253  * Side Effects:
254  *	The given list is extended
255  *-----------------------------------------------------------------------
256  */
257 static int
258 MakeAddChild (gnp, lp)
259     ClientData     gnp;		/* the node to add */
260     ClientData     lp;		/* the list to which to add it */
261 {
262     GNode          *gn = (GNode *) gnp;
263     Lst            l = (Lst) lp;
264     if (!gn->make && !(gn->type & OP_USE)) {
265 	(void)Lst_EnQueue (l, (ClientData)gn);
266     }
267     return (0);
268 }
269 
270 /*-
271  *-----------------------------------------------------------------------
272  * Make_HandleUse --
273  *	Function called by Make_Run and SuffApplyTransform on the downward
274  *	pass to handle .USE and transformation nodes. A callback function
275  *	for Lst_ForEach, it implements the .USE and transformation
276  *	functionality by copying the node's commands, type flags
277  *	and children to the parent node. Should be called before the
278  *	children are enqueued to be looked at by MakeAddChild.
279  *
280  *	A .USE node is much like an explicit transformation rule, except
281  *	its commands are always added to the target node, even if the
282  *	target already has commands.
283  *
284  * Results:
285  *	returns 0.
286  *
287  * Side Effects:
288  *	Children and commands may be added to the parent and the parent's
289  *	type may be changed.
290  *
291  *-----------------------------------------------------------------------
292  */
293 int
294 Make_HandleUse (cgn, pgn)
295     register GNode	*cgn;	/* The .USE node */
296     register GNode   	*pgn;	/* The target of the .USE node */
297 {
298     register GNode	*gn;	/* A child of the .USE node */
299     register LstNode	ln; 	/* An element in the children list */
300 
301     if (cgn->type & (OP_USE|OP_TRANSFORM)) {
302 	if ((cgn->type & OP_USE) || Lst_IsEmpty(pgn->commands)) {
303 	    /*
304 	     * .USE or transformation and target has no commands -- append
305 	     * the child's commands to the parent.
306 	     */
307 	    (void) Lst_Concat (pgn->commands, cgn->commands, LST_CONCNEW);
308 	}
309 
310 	if (Lst_Open (cgn->children) == SUCCESS) {
311 	    while ((ln = Lst_Next (cgn->children)) != NILLNODE) {
312 		gn = (GNode *)Lst_Datum (ln);
313 
314 		if (Lst_Member (pgn->children, gn) == NILLNODE) {
315 		    (void) Lst_AtEnd (pgn->children, gn);
316 		    (void) Lst_AtEnd (gn->parents, pgn);
317 		    pgn->unmade += 1;
318 		}
319 	    }
320 	    Lst_Close (cgn->children);
321 	}
322 
323 	pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
324 
325 	/*
326 	 * This child node is now "made", so we decrement the count of
327 	 * unmade children in the parent... We also remove the child
328 	 * from the parent's list to accurately reflect the number of decent
329 	 * children the parent has. This is used by Make_Run to decide
330 	 * whether to queue the parent or examine its children...
331 	 */
332 	if (cgn->type & OP_USE) {
333 	    pgn->unmade -= 1;
334 	}
335     }
336     return (0);
337 }
338 static int
339 MakeHandleUse (pgn, cgn)
340     ClientData pgn;	/* the current parent */
341     ClientData cgn;	/* the child we've just examined */
342 {
343     return Make_HandleUse((GNode *) pgn, (GNode *) cgn);
344 }
345 
346 /*-
347  *-----------------------------------------------------------------------
348  * Make_Update  --
349  *	Perform update on the parents of a node. Used by JobFinish once
350  *	a node has been dealt with and by MakeStartJobs if it finds an
351  *	up-to-date node.
352  *
353  * Results:
354  *	Always returns 0
355  *
356  * Side Effects:
357  *	The unmade field of pgn is decremented and pgn may be placed on
358  *	the toBeMade queue if this field becomes 0.
359  *
360  * 	If the child was made, the parent's childMade field will be set true
361  *	and its cmtime set to now.
362  *
363  *	If the child wasn't made, the cmtime field of the parent will be
364  *	altered if the child's mtime is big enough.
365  *
366  *	Finally, if the child is the implied source for the parent, the
367  *	parent's IMPSRC variable is set appropriately.
368  *
369  *-----------------------------------------------------------------------
370  */
371 void
372 Make_Update (cgn)
373     register GNode *cgn;	/* the child node */
374 {
375     register GNode 	*pgn;	/* the parent node */
376     register char  	*cname;	/* the child's name */
377     register LstNode	ln; 	/* Element in parents and iParents lists */
378     char *p1;
379 
380     cname = Var_Value (TARGET, cgn, &p1);
381     if (p1)
382 	free(p1);
383 
384     /*
385      * If the child was actually made, see what its modification time is
386      * now -- some rules won't actually update the file. If the file still
387      * doesn't exist, make its mtime now.
388      */
389     if (cgn->made != UPTODATE) {
390 #ifndef RECHECK
391 	/*
392 	 * We can't re-stat the thing, but we can at least take care of rules
393 	 * where a target depends on a source that actually creates the
394 	 * target, but only if it has changed, e.g.
395 	 *
396 	 * parse.h : parse.o
397 	 *
398 	 * parse.o : parse.y
399 	 *  	yacc -d parse.y
400 	 *  	cc -c y.tab.c
401 	 *  	mv y.tab.o parse.o
402 	 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
403 	 *
404 	 * In this case, if the definitions produced by yacc haven't changed
405 	 * from before, parse.h won't have been updated and cgn->mtime will
406 	 * reflect the current modification time for parse.h. This is
407 	 * something of a kludge, I admit, but it's a useful one..
408 	 * XXX: People like to use a rule like
409 	 *
410 	 * FRC:
411 	 *
412 	 * To force things that depend on FRC to be made, so we have to
413 	 * check for gn->children being empty as well...
414 	 */
415 	if (!Lst_IsEmpty(cgn->commands) || Lst_IsEmpty(cgn->children)) {
416 	    cgn->mtime = now;
417 	}
418 #else
419 	/*
420 	 * This is what Make does and it's actually a good thing, as it
421 	 * allows rules like
422 	 *
423 	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
424 	 *
425 	 * to function as intended. Unfortunately, thanks to the stateless
426 	 * nature of NFS (by which I mean the loose coupling of two clients
427 	 * using the same file from a common server), there are times
428 	 * when the modification time of a file created on a remote
429 	 * machine will not be modified before the local stat() implied by
430 	 * the Dir_MTime occurs, thus leading us to believe that the file
431 	 * is unchanged, wreaking havoc with files that depend on this one.
432 	 *
433 	 * I have decided it is better to make too much than to make too
434 	 * little, so this stuff is commented out unless you're sure it's ok.
435 	 * -- ardeb 1/12/88
436 	 */
437 	/*
438 	 * Christos, 4/9/92: If we are  saving commands pretend that
439 	 * the target is made now. Otherwise archives with ... rules
440 	 * don't work!
441 	 */
442 	if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == 0) {
443 	    cgn->mtime = now;
444 	}
445 	if (DEBUG(MAKE)) {
446 	    printf("update time: %s\n", Targ_FmtTime(cgn->mtime));
447 	}
448 #endif
449     }
450 
451     if (Lst_Open (cgn->parents) == SUCCESS) {
452 	while ((ln = Lst_Next (cgn->parents)) != NILLNODE) {
453 	    pgn = (GNode *)Lst_Datum (ln);
454 	    if (pgn->make) {
455 		pgn->unmade -= 1;
456 
457 		if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
458 		    if (cgn->made == MADE) {
459 			pgn->childMade = TRUE;
460 			if (pgn->cmtime < cgn->mtime) {
461 			    pgn->cmtime = cgn->mtime;
462 			}
463 		    } else {
464 			(void)Make_TimeStamp (pgn, cgn);
465 		    }
466 		}
467 		if (pgn->unmade == 0) {
468 		    /*
469 		     * Queue the node up -- any unmade predecessors will
470 		     * be dealt with in MakeStartJobs.
471 		     */
472 		    (void)Lst_EnQueue (toBeMade, (ClientData)pgn);
473 		} else if (pgn->unmade < 0) {
474 		    Error ("Graph cycles through %s", pgn->name);
475 		}
476 	    }
477 	}
478 	Lst_Close (cgn->parents);
479     }
480     /*
481      * Deal with successor nodes. If any is marked for making and has an unmade
482      * count of 0, has not been made and isn't in the examination queue,
483      * it means we need to place it in the queue as it restrained itself
484      * before.
485      */
486     for (ln = Lst_First(cgn->successors); ln != NILLNODE; ln = Lst_Succ(ln)) {
487 	GNode	*succ = (GNode *)Lst_Datum(ln);
488 
489 	if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
490 	    Lst_Member(toBeMade, (ClientData)succ) == NILLNODE)
491 	{
492 	    (void)Lst_EnQueue(toBeMade, (ClientData)succ);
493 	}
494     }
495 
496     /*
497      * Set the .PREFIX and .IMPSRC variables for all the implied parents
498      * of this node.
499      */
500     if (Lst_Open (cgn->iParents) == SUCCESS) {
501 	char    *p1;
502 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
503 
504 	while ((ln = Lst_Next (cgn->iParents)) != NILLNODE) {
505 	    pgn = (GNode *)Lst_Datum (ln);
506 	    if (pgn->make) {
507 		Var_Set (IMPSRC, cname, pgn);
508 		Var_Set (PREFIX, cpref, pgn);
509 	    }
510 	}
511 	if (p1)
512 	    free(p1);
513 	Lst_Close (cgn->iParents);
514     }
515 }
516 
517 /*-
518  *-----------------------------------------------------------------------
519  * MakeAddAllSrc --
520  *	Add a child's name to the ALLSRC and OODATE variables of the given
521  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
522  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
523  *	.EXEC and .USE children are very rarely going to be files, so...
524  *	A child is added to the OODATE variable if its modification time is
525  *	later than that of its parent, as defined by Make, except if the
526  *	parent is a .JOIN node. In that case, it is only added to the OODATE
527  *	variable if it was actually made (since .JOIN nodes don't have
528  *	modification times, the comparison is rather unfair...)..
529  *
530  * Results:
531  *	Always returns 0
532  *
533  * Side Effects:
534  *	The ALLSRC variable for the given node is extended.
535  *-----------------------------------------------------------------------
536  */
537 static int
538 MakeAddAllSrc (cgnp, pgnp)
539     ClientData	cgnp;	/* The child to add */
540     ClientData	pgnp;	/* The parent to whose ALLSRC variable it should be */
541 			/* added */
542 {
543     GNode	*cgn = (GNode *) cgnp;
544     GNode	*pgn = (GNode *) pgnp;
545     if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
546 	char *child;
547 	char *p1;
548 
549 	child = Var_Value(TARGET, cgn, &p1);
550 	Var_Append (ALLSRC, child, pgn);
551 	if (pgn->type & OP_JOIN) {
552 	    if (cgn->made == MADE) {
553 		Var_Append(OODATE, child, pgn);
554 	    }
555 	} else if ((pgn->mtime < cgn->mtime) ||
556 		   (cgn->mtime >= now && cgn->made == MADE))
557 	{
558 	    /*
559 	     * It goes in the OODATE variable if the parent is younger than the
560 	     * child or if the child has been modified more recently than
561 	     * the start of the make. This is to keep pmake from getting
562 	     * confused if something else updates the parent after the
563 	     * make starts (shouldn't happen, I know, but sometimes it
564 	     * does). In such a case, if we've updated the kid, the parent
565 	     * is likely to have a modification time later than that of
566 	     * the kid and anything that relies on the OODATE variable will
567 	     * be hosed.
568 	     *
569 	     * XXX: This will cause all made children to go in the OODATE
570 	     * variable, even if they're not touched, if RECHECK isn't defined,
571 	     * since cgn->mtime is set to now in Make_Update. According to
572 	     * some people, this is good...
573 	     */
574 	    Var_Append(OODATE, child, pgn);
575 	}
576 	if (p1)
577 	    free(p1);
578     }
579     return (0);
580 }
581 
582 /*-
583  *-----------------------------------------------------------------------
584  * Make_DoAllVar --
585  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
586  *	done separately, rather than while traversing the graph. This is
587  *	because Make defined OODATE to contain all sources whose modification
588  *	times were later than that of the target, *not* those sources that
589  *	were out-of-date. Since in both compatibility and native modes,
590  *	the modification time of the parent isn't found until the child
591  *	has been dealt with, we have to wait until now to fill in the
592  *	variable. As for ALLSRC, the ordering is important and not
593  *	guaranteed when in native mode, so it must be set here, too.
594  *
595  * Results:
596  *	None
597  *
598  * Side Effects:
599  *	The ALLSRC and OODATE variables of the given node is filled in.
600  *	If the node is a .JOIN node, its TARGET variable will be set to
601  * 	match its ALLSRC variable.
602  *-----------------------------------------------------------------------
603  */
604 void
605 Make_DoAllVar (gn)
606     GNode	*gn;
607 {
608     Lst_ForEach (gn->children, MakeAddAllSrc, (ClientData) gn);
609 
610     if (!Var_Exists (OODATE, gn)) {
611 	Var_Set (OODATE, "", gn);
612     }
613     if (!Var_Exists (ALLSRC, gn)) {
614 	Var_Set (ALLSRC, "", gn);
615     }
616 
617     if (gn->type & OP_JOIN) {
618 	char *p1;
619 	Var_Set (TARGET, Var_Value (ALLSRC, gn, &p1), gn);
620 	if (p1)
621 	    free(p1);
622     }
623 }
624 
625 /*-
626  *-----------------------------------------------------------------------
627  * MakeStartJobs --
628  *	Start as many jobs as possible.
629  *
630  * Results:
631  *	If the query flag was given to pmake, no job will be started,
632  *	but as soon as an out-of-date target is found, this function
633  *	returns TRUE. At all other times, this function returns FALSE.
634  *
635  * Side Effects:
636  *	Nodes are removed from the toBeMade queue and job table slots
637  *	are filled.
638  *
639  *-----------------------------------------------------------------------
640  */
641 static Boolean
642 MakeStartJobs ()
643 {
644     register GNode	*gn;
645 
646     while (!Job_Full() && !Lst_IsEmpty (toBeMade)) {
647 	gn = (GNode *) Lst_DeQueue (toBeMade);
648 	if (DEBUG(MAKE)) {
649 	    printf ("Examining %s...", gn->name);
650 	}
651 	/*
652 	 * Make sure any and all predecessors that are going to be made,
653 	 * have been.
654 	 */
655 	if (!Lst_IsEmpty(gn->preds)) {
656 	    LstNode ln;
657 
658 	    for (ln = Lst_First(gn->preds); ln != NILLNODE; ln = Lst_Succ(ln)){
659 		GNode	*pgn = (GNode *)Lst_Datum(ln);
660 
661 		if (pgn->make && pgn->made == UNMADE) {
662 		    if (DEBUG(MAKE)) {
663 			printf("predecessor %s not made yet.\n", pgn->name);
664 		    }
665 		    break;
666 		}
667 	    }
668 	    /*
669 	     * If ln isn't nil, there's a predecessor as yet unmade, so we
670 	     * just drop this node on the floor. When the node in question
671 	     * has been made, it will notice this node as being ready to
672 	     * make but as yet unmade and will place the node on the queue.
673 	     */
674 	    if (ln != NILLNODE) {
675 		continue;
676 	    }
677 	}
678 
679 	numNodes--;
680 	if (Make_OODate (gn)) {
681 	    if (DEBUG(MAKE)) {
682 		printf ("out-of-date\n");
683 	    }
684 	    if (queryFlag) {
685 		return (TRUE);
686 	    }
687 	    Make_DoAllVar (gn);
688 	    Job_Make (gn);
689 	} else {
690 	    if (DEBUG(MAKE)) {
691 		printf ("up-to-date\n");
692 	    }
693 	    gn->made = UPTODATE;
694 	    if (gn->type & OP_JOIN) {
695 		/*
696 		 * Even for an up-to-date .JOIN node, we need it to have its
697 		 * context variables so references to it get the correct
698 		 * value for .TARGET when building up the context variables
699 		 * of its parent(s)...
700 		 */
701 		Make_DoAllVar (gn);
702 	    }
703 
704 	    Make_Update (gn);
705 	}
706     }
707     return (FALSE);
708 }
709 
710 /*-
711  *-----------------------------------------------------------------------
712  * MakePrintStatus --
713  *	Print the status of a top-level node, viz. it being up-to-date
714  *	already or not created due to an error in a lower level.
715  *	Callback function for Make_Run via Lst_ForEach.
716  *
717  * Results:
718  *	Always returns 0.
719  *
720  * Side Effects:
721  *	A message may be printed.
722  *
723  *-----------------------------------------------------------------------
724  */
725 static int
726 MakePrintStatus(gnp, cyclep)
727     ClientData  gnp;	    /* Node to examine */
728     ClientData 	cyclep;	    /* True if gn->unmade being non-zero implies
729 			     * a cycle in the graph, not an error in an
730 			     * inferior */
731 {
732     GNode   	*gn = (GNode *) gnp;
733     Boolean 	cycle = *(Boolean *) cyclep;
734     if (gn->made == UPTODATE) {
735 	printf ("`%s' is up to date.\n", gn->name);
736     } else if (gn->unmade != 0) {
737 	if (cycle) {
738 	    Boolean t = TRUE;
739 	    /*
740 	     * If printing cycles and came to one that has unmade children,
741 	     * print out the cycle by recursing on its children. Note a
742 	     * cycle like:
743 	     *	a : b
744 	     *	b : c
745 	     *	c : b
746 	     * will cause this to erroneously complain about a being in
747 	     * the cycle, but this is a good approximation.
748 	     */
749 	    if (gn->made == CYCLE) {
750 		Error("Graph cycles through `%s'", gn->name);
751 		gn->made = ENDCYCLE;
752 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
753 		gn->made = UNMADE;
754 	    } else if (gn->made != ENDCYCLE) {
755 		gn->made = CYCLE;
756 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
757 	    }
758 	} else {
759 	    printf ("`%s' not remade because of errors.\n", gn->name);
760 	}
761     }
762     return (0);
763 }
764 
765 /*-
766  *-----------------------------------------------------------------------
767  * Make_Run --
768  *	Initialize the nodes to remake and the list of nodes which are
769  *	ready to be made by doing a breadth-first traversal of the graph
770  *	starting from the nodes in the given list. Once this traversal
771  *	is finished, all the 'leaves' of the graph are in the toBeMade
772  *	queue.
773  *	Using this queue and the Job module, work back up the graph,
774  *	calling on MakeStartJobs to keep the job table as full as
775  *	possible.
776  *
777  * Results:
778  *	TRUE if work was done. FALSE otherwise.
779  *
780  * Side Effects:
781  *	The make field of all nodes involved in the creation of the given
782  *	targets is set to 1. The toBeMade list is set to contain all the
783  *	'leaves' of these subgraphs.
784  *-----------------------------------------------------------------------
785  */
786 Boolean
787 Make_Run (targs)
788     Lst             targs;	/* the initial list of targets */
789 {
790     register GNode  *gn;	/* a temporary pointer */
791     register Lst    examine; 	/* List of targets to examine */
792     int	    	    errors; 	/* Number of errors the Job module reports */
793 
794     toBeMade = Lst_Init (FALSE);
795 
796     examine = Lst_Duplicate(targs, NOCOPY);
797     numNodes = 0;
798 
799     /*
800      * Make an initial downward pass over the graph, marking nodes to be made
801      * as we go down. We call Suff_FindDeps to find where a node is and
802      * to get some children for it if it has none and also has no commands.
803      * If the node is a leaf, we stick it on the toBeMade queue to
804      * be looked at in a minute, otherwise we add its children to our queue
805      * and go on about our business.
806      */
807     while (!Lst_IsEmpty (examine)) {
808 	gn = (GNode *) Lst_DeQueue (examine);
809 
810 	if (!gn->make) {
811 	    gn->make = TRUE;
812 	    numNodes++;
813 
814 	    /*
815 	     * Apply any .USE rules before looking for implicit dependencies
816 	     * to make sure everything has commands that should...
817 	     */
818 	    Lst_ForEach (gn->children, MakeHandleUse, (ClientData)gn);
819 	    Suff_FindDeps (gn);
820 
821 	    if (gn->unmade != 0) {
822 		Lst_ForEach (gn->children, MakeAddChild, (ClientData)examine);
823 	    } else {
824 		(void)Lst_EnQueue (toBeMade, (ClientData)gn);
825 	    }
826 	}
827     }
828 
829     Lst_Destroy (examine, NOFREE);
830 
831     if (queryFlag) {
832 	/*
833 	 * We wouldn't do any work unless we could start some jobs in the
834 	 * next loop... (we won't actually start any, of course, this is just
835 	 * to see if any of the targets was out of date)
836 	 */
837 	return (MakeStartJobs());
838     } else {
839 	/*
840 	 * Initialization. At the moment, no jobs are running and until some
841 	 * get started, nothing will happen since the remaining upward
842 	 * traversal of the graph is performed by the routines in job.c upon
843 	 * the finishing of a job. So we fill the Job table as much as we can
844 	 * before going into our loop.
845 	 */
846 	(void) MakeStartJobs();
847     }
848 
849     /*
850      * Main Loop: The idea here is that the ending of jobs will take
851      * care of the maintenance of data structures and the waiting for output
852      * will cause us to be idle most of the time while our children run as
853      * much as possible. Because the job table is kept as full as possible,
854      * the only time when it will be empty is when all the jobs which need
855      * running have been run, so that is the end condition of this loop.
856      * Note that the Job module will exit if there were any errors unless the
857      * keepgoing flag was given.
858      */
859     while (!Job_Empty ()) {
860 	Job_CatchOutput ();
861 	Job_CatchChildren (!usePipes);
862 	(void)MakeStartJobs();
863     }
864 
865     errors = Job_End();
866 
867     /*
868      * Print the final status of each target. E.g. if it wasn't made
869      * because some inferior reported an error.
870      */
871     errors = ((errors == 0) && (numNodes != 0));
872     Lst_ForEach(targs, MakePrintStatus, (ClientData) &errors);
873 
874     return (TRUE);
875 }
876