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

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