Replace legacy make with bmake
[dragonfly.git] / usr.bin / make / make.c
1 /*-
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
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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
36  * SUCH DAMAGE.
37  *
38  * @(#)make.c   8.1 (Berkeley) 6/6/93
39  * $FreeBSD: src/usr.bin/make/make.c,v 1.33 2005/02/04 12:38:57 harti Exp $
40  * $DragonFly: src/usr.bin/make/make.c,v 1.33 2005/09/24 07:27:26 okumoto Exp $
41  */
42
43 /*
44  * make.c
45  *      The functions which perform the examination of targets and
46  *      their suitability for creation
47  *
48  * Interface:
49  *      Make_Run        Initialize things for the module and recreate
50  *                      whatever needs recreating. Returns true if
51  *                      work was (or would have been) done and false
52  *                      otherwise.
53  *
54  *      Make_Update     Update all parents of a given child. Performs
55  *                      various bookkeeping chores like the updating
56  *                      of the cmtime field of the parent, filling
57  *                      of the IMPSRC context variable, etc. It will
58  *                      place the parent on the toBeMade queue if it should be.
59  *
60  *      Make_TimeStamp  Function to set the parent's cmtime field
61  *                      based on a child's modification time.
62  *
63  *      Make_DoAllVar   Set up the various local variables for a
64  *                      target, including the .ALLSRC variable, making
65  *                      sure that any variable that needs to exist
66  *                      at the very least has the empty value.
67  *
68  *      Make_OODate     Determine if a target is out-of-date.
69  *
70  *      Make_HandleUse  See if a child is a .USE node for a parent
71  *                      and perform the .USE actions if so.
72  */
73
74 #include <stdlib.h>
75
76 #include "arch.h"
77 #include "config.h"
78 #include "dir.h"
79 #include "globals.h"
80 #include "GNode.h"
81 #include "job.h"
82 #include "make.h"
83 #include "parse.h"
84 #include "suff.h"
85 #include "targ.h"
86 #include "util.h"
87 #include "var.h"
88
89 /* The current fringe of the graph. These are nodes which await examination
90  * by MakeOODate. It is added to by Make_Update and subtracted from by
91  * MakeStartJobs */
92 static Lst toBeMade = Lst_Initializer(toBeMade);
93
94 /*
95  * Number of nodes to be processed. If this is non-zero when Job_Empty()
96  * returns true, there's a cycle in the graph.
97  */
98 static int numNodes;
99
100 /**
101  * Make_TimeStamp
102  *      Set the cmtime field of a parent node based on the mtime stamp in its
103  *      child. Called from MakeOODate via LST_FOREACH.
104  *
105  * Results:
106  *      Always returns 0.
107  *
108  * Side Effects:
109  *      The cmtime of the parent node will be changed if the mtime
110  *      field of the child is greater than it.
111  */
112 int
113 Make_TimeStamp(GNode *pgn, GNode *cgn)
114 {
115
116         if (cgn->mtime > pgn->cmtime) {
117                 pgn->cmtime = cgn->mtime;
118         }
119         return (0);
120 }
121
122 /**
123  * Make_OODate
124  *      See if a given node is out of date with respect to its sources.
125  *      Used by Make_Run when deciding which nodes to place on the
126  *      toBeMade queue initially and by Make_Update to screen out USE and
127  *      EXEC nodes. In the latter case, however, any other sort of node
128  *      must be considered out-of-date since at least one of its children
129  *      will have been recreated.
130  *
131  * Results:
132  *      true if the node is out of date. false otherwise.
133  *
134  * Side Effects:
135  *      The mtime field of the node and the cmtime field of its parents
136  *      will/may be changed.
137  */
138 bool
139 Make_OODate(GNode *gn)
140 {
141         bool    oodate;
142         LstNode *ln;
143
144         /*
145          * Certain types of targets needn't even be sought as their datedness
146          * doesn't depend on their modification time...
147          */
148         if ((gn->type & (OP_JOIN | OP_USE | OP_EXEC)) == 0) {
149                 Dir_MTime(gn);
150                 if (gn->mtime != 0) {
151                         DEBUGF(MAKE, ("modified %s...",
152                             Targ_FmtTime(gn->mtime)));
153                 } else {
154                         DEBUGF(MAKE, ("non-existent..."));
155                 }
156         }
157
158         /*
159          * A target is remade in one of the following circumstances:
160          *      its modification time is smaller than that of its youngest child
161          *          and it would actually be run (has commands or type OP_NOP)
162          *      it's the object of a force operator
163          *      it has no children, was on the lhs of an operator and doesn't
164          *          exist already.
165          *
166          * Libraries are only considered out-of-date if the archive module says
167          * they are.
168          *
169          * These weird rules are brought to you by Backward-Compatibility and
170          * the strange people who wrote 'Make'.
171          */
172         if (gn->type & OP_USE) {
173                 /*
174                  * If the node is a USE node it is *never* out of date
175                  * no matter *what*.
176                  */
177                 DEBUGF(MAKE, (".USE node..."));
178                 oodate = false;
179
180         } else if (gn->type & OP_LIB) {
181                 DEBUGF(MAKE, ("library..."));
182
183                 /*
184                  * always out of date if no children and :: target
185                  */
186                 oodate = Arch_LibOODate(gn) ||
187                     ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
188
189         } else if (gn->type & OP_JOIN) {
190                 /*
191                  * A target with the .JOIN attribute is only considered
192                  * out-of-date if any of its children was out-of-date.
193                  */
194                 DEBUGF(MAKE, (".JOIN node..."));
195                 oodate = gn->childMade;
196
197         } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
198                 /*
199                  * A node which is the object of the force (!) operator or
200                  * which has the .EXEC attribute is always considered
201                  * out-of-date.
202                  */
203                 if (gn->type & OP_FORCE) {
204                         DEBUGF(MAKE, ("! operator..."));
205                 } else if (gn->type & OP_PHONY) {
206                         DEBUGF(MAKE, (".PHONY node..."));
207                 } else {
208                         DEBUGF(MAKE, (".EXEC node..."));
209                 }
210                 oodate = true;
211
212         } else if ((gn->mtime < gn->cmtime) ||
213             ((gn->cmtime == 0) &&
214             ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP)))) {
215                 /*
216                  * A node whose modification time is less than that of its
217                  * youngest child or that has no children (cmtime == 0) and
218                  * either doesn't exist (mtime == 0) or was the object of a
219                  * :: operator is out-of-date. Why? Because that's the way
220                  * Make does it.
221                  */
222                 if (gn->mtime < gn->cmtime) {
223                         DEBUGF(MAKE, ("modified before source..."));
224                 } else if (gn->mtime == 0) {
225                         DEBUGF(MAKE, ("non-existent and no sources..."));
226                 } else {
227                         DEBUGF(MAKE, (":: operator and no sources..."));
228                 }
229                 oodate = true;
230         } else
231                 oodate = false;
232
233         /*
234          * If the target isn't out-of-date, the parents need to know its
235          * modification time. Note that targets that appear to be out-of-date
236          * but aren't, because they have no commands and aren't of type OP_NOP,
237          * have their mtime stay below their children's mtime to keep parents
238          * from thinking they're out-of-date.
239          */
240         if (!oodate) {
241                 LST_FOREACH(ln, &gn->parents)
242                         if (Make_TimeStamp(Lst_Datum(ln), gn))
243                                 break;
244         }
245
246         return (oodate);
247 }
248
249 /**
250  * Make_HandleUse
251  *      Function called by Make_Run and SuffApplyTransform on the downward
252  *      pass to handle .USE and transformation nodes. A callback function
253  *      for LST_FOREACH, it implements the .USE and transformation
254  *      functionality by copying the node's commands, type flags
255  *      and children to the parent node. Should be called before the
256  *      children are enqueued to be looked at.
257  *
258  *      A .USE node is much like an explicit transformation rule, except
259  *      its commands are always added to the target node, even if the
260  *      target already has commands.
261  *
262  * Results:
263  *      returns 0.
264  *
265  * Side Effects:
266  *      Children and commands may be added to the parent and the parent's
267  *      type may be changed.
268  */
269 int
270 Make_HandleUse(GNode *cgn, GNode *pgn)
271 {
272         GNode   *gn;    /* A child of the .USE node */
273         LstNode *ln;    /* An element in the children list */
274
275         if (cgn->type & (OP_USE | OP_TRANSFORM)) {
276                 if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
277                         /*
278                          * .USE or transformation and target has no commands --
279                          * append the child's commands to the parent.
280                          */
281                         Lst_Concat(&pgn->commands, &cgn->commands, LST_CONCNEW);
282                 }
283
284                 for (ln = Lst_First(&cgn->children); ln != NULL;
285                     ln = Lst_Succ(ln)) {
286                         gn = Lst_Datum(ln);
287
288                         if (Lst_Member(&pgn->children, gn) == NULL) {
289                                 Lst_AtEnd(&pgn->children, gn);
290                                 Lst_AtEnd(&gn->parents, pgn);
291                                 pgn->unmade += 1;
292                         }
293                 }
294
295                 pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
296
297                 /*
298                  * This child node is now "made", so we decrement the count of
299                  * unmade children in the parent... We also remove the child
300                  * from the parent's list to accurately reflect the number of
301                  * decent children the parent has. This is used by Make_Run to
302                  * decide whether to queue the parent or examine its children...
303                  */
304                 if (cgn->type & OP_USE) {
305                         pgn->unmade--;
306                 }
307         }
308         return (0);
309 }
310
311 /**
312  * Make_Update
313  *      Perform update on the parents of a node. Used by JobFinish once
314  *      a node has been dealt with and by MakeStartJobs if it finds an
315  *      up-to-date node.
316  *
317  * Results:
318  *      Always returns 0
319  *
320  * Side Effects:
321  *      The unmade field of pgn is decremented and pgn may be placed on
322  *      the toBeMade queue if this field becomes 0.
323  *
324  *      If the child was made, the parent's childMade field will be set true
325  *      and its cmtime set to now.
326  *
327  *      If the child wasn't made, the cmtime field of the parent will be
328  *      altered if the child's mtime is big enough.
329  *
330  *      Finally, if the child is the implied source for the parent, the
331  *      parent's IMPSRC variable is set appropriately.
332  */
333 void
334 Make_Update(GNode *cgn)
335 {
336         GNode           *pgn;   /* the parent node */
337         const char      *cname; /* the child's name */
338         LstNode         *ln;    /* Element in parents and iParents lists */
339         const char      *cpref;
340
341         cname = Var_Value(TARGET, cgn);
342
343         /*
344          * If the child was actually made, see what its modification time is
345          * now -- some rules won't actually update the file. If the file still
346          * doesn't exist, make its mtime now.
347          */
348         if (cgn->made != UPTODATE) {
349 #ifndef RECHECK
350                 /*
351                  * We can't re-stat the thing, but we can at least take care
352                  * of rules where a target depends on a source that actually
353                  * creates the target, but only if it has changed, e.g.
354                  *
355                  * parse.h : parse.o
356                  *
357                  * parse.o : parse.y
358                  *      yacc -d parse.y
359                  *      cc -c y.tab.c
360                  *      mv y.tab.o parse.o
361                  *      cmp -s y.tab.h parse.h || mv y.tab.h parse.h
362                  *
363                  * In this case, if the definitions produced by yacc haven't
364                  * changed from before, parse.h won't have been updated and
365                  * cgn->mtime will reflect the current modification time for
366                  * parse.h. This is something of a kludge, I admit, but it's a
367                  * useful one..
368                  * XXX: People like to use a rule like
369                  *
370                  * FRC:
371                  *
372                  * To force things that depend on FRC to be made, so we have to
373                  * check for gn->children being empty as well...
374                  */
375                 if (!Lst_IsEmpty(&cgn->commands) ||
376                     Lst_IsEmpty(&cgn->children)) {
377                         cgn->mtime = now;
378                 }
379         #else
380                 /*
381                  * This is what Make does and it's actually a good thing, as it
382                  * allows rules like
383                  *
384                  *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
385                  *
386                  * to function as intended. Unfortunately, thanks to the
387                  * stateless nature of NFS (by which I mean the loose coupling
388                  * of two clients using the same file from a common server),
389                  * there are times when the modification time of a file created
390                  * on a remote machine will not be modified before the local
391                  * stat() implied by the Dir_MTime occurs, thus leading us to
392                  * believe that the file is unchanged, wreaking havoc with
393                  * files that depend on this one.
394                  *
395                  * I have decided it is better to make too much than to make too
396                  * little, so this stuff is commented out unless you're sure
397                  * it's ok.
398                  * -- ardeb 1/12/88
399                  */
400                 /*
401                  * Christos, 4/9/92: If we are  saving commands pretend that
402                  * the target is made now. Otherwise archives with ... rules
403                  * don't work!
404                  */
405                 if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
406                     Dir_MTime(cgn) == 0) {
407                         cgn->mtime = now;
408                 }
409                 DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
410 #endif
411         }
412
413         for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Succ(ln)) {
414                 pgn = Lst_Datum(ln);
415                 if (pgn->make) {
416                         pgn->unmade -= 1;
417
418                         if (!(cgn->type & (OP_EXEC | OP_USE))) {
419                                 if (cgn->made == MADE) {
420                                         pgn->childMade = true;
421                                         if (pgn->cmtime < cgn->mtime) {
422                                                 pgn->cmtime = cgn->mtime;
423                                         }
424                                 } else {
425                                         Make_TimeStamp(pgn, cgn);
426                                 }
427                         }
428                         if (pgn->unmade == 0) {
429                                 /*
430                                  * Queue the node up -- any unmade predecessors
431                                  * will be dealt with in MakeStartJobs.
432                                  */
433                                 Lst_EnQueue(&toBeMade, pgn);
434                         } else if (pgn->unmade < 0) {
435                                 Error("Graph cycles through %s", pgn->name);
436                         }
437                 }
438         }
439
440         /*
441          * Deal with successor nodes. If any is marked for making and has an
442          * unmade count of 0, has not been made and isn't in the examination
443          * queue, it means we need to place it in the queue as it restrained
444          * itself before.
445          */
446         for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
447                 GNode   *succ = Lst_Datum(ln);
448
449                 if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
450                     Lst_Member(&toBeMade, succ) == NULL) {
451                         Lst_EnQueue(&toBeMade, succ);
452                 }
453         }
454
455         /*
456          * Set the .PREFIX and .IMPSRC variables for all the implied parents
457          * of this node.
458          */
459         cpref = Var_Value(PREFIX, cgn);
460         for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Succ(ln)) {
461                 pgn = Lst_Datum(ln);
462                 if (pgn->make) {
463                         Var_Set(IMPSRC, cname, pgn);
464                         Var_Set(PREFIX, cpref, pgn);
465                 }
466         }
467 }
468
469 /**
470  * Make_DoAllVar
471  *      Set up the ALLSRC and OODATE variables. Sad to say, it must be
472  *      done separately, rather than while traversing the graph. This is
473  *      because Make defined OODATE to contain all sources whose modification
474  *      times were later than that of the target, *not* those sources that
475  *      were out-of-date. Since in both compatibility and native modes,
476  *      the modification time of the parent isn't found until the child
477  *      has been dealt with, we have to wait until now to fill in the
478  *      variable. As for ALLSRC, the ordering is important and not
479  *      guaranteed when in native mode, so it must be set here, too.
480  *
481  * Side Effects:
482  *      The ALLSRC and OODATE variables of the given node is filled in.
483  *      If the node is a .JOIN node, its TARGET variable will be set to
484  *      match its ALLSRC variable.
485  */
486 void
487 Make_DoAllVar(GNode *gn)
488 {
489         LstNode *ln;
490
491         LST_FOREACH(ln, &gn->children) {
492                 /*
493                  * Add the child's name to the ALLSRC and OODATE variables of
494                  * the given node. The child is added only if it has not been
495                  * given the .EXEC, .USE or .INVISIBLE attributes. .EXEC and
496                  * .USE children are very rarely going to be files, so...
497                  *
498                  * A child is added to the OODATE variable if its modification
499                  * time is later than that of its parent, as defined by Make,
500                  * except if the parent is a .JOIN node. In that case, it is
501                  * only added to the OODATE variable if it was actually made
502                  * (since .JOIN nodes don't have modification times, the
503                  * comparison is rather unfair...).
504                  */
505                 GNode   *cgn = Lst_Datum(ln);
506
507                 if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
508                         const char      *child;
509
510                         if (OP_NOP(cgn->type)) {
511                                 /*
512                                  * this node is only source; use the specific
513                                  * pathname for it
514                                  */
515                                 child = cgn->path ? cgn->path : cgn->name;
516                         } else {
517                                 child = Var_Value(TARGET, cgn);
518                         }
519
520                         Var_Append(ALLSRC, child, gn);
521                         if (gn->type & OP_JOIN) {
522                                 if (cgn->made == MADE) {
523                                         Var_Append(OODATE, child, gn);
524                                 }
525                         } else if (gn->mtime < cgn->mtime ||
526                             (cgn->mtime >= now && cgn->made == MADE)) {
527                                 /*
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.
539                                  *
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...
545                                  */
546                                 Var_Append(OODATE, child, gn);
547                         }
548                 }
549         }
550
551         if (Var_Value(OODATE, gn) == NULL) {
552                 Var_Set(OODATE, "", gn);
553         }
554         if (Var_Value(ALLSRC, gn) == NULL) {
555                 Var_Set(ALLSRC, "", gn);
556         }
557
558         if (gn->type & OP_JOIN) {
559                 Var_Set(TARGET, Var_Value(ALLSRC, gn), gn);
560         }
561 }
562
563 /**
564  * MakeStartJobs
565  *      Start as many jobs as possible.
566  *
567  * Results:
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 1. At all other times, this function returns 0.
571  *
572  * Side Effects:
573  *      Nodes are removed from the toBeMade queue and job table slots
574  *      are filled.
575  */
576 static int
577 MakeStartJobs(bool queryFlag)
578 {
579         GNode   *gn;
580
581         while (!Lst_IsEmpty(&toBeMade) && !Job_Full()) {
582                 gn = Lst_DeQueue(&toBeMade);
583                 DEBUGF(MAKE, ("Examining %s...", gn->name));
584
585                 /*
586                  * Make sure any and all predecessors that are going to be made,
587                  * have been.
588                  */
589                 if (!Lst_IsEmpty(&gn->preds)) {
590                         LstNode *ln;
591
592                         for (ln = Lst_First(&gn->preds); ln != NULL;
593                             ln = Lst_Succ(ln)){
594                                 GNode   *pgn = Lst_Datum(ln);
595
596                                 if (pgn->make && pgn->made == UNMADE) {
597                                         DEBUGF(MAKE, ("predecessor %s not made "
598                                             "yet.\n", pgn->name));
599                                         break;
600                                 }
601                         }
602                         /*
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.
608                          */
609                         if (ln != NULL) {
610                                 continue;
611                         }
612                 }
613
614                 numNodes--;
615                 if (Make_OODate(gn)) {
616                         DEBUGF(MAKE, ("out-of-date\n"));
617                         if (queryFlag) {
618                                 return (1);     /* target was not up-to-date */
619                         }
620                         Make_DoAllVar(gn);
621                         Job_Make(gn);
622                 } else {
623                         DEBUGF(MAKE, ("up-to-date\n"));
624                         gn->made = UPTODATE;
625                         if (gn->type & OP_JOIN) {
626                                 /*
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)...
632                                  */
633                                 Make_DoAllVar(gn);
634                         }
635
636                         Make_Update(gn);
637                 }
638         }
639         return (0);     /* Successful completion */
640 }
641
642 /**
643  * MakePrintStatus
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
648  *      cycle is true.
649  *
650  * Side Effects:
651  *      A message may be printed.
652  */
653 static void
654 MakePrintStatus(GNode *gn, bool cycle)
655 {
656         LstNode *ln;
657
658         if (gn->made == UPTODATE) {
659                 printf("`%s' is up to date.\n", gn->name);
660
661         } else if (gn->unmade != 0) {
662                 if (cycle) {
663                         /*
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:
667                          *      a : b
668                          *      b : c
669                          *      c : b
670                          * will cause this to erroneously complain about a
671                          * being in the cycle, but this is a good approximation.
672                          */
673                         if (gn->made == CYCLE) {
674                                 Error("Graph cycles through `%s'", gn->name);
675                                 gn->made = ENDCYCLE;
676                                 LST_FOREACH(ln, &gn->children)
677                                         MakePrintStatus(Lst_Datum(ln), true);
678                                 gn->made = UNMADE;
679                         } else if (gn->made != ENDCYCLE) {
680                                 gn->made = CYCLE;
681                                 LST_FOREACH(ln, &gn->children)
682                                         MakePrintStatus(Lst_Datum(ln), true);
683                         }
684                 } else {
685                         printf("`%s' not remade because of errors.\n",
686                             gn->name);
687                 }
688         }
689 }
690
691 /**
692  * Make_Run
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
697  *      queue.
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
700  *      possible.
701  *
702  * Results:
703  *      Exit status of make.
704  *
705  * Side Effects:
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.
709  */
710 bool
711 Make_Run(Lst *targs, bool queryFlag)
712 {
713         GNode   *gn;            /* a temporary pointer */
714         GNode   *cgn;
715         Lst     examine;        /* List of targets to examine */
716         int     errors;         /* Number of errors the Job module reports */
717         LstNode *ln;
718
719         /*
720          * Initialize job module before traversing
721          * the graph, now that any .BEGIN and .END
722          * targets have been read.  This is done
723          * only if the -q flag wasn't given (to
724          * prevent the .BEGIN from being executed
725          * should it exist).
726          */
727         if (!queryFlag) {
728                 Job_Init(jobLimit);
729                 jobsRunning = true;
730         }
731
732         Lst_Init(&examine);
733         Lst_Duplicate(&examine, targs, NOCOPY);
734         numNodes = 0;
735
736         /*
737          * Make an initial downward pass over the graph, marking nodes to be
738          * made as we go down. We call Suff_FindDeps to find where a node is and
739          * to get some children for it if it has none and also has no commands.
740          * If the node is a leaf, we stick it on the toBeMade queue to
741          * be looked at in a minute, otherwise we add its children to our queue
742          * and go on about our business.
743          */
744         while (!Lst_IsEmpty(&examine)) {
745                 gn = Lst_DeQueue(&examine);
746
747                 if (!gn->make) {
748                         gn->make = true;
749                         numNodes++;
750
751                         /*
752                          * Apply any .USE rules before looking for implicit
753                          * dependencies to make sure everything has commands
754                          * that should...
755                          */
756                         LST_FOREACH(ln, &gn->children)
757                                 if (Make_HandleUse(Lst_Datum(ln), gn))
758                                         break;
759
760                         Suff_FindDeps(gn);
761
762                         if (gn->unmade != 0) {
763                                 LST_FOREACH(ln, &gn->children) {
764                                         cgn = Lst_Datum(ln);
765                                         if (!cgn->make && !(cgn->type & OP_USE))
766                                                 Lst_EnQueue(&examine, cgn);
767                                 }
768                         } else {
769                                 Lst_EnQueue(&toBeMade, gn);
770                         }
771                 }
772         }
773
774         if (queryFlag) {
775                 /*
776                  * We wouldn't do any work unless we could start some jobs in
777                  * the next loop... (we won't actually start any, of course,
778                  * this is just to see if any of the targets was out of date)
779                  */
780                 return MakeStartJobs(true);
781         }
782
783         /*
784          * Initialization. At the moment, no jobs are running and
785          * until some get started, nothing will happen since the
786          * remaining upward traversal of the graph is performed by the
787          * routines in job.c upon the finishing of a job. So we fill
788          * the Job table as much as we can before going into our loop.
789          */
790         MakeStartJobs(false);
791
792         /*
793          * Main Loop: The idea here is that the ending of jobs will take
794          * care of the maintenance of data structures and the waiting for output
795          * will cause us to be idle most of the time while our children run as
796          * much as possible. Because the job table is kept as full as possible,
797          * the only time when it will be empty is when all the jobs which need
798          * running have been run, so that is the end condition of this loop.
799          * Note that the Job module will exit if there were any errors unless
800          * the keepgoing flag was given.
801          */
802         while (!Job_Empty()) {
803                 Job_CatchOutput(!Lst_IsEmpty(&toBeMade));
804                 Job_CatchChildren(!usePipes);
805                 MakeStartJobs(false);
806         }
807
808         errors = Job_Finish();
809
810         /*
811          * Print the final status of each target. E.g. if it wasn't made
812          * because some inferior reported an error.
813          */
814         errors = ((errors == 0) && (numNodes != 0));
815         LST_FOREACH(ln, targs)
816                 MakePrintStatus(Lst_Datum(ln), errors);
817
818         return 0;       /* Successful completion */
819 }