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