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

Annotation of src/usr.bin/make/engine.c, Revision 1.36

1.36    ! espie       1: /*     $OpenBSD: engine.c,v 1.35 2012/10/04 13:20:46 espie Exp $ */
1.33      espie       2: /*
                      3:  * Copyright (c) 2012 Marc Espie.
                      4:  *
                      5:  * Extensive code modifications for the OpenBSD project.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     17:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     18:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     19:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     20:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     21:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     22:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     23:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     24:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     25:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     26:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     27:  */
1.1       espie      28: /*
                     29:  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
                     30:  * Copyright (c) 1988, 1989 by Adam de Boor
                     31:  * Copyright (c) 1989 by Berkeley Softworks
                     32:  * All rights reserved.
                     33:  *
                     34:  * This code is derived from software contributed to Berkeley by
                     35:  * Adam de Boor.
                     36:  *
                     37:  * Redistribution and use in source and binary forms, with or without
                     38:  * modification, are permitted provided that the following conditions
                     39:  * are met:
                     40:  * 1. Redistributions of source code must retain the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer.
                     42:  * 2. Redistributions in binary form must reproduce the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer in the
                     44:  *    documentation and/or other materials provided with the distribution.
                     45:  * 3. Neither the name of the University nor the names of its contributors
                     46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
                     61:
1.10      espie      62: #include <sys/types.h>
1.33      espie      63: #include <sys/time.h>
1.10      espie      64: #include <sys/wait.h>
1.20      espie      65: #include <assert.h>
1.1       espie      66: #include <limits.h>
                     67: #include <stdio.h>
1.10      espie      68: #include <stdlib.h>
                     69: #include <ctype.h>
1.1       espie      70: #include <errno.h>
                     71: #include <fcntl.h>
                     72: #include <unistd.h>
                     73: #include <string.h>
1.10      espie      74: #include <signal.h>
1.1       espie      75: #include "config.h"
                     76: #include "defines.h"
                     77: #include "dir.h"
                     78: #include "engine.h"
                     79: #include "arch.h"
                     80: #include "gnode.h"
                     81: #include "targ.h"
                     82: #include "var.h"
                     83: #include "extern.h"
                     84: #include "lst.h"
                     85: #include "timestamp.h"
                     86: #include "make.h"
1.10      espie      87: #include "pathnames.h"
                     88: #include "error.h"
                     89: #include "str.h"
                     90: #include "memory.h"
1.16      espie      91: #include "buf.h"
1.24      espie      92: #include "job.h"
1.33      espie      93: #include "lowparse.h"
1.1       espie      94:
                     95: static void MakeTimeStamp(void *, void *);
1.6       espie      96: static int rewrite_time(const char *);
1.10      espie      97: static void setup_meta(void);
1.33      espie      98: static void setup_engine(void);
1.10      espie      99: static char **recheck_command_for_shell(char **);
1.34      espie     100: static void list_parents(GNode *, FILE *);
1.10      espie     101:
1.36    ! espie     102: /* XXX due to a bug in make's logic, targets looking like *.a or -l*
        !           103:  * have been silently dropped when make couldn't figure them out.
        !           104:  * Now, we warn about them until all Makefile bugs have been fixed.
        !           105:  */
        !           106: static bool
        !           107: drop_silently(const char *s)
        !           108: {
        !           109:        size_t len;
        !           110:
        !           111:        if (s[0] == '-' && s[1] == 'l')
        !           112:                return true;
        !           113:
        !           114:        len = strlen(s);
        !           115:        if (len >=2 && s[len-2] == '.' && s[len-1] == 'a')
        !           116:                return true;
        !           117:        return false;
        !           118: }
        !           119:
1.1       espie     120: bool
1.33      espie     121: node_find_valid_commands(GNode *gn)
1.1       espie     122: {
1.11      espie     123:        /* Alter our type to tell if errors should be ignored or things
1.21      espie     124:         * should not be printed so setup_and_run_command knows what to do.
1.11      espie     125:         */
                    126:        if (Targ_Ignore(gn))
                    127:                gn->type |= OP_IGNORE;
                    128:        if (Targ_Silent(gn))
                    129:                gn->type |= OP_SILENT;
                    130:
1.34      espie     131:        if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) {
1.36    ! espie     132:                if (drop_silently(gn->name)) {
1.34      espie     133:                        printf("Warning: target %s", gn->name);
                    134:                        list_parents(gn, stdout);
1.36    ! espie     135:                        printf(" does not have any command (BUG)\n");
1.34      espie     136:                        return true;
                    137:                }
1.3       espie     138:                /*
                    139:                 * No commands. Look for .DEFAULT rule from which we might infer
                    140:                 * commands
                    141:                 */
1.28      espie     142:                if ((gn->type & OP_NODEFAULT) == 0 &&
1.18      espie     143:                    (DEFAULT->type & OP_DUMMY) == 0 &&
1.9       espie     144:                    !Lst_IsEmpty(&DEFAULT->commands)) {
1.3       espie     145:                        /*
                    146:                         * Make only looks for a .DEFAULT if the node was never
                    147:                         * the target of an operator, so that's what we do too.
                    148:                         * If a .DEFAULT was given, we substitute its commands
                    149:                         * for gn's commands and set the IMPSRC variable to be
                    150:                         * the target's name The DEFAULT node acts like a
                    151:                         * transformation rule, in that gn also inherits any
                    152:                         * attributes or sources attached to .DEFAULT itself.
                    153:                         */
                    154:                        Make_HandleUse(DEFAULT, gn);
1.16      espie     155:                        Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
1.3       espie     156:                } else if (is_out_of_date(Dir_MTime(gn))) {
                    157:                        /*
                    158:                         * The node wasn't the target of an operator we have no
                    159:                         * .DEFAULT rule to go on and the target doesn't
                    160:                         * already exist. There's nothing more we can do for
1.28      espie     161:                         * this branch.
1.3       espie     162:                         */
1.20      espie     163:                         return false;
                    164:                }
1.1       espie     165:        }
1.3       espie     166:        return true;
1.1       espie     167: }
                    168:
1.34      espie     169: static void
                    170: list_parents(GNode *gn, FILE *out)
                    171: {
                    172:        LstNode ln;
                    173:        bool first = true;
                    174:
                    175:        for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
                    176:                GNode *p = Lst_Datum(ln);
                    177:                if (!p->must_make)
                    178:                        continue;
                    179:                if (first) {
                    180:                        fprintf(out, " (prerequisite of:");
                    181:                        first = false;
                    182:                }
                    183:                fprintf(out, " %s", p->name);
                    184:        }
                    185:        if (!first)
                    186:                fprintf(out, ")");
                    187: }
                    188:
1.20      espie     189: void
1.33      espie     190: node_failure(GNode *gn)
1.20      espie     191: {
                    192:        /*
                    193:         If the -k flag wasn't given, we stop in
                    194:         * our tracks, otherwise we just don't update this
                    195:         * node's parents so they never get examined.
                    196:         */
1.34      espie     197:        const char *diag;
                    198:        FILE *out;
1.20      espie     199:
                    200:        if (gn->type & OP_OPTIONAL) {
1.34      espie     201:                out = stdout;
                    202:                diag = "(ignored)";
1.20      espie     203:        } else if (keepgoing) {
1.34      espie     204:                out = stdout;
                    205:                diag = "(continuing)";
1.20      espie     206:        } else {
1.34      espie     207:                out = stderr;
                    208:                diag = "";
                    209:        }
                    210:        fprintf(out, "make: don't know how to make %s", gn->name);
                    211:        list_parents(gn, out);
                    212:        fprintf(out, "%s\n", diag);
                    213:        if (out == stdout)
                    214:                fflush(stdout);
                    215:        else {
1.33      espie     216:                print_errors();
                    217:                Punt(NULL);
1.20      espie     218:        }
                    219: }
1.33      espie     220:
1.6       espie     221: /* touch files the hard way, by writing stuff to them */
                    222: static int
                    223: rewrite_time(const char *name)
                    224: {
                    225:        int fd;
                    226:        char c;
                    227:
                    228:        fd = open(name, O_RDWR | O_CREAT, 0666);
                    229:        if (fd < 0)
                    230:                return -1;
                    231:        /*
                    232:         * Read and write a byte to the file to change
                    233:         * the modification time.
                    234:         */
                    235:        if (read(fd, &c, 1) == 1) {
                    236:                (void)lseek(fd, 0, SEEK_SET);
                    237:                (void)write(fd, &c, 1);
                    238:        }
                    239:
                    240:        (void)close(fd);
                    241:        return 0;
                    242: }
                    243:
1.1       espie     244: void
1.12      espie     245: Job_Touch(GNode *gn)
1.1       espie     246: {
1.33      espie     247:        handle_all_signals();
1.26      espie     248:        if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
1.3       espie     249:                /*
1.9       espie     250:                 * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
                    251:                 * and, as such, shouldn't really be created.
1.26      espie     252:                 * Likewise, .PHONY targets are not really files
1.3       espie     253:                 */
                    254:                return;
                    255:        }
1.1       espie     256:
1.12      espie     257:        if (!(gn->type & OP_SILENT)) {
1.3       espie     258:                (void)fprintf(stdout, "touch %s\n", gn->name);
                    259:                (void)fflush(stdout);
                    260:        }
1.1       espie     261:
1.3       espie     262:        if (noExecute) {
                    263:                return;
                    264:        }
1.1       espie     265:
1.3       espie     266:        if (gn->type & OP_ARCHV) {
                    267:                Arch_Touch(gn);
                    268:        } else {
                    269:                const char *file = gn->path != NULL ? gn->path : gn->name;
                    270:
                    271:                if (set_times(file) == -1){
1.6       espie     272:                        if (rewrite_time(file) == -1) {
1.5       espie     273:                                (void)fprintf(stdout,
                    274:                                    "*** couldn't touch %s: %s", file,
1.3       espie     275:                                    strerror(errno));
                    276:                                (void)fflush(stdout);
1.9       espie     277:                        }
1.1       espie     278:                }
                    279:        }
                    280: }
                    281:
                    282: void
1.7       espie     283: Make_TimeStamp(GNode *parent, GNode *child)
1.1       espie     284: {
1.8       espie     285:        if (is_strictly_before(parent->cmtime, child->mtime))
                    286:                parent->cmtime = child->mtime;
1.1       espie     287: }
                    288:
                    289: void
1.9       espie     290: Make_HandleUse(GNode   *cgn,   /* The .USE node */
1.1       espie     291:     GNode      *pgn)   /* The target of the .USE node */
                    292: {
1.3       espie     293:        GNode   *gn;    /* A child of the .USE node */
                    294:        LstNode ln;     /* An element in the children list */
1.1       espie     295:
1.20      espie     296:
                    297:        assert(cgn->type & (OP_USE|OP_TRANSFORM));
                    298:
                    299:        if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
                    300:                /* .USE or transformation and target has no commands
                    301:                 * -- append the child's commands to the parent.  */
                    302:                Lst_Concat(&pgn->commands, &cgn->commands);
                    303:        }
                    304:
                    305:        for (ln = Lst_First(&cgn->children); ln != NULL;
                    306:            ln = Lst_Adv(ln)) {
                    307:                gn = (GNode *)Lst_Datum(ln);
                    308:
                    309:                if (Lst_AddNew(&pgn->children, gn)) {
                    310:                        Lst_AtEnd(&gn->parents, pgn);
                    311:                        pgn->unmade++;
1.3       espie     312:                }
1.20      espie     313:        }
1.1       espie     314:
1.20      espie     315:        pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
1.1       espie     316:
1.20      espie     317:        /*
                    318:         * This child node is now "made", so we decrement the count of
                    319:         * unmade children in the parent... We also remove the child
                    320:         * from the parent's list to accurately reflect the number of
                    321:         * decent children the parent has. This is used by Make_Run to
                    322:         * decide whether to queue the parent or examine its children...
                    323:         */
                    324:        if (cgn->type & OP_USE)
                    325:                pgn->unmade--;
1.1       espie     326: }
                    327:
1.16      espie     328: void
                    329: Make_DoAllVar(GNode *gn)
1.1       espie     330: {
1.16      espie     331:        GNode *child;
                    332:        LstNode ln;
                    333:        BUFFER allsrc, oodate;
                    334:        char *target;
                    335:        bool do_oodate;
                    336:        int oodate_count, allsrc_count = 0;
                    337:
                    338:        oodate_count = 0;
                    339:        allsrc_count = 0;
                    340:
                    341:        for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
                    342:                child = (GNode *)Lst_Datum(ln);
                    343:                if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
                    344:                        continue;
1.9       espie     345:                if (OP_NOP(child->type) ||
1.16      espie     346:                    (target = Var(TARGET_INDEX, child)) == NULL) {
1.3       espie     347:                        /*
                    348:                         * this node is only source; use the specific pathname
                    349:                         * for it
                    350:                         */
1.9       espie     351:                        target = child->path != NULL ? child->path :
                    352:                            child->name;
1.3       espie     353:                }
1.1       espie     354:
1.16      espie     355:                /*
                    356:                 * It goes in the OODATE variable if the parent is younger than
                    357:                 * the child or if the child has been modified more recently
                    358:                 * than the start of the make.  This is to keep make from
                    359:                 * getting confused if something else updates the parent after
                    360:                 * the make starts (shouldn't happen, I know, but sometimes it
                    361:                 * does). In such a case, if we've updated the kid, the parent
                    362:                 * is likely to have a modification time later than that of the
                    363:                 * kid and anything that relies on the OODATE variable will be
                    364:                 * hosed.
                    365:                 */
                    366:                do_oodate = false;
                    367:                if (gn->type & OP_JOIN) {
1.14      espie     368:                        if (child->built_status == MADE)
1.16      espie     369:                                do_oodate = true;
                    370:                } else if (is_strictly_before(gn->mtime, child->mtime) ||
1.9       espie     371:                   (!is_strictly_before(child->mtime, now) &&
1.16      espie     372:                   child->built_status == MADE))
                    373:                        do_oodate = true;
                    374:                if (do_oodate) {
                    375:                        oodate_count++;
                    376:                        if (oodate_count == 1)
                    377:                                Var(OODATE_INDEX, gn) = target;
                    378:                        else {
                    379:                                if (oodate_count == 2) {
                    380:                                        Buf_Init(&oodate, 0);
1.28      espie     381:                                        Buf_AddString(&oodate,
1.16      espie     382:                                            Var(OODATE_INDEX, gn));
                    383:                                }
                    384:                                Buf_AddSpace(&oodate);
                    385:                                Buf_AddString(&oodate, target);
                    386:                        }
                    387:                }
                    388:                allsrc_count++;
                    389:                if (allsrc_count == 1)
                    390:                        Var(ALLSRC_INDEX, gn) = target;
                    391:                else {
                    392:                        if (allsrc_count == 2) {
                    393:                                Buf_Init(&allsrc, 0);
1.28      espie     394:                                Buf_AddString(&allsrc,
1.16      espie     395:                                    Var(ALLSRC_INDEX, gn));
                    396:                        }
                    397:                        Buf_AddSpace(&allsrc);
                    398:                        Buf_AddString(&allsrc, target);
1.3       espie     399:                }
1.1       espie     400:        }
                    401:
1.16      espie     402:        if (allsrc_count > 1)
                    403:                Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
                    404:        if (oodate_count > 1)
                    405:                Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
1.1       espie     406:
1.13      espie     407:        if (gn->impliedsrc)
1.16      espie     408:                Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
1.28      espie     409:
1.3       espie     410:        if (gn->type & OP_JOIN)
1.16      espie     411:                Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
1.1       espie     412: }
                    413:
                    414: /* Wrapper to call Make_TimeStamp from a forEach loop. */
                    415: static void
1.9       espie     416: MakeTimeStamp(void *parent, void *child)
1.1       espie     417: {
1.9       espie     418:     Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1       espie     419: }
                    420:
                    421: bool
1.9       espie     422: Make_OODate(GNode *gn)
1.1       espie     423: {
1.9       espie     424:        bool        oodate;
1.1       espie     425:
                    426:        /*
1.3       espie     427:         * Certain types of targets needn't even be sought as their datedness
                    428:         * doesn't depend on their modification time...
1.1       espie     429:         */
1.15      espie     430:        if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
1.3       espie     431:                (void)Dir_MTime(gn);
                    432:                if (DEBUG(MAKE)) {
1.9       espie     433:                        if (!is_out_of_date(gn->mtime))
1.5       espie     434:                                printf("modified %s...",
1.3       espie     435:                                    time_to_string(gn->mtime));
1.9       espie     436:                        else
1.3       espie     437:                                printf("non-existent...");
                    438:                }
1.1       espie     439:        }
                    440:
                    441:        /*
1.3       espie     442:         * A target is remade in one of the following circumstances:
1.9       espie     443:         * - its modification time is smaller than that of its youngest child
                    444:         *   and it would actually be run (has commands or type OP_NOP)
                    445:         * - it's the object of a force operator
                    446:         * - it has no children, was on the lhs of an operator and doesn't
                    447:         *   exist already.
1.3       espie     448:         *
1.1       espie     449:         */
1.3       espie     450:        if (gn->type & OP_USE) {
                    451:                /*
                    452:                 * If the node is a USE node it is *never* out of date
                    453:                 * no matter *what*.
                    454:                 */
1.9       espie     455:                if (DEBUG(MAKE))
1.3       espie     456:                        printf(".USE node...");
                    457:                oodate = false;
                    458:        } else if (gn->type & OP_JOIN) {
                    459:                /*
                    460:                 * A target with the .JOIN attribute is only considered
                    461:                 * out-of-date if any of its children was out-of-date.
                    462:                 */
1.9       espie     463:                if (DEBUG(MAKE))
1.3       espie     464:                        printf(".JOIN node...");
                    465:                oodate = gn->childMade;
                    466:        } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
                    467:                /*
1.9       espie     468:                 * A node which is the object of the force (!) operator or which
                    469:                 * has the .EXEC attribute is always considered out-of-date.
1.3       espie     470:                 */
                    471:                if (DEBUG(MAKE)) {
1.9       espie     472:                        if (gn->type & OP_FORCE)
1.3       espie     473:                                printf("! operator...");
1.9       espie     474:                        else if (gn->type & OP_PHONY)
1.3       espie     475:                                printf(".PHONY node...");
1.9       espie     476:                        else
1.3       espie     477:                                printf(".EXEC node...");
                    478:                }
                    479:                oodate = true;
                    480:        } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9       espie     481:           (is_out_of_date(gn->cmtime) &&
1.3       espie     482:            (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
                    483:                /*
                    484:                 * A node whose modification time is less than that of its
                    485:                 * youngest child or that has no children (cmtime ==
                    486:                 * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
1.9       espie     487:                 * or was the object of a :: operator is out-of-date.
1.3       espie     488:                 */
                    489:                if (DEBUG(MAKE)) {
1.9       espie     490:                        if (is_strictly_before(gn->mtime, gn->cmtime))
1.3       espie     491:                                printf("modified before source...");
1.9       espie     492:                        else if (is_out_of_date(gn->mtime))
1.3       espie     493:                                printf("non-existent and no sources...");
1.9       espie     494:                        else
1.3       espie     495:                                printf(":: operator and no sources...");
                    496:                }
                    497:                oodate = true;
                    498:        } else {
                    499:                oodate = false;
                    500:        }
1.1       espie     501:
1.3       espie     502:        /*
                    503:         * If the target isn't out-of-date, the parents need to know its
                    504:         * modification time. Note that targets that appear to be out-of-date
                    505:         * but aren't, because they have no commands and aren't of type OP_NOP,
                    506:         * have their mtime stay below their children's mtime to keep parents
                    507:         * from thinking they're out-of-date.
                    508:         */
                    509:        if (!oodate)
                    510:                Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1       espie     511:
1.3       espie     512:        return oodate;
1.1       espie     513: }
                    514:
1.10      espie     515: /* The following array is used to make a fast determination of which
                    516:  * characters are interpreted specially by the shell.  If a command
                    517:  * contains any of these characters, it is executed by the shell, not
                    518:  * directly by us.  */
                    519: static char        meta[256];
                    520:
                    521: void
                    522: setup_meta(void)
                    523: {
                    524:        char *p;
                    525:
1.32      espie     526:        for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
1.10      espie     527:                meta[(unsigned char) *p] = 1;
                    528:        /* The null character serves as a sentinel in the string.  */
                    529:        meta[0] = 1;
                    530: }
                    531:
                    532: static char **
                    533: recheck_command_for_shell(char **av)
                    534: {
                    535:        char *runsh[] = {
1.32      espie     536:                "!", "alias", "cd", "eval", "exit", "read", "set", "ulimit",
1.10      espie     537:                "unalias", "unset", "wait", "umask", NULL
                    538:        };
                    539:
                    540:        char **p;
                    541:
                    542:        /* optimization: if exec cmd, we avoid the intermediate shell */
                    543:        if (strcmp(av[0], "exec") == 0)
                    544:                av++;
                    545:
                    546:        for (p = runsh; *p; p++)
                    547:                if (strcmp(av[0], *p) == 0)
                    548:                        return NULL;
                    549:
                    550:        return av;
                    551: }
                    552:
1.33      espie     553: void
1.10      espie     554: run_command(const char *cmd, bool errCheck)
                    555: {
                    556:        const char *p;
                    557:        char *shargv[4];
                    558:        char **todo;
                    559:
                    560:        shargv[0] = _PATH_BSHELL;
                    561:
                    562:        shargv[1] = errCheck ? "-ec" : "-c";
                    563:        shargv[2] = (char *)cmd;
                    564:        shargv[3] = NULL;
                    565:
                    566:        todo = shargv;
                    567:
                    568:
                    569:        /* Search for meta characters in the command. If there are no meta
                    570:         * characters, there's no need to execute a shell to execute the
                    571:         * command.  */
                    572:        for (p = cmd; !meta[(unsigned char)*p]; p++)
                    573:                continue;
                    574:        if (*p == '\0') {
                    575:                char *bp;
                    576:                char **av;
                    577:                int argc;
                    578:                /* No meta-characters, so probably no need to exec a shell.
                    579:                 * Break the command into words to form an argument vector
                    580:                 * we can execute.  */
                    581:                av = brk_string(cmd, &argc, &bp);
                    582:                av = recheck_command_for_shell(av);
                    583:                if (av != NULL)
                    584:                        todo = av;
                    585:        }
                    586:        execvp(todo[0], todo);
                    587:
                    588:        if (errno == ENOENT)
                    589:                fprintf(stderr, "%s: not found\n", todo[0]);
                    590:        else
                    591:                perror(todo[0]);
                    592:        _exit(1);
                    593: }
                    594:
1.33      espie     595: static Job myjob;
                    596:
                    597: void
                    598: job_attach_node(Job *job, GNode *node)
                    599: {
                    600:        job->node = node;
1.34      espie     601:        job->node->built_status = BUILDING;
1.33      espie     602:        job->next_cmd = Lst_First(&node->commands);
                    603:        job->exit_type = JOB_EXIT_OKAY;
                    604:        job->location = NULL;
                    605:        job->flags = 0;
1.36    ! espie     606:        job->sent_signal = 0;
1.33      espie     607: }
                    608:
                    609: void
                    610: job_handle_status(Job *job, int status)
                    611: {
1.36    ! espie     612:        bool silent;
        !           613:
        !           614:        /* if there's one job running and we don't keep going, no need
        !           615:         * to report right now.
        !           616:         */
        !           617:        if ((job->flags & JOB_ERRCHECK) && !keepgoing && runningJobs == NULL)
        !           618:                silent = !DEBUG(JOB);
        !           619:        else
        !           620:                silent = false;
        !           621:
1.33      espie     622:        debug_job_printf("Process %ld (%s) exited with status %d.\n",
                    623:            (long)job->pid, job->node->name, status);
                    624:
                    625:        /* classify status */
                    626:        if (WIFEXITED(status)) {
                    627:                job->code = WEXITSTATUS(status);/* exited */
                    628:                if (status != 0) {
1.36    ! espie     629:                        if (!silent)
        !           630:                                printf("*** Error %d", job->code);
1.33      espie     631:                        job->exit_type = JOB_EXIT_BAD;
                    632:                } else
                    633:                        job->exit_type = JOB_EXIT_OKAY;
                    634:        } else {
                    635:                job->exit_type = JOB_SIGNALED;
                    636:                job->code = WTERMSIG(status);   /* signaled */
1.36    ! espie     637:                if (!silent)
        !           638:                        printf("*** Signal %d", job->code);
1.33      espie     639:        }
                    640:
                    641:        /* if there is a problem, what's going on ? */
                    642:        if (job->exit_type != JOB_EXIT_OKAY) {
1.36    ! espie     643:                if (!silent)
        !           644:                        printf(" in target '%s'", job->node->name);
1.33      espie     645:                if (job->flags & JOB_ERRCHECK) {
                    646:                        job->node->built_status = ERROR;
                    647:                        /* compute expensive status if we really want it */
                    648:                        if ((job->flags & JOB_SILENT) && job == &myjob)
                    649:                                determine_expensive_job(job);
                    650:                        if (!keepgoing) {
1.36    ! espie     651:                                if (!silent)
        !           652:                                        printf("\n");
1.33      espie     653:                                job->next = errorJobs;
                    654:                                errorJobs = job;
                    655:                                /* XXX don't free the command */
                    656:                                return;
                    657:                        }
                    658:                        printf(", line %lu of %s", job->location->lineno,
                    659:                            job->location->fname);
                    660:                        if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE))
                    661:                            == JOB_SILENT)
                    662:                                printf(": %s", job->cmd);
                    663:                        /* Abort the current target,
                    664:                         * but let others continue.  */
                    665:                        printf(" (continuing)\n");
                    666:                } else {
                    667:                        /* Continue executing commands for
                    668:                         * this target.  If we return 0,
                    669:                         * this will happen...  */
                    670:                        printf(" (ignored)\n");
                    671:                        job->exit_type = JOB_EXIT_OKAY;
                    672:                }
                    673:        }
                    674:        free(job->cmd);
                    675: }
                    676:
                    677: int
                    678: run_gnode(GNode *gn)
                    679: {
                    680:        if (!gn || (gn->type & OP_DUMMY))
                    681:                return NOSUCHNODE;
                    682:
                    683:        gn->built_status = MADE;
                    684:
                    685:        job_attach_node(&myjob, gn);
                    686:        while (myjob.exit_type == JOB_EXIT_OKAY) {
                    687:                bool finished = job_run_next(&myjob);
                    688:                if (finished)
                    689:                        break;
                    690:                handle_one_job(&myjob);
                    691:        }
                    692:
                    693:        return gn->built_status;
                    694: }
                    695:
                    696:
                    697: static void
                    698: setup_engine(void)
                    699: {
                    700:        static int already_setup = 0;
                    701:
                    702:        if (!already_setup) {
                    703:                setup_meta();
                    704:                already_setup = 1;
                    705:        }
                    706: }
                    707:
                    708: static bool
                    709: do_run_command(Job *job)
1.10      espie     710: {
                    711:        bool silent;    /* Don't print command */
                    712:        bool doExecute; /* Execute the command */
                    713:        bool errCheck;  /* Check errors */
1.33      espie     714:        pid_t cpid;     /* Child pid */
                    715:
                    716:        const char *cmd = job->cmd;
                    717:        silent = job->node->type & OP_SILENT;
                    718:        errCheck = !(job->node->type & OP_IGNORE);
                    719:        if (job->node->type & OP_MAKE)
                    720:                doExecute = true;
                    721:        else
                    722:                doExecute = !noExecute;
1.10      espie     723:
                    724:        /* How can we execute a null command ? we warn the user that the
                    725:         * command expanded to nothing (is this the right thing to do?).  */
1.19      espie     726:        if (*cmd == '\0') {
1.10      espie     727:                Error("%s expands to empty string", cmd);
1.33      espie     728:                return false;
1.28      espie     729:        }
1.10      espie     730:
                    731:        for (;; cmd++) {
                    732:                if (*cmd == '@')
                    733:                        silent = DEBUG(LOUD) ? false : true;
                    734:                else if (*cmd == '-')
                    735:                        errCheck = false;
                    736:                else if (*cmd == '+')
                    737:                        doExecute = true;
                    738:                else
                    739:                        break;
                    740:        }
                    741:        while (isspace(*cmd))
                    742:                cmd++;
1.33      espie     743:        /* Print the command before fork if make -n or !silent*/
                    744:        if ( noExecute || !silent)
1.10      espie     745:                printf("%s\n", cmd);
1.33      espie     746:
                    747:        if (silent)
                    748:                job->flags |= JOB_SILENT;
                    749:        else
                    750:                job->flags &= ~JOB_SILENT;
                    751:
1.10      espie     752:        /* If we're not supposed to execute any commands, this is as far as
                    753:         * we go...  */
                    754:        if (!doExecute)
1.33      espie     755:                return false;
                    756:        /* always flush for other stuff */
                    757:        fflush(stdout);
1.10      espie     758:
                    759:        /* Fork and execute the single command. If the fork fails, we abort.  */
                    760:        switch (cpid = fork()) {
                    761:        case -1:
1.33      espie     762:                Punt("Could not fork");
1.10      espie     763:                /*NOTREACHED*/
                    764:        case 0:
1.34      espie     765:                /* put a random delay unless we're the only job running
                    766:                 * and there's nothing left to do.
                    767:                 */
                    768:                if (random_delay)
                    769:                        if (!(runningJobs == NULL && no_jobs_left()))
                    770:                                usleep(random() % random_delay);
1.10      espie     771:                run_command(cmd, errCheck);
                    772:                /*NOTREACHED*/
                    773:        default:
1.33      espie     774:                job->pid = cpid;
                    775:                job->next = runningJobs;
                    776:                runningJobs = job;
                    777:                if (errCheck)
                    778:                        job->flags |= JOB_ERRCHECK;
                    779:                else
                    780:                        job->flags &= ~JOB_ERRCHECK;
                    781:                debug_job_printf("Running %ld (%s) %s\n", (long)job->pid,
                    782:                    job->node->name, (noExecute || !silent) ? "" : cmd);
                    783:                return true;
1.10      espie     784:        }
                    785: }
                    786:
1.33      espie     787: bool
                    788: job_run_next(Job *job)
1.10      espie     789: {
1.33      espie     790:        bool started;
                    791:        GNode *gn = job->node;
1.19      espie     792:
1.33      espie     793:        setup_engine();
                    794:        while (job->next_cmd != NULL) {
                    795:                struct command *command = Lst_Datum(job->next_cmd);
                    796:
                    797:                handle_all_signals();
                    798:                job->location = &command->location;
                    799:                Parse_SetLocation(job->location);
                    800:                job->cmd = Var_Subst(command->string, &gn->context, false);
                    801:                job->next_cmd = Lst_Adv(job->next_cmd);
1.30      espie     802:                if (fatal_errors)
1.33      espie     803:                        Punt(NULL);
                    804:                started = do_run_command(job);
                    805:                if (started)
                    806:                        return false;
                    807:                else
                    808:                        free(job->cmd);
1.19      espie     809:        }
1.33      espie     810:        job->exit_type = JOB_EXIT_OKAY;
                    811:        return true;
1.19      espie     812: }
                    813: