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