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

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