[BACK]Return to make.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/make.c, Revision 1.21

1.21    ! espie       1: /*     $OpenBSD: make.c,v 1.20 2000/06/23 16:18:09 espie Exp $ */
1.6       millert     2: /*     $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $       */
1.1       deraadt     3:
                      4: /*
1.4       millert     5:  * Copyright (c) 1988, 1989, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  * Copyright (c) 1989 by Berkeley Softworks
                      8:  * All rights reserved.
                      9:  *
                     10:  * This code is derived from software contributed to Berkeley by
                     11:  * Adam de Boor.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
                     21:  * 3. All advertising materials mentioning features or use of this software
                     22:  *    must display the following acknowledgement:
                     23:  *     This product includes software developed by the University of
                     24:  *     California, Berkeley and its contributors.
                     25:  * 4. Neither the name of the University nor the names of its contributors
                     26:  *    may be used to endorse or promote products derived from this software
                     27:  *    without specific prior written permission.
                     28:  *
                     29:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     30:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     31:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     32:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     33:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     34:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     35:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     36:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     37:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     38:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     39:  * SUCH DAMAGE.
                     40:  */
                     41:
                     42: /*-
                     43:  * make.c --
                     44:  *     The functions which perform the examination of targets and
                     45:  *     their suitability for creation
                     46:  *
                     47:  * Interface:
                     48:  *     Make_Run                Initialize things for the module and recreate
                     49:  *                             whatever needs recreating. Returns TRUE if
                     50:  *                             work was (or would have been) done and FALSE
                     51:  *                             otherwise.
                     52:  *
                     53:  *     Make_Update             Update all parents of a given child. Performs
                     54:  *                             various bookkeeping chores like the updating
                     55:  *                             of the cmtime field of the parent, filling
                     56:  *                             of the IMPSRC context variable, etc. It will
                     57:  *                             place the parent on the toBeMade queue if it
                     58:  *                             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:  *
1.5       millert    70:  *     Make_HandleUse          See if a child is a .USE node for a parent
1.1       deraadt    71:  *                             and perform the .USE actions if so.
                     72:  */
                     73:
                     74: #include    "make.h"
                     75: #include    "hash.h"
                     76: #include    "dir.h"
                     77: #include    "job.h"
1.21    ! espie      78:
        !            79: #ifndef lint
        !            80: #if 0
        !            81: static char sccsid[] = "@(#)make.c     8.1 (Berkeley) 6/6/93";
        !            82: #else
        !            83: UNUSED
        !            84: static char rcsid[] = "$OpenBSD: make.c,v 1.20 2000/06/23 16:18:09 espie Exp $";
        !            85: #endif
        !            86: #endif /* not lint */
1.1       deraadt    87:
1.17      espie      88: static LIST     toBeMade;      /* The current fringe of the graph. These
1.1       deraadt    89:                                 * are nodes which await examination by
                     90:                                 * MakeOODate. It is added to by
                     91:                                 * Make_Update and subtracted from by
                     92:                                 * MakeStartJobs */
                     93: static int     numNodes;       /* Number of nodes to be processed. If this
                     94:                                 * is non-zero when Job_Empty() returns
                     95:                                 * TRUE, there's a cycle in the graph */
                     96:
1.16      espie      97: static void MakeAddChild __P((void *, void *));
                     98: static void MakeAddAllSrc __P((void *, void *));
                     99: static void MakeTimeStamp __P((void *, void *));
                    100: static void MakeHandleUse __P((void *, void *));
1.1       deraadt   101: static Boolean MakeStartJobs __P((void));
1.16      espie     102: static void MakePrintStatus __P((void *, void *));
1.1       deraadt   103: /*-
                    104:  *-----------------------------------------------------------------------
                    105:  * Make_TimeStamp --
                    106:  *     Set the cmtime field of a parent node based on the mtime stamp in its
1.4       millert   107:  *     child. Called from MakeOODate via Lst_ForEach.
1.1       deraadt   108:  *
                    109:  * Results:
1.4       millert   110:  *     Always returns 0.
1.1       deraadt   111:  *
                    112:  * Side Effects:
                    113:  *     The cmtime of the parent node will be changed if the mtime
                    114:  *     field of the child is greater than it.
                    115:  *-----------------------------------------------------------------------
                    116:  */
1.15      espie     117: void
                    118: Make_TimeStamp(pgn, cgn)
1.1       deraadt   119:     GNode *pgn;        /* the current parent */
                    120:     GNode *cgn;        /* the child we've just examined */
                    121: {
1.15      espie     122:     if (cgn->mtime > pgn->cmtime)
1.1       deraadt   123:        pgn->cmtime = cgn->mtime;
                    124: }
                    125:
1.15      espie     126: static void
                    127: MakeTimeStamp(pgn, cgn)
1.16      espie     128:     void *pgn; /* the current parent */
                    129:     void *cgn; /* the child we've just examined */
1.1       deraadt   130: {
1.15      espie     131:     Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
1.1       deraadt   132: }
                    133: 
                    134: /*-
                    135:  *-----------------------------------------------------------------------
                    136:  * Make_OODate --
                    137:  *     See if a given node is out of date with respect to its sources.
                    138:  *     Used by Make_Run when deciding which nodes to place on the
                    139:  *     toBeMade queue initially and by Make_Update to screen out USE and
                    140:  *     EXEC nodes. In the latter case, however, any other sort of node
                    141:  *     must be considered out-of-date since at least one of its children
                    142:  *     will have been recreated.
                    143:  *
                    144:  * Results:
1.4       millert   145:  *     TRUE if the node is out of date. FALSE otherwise.
1.1       deraadt   146:  *
                    147:  * Side Effects:
                    148:  *     The mtime field of the node and the cmtime field of its parents
                    149:  *     will/may be changed.
                    150:  *-----------------------------------------------------------------------
                    151:  */
                    152: Boolean
                    153: Make_OODate (gn)
                    154:     register GNode *gn;              /* the node to check */
                    155: {
                    156:     Boolean         oodate;
                    157:
                    158:     /*
                    159:      * Certain types of targets needn't even be sought as their datedness
                    160:      * doesn't depend on their modification time...
                    161:      */
                    162:     if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
                    163:        (void) Dir_MTime (gn);
                    164:        if (DEBUG(MAKE)) {
1.13      espie     165:            if (gn->mtime != OUT_OF_DATE) {
1.1       deraadt   166:                printf ("modified %s...", Targ_FmtTime(gn->mtime));
                    167:            } else {
                    168:                printf ("non-existent...");
                    169:            }
                    170:        }
                    171:     }
                    172:
                    173:     /*
                    174:      * A target is remade in one of the following circumstances:
                    175:      * its modification time is smaller than that of its youngest child
                    176:      *     and it would actually be run (has commands or type OP_NOP)
                    177:      * it's the object of a force operator
                    178:      * it has no children, was on the lhs of an operator and doesn't exist
                    179:      *     already.
                    180:      *
                    181:      * Libraries are only considered out-of-date if the archive module says
                    182:      * they are.
                    183:      *
                    184:      * These weird rules are brought to you by Backward-Compatability and
                    185:      * the strange people who wrote 'Make'.
                    186:      */
                    187:     if (gn->type & OP_USE) {
                    188:        /*
                    189:         * If the node is a USE node it is *never* out of date
                    190:         * no matter *what*.
                    191:         */
                    192:        if (DEBUG(MAKE)) {
                    193:            printf(".USE node...");
                    194:        }
                    195:        oodate = FALSE;
1.4       millert   196:     } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.1       deraadt   197:        if (DEBUG(MAKE)) {
                    198:            printf("library...");
                    199:        }
                    200:
                    201:        /*
                    202:         * always out of date if no children and :: target
                    203:         */
                    204:
                    205:        oodate = Arch_LibOODate (gn) ||
1.13      espie     206:            (gn->cmtime == OUT_OF_DATE && (gn->type & OP_DOUBLEDEP));
1.1       deraadt   207:     } else if (gn->type & OP_JOIN) {
                    208:        /*
                    209:         * A target with the .JOIN attribute is only considered
                    210:         * out-of-date if any of its children was out-of-date.
                    211:         */
                    212:        if (DEBUG(MAKE)) {
                    213:            printf(".JOIN node...");
                    214:        }
                    215:        oodate = gn->childMade;
1.2       niklas    216:     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
1.1       deraadt   217:        /*
                    218:         * A node which is the object of the force (!) operator or which has
                    219:         * the .EXEC attribute is always considered out-of-date.
                    220:         */
                    221:        if (DEBUG(MAKE)) {
                    222:            if (gn->type & OP_FORCE) {
                    223:                printf("! operator...");
1.2       niklas    224:            } else if (gn->type & OP_PHONY) {
                    225:                printf(".PHONY node...");
1.1       deraadt   226:            } else {
                    227:                printf(".EXEC node...");
                    228:            }
                    229:        }
                    230:        oodate = TRUE;
1.13      espie     231:     } else if (gn->mtime < gn->cmtime ||
                    232:               (gn->cmtime == OUT_OF_DATE &&
                    233:                (gn->mtime == OUT_OF_DATE || (gn->type & OP_DOUBLEDEP))))
1.1       deraadt   234:     {
                    235:        /*
                    236:         * A node whose modification time is less than that of its
1.13      espie     237:         * youngest child or that has no children (cmtime == OUT_OF_DATE) and
                    238:         * either doesn't exist (mtime == OUT_OF_DATE) or was the object of a
1.1       deraadt   239:         * :: operator is out-of-date. Why? Because that's the way Make does
                    240:         * it.
                    241:         */
                    242:        if (DEBUG(MAKE)) {
                    243:            if (gn->mtime < gn->cmtime) {
                    244:                printf("modified before source...");
1.13      espie     245:            } else if (gn->mtime == OUT_OF_DATE) {
1.1       deraadt   246:                printf("non-existent and no sources...");
                    247:            } else {
                    248:                printf(":: operator and no sources...");
                    249:            }
                    250:        }
                    251:        oodate = TRUE;
                    252:     } else {
                    253: #if 0
                    254:        /* WHY? */
                    255:        if (DEBUG(MAKE)) {
                    256:            printf("source %smade...", gn->childMade ? "" : "not ");
                    257:        }
                    258:        oodate = gn->childMade;
                    259: #else
                    260:        oodate = FALSE;
                    261: #endif /* 0 */
                    262:     }
                    263:
                    264:     /*
                    265:      * If the target isn't out-of-date, the parents need to know its
                    266:      * modification time. Note that targets that appear to be out-of-date
                    267:      * but aren't, because they have no commands and aren't of type OP_NOP,
                    268:      * have their mtime stay below their children's mtime to keep parents from
                    269:      * thinking they're out-of-date.
                    270:      */
1.15      espie     271:     if (!oodate)
1.17      espie     272:        Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1       deraadt   273:
                    274:     return (oodate);
                    275: }
                    276: 
                    277: /*-
                    278:  *-----------------------------------------------------------------------
                    279:  * MakeAddChild  --
                    280:  *     Function used by Make_Run to add a child to the list l.
                    281:  *     It will only add the child if its make field is FALSE.
                    282:  *
                    283:  * Side Effects:
                    284:  *     The given list is extended
                    285:  *-----------------------------------------------------------------------
                    286:  */
1.15      espie     287: static void
                    288: MakeAddChild(gnp, lp)
1.16      espie     289:     void *gnp;         /* the node to add */
                    290:     void *lp;          /* the list to which to add it */
1.1       deraadt   291: {
1.15      espie     292:     GNode          *gn = (GNode *)gnp;
                    293:     Lst            l = (Lst)lp;
1.5       millert   294:
1.15      espie     295:     if (!gn->make && !(gn->type & OP_USE))
1.14      espie     296:        Lst_EnQueue(l, gn);
1.1       deraadt   297: }
1.5       millert   298: 
                    299: /*-
                    300:  *-----------------------------------------------------------------------
1.1       deraadt   301:  * Make_HandleUse --
                    302:  *     Function called by Make_Run and SuffApplyTransform on the downward
                    303:  *     pass to handle .USE and transformation nodes. A callback function
                    304:  *     for Lst_ForEach, it implements the .USE and transformation
                    305:  *     functionality by copying the node's commands, type flags
                    306:  *     and children to the parent node. Should be called before the
                    307:  *     children are enqueued to be looked at by MakeAddChild.
                    308:  *
                    309:  *     A .USE node is much like an explicit transformation rule, except
                    310:  *     its commands are always added to the target node, even if the
                    311:  *     target already has commands.
                    312:  *
                    313:  * Side Effects:
                    314:  *     Children and commands may be added to the parent and the parent's
                    315:  *     type may be changed.
                    316:  *
                    317:  *-----------------------------------------------------------------------
                    318:  */
1.15      espie     319: void
                    320: Make_HandleUse(cgn, pgn)
                    321:     GNode      *cgn;   /* The .USE node */
                    322:     GNode      *pgn;   /* The target of the .USE node */
1.1       deraadt   323: {
1.15      espie     324:     GNode      *gn;    /* A child of the .USE node */
                    325:     LstNode    ln;     /* An element in the children list */
1.1       deraadt   326:
                    327:     if (cgn->type & (OP_USE|OP_TRANSFORM)) {
1.17      espie     328:        if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
1.1       deraadt   329:            /*
                    330:             * .USE or transformation and target has no commands -- append
                    331:             * the child's commands to the parent.
                    332:             */
1.18      espie     333:            Lst_Concat(&pgn->commands, &cgn->commands);
1.1       deraadt   334:        }
1.4       millert   335:
1.19      espie     336:        Lst_Open(&cgn->children);
                    337:        while ((ln = Lst_Next(&cgn->children)) != NULL) {
                    338:            gn = (GNode *)Lst_Datum(ln);
                    339:
                    340:            if (Lst_Member(&pgn->children, gn) == NULL) {
                    341:                Lst_AtEnd(&pgn->children, gn);
                    342:                Lst_AtEnd(&gn->parents, pgn);
                    343:                pgn->unmade += 1;
1.1       deraadt   344:            }
                    345:        }
1.19      espie     346:        Lst_Close(&cgn->children);
1.4       millert   347:
1.1       deraadt   348:        pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
                    349:
                    350:        /*
                    351:         * This child node is now "made", so we decrement the count of
                    352:         * unmade children in the parent... We also remove the child
                    353:         * from the parent's list to accurately reflect the number of decent
                    354:         * children the parent has. This is used by Make_Run to decide
                    355:         * whether to queue the parent or examine its children...
                    356:         */
1.6       millert   357:        if (cgn->type & OP_USE) {
1.5       millert   358:            pgn->unmade--;
1.1       deraadt   359:        }
                    360:     }
                    361: }
1.15      espie     362: static void
                    363: MakeHandleUse(pgn, cgn)
1.16      espie     364:     void *pgn; /* the current parent */
                    365:     void *cgn; /* the child we've just examined */
1.1       deraadt   366: {
1.15      espie     367:     Make_HandleUse((GNode *)pgn, (GNode *)cgn);
1.1       deraadt   368: }
                    369: 
                    370: /*-
                    371:  *-----------------------------------------------------------------------
                    372:  * Make_Update  --
                    373:  *     Perform update on the parents of a node. Used by JobFinish once
                    374:  *     a node has been dealt with and by MakeStartJobs if it finds an
1.4       millert   375:  *     up-to-date node.
1.1       deraadt   376:  *
                    377:  * Results:
                    378:  *     Always returns 0
                    379:  *
                    380:  * Side Effects:
                    381:  *     The unmade field of pgn is decremented and pgn may be placed on
                    382:  *     the toBeMade queue if this field becomes 0.
                    383:  *
                    384:  *     If the child was made, the parent's childMade field will be set true
                    385:  *     and its cmtime set to now.
                    386:  *
                    387:  *     If the child wasn't made, the cmtime field of the parent will be
                    388:  *     altered if the child's mtime is big enough.
                    389:  *
                    390:  *     Finally, if the child is the implied source for the parent, the
                    391:  *     parent's IMPSRC variable is set appropriately.
                    392:  *
                    393:  *-----------------------------------------------------------------------
                    394:  */
                    395: void
                    396: Make_Update (cgn)
                    397:     register GNode *cgn;       /* the child node */
                    398: {
                    399:     register GNode     *pgn;   /* the parent node */
                    400:     register char      *cname; /* the child's name */
                    401:     register LstNode   ln;     /* Element in parents and iParents lists */
                    402:
1.20      espie     403:     cname = Varq_Value(TARGET_INDEX, cgn);
1.1       deraadt   404:
                    405:     /*
                    406:      * If the child was actually made, see what its modification time is
                    407:      * now -- some rules won't actually update the file. If the file still
                    408:      * doesn't exist, make its mtime now.
                    409:      */
                    410:     if (cgn->made != UPTODATE) {
                    411: #ifndef RECHECK
                    412:        /*
                    413:         * We can't re-stat the thing, but we can at least take care of rules
                    414:         * where a target depends on a source that actually creates the
                    415:         * target, but only if it has changed, e.g.
                    416:         *
                    417:         * parse.h : parse.o
                    418:         *
                    419:         * parse.o : parse.y
                    420:         *      yacc -d parse.y
                    421:         *      cc -c y.tab.c
                    422:         *      mv y.tab.o parse.o
                    423:         *      cmp -s y.tab.h parse.h || mv y.tab.h parse.h
                    424:         *
                    425:         * In this case, if the definitions produced by yacc haven't changed
                    426:         * from before, parse.h won't have been updated and cgn->mtime will
                    427:         * reflect the current modification time for parse.h. This is
                    428:         * something of a kludge, I admit, but it's a useful one..
                    429:         * XXX: People like to use a rule like
                    430:         *
                    431:         * FRC:
                    432:         *
                    433:         * To force things that depend on FRC to be made, so we have to
                    434:         * check for gn->children being empty as well...
                    435:         */
1.17      espie     436:        if (!Lst_IsEmpty(&cgn->commands) || Lst_IsEmpty(&cgn->children)) {
1.1       deraadt   437:            cgn->mtime = now;
                    438:        }
                    439: #else
                    440:        /*
                    441:         * This is what Make does and it's actually a good thing, as it
                    442:         * allows rules like
                    443:         *
                    444:         *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    445:         *
                    446:         * to function as intended. Unfortunately, thanks to the stateless
                    447:         * nature of NFS (by which I mean the loose coupling of two clients
                    448:         * using the same file from a common server), there are times
                    449:         * when the modification time of a file created on a remote
                    450:         * machine will not be modified before the local stat() implied by
                    451:         * the Dir_MTime occurs, thus leading us to believe that the file
                    452:         * is unchanged, wreaking havoc with files that depend on this one.
                    453:         *
                    454:         * I have decided it is better to make too much than to make too
                    455:         * little, so this stuff is commented out unless you're sure it's ok.
                    456:         * -- ardeb 1/12/88
                    457:         */
                    458:        /*
                    459:         * Christos, 4/9/92: If we are  saving commands pretend that
                    460:         * the target is made now. Otherwise archives with ... rules
                    461:         * don't work!
                    462:         */
1.13      espie     463:        if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == FALSE) {
1.1       deraadt   464:            cgn->mtime = now;
                    465:        }
                    466:        if (DEBUG(MAKE)) {
                    467:            printf("update time: %s\n", Targ_FmtTime(cgn->mtime));
                    468:        }
                    469: #endif
                    470:     }
1.4       millert   471:
1.19      espie     472:     Lst_Open(&cgn->parents);
                    473:     while ((ln = Lst_Next(&cgn->parents)) != NULL) {
                    474:        pgn = (GNode *)Lst_Datum(ln);
                    475:        if (pgn->make) {
                    476:            pgn->unmade -= 1;
                    477:
                    478:            if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
                    479:                if (cgn->made == MADE) {
                    480:                    pgn->childMade = TRUE;
                    481:                    if (pgn->cmtime < cgn->mtime) {
                    482:                        pgn->cmtime = cgn->mtime;
1.1       deraadt   483:                    }
1.19      espie     484:                } else {
                    485:                    (void)Make_TimeStamp (pgn, cgn);
1.1       deraadt   486:                }
1.19      espie     487:            }
                    488:            if (pgn->unmade == 0) {
                    489:                /*
                    490:                 * Queue the node up -- any unmade predecessors will
                    491:                 * be dealt with in MakeStartJobs.
                    492:                 */
                    493:                Lst_EnQueue(&toBeMade, pgn);
                    494:            } else if (pgn->unmade < 0) {
                    495:                Error ("Graph cycles through %s", pgn->name);
1.1       deraadt   496:            }
                    497:        }
                    498:     }
1.19      espie     499:     Lst_Close(&cgn->parents);
1.1       deraadt   500:     /*
                    501:      * Deal with successor nodes. If any is marked for making and has an unmade
                    502:      * count of 0, has not been made and isn't in the examination queue,
                    503:      * it means we need to place it in the queue as it restrained itself
                    504:      * before.
                    505:      */
1.19      espie     506:     for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
1.1       deraadt   507:        GNode   *succ = (GNode *)Lst_Datum(ln);
                    508:
                    509:        if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
1.17      espie     510:            Lst_Member(&toBeMade, succ) == NULL)
                    511:            Lst_EnQueue(&toBeMade, succ);
1.1       deraadt   512:     }
1.4       millert   513:
1.1       deraadt   514:     /*
                    515:      * Set the .PREFIX and .IMPSRC variables for all the implied parents
                    516:      * of this node.
                    517:      */
1.19      espie     518:     {
1.20      espie     519:     char       *cpref = Varq_Value(PREFIX_INDEX, cgn);
1.1       deraadt   520:
1.19      espie     521:        Lst_Open(&cgn->iParents);
1.17      espie     522:        while ((ln = Lst_Next(&cgn->iParents)) != NULL) {
1.1       deraadt   523:            pgn = (GNode *)Lst_Datum (ln);
                    524:            if (pgn->make) {
1.20      espie     525:                Varq_Set(IMPSRC_INDEX, cname, pgn);
                    526:                Varq_Set(PREFIX_INDEX, cpref, pgn);
1.1       deraadt   527:            }
                    528:        }
1.17      espie     529:        Lst_Close(&cgn->iParents);
1.1       deraadt   530:     }
                    531: }
                    532: 
                    533: /*-
                    534:  *-----------------------------------------------------------------------
                    535:  * MakeAddAllSrc --
                    536:  *     Add a child's name to the ALLSRC and OODATE variables of the given
                    537:  *     node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
                    538:  *     if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
                    539:  *     .EXEC and .USE children are very rarely going to be files, so...
                    540:  *     A child is added to the OODATE variable if its modification time is
                    541:  *     later than that of its parent, as defined by Make, except if the
                    542:  *     parent is a .JOIN node. In that case, it is only added to the OODATE
                    543:  *     variable if it was actually made (since .JOIN nodes don't have
                    544:  *     modification times, the comparison is rather unfair...)..
                    545:  *
                    546:  * Side Effects:
                    547:  *     The ALLSRC variable for the given node is extended.
                    548:  *-----------------------------------------------------------------------
                    549:  */
1.15      espie     550: static void
                    551: MakeAddAllSrc(cgnp, pgnp)
1.16      espie     552:     void *cgnp;                /* The child to add */
                    553:     void *pgnp;                /* The parent to whose ALLSRC variable it should be */
1.1       deraadt   554:                        /* added */
                    555: {
                    556:     GNode      *cgn = (GNode *) cgnp;
                    557:     GNode      *pgn = (GNode *) pgnp;
                    558:     if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
                    559:        char *child;
                    560:
1.5       millert   561:        if (OP_NOP(cgn->type) ||
1.20      espie     562:            (child = Varq_Value(TARGET_INDEX, cgn)) == NULL) {
1.3       briggs    563:            /*
                    564:             * this node is only source; use the specific pathname for it
                    565:             */
                    566:            child = cgn->path ? cgn->path : cgn->name;
                    567:        }
1.5       millert   568:
1.20      espie     569:        Varq_Append(ALLSRC_INDEX, child, pgn);
1.1       deraadt   570:        if (pgn->type & OP_JOIN) {
1.20      espie     571:            if (cgn->made == MADE)
                    572:                Varq_Append(OODATE_INDEX, child, pgn);
1.1       deraadt   573:        } else if ((pgn->mtime < cgn->mtime) ||
                    574:                   (cgn->mtime >= now && cgn->made == MADE))
                    575:        {
                    576:            /*
                    577:             * It goes in the OODATE variable if the parent is younger than the
                    578:             * child or if the child has been modified more recently than
                    579:             * the start of the make. This is to keep pmake from getting
                    580:             * confused if something else updates the parent after the
                    581:             * make starts (shouldn't happen, I know, but sometimes it
                    582:             * does). In such a case, if we've updated the kid, the parent
                    583:             * is likely to have a modification time later than that of
                    584:             * the kid and anything that relies on the OODATE variable will
                    585:             * be hosed.
                    586:             *
                    587:             * XXX: This will cause all made children to go in the OODATE
                    588:             * variable, even if they're not touched, if RECHECK isn't defined,
                    589:             * since cgn->mtime is set to now in Make_Update. According to
                    590:             * some people, this is good...
                    591:             */
1.20      espie     592:            Varq_Append(OODATE_INDEX, child, pgn);
1.1       deraadt   593:        }
                    594:     }
                    595: }
                    596: 
                    597: /*-
                    598:  *-----------------------------------------------------------------------
                    599:  * Make_DoAllVar --
                    600:  *     Set up the ALLSRC and OODATE variables. Sad to say, it must be
                    601:  *     done separately, rather than while traversing the graph. This is
                    602:  *     because Make defined OODATE to contain all sources whose modification
                    603:  *     times were later than that of the target, *not* those sources that
                    604:  *     were out-of-date. Since in both compatibility and native modes,
                    605:  *     the modification time of the parent isn't found until the child
                    606:  *     has been dealt with, we have to wait until now to fill in the
                    607:  *     variable. As for ALLSRC, the ordering is important and not
                    608:  *     guaranteed when in native mode, so it must be set here, too.
                    609:  *
                    610:  * Results:
                    611:  *     None
                    612:  *
                    613:  * Side Effects:
                    614:  *     The ALLSRC and OODATE variables of the given node is filled in.
                    615:  *     If the node is a .JOIN node, its TARGET variable will be set to
                    616:  *     match its ALLSRC variable.
                    617:  *-----------------------------------------------------------------------
                    618:  */
                    619: void
                    620: Make_DoAllVar (gn)
                    621:     GNode      *gn;
                    622: {
1.17      espie     623:     Lst_ForEach(&gn->children, MakeAddAllSrc, gn);
1.1       deraadt   624:
1.20      espie     625:     if (!Varq_Exists(OODATE_INDEX, gn))
                    626:        Varq_Set(OODATE_INDEX, "", gn);
                    627:     if (!Varq_Exists(ALLSRC_INDEX, gn))
                    628:        Varq_Set(ALLSRC_INDEX, "", gn);
1.1       deraadt   629:
1.8       espie     630:     if (gn->type & OP_JOIN)
1.20      espie     631:        Varq_Set(TARGET_INDEX, Varq_Value(ALLSRC_INDEX, gn), gn);
1.1       deraadt   632: }
                    633: 
                    634: /*-
                    635:  *-----------------------------------------------------------------------
                    636:  * MakeStartJobs --
                    637:  *     Start as many jobs as possible.
                    638:  *
                    639:  * Results:
                    640:  *     If the query flag was given to pmake, no job will be started,
                    641:  *     but as soon as an out-of-date target is found, this function
                    642:  *     returns TRUE. At all other times, this function returns FALSE.
                    643:  *
                    644:  * Side Effects:
                    645:  *     Nodes are removed from the toBeMade queue and job table slots
                    646:  *     are filled.
                    647:  *
                    648:  *-----------------------------------------------------------------------
                    649:  */
                    650: static Boolean
                    651: MakeStartJobs ()
                    652: {
                    653:     register GNode     *gn;
1.4       millert   654:
1.17      espie     655:     while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
1.1       deraadt   656:        if (DEBUG(MAKE)) {
                    657:            printf ("Examining %s...", gn->name);
                    658:        }
                    659:        /*
                    660:         * Make sure any and all predecessors that are going to be made,
                    661:         * have been.
                    662:         */
1.17      espie     663:        if (!Lst_IsEmpty(&gn->preds)) {
1.1       deraadt   664:            LstNode ln;
                    665:
1.19      espie     666:            for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)){
1.1       deraadt   667:                GNode   *pgn = (GNode *)Lst_Datum(ln);
                    668:
                    669:                if (pgn->make && pgn->made == UNMADE) {
                    670:                    if (DEBUG(MAKE)) {
                    671:                        printf("predecessor %s not made yet.\n", pgn->name);
                    672:                    }
                    673:                    break;
                    674:                }
                    675:            }
                    676:            /*
1.10      espie     677:             * If ln isn't null, there's a predecessor as yet unmade, so we
1.1       deraadt   678:             * just drop this node on the floor. When the node in question
                    679:             * has been made, it will notice this node as being ready to
                    680:             * make but as yet unmade and will place the node on the queue.
                    681:             */
1.10      espie     682:            if (ln != NULL) {
1.1       deraadt   683:                continue;
                    684:            }
                    685:        }
1.4       millert   686:
1.1       deraadt   687:        numNodes--;
                    688:        if (Make_OODate (gn)) {
                    689:            if (DEBUG(MAKE)) {
                    690:                printf ("out-of-date\n");
                    691:            }
                    692:            if (queryFlag) {
                    693:                return (TRUE);
                    694:            }
                    695:            Make_DoAllVar (gn);
                    696:            Job_Make (gn);
                    697:        } else {
                    698:            if (DEBUG(MAKE)) {
                    699:                printf ("up-to-date\n");
                    700:            }
                    701:            gn->made = UPTODATE;
                    702:            if (gn->type & OP_JOIN) {
                    703:                /*
                    704:                 * Even for an up-to-date .JOIN node, we need it to have its
                    705:                 * context variables so references to it get the correct
                    706:                 * value for .TARGET when building up the context variables
                    707:                 * of its parent(s)...
                    708:                 */
                    709:                Make_DoAllVar (gn);
                    710:            }
1.4       millert   711:
1.1       deraadt   712:            Make_Update (gn);
                    713:        }
                    714:     }
                    715:     return (FALSE);
                    716: }
                    717: 
                    718: /*-
                    719:  *-----------------------------------------------------------------------
                    720:  * MakePrintStatus --
                    721:  *     Print the status of a top-level node, viz. it being up-to-date
                    722:  *     already or not created due to an error in a lower level.
                    723:  *     Callback function for Make_Run via Lst_ForEach.
                    724:  *-----------------------------------------------------------------------
                    725:  */
1.15      espie     726: static void
1.1       deraadt   727: MakePrintStatus(gnp, cyclep)
1.16      espie     728:     void       *gnp;       /* Node to examine */
                    729:     void       *cyclep;    /* True if gn->unmade being non-zero implies
1.1       deraadt   730:                             * a cycle in the graph, not an error in an
                    731:                             * inferior */
                    732: {
                    733:     GNode      *gn = (GNode *) gnp;
                    734:     Boolean    cycle = *(Boolean *) cyclep;
                    735:     if (gn->made == UPTODATE) {
                    736:        printf ("`%s' is up to date.\n", gn->name);
                    737:     } else if (gn->unmade != 0) {
                    738:        if (cycle) {
                    739:            Boolean t = TRUE;
                    740:            /*
                    741:             * If printing cycles and came to one that has unmade children,
                    742:             * print out the cycle by recursing on its children. Note a
                    743:             * cycle like:
                    744:             *  a : b
                    745:             *  b : c
                    746:             *  c : b
                    747:             * will cause this to erroneously complain about a being in
                    748:             * the cycle, but this is a good approximation.
                    749:             */
                    750:            if (gn->made == CYCLE) {
                    751:                Error("Graph cycles through `%s'", gn->name);
                    752:                gn->made = ENDCYCLE;
1.17      espie     753:                Lst_ForEach(&gn->children, MakePrintStatus, &t);
1.1       deraadt   754:                gn->made = UNMADE;
                    755:            } else if (gn->made != ENDCYCLE) {
                    756:                gn->made = CYCLE;
1.17      espie     757:                Lst_ForEach(&gn->children, MakePrintStatus, &t);
1.1       deraadt   758:            }
                    759:        } else {
                    760:            printf ("`%s' not remade because of errors.\n", gn->name);
                    761:        }
                    762:     }
                    763: }
                    764: 
1.5       millert   765:
1.1       deraadt   766: /*-
                    767:  *-----------------------------------------------------------------------
1.6       millert   768:  * Make_Run --
                    769:  *     Initialize the nodes to remake and the list of nodes which are
                    770:  *     ready to be made by doing a breadth-first traversal of the graph
                    771:  *     starting from the nodes in the given list. Once this traversal
                    772:  *     is finished, all the 'leaves' of the graph are in the toBeMade
                    773:  *     queue.
                    774:  *     Using this queue and the Job module, work back up the graph,
                    775:  *     calling on MakeStartJobs to keep the job table as full as
                    776:  *     possible.
                    777:  *
1.1       deraadt   778:  * Results:
1.6       millert   779:  *     TRUE if work was done. FALSE otherwise.
1.1       deraadt   780:  *
                    781:  * Side Effects:
1.6       millert   782:  *     The make field of all nodes involved in the creation of the given
                    783:  *     targets is set to 1. The toBeMade list is set to contain all the
                    784:  *     'leaves' of these subgraphs.
1.1       deraadt   785:  *-----------------------------------------------------------------------
                    786:  */
1.6       millert   787: Boolean
1.17      espie     788: Make_Run(targs)
1.1       deraadt   789:     Lst             targs;     /* the initial list of targets */
                    790: {
                    791:     register GNode  *gn;       /* a temporary pointer */
1.18      espie     792:     LIST    examine;           /* List of targets to examine */
1.6       millert   793:     int                    errors;     /* Number of errors the Job module reports */
1.1       deraadt   794:
1.17      espie     795:     Lst_Init(&toBeMade);
1.1       deraadt   796:
1.18      espie     797:     Lst_Clone(&examine, targs, NOCOPY);
1.1       deraadt   798:     numNodes = 0;
1.4       millert   799:
1.1       deraadt   800:     /*
                    801:      * Make an initial downward pass over the graph, marking nodes to be made
                    802:      * as we go down. We call Suff_FindDeps to find where a node is and
                    803:      * to get some children for it if it has none and also has no commands.
                    804:      * If the node is a leaf, we stick it on the toBeMade queue to
                    805:      * be looked at in a minute, otherwise we add its children to our queue
1.4       millert   806:      * and go on about our business.
1.1       deraadt   807:      */
1.18      espie     808:     while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
1.4       millert   809:
1.1       deraadt   810:        if (!gn->make) {
                    811:            gn->make = TRUE;
                    812:            numNodes++;
1.4       millert   813:
1.1       deraadt   814:            /*
                    815:             * Apply any .USE rules before looking for implicit dependencies
                    816:             * to make sure everything has commands that should...
                    817:             */
1.17      espie     818:            Lst_ForEach(&gn->children, MakeHandleUse, gn);
1.1       deraadt   819:            Suff_FindDeps (gn);
                    820:
1.6       millert   821:            if (gn->unmade != 0) {
1.18      espie     822:                Lst_ForEach(&gn->children, MakeAddChild, &examine);
1.1       deraadt   823:            } else {
1.17      espie     824:                Lst_EnQueue(&toBeMade, gn);
1.1       deraadt   825:            }
                    826:        }
                    827:     }
1.4       millert   828:
1.18      espie     829:     Lst_Destroy(&examine, NOFREE);
1.1       deraadt   830:
                    831:     if (queryFlag) {
                    832:        /*
                    833:         * We wouldn't do any work unless we could start some jobs in the
                    834:         * next loop... (we won't actually start any, of course, this is just
                    835:         * to see if any of the targets was out of date)
                    836:         */
                    837:        return (MakeStartJobs());
                    838:     } else {
                    839:        /*
                    840:         * Initialization. At the moment, no jobs are running and until some
                    841:         * get started, nothing will happen since the remaining upward
                    842:         * traversal of the graph is performed by the routines in job.c upon
                    843:         * the finishing of a job. So we fill the Job table as much as we can
1.4       millert   844:         * before going into our loop.
1.1       deraadt   845:         */
                    846:        (void) MakeStartJobs();
                    847:     }
                    848:
                    849:     /*
                    850:      * Main Loop: The idea here is that the ending of jobs will take
                    851:      * care of the maintenance of data structures and the waiting for output
                    852:      * will cause us to be idle most of the time while our children run as
                    853:      * much as possible. Because the job table is kept as full as possible,
                    854:      * the only time when it will be empty is when all the jobs which need
                    855:      * running have been run, so that is the end condition of this loop.
                    856:      * Note that the Job module will exit if there were any errors unless the
                    857:      * keepgoing flag was given.
                    858:      */
                    859:     while (!Job_Empty ()) {
                    860:        Job_CatchOutput ();
                    861:        Job_CatchChildren (!usePipes);
                    862:        (void)MakeStartJobs();
                    863:     }
                    864:
1.7       espie     865:     errors = Job_Finish();
1.1       deraadt   866:
                    867:     /*
                    868:      * Print the final status of each target. E.g. if it wasn't made
                    869:      * because some inferior reported an error.
                    870:      */
                    871:     errors = ((errors == 0) && (numNodes != 0));
1.14      espie     872:     Lst_ForEach(targs, MakePrintStatus, &errors);
1.4       millert   873:
1.1       deraadt   874:     return (TRUE);
                    875: }