xref: /netbsd/usr.bin/make/make.c (revision bf9ec67e)
1 /*	$NetBSD: make.c,v 1.49 2002/03/21 11:42:21 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.49 2002/03/21 11:42:21 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.49 2002/03/21 11:42:21 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     Lst		parents;
619     GNode	*centurion;
620 
621     cname = Var_Value (TARGET, cgn, &p1);
622     if (p1)
623 	free(p1);
624 
625     /*
626      * If the child was actually made, see what its modification time is
627      * now -- some rules won't actually update the file. If the file still
628      * doesn't exist, make its mtime now.
629      */
630     if (cgn->made != UPTODATE) {
631 	mtime = Make_Recheck(cgn);
632     }
633 
634     /*
635      * If this is a `::' node, we must consult its first instance
636      * which is where all parents are linked.
637      */
638     if ((centurion = cgn->centurion) != NULL) {
639 	if (!Lst_IsEmpty(cgn->parents))
640 		Punt("%s: cohort has parents", cgn->name);
641 	centurion->unmade_cohorts -= 1;
642 	if (centurion->unmade_cohorts < 0)
643 	    Error ("Graph cycles through centurion %s", centurion->name);
644 	parents = centurion->parents;
645     } else {
646 	centurion = cgn;
647 	parents = cgn->parents;
648     }
649     if (Lst_Open (parents) == SUCCESS) {
650 	while ((ln = Lst_Next (parents)) != NILLNODE) {
651 	    pgn = (GNode *)Lst_Datum (ln);
652 	    if (mtime == 0)
653 		pgn->flags |= FORCE;
654 	    /*
655 	     * If the parent has the .MADE attribute, it has already
656 	     * been queued on the `toBeMade' list in Make_ExpandUse()
657 	     * and its unmade children counter is zero.
658 	     */
659 	    if ((pgn->flags & REMAKE) == 0 || (pgn->type & OP_MADE) != 0)
660 		continue;
661 
662 	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
663 		if (cgn->made == MADE)
664 		    pgn->flags |= CHILDMADE;
665 		(void)Make_TimeStamp (pgn, cgn);
666 	    }
667 
668 	    /*
669 	     * A parent must wait for the completion of all instances
670 	     * of a `::' dependency.
671 	     */
672 	    if (centurion->unmade_cohorts != 0 || centurion->made == UNMADE)
673 		continue;
674 
675 	    /* One more child of this parent is now made */
676 	    pgn->unmade -= 1;
677 	    if (pgn->unmade == 0) {
678 		/*
679 		 * Queue the node up -- any unmade predecessors will
680 		 * be dealt with in MakeStartJobs.
681 		 */
682 		(void)Lst_EnQueue (toBeMade, (ClientData)pgn);
683 	    } else if (pgn->unmade < 0) {
684 		Error ("Graph cycles through %s", pgn->name);
685 	    }
686 	}
687 	Lst_Close (parents);
688     }
689     /*
690      * Deal with successor nodes. If any is marked for making and has an unmade
691      * count of 0, has not been made and isn't in the examination queue,
692      * it means we need to place it in the queue as it restrained itself
693      * before.
694      */
695     for (ln = Lst_First(centurion->successors); ln != NILLNODE;
696 	 ln = Lst_Succ(ln)) {
697 	GNode	*succ = (GNode *)Lst_Datum(ln);
698 
699 	if ((succ->flags & REMAKE) != 0 && succ->unmade == 0 &&
700 	    succ->made == UNMADE &&
701 	    Lst_Member(toBeMade, (ClientData)succ) == NILLNODE)
702 	{
703 	    (void)Lst_EnQueue(toBeMade, (ClientData)succ);
704 	}
705     }
706 
707     /*
708      * Set the .PREFIX and .IMPSRC variables for all the implied parents
709      * of this node.
710      */
711     if (Lst_Open (cgn->iParents) == SUCCESS) {
712 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
713 
714 	while ((ln = Lst_Next (cgn->iParents)) != NILLNODE) {
715 	    pgn = (GNode *)Lst_Datum (ln);
716 	    if (pgn->flags & REMAKE) {
717 		Var_Set (IMPSRC, cname, pgn, 0);
718 		if (cpref != NULL)
719 		    Var_Set (PREFIX, cpref, pgn, 0);
720 	    }
721 	}
722 	if (p1)
723 	    free(p1);
724 	Lst_Close (cgn->iParents);
725     }
726 }
727 
728 /*-
729  *-----------------------------------------------------------------------
730  * MakeAddAllSrc --
731  *	Add a child's name to the ALLSRC and OODATE variables of the given
732  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
733  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
734  *	.EXEC and .USE children are very rarely going to be files, so...
735  *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
736  *
737  *	A child is added to the OODATE variable if its modification time is
738  *	later than that of its parent, as defined by Make, except if the
739  *	parent is a .JOIN node. In that case, it is only added to the OODATE
740  *	variable if it was actually made (since .JOIN nodes don't have
741  *	modification times, the comparison is rather unfair...)..
742  *
743  * Results:
744  *	Always returns 0
745  *
746  * Side Effects:
747  *	The ALLSRC variable for the given node is extended.
748  *-----------------------------------------------------------------------
749  */
750 static int
751 MakeUnmark (cgnp, pgnp)
752     ClientData	cgnp;
753     ClientData	pgnp;
754 {
755     GNode	*cgn = (GNode *) cgnp;
756 
757     cgn->type &= ~OP_MARK;
758     return (0);
759 }
760 
761 static int
762 MakeAddAllSrc (cgnp, pgnp)
763     ClientData	cgnp;	/* The child to add */
764     ClientData	pgnp;	/* The parent to whose ALLSRC variable it should be */
765 			/* added */
766 {
767     GNode	*cgn = (GNode *) cgnp;
768     GNode	*pgn = (GNode *) pgnp;
769 
770     if (cgn->type & OP_MARK)
771 	return (0);
772     cgn->type |= OP_MARK;
773 
774     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
775 	char *child, *allsrc;
776 	char *p1 = NULL, *p2 = NULL;
777 
778 	if (cgn->type & OP_ARCHV)
779 	    child = Var_Value (MEMBER, cgn, &p1);
780 	else
781 	    child = cgn->path ? cgn->path : cgn->name;
782 	if (cgn->type & OP_JOIN) {
783 	    allsrc = Var_Value (ALLSRC, cgn, &p2);
784 	} else {
785 	    allsrc = child;
786 	}
787 	if (allsrc != NULL)
788 		Var_Append (ALLSRC, allsrc, pgn);
789 	if (p2)
790 	    free(p2);
791 	if (pgn->type & OP_JOIN) {
792 	    if (cgn->made == MADE) {
793 		Var_Append(OODATE, child, pgn);
794 	    }
795 	} else if ((pgn->mtime < cgn->mtime) ||
796 		   (cgn->mtime >= now && cgn->made == MADE))
797 	{
798 	    /*
799 	     * It goes in the OODATE variable if the parent is younger than the
800 	     * child or if the child has been modified more recently than
801 	     * the start of the make. This is to keep pmake from getting
802 	     * confused if something else updates the parent after the
803 	     * make starts (shouldn't happen, I know, but sometimes it
804 	     * does). In such a case, if we've updated the kid, the parent
805 	     * is likely to have a modification time later than that of
806 	     * the kid and anything that relies on the OODATE variable will
807 	     * be hosed.
808 	     *
809 	     * XXX: This will cause all made children to go in the OODATE
810 	     * variable, even if they're not touched, if RECHECK isn't defined,
811 	     * since cgn->mtime is set to now in Make_Update. According to
812 	     * some people, this is good...
813 	     */
814 	    Var_Append(OODATE, child, pgn);
815 	}
816 	if (p1)
817 	    free(p1);
818     }
819     return (0);
820 }
821 
822 /*-
823  *-----------------------------------------------------------------------
824  * Make_DoAllVar --
825  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
826  *	done separately, rather than while traversing the graph. This is
827  *	because Make defined OODATE to contain all sources whose modification
828  *	times were later than that of the target, *not* those sources that
829  *	were out-of-date. Since in both compatibility and native modes,
830  *	the modification time of the parent isn't found until the child
831  *	has been dealt with, we have to wait until now to fill in the
832  *	variable. As for ALLSRC, the ordering is important and not
833  *	guaranteed when in native mode, so it must be set here, too.
834  *
835  * Results:
836  *	None
837  *
838  * Side Effects:
839  *	The ALLSRC and OODATE variables of the given node is filled in.
840  *	If the node is a .JOIN node, its TARGET variable will be set to
841  * 	match its ALLSRC variable.
842  *-----------------------------------------------------------------------
843  */
844 void
845 Make_DoAllVar (gn)
846     GNode	*gn;
847 {
848     Lst_ForEach (gn->children, MakeUnmark, (ClientData) gn);
849     Lst_ForEach (gn->children, MakeAddAllSrc, (ClientData) gn);
850 
851     if (!Var_Exists (OODATE, gn)) {
852 	Var_Set (OODATE, "", gn, 0);
853     }
854     if (!Var_Exists (ALLSRC, gn)) {
855 	Var_Set (ALLSRC, "", gn, 0);
856     }
857 
858     if (gn->type & OP_JOIN) {
859 	char *p1;
860 	Var_Set (TARGET, Var_Value (ALLSRC, gn, &p1), gn, 0);
861 	if (p1)
862 	    free(p1);
863     }
864 }
865 
866 /*-
867  *-----------------------------------------------------------------------
868  * MakeStartJobs --
869  *	Start as many jobs as possible.
870  *
871  * Results:
872  *	If the query flag was given to pmake, no job will be started,
873  *	but as soon as an out-of-date target is found, this function
874  *	returns TRUE. At all other times, this function returns FALSE.
875  *
876  * Side Effects:
877  *	Nodes are removed from the toBeMade queue and job table slots
878  *	are filled.
879  *
880  *-----------------------------------------------------------------------
881  */
882 static Boolean
883 MakeStartJobs ()
884 {
885     GNode	*gn;
886 
887     while (!Lst_IsEmpty (toBeMade)) {
888 	gn = (GNode *) Lst_DeQueue (toBeMade);
889 	if (DEBUG(MAKE)) {
890 	    printf ("Examining %s...", gn->name);
891 	}
892 	/*
893 	 * Make sure any and all predecessors that are going to be made,
894 	 * have been.
895 	 */
896 	if (!Lst_IsEmpty(gn->preds)) {
897 	    LstNode ln;
898 
899 	    for (ln = Lst_First(gn->preds); ln != NILLNODE; ln = Lst_Succ(ln)){
900 		GNode	*pgn = (GNode *)Lst_Datum(ln);
901 
902 		if ((pgn->flags & REMAKE) &&
903 		    (pgn->made == UNMADE || pgn->unmade_cohorts != 0)) {
904 		    if (DEBUG(MAKE)) {
905 			printf("predecessor %s not made yet.\n", pgn->name);
906 		    }
907 		    break;
908 		}
909 	    }
910 	    /*
911 	     * If ln isn't nil, there's a predecessor as yet unmade, so we
912 	     * just drop this node on the floor. When the node in question
913 	     * has been made, it will notice this node as being ready to
914 	     * make but as yet unmade and will place the node on the queue.
915 	     */
916 	    if (ln != NILLNODE) {
917 		continue;
918 	    }
919 	}
920 
921 	if (!Job_TokenWithdraw()) {
922 	    Lst_AtFront(toBeMade, gn);
923 	    break;
924 	}
925 
926 	numNodes--;
927 	if (Make_OODate (gn)) {
928 	    if (DEBUG(MAKE)) {
929 		printf ("out-of-date\n");
930 	    }
931 	    if (queryFlag) {
932 		return (TRUE);
933 	    }
934 	    Make_DoAllVar (gn);
935 	    Job_Make (gn);
936 	} else {
937 	    if (DEBUG(MAKE)) {
938 		printf ("up-to-date\n");
939 	    }
940 	    gn->made = UPTODATE;
941 	    if (gn->type & OP_JOIN) {
942 		/*
943 		 * Even for an up-to-date .JOIN node, we need it to have its
944 		 * context variables so references to it get the correct
945 		 * value for .TARGET when building up the context variables
946 		 * of its parent(s)...
947 		 */
948 		Make_DoAllVar (gn);
949 	    }
950 	    Job_TokenReturn();
951 	    Make_Update (gn);
952 	}
953     }
954     return (FALSE);
955 }
956 
957 /*-
958  *-----------------------------------------------------------------------
959  * MakePrintStatus --
960  *	Print the status of a top-level node, viz. it being up-to-date
961  *	already or not created due to an error in a lower level.
962  *	Callback function for Make_Run via Lst_ForEach.
963  *
964  * Results:
965  *	Always returns 0.
966  *
967  * Side Effects:
968  *	A message may be printed.
969  *
970  *-----------------------------------------------------------------------
971  */
972 static int
973 MakePrintStatus(gnp, cyclep)
974     ClientData  gnp;	    /* Node to examine */
975     ClientData 	cyclep;	    /* True if gn->unmade being non-zero implies
976 			     * a cycle in the graph, not an error in an
977 			     * inferior */
978 {
979     GNode   	*gn = (GNode *) gnp;
980     Boolean 	cycle = *(Boolean *) cyclep;
981     if (gn->made == UPTODATE) {
982 	printf ("`%s' is up to date.\n", gn->name);
983     } else if (gn->unmade != 0) {
984 	if (cycle) {
985 	    Boolean t = TRUE;
986 	    /*
987 	     * If printing cycles and came to one that has unmade children,
988 	     * print out the cycle by recursing on its children. Note a
989 	     * cycle like:
990 	     *	a : b
991 	     *	b : c
992 	     *	c : b
993 	     * will cause this to erroneously complain about a being in
994 	     * the cycle, but this is a good approximation.
995 	     */
996 	    if (gn->made == CYCLE) {
997 		Error("Graph cycles through `%s'", gn->name);
998 		gn->made = ENDCYCLE;
999 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
1000 		gn->made = UNMADE;
1001 	    } else if (gn->made != ENDCYCLE) {
1002 		gn->made = CYCLE;
1003 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
1004 	    }
1005 	} else {
1006 	    printf ("`%s' not remade because of errors.\n", gn->name);
1007 	}
1008     }
1009     return (0);
1010 }
1011 
1012 
1013 /*-
1014  *-----------------------------------------------------------------------
1015  * Make_ExpandUse --
1016  *	Expand .USE nodes and create a new targets list
1017  * Results:
1018  *	The new list of targets.
1019  *
1020  * Side Effects:
1021  *	numNodes is set to the number of elements in the list of targets.
1022  *-----------------------------------------------------------------------
1023  */
1024 Lst
1025 Make_ExpandUse (targs)
1026     Lst             targs;	/* the initial list of targets */
1027 {
1028     GNode  *gn;		/* a temporary pointer */
1029     Lst    examine; 	/* List of targets to examine */
1030     Lst    ntargs;	/* List of new targets to be made */
1031 
1032     ntargs = Lst_Init (FALSE);
1033 
1034     examine = Lst_Duplicate(targs, NOCOPY);
1035     numNodes = 0;
1036 
1037     /*
1038      * Make an initial downward pass over the graph, marking nodes to be made
1039      * as we go down. We call Suff_FindDeps to find where a node is and
1040      * to get some children for it if it has none and also has no commands.
1041      * If the node is a leaf, we stick it on the toBeMade queue to
1042      * be looked at in a minute, otherwise we add its children to our queue
1043      * and go on about our business.
1044      */
1045     while (!Lst_IsEmpty (examine)) {
1046 	gn = (GNode *) Lst_DeQueue (examine);
1047 
1048 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
1049 	    Lst new;
1050 	    new = Lst_Duplicate (gn->cohorts, NOCOPY);
1051 	    Lst_Concat (new, examine, LST_CONCLINK);
1052 	    examine = new;
1053 	}
1054 
1055 	if ((gn->flags & REMAKE) == 0) {
1056 	    gn->flags |= REMAKE;
1057 	    numNodes++;
1058 
1059 	    /*
1060 	     * Apply any .USE rules before looking for implicit dependencies
1061 	     * to make sure everything has commands that should...
1062 	     * Make sure that the TARGET is set, so that we can make
1063 	     * expansions.
1064 	     */
1065 	    if (gn->type & OP_ARCHV) {
1066 		char *eoa, *eon;
1067 		eoa = strchr(gn->name, '(');
1068 		eon = strchr(gn->name, ')');
1069 		if (eoa == NULL || eon == NULL)
1070 		    continue;
1071 		*eoa = '\0';
1072 		*eon = '\0';
1073 		Var_Set (MEMBER, eoa + 1, gn, 0);
1074 		Var_Set (ARCHIVE, gn->name, gn, 0);
1075 		*eoa = '(';
1076 		*eon = ')';
1077 	    }
1078 
1079 	    (void)Dir_MTime(gn);
1080 	    Var_Set (TARGET, gn->path ? gn->path : gn->name, gn, 0);
1081 	    Lst_ForEach (gn->children, MakeUnmark, (ClientData)gn);
1082 	    Lst_ForEach (gn->children, MakeHandleUse, (ClientData)gn);
1083 
1084 	    if ((gn->type & OP_MADE) == 0)
1085 		Suff_FindDeps (gn);
1086 	    else {
1087 		/* Pretend we made all this node's children */
1088 		Lst_ForEach (gn->children, MakeFindChild, (ClientData)gn);
1089 		if (gn->unmade != 0)
1090 			printf("Warning: %s still has %d unmade children\n",
1091 				gn->name, gn->unmade);
1092 	    }
1093 
1094 	    if (gn->unmade != 0) {
1095 		Lst_ForEach (gn->children, MakeAddChild, (ClientData)examine);
1096 	    } else {
1097 		(void)Lst_EnQueue (ntargs, (ClientData)gn);
1098 	    }
1099 	}
1100     }
1101 
1102     Lst_Destroy (examine, NOFREE);
1103     return ntargs;
1104 }
1105 
1106 /*-
1107  *-----------------------------------------------------------------------
1108  * Make_Run --
1109  *	Initialize the nodes to remake and the list of nodes which are
1110  *	ready to be made by doing a breadth-first traversal of the graph
1111  *	starting from the nodes in the given list. Once this traversal
1112  *	is finished, all the 'leaves' of the graph are in the toBeMade
1113  *	queue.
1114  *	Using this queue and the Job module, work back up the graph,
1115  *	calling on MakeStartJobs to keep the job table as full as
1116  *	possible.
1117  *
1118  * Results:
1119  *	TRUE if work was done. FALSE otherwise.
1120  *
1121  * Side Effects:
1122  *	The make field of all nodes involved in the creation of the given
1123  *	targets is set to 1. The toBeMade list is set to contain all the
1124  *	'leaves' of these subgraphs.
1125  *-----------------------------------------------------------------------
1126  */
1127 Boolean
1128 Make_Run (targs)
1129     Lst             targs;	/* the initial list of targets */
1130 {
1131     int	    	    errors; 	/* Number of errors the Job module reports */
1132 
1133     toBeMade = Make_ExpandUse (targs);
1134 
1135     if (queryFlag) {
1136 	/*
1137 	 * We wouldn't do any work unless we could start some jobs in the
1138 	 * next loop... (we won't actually start any, of course, this is just
1139 	 * to see if any of the targets was out of date)
1140 	 */
1141 	return (MakeStartJobs());
1142     } else {
1143 	/*
1144 	 * Initialization. At the moment, no jobs are running and until some
1145 	 * get started, nothing will happen since the remaining upward
1146 	 * traversal of the graph is performed by the routines in job.c upon
1147 	 * the finishing of a job. So we fill the Job table as much as we can
1148 	 * before going into our loop.
1149 	 */
1150 	(void) MakeStartJobs();
1151     }
1152 
1153     /*
1154      * Main Loop: The idea here is that the ending of jobs will take
1155      * care of the maintenance of data structures and the waiting for output
1156      * will cause us to be idle most of the time while our children run as
1157      * much as possible. Because the job table is kept as full as possible,
1158      * the only time when it will be empty is when all the jobs which need
1159      * running have been run, so that is the end condition of this loop.
1160      * Note that the Job module will exit if there were any errors unless the
1161      * keepgoing flag was given.
1162      */
1163     while (!Lst_IsEmpty(toBeMade) || !Job_Empty ()) {
1164 	Job_CatchOutput ();
1165 	Job_CatchChildren (!usePipes);
1166 	(void)MakeStartJobs();
1167     }
1168 
1169     errors = Job_Finish();
1170 
1171     /*
1172      * Print the final status of each target. E.g. if it wasn't made
1173      * because some inferior reported an error.
1174      */
1175     errors = ((errors == 0) && (numNodes != 0));
1176     Lst_ForEach(targs, MakePrintStatus, (ClientData) &errors);
1177 
1178     return (TRUE);
1179 }
1180