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