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