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