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