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

1.253   ! rillig      1: /*     $NetBSD: make.c,v 1.252 2022/01/09 15:48:30 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:
1.233     rillig     71: /*
                     72:  * Examination of targets and their suitability for creation.
1.1       cgd        73:  *
                     74:  * Interface:
1.243     rillig     75:  *     Make_Run        Initialize things for the module. Returns true if
1.207     rillig     76:  *                     work was (or would have been) done.
1.151     rillig     77:  *
1.207     rillig     78:  *     Make_Update     After a target is made, update all its parents.
                     79:  *                     Perform various bookkeeping chores like the updating
1.178     rillig     80:  *                     of the youngestChild field of the parent, filling
1.241     rillig     81:  *                     of the IMPSRC variable, etc. Place the parent on the
                     82:  *                     toBeMade queue if it should be.
1.151     rillig     83:  *
1.189     rillig     84:  *     GNode_UpdateYoungestChild
                     85:  *                     Update the node's youngestChild field based on the
                     86:  *                     child's modification time.
1.151     rillig     87:  *
1.244     rillig     88:  *     GNode_SetLocalVars
                     89:  *                     Set up the various local variables for a
1.151     rillig     90:  *                     target, including the .ALLSRC variable, making
                     91:  *                     sure that any variable that needs to exist
                     92:  *                     at the very least has the empty value.
1.1       cgd        93:  *
1.190     rillig     94:  *     GNode_IsOODate  Determine if a target is out-of-date.
1.1       cgd        95:  *
1.151     rillig     96:  *     Make_HandleUse  See if a child is a .USE node for a parent
                     97:  *                     and perform the .USE actions if so.
1.11      christos   98:  *
1.151     rillig     99:  *     Make_ExpandUse  Expand .USE nodes
1.1       cgd       100:  */
                    101:
1.185     rillig    102: #include "make.h"
                    103: #include "dir.h"
                    104: #include "job.h"
1.1       cgd       105:
1.136     rillig    106: /*     "@(#)make.c     8.1 (Berkeley) 6/6/93"  */
1.253   ! rillig    107: MAKE_RCSID("$NetBSD: make.c,v 1.252 2022/01/09 15:48:30 rillig Exp $");
1.136     rillig    108:
1.149     rillig    109: /* Sequence # to detect recursion. */
1.206     rillig    110: static unsigned int checked_seqno = 1;
1.149     rillig    111:
1.233     rillig    112: /*
                    113:  * The current fringe of the graph.
1.149     rillig    114:  * These are nodes which await examination by MakeOODate.
1.233     rillig    115:  * It is added to by Make_Update and subtracted from by MakeStartJobs
                    116:  */
1.226     rillig    117: static GNodeList toBeMade = LST_INIT;
1.1       cgd       118:
1.68      dsl       119:
1.154     rillig    120: void
                    121: debug_printf(const char *fmt, ...)
                    122: {
1.252     rillig    123:        va_list ap;
1.154     rillig    124:
1.252     rillig    125:        va_start(ap, fmt);
                    126:        vfprintf(opts.debug_file, fmt, ap);
                    127:        va_end(ap);
1.154     rillig    128: }
                    129:
1.245     rillig    130: static const char *
                    131: GNodeType_ToString(GNodeType type, void **freeIt)
                    132: {
                    133:        Buffer buf;
                    134:
                    135:        Buf_InitSize(&buf, 32);
1.248     rillig    136: #define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != OP_NONE, #flag)
1.245     rillig    137:        ADD(OP_DEPENDS);
                    138:        ADD(OP_FORCE);
                    139:        ADD(OP_DOUBLEDEP);
                    140:        ADD(OP_OPTIONAL);
                    141:        ADD(OP_USE);
                    142:        ADD(OP_EXEC);
                    143:        ADD(OP_IGNORE);
                    144:        ADD(OP_PRECIOUS);
                    145:        ADD(OP_SILENT);
                    146:        ADD(OP_MAKE);
                    147:        ADD(OP_JOIN);
                    148:        ADD(OP_MADE);
                    149:        ADD(OP_SPECIAL);
                    150:        ADD(OP_USEBEFORE);
                    151:        ADD(OP_INVISIBLE);
                    152:        ADD(OP_NOTMAIN);
                    153:        ADD(OP_PHONY);
                    154:        ADD(OP_NOPATH);
                    155:        ADD(OP_WAIT);
                    156:        ADD(OP_NOMETA);
                    157:        ADD(OP_META);
                    158:        ADD(OP_NOMETA_CMP);
                    159:        ADD(OP_SUBMAKE);
                    160:        ADD(OP_TRANSFORM);
                    161:        ADD(OP_MEMBER);
                    162:        ADD(OP_LIB);
                    163:        ADD(OP_ARCHV);
                    164:        ADD(OP_HAS_COMMANDS);
                    165:        ADD(OP_SAVE_CMDS);
                    166:        ADD(OP_DEPS_FOUND);
                    167:        ADD(OP_MARK);
                    168: #undef ADD
                    169:        return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
                    170: }
                    171:
                    172: static const char *
                    173: GNodeFlags_ToString(GNodeFlags flags, void **freeIt)
                    174: {
                    175:        Buffer buf;
                    176:
                    177:        Buf_InitSize(&buf, 32);
1.246     rillig    178: #define ADD(flag, name) Buf_AddFlag(&buf, flags.flag, name)
                    179:        ADD(remake, "REMAKE");
                    180:        ADD(childMade, "CHILDMADE");
                    181:        ADD(force, "FORCE");
                    182:        ADD(doneWait, "DONE_WAIT");
                    183:        ADD(doneOrder, "DONE_ORDER");
                    184:        ADD(fromDepend, "FROM_DEPEND");
                    185:        ADD(doneAllsrc, "DONE_ALLSRC");
                    186:        ADD(cycle, "CYCLE");
                    187:        ADD(doneCycle, "DONECYCLE");
1.245     rillig    188: #undef ADD
                    189:        return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
                    190: }
1.122     rillig    191:
                    192: void
                    193: GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
                    194:                    const char *suffix)
                    195: {
1.245     rillig    196:        void *type_freeIt = NULL;
                    197:        void *flags_freeIt = NULL;
1.122     rillig    198:
1.239     rillig    199:        fprintf(f, "%s%s, type %s, flags %s%s",
1.122     rillig    200:            prefix,
1.239     rillig    201:            GNodeMade_Name(gn->made),
1.245     rillig    202:            GNodeType_ToString(gn->type, &type_freeIt),
                    203:            GNodeFlags_ToString(gn->flags, &flags_freeIt),
1.122     rillig    204:            suffix);
1.245     rillig    205:        free(type_freeIt);
                    206:        free(flags_freeIt);
1.122     rillig    207: }
                    208:
1.243     rillig    209: bool
1.186     rillig    210: GNode_ShouldExecute(GNode *gn)
1.159     rillig    211: {
1.217     rillig    212:        return !((gn->type & OP_MAKE)
                    213:            ? opts.noRecursiveExecute
                    214:            : opts.noExecute);
1.159     rillig    215: }
                    216:
1.149     rillig    217: /* Update the youngest child of the node, according to the given child. */
1.143     rillig    218: void
1.189     rillig    219: GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
1.1       cgd       220: {
1.217     rillig    221:        if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
                    222:                gn->youngestChild = cgn;
1.1       cgd       223: }
1.5       jtc       224:
1.243     rillig    225: static bool
1.197     rillig    226: IsOODateRegular(GNode *gn)
1.195     rillig    227: {
1.217     rillig    228:        /* These rules are inherited from the original Make. */
1.197     rillig    229:
1.217     rillig    230:        if (gn->youngestChild != NULL) {
                    231:                if (gn->mtime < gn->youngestChild->mtime) {
                    232:                        DEBUG1(MAKE, "modified before source \"%s\"...",
                    233:                            GNode_Path(gn->youngestChild));
1.243     rillig    234:                        return true;
1.217     rillig    235:                }
1.243     rillig    236:                return false;
1.197     rillig    237:        }
                    238:
1.217     rillig    239:        if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
1.231     rillig    240:                DEBUG0(MAKE, "nonexistent and no sources...");
1.243     rillig    241:                return true;
1.217     rillig    242:        }
1.197     rillig    243:
1.217     rillig    244:        if (gn->type & OP_DOUBLEDEP) {
                    245:                DEBUG0(MAKE, ":: operator and no sources...");
1.243     rillig    246:                return true;
1.217     rillig    247:        }
1.197     rillig    248:
1.243     rillig    249:        return false;
1.195     rillig    250: }
                    251:
1.233     rillig    252: /*
                    253:  * See if the node is out of date with respect to its sources.
1.1       cgd       254:  *
1.149     rillig    255:  * Used by Make_Run when deciding which nodes to place on the
                    256:  * toBeMade queue initially and by Make_Update to screen out .USE and
                    257:  * .EXEC nodes. In the latter case, however, any other sort of node
                    258:  * must be considered out-of-date since at least one of its children
                    259:  * will have been recreated.
1.1       cgd       260:  *
1.178     rillig    261:  * The mtime field of the node and the youngestChild field of its parents
1.149     rillig    262:  * may be changed.
1.1       cgd       263:  */
1.243     rillig    264: bool
1.190     rillig    265: GNode_IsOODate(GNode *gn)
1.1       cgd       266: {
1.243     rillig    267:        bool oodate;
1.1       cgd       268:
                    269:        /*
1.229     rillig    270:         * Certain types of targets needn't even be sought as their datedness
                    271:         * doesn't depend on their modification time...
1.1       cgd       272:         */
1.229     rillig    273:        if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
1.243     rillig    274:                Dir_UpdateMTime(gn, true);
1.229     rillig    275:                if (DEBUG(MAKE)) {
                    276:                        if (gn->mtime != 0)
                    277:                                debug_printf("modified %s...",
                    278:                                    Targ_FmtTime(gn->mtime));
                    279:                        else
1.231     rillig    280:                                debug_printf("nonexistent...");
1.229     rillig    281:                }
                    282:        }
1.6       christos  283:
                    284:        /*
1.229     rillig    285:         * A target is remade in one of the following circumstances:
                    286:         *
                    287:         *      its modification time is smaller than that of its youngest
                    288:         *      child and it would actually be run (has commands or is not
                    289:         *      GNode_IsTarget)
                    290:         *
                    291:         *      it's the object of a force operator
                    292:         *
                    293:         *      it has no children, was on the lhs of an operator and doesn't
                    294:         *      exist already.
                    295:         *
                    296:         * Libraries are only considered out-of-date if the archive module
                    297:         * says they are.
                    298:         *
                    299:         * These weird rules are brought to you by Backward-Compatibility
                    300:         * and the strange people who wrote 'Make'.
1.6       christos  301:         */
1.229     rillig    302:        if (gn->type & (OP_USE | OP_USEBEFORE)) {
                    303:                /*
                    304:                 * If the node is a USE node it is *never* out of date
                    305:                 * no matter *what*.
                    306:                 */
                    307:                DEBUG0(MAKE, ".USE node...");
1.243     rillig    308:                oodate = false;
1.229     rillig    309:        } else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
                    310:                DEBUG0(MAKE, "library...");
                    311:
                    312:                /*
                    313:                 * always out of date if no children and :: target
1.231     rillig    314:                 * or nonexistent.
1.229     rillig    315:                 */
                    316:                oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
                    317:                          (gn->youngestChild == NULL &&
                    318:                           (gn->type & OP_DOUBLEDEP)));
                    319:        } else if (gn->type & OP_JOIN) {
                    320:                /*
                    321:                 * A target with the .JOIN attribute is only considered
                    322:                 * out-of-date if any of its children was out-of-date.
                    323:                 */
                    324:                DEBUG0(MAKE, ".JOIN node...");
                    325:                DEBUG1(MAKE, "source %smade...",
1.246     rillig    326:                    gn->flags.childMade ? "" : "not ");
                    327:                oodate = gn->flags.childMade;
1.229     rillig    328:        } else if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
                    329:                /*
                    330:                 * A node which is the object of the force (!) operator or
                    331:                 * which has the .EXEC attribute is always considered
                    332:                 * out-of-date.
                    333:                 */
                    334:                if (DEBUG(MAKE)) {
                    335:                        if (gn->type & OP_FORCE) {
                    336:                                debug_printf("! operator...");
                    337:                        } else if (gn->type & OP_PHONY) {
                    338:                                debug_printf(".PHONY node...");
                    339:                        } else {
                    340:                                debug_printf(".EXEC node...");
                    341:                        }
                    342:                }
1.243     rillig    343:                oodate = true;
1.229     rillig    344:        } else if (IsOODateRegular(gn)) {
1.243     rillig    345:                oodate = true;
1.229     rillig    346:        } else {
                    347:                /*
1.231     rillig    348:                 * When a nonexistent child with no sources
1.229     rillig    349:                 * (such as a typically used FORCE source) has been made and
                    350:                 * the target of the child (usually a directory) has the same
1.231     rillig    351:                 * timestamp as the timestamp just given to the nonexistent
1.229     rillig    352:                 * child after it was considered made.
                    353:                 */
                    354:                if (DEBUG(MAKE)) {
1.246     rillig    355:                        if (gn->flags.force)
1.229     rillig    356:                                debug_printf("non existing child...");
                    357:                }
1.246     rillig    358:                oodate = gn->flags.force;
1.1       cgd       359:        }
                    360:
1.82      sjg       361: #ifdef USE_META
1.251     rillig    362:        if (useMeta)
1.229     rillig    363:                oodate = meta_oodate(gn, oodate);
1.82      sjg       364: #endif
                    365:
1.229     rillig    366:        /*
                    367:         * If the target isn't out-of-date, the parents need to know its
                    368:         * modification time. Note that targets that appear to be out-of-date
                    369:         * but aren't, because they have no commands and are GNode_IsTarget,
                    370:         * have their mtime stay below their children's mtime to keep parents
                    371:         * from thinking they're out-of-date.
                    372:         */
                    373:        if (!oodate) {
                    374:                GNodeListNode *ln;
                    375:                for (ln = gn->parents.first; ln != NULL; ln = ln->next)
                    376:                        GNode_UpdateYoungestChild(ln->datum, gn);
                    377:        }
1.1       cgd       378:
1.229     rillig    379:        return oodate;
1.1       cgd       380: }
1.108     rillig    381:
1.199     rillig    382: static void
                    383: PretendAllChildrenAreMade(GNode *pgn)
1.13      christos  384: {
1.217     rillig    385:        GNodeListNode *ln;
1.14      christos  386:
1.220     rillig    387:        for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
1.217     rillig    388:                GNode *cgn = ln->datum;
1.148     rillig    389:
1.217     rillig    390:                /* This may also update cgn->path. */
1.243     rillig    391:                Dir_UpdateMTime(cgn, false);
1.217     rillig    392:                GNode_UpdateYoungestChild(pgn, cgn);
                    393:                pgn->unmade--;
                    394:        }
1.198     rillig    395: }
                    396:
1.233     rillig    397: /*
                    398:  * Called by Make_Run and SuffApplyTransform on the downward pass to handle
1.109     rillig    399:  * .USE and transformation nodes, by copying the child node's commands, type
                    400:  * flags and children to the parent node.
                    401:  *
                    402:  * A .USE node is much like an explicit transformation rule, except its
                    403:  * commands are always added to the target node, even if the target already
                    404:  * has commands.
1.1       cgd       405:  *
1.50      wiz       406:  * Input:
1.161     rillig    407:  *     cgn             The source node, which is either a .USE/.USEBEFORE
                    408:  *                     node or a transformation node (OP_TRANSFORM).
                    409:  *     pgn             The target node
1.1       cgd       410:  */
1.43      pk        411: void
1.50      wiz       412: Make_HandleUse(GNode *cgn, GNode *pgn)
1.1       cgd       413: {
1.229     rillig    414:        GNodeListNode *ln;      /* An element in the children list */
1.44      pk        415:
                    416: #ifdef DEBUG_SRC
1.229     rillig    417:        if (!(cgn->type & (OP_USE | OP_USEBEFORE | OP_TRANSFORM))) {
                    418:                debug_printf("Make_HandleUse: called for plain node %s\n",
                    419:                    cgn->name);
                    420:                /* XXX: debug mode should not affect control flow */
                    421:                return;
                    422:        }
1.44      pk        423: #endif
1.1       cgd       424:
1.229     rillig    425:        if ((cgn->type & (OP_USE | OP_USEBEFORE)) ||
                    426:            Lst_IsEmpty(&pgn->commands)) {
                    427:                if (cgn->type & OP_USEBEFORE) {
                    428:                        /* .USEBEFORE */
                    429:                        Lst_PrependAll(&pgn->commands, &cgn->commands);
                    430:                } else {
                    431:                        /* .USE, or target has no commands */
                    432:                        Lst_AppendAll(&pgn->commands, &cgn->commands);
                    433:                }
1.210     rillig    434:        }
1.10      christos  435:
1.229     rillig    436:        for (ln = cgn->children.first; ln != NULL; ln = ln->next) {
                    437:                GNode *gn = ln->datum;
                    438:
                    439:                /*
                    440:                 * Expand variables in the .USE node's name
                    441:                 * and save the unexpanded form.
                    442:                 * We don't need to do this for commands.
                    443:                 * They get expanded properly when we execute.
                    444:                 */
                    445:                if (gn->uname == NULL) {
                    446:                        gn->uname = gn->name;
                    447:                } else {
                    448:                        free(gn->name);
                    449:                }
                    450:                (void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
                    451:                /* TODO: handle errors */
1.234     rillig    452:                if (gn->uname != NULL && strcmp(gn->name, gn->uname) != 0) {
1.229     rillig    453:                        /* See if we have a target for this node. */
                    454:                        GNode *tgn = Targ_FindNode(gn->name);
                    455:                        if (tgn != NULL)
                    456:                                gn = tgn;
                    457:                }
1.11      christos  458:
1.229     rillig    459:                Lst_Append(&pgn->children, gn);
                    460:                Lst_Append(&gn->parents, pgn);
                    461:                pgn->unmade++;
1.119     rillig    462:        }
1.10      christos  463:
1.229     rillig    464:        pgn->type |=
                    465:            cgn->type & ~(OP_OPMASK | OP_USE | OP_USEBEFORE | OP_TRANSFORM);
1.1       cgd       466: }
1.31      mycroft   467:
1.233     rillig    468: /*
                    469:  * Used by Make_Run on the downward pass to handle .USE nodes. Should be
1.149     rillig    470:  * called before the children are enqueued to be looked at by MakeAddChild.
                    471:  *
1.150     rillig    472:  * For a .USE child, the commands, type flags and children are copied to the
1.149     rillig    473:  * parent node, and since the relation to the .USE node is then no longer
                    474:  * needed, that relation is removed.
1.44      pk        475:  *
1.50      wiz       476:  * Input:
1.150     rillig    477:  *     cgn             the child, which may be a .USE node
                    478:  *     pgn             the current parent
1.44      pk        479:  */
1.150     rillig    480: static void
                    481: MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
1.5       jtc       482: {
1.243     rillig    483:        bool unmarked;
1.229     rillig    484:
                    485:        unmarked = !(cgn->type & OP_MARK);
                    486:        cgn->type |= OP_MARK;
1.44      pk        487:
1.229     rillig    488:        if (!(cgn->type & (OP_USE | OP_USEBEFORE)))
                    489:                return;
1.31      mycroft   490:
1.229     rillig    491:        if (unmarked)
                    492:                Make_HandleUse(cgn, pgn);
1.44      pk        493:
1.229     rillig    494:        /*
                    495:         * This child node is now "made", so we decrement the count of
                    496:         * unmade children in the parent... We also remove the child
                    497:         * from the parent's list to accurately reflect the number of decent
                    498:         * children the parent has. This is used by Make_Run to decide
                    499:         * whether to queue the parent or examine its children...
                    500:         */
                    501:        Lst_Remove(&pgn->children, ln);
                    502:        pgn->unmade--;
1.150     rillig    503: }
                    504:
                    505: static void
                    506: HandleUseNodes(GNode *gn)
                    507: {
1.217     rillig    508:        GNodeListNode *ln, *nln;
1.220     rillig    509:        for (ln = gn->children.first; ln != NULL; ln = nln) {
1.217     rillig    510:                nln = ln->next;
                    511:                MakeHandleUse(ln->datum, gn, ln);
                    512:        }
1.5       jtc       513: }
1.22      christos  514:
                    515:
1.233     rillig    516: /*
                    517:  * Check the modification time of a gnode, and update it if necessary.
                    518:  * Return 0 if the gnode does not exist, or its filesystem time if it does.
                    519:  */
1.22      christos  520: time_t
1.50      wiz       521: Make_Recheck(GNode *gn)
1.22      christos  522: {
1.229     rillig    523:        time_t mtime;
1.194     rillig    524:
1.243     rillig    525:        Dir_UpdateMTime(gn, true);
1.229     rillig    526:        mtime = gn->mtime;
1.22      christos  527:
                    528: #ifndef RECHECK
1.229     rillig    529:        /*
                    530:         * We can't re-stat the thing, but we can at least take care of rules
                    531:         * where a target depends on a source that actually creates the
                    532:         * target, but only if it has changed, e.g.
                    533:         *
                    534:         * parse.h : parse.o
                    535:         *
                    536:         * parse.o : parse.y
                    537:         *              yacc -d parse.y
                    538:         *              cc -c y.tab.c
                    539:         *              mv y.tab.o parse.o
                    540:         *              cmp -s y.tab.h parse.h || mv y.tab.h parse.h
                    541:         *
                    542:         * In this case, if the definitions produced by yacc haven't changed
                    543:         * from before, parse.h won't have been updated and gn->mtime will
                    544:         * reflect the current modification time for parse.h. This is
                    545:         * something of a kludge, I admit, but it's a useful one.
                    546:         *
                    547:         * XXX: People like to use a rule like "FRC:" to force things that
                    548:         * depend on FRC to be made, so we have to check for gn->children
                    549:         * being empty as well.
                    550:         */
                    551:        if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
                    552:                gn->mtime = now;
                    553:        }
1.22      christos  554: #else
1.229     rillig    555:        /*
                    556:         * This is what Make does and it's actually a good thing, as it
                    557:         * allows rules like
                    558:         *
                    559:         *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    560:         *
                    561:         * to function as intended. Unfortunately, thanks to the stateless
                    562:         * nature of NFS (by which I mean the loose coupling of two clients
                    563:         * using the same file from a common server), there are times when
                    564:         * the modification time of a file created on a remote machine
                    565:         * will not be modified before the local stat() implied by the
                    566:         * Dir_UpdateMTime occurs, thus leading us to believe that the file
                    567:         * is unchanged, wreaking havoc with files that depend on this one.
                    568:         *
                    569:         * I have decided it is better to make too much than to make too
                    570:         * little, so this stuff is commented out unless you're sure it's ok.
                    571:         * -- ardeb 1/12/88
                    572:         */
                    573:        /*
                    574:         * Christos, 4/9/92: If we are saving commands, pretend that
                    575:         * the target is made now. Otherwise archives with '...' rules
                    576:         * don't work!
                    577:         */
                    578:        if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
1.73      dsl       579:            (mtime == 0 && !(gn->type & OP_WAIT))) {
1.229     rillig    580:                DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
1.230     rillig    581:                    gn->name,
                    582:                    gn->mtime == 0 ? "nonexistent" : Targ_FmtTime(gn->mtime));
1.229     rillig    583:                gn->mtime = now;
                    584:        } else {
                    585:                DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
                    586:                    gn->name, Targ_FmtTime(gn->mtime));
                    587:        }
1.22      christos  588: #endif
1.193     rillig    589:
1.249     rillig    590:        /*
                    591:         * XXX: The returned mtime may differ from gn->mtime. Intentionally?
                    592:         */
1.229     rillig    593:        return mtime;
1.22      christos  594: }
                    595:
1.169     rillig    596: /*
                    597:  * Set the .PREFIX and .IMPSRC variables for all the implied parents
                    598:  * of this node.
                    599:  */
                    600: static void
                    601: UpdateImplicitParentsVars(GNode *cgn, const char *cname)
                    602: {
1.217     rillig    603:        GNodeListNode *ln;
                    604:        const char *cpref = GNode_VarPrefix(cgn);
1.169     rillig    605:
1.223     rillig    606:        for (ln = cgn->implicitParents.first; ln != NULL; ln = ln->next) {
1.217     rillig    607:                GNode *pgn = ln->datum;
1.246     rillig    608:                if (pgn->flags.remake) {
1.242     rillig    609:                        Var_Set(pgn, IMPSRC, cname);
1.217     rillig    610:                        if (cpref != NULL)
1.242     rillig    611:                                Var_Set(pgn, PREFIX, cpref);
1.217     rillig    612:                }
1.169     rillig    613:        }
                    614: }
                    615:
1.201     rillig    616: /* See if a .ORDER rule stops us from building this node. */
1.243     rillig    617: static bool
1.201     rillig    618: IsWaitingForOrder(GNode *gn)
                    619: {
1.217     rillig    620:        GNodeListNode *ln;
1.201     rillig    621:
1.221     rillig    622:        for (ln = gn->order_pred.first; ln != NULL; ln = ln->next) {
1.217     rillig    623:                GNode *ogn = ln->datum;
1.201     rillig    624:
1.246     rillig    625:                if (GNode_IsDone(ogn) || !ogn->flags.remake)
1.217     rillig    626:                        continue;
1.201     rillig    627:
1.217     rillig    628:                DEBUG2(MAKE,
                    629:                    "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
                    630:                    ogn->name, ogn->cohort_num);
1.243     rillig    631:                return true;
1.217     rillig    632:        }
1.243     rillig    633:        return false;
1.201     rillig    634: }
                    635:
1.238     rillig    636: static void MakeBuildParent(GNode *, GNodeListNode *);
1.216     rillig    637:
1.215     rillig    638: static void
                    639: ScheduleOrderSuccessors(GNode *gn)
                    640: {
1.226     rillig    641:        GNodeListNode *toBeMadeNext = toBeMade.first;
1.215     rillig    642:        GNodeListNode *ln;
                    643:
1.221     rillig    644:        for (ln = gn->order_succ.first; ln != NULL; ln = ln->next)
1.238     rillig    645:                MakeBuildParent(ln->datum, toBeMadeNext);
1.215     rillig    646: }
                    647:
1.233     rillig    648: /*
                    649:  * Perform update on the parents of a node. Used by JobFinish once
1.149     rillig    650:  * a node has been dealt with and by MakeStartJobs if it finds an
                    651:  * up-to-date node.
1.1       cgd       652:  *
1.149     rillig    653:  * The unmade field of pgn is decremented and pgn may be placed on
                    654:  * the toBeMade queue if this field becomes 0.
1.22      christos  655:  *
1.149     rillig    656:  * If the child was made, the parent's flag CHILDMADE field will be
                    657:  * set true.
1.1       cgd       658:  *
1.149     rillig    659:  * If the child is not up-to-date and still does not exist,
                    660:  * set the FORCE flag on the parents.
1.1       cgd       661:  *
1.178     rillig    662:  * If the child wasn't made, the youngestChild field of the parent will be
1.149     rillig    663:  * altered if the child's mtime is big enough.
1.1       cgd       664:  *
1.149     rillig    665:  * Finally, if the child is the implied source for the parent, the
                    666:  * parent's IMPSRC variable is set appropriately.
1.1       cgd       667:  */
                    668: void
1.50      wiz       669: Make_Update(GNode *cgn)
1.1       cgd       670: {
1.229     rillig    671:        const char *cname;      /* the child's name */
                    672:        time_t mtime = -1;
                    673:        GNodeList *parents;
                    674:        GNodeListNode *ln;
                    675:        GNode *centurion;
1.169     rillig    676:
1.229     rillig    677:        /* It is save to re-examine any nodes again */
                    678:        checked_seqno++;
                    679:
                    680:        cname = GNode_VarTarget(cgn);
1.119     rillig    681:
1.229     rillig    682:        DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
1.119     rillig    683:
                    684:        /*
1.229     rillig    685:         * If the child was actually made, see what its modification time is
                    686:         * now -- some rules won't actually update the file. If the file
                    687:         * still doesn't exist, make its mtime now.
1.119     rillig    688:         */
1.229     rillig    689:        if (cgn->made != UPTODATE) {
                    690:                mtime = Make_Recheck(cgn);
1.119     rillig    691:        }
1.68      dsl       692:
1.119     rillig    693:        /*
1.229     rillig    694:         * If this is a `::' node, we must consult its first instance
                    695:         * which is where all parents are linked.
1.119     rillig    696:         */
1.229     rillig    697:        if ((centurion = cgn->centurion) != NULL) {
                    698:                if (!Lst_IsEmpty(&cgn->parents))
                    699:                        Punt("%s%s: cohort has parents", cgn->name,
                    700:                            cgn->cohort_num);
                    701:                centurion->unmade_cohorts--;
                    702:                if (centurion->unmade_cohorts < 0)
                    703:                        Error("Graph cycles through centurion %s",
                    704:                            centurion->name);
                    705:        } else {
                    706:                centurion = cgn;
1.119     rillig    707:        }
1.229     rillig    708:        parents = &centurion->parents;
                    709:
                    710:        /* If this was a .ORDER node, schedule the RHS */
                    711:        ScheduleOrderSuccessors(centurion);
                    712:
                    713:        /* Now mark all the parents as having one less unmade child */
                    714:        for (ln = parents->first; ln != NULL; ln = ln->next) {
                    715:                GNode *pgn = ln->datum;
                    716:
                    717:                if (DEBUG(MAKE)) {
                    718:                        debug_printf("inspect parent %s%s: ", pgn->name,
                    719:                            pgn->cohort_num);
                    720:                        GNode_FprintDetails(opts.debug_file, "", pgn, "");
                    721:                        debug_printf(", unmade %d ", pgn->unmade - 1);
                    722:                }
1.47      pk        723:
1.246     rillig    724:                if (!pgn->flags.remake) {
1.229     rillig    725:                        /* This parent isn't needed */
                    726:                        DEBUG0(MAKE, "- not needed\n");
                    727:                        continue;
                    728:                }
                    729:                if (mtime == 0 && !(cgn->type & OP_WAIT))
1.246     rillig    730:                        pgn->flags.force = true;
1.229     rillig    731:
                    732:                /*
                    733:                 * If the parent has the .MADE attribute, its timestamp got
                    734:                 * updated to that of its newest child, and its unmade
                    735:                 * child count got set to zero in Make_ExpandUse().
                    736:                 * However other things might cause us to build one of its
                    737:                 * children - and so we mustn't do any processing here when
                    738:                 * the child build finishes.
                    739:                 */
                    740:                if (pgn->type & OP_MADE) {
                    741:                        DEBUG0(MAKE, "- .MADE\n");
                    742:                        continue;
                    743:                }
                    744:
                    745:                if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
                    746:                        if (cgn->made == MADE)
1.246     rillig    747:                                pgn->flags.childMade = true;
1.229     rillig    748:                        GNode_UpdateYoungestChild(pgn, cgn);
                    749:                }
                    750:
                    751:                /*
                    752:                 * A parent must wait for the completion of all instances
                    753:                 * of a `::' dependency.
                    754:                 */
                    755:                if (centurion->unmade_cohorts != 0 ||
                    756:                    !GNode_IsDone(centurion)) {
                    757:                        DEBUG2(MAKE,
                    758:                            "- centurion made %d, %d unmade cohorts\n",
                    759:                            centurion->made, centurion->unmade_cohorts);
                    760:                        continue;
                    761:                }
                    762:
                    763:                /* One more child of this parent is now made */
                    764:                pgn->unmade--;
                    765:                if (pgn->unmade < 0) {
                    766:                        if (DEBUG(MAKE)) {
                    767:                                debug_printf("Graph cycles through %s%s\n",
                    768:                                    pgn->name, pgn->cohort_num);
                    769:                                Targ_PrintGraph(2);
                    770:                        }
                    771:                        Error("Graph cycles through %s%s", pgn->name,
                    772:                            pgn->cohort_num);
                    773:                }
                    774:
                    775:                /*
                    776:                 * We must always rescan the parents of .WAIT and .ORDER
                    777:                 * nodes.
                    778:                 */
                    779:                if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
1.246     rillig    780:                    && !centurion->flags.doneOrder) {
1.229     rillig    781:                        DEBUG0(MAKE, "- unmade children\n");
                    782:                        continue;
                    783:                }
                    784:                if (pgn->made != DEFERRED) {
                    785:                        /*
                    786:                         * Either this parent is on a different branch of
                    787:                         * the tree, or it on the RHS of a .WAIT directive
                    788:                         * or it is already on the toBeMade list.
                    789:                         */
                    790:                        DEBUG0(MAKE, "- not deferred\n");
                    791:                        continue;
                    792:                }
1.201     rillig    793:
1.229     rillig    794:                if (IsWaitingForOrder(pgn))
                    795:                        continue;
1.201     rillig    796:
1.229     rillig    797:                if (DEBUG(MAKE)) {
                    798:                        debug_printf("- %s%s made, schedule %s%s (made %d)\n",
                    799:                            cgn->name, cgn->cohort_num,
                    800:                            pgn->name, pgn->cohort_num, pgn->made);
                    801:                        Targ_PrintNode(pgn, 2);
                    802:                }
                    803:                /* Ok, we can schedule the parent again */
                    804:                pgn->made = REQUESTED;
                    805:                Lst_Enqueue(&toBeMade, pgn);
                    806:        }
1.10      christos  807:
1.229     rillig    808:        UpdateImplicitParentsVars(cgn, cname);
1.1       cgd       809: }
1.108     rillig    810:
1.143     rillig    811: static void
1.144     rillig    812: UnmarkChildren(GNode *gn)
1.141     rillig    813: {
1.217     rillig    814:        GNodeListNode *ln;
1.141     rillig    815:
1.220     rillig    816:        for (ln = gn->children.first; ln != NULL; ln = ln->next) {
1.217     rillig    817:                GNode *child = ln->datum;
                    818:                child->type &= ~OP_MARK;
                    819:        }
1.141     rillig    820: }
                    821:
1.233     rillig    822: /*
                    823:  * Add a child's name to the ALLSRC and OODATE variables of the given
1.172     rillig    824:  * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
                    825:  * attributes. .EXEC and .USE children are very rarely going to be files,
                    826:  * so...
                    827:  *
1.149     rillig    828:  * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
                    829:  *
                    830:  * A child is added to the OODATE variable if its modification time is
                    831:  * later than that of its parent, as defined by Make, except if the
                    832:  * parent is a .JOIN node. In that case, it is only added to the OODATE
                    833:  * variable if it was actually made (since .JOIN nodes don't have
                    834:  * modification times, the comparison is rather unfair...)..
1.1       cgd       835:  *
1.141     rillig    836:  * Input:
1.162     rillig    837:  *     cgn             The child to add
                    838:  *     pgn             The parent to whose ALLSRC variable it should
1.141     rillig    839:  *                     be added
1.1       cgd       840:  */
1.142     rillig    841: static void
1.162     rillig    842: MakeAddAllSrc(GNode *cgn, GNode *pgn)
1.1       cgd       843: {
1.237     rillig    844:        const char *child, *allsrc;
                    845:
1.229     rillig    846:        if (cgn->type & OP_MARK)
                    847:                return;
                    848:        cgn->type |= OP_MARK;
1.31      mycroft   849:
1.237     rillig    850:        if (cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE | OP_INVISIBLE))
                    851:                return;
1.1       cgd       852:
1.237     rillig    853:        if (cgn->type & OP_ARCHV)
                    854:                child = GNode_VarMember(cgn);
                    855:        else
                    856:                child = GNode_Path(cgn);
                    857:
                    858:        if (cgn->type & OP_JOIN)
                    859:                allsrc = GNode_VarAllsrc(cgn);
                    860:        else
                    861:                allsrc = child;
                    862:
                    863:        if (allsrc != NULL)
1.242     rillig    864:                Var_Append(pgn, ALLSRC, allsrc);
1.237     rillig    865:
                    866:        if (pgn->type & OP_JOIN) {
                    867:                if (cgn->made == MADE)
1.242     rillig    868:                        Var_Append(pgn, OODATE, child);
1.237     rillig    869:
                    870:        } else if ((pgn->mtime < cgn->mtime) ||
                    871:                   (cgn->mtime >= now && cgn->made == MADE)) {
                    872:                /*
                    873:                 * It goes in the OODATE variable if the parent is
                    874:                 * younger than the child or if the child has been
                    875:                 * modified more recently than the start of the make.
                    876:                 * This is to keep pmake from getting confused if
                    877:                 * something else updates the parent after the make
                    878:                 * starts (shouldn't happen, I know, but sometimes it
                    879:                 * does). In such a case, if we've updated the child,
                    880:                 * the parent is likely to have a modification time
                    881:                 * later than that of the child and anything that
                    882:                 * relies on the OODATE variable will be hosed.
                    883:                 *
                    884:                 * XXX: This will cause all made children to go in
                    885:                 * the OODATE variable, even if they're not touched,
                    886:                 * if RECHECK isn't defined, since cgn->mtime is set
                    887:                 * to now in Make_Update. According to some people,
                    888:                 * this is good...
                    889:                 */
1.242     rillig    890:                Var_Append(pgn, OODATE, child);
1.45      pk        891:        }
1.1       cgd       892: }
1.108     rillig    893:
1.233     rillig    894: /*
                    895:  * Set up the ALLSRC and OODATE variables. Sad to say, it must be
1.149     rillig    896:  * done separately, rather than while traversing the graph. This is
                    897:  * because Make defined OODATE to contain all sources whose modification
                    898:  * times were later than that of the target, *not* those sources that
                    899:  * were out-of-date. Since in both compatibility and native modes,
                    900:  * the modification time of the parent isn't found until the child
                    901:  * has been dealt with, we have to wait until now to fill in the
                    902:  * variable. As for ALLSRC, the ordering is important and not
                    903:  * guaranteed when in native mode, so it must be set here, too.
1.1       cgd       904:  *
1.149     rillig    905:  * If the node is a .JOIN node, its TARGET variable will be set to
                    906:  * match its ALLSRC variable.
1.1       cgd       907:  */
                    908: void
1.244     rillig    909: GNode_SetLocalVars(GNode *gn)
1.1       cgd       910: {
1.217     rillig    911:        GNodeListNode *ln;
                    912:
1.246     rillig    913:        if (gn->flags.doneAllsrc)
1.217     rillig    914:                return;
1.162     rillig    915:
1.217     rillig    916:        UnmarkChildren(gn);
1.220     rillig    917:        for (ln = gn->children.first; ln != NULL; ln = ln->next)
1.217     rillig    918:                MakeAddAllSrc(ln->datum, gn);
1.99      rillig    919:
1.242     rillig    920:        if (!Var_Exists(gn, OODATE))
                    921:                Var_Set(gn, OODATE, "");
                    922:        if (!Var_Exists(gn, ALLSRC))
                    923:                Var_Set(gn, ALLSRC, "");
1.217     rillig    924:
                    925:        if (gn->type & OP_JOIN)
1.242     rillig    926:                Var_Set(gn, TARGET, GNode_VarAllsrc(gn));
1.246     rillig    927:        gn->flags.doneAllsrc = true;
1.1       cgd       928: }
1.108     rillig    929:
1.243     rillig    930: static bool
1.214     rillig    931: MakeBuildChild(GNode *cn, GNodeListNode *toBeMadeNext)
1.68      dsl       932: {
                    933:
1.217     rillig    934:        if (DEBUG(MAKE)) {
                    935:                debug_printf("MakeBuildChild: inspect %s%s, ",
                    936:                    cn->name, cn->cohort_num);
                    937:                GNode_FprintDetails(opts.debug_file, "", cn, "\n");
                    938:        }
                    939:        if (GNode_IsReady(cn))
1.243     rillig    940:                return false;
1.217     rillig    941:
                    942:        /* If this node is on the RHS of a .ORDER, check LHSs. */
                    943:        if (IsWaitingForOrder(cn)) {
1.249     rillig    944:                /*
                    945:                 * Can't build this (or anything else in this child list) yet
                    946:                 */
1.217     rillig    947:                cn->made = DEFERRED;
1.243     rillig    948:                return false;   /* but keep looking */
1.217     rillig    949:        }
                    950:
                    951:        DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n",
1.229     rillig    952:            cn->name, cn->cohort_num);
1.68      dsl       953:
1.217     rillig    954:        cn->made = REQUESTED;
                    955:        if (toBeMadeNext == NULL)
1.226     rillig    956:                Lst_Append(&toBeMade, cn);
1.217     rillig    957:        else
1.226     rillig    958:                Lst_InsertBefore(&toBeMade, toBeMadeNext, cn);
1.68      dsl       959:
1.217     rillig    960:        if (cn->unmade_cohorts != 0) {
                    961:                ListNode *ln;
1.212     rillig    962:
1.222     rillig    963:                for (ln = cn->cohorts.first; ln != NULL; ln = ln->next)
1.235     rillig    964:                        if (MakeBuildChild(ln->datum, toBeMadeNext))
1.217     rillig    965:                                break;
                    966:        }
1.68      dsl       967:
1.217     rillig    968:        /*
                    969:         * If this node is a .WAIT node with unmade children
                    970:         * then don't add the next sibling.
                    971:         */
                    972:        return cn->type & OP_WAIT && cn->unmade > 0;
1.68      dsl       973: }
                    974:
1.207     rillig    975: /* When a .ORDER LHS node completes, we do this on each RHS. */
1.238     rillig    976: static void
1.216     rillig    977: MakeBuildParent(GNode *pn, GNodeListNode *toBeMadeNext)
1.68      dsl       978: {
1.217     rillig    979:        if (pn->made != DEFERRED)
1.238     rillig    980:                return;
1.68      dsl       981:
1.235     rillig    982:        if (!MakeBuildChild(pn, toBeMadeNext)) {
1.217     rillig    983:                /* When this node is built, reschedule its parents. */
1.246     rillig    984:                pn->flags.doneOrder = true;
1.217     rillig    985:        }
1.68      dsl       986: }
                    987:
1.228     rillig    988: static void
                    989: MakeChildren(GNode *gn)
                    990: {
                    991:        GNodeListNode *toBeMadeNext = toBeMade.first;
                    992:        GNodeListNode *ln;
                    993:
                    994:        for (ln = gn->children.first; ln != NULL; ln = ln->next)
1.235     rillig    995:                if (MakeBuildChild(ln->datum, toBeMadeNext))
1.228     rillig    996:                        break;
                    997: }
                    998:
1.233     rillig    999: /*
                   1000:  * Start as many jobs as possible, taking them from the toBeMade queue.
1.149     rillig   1001:  *
1.207     rillig   1002:  * If the -q option was given, no job will be started,
1.149     rillig   1003:  * but as soon as an out-of-date target is found, this function
1.243     rillig   1004:  * returns true. In all other cases, this function returns false.
1.149     rillig   1005:  */
1.243     rillig   1006: static bool
1.50      wiz      1007: MakeStartJobs(void)
1.1       cgd      1008: {
1.229     rillig   1009:        GNode *gn;
1.243     rillig   1010:        bool have_token = false;
1.10      christos 1011:
1.229     rillig   1012:        while (!Lst_IsEmpty(&toBeMade)) {
                   1013:                /*
                   1014:                 * Get token now to avoid cycling job-list when we only
                   1015:                 * have 1 token
                   1016:                 */
                   1017:                if (!have_token && !Job_TokenWithdraw())
                   1018:                        break;
1.243     rillig   1019:                have_token = true;
1.60      dsl      1020:
1.229     rillig   1021:                gn = Lst_Dequeue(&toBeMade);
                   1022:                DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
1.68      dsl      1023:
1.229     rillig   1024:                if (gn->made != REQUESTED) {
1.253   ! rillig   1025:                        debug_printf("internal error: made = %s\n",
        !          1026:                            GNodeMade_Name(gn->made));
        !          1027:                        Targ_PrintNode(gn, 2);
        !          1028:                        Targ_PrintNodes(&toBeMade, 2);
        !          1029:                        Targ_PrintGraph(3);
        !          1030:                        abort();
1.229     rillig   1031:                }
1.1       cgd      1032:
1.229     rillig   1033:                if (gn->checked_seqno == checked_seqno) {
                   1034:                        /*
                   1035:                         * We've already looked at this node since a job
                   1036:                         * finished...
                   1037:                         */
                   1038:                        DEBUG2(MAKE, "already checked %s%s\n", gn->name,
                   1039:                            gn->cohort_num);
                   1040:                        gn->made = DEFERRED;
                   1041:                        continue;
                   1042:                }
                   1043:                gn->checked_seqno = checked_seqno;
1.70      dsl      1044:
1.229     rillig   1045:                if (gn->unmade != 0) {
                   1046:                        /*
                   1047:                         * We can't build this yet, add all unmade children
                   1048:                         * to toBeMade, just before the current first element.
                   1049:                         */
                   1050:                        gn->made = DEFERRED;
                   1051:
                   1052:                        MakeChildren(gn);
                   1053:
                   1054:                        /* and drop this node on the floor */
                   1055:                        DEBUG2(MAKE, "dropped %s%s\n", gn->name,
                   1056:                            gn->cohort_num);
                   1057:                        continue;
                   1058:                }
1.213     rillig   1059:
1.229     rillig   1060:                gn->made = BEINGMADE;
                   1061:                if (GNode_IsOODate(gn)) {
                   1062:                        DEBUG0(MAKE, "out-of-date\n");
1.250     rillig   1063:                        if (opts.query)
1.243     rillig   1064:                                return true;
1.244     rillig   1065:                        GNode_SetLocalVars(gn);
1.229     rillig   1066:                        Job_Make(gn);
1.243     rillig   1067:                        have_token = false;
1.229     rillig   1068:                } else {
                   1069:                        DEBUG0(MAKE, "up-to-date\n");
                   1070:                        gn->made = UPTODATE;
                   1071:                        if (gn->type & OP_JOIN) {
                   1072:                                /*
                   1073:                                 * Even for an up-to-date .JOIN node, we
1.241     rillig   1074:                                 * need it to have its local variables so
1.229     rillig   1075:                                 * references to it get the correct value
1.241     rillig   1076:                                 * for .TARGET when building up the local
1.229     rillig   1077:                                 * variables of its parent(s)...
                   1078:                                 */
1.244     rillig   1079:                                GNode_SetLocalVars(gn);
1.229     rillig   1080:                        }
                   1081:                        Make_Update(gn);
                   1082:                }
1.1       cgd      1083:        }
1.59      dsl      1084:
1.229     rillig   1085:        if (have_token)
                   1086:                Job_TokenReturn();
1.59      dsl      1087:
1.243     rillig   1088:        return false;
1.1       cgd      1089: }
1.108     rillig   1090:
1.207     rillig   1091: /* Print the status of a .ORDER node. */
1.173     rillig   1092: static void
                   1093: MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
1.68      dsl      1094: {
1.217     rillig   1095:        if (!GNode_IsWaitingFor(ogn))
                   1096:                return;
1.68      dsl      1097:
1.217     rillig   1098:        printf("    `%s%s' has .ORDER dependency against %s%s ",
                   1099:            gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
                   1100:        GNode_FprintDetails(stdout, "(", ogn, ")\n");
                   1101:
                   1102:        if (DEBUG(MAKE) && opts.debug_file != stdout) {
                   1103:                debug_printf("    `%s%s' has .ORDER dependency against %s%s ",
                   1104:                    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
                   1105:                GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
                   1106:        }
1.173     rillig   1107: }
                   1108:
                   1109: static void
                   1110: MakePrintStatusOrder(GNode *gn)
                   1111: {
1.217     rillig   1112:        GNodeListNode *ln;
1.221     rillig   1113:        for (ln = gn->order_pred.first; ln != NULL; ln = ln->next)
1.217     rillig   1114:                MakePrintStatusOrderNode(ln->datum, gn);
1.68      dsl      1115: }
                   1116:
1.174     rillig   1117: static void MakePrintStatusList(GNodeList *, int *);
                   1118:
1.218     rillig   1119: /*
                   1120:  * Print the status of a top-level node, viz. it being up-to-date already
1.149     rillig   1121:  * or not created due to an error in a lower level.
                   1122:  */
1.243     rillig   1123: static bool
1.174     rillig   1124: MakePrintStatus(GNode *gn, int *errors)
1.1       cgd      1125: {
1.246     rillig   1126:        if (gn->flags.doneCycle) {
1.229     rillig   1127:                /*
                   1128:                 * We've completely processed this node before, don't do
                   1129:                 * it again.
                   1130:                 */
1.243     rillig   1131:                return false;
1.229     rillig   1132:        }
                   1133:
                   1134:        if (gn->unmade == 0) {
1.246     rillig   1135:                gn->flags.doneCycle = true;
1.229     rillig   1136:                switch (gn->made) {
                   1137:                case UPTODATE:
                   1138:                        printf("`%s%s' is up to date.\n", gn->name,
                   1139:                            gn->cohort_num);
                   1140:                        break;
                   1141:                case MADE:
                   1142:                        break;
                   1143:                case UNMADE:
                   1144:                case DEFERRED:
                   1145:                case REQUESTED:
                   1146:                case BEINGMADE:
                   1147:                        (*errors)++;
                   1148:                        printf("`%s%s' was not built", gn->name,
                   1149:                            gn->cohort_num);
                   1150:                        GNode_FprintDetails(stdout, " (", gn, ")!\n");
                   1151:                        if (DEBUG(MAKE) && opts.debug_file != stdout) {
                   1152:                                debug_printf("`%s%s' was not built", gn->name,
                   1153:                                    gn->cohort_num);
                   1154:                                GNode_FprintDetails(opts.debug_file, " (", gn,
                   1155:                                    ")!\n");
                   1156:                        }
                   1157:                        /* Most likely problem is actually caused by .ORDER */
                   1158:                        MakePrintStatusOrder(gn);
                   1159:                        break;
                   1160:                default:
                   1161:                        /* Errors - already counted */
                   1162:                        printf("`%s%s' not remade because of errors.\n",
                   1163:                            gn->name, gn->cohort_num);
                   1164:                        if (DEBUG(MAKE) && opts.debug_file != stdout)
                   1165:                                debug_printf(
                   1166:                                    "`%s%s' not remade because of errors.\n",
                   1167:                                    gn->name, gn->cohort_num);
                   1168:                        break;
                   1169:                }
1.243     rillig   1170:                return false;
1.229     rillig   1171:        }
                   1172:
                   1173:        DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
                   1174:            gn->name, gn->cohort_num, gn->unmade);
                   1175:        /*
                   1176:         * If printing cycles and came to one that has unmade children,
                   1177:         * print out the cycle by recursing on its children.
                   1178:         */
1.246     rillig   1179:        if (!gn->flags.cycle) {
1.229     rillig   1180:                /* First time we've seen this node, check all children */
1.246     rillig   1181:                gn->flags.cycle = true;
1.229     rillig   1182:                MakePrintStatusList(&gn->children, errors);
                   1183:                /* Mark that this node needn't be processed again */
1.246     rillig   1184:                gn->flags.doneCycle = true;
1.243     rillig   1185:                return false;
1.229     rillig   1186:        }
1.68      dsl      1187:
1.229     rillig   1188:        /* Only output the error once per node */
1.246     rillig   1189:        gn->flags.doneCycle = true;
1.229     rillig   1190:        Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
                   1191:        if ((*errors)++ > 100)
                   1192:                /* Abandon the whole error report */
1.243     rillig   1193:                return true;
1.68      dsl      1194:
1.229     rillig   1195:        /* Reporting for our children will give the rest of the loop */
1.220     rillig   1196:        MakePrintStatusList(&gn->children, errors);
1.243     rillig   1197:        return false;
1.1       cgd      1198: }
1.108     rillig   1199:
1.174     rillig   1200: static void
                   1201: MakePrintStatusList(GNodeList *gnodes, int *errors)
                   1202: {
1.217     rillig   1203:        GNodeListNode *ln;
                   1204:
                   1205:        for (ln = gnodes->first; ln != NULL; ln = ln->next)
                   1206:                if (MakePrintStatus(ln->datum, errors))
                   1207:                        break;
1.174     rillig   1208: }
1.11      christos 1209:
1.200     rillig   1210: static void
                   1211: ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
                   1212: {
1.217     rillig   1213:        ListNode *ln;
1.200     rillig   1214:
1.217     rillig   1215:        for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
                   1216:                GNode *gn = ln->datum;
1.200     rillig   1217:
1.246     rillig   1218:                if (gn->flags.remake)
1.217     rillig   1219:                        continue;
                   1220:                if (gn->type & (OP_USE | OP_USEBEFORE))
                   1221:                        continue;
1.200     rillig   1222:
1.217     rillig   1223:                DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
                   1224:                    gn->name, gn->cohort_num);
                   1225:                Lst_Enqueue(examine, gn);
                   1226:        }
1.200     rillig   1227: }
                   1228:
1.233     rillig   1229: /*
                   1230:  * Expand .USE nodes and create a new targets list.
1.50      wiz      1231:  *
                   1232:  * Input:
                   1233:  *     targs           the initial list of targets
1.1       cgd      1234:  */
1.68      dsl      1235: void
1.137     rillig   1236: Make_ExpandUse(GNodeList *targs)
1.1       cgd      1237: {
1.229     rillig   1238:        GNodeList examine = LST_INIT;   /* Queue of targets to examine */
                   1239:        Lst_AppendAll(&examine, targs);
                   1240:
                   1241:        /*
                   1242:         * Make an initial downward pass over the graph, marking nodes to
                   1243:         * be made as we go down.
                   1244:         *
                   1245:         * We call Suff_FindDeps to find where a node is and to get some
                   1246:         * children for it if it has none and also has no commands. If the
                   1247:         * node is a leaf, we stick it on the toBeMade queue to be looked
                   1248:         * at in a minute, otherwise we add its children to our queue and
                   1249:         * go on about our business.
                   1250:         */
                   1251:        while (!Lst_IsEmpty(&examine)) {
                   1252:                GNode *gn = Lst_Dequeue(&examine);
1.10      christos 1253:
1.246     rillig   1254:                if (gn->flags.remake)
1.229     rillig   1255:                        /* We've looked at this one already */
                   1256:                        continue;
1.246     rillig   1257:                gn->flags.remake = true;
1.229     rillig   1258:                DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
                   1259:                    gn->name, gn->cohort_num);
1.10      christos 1260:
1.229     rillig   1261:                if (gn->type & OP_DOUBLEDEP)
                   1262:                        Lst_PrependAll(&examine, &gn->cohorts);
1.10      christos 1263:
1.229     rillig   1264:                /*
                   1265:                 * Apply any .USE rules before looking for implicit
                   1266:                 * dependencies to make sure everything has commands that
                   1267:                 * should.
                   1268:                 *
                   1269:                 * Make sure that the TARGET is set, so that we can make
                   1270:                 * expansions.
                   1271:                 */
                   1272:                if (gn->type & OP_ARCHV) {
                   1273:                        char *eoa = strchr(gn->name, '(');
                   1274:                        char *eon = strchr(gn->name, ')');
                   1275:                        if (eoa == NULL || eon == NULL)
                   1276:                                continue;
                   1277:                        *eoa = '\0';
                   1278:                        *eon = '\0';
1.242     rillig   1279:                        Var_Set(gn, MEMBER, eoa + 1);
                   1280:                        Var_Set(gn, ARCHIVE, gn->name);
1.229     rillig   1281:                        *eoa = '(';
                   1282:                        *eon = ')';
                   1283:                }
1.68      dsl      1284:
1.243     rillig   1285:                Dir_UpdateMTime(gn, false);
1.242     rillig   1286:                Var_Set(gn, TARGET, GNode_Path(gn));
1.229     rillig   1287:                UnmarkChildren(gn);
                   1288:                HandleUseNodes(gn);
                   1289:
                   1290:                if (!(gn->type & OP_MADE))
                   1291:                        Suff_FindDeps(gn);
                   1292:                else {
                   1293:                        PretendAllChildrenAreMade(gn);
                   1294:                        if (gn->unmade != 0) {
                   1295:                                printf(
                   1296:                                    "Warning: "
                   1297:                                    "%s%s still has %d unmade children\n",
                   1298:                                    gn->name, gn->cohort_num, gn->unmade);
                   1299:                        }
                   1300:                }
1.68      dsl      1301:
1.229     rillig   1302:                if (gn->unmade != 0)
                   1303:                        ExamineLater(&examine, &gn->children);
                   1304:        }
1.68      dsl      1305:
1.229     rillig   1306:        Lst_Done(&examine);
1.68      dsl      1307: }
                   1308:
1.139     rillig   1309: /* Make the .WAIT node depend on the previous children */
                   1310: static void
                   1311: add_wait_dependency(GNodeListNode *owln, GNode *wn)
1.68      dsl      1312: {
1.217     rillig   1313:        GNodeListNode *cln;
                   1314:        GNode *cn;
1.68      dsl      1315:
1.217     rillig   1316:        for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
                   1317:                DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
                   1318:                    cn->name, cn->cohort_num, wn->name);
                   1319:
1.249     rillig   1320:                /*
                   1321:                 * XXX: This pattern should be factored out, it repeats often
                   1322:                 */
1.220     rillig   1323:                Lst_Append(&wn->children, cn);
1.217     rillig   1324:                wn->unmade++;
1.220     rillig   1325:                Lst_Append(&cn->parents, wn);
1.217     rillig   1326:        }
1.68      dsl      1327: }
                   1328:
1.149     rillig   1329: /* Convert .WAIT nodes into dependencies. */
1.68      dsl      1330: static void
1.137     rillig   1331: Make_ProcessWait(GNodeList *targs)
1.68      dsl      1332: {
1.229     rillig   1333:        GNode *pgn;             /* 'parent' node we are examining */
                   1334:        GNodeListNode *owln;    /* Previous .WAIT node */
                   1335:        GNodeList examine;      /* List of targets to examine */
1.68      dsl      1336:
1.229     rillig   1337:        /*
                   1338:         * We need all the nodes to have a common parent in order for the
                   1339:         * .WAIT and .ORDER scheduling to work.
                   1340:         * Perhaps this should be done earlier...
                   1341:         */
                   1342:
                   1343:        pgn = GNode_New(".MAIN");
1.246     rillig   1344:        pgn->flags.remake = true;
1.229     rillig   1345:        pgn->type = OP_PHONY | OP_DEPENDS;
                   1346:        /* Get it displayed in the diag dumps */
                   1347:        Lst_Prepend(Targ_List(), pgn);
1.164     rillig   1348:
1.229     rillig   1349:        {
                   1350:                GNodeListNode *ln;
                   1351:                for (ln = targs->first; ln != NULL; ln = ln->next) {
                   1352:                        GNode *cgn = ln->datum;
                   1353:
                   1354:                        Lst_Append(&pgn->children, cgn);
                   1355:                        Lst_Append(&cgn->parents, pgn);
                   1356:                        pgn->unmade++;
                   1357:                }
1.164     rillig   1358:        }
1.68      dsl      1359:
1.229     rillig   1360:        /* Start building with the 'dummy' .MAIN' node */
                   1361:        MakeBuildChild(pgn, NULL);
1.68      dsl      1362:
1.229     rillig   1363:        Lst_Init(&examine);
                   1364:        Lst_Append(&examine, pgn);
1.68      dsl      1365:
1.229     rillig   1366:        while (!Lst_IsEmpty(&examine)) {
                   1367:                GNodeListNode *ln;
1.164     rillig   1368:
1.229     rillig   1369:                pgn = Lst_Dequeue(&examine);
1.99      rillig   1370:
1.229     rillig   1371:                /* We only want to process each child-list once */
1.246     rillig   1372:                if (pgn->flags.doneWait)
1.229     rillig   1373:                        continue;
1.246     rillig   1374:                pgn->flags.doneWait = true;
1.229     rillig   1375:                DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
1.68      dsl      1376:
1.229     rillig   1377:                if (pgn->type & OP_DOUBLEDEP)
                   1378:                        Lst_PrependAll(&examine, &pgn->cohorts);
1.1       cgd      1379:
1.229     rillig   1380:                owln = pgn->children.first;
                   1381:                for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
                   1382:                        GNode *cgn = ln->datum;
                   1383:                        if (cgn->type & OP_WAIT) {
                   1384:                                add_wait_dependency(owln, cgn);
                   1385:                                owln = ln;
                   1386:                        } else {
                   1387:                                Lst_Append(&examine, cgn);
                   1388:                        }
                   1389:                }
1.1       cgd      1390:        }
1.10      christos 1391:
1.229     rillig   1392:        Lst_Done(&examine);
1.11      christos 1393: }
                   1394:
1.227     rillig   1395: /*
                   1396:  * Initialize the nodes to remake and the list of nodes which are ready to
                   1397:  * be made by doing a breadth-first traversal of the graph starting from the
                   1398:  * nodes in the given list. Once this traversal is finished, all the 'leaves'
                   1399:  * of the graph are in the toBeMade queue.
                   1400:  *
                   1401:  * Using this queue and the Job module, work back up the graph, calling on
                   1402:  * MakeStartJobs to keep the job table as full as possible.
1.11      christos 1403:  *
1.50      wiz      1404:  * Input:
                   1405:  *     targs           the initial list of targets
                   1406:  *
1.11      christos 1407:  * Results:
1.243     rillig   1408:  *     True if work was done, false otherwise.
1.11      christos 1409:  *
                   1410:  * Side Effects:
                   1411:  *     The make field of all nodes involved in the creation of the given
                   1412:  *     targets is set to 1. The toBeMade list is set to contain all the
                   1413:  *     'leaves' of these subgraphs.
                   1414:  */
1.243     rillig   1415: bool
1.137     rillig   1416: Make_Run(GNodeList *targs)
1.11      christos 1417: {
1.229     rillig   1418:        int errors;             /* Number of errors the Job module reports */
1.11      christos 1419:
1.229     rillig   1420:        /* Start trying to make the current targets... */
                   1421:        Lst_Init(&toBeMade);
1.68      dsl      1422:
1.229     rillig   1423:        Make_ExpandUse(targs);
                   1424:        Make_ProcessWait(targs);
1.68      dsl      1425:
1.229     rillig   1426:        if (DEBUG(MAKE)) {
                   1427:                debug_printf("#***# full graph\n");
                   1428:                Targ_PrintGraph(1);
                   1429:        }
1.1       cgd      1430:
1.250     rillig   1431:        if (opts.query) {
1.229     rillig   1432:                /*
                   1433:                 * We wouldn't do any work unless we could start some jobs
                   1434:                 * in the next loop... (we won't actually start any, of
                   1435:                 * course, this is just to see if any of the targets was out
                   1436:                 * of date)
                   1437:                 */
                   1438:                return MakeStartJobs();
                   1439:        }
1.1       cgd      1440:        /*
1.229     rillig   1441:         * Initialization. At the moment, no jobs are running and until some
                   1442:         * get started, nothing will happen since the remaining upward
                   1443:         * traversal of the graph is performed by the routines in job.c upon
                   1444:         * the finishing of a job. So we fill the Job table as much as we can
                   1445:         * before going into our loop.
1.1       cgd      1446:         */
                   1447:        (void)MakeStartJobs();
                   1448:
1.229     rillig   1449:        /*
                   1450:         * Main Loop: The idea here is that the ending of jobs will take
                   1451:         * care of the maintenance of data structures and the waiting for
                   1452:         * output will cause us to be idle most of the time while our
                   1453:         * children run as much as possible. Because the job table is kept
                   1454:         * as full as possible, the only time when it will be empty is when
                   1455:         * all the jobs which need running have been run, so that is the end
                   1456:         * condition of this loop. Note that the Job module will exit if
                   1457:         * there were any errors unless the keepgoing flag was given.
                   1458:         */
                   1459:        while (!Lst_IsEmpty(&toBeMade) || jobTokensRunning > 0) {
                   1460:                Job_CatchOutput();
                   1461:                (void)MakeStartJobs();
                   1462:        }
1.1       cgd      1463:
1.229     rillig   1464:        errors = Job_Finish();
                   1465:
                   1466:        /*
                   1467:         * Print the final status of each target. E.g. if it wasn't made
                   1468:         * because some inferior reported an error.
                   1469:         */
                   1470:        DEBUG1(MAKE, "done: errors %d\n", errors);
                   1471:        if (errors == 0) {
                   1472:                MakePrintStatusList(targs, &errors);
                   1473:                if (DEBUG(MAKE)) {
                   1474:                        debug_printf("done: errors %d\n", errors);
                   1475:                        if (errors > 0)
                   1476:                                Targ_PrintGraph(4);
                   1477:                }
1.68      dsl      1478:        }
1.229     rillig   1479:        return errors > 0;
1.1       cgd      1480: }

CVSweb <webmaster@jp.NetBSD.org>