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