1 /* $NetBSD: make.c,v 1.133 2020/08/30 14:11:42 rillig 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.133 2020/08/30 14:11:42 rillig 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.133 2020/08/30 14:11:42 rillig 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 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
102 * Make_TimeStamp Function to set the parent's cmgn 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 *);
142 MAKE_ATTR_DEAD static 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);
154 ENUM_VALUE_RTTI_8(GNodeMade,
155 UNMADE, DEFERRED, REQUESTED, BEINGMADE,
156 MADE, UPTODATE, ERROR, ABORTED);
158 ENUM_FLAGS_RTTI_31(GNodeType,
159 OP_DEPENDS, OP_FORCE, OP_DOUBLEDEP,
160 /* OP_OPMASK is omitted since it combines other flags */
161 OP_OPTIONAL, OP_USE, OP_EXEC, OP_IGNORE,
162 OP_PRECIOUS, OP_SILENT, OP_MAKE, OP_JOIN,
163 OP_MADE, OP_SPECIAL, OP_USEBEFORE, OP_INVISIBLE,
164 OP_NOTMAIN, OP_PHONY, OP_NOPATH, OP_WAIT,
165 OP_NOMETA, OP_META, OP_NOMETA_CMP, OP_SUBMAKE,
166 OP_TRANSFORM, OP_MEMBER, OP_LIB, OP_ARCHV,
167 OP_HAS_COMMANDS, OP_SAVE_CMDS, OP_DEPS_FOUND, OP_MARK);
169 ENUM_FLAGS_RTTI_10(GNodeFlags,
170 REMAKE, CHILDMADE, FORCE, DONE_WAIT,
171 DONE_ORDER, FROM_DEPEND, DONE_ALLSRC, CYCLE,
172 DONECYCLE, INTERNAL);
175 GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
178 char type_buf[GNodeType_ToStringSize];
179 char flags_buf[GNodeFlags_ToStringSize];
181 fprintf(f, "%smade %s, type %s, flags %s%s",
183 Enum_ValueToString(gn->made, GNodeMade_ToStringSpecs),
184 Enum_FlagsToString(type_buf, sizeof type_buf,
185 gn->type, GNodeType_ToStringSpecs),
186 Enum_FlagsToString(flags_buf, sizeof flags_buf,
187 gn->flags, GNodeFlags_ToStringSpecs),
192 *-----------------------------------------------------------------------
194 * Set the cmgn field of a parent node based on the mtime stamp in its
195 * child. Called from MakeOODate via Lst_ForEach.
198 * pgn the current parent
199 * cgn the child we've just examined
205 * The cmgn of the parent node will be changed if the mtime
206 * field of the child is greater than it.
207 *-----------------------------------------------------------------------
210 Make_TimeStamp(GNode *pgn, GNode *cgn)
212 if (pgn->cmgn == NULL || cgn->mtime > pgn->cmgn->mtime) {
220 * pgn the current parent
221 * cgn the child we've just examined
225 MakeTimeStamp(void *pgn, void *cgn)
227 return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
231 *-----------------------------------------------------------------------
233 * See if a given node is out of date with respect to its sources.
234 * Used by Make_Run when deciding which nodes to place on the
235 * toBeMade queue initially and by Make_Update to screen out USE and
236 * EXEC nodes. In the latter case, however, any other sort of node
237 * must be considered out-of-date since at least one of its children
238 * will have been recreated.
241 * gn the node to check
244 * TRUE if the node is out of date. FALSE otherwise.
247 * The mtime field of the node and the cmgn field of its parents
248 * will/may be changed.
249 *-----------------------------------------------------------------------
252 Make_OODate(GNode *gn)
257 * Certain types of targets needn't even be sought as their datedness
258 * doesn't depend on their modification time...
260 if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
261 (void)Dir_MTime(gn, 1);
263 if (gn->mtime != 0) {
264 fprintf(debug_file, "modified %s...", Targ_FmtTime(gn->mtime));
266 fprintf(debug_file, "non-existent...");
272 * A target is remade in one of the following circumstances:
273 * its modification time is smaller than that of its youngest child
274 * and it would actually be run (has commands or type OP_NOP)
275 * it's the object of a force operator
276 * it has no children, was on the lhs of an operator and doesn't exist
279 * Libraries are only considered out-of-date if the archive module says
282 * These weird rules are brought to you by Backward-Compatibility and
283 * the strange people who wrote 'Make'.
285 if (gn->type & (OP_USE|OP_USEBEFORE)) {
287 * If the node is a USE node it is *never* out of date
291 fprintf(debug_file, ".USE node...");
294 } else if ((gn->type & OP_LIB) &&
295 ((gn->mtime==0) || Arch_IsLib(gn))) {
297 fprintf(debug_file, "library...");
301 * always out of date if no children and :: target
304 oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
305 (gn->cmgn == NULL && (gn->type & OP_DOUBLEDEP)));
306 } else if (gn->type & OP_JOIN) {
308 * A target with the .JOIN attribute is only considered
309 * out-of-date if any of its children was out-of-date.
312 fprintf(debug_file, ".JOIN node...");
315 fprintf(debug_file, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
317 oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
318 } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
320 * A node which is the object of the force (!) operator or which has
321 * the .EXEC attribute is always considered out-of-date.
324 if (gn->type & OP_FORCE) {
325 fprintf(debug_file, "! operator...");
326 } else if (gn->type & OP_PHONY) {
327 fprintf(debug_file, ".PHONY node...");
329 fprintf(debug_file, ".EXEC node...");
333 } else if ((gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) ||
335 ((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
336 || gn->type & OP_DOUBLEDEP)))
339 * A node whose modification time is less than that of its
340 * youngest child or that has no children (cmgn == NULL) and
341 * either doesn't exist (mtime == 0) and it isn't optional
342 * or was the object of a * :: operator is out-of-date.
343 * Why? Because that's the way Make does it.
346 if (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) {
347 fprintf(debug_file, "modified before source %s...",
348 gn->cmgn->path ? gn->cmgn->path : gn->cmgn->name);
349 } else if (gn->mtime == 0) {
350 fprintf(debug_file, "non-existent and no sources...");
352 fprintf(debug_file, ":: operator and no sources...");
358 * When a non-existing child with no sources
359 * (such as a typically used FORCE source) has been made and
360 * the target of the child (usually a directory) has the same
361 * timestamp as the timestamp just given to the non-existing child
362 * after it was considered made.
365 if (gn->flags & FORCE)
366 fprintf(debug_file, "non existing child...");
368 oodate = (gn->flags & FORCE) ? TRUE : FALSE;
373 oodate = meta_oodate(gn, oodate);
378 * If the target isn't out-of-date, the parents need to know its
379 * modification time. Note that targets that appear to be out-of-date
380 * but aren't, because they have no commands and aren't of type OP_NOP,
381 * have their mtime stay below their children's mtime to keep parents from
382 * thinking they're out-of-date.
385 Lst_ForEach(gn->parents, MakeTimeStamp, gn);
392 *-----------------------------------------------------------------------
394 * Function used by Make_Run to add a child to the list l.
395 * It will only add the child if its make field is FALSE.
398 * gnp the node to add
399 * lp the list to which to add it
405 * The given list is extended
406 *-----------------------------------------------------------------------
409 MakeAddChild(void *gnp, void *lp)
411 GNode *gn = (GNode *)gnp;
414 if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
416 fprintf(debug_file, "MakeAddChild: need to examine %s%s\n",
417 gn->name, gn->cohort_num);
424 *-----------------------------------------------------------------------
426 * Function used by Make_Run to find the pathname of a child
427 * that was already made.
430 * gnp the node to find
436 * The path and mtime of the node and the cmgn of the parent are
437 * updated; the unmade children count of the parent is decremented.
438 *-----------------------------------------------------------------------
441 MakeFindChild(void *gnp, void *pgnp)
443 GNode *gn = (GNode *)gnp;
444 GNode *pgn = (GNode *)pgnp;
446 (void)Dir_MTime(gn, 0);
447 Make_TimeStamp(pgn, gn);
453 /* Called by Make_Run and SuffApplyTransform on the downward pass to handle
454 * .USE and transformation nodes, by copying the child node's commands, type
455 * flags and children to the parent node.
457 * A .USE node is much like an explicit transformation rule, except its
458 * commands are always added to the target node, even if the target already
463 * pgn The target of the .USE node
466 Make_HandleUse(GNode *cgn, GNode *pgn)
468 LstNode ln; /* An element in the children list */
471 if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
472 fprintf(debug_file, "Make_HandleUse: called for plain node %s\n", cgn->name);
477 if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
478 if (cgn->type & OP_USEBEFORE) {
480 Lst_PrependAll(pgn->commands, cgn->commands);
482 /* .USE, or target has no commands */
483 Lst_AppendAll(pgn->commands, cgn->commands);
487 Lst_Open(cgn->children);
488 while ((ln = Lst_Next(cgn->children)) != NULL) {
489 GNode *gn = LstNode_Datum(ln);
492 * Expand variables in the .USE node's name
493 * and save the unexpanded form.
494 * We don't need to do this for commands.
495 * They get expanded properly when we execute.
497 if (gn->uname == NULL) {
498 gn->uname = gn->name;
502 gn->name = Var_Subst(gn->uname, pgn, VARE_WANTRES);
503 if (gn->uname && strcmp(gn->name, gn->uname) != 0) {
504 /* See if we have a target for this node. */
505 GNode *tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
510 Lst_Append(pgn->children, gn);
511 Lst_Append(gn->parents, pgn);
514 Lst_Close(cgn->children);
516 pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
520 *-----------------------------------------------------------------------
522 * Callback function for Lst_ForEach, used by Make_Run on the downward
523 * pass to handle .USE nodes. Should be called before the children
524 * are enqueued to be looked at by MakeAddChild.
525 * This function calls Make_HandleUse to copy the .USE node's commands,
526 * type flags and children to the parent node.
529 * cgnp the child we've just examined
530 * pgnp the current parent
536 * After expansion, .USE child nodes are removed from the parent
538 *-----------------------------------------------------------------------
541 MakeHandleUse(void *cgnp, void *pgnp)
543 GNode *cgn = (GNode *)cgnp;
544 GNode *pgn = (GNode *)pgnp;
545 LstNode ln; /* An element in the children list */
548 unmarked = ((cgn->type & OP_MARK) == 0);
549 cgn->type |= OP_MARK;
551 if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
555 Make_HandleUse(cgn, pgn);
558 * This child node is now "made", so we decrement the count of
559 * unmade children in the parent... We also remove the child
560 * from the parent's list to accurately reflect the number of decent
561 * children the parent has. This is used by Make_Run to decide
562 * whether to queue the parent or examine its children...
564 if ((ln = Lst_FindDatum(pgn->children, cgn)) != NULL) {
565 Lst_Remove(pgn->children, ln);
573 *-----------------------------------------------------------------------
575 * Check the modification time of a gnode, and update it as described
576 * in the comments below.
579 * returns 0 if the gnode does not exist, or its filesystem
583 * the gnode's modification time and path name are affected.
585 *-----------------------------------------------------------------------
588 Make_Recheck(GNode *gn)
590 time_t mtime = Dir_MTime(gn, 1);
594 * We can't re-stat the thing, but we can at least take care of rules
595 * where a target depends on a source that actually creates the
596 * target, but only if it has changed, e.g.
604 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
606 * In this case, if the definitions produced by yacc haven't changed
607 * from before, parse.h won't have been updated and gn->mtime will
608 * reflect the current modification time for parse.h. This is
609 * something of a kludge, I admit, but it's a useful one..
610 * XXX: People like to use a rule like
614 * To force things that depend on FRC to be made, so we have to
615 * check for gn->children being empty as well...
617 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
622 * This is what Make does and it's actually a good thing, as it
625 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
627 * to function as intended. Unfortunately, thanks to the stateless
628 * nature of NFS (by which I mean the loose coupling of two clients
629 * using the same file from a common server), there are times
630 * when the modification time of a file created on a remote
631 * machine will not be modified before the local stat() implied by
632 * the Dir_MTime occurs, thus leading us to believe that the file
633 * is unchanged, wreaking havoc with files that depend on this one.
635 * I have decided it is better to make too much than to make too
636 * little, so this stuff is commented out unless you're sure it's ok.
640 * Christos, 4/9/92: If we are saving commands pretend that
641 * the target is made now. Otherwise archives with ... rules
644 if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
645 (mtime == 0 && !(gn->type & OP_WAIT))) {
647 fprintf(debug_file, " recheck(%s): update time from %s to now\n",
648 gn->name, Targ_FmtTime(gn->mtime));
654 fprintf(debug_file, " recheck(%s): current update time: %s\n",
655 gn->name, Targ_FmtTime(gn->mtime));
663 *-----------------------------------------------------------------------
665 * Perform update on the parents of a node. Used by JobFinish once
666 * a node has been dealt with and by MakeStartJobs if it finds an
676 * The unmade field of pgn is decremented and pgn may be placed on
677 * the toBeMade queue if this field becomes 0.
679 * If the child was made, the parent's flag CHILDMADE field will be
682 * If the child is not up-to-date and still does not exist,
683 * set the FORCE flag on the parents.
685 * If the child wasn't made, the cmgn field of the parent will be
686 * altered if the child's mtime is big enough.
688 * Finally, if the child is the implied source for the parent, the
689 * parent's IMPSRC variable is set appropriately.
691 *-----------------------------------------------------------------------
694 Make_Update(GNode *cgn)
696 GNode *pgn; /* the parent node */
697 const char *cname; /* the child's name */
698 LstNode ln; /* Element in parents and implicitParents lists */
704 /* It is save to re-examine any nodes again */
707 cname = Var_Value(TARGET, cgn, &p1);
711 fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
714 * If the child was actually made, see what its modification time is
715 * now -- some rules won't actually update the file. If the file still
716 * doesn't exist, make its mtime now.
718 if (cgn->made != UPTODATE) {
719 mtime = Make_Recheck(cgn);
723 * If this is a `::' node, we must consult its first instance
724 * which is where all parents are linked.
726 if ((centurion = cgn->centurion) != NULL) {
727 if (!Lst_IsEmpty(cgn->parents))
728 Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
729 centurion->unmade_cohorts -= 1;
730 if (centurion->unmade_cohorts < 0)
731 Error("Graph cycles through centurion %s", centurion->name);
735 parents = centurion->parents;
737 /* If this was a .ORDER node, schedule the RHS */
738 Lst_ForEach(centurion->order_succ, MakeBuildParent, Lst_First(toBeMade));
740 /* Now mark all the parents as having one less unmade child */
742 while ((ln = Lst_Next(parents)) != NULL) {
743 pgn = LstNode_Datum(ln);
745 fprintf(debug_file, "inspect parent %s%s: flags %x, "
746 "type %x, made %d, unmade %d ",
747 pgn->name, pgn->cohort_num, pgn->flags,
748 pgn->type, pgn->made, pgn->unmade-1);
750 if (!(pgn->flags & REMAKE)) {
751 /* This parent isn't needed */
753 fprintf(debug_file, "- not needed\n");
756 if (mtime == 0 && !(cgn->type & OP_WAIT))
760 * If the parent has the .MADE attribute, its timestamp got
761 * updated to that of its newest child, and its unmake
762 * child count got set to zero in Make_ExpandUse().
763 * However other things might cause us to build one of its
764 * children - and so we mustn't do any processing here when
765 * the child build finishes.
767 if (pgn->type & OP_MADE) {
769 fprintf(debug_file, "- .MADE\n");
773 if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
774 if (cgn->made == MADE)
775 pgn->flags |= CHILDMADE;
776 (void)Make_TimeStamp(pgn, cgn);
780 * A parent must wait for the completion of all instances
781 * of a `::' dependency.
783 if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
786 "- centurion made %d, %d unmade cohorts\n",
787 centurion->made, centurion->unmade_cohorts);
791 /* One more child of this parent is now made */
793 if (pgn->unmade < 0) {
795 fprintf(debug_file, "Graph cycles through %s%s\n",
796 pgn->name, pgn->cohort_num);
799 Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
802 /* We must always rescan the parents of .WAIT and .ORDER nodes. */
803 if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
804 && !(centurion->flags & DONE_ORDER)) {
806 fprintf(debug_file, "- unmade children\n");
809 if (pgn->made != DEFERRED) {
811 * Either this parent is on a different branch of the tree,
812 * or it on the RHS of a .WAIT directive
813 * or it is already on the toBeMade list.
816 fprintf(debug_file, "- not deferred\n");
819 assert(pgn->order_pred != NULL);
820 if (Lst_ForEach(pgn->order_pred, MakeCheckOrder, 0)) {
821 /* A .ORDER rule stops us building this */
826 fprintf(debug_file, "- %s%s made, schedule %s%s (made %d)\n",
827 cgn->name, cgn->cohort_num,
828 pgn->name, pgn->cohort_num, pgn->made);
829 Targ_PrintNode(pgn, &two);
831 /* Ok, we can schedule the parent again */
832 pgn->made = REQUESTED;
833 Lst_Enqueue(toBeMade, pgn);
838 * Set the .PREFIX and .IMPSRC variables for all the implied parents
841 Lst_Open(cgn->implicitParents);
843 const char *cpref = Var_Value(PREFIX, cgn, &p1);
845 while ((ln = Lst_Next(cgn->implicitParents)) != NULL) {
846 pgn = LstNode_Datum(ln);
847 if (pgn->flags & REMAKE) {
848 Var_Set(IMPSRC, cname, pgn);
850 Var_Set(PREFIX, cpref, pgn);
854 Lst_Close(cgn->implicitParents);
859 *-----------------------------------------------------------------------
861 * Add a child's name to the ALLSRC and OODATE variables of the given
862 * node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
863 * if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
864 * .EXEC and .USE children are very rarely going to be files, so...
865 * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
867 * A child is added to the OODATE variable if its modification time is
868 * later than that of its parent, as defined by Make, except if the
869 * parent is a .JOIN node. In that case, it is only added to the OODATE
870 * variable if it was actually made (since .JOIN nodes don't have
871 * modification times, the comparison is rather unfair...)..
877 * The ALLSRC variable for the given node is extended.
878 *-----------------------------------------------------------------------
881 MakeUnmark(void *cgnp, void *pgnp MAKE_ATTR_UNUSED)
883 GNode *cgn = (GNode *)cgnp;
885 cgn->type &= ~OP_MARK;
891 * cgnp The child to add
892 * pgnp The parent to whose ALLSRC variable it should
897 MakeAddAllSrc(void *cgnp, void *pgnp)
899 GNode *cgn = (GNode *)cgnp;
900 GNode *pgn = (GNode *)pgnp;
902 if (cgn->type & OP_MARK)
904 cgn->type |= OP_MARK;
906 if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
907 const char *child, *allsrc;
908 char *p1 = NULL, *p2 = NULL;
910 if (cgn->type & OP_ARCHV)
911 child = Var_Value(MEMBER, cgn, &p1);
913 child = cgn->path ? cgn->path : cgn->name;
914 if (cgn->type & OP_JOIN) {
915 allsrc = Var_Value(ALLSRC, cgn, &p2);
920 Var_Append(ALLSRC, allsrc, pgn);
922 if (pgn->type & OP_JOIN) {
923 if (cgn->made == MADE) {
924 Var_Append(OODATE, child, pgn);
926 } else if ((pgn->mtime < cgn->mtime) ||
927 (cgn->mtime >= now && cgn->made == MADE))
930 * It goes in the OODATE variable if the parent is younger than the
931 * child or if the child has been modified more recently than
932 * the start of the make. This is to keep pmake from getting
933 * confused if something else updates the parent after the
934 * make starts (shouldn't happen, I know, but sometimes it
935 * does). In such a case, if we've updated the kid, the parent
936 * is likely to have a modification time later than that of
937 * the kid and anything that relies on the OODATE variable will
940 * XXX: This will cause all made children to go in the OODATE
941 * variable, even if they're not touched, if RECHECK isn't defined,
942 * since cgn->mtime is set to now in Make_Update. According to
943 * some people, this is good...
945 Var_Append(OODATE, child, pgn);
953 *-----------------------------------------------------------------------
955 * Set up the ALLSRC and OODATE variables. Sad to say, it must be
956 * done separately, rather than while traversing the graph. This is
957 * because Make defined OODATE to contain all sources whose modification
958 * times were later than that of the target, *not* those sources that
959 * were out-of-date. Since in both compatibility and native modes,
960 * the modification time of the parent isn't found until the child
961 * has been dealt with, we have to wait until now to fill in the
962 * variable. As for ALLSRC, the ordering is important and not
963 * guaranteed when in native mode, so it must be set here, too.
969 * The ALLSRC and OODATE variables of the given node is filled in.
970 * If the node is a .JOIN node, its TARGET variable will be set to
971 * match its ALLSRC variable.
972 *-----------------------------------------------------------------------
975 Make_DoAllVar(GNode *gn)
977 if (gn->flags & DONE_ALLSRC)
980 Lst_ForEach(gn->children, MakeUnmark, gn);
981 Lst_ForEach(gn->children, MakeAddAllSrc, gn);
983 if (!Var_Exists (OODATE, gn)) {
984 Var_Set(OODATE, "", gn);
986 if (!Var_Exists (ALLSRC, gn)) {
987 Var_Set(ALLSRC, "", gn);
990 if (gn->type & OP_JOIN) {
992 Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn);
995 gn->flags |= DONE_ALLSRC;
999 *-----------------------------------------------------------------------
1001 * Start as many jobs as possible.
1004 * If the query flag was given to pmake, no job will be started,
1005 * but as soon as an out-of-date target is found, this function
1006 * returns TRUE. At all other times, this function returns FALSE.
1009 * Nodes are removed from the toBeMade queue and job table slots
1012 *-----------------------------------------------------------------------
1016 MakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
1020 if (bn->made >= MADE || !(bn->flags & REMAKE))
1023 fprintf(debug_file, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
1024 bn->name, bn->cohort_num);
1029 MakeBuildChild(void *v_cn, void *toBeMade_next)
1034 fprintf(debug_file, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
1035 cn->name, cn->cohort_num, cn->made, cn->type);
1036 if (cn->made > DEFERRED)
1039 /* If this node is on the RHS of a .ORDER, check LHSs. */
1040 assert(cn->order_pred);
1041 if (Lst_ForEach(cn->order_pred, MakeCheckOrder, 0)) {
1042 /* Can't build this (or anything else in this child list) yet */
1043 cn->made = DEFERRED;
1044 return 0; /* but keep looking */
1048 fprintf(debug_file, "MakeBuildChild: schedule %s%s\n",
1049 cn->name, cn->cohort_num);
1051 cn->made = REQUESTED;
1052 if (toBeMade_next == NULL)
1053 Lst_Append(toBeMade, cn);
1055 Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1057 if (cn->unmade_cohorts != 0)
1058 Lst_ForEach(cn->cohorts, MakeBuildChild, toBeMade_next);
1061 * If this node is a .WAIT node with unmade chlidren
1062 * then don't add the next sibling.
1064 return cn->type & OP_WAIT && cn->unmade > 0;
1067 /* When a .ORDER LHS node completes we do this on each RHS */
1069 MakeBuildParent(void *v_pn, void *toBeMade_next)
1073 if (pn->made != DEFERRED)
1076 if (MakeBuildChild(pn, toBeMade_next) == 0) {
1077 /* Mark so that when this node is built we reschedule its parents */
1078 pn->flags |= DONE_ORDER;
1090 while (!Lst_IsEmpty(toBeMade)) {
1091 /* Get token now to avoid cycling job-list when we only have 1 token */
1092 if (!have_token && !Job_TokenWithdraw())
1096 gn = Lst_Dequeue(toBeMade);
1098 fprintf(debug_file, "Examining %s%s...\n",
1099 gn->name, gn->cohort_num);
1101 if (gn->made != REQUESTED) {
1103 fprintf(debug_file, "state %d\n", gn->made);
1105 make_abort(gn, __LINE__);
1108 if (gn->checked == checked) {
1109 /* We've already looked at this node since a job finished... */
1111 fprintf(debug_file, "already checked %s%s\n",
1112 gn->name, gn->cohort_num);
1113 gn->made = DEFERRED;
1116 gn->checked = checked;
1118 if (gn->unmade != 0) {
1120 * We can't build this yet, add all unmade children to toBeMade,
1121 * just before the current first element.
1123 gn->made = DEFERRED;
1124 Lst_ForEach(gn->children, MakeBuildChild, Lst_First(toBeMade));
1125 /* and drop this node on the floor */
1127 fprintf(debug_file, "dropped %s%s\n", gn->name, gn->cohort_num);
1131 gn->made = BEINGMADE;
1132 if (Make_OODate(gn)) {
1134 fprintf(debug_file, "out-of-date\n");
1144 fprintf(debug_file, "up-to-date\n");
1146 gn->made = UPTODATE;
1147 if (gn->type & OP_JOIN) {
1149 * Even for an up-to-date .JOIN node, we need it to have its
1150 * context variables so references to it get the correct
1151 * value for .TARGET when building up the context variables
1152 * of its parent(s)...
1167 *-----------------------------------------------------------------------
1168 * MakePrintStatus --
1169 * Print the status of a top-level node, viz. it being up-to-date
1170 * already or not created due to an error in a lower level.
1171 * Callback function for Make_Run via Lst_ForEach.
1174 * gnp Node to examine
1175 * cyclep True if gn->unmade being non-zero implies a
1176 * cycle in the graph, not an error in an
1183 * A message may be printed.
1185 *-----------------------------------------------------------------------
1188 MakePrintStatusOrder(void *ognp, void *gnp)
1193 if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
1194 /* not waiting for this one */
1197 printf(" `%s%s' has .ORDER dependency against %s%s ",
1198 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1199 GNode_FprintDetails(stdout, "(", ogn, ")\n");
1201 if (DEBUG(MAKE) && debug_file != stdout) {
1202 fprintf(debug_file, " `%s%s' has .ORDER dependency against %s%s ",
1203 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1204 GNode_FprintDetails(debug_file, "(", ogn, ")\n");
1210 MakePrintStatus(void *gnp, void *v_errors)
1212 GNode *gn = (GNode *)gnp;
1213 int *errors = v_errors;
1215 if (gn->flags & DONECYCLE)
1216 /* We've completely processed this node before, don't do it again. */
1219 if (gn->unmade == 0) {
1220 gn->flags |= DONECYCLE;
1223 printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1232 printf("`%s%s' was not built", gn->name, gn->cohort_num);
1233 GNode_FprintDetails(stdout, " (", gn, ")!\n");
1234 if (DEBUG(MAKE) && debug_file != stdout) {
1235 fprintf(debug_file, "`%s%s' was not built",
1236 gn->name, gn->cohort_num);
1237 GNode_FprintDetails(debug_file, " (", gn, ")!\n");
1239 /* Most likely problem is actually caused by .ORDER */
1240 Lst_ForEach(gn->order_pred, MakePrintStatusOrder, gn);
1243 /* Errors - already counted */
1244 printf("`%s%s' not remade because of errors.\n",
1245 gn->name, gn->cohort_num);
1246 if (DEBUG(MAKE) && debug_file != stdout)
1247 fprintf(debug_file, "`%s%s' not remade because of errors.\n",
1248 gn->name, gn->cohort_num);
1255 fprintf(debug_file, "MakePrintStatus: %s%s has %d unmade children\n",
1256 gn->name, gn->cohort_num, gn->unmade);
1258 * If printing cycles and came to one that has unmade children,
1259 * print out the cycle by recursing on its children.
1261 if (!(gn->flags & CYCLE)) {
1262 /* Fist time we've seen this node, check all children */
1264 Lst_ForEach(gn->children, MakePrintStatus, errors);
1265 /* Mark that this node needn't be processed again */
1266 gn->flags |= DONECYCLE;
1270 /* Only output the error once per node */
1271 gn->flags |= DONECYCLE;
1272 Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1273 if ((*errors)++ > 100)
1274 /* Abandon the whole error report */
1277 /* Reporting for our children will give the rest of the loop */
1278 Lst_ForEach(gn->children, MakePrintStatus, errors);
1284 *-----------------------------------------------------------------------
1286 * Expand .USE nodes and create a new targets list
1289 * targs the initial list of targets
1292 *-----------------------------------------------------------------------
1295 Make_ExpandUse(Lst targs)
1297 GNode *gn; /* a temporary pointer */
1298 Lst examine; /* List of targets to examine */
1300 examine = Lst_Copy(targs, NULL);
1303 * Make an initial downward pass over the graph, marking nodes to be made
1304 * as we go down. We call Suff_FindDeps to find where a node is and
1305 * to get some children for it if it has none and also has no commands.
1306 * If the node is a leaf, we stick it on the toBeMade queue to
1307 * be looked at in a minute, otherwise we add its children to our queue
1308 * and go on about our business.
1310 while (!Lst_IsEmpty(examine)) {
1311 gn = Lst_Dequeue(examine);
1313 if (gn->flags & REMAKE)
1314 /* We've looked at this one already */
1316 gn->flags |= REMAKE;
1318 fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
1319 gn->name, gn->cohort_num);
1321 if (gn->type & OP_DOUBLEDEP)
1322 Lst_PrependAll(examine, gn->cohorts);
1325 * Apply any .USE rules before looking for implicit dependencies
1326 * to make sure everything has commands that should...
1327 * Make sure that the TARGET is set, so that we can make
1330 if (gn->type & OP_ARCHV) {
1332 eoa = strchr(gn->name, '(');
1333 eon = strchr(gn->name, ')');
1334 if (eoa == NULL || eon == NULL)
1338 Var_Set(MEMBER, eoa + 1, gn);
1339 Var_Set(ARCHIVE, gn->name, gn);
1344 (void)Dir_MTime(gn, 0);
1345 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
1346 Lst_ForEach(gn->children, MakeUnmark, gn);
1347 Lst_ForEach(gn->children, MakeHandleUse, gn);
1349 if ((gn->type & OP_MADE) == 0)
1352 /* Pretend we made all this node's children */
1353 Lst_ForEach(gn->children, MakeFindChild, gn);
1354 if (gn->unmade != 0)
1355 printf("Warning: %s%s still has %d unmade children\n",
1356 gn->name, gn->cohort_num, gn->unmade);
1359 if (gn->unmade != 0)
1360 Lst_ForEach(gn->children, MakeAddChild, examine);
1367 *-----------------------------------------------------------------------
1368 * Make_ProcessWait --
1369 * Convert .WAIT nodes into dependencies
1372 * targs the initial list of targets
1374 *-----------------------------------------------------------------------
1378 link_parent(void *cnp, void *pnp)
1383 Lst_Append(pn->children, cn);
1384 Lst_Append(cn->parents, pn);
1390 add_wait_dep(void *v_cn, void *v_wn)
1398 if (cn == NULL || wn == NULL) {
1399 printf("bad wait dep %p %p\n", cn, wn);
1403 fprintf(debug_file, ".WAIT: add dependency %s%s -> %s\n",
1404 cn->name, cn->cohort_num, wn->name);
1406 Lst_Append(wn->children, cn);
1408 Lst_Append(cn->parents, wn);
1413 Make_ProcessWait(Lst targs)
1415 GNode *pgn; /* 'parent' node we are examining */
1416 GNode *cgn; /* Each child in turn */
1417 LstNode owln; /* Previous .WAIT node */
1418 Lst examine; /* List of targets to examine */
1422 * We need all the nodes to have a common parent in order for the
1423 * .WAIT and .ORDER scheduling to work.
1424 * Perhaps this should be done earlier...
1427 pgn = Targ_NewGN(".MAIN");
1428 pgn->flags = REMAKE;
1429 pgn->type = OP_PHONY | OP_DEPENDS;
1430 /* Get it displayed in the diag dumps */
1431 Lst_Prepend(Targ_List(), pgn);
1433 Lst_ForEach(targs, link_parent, pgn);
1435 /* Start building with the 'dummy' .MAIN' node */
1436 MakeBuildChild(pgn, NULL);
1438 examine = Lst_Init();
1439 Lst_Append(examine, pgn);
1441 while (!Lst_IsEmpty(examine)) {
1442 pgn = Lst_Dequeue(examine);
1444 /* We only want to process each child-list once */
1445 if (pgn->flags & DONE_WAIT)
1447 pgn->flags |= DONE_WAIT;
1449 fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
1451 if (pgn->type & OP_DOUBLEDEP)
1452 Lst_PrependAll(examine, pgn->cohorts);
1454 owln = Lst_First(pgn->children);
1455 Lst_Open(pgn->children);
1456 for (; (ln = Lst_Next(pgn->children)) != NULL; ) {
1457 cgn = LstNode_Datum(ln);
1458 if (cgn->type & OP_WAIT) {
1459 /* Make the .WAIT node depend on the previous children */
1460 Lst_ForEachFrom(pgn->children, owln, add_wait_dep, cgn);
1463 Lst_Append(examine, cgn);
1466 Lst_Close(pgn->children);
1473 *-----------------------------------------------------------------------
1475 * Initialize the nodes to remake and the list of nodes which are
1476 * ready to be made by doing a breadth-first traversal of the graph
1477 * starting from the nodes in the given list. Once this traversal
1478 * is finished, all the 'leaves' of the graph are in the toBeMade
1480 * Using this queue and the Job module, work back up the graph,
1481 * calling on MakeStartJobs to keep the job table as full as
1485 * targs the initial list of targets
1488 * TRUE if work was done. FALSE otherwise.
1491 * The make field of all nodes involved in the creation of the given
1492 * targets is set to 1. The toBeMade list is set to contain all the
1493 * 'leaves' of these subgraphs.
1494 *-----------------------------------------------------------------------
1499 int errors; /* Number of errors the Job module reports */
1501 /* Start trying to make the current targets... */
1502 toBeMade = Lst_Init();
1504 Make_ExpandUse(targs);
1505 Make_ProcessWait(targs);
1508 fprintf(debug_file, "#***# full graph\n");
1514 * We wouldn't do any work unless we could start some jobs in the
1515 * next loop... (we won't actually start any, of course, this is just
1516 * to see if any of the targets was out of date)
1518 return MakeStartJobs();
1521 * Initialization. At the moment, no jobs are running and until some
1522 * get started, nothing will happen since the remaining upward
1523 * traversal of the graph is performed by the routines in job.c upon
1524 * the finishing of a job. So we fill the Job table as much as we can
1525 * before going into our loop.
1527 (void)MakeStartJobs();
1530 * Main Loop: The idea here is that the ending of jobs will take
1531 * care of the maintenance of data structures and the waiting for output
1532 * will cause us to be idle most of the time while our children run as
1533 * much as possible. Because the job table is kept as full as possible,
1534 * the only time when it will be empty is when all the jobs which need
1535 * running have been run, so that is the end condition of this loop.
1536 * Note that the Job module will exit if there were any errors unless the
1537 * keepgoing flag was given.
1539 while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1541 (void)MakeStartJobs();
1544 errors = Job_Finish();
1547 * Print the final status of each target. E.g. if it wasn't made
1548 * because some inferior reported an error.
1551 fprintf(debug_file, "done: errors %d\n", errors);
1553 Lst_ForEach(targs, MakePrintStatus, &errors);
1555 fprintf(debug_file, "done: errors %d\n", errors);