1 /* $NetBSD: make.c,v 1.258 2022/12/05 23:28:08 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 * Examination of targets and their suitability for creation.
75 * Make_Run Initialize things for the module. Returns true if
76 * work was (or would have been) done.
78 * Make_Update After a target is made, update all its parents.
79 * Perform various bookkeeping chores like the updating
80 * of the youngestChild field of the parent, filling
81 * of the IMPSRC variable, etc. Place the parent on the
82 * toBeMade queue if it should be.
84 * GNode_UpdateYoungestChild
85 * Update the node's youngestChild field based on the
86 * child's modification time.
89 * Set up the various local variables for a
90 * target, including the .ALLSRC variable, making
91 * sure that any variable that needs to exist
92 * at the very least has the empty value.
94 * GNode_IsOODate Determine if a target is out-of-date.
96 * Make_HandleUse See if a child is a .USE node for a parent
97 * and perform the .USE actions if so.
99 * Make_ExpandUse Expand .USE nodes
106 /* "@(#)make.c 8.1 (Berkeley) 6/6/93" */
107 MAKE_RCSID("$NetBSD: make.c,v 1.258 2022/12/05 23:28:08 rillig Exp $");
109 /* Sequence # to detect recursion. */
110 static unsigned int checked_seqno = 1;
113 * The current fringe of the graph.
114 * These are nodes which await examination by MakeOODate.
115 * It is added to by Make_Update and subtracted from by MakeStartJobs
117 static GNodeList toBeMade = LST_INIT;
121 debug_printf(const char *fmt, ...)
126 vfprintf(opts.debug_file, fmt, ap);
131 GNodeType_ToString(GNodeType type, void **freeIt)
135 Buf_InitSize(&buf, 32);
136 #define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != OP_NONE, #flag)
164 ADD(OP_HAS_COMMANDS);
169 return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
173 GNodeFlags_ToString(GNodeFlags flags, void **freeIt)
177 Buf_InitSize(&buf, 32);
178 Buf_AddFlag(&buf, flags.remake, "REMAKE");
179 Buf_AddFlag(&buf, flags.childMade, "CHILDMADE");
180 Buf_AddFlag(&buf, flags.force, "FORCE");
181 Buf_AddFlag(&buf, flags.doneWait, "DONE_WAIT");
182 Buf_AddFlag(&buf, flags.doneOrder, "DONE_ORDER");
183 Buf_AddFlag(&buf, flags.fromDepend, "FROM_DEPEND");
184 Buf_AddFlag(&buf, flags.doneAllsrc, "DONE_ALLSRC");
185 Buf_AddFlag(&buf, flags.cycle, "CYCLE");
186 Buf_AddFlag(&buf, flags.doneCycle, "DONECYCLE");
187 return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
191 GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
194 void *type_freeIt = NULL;
195 void *flags_freeIt = NULL;
197 fprintf(f, "%s%s, type %s, flags %s%s",
199 GNodeMade_Name(gn->made),
200 GNodeType_ToString(gn->type, &type_freeIt),
201 GNodeFlags_ToString(gn->flags, &flags_freeIt),
208 GNode_ShouldExecute(GNode *gn)
210 return !((gn->type & OP_MAKE)
211 ? opts.noRecursiveExecute
215 /* Update the youngest child of the node, according to the given child. */
217 GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
219 if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
220 gn->youngestChild = cgn;
224 IsOODateRegular(GNode *gn)
226 /* These rules are inherited from the original Make. */
228 if (gn->youngestChild != NULL) {
229 if (gn->mtime < gn->youngestChild->mtime) {
230 DEBUG1(MAKE, "modified before source \"%s\"...",
231 GNode_Path(gn->youngestChild));
237 if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
238 DEBUG0(MAKE, "nonexistent and no sources...");
242 if (gn->type & OP_DOUBLEDEP) {
243 DEBUG0(MAKE, ":: operator and no sources...");
251 * See if the node is out of date with respect to its sources.
253 * Used by Make_Run when deciding which nodes to place on the
254 * toBeMade queue initially and by Make_Update to screen out .USE and
255 * .EXEC nodes. In the latter case, however, any other sort of node
256 * must be considered out-of-date since at least one of its children
257 * will have been recreated.
259 * The mtime field of the node and the youngestChild field of its parents
263 GNode_IsOODate(GNode *gn)
268 * Certain types of targets needn't even be sought as their datedness
269 * doesn't depend on their modification time...
271 if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
272 Dir_UpdateMTime(gn, true);
275 debug_printf("modified %s...",
276 Targ_FmtTime(gn->mtime));
278 debug_printf("nonexistent...");
283 * A target is remade in one of the following circumstances:
285 * its modification time is smaller than that of its youngest
286 * child and it would actually be run (has commands or is not
289 * it's the object of a force operator
291 * it has no children, was on the lhs of an operator and doesn't
294 * Libraries are only considered out-of-date if the archive module
297 * These weird rules are brought to you by Backward-Compatibility
298 * and the strange people who wrote 'Make'.
300 if (gn->type & (OP_USE | OP_USEBEFORE)) {
302 * If the node is a USE node it is *never* out of date
305 DEBUG0(MAKE, ".USE node...");
307 } else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
308 DEBUG0(MAKE, "library...");
311 * always out of date if no children and :: target
314 oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
315 (gn->youngestChild == NULL &&
316 (gn->type & OP_DOUBLEDEP)));
317 } else if (gn->type & OP_JOIN) {
319 * A target with the .JOIN attribute is only considered
320 * out-of-date if any of its children was out-of-date.
322 DEBUG0(MAKE, ".JOIN node...");
323 DEBUG1(MAKE, "source %smade...",
324 gn->flags.childMade ? "" : "not ");
325 oodate = gn->flags.childMade;
326 } else if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
328 * A node which is the object of the force (!) operator or
329 * which has the .EXEC attribute is always considered
333 if (gn->type & OP_FORCE) {
334 debug_printf("! operator...");
335 } else if (gn->type & OP_PHONY) {
336 debug_printf(".PHONY node...");
338 debug_printf(".EXEC node...");
342 } else if (IsOODateRegular(gn)) {
346 * When a nonexistent child with no sources
347 * (such as a typically used FORCE source) has been made and
348 * the target of the child (usually a directory) has the same
349 * timestamp as the timestamp just given to the nonexistent
350 * child after it was considered made.
354 debug_printf("non existing child...");
356 oodate = gn->flags.force;
361 oodate = meta_oodate(gn, oodate);
365 * If the target isn't out-of-date, the parents need to know its
366 * modification time. Note that targets that appear to be out-of-date
367 * but aren't, because they have no commands and are GNode_IsTarget,
368 * have their mtime stay below their children's mtime to keep parents
369 * from thinking they're out-of-date.
373 for (ln = gn->parents.first; ln != NULL; ln = ln->next)
374 GNode_UpdateYoungestChild(ln->datum, gn);
381 PretendAllChildrenAreMade(GNode *pgn)
385 for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
386 GNode *cgn = ln->datum;
388 /* This may also update cgn->path. */
389 Dir_UpdateMTime(cgn, false);
390 GNode_UpdateYoungestChild(pgn, cgn);
396 * Called by Make_Run and SuffApplyTransform on the downward pass to handle
397 * .USE and transformation nodes, by copying the child node's commands, type
398 * flags and children to the parent node.
400 * A .USE node is much like an explicit transformation rule, except its
401 * commands are always added to the target node, even if the target already
405 * cgn The source node, which is either a .USE/.USEBEFORE
406 * node or a transformation node (OP_TRANSFORM).
407 * pgn The target node
410 Make_HandleUse(GNode *cgn, GNode *pgn)
412 GNodeListNode *ln; /* An element in the children list */
415 if (!(cgn->type & (OP_USE | OP_USEBEFORE | OP_TRANSFORM))) {
416 debug_printf("Make_HandleUse: called for plain node %s\n",
418 /* XXX: debug mode should not affect control flow */
423 if ((cgn->type & (OP_USE | OP_USEBEFORE)) ||
424 Lst_IsEmpty(&pgn->commands)) {
425 if (cgn->type & OP_USEBEFORE) {
427 Lst_PrependAll(&pgn->commands, &cgn->commands);
429 /* .USE, or target has no commands */
430 Lst_AppendAll(&pgn->commands, &cgn->commands);
434 for (ln = cgn->children.first; ln != NULL; ln = ln->next) {
435 GNode *gn = ln->datum;
438 * Expand variables in the .USE node's name
439 * and save the unexpanded form.
440 * We don't need to do this for commands.
441 * They get expanded properly when we execute.
443 if (gn->uname == NULL) {
444 gn->uname = gn->name;
448 (void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
449 /* TODO: handle errors */
450 if (gn->uname != NULL && strcmp(gn->name, gn->uname) != 0) {
451 /* See if we have a target for this node. */
452 GNode *tgn = Targ_FindNode(gn->name);
457 Lst_Append(&pgn->children, gn);
458 Lst_Append(&gn->parents, pgn);
463 cgn->type & (unsigned)~(OP_OPMASK | OP_USE | OP_USEBEFORE | OP_TRANSFORM);
467 * Used by Make_Run on the downward pass to handle .USE nodes. Should be
468 * called before the children are enqueued to be looked at by MakeAddChild.
470 * For a .USE child, the commands, type flags and children are copied to the
471 * parent node, and since the relation to the .USE node is then no longer
472 * needed, that relation is removed.
475 * cgn the child, which may be a .USE node
476 * pgn the current parent
479 MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
483 unmarked = !(cgn->type & OP_MARK);
484 cgn->type |= OP_MARK;
486 if (!(cgn->type & (OP_USE | OP_USEBEFORE)))
490 Make_HandleUse(cgn, pgn);
493 * This child node is now "made", so we decrement the count of
494 * unmade children in the parent... We also remove the child
495 * from the parent's list to accurately reflect the number of decent
496 * children the parent has. This is used by Make_Run to decide
497 * whether to queue the parent or examine its children...
499 Lst_Remove(&pgn->children, ln);
504 HandleUseNodes(GNode *gn)
506 GNodeListNode *ln, *nln;
507 for (ln = gn->children.first; ln != NULL; ln = nln) {
509 MakeHandleUse(ln->datum, gn, ln);
515 * Check the modification time of a gnode, and update it if necessary.
516 * Return 0 if the gnode does not exist, or its filesystem time if it does.
519 Make_Recheck(GNode *gn)
523 Dir_UpdateMTime(gn, true);
528 * We can't re-stat the thing, but we can at least take care of rules
529 * where a target depends on a source that actually creates the
530 * target, but only if it has changed, e.g.
538 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
540 * In this case, if the definitions produced by yacc haven't changed
541 * from before, parse.h won't have been updated and gn->mtime will
542 * reflect the current modification time for parse.h. This is
543 * something of a kludge, I admit, but it's a useful one.
545 * XXX: People like to use a rule like "FRC:" to force things that
546 * depend on FRC to be made, so we have to check for gn->children
547 * being empty as well.
549 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
554 * This is what Make does and it's actually a good thing, as it
557 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
559 * to function as intended. Unfortunately, thanks to the stateless
560 * nature of NFS (by which I mean the loose coupling of two clients
561 * using the same file from a common server), there are times when
562 * the modification time of a file created on a remote machine
563 * will not be modified before the local stat() implied by the
564 * Dir_UpdateMTime occurs, thus leading us to believe that the file
565 * is unchanged, wreaking havoc with files that depend on this one.
567 * I have decided it is better to make too much than to make too
568 * little, so this stuff is commented out unless you're sure it's ok.
572 * Christos, 4/9/92: If we are saving commands, pretend that
573 * the target is made now. Otherwise archives with '...' rules
576 if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
577 (mtime == 0 && !(gn->type & OP_WAIT))) {
578 DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
580 gn->mtime == 0 ? "nonexistent" : Targ_FmtTime(gn->mtime));
583 DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
584 gn->name, Targ_FmtTime(gn->mtime));
589 * XXX: The returned mtime may differ from gn->mtime. Intentionally?
595 * Set the .PREFIX and .IMPSRC variables for all the implied parents
599 UpdateImplicitParentsVars(GNode *cgn, const char *cname)
602 const char *cpref = GNode_VarPrefix(cgn);
604 for (ln = cgn->implicitParents.first; ln != NULL; ln = ln->next) {
605 GNode *pgn = ln->datum;
606 if (pgn->flags.remake) {
607 Var_Set(pgn, IMPSRC, cname);
609 Var_Set(pgn, PREFIX, cpref);
614 /* See if a .ORDER rule stops us from building this node. */
616 IsWaitingForOrder(GNode *gn)
620 for (ln = gn->order_pred.first; ln != NULL; ln = ln->next) {
621 GNode *ogn = ln->datum;
623 if (GNode_IsDone(ogn) || !ogn->flags.remake)
627 "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
628 ogn->name, ogn->cohort_num);
634 static bool MakeBuildChild(GNode *, GNodeListNode *);
637 ScheduleOrderSuccessors(GNode *gn)
639 GNodeListNode *toBeMadeNext = toBeMade.first;
642 for (ln = gn->order_succ.first; ln != NULL; ln = ln->next) {
643 GNode *succ = ln->datum;
645 if (succ->made == DEFERRED &&
646 !MakeBuildChild(succ, toBeMadeNext))
647 succ->flags.doneOrder = true;
652 * Perform update on the parents of a node. Used by JobFinish once
653 * 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
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 youngestChild 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.
672 Make_Update(GNode *cgn)
674 const char *cname; /* the child's name */
680 /* It is save to re-examine any nodes again */
683 cname = GNode_VarTarget(cgn);
685 DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
688 * If the child was actually made, see what its modification time is
689 * now -- some rules won't actually update the file. If the file
690 * still doesn't exist, make its mtime now.
692 if (cgn->made != UPTODATE) {
693 mtime = Make_Recheck(cgn);
697 * If this is a `::' node, we must consult its first instance
698 * which is where all parents are linked.
700 if ((centurion = cgn->centurion) != NULL) {
701 if (!Lst_IsEmpty(&cgn->parents))
702 Punt("%s%s: cohort has parents", cgn->name,
704 centurion->unmade_cohorts--;
705 if (centurion->unmade_cohorts < 0)
706 Error("Graph cycles through centurion %s",
711 parents = ¢urion->parents;
713 /* If this was a .ORDER node, schedule the RHS */
714 ScheduleOrderSuccessors(centurion);
716 /* Now mark all the parents as having one less unmade child */
717 for (ln = parents->first; ln != NULL; ln = ln->next) {
718 GNode *pgn = ln->datum;
721 debug_printf("inspect parent %s%s: ", pgn->name,
723 GNode_FprintDetails(opts.debug_file, "", pgn, "");
724 debug_printf(", unmade %d ", pgn->unmade - 1);
727 if (!pgn->flags.remake) {
728 /* This parent isn't needed */
729 DEBUG0(MAKE, "- not needed\n");
732 if (mtime == 0 && !(cgn->type & OP_WAIT))
733 pgn->flags.force = true;
736 * If the parent has the .MADE attribute, its timestamp got
737 * updated to that of its newest child, and its unmade
738 * child count got set to zero in Make_ExpandUse().
739 * However other things might cause us to build one of its
740 * children - and so we mustn't do any processing here when
741 * the child build finishes.
743 if (pgn->type & OP_MADE) {
744 DEBUG0(MAKE, "- .MADE\n");
748 if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
749 if (cgn->made == MADE)
750 pgn->flags.childMade = true;
751 GNode_UpdateYoungestChild(pgn, cgn);
755 * A parent must wait for the completion of all instances
756 * of a `::' dependency.
758 if (centurion->unmade_cohorts != 0 ||
759 !GNode_IsDone(centurion)) {
761 "- centurion made %d, %d unmade cohorts\n",
762 centurion->made, centurion->unmade_cohorts);
766 /* One more child of this parent is now made */
768 if (pgn->unmade < 0) {
770 debug_printf("Graph cycles through %s%s\n",
771 pgn->name, pgn->cohort_num);
774 Error("Graph cycles through %s%s", pgn->name,
779 * We must always rescan the parents of .WAIT and .ORDER
782 if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
783 && !centurion->flags.doneOrder) {
784 DEBUG0(MAKE, "- unmade children\n");
787 if (pgn->made != DEFERRED) {
789 * Either this parent is on a different branch of
790 * the tree, or it on the RHS of a .WAIT directive
791 * or it is already on the toBeMade list.
793 DEBUG0(MAKE, "- not deferred\n");
797 if (IsWaitingForOrder(pgn))
801 debug_printf("- %s%s made, schedule %s%s (made %d)\n",
802 cgn->name, cgn->cohort_num,
803 pgn->name, pgn->cohort_num, pgn->made);
804 Targ_PrintNode(pgn, 2);
806 /* Ok, we can schedule the parent again */
807 pgn->made = REQUESTED;
808 Lst_Enqueue(&toBeMade, pgn);
811 UpdateImplicitParentsVars(cgn, cname);
815 UnmarkChildren(GNode *gn)
819 for (ln = gn->children.first; ln != NULL; ln = ln->next) {
820 GNode *child = ln->datum;
821 child->type &= (unsigned)~OP_MARK;
826 * Add a child's name to the ALLSRC and OODATE variables of the given
827 * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
828 * attributes. .EXEC and .USE children are very rarely going to be files,
831 * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
833 * A child is added to the OODATE variable if its modification time is
834 * later than that of its parent, as defined by Make, except if the
835 * parent is a .JOIN node. In that case, it is only added to the OODATE
836 * variable if it was actually made (since .JOIN nodes don't have
837 * modification times, the comparison is rather unfair...)..
840 * cgn The child to add
841 * pgn The parent to whose ALLSRC variable it should
845 MakeAddAllSrc(GNode *cgn, GNode *pgn)
847 const char *child, *allsrc;
849 if (cgn->type & OP_MARK)
851 cgn->type |= OP_MARK;
853 if (cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE | OP_INVISIBLE))
856 if (cgn->type & OP_ARCHV)
857 child = GNode_VarMember(cgn);
859 child = GNode_Path(cgn);
861 if (cgn->type & OP_JOIN)
862 allsrc = GNode_VarAllsrc(cgn);
867 Var_Append(pgn, ALLSRC, allsrc);
869 if (pgn->type & OP_JOIN) {
870 if (cgn->made == MADE)
871 Var_Append(pgn, OODATE, child);
873 } else if ((pgn->mtime < cgn->mtime) ||
874 (cgn->mtime >= now && cgn->made == MADE)) {
876 * It goes in the OODATE variable if the parent is
877 * younger than the child or if the child has been
878 * modified more recently than the start of the make.
879 * This is to keep pmake from getting confused if
880 * something else updates the parent after the make
881 * starts (shouldn't happen, I know, but sometimes it
882 * does). In such a case, if we've updated the child,
883 * the parent is likely to have a modification time
884 * later than that of the child and anything that
885 * relies on the OODATE variable will be hosed.
887 * XXX: This will cause all made children to go in
888 * the OODATE variable, even if they're not touched,
889 * if RECHECK isn't defined, since cgn->mtime is set
890 * to now in Make_Update. According to some people,
893 Var_Append(pgn, OODATE, child);
898 * Set up the ALLSRC and OODATE variables. Sad to say, it must be
899 * done separately, rather than while traversing the graph. This is
900 * because Make defined OODATE to contain all sources whose modification
901 * times were later than that of the target, *not* those sources that
902 * were out-of-date. Since in both compatibility and native modes,
903 * the modification time of the parent isn't found until the child
904 * has been dealt with, we have to wait until now to fill in the
905 * variable. As for ALLSRC, the ordering is important and not
906 * guaranteed when in native mode, so it must be set here, too.
908 * If the node is a .JOIN node, its TARGET variable will be set to
909 * match its ALLSRC variable.
912 GNode_SetLocalVars(GNode *gn)
916 if (gn->flags.doneAllsrc)
920 for (ln = gn->children.first; ln != NULL; ln = ln->next)
921 MakeAddAllSrc(ln->datum, gn);
923 if (!Var_Exists(gn, OODATE))
924 Var_Set(gn, OODATE, "");
925 if (!Var_Exists(gn, ALLSRC))
926 Var_Set(gn, ALLSRC, "");
928 if (gn->type & OP_JOIN)
929 Var_Set(gn, TARGET, GNode_VarAllsrc(gn));
930 gn->flags.doneAllsrc = true;
934 ScheduleRandomly(GNode *gn)
940 for (ln = toBeMade.first; ln != NULL; ln = ln->next)
942 i = n > 0 ? (size_t)random() % (n + 1) : 0;
945 Lst_Append(&toBeMade, gn);
950 for (ln = toBeMade.first; i > 0; ln = ln->next)
952 Lst_InsertBefore(&toBeMade, ln, gn);
956 MakeBuildChild(GNode *cn, GNodeListNode *toBeMadeNext)
960 debug_printf("MakeBuildChild: inspect %s%s, ",
961 cn->name, cn->cohort_num);
962 GNode_FprintDetails(opts.debug_file, "", cn, "\n");
964 if (GNode_IsReady(cn))
967 /* If this node is on the RHS of a .ORDER, check LHSs. */
968 if (IsWaitingForOrder(cn)) {
970 * Can't build this (or anything else in this child list) yet
973 return false; /* but keep looking */
976 DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n",
977 cn->name, cn->cohort_num);
979 cn->made = REQUESTED;
980 if (opts.randomizeTargets && !(cn->type & OP_WAIT))
981 ScheduleRandomly(cn);
982 else if (toBeMadeNext == NULL)
983 Lst_Append(&toBeMade, cn);
985 Lst_InsertBefore(&toBeMade, toBeMadeNext, cn);
987 if (cn->unmade_cohorts != 0) {
990 for (ln = cn->cohorts.first; ln != NULL; ln = ln->next)
991 if (MakeBuildChild(ln->datum, toBeMadeNext))
996 * If this node is a .WAIT node with unmade children
997 * then don't add the next sibling.
999 return cn->type & OP_WAIT && cn->unmade > 0;
1003 MakeChildren(GNode *gn)
1005 GNodeListNode *toBeMadeNext = toBeMade.first;
1008 for (ln = gn->children.first; ln != NULL; ln = ln->next)
1009 if (MakeBuildChild(ln->datum, toBeMadeNext))
1014 * Start as many jobs as possible, taking them from the toBeMade queue.
1016 * If the -q option was given, no job will be started,
1017 * but as soon as an out-of-date target is found, this function
1018 * returns true. In all other cases, this function returns false.
1024 bool have_token = false;
1026 while (!Lst_IsEmpty(&toBeMade)) {
1028 * Get token now to avoid cycling job-list when we only
1031 if (!have_token && !Job_TokenWithdraw())
1035 gn = Lst_Dequeue(&toBeMade);
1036 DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
1038 if (gn->made != REQUESTED) {
1039 debug_printf("internal error: made = %s\n",
1040 GNodeMade_Name(gn->made));
1041 Targ_PrintNode(gn, 2);
1042 Targ_PrintNodes(&toBeMade, 2);
1047 if (gn->checked_seqno == checked_seqno) {
1049 * We've already looked at this node since a job
1052 DEBUG2(MAKE, "already checked %s%s\n", gn->name,
1054 gn->made = DEFERRED;
1057 gn->checked_seqno = checked_seqno;
1059 if (gn->unmade != 0) {
1061 * We can't build this yet, add all unmade children
1062 * to toBeMade, just before the current first element.
1064 gn->made = DEFERRED;
1068 /* and drop this node on the floor */
1069 DEBUG2(MAKE, "dropped %s%s\n", gn->name,
1074 gn->made = BEINGMADE;
1075 if (GNode_IsOODate(gn)) {
1076 DEBUG0(MAKE, "out-of-date\n");
1078 return strcmp(gn->name, ".MAIN") != 0;
1079 GNode_SetLocalVars(gn);
1083 DEBUG0(MAKE, "up-to-date\n");
1084 gn->made = UPTODATE;
1085 if (gn->type & OP_JOIN) {
1087 * Even for an up-to-date .JOIN node, we
1088 * need it to have its local variables so
1089 * references to it get the correct value
1090 * for .TARGET when building up the local
1091 * variables of its parent(s)...
1093 GNode_SetLocalVars(gn);
1105 /* Print the status of a .ORDER node. */
1107 MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
1109 if (!GNode_IsWaitingFor(ogn))
1112 printf(" `%s%s' has .ORDER dependency against %s%s ",
1113 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1114 GNode_FprintDetails(stdout, "(", ogn, ")\n");
1116 if (DEBUG(MAKE) && opts.debug_file != stdout) {
1117 debug_printf(" `%s%s' has .ORDER dependency against %s%s ",
1118 gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1119 GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
1124 MakePrintStatusOrder(GNode *gn)
1127 for (ln = gn->order_pred.first; ln != NULL; ln = ln->next)
1128 MakePrintStatusOrderNode(ln->datum, gn);
1131 static void MakePrintStatusList(GNodeList *, int *);
1134 * Print the status of a top-level node, viz. it being up-to-date already
1135 * or not created due to an error in a lower level.
1138 MakePrintStatus(GNode *gn, int *errors)
1140 if (gn->flags.doneCycle) {
1142 * We've completely processed this node before, don't do
1148 if (gn->unmade == 0) {
1149 gn->flags.doneCycle = true;
1152 printf("`%s%s' is up to date.\n", gn->name,
1162 printf("`%s%s' was not built", gn->name,
1164 GNode_FprintDetails(stdout, " (", gn, ")!\n");
1165 if (DEBUG(MAKE) && opts.debug_file != stdout) {
1166 debug_printf("`%s%s' was not built", gn->name,
1168 GNode_FprintDetails(opts.debug_file, " (", gn,
1171 /* Most likely problem is actually caused by .ORDER */
1172 MakePrintStatusOrder(gn);
1175 /* Errors - already counted */
1176 printf("`%s%s' not remade because of errors.\n",
1177 gn->name, gn->cohort_num);
1178 if (DEBUG(MAKE) && opts.debug_file != stdout)
1180 "`%s%s' not remade because of errors.\n",
1181 gn->name, gn->cohort_num);
1187 DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
1188 gn->name, gn->cohort_num, gn->unmade);
1190 * If printing cycles and came to one that has unmade children,
1191 * print out the cycle by recursing on its children.
1193 if (!gn->flags.cycle) {
1194 /* First time we've seen this node, check all children */
1195 gn->flags.cycle = true;
1196 MakePrintStatusList(&gn->children, errors);
1197 /* Mark that this node needn't be processed again */
1198 gn->flags.doneCycle = true;
1202 /* Only output the error once per node */
1203 gn->flags.doneCycle = true;
1204 Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1205 if ((*errors)++ > 100)
1206 /* Abandon the whole error report */
1209 /* Reporting for our children will give the rest of the loop */
1210 MakePrintStatusList(&gn->children, errors);
1215 MakePrintStatusList(GNodeList *gnodes, int *errors)
1219 for (ln = gnodes->first; ln != NULL; ln = ln->next)
1220 if (MakePrintStatus(ln->datum, errors))
1225 ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
1229 for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
1230 GNode *gn = ln->datum;
1232 if (gn->flags.remake)
1234 if (gn->type & (OP_USE | OP_USEBEFORE))
1237 DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
1238 gn->name, gn->cohort_num);
1239 Lst_Enqueue(examine, gn);
1244 * Expand .USE nodes and create a new targets list.
1247 * targs the initial list of targets
1250 Make_ExpandUse(GNodeList *targs)
1252 GNodeList examine = LST_INIT; /* Queue of targets to examine */
1253 Lst_AppendAll(&examine, targs);
1256 * Make an initial downward pass over the graph, marking nodes to
1257 * be made as we go down.
1259 * We call Suff_FindDeps to find where a node is and to get some
1260 * children for it if it has none and also has no commands. If the
1261 * node is a leaf, we stick it on the toBeMade queue to be looked
1262 * at in a minute, otherwise we add its children to our queue and
1263 * go on about our business.
1265 while (!Lst_IsEmpty(&examine)) {
1266 GNode *gn = Lst_Dequeue(&examine);
1268 if (gn->flags.remake)
1269 /* We've looked at this one already */
1271 gn->flags.remake = true;
1272 DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
1273 gn->name, gn->cohort_num);
1275 if (gn->type & OP_DOUBLEDEP)
1276 Lst_PrependAll(&examine, &gn->cohorts);
1279 * Apply any .USE rules before looking for implicit
1280 * dependencies to make sure everything has commands that
1283 * Make sure that the TARGET is set, so that we can make
1286 if (gn->type & OP_ARCHV) {
1287 char *eoa = strchr(gn->name, '(');
1288 char *eon = strchr(gn->name, ')');
1289 if (eoa == NULL || eon == NULL)
1293 Var_Set(gn, MEMBER, eoa + 1);
1294 Var_Set(gn, ARCHIVE, gn->name);
1299 Dir_UpdateMTime(gn, false);
1300 Var_Set(gn, TARGET, GNode_Path(gn));
1304 if (!(gn->type & OP_MADE))
1307 PretendAllChildrenAreMade(gn);
1308 if (gn->unmade != 0) {
1311 "%s%s still has %d unmade children\n",
1312 gn->name, gn->cohort_num, gn->unmade);
1316 if (gn->unmade != 0)
1317 ExamineLater(&examine, &gn->children);
1323 /* Make the .WAIT node depend on the previous children */
1325 add_wait_dependency(GNodeListNode *owln, GNode *wn)
1330 for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1331 DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
1332 cn->name, cn->cohort_num, wn->name);
1335 * XXX: This pattern should be factored out, it repeats often
1337 Lst_Append(&wn->children, cn);
1339 Lst_Append(&cn->parents, wn);
1343 /* Convert .WAIT nodes into dependencies. */
1345 Make_ProcessWait(GNodeList *targs)
1347 GNode *pgn; /* 'parent' node we are examining */
1348 GNodeListNode *owln; /* Previous .WAIT node */
1349 GNodeList examine; /* List of targets to examine */
1352 * We need all the nodes to have a common parent in order for the
1353 * .WAIT and .ORDER scheduling to work.
1354 * Perhaps this should be done earlier...
1357 pgn = GNode_New(".MAIN");
1358 pgn->flags.remake = true;
1359 pgn->type = OP_PHONY | OP_DEPENDS;
1360 /* Get it displayed in the diag dumps */
1361 Lst_Prepend(Targ_List(), pgn);
1365 for (ln = targs->first; ln != NULL; ln = ln->next) {
1366 GNode *cgn = ln->datum;
1368 Lst_Append(&pgn->children, cgn);
1369 Lst_Append(&cgn->parents, pgn);
1374 /* Start building with the 'dummy' .MAIN' node */
1375 MakeBuildChild(pgn, NULL);
1378 Lst_Append(&examine, pgn);
1380 while (!Lst_IsEmpty(&examine)) {
1383 pgn = Lst_Dequeue(&examine);
1385 /* We only want to process each child-list once */
1386 if (pgn->flags.doneWait)
1388 pgn->flags.doneWait = true;
1389 DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
1391 if (pgn->type & OP_DOUBLEDEP)
1392 Lst_PrependAll(&examine, &pgn->cohorts);
1394 owln = pgn->children.first;
1395 for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
1396 GNode *cgn = ln->datum;
1397 if (cgn->type & OP_WAIT) {
1398 add_wait_dependency(owln, cgn);
1401 Lst_Append(&examine, cgn);
1410 * Initialize the nodes to remake and the list of nodes which are ready to
1411 * be made by doing a breadth-first traversal of the graph starting from the
1412 * nodes in the given list. Once this traversal is finished, all the 'leaves'
1413 * of the graph are in the toBeMade queue.
1415 * Using this queue and the Job module, work back up the graph, calling on
1416 * MakeStartJobs to keep the job table as full as possible.
1419 * targs the initial list of targets
1422 * True if work was done, false otherwise.
1425 * The make field of all nodes involved in the creation of the given
1426 * targets is set to 1. The toBeMade list is set to contain all the
1427 * 'leaves' of these subgraphs.
1430 Make_Run(GNodeList *targs)
1432 int errors; /* Number of errors the Job module reports */
1434 /* Start trying to make the current targets... */
1435 Lst_Init(&toBeMade);
1437 Make_ExpandUse(targs);
1438 Make_ProcessWait(targs);
1441 debug_printf("#***# full graph\n");
1447 * We wouldn't do any work unless we could start some jobs
1448 * in the next loop... (we won't actually start any, of
1449 * course, this is just to see if any of the targets was out
1452 return MakeStartJobs();
1455 * Initialization. At the moment, no jobs are running and until some
1456 * get started, nothing will happen since the remaining upward
1457 * traversal of the graph is performed by the routines in job.c upon
1458 * the finishing of a job. So we fill the Job table as much as we can
1459 * before going into our loop.
1461 (void)MakeStartJobs();
1464 * Main Loop: The idea here is that the ending of jobs will take
1465 * care of the maintenance of data structures and the waiting for
1466 * output will cause us to be idle most of the time while our
1467 * children run as much as possible. Because the job table is kept
1468 * as full as possible, the only time when it will be empty is when
1469 * all the jobs which need running have been run, so that is the end
1470 * condition of this loop. Note that the Job module will exit if
1471 * there were any errors unless the keepgoing flag was given.
1473 while (!Lst_IsEmpty(&toBeMade) || jobTokensRunning > 0) {
1475 (void)MakeStartJobs();
1478 errors = Job_Finish();
1481 * Print the final status of each target. E.g. if it wasn't made
1482 * because some inferior reported an error.
1484 DEBUG1(MAKE, "done: errors %d\n", errors);
1486 MakePrintStatusList(targs, &errors);
1488 debug_printf("done: errors %d\n", errors);