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

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