xref: /openbsd-src/usr.bin/make/make.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: make.c,v 1.33 2003/06/03 02:56:12 millert Exp $	*/
3 /*	$NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1988, 1989, 1990, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * Copyright (c) 1989 by Berkeley Softworks
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Adam de Boor.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 /*-
40  * make.c --
41  *	The functions which perform the examination of targets and
42  *	their suitability for creation
43  *
44  * Interface:
45  *	Make_Run		Initialize things for the module and recreate
46  *				whatever needs recreating. Returns true if
47  *				work was (or would have been) done and
48  *				false
49  *				otherwise.
50  *
51  *	Make_Update		Update all parents of a given child. Performs
52  *				various bookkeeping chores like the updating
53  *				of the cmtime field of the parent, filling
54  *				of the IMPSRC context variable, etc. It will
55  *				place the parent on the toBeMade queue if it
56  *				should be.
57  *
58  *	Make_TimeStamp		Function to set the parent's cmtime field
59  *				based on a child's modification time.
60  *
61  *	Make_DoAllVar		Set up the various local variables for a
62  *				target, including the .ALLSRC variable, making
63  *				sure that any variable that needs to exist
64  *				at the very least has the empty value.
65  *
66  *	Make_OODate		Determine if a target is out-of-date.
67  *
68  *	Make_HandleUse		See if a child is a .USE node for a parent
69  *				and perform the .USE actions if so.
70  */
71 
72 #include <limits.h>
73 #include <stdio.h>
74 #include "config.h"
75 #include "defines.h"
76 #include "dir.h"
77 #include "job.h"
78 #include "arch.h"
79 #include "suff.h"
80 #include "var.h"
81 #include "targ.h"
82 #include "error.h"
83 #include "make.h"
84 #include "gnode.h"
85 #include "extern.h"
86 #include "timestamp.h"
87 #include "lst.h"
88 
89 static LIST	toBeMade;	/* The current fringe of the graph. These
90 				 * are nodes which await examination by
91 				 * MakeOODate. It is added to by
92 				 * Make_Update and subtracted from by
93 				 * MakeStartJobs */
94 static int	numNodes;	/* Number of nodes to be processed. If this
95 				 * is non-zero when Job_Empty() returns
96 				 * true, there's a cycle in the graph */
97 
98 static void MakeAddChild(void *, void *);
99 static void MakeAddAllSrc(void *, void *);
100 static void MakeTimeStamp(void *, void *);
101 static void MakeHandleUse(void *, void *);
102 static bool MakeStartJobs(void);
103 static void MakePrintStatus(void *, void *);
104 /*-
105  *-----------------------------------------------------------------------
106  * Make_TimeStamp --
107  *	Set the cmtime field of a parent node based on the mtime stamp in its
108  *	child.
109  *
110  * Side Effects:
111  *	The cmtime of the parent node will be changed if the mtime
112  *	field of the child is greater than it.
113  *-----------------------------------------------------------------------
114  */
115 void
116 Make_TimeStamp(pgn, cgn)
117     GNode *pgn; /* the current parent */
118     GNode *cgn; /* the child we've just examined */
119 {
120     if (is_strictly_before(pgn->cmtime, cgn->mtime))
121 	pgn->cmtime = cgn->mtime;
122 }
123 
124 /* Wrapper to call Make_TimeStamp from a forEach loop.	*/
125 static void
126 MakeTimeStamp(pgn, cgn)
127     void *pgn;	/* the current parent */
128     void *cgn;	/* the child we've just examined */
129 {
130     Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
131 }
132 
133 /*-
134  *-----------------------------------------------------------------------
135  * Make_OODate --
136  *	See if a given node is out of date with respect to its sources.
137  *	Used by Make_Run when deciding which nodes to place on the
138  *	toBeMade queue initially and by Make_Update to screen out USE and
139  *	EXEC nodes. In the latter case, however, any other sort of node
140  *	must be considered out-of-date since at least one of its children
141  *	will have been recreated.
142  *
143  * Results:
144  *	true if the node is out of date. false otherwise.
145  *
146  * Side Effects:
147  *	The mtime field of the node and the cmtime field of its parents
148  *	will/may be changed.
149  *-----------------------------------------------------------------------
150  */
151 bool
152 Make_OODate(gn)
153     GNode	    *gn;	      /* the node to check */
154 {
155     bool	    oodate;
156 
157     /*
158      * Certain types of targets needn't even be sought as their datedness
159      * doesn't depend on their modification time...
160      */
161     if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
162 	(void)Dir_MTime(gn);
163 	if (DEBUG(MAKE)) {
164 	    if (!is_out_of_date(gn->mtime)) {
165 		printf("modified %s...", Targ_FmtTime(gn->mtime));
166 	    } else {
167 		printf("non-existent...");
168 	    }
169 	}
170     }
171 
172     /*
173      * A target is remade in one of the following circumstances:
174      *	its modification time is smaller than that of its youngest child
175      *	    and it would actually be run (has commands or type OP_NOP)
176      *	it's the object of a force operator
177      *	it has no children, was on the lhs of an operator and doesn't exist
178      *	    already.
179      *
180      * Libraries are only considered out-of-date if the archive module says
181      * they are.
182      *
183      * These weird rules are brought to you by Backward-Compatability and
184      * the strange people who wrote 'Make'.
185      */
186     if (gn->type & OP_USE) {
187 	/*
188 	 * If the node is a USE node it is *never* out of date
189 	 * no matter *what*.
190 	 */
191 	if (DEBUG(MAKE)) {
192 	    printf(".USE node...");
193 	}
194 	oodate = false;
195     } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
196 	if (DEBUG(MAKE)) {
197 	    printf("library...");
198 	}
199 
200 	/*
201 	 * always out of date if no children and :: target
202 	 */
203 
204 	oodate = Arch_LibOODate(gn) ||
205 	    (is_out_of_date(gn->cmtime) && (gn->type & OP_DOUBLEDEP));
206     } else if (gn->type & OP_JOIN) {
207 	/*
208 	 * A target with the .JOIN attribute is only considered
209 	 * out-of-date if any of its children was out-of-date.
210 	 */
211 	if (DEBUG(MAKE)) {
212 	    printf(".JOIN node...");
213 	}
214 	oodate = gn->childMade;
215     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
216 	/*
217 	 * A node which is the object of the force (!) operator or which has
218 	 * the .EXEC attribute is always considered out-of-date.
219 	 */
220 	if (DEBUG(MAKE)) {
221 	    if (gn->type & OP_FORCE) {
222 		printf("! operator...");
223 	    } else if (gn->type & OP_PHONY) {
224 		printf(".PHONY node...");
225 	    } else {
226 		printf(".EXEC node...");
227 	    }
228 	}
229 	oodate = true;
230     } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
231 	       (is_out_of_date(gn->cmtime) &&
232 		(is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP))))
233     {
234 	/*
235 	 * A node whose modification time is less than that of its
236 	 * youngest child or that has no children (cmtime == OUT_OF_DATE) and
237 	 * either doesn't exist (mtime == OUT_OF_DATE) or was the object of a
238 	 * :: operator is out-of-date. Why? Because that's the way Make does
239 	 * it.
240 	 */
241 	if (DEBUG(MAKE)) {
242 	    if (is_strictly_before(gn->mtime, gn->cmtime)) {
243 		printf("modified before source...");
244 	    } else if (is_out_of_date(gn->mtime)) {
245 		printf("non-existent and no sources...");
246 	    } else {
247 		printf(":: operator and no sources...");
248 	    }
249 	}
250 	oodate = true;
251     } else {
252 #if 0
253 	/* WHY? */
254 	if (DEBUG(MAKE)) {
255 	    printf("source %smade...", gn->childMade ? "" : "not ");
256 	}
257 	oodate = gn->childMade;
258 #else
259 	oodate = false;
260 #endif /* 0 */
261     }
262 
263     /*
264      * If the target isn't out-of-date, the parents need to know its
265      * modification time. Note that targets that appear to be out-of-date
266      * but aren't, because they have no commands and aren't of type OP_NOP,
267      * have their mtime stay below their children's mtime to keep parents from
268      * thinking they're out-of-date.
269      */
270     if (!oodate)
271 	Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
272 
273     return oodate;
274 }
275 
276 /*-
277  *-----------------------------------------------------------------------
278  * MakeAddChild  --
279  *	Function used by Make_Run to add a child to the list l.
280  *	It will only add the child if its make field is false.
281  *
282  * Side Effects:
283  *	The given list is extended
284  *-----------------------------------------------------------------------
285  */
286 static void
287 MakeAddChild(gnp, lp)
288     void *gnp;		/* the node to add */
289     void *lp;		/* the list to which to add it */
290 {
291     GNode	   *gn = (GNode *)gnp;
292     Lst 	   l = (Lst)lp;
293 
294     if (!gn->make && !(gn->type & OP_USE))
295 	Lst_EnQueue(l, gn);
296 }
297 
298 /*-
299  *-----------------------------------------------------------------------
300  * Make_HandleUse --
301  *	Function called by Make_Run and SuffApplyTransform on the downward
302  *	pass to handle .USE and transformation nodes. A callback function
303  *	for Lst_ForEach, it implements the .USE and transformation
304  *	functionality by copying the node's commands, type flags
305  *	and children to the parent node. Should be called before the
306  *	children are enqueued to be looked at by MakeAddChild.
307  *
308  *	A .USE node is much like an explicit transformation rule, except
309  *	its commands are always added to the target node, even if the
310  *	target already has commands.
311  *
312  * Side Effects:
313  *	Children and commands may be added to the parent and the parent's
314  *	type may be changed.
315  *
316  *-----------------------------------------------------------------------
317  */
318 void
319 Make_HandleUse(cgn, pgn)
320     GNode	*cgn;	/* The .USE node */
321     GNode	*pgn;	/* The target of the .USE node */
322 {
323     GNode	*gn;	/* A child of the .USE node */
324     LstNode	ln;	/* An element in the children list */
325 
326     if (cgn->type & (OP_USE|OP_TRANSFORM)) {
327 	if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
328 	    /* .USE or transformation and target has no commands -- append
329 	     * the child's commands to the parent.  */
330 	    Lst_Concat(&pgn->commands, &cgn->commands);
331 	}
332 
333 	for (ln = Lst_First(&cgn->children); ln != NULL; ln = Lst_Adv(ln)) {
334 	    gn = (GNode *)Lst_Datum(ln);
335 
336 	    if (Lst_AddNew(&pgn->children, gn)) {
337 		Lst_AtEnd(&gn->parents, pgn);
338 		pgn->unmade += 1;
339 	    }
340 	}
341 
342 	pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
343 
344 	/*
345 	 * This child node is now "made", so we decrement the count of
346 	 * unmade children in the parent... We also remove the child
347 	 * from the parent's list to accurately reflect the number of decent
348 	 * children the parent has. This is used by Make_Run to decide
349 	 * whether to queue the parent or examine its children...
350 	 */
351 	if (cgn->type & OP_USE) {
352 	    pgn->unmade--;
353 	}
354     }
355 }
356 static void
357 MakeHandleUse(pgn, cgn)
358     void *pgn;	/* the current parent */
359     void *cgn;	/* the child we've just examined */
360 {
361     Make_HandleUse((GNode *)pgn, (GNode *)cgn);
362 }
363 
364 /*-
365  *-----------------------------------------------------------------------
366  * Make_Update	--
367  *	Perform update on the parents of a node. Used by JobFinish once
368  *	a node has been dealt with and by MakeStartJobs if it finds an
369  *	up-to-date node.
370  *
371  * Results:
372  *	Always returns 0
373  *
374  * Side Effects:
375  *	The unmade field of pgn is decremented and pgn may be placed on
376  *	the toBeMade queue if this field becomes 0.
377  *
378  *	If the child was made, the parent's childMade field will be set true
379  *	and its cmtime set to now.
380  *
381  *	If the child wasn't made, the cmtime field of the parent will be
382  *	altered if the child's mtime is big enough.
383  *
384  *	Finally, if the child is the implied source for the parent, the
385  *	parent's IMPSRC variable is set appropriately.
386  *
387  *-----------------------------------------------------------------------
388  */
389 void
390 Make_Update(cgn)
391     GNode	*cgn;	/* the child node */
392 {
393     GNode	*pgn;	/* the parent node */
394     char	*cname; /* the child's name */
395     LstNode	ln;	/* Element in parents and iParents lists */
396 
397     cname = Varq_Value(TARGET_INDEX, cgn);
398 
399     /*
400      * If the child was actually made, see what its modification time is
401      * now -- some rules won't actually update the file. If the file still
402      * doesn't exist, make its mtime now.
403      */
404     if (cgn->made != UPTODATE) {
405 #ifndef RECHECK
406 	/*
407 	 * We can't re-stat the thing, but we can at least take care of rules
408 	 * where a target depends on a source that actually creates the
409 	 * target, but only if it has changed, e.g.
410 	 *
411 	 * parse.h : parse.o
412 	 *
413 	 * parse.o : parse.y
414 	 *	yacc -d parse.y
415 	 *	cc -c y.tab.c
416 	 *	mv y.tab.o parse.o
417 	 *	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
418 	 *
419 	 * In this case, if the definitions produced by yacc haven't changed
420 	 * from before, parse.h won't have been updated and cgn->mtime will
421 	 * reflect the current modification time for parse.h. This is
422 	 * something of a kludge, I admit, but it's a useful one..
423 	 * XXX: People like to use a rule like
424 	 *
425 	 * FRC:
426 	 *
427 	 * To force things that depend on FRC to be made, so we have to
428 	 * check for gn->children being empty as well...
429 	 */
430 	if (!Lst_IsEmpty(&cgn->commands) || Lst_IsEmpty(&cgn->children)) {
431 	    cgn->mtime = now;
432 	}
433 #else
434 	/*
435 	 * This is what Make does and it's actually a good thing, as it
436 	 * allows rules like
437 	 *
438 	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
439 	 *
440 	 * to function as intended. Unfortunately, thanks to the stateless
441 	 * nature of NFS (by which I mean the loose coupling of two clients
442 	 * using the same file from a common server), there are times
443 	 * when the modification time of a file created on a remote
444 	 * machine will not be modified before the local stat() implied by
445 	 * the Dir_MTime occurs, thus leading us to believe that the file
446 	 * is unchanged, wreaking havoc with files that depend on this one.
447 	 *
448 	 * I have decided it is better to make too much than to make too
449 	 * little, so this stuff is commented out unless you're sure it's ok.
450 	 * -- ardeb 1/12/88
451 	 */
452 	/*
453 	 * Christos, 4/9/92: If we are	saving commands pretend that
454 	 * the target is made now. Otherwise archives with ... rules
455 	 * don't work!
456 	 */
457 	if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
458 	    is_out_of_date(Dir_MTime(cgn))) {
459 	    cgn->mtime = now;
460 	}
461 	if (DEBUG(MAKE)) {
462 	    printf("update time: %s\n", Targ_FmtTime(cgn->mtime));
463 	}
464 #endif
465     }
466 
467     for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
468 	pgn = (GNode *)Lst_Datum(ln);
469 	if (pgn->make) {
470 	    pgn->unmade -= 1;
471 
472 	    if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
473 		if (cgn->made == MADE) {
474 		    pgn->childMade = true;
475 		    if (is_strictly_before(pgn->cmtime, cgn->mtime))
476 			pgn->cmtime = cgn->mtime;
477 		} else {
478 		    (void)Make_TimeStamp(pgn, cgn);
479 		}
480 	    }
481 	    if (pgn->unmade == 0) {
482 		/*
483 		 * Queue the node up -- any unmade predecessors will
484 		 * be dealt with in MakeStartJobs.
485 		 */
486 		Lst_EnQueue(&toBeMade, pgn);
487 	    } else if (pgn->unmade < 0) {
488 		Error("Graph cycles through %s", pgn->name);
489 	    }
490 	}
491     }
492     /* Deal with successor nodes. If any is marked for making and has an unmade
493      * count of 0, has not been made and isn't in the examination queue,
494      * it means we need to place it in the queue as it restrained itself
495      * before.	*/
496     for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
497 	GNode	*succ = (GNode *)Lst_Datum(ln);
498 
499 	if (succ->make && succ->unmade == 0 && succ->made == UNMADE)
500 	    (void)Lst_QueueNew(&toBeMade, succ);
501     }
502 
503     /* Set the .PREFIX and .IMPSRC variables for all the implied parents
504      * of this node.  */
505     {
506     char	*cpref = Varq_Value(PREFIX_INDEX, cgn);
507 
508     for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Adv(ln)) {
509 	pgn = (GNode *)Lst_Datum(ln);
510 	if (pgn->make) {
511 	    Varq_Set(IMPSRC_INDEX, cname, pgn);
512 	    Varq_Set(PREFIX_INDEX, cpref, pgn);
513 	}
514     }
515     }
516 }
517 
518 /*-
519  *-----------------------------------------------------------------------
520  * MakeAddAllSrc --
521  *	Add a child's name to the ALLSRC and OODATE variables of the given
522  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
523  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
524  *	.EXEC and .USE children are very rarely going to be files, so...
525  *	A child is added to the OODATE variable if its modification time is
526  *	later than that of its parent, as defined by Make, except if the
527  *	parent is a .JOIN node. In that case, it is only added to the OODATE
528  *	variable if it was actually made (since .JOIN nodes don't have
529  *	modification times, the comparison is rather unfair...)..
530  *
531  * Side Effects:
532  *	The ALLSRC variable for the given node is extended.
533  *-----------------------------------------------------------------------
534  */
535 static void
536 MakeAddAllSrc(cgnp, pgnp)
537     void *cgnp; /* The child to add */
538     void *pgnp; /* The parent to whose ALLSRC variable it should be */
539 			/* added */
540 {
541     GNode	*cgn = (GNode *)cgnp;
542     GNode	*pgn = (GNode *)pgnp;
543     if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
544 	const char *child;
545 
546 	if (OP_NOP(cgn->type) ||
547 	    (child = Varq_Value(TARGET_INDEX, cgn)) == NULL) {
548 	    /*
549 	     * this node is only source; use the specific pathname for it
550 	     */
551 	    child = cgn->path != NULL ? cgn->path : cgn->name;
552 	}
553 
554 	Varq_Append(ALLSRC_INDEX, child, pgn);
555 	if (pgn->type & OP_JOIN) {
556 	    if (cgn->made == MADE) {
557 		Varq_Append(OODATE_INDEX, child, pgn);
558 	    }
559 	} else if (is_strictly_before(pgn->mtime, cgn->mtime) ||
560 		   (!is_strictly_before(cgn->mtime, now) && cgn->made == MADE))
561 	{
562 	    /*
563 	     * It goes in the OODATE variable if the parent is younger than the
564 	     * child or if the child has been modified more recently than
565 	     * the start of the make. This is to keep pmake from getting
566 	     * confused if something else updates the parent after the
567 	     * make starts (shouldn't happen, I know, but sometimes it
568 	     * does). In such a case, if we've updated the kid, the parent
569 	     * is likely to have a modification time later than that of
570 	     * the kid and anything that relies on the OODATE variable will
571 	     * be hosed.
572 	     *
573 	     * XXX: This will cause all made children to go in the OODATE
574 	     * variable, even if they're not touched, if RECHECK isn't defined,
575 	     * since cgn->mtime is set to now in Make_Update. According to
576 	     * some people, this is good...
577 	     */
578 	    Varq_Append(OODATE_INDEX, child, pgn);
579 	}
580     }
581 }
582 
583 /*-
584  *-----------------------------------------------------------------------
585  * Make_DoAllVar --
586  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
587  *	done separately, rather than while traversing the graph. This is
588  *	because Make defined OODATE to contain all sources whose modification
589  *	times were later than that of the target, *not* those sources that
590  *	were out-of-date. Since in both compatibility and native modes,
591  *	the modification time of the parent isn't found until the child
592  *	has been dealt with, we have to wait until now to fill in the
593  *	variable. As for ALLSRC, the ordering is important and not
594  *	guaranteed when in native mode, so it must be set here, too.
595  *
596  * Side Effects:
597  *	The ALLSRC and OODATE variables of the given node is filled in.
598  *	If the node is a .JOIN node, its TARGET variable will be set to
599  *	match its ALLSRC variable.
600  *-----------------------------------------------------------------------
601  */
602 void
603 Make_DoAllVar(gn)
604     GNode	*gn;
605 {
606     Lst_ForEach(&gn->children, MakeAddAllSrc, gn);
607 
608     if (Varq_Value(OODATE_INDEX, gn) == NULL)
609 	Varq_Set(OODATE_INDEX, "", gn);
610     if (Varq_Value(ALLSRC_INDEX, gn) == NULL)
611 	Varq_Set(ALLSRC_INDEX, "", gn);
612 
613     if (gn->type & OP_JOIN)
614 	Varq_Set(TARGET_INDEX, Varq_Value(ALLSRC_INDEX, gn), gn);
615 }
616 
617 /*-
618  *-----------------------------------------------------------------------
619  * MakeStartJobs --
620  *	Start as many jobs as possible.
621  *
622  * Results:
623  *	If the query flag was given to pmake, no job will be started,
624  *	but as soon as an out-of-date target is found, this function
625  *	returns true. At all other times, this function returns false.
626  *
627  * Side Effects:
628  *	Nodes are removed from the toBeMade queue and job table slots
629  *	are filled.
630  *-----------------------------------------------------------------------
631  */
632 static bool
633 MakeStartJobs()
634 {
635     GNode	*gn;
636 
637     while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
638 	if (DEBUG(MAKE)) {
639 	    printf("Examining %s...", gn->name);
640 	}
641 	/*
642 	 * Make sure any and all predecessors that are going to be made,
643 	 * have been.
644 	 */
645 	if (!Lst_IsEmpty(&gn->preds)) {
646 	    LstNode ln;
647 
648 	    for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)){
649 		GNode	*pgn = (GNode *)Lst_Datum(ln);
650 
651 		if (pgn->make && pgn->made == UNMADE) {
652 		    if (DEBUG(MAKE)) {
653 			printf("predecessor %s not made yet.\n", pgn->name);
654 		    }
655 		    break;
656 		}
657 	    }
658 	    /*
659 	     * If ln isn't NULL, there's a predecessor as yet unmade, so we
660 	     * just drop this node on the floor. When the node in question
661 	     * has been made, it will notice this node as being ready to
662 	     * make but as yet unmade and will place the node on the queue.
663 	     */
664 	    if (ln != NULL) {
665 		continue;
666 	    }
667 	}
668 
669 	numNodes--;
670 	if (Make_OODate(gn)) {
671 	    if (DEBUG(MAKE)) {
672 		printf("out-of-date\n");
673 	    }
674 	    if (queryFlag) {
675 		return true;
676 	    }
677 	    Make_DoAllVar(gn);
678 	    Job_Make(gn);
679 	} else {
680 	    if (DEBUG(MAKE)) {
681 		printf("up-to-date\n");
682 	    }
683 	    gn->made = UPTODATE;
684 	    if (gn->type & OP_JOIN) {
685 		/*
686 		 * Even for an up-to-date .JOIN node, we need it to have its
687 		 * context variables so references to it get the correct
688 		 * value for .TARGET when building up the context variables
689 		 * of its parent(s)...
690 		 */
691 		Make_DoAllVar(gn);
692 	    }
693 
694 	    Make_Update(gn);
695 	}
696     }
697     return false;
698 }
699 
700 /*-
701  *-----------------------------------------------------------------------
702  * MakePrintStatus --
703  *	Print the status of a top-level node, viz. it being up-to-date
704  *	already or not created due to an error in a lower level.
705  *	Callback function for Make_Run via Lst_ForEach.
706  *
707  * Side Effects:
708  *	A message may be printed.
709  *-----------------------------------------------------------------------
710  */
711 static void
712 MakePrintStatus(gnp, cyclep)
713     void *gnp;		    /* Node to examine */
714     void *cyclep;	    /* True if gn->unmade being non-zero implies
715 			     * a cycle in the graph, not an error in an
716 			     * inferior */
717 {
718     GNode	*gn = (GNode *)gnp;
719     bool	cycle = *(bool *)cyclep;
720     if (gn->made == UPTODATE) {
721 	printf("`%s' is up to date.\n", gn->name);
722     } else if (gn->unmade != 0) {
723 	if (cycle) {
724 	    bool t = true;
725 	    /*
726 	     * If printing cycles and came to one that has unmade children,
727 	     * print out the cycle by recursing on its children. Note a
728 	     * cycle like:
729 	     *	a : b
730 	     *	b : c
731 	     *	c : b
732 	     * will cause this to erroneously complain about a being in
733 	     * the cycle, but this is a good approximation.
734 	     */
735 	    if (gn->made == CYCLE) {
736 		Error("Graph cycles through `%s'", gn->name);
737 		gn->made = ENDCYCLE;
738 		Lst_ForEach(&gn->children, MakePrintStatus, &t);
739 		gn->made = UNMADE;
740 	    } else if (gn->made != ENDCYCLE) {
741 		gn->made = CYCLE;
742 		Lst_ForEach(&gn->children, MakePrintStatus, &t);
743 	    }
744 	} else {
745 	    printf("`%s' not remade because of errors.\n", gn->name);
746 	}
747     }
748 }
749 
750 
751 /*-
752  *-----------------------------------------------------------------------
753  * Make_Run --
754  *	Initialize the nodes to remake and the list of nodes which are
755  *	ready to be made by doing a breadth-first traversal of the graph
756  *	starting from the nodes in the given list. Once this traversal
757  *	is finished, all the 'leaves' of the graph are in the toBeMade
758  *	queue.
759  *	Using this queue and the Job module, work back up the graph,
760  *	calling on MakeStartJobs to keep the job table as full as
761  *	possible.
762  *
763  * Results:
764  *	true if work was done. false otherwise.
765  *
766  * Side Effects:
767  *	The make field of all nodes involved in the creation of the given
768  *	targets is set to 1. The toBeMade list is set to contain all the
769  *	'leaves' of these subgraphs.
770  *-----------------------------------------------------------------------
771  */
772 bool
773 Make_Run(targs)
774     Lst 	    targs;	/* the initial list of targets */
775 {
776     GNode	    *gn;	/* a temporary pointer */
777     LIST	    examine;	/* List of targets to examine */
778     int 	    errors;	/* Number of errors the Job module reports */
779 
780     Static_Lst_Init(&toBeMade);
781 
782     Lst_Clone(&examine, targs, NOCOPY);
783     numNodes = 0;
784 
785     /*
786      * Make an initial downward pass over the graph, marking nodes to be made
787      * as we go down. We call Suff_FindDeps to find where a node is and
788      * to get some children for it if it has none and also has no commands.
789      * If the node is a leaf, we stick it on the toBeMade queue to
790      * be looked at in a minute, otherwise we add its children to our queue
791      * and go on about our business.
792      */
793     while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
794 	if (!gn->make) {
795 	    gn->make = true;
796 	    numNodes++;
797 
798 	    /*
799 	     * Apply any .USE rules before looking for implicit dependencies
800 	     * to make sure everything has commands that should...
801 	     */
802 	    Lst_ForEach(&gn->children, MakeHandleUse, gn);
803 	    Suff_FindDeps(gn);
804 
805 	    if (gn->unmade != 0) {
806 		Lst_ForEach(&gn->children, MakeAddChild, &examine);
807 	    } else {
808 		Lst_EnQueue(&toBeMade, gn);
809 	    }
810 	}
811     }
812 
813     if (queryFlag) {
814 	/*
815 	 * We wouldn't do any work unless we could start some jobs in the
816 	 * next loop... (we won't actually start any, of course, this is just
817 	 * to see if any of the targets was out of date)
818 	 */
819 	return MakeStartJobs();
820     } else {
821 	/*
822 	 * Initialization. At the moment, no jobs are running and until some
823 	 * get started, nothing will happen since the remaining upward
824 	 * traversal of the graph is performed by the routines in job.c upon
825 	 * the finishing of a job. So we fill the Job table as much as we can
826 	 * before going into our loop.
827 	 */
828 	(void)MakeStartJobs();
829     }
830 
831     /*
832      * Main Loop: The idea here is that the ending of jobs will take
833      * care of the maintenance of data structures and the waiting for output
834      * will cause us to be idle most of the time while our children run as
835      * much as possible. Because the job table is kept as full as possible,
836      * the only time when it will be empty is when all the jobs which need
837      * running have been run, so that is the end condition of this loop.
838      * Note that the Job module will exit if there were any errors unless the
839      * keepgoing flag was given.
840      */
841     while (!Job_Empty()) {
842 	Job_CatchOutput();
843 	Job_CatchChildren(!usePipes);
844 	(void)MakeStartJobs();
845     }
846 
847     errors = Job_Finish();
848 
849     /*
850      * Print the final status of each target. E.g. if it wasn't made
851      * because some inferior reported an error.
852      */
853     errors = errors == 0 && numNodes != 0;
854     Lst_ForEach(targs, MakePrintStatus, &errors);
855 
856     return true;
857 }
858