1 /* $NetBSD: make.c,v 1.186 2020/11/01 17:47:26 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
73 * The functions which perform the examination of targets and
74 * their suitability for creation
77 * Make_Run Initialize things for the module and recreate
78 * whatever needs recreating. Returns TRUE if
79 * work was (or would have been) done and FALSE
82 * Make_Update Update all parents of a given child. Performs
83 * various bookkeeping chores like the updating
84 * of the youngestChild field of the parent, filling
85 * of the IMPSRC context variable, etc. It will
86 * place the parent on the toBeMade queue if it
89 * Make_TimeStamp Function to set the parent's youngestChild field
90 * based on a child's modification time.
92 * Make_DoAllVar Set up the various local variables for a
93 * target, including the .ALLSRC variable, making
94 * sure that any variable that needs to exist
95 * at the very least has the empty value.
97 * Make_OODate Determine if a target is out-of-date.
99 * Make_HandleUse See if a child is a .USE node for a parent
100 * and perform the .USE actions if so.
102 * Make_ExpandUse Expand .USE nodes
109 /* "@(#)make.c 8.1 (Berkeley) 6/6/93" */
110 MAKE_RCSID("$NetBSD: make.c,v 1.186 2020/11/01 17:47:26 rillig Exp $");
112 /* Sequence # to detect recursion. */
113 static unsigned int checked = 1;
115 /* The current fringe of the graph.
116 * These are nodes which await examination by MakeOODate.
117 * It is added to by Make_Update and subtracted from by MakeStartJobs */
118 static GNodeList *toBeMade;
120 static int MakeCheckOrder(void *, void *);
121 static int MakeBuildParent(void *, void *);
124 debug_printf(const char *fmt, ...)
129 vfprintf(opts.debug_file, fmt, args);
133 MAKE_ATTR_DEAD static void
134 make_abort(GNode *gn, int line)
136 debug_printf("make_abort from line %d\n", line);
137 Targ_PrintNode(gn, 2);
138 Targ_PrintNodes(toBeMade, 2);
143 ENUM_VALUE_RTTI_8(GNodeMade,
144 UNMADE, DEFERRED, REQUESTED, BEINGMADE,
145 MADE, UPTODATE, ERROR, ABORTED);
147 ENUM_FLAGS_RTTI_31(GNodeType,
148 OP_DEPENDS, OP_FORCE, OP_DOUBLEDEP,
149 /* OP_OPMASK is omitted since it combines other flags */
150 OP_OPTIONAL, OP_USE, OP_EXEC, OP_IGNORE,
151 OP_PRECIOUS, OP_SILENT, OP_MAKE, OP_JOIN,
152 OP_MADE, OP_SPECIAL, OP_USEBEFORE, OP_INVISIBLE,
153 OP_NOTMAIN, OP_PHONY, OP_NOPATH, OP_WAIT,
154 OP_NOMETA, OP_META, OP_NOMETA_CMP, OP_SUBMAKE,
155 OP_TRANSFORM, OP_MEMBER, OP_LIB, OP_ARCHV,
156 OP_HAS_COMMANDS, OP_SAVE_CMDS, OP_DEPS_FOUND, OP_MARK);
158 ENUM_FLAGS_RTTI_10(GNodeFlags,
159 REMAKE, CHILDMADE, FORCE, DONE_WAIT,
160 DONE_ORDER, FROM_DEPEND, DONE_ALLSRC, CYCLE,
161 DONECYCLE, INTERNAL);
164 GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
167 char type_buf[GNodeType_ToStringSize];
168 char flags_buf[GNodeFlags_ToStringSize];
170 fprintf(f, "%smade %s, type %s, flags %s%s",
172 Enum_ValueToString(gn->made, GNodeMade_ToStringSpecs),
173 Enum_FlagsToString(type_buf, sizeof type_buf,
174 gn->type, GNodeType_ToStringSpecs),
175 Enum_FlagsToString(flags_buf, sizeof flags_buf,
176 gn->flags, GNodeFlags_ToStringSpecs),
181 GNode_ShouldExecute(GNode *gn)
183 return !((gn->type & OP_MAKE) ? opts.noRecursiveExecute : opts.noExecute);
186 /* Update the youngest child of the node, according to the given child. */
188 Make_TimeStamp(GNode *pgn, GNode *cgn)
190 if (pgn->youngestChild == NULL || cgn->mtime > pgn->youngestChild->mtime) {
191 pgn->youngestChild = cgn;
195 /* See if the 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.
203 * The mtime field of the node and the youngestChild field of its parents
207 Make_OODate(GNode *gn)
212 * Certain types of targets needn't even be sought as their datedness
213 * doesn't depend on their modification time...
215 if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
216 (void)Dir_MTime(gn, 1);
218 if (gn->mtime != 0) {
219 debug_printf("modified %s...", Targ_FmtTime(gn->mtime));
221 debug_printf("non-existent...");
227 * A target is remade in one of the following circumstances:
228 * its modification time is smaller than that of its youngest child and
229 * it would actually be run (has commands or is not GNode_IsTarget)
230 * it's the object of a force operator
231 * it has no children, was on the lhs of an operator and doesn't exist
234 * Libraries are only considered out-of-date if the archive module says
237 * These weird rules are brought to you by Backward-Compatibility and
238 * the strange people who wrote 'Make'.
240 if (gn->type & (OP_USE|OP_USEBEFORE)) {
242 * If the node is a USE node it is *never* out of date
245 DEBUG0(MAKE, ".USE node...");
247 } else if ((gn->type & OP_LIB) &&
248 ((gn->mtime==0) || Arch_IsLib(gn))) {
249 DEBUG0(MAKE, "library...");
252 * always out of date if no children and :: target
255 oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
256 (gn->youngestChild == NULL && (gn->type & OP_DOUBLEDEP)));
257 } else if (gn->type & OP_JOIN) {
259 * A target with the .JOIN attribute is only considered
260 * out-of-date if any of its children was out-of-date.
262 DEBUG0(MAKE, ".JOIN node...");
263 DEBUG1(MAKE, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
264 oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
265 } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
267 * A node which is the object of the force (!) operator or which has
268 * the .EXEC attribute is always considered out-of-date.
271 if (gn->type & OP_FORCE) {
272 debug_printf("! operator...");
273 } else if (gn->type & OP_PHONY) {
274 debug_printf(".PHONY node...");
276 debug_printf(".EXEC node...");
280 } else if ((gn->youngestChild != NULL &&
281 gn->mtime < gn->youngestChild->mtime) ||
282 (gn->youngestChild == NULL &&
283 ((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
284 || gn->type & OP_DOUBLEDEP)))
287 * A node whose modification time is less than that of its
288 * youngest child or that has no children (youngestChild == NULL) and
289 * either doesn't exist (mtime == 0) and it isn't optional
290 * or was the object of a * :: operator is out-of-date.
291 * Why? Because that's the way Make does it.
294 if (gn->youngestChild != NULL &&
295 gn->mtime < gn->youngestChild->mtime) {
296 debug_printf("modified before source %s...",
297 GNode_Path(gn->youngestChild));
298 } else if (gn->mtime == 0) {
299 debug_printf("non-existent and no sources...");
301 debug_printf(":: operator and no sources...");
307 * When a non-existing child with no sources
308 * (such as a typically used FORCE source) has been made and
309 * the target of the child (usually a directory) has the same
310 * timestamp as the timestamp just given to the non-existing child
311 * after it was considered made.
314 if (gn->flags & FORCE)
315 debug_printf("non existing child...");
317 oodate = (gn->flags & FORCE) ? TRUE : FALSE;
322 oodate = meta_oodate(gn, oodate);
327 * If the target isn't out-of-date, the parents need to know its
328 * modification time. Note that targets that appear to be out-of-date
329 * but aren't, because they have no commands and are GNode_IsTarget,
330 * have their mtime stay below their children's mtime to keep parents from
331 * thinking they're out-of-date.
335 for (ln = gn->parents->first; ln != NULL; ln = ln->next)
336 Make_TimeStamp(ln->datum, gn);
342 /* Add the node to the list if it needs to be examined. */
344 MakeAddChild(void *gnp, void *lp)
349 if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
350 DEBUG2(MAKE, "MakeAddChild: need to examine %s%s\n",
351 gn->name, gn->cohort_num);
357 /* Find the pathname of a child that was already made.
359 * The path and mtime of the node and the youngestChild of the parent are
360 * updated; the unmade children count of the parent is decremented.
363 * gnp the node to find
366 MakeFindChild(void *gnp, void *pgnp)
371 (void)Dir_MTime(gn, 0);
372 Make_TimeStamp(pgn, gn);
378 /* Called by Make_Run and SuffApplyTransform on the downward pass to handle
379 * .USE and transformation nodes, by copying the child node's commands, type
380 * flags and children to the parent node.
382 * A .USE node is much like an explicit transformation rule, except its
383 * commands are always added to the target node, even if the target already
387 * cgn The source node, which is either a .USE/.USEBEFORE
388 * node or a transformation node (OP_TRANSFORM).
389 * pgn The target node
392 Make_HandleUse(GNode *cgn, GNode *pgn)
394 GNodeListNode *ln; /* An element in the children list */
397 if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
398 debug_printf("Make_HandleUse: called for plain node %s\n", cgn->name);
403 if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
404 if (cgn->type & OP_USEBEFORE) {
406 Lst_PrependAll(pgn->commands, cgn->commands);
408 /* .USE, or target has no commands */
409 Lst_AppendAll(pgn->commands, cgn->commands);
413 for (ln = cgn->children->first; ln != NULL; ln = ln->next) {
414 GNode *gn = ln->datum;
417 * Expand variables in the .USE node's name
418 * and save the unexpanded form.
419 * We don't need to do this for commands.
420 * They get expanded properly when we execute.
422 if (gn->uname == NULL) {
423 gn->uname = gn->name;
427 (void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
428 /* TODO: handle errors */
429 if (gn->uname && strcmp(gn->name, gn->uname) != 0) {
430 /* See if we have a target for this node. */
431 GNode *tgn = Targ_FindNode(gn->name);
436 Lst_Append(pgn->children, gn);
437 Lst_Append(gn->parents, pgn);
441 pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
444 /* Used by Make_Run on the downward pass to handle .USE nodes. Should be
445 * called before the children are enqueued to be looked at by MakeAddChild.
447 * For a .USE child, the commands, type flags and children are copied to the
448 * parent node, and since the relation to the .USE node is then no longer
449 * needed, that relation is removed.
452 * cgn the child, which may be a .USE node
453 * pgn the current parent
456 MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
460 unmarked = ((cgn->type & OP_MARK) == 0);
461 cgn->type |= OP_MARK;
463 if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
467 Make_HandleUse(cgn, pgn);
470 * This child node is now "made", so we decrement the count of
471 * unmade children in the parent... We also remove the child
472 * from the parent's list to accurately reflect the number of decent
473 * children the parent has. This is used by Make_Run to decide
474 * whether to queue the parent or examine its children...
476 Lst_Remove(pgn->children, ln);
481 HandleUseNodes(GNode *gn)
483 GNodeListNode *ln, *nln;
484 for (ln = gn->children->first; ln != NULL; ln = nln) {
486 MakeHandleUse(ln->datum, gn, ln);
491 /* Check the modification time of a gnode, and update it if necessary.
492 * Return 0 if the gnode does not exist, or its filesystem time if it does. */
494 Make_Recheck(GNode *gn)
496 time_t mtime = Dir_MTime(gn, 1);
500 * We can't re-stat the thing, but we can at least take care of rules
501 * where a target depends on a source that actually creates the
502 * target, but only if it has changed, e.g.
510 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
512 * In this case, if the definitions produced by yacc haven't changed
513 * from before, parse.h won't have been updated and gn->mtime will
514 * reflect the current modification time for parse.h. This is
515 * something of a kludge, I admit, but it's a useful one..
516 * XXX: People like to use a rule like
520 * To force things that depend on FRC to be made, so we have to
521 * check for gn->children being empty as well...
523 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
528 * This is what Make does and it's actually a good thing, as it
531 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
533 * to function as intended. Unfortunately, thanks to the stateless
534 * nature of NFS (by which I mean the loose coupling of two clients
535 * using the same file from a common server), there are times
536 * when the modification time of a file created on a remote
537 * machine will not be modified before the local stat() implied by
538 * the Dir_MTime occurs, thus leading us to believe that the file
539 * is unchanged, wreaking havoc with files that depend on this one.
541 * I have decided it is better to make too much than to make too
542 * little, so this stuff is commented out unless you're sure it's ok.
546 * Christos, 4/9/92: If we are saving commands pretend that
547 * the target is made now. Otherwise archives with ... rules
550 if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
551 (mtime == 0 && !(gn->type & OP_WAIT))) {
552 DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
553 gn->name, Targ_FmtTime(gn->mtime));
557 DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
558 gn->name, Targ_FmtTime(gn->mtime));
565 * Set the .PREFIX and .IMPSRC variables for all the implied parents
569 UpdateImplicitParentsVars(GNode *cgn, const char *cname)
572 const char *cpref = GNode_VarPrefix(cgn);
574 for (ln = cgn->implicitParents->first; ln != NULL; ln = ln->next) {
575 GNode *pgn = ln->datum;
576 if (pgn->flags & REMAKE) {
577 Var_Set(IMPSRC, cname, pgn);
579 Var_Set(PREFIX, cpref, pgn);
584 /* Perform update on the parents of a node. Used by JobFinish once
585 * a node has been dealt with and by MakeStartJobs if it finds an
588 * The unmade field of pgn is decremented and pgn may be placed on
589 * the toBeMade queue if this field becomes 0.
591 * If the child was made, the parent's flag CHILDMADE field will be
594 * If the child is not up-to-date and still does not exist,
595 * set the FORCE flag on the parents.
597 * If the child wasn't made, the youngestChild field of the parent will be
598 * altered if the child's mtime is big enough.
600 * Finally, if the child is the implied source for the parent, the
601 * parent's IMPSRC variable is set appropriately.
604 Make_Update(GNode *cgn)
606 const char *cname; /* the child's name */
612 /* It is save to re-examine any nodes again */
615 cname = GNode_VarTarget(cgn);
617 DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
620 * If the child was actually made, see what its modification time is
621 * now -- some rules won't actually update the file. If the file still
622 * doesn't exist, make its mtime now.
624 if (cgn->made != UPTODATE) {
625 mtime = Make_Recheck(cgn);
629 * If this is a `::' node, we must consult its first instance
630 * which is where all parents are linked.
632 if ((centurion = cgn->centurion) != NULL) {
633 if (!Lst_IsEmpty(cgn->parents))
634 Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
635 centurion->unmade_cohorts--;
636 if (centurion->unmade_cohorts < 0)
637 Error("Graph cycles through centurion %s", centurion->name);
641 parents = centurion->parents;
643 /* If this was a .ORDER node, schedule the RHS */
644 Lst_ForEachUntil(centurion->order_succ, MakeBuildParent, toBeMade->first);
646 /* Now mark all the parents as having one less unmade child */
647 for (ln = parents->first; ln != NULL; ln = ln->next) {
648 GNode *pgn = ln->datum;
651 debug_printf("inspect parent %s%s: flags %x, "
652 "type %x, made %d, unmade %d ",
653 pgn->name, pgn->cohort_num, pgn->flags,
654 pgn->type, pgn->made, pgn->unmade - 1);
656 if (!(pgn->flags & REMAKE)) {
657 /* This parent isn't needed */
658 DEBUG0(MAKE, "- not needed\n");
661 if (mtime == 0 && !(cgn->type & OP_WAIT))
665 * If the parent has the .MADE attribute, its timestamp got
666 * updated to that of its newest child, and its unmade
667 * child count got set to zero in Make_ExpandUse().
668 * However other things might cause us to build one of its
669 * children - and so we mustn't do any processing here when
670 * the child build finishes.
672 if (pgn->type & OP_MADE) {
673 DEBUG0(MAKE, "- .MADE\n");
677 if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
678 if (cgn->made == MADE)
679 pgn->flags |= CHILDMADE;
680 (void)Make_TimeStamp(pgn, cgn);
684 * A parent must wait for the completion of all instances
685 * of a `::' dependency.
687 if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
688 DEBUG2(MAKE, "- centurion made %d, %d unmade cohorts\n",
689 centurion->made, centurion->unmade_cohorts);
693 /* One more child of this parent is now made */
695 if (pgn->unmade < 0) {
697 debug_printf("Graph cycles through %s%s\n",
698 pgn->name, pgn->cohort_num);
701 Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
704 /* We must always rescan the parents of .WAIT and .ORDER nodes. */
705 if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
706 && !(centurion->flags & DONE_ORDER)) {
707 DEBUG0(MAKE, "- unmade children\n");
710 if (pgn->made != DEFERRED) {
712 * Either this parent is on a different branch of the tree,
713 * or it on the RHS of a .WAIT directive
714 * or it is already on the toBeMade list.
716 DEBUG0(MAKE, "- not deferred\n");
719 assert(pgn->order_pred != NULL);
720 if (Lst_ForEachUntil(pgn->order_pred, MakeCheckOrder, 0)) {
721 /* A .ORDER rule stops us building this */
725 debug_printf("- %s%s made, schedule %s%s (made %d)\n",
726 cgn->name, cgn->cohort_num,
727 pgn->name, pgn->cohort_num, pgn->made);
728 Targ_PrintNode(pgn, 2);
730 /* Ok, we can schedule the parent again */
731 pgn->made = REQUESTED;
732 Lst_Enqueue(toBeMade, pgn);
735 UpdateImplicitParentsVars(cgn, cname);
739 UnmarkChildren(GNode *gn)
743 for (ln = gn->children->first; ln != NULL; ln = ln->next) {
744 GNode *child = ln->datum;
745 child->type &= ~OP_MARK;
749 /* Add a child's name to the ALLSRC and OODATE variables of the given
750 * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
751 * attributes. .EXEC and .USE children are very rarely going to be files,
754 * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
756 * A child is added to the OODATE variable if its modification time is
757 * later than that of its parent, as defined by Make, except if the
758 * parent is a .JOIN node. In that case, it is only added to the OODATE
759 * variable if it was actually made (since .JOIN nodes don't have
760 * modification times, the comparison is rather unfair...)..
763 * cgn The child to add
764 * pgn The parent to whose ALLSRC variable it should
768 MakeAddAllSrc(GNode *cgn, GNode *pgn)
770 if (cgn->type & OP_MARK)
772 cgn->type |= OP_MARK;
774 if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
775 const char *child, *allsrc;
777 if (cgn->type & OP_ARCHV)
778 child = GNode_VarMember(cgn);
780 child = GNode_Path(cgn);
781 if (cgn->type & OP_JOIN) {
782 allsrc = GNode_VarAllsrc(cgn);
787 Var_Append(ALLSRC, allsrc, pgn);
788 if (pgn->type & OP_JOIN) {
789 if (cgn->made == MADE) {
790 Var_Append(OODATE, child, pgn);
792 } else if ((pgn->mtime < cgn->mtime) ||
793 (cgn->mtime >= now && cgn->made == MADE))
796 * It goes in the OODATE variable if the parent is younger than the
797 * child or if the child has been modified more recently than
798 * the start of the make. This is to keep pmake from getting
799 * confused if something else updates the parent after the
800 * make starts (shouldn't happen, I know, but sometimes it
801 * does). In such a case, if we've updated the kid, the parent
802 * is likely to have a modification time later than that of
803 * the kid and anything that relies on the OODATE variable will
806 * XXX: This will cause all made children to go in the OODATE
807 * variable, even if they're not touched, if RECHECK isn't defined,
808 * since cgn->mtime is set to now in Make_Update. According to
809 * some people, this is good...
811 Var_Append(OODATE, child, pgn);
816 /* Set up the ALLSRC and OODATE variables. Sad to say, it must be
817 * done separately, rather than while traversing the graph. This is
818 * because Make defined OODATE to contain all sources whose modification
819 * times were later than that of the target, *not* those sources that
820 * were out-of-date. Since in both compatibility and native modes,
821 * the modification time of the parent isn't found until the child
822 * has been dealt with, we have to wait until now to fill in the
823 * variable. As for ALLSRC, the ordering is important and not
824 * guaranteed when in native mode, so it must be set here, too.
826 * If the node is a .JOIN node, its TARGET variable will be set to
827 * match its ALLSRC variable.
830 Make_DoAllVar(GNode *gn)
834 if (gn->flags & DONE_ALLSRC)
838 for (ln = gn->children->first; ln != NULL; ln = ln->next)
839 MakeAddAllSrc(ln->datum, gn);
841 if (!Var_Exists(OODATE, gn)) {
842 Var_Set(OODATE, "", gn);
844 if (!Var_Exists(ALLSRC, gn)) {
845 Var_Set(ALLSRC, "", gn);
848 if (gn->type & OP_JOIN)
849 Var_Set(TARGET, GNode_VarAllsrc(gn), gn);
850 gn->flags |= DONE_ALLSRC;
854 MakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
858 if (bn->made >= MADE || !(bn->flags & REMAKE))
861 DEBUG2(MAKE, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
862 bn->name, bn->cohort_num);
867 MakeBuildChild(void *v_cn, void *toBeMade_next)
871 DEBUG4(MAKE, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
872 cn->name, cn->cohort_num, cn->made, cn->type);
873 if (cn->made > DEFERRED)
876 /* If this node is on the RHS of a .ORDER, check LHSs. */
877 assert(cn->order_pred);
878 if (Lst_ForEachUntil(cn->order_pred, MakeCheckOrder, 0)) {
879 /* Can't build this (or anything else in this child list) yet */
881 return 0; /* but keep looking */
884 DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n", cn->name, cn->cohort_num);
886 cn->made = REQUESTED;
887 if (toBeMade_next == NULL)
888 Lst_Append(toBeMade, cn);
890 Lst_InsertBefore(toBeMade, toBeMade_next, cn);
892 if (cn->unmade_cohorts != 0)
893 Lst_ForEachUntil(cn->cohorts, MakeBuildChild, toBeMade_next);
896 * If this node is a .WAIT node with unmade children
897 * then don't add the next sibling.
899 return cn->type & OP_WAIT && cn->unmade > 0;
902 /* When a .ORDER LHS node completes we do this on each RHS */
904 MakeBuildParent(void *v_pn, void *toBeMade_next)
908 if (pn->made != DEFERRED)
911 if (MakeBuildChild(pn, toBeMade_next) == 0) {
912 /* Mark so that when this node is built we reschedule its parents */
913 pn->flags |= DONE_ORDER;
919 /* Start as many jobs as possible, taking them from the toBeMade queue.
921 * If the query flag was given to pmake, no job will be started,
922 * but as soon as an out-of-date target is found, this function
923 * returns TRUE. At all other times, this function returns FALSE.
931 while (!Lst_IsEmpty(toBeMade)) {
932 /* Get token now to avoid cycling job-list when we only have 1 token */
933 if (!have_token && !Job_TokenWithdraw())
937 gn = Lst_Dequeue(toBeMade);
938 DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
940 if (gn->made != REQUESTED) {
941 DEBUG1(MAKE, "state %d\n", gn->made);
943 make_abort(gn, __LINE__);
946 if (gn->checked_seqno == checked) {
947 /* We've already looked at this node since a job finished... */
948 DEBUG2(MAKE, "already checked %s%s\n", gn->name, gn->cohort_num);
952 gn->checked_seqno = checked;
954 if (gn->unmade != 0) {
956 * We can't build this yet, add all unmade children to toBeMade,
957 * just before the current first element.
960 Lst_ForEachUntil(gn->children, MakeBuildChild, toBeMade->first);
961 /* and drop this node on the floor */
962 DEBUG2(MAKE, "dropped %s%s\n", gn->name, gn->cohort_num);
966 gn->made = BEINGMADE;
967 if (Make_OODate(gn)) {
968 DEBUG0(MAKE, "out-of-date\n");
969 if (opts.queryFlag) {
976 DEBUG0(MAKE, "up-to-date\n");
978 if (gn->type & OP_JOIN) {
980 * Even for an up-to-date .JOIN node, we need it to have its
981 * context variables so references to it get the correct
982 * value for .TARGET when building up the context variables
983 * of its parent(s)...
998 MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
1000 if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
1001 /* not waiting for this one */
1004 printf(" `%s%s' has .ORDER dependency against %s%s ",
1005 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1006 GNode_FprintDetails(stdout, "(", ogn, ")\n");
1008 if (DEBUG(MAKE) && opts.debug_file != stdout) {
1009 debug_printf(" `%s%s' has .ORDER dependency against %s%s ",
1010 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1011 GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
1016 MakePrintStatusOrder(GNode *gn)
1019 for (ln = gn->order_pred->first; ln != NULL; ln = ln->next)
1020 MakePrintStatusOrderNode(ln->datum, gn);
1023 static void MakePrintStatusList(GNodeList *, int *);
1025 /* Print the status of a top-level node, viz. it being up-to-date already
1026 * or not created due to an error in a lower level.
1027 * Callback function for Make_Run via Lst_ForEachUntil.
1030 MakePrintStatus(GNode *gn, int *errors)
1032 if (gn->flags & DONECYCLE)
1033 /* We've completely processed this node before, don't do it again. */
1036 if (gn->unmade == 0) {
1037 gn->flags |= DONECYCLE;
1040 printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1049 printf("`%s%s' was not built", gn->name, gn->cohort_num);
1050 GNode_FprintDetails(stdout, " (", gn, ")!\n");
1051 if (DEBUG(MAKE) && opts.debug_file != stdout) {
1052 debug_printf("`%s%s' was not built", gn->name, gn->cohort_num);
1053 GNode_FprintDetails(opts.debug_file, " (", gn, ")!\n");
1055 /* Most likely problem is actually caused by .ORDER */
1056 MakePrintStatusOrder(gn);
1059 /* Errors - already counted */
1060 printf("`%s%s' not remade because of errors.\n",
1061 gn->name, gn->cohort_num);
1062 if (DEBUG(MAKE) && opts.debug_file != stdout)
1063 debug_printf("`%s%s' not remade because of errors.\n",
1064 gn->name, gn->cohort_num);
1070 DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
1071 gn->name, gn->cohort_num, gn->unmade);
1073 * If printing cycles and came to one that has unmade children,
1074 * print out the cycle by recursing on its children.
1076 if (!(gn->flags & CYCLE)) {
1077 /* Fist time we've seen this node, check all children */
1079 MakePrintStatusList(gn->children, errors);
1080 /* Mark that this node needn't be processed again */
1081 gn->flags |= DONECYCLE;
1085 /* Only output the error once per node */
1086 gn->flags |= DONECYCLE;
1087 Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1088 if ((*errors)++ > 100)
1089 /* Abandon the whole error report */
1092 /* Reporting for our children will give the rest of the loop */
1093 MakePrintStatusList(gn->children, errors);
1098 MakePrintStatusList(GNodeList *gnodes, int *errors)
1101 for (ln = gnodes->first; ln != NULL; ln = ln->next)
1102 if (MakePrintStatus(ln->datum, errors))
1106 /* Expand .USE nodes and create a new targets list.
1109 * targs the initial list of targets
1112 Make_ExpandUse(GNodeList *targs)
1114 GNodeList *examine; /* List of targets to examine */
1117 /* XXX: Why is it necessary to copy the list? There shouldn't be
1118 * any modifications to the list, at least the function name
1119 * ExpandUse doesn't suggest that. */
1121 examine = Lst_New();
1122 for (ln = targs->first; ln != NULL; ln = ln->next)
1123 Lst_Append(examine, ln->datum);
1127 * Make an initial downward pass over the graph, marking nodes to be made
1128 * as we go down. We call Suff_FindDeps to find where a node is and
1129 * to get some children for it if it has none and also has no commands.
1130 * If the node is a leaf, we stick it on the toBeMade queue to
1131 * be looked at in a minute, otherwise we add its children to our queue
1132 * and go on about our business.
1134 while (!Lst_IsEmpty(examine)) {
1135 GNode *gn = Lst_Dequeue(examine);
1137 if (gn->flags & REMAKE)
1138 /* We've looked at this one already */
1140 gn->flags |= REMAKE;
1141 DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
1142 gn->name, gn->cohort_num);
1144 if (gn->type & OP_DOUBLEDEP)
1145 Lst_PrependAll(examine, gn->cohorts);
1148 * Apply any .USE rules before looking for implicit dependencies
1149 * to make sure everything has commands that should...
1150 * Make sure that the TARGET is set, so that we can make
1153 if (gn->type & OP_ARCHV) {
1155 eoa = strchr(gn->name, '(');
1156 eon = strchr(gn->name, ')');
1157 if (eoa == NULL || eon == NULL)
1161 Var_Set(MEMBER, eoa + 1, gn);
1162 Var_Set(ARCHIVE, gn->name, gn);
1167 (void)Dir_MTime(gn, 0);
1168 Var_Set(TARGET, GNode_Path(gn), gn);
1172 if ((gn->type & OP_MADE) == 0)
1175 /* Pretend we made all this node's children */
1176 Lst_ForEachUntil(gn->children, MakeFindChild, gn);
1177 if (gn->unmade != 0)
1178 printf("Warning: %s%s still has %d unmade children\n",
1179 gn->name, gn->cohort_num, gn->unmade);
1182 if (gn->unmade != 0)
1183 Lst_ForEachUntil(gn->children, MakeAddChild, examine);
1189 /* Make the .WAIT node depend on the previous children */
1191 add_wait_dependency(GNodeListNode *owln, GNode *wn)
1196 for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1197 DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
1198 cn->name, cn->cohort_num, wn->name);
1200 /* XXX: This pattern should be factored out, it repeats often */
1201 Lst_Append(wn->children, cn);
1203 Lst_Append(cn->parents, wn);
1207 /* Convert .WAIT nodes into dependencies. */
1209 Make_ProcessWait(GNodeList *targs)
1211 GNode *pgn; /* 'parent' node we are examining */
1212 GNodeListNode *owln; /* Previous .WAIT node */
1213 GNodeList *examine; /* List of targets to examine */
1216 * We need all the nodes to have a common parent in order for the
1217 * .WAIT and .ORDER scheduling to work.
1218 * Perhaps this should be done earlier...
1221 pgn = Targ_NewGN(".MAIN");
1222 pgn->flags = REMAKE;
1223 pgn->type = OP_PHONY | OP_DEPENDS;
1224 /* Get it displayed in the diag dumps */
1225 Lst_Prepend(Targ_List(), pgn);
1229 for (ln = targs->first; ln != NULL; ln = ln->next) {
1230 GNode *cgn = ln->datum;
1232 Lst_Append(pgn->children, cgn);
1233 Lst_Append(cgn->parents, pgn);
1238 /* Start building with the 'dummy' .MAIN' node */
1239 MakeBuildChild(pgn, NULL);
1241 examine = Lst_New();
1242 Lst_Append(examine, pgn);
1244 while (!Lst_IsEmpty(examine)) {
1247 pgn = Lst_Dequeue(examine);
1249 /* We only want to process each child-list once */
1250 if (pgn->flags & DONE_WAIT)
1252 pgn->flags |= DONE_WAIT;
1253 DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
1255 if (pgn->type & OP_DOUBLEDEP)
1256 Lst_PrependAll(examine, pgn->cohorts);
1258 owln = pgn->children->first;
1259 for (ln = pgn->children->first; ln != NULL; ln = ln->next) {
1260 GNode *cgn = ln->datum;
1261 if (cgn->type & OP_WAIT) {
1262 add_wait_dependency(owln, cgn);
1265 Lst_Append(examine, cgn);
1274 *-----------------------------------------------------------------------
1276 * Initialize the nodes to remake and the list of nodes which are
1277 * ready to be made by doing a breadth-first traversal of the graph
1278 * starting from the nodes in the given list. Once this traversal
1279 * is finished, all the 'leaves' of the graph are in the toBeMade
1281 * Using this queue and the Job module, work back up the graph,
1282 * calling on MakeStartJobs to keep the job table as full as
1286 * targs the initial list of targets
1289 * TRUE if work was done. FALSE otherwise.
1292 * The make field of all nodes involved in the creation of the given
1293 * targets is set to 1. The toBeMade list is set to contain all the
1294 * 'leaves' of these subgraphs.
1295 *-----------------------------------------------------------------------
1298 Make_Run(GNodeList *targs)
1300 int errors; /* Number of errors the Job module reports */
1302 /* Start trying to make the current targets... */
1303 toBeMade = Lst_New();
1305 Make_ExpandUse(targs);
1306 Make_ProcessWait(targs);
1309 debug_printf("#***# full graph\n");
1313 if (opts.queryFlag) {
1315 * We wouldn't do any work unless we could start some jobs in the
1316 * next loop... (we won't actually start any, of course, this is just
1317 * to see if any of the targets was out of date)
1319 return MakeStartJobs();
1322 * Initialization. At the moment, no jobs are running and until some
1323 * get started, nothing will happen since the remaining upward
1324 * traversal of the graph is performed by the routines in job.c upon
1325 * the finishing of a job. So we fill the Job table as much as we can
1326 * before going into our loop.
1328 (void)MakeStartJobs();
1331 * Main Loop: The idea here is that the ending of jobs will take
1332 * care of the maintenance of data structures and the waiting for output
1333 * will cause us to be idle most of the time while our children run as
1334 * much as possible. Because the job table is kept as full as possible,
1335 * the only time when it will be empty is when all the jobs which need
1336 * running have been run, so that is the end condition of this loop.
1337 * Note that the Job module will exit if there were any errors unless the
1338 * keepgoing flag was given.
1340 while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1342 (void)MakeStartJobs();
1345 errors = Job_Finish();
1348 * Print the final status of each target. E.g. if it wasn't made
1349 * because some inferior reported an error.
1351 DEBUG1(MAKE, "done: errors %d\n", errors);
1353 MakePrintStatusList(targs, &errors);
1355 debug_printf("done: errors %d\n", errors);