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