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

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

1.25      espie       1: /*     $OpenPackages$ */
1.43      espie       2: /*     $OpenBSD$       */
1.6       millert     3: /*     $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $       */
1.1       deraadt     4:
                      5: /*
1.4       millert     6:  * Copyright (c) 1988, 1989, 1990, 1993
                      7:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     8:  * Copyright (c) 1989 by Berkeley Softworks
                      9:  * All rights reserved.
                     10:  *
                     11:  * This code is derived from software contributed to Berkeley by
                     12:  * Adam de Boor.
                     13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
1.33      millert    22:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*-
                     40:  * make.c --
                     41:  *     The functions which perform the examination of targets and
                     42:  *     their suitability for creation
                     43:  *
                     44:  * Interface:
1.25      espie      45:  *     Make_Run                Initialize things for the module and recreate
1.26      espie      46:  *                             whatever needs recreating. Returns true if
                     47:  *                             work was (or would have been) done and
                     48:  *                             false
1.25      espie      49:  *                             otherwise.
                     50:  *
                     51:  *     Make_Update             Update all parents of a given child. Performs
                     52:  *                             various bookkeeping chores like the updating
                     53:  *                             of the cmtime field of the parent, filling
                     54:  *                             of the IMPSRC context variable, etc. It will
                     55:  *                             place the parent on the toBeMade queue if it
                     56:  *                             should be.
                     57:  *
1.1       deraadt    58:  */
                     59:
1.27      espie      60: #include <limits.h>
1.26      espie      61: #include <stdio.h>
1.43      espie      62: #include <signal.h>
1.26      espie      63: #include "config.h"
                     64: #include "defines.h"
                     65: #include "dir.h"
                     66: #include "job.h"
                     67: #include "suff.h"
                     68: #include "var.h"
                     69: #include "error.h"
                     70: #include "make.h"
                     71: #include "gnode.h"
                     72: #include "extern.h"
                     73: #include "timestamp.h"
1.37      espie      74: #include "engine.h"
1.26      espie      75: #include "lst.h"
1.43      espie      76: #include "targ.h"
1.25      espie      77:
                     78: static LIST    toBeMade;       /* The current fringe of the graph. These
1.1       deraadt    79:                                 * are nodes which await examination by
                     80:                                 * MakeOODate. It is added to by
                     81:                                 * Make_Update and subtracted from by
                     82:                                 * MakeStartJobs */
1.25      espie      83: static int     numNodes;       /* Number of nodes to be processed. If this
1.1       deraadt    84:                                 * is non-zero when Job_Empty() returns
1.26      espie      85:                                 * true, there's a cycle in the graph */
1.1       deraadt    86:
1.25      espie      87: static void MakeAddChild(void *, void *);
                     88: static void MakeHandleUse(void *, void *);
1.26      espie      89: static bool MakeStartJobs(void);
1.25      espie      90: static void MakePrintStatus(void *, void *);
1.40      espie      91:
1.1       deraadt    92: /*-
                     93:  *-----------------------------------------------------------------------
                     94:  * MakeAddChild  --
                     95:  *     Function used by Make_Run to add a child to the list l.
1.26      espie      96:  *     It will only add the child if its make field is false.
1.1       deraadt    97:  *
                     98:  * Side Effects:
                     99:  *     The given list is extended
                    100:  *-----------------------------------------------------------------------
                    101:  */
1.15      espie     102: static void
1.40      espie     103: MakeAddChild(void *to_addp, void *lp)
1.1       deraadt   104: {
1.40      espie     105:        GNode      *to_add = (GNode *)to_addp;
                    106:        Lst        l = (Lst)lp;
1.5       millert   107:
1.40      espie     108:        if (!to_add->make && !(to_add->type & OP_USE))
                    109:                Lst_EnQueue(l, to_add);
1.1       deraadt   110: }
1.25      espie     111:
1.15      espie     112: static void
1.40      espie     113: MakeHandleUse(void *pgn, void *cgn)
1.1       deraadt   114: {
1.15      espie     115:     Make_HandleUse((GNode *)pgn, (GNode *)cgn);
1.1       deraadt   116: }
1.25      espie     117:
1.1       deraadt   118: /*-
                    119:  *-----------------------------------------------------------------------
1.25      espie     120:  * Make_Update --
1.1       deraadt   121:  *     Perform update on the parents of a node. Used by JobFinish once
                    122:  *     a node has been dealt with and by MakeStartJobs if it finds an
1.4       millert   123:  *     up-to-date node.
1.1       deraadt   124:  *
                    125:  * Results:
                    126:  *     Always returns 0
                    127:  *
                    128:  * Side Effects:
                    129:  *     The unmade field of pgn is decremented and pgn may be placed on
                    130:  *     the toBeMade queue if this field becomes 0.
                    131:  *
1.25      espie     132:  *     If the child was made, the parent's childMade field will be set true
1.1       deraadt   133:  *     and its cmtime set to now.
                    134:  *
                    135:  *     If the child wasn't made, the cmtime field of the parent will be
                    136:  *     altered if the child's mtime is big enough.
                    137:  *
                    138:  *     Finally, if the child is the implied source for the parent, the
                    139:  *     parent's IMPSRC variable is set appropriately.
                    140:  *
                    141:  *-----------------------------------------------------------------------
                    142:  */
                    143: void
1.35      espie     144: Make_Update(GNode *cgn)        /* the child node */
1.1       deraadt   145: {
1.40      espie     146:        GNode   *pgn;   /* the parent node */
                    147:        char    *cname; /* the child's name */
                    148:        LstNode ln;     /* Element in parents and iParents lists */
                    149:
                    150:        cname = Varq_Value(TARGET_INDEX, cgn);
                    151:
1.1       deraadt   152:        /*
1.40      espie     153:         * If the child was actually made, see what its modification time is
                    154:         * now -- some rules won't actually update the file. If the file still
                    155:         * doesn't exist, make its mtime now.
1.1       deraadt   156:         */
1.40      espie     157:        if (cgn->made != UPTODATE) {
                    158:                /*
                    159:                 * This is what Make does and it's actually a good thing, as it
                    160:                 * allows rules like
                    161:                 *
                    162:                 *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    163:                 *
1.41      espie     164:                 * to function as intended. Unfortunately, thanks to the
                    165:                 * stateless nature of NFS, there are times when the
                    166:                 * modification time of a file created on a remote machine
1.40      espie     167:                 * will not be modified before the local stat() implied by
1.41      espie     168:                 * the Dir_MTime occurs, thus leading us to believe that the
                    169:                 * file is unchanged, wreaking havoc with files that depend
1.40      espie     170:                 * on this one.
                    171:                 */
1.43      espie     172:                if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.40      espie     173:                        cgn->mtime = now;
                    174:                if (DEBUG(MAKE))
                    175:                        printf("update time: %s\n", time_to_string(cgn->mtime));
                    176:        }
                    177:
                    178:        for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    179:                pgn = (GNode *)Lst_Datum(ln);
                    180:                if (pgn->make) {
                    181:                        pgn->unmade--;
                    182:
                    183:                        if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
                    184:                                if (cgn->made == MADE) {
                    185:                                        pgn->childMade = true;
1.41      espie     186:                                        if (is_strictly_before(pgn->cmtime,
1.40      espie     187:                                            cgn->mtime))
                    188:                                                pgn->cmtime = cgn->mtime;
                    189:                                } else {
                    190:                                        (void)Make_TimeStamp(pgn, cgn);
                    191:                                }
                    192:                        }
                    193:                        if (pgn->unmade == 0) {
                    194:                                /*
1.41      espie     195:                                 * Queue the node up -- any unmade
                    196:                                 * predecessors will be dealt with in
1.40      espie     197:                                 * MakeStartJobs.
                    198:                                 */
                    199:                                Lst_EnQueue(&toBeMade, pgn);
                    200:                        } else if (pgn->unmade < 0) {
                    201:                                Error("Graph cycles through %s", pgn->name);
                    202:                        }
                    203:                }
1.1       deraadt   204:        }
1.41      espie     205:        /* Deal with successor nodes. If any is marked for making and has an
                    206:         * unmade count of 0, has not been made and isn't in the examination
                    207:         * queue, it means we need to place it in the queue as it restrained
1.40      espie     208:         * itself before.       */
                    209:        for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
                    210:                GNode   *succ = (GNode *)Lst_Datum(ln);
                    211:
                    212:                if (succ->make && succ->unmade == 0 && succ->made == UNMADE)
                    213:                        (void)Lst_QueueNew(&toBeMade, succ);
1.1       deraadt   214:        }
1.4       millert   215:
1.44      espie     216:        /* Set the .IMPSRC variables for all the implied parents
1.40      espie     217:         * of this node.  */
                    218:        for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Adv(ln)) {
                    219:                pgn = (GNode *)Lst_Datum(ln);
1.44      espie     220:                if (pgn->make)
1.40      espie     221:                        Varq_Set(IMPSRC_INDEX, cname, pgn);
1.1       deraadt   222:        }
1.40      espie     223:
1.1       deraadt   224: }
1.25      espie     225:
1.40      espie     226: /*
1.1       deraadt   227:  *-----------------------------------------------------------------------
                    228:  * MakeStartJobs --
                    229:  *     Start as many jobs as possible.
                    230:  *
                    231:  * Results:
                    232:  *     If the query flag was given to pmake, no job will be started,
                    233:  *     but as soon as an out-of-date target is found, this function
1.26      espie     234:  *     returns true. At all other times, this function returns false.
1.1       deraadt   235:  *
                    236:  * Side Effects:
                    237:  *     Nodes are removed from the toBeMade queue and job table slots
                    238:  *     are filled.
                    239:  *-----------------------------------------------------------------------
                    240:  */
1.26      espie     241: static bool
1.35      espie     242: MakeStartJobs(void)
1.1       deraadt   243: {
1.40      espie     244:        GNode   *gn;
1.4       millert   245:
1.40      espie     246:        while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
                    247:                if (DEBUG(MAKE))
                    248:                        printf("Examining %s...", gn->name);
                    249:                /*
                    250:                 * Make sure any and all predecessors that are going to be made,
                    251:                 * have been.
                    252:                 */
                    253:                if (!Lst_IsEmpty(&gn->preds)) {
                    254:                        LstNode ln;
1.1       deraadt   255:
1.41      espie     256:                        for (ln = Lst_First(&gn->preds); ln != NULL;
1.40      espie     257:                            ln = Lst_Adv(ln)){
                    258:                                GNode   *pgn = (GNode *)Lst_Datum(ln);
                    259:
                    260:                                if (pgn->make && pgn->made == UNMADE) {
                    261:                                        if (DEBUG(MAKE))
                    262:                                            printf("predecessor %s not made yet.\n", pgn->name);
                    263:                                        break;
                    264:                                }
                    265:                        }
                    266:                        /*
1.41      espie     267:                         * If ln isn't NULL, there's a predecessor as yet
1.40      espie     268:                         * unmade, so we just drop this node on the floor. When
1.41      espie     269:                         * the node in question has been made, it will notice
                    270:                         * this node as being ready to make but as yet unmade
1.40      espie     271:                         * and will place the node on the queue.
                    272:                         */
                    273:                        if (ln != NULL)
                    274:                                continue;
1.1       deraadt   275:                }
1.4       millert   276:
1.40      espie     277:                numNodes--;
                    278:                if (Make_OODate(gn)) {
                    279:                        if (DEBUG(MAKE))
                    280:                                printf("out-of-date\n");
                    281:                        if (queryFlag)
                    282:                                return true;
                    283:                        Make_DoAllVar(gn);
                    284:                        Job_Make(gn);
                    285:                } else {
                    286:                        if (DEBUG(MAKE))
                    287:                                printf("up-to-date\n");
                    288:                        gn->made = UPTODATE;
                    289:                        if (gn->type & OP_JOIN) {
                    290:                                /*
1.41      espie     291:                                 * Even for an up-to-date .JOIN node, we need it
                    292:                                 * to have its context variables so references
                    293:                                 * to it get the correct value for .TARGET when
                    294:                                 * building up the context variables of its
1.40      espie     295:                                 * parent(s)...
                    296:                                 */
                    297:                                Make_DoAllVar(gn);
                    298:                        }
1.4       millert   299:
1.40      espie     300:                        Make_Update(gn);
                    301:                }
1.1       deraadt   302:        }
1.40      espie     303:        return false;
1.1       deraadt   304: }
1.25      espie     305:
1.1       deraadt   306: /*-
                    307:  *-----------------------------------------------------------------------
                    308:  * MakePrintStatus --
                    309:  *     Print the status of a top-level node, viz. it being up-to-date
                    310:  *     already or not created due to an error in a lower level.
                    311:  *     Callback function for Make_Run via Lst_ForEach.
1.25      espie     312:  *
                    313:  * Side Effects:
                    314:  *     A message may be printed.
1.1       deraadt   315:  *-----------------------------------------------------------------------
                    316:  */
1.15      espie     317: static void
1.35      espie     318: MakePrintStatus(
                    319:     void *gnp,             /* Node to examine */
                    320:     void *cyclep)          /* True if gn->unmade being non-zero implies
1.1       deraadt   321:                             * a cycle in the graph, not an error in an
                    322:                             * inferior */
                    323: {
1.40      espie     324:        GNode   *gn = (GNode *)gnp;
                    325:        bool    cycle = *(bool *)cyclep;
                    326:        if (gn->made == UPTODATE) {
                    327:                printf("`%s' is up to date.\n", gn->name);
                    328:        } else if (gn->unmade != 0) {
                    329:                if (cycle) {
                    330:                        bool t = true;
                    331:                        /*
1.41      espie     332:                         * If printing cycles and came to one that has unmade
                    333:                         * children, print out the cycle by recursing on its
1.40      espie     334:                         * children. Note a cycle like:
                    335:                         *      a : b
                    336:                         *      b : c
                    337:                         *      c : b
1.41      espie     338:                         * will cause this to erroneously complain about a
1.40      espie     339:                         * being in the cycle, but this is a good approximation.
                    340:                         */
                    341:                        if (gn->made == CYCLE) {
                    342:                                Error("Graph cycles through `%s'", gn->name);
                    343:                                gn->made = ENDCYCLE;
                    344:                                Lst_ForEach(&gn->children, MakePrintStatus, &t);
                    345:                                gn->made = UNMADE;
                    346:                        } else if (gn->made != ENDCYCLE) {
                    347:                                gn->made = CYCLE;
                    348:                                Lst_ForEach(&gn->children, MakePrintStatus, &t);
                    349:                        }
                    350:                } else {
1.41      espie     351:                        printf("`%s' not remade because of errors.\n",
1.40      espie     352:                            gn->name);
                    353:                }
1.1       deraadt   354:        }
                    355: }
1.25      espie     356:
1.5       millert   357:
1.1       deraadt   358: /*-
                    359:  *-----------------------------------------------------------------------
1.6       millert   360:  * Make_Run --
                    361:  *     Initialize the nodes to remake and the list of nodes which are
                    362:  *     ready to be made by doing a breadth-first traversal of the graph
                    363:  *     starting from the nodes in the given list. Once this traversal
                    364:  *     is finished, all the 'leaves' of the graph are in the toBeMade
                    365:  *     queue.
                    366:  *     Using this queue and the Job module, work back up the graph,
                    367:  *     calling on MakeStartJobs to keep the job table as full as
                    368:  *     possible.
                    369:  *
1.1       deraadt   370:  * Results:
1.26      espie     371:  *     true if work was done. false otherwise.
1.1       deraadt   372:  *
                    373:  * Side Effects:
1.6       millert   374:  *     The make field of all nodes involved in the creation of the given
                    375:  *     targets is set to 1. The toBeMade list is set to contain all the
                    376:  *     'leaves' of these subgraphs.
1.1       deraadt   377:  *-----------------------------------------------------------------------
                    378:  */
1.26      espie     379: bool
1.35      espie     380: Make_Run(Lst targs)            /* the initial list of targets */
1.1       deraadt   381: {
1.40      espie     382:        GNode       *gn;        /* a temporary pointer */
                    383:        LIST        examine;    /* List of targets to examine */
                    384:        int         errors;     /* Number of errors the Job module reports */
                    385:
                    386:        Static_Lst_Init(&toBeMade);
                    387:
                    388:        Lst_Clone(&examine, targs, NOCOPY);
                    389:        numNodes = 0;
                    390:
                    391:        /*
1.41      espie     392:         * Make an initial downward pass over the graph, marking nodes to be
1.40      espie     393:         * made as we go down. We call Suff_FindDeps to find where a node is and
                    394:         * to get some children for it if it has none and also has no commands.
                    395:         * If the node is a leaf, we stick it on the toBeMade queue to
                    396:         * be looked at in a minute, otherwise we add its children to our queue
                    397:         * and go on about our business.
                    398:         */
                    399:        while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
                    400:                if (!gn->make) {
                    401:                        gn->make = true;
                    402:                        numNodes++;
                    403:
1.43      espie     404:                        look_harder_for_target(gn);
1.40      espie     405:                        /*
1.41      espie     406:                         * Apply any .USE rules before looking for implicit
1.40      espie     407:                         * dependencies to make sure everything that should have
                    408:                         * commands has commands ...
                    409:                         */
                    410:                        Lst_ForEach(&gn->children, MakeHandleUse, gn);
                    411:                        Suff_FindDeps(gn);
                    412:
                    413:                        if (gn->unmade != 0)
1.41      espie     414:                                Lst_ForEach(&gn->children, MakeAddChild,
1.40      espie     415:                                    &examine);
                    416:                        else
                    417:                                Lst_EnQueue(&toBeMade, gn);
                    418:                }
                    419:        }
                    420:
                    421:        if (queryFlag) {
                    422:                /*
1.41      espie     423:                 * We wouldn't do any work unless we could start some jobs in
                    424:                 * the next loop... (we won't actually start any, of course,
1.40      espie     425:                 * this is just to see if any of the targets was out of date)
                    426:                 */
                    427:                return MakeStartJobs();
                    428:        } else {
                    429:                /*
                    430:                 * Initialization. At the moment, no jobs are running and until
                    431:                 * some get started, nothing will happen since the remaining
1.41      espie     432:                 * upward traversal of the graph is performed by the routines
                    433:                 * in job.c upon the finishing of a job. So we fill the Job
1.40      espie     434:                 * table as much as we can before going into our loop.
                    435:                 */
                    436:                (void)MakeStartJobs();
1.1       deraadt   437:        }
1.4       millert   438:
1.1       deraadt   439:        /*
1.40      espie     440:         * Main Loop: The idea here is that the ending of jobs will take
                    441:         * care of the maintenance of data structures and the waiting for output
                    442:         * will cause us to be idle most of the time while our children run as
                    443:         * much as possible. Because the job table is kept as full as possible,
                    444:         * the only time when it will be empty is when all the jobs which need
                    445:         * running have been run, so that is the end condition of this loop.
1.41      espie     446:         * Note that the Job module will exit if there were any errors unless
1.40      espie     447:         * the keepgoing flag was given.
1.1       deraadt   448:         */
1.40      espie     449:        while (!Job_Empty()) {
                    450:                Job_CatchOutput();
1.42      espie     451:                Job_CatchChildren();
1.40      espie     452:                (void)MakeStartJobs();
                    453:        }
                    454:
                    455:        errors = Job_Finish();
                    456:
1.1       deraadt   457:        /*
1.40      espie     458:         * Print the final status of each target. E.g. if it wasn't made
                    459:         * because some inferior reported an error.
1.1       deraadt   460:         */
1.40      espie     461:        errors = errors == 0 && numNodes != 0;
                    462:        Lst_ForEach(targs, MakePrintStatus, &errors);
1.4       millert   463:
1.40      espie     464:        return true;
1.1       deraadt   465: }