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

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