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

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

1.177   ! rillig      1: /*     $NetBSD: make.c,v 1.176 2020/10/23 04:58:33 rillig Exp $        */
1.7       christos    2:
1.1       cgd         3: /*
1.10      christos    4:  * Copyright (c) 1988, 1989, 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.51      agc         6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Adam de Boor.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. Neither the name of the University nor the names of its contributors
                     19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: /*
1.1       cgd        36:  * Copyright (c) 1989 by Berkeley Softworks
                     37:  * All rights reserved.
                     38:  *
                     39:  * This code is derived from software contributed to Berkeley by
                     40:  * Adam de Boor.
                     41:  *
                     42:  * Redistribution and use in source and binary forms, with or without
                     43:  * modification, are permitted provided that the following conditions
                     44:  * are met:
                     45:  * 1. Redistributions of source code must retain the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer.
                     47:  * 2. Redistributions in binary form must reproduce the above copyright
                     48:  *    notice, this list of conditions and the following disclaimer in the
                     49:  *    documentation and/or other materials provided with the distribution.
                     50:  * 3. All advertising materials mentioning features or use of this software
                     51:  *    must display the following acknowledgement:
                     52:  *     This product includes software developed by the University of
                     53:  *     California, Berkeley and its contributors.
                     54:  * 4. Neither the name of the University nor the names of its contributors
                     55:  *    may be used to endorse or promote products derived from this software
                     56:  *    without specific prior written permission.
                     57:  *
                     58:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     59:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     60:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     61:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     62:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     63:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     64:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     65:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     66:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     67:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     68:  * SUCH DAMAGE.
                     69:  */
                     70:
                     71: /*-
                     72:  * make.c --
                     73:  *     The functions which perform the examination of targets and
                     74:  *     their suitability for creation
                     75:  *
                     76:  * Interface:
1.151     rillig     77:  *     Make_Run        Initialize things for the module and recreate
                     78:  *                     whatever needs recreating. Returns TRUE if
                     79:  *                     work was (or would have been) done and FALSE
                     80:  *                     otherwise.
                     81:  *
                     82:  *     Make_Update     Update all parents of a given child. Performs
                     83:  *                     various bookkeeping chores like the updating
                     84:  *                     of the cmgn field of the parent, filling
                     85:  *                     of the IMPSRC context variable, etc. It will
                     86:  *                     place the parent on the toBeMade queue if it
                     87:  *                     should be.
                     88:  *
                     89:  *     Make_TimeStamp  Function to set the parent's cmgn field
                     90:  *                     based on a child's modification time.
                     91:  *
                     92:  *     Make_DoAllVar   Set up the various local variables for a
                     93:  *                     target, including the .ALLSRC variable, making
                     94:  *                     sure that any variable that needs to exist
                     95:  *                     at the very least has the empty value.
1.1       cgd        96:  *
1.151     rillig     97:  *     Make_OODate     Determine if a target is out-of-date.
1.1       cgd        98:  *
1.151     rillig     99:  *     Make_HandleUse  See if a child is a .USE node for a parent
                    100:  *                     and perform the .USE actions if so.
1.11      christos  101:  *
1.151     rillig    102:  *     Make_ExpandUse  Expand .USE nodes
1.1       cgd       103:  */
                    104:
                    105: #include    "make.h"
1.4       cgd       106: #include    "dir.h"
                    107: #include    "job.h"
1.1       cgd       108:
1.136     rillig    109: /*     "@(#)make.c     8.1 (Berkeley) 6/6/93"  */
1.177   ! rillig    110: MAKE_RCSID("$NetBSD: make.c,v 1.176 2020/10/23 04:58:33 rillig Exp $");
1.136     rillig    111:
1.149     rillig    112: /* Sequence # to detect recursion. */
                    113: static unsigned int checked = 1;
                    114:
                    115: /* The current fringe of the graph.
                    116:  * These are nodes which await examination by MakeOODate.
                    117:  * It is added to by Make_Update and subtracted from by MakeStartJobs */
                    118: static GNodeList *toBeMade;
1.1       cgd       119:
1.78      dsl       120: static int MakeCheckOrder(void *, void *);
1.148     rillig    121: static int MakeBuildParent(void *, void *);
1.68      dsl       122:
1.154     rillig    123: void
                    124: debug_printf(const char *fmt, ...)
                    125: {
                    126:     va_list args;
                    127:
                    128:     va_start(args, fmt);
                    129:     vfprintf(debug_file, fmt, args);
                    130:     va_end(args);
                    131: }
                    132:
1.87      joerg     133: MAKE_ATTR_DEAD static void
1.68      dsl       134: make_abort(GNode *gn, int line)
                    135: {
1.153     rillig    136:     debug_printf("make_abort from line %d\n", line);
1.135     rillig    137:     Targ_PrintNode(gn, 2);
                    138:     Targ_PrintNodes(toBeMade, 2);
1.68      dsl       139:     Targ_PrintGraph(3);
                    140:     abort();
                    141: }
                    142:
1.123     rillig    143: ENUM_VALUE_RTTI_8(GNodeMade,
                    144:                  UNMADE, DEFERRED, REQUESTED, BEINGMADE,
                    145:                  MADE, UPTODATE, ERROR, ABORTED);
                    146:
                    147: ENUM_FLAGS_RTTI_31(GNodeType,
                    148:                   OP_DEPENDS, OP_FORCE, OP_DOUBLEDEP,
                    149:                   /* OP_OPMASK is omitted since it combines other flags */
                    150:                   OP_OPTIONAL, OP_USE, OP_EXEC, OP_IGNORE,
                    151:                   OP_PRECIOUS, OP_SILENT, OP_MAKE, OP_JOIN,
                    152:                   OP_MADE, OP_SPECIAL, OP_USEBEFORE, OP_INVISIBLE,
                    153:                   OP_NOTMAIN, OP_PHONY, OP_NOPATH, OP_WAIT,
                    154:                   OP_NOMETA, OP_META, OP_NOMETA_CMP, OP_SUBMAKE,
                    155:                   OP_TRANSFORM, OP_MEMBER, OP_LIB, OP_ARCHV,
                    156:                   OP_HAS_COMMANDS, OP_SAVE_CMDS, OP_DEPS_FOUND, OP_MARK);
                    157:
                    158: ENUM_FLAGS_RTTI_10(GNodeFlags,
                    159:                   REMAKE, CHILDMADE, FORCE, DONE_WAIT,
                    160:                   DONE_ORDER, FROM_DEPEND, DONE_ALLSRC, CYCLE,
                    161:                   DONECYCLE, INTERNAL);
1.122     rillig    162:
                    163: void
                    164: GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
                    165:                    const char *suffix)
                    166: {
                    167:     char type_buf[GNodeType_ToStringSize];
                    168:     char flags_buf[GNodeFlags_ToStringSize];
                    169:
                    170:     fprintf(f, "%smade %s, type %s, flags %s%s",
                    171:            prefix,
                    172:            Enum_ValueToString(gn->made, GNodeMade_ToStringSpecs),
                    173:            Enum_FlagsToString(type_buf, sizeof type_buf,
                    174:                               gn->type, GNodeType_ToStringSpecs),
                    175:            Enum_FlagsToString(flags_buf, sizeof flags_buf,
                    176:                               gn->flags, GNodeFlags_ToStringSpecs),
                    177:            suffix);
                    178: }
                    179:
1.159     rillig    180: Boolean
                    181: NoExecute(GNode *gn)
                    182: {
                    183:     return (gn->type & OP_MAKE) ? noRecursiveExecute : noExecute;
                    184: }
                    185:
1.149     rillig    186: /* Update the youngest child of the node, according to the given child. */
1.143     rillig    187: void
1.50      wiz       188: Make_TimeStamp(GNode *pgn, GNode *cgn)
1.1       cgd       189: {
1.83      christos  190:     if (pgn->cmgn == NULL || cgn->mtime > pgn->cmgn->mtime) {
                    191:        pgn->cmgn = cgn;
1.1       cgd       192:     }
                    193: }
1.5       jtc       194:
1.149     rillig    195: /* See if the node is out of date with respect to its sources.
1.1       cgd       196:  *
1.149     rillig    197:  * Used by Make_Run when deciding which nodes to place on the
                    198:  * toBeMade queue initially and by Make_Update to screen out .USE and
                    199:  * .EXEC nodes. In the latter case, however, any other sort of node
                    200:  * must be considered out-of-date since at least one of its children
                    201:  * will have been recreated.
1.1       cgd       202:  *
1.149     rillig    203:  * The mtime field of the node and the cmgn field of its parents
                    204:  * may be changed.
1.1       cgd       205:  */
                    206: Boolean
1.50      wiz       207: Make_OODate(GNode *gn)
1.1       cgd       208: {
                    209:     Boolean         oodate;
                    210:
                    211:     /*
                    212:      * Certain types of targets needn't even be sought as their datedness
                    213:      * doesn't depend on their modification time...
                    214:      */
1.39      christos  215:     if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
1.86      christos  216:        (void)Dir_MTime(gn, 1);
1.1       cgd       217:        if (DEBUG(MAKE)) {
                    218:            if (gn->mtime != 0) {
1.153     rillig    219:                debug_printf("modified %s...", Targ_FmtTime(gn->mtime));
1.1       cgd       220:            } else {
1.153     rillig    221:                debug_printf("non-existent...");
1.1       cgd       222:            }
                    223:        }
                    224:     }
                    225:
                    226:     /*
                    227:      * A target is remade in one of the following circumstances:
1.177   ! rillig    228:      * its modification time is smaller than that of its youngest child and
        !           229:      *     it would actually be run (has commands or is not GNode_IsTarget)
1.1       cgd       230:      * it's the object of a force operator
                    231:      * it has no children, was on the lhs of an operator and doesn't exist
                    232:      *     already.
                    233:      *
                    234:      * Libraries are only considered out-of-date if the archive module says
                    235:      * they are.
                    236:      *
1.37      wiz       237:      * These weird rules are brought to you by Backward-Compatibility and
1.1       cgd       238:      * the strange people who wrote 'Make'.
                    239:      */
1.39      christos  240:     if (gn->type & (OP_USE|OP_USEBEFORE)) {
1.1       cgd       241:        /*
                    242:         * If the node is a USE node it is *never* out of date
                    243:         * no matter *what*.
                    244:         */
1.152     rillig    245:        DEBUG0(MAKE, ".USE node...");
1.1       cgd       246:        oodate = FALSE;
1.25      sjg       247:     } else if ((gn->type & OP_LIB) &&
                    248:               ((gn->mtime==0) || Arch_IsLib(gn))) {
1.152     rillig    249:        DEBUG0(MAKE, "library...");
1.6       christos  250:
                    251:        /*
                    252:         * always out of date if no children and :: target
1.26      sjg       253:         * or non-existent.
1.6       christos  254:         */
1.99      rillig    255:        oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
1.83      christos  256:                  (gn->cmgn == NULL && (gn->type & OP_DOUBLEDEP)));
1.1       cgd       257:     } else if (gn->type & OP_JOIN) {
                    258:        /*
                    259:         * A target with the .JOIN attribute is only considered
                    260:         * out-of-date if any of its children was out-of-date.
                    261:         */
1.152     rillig    262:        DEBUG0(MAKE, ".JOIN node...");
                    263:        DEBUG1(MAKE, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
1.52      dsl       264:        oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
1.8       christos  265:     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
1.1       cgd       266:        /*
                    267:         * A node which is the object of the force (!) operator or which has
                    268:         * the .EXEC attribute is always considered out-of-date.
                    269:         */
                    270:        if (DEBUG(MAKE)) {
                    271:            if (gn->type & OP_FORCE) {
1.153     rillig    272:                debug_printf("! operator...");
1.8       christos  273:            } else if (gn->type & OP_PHONY) {
1.153     rillig    274:                debug_printf(".PHONY node...");
1.1       cgd       275:            } else {
1.153     rillig    276:                debug_printf(".EXEC node...");
1.1       cgd       277:            }
                    278:        }
                    279:        oodate = TRUE;
1.83      christos  280:     } else if ((gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) ||
                    281:               (gn->cmgn == NULL &&
1.52      dsl       282:                ((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
                    283:                  || gn->type & OP_DOUBLEDEP)))
1.1       cgd       284:     {
                    285:        /*
                    286:         * A node whose modification time is less than that of its
1.83      christos  287:         * youngest child or that has no children (cmgn == NULL) and
1.52      dsl       288:         * either doesn't exist (mtime == 0) and it isn't optional
                    289:         * or was the object of a * :: operator is out-of-date.
                    290:         * Why? Because that's the way Make does it.
1.1       cgd       291:         */
                    292:        if (DEBUG(MAKE)) {
1.83      christos  293:            if (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) {
1.153     rillig    294:                debug_printf("modified before source %s...",
                    295:                             gn->cmgn->path ? gn->cmgn->path : gn->cmgn->name);
1.1       cgd       296:            } else if (gn->mtime == 0) {
1.153     rillig    297:                debug_printf("non-existent and no sources...");
1.1       cgd       298:            } else {
1.153     rillig    299:                debug_printf(":: operator and no sources...");
1.1       cgd       300:            }
                    301:        }
                    302:        oodate = TRUE;
                    303:     } else {
1.99      rillig    304:        /*
1.22      christos  305:         * When a non-existing child with no sources
1.21      christos  306:         * (such as a typically used FORCE source) has been made and
                    307:         * the target of the child (usually a directory) has the same
                    308:         * timestamp as the timestamp just given to the non-existing child
                    309:         * after it was considered made.
                    310:         */
1.1       cgd       311:        if (DEBUG(MAKE)) {
1.22      christos  312:            if (gn->flags & FORCE)
1.153     rillig    313:                debug_printf("non existing child...");
1.1       cgd       314:        }
1.52      dsl       315:        oodate = (gn->flags & FORCE) ? TRUE : FALSE;
1.1       cgd       316:     }
                    317:
1.82      sjg       318: #ifdef USE_META
                    319:     if (useMeta) {
                    320:        oodate = meta_oodate(gn, oodate);
                    321:     }
                    322: #endif
                    323:
1.1       cgd       324:     /*
                    325:      * If the target isn't out-of-date, the parents need to know its
                    326:      * modification time. Note that targets that appear to be out-of-date
1.177   ! rillig    327:      * but aren't, because they have no commands and are GNode_IsTarget,
1.1       cgd       328:      * have their mtime stay below their children's mtime to keep parents from
                    329:      * thinking they're out-of-date.
                    330:      */
                    331:     if (!oodate) {
1.163     rillig    332:        GNodeListNode *ln;
                    333:        for (ln = gn->parents->first; ln != NULL; ln = ln->next)
                    334:            Make_TimeStamp(ln->datum, gn);
1.1       cgd       335:     }
                    336:
1.98      rillig    337:     return oodate;
1.1       cgd       338: }
1.108     rillig    339:
1.149     rillig    340: /* Add the node to the list if it needs to be examined. */
1.148     rillig    341: static int
1.78      dsl       342: MakeAddChild(void *gnp, void *lp)
1.1       cgd       343: {
1.137     rillig    344:     GNode *gn = gnp;
                    345:     GNodeList *l = lp;
1.14      christos  346:
1.39      christos  347:     if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
1.152     rillig    348:        DEBUG2(MAKE, "MakeAddChild: need to examine %s%s\n",
                    349:               gn->name, gn->cohort_num);
1.129     rillig    350:        Lst_Enqueue(l, gn);
1.1       cgd       351:     }
1.148     rillig    352:     return 0;
1.1       cgd       353: }
1.108     rillig    354:
1.149     rillig    355: /* Find the pathname of a child that was already made.
                    356:  *
                    357:  * The path and mtime of the node and the cmgn of the parent are
                    358:  * updated; the unmade children count of the parent is decremented.
1.13      christos  359:  *
1.50      wiz       360:  * Input:
                    361:  *     gnp             the node to find
1.13      christos  362:  */
1.148     rillig    363: static int
1.78      dsl       364: MakeFindChild(void *gnp, void *pgnp)
1.13      christos  365: {
1.167     rillig    366:     GNode *gn = gnp;
                    367:     GNode *pgn = pgnp;
1.14      christos  368:
1.85      christos  369:     (void)Dir_MTime(gn, 0);
1.22      christos  370:     Make_TimeStamp(pgn, gn);
1.42      pk        371:     pgn->unmade--;
1.148     rillig    372:
                    373:     return 0;
1.13      christos  374: }
1.108     rillig    375:
1.109     rillig    376: /* Called by Make_Run and SuffApplyTransform on the downward pass to handle
                    377:  * .USE and transformation nodes, by copying the child node's commands, type
                    378:  * flags and children to the parent node.
                    379:  *
                    380:  * A .USE node is much like an explicit transformation rule, except its
                    381:  * commands are always added to the target node, even if the target already
                    382:  * has commands.
1.1       cgd       383:  *
1.50      wiz       384:  * Input:
1.161     rillig    385:  *     cgn             The source node, which is either a .USE/.USEBEFORE
                    386:  *                     node or a transformation node (OP_TRANSFORM).
                    387:  *     pgn             The target node
1.1       cgd       388:  */
1.43      pk        389: void
1.50      wiz       390: Make_HandleUse(GNode *cgn, GNode *pgn)
1.1       cgd       391: {
1.151     rillig    392:     GNodeListNode *ln; /* An element in the children list */
1.44      pk        393:
                    394: #ifdef DEBUG_SRC
                    395:     if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
1.153     rillig    396:        debug_printf("Make_HandleUse: called for plain node %s\n", cgn->name);
1.44      pk        397:        return;
                    398:     }
                    399: #endif
1.1       cgd       400:
1.129     rillig    401:     if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
1.39      christos  402:            if (cgn->type & OP_USEBEFORE) {
1.112     rillig    403:                /* .USEBEFORE */
1.129     rillig    404:                Lst_PrependAll(pgn->commands, cgn->commands);
1.39      christos  405:            } else {
1.112     rillig    406:                /* .USE, or target has no commands */
1.129     rillig    407:                Lst_AppendAll(pgn->commands, cgn->commands);
1.39      christos  408:            }
1.44      pk        409:     }
1.10      christos  410:
1.161     rillig    411:     for (ln = cgn->children->first; ln != NULL; ln = ln->next) {
                    412:        GNode *gn = ln->datum;
1.11      christos  413:
1.119     rillig    414:        /*
                    415:         * Expand variables in the .USE node's name
                    416:         * and save the unexpanded form.
                    417:         * We don't need to do this for commands.
                    418:         * They get expanded properly when we execute.
                    419:         */
                    420:        if (gn->uname == NULL) {
                    421:            gn->uname = gn->name;
                    422:        } else {
                    423:            free(gn->name);
                    424:        }
1.138     rillig    425:        (void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
                    426:        /* TODO: handle errors */
1.130     rillig    427:        if (gn->uname && strcmp(gn->name, gn->uname) != 0) {
1.119     rillig    428:            /* See if we have a target for this node. */
1.145     rillig    429:            GNode *tgn = Targ_FindNode(gn->name);
1.119     rillig    430:            if (tgn != NULL)
                    431:                gn = tgn;
                    432:        }
1.1       cgd       433:
1.129     rillig    434:        Lst_Append(pgn->children, gn);
                    435:        Lst_Append(gn->parents, pgn);
1.156     rillig    436:        pgn->unmade++;
1.44      pk        437:     }
1.10      christos  438:
1.90      joerg     439:     pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
1.1       cgd       440: }
1.31      mycroft   441:
1.149     rillig    442: /* Used by Make_Run on the downward pass to handle .USE nodes. Should be
                    443:  * called before the children are enqueued to be looked at by MakeAddChild.
                    444:  *
1.150     rillig    445:  * For a .USE child, the commands, type flags and children are copied to the
1.149     rillig    446:  * parent node, and since the relation to the .USE node is then no longer
                    447:  * needed, that relation is removed.
1.44      pk        448:  *
1.50      wiz       449:  * Input:
1.150     rillig    450:  *     cgn             the child, which may be a .USE node
                    451:  *     pgn             the current parent
1.44      pk        452:  */
1.150     rillig    453: static void
                    454: MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
1.5       jtc       455: {
1.150     rillig    456:     Boolean unmarked;
1.44      pk        457:
                    458:     unmarked = ((cgn->type & OP_MARK) == 0);
                    459:     cgn->type |= OP_MARK;
1.31      mycroft   460:
1.44      pk        461:     if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
1.150     rillig    462:        return;
1.44      pk        463:
                    464:     if (unmarked)
1.43      pk        465:        Make_HandleUse(cgn, pgn);
1.31      mycroft   466:
1.43      pk        467:     /*
                    468:      * This child node is now "made", so we decrement the count of
                    469:      * unmade children in the parent... We also remove the child
                    470:      * from the parent's list to accurately reflect the number of decent
                    471:      * children the parent has. This is used by Make_Run to decide
                    472:      * whether to queue the parent or examine its children...
                    473:      */
1.150     rillig    474:     Lst_Remove(pgn->children, ln);
                    475:     pgn->unmade--;
                    476: }
                    477:
                    478: static void
                    479: HandleUseNodes(GNode *gn)
                    480: {
                    481:     GNodeListNode *ln, *nln;
                    482:     for (ln = gn->children->first; ln != NULL; ln = nln) {
                    483:        nln = ln->next;
1.158     rillig    484:        MakeHandleUse(ln->datum, gn, ln);
1.43      pk        485:     }
1.5       jtc       486: }
1.22      christos  487:
                    488:
1.149     rillig    489: /* Check the modification time of a gnode, and update it if necessary.
                    490:  * Return 0 if the gnode does not exist, or its filesystem time if it does. */
1.22      christos  491: time_t
1.50      wiz       492: Make_Recheck(GNode *gn)
1.22      christos  493: {
1.85      christos  494:     time_t mtime = Dir_MTime(gn, 1);
1.22      christos  495:
                    496: #ifndef RECHECK
                    497:     /*
                    498:      * We can't re-stat the thing, but we can at least take care of rules
                    499:      * where a target depends on a source that actually creates the
                    500:      * target, but only if it has changed, e.g.
                    501:      *
                    502:      * parse.h : parse.o
                    503:      *
                    504:      * parse.o : parse.y
1.151     rillig    505:      *         yacc -d parse.y
                    506:      *         cc -c y.tab.c
                    507:      *         mv y.tab.o parse.o
                    508:      *         cmp -s y.tab.h parse.h || mv y.tab.h parse.h
1.22      christos  509:      *
                    510:      * In this case, if the definitions produced by yacc haven't changed
                    511:      * from before, parse.h won't have been updated and gn->mtime will
                    512:      * reflect the current modification time for parse.h. This is
                    513:      * something of a kludge, I admit, but it's a useful one..
                    514:      * XXX: People like to use a rule like
                    515:      *
                    516:      * FRC:
                    517:      *
                    518:      * To force things that depend on FRC to be made, so we have to
                    519:      * check for gn->children being empty as well...
                    520:      */
1.129     rillig    521:     if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
1.22      christos  522:        gn->mtime = now;
                    523:     }
                    524: #else
                    525:     /*
                    526:      * This is what Make does and it's actually a good thing, as it
                    527:      * allows rules like
                    528:      *
                    529:      * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    530:      *
                    531:      * to function as intended. Unfortunately, thanks to the stateless
                    532:      * nature of NFS (by which I mean the loose coupling of two clients
                    533:      * using the same file from a common server), there are times
                    534:      * when the modification time of a file created on a remote
                    535:      * machine will not be modified before the local stat() implied by
                    536:      * the Dir_MTime occurs, thus leading us to believe that the file
                    537:      * is unchanged, wreaking havoc with files that depend on this one.
                    538:      *
                    539:      * I have decided it is better to make too much than to make too
                    540:      * little, so this stuff is commented out unless you're sure it's ok.
                    541:      * -- ardeb 1/12/88
                    542:      */
                    543:     /*
                    544:      * Christos, 4/9/92: If we are  saving commands pretend that
                    545:      * the target is made now. Otherwise archives with ... rules
                    546:      * don't work!
                    547:      */
1.73      dsl       548:     if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
                    549:            (mtime == 0 && !(gn->type & OP_WAIT))) {
1.152     rillig    550:        DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
                    551:               gn->name, Targ_FmtTime(gn->mtime));
1.22      christos  552:        gn->mtime = now;
                    553:     }
                    554:     else {
1.152     rillig    555:        DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
                    556:               gn->name, Targ_FmtTime(gn->mtime));
1.22      christos  557:     }
                    558: #endif
                    559:     return mtime;
                    560: }
                    561:
1.169     rillig    562: /*
                    563:  * Set the .PREFIX and .IMPSRC variables for all the implied parents
                    564:  * of this node.
                    565:  */
                    566: static void
                    567: UpdateImplicitParentsVars(GNode *cgn, const char *cname)
                    568: {
                    569:     GNodeListNode *ln;
                    570:     char *cpref_freeIt;
                    571:     const char *cpref = Var_Value(PREFIX, cgn, &cpref_freeIt);
                    572:
                    573:     for (ln = cgn->implicitParents->first; ln != NULL; ln = ln->next) {
                    574:        GNode *pgn = ln->datum;
                    575:        if (pgn->flags & REMAKE) {
                    576:            Var_Set(IMPSRC, cname, pgn);
                    577:            if (cpref != NULL)
                    578:                Var_Set(PREFIX, cpref, pgn);
                    579:        }
                    580:     }
                    581:     bmake_free(cpref_freeIt);
                    582: }
                    583:
1.149     rillig    584: /* Perform update on the parents of a node. Used by JobFinish once
                    585:  * a node has been dealt with and by MakeStartJobs if it finds an
                    586:  * up-to-date node.
1.1       cgd       587:  *
1.149     rillig    588:  * The unmade field of pgn is decremented and pgn may be placed on
                    589:  * the toBeMade queue if this field becomes 0.
1.22      christos  590:  *
1.149     rillig    591:  * If the child was made, the parent's flag CHILDMADE field will be
                    592:  * set true.
1.1       cgd       593:  *
1.149     rillig    594:  * If the child is not up-to-date and still does not exist,
                    595:  * set the FORCE flag on the parents.
1.1       cgd       596:  *
1.149     rillig    597:  * If the child wasn't made, the cmgn field of the parent will be
                    598:  * altered if the child's mtime is big enough.
1.1       cgd       599:  *
1.149     rillig    600:  * Finally, if the child is the implied source for the parent, the
                    601:  * parent's IMPSRC variable is set appropriately.
1.1       cgd       602:  */
                    603: void
1.50      wiz       604: Make_Update(GNode *cgn)
1.1       cgd       605: {
1.151     rillig    606:     const char *cname;         /* the child's name */
1.44      pk        607:     time_t     mtime = -1;
1.137     rillig    608:     GNodeList *parents;
1.170     rillig    609:     GNodeListNode *ln;
1.47      pk        610:     GNode      *centurion;
1.1       cgd       611:
1.70      dsl       612:     /* It is save to re-examine any nodes again */
                    613:     checked++;
                    614:
1.168     rillig    615:     {
                    616:         char *cname_freeIt;
                    617:        cname = Var_Value(TARGET, cgn, &cname_freeIt);
                    618:        assert(cname_freeIt == NULL);
                    619:     }
1.1       cgd       620:
1.152     rillig    621:     DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
1.68      dsl       622:
1.1       cgd       623:     /*
                    624:      * If the child was actually made, see what its modification time is
                    625:      * now -- some rules won't actually update the file. If the file still
                    626:      * doesn't exist, make its mtime now.
                    627:      */
                    628:     if (cgn->made != UPTODATE) {
1.22      christos  629:        mtime = Make_Recheck(cgn);
1.1       cgd       630:     }
1.10      christos  631:
1.47      pk        632:     /*
                    633:      * If this is a `::' node, we must consult its first instance
                    634:      * which is where all parents are linked.
                    635:      */
                    636:     if ((centurion = cgn->centurion) != NULL) {
1.129     rillig    637:        if (!Lst_IsEmpty(cgn->parents))
1.68      dsl       638:                Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
1.156     rillig    639:        centurion->unmade_cohorts--;
1.47      pk        640:        if (centurion->unmade_cohorts < 0)
1.56      christos  641:            Error("Graph cycles through centurion %s", centurion->name);
1.47      pk        642:     } else {
                    643:        centurion = cgn;
                    644:     }
1.68      dsl       645:     parents = centurion->parents;
                    646:
                    647:     /* If this was a .ORDER node, schedule the RHS */
1.176     rillig    648:     Lst_ForEachUntil(centurion->order_succ, MakeBuildParent, toBeMade->first);
1.68      dsl       649:
                    650:     /* Now mark all the parents as having one less unmade child */
1.170     rillig    651:     for (ln = parents->first; ln != NULL; ln = ln->next) {
                    652:        GNode *pgn = ln->datum;
1.169     rillig    653:
1.119     rillig    654:        if (DEBUG(MAKE))
1.153     rillig    655:            debug_printf("inspect parent %s%s: flags %x, "
                    656:                         "type %x, made %d, unmade %d ",
                    657:                         pgn->name, pgn->cohort_num, pgn->flags,
                    658:                         pgn->type, pgn->made, pgn->unmade - 1);
1.119     rillig    659:
                    660:        if (!(pgn->flags & REMAKE)) {
                    661:            /* This parent isn't needed */
1.152     rillig    662:            DEBUG0(MAKE, "- not needed\n");
1.119     rillig    663:            continue;
                    664:        }
                    665:        if (mtime == 0 && !(cgn->type & OP_WAIT))
                    666:            pgn->flags |= FORCE;
                    667:
                    668:        /*
                    669:         * If the parent has the .MADE attribute, its timestamp got
                    670:         * updated to that of its newest child, and its unmake
                    671:         * child count got set to zero in Make_ExpandUse().
                    672:         * However other things might cause us to build one of its
                    673:         * children - and so we mustn't do any processing here when
                    674:         * the child build finishes.
                    675:         */
                    676:        if (pgn->type & OP_MADE) {
1.152     rillig    677:            DEBUG0(MAKE, "- .MADE\n");
1.119     rillig    678:            continue;
                    679:        }
                    680:
                    681:        if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
                    682:            if (cgn->made == MADE)
                    683:                pgn->flags |= CHILDMADE;
                    684:            (void)Make_TimeStamp(pgn, cgn);
                    685:        }
1.68      dsl       686:
1.119     rillig    687:        /*
                    688:         * A parent must wait for the completion of all instances
                    689:         * of a `::' dependency.
                    690:         */
                    691:        if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
1.152     rillig    692:            DEBUG2(MAKE, "- centurion made %d, %d unmade cohorts\n",
                    693:                   centurion->made, centurion->unmade_cohorts);
1.119     rillig    694:            continue;
                    695:        }
1.47      pk        696:
1.119     rillig    697:        /* One more child of this parent is now made */
1.156     rillig    698:        pgn->unmade--;
1.119     rillig    699:        if (pgn->unmade < 0) {
                    700:            if (DEBUG(MAKE)) {
1.153     rillig    701:                debug_printf("Graph cycles through %s%s\n",
                    702:                             pgn->name, pgn->cohort_num);
1.119     rillig    703:                Targ_PrintGraph(2);
1.47      pk        704:            }
1.119     rillig    705:            Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
                    706:        }
1.1       cgd       707:
1.119     rillig    708:        /* We must always rescan the parents of .WAIT and .ORDER nodes. */
                    709:        if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
                    710:                && !(centurion->flags & DONE_ORDER)) {
1.152     rillig    711:            DEBUG0(MAKE, "- unmade children\n");
1.119     rillig    712:            continue;
                    713:        }
                    714:        if (pgn->made != DEFERRED) {
1.47      pk        715:            /*
1.119     rillig    716:             * Either this parent is on a different branch of the tree,
                    717:             * or it on the RHS of a .WAIT directive
                    718:             * or it is already on the toBeMade list.
1.47      pk        719:             */
1.152     rillig    720:            DEBUG0(MAKE, "- not deferred\n");
1.119     rillig    721:            continue;
                    722:        }
1.126     rillig    723:        assert(pgn->order_pred != NULL);
1.140     rillig    724:        if (Lst_ForEachUntil(pgn->order_pred, MakeCheckOrder, 0)) {
1.119     rillig    725:            /* A .ORDER rule stops us building this */
                    726:            continue;
1.1       cgd       727:        }
1.119     rillig    728:        if (DEBUG(MAKE)) {
1.153     rillig    729:            debug_printf("- %s%s made, schedule %s%s (made %d)\n",
                    730:                         cgn->name, cgn->cohort_num,
                    731:                         pgn->name, pgn->cohort_num, pgn->made);
1.135     rillig    732:            Targ_PrintNode(pgn, 2);
1.119     rillig    733:        }
                    734:        /* Ok, we can schedule the parent again */
                    735:        pgn->made = REQUESTED;
1.129     rillig    736:        Lst_Enqueue(toBeMade, pgn);
1.1       cgd       737:     }
1.10      christos  738:
1.169     rillig    739:     UpdateImplicitParentsVars(cgn, cname);
1.1       cgd       740: }
1.108     rillig    741:
1.143     rillig    742: static void
1.144     rillig    743: UnmarkChildren(GNode *gn)
1.141     rillig    744: {
1.144     rillig    745:     GNodeListNode *ln;
1.141     rillig    746:
1.144     rillig    747:     for (ln = gn->children->first; ln != NULL; ln = ln->next) {
                    748:        GNode *child = ln->datum;
                    749:        child->type &= ~OP_MARK;
                    750:     }
1.141     rillig    751: }
                    752:
1.149     rillig    753: /* Add a child's name to the ALLSRC and OODATE variables of the given
1.172     rillig    754:  * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
                    755:  * attributes. .EXEC and .USE children are very rarely going to be files,
                    756:  * so...
                    757:  *
1.149     rillig    758:  * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
                    759:  *
                    760:  * A child is added to the OODATE variable if its modification time is
                    761:  * later than that of its parent, as defined by Make, except if the
                    762:  * parent is a .JOIN node. In that case, it is only added to the OODATE
                    763:  * variable if it was actually made (since .JOIN nodes don't have
                    764:  * modification times, the comparison is rather unfair...)..
1.1       cgd       765:  *
1.141     rillig    766:  * Input:
1.162     rillig    767:  *     cgn             The child to add
                    768:  *     pgn             The parent to whose ALLSRC variable it should
1.141     rillig    769:  *                     be added
1.1       cgd       770:  */
1.142     rillig    771: static void
1.162     rillig    772: MakeAddAllSrc(GNode *cgn, GNode *pgn)
1.1       cgd       773: {
1.31      mycroft   774:     if (cgn->type & OP_MARK)
1.142     rillig    775:        return;
1.31      mycroft   776:     cgn->type |= OP_MARK;
                    777:
1.39      christos  778:     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
1.102     rillig    779:        const char *child, *allsrc;
1.45      pk        780:        char *p1 = NULL, *p2 = NULL;
1.1       cgd       781:
1.17      christos  782:        if (cgn->type & OP_ARCHV)
1.56      christos  783:            child = Var_Value(MEMBER, cgn, &p1);
1.17      christos  784:        else
                    785:            child = cgn->path ? cgn->path : cgn->name;
1.45      pk        786:        if (cgn->type & OP_JOIN) {
1.56      christos  787:            allsrc = Var_Value(ALLSRC, cgn, &p2);
1.45      pk        788:        } else {
                    789:            allsrc = child;
                    790:        }
1.46      pk        791:        if (allsrc != NULL)
1.56      christos  792:                Var_Append(ALLSRC, allsrc, pgn);
1.103     rillig    793:        bmake_free(p2);
1.1       cgd       794:        if (pgn->type & OP_JOIN) {
                    795:            if (cgn->made == MADE) {
                    796:                Var_Append(OODATE, child, pgn);
                    797:            }
                    798:        } else if ((pgn->mtime < cgn->mtime) ||
                    799:                   (cgn->mtime >= now && cgn->made == MADE))
                    800:        {
                    801:            /*
                    802:             * It goes in the OODATE variable if the parent is younger than the
                    803:             * child or if the child has been modified more recently than
                    804:             * the start of the make. This is to keep pmake from getting
                    805:             * confused if something else updates the parent after the
                    806:             * make starts (shouldn't happen, I know, but sometimes it
                    807:             * does). In such a case, if we've updated the kid, the parent
                    808:             * is likely to have a modification time later than that of
                    809:             * the kid and anything that relies on the OODATE variable will
                    810:             * be hosed.
                    811:             *
                    812:             * XXX: This will cause all made children to go in the OODATE
                    813:             * variable, even if they're not touched, if RECHECK isn't defined,
                    814:             * since cgn->mtime is set to now in Make_Update. According to
                    815:             * some people, this is good...
                    816:             */
                    817:            Var_Append(OODATE, child, pgn);
                    818:        }
1.103     rillig    819:        bmake_free(p1);
1.1       cgd       820:     }
                    821: }
1.108     rillig    822:
1.149     rillig    823: /* Set up the ALLSRC and OODATE variables. Sad to say, it must be
                    824:  * done separately, rather than while traversing the graph. This is
                    825:  * because Make defined OODATE to contain all sources whose modification
                    826:  * times were later than that of the target, *not* those sources that
                    827:  * were out-of-date. Since in both compatibility and native modes,
                    828:  * the modification time of the parent isn't found until the child
                    829:  * has been dealt with, we have to wait until now to fill in the
                    830:  * variable. As for ALLSRC, the ordering is important and not
                    831:  * guaranteed when in native mode, so it must be set here, too.
1.1       cgd       832:  *
1.149     rillig    833:  * If the node is a .JOIN node, its TARGET variable will be set to
                    834:  * match its ALLSRC variable.
1.1       cgd       835:  */
                    836: void
1.50      wiz       837: Make_DoAllVar(GNode *gn)
1.1       cgd       838: {
1.162     rillig    839:     GNodeListNode *ln;
                    840:
1.79      sjg       841:     if (gn->flags & DONE_ALLSRC)
                    842:        return;
1.99      rillig    843:
1.144     rillig    844:     UnmarkChildren(gn);
1.162     rillig    845:     for (ln = gn->children->first; ln != NULL; ln = ln->next)
                    846:         MakeAddAllSrc(ln->datum, gn);
1.1       cgd       847:
1.149     rillig    848:     if (!Var_Exists(OODATE, gn)) {
1.97      rillig    849:        Var_Set(OODATE, "", gn);
1.1       cgd       850:     }
1.149     rillig    851:     if (!Var_Exists(ALLSRC, gn)) {
1.97      rillig    852:        Var_Set(ALLSRC, "", gn);
1.1       cgd       853:     }
                    854:
                    855:     if (gn->type & OP_JOIN) {
1.5       jtc       856:        char *p1;
1.97      rillig    857:        Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn);
1.103     rillig    858:        bmake_free(p1);
1.1       cgd       859:     }
1.79      sjg       860:     gn->flags |= DONE_ALLSRC;
1.1       cgd       861: }
1.108     rillig    862:
1.68      dsl       863: static int
1.87      joerg     864: MakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
1.68      dsl       865: {
                    866:     GNode *bn = v_bn;
                    867:
                    868:     if (bn->made >= MADE || !(bn->flags & REMAKE))
                    869:        return 0;
1.170     rillig    870:
1.152     rillig    871:     DEBUG2(MAKE, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
                    872:           bn->name, bn->cohort_num);
1.68      dsl       873:     return 1;
                    874: }
                    875:
                    876: static int
1.78      dsl       877: MakeBuildChild(void *v_cn, void *toBeMade_next)
1.68      dsl       878: {
                    879:     GNode *cn = v_cn;
                    880:
1.152     rillig    881:     DEBUG4(MAKE, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
                    882:           cn->name, cn->cohort_num, cn->made, cn->type);
1.68      dsl       883:     if (cn->made > DEFERRED)
                    884:        return 0;
                    885:
                    886:     /* If this node is on the RHS of a .ORDER, check LHSs. */
1.126     rillig    887:     assert(cn->order_pred);
1.140     rillig    888:     if (Lst_ForEachUntil(cn->order_pred, MakeCheckOrder, 0)) {
1.68      dsl       889:        /* Can't build this (or anything else in this child list) yet */
                    890:        cn->made = DEFERRED;
1.88      sjg       891:        return 0;                       /* but keep looking */
1.68      dsl       892:     }
                    893:
1.152     rillig    894:     DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n", cn->name, cn->cohort_num);
1.68      dsl       895:
                    896:     cn->made = REQUESTED;
1.77      dsl       897:     if (toBeMade_next == NULL)
1.129     rillig    898:        Lst_Append(toBeMade, cn);
1.68      dsl       899:     else
1.129     rillig    900:        Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1.68      dsl       901:
                    902:     if (cn->unmade_cohorts != 0)
1.176     rillig    903:        Lst_ForEachUntil(cn->cohorts, MakeBuildChild, toBeMade_next);
1.68      dsl       904:
                    905:     /*
1.142     rillig    906:      * If this node is a .WAIT node with unmade children
1.68      dsl       907:      * then don't add the next sibling.
                    908:      */
                    909:     return cn->type & OP_WAIT && cn->unmade > 0;
                    910: }
                    911:
1.88      sjg       912: /* When a .ORDER LHS node completes we do this on each RHS */
1.148     rillig    913: static int
1.78      dsl       914: MakeBuildParent(void *v_pn, void *toBeMade_next)
1.68      dsl       915: {
                    916:     GNode *pn = v_pn;
                    917:
                    918:     if (pn->made != DEFERRED)
1.148     rillig    919:        return 0;
1.68      dsl       920:
                    921:     if (MakeBuildChild(pn, toBeMade_next) == 0) {
                    922:        /* Mark so that when this node is built we reschedule its parents */
                    923:        pn->flags |= DONE_ORDER;
                    924:     }
1.148     rillig    925:
                    926:     return 0;
1.68      dsl       927: }
                    928:
1.149     rillig    929: /* Start as many jobs as possible, taking them from the toBeMade queue.
                    930:  *
                    931:  * If the query flag was given to pmake, no job will be started,
                    932:  * but as soon as an out-of-date target is found, this function
                    933:  * returns TRUE. At all other times, this function returns FALSE.
                    934:  */
1.1       cgd       935: static Boolean
1.50      wiz       936: MakeStartJobs(void)
1.1       cgd       937: {
1.44      pk        938:     GNode      *gn;
1.59      dsl       939:     int                have_token = 0;
1.10      christos  940:
1.129     rillig    941:     while (!Lst_IsEmpty(toBeMade)) {
1.60      dsl       942:        /* Get token now to avoid cycling job-list when we only have 1 token */
                    943:        if (!have_token && !Job_TokenWithdraw())
                    944:            break;
                    945:        have_token = 1;
                    946:
1.129     rillig    947:        gn = Lst_Dequeue(toBeMade);
1.152     rillig    948:        DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
1.68      dsl       949:
                    950:        if (gn->made != REQUESTED) {
1.152     rillig    951:            DEBUG1(MAKE, "state %d\n", gn->made);
1.68      dsl       952:
                    953:            make_abort(gn, __LINE__);
1.1       cgd       954:        }
                    955:
1.166     rillig    956:        if (gn->checked_seqno == checked) {
1.70      dsl       957:            /* We've already looked at this node since a job finished... */
1.152     rillig    958:            DEBUG2(MAKE, "already checked %s%s\n", gn->name, gn->cohort_num);
1.71      dsl       959:            gn->made = DEFERRED;
1.70      dsl       960:            continue;
                    961:        }
1.166     rillig    962:        gn->checked_seqno = checked;
1.70      dsl       963:
1.68      dsl       964:        if (gn->unmade != 0) {
1.1       cgd       965:            /*
1.68      dsl       966:             * We can't build this yet, add all unmade children to toBeMade,
                    967:             * just before the current first element.
1.1       cgd       968:             */
1.68      dsl       969:            gn->made = DEFERRED;
1.176     rillig    970:            Lst_ForEachUntil(gn->children, MakeBuildChild, toBeMade->first);
1.68      dsl       971:            /* and drop this node on the floor */
1.152     rillig    972:            DEBUG2(MAKE, "dropped %s%s\n", gn->name, gn->cohort_num);
1.68      dsl       973:            continue;
1.1       cgd       974:        }
1.10      christos  975:
1.68      dsl       976:        gn->made = BEINGMADE;
1.56      christos  977:        if (Make_OODate(gn)) {
1.152     rillig    978:            DEBUG0(MAKE, "out-of-date\n");
1.1       cgd       979:            if (queryFlag) {
1.98      rillig    980:                return TRUE;
1.1       cgd       981:            }
1.56      christos  982:            Make_DoAllVar(gn);
                    983:            Job_Make(gn);
1.59      dsl       984:            have_token = 0;
1.1       cgd       985:        } else {
1.152     rillig    986:            DEBUG0(MAKE, "up-to-date\n");
1.1       cgd       987:            gn->made = UPTODATE;
                    988:            if (gn->type & OP_JOIN) {
                    989:                /*
                    990:                 * Even for an up-to-date .JOIN node, we need it to have its
                    991:                 * context variables so references to it get the correct
                    992:                 * value for .TARGET when building up the context variables
                    993:                 * of its parent(s)...
                    994:                 */
1.56      christos  995:                Make_DoAllVar(gn);
1.1       cgd       996:            }
1.56      christos  997:            Make_Update(gn);
1.1       cgd       998:        }
                    999:     }
1.59      dsl      1000:
                   1001:     if (have_token)
                   1002:        Job_TokenReturn();
                   1003:
1.98      rillig   1004:     return FALSE;
1.1       cgd      1005: }
1.108     rillig   1006:
1.173     rillig   1007: static void
                   1008: MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
1.68      dsl      1009: {
                   1010:     if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
                   1011:        /* not waiting for this one */
1.173     rillig   1012:        return;
1.68      dsl      1013:
1.122     rillig   1014:     printf("    `%s%s' has .ORDER dependency against %s%s ",
1.173     rillig   1015:           gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1.122     rillig   1016:     GNode_FprintDetails(stdout, "(", ogn, ")\n");
                   1017:
                   1018:     if (DEBUG(MAKE) && debug_file != stdout) {
1.153     rillig   1019:        debug_printf("    `%s%s' has .ORDER dependency against %s%s ",
                   1020:                     gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1.122     rillig   1021:        GNode_FprintDetails(debug_file, "(", ogn, ")\n");
                   1022:     }
1.173     rillig   1023: }
                   1024:
                   1025: static void
                   1026: MakePrintStatusOrder(GNode *gn)
                   1027: {
                   1028:     GNodeListNode *ln;
                   1029:     for (ln = gn->order_pred->first; ln != NULL; ln = ln->next)
                   1030:         MakePrintStatusOrderNode(ln->datum, gn);
1.68      dsl      1031: }
                   1032:
1.174     rillig   1033: static void MakePrintStatusList(GNodeList *, int *);
                   1034:
1.149     rillig   1035: /* Print the status of a top-level node, viz. it being up-to-date already
                   1036:  * or not created due to an error in a lower level.
                   1037:  * Callback function for Make_Run via Lst_ForEachUntil.
                   1038:  */
1.175     rillig   1039: static Boolean
1.174     rillig   1040: MakePrintStatus(GNode *gn, int *errors)
1.1       cgd      1041: {
1.68      dsl      1042:     if (gn->flags & DONECYCLE)
                   1043:        /* We've completely processed this node before, don't do it again. */
1.175     rillig   1044:        return FALSE;
1.68      dsl      1045:
                   1046:     if (gn->unmade == 0) {
                   1047:        gn->flags |= DONECYCLE;
                   1048:        switch (gn->made) {
                   1049:        case UPTODATE:
                   1050:            printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
                   1051:            break;
                   1052:        case MADE:
                   1053:            break;
                   1054:        case UNMADE:
                   1055:        case DEFERRED:
                   1056:        case REQUESTED:
                   1057:        case BEINGMADE:
                   1058:            (*errors)++;
1.122     rillig   1059:            printf("`%s%s' was not built", gn->name, gn->cohort_num);
                   1060:            GNode_FprintDetails(stdout, " (", gn, ")!\n");
                   1061:            if (DEBUG(MAKE) && debug_file != stdout) {
1.153     rillig   1062:                debug_printf("`%s%s' was not built", gn->name, gn->cohort_num);
1.122     rillig   1063:                GNode_FprintDetails(debug_file, " (", gn, ")!\n");
                   1064:            }
1.68      dsl      1065:            /* Most likely problem is actually caused by .ORDER */
1.173     rillig   1066:            MakePrintStatusOrder(gn);
1.68      dsl      1067:            break;
                   1068:        default:
                   1069:            /* Errors - already counted */
                   1070:            printf("`%s%s' not remade because of errors.\n",
                   1071:                    gn->name, gn->cohort_num);
                   1072:            if (DEBUG(MAKE) && debug_file != stdout)
1.153     rillig   1073:                debug_printf("`%s%s' not remade because of errors.\n",
                   1074:                             gn->name, gn->cohort_num);
1.68      dsl      1075:            break;
1.1       cgd      1076:        }
1.175     rillig   1077:        return FALSE;
1.68      dsl      1078:     }
                   1079:
1.152     rillig   1080:     DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
                   1081:           gn->name, gn->cohort_num, gn->unmade);
1.68      dsl      1082:     /*
                   1083:      * If printing cycles and came to one that has unmade children,
1.70      dsl      1084:      * print out the cycle by recursing on its children.
1.68      dsl      1085:      */
1.70      dsl      1086:     if (!(gn->flags & CYCLE)) {
                   1087:        /* Fist time we've seen this node, check all children */
1.68      dsl      1088:        gn->flags |= CYCLE;
1.174     rillig   1089:        MakePrintStatusList(gn->children, errors);
1.70      dsl      1090:        /* Mark that this node needn't be processed again */
1.68      dsl      1091:        gn->flags |= DONECYCLE;
1.175     rillig   1092:        return FALSE;
1.1       cgd      1093:     }
1.68      dsl      1094:
1.70      dsl      1095:     /* Only output the error once per node */
                   1096:     gn->flags |= DONECYCLE;
                   1097:     Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
                   1098:     if ((*errors)++ > 100)
                   1099:        /* Abandon the whole error report */
1.175     rillig   1100:        return TRUE;
1.70      dsl      1101:
                   1102:     /* Reporting for our children will give the rest of the loop */
1.174     rillig   1103:     MakePrintStatusList(gn->children, errors);
1.175     rillig   1104:     return FALSE;
1.1       cgd      1105: }
1.108     rillig   1106:
1.174     rillig   1107: static void
                   1108: MakePrintStatusList(GNodeList *gnodes, int *errors)
                   1109: {
                   1110:     GNodeListNode *ln;
                   1111:     for (ln = gnodes->first; ln != NULL; ln = ln->next)
1.175     rillig   1112:         if (MakePrintStatus(ln->datum, errors))
                   1113:            break;
1.174     rillig   1114: }
1.11      christos 1115:
1.149     rillig   1116: /* Expand .USE nodes and create a new targets list.
1.50      wiz      1117:  *
                   1118:  * Input:
                   1119:  *     targs           the initial list of targets
1.1       cgd      1120:  */
1.68      dsl      1121: void
1.137     rillig   1122: Make_ExpandUse(GNodeList *targs)
1.1       cgd      1123: {
1.137     rillig   1124:     GNodeList *examine;                /* List of targets to examine */
1.1       cgd      1125:
1.129     rillig   1126:     examine = Lst_Copy(targs, NULL);
1.10      christos 1127:
1.1       cgd      1128:     /*
                   1129:      * Make an initial downward pass over the graph, marking nodes to be made
                   1130:      * as we go down. We call Suff_FindDeps to find where a node is and
                   1131:      * to get some children for it if it has none and also has no commands.
                   1132:      * If the node is a leaf, we stick it on the toBeMade queue to
                   1133:      * be looked at in a minute, otherwise we add its children to our queue
1.10      christos 1134:      * and go on about our business.
1.1       cgd      1135:      */
1.129     rillig   1136:     while (!Lst_IsEmpty(examine)) {
1.137     rillig   1137:        GNode *gn = Lst_Dequeue(examine);
1.99      rillig   1138:
1.68      dsl      1139:        if (gn->flags & REMAKE)
                   1140:            /* We've looked at this one already */
                   1141:            continue;
                   1142:        gn->flags |= REMAKE;
1.152     rillig   1143:        DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
                   1144:               gn->name, gn->cohort_num);
1.10      christos 1145:
1.120     rillig   1146:        if (gn->type & OP_DOUBLEDEP)
1.129     rillig   1147:            Lst_PrependAll(examine, gn->cohorts);
1.10      christos 1148:
1.68      dsl      1149:        /*
                   1150:         * Apply any .USE rules before looking for implicit dependencies
                   1151:         * to make sure everything has commands that should...
                   1152:         * Make sure that the TARGET is set, so that we can make
                   1153:         * expansions.
                   1154:         */
                   1155:        if (gn->type & OP_ARCHV) {
                   1156:            char *eoa, *eon;
                   1157:            eoa = strchr(gn->name, '(');
                   1158:            eon = strchr(gn->name, ')');
                   1159:            if (eoa == NULL || eon == NULL)
                   1160:                continue;
                   1161:            *eoa = '\0';
                   1162:            *eon = '\0';
1.97      rillig   1163:            Var_Set(MEMBER, eoa + 1, gn);
                   1164:            Var_Set(ARCHIVE, gn->name, gn);
1.68      dsl      1165:            *eoa = '(';
                   1166:            *eon = ')';
                   1167:        }
                   1168:
1.85      christos 1169:        (void)Dir_MTime(gn, 0);
1.97      rillig   1170:        Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
1.144     rillig   1171:        UnmarkChildren(gn);
1.150     rillig   1172:        HandleUseNodes(gn);
1.68      dsl      1173:
                   1174:        if ((gn->type & OP_MADE) == 0)
                   1175:            Suff_FindDeps(gn);
                   1176:        else {
                   1177:            /* Pretend we made all this node's children */
1.176     rillig   1178:            Lst_ForEachUntil(gn->children, MakeFindChild, gn);
1.68      dsl      1179:            if (gn->unmade != 0)
                   1180:                    printf("Warning: %s%s still has %d unmade children\n",
                   1181:                            gn->name, gn->cohort_num, gn->unmade);
                   1182:        }
                   1183:
                   1184:        if (gn->unmade != 0)
1.148     rillig   1185:            Lst_ForEachUntil(gn->children, MakeAddChild, examine);
1.68      dsl      1186:     }
                   1187:
1.129     rillig   1188:     Lst_Free(examine);
1.68      dsl      1189: }
                   1190:
1.139     rillig   1191: /* Make the .WAIT node depend on the previous children */
                   1192: static void
                   1193: add_wait_dependency(GNodeListNode *owln, GNode *wn)
1.68      dsl      1194: {
1.139     rillig   1195:     GNodeListNode *cln;
                   1196:     GNode *cn;
1.68      dsl      1197:
1.147     rillig   1198:     for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1.152     rillig   1199:        DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
                   1200:               cn->name, cn->cohort_num, wn->name);
1.68      dsl      1201:
1.139     rillig   1202:        /* XXX: This pattern should be factored out, it repeats often */
                   1203:        Lst_Append(wn->children, cn);
                   1204:        wn->unmade++;
                   1205:        Lst_Append(cn->parents, wn);
1.68      dsl      1206:     }
                   1207: }
                   1208:
1.149     rillig   1209: /* Convert .WAIT nodes into dependencies. */
1.68      dsl      1210: static void
1.137     rillig   1211: Make_ProcessWait(GNodeList *targs)
1.68      dsl      1212: {
1.137     rillig   1213:     GNode  *pgn;               /* 'parent' node we are examining */
                   1214:     GNodeListNode *owln;       /* Previous .WAIT node */
1.151     rillig   1215:     GNodeList *examine;                /* List of targets to examine */
1.68      dsl      1216:
                   1217:     /*
                   1218:      * We need all the nodes to have a common parent in order for the
                   1219:      * .WAIT and .ORDER scheduling to work.
                   1220:      * Perhaps this should be done earlier...
                   1221:      */
                   1222:
                   1223:     pgn = Targ_NewGN(".MAIN");
                   1224:     pgn->flags = REMAKE;
                   1225:     pgn->type = OP_PHONY | OP_DEPENDS;
                   1226:     /* Get it displayed in the diag dumps */
1.129     rillig   1227:     Lst_Prepend(Targ_List(), pgn);
1.68      dsl      1228:
1.164     rillig   1229:     {
                   1230:        GNodeListNode *ln;
                   1231:        for (ln = targs->first; ln != NULL; ln = ln->next) {
                   1232:            GNode *cgn = ln->datum;
                   1233:
                   1234:            Lst_Append(pgn->children, cgn);
                   1235:            Lst_Append(cgn->parents, pgn);
                   1236:            pgn->unmade++;
                   1237:        }
                   1238:     }
1.68      dsl      1239:
                   1240:     /* Start building with the 'dummy' .MAIN' node */
1.77      dsl      1241:     MakeBuildChild(pgn, NULL);
1.68      dsl      1242:
1.160     rillig   1243:     examine = Lst_New();
1.129     rillig   1244:     Lst_Append(examine, pgn);
1.68      dsl      1245:
1.129     rillig   1246:     while (!Lst_IsEmpty(examine)) {
1.164     rillig   1247:        GNodeListNode *ln;
                   1248:
1.129     rillig   1249:        pgn = Lst_Dequeue(examine);
1.99      rillig   1250:
1.68      dsl      1251:        /* We only want to process each child-list once */
                   1252:        if (pgn->flags & DONE_WAIT)
                   1253:            continue;
                   1254:        pgn->flags |= DONE_WAIT;
1.152     rillig   1255:        DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
1.68      dsl      1256:
1.120     rillig   1257:        if (pgn->type & OP_DOUBLEDEP)
1.129     rillig   1258:            Lst_PrependAll(examine, pgn->cohorts);
1.1       cgd      1259:
1.165     rillig   1260:        owln = pgn->children->first;
1.171     rillig   1261:        for (ln = pgn->children->first; ln != NULL; ln = ln->next) {
1.165     rillig   1262:            GNode *cgn = ln->datum;
1.68      dsl      1263:            if (cgn->type & OP_WAIT) {
1.139     rillig   1264:                add_wait_dependency(owln, cgn);
1.68      dsl      1265:                owln = ln;
1.1       cgd      1266:            } else {
1.129     rillig   1267:                Lst_Append(examine, cgn);
1.1       cgd      1268:            }
                   1269:        }
                   1270:     }
1.10      christos 1271:
1.129     rillig   1272:     Lst_Free(examine);
1.11      christos 1273: }
                   1274:
                   1275: /*-
                   1276:  *-----------------------------------------------------------------------
                   1277:  * Make_Run --
                   1278:  *     Initialize the nodes to remake and the list of nodes which are
                   1279:  *     ready to be made by doing a breadth-first traversal of the graph
                   1280:  *     starting from the nodes in the given list. Once this traversal
                   1281:  *     is finished, all the 'leaves' of the graph are in the toBeMade
                   1282:  *     queue.
                   1283:  *     Using this queue and the Job module, work back up the graph,
                   1284:  *     calling on MakeStartJobs to keep the job table as full as
                   1285:  *     possible.
                   1286:  *
1.50      wiz      1287:  * Input:
                   1288:  *     targs           the initial list of targets
                   1289:  *
1.11      christos 1290:  * Results:
                   1291:  *     TRUE if work was done. FALSE otherwise.
                   1292:  *
                   1293:  * Side Effects:
                   1294:  *     The make field of all nodes involved in the creation of the given
                   1295:  *     targets is set to 1. The toBeMade list is set to contain all the
                   1296:  *     'leaves' of these subgraphs.
                   1297:  *-----------------------------------------------------------------------
                   1298:  */
                   1299: Boolean
1.137     rillig   1300: Make_Run(GNodeList *targs)
1.11      christos 1301: {
1.151     rillig   1302:     int errors;                        /* Number of errors the Job module reports */
1.11      christos 1303:
1.68      dsl      1304:     /* Start trying to make the current targets... */
1.160     rillig   1305:     toBeMade = Lst_New();
1.68      dsl      1306:
                   1307:     Make_ExpandUse(targs);
                   1308:     Make_ProcessWait(targs);
                   1309:
1.60      dsl      1310:     if (DEBUG(MAKE)) {
1.153     rillig   1311:         debug_printf("#***# full graph\n");
1.68      dsl      1312:         Targ_PrintGraph(1);
1.60      dsl      1313:     }
1.1       cgd      1314:
                   1315:     if (queryFlag) {
                   1316:        /*
                   1317:         * We wouldn't do any work unless we could start some jobs in the
                   1318:         * next loop... (we won't actually start any, of course, this is just
                   1319:         * to see if any of the targets was out of date)
                   1320:         */
1.98      rillig   1321:        return MakeStartJobs();
1.1       cgd      1322:     }
1.68      dsl      1323:     /*
                   1324:      * Initialization. At the moment, no jobs are running and until some
                   1325:      * get started, nothing will happen since the remaining upward
                   1326:      * traversal of the graph is performed by the routines in job.c upon
                   1327:      * the finishing of a job. So we fill the Job table as much as we can
                   1328:      * before going into our loop.
                   1329:      */
                   1330:     (void)MakeStartJobs();
1.1       cgd      1331:
                   1332:     /*
                   1333:      * Main Loop: The idea here is that the ending of jobs will take
                   1334:      * care of the maintenance of data structures and the waiting for output
                   1335:      * will cause us to be idle most of the time while our children run as
                   1336:      * much as possible. Because the job table is kept as full as possible,
                   1337:      * the only time when it will be empty is when all the jobs which need
                   1338:      * running have been run, so that is the end condition of this loop.
                   1339:      * Note that the Job module will exit if there were any errors unless the
                   1340:      * keepgoing flag was given.
                   1341:      */
1.129     rillig   1342:     while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1.56      christos 1343:        Job_CatchOutput();
1.1       cgd      1344:        (void)MakeStartJobs();
                   1345:     }
                   1346:
1.20      christos 1347:     errors = Job_Finish();
1.1       cgd      1348:
                   1349:     /*
                   1350:      * Print the final status of each target. E.g. if it wasn't made
                   1351:      * because some inferior reported an error.
                   1352:      */
1.152     rillig   1353:     DEBUG1(MAKE, "done: errors %d\n", errors);
1.68      dsl      1354:     if (errors == 0) {
1.174     rillig   1355:        MakePrintStatusList(targs, &errors);
1.68      dsl      1356:        if (DEBUG(MAKE)) {
1.153     rillig   1357:            debug_printf("done: errors %d\n", errors);
1.68      dsl      1358:            if (errors)
                   1359:                Targ_PrintGraph(4);
                   1360:        }
                   1361:     }
                   1362:     return errors != 0;
1.1       cgd      1363: }

CVSweb <webmaster@jp.NetBSD.org>