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