[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.2

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