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