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