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