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