xref: /freebsd/contrib/bmake/make.c (revision 3e5645b78f476816ca3b5acc28b29bbafbb9c444)
1 /*	$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1989 by Berkeley Softworks
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Adam de Boor.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70 
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)make.c	8.1 (Berkeley) 6/6/93";
78 #else
79 __RCSID("$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83 
84 /*-
85  * make.c --
86  *	The functions which perform the examination of targets and
87  *	their suitability for creation
88  *
89  * Interface:
90  *	Make_Run 	    	Initialize things for the module and recreate
91  *	    	  	    	whatever needs recreating. Returns TRUE if
92  *	    	    	    	work was (or would have been) done and FALSE
93  *	    	  	    	otherwise.
94  *
95  *	Make_Update	    	Update all parents of a given child. Performs
96  *	    	  	    	various bookkeeping chores like the updating
97  *	    	  	    	of the cmgn field of the parent, filling
98  *	    	  	    	of the IMPSRC context variable, etc. It will
99  *	    	  	    	place the parent on the toBeMade queue if it
100  *	    	  	    	should be.
101  *
102  *	Make_TimeStamp	    	Function to set the parent's cmgn field
103  *	    	  	    	based on a child's modification time.
104  *
105  *	Make_DoAllVar	    	Set up the various local variables for a
106  *	    	  	    	target, including the .ALLSRC variable, making
107  *	    	  	    	sure that any variable that needs to exist
108  *	    	  	    	at the very least has the empty value.
109  *
110  *	Make_OODate 	    	Determine if a target is out-of-date.
111  *
112  *	Make_HandleUse	    	See if a child is a .USE node for a parent
113  *				and perform the .USE actions if so.
114  *
115  *	Make_ExpandUse	    	Expand .USE nodes
116  */
117 
118 #include    "make.h"
119 #include    "hash.h"
120 #include    "dir.h"
121 #include    "job.h"
122 
123 static unsigned int checked = 1;/* Sequence # to detect recursion */
124 static Lst     	toBeMade;	/* The current fringe of the graph. These
125 				 * are nodes which await examination by
126 				 * MakeOODate. It is added to by
127 				 * Make_Update and subtracted from by
128 				 * MakeStartJobs */
129 
130 static int MakeAddChild(void *, void *);
131 static int MakeFindChild(void *, void *);
132 static int MakeUnmark(void *, void *);
133 static int MakeAddAllSrc(void *, void *);
134 static int MakeTimeStamp(void *, void *);
135 static int MakeHandleUse(void *, void *);
136 static Boolean MakeStartJobs(void);
137 static int MakePrintStatus(void *, void *);
138 static int MakeCheckOrder(void *, void *);
139 static int MakeBuildChild(void *, void *);
140 static int MakeBuildParent(void *, void *);
141 
142 MAKE_ATTR_DEAD static void
143 make_abort(GNode *gn, int line)
144 {
145     static int two = 2;
146 
147     fprintf(debug_file, "make_abort from line %d\n", line);
148     Targ_PrintNode(gn, &two);
149     Lst_ForEach(toBeMade, Targ_PrintNode, &two);
150     Targ_PrintGraph(3);
151     abort();
152 }
153 
154 /*-
155  *-----------------------------------------------------------------------
156  * Make_TimeStamp --
157  *	Set the cmgn field of a parent node based on the mtime stamp in its
158  *	child. Called from MakeOODate via Lst_ForEach.
159  *
160  * Input:
161  *	pgn		the current parent
162  *	cgn		the child we've just examined
163  *
164  * Results:
165  *	Always returns 0.
166  *
167  * Side Effects:
168  *	The cmgn of the parent node will be changed if the mtime
169  *	field of the child is greater than it.
170  *-----------------------------------------------------------------------
171  */
172 int
173 Make_TimeStamp(GNode *pgn, GNode *cgn)
174 {
175     if (pgn->cmgn == NULL || cgn->mtime > pgn->cmgn->mtime) {
176 	pgn->cmgn = cgn;
177     }
178     return (0);
179 }
180 
181 /*
182  * Input:
183  *	pgn		the current parent
184  *	cgn		the child we've just examined
185  *
186  */
187 static int
188 MakeTimeStamp(void *pgn, void *cgn)
189 {
190     return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
191 }
192 
193 /*-
194  *-----------------------------------------------------------------------
195  * Make_OODate --
196  *	See if a given node is out of date with respect to its sources.
197  *	Used by Make_Run when deciding which nodes to place on the
198  *	toBeMade queue initially and by Make_Update to screen out USE and
199  *	EXEC nodes. In the latter case, however, any other sort of node
200  *	must be considered out-of-date since at least one of its children
201  *	will have been recreated.
202  *
203  * Input:
204  *	gn		the node to check
205  *
206  * Results:
207  *	TRUE if the node is out of date. FALSE otherwise.
208  *
209  * Side Effects:
210  *	The mtime field of the node and the cmgn field of its parents
211  *	will/may be changed.
212  *-----------------------------------------------------------------------
213  */
214 Boolean
215 Make_OODate(GNode *gn)
216 {
217     Boolean         oodate;
218 
219     /*
220      * Certain types of targets needn't even be sought as their datedness
221      * doesn't depend on their modification time...
222      */
223     if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
224 	(void)Dir_MTime(gn, 1);
225 	if (DEBUG(MAKE)) {
226 	    if (gn->mtime != 0) {
227 		fprintf(debug_file, "modified %s...", Targ_FmtTime(gn->mtime));
228 	    } else {
229 		fprintf(debug_file, "non-existent...");
230 	    }
231 	}
232     }
233 
234     /*
235      * A target is remade in one of the following circumstances:
236      *	its modification time is smaller than that of its youngest child
237      *	    and it would actually be run (has commands or type OP_NOP)
238      *	it's the object of a force operator
239      *	it has no children, was on the lhs of an operator and doesn't exist
240      *	    already.
241      *
242      * Libraries are only considered out-of-date if the archive module says
243      * they are.
244      *
245      * These weird rules are brought to you by Backward-Compatibility and
246      * the strange people who wrote 'Make'.
247      */
248     if (gn->type & (OP_USE|OP_USEBEFORE)) {
249 	/*
250 	 * If the node is a USE node it is *never* out of date
251 	 * no matter *what*.
252 	 */
253 	if (DEBUG(MAKE)) {
254 	    fprintf(debug_file, ".USE node...");
255 	}
256 	oodate = FALSE;
257     } else if ((gn->type & OP_LIB) &&
258 	       ((gn->mtime==0) || Arch_IsLib(gn))) {
259 	if (DEBUG(MAKE)) {
260 	    fprintf(debug_file, "library...");
261 	}
262 
263 	/*
264 	 * always out of date if no children and :: target
265 	 * or non-existent.
266 	 */
267 	oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
268 		  (gn->cmgn == NULL && (gn->type & OP_DOUBLEDEP)));
269     } else if (gn->type & OP_JOIN) {
270 	/*
271 	 * A target with the .JOIN attribute is only considered
272 	 * out-of-date if any of its children was out-of-date.
273 	 */
274 	if (DEBUG(MAKE)) {
275 	    fprintf(debug_file, ".JOIN node...");
276 	}
277 	if (DEBUG(MAKE)) {
278 	    fprintf(debug_file, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
279 	}
280 	oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
281     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
282 	/*
283 	 * A node which is the object of the force (!) operator or which has
284 	 * the .EXEC attribute is always considered out-of-date.
285 	 */
286 	if (DEBUG(MAKE)) {
287 	    if (gn->type & OP_FORCE) {
288 		fprintf(debug_file, "! operator...");
289 	    } else if (gn->type & OP_PHONY) {
290 		fprintf(debug_file, ".PHONY node...");
291 	    } else {
292 		fprintf(debug_file, ".EXEC node...");
293 	    }
294 	}
295 	oodate = TRUE;
296     } else if ((gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) ||
297 	       (gn->cmgn == NULL &&
298 		((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
299 		  || gn->type & OP_DOUBLEDEP)))
300     {
301 	/*
302 	 * A node whose modification time is less than that of its
303 	 * youngest child or that has no children (cmgn == NULL) and
304 	 * either doesn't exist (mtime == 0) and it isn't optional
305 	 * or was the object of a * :: operator is out-of-date.
306 	 * Why? Because that's the way Make does it.
307 	 */
308 	if (DEBUG(MAKE)) {
309 	    if (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) {
310 		fprintf(debug_file, "modified before source %s...",
311 		    gn->cmgn->path);
312 	    } else if (gn->mtime == 0) {
313 		fprintf(debug_file, "non-existent and no sources...");
314 	    } else {
315 		fprintf(debug_file, ":: operator and no sources...");
316 	    }
317 	}
318 	oodate = TRUE;
319     } else {
320 	/*
321 	 * When a non-existing child with no sources
322 	 * (such as a typically used FORCE source) has been made and
323 	 * the target of the child (usually a directory) has the same
324 	 * timestamp as the timestamp just given to the non-existing child
325 	 * after it was considered made.
326 	 */
327 	if (DEBUG(MAKE)) {
328 	    if (gn->flags & FORCE)
329 		fprintf(debug_file, "non existing child...");
330 	}
331 	oodate = (gn->flags & FORCE) ? TRUE : FALSE;
332     }
333 
334 #ifdef USE_META
335     if (useMeta) {
336 	oodate = meta_oodate(gn, oodate);
337     }
338 #endif
339 
340     /*
341      * If the target isn't out-of-date, the parents need to know its
342      * modification time. Note that targets that appear to be out-of-date
343      * but aren't, because they have no commands and aren't of type OP_NOP,
344      * have their mtime stay below their children's mtime to keep parents from
345      * thinking they're out-of-date.
346      */
347     if (!oodate) {
348 	Lst_ForEach(gn->parents, MakeTimeStamp, gn);
349     }
350 
351     return (oodate);
352 }
353 
354 /*-
355  *-----------------------------------------------------------------------
356  * MakeAddChild  --
357  *	Function used by Make_Run to add a child to the list l.
358  *	It will only add the child if its make field is FALSE.
359  *
360  * Input:
361  *	gnp		the node to add
362  *	lp		the list to which to add it
363  *
364  * Results:
365  *	Always returns 0
366  *
367  * Side Effects:
368  *	The given list is extended
369  *-----------------------------------------------------------------------
370  */
371 static int
372 MakeAddChild(void *gnp, void *lp)
373 {
374     GNode          *gn = (GNode *)gnp;
375     Lst            l = (Lst) lp;
376 
377     if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
378 	if (DEBUG(MAKE))
379 	    fprintf(debug_file, "MakeAddChild: need to examine %s%s\n",
380 		gn->name, gn->cohort_num);
381 	(void)Lst_EnQueue(l, gn);
382     }
383     return (0);
384 }
385 
386 /*-
387  *-----------------------------------------------------------------------
388  * MakeFindChild  --
389  *	Function used by Make_Run to find the pathname of a child
390  *	that was already made.
391  *
392  * Input:
393  *	gnp		the node to find
394  *
395  * Results:
396  *	Always returns 0
397  *
398  * Side Effects:
399  *	The path and mtime of the node and the cmgn of the parent are
400  *	updated; the unmade children count of the parent is decremented.
401  *-----------------------------------------------------------------------
402  */
403 static int
404 MakeFindChild(void *gnp, void *pgnp)
405 {
406     GNode          *gn = (GNode *)gnp;
407     GNode          *pgn = (GNode *)pgnp;
408 
409     (void)Dir_MTime(gn, 0);
410     Make_TimeStamp(pgn, gn);
411     pgn->unmade--;
412 
413     return (0);
414 }
415 
416 /*-
417  *-----------------------------------------------------------------------
418  * Make_HandleUse --
419  *	Function called by Make_Run and SuffApplyTransform on the downward
420  *	pass to handle .USE and transformation nodes. It implements the
421  *	.USE and transformation functionality by copying the node's commands,
422  *	type flags and children to the parent node.
423  *
424  *	A .USE node is much like an explicit transformation rule, except
425  *	its commands are always added to the target node, even if the
426  *	target already has commands.
427  *
428  * Input:
429  *	cgn		The .USE node
430  *	pgn		The target of the .USE node
431  *
432  * Results:
433  *	none
434  *
435  * Side Effects:
436  *	Children and commands may be added to the parent and the parent's
437  *	type may be changed.
438  *
439  *-----------------------------------------------------------------------
440  */
441 void
442 Make_HandleUse(GNode *cgn, GNode *pgn)
443 {
444     LstNode	ln; 	/* An element in the children list */
445 
446 #ifdef DEBUG_SRC
447     if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
448 	fprintf(debug_file, "Make_HandleUse: called for plain node %s\n", cgn->name);
449 	return;
450     }
451 #endif
452 
453     if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
454 	    if (cgn->type & OP_USEBEFORE) {
455 		/*
456 		 * .USEBEFORE --
457 		 *	prepend the child's commands to the parent.
458 		 */
459 		Lst cmds = pgn->commands;
460 		pgn->commands = Lst_Duplicate(cgn->commands, NULL);
461 		(void)Lst_Concat(pgn->commands, cmds, LST_CONCNEW);
462 		Lst_Destroy(cmds, NULL);
463 	    } else {
464 		/*
465 		 * .USE or target has no commands --
466 		 *	append the child's commands to the parent.
467 		 */
468 		(void)Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
469 	    }
470     }
471 
472     if (Lst_Open(cgn->children) == SUCCESS) {
473 	while ((ln = Lst_Next(cgn->children)) != NULL) {
474 	    GNode *tgn, *gn = (GNode *)Lst_Datum(ln);
475 
476 	    /*
477 	     * Expand variables in the .USE node's name
478 	     * and save the unexpanded form.
479 	     * We don't need to do this for commands.
480 	     * They get expanded properly when we execute.
481 	     */
482 	    if (gn->uname == NULL) {
483 		gn->uname = gn->name;
484 	    } else {
485 		if (gn->name)
486 		    free(gn->name);
487 	    }
488 	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE);
489 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
490 		/* See if we have a target for this node. */
491 		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
492 		if (tgn != NULL)
493 		    gn = tgn;
494 	    }
495 
496 	    (void)Lst_AtEnd(pgn->children, gn);
497 	    (void)Lst_AtEnd(gn->parents, pgn);
498 	    pgn->unmade += 1;
499 	}
500 	Lst_Close(cgn->children);
501     }
502 
503     pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
504 }
505 
506 /*-
507  *-----------------------------------------------------------------------
508  * MakeHandleUse --
509  *	Callback function for Lst_ForEach, used by Make_Run on the downward
510  *	pass to handle .USE nodes. Should be called before the children
511  *	are enqueued to be looked at by MakeAddChild.
512  *	This function calls Make_HandleUse to copy the .USE node's commands,
513  *	type flags and children to the parent node.
514  *
515  * Input:
516  *	cgnp		the child we've just examined
517  *	pgnp		the current parent
518  *
519  * Results:
520  *	returns 0.
521  *
522  * Side Effects:
523  *	After expansion, .USE child nodes are removed from the parent
524  *
525  *-----------------------------------------------------------------------
526  */
527 static int
528 MakeHandleUse(void *cgnp, void *pgnp)
529 {
530     GNode	*cgn = (GNode *)cgnp;
531     GNode	*pgn = (GNode *)pgnp;
532     LstNode	ln; 	/* An element in the children list */
533     int		unmarked;
534 
535     unmarked = ((cgn->type & OP_MARK) == 0);
536     cgn->type |= OP_MARK;
537 
538     if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
539 	return (0);
540 
541     if (unmarked)
542 	Make_HandleUse(cgn, pgn);
543 
544     /*
545      * This child node is now "made", so we decrement the count of
546      * unmade children in the parent... We also remove the child
547      * from the parent's list to accurately reflect the number of decent
548      * children the parent has. This is used by Make_Run to decide
549      * whether to queue the parent or examine its children...
550      */
551     if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
552 	Lst_Remove(pgn->children, ln);
553 	pgn->unmade--;
554     }
555     return (0);
556 }
557 
558 
559 /*-
560  *-----------------------------------------------------------------------
561  * Make_Recheck --
562  *	Check the modification time of a gnode, and update it as described
563  *	in the comments below.
564  *
565  * Results:
566  *	returns 0 if the gnode does not exist, or its filesystem
567  *	time if it does.
568  *
569  * Side Effects:
570  *	the gnode's modification time and path name are affected.
571  *
572  *-----------------------------------------------------------------------
573  */
574 time_t
575 Make_Recheck(GNode *gn)
576 {
577     time_t mtime = Dir_MTime(gn, 1);
578 
579 #ifndef RECHECK
580     /*
581      * We can't re-stat the thing, but we can at least take care of rules
582      * where a target depends on a source that actually creates the
583      * target, but only if it has changed, e.g.
584      *
585      * parse.h : parse.o
586      *
587      * parse.o : parse.y
588      *  	yacc -d parse.y
589      *  	cc -c y.tab.c
590      *  	mv y.tab.o parse.o
591      *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
592      *
593      * In this case, if the definitions produced by yacc haven't changed
594      * from before, parse.h won't have been updated and gn->mtime will
595      * reflect the current modification time for parse.h. This is
596      * something of a kludge, I admit, but it's a useful one..
597      * XXX: People like to use a rule like
598      *
599      * FRC:
600      *
601      * To force things that depend on FRC to be made, so we have to
602      * check for gn->children being empty as well...
603      */
604     if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
605 	gn->mtime = now;
606     }
607 #else
608     /*
609      * This is what Make does and it's actually a good thing, as it
610      * allows rules like
611      *
612      *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
613      *
614      * to function as intended. Unfortunately, thanks to the stateless
615      * nature of NFS (by which I mean the loose coupling of two clients
616      * using the same file from a common server), there are times
617      * when the modification time of a file created on a remote
618      * machine will not be modified before the local stat() implied by
619      * the Dir_MTime occurs, thus leading us to believe that the file
620      * is unchanged, wreaking havoc with files that depend on this one.
621      *
622      * I have decided it is better to make too much than to make too
623      * little, so this stuff is commented out unless you're sure it's ok.
624      * -- ardeb 1/12/88
625      */
626     /*
627      * Christos, 4/9/92: If we are  saving commands pretend that
628      * the target is made now. Otherwise archives with ... rules
629      * don't work!
630      */
631     if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
632 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
633 	if (DEBUG(MAKE)) {
634 	    fprintf(debug_file, " recheck(%s): update time from %s to now\n",
635 		   gn->name, Targ_FmtTime(gn->mtime));
636 	}
637 	gn->mtime = now;
638     }
639     else {
640 	if (DEBUG(MAKE)) {
641 	    fprintf(debug_file, " recheck(%s): current update time: %s\n",
642 		   gn->name, Targ_FmtTime(gn->mtime));
643 	}
644     }
645 #endif
646     return mtime;
647 }
648 
649 /*-
650  *-----------------------------------------------------------------------
651  * Make_Update  --
652  *	Perform update on the parents of a node. Used by JobFinish once
653  *	a node has been dealt with and by MakeStartJobs if it finds an
654  *	up-to-date node.
655  *
656  * Input:
657  *	cgn		the child node
658  *
659  * Results:
660  *	Always returns 0
661  *
662  * Side Effects:
663  *	The unmade field of pgn is decremented and pgn may be placed on
664  *	the toBeMade queue if this field becomes 0.
665  *
666  * 	If the child was made, the parent's flag CHILDMADE field will be
667  *	set true.
668  *
669  *	If the child is not up-to-date and still does not exist,
670  *	set the FORCE flag on the parents.
671  *
672  *	If the child wasn't made, the cmgn field of the parent will be
673  *	altered if the child's mtime is big enough.
674  *
675  *	Finally, if the child is the implied source for the parent, the
676  *	parent's IMPSRC variable is set appropriately.
677  *
678  *-----------------------------------------------------------------------
679  */
680 void
681 Make_Update(GNode *cgn)
682 {
683     GNode 	*pgn;	/* the parent node */
684     char  	*cname;	/* the child's name */
685     LstNode	ln; 	/* Element in parents and iParents lists */
686     time_t	mtime = -1;
687     char	*p1;
688     Lst		parents;
689     GNode	*centurion;
690 
691     /* It is save to re-examine any nodes again */
692     checked++;
693 
694     cname = Var_Value(TARGET, cgn, &p1);
695     if (p1)
696 	free(p1);
697 
698     if (DEBUG(MAKE))
699 	fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
700 
701     /*
702      * If the child was actually made, see what its modification time is
703      * now -- some rules won't actually update the file. If the file still
704      * doesn't exist, make its mtime now.
705      */
706     if (cgn->made != UPTODATE) {
707 	mtime = Make_Recheck(cgn);
708     }
709 
710     /*
711      * If this is a `::' node, we must consult its first instance
712      * which is where all parents are linked.
713      */
714     if ((centurion = cgn->centurion) != NULL) {
715 	if (!Lst_IsEmpty(cgn->parents))
716 		Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
717 	centurion->unmade_cohorts -= 1;
718 	if (centurion->unmade_cohorts < 0)
719 	    Error("Graph cycles through centurion %s", centurion->name);
720     } else {
721 	centurion = cgn;
722     }
723     parents = centurion->parents;
724 
725     /* If this was a .ORDER node, schedule the RHS */
726     Lst_ForEach(centurion->order_succ, MakeBuildParent, Lst_First(toBeMade));
727 
728     /* Now mark all the parents as having one less unmade child */
729     if (Lst_Open(parents) == SUCCESS) {
730 	while ((ln = Lst_Next(parents)) != NULL) {
731 	    pgn = (GNode *)Lst_Datum(ln);
732 	    if (DEBUG(MAKE))
733 		fprintf(debug_file, "inspect parent %s%s: flags %x, "
734 			    "type %x, made %d, unmade %d ",
735 			pgn->name, pgn->cohort_num, pgn->flags,
736 			pgn->type, pgn->made, pgn->unmade-1);
737 
738 	    if (!(pgn->flags & REMAKE)) {
739 		/* This parent isn't needed */
740 		if (DEBUG(MAKE))
741 		    fprintf(debug_file, "- not needed\n");
742 		continue;
743 	    }
744 	    if (mtime == 0 && !(cgn->type & OP_WAIT))
745 		pgn->flags |= FORCE;
746 
747 	    /*
748 	     * If the parent has the .MADE attribute, its timestamp got
749 	     * updated to that of its newest child, and its unmake
750 	     * child count got set to zero in Make_ExpandUse().
751 	     * However other things might cause us to build one of its
752 	     * children - and so we mustn't do any processing here when
753 	     * the child build finishes.
754 	     */
755 	    if (pgn->type & OP_MADE) {
756 		if (DEBUG(MAKE))
757 		    fprintf(debug_file, "- .MADE\n");
758 		continue;
759 	    }
760 
761 	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
762 		if (cgn->made == MADE)
763 		    pgn->flags |= CHILDMADE;
764 		(void)Make_TimeStamp(pgn, cgn);
765 	    }
766 
767 	    /*
768 	     * A parent must wait for the completion of all instances
769 	     * of a `::' dependency.
770 	     */
771 	    if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
772 		if (DEBUG(MAKE))
773 		    fprintf(debug_file,
774 			    "- centurion made %d, %d unmade cohorts\n",
775 			    centurion->made, centurion->unmade_cohorts);
776 		continue;
777 	    }
778 
779 	    /* One more child of this parent is now made */
780 	    pgn->unmade -= 1;
781 	    if (pgn->unmade < 0) {
782 		if (DEBUG(MAKE)) {
783 		    fprintf(debug_file, "Graph cycles through %s%s\n",
784 			pgn->name, pgn->cohort_num);
785 		    Targ_PrintGraph(2);
786 		}
787 		Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
788 	    }
789 
790 	    /* We must always rescan the parents of .WAIT and .ORDER nodes. */
791 	    if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
792 		    && !(centurion->flags & DONE_ORDER)) {
793 		if (DEBUG(MAKE))
794 		    fprintf(debug_file, "- unmade children\n");
795 		continue;
796 	    }
797 	    if (pgn->made != DEFERRED) {
798 		/*
799 		 * Either this parent is on a different branch of the tree,
800 		 * or it on the RHS of a .WAIT directive
801 		 * or it is already on the toBeMade list.
802 		 */
803 		if (DEBUG(MAKE))
804 		    fprintf(debug_file, "- not deferred\n");
805 		continue;
806 	    }
807 	    if (pgn->order_pred
808 		    && Lst_ForEach(pgn->order_pred, MakeCheckOrder, 0)) {
809 		/* A .ORDER rule stops us building this */
810 		continue;
811 	    }
812 	    if (DEBUG(MAKE)) {
813 		static int two = 2;
814 		fprintf(debug_file, "- %s%s made, schedule %s%s (made %d)\n",
815 			cgn->name, cgn->cohort_num,
816 			pgn->name, pgn->cohort_num, pgn->made);
817 		Targ_PrintNode(pgn, &two);
818 	    }
819 	    /* Ok, we can schedule the parent again */
820 	    pgn->made = REQUESTED;
821 	    (void)Lst_EnQueue(toBeMade, pgn);
822 	}
823 	Lst_Close(parents);
824     }
825 
826     /*
827      * Set the .PREFIX and .IMPSRC variables for all the implied parents
828      * of this node.
829      */
830     if (Lst_Open(cgn->iParents) == SUCCESS) {
831 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
832 
833 	while ((ln = Lst_Next(cgn->iParents)) != NULL) {
834 	    pgn = (GNode *)Lst_Datum(ln);
835 	    if (pgn->flags & REMAKE) {
836 		Var_Set(IMPSRC, cname, pgn, 0);
837 		if (cpref != NULL)
838 		    Var_Set(PREFIX, cpref, pgn, 0);
839 	    }
840 	}
841 	if (p1)
842 	    free(p1);
843 	Lst_Close(cgn->iParents);
844     }
845 }
846 
847 /*-
848  *-----------------------------------------------------------------------
849  * MakeAddAllSrc --
850  *	Add a child's name to the ALLSRC and OODATE variables of the given
851  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
852  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
853  *	.EXEC and .USE children are very rarely going to be files, so...
854  *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
855  *
856  *	A child is added to the OODATE variable if its modification time is
857  *	later than that of its parent, as defined by Make, except if the
858  *	parent is a .JOIN node. In that case, it is only added to the OODATE
859  *	variable if it was actually made (since .JOIN nodes don't have
860  *	modification times, the comparison is rather unfair...)..
861  *
862  * Results:
863  *	Always returns 0
864  *
865  * Side Effects:
866  *	The ALLSRC variable for the given node is extended.
867  *-----------------------------------------------------------------------
868  */
869 static int
870 MakeUnmark(void *cgnp, void *pgnp MAKE_ATTR_UNUSED)
871 {
872     GNode	*cgn = (GNode *)cgnp;
873 
874     cgn->type &= ~OP_MARK;
875     return (0);
876 }
877 
878 /*
879  * Input:
880  *	cgnp		The child to add
881  *	pgnp		The parent to whose ALLSRC variable it should
882  *			be added
883  *
884  */
885 static int
886 MakeAddAllSrc(void *cgnp, void *pgnp)
887 {
888     GNode	*cgn = (GNode *)cgnp;
889     GNode	*pgn = (GNode *)pgnp;
890 
891     if (cgn->type & OP_MARK)
892 	return (0);
893     cgn->type |= OP_MARK;
894 
895     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
896 	char *child, *allsrc;
897 	char *p1 = NULL, *p2 = NULL;
898 
899 	if (cgn->type & OP_ARCHV)
900 	    child = Var_Value(MEMBER, cgn, &p1);
901 	else
902 	    child = cgn->path ? cgn->path : cgn->name;
903 	if (cgn->type & OP_JOIN) {
904 	    allsrc = Var_Value(ALLSRC, cgn, &p2);
905 	} else {
906 	    allsrc = child;
907 	}
908 	if (allsrc != NULL)
909 		Var_Append(ALLSRC, allsrc, pgn);
910 	if (p2)
911 	    free(p2);
912 	if (pgn->type & OP_JOIN) {
913 	    if (cgn->made == MADE) {
914 		Var_Append(OODATE, child, pgn);
915 	    }
916 	} else if ((pgn->mtime < cgn->mtime) ||
917 		   (cgn->mtime >= now && cgn->made == MADE))
918 	{
919 	    /*
920 	     * It goes in the OODATE variable if the parent is younger than the
921 	     * child or if the child has been modified more recently than
922 	     * the start of the make. This is to keep pmake from getting
923 	     * confused if something else updates the parent after the
924 	     * make starts (shouldn't happen, I know, but sometimes it
925 	     * does). In such a case, if we've updated the kid, the parent
926 	     * is likely to have a modification time later than that of
927 	     * the kid and anything that relies on the OODATE variable will
928 	     * be hosed.
929 	     *
930 	     * XXX: This will cause all made children to go in the OODATE
931 	     * variable, even if they're not touched, if RECHECK isn't defined,
932 	     * since cgn->mtime is set to now in Make_Update. According to
933 	     * some people, this is good...
934 	     */
935 	    Var_Append(OODATE, child, pgn);
936 	}
937 	if (p1)
938 	    free(p1);
939     }
940     return (0);
941 }
942 
943 /*-
944  *-----------------------------------------------------------------------
945  * Make_DoAllVar --
946  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
947  *	done separately, rather than while traversing the graph. This is
948  *	because Make defined OODATE to contain all sources whose modification
949  *	times were later than that of the target, *not* those sources that
950  *	were out-of-date. Since in both compatibility and native modes,
951  *	the modification time of the parent isn't found until the child
952  *	has been dealt with, we have to wait until now to fill in the
953  *	variable. As for ALLSRC, the ordering is important and not
954  *	guaranteed when in native mode, so it must be set here, too.
955  *
956  * Results:
957  *	None
958  *
959  * Side Effects:
960  *	The ALLSRC and OODATE variables of the given node is filled in.
961  *	If the node is a .JOIN node, its TARGET variable will be set to
962  * 	match its ALLSRC variable.
963  *-----------------------------------------------------------------------
964  */
965 void
966 Make_DoAllVar(GNode *gn)
967 {
968     if (gn->flags & DONE_ALLSRC)
969 	return;
970 
971     Lst_ForEach(gn->children, MakeUnmark, gn);
972     Lst_ForEach(gn->children, MakeAddAllSrc, gn);
973 
974     if (!Var_Exists (OODATE, gn)) {
975 	Var_Set(OODATE, "", gn, 0);
976     }
977     if (!Var_Exists (ALLSRC, gn)) {
978 	Var_Set(ALLSRC, "", gn, 0);
979     }
980 
981     if (gn->type & OP_JOIN) {
982 	char *p1;
983 	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
984 	if (p1)
985 	    free(p1);
986     }
987     gn->flags |= DONE_ALLSRC;
988 }
989 
990 /*-
991  *-----------------------------------------------------------------------
992  * MakeStartJobs --
993  *	Start as many jobs as possible.
994  *
995  * Results:
996  *	If the query flag was given to pmake, no job will be started,
997  *	but as soon as an out-of-date target is found, this function
998  *	returns TRUE. At all other times, this function returns FALSE.
999  *
1000  * Side Effects:
1001  *	Nodes are removed from the toBeMade queue and job table slots
1002  *	are filled.
1003  *
1004  *-----------------------------------------------------------------------
1005  */
1006 
1007 static int
1008 MakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
1009 {
1010     GNode *bn = v_bn;
1011 
1012     if (bn->made >= MADE || !(bn->flags & REMAKE))
1013 	return 0;
1014     if (DEBUG(MAKE))
1015 	fprintf(debug_file, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
1016 		bn->name, bn->cohort_num);
1017     return 1;
1018 }
1019 
1020 static int
1021 MakeBuildChild(void *v_cn, void *toBeMade_next)
1022 {
1023     GNode *cn = v_cn;
1024 
1025     if (DEBUG(MAKE))
1026 	fprintf(debug_file, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
1027 	    cn->name, cn->cohort_num, cn->made, cn->type);
1028     if (cn->made > DEFERRED)
1029 	return 0;
1030 
1031     /* If this node is on the RHS of a .ORDER, check LHSs. */
1032     if (cn->order_pred && Lst_ForEach(cn->order_pred, MakeCheckOrder, 0)) {
1033 	/* Can't build this (or anything else in this child list) yet */
1034 	cn->made = DEFERRED;
1035 	return 0;			/* but keep looking */
1036     }
1037 
1038     if (DEBUG(MAKE))
1039 	fprintf(debug_file, "MakeBuildChild: schedule %s%s\n",
1040 		cn->name, cn->cohort_num);
1041 
1042     cn->made = REQUESTED;
1043     if (toBeMade_next == NULL)
1044 	Lst_AtEnd(toBeMade, cn);
1045     else
1046 	Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1047 
1048     if (cn->unmade_cohorts != 0)
1049 	Lst_ForEach(cn->cohorts, MakeBuildChild, toBeMade_next);
1050 
1051     /*
1052      * If this node is a .WAIT node with unmade chlidren
1053      * then don't add the next sibling.
1054      */
1055     return cn->type & OP_WAIT && cn->unmade > 0;
1056 }
1057 
1058 /* When a .ORDER LHS node completes we do this on each RHS */
1059 static int
1060 MakeBuildParent(void *v_pn, void *toBeMade_next)
1061 {
1062     GNode *pn = v_pn;
1063 
1064     if (pn->made != DEFERRED)
1065 	return 0;
1066 
1067     if (MakeBuildChild(pn, toBeMade_next) == 0) {
1068 	/* Mark so that when this node is built we reschedule its parents */
1069 	pn->flags |= DONE_ORDER;
1070     }
1071 
1072     return 0;
1073 }
1074 
1075 static Boolean
1076 MakeStartJobs(void)
1077 {
1078     GNode	*gn;
1079     int		have_token = 0;
1080 
1081     while (!Lst_IsEmpty (toBeMade)) {
1082 	/* Get token now to avoid cycling job-list when we only have 1 token */
1083 	if (!have_token && !Job_TokenWithdraw())
1084 	    break;
1085 	have_token = 1;
1086 
1087 	gn = (GNode *)Lst_DeQueue(toBeMade);
1088 	if (DEBUG(MAKE))
1089 	    fprintf(debug_file, "Examining %s%s...\n",
1090 		    gn->name, gn->cohort_num);
1091 
1092 	if (gn->made != REQUESTED) {
1093 	    if (DEBUG(MAKE))
1094 		fprintf(debug_file, "state %d\n", gn->made);
1095 
1096 	    make_abort(gn, __LINE__);
1097 	}
1098 
1099 	if (gn->checked == checked) {
1100 	    /* We've already looked at this node since a job finished... */
1101 	    if (DEBUG(MAKE))
1102 		fprintf(debug_file, "already checked %s%s\n",
1103 			gn->name, gn->cohort_num);
1104 	    gn->made = DEFERRED;
1105 	    continue;
1106 	}
1107 	gn->checked = checked;
1108 
1109 	if (gn->unmade != 0) {
1110 	    /*
1111 	     * We can't build this yet, add all unmade children to toBeMade,
1112 	     * just before the current first element.
1113 	     */
1114 	    gn->made = DEFERRED;
1115 	    Lst_ForEach(gn->children, MakeBuildChild, Lst_First(toBeMade));
1116 	    /* and drop this node on the floor */
1117 	    if (DEBUG(MAKE))
1118 		fprintf(debug_file, "dropped %s%s\n", gn->name, gn->cohort_num);
1119 	    continue;
1120 	}
1121 
1122 	gn->made = BEINGMADE;
1123 	if (Make_OODate(gn)) {
1124 	    if (DEBUG(MAKE)) {
1125 		fprintf(debug_file, "out-of-date\n");
1126 	    }
1127 	    if (queryFlag) {
1128 		return (TRUE);
1129 	    }
1130 	    Make_DoAllVar(gn);
1131 	    Job_Make(gn);
1132 	    have_token = 0;
1133 	} else {
1134 	    if (DEBUG(MAKE)) {
1135 		fprintf(debug_file, "up-to-date\n");
1136 	    }
1137 	    gn->made = UPTODATE;
1138 	    if (gn->type & OP_JOIN) {
1139 		/*
1140 		 * Even for an up-to-date .JOIN node, we need it to have its
1141 		 * context variables so references to it get the correct
1142 		 * value for .TARGET when building up the context variables
1143 		 * of its parent(s)...
1144 		 */
1145 		Make_DoAllVar(gn);
1146 	    }
1147 	    Make_Update(gn);
1148 	}
1149     }
1150 
1151     if (have_token)
1152 	Job_TokenReturn();
1153 
1154     return (FALSE);
1155 }
1156 
1157 /*-
1158  *-----------------------------------------------------------------------
1159  * MakePrintStatus --
1160  *	Print the status of a top-level node, viz. it being up-to-date
1161  *	already or not created due to an error in a lower level.
1162  *	Callback function for Make_Run via Lst_ForEach.
1163  *
1164  * Input:
1165  *	gnp		Node to examine
1166  *	cyclep		True if gn->unmade being non-zero implies a
1167  *			cycle in the graph, not an error in an
1168  *			inferior.
1169  *
1170  * Results:
1171  *	Always returns 0.
1172  *
1173  * Side Effects:
1174  *	A message may be printed.
1175  *
1176  *-----------------------------------------------------------------------
1177  */
1178 static int
1179 MakePrintStatusOrder(void *ognp, void *gnp)
1180 {
1181     GNode *ogn = ognp;
1182     GNode *gn = gnp;
1183 
1184     if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
1185 	/* not waiting for this one */
1186 	return 0;
1187 
1188     printf("    `%s%s' has .ORDER dependency against %s%s "
1189 		"(made %d, flags %x, type %x)\n",
1190 	    gn->name, gn->cohort_num,
1191 	    ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1192     if (DEBUG(MAKE) && debug_file != stdout)
1193 	fprintf(debug_file, "    `%s%s' has .ORDER dependency against %s%s "
1194 		    "(made %d, flags %x, type %x)\n",
1195 		gn->name, gn->cohort_num,
1196 		ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1197     return 0;
1198 }
1199 
1200 static int
1201 MakePrintStatus(void *gnp, void *v_errors)
1202 {
1203     GNode   	*gn = (GNode *)gnp;
1204     int 	*errors = v_errors;
1205 
1206     if (gn->flags & DONECYCLE)
1207 	/* We've completely processed this node before, don't do it again. */
1208 	return 0;
1209 
1210     if (gn->unmade == 0) {
1211 	gn->flags |= DONECYCLE;
1212 	switch (gn->made) {
1213 	case UPTODATE:
1214 	    printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1215 	    break;
1216 	case MADE:
1217 	    break;
1218 	case UNMADE:
1219 	case DEFERRED:
1220 	case REQUESTED:
1221 	case BEINGMADE:
1222 	    (*errors)++;
1223 	    printf("`%s%s' was not built (made %d, flags %x, type %x)!\n",
1224 		    gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1225 	    if (DEBUG(MAKE) && debug_file != stdout)
1226 		fprintf(debug_file,
1227 			"`%s%s' was not built (made %d, flags %x, type %x)!\n",
1228 			gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1229 	    /* Most likely problem is actually caused by .ORDER */
1230 	    Lst_ForEach(gn->order_pred, MakePrintStatusOrder, gn);
1231 	    break;
1232 	default:
1233 	    /* Errors - already counted */
1234 	    printf("`%s%s' not remade because of errors.\n",
1235 		    gn->name, gn->cohort_num);
1236 	    if (DEBUG(MAKE) && debug_file != stdout)
1237 		fprintf(debug_file, "`%s%s' not remade because of errors.\n",
1238 			gn->name, gn->cohort_num);
1239 	    break;
1240 	}
1241 	return 0;
1242     }
1243 
1244     if (DEBUG(MAKE))
1245 	fprintf(debug_file, "MakePrintStatus: %s%s has %d unmade children\n",
1246 		gn->name, gn->cohort_num, gn->unmade);
1247     /*
1248      * If printing cycles and came to one that has unmade children,
1249      * print out the cycle by recursing on its children.
1250      */
1251     if (!(gn->flags & CYCLE)) {
1252 	/* Fist time we've seen this node, check all children */
1253 	gn->flags |= CYCLE;
1254 	Lst_ForEach(gn->children, MakePrintStatus, errors);
1255 	/* Mark that this node needn't be processed again */
1256 	gn->flags |= DONECYCLE;
1257 	return 0;
1258     }
1259 
1260     /* Only output the error once per node */
1261     gn->flags |= DONECYCLE;
1262     Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1263     if ((*errors)++ > 100)
1264 	/* Abandon the whole error report */
1265 	return 1;
1266 
1267     /* Reporting for our children will give the rest of the loop */
1268     Lst_ForEach(gn->children, MakePrintStatus, errors);
1269     return 0;
1270 }
1271 
1272 
1273 /*-
1274  *-----------------------------------------------------------------------
1275  * Make_ExpandUse --
1276  *	Expand .USE nodes and create a new targets list
1277  *
1278  * Input:
1279  *	targs		the initial list of targets
1280  *
1281  * Side Effects:
1282  *-----------------------------------------------------------------------
1283  */
1284 void
1285 Make_ExpandUse(Lst targs)
1286 {
1287     GNode  *gn;		/* a temporary pointer */
1288     Lst    examine; 	/* List of targets to examine */
1289 
1290     examine = Lst_Duplicate(targs, NULL);
1291 
1292     /*
1293      * Make an initial downward pass over the graph, marking nodes to be made
1294      * as we go down. We call Suff_FindDeps to find where a node is and
1295      * to get some children for it if it has none and also has no commands.
1296      * If the node is a leaf, we stick it on the toBeMade queue to
1297      * be looked at in a minute, otherwise we add its children to our queue
1298      * and go on about our business.
1299      */
1300     while (!Lst_IsEmpty (examine)) {
1301 	gn = (GNode *)Lst_DeQueue(examine);
1302 
1303 	if (gn->flags & REMAKE)
1304 	    /* We've looked at this one already */
1305 	    continue;
1306 	gn->flags |= REMAKE;
1307 	if (DEBUG(MAKE))
1308 	    fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
1309 		    gn->name, gn->cohort_num);
1310 
1311 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
1312 	    /* Append all the 'cohorts' to the list of things to examine */
1313 	    Lst new;
1314 	    new = Lst_Duplicate(gn->cohorts, NULL);
1315 	    Lst_Concat(new, examine, LST_CONCLINK);
1316 	    examine = new;
1317 	}
1318 
1319 	/*
1320 	 * Apply any .USE rules before looking for implicit dependencies
1321 	 * to make sure everything has commands that should...
1322 	 * Make sure that the TARGET is set, so that we can make
1323 	 * expansions.
1324 	 */
1325 	if (gn->type & OP_ARCHV) {
1326 	    char *eoa, *eon;
1327 	    eoa = strchr(gn->name, '(');
1328 	    eon = strchr(gn->name, ')');
1329 	    if (eoa == NULL || eon == NULL)
1330 		continue;
1331 	    *eoa = '\0';
1332 	    *eon = '\0';
1333 	    Var_Set(MEMBER, eoa + 1, gn, 0);
1334 	    Var_Set(ARCHIVE, gn->name, gn, 0);
1335 	    *eoa = '(';
1336 	    *eon = ')';
1337 	}
1338 
1339 	(void)Dir_MTime(gn, 0);
1340 	Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
1341 	Lst_ForEach(gn->children, MakeUnmark, gn);
1342 	Lst_ForEach(gn->children, MakeHandleUse, gn);
1343 
1344 	if ((gn->type & OP_MADE) == 0)
1345 	    Suff_FindDeps(gn);
1346 	else {
1347 	    /* Pretend we made all this node's children */
1348 	    Lst_ForEach(gn->children, MakeFindChild, gn);
1349 	    if (gn->unmade != 0)
1350 		    printf("Warning: %s%s still has %d unmade children\n",
1351 			    gn->name, gn->cohort_num, gn->unmade);
1352 	}
1353 
1354 	if (gn->unmade != 0)
1355 	    Lst_ForEach(gn->children, MakeAddChild, examine);
1356     }
1357 
1358     Lst_Destroy(examine, NULL);
1359 }
1360 
1361 /*-
1362  *-----------------------------------------------------------------------
1363  * Make_ProcessWait --
1364  *	Convert .WAIT nodes into dependencies
1365  *
1366  * Input:
1367  *	targs		the initial list of targets
1368  *
1369  *-----------------------------------------------------------------------
1370  */
1371 
1372 static int
1373 link_parent(void *cnp, void *pnp)
1374 {
1375     GNode *cn = cnp;
1376     GNode *pn = pnp;
1377 
1378     Lst_AtEnd(pn->children, cn);
1379     Lst_AtEnd(cn->parents, pn);
1380     pn->unmade++;
1381     return 0;
1382 }
1383 
1384 static int
1385 add_wait_dep(void *v_cn, void *v_wn)
1386 {
1387     GNode *cn = v_cn;
1388     GNode *wn = v_wn;
1389 
1390     if (cn == wn)
1391 	return 1;
1392 
1393     if (cn == NULL || wn == NULL) {
1394 	printf("bad wait dep %p %p\n", cn, wn);
1395 	exit(4);
1396     }
1397     if (DEBUG(MAKE))
1398 	 fprintf(debug_file, ".WAIT: add dependency %s%s -> %s\n",
1399 		cn->name, cn->cohort_num, wn->name);
1400 
1401     Lst_AtEnd(wn->children, cn);
1402     wn->unmade++;
1403     Lst_AtEnd(cn->parents, wn);
1404     return 0;
1405 }
1406 
1407 static void
1408 Make_ProcessWait(Lst targs)
1409 {
1410     GNode  *pgn;	/* 'parent' node we are examining */
1411     GNode  *cgn;	/* Each child in turn */
1412     LstNode owln;	/* Previous .WAIT node */
1413     Lst    examine; 	/* List of targets to examine */
1414     LstNode ln;
1415 
1416     /*
1417      * We need all the nodes to have a common parent in order for the
1418      * .WAIT and .ORDER scheduling to work.
1419      * Perhaps this should be done earlier...
1420      */
1421 
1422     pgn = Targ_NewGN(".MAIN");
1423     pgn->flags = REMAKE;
1424     pgn->type = OP_PHONY | OP_DEPENDS;
1425     /* Get it displayed in the diag dumps */
1426     Lst_AtFront(Targ_List(), pgn);
1427 
1428     Lst_ForEach(targs, link_parent, pgn);
1429 
1430     /* Start building with the 'dummy' .MAIN' node */
1431     MakeBuildChild(pgn, NULL);
1432 
1433     examine = Lst_Init(FALSE);
1434     Lst_AtEnd(examine, pgn);
1435 
1436     while (!Lst_IsEmpty (examine)) {
1437 	pgn = Lst_DeQueue(examine);
1438 
1439 	/* We only want to process each child-list once */
1440 	if (pgn->flags & DONE_WAIT)
1441 	    continue;
1442 	pgn->flags |= DONE_WAIT;
1443 	if (DEBUG(MAKE))
1444 	    fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
1445 
1446 	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) {
1447 	    /* Append all the 'cohorts' to the list of things to examine */
1448 	    Lst new;
1449 	    new = Lst_Duplicate(pgn->cohorts, NULL);
1450 	    Lst_Concat(new, examine, LST_CONCLINK);
1451 	    examine = new;
1452 	}
1453 
1454 	owln = Lst_First(pgn->children);
1455 	Lst_Open(pgn->children);
1456 	for (; (ln = Lst_Next(pgn->children)) != NULL; ) {
1457 	    cgn = Lst_Datum(ln);
1458 	    if (cgn->type & OP_WAIT) {
1459 		/* Make the .WAIT node depend on the previous children */
1460 		Lst_ForEachFrom(pgn->children, owln, add_wait_dep, cgn);
1461 		owln = ln;
1462 	    } else {
1463 		Lst_AtEnd(examine, cgn);
1464 	    }
1465 	}
1466 	Lst_Close(pgn->children);
1467     }
1468 
1469     Lst_Destroy(examine, NULL);
1470 }
1471 
1472 /*-
1473  *-----------------------------------------------------------------------
1474  * Make_Run --
1475  *	Initialize the nodes to remake and the list of nodes which are
1476  *	ready to be made by doing a breadth-first traversal of the graph
1477  *	starting from the nodes in the given list. Once this traversal
1478  *	is finished, all the 'leaves' of the graph are in the toBeMade
1479  *	queue.
1480  *	Using this queue and the Job module, work back up the graph,
1481  *	calling on MakeStartJobs to keep the job table as full as
1482  *	possible.
1483  *
1484  * Input:
1485  *	targs		the initial list of targets
1486  *
1487  * Results:
1488  *	TRUE if work was done. FALSE otherwise.
1489  *
1490  * Side Effects:
1491  *	The make field of all nodes involved in the creation of the given
1492  *	targets is set to 1. The toBeMade list is set to contain all the
1493  *	'leaves' of these subgraphs.
1494  *-----------------------------------------------------------------------
1495  */
1496 Boolean
1497 Make_Run(Lst targs)
1498 {
1499     int	    	    errors; 	/* Number of errors the Job module reports */
1500 
1501     /* Start trying to make the current targets... */
1502     toBeMade = Lst_Init(FALSE);
1503 
1504     Make_ExpandUse(targs);
1505     Make_ProcessWait(targs);
1506 
1507     if (DEBUG(MAKE)) {
1508 	 fprintf(debug_file, "#***# full graph\n");
1509 	 Targ_PrintGraph(1);
1510     }
1511 
1512     if (queryFlag) {
1513 	/*
1514 	 * We wouldn't do any work unless we could start some jobs in the
1515 	 * next loop... (we won't actually start any, of course, this is just
1516 	 * to see if any of the targets was out of date)
1517 	 */
1518 	return (MakeStartJobs());
1519     }
1520     /*
1521      * Initialization. At the moment, no jobs are running and until some
1522      * get started, nothing will happen since the remaining upward
1523      * traversal of the graph is performed by the routines in job.c upon
1524      * the finishing of a job. So we fill the Job table as much as we can
1525      * before going into our loop.
1526      */
1527     (void)MakeStartJobs();
1528 
1529     /*
1530      * Main Loop: The idea here is that the ending of jobs will take
1531      * care of the maintenance of data structures and the waiting for output
1532      * will cause us to be idle most of the time while our children run as
1533      * much as possible. Because the job table is kept as full as possible,
1534      * the only time when it will be empty is when all the jobs which need
1535      * running have been run, so that is the end condition of this loop.
1536      * Note that the Job module will exit if there were any errors unless the
1537      * keepgoing flag was given.
1538      */
1539     while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1540 	Job_CatchOutput();
1541 	(void)MakeStartJobs();
1542     }
1543 
1544     errors = Job_Finish();
1545 
1546     /*
1547      * Print the final status of each target. E.g. if it wasn't made
1548      * because some inferior reported an error.
1549      */
1550     if (DEBUG(MAKE))
1551 	 fprintf(debug_file, "done: errors %d\n", errors);
1552     if (errors == 0) {
1553 	Lst_ForEach(targs, MakePrintStatus, &errors);
1554 	if (DEBUG(MAKE)) {
1555 	    fprintf(debug_file, "done: errors %d\n", errors);
1556 	    if (errors)
1557 		Targ_PrintGraph(4);
1558 	}
1559     }
1560     return errors != 0;
1561 }
1562