2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)make.c 8.1 (Berkeley) 6/6/93
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
46 * The functions which perform the examination of targets and
47 * their suitability for creation
50 * Make_Run Initialize things for the module and recreate
51 * whatever needs recreating. Returns TRUE if
52 * work was (or would have been) done and FALSE
55 * Make_Update Update all parents of a given child. Performs
56 * various bookkeeping chores like the updating
57 * of the cmtime field of the parent, filling
58 * of the IMPSRC context variable, etc. It will
59 * place the parent on the toBeMade queue if it should be.
61 * Make_TimeStamp Function to set the parent's cmtime field
62 * based on a child's modification time.
64 * Make_DoAllVar Set up the various local variables for a
65 * target, including the .ALLSRC variable, making
66 * sure that any variable that needs to exist
67 * at the very least has the empty value.
69 * Make_OODate Determine if a target is out-of-date.
71 * Make_HandleUse See if a child is a .USE node for a parent
72 * and perform the .USE actions if so.
90 /* The current fringe of the graph. These are nodes which await examination
91 * by MakeOODate. It is added to by Make_Update and subtracted from by
93 static Lst toBeMade = Lst_Initializer(toBeMade);
96 * Number of nodes to be processed. If this is non-zero when Job_Empty()
97 * returns TRUE, there's a cycle in the graph.
101 static Boolean MakeStartJobs(void);
105 * Set the cmtime field of a parent node based on the mtime stamp in its
106 * child. Called from MakeOODate via LST_FOREACH.
112 * The cmtime of the parent node will be changed if the mtime
113 * field of the child is greater than it.
116 Make_TimeStamp(GNode *pgn, GNode *cgn)
119 if (cgn->mtime > pgn->cmtime) {
120 pgn->cmtime = cgn->mtime;
121 pgn->cmtime_gn = cgn;
128 * See if a given node is out of date with respect to its sources.
129 * Used by Make_Run when deciding which nodes to place on the
130 * toBeMade queue initially and by Make_Update to screen out USE and
131 * EXEC nodes. In the latter case, however, any other sort of node
132 * must be considered out-of-date since at least one of its children
133 * will have been recreated.
136 * TRUE if the node is out of date. FALSE otherwise.
139 * The mtime field of the node and the cmtime field of its parents
140 * will/may be changed.
143 Make_OODate(GNode *gn)
149 * Certain types of targets needn't even be sought as their datedness
150 * doesn't depend on their modification time...
152 if ((gn->type & (OP_JOIN | OP_USE | OP_EXEC)) == 0) {
154 if (gn->mtime != 0) {
155 DEBUGF(MAKE, ("modified %s...",
156 Targ_FmtTime(gn->mtime)));
158 DEBUGF(MAKE, ("non-existent..."));
163 * A target is remade in one of the following circumstances:
164 * its modification time is smaller than that of its youngest child
165 * and it would actually be run (has commands or type OP_NOP)
166 * it's the object of a force operator
167 * it has no children, was on the lhs of an operator and doesn't
170 * Libraries are only considered out-of-date if the archive module says
173 * These weird rules are brought to you by Backward-Compatibility and
174 * the strange people who wrote 'Make'.
176 if (gn->type & OP_USE) {
178 * If the node is a USE node it is *never* out of date
181 DEBUGF(MAKE, (".USE node..."));
184 } else if (gn->type & OP_LIB) {
185 DEBUGF(MAKE, ("library..."));
188 * always out of date if no children and :: target
190 oodate = Arch_LibOODate(gn) ||
191 ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
193 } else if (gn->type & OP_JOIN) {
195 * A target with the .JOIN attribute is only considered
196 * out-of-date if any of its children was out-of-date.
198 DEBUGF(MAKE, (".JOIN node..."));
199 oodate = gn->childMade;
201 } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
203 * A node which is the object of the force (!) operator or
204 * which has the .EXEC attribute is always considered
207 if (gn->type & OP_FORCE) {
208 DEBUGF(MAKE, ("! operator..."));
209 } else if (gn->type & OP_PHONY) {
210 DEBUGF(MAKE, (".PHONY node..."));
212 DEBUGF(MAKE, (".EXEC node..."));
216 } else if ((gn->mtime < gn->cmtime) ||
217 ((gn->cmtime == 0) &&
218 ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP)))) {
220 * A node whose modification time is less than that of its
221 * youngest child or that has no children (cmtime == 0) and
222 * either doesn't exist (mtime == 0) or was the object of a
223 * :: operator is out-of-date. Why? Because that's the way
226 if (gn->mtime < gn->cmtime) {
227 DEBUGF(MAKE, ("modified before source (%s)...",
228 gn->cmtime_gn ? gn->cmtime_gn->path : "???"));
229 } else if (gn->mtime == 0) {
230 DEBUGF(MAKE, ("non-existent and no sources..."));
232 DEBUGF(MAKE, (":: operator and no sources..."));
239 * If the target isn't out-of-date, the parents need to know its
240 * modification time. Note that targets that appear to be out-of-date
241 * but aren't, because they have no commands and aren't of type OP_NOP,
242 * have their mtime stay below their children's mtime to keep parents
243 * from thinking they're out-of-date.
246 LST_FOREACH(ln, &gn->parents)
247 if (Make_TimeStamp(Lst_Datum(ln), gn))
256 * Function called by Make_Run and SuffApplyTransform on the downward
257 * pass to handle .USE and transformation nodes. A callback function
258 * for LST_FOREACH, it implements the .USE and transformation
259 * functionality by copying the node's commands, type flags
260 * and children to the parent node. Should be called before the
261 * children are enqueued to be looked at.
263 * A .USE node is much like an explicit transformation rule, except
264 * its commands are always added to the target node, even if the
265 * target already has commands.
271 * Children and commands may be added to the parent and the parent's
272 * type may be changed.
274 *-----------------------------------------------------------------------
277 Make_HandleUse(GNode *cgn, GNode *pgn)
279 GNode *gn; /* A child of the .USE node */
280 LstNode *ln; /* An element in the children list */
282 if (cgn->type & (OP_USE | OP_TRANSFORM)) {
283 if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
285 * .USE or transformation and target has no commands --
286 * append the child's commands to the parent.
288 Lst_Concat(&pgn->commands, &cgn->commands, LST_CONCNEW);
291 for (ln = Lst_First(&cgn->children); ln != NULL;
295 if (Lst_Member(&pgn->children, gn) == NULL) {
296 Lst_AtEnd(&pgn->children, gn);
297 Lst_AtEnd(&gn->parents, pgn);
302 pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
305 * This child node is now "made", so we decrement the count of
306 * unmade children in the parent... We also remove the child
307 * from the parent's list to accurately reflect the number of
308 * decent children the parent has. This is used by Make_Run to
309 * decide whether to queue the parent or examine its children...
311 if (cgn->type & OP_USE) {
320 * Perform update on the parents of a node. Used by JobFinish once
321 * a node has been dealt with and by MakeStartJobs if it finds an
328 * The unmade field of pgn is decremented and pgn may be placed on
329 * the toBeMade queue if this field becomes 0.
331 * If the child was made, the parent's childMade field will be set true
332 * and its cmtime set to now.
334 * If the child wasn't made, the cmtime field of the parent will be
335 * altered if the child's mtime is big enough.
337 * Finally, if the child is the implied source for the parent, the
338 * parent's IMPSRC variable is set appropriately.
341 Make_Update(GNode *cgn)
343 GNode *pgn; /* the parent node */
344 const char *cname; /* the child's name */
345 LstNode *ln; /* Element in parents and iParents lists */
348 cname = Var_Value(TARGET, cgn);
351 * If the child was actually made, see what its modification time is
352 * now -- some rules won't actually update the file. If the file still
353 * doesn't exist, make its mtime now.
355 if (cgn->made != UPTODATE) {
358 * We can't re-stat the thing, but we can at least take care
359 * of rules where a target depends on a source that actually
360 * creates the target, but only if it has changed, e.g.
368 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
370 * In this case, if the definitions produced by yacc haven't
371 * changed from before, parse.h won't have been updated and
372 * cgn->mtime will reflect the current modification time for
373 * parse.h. This is something of a kludge, I admit, but it's a
375 * XXX: People like to use a rule like
379 * To force things that depend on FRC to be made, so we have to
380 * check for gn->children being empty as well...
382 if (!Lst_IsEmpty(&cgn->commands) ||
383 Lst_IsEmpty(&cgn->children)) {
388 * This is what Make does and it's actually a good thing, as it
391 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
393 * to function as intended. Unfortunately, thanks to the
394 * stateless nature of NFS (by which I mean the loose coupling
395 * of two clients using the same file from a common server),
396 * there are times when the modification time of a file created
397 * on a remote machine will not be modified before the local
398 * stat() implied by the Dir_MTime occurs, thus leading us to
399 * believe that the file is unchanged, wreaking havoc with
400 * files that depend on this one.
402 * I have decided it is better to make too much than to make too
403 * little, so this stuff is commented out unless you're sure
408 * Christos, 4/9/92: If we are saving commands pretend that
409 * the target is made now. Otherwise archives with ... rules
412 if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
413 Dir_MTime(cgn) == 0) {
416 DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
420 for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Succ(ln)) {
425 if (!(cgn->type & (OP_EXEC | OP_USE))) {
426 if (cgn->made == MADE)
427 pgn->childMade = TRUE;
428 Make_TimeStamp(pgn, cgn);
430 if (pgn->unmade == 0) {
432 * Queue the node up -- any unmade predecessors
433 * will be dealt with in MakeStartJobs.
435 Lst_EnQueue(&toBeMade, pgn);
436 } else if (pgn->unmade < 0) {
437 Error("Graph cycles through %s", pgn->name);
443 * Deal with successor nodes. If any is marked for making and has an
444 * unmade count of 0, has not been made and isn't in the examination
445 * queue, it means we need to place it in the queue as it restrained
448 for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
449 GNode *succ = Lst_Datum(ln);
451 if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
452 Lst_Member(&toBeMade, succ) == NULL) {
453 Lst_EnQueue(&toBeMade, succ);
458 * Set the .PREFIX and .IMPSRC variables for all the implied parents
461 cpref = Var_Value(PREFIX, cgn);
462 for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Succ(ln)) {
465 Var_Set(IMPSRC, cname, pgn);
466 Var_Set(PREFIX, cpref, pgn);
473 * Set up the ALLSRC and OODATE variables. Sad to say, it must be
474 * done separately, rather than while traversing the graph. This is
475 * because Make defined OODATE to contain all sources whose modification
476 * times were later than that of the target, *not* those sources that
477 * were out-of-date. Since in both compatibility and native modes,
478 * the modification time of the parent isn't found until the child
479 * has been dealt with, we have to wait until now to fill in the
480 * variable. As for ALLSRC, the ordering is important and not
481 * guaranteed when in native mode, so it must be set here, too.
484 * The ALLSRC and OODATE variables of the given node is filled in.
485 * If the node is a .JOIN node, its TARGET variable will be set to
486 * match its ALLSRC variable.
489 Make_DoAllVar(GNode *gn)
495 LST_FOREACH(ln, &gn->children) {
497 * Add the child's name to the ALLSRC and OODATE variables of
498 * the given node. The child is added only if it has not been
499 * given the .EXEC, .USE or .INVISIBLE attributes. .EXEC and
500 * .USE children are very rarely going to be files, so...
502 * A child is added to the OODATE variable if its modification
503 * time is later than that of its parent, as defined by Make,
504 * except if the parent is a .JOIN node. In that case, it is
505 * only added to the OODATE variable if it was actually made
506 * (since .JOIN nodes don't have modification times, the
507 * comparison is rather unfair...).
511 if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
512 if (OP_NOP(cgn->type)) {
514 * this node is only source; use the specific
517 child = cgn->path ? cgn->path : cgn->name;
519 child = Var_Value(TARGET, cgn);
520 Var_Append(ALLSRC, child, gn);
521 if (gn->type & OP_JOIN) {
522 if (cgn->made == MADE) {
523 Var_Append(OODATE, child, gn);
525 } else if (gn->mtime < cgn->mtime ||
526 (cgn->mtime >= now && cgn->made == MADE)) {
528 * It goes in the OODATE variable if the parent
529 * is younger than the child or if the child has
530 * been modified more recently than the start of
531 * the make. This is to keep pmake from getting
532 * confused if something else updates the parent
533 * after the make starts (shouldn't happen, I
534 * know, but sometimes it does). In such a case,
535 * if we've updated the kid, the parent is
536 * likely to have a modification time later than
537 * that of the kid and anything that relies on
538 * the OODATE variable will be hosed.
540 * XXX: This will cause all made children to
541 * go in the OODATE variable, even if they're
542 * not touched, if RECHECK isn't defined, since
543 * cgn->mtime is set to now in Make_Update.
544 * According to some people, this is good...
546 Var_Append(OODATE, child, gn);
551 if (!Var_Exists (OODATE, gn)) {
552 Var_Set(OODATE, "", gn);
554 if (!Var_Exists (ALLSRC, gn)) {
555 Var_Set(ALLSRC, "", gn);
558 if (gn->type & OP_JOIN) {
559 Var_Set(TARGET, Var_Value(ALLSRC, gn), gn);
565 * Start as many jobs as possible.
568 * If the query flag was given to pmake, no job will be started,
569 * but as soon as an out-of-date target is found, this function
570 * returns TRUE. At all other times, this function returns FALSE.
573 * Nodes are removed from the toBeMade queue and job table slots
581 while (!Lst_IsEmpty(&toBeMade) && !Job_Full()) {
582 gn = Lst_DeQueue(&toBeMade);
583 DEBUGF(MAKE, ("Examining %s...", gn->name));
586 * Make sure any and all predecessors that are going to be made,
589 if (!Lst_IsEmpty(&gn->preds)) {
592 for (ln = Lst_First(&gn->preds); ln != NULL;
594 GNode *pgn = Lst_Datum(ln);
596 if (pgn->make && pgn->made == UNMADE) {
597 DEBUGF(MAKE, ("predecessor %s not made "
598 "yet.\n", pgn->name));
603 * If ln isn't NULL, there's a predecessor as yet
604 * unmade, so we just drop this node on the floor.
605 * When the node in question has been made, it will
606 * notice this node as being ready to make but as yet
607 * unmade and will place the node on the queue.
615 if (Make_OODate(gn)) {
616 DEBUGF(MAKE, ("out-of-date\n"));
623 DEBUGF(MAKE, ("up-to-date\n"));
625 if (gn->type & OP_JOIN) {
627 * Even for an up-to-date .JOIN node, we need
628 * it to have its context variables so
629 * references to it get the correct value for
630 * .TARGET when building up the context
631 * variables of its parent(s)...
644 * Print the status of a top-level node, viz. it being up-to-date
645 * already or not created due to an error in a lower level.
646 * Callback function for Make_Run via LST_FOREACH. If gn->unmade is
647 * nonzero and that is meant to imply a cycle in the graph, then
651 * A message may be printed.
654 MakePrintStatus(GNode *gn, Boolean cycle)
658 if (gn->made == UPTODATE) {
659 printf("`%s' is up to date.\n", gn->name);
661 } else if (gn->unmade != 0) {
664 * If printing cycles and came to one that has unmade
665 * children, print out the cycle by recursing on its
666 * children. Note a cycle like:
670 * will cause this to erroneously complain about a
671 * being in the cycle, but this is a good approximation.
673 if (gn->made == CYCLE) {
674 Error("Graph cycles through `%s'", gn->name);
676 LST_FOREACH(ln, &gn->children)
677 MakePrintStatus(Lst_Datum(ln), TRUE);
679 } else if (gn->made != ENDCYCLE) {
681 LST_FOREACH(ln, &gn->children)
682 MakePrintStatus(Lst_Datum(ln), TRUE);
685 printf("`%s' not remade because of errors.\n",
693 * Initialize the nodes to remake and the list of nodes which are
694 * ready to be made by doing a breadth-first traversal of the graph
695 * starting from the nodes in the given list. Once this traversal
696 * is finished, all the 'leaves' of the graph are in the toBeMade
698 * Using this queue and the Job module, work back up the graph,
699 * calling on MakeStartJobs to keep the job table as full as
703 * TRUE if work was done. FALSE otherwise.
706 * The make field of all nodes involved in the creation of the given
707 * targets is set to 1. The toBeMade list is set to contain all the
708 * 'leaves' of these subgraphs.
713 GNode *gn; /* a temporary pointer */
715 Lst examine; /* List of targets to examine */
716 int errors; /* Number of errors the Job module reports */
720 Lst_Duplicate(&examine, targs, NOCOPY);
724 * Make an initial downward pass over the graph, marking nodes to be
725 * made as we go down. We call Suff_FindDeps to find where a node is and
726 * to get some children for it if it has none and also has no commands.
727 * If the node is a leaf, we stick it on the toBeMade queue to
728 * be looked at in a minute, otherwise we add its children to our queue
729 * and go on about our business.
731 while (!Lst_IsEmpty(&examine)) {
732 gn = Lst_DeQueue(&examine);
739 * Apply any .USE rules before looking for implicit
740 * dependencies to make sure everything has commands
743 LST_FOREACH(ln, &gn->children)
744 if (Make_HandleUse(Lst_Datum(ln), gn))
749 if (gn->unmade != 0) {
750 LST_FOREACH(ln, &gn->children) {
752 if (!cgn->make && !(cgn->type & OP_USE))
753 Lst_EnQueue(&examine, cgn);
756 Lst_EnQueue(&toBeMade, gn);
763 * We wouldn't do any work unless we could start some jobs in
764 * the next loop... (we won't actually start any, of course,
765 * this is just to see if any of the targets was out of date)
767 return (MakeStartJobs());
771 * Initialization. At the moment, no jobs are running and
772 * until some get started, nothing will happen since the
773 * remaining upward traversal of the graph is performed by the
774 * routines in job.c upon the finishing of a job. So we fill
775 * the Job table as much as we can before going into our loop.
781 * Main Loop: The idea here is that the ending of jobs will take
782 * care of the maintenance of data structures and the waiting for output
783 * will cause us to be idle most of the time while our children run as
784 * much as possible. Because the job table is kept as full as possible,
785 * the only time when it will be empty is when all the jobs which need
786 * running have been run, so that is the end condition of this loop.
787 * Note that the Job module will exit if there were any errors unless
788 * the keepgoing flag was given.
790 while (!Job_Empty()) {
791 Job_CatchOutput(!Lst_IsEmpty(&toBeMade));
792 Job_CatchChildren(!usePipes);
796 errors = Job_Finish();
799 * Print the final status of each target. E.g. if it wasn't made
800 * because some inferior reported an error.
802 errors = ((errors == 0) && (numNodes != 0));
803 LST_FOREACH(ln, targs)
804 MakePrintStatus(Lst_Datum(ln), errors);