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