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