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