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

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