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