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