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

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.46    ! espie      91: static bool try_to_make_node(GNode *);
        !            92: static void add_targets_to_make(Lst);
1.40      espie      93:
1.1       deraadt    94: /*-
                     95:  *-----------------------------------------------------------------------
                     96:  * MakeAddChild  --
                     97:  *     Function used by Make_Run to add a child to the list l.
1.26      espie      98:  *     It will only add the child if its make field is false.
1.1       deraadt    99:  *
                    100:  * Side Effects:
                    101:  *     The given list is extended
                    102:  *-----------------------------------------------------------------------
                    103:  */
1.15      espie     104: static void
1.40      espie     105: MakeAddChild(void *to_addp, void *lp)
1.1       deraadt   106: {
1.40      espie     107:        GNode      *to_add = (GNode *)to_addp;
                    108:        Lst        l = (Lst)lp;
1.5       millert   109:
1.40      espie     110:        if (!to_add->make && !(to_add->type & OP_USE))
                    111:                Lst_EnQueue(l, to_add);
1.1       deraadt   112: }
1.25      espie     113:
1.15      espie     114: static void
1.40      espie     115: MakeHandleUse(void *pgn, void *cgn)
1.1       deraadt   116: {
1.46    ! espie     117:        Make_HandleUse((GNode *)pgn, (GNode *)cgn);
1.1       deraadt   118: }
1.25      espie     119:
1.1       deraadt   120: /*-
                    121:  *-----------------------------------------------------------------------
1.25      espie     122:  * Make_Update --
1.1       deraadt   123:  *     Perform update on the parents of a node. Used by JobFinish once
                    124:  *     a node has been dealt with and by MakeStartJobs if it finds an
1.4       millert   125:  *     up-to-date node.
1.1       deraadt   126:  *
                    127:  * Results:
                    128:  *     Always returns 0
                    129:  *
                    130:  * Side Effects:
                    131:  *     The unmade field of pgn is decremented and pgn may be placed on
                    132:  *     the toBeMade queue if this field becomes 0.
                    133:  *
1.25      espie     134:  *     If the child was made, the parent's childMade field will be set true
1.1       deraadt   135:  *     and its cmtime set to now.
                    136:  *
                    137:  *     If the child wasn't made, the cmtime field of the parent will be
                    138:  *     altered if the child's mtime is big enough.
                    139:  *
                    140:  *     Finally, if the child is the implied source for the parent, the
                    141:  *     parent's IMPSRC variable is set appropriately.
                    142:  *
                    143:  *-----------------------------------------------------------------------
                    144:  */
                    145: void
1.35      espie     146: Make_Update(GNode *cgn)        /* the child node */
1.1       deraadt   147: {
1.40      espie     148:        GNode   *pgn;   /* the parent node */
                    149:        char    *cname; /* the child's name */
                    150:        LstNode ln;     /* Element in parents and iParents lists */
                    151:
                    152:        cname = Varq_Value(TARGET_INDEX, cgn);
                    153:
1.1       deraadt   154:        /*
1.40      espie     155:         * If the child was actually made, see what its modification time is
                    156:         * now -- some rules won't actually update the file. If the file still
                    157:         * doesn't exist, make its mtime now.
1.1       deraadt   158:         */
1.40      espie     159:        if (cgn->made != UPTODATE) {
                    160:                /*
                    161:                 * This is what Make does and it's actually a good thing, as it
                    162:                 * allows rules like
                    163:                 *
                    164:                 *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    165:                 *
1.41      espie     166:                 * to function as intended. Unfortunately, thanks to the
                    167:                 * stateless nature of NFS, there are times when the
                    168:                 * modification time of a file created on a remote machine
1.40      espie     169:                 * will not be modified before the local stat() implied by
1.41      espie     170:                 * the Dir_MTime occurs, thus leading us to believe that the
                    171:                 * file is unchanged, wreaking havoc with files that depend
1.40      espie     172:                 * on this one.
                    173:                 */
1.43      espie     174:                if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.40      espie     175:                        cgn->mtime = now;
                    176:                if (DEBUG(MAKE))
                    177:                        printf("update time: %s\n", time_to_string(cgn->mtime));
                    178:        }
                    179:
                    180:        for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    181:                pgn = (GNode *)Lst_Datum(ln);
                    182:                if (pgn->make) {
                    183:                        pgn->unmade--;
                    184:
                    185:                        if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
                    186:                                if (cgn->made == MADE) {
                    187:                                        pgn->childMade = true;
1.41      espie     188:                                        if (is_strictly_before(pgn->cmtime,
1.40      espie     189:                                            cgn->mtime))
                    190:                                                pgn->cmtime = cgn->mtime;
                    191:                                } else {
                    192:                                        (void)Make_TimeStamp(pgn, cgn);
                    193:                                }
                    194:                        }
                    195:                        if (pgn->unmade == 0) {
                    196:                                /*
1.41      espie     197:                                 * Queue the node up -- any unmade
                    198:                                 * predecessors will be dealt with in
1.40      espie     199:                                 * MakeStartJobs.
                    200:                                 */
                    201:                                Lst_EnQueue(&toBeMade, pgn);
                    202:                        } else if (pgn->unmade < 0) {
                    203:                                Error("Graph cycles through %s", pgn->name);
                    204:                        }
                    205:                }
1.1       deraadt   206:        }
1.41      espie     207:        /* Deal with successor nodes. If any is marked for making and has an
                    208:         * unmade count of 0, has not been made and isn't in the examination
                    209:         * queue, it means we need to place it in the queue as it restrained
1.40      espie     210:         * itself before.       */
                    211:        for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
                    212:                GNode   *succ = (GNode *)Lst_Datum(ln);
                    213:
                    214:                if (succ->make && succ->unmade == 0 && succ->made == UNMADE)
                    215:                        (void)Lst_QueueNew(&toBeMade, succ);
1.1       deraadt   216:        }
1.4       millert   217:
1.44      espie     218:        /* Set the .IMPSRC variables for all the implied parents
1.40      espie     219:         * of this node.  */
                    220:        for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Adv(ln)) {
                    221:                pgn = (GNode *)Lst_Datum(ln);
1.44      espie     222:                if (pgn->make)
1.40      espie     223:                        Varq_Set(IMPSRC_INDEX, cname, pgn);
1.1       deraadt   224:        }
1.40      espie     225:
1.1       deraadt   226: }
1.25      espie     227:
1.46    ! espie     228: static bool
        !           229: try_to_make_node(GNode *gn)
        !           230: {
        !           231:        if (DEBUG(MAKE))
        !           232:                printf("Examining %s...", gn->name);
        !           233:        /*
        !           234:         * Make sure any and all predecessors that are going to be made,
        !           235:         * have been.
        !           236:         */
        !           237:        if (!Lst_IsEmpty(&gn->preds)) {
        !           238:                LstNode ln;
        !           239:
        !           240:                for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)){
        !           241:                        GNode   *pgn = (GNode *)Lst_Datum(ln);
        !           242:
        !           243:                        if (pgn->make && pgn->made == UNMADE) {
        !           244:                                if (DEBUG(MAKE))
        !           245:                                        printf(
        !           246:                                            "predecessor %s not made yet.\n",
        !           247:                                            pgn->name);
        !           248:                                break;
        !           249:                        }
        !           250:                }
        !           251:                /*
        !           252:                 * If ln isn't NULL, there's a predecessor as yet
        !           253:                 * unmade, so we just drop this node on the floor. When
        !           254:                 * the node in question has been made, it will notice
        !           255:                 * this node as being ready to make but as yet unmade
        !           256:                 * and will place the node on the queue.
        !           257:                 */
        !           258:                if (ln != NULL)
        !           259:                        return false;
        !           260:        }
        !           261:
        !           262:        numNodes--;
        !           263:        if (Make_OODate(gn)) {
        !           264:                if (DEBUG(MAKE))
        !           265:                        printf("out-of-date\n");
        !           266:                if (queryFlag)
        !           267:                        return true;
        !           268:                Make_DoAllVar(gn);
        !           269:                Job_Make(gn);
        !           270:        } else {
        !           271:                if (DEBUG(MAKE))
        !           272:                        printf("up-to-date\n");
        !           273:                gn->made = UPTODATE;
        !           274:                if (gn->type & OP_JOIN) {
        !           275:                        /*
        !           276:                         * Even for an up-to-date .JOIN node, we need it
        !           277:                         * to have its context variables so references
        !           278:                         * to it get the correct value for .TARGET when
        !           279:                         * building up the context variables of its
        !           280:                         * parent(s)...
        !           281:                         */
        !           282:                        Make_DoAllVar(gn);
        !           283:                }
        !           284:
        !           285:                Make_Update(gn);
        !           286:        }
        !           287:        return false;
        !           288: }
        !           289:
1.40      espie     290: /*
1.1       deraadt   291:  *-----------------------------------------------------------------------
                    292:  * MakeStartJobs --
                    293:  *     Start as many jobs as possible.
                    294:  *
                    295:  * Results:
                    296:  *     If the query flag was given to pmake, no job will be started,
                    297:  *     but as soon as an out-of-date target is found, this function
1.26      espie     298:  *     returns true. At all other times, this function returns false.
1.1       deraadt   299:  *
                    300:  * Side Effects:
                    301:  *     Nodes are removed from the toBeMade queue and job table slots
                    302:  *     are filled.
                    303:  *-----------------------------------------------------------------------
                    304:  */
1.26      espie     305: static bool
1.35      espie     306: MakeStartJobs(void)
1.1       deraadt   307: {
1.40      espie     308:        GNode   *gn;
1.4       millert   309:
1.40      espie     310:        while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
1.46    ! espie     311:                if (try_to_make_node(gn))
        !           312:                        return true;
1.1       deraadt   313:        }
1.40      espie     314:        return false;
1.1       deraadt   315: }
1.25      espie     316:
1.1       deraadt   317: /*-
                    318:  *-----------------------------------------------------------------------
                    319:  * MakePrintStatus --
                    320:  *     Print the status of a top-level node, viz. it being up-to-date
                    321:  *     already or not created due to an error in a lower level.
                    322:  *     Callback function for Make_Run via Lst_ForEach.
1.25      espie     323:  *
                    324:  * Side Effects:
                    325:  *     A message may be printed.
1.1       deraadt   326:  *-----------------------------------------------------------------------
                    327:  */
1.15      espie     328: static void
1.35      espie     329: MakePrintStatus(
                    330:     void *gnp,             /* Node to examine */
                    331:     void *cyclep)          /* True if gn->unmade being non-zero implies
1.1       deraadt   332:                             * a cycle in the graph, not an error in an
                    333:                             * inferior */
                    334: {
1.40      espie     335:        GNode   *gn = (GNode *)gnp;
                    336:        bool    cycle = *(bool *)cyclep;
                    337:        if (gn->made == UPTODATE) {
                    338:                printf("`%s' is up to date.\n", gn->name);
                    339:        } else if (gn->unmade != 0) {
                    340:                if (cycle) {
                    341:                        bool t = true;
                    342:                        /*
1.41      espie     343:                         * If printing cycles and came to one that has unmade
                    344:                         * children, print out the cycle by recursing on its
1.40      espie     345:                         * children. Note a cycle like:
                    346:                         *      a : b
                    347:                         *      b : c
                    348:                         *      c : b
1.41      espie     349:                         * will cause this to erroneously complain about a
1.40      espie     350:                         * being in the cycle, but this is a good approximation.
                    351:                         */
                    352:                        if (gn->made == CYCLE) {
                    353:                                Error("Graph cycles through `%s'", gn->name);
                    354:                                gn->made = ENDCYCLE;
                    355:                                Lst_ForEach(&gn->children, MakePrintStatus, &t);
                    356:                                gn->made = UNMADE;
                    357:                        } else if (gn->made != ENDCYCLE) {
                    358:                                gn->made = CYCLE;
                    359:                                Lst_ForEach(&gn->children, MakePrintStatus, &t);
                    360:                        }
                    361:                } else {
1.41      espie     362:                        printf("`%s' not remade because of errors.\n",
1.40      espie     363:                            gn->name);
                    364:                }
1.1       deraadt   365:        }
                    366: }
1.25      espie     367:
1.5       millert   368:
1.46    ! espie     369: /*
        !           370:  * Make an initial downward pass over the graph, marking nodes to be
        !           371:  * made as we go down. We call Suff_FindDeps to find where a node is and
        !           372:  * to get some children for it if it has none and also has no commands.
        !           373:  * If the node is a leaf, we stick it on the toBeMade queue to
        !           374:  * be looked at in a minute, otherwise we add its children to our queue
        !           375:  * and go on about our business.
        !           376:  */
        !           377: static void
        !           378: add_targets_to_make(Lst targs)
        !           379: {
        !           380:        LIST examine;   /* List of targets to examine */
        !           381:        GNode *gn;
        !           382:
        !           383:        Lst_Clone(&examine, targs, NOCOPY);
        !           384:        while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
        !           385:                if (!gn->make) {
        !           386:                        gn->make = true;
        !           387:                        numNodes++;
        !           388:
        !           389:                        look_harder_for_target(gn);
        !           390:                        /*
        !           391:                         * Apply any .USE rules before looking for implicit
        !           392:                         * dependencies to make sure everything that should have
        !           393:                         * commands has commands ...
        !           394:                         */
        !           395:                        Lst_ForEach(&gn->children, MakeHandleUse, gn);
        !           396:                        Suff_FindDeps(gn);
        !           397:
        !           398:                        if (gn->unmade != 0)
        !           399:                                Lst_ForEach(&gn->children, MakeAddChild,
        !           400:                                    &examine);
        !           401:                        else
        !           402:                                Lst_EnQueue(&toBeMade, gn);
        !           403:                }
        !           404:        }
        !           405: }
        !           406:
1.1       deraadt   407: /*-
                    408:  *-----------------------------------------------------------------------
1.6       millert   409:  * Make_Run --
                    410:  *     Initialize the nodes to remake and the list of nodes which are
                    411:  *     ready to be made by doing a breadth-first traversal of the graph
                    412:  *     starting from the nodes in the given list. Once this traversal
                    413:  *     is finished, all the 'leaves' of the graph are in the toBeMade
                    414:  *     queue.
                    415:  *     Using this queue and the Job module, work back up the graph,
                    416:  *     calling on MakeStartJobs to keep the job table as full as
                    417:  *     possible.
                    418:  *
1.1       deraadt   419:  * Results:
1.26      espie     420:  *     true if work was done. false otherwise.
1.1       deraadt   421:  *
                    422:  * Side Effects:
1.6       millert   423:  *     The make field of all nodes involved in the creation of the given
                    424:  *     targets is set to 1. The toBeMade list is set to contain all the
                    425:  *     'leaves' of these subgraphs.
1.1       deraadt   426:  *-----------------------------------------------------------------------
                    427:  */
1.26      espie     428: bool
1.35      espie     429: Make_Run(Lst targs)            /* the initial list of targets */
1.1       deraadt   430: {
1.40      espie     431:        int         errors;     /* Number of errors the Job module reports */
                    432:
                    433:        Static_Lst_Init(&toBeMade);
                    434:
                    435:        numNodes = 0;
                    436:
1.46    ! espie     437:        add_targets_to_make(targs);
1.40      espie     438:        if (queryFlag) {
                    439:                /*
1.41      espie     440:                 * We wouldn't do any work unless we could start some jobs in
                    441:                 * the next loop... (we won't actually start any, of course,
1.40      espie     442:                 * this is just to see if any of the targets was out of date)
                    443:                 */
                    444:                return MakeStartJobs();
                    445:        } else {
                    446:                /*
                    447:                 * Initialization. At the moment, no jobs are running and until
                    448:                 * some get started, nothing will happen since the remaining
1.41      espie     449:                 * upward traversal of the graph is performed by the routines
                    450:                 * in job.c upon the finishing of a job. So we fill the Job
1.40      espie     451:                 * table as much as we can before going into our loop.
                    452:                 */
                    453:                (void)MakeStartJobs();
1.1       deraadt   454:        }
1.4       millert   455:
1.1       deraadt   456:        /*
1.40      espie     457:         * Main Loop: The idea here is that the ending of jobs will take
                    458:         * care of the maintenance of data structures and the waiting for output
                    459:         * will cause us to be idle most of the time while our children run as
                    460:         * much as possible. Because the job table is kept as full as possible,
                    461:         * the only time when it will be empty is when all the jobs which need
                    462:         * running have been run, so that is the end condition of this loop.
1.41      espie     463:         * Note that the Job module will exit if there were any errors unless
1.40      espie     464:         * the keepgoing flag was given.
1.1       deraadt   465:         */
1.40      espie     466:        while (!Job_Empty()) {
                    467:                Job_CatchOutput();
1.42      espie     468:                Job_CatchChildren();
1.40      espie     469:                (void)MakeStartJobs();
                    470:        }
                    471:
                    472:        errors = Job_Finish();
                    473:
1.1       deraadt   474:        /*
1.40      espie     475:         * Print the final status of each target. E.g. if it wasn't made
                    476:         * because some inferior reported an error.
1.1       deraadt   477:         */
1.40      espie     478:        errors = errors == 0 && numNodes != 0;
                    479:        Lst_ForEach(targs, MakePrintStatus, &errors);
1.4       millert   480:
1.40      espie     481:        return true;
1.1       deraadt   482: }