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

1.80    ! espie       1: /*     $OpenBSD: make.c,v 1.79 2020/01/13 15:15:17 espie Exp $ */
1.6       millert     2: /*     $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $       */
1.1       deraadt     3:
                      4: /*
1.4       millert     5:  * Copyright (c) 1988, 1989, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  * Copyright (c) 1989 by Berkeley Softworks
                      8:  * All rights reserved.
                      9:  *
                     10:  * This code is derived from software contributed to Berkeley by
                     11:  * Adam de Boor.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
1.33      millert    21:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    22:  *    may be used to endorse or promote products derived from this software
                     23:  *    without specific prior written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     26:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     27:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     28:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     29:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     30:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     31:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     32:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     33:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     34:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     35:  * SUCH DAMAGE.
                     36:  */
                     37:
                     38: /*-
                     39:  * make.c --
                     40:  *     The functions which perform the examination of targets and
                     41:  *     their suitability for creation
                     42:  *
                     43:  * Interface:
1.25      espie      44:  *     Make_Run                Initialize things for the module and recreate
1.26      espie      45:  *                             whatever needs recreating. Returns true if
                     46:  *                             work was (or would have been) done and
                     47:  *                             false
1.25      espie      48:  *                             otherwise.
                     49:  *
                     50:  *     Make_Update             Update all parents of a given child. Performs
1.67      espie      51:  *                             various bookkeeping chores like finding the
                     52:  *                             youngest child of the parent, filling
1.76      espie      53:  *                             the IMPSRC local variable, etc. It will
1.75      espie      54:  *                             place the parent on the to_build queue if it
1.25      espie      55:  *                             should be.
                     56:  *
1.1       deraadt    57:  */
                     58:
1.27      espie      59: #include <limits.h>
1.43      espie      60: #include <signal.h>
1.52      espie      61: #include <stddef.h>
1.66      espie      62: #include <stdint.h>
                     63: #include <stdio.h>
1.54      espie      64: #include <stdlib.h>
1.52      espie      65: #include <string.h>
                     66: #include <ohash.h>
1.26      espie      67: #include "config.h"
                     68: #include "defines.h"
                     69: #include "dir.h"
                     70: #include "job.h"
                     71: #include "suff.h"
                     72: #include "var.h"
                     73: #include "error.h"
1.78      espie      74: #include "expandchildren.h"
1.26      espie      75: #include "make.h"
                     76: #include "gnode.h"
                     77: #include "extern.h"
                     78: #include "timestamp.h"
1.37      espie      79: #include "engine.h"
1.26      espie      80: #include "lst.h"
1.43      espie      81: #include "targ.h"
1.58      espie      82: #include "targequiv.h"
1.56      espie      83: #include "garray.h"
                     84: #include "memory.h"
                     85:
                     86: /* what gets added each time. Kept as one static array so that it doesn't
                     87:  * get resized every time.
                     88:  */
                     89: static struct growableArray examine;
                     90: /* The current fringe of the graph. These are nodes which await examination by
                     91:  * MakeOODate. It is added to by Make_Update and subtracted from by
                     92:  * MakeStartJobs */
1.75      espie      93: static struct growableArray to_build;
1.25      espie      94:
1.64      espie      95: /* Hold back on nodes where equivalent stuff is already building... */
                     96: static struct growableArray heldBack;
                     97:
1.52      espie      98: static struct ohash targets;   /* stuff we must build */
1.1       deraadt    99:
1.25      espie     100: static void MakeAddChild(void *, void *);
                    101: static void MakeHandleUse(void *, void *);
1.26      espie     102: static bool MakeStartJobs(void);
1.72      espie     103: static void MakePrintStatus(void *);
                    104:
                    105: /* Cycle detection functions */
                    106: static bool targets_contain_cycles(void);
                    107: static void print_unlink_cycle(struct growableArray *, GNode *);
                    108: static void break_and_print_cycles(Lst);
                    109: static GNode *find_cycle(Lst, struct growableArray *);
                    110:
1.46      espie     111: static bool try_to_make_node(GNode *);
                    112: static void add_targets_to_make(Lst);
1.40      espie     113:
1.75      espie     114: static bool has_predecessor_left_to_build(GNode *);
1.52      espie     115: static void requeue_successors(GNode *);
1.54      espie     116: static void random_setup(void);
                    117:
                    118: static bool randomize_queue;
                    119: long random_delay = 0;
                    120:
1.73      espie     121: bool
1.79      espie     122: nothing_left_to_build()
1.56      espie     123: {
1.75      espie     124:        return Array_IsEmpty(&to_build);
1.56      espie     125: }
                    126:
1.54      espie     127: static void
                    128: random_setup()
                    129: {
                    130:        randomize_queue = Var_Definedi("RANDOM_ORDER", NULL);
                    131:
1.67      espie     132: /* no random delay in the new engine for now */
                    133: #if 0
1.54      espie     134:        if (Var_Definedi("RANDOM_DELAY", NULL))
1.73      espie     135:                random_delay = strtonum(Var_Value("RANDOM_DELAY"), 0, 1000,
1.54      espie     136:                    NULL) * 1000000;
1.67      espie     137: #endif
1.54      espie     138:
                    139: }
1.52      espie     140:
1.56      espie     141: static void
                    142: randomize_garray(struct growableArray *g)
                    143: {
                    144:        /* This is a fairly standard algorithm to randomize an array. */
                    145:        unsigned int i, v;
                    146:        GNode *e;
                    147:
                    148:        for (i = g->n; i > 0; i--) {
1.67      espie     149:                v = arc4random_uniform(i);
1.56      espie     150:                if (v == i-1)
                    151:                        continue;
                    152:                else {
                    153:                        e = g->a[i-1];
                    154:                        g->a[i-1] = g->a[v];
                    155:                        g->a[v] = e;
                    156:                }
                    157:        }
                    158: }
                    159:
1.52      espie     160: static bool
1.75      espie     161: has_predecessor_left_to_build(GNode *gn)
1.1       deraadt   162: {
1.52      espie     163:        LstNode ln;
                    164:
1.75      espie     165:        if (Lst_IsEmpty(&gn->predecessors))
1.52      espie     166:                return false;
1.5       millert   167:
1.52      espie     168:
1.75      espie     169:        for (ln = Lst_First(&gn->predecessors); ln != NULL; ln = Lst_Adv(ln)) {
1.71      espie     170:                GNode   *pgn = Lst_Datum(ln);
1.52      espie     171:
                    172:                if (pgn->must_make && pgn->built_status == UNKNOWN) {
                    173:                        if (DEBUG(MAKE))
1.73      espie     174:                                printf("predecessor %s not made yet.\n",
1.52      espie     175:                                    pgn->name);
                    176:                        return true;
                    177:                }
                    178:        }
                    179:        return false;
1.1       deraadt   180: }
1.25      espie     181:
1.15      espie     182: static void
1.52      espie     183: requeue_successors(GNode *gn)
1.1       deraadt   184: {
1.52      espie     185:        LstNode ln;
                    186:        /* Deal with successor nodes. If any is marked for making and has an
1.75      espie     187:         * children_left count of 0, has not been made and isn't in the
                    188:         * examination queue, it means we need to place it in the queue as
                    189:         * it restrained itself before. */
1.52      espie     190:        for (ln = Lst_First(&gn->successors); ln != NULL; ln = Lst_Adv(ln)) {
1.71      espie     191:                GNode   *succ = Lst_Datum(ln);
1.52      espie     192:
1.75      espie     193:                if (succ->must_make && succ->children_left == 0
1.52      espie     194:                    && succ->built_status == UNKNOWN)
1.75      espie     195:                        Array_PushNew(&to_build, succ);
1.52      espie     196:        }
1.1       deraadt   197: }
1.25      espie     198:
1.64      espie     199: static void
                    200: requeue(GNode *gn)
                    201: {
                    202:        /* this is where we go inside the array and move things around */
                    203:        unsigned int i, j;
                    204:
                    205:        for (i = 0, j = 0; i < heldBack.n; i++, j++) {
                    206:                if (heldBack.a[i]->watched == gn) {
                    207:                        j--;
                    208:                        heldBack.a[i]->built_status = UNKNOWN;
                    209:                        if (DEBUG(HELDJOBS))
1.73      espie     210:                                printf("%s finished, releasing: %s\n",
1.64      espie     211:                                    gn->name, heldBack.a[i]->name);
1.75      espie     212:                        Array_Push(&to_build, heldBack.a[i]);
1.64      espie     213:                        continue;
                    214:                }
                    215:                heldBack.a[j] = heldBack.a[i];
                    216:        }
                    217:        heldBack.n = j;
                    218: }
                    219:
1.1       deraadt   220: /*-
                    221:  *-----------------------------------------------------------------------
1.25      espie     222:  * Make_Update --
1.1       deraadt   223:  *     Perform update on the parents of a node. Used by JobFinish once
                    224:  *     a node has been dealt with and by MakeStartJobs if it finds an
1.4       millert   225:  *     up-to-date node.
1.1       deraadt   226:  *
                    227:  * Results:
                    228:  *     Always returns 0
                    229:  *
                    230:  * Side Effects:
1.75      espie     231:  *     The children_left field of pgn is decremented and pgn may be placed on
                    232:  *     the to_build queue if this field becomes 0.
1.1       deraadt   233:  *
1.75      espie     234:  *     If the child got built, the parent's child_rebuilt field will be set to
1.67      espie     235:  *     true
1.1       deraadt   236:  *-----------------------------------------------------------------------
                    237:  */
                    238: void
1.35      espie     239: Make_Update(GNode *cgn)        /* the child node */
1.1       deraadt   240: {
1.40      espie     241:        GNode   *pgn;   /* the parent node */
1.47      espie     242:        LstNode ln;     /* Element in parents list */
1.40      espie     243:
1.1       deraadt   244:        /*
1.40      espie     245:         * If the child was actually made, see what its modification time is
                    246:         * now -- some rules won't actually update the file. If the file still
                    247:         * doesn't exist, make its mtime now.
1.1       deraadt   248:         */
1.48      espie     249:        if (cgn->built_status != UPTODATE) {
1.40      espie     250:                /*
                    251:                 * This is what Make does and it's actually a good thing, as it
                    252:                 * allows rules like
                    253:                 *
                    254:                 *      cmp -s y.tab.h parse.h || cp y.tab.h parse.h
                    255:                 *
1.41      espie     256:                 * to function as intended. Unfortunately, thanks to the
                    257:                 * stateless nature of NFS, there are times when the
                    258:                 * modification time of a file created on a remote machine
1.40      espie     259:                 * will not be modified before the local stat() implied by
1.41      espie     260:                 * the Dir_MTime occurs, thus leading us to believe that the
                    261:                 * file is unchanged, wreaking havoc with files that depend
1.40      espie     262:                 * on this one.
                    263:                 */
1.43      espie     264:                if (noExecute || is_out_of_date(Dir_MTime(cgn)))
1.67      espie     265:                        clock_gettime(CLOCK_REALTIME, &cgn->mtime);
1.40      espie     266:                if (DEBUG(MAKE))
1.73      espie     267:                        printf("update time: %s\n",
1.67      espie     268:                            time_to_string(&cgn->mtime));
1.40      espie     269:        }
                    270:
1.64      espie     271:        requeue(cgn);
1.58      espie     272:        /* SIB: this is where I should mark the build as finished */
1.40      espie     273:        for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Adv(ln)) {
1.71      espie     274:                pgn = Lst_Datum(ln);
1.58      espie     275:                /* SIB: there should be a siblings loop there */
1.75      espie     276:                pgn->children_left--;
1.48      espie     277:                if (pgn->must_make) {
1.52      espie     278:                        if (DEBUG(MAKE))
1.73      espie     279:                                printf("%s--=%d ",
1.75      espie     280:                                    pgn->name, pgn->children_left);
1.40      espie     281:
                    282:                        if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
1.74      espie     283:                                if (cgn->built_status == REBUILT)
1.75      espie     284:                                        pgn->child_rebuilt = true;
1.63      guenther  285:                                (void)Make_TimeStamp(pgn, cgn);
1.40      espie     286:                        }
1.75      espie     287:                        if (pgn->children_left == 0) {
1.40      espie     288:                                /*
1.75      espie     289:                                 * Queue the node up -- any yet-to-build
1.41      espie     290:                                 * predecessors will be dealt with in
1.40      espie     291:                                 * MakeStartJobs.
                    292:                                 */
1.52      espie     293:                                if (DEBUG(MAKE))
                    294:                                        printf("QUEUING ");
1.75      espie     295:                                Array_Push(&to_build, pgn);
                    296:                        } else if (pgn->children_left < 0) {
1.52      espie     297:                                Error("Child %s discovered graph cycles through %s", cgn->name, pgn->name);
1.40      espie     298:                        }
                    299:                }
1.1       deraadt   300:        }
1.52      espie     301:        if (DEBUG(MAKE))
                    302:                printf("\n");
                    303:        requeue_successors(cgn);
1.1       deraadt   304: }
1.25      espie     305:
1.46      espie     306: static bool
                    307: try_to_make_node(GNode *gn)
                    308: {
                    309:        if (DEBUG(MAKE))
                    310:                printf("Examining %s...", gn->name);
1.52      espie     311:
1.64      espie     312:        if (gn->built_status == HELDBACK) {
                    313:                if (DEBUG(HELDJOBS))
                    314:                        printf("%s already held back ???\n", gn->name);
                    315:                return false;
                    316:        }
                    317:
1.75      espie     318:        if (gn->children_left != 0) {
1.52      espie     319:                if (DEBUG(MAKE))
1.75      espie     320:                        printf(" Requeuing (%d)\n", gn->children_left);
1.52      espie     321:                add_targets_to_make(&gn->children);
1.75      espie     322:                Array_Push(&to_build, gn);
1.52      espie     323:                return false;
                    324:        }
                    325:        if (has_been_built(gn)) {
                    326:                if (DEBUG(MAKE))
                    327:                        printf(" already made\n");
1.70      jsg       328:                return false;
1.52      espie     329:        }
1.75      espie     330:        if (has_predecessor_left_to_build(gn)) {
1.52      espie     331:                if (DEBUG(MAKE))
                    332:                        printf(" Dropping for now\n");
                    333:                return false;
                    334:        }
1.46      espie     335:
1.58      espie     336:        /* SIB: this is where there should be a siblings loop */
1.75      espie     337:        if (gn->children_left != 0) {
1.52      espie     338:                if (DEBUG(MAKE))
1.75      espie     339:                        printf(" Requeuing (after deps: %d)\n",
                    340:                            gn->children_left);
1.52      espie     341:                add_targets_to_make(&gn->children);
                    342:                return false;
1.46      espie     343:        }
1.64      espie     344:        /* this is where we hold back nodes */
                    345:        if (gn->groupling != NULL) {
                    346:                GNode *gn2;
                    347:                for (gn2 = gn->groupling; gn2 != gn; gn2 = gn2->groupling)
                    348:                        if (gn2->built_status == BUILDING) {
                    349:                                gn->watched = gn2;
                    350:                                gn->built_status = HELDBACK;
                    351:                                if (DEBUG(HELDJOBS))
                    352:                                        printf("Holding back job %s, "
1.73      espie     353:                                            "groupling to %s\n",
1.64      espie     354:                                            gn->name, gn2->name);
                    355:                                Array_Push(&heldBack, gn);
                    356:                                return false;
                    357:                        }
                    358:        }
                    359:        if (gn->sibling != gn) {
                    360:                GNode *gn2;
                    361:                for (gn2 = gn->sibling; gn2 != gn; gn2 = gn2->sibling)
                    362:                        if (gn2->built_status == BUILDING) {
                    363:                                gn->watched = gn2;
                    364:                                gn->built_status = HELDBACK;
                    365:                                if (DEBUG(HELDJOBS))
                    366:                                        printf("Holding back job %s, "
1.73      espie     367:                                            "sibling to %s\n",
1.64      espie     368:                                            gn->name, gn2->name);
                    369:                                Array_Push(&heldBack, gn);
                    370:                                return false;
                    371:                        }
                    372:        }
1.46      espie     373:        if (Make_OODate(gn)) {
                    374:                if (DEBUG(MAKE))
                    375:                        printf("out-of-date\n");
                    376:                if (queryFlag)
                    377:                        return true;
1.58      espie     378:                /* SIB: this is where commands should get prepared */
1.46      espie     379:                Make_DoAllVar(gn);
1.80    ! espie     380:                if (node_find_valid_commands(gn)) {
        !           381:                        if (touchFlag)
        !           382:                                Job_Touch(gn);
        !           383:                        else
        !           384:                                Job_Make(gn);
        !           385:                } else
        !           386:                        node_failure(gn);
1.46      espie     387:        } else {
                    388:                if (DEBUG(MAKE))
                    389:                        printf("up-to-date\n");
1.48      espie     390:                gn->built_status = UPTODATE;
1.46      espie     391:                if (gn->type & OP_JOIN) {
                    392:                        /*
1.76      espie     393:                         * Even for an up-to-date .JOIN node, we need its
                    394:                         * local variables, so that we have the right
                    395:                         * value for .TARGET when computing the
                    396:                         * local variables of its parent(s)...
1.46      espie     397:                         */
                    398:                        Make_DoAllVar(gn);
                    399:                }
                    400:
                    401:                Make_Update(gn);
                    402:        }
                    403:        return false;
                    404: }
                    405:
1.40      espie     406: /*
1.1       deraadt   407:  *-----------------------------------------------------------------------
                    408:  * MakeStartJobs --
                    409:  *     Start as many jobs as possible.
                    410:  *
                    411:  * Results:
                    412:  *     If the query flag was given to pmake, no job will be started,
                    413:  *     but as soon as an out-of-date target is found, this function
1.26      espie     414:  *     returns true. At all other times, this function returns false.
1.1       deraadt   415:  *
                    416:  * Side Effects:
1.75      espie     417:  *     Nodes are removed from the to_build queue and job table slots
1.1       deraadt   418:  *     are filled.
                    419:  *-----------------------------------------------------------------------
                    420:  */
1.26      espie     421: static bool
1.35      espie     422: MakeStartJobs(void)
1.1       deraadt   423: {
1.40      espie     424:        GNode   *gn;
1.4       millert   425:
1.75      espie     426:        while (can_start_job() && (gn = Array_Pop(&to_build)) != NULL) {
1.46      espie     427:                if (try_to_make_node(gn))
                    428:                        return true;
1.1       deraadt   429:        }
1.40      espie     430:        return false;
1.1       deraadt   431: }
1.25      espie     432:
1.15      espie     433: static void
1.72      espie     434: MakePrintStatus(void *gnp)
1.1       deraadt   435: {
1.69      espie     436:        GNode   *gn = gnp;
1.48      espie     437:        if (gn->built_status == UPTODATE) {
1.40      espie     438:                printf("`%s' is up to date.\n", gn->name);
1.75      espie     439:        } else if (gn->children_left != 0) {
1.72      espie     440:                printf("`%s' not remade because of errors.\n", gn->name);
1.1       deraadt   441:        }
                    442: }
1.25      espie     443:
1.52      espie     444: static void
1.56      espie     445: MakeAddChild(void *to_addp, void *ap)
1.52      espie     446: {
1.69      espie     447:        GNode *gn = to_addp;
                    448:        struct growableArray *a = ap;
1.52      espie     449:
                    450:        if (!gn->must_make && !(gn->type & OP_USE))
1.69      espie     451:                Array_Push(a, gn);
1.52      espie     452: }
                    453:
                    454: static void
1.58      espie     455: MakeHandleUse(void *cgnp, void *pgnp)
1.52      espie     456: {
1.69      espie     457:        GNode *cgn = cgnp;
                    458:        GNode *pgn = pgnp;
1.58      espie     459:
                    460:        if (cgn->type & OP_USE)
                    461:                Make_HandleUse(cgn, pgn);
1.52      espie     462: }
                    463:
1.75      espie     464: /* Add stuff to the to_build queue. we try to sort things so that stuff
1.52      espie     465:  * that can be done directly is done right away.  This won't be perfect,
                    466:  * since some dependencies are only discovered later (e.g., SuffFindDeps).
1.46      espie     467:  */
                    468: static void
1.52      espie     469: add_targets_to_make(Lst todo)
1.46      espie     470: {
                    471:        GNode *gn;
1.56      espie     472:
1.52      espie     473:        unsigned int slot;
                    474:
1.56      espie     475:        AppendList2Array(todo, &examine);
1.46      espie     476:
1.56      espie     477:        while ((gn = Array_Pop(&examine)) != NULL) {
1.52      espie     478:                if (gn->must_make)      /* already known */
                    479:                        continue;
                    480:                gn->must_make = true;
1.46      espie     481:
1.52      espie     482:                slot = ohash_qlookup(&targets, gn->name);
                    483:                if (!ohash_find(&targets, slot))
                    484:                        ohash_insert(&targets, slot, gn);
                    485:
                    486:
                    487:                look_harder_for_target(gn);
1.58      espie     488:                kludge_look_harder_for_target(gn);
1.52      espie     489:                /*
                    490:                 * Apply any .USE rules before looking for implicit
                    491:                 * dependencies to make sure everything that should have
                    492:                 * commands has commands ...
                    493:                 */
                    494:                Lst_ForEach(&gn->children, MakeHandleUse, gn);
1.68      espie     495:                Suff_FindDeps(gn);
1.52      espie     496:                expand_all_children(gn);
1.46      espie     497:
1.75      espie     498:                if (gn->children_left != 0) {
1.52      espie     499:                        if (DEBUG(MAKE))
1.75      espie     500:                                printf("%s: not queuing (%d children left to build)\n",
                    501:                                    gn->name, gn->children_left);
1.52      espie     502:                        Lst_ForEach(&gn->children, MakeAddChild,
                    503:                            &examine);
                    504:                } else {
                    505:                        if (DEBUG(MAKE))
                    506:                                printf("%s: queuing\n", gn->name);
1.75      espie     507:                        Array_Push(&to_build, gn);
1.46      espie     508:                }
                    509:        }
1.56      espie     510:        if (randomize_queue)
1.75      espie     511:                randomize_garray(&to_build);
1.46      espie     512: }
                    513:
1.1       deraadt   514: /*-
                    515:  *-----------------------------------------------------------------------
1.6       millert   516:  * Make_Run --
                    517:  *     Initialize the nodes to remake and the list of nodes which are
                    518:  *     ready to be made by doing a breadth-first traversal of the graph
                    519:  *     starting from the nodes in the given list. Once this traversal
1.75      espie     520:  *     is finished, all the 'leaves' of the graph are in the to_build
1.6       millert   521:  *     queue.
                    522:  *     Using this queue and the Job module, work back up the graph,
                    523:  *     calling on MakeStartJobs to keep the job table as full as
                    524:  *     possible.
                    525:  *
1.1       deraadt   526:  * Results:
1.26      espie     527:  *     true if work was done. false otherwise.
1.1       deraadt   528:  *
                    529:  * Side Effects:
1.48      espie     530:  *     The must_make field of all nodes involved in the creation of the given
1.75      espie     531:  *     targets is set to 1. The to_build list is set to contain all the
1.6       millert   532:  *     'leaves' of these subgraphs.
1.1       deraadt   533:  *-----------------------------------------------------------------------
                    534:  */
1.26      espie     535: bool
1.35      espie     536: Make_Run(Lst targs)            /* the initial list of targets */
1.1       deraadt   537: {
1.65      espie     538:        bool problem;   /* errors occurred */
1.40      espie     539:
1.54      espie     540:        /* wild guess at initial sizes */
1.75      espie     541:        Array_Init(&to_build, 500);
1.56      espie     542:        Array_Init(&examine, 150);
1.64      espie     543:        Array_Init(&heldBack, 100);
1.52      espie     544:        ohash_init(&targets, 10, &gnode_info);
1.54      espie     545:        if (DEBUG(PARALLEL))
                    546:                random_setup();
1.40      espie     547:
1.46      espie     548:        add_targets_to_make(targs);
1.40      espie     549:        if (queryFlag) {
                    550:                /*
1.41      espie     551:                 * We wouldn't do any work unless we could start some jobs in
                    552:                 * the next loop... (we won't actually start any, of course,
1.40      espie     553:                 * this is just to see if any of the targets was out of date)
                    554:                 */
                    555:                return MakeStartJobs();
                    556:        } else {
                    557:                /*
                    558:                 * Initialization. At the moment, no jobs are running and until
                    559:                 * some get started, nothing will happen since the remaining
1.41      espie     560:                 * upward traversal of the graph is performed by the routines
                    561:                 * in job.c upon the finishing of a job. So we fill the Job
1.40      espie     562:                 * table as much as we can before going into our loop.
                    563:                 */
                    564:                (void)MakeStartJobs();
1.1       deraadt   565:        }
1.4       millert   566:
1.1       deraadt   567:        /*
1.40      espie     568:         * Main Loop: The idea here is that the ending of jobs will take
                    569:         * care of the maintenance of data structures and the waiting for output
                    570:         * will cause us to be idle most of the time while our children run as
                    571:         * much as possible. Because the job table is kept as full as possible,
                    572:         * the only time when it will be empty is when all the jobs which need
                    573:         * running have been run, so that is the end condition of this loop.
1.41      espie     574:         * Note that the Job module will exit if there were any errors unless
1.40      espie     575:         * the keepgoing flag was given.
1.1       deraadt   576:         */
1.40      espie     577:        while (!Job_Empty()) {
1.57      espie     578:                handle_running_jobs();
1.40      espie     579:                (void)MakeStartJobs();
                    580:        }
                    581:
1.77      espie     582:        if (!queryFlag)
                    583:                problem = Job_Finish();
1.40      espie     584:
1.1       deraadt   585:        /*
1.40      espie     586:         * Print the final status of each target. E.g. if it wasn't made
                    587:         * because some inferior reported an error.
1.1       deraadt   588:         */
1.72      espie     589:        if (targets_contain_cycles()) {
                    590:                break_and_print_cycles(targs);
                    591:                problem = true;
                    592:        }
                    593:        Lst_Every(targs, MakePrintStatus);
1.65      espie     594:        if (problem)
1.52      espie     595:                Fatal("Errors while building");
1.4       millert   596:
1.40      espie     597:        return true;
1.72      espie     598: }
                    599:
                    600: /* round-about detection: assume make is bug-free, if there are targets
                    601:  * that have not been touched, it means they never were reached, so we can
                    602:  * look for a cycle
                    603:  */
                    604: static bool
                    605: targets_contain_cycles(void)
                    606: {
                    607:        GNode *gn;
                    608:        unsigned int i;
                    609:        bool cycle = false;
                    610:        bool first = true;
                    611:
1.73      espie     612:        for (gn = ohash_first(&targets, &i); gn != NULL;
1.72      espie     613:            gn = ohash_next(&targets, &i)) {
                    614:                if (has_been_built(gn))
                    615:                        continue;
                    616:                cycle = true;
                    617:                if (first)
                    618:                        printf("Error target(s) unaccounted for: ");
                    619:                printf("%s ", gn->name);
                    620:                first = false;
                    621:        }
                    622:        if (!first)
                    623:                printf("\n");
                    624:        return cycle;
                    625: }
                    626:
                    627: static void
                    628: print_unlink_cycle(struct growableArray *l, GNode *c)
                    629: {
                    630:        LstNode ln;
                    631:        GNode *gn = NULL;
                    632:        unsigned int i;
                    633:
                    634:        printf("Cycle found: ");
                    635:
                    636:        for (i = 0; i != l->n; i++) {
                    637:                gn = l->a[i];
                    638:                if (gn == c)
                    639:                        printf("(");
                    640:                printf("%s -> ", gn->name);
                    641:        }
                    642:        printf("%s)\n", c->name);
                    643:        assert(gn);
                    644:
                    645:        /* So the first element is tied to our node, find and kill the link */
                    646:        for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
                    647:                GNode *gn2 = Lst_Datum(ln);
                    648:                if (gn2 == c) {
                    649:                        Lst_Remove(&gn->children, ln);
                    650:                        return;
                    651:                }
                    652:        }
                    653:        /* this shouldn't happen ever */
                    654:        assert(0);
                    655: }
                    656:
                    657: /* each call to find_cycle records a cycle in cycle, to break at node c.
                    658:  * this will stop eventually.
                    659:  */
                    660: static void
                    661: break_and_print_cycles(Lst t)
                    662: {
                    663:        struct growableArray cycle;
                    664:
                    665:        Array_Init(&cycle, 16); /* cycles are generally shorter */
                    666:        while (1) {
                    667:                GNode *c;
                    668:
                    669:                Array_Reset(&cycle);
                    670:                c = find_cycle(t, &cycle);
                    671:                if (c)
                    672:                        print_unlink_cycle(&cycle, c);
                    673:                else
                    674:                        break;
                    675:        }
                    676:        free(cycle.a);
                    677: }
                    678:
                    679:
                    680: static GNode *
                    681: find_cycle(Lst l, struct growableArray *cycle)
                    682: {
                    683:        LstNode ln;
                    684:
                    685:        for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln)) {
                    686:                GNode *gn = Lst_Datum(ln);
                    687:                if (gn->in_cycle) {
                    688:                        /* we should print the cycle and not do more */
                    689:                        return gn;
                    690:                }
                    691:
                    692:                if (gn->built_status == UPTODATE)
                    693:                        continue;
1.75      espie     694:                if (gn->children_left != 0) {
1.73      espie     695:                        GNode *c;
1.72      espie     696:
                    697:                        gn->in_cycle = true;
                    698:                        Array_Push(cycle, gn);
                    699:                        c = find_cycle(&gn->children, cycle);
                    700:                        gn->in_cycle = false;
                    701:                        if (c)
                    702:                                return c;
                    703:                        Array_Pop(cycle);
                    704:                }
                    705:        }
                    706:        return NULL;
1.1       deraadt   707: }