1 /* $NetBSD: make.c,v 1.77 2008/12/13 15:19:29 dsl Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
72 static char rcsid
[] = "$NetBSD: make.c,v 1.77 2008/12/13 15:19:29 dsl Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid
[] = "@(#)make.c 8.1 (Berkeley) 6/6/93";
79 __RCSID("$NetBSD: make.c,v 1.77 2008/12/13 15:19:29 dsl Exp $");
86 * The functions which perform the examination of targets and
87 * their suitability for creation
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
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
102 * Make_TimeStamp Function to set the parent's cmtime field
103 * based on a child's modification time.
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.
110 * Make_OODate Determine if a target is out-of-date.
112 * Make_HandleUse See if a child is a .USE node for a parent
113 * and perform the .USE actions if so.
115 * Make_ExpandUse Expand .USE nodes
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
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 *);
143 make_abort(GNode
*gn
, int line
)
147 fprintf(debug_file
, "make_abort from line %d\n", line
);
148 Targ_PrintNode(gn
, &two
);
149 Lst_ForEach(toBeMade
, Targ_PrintNode
, &two
);
155 *-----------------------------------------------------------------------
157 * Set the cmtime field of a parent node based on the mtime stamp in its
158 * child. Called from MakeOODate via Lst_ForEach.
161 * pgn the current parent
162 * cgn the child we've just examined
168 * The cmtime of the parent node will be changed if the mtime
169 * field of the child is greater than it.
170 *-----------------------------------------------------------------------
173 Make_TimeStamp(GNode
*pgn
, GNode
*cgn
)
175 if (cgn
->mtime
> pgn
->cmtime
) {
176 pgn
->cmtime
= cgn
->mtime
;
183 * pgn the current parent
184 * cgn the child we've just examined
188 MakeTimeStamp(void *pgn
, void *cgn
)
190 return Make_TimeStamp((GNode
*)pgn
, (GNode
*)cgn
);
194 *-----------------------------------------------------------------------
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.
204 * gn the node to check
207 * TRUE if the node is out of date. FALSE otherwise.
210 * The mtime field of the node and the cmtime field of its parents
211 * will/may be changed.
212 *-----------------------------------------------------------------------
215 Make_OODate(GNode
*gn
)
220 * Certain types of targets needn't even be sought as their datedness
221 * doesn't depend on their modification time...
223 if ((gn
->type
& (OP_JOIN
|OP_USE
|OP_USEBEFORE
|OP_EXEC
)) == 0) {
226 if (gn
->mtime
!= 0) {
227 fprintf(debug_file
, "modified %s...", Targ_FmtTime(gn
->mtime
));
229 fprintf(debug_file
, "non-existent...");
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
242 * Libraries are only considered out-of-date if the archive module says
245 * These weird rules are brought to you by Backward-Compatibility and
246 * the strange people who wrote 'Make'.
248 if (gn
->type
& (OP_USE
|OP_USEBEFORE
)) {
250 * If the node is a USE node it is *never* out of date
254 fprintf(debug_file
, ".USE node...");
257 } else if ((gn
->type
& OP_LIB
) &&
258 ((gn
->mtime
==0) || Arch_IsLib(gn
))) {
260 fprintf(debug_file
, "library...");
264 * always out of date if no children and :: target
267 oodate
= (gn
->mtime
== 0 || Arch_LibOODate(gn
) ||
268 (gn
->cmtime
== 0 && (gn
->type
& OP_DOUBLEDEP
)));
269 } else if (gn
->type
& OP_JOIN
) {
271 * A target with the .JOIN attribute is only considered
272 * out-of-date if any of its children was out-of-date.
275 fprintf(debug_file
, ".JOIN node...");
278 fprintf(debug_file
, "source %smade...", gn
->flags
& CHILDMADE
? "" : "not ");
280 oodate
= (gn
->flags
& CHILDMADE
) ? TRUE
: FALSE
;
281 } else if (gn
->type
& (OP_FORCE
|OP_EXEC
|OP_PHONY
)) {
283 * A node which is the object of the force (!) operator or which has
284 * the .EXEC attribute is always considered out-of-date.
287 if (gn
->type
& OP_FORCE
) {
288 fprintf(debug_file
, "! operator...");
289 } else if (gn
->type
& OP_PHONY
) {
290 fprintf(debug_file
, ".PHONY node...");
292 fprintf(debug_file
, ".EXEC node...");
296 } else if (gn
->mtime
< gn
->cmtime
||
298 ((gn
->mtime
== 0 && !(gn
->type
& OP_OPTIONAL
))
299 || gn
->type
& OP_DOUBLEDEP
)))
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.
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...");
314 fprintf(debug_file
, ":: operator and no sources...");
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.
327 if (gn
->flags
& FORCE
)
328 fprintf(debug_file
, "non existing child...");
330 oodate
= (gn
->flags
& FORCE
) ? TRUE
: FALSE
;
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.
341 Lst_ForEach(gn
->parents
, MakeTimeStamp
, gn
);
348 *-----------------------------------------------------------------------
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.
354 * gnp the node to add
355 * lp the list to which to add it
361 * The given list is extended
362 *-----------------------------------------------------------------------
365 MakeAddChild(void *gnp
, void *lp
)
367 GNode
*gn
= (GNode
*)gnp
;
370 if ((gn
->flags
& REMAKE
) == 0 && !(gn
->type
& (OP_USE
|OP_USEBEFORE
))) {
372 fprintf(debug_file
, "MakeAddChild: need to examine %s%s\n",
373 gn
->name
, gn
->cohort_num
);
374 (void)Lst_EnQueue(l
, gn
);
380 *-----------------------------------------------------------------------
382 * Function used by Make_Run to find the pathname of a child
383 * that was already made.
386 * gnp the node to find
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 *-----------------------------------------------------------------------
397 MakeFindChild(void *gnp
, void *pgnp
)
399 GNode
*gn
= (GNode
*)gnp
;
400 GNode
*pgn
= (GNode
*)pgnp
;
403 Make_TimeStamp(pgn
, gn
);
410 *-----------------------------------------------------------------------
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.
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.
423 * pgn The target of the .USE node
429 * Children and commands may be added to the parent and the parent's
430 * type may be changed.
432 *-----------------------------------------------------------------------
435 Make_HandleUse(GNode
*cgn
, GNode
*pgn
)
437 LstNode ln
; /* An element in the children list */
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
);
446 if ((cgn
->type
& (OP_USE
|OP_USEBEFORE
)) || Lst_IsEmpty(pgn
->commands
)) {
447 if (cgn
->type
& OP_USEBEFORE
) {
450 * prepend the child's commands to the parent.
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
);
458 * .USE or target has no commands --
459 * append the child's commands to the parent.
461 (void)Lst_Concat(pgn
->commands
, cgn
->commands
, LST_CONCNEW
);
465 if (Lst_Open(cgn
->children
) == SUCCESS
) {
466 while ((ln
= Lst_Next(cgn
->children
)) != NULL
) {
467 GNode
*tgn
, *gn
= (GNode
*)Lst_Datum(ln
);
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.
475 if (gn
->uname
== NULL
) {
476 gn
->uname
= gn
->name
;
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
);
489 (void)Lst_AtEnd(pgn
->children
, gn
);
490 (void)Lst_AtEnd(gn
->parents
, pgn
);
493 Lst_Close(cgn
->children
);
496 pgn
->type
|= cgn
->type
& ~(OP_OPMASK
|OP_USE
|OP_USEBEFORE
|OP_TRANSFORM
);
500 *-----------------------------------------------------------------------
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.
509 * cgnp the child we've just examined
510 * pgnp the current parent
516 * After expansion, .USE child nodes are removed from the parent
518 *-----------------------------------------------------------------------
521 MakeHandleUse(void *cgnp
, void *pgnp
)
523 GNode
*cgn
= (GNode
*)cgnp
;
524 GNode
*pgn
= (GNode
*)pgnp
;
525 LstNode ln
; /* An element in the children list */
528 unmarked
= ((cgn
->type
& OP_MARK
) == 0);
529 cgn
->type
|= OP_MARK
;
531 if ((cgn
->type
& (OP_USE
|OP_USEBEFORE
)) == 0)
535 Make_HandleUse(cgn
, pgn
);
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...
544 if ((ln
= Lst_Member(pgn
->children
, cgn
)) != NULL
) {
545 Lst_Remove(pgn
->children
, ln
);
553 *-----------------------------------------------------------------------
555 * Check the modification time of a gnode, and update it as described
556 * in the comments below.
559 * returns 0 if the gnode does not exist, or it's filesystem
563 * the gnode's modification time and path name are affected.
565 *-----------------------------------------------------------------------
568 Make_Recheck(GNode
*gn
)
570 time_t mtime
= Dir_MTime(gn
);
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.
584 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
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
594 * To force things that depend on FRC to be made, so we have to
595 * check for gn->children being empty as well...
597 if (!Lst_IsEmpty(gn
->commands
) || Lst_IsEmpty(gn
->children
)) {
602 * This is what Make does and it's actually a good thing, as it
605 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
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.
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.
620 * Christos, 4/9/92: If we are saving commands pretend that
621 * the target is made now. Otherwise archives with ... rules
624 if (NoExecute(gn
) || (gn
->type
& OP_SAVE_CMDS
) ||
625 (mtime
== 0 && !(gn
->type
& OP_WAIT
))) {
627 fprintf(debug_file
, " recheck(%s): update time from %s to now\n",
628 gn
->name
, Targ_FmtTime(gn
->mtime
));
634 fprintf(debug_file
, " recheck(%s): current update time: %s\n",
635 gn
->name
, Targ_FmtTime(gn
->mtime
));
643 *-----------------------------------------------------------------------
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
656 * The unmade field of pgn is decremented and pgn may be placed on
657 * the toBeMade queue if this field becomes 0.
659 * If the child was made, the parent's flag CHILDMADE field will be
660 * set true and its cmtime set to now.
662 * If the child is not up-to-date and still does not exist,
663 * set the FORCE flag on the parents.
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.
668 * Finally, if the child is the implied source for the parent, the
669 * parent's IMPSRC variable is set appropriately.
671 *-----------------------------------------------------------------------
674 Make_Update(GNode
*cgn
)
676 GNode
*pgn
; /* the parent node */
677 char *cname
; /* the child's name */
678 LstNode ln
; /* Element in parents and iParents lists */
684 /* It is save to re-examine any nodes again */
687 cname
= Var_Value(TARGET
, cgn
, &p1
);
692 fprintf(debug_file
, "Make_Update: %s%s\n", cgn
->name
, cgn
->cohort_num
);
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.
699 if (cgn
->made
!= UPTODATE
) {
700 mtime
= Make_Recheck(cgn
);
704 * If this is a `::' node, we must consult its first instance
705 * which is where all parents are linked.
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
);
716 parents
= centurion
->parents
;
718 /* If this was a .ORDER node, schedule the RHS */
719 Lst_ForEach(centurion
->order_succ
, MakeBuildParent
, Lst_First(toBeMade
));
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
);
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);
731 if (!(pgn
->flags
& REMAKE
)) {
732 /* This parent isn't needed */
734 fprintf(debug_file
, "- not needed\n");
737 if (mtime
== 0 && !(cgn
->type
& OP_WAIT
))
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.
748 if (pgn
->type
& OP_MADE
) {
750 fprintf(debug_file
, "- .MADE\n");
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
);
761 * A parent must wait for the completion of all instances
762 * of a `::' dependency.
764 if (centurion
->unmade_cohorts
!= 0 || centurion
->made
< MADE
) {
767 "- centurion made %d, %d unmade cohorts\n",
768 centurion
->made
, centurion
->unmade_cohorts
);
772 /* One more child of this parent is now made */
774 if (pgn
->unmade
< 0) {
776 fprintf(debug_file
, "Graph cycles through %s%s\n",
777 pgn
->name
, pgn
->cohort_num
);
780 Error("Graph cycles through %s%s", pgn
->name
, pgn
->cohort_num
);
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
)) {
787 fprintf(debug_file
, "- unmade children\n");
790 if (pgn
->made
!= DEFERRED
) {
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.
797 fprintf(debug_file
, "- not deferred\n");
801 && Lst_ForEach(pgn
->order_pred
, MakeCheckOrder
, 0)) {
802 /* A .ORDER rule stops us building this */
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
);
812 /* Ok, we can schedule the parent again */
813 pgn
->made
= REQUESTED
;
814 (void)Lst_EnQueue(toBeMade
, pgn
);
820 * Set the .PREFIX and .IMPSRC variables for all the implied parents
823 if (Lst_Open(cgn
->iParents
) == SUCCESS
) {
824 char *cpref
= Var_Value(PREFIX
, cgn
, &p1
);
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);
831 Var_Set(PREFIX
, cpref
, pgn
, 0);
836 Lst_Close(cgn
->iParents
);
841 *-----------------------------------------------------------------------
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.
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...)..
859 * The ALLSRC variable for the given node is extended.
860 *-----------------------------------------------------------------------
863 MakeUnmark(void *cgnp
, void *pgnp __unused
)
865 GNode
*cgn
= (GNode
*)cgnp
;
867 cgn
->type
&= ~OP_MARK
;
873 * cgnp The child to add
874 * pgnp The parent to whose ALLSRC variable it should
879 MakeAddAllSrc(void *cgnp
, void *pgnp
)
881 GNode
*cgn
= (GNode
*)cgnp
;
882 GNode
*pgn
= (GNode
*)pgnp
;
884 if (cgn
->type
& OP_MARK
)
886 cgn
->type
|= OP_MARK
;
888 if ((cgn
->type
& (OP_EXEC
|OP_USE
|OP_USEBEFORE
|OP_INVISIBLE
)) == 0) {
889 char *child
, *allsrc
;
890 char *p1
= NULL
, *p2
= NULL
;
892 if (cgn
->type
& OP_ARCHV
)
893 child
= Var_Value(MEMBER
, cgn
, &p1
);
895 child
= cgn
->path
? cgn
->path
: cgn
->name
;
896 if (cgn
->type
& OP_JOIN
) {
897 allsrc
= Var_Value(ALLSRC
, cgn
, &p2
);
902 Var_Append(ALLSRC
, allsrc
, pgn
);
905 if (pgn
->type
& OP_JOIN
) {
906 if (cgn
->made
== MADE
) {
907 Var_Append(OODATE
, child
, pgn
);
909 } else if ((pgn
->mtime
< cgn
->mtime
) ||
910 (cgn
->mtime
>= now
&& cgn
->made
== MADE
))
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
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...
928 Var_Append(OODATE
, child
, pgn
);
937 *-----------------------------------------------------------------------
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.
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 *-----------------------------------------------------------------------
959 Make_DoAllVar(GNode
*gn
)
961 Lst_ForEach(gn
->children
, MakeUnmark
, gn
);
962 Lst_ForEach(gn
->children
, MakeAddAllSrc
, gn
);
964 if (!Var_Exists (OODATE
, gn
)) {
965 Var_Set(OODATE
, "", gn
, 0);
967 if (!Var_Exists (ALLSRC
, gn
)) {
968 Var_Set(ALLSRC
, "", gn
, 0);
971 if (gn
->type
& OP_JOIN
) {
973 Var_Set(TARGET
, Var_Value(ALLSRC
, gn
, &p1
), gn
, 0);
980 *-----------------------------------------------------------------------
982 * Start as many jobs as possible.
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.
990 * Nodes are removed from the toBeMade queue and job table slots
993 *-----------------------------------------------------------------------
997 MakeCheckOrder(void *v_bn
, void *ignore __unused
)
1001 if (bn
->made
>= MADE
|| !(bn
->flags
& REMAKE
))
1004 fprintf(debug_file
, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
1005 bn
->name
, bn
->cohort_num
);
1010 MakeBuildChild(void *v_cn
, void *toBeMade_next
)
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
)
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
;
1028 fprintf(debug_file
, "MakeBuildChild: schedule %s%s\n",
1029 cn
->name
, cn
->cohort_num
);
1031 cn
->made
= REQUESTED
;
1032 if (toBeMade_next
== NULL
)
1033 Lst_AtEnd(toBeMade
, cn
);
1035 Lst_InsertBefore(toBeMade
, toBeMade_next
, cn
);
1037 if (cn
->unmade_cohorts
!= 0)
1038 Lst_ForEach(cn
->cohorts
, MakeBuildChild
, toBeMade_next
);
1041 * If this node is a .WAIT node with unmade chlidren
1042 * then don't add the next sibling.
1044 return cn
->type
& OP_WAIT
&& cn
->unmade
> 0;
1047 /* When a .ORDER RHS node completes we do this on each LHS */
1049 MakeBuildParent(void *v_pn
, void *toBeMade_next
)
1053 if (pn
->made
!= DEFERRED
)
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
;
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())
1076 gn
= (GNode
*)Lst_DeQueue(toBeMade
);
1078 fprintf(debug_file
, "Examining %s%s...\n",
1079 gn
->name
, gn
->cohort_num
);
1081 if (gn
->made
!= REQUESTED
) {
1083 fprintf(debug_file
, "state %d\n", gn
->made
);
1085 make_abort(gn
, __LINE__
);
1088 if (gn
->checked
== checked
) {
1089 /* We've already looked at this node since a job finished... */
1091 fprintf(debug_file
, "already checked %s%s\n",
1092 gn
->name
, gn
->cohort_num
);
1093 gn
->made
= DEFERRED
;
1096 gn
->checked
= checked
;
1098 if (gn
->unmade
!= 0) {
1100 * We can't build this yet, add all unmade children to toBeMade,
1101 * just before the current first element.
1103 gn
->made
= DEFERRED
;
1104 Lst_ForEach(gn
->children
, MakeBuildChild
, Lst_First(toBeMade
));
1105 /* and drop this node on the floor */
1107 fprintf(debug_file
, "dropped %s%s\n", gn
->name
, gn
->cohort_num
);
1111 gn
->made
= BEINGMADE
;
1112 if (Make_OODate(gn
)) {
1114 fprintf(debug_file
, "out-of-date\n");
1124 fprintf(debug_file
, "up-to-date\n");
1126 gn
->made
= UPTODATE
;
1127 if (gn
->type
& OP_JOIN
) {
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)...
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.
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
1163 * A message may be printed.
1165 *-----------------------------------------------------------------------
1168 MakePrintStatusOrder(void *ognp
, void *gnp
)
1173 if (!(ogn
->flags
& REMAKE
) || ogn
->made
> REQUESTED
)
1174 /* not waiting for this one */
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
);
1190 MakePrintStatus(void *gnp
, void *v_errors
)
1192 GNode
*gn
= (GNode
*)gnp
;
1193 int *errors
= v_errors
;
1195 if (gn
->flags
& DONECYCLE
)
1196 /* We've completely processed this node before, don't do it again. */
1199 if (gn
->unmade
== 0) {
1200 gn
->flags
|= DONECYCLE
;
1203 printf("`%s%s' is up to date.\n", gn
->name
, gn
->cohort_num
);
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
)
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
);
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
);
1234 fprintf(debug_file
, "MakePrintStatus: %s%s has %d unmade children\n",
1235 gn
->name
, gn
->cohort_num
, gn
->unmade
);
1237 * If printing cycles and came to one that has unmade children,
1238 * print out the cycle by recursing on its children.
1240 if (!(gn
->flags
& CYCLE
)) {
1241 /* Fist time we've seen this node, check all children */
1243 Lst_ForEach(gn
->children
, MakePrintStatus
, errors
);
1244 /* Mark that this node needn't be processed again */
1245 gn
->flags
|= DONECYCLE
;
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 */
1256 /* Reporting for our children will give the rest of the loop */
1257 Lst_ForEach(gn
->children
, MakePrintStatus
, errors
);
1263 *-----------------------------------------------------------------------
1265 * Expand .USE nodes and create a new targets list
1268 * targs the initial list of targets
1271 *-----------------------------------------------------------------------
1274 Make_ExpandUse(Lst targs
)
1276 GNode
*gn
; /* a temporary pointer */
1277 Lst examine
; /* List of targets to examine */
1279 examine
= Lst_Duplicate(targs
, NULL
);
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.
1289 while (!Lst_IsEmpty (examine
)) {
1290 gn
= (GNode
*)Lst_DeQueue(examine
);
1292 if (gn
->flags
& REMAKE
)
1293 /* We've looked at this one already */
1295 gn
->flags
|= REMAKE
;
1297 fprintf(debug_file
, "Make_ExpandUse: examine %s%s\n",
1298 gn
->name
, gn
->cohort_num
);
1300 if ((gn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (gn
->cohorts
)) {
1301 /* Append all the 'cohorts' to the list of things to examine */
1303 new = Lst_Duplicate(gn
->cohorts
, NULL
);
1304 Lst_Concat(new, examine
, LST_CONCLINK
);
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
1314 if (gn
->type
& OP_ARCHV
) {
1316 eoa
= strchr(gn
->name
, '(');
1317 eon
= strchr(gn
->name
, ')');
1318 if (eoa
== NULL
|| eon
== NULL
)
1322 Var_Set(MEMBER
, eoa
+ 1, gn
, 0);
1323 Var_Set(ARCHIVE
, gn
->name
, gn
, 0);
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
);
1333 if ((gn
->type
& OP_MADE
) == 0)
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
);
1343 if (gn
->unmade
!= 0)
1344 Lst_ForEach(gn
->children
, MakeAddChild
, examine
);
1347 Lst_Destroy(examine
, NULL
);
1351 *-----------------------------------------------------------------------
1352 * Make_ProcessWait --
1353 * Convert .WAIT nodes into dependencies
1356 * targs the initial list of targets
1358 *-----------------------------------------------------------------------
1362 link_parent(void *cnp
, void *pnp
)
1367 Lst_AtEnd(pn
->children
, cn
);
1368 Lst_AtEnd(cn
->parents
, pn
);
1374 add_wait_dep(void *v_cn
, void *v_wn
)
1382 if (cn
== NULL
|| wn
== NULL
) {
1383 printf("bad wait dep %p %p\n", cn
, wn
);
1387 fprintf(debug_file
, ".WAIT: add dependency %s%s -> %s\n",
1388 cn
->name
, cn
->cohort_num
, wn
->name
);
1390 Lst_AtEnd(wn
->children
, cn
);
1392 Lst_AtEnd(cn
->parents
, wn
);
1397 Make_ProcessWait(Lst targs
)
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 */
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...
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
);
1417 Lst_ForEach(targs
, link_parent
, pgn
);
1419 /* Start building with the 'dummy' .MAIN' node */
1420 MakeBuildChild(pgn
, NULL
);
1422 examine
= Lst_Init(FALSE
);
1423 Lst_AtEnd(examine
, pgn
);
1425 while (!Lst_IsEmpty (examine
)) {
1426 pgn
= Lst_DeQueue(examine
);
1428 /* We only want to process each child-list once */
1429 if (pgn
->flags
& DONE_WAIT
)
1431 pgn
->flags
|= DONE_WAIT
;
1433 fprintf(debug_file
, "Make_ProcessWait: examine %s\n", pgn
->name
);
1435 if ((pgn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (pgn
->cohorts
)) {
1436 /* Append all the 'cohorts' to the list of things to examine */
1438 new = Lst_Duplicate(pgn
->cohorts
, NULL
);
1439 Lst_Concat(new, examine
, LST_CONCLINK
);
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
);
1452 Lst_AtEnd(examine
, cgn
);
1455 Lst_Close(pgn
->children
);
1458 Lst_Destroy(examine
, NULL
);
1462 *-----------------------------------------------------------------------
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
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
1474 * targs the initial list of targets
1477 * TRUE if work was done. FALSE otherwise.
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 *-----------------------------------------------------------------------
1488 int errors
; /* Number of errors the Job module reports */
1490 /* Start trying to make the current targets... */
1491 toBeMade
= Lst_Init(FALSE
);
1493 Make_ExpandUse(targs
);
1494 Make_ProcessWait(targs
);
1497 fprintf(debug_file
, "#***# full graph\n");
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)
1507 return (MakeStartJobs());
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.
1516 (void)MakeStartJobs();
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.
1528 while (!Lst_IsEmpty(toBeMade
) || jobTokensRunning
> 0) {
1530 (void)MakeStartJobs();
1533 errors
= Job_Finish();
1536 * Print the final status of each target. E.g. if it wasn't made
1537 * because some inferior reported an error.
1540 fprintf(debug_file
, "done: errors %d\n", errors
);
1542 Lst_ForEach(targs
, MakePrintStatus
, &errors
);
1544 fprintf(debug_file
, "done: errors %d\n", errors
);