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