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

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