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

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.48      espie     110:        if (!to_add->must_make && !(to_add->type & OP_USE))
1.40      espie     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:  *-----------------------------------------------------------------------
                    141:  */
                    142: void
1.35      espie     143: Make_Update(GNode *cgn)        /* the child node */
1.1       deraadt   144: {
1.40      espie     145:        GNode   *pgn;   /* the parent node */
                    146:        char    *cname; /* the child's name */
1.47      espie     147:        LstNode ln;     /* Element in parents list */
1.40      espie     148:
                    149:        cname = Varq_Value(TARGET_INDEX, cgn);
                    150:
1.1       deraadt   151:        /*
1.40      espie     152:         * If the child was actually made, see what its modification time is
                    153:         * now -- some rules won't actually update the file. If the file still
                    154:         * doesn't exist, make its mtime now.
1.1       deraadt   155:         */
1.48      espie     156:        if (cgn->built_status != UPTODATE) {
1.40      espie     157:                /*
                    158:                 * This is what Make does and it's actually a good thing, as it
                    159:                 * allows rules like
                    160:                 *
                    161:                 *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    162:                 *
1.41      espie     163:                 * to function as intended. Unfortunately, thanks to the
                    164:                 * stateless nature of NFS, there are times when the
                    165:                 * modification time of a file created on a remote machine
1.40      espie     166:                 * will not be modified before the local stat() implied by
1.41      espie     167:                 * the Dir_MTime occurs, thus leading us to believe that the
                    168:                 * file is unchanged, wreaking havoc with files that depend
1.40      espie     169:                 * on this one.
                    170:                 */
1.43      espie     171:                if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.40      espie     172:                        cgn->mtime = now;
                    173:                if (DEBUG(MAKE))
                    174:                        printf("update time: %s\n", time_to_string(cgn->mtime));
                    175:        }
                    176:
                    177:        for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    178:                pgn = (GNode *)Lst_Datum(ln);
1.48      espie     179:                if (pgn->must_make) {
1.40      espie     180:                        pgn->unmade--;
                    181:
                    182:                        if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
1.48      espie     183:                                if (cgn->built_status == MADE) {
1.40      espie     184:                                        pgn->childMade = true;
1.41      espie     185:                                        if (is_strictly_before(pgn->cmtime,
1.40      espie     186:                                            cgn->mtime))
                    187:                                                pgn->cmtime = cgn->mtime;
                    188:                                } else {
                    189:                                        (void)Make_TimeStamp(pgn, cgn);
                    190:                                }
                    191:                        }
                    192:                        if (pgn->unmade == 0) {
                    193:                                /*
1.41      espie     194:                                 * Queue the node up -- any unmade
                    195:                                 * predecessors will be dealt with in
1.40      espie     196:                                 * MakeStartJobs.
                    197:                                 */
                    198:                                Lst_EnQueue(&toBeMade, pgn);
                    199:                        } else if (pgn->unmade < 0) {
                    200:                                Error("Graph cycles through %s", pgn->name);
                    201:                        }
                    202:                }
1.1       deraadt   203:        }
1.41      espie     204:        /* Deal with successor nodes. If any is marked for making and has an
                    205:         * unmade count of 0, has not been made and isn't in the examination
                    206:         * queue, it means we need to place it in the queue as it restrained
1.40      espie     207:         * itself before.       */
                    208:        for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Adv(ln)) {
                    209:                GNode   *succ = (GNode *)Lst_Datum(ln);
                    210:
1.48      espie     211:                if (succ->must_make && succ->unmade == 0
1.49    ! espie     212:                    && succ->built_status == UNKNOWN)
1.40      espie     213:                        (void)Lst_QueueNew(&toBeMade, succ);
1.1       deraadt   214:        }
                    215: }
1.25      espie     216:
1.46      espie     217: static bool
                    218: try_to_make_node(GNode *gn)
                    219: {
                    220:        if (DEBUG(MAKE))
                    221:                printf("Examining %s...", gn->name);
                    222:        /*
                    223:         * Make sure any and all predecessors that are going to be made,
                    224:         * have been.
                    225:         */
                    226:        if (!Lst_IsEmpty(&gn->preds)) {
                    227:                LstNode ln;
                    228:
                    229:                for (ln = Lst_First(&gn->preds); ln != NULL; ln = Lst_Adv(ln)){
                    230:                        GNode   *pgn = (GNode *)Lst_Datum(ln);
                    231:
1.49    ! espie     232:                        if (pgn->must_make && pgn->built_status == UNKNOWN) {
1.46      espie     233:                                if (DEBUG(MAKE))
                    234:                                        printf(
                    235:                                            "predecessor %s not made yet.\n",
                    236:                                            pgn->name);
                    237:                                break;
                    238:                        }
                    239:                }
                    240:                /*
                    241:                 * If ln isn't NULL, there's a predecessor as yet
                    242:                 * unmade, so we just drop this node on the floor. When
                    243:                 * the node in question has been made, it will notice
                    244:                 * this node as being ready to make but as yet unmade
                    245:                 * and will place the node on the queue.
                    246:                 */
                    247:                if (ln != NULL)
                    248:                        return false;
                    249:        }
                    250:
                    251:        numNodes--;
                    252:        if (Make_OODate(gn)) {
                    253:                if (DEBUG(MAKE))
                    254:                        printf("out-of-date\n");
                    255:                if (queryFlag)
                    256:                        return true;
                    257:                Make_DoAllVar(gn);
                    258:                Job_Make(gn);
                    259:        } else {
                    260:                if (DEBUG(MAKE))
                    261:                        printf("up-to-date\n");
1.48      espie     262:                gn->built_status = UPTODATE;
1.46      espie     263:                if (gn->type & OP_JOIN) {
                    264:                        /*
                    265:                         * Even for an up-to-date .JOIN node, we need it
                    266:                         * to have its context variables so references
                    267:                         * to it get the correct value for .TARGET when
                    268:                         * building up the context variables of its
                    269:                         * parent(s)...
                    270:                         */
                    271:                        Make_DoAllVar(gn);
                    272:                }
                    273:
                    274:                Make_Update(gn);
                    275:        }
                    276:        return false;
                    277: }
                    278:
1.40      espie     279: /*
1.1       deraadt   280:  *-----------------------------------------------------------------------
                    281:  * MakeStartJobs --
                    282:  *     Start as many jobs as possible.
                    283:  *
                    284:  * Results:
                    285:  *     If the query flag was given to pmake, no job will be started,
                    286:  *     but as soon as an out-of-date target is found, this function
1.26      espie     287:  *     returns true. At all other times, this function returns false.
1.1       deraadt   288:  *
                    289:  * Side Effects:
                    290:  *     Nodes are removed from the toBeMade queue and job table slots
                    291:  *     are filled.
                    292:  *-----------------------------------------------------------------------
                    293:  */
1.26      espie     294: static bool
1.35      espie     295: MakeStartJobs(void)
1.1       deraadt   296: {
1.40      espie     297:        GNode   *gn;
1.4       millert   298:
1.40      espie     299:        while (!Job_Full() && (gn = (GNode *)Lst_DeQueue(&toBeMade)) != NULL) {
1.46      espie     300:                if (try_to_make_node(gn))
                    301:                        return true;
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;
1.48      espie     326:        if (gn->built_status == UPTODATE) {
1.40      espie     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:                         */
1.48      espie     341:                        if (gn->built_status == CYCLE) {
1.40      espie     342:                                Error("Graph cycles through `%s'", gn->name);
1.48      espie     343:                                gn->built_status = ENDCYCLE;
1.40      espie     344:                                Lst_ForEach(&gn->children, MakePrintStatus, &t);
1.49    ! espie     345:                                gn->built_status = UNKNOWN;
1.48      espie     346:                        } else if (gn->built_status != ENDCYCLE) {
                    347:                                gn->built_status = CYCLE;
1.40      espie     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.46      espie     358: /*
                    359:  * Make an initial downward pass over the graph, marking nodes to be
                    360:  * made as we go down. We call Suff_FindDeps to find where a node is and
                    361:  * to get some children for it if it has none and also has no commands.
                    362:  * If the node is a leaf, we stick it on the toBeMade queue to
                    363:  * be looked at in a minute, otherwise we add its children to our queue
                    364:  * and go on about our business.
                    365:  */
                    366: static void
                    367: add_targets_to_make(Lst targs)
                    368: {
                    369:        LIST examine;   /* List of targets to examine */
                    370:        GNode *gn;
                    371:
                    372:        Lst_Clone(&examine, targs, NOCOPY);
                    373:        while ((gn = (GNode *)Lst_DeQueue(&examine)) != NULL) {
1.48      espie     374:                if (!gn->must_make) {
                    375:                        gn->must_make = true;
1.46      espie     376:                        numNodes++;
                    377:
                    378:                        look_harder_for_target(gn);
                    379:                        /*
                    380:                         * Apply any .USE rules before looking for implicit
                    381:                         * dependencies to make sure everything that should have
                    382:                         * commands has commands ...
                    383:                         */
                    384:                        Lst_ForEach(&gn->children, MakeHandleUse, gn);
                    385:                        Suff_FindDeps(gn);
                    386:
                    387:                        if (gn->unmade != 0)
                    388:                                Lst_ForEach(&gn->children, MakeAddChild,
                    389:                                    &examine);
                    390:                        else
                    391:                                Lst_EnQueue(&toBeMade, gn);
                    392:                }
                    393:        }
                    394: }
                    395:
1.1       deraadt   396: /*-
                    397:  *-----------------------------------------------------------------------
1.6       millert   398:  * Make_Run --
                    399:  *     Initialize the nodes to remake and the list of nodes which are
                    400:  *     ready to be made by doing a breadth-first traversal of the graph
                    401:  *     starting from the nodes in the given list. Once this traversal
                    402:  *     is finished, all the 'leaves' of the graph are in the toBeMade
                    403:  *     queue.
                    404:  *     Using this queue and the Job module, work back up the graph,
                    405:  *     calling on MakeStartJobs to keep the job table as full as
                    406:  *     possible.
                    407:  *
1.1       deraadt   408:  * Results:
1.26      espie     409:  *     true if work was done. false otherwise.
1.1       deraadt   410:  *
                    411:  * Side Effects:
1.48      espie     412:  *     The must_make field of all nodes involved in the creation of the given
1.6       millert   413:  *     targets is set to 1. The toBeMade list is set to contain all the
                    414:  *     'leaves' of these subgraphs.
1.1       deraadt   415:  *-----------------------------------------------------------------------
                    416:  */
1.26      espie     417: bool
1.35      espie     418: Make_Run(Lst targs)            /* the initial list of targets */
1.1       deraadt   419: {
1.40      espie     420:        int         errors;     /* Number of errors the Job module reports */
                    421:
                    422:        Static_Lst_Init(&toBeMade);
                    423:
                    424:        numNodes = 0;
                    425:
1.46      espie     426:        add_targets_to_make(targs);
1.40      espie     427:        if (queryFlag) {
                    428:                /*
1.41      espie     429:                 * We wouldn't do any work unless we could start some jobs in
                    430:                 * the next loop... (we won't actually start any, of course,
1.40      espie     431:                 * this is just to see if any of the targets was out of date)
                    432:                 */
                    433:                return MakeStartJobs();
                    434:        } else {
                    435:                /*
                    436:                 * Initialization. At the moment, no jobs are running and until
                    437:                 * some get started, nothing will happen since the remaining
1.41      espie     438:                 * upward traversal of the graph is performed by the routines
                    439:                 * in job.c upon the finishing of a job. So we fill the Job
1.40      espie     440:                 * table as much as we can before going into our loop.
                    441:                 */
                    442:                (void)MakeStartJobs();
1.1       deraadt   443:        }
1.4       millert   444:
1.1       deraadt   445:        /*
1.40      espie     446:         * Main Loop: The idea here is that the ending of jobs will take
                    447:         * care of the maintenance of data structures and the waiting for output
                    448:         * will cause us to be idle most of the time while our children run as
                    449:         * much as possible. Because the job table is kept as full as possible,
                    450:         * the only time when it will be empty is when all the jobs which need
                    451:         * running have been run, so that is the end condition of this loop.
1.41      espie     452:         * Note that the Job module will exit if there were any errors unless
1.40      espie     453:         * the keepgoing flag was given.
1.1       deraadt   454:         */
1.40      espie     455:        while (!Job_Empty()) {
                    456:                Job_CatchOutput();
1.42      espie     457:                Job_CatchChildren();
1.40      espie     458:                (void)MakeStartJobs();
                    459:        }
                    460:
                    461:        errors = Job_Finish();
                    462:
1.1       deraadt   463:        /*
1.40      espie     464:         * Print the final status of each target. E.g. if it wasn't made
                    465:         * because some inferior reported an error.
1.1       deraadt   466:         */
1.40      espie     467:        errors = errors == 0 && numNodes != 0;
                    468:        Lst_ForEach(targs, MakePrintStatus, &errors);
1.4       millert   469:
1.40      espie     470:        return true;
1.1       deraadt   471: }