xref: /netbsd-src/usr.bin/make/make.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: make.c,v 1.78 2009/01/23 21:26:30 dsl Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1993
5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1989 by Berkeley Softworks
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Adam de Boor.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *	This product includes software developed by the University of
53  *	California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70 
71 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: make.c,v 1.78 2009/01/23 21:26:30 dsl Exp $";
73 #else
74 #include <sys/cdefs.h>
75 #ifndef lint
76 #if 0
77 static char sccsid[] = "@(#)make.c	8.1 (Berkeley) 6/6/93";
78 #else
79 __RCSID("$NetBSD: make.c,v 1.78 2009/01/23 21:26:30 dsl Exp $");
80 #endif
81 #endif /* not lint */
82 #endif
83 
84 /*-
85  * make.c --
86  *	The functions which perform the examination of targets and
87  *	their suitability for creation
88  *
89  * Interface:
90  *	Make_Run 	    	Initialize things for the module and recreate
91  *	    	  	    	whatever needs recreating. Returns TRUE if
92  *	    	    	    	work was (or would have been) done and FALSE
93  *	    	  	    	otherwise.
94  *
95  *	Make_Update	    	Update all parents of a given child. Performs
96  *	    	  	    	various bookkeeping chores like the updating
97  *	    	  	    	of the cmtime field of the parent, filling
98  *	    	  	    	of the IMPSRC context variable, etc. It will
99  *	    	  	    	place the parent on the toBeMade queue if it
100  *	    	  	    	should be.
101  *
102  *	Make_TimeStamp	    	Function to set the parent's cmtime field
103  *	    	  	    	based on a child's modification time.
104  *
105  *	Make_DoAllVar	    	Set up the various local variables for a
106  *	    	  	    	target, including the .ALLSRC variable, making
107  *	    	  	    	sure that any variable that needs to exist
108  *	    	  	    	at the very least has the empty value.
109  *
110  *	Make_OODate 	    	Determine if a target is out-of-date.
111  *
112  *	Make_HandleUse	    	See if a child is a .USE node for a parent
113  *				and perform the .USE actions if so.
114  *
115  *	Make_ExpandUse	    	Expand .USE nodes
116  */
117 
118 #include    "make.h"
119 #include    "hash.h"
120 #include    "dir.h"
121 #include    "job.h"
122 
123 static unsigned int checked = 1;/* Sequence # to detect recursion */
124 static Lst     	toBeMade;	/* The current fringe of the graph. These
125 				 * are nodes which await examination by
126 				 * MakeOODate. It is added to by
127 				 * Make_Update and subtracted from by
128 				 * MakeStartJobs */
129 
130 static int MakeAddChild(void *, void *);
131 static int MakeFindChild(void *, void *);
132 static int MakeUnmark(void *, void *);
133 static int MakeAddAllSrc(void *, void *);
134 static int MakeTimeStamp(void *, void *);
135 static int MakeHandleUse(void *, void *);
136 static Boolean MakeStartJobs(void);
137 static int MakePrintStatus(void *, void *);
138 static int MakeCheckOrder(void *, void *);
139 static int MakeBuildChild(void *, void *);
140 static int MakeBuildParent(void *, void *);
141 
142 static void
143 make_abort(GNode *gn, int line)
144 {
145     static int two = 2;
146 
147     fprintf(debug_file, "make_abort from line %d\n", line);
148     Targ_PrintNode(gn, &two);
149     Lst_ForEach(toBeMade, Targ_PrintNode, &two);
150     Targ_PrintGraph(3);
151     abort();
152 }
153 
154 /*-
155  *-----------------------------------------------------------------------
156  * Make_TimeStamp --
157  *	Set the cmtime field of a parent node based on the mtime stamp in its
158  *	child. Called from MakeOODate via Lst_ForEach.
159  *
160  * Input:
161  *	pgn		the current parent
162  *	cgn		the child we've just examined
163  *
164  * Results:
165  *	Always returns 0.
166  *
167  * Side Effects:
168  *	The cmtime of the parent node will be changed if the mtime
169  *	field of the child is greater than it.
170  *-----------------------------------------------------------------------
171  */
172 int
173 Make_TimeStamp(GNode *pgn, GNode *cgn)
174 {
175     if (cgn->mtime > pgn->cmtime) {
176 	pgn->cmtime = cgn->mtime;
177     }
178     return (0);
179 }
180 
181 /*
182  * Input:
183  *	pgn		the current parent
184  *	cgn		the child we've just examined
185  *
186  */
187 static int
188 MakeTimeStamp(void *pgn, void *cgn)
189 {
190     return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
191 }
192 
193 /*-
194  *-----------------------------------------------------------------------
195  * Make_OODate --
196  *	See if a given node is out of date with respect to its sources.
197  *	Used by Make_Run when deciding which nodes to place on the
198  *	toBeMade queue initially and by Make_Update to screen out USE and
199  *	EXEC nodes. In the latter case, however, any other sort of node
200  *	must be considered out-of-date since at least one of its children
201  *	will have been recreated.
202  *
203  * Input:
204  *	gn		the node to check
205  *
206  * Results:
207  *	TRUE if the node is out of date. FALSE otherwise.
208  *
209  * Side Effects:
210  *	The mtime field of the node and the cmtime field of its parents
211  *	will/may be changed.
212  *-----------------------------------------------------------------------
213  */
214 Boolean
215 Make_OODate(GNode *gn)
216 {
217     Boolean         oodate;
218 
219     /*
220      * Certain types of targets needn't even be sought as their datedness
221      * doesn't depend on their modification time...
222      */
223     if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
224 	(void)Dir_MTime(gn);
225 	if (DEBUG(MAKE)) {
226 	    if (gn->mtime != 0) {
227 		fprintf(debug_file, "modified %s...", Targ_FmtTime(gn->mtime));
228 	    } else {
229 		fprintf(debug_file, "non-existent...");
230 	    }
231 	}
232     }
233 
234     /*
235      * A target is remade in one of the following circumstances:
236      *	its modification time is smaller than that of its youngest child
237      *	    and it would actually be run (has commands or type OP_NOP)
238      *	it's the object of a force operator
239      *	it has no children, was on the lhs of an operator and doesn't exist
240      *	    already.
241      *
242      * Libraries are only considered out-of-date if the archive module says
243      * they are.
244      *
245      * These weird rules are brought to you by Backward-Compatibility and
246      * the strange people who wrote 'Make'.
247      */
248     if (gn->type & (OP_USE|OP_USEBEFORE)) {
249 	/*
250 	 * If the node is a USE node it is *never* out of date
251 	 * no matter *what*.
252 	 */
253 	if (DEBUG(MAKE)) {
254 	    fprintf(debug_file, ".USE node...");
255 	}
256 	oodate = FALSE;
257     } else if ((gn->type & OP_LIB) &&
258 	       ((gn->mtime==0) || Arch_IsLib(gn))) {
259 	if (DEBUG(MAKE)) {
260 	    fprintf(debug_file, "library...");
261 	}
262 
263 	/*
264 	 * always out of date if no children and :: target
265 	 * or non-existent.
266 	 */
267 	oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
268 		  (gn->cmtime == 0 && (gn->type & OP_DOUBLEDEP)));
269     } else if (gn->type & OP_JOIN) {
270 	/*
271 	 * A target with the .JOIN attribute is only considered
272 	 * out-of-date if any of its children was out-of-date.
273 	 */
274 	if (DEBUG(MAKE)) {
275 	    fprintf(debug_file, ".JOIN node...");
276 	}
277 	if (DEBUG(MAKE)) {
278 	    fprintf(debug_file, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
279 	}
280 	oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
281     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
282 	/*
283 	 * A node which is the object of the force (!) operator or which has
284 	 * the .EXEC attribute is always considered out-of-date.
285 	 */
286 	if (DEBUG(MAKE)) {
287 	    if (gn->type & OP_FORCE) {
288 		fprintf(debug_file, "! operator...");
289 	    } else if (gn->type & OP_PHONY) {
290 		fprintf(debug_file, ".PHONY node...");
291 	    } else {
292 		fprintf(debug_file, ".EXEC node...");
293 	    }
294 	}
295 	oodate = TRUE;
296     } else if (gn->mtime < gn->cmtime ||
297 	       (gn->cmtime == 0 &&
298 		((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
299 		  || gn->type & OP_DOUBLEDEP)))
300     {
301 	/*
302 	 * A node whose modification time is less than that of its
303 	 * youngest child or that has no children (cmtime == 0) and
304 	 * either doesn't exist (mtime == 0) and it isn't optional
305 	 * or was the object of a * :: operator is out-of-date.
306 	 * Why? Because that's the way Make does it.
307 	 */
308 	if (DEBUG(MAKE)) {
309 	    if (gn->mtime < gn->cmtime) {
310 		fprintf(debug_file, "modified before source...");
311 	    } else if (gn->mtime == 0) {
312 		fprintf(debug_file, "non-existent and no sources...");
313 	    } else {
314 		fprintf(debug_file, ":: operator and no sources...");
315 	    }
316 	}
317 	oodate = TRUE;
318     } else {
319 	/*
320 	 * When a non-existing child with no sources
321 	 * (such as a typically used FORCE source) has been made and
322 	 * the target of the child (usually a directory) has the same
323 	 * timestamp as the timestamp just given to the non-existing child
324 	 * after it was considered made.
325 	 */
326 	if (DEBUG(MAKE)) {
327 	    if (gn->flags & FORCE)
328 		fprintf(debug_file, "non existing child...");
329 	}
330 	oodate = (gn->flags & FORCE) ? TRUE : FALSE;
331     }
332 
333     /*
334      * If the target isn't out-of-date, the parents need to know its
335      * modification time. Note that targets that appear to be out-of-date
336      * but aren't, because they have no commands and aren't of type OP_NOP,
337      * have their mtime stay below their children's mtime to keep parents from
338      * thinking they're out-of-date.
339      */
340     if (!oodate) {
341 	Lst_ForEach(gn->parents, MakeTimeStamp, gn);
342     }
343 
344     return (oodate);
345 }
346 
347 /*-
348  *-----------------------------------------------------------------------
349  * MakeAddChild  --
350  *	Function used by Make_Run to add a child to the list l.
351  *	It will only add the child if its make field is FALSE.
352  *
353  * Input:
354  *	gnp		the node to add
355  *	lp		the list to which to add it
356  *
357  * Results:
358  *	Always returns 0
359  *
360  * Side Effects:
361  *	The given list is extended
362  *-----------------------------------------------------------------------
363  */
364 static int
365 MakeAddChild(void *gnp, void *lp)
366 {
367     GNode          *gn = (GNode *)gnp;
368     Lst            l = (Lst) lp;
369 
370     if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
371 	if (DEBUG(MAKE))
372 	    fprintf(debug_file, "MakeAddChild: need to examine %s%s\n",
373 		gn->name, gn->cohort_num);
374 	(void)Lst_EnQueue(l, gn);
375     }
376     return (0);
377 }
378 
379 /*-
380  *-----------------------------------------------------------------------
381  * MakeFindChild  --
382  *	Function used by Make_Run to find the pathname of a child
383  *	that was already made.
384  *
385  * Input:
386  *	gnp		the node to find
387  *
388  * Results:
389  *	Always returns 0
390  *
391  * Side Effects:
392  *	The path and mtime of the node and the cmtime of the parent are
393  *	updated; the unmade children count of the parent is decremented.
394  *-----------------------------------------------------------------------
395  */
396 static int
397 MakeFindChild(void *gnp, void *pgnp)
398 {
399     GNode          *gn = (GNode *)gnp;
400     GNode          *pgn = (GNode *)pgnp;
401 
402     (void)Dir_MTime(gn);
403     Make_TimeStamp(pgn, gn);
404     pgn->unmade--;
405 
406     return (0);
407 }
408 
409 /*-
410  *-----------------------------------------------------------------------
411  * Make_HandleUse --
412  *	Function called by Make_Run and SuffApplyTransform on the downward
413  *	pass to handle .USE and transformation nodes. It implements the
414  *	.USE and transformation functionality by copying the node's commands,
415  *	type flags and children to the parent node.
416  *
417  *	A .USE node is much like an explicit transformation rule, except
418  *	its commands are always added to the target node, even if the
419  *	target already has commands.
420  *
421  * Input:
422  *	cgn		The .USE node
423  *	pgn		The target of the .USE node
424  *
425  * Results:
426  *	none
427  *
428  * Side Effects:
429  *	Children and commands may be added to the parent and the parent's
430  *	type may be changed.
431  *
432  *-----------------------------------------------------------------------
433  */
434 void
435 Make_HandleUse(GNode *cgn, GNode *pgn)
436 {
437     LstNode	ln; 	/* An element in the children list */
438 
439 #ifdef DEBUG_SRC
440     if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
441 	fprintf(debug_file, "Make_HandleUse: called for plain node %s\n", cgn->name);
442 	return;
443     }
444 #endif
445 
446     if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
447 	    if (cgn->type & OP_USEBEFORE) {
448 		/*
449 		 * .USEBEFORE --
450 		 *	prepend the child's commands to the parent.
451 		 */
452 		Lst cmds = pgn->commands;
453 		pgn->commands = Lst_Duplicate(cgn->commands, NULL);
454 		(void)Lst_Concat(pgn->commands, cmds, LST_CONCNEW);
455 		Lst_Destroy(cmds, NULL);
456 	    } else {
457 		/*
458 		 * .USE or target has no commands --
459 		 *	append the child's commands to the parent.
460 		 */
461 		(void)Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
462 	    }
463     }
464 
465     if (Lst_Open(cgn->children) == SUCCESS) {
466 	while ((ln = Lst_Next(cgn->children)) != NULL) {
467 	    GNode *tgn, *gn = (GNode *)Lst_Datum(ln);
468 
469 	    /*
470 	     * Expand variables in the .USE node's name
471 	     * and save the unexpanded form.
472 	     * We don't need to do this for commands.
473 	     * They get expanded properly when we execute.
474 	     */
475 	    if (gn->uname == NULL) {
476 		gn->uname = gn->name;
477 	    } else {
478 		if (gn->name)
479 		    free(gn->name);
480 	    }
481 	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE);
482 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
483 		/* See if we have a target for this node. */
484 		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
485 		if (tgn != NULL)
486 		    gn = tgn;
487 	    }
488 
489 	    (void)Lst_AtEnd(pgn->children, gn);
490 	    (void)Lst_AtEnd(gn->parents, pgn);
491 	    pgn->unmade += 1;
492 	}
493 	Lst_Close(cgn->children);
494     }
495 
496     pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
497 }
498 
499 /*-
500  *-----------------------------------------------------------------------
501  * MakeHandleUse --
502  *	Callback function for Lst_ForEach, used by Make_Run on the downward
503  *	pass to handle .USE nodes. Should be called before the children
504  *	are enqueued to be looked at by MakeAddChild.
505  *	This function calls Make_HandleUse to copy the .USE node's commands,
506  *	type flags and children to the parent node.
507  *
508  * Input:
509  *	cgnp		the child we've just examined
510  *	pgnp		the current parent
511  *
512  * Results:
513  *	returns 0.
514  *
515  * Side Effects:
516  *	After expansion, .USE child nodes are removed from the parent
517  *
518  *-----------------------------------------------------------------------
519  */
520 static int
521 MakeHandleUse(void *cgnp, void *pgnp)
522 {
523     GNode	*cgn = (GNode *)cgnp;
524     GNode	*pgn = (GNode *)pgnp;
525     LstNode	ln; 	/* An element in the children list */
526     int		unmarked;
527 
528     unmarked = ((cgn->type & OP_MARK) == 0);
529     cgn->type |= OP_MARK;
530 
531     if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
532 	return (0);
533 
534     if (unmarked)
535 	Make_HandleUse(cgn, pgn);
536 
537     /*
538      * This child node is now "made", so we decrement the count of
539      * unmade children in the parent... We also remove the child
540      * from the parent's list to accurately reflect the number of decent
541      * children the parent has. This is used by Make_Run to decide
542      * whether to queue the parent or examine its children...
543      */
544     if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
545 	Lst_Remove(pgn->children, ln);
546 	pgn->unmade--;
547     }
548     return (0);
549 }
550 
551 
552 /*-
553  *-----------------------------------------------------------------------
554  * Make_Recheck --
555  *	Check the modification time of a gnode, and update it as described
556  *	in the comments below.
557  *
558  * Results:
559  *	returns 0 if the gnode does not exist, or it's filesystem
560  *	time if it does.
561  *
562  * Side Effects:
563  *	the gnode's modification time and path name are affected.
564  *
565  *-----------------------------------------------------------------------
566  */
567 time_t
568 Make_Recheck(GNode *gn)
569 {
570     time_t mtime = Dir_MTime(gn);
571 
572 #ifndef RECHECK
573     /*
574      * We can't re-stat the thing, but we can at least take care of rules
575      * where a target depends on a source that actually creates the
576      * target, but only if it has changed, e.g.
577      *
578      * parse.h : parse.o
579      *
580      * parse.o : parse.y
581      *  	yacc -d parse.y
582      *  	cc -c y.tab.c
583      *  	mv y.tab.o parse.o
584      *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
585      *
586      * In this case, if the definitions produced by yacc haven't changed
587      * from before, parse.h won't have been updated and gn->mtime will
588      * reflect the current modification time for parse.h. This is
589      * something of a kludge, I admit, but it's a useful one..
590      * XXX: People like to use a rule like
591      *
592      * FRC:
593      *
594      * To force things that depend on FRC to be made, so we have to
595      * check for gn->children being empty as well...
596      */
597     if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
598 	gn->mtime = now;
599     }
600 #else
601     /*
602      * This is what Make does and it's actually a good thing, as it
603      * allows rules like
604      *
605      *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
606      *
607      * to function as intended. Unfortunately, thanks to the stateless
608      * nature of NFS (by which I mean the loose coupling of two clients
609      * using the same file from a common server), there are times
610      * when the modification time of a file created on a remote
611      * machine will not be modified before the local stat() implied by
612      * the Dir_MTime occurs, thus leading us to believe that the file
613      * is unchanged, wreaking havoc with files that depend on this one.
614      *
615      * I have decided it is better to make too much than to make too
616      * little, so this stuff is commented out unless you're sure it's ok.
617      * -- ardeb 1/12/88
618      */
619     /*
620      * Christos, 4/9/92: If we are  saving commands pretend that
621      * the target is made now. Otherwise archives with ... rules
622      * don't work!
623      */
624     if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
625 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
626 	if (DEBUG(MAKE)) {
627 	    fprintf(debug_file, " recheck(%s): update time from %s to now\n",
628 		   gn->name, Targ_FmtTime(gn->mtime));
629 	}
630 	gn->mtime = now;
631     }
632     else {
633 	if (DEBUG(MAKE)) {
634 	    fprintf(debug_file, " recheck(%s): current update time: %s\n",
635 		   gn->name, Targ_FmtTime(gn->mtime));
636 	}
637     }
638 #endif
639     return mtime;
640 }
641 
642 /*-
643  *-----------------------------------------------------------------------
644  * Make_Update  --
645  *	Perform update on the parents of a node. Used by JobFinish once
646  *	a node has been dealt with and by MakeStartJobs if it finds an
647  *	up-to-date node.
648  *
649  * Input:
650  *	cgn		the child node
651  *
652  * Results:
653  *	Always returns 0
654  *
655  * Side Effects:
656  *	The unmade field of pgn is decremented and pgn may be placed on
657  *	the toBeMade queue if this field becomes 0.
658  *
659  * 	If the child was made, the parent's flag CHILDMADE field will be
660  *	set true and its cmtime set to now.
661  *
662  *	If the child is not up-to-date and still does not exist,
663  *	set the FORCE flag on the parents.
664  *
665  *	If the child wasn't made, the cmtime field of the parent will be
666  *	altered if the child's mtime is big enough.
667  *
668  *	Finally, if the child is the implied source for the parent, the
669  *	parent's IMPSRC variable is set appropriately.
670  *
671  *-----------------------------------------------------------------------
672  */
673 void
674 Make_Update(GNode *cgn)
675 {
676     GNode 	*pgn;	/* the parent node */
677     char  	*cname;	/* the child's name */
678     LstNode	ln; 	/* Element in parents and iParents lists */
679     time_t	mtime = -1;
680     char	*p1;
681     Lst		parents;
682     GNode	*centurion;
683 
684     /* It is save to re-examine any nodes again */
685     checked++;
686 
687     cname = Var_Value(TARGET, cgn, &p1);
688     if (p1)
689 	free(p1);
690 
691     if (DEBUG(MAKE))
692 	fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
693 
694     /*
695      * If the child was actually made, see what its modification time is
696      * now -- some rules won't actually update the file. If the file still
697      * doesn't exist, make its mtime now.
698      */
699     if (cgn->made != UPTODATE) {
700 	mtime = Make_Recheck(cgn);
701     }
702 
703     /*
704      * If this is a `::' node, we must consult its first instance
705      * which is where all parents are linked.
706      */
707     if ((centurion = cgn->centurion) != NULL) {
708 	if (!Lst_IsEmpty(cgn->parents))
709 		Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
710 	centurion->unmade_cohorts -= 1;
711 	if (centurion->unmade_cohorts < 0)
712 	    Error("Graph cycles through centurion %s", centurion->name);
713     } else {
714 	centurion = cgn;
715     }
716     parents = centurion->parents;
717 
718     /* If this was a .ORDER node, schedule the RHS */
719     Lst_ForEach(centurion->order_succ, MakeBuildParent, Lst_First(toBeMade));
720 
721     /* Now mark all the parents as having one less unmade child */
722     if (Lst_Open(parents) == SUCCESS) {
723 	while ((ln = Lst_Next(parents)) != NULL) {
724 	    pgn = (GNode *)Lst_Datum(ln);
725 	    if (DEBUG(MAKE))
726 		fprintf(debug_file, "inspect parent %s%s: flags %x, "
727 			    "type %x, made %d, unmade %d ",
728 			pgn->name, pgn->cohort_num, pgn->flags,
729 			pgn->type, pgn->made, pgn->unmade-1);
730 
731 	    if (!(pgn->flags & REMAKE)) {
732 		/* This parent isn't needed */
733 		if (DEBUG(MAKE))
734 		    fprintf(debug_file, "- not needed\n");
735 		continue;
736 	    }
737 	    if (mtime == 0 && !(cgn->type & OP_WAIT))
738 		pgn->flags |= FORCE;
739 
740 	    /*
741 	     * If the parent has the .MADE attribute, its timestamp got
742 	     * updated to that of its newest child, and its unmake
743 	     * child count got set to zero in Make_ExpandUse().
744 	     * However other things might cause us to build one of its
745 	     * children - and so we mustn't do any processing here when
746 	     * the child build finishes.
747 	     */
748 	    if (pgn->type & OP_MADE) {
749 		if (DEBUG(MAKE))
750 		    fprintf(debug_file, "- .MADE\n");
751 		continue;
752 	    }
753 
754 	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
755 		if (cgn->made == MADE)
756 		    pgn->flags |= CHILDMADE;
757 		(void)Make_TimeStamp(pgn, cgn);
758 	    }
759 
760 	    /*
761 	     * A parent must wait for the completion of all instances
762 	     * of a `::' dependency.
763 	     */
764 	    if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
765 		if (DEBUG(MAKE))
766 		    fprintf(debug_file,
767 			    "- centurion made %d, %d unmade cohorts\n",
768 			    centurion->made, centurion->unmade_cohorts);
769 		continue;
770 	    }
771 
772 	    /* One more child of this parent is now made */
773 	    pgn->unmade -= 1;
774 	    if (pgn->unmade < 0) {
775 		if (DEBUG(MAKE)) {
776 		    fprintf(debug_file, "Graph cycles through %s%s\n",
777 			pgn->name, pgn->cohort_num);
778 		    Targ_PrintGraph(2);
779 		}
780 		Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
781 	    }
782 
783 	    /* We must always rescan the parents of .WAIT and .ORDER nodes. */
784 	    if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
785 		    && !(centurion->flags & DONE_ORDER)) {
786 		if (DEBUG(MAKE))
787 		    fprintf(debug_file, "- unmade children\n");
788 		continue;
789 	    }
790 	    if (pgn->made != DEFERRED) {
791 		/*
792 		 * Either this parent is on a different branch of the tree,
793 		 * or it on the RHS of a .WAIT directive
794 		 * or it is already on the toBeMade list.
795 		 */
796 		if (DEBUG(MAKE))
797 		    fprintf(debug_file, "- not deferred\n");
798 		continue;
799 	    }
800 	    if (pgn->order_pred
801 		    && Lst_ForEach(pgn->order_pred, MakeCheckOrder, 0)) {
802 		/* A .ORDER rule stops us building this */
803 		continue;
804 	    }
805 	    if (DEBUG(MAKE)) {
806 		static int two = 2;
807 		fprintf(debug_file, "- %s%s made, schedule %s%s (made %d)\n",
808 			cgn->name, cgn->cohort_num,
809 			pgn->name, pgn->cohort_num, pgn->made);
810 		Targ_PrintNode(pgn, &two);
811 	    }
812 	    /* Ok, we can schedule the parent again */
813 	    pgn->made = REQUESTED;
814 	    (void)Lst_EnQueue(toBeMade, pgn);
815 	}
816 	Lst_Close(parents);
817     }
818 
819     /*
820      * Set the .PREFIX and .IMPSRC variables for all the implied parents
821      * of this node.
822      */
823     if (Lst_Open(cgn->iParents) == SUCCESS) {
824 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
825 
826 	while ((ln = Lst_Next(cgn->iParents)) != NULL) {
827 	    pgn = (GNode *)Lst_Datum(ln);
828 	    if (pgn->flags & REMAKE) {
829 		Var_Set(IMPSRC, cname, pgn, 0);
830 		if (cpref != NULL)
831 		    Var_Set(PREFIX, cpref, pgn, 0);
832 	    }
833 	}
834 	if (p1)
835 	    free(p1);
836 	Lst_Close(cgn->iParents);
837     }
838 }
839 
840 /*-
841  *-----------------------------------------------------------------------
842  * MakeAddAllSrc --
843  *	Add a child's name to the ALLSRC and OODATE variables of the given
844  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
845  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
846  *	.EXEC and .USE children are very rarely going to be files, so...
847  *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
848  *
849  *	A child is added to the OODATE variable if its modification time is
850  *	later than that of its parent, as defined by Make, except if the
851  *	parent is a .JOIN node. In that case, it is only added to the OODATE
852  *	variable if it was actually made (since .JOIN nodes don't have
853  *	modification times, the comparison is rather unfair...)..
854  *
855  * Results:
856  *	Always returns 0
857  *
858  * Side Effects:
859  *	The ALLSRC variable for the given node is extended.
860  *-----------------------------------------------------------------------
861  */
862 static int
863 MakeUnmark(void *cgnp, void *pgnp __unused)
864 {
865     GNode	*cgn = (GNode *)cgnp;
866 
867     cgn->type &= ~OP_MARK;
868     return (0);
869 }
870 
871 /*
872  * Input:
873  *	cgnp		The child to add
874  *	pgnp		The parent to whose ALLSRC variable it should
875  *			be added
876  *
877  */
878 static int
879 MakeAddAllSrc(void *cgnp, void *pgnp)
880 {
881     GNode	*cgn = (GNode *)cgnp;
882     GNode	*pgn = (GNode *)pgnp;
883 
884     if (cgn->type & OP_MARK)
885 	return (0);
886     cgn->type |= OP_MARK;
887 
888     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
889 	char *child, *allsrc;
890 	char *p1 = NULL, *p2 = NULL;
891 
892 	if (cgn->type & OP_ARCHV)
893 	    child = Var_Value(MEMBER, cgn, &p1);
894 	else
895 	    child = cgn->path ? cgn->path : cgn->name;
896 	if (cgn->type & OP_JOIN) {
897 	    allsrc = Var_Value(ALLSRC, cgn, &p2);
898 	} else {
899 	    allsrc = child;
900 	}
901 	if (allsrc != NULL)
902 		Var_Append(ALLSRC, allsrc, pgn);
903 	if (p2)
904 	    free(p2);
905 	if (pgn->type & OP_JOIN) {
906 	    if (cgn->made == MADE) {
907 		Var_Append(OODATE, child, pgn);
908 	    }
909 	} else if ((pgn->mtime < cgn->mtime) ||
910 		   (cgn->mtime >= now && cgn->made == MADE))
911 	{
912 	    /*
913 	     * It goes in the OODATE variable if the parent is younger than the
914 	     * child or if the child has been modified more recently than
915 	     * the start of the make. This is to keep pmake from getting
916 	     * confused if something else updates the parent after the
917 	     * make starts (shouldn't happen, I know, but sometimes it
918 	     * does). In such a case, if we've updated the kid, the parent
919 	     * is likely to have a modification time later than that of
920 	     * the kid and anything that relies on the OODATE variable will
921 	     * be hosed.
922 	     *
923 	     * XXX: This will cause all made children to go in the OODATE
924 	     * variable, even if they're not touched, if RECHECK isn't defined,
925 	     * since cgn->mtime is set to now in Make_Update. According to
926 	     * some people, this is good...
927 	     */
928 	    Var_Append(OODATE, child, pgn);
929 	}
930 	if (p1)
931 	    free(p1);
932     }
933     return (0);
934 }
935 
936 /*-
937  *-----------------------------------------------------------------------
938  * Make_DoAllVar --
939  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
940  *	done separately, rather than while traversing the graph. This is
941  *	because Make defined OODATE to contain all sources whose modification
942  *	times were later than that of the target, *not* those sources that
943  *	were out-of-date. Since in both compatibility and native modes,
944  *	the modification time of the parent isn't found until the child
945  *	has been dealt with, we have to wait until now to fill in the
946  *	variable. As for ALLSRC, the ordering is important and not
947  *	guaranteed when in native mode, so it must be set here, too.
948  *
949  * Results:
950  *	None
951  *
952  * Side Effects:
953  *	The ALLSRC and OODATE variables of the given node is filled in.
954  *	If the node is a .JOIN node, its TARGET variable will be set to
955  * 	match its ALLSRC variable.
956  *-----------------------------------------------------------------------
957  */
958 void
959 Make_DoAllVar(GNode *gn)
960 {
961     Lst_ForEach(gn->children, MakeUnmark, gn);
962     Lst_ForEach(gn->children, MakeAddAllSrc, gn);
963 
964     if (!Var_Exists (OODATE, gn)) {
965 	Var_Set(OODATE, "", gn, 0);
966     }
967     if (!Var_Exists (ALLSRC, gn)) {
968 	Var_Set(ALLSRC, "", gn, 0);
969     }
970 
971     if (gn->type & OP_JOIN) {
972 	char *p1;
973 	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
974 	if (p1)
975 	    free(p1);
976     }
977 }
978 
979 /*-
980  *-----------------------------------------------------------------------
981  * MakeStartJobs --
982  *	Start as many jobs as possible.
983  *
984  * Results:
985  *	If the query flag was given to pmake, no job will be started,
986  *	but as soon as an out-of-date target is found, this function
987  *	returns TRUE. At all other times, this function returns FALSE.
988  *
989  * Side Effects:
990  *	Nodes are removed from the toBeMade queue and job table slots
991  *	are filled.
992  *
993  *-----------------------------------------------------------------------
994  */
995 
996 static int
997 MakeCheckOrder(void *v_bn, void *ignore __unused)
998 {
999     GNode *bn = v_bn;
1000 
1001     if (bn->made >= MADE || !(bn->flags & REMAKE))
1002 	return 0;
1003     if (DEBUG(MAKE))
1004 	fprintf(debug_file, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
1005 		bn->name, bn->cohort_num);
1006     return 1;
1007 }
1008 
1009 static int
1010 MakeBuildChild(void *v_cn, void *toBeMade_next)
1011 {
1012     GNode *cn = v_cn;
1013 
1014     if (DEBUG(MAKE))
1015 	fprintf(debug_file, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
1016 	    cn->name, cn->cohort_num, cn->made, cn->type);
1017     if (cn->made > DEFERRED)
1018 	return 0;
1019 
1020     /* If this node is on the RHS of a .ORDER, check LHSs. */
1021     if (cn->order_pred && Lst_ForEach(cn->order_pred, MakeCheckOrder, 0)) {
1022 	/* Can't build this (or anything else in this child list) yet */
1023 	cn->made = DEFERRED;
1024 	return 1;
1025     }
1026 
1027     if (DEBUG(MAKE))
1028 	fprintf(debug_file, "MakeBuildChild: schedule %s%s\n",
1029 		cn->name, cn->cohort_num);
1030 
1031     cn->made = REQUESTED;
1032     if (toBeMade_next == NULL)
1033 	Lst_AtEnd(toBeMade, cn);
1034     else
1035 	Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1036 
1037     if (cn->unmade_cohorts != 0)
1038 	Lst_ForEach(cn->cohorts, MakeBuildChild, toBeMade_next);
1039 
1040     /*
1041      * If this node is a .WAIT node with unmade chlidren
1042      * then don't add the next sibling.
1043      */
1044     return cn->type & OP_WAIT && cn->unmade > 0;
1045 }
1046 
1047 /* When a .ORDER RHS node completes we do this on each LHS */
1048 static int
1049 MakeBuildParent(void *v_pn, void *toBeMade_next)
1050 {
1051     GNode *pn = v_pn;
1052 
1053     if (pn->made != DEFERRED)
1054 	return 0;
1055 
1056     if (MakeBuildChild(pn, toBeMade_next) == 0) {
1057 	/* Mark so that when this node is built we reschedule its parents */
1058 	pn->flags |= DONE_ORDER;
1059     }
1060 
1061     return 0;
1062 }
1063 
1064 static Boolean
1065 MakeStartJobs(void)
1066 {
1067     GNode	*gn;
1068     int		have_token = 0;
1069 
1070     while (!Lst_IsEmpty (toBeMade)) {
1071 	/* Get token now to avoid cycling job-list when we only have 1 token */
1072 	if (!have_token && !Job_TokenWithdraw())
1073 	    break;
1074 	have_token = 1;
1075 
1076 	gn = (GNode *)Lst_DeQueue(toBeMade);
1077 	if (DEBUG(MAKE))
1078 	    fprintf(debug_file, "Examining %s%s...\n",
1079 		    gn->name, gn->cohort_num);
1080 
1081 	if (gn->made != REQUESTED) {
1082 	    if (DEBUG(MAKE))
1083 		fprintf(debug_file, "state %d\n", gn->made);
1084 
1085 	    make_abort(gn, __LINE__);
1086 	}
1087 
1088 	if (gn->checked == checked) {
1089 	    /* We've already looked at this node since a job finished... */
1090 	    if (DEBUG(MAKE))
1091 		fprintf(debug_file, "already checked %s%s\n",
1092 			gn->name, gn->cohort_num);
1093 	    gn->made = DEFERRED;
1094 	    continue;
1095 	}
1096 	gn->checked = checked;
1097 
1098 	if (gn->unmade != 0) {
1099 	    /*
1100 	     * We can't build this yet, add all unmade children to toBeMade,
1101 	     * just before the current first element.
1102 	     */
1103 	    gn->made = DEFERRED;
1104 	    Lst_ForEach(gn->children, MakeBuildChild, Lst_First(toBeMade));
1105 	    /* and drop this node on the floor */
1106 	    if (DEBUG(MAKE))
1107 		fprintf(debug_file, "dropped %s%s\n", gn->name, gn->cohort_num);
1108 	    continue;
1109 	}
1110 
1111 	gn->made = BEINGMADE;
1112 	if (Make_OODate(gn)) {
1113 	    if (DEBUG(MAKE)) {
1114 		fprintf(debug_file, "out-of-date\n");
1115 	    }
1116 	    if (queryFlag) {
1117 		return (TRUE);
1118 	    }
1119 	    Make_DoAllVar(gn);
1120 	    Job_Make(gn);
1121 	    have_token = 0;
1122 	} else {
1123 	    if (DEBUG(MAKE)) {
1124 		fprintf(debug_file, "up-to-date\n");
1125 	    }
1126 	    gn->made = UPTODATE;
1127 	    if (gn->type & OP_JOIN) {
1128 		/*
1129 		 * Even for an up-to-date .JOIN node, we need it to have its
1130 		 * context variables so references to it get the correct
1131 		 * value for .TARGET when building up the context variables
1132 		 * of its parent(s)...
1133 		 */
1134 		Make_DoAllVar(gn);
1135 	    }
1136 	    Make_Update(gn);
1137 	}
1138     }
1139 
1140     if (have_token)
1141 	Job_TokenReturn();
1142 
1143     return (FALSE);
1144 }
1145 
1146 /*-
1147  *-----------------------------------------------------------------------
1148  * MakePrintStatus --
1149  *	Print the status of a top-level node, viz. it being up-to-date
1150  *	already or not created due to an error in a lower level.
1151  *	Callback function for Make_Run via Lst_ForEach.
1152  *
1153  * Input:
1154  *	gnp		Node to examine
1155  *	cyclep		True if gn->unmade being non-zero implies a
1156  *			cycle in the graph, not an error in an
1157  *			inferior.
1158  *
1159  * Results:
1160  *	Always returns 0.
1161  *
1162  * Side Effects:
1163  *	A message may be printed.
1164  *
1165  *-----------------------------------------------------------------------
1166  */
1167 static int
1168 MakePrintStatusOrder(void *ognp, void *gnp)
1169 {
1170     GNode *ogn = ognp;
1171     GNode *gn = gnp;
1172 
1173     if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
1174 	/* not waiting for this one */
1175 	return 0;
1176 
1177     printf("    `%s%s' has .ORDER dependency against %s%s "
1178 		"(made %d, flags %x, type %x)\n",
1179 	    gn->name, gn->cohort_num,
1180 	    ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1181     if (DEBUG(MAKE) && debug_file != stdout)
1182 	fprintf(debug_file, "    `%s%s' has .ORDER dependency against %s%s "
1183 		    "(made %d, flags %x, type %x)\n",
1184 		gn->name, gn->cohort_num,
1185 		ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1186     return 0;
1187 }
1188 
1189 static int
1190 MakePrintStatus(void *gnp, void *v_errors)
1191 {
1192     GNode   	*gn = (GNode *)gnp;
1193     int 	*errors = v_errors;
1194 
1195     if (gn->flags & DONECYCLE)
1196 	/* We've completely processed this node before, don't do it again. */
1197 	return 0;
1198 
1199     if (gn->unmade == 0) {
1200 	gn->flags |= DONECYCLE;
1201 	switch (gn->made) {
1202 	case UPTODATE:
1203 	    printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1204 	    break;
1205 	case MADE:
1206 	    break;
1207 	case UNMADE:
1208 	case DEFERRED:
1209 	case REQUESTED:
1210 	case BEINGMADE:
1211 	    (*errors)++;
1212 	    printf("`%s%s' was not built (made %d, flags %x, type %x)!\n",
1213 		    gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1214 	    if (DEBUG(MAKE) && debug_file != stdout)
1215 		fprintf(debug_file,
1216 			"`%s%s' was not built (made %d, flags %x, type %x)!\n",
1217 			gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1218 	    /* Most likely problem is actually caused by .ORDER */
1219 	    Lst_ForEach(gn->order_pred, MakePrintStatusOrder, gn);
1220 	    break;
1221 	default:
1222 	    /* Errors - already counted */
1223 	    printf("`%s%s' not remade because of errors.\n",
1224 		    gn->name, gn->cohort_num);
1225 	    if (DEBUG(MAKE) && debug_file != stdout)
1226 		fprintf(debug_file, "`%s%s' not remade because of errors.\n",
1227 			gn->name, gn->cohort_num);
1228 	    break;
1229 	}
1230 	return 0;
1231     }
1232 
1233     if (DEBUG(MAKE))
1234 	fprintf(debug_file, "MakePrintStatus: %s%s has %d unmade children\n",
1235 		gn->name, gn->cohort_num, gn->unmade);
1236     /*
1237      * If printing cycles and came to one that has unmade children,
1238      * print out the cycle by recursing on its children.
1239      */
1240     if (!(gn->flags & CYCLE)) {
1241 	/* Fist time we've seen this node, check all children */
1242 	gn->flags |= CYCLE;
1243 	Lst_ForEach(gn->children, MakePrintStatus, errors);
1244 	/* Mark that this node needn't be processed again */
1245 	gn->flags |= DONECYCLE;
1246 	return 0;
1247     }
1248 
1249     /* Only output the error once per node */
1250     gn->flags |= DONECYCLE;
1251     Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1252     if ((*errors)++ > 100)
1253 	/* Abandon the whole error report */
1254 	return 1;
1255 
1256     /* Reporting for our children will give the rest of the loop */
1257     Lst_ForEach(gn->children, MakePrintStatus, errors);
1258     return 0;
1259 }
1260 
1261 
1262 /*-
1263  *-----------------------------------------------------------------------
1264  * Make_ExpandUse --
1265  *	Expand .USE nodes and create a new targets list
1266  *
1267  * Input:
1268  *	targs		the initial list of targets
1269  *
1270  * Side Effects:
1271  *-----------------------------------------------------------------------
1272  */
1273 void
1274 Make_ExpandUse(Lst targs)
1275 {
1276     GNode  *gn;		/* a temporary pointer */
1277     Lst    examine; 	/* List of targets to examine */
1278 
1279     examine = Lst_Duplicate(targs, NULL);
1280 
1281     /*
1282      * Make an initial downward pass over the graph, marking nodes to be made
1283      * as we go down. We call Suff_FindDeps to find where a node is and
1284      * to get some children for it if it has none and also has no commands.
1285      * If the node is a leaf, we stick it on the toBeMade queue to
1286      * be looked at in a minute, otherwise we add its children to our queue
1287      * and go on about our business.
1288      */
1289     while (!Lst_IsEmpty (examine)) {
1290 	gn = (GNode *)Lst_DeQueue(examine);
1291 
1292 	if (gn->flags & REMAKE)
1293 	    /* We've looked at this one already */
1294 	    continue;
1295 	gn->flags |= REMAKE;
1296 	if (DEBUG(MAKE))
1297 	    fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
1298 		    gn->name, gn->cohort_num);
1299 
1300 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
1301 	    /* Append all the 'cohorts' to the list of things to examine */
1302 	    Lst new;
1303 	    new = Lst_Duplicate(gn->cohorts, NULL);
1304 	    Lst_Concat(new, examine, LST_CONCLINK);
1305 	    examine = new;
1306 	}
1307 
1308 	/*
1309 	 * Apply any .USE rules before looking for implicit dependencies
1310 	 * to make sure everything has commands that should...
1311 	 * Make sure that the TARGET is set, so that we can make
1312 	 * expansions.
1313 	 */
1314 	if (gn->type & OP_ARCHV) {
1315 	    char *eoa, *eon;
1316 	    eoa = strchr(gn->name, '(');
1317 	    eon = strchr(gn->name, ')');
1318 	    if (eoa == NULL || eon == NULL)
1319 		continue;
1320 	    *eoa = '\0';
1321 	    *eon = '\0';
1322 	    Var_Set(MEMBER, eoa + 1, gn, 0);
1323 	    Var_Set(ARCHIVE, gn->name, gn, 0);
1324 	    *eoa = '(';
1325 	    *eon = ')';
1326 	}
1327 
1328 	(void)Dir_MTime(gn);
1329 	Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
1330 	Lst_ForEach(gn->children, MakeUnmark, gn);
1331 	Lst_ForEach(gn->children, MakeHandleUse, gn);
1332 
1333 	if ((gn->type & OP_MADE) == 0)
1334 	    Suff_FindDeps(gn);
1335 	else {
1336 	    /* Pretend we made all this node's children */
1337 	    Lst_ForEach(gn->children, MakeFindChild, gn);
1338 	    if (gn->unmade != 0)
1339 		    printf("Warning: %s%s still has %d unmade children\n",
1340 			    gn->name, gn->cohort_num, gn->unmade);
1341 	}
1342 
1343 	if (gn->unmade != 0)
1344 	    Lst_ForEach(gn->children, MakeAddChild, examine);
1345     }
1346 
1347     Lst_Destroy(examine, NULL);
1348 }
1349 
1350 /*-
1351  *-----------------------------------------------------------------------
1352  * Make_ProcessWait --
1353  *	Convert .WAIT nodes into dependencies
1354  *
1355  * Input:
1356  *	targs		the initial list of targets
1357  *
1358  *-----------------------------------------------------------------------
1359  */
1360 
1361 static int
1362 link_parent(void *cnp, void *pnp)
1363 {
1364     GNode *cn = cnp;
1365     GNode *pn = pnp;
1366 
1367     Lst_AtEnd(pn->children, cn);
1368     Lst_AtEnd(cn->parents, pn);
1369     pn->unmade++;
1370     return 0;
1371 }
1372 
1373 static int
1374 add_wait_dep(void *v_cn, void *v_wn)
1375 {
1376     GNode *cn = v_cn;
1377     GNode *wn = v_wn;
1378 
1379     if (cn == wn)
1380 	return 1;
1381 
1382     if (cn == NULL || wn == NULL) {
1383 	printf("bad wait dep %p %p\n", cn, wn);
1384 	exit(4);
1385     }
1386     if (DEBUG(MAKE))
1387 	 fprintf(debug_file, ".WAIT: add dependency %s%s -> %s\n",
1388 		cn->name, cn->cohort_num, wn->name);
1389 
1390     Lst_AtEnd(wn->children, cn);
1391     wn->unmade++;
1392     Lst_AtEnd(cn->parents, wn);
1393     return 0;
1394 }
1395 
1396 static void
1397 Make_ProcessWait(Lst targs)
1398 {
1399     GNode  *pgn;	/* 'parent' node we are examining */
1400     GNode  *cgn;	/* Each child in turn */
1401     LstNode owln;	/* Previous .WAIT node */
1402     Lst    examine; 	/* List of targets to examine */
1403     LstNode ln;
1404 
1405     /*
1406      * We need all the nodes to have a common parent in order for the
1407      * .WAIT and .ORDER scheduling to work.
1408      * Perhaps this should be done earlier...
1409      */
1410 
1411     pgn = Targ_NewGN(".MAIN");
1412     pgn->flags = REMAKE;
1413     pgn->type = OP_PHONY | OP_DEPENDS;
1414     /* Get it displayed in the diag dumps */
1415     Lst_AtFront(Targ_List(), pgn);
1416 
1417     Lst_ForEach(targs, link_parent, pgn);
1418 
1419     /* Start building with the 'dummy' .MAIN' node */
1420     MakeBuildChild(pgn, NULL);
1421 
1422     examine = Lst_Init(FALSE);
1423     Lst_AtEnd(examine, pgn);
1424 
1425     while (!Lst_IsEmpty (examine)) {
1426 	pgn = Lst_DeQueue(examine);
1427 
1428 	/* We only want to process each child-list once */
1429 	if (pgn->flags & DONE_WAIT)
1430 	    continue;
1431 	pgn->flags |= DONE_WAIT;
1432 	if (DEBUG(MAKE))
1433 	    fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
1434 
1435 	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) {
1436 	    /* Append all the 'cohorts' to the list of things to examine */
1437 	    Lst new;
1438 	    new = Lst_Duplicate(pgn->cohorts, NULL);
1439 	    Lst_Concat(new, examine, LST_CONCLINK);
1440 	    examine = new;
1441 	}
1442 
1443 	owln = Lst_First(pgn->children);
1444 	Lst_Open(pgn->children);
1445 	for (; (ln = Lst_Next(pgn->children)) != NULL; ) {
1446 	    cgn = Lst_Datum(ln);
1447 	    if (cgn->type & OP_WAIT) {
1448 		/* Make the .WAIT node depend on the previous children */
1449 		Lst_ForEachFrom(pgn->children, owln, add_wait_dep, cgn);
1450 		owln = ln;
1451 	    } else {
1452 		Lst_AtEnd(examine, cgn);
1453 	    }
1454 	}
1455 	Lst_Close(pgn->children);
1456     }
1457 
1458     Lst_Destroy(examine, NULL);
1459 }
1460 
1461 /*-
1462  *-----------------------------------------------------------------------
1463  * Make_Run --
1464  *	Initialize the nodes to remake and the list of nodes which are
1465  *	ready to be made by doing a breadth-first traversal of the graph
1466  *	starting from the nodes in the given list. Once this traversal
1467  *	is finished, all the 'leaves' of the graph are in the toBeMade
1468  *	queue.
1469  *	Using this queue and the Job module, work back up the graph,
1470  *	calling on MakeStartJobs to keep the job table as full as
1471  *	possible.
1472  *
1473  * Input:
1474  *	targs		the initial list of targets
1475  *
1476  * Results:
1477  *	TRUE if work was done. FALSE otherwise.
1478  *
1479  * Side Effects:
1480  *	The make field of all nodes involved in the creation of the given
1481  *	targets is set to 1. The toBeMade list is set to contain all the
1482  *	'leaves' of these subgraphs.
1483  *-----------------------------------------------------------------------
1484  */
1485 Boolean
1486 Make_Run(Lst targs)
1487 {
1488     int	    	    errors; 	/* Number of errors the Job module reports */
1489 
1490     /* Start trying to make the current targets... */
1491     toBeMade = Lst_Init(FALSE);
1492 
1493     Make_ExpandUse(targs);
1494     Make_ProcessWait(targs);
1495 
1496     if (DEBUG(MAKE)) {
1497 	 fprintf(debug_file, "#***# full graph\n");
1498 	 Targ_PrintGraph(1);
1499     }
1500 
1501     if (queryFlag) {
1502 	/*
1503 	 * We wouldn't do any work unless we could start some jobs in the
1504 	 * next loop... (we won't actually start any, of course, this is just
1505 	 * to see if any of the targets was out of date)
1506 	 */
1507 	return (MakeStartJobs());
1508     }
1509     /*
1510      * Initialization. At the moment, no jobs are running and until some
1511      * get started, nothing will happen since the remaining upward
1512      * traversal of the graph is performed by the routines in job.c upon
1513      * the finishing of a job. So we fill the Job table as much as we can
1514      * before going into our loop.
1515      */
1516     (void)MakeStartJobs();
1517 
1518     /*
1519      * Main Loop: The idea here is that the ending of jobs will take
1520      * care of the maintenance of data structures and the waiting for output
1521      * will cause us to be idle most of the time while our children run as
1522      * much as possible. Because the job table is kept as full as possible,
1523      * the only time when it will be empty is when all the jobs which need
1524      * running have been run, so that is the end condition of this loop.
1525      * Note that the Job module will exit if there were any errors unless the
1526      * keepgoing flag was given.
1527      */
1528     while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1529 	Job_CatchOutput();
1530 	(void)MakeStartJobs();
1531     }
1532 
1533     errors = Job_Finish();
1534 
1535     /*
1536      * Print the final status of each target. E.g. if it wasn't made
1537      * because some inferior reported an error.
1538      */
1539     if (DEBUG(MAKE))
1540 	 fprintf(debug_file, "done: errors %d\n", errors);
1541     if (errors == 0) {
1542 	Lst_ForEach(targs, MakePrintStatus, &errors);
1543 	if (DEBUG(MAKE)) {
1544 	    fprintf(debug_file, "done: errors %d\n", errors);
1545 	    if (errors)
1546 		Targ_PrintGraph(4);
1547 	}
1548     }
1549     return errors != 0;
1550 }
1551