[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/main.c, Revision 1.97

1.97    ! espie       1: /*     $OpenBSD: main.c,v 1.96 2012/09/21 07:55:20 espie Exp $ */
1.12      millert     2: /*     $NetBSD: main.c,v 1.34 1997/03/24 20:56:36 gwr Exp $    */
1.1       deraadt     3:
                      4: /*
1.9       millert     5:  * Copyright (c) 1988, 1989, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  * Copyright (c) 1989 by Berkeley Softworks
                      8:  * All rights reserved.
                      9:  *
                     10:  * This code is derived from software contributed to Berkeley by
                     11:  * Adam de Boor.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
1.62      millert    21:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    22:  *    may be used to endorse or promote products derived from this software
                     23:  *    without specific prior written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     26:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     27:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     28:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     29:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     30:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     31:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     32:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     33:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     34:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     35:  * SUCH DAMAGE.
                     36:  */
                     37:
1.49      espie      38: #include <sys/param.h>
1.1       deraadt    39: #include <sys/types.h>
                     40: #include <sys/stat.h>
1.12      millert    41: #ifndef MAKE_BOOTSTRAP
1.1       deraadt    42: #include <sys/utsname.h>
1.9       millert    43: #endif
1.1       deraadt    44: #include <errno.h>
                     45: #include <stdio.h>
1.49      espie      46: #include <stdlib.h>
1.48      espie      47: #include <string.h>
                     48: #include <unistd.h>
                     49: #include "config.h"
                     50: #include "defines.h"
                     51: #include "var.h"
1.96      espie      52: #include "lowparse.h"
1.48      espie      53: #include "parse.h"
                     54: #include "parsevar.h"
1.1       deraadt    55: #include "dir.h"
1.78      espie      56: #include "direxpand.h"
1.48      espie      57: #include "error.h"
                     58: #include "pathnames.h"
                     59: #include "init.h"
1.1       deraadt    60: #include "job.h"
1.48      espie      61: #include "compat.h"
                     62: #include "targ.h"
                     63: #include "suff.h"
                     64: #include "str.h"
                     65: #include "main.h"
                     66: #include "lst.h"
                     67: #include "memory.h"
                     68: #include "make.h"
1.96      espie      69: #include "dump.h"
1.1       deraadt    70:
1.53      espie      71: #ifndef PATH_MAX
                     72: # ifdef MAXPATHLEN
                     73: #  define PATH_MAX (MAXPATHLEN+1)
                     74: # else
                     75: #  define PATH_MAX     1024
                     76: # endif
                     77: #endif
                     78:
1.84      espie      79:
1.46      espie      80: #define MAKEFLAGS      ".MAKEFLAGS"
1.1       deraadt    81:
1.48      espie      82: static LIST            to_create;      /* Targets to be made */
                     83: Lst create = &to_create;
                     84: bool           allPrecious;    /* .PRECIOUS given on line by itself */
1.1       deraadt    85:
1.48      espie      86: static bool            noBuiltins;     /* -r flag */
1.33      espie      87: static LIST            makefiles;      /* ordered list of makefiles to read */
1.48      espie      88: static LIST            varstoprint;    /* list of variables to print */
1.2       deraadt    89: int                    maxJobs;        /* -j argument */
1.48      espie      90: bool           compatMake;     /* -B argument */
1.86      espie      91: static bool            forceJobs = false;
1.63      espie      92: int            debug;          /* -d flag */
1.48      espie      93: bool           noExecute;      /* -n flag */
                     94: bool           keepgoing;      /* -k flag */
                     95: bool           queryFlag;      /* -q flag */
                     96: bool           touchFlag;      /* -t flag */
                     97: bool           ignoreErrors;   /* -i flag */
                     98: bool           beSilent;       /* -s flag */
1.96      espie      99: bool           dumpData;       /* -p flag */
1.1       deraadt   100:
1.75      espie     101: struct dirs {
                    102:        char *current;
                    103:        char *object;
                    104: };
                    105:
                    106: static void MainParseArgs(int, char **);
                    107: static void add_dirpath(Lst, const char *);
                    108: static void usage(void);
                    109: static void posixParseOptLetter(int);
                    110: static void record_option(int, const char *);
                    111:
                    112: static char *figure_out_MACHINE(void);
                    113: static char *figure_out_MACHINE_ARCH(void);
1.93      miod      114: static char *figure_out_MACHINE_CPU(void);
1.75      espie     115:
                    116: static char *chdir_verify_path(const char *, struct dirs *);
                    117: static char *concat_verify(const char *, const char *, char, struct dirs *);
                    118: static char *figure_out_CURDIR(void);
                    119: static void setup_CURDIR_OBJDIR(struct dirs *, const char *);
                    120:
                    121: static void setup_VPATH(void);
                    122:
1.89      espie     123: static void read_all_make_rules(bool, bool, Lst, struct dirs *);
1.75      espie     124: static void read_makefile_list(Lst, struct dirs *);
                    125: static int ReadMakefile(void *, void *);
1.1       deraadt   126:
1.64      espie     127: static void record_option(int c, const char *arg)
1.48      espie     128: {
                    129:     char opt[3];
                    130:
1.75      espie     131:        opt[0] = '-';
                    132:        opt[1] = c;
                    133:        opt[2] = '\0';
1.76      espie     134:        Var_Append(MAKEFLAGS, opt);
1.75      espie     135:        if (arg != NULL)
1.76      espie     136:                Var_Append(MAKEFLAGS, arg);
1.48      espie     137: }
                    138:
1.39      espie     139: static void
1.64      espie     140: posixParseOptLetter(int c)
1.39      espie     141: {
                    142:        switch(c) {
                    143:        case 'B':
1.48      espie     144:                compatMake = true;
                    145:                return; /* XXX don't pass to submakes. */
1.39      espie     146:        case 'P':
1.82      espie     147:                break;  /* old option */
1.39      espie     148:        case 'S':
1.48      espie     149:                keepgoing = false;
1.39      espie     150:                break;
                    151:        case 'e':
1.70      espie     152:                Var_setCheckEnvFirst(true);
1.39      espie     153:                break;
                    154:        case 'i':
1.48      espie     155:                ignoreErrors = true;
1.39      espie     156:                break;
                    157:        case 'k':
1.48      espie     158:                keepgoing = true;
1.39      espie     159:                break;
                    160:        case 'n':
1.48      espie     161:                noExecute = true;
1.39      espie     162:                break;
1.96      espie     163:        case 'p':
                    164:                dumpData = true;
                    165:                break;
1.39      espie     166:        case 'q':
1.48      espie     167:                queryFlag = true;
1.39      espie     168:                /* Kind of nonsensical, wot? */
                    169:                break;
                    170:        case 'r':
1.48      espie     171:                noBuiltins = true;
1.39      espie     172:                break;
                    173:        case 's':
1.48      espie     174:                beSilent = true;
1.39      espie     175:                break;
                    176:        case 't':
1.48      espie     177:                touchFlag = true;
1.39      espie     178:                break;
                    179:        default:
                    180:        case '?':
                    181:                usage();
                    182:        }
1.48      espie     183:        record_option(c, NULL);
1.39      espie     184: }
                    185:
1.1       deraadt   186: /*-
                    187:  * MainParseArgs --
                    188:  *     Parse a given argument vector. Called from main() and from
                    189:  *     Main_ParseArgLine() when the .MAKEFLAGS target is used.
                    190:  *
                    191:  *     XXX: Deal with command line overriding .MAKEFLAGS in makefile
                    192:  *
                    193:  * Side Effects:
                    194:  *     Various global and local flags will be set depending on the flags
                    195:  *     given
                    196:  */
                    197: static void
1.64      espie     198: MainParseArgs(int argc, char **argv)
1.1       deraadt   199: {
1.61      millert   200:        int c, optend;
1.1       deraadt   201:
1.96      espie     202: #define OPTFLAGS "BD:I:PSV:d:ef:ij:km:npqrst"
                    203: #define OPTLETTERS "BPSiknpqrst"
1.58      millert   204:
1.1       deraadt   205:        optind = 1;     /* since we're called more than once */
1.58      millert   206:        optreset = 1;
1.61      millert   207:        optend = 0;
1.58      millert   208:        while (optind < argc) {
1.61      millert   209:                if (!optend && argv[optind][0] == '-') {
                    210:                        if (argv[optind][1] == '\0')
                    211:                                optind++;       /* ignore "-" */
                    212:                        else if (argv[optind][1] == '-' &&
                    213:                            argv[optind][2] == '\0') {
                    214:                                optind++;       /* ignore "--" */
                    215:                                optend++;       /* "--" denotes end of flags */
                    216:                        }
                    217:                }
                    218:                c = optend ? -1 : getopt(argc, argv, OPTFLAGS);
                    219:                switch (c) {
1.1       deraadt   220:                case 'D':
1.76      espie     221:                        Var_Set(optarg, "1");
1.48      espie     222:                        record_option(c, optarg);
1.1       deraadt   223:                        break;
                    224:                case 'I':
                    225:                        Parse_AddIncludeDir(optarg);
1.48      espie     226:                        record_option(c, optarg);
1.1       deraadt   227:                        break;
1.9       millert   228:                case 'V':
1.48      espie     229:                        Lst_AtEnd(&varstoprint, optarg);
                    230:                        record_option(c, optarg);
1.9       millert   231:                        break;
1.1       deraadt   232:                case 'd': {
                    233:                        char *modules = optarg;
                    234:
                    235:                        for (; *modules; ++modules)
                    236:                                switch (*modules) {
                    237:                                case 'A':
                    238:                                        debug = ~0;
                    239:                                        break;
                    240:                                case 'a':
                    241:                                        debug |= DEBUG_ARCH;
                    242:                                        break;
                    243:                                case 'c':
                    244:                                        debug |= DEBUG_COND;
                    245:                                        break;
                    246:                                case 'd':
                    247:                                        debug |= DEBUG_DIR;
                    248:                                        break;
1.96      espie     249:                                case 'e':
                    250:                                        debug |= DEBUG_EXPENSIVE;
                    251:                                        break;
1.1       deraadt   252:                                case 'f':
                    253:                                        debug |= DEBUG_FOR;
                    254:                                        break;
                    255:                                case 'g':
                    256:                                        if (modules[1] == '1') {
                    257:                                                debug |= DEBUG_GRAPH1;
                    258:                                                ++modules;
                    259:                                        }
                    260:                                        else if (modules[1] == '2') {
                    261:                                                debug |= DEBUG_GRAPH2;
                    262:                                                ++modules;
                    263:                                        }
                    264:                                        break;
                    265:                                case 'j':
                    266:                                        debug |= DEBUG_JOB;
                    267:                                        break;
1.87      espie     268:                                case 'J':
1.96      espie     269:                                        /* ignore */
1.87      espie     270:                                        break;
1.46      espie     271:                                case 'l':
                    272:                                        debug |= DEBUG_LOUD;
                    273:                                        break;
1.1       deraadt   274:                                case 'm':
                    275:                                        debug |= DEBUG_MAKE;
1.92      espie     276:                                        break;
                    277:                                case 'n':
                    278:                                        debug |= DEBUG_NAME_MATCHING;
1.1       deraadt   279:                                        break;
1.90      espie     280:                                case 'p':
                    281:                                        debug |= DEBUG_PARALLEL;
                    282:                                        break;
1.96      espie     283:                                case 'q':
                    284:                                        debug |= DEBUG_QUICKDEATH;
                    285:                                        break;
1.1       deraadt   286:                                case 's':
                    287:                                        debug |= DEBUG_SUFF;
                    288:                                        break;
                    289:                                case 't':
                    290:                                        debug |= DEBUG_TARG;
                    291:                                        break;
                    292:                                case 'v':
                    293:                                        debug |= DEBUG_VAR;
                    294:                                        break;
                    295:                                default:
                    296:                                        (void)fprintf(stderr,
1.67      jmc       297:                                "make: illegal argument to -d option -- %c\n",
1.1       deraadt   298:                                            *modules);
                    299:                                        usage();
                    300:                                }
1.48      espie     301:                        record_option(c, optarg);
1.1       deraadt   302:                        break;
                    303:                }
                    304:                case 'f':
1.33      espie     305:                        Lst_AtEnd(&makefiles, optarg);
1.1       deraadt   306:                        break;
1.15      espie     307:                case 'j': {
                    308:                   char *endptr;
                    309:
1.48      espie     310:                        forceJobs = true;
1.15      espie     311:                        maxJobs = strtol(optarg, &endptr, 0);
                    312:                        if (endptr == optarg) {
                    313:                                fprintf(stderr,
                    314:                                        "make: illegal argument to -j option -- %s -- not a number\n",
                    315:                                        optarg);
                    316:                                usage();
                    317:                        }
1.48      espie     318:                        record_option(c, optarg);
1.1       deraadt   319:                        break;
1.15      espie     320:                }
1.5       niklas    321:                case 'm':
1.81      espie     322:                        Dir_AddDir(systemIncludePath, optarg);
1.48      espie     323:                        record_option(c, optarg);
1.5       niklas    324:                        break;
1.58      millert   325:                case -1:
                    326:                        /* Check for variable assignments and targets. */
1.59      millert   327:                        if (argv[optind] != NULL &&
1.70      espie     328:                            !Parse_CmdlineVar(argv[optind])) {
1.58      millert   329:                                if (!*argv[optind])
                    330:                                        Punt("illegal (null) argument.");
1.61      millert   331:                                Lst_AtEnd(create, estrdup(argv[optind]));
1.58      millert   332:                        }
                    333:                        optind++;       /* skip over non-option */
                    334:                        break;
1.1       deraadt   335:                default:
1.39      espie     336:                        posixParseOptLetter(c);
1.1       deraadt   337:                }
                    338:        }
                    339:
                    340: }
                    341:
                    342: /*-
                    343:  * Main_ParseArgLine --
1.46      espie     344:  *     Used by the parse module when a .MFLAGS or .MAKEFLAGS target
1.1       deraadt   345:  *     is encountered and by main() when reading the .MAKEFLAGS envariable.
                    346:  *     Takes a line of arguments and breaks it into its
1.46      espie     347:  *     component words and passes those words and the number of them to the
1.1       deraadt   348:  *     MainParseArgs function.
                    349:  *     The line should have all its leading whitespace removed.
                    350:  *
                    351:  * Side Effects:
                    352:  *     Only those that come from the various arguments.
                    353:  */
                    354: void
1.64      espie     355: Main_ParseArgLine(const char *line)    /* Line to fracture */
1.1       deraadt   356: {
                    357:        char **argv;                    /* Manufactured argument vector */
                    358:        int argc;                       /* Number of arguments in argv */
1.14      espie     359:        char *args;                     /* Space used by the args */
1.19      espie     360:        char *buf;
1.39      espie     361:        char *argv0;
1.48      espie     362:        const char *s;
1.60      espie     363:        size_t len;
1.1       deraadt   364:
1.46      espie     365:
1.1       deraadt   366:        if (line == NULL)
                    367:                return;
                    368:        for (; *line == ' '; ++line)
                    369:                continue;
                    370:        if (!*line)
                    371:                return;
                    372:
1.39      espie     373:        /* POSIX rule: MAKEFLAGS can hold a set of option letters without
                    374:         * any blanks or dashes. */
                    375:        for (s = line;; s++) {
                    376:                if (*s == '\0') {
1.46      espie     377:                        while (line != s)
1.39      espie     378:                                posixParseOptLetter(*line++);
                    379:                        return;
1.46      espie     380:                }
1.39      espie     381:                if (strchr(OPTLETTERS, *s) == NULL)
                    382:                        break;
                    383:        }
1.46      espie     384:        argv0 = Var_Value(".MAKE");
1.60      espie     385:        len = strlen(line) + strlen(argv0) + 2;
                    386:        buf = emalloc(len);
                    387:        (void)snprintf(buf, len, "%s %s", argv0, line);
1.14      espie     388:
1.46      espie     389:        argv = brk_string(buf, &argc, &args);
1.14      espie     390:        free(buf);
1.1       deraadt   391:        MainParseArgs(argc, argv);
1.14      espie     392:
                    393:        free(args);
                    394:        free(argv);
1.1       deraadt   395: }
                    396:
1.75      espie     397: /* Add a :-separated path to a Lst of directories.  */
                    398: static void
                    399: add_dirpath(Lst l, const char *n)
1.9       millert   400: {
1.75      espie     401:        const char *start;
                    402:        const char *cp;
1.9       millert   403:
1.75      espie     404:        for (start = n;;) {
                    405:                for (cp = start; *cp != '\0' && *cp != ':';)
                    406:                        cp++;
                    407:                Dir_AddDiri(l, start, cp);
                    408:                if (*cp == '\0')
                    409:                        break;
                    410:                else
                    411:                        start= cp+1;
1.9       millert   412:        }
                    413: }
                    414:
1.75      espie     415: /*
1.80      espie     416:  * Get the name of this type of MACHINE from utsname so we can share an
1.75      espie     417:  * executable for similar machines. (i.e. m68k: amiga hp300, mac68k, sun3, ...)
                    418:  *
1.93      miod      419:  * Note that MACHINE, MACHINE_ARCH and MACHINE_CPU are decided at
1.75      espie     420:  * run-time.
                    421:  */
                    422: static char *
                    423: figure_out_MACHINE()
1.42      espie     424: {
1.75      espie     425:        char *r = getenv("MACHINE");
                    426:        if (r == NULL) {
                    427: #ifndef MAKE_BOOTSTRAP
                    428:                static struct utsname utsname;
1.42      espie     429:
1.75      espie     430:                if (uname(&utsname) == -1) {
                    431:                        perror("make: uname");
                    432:                        exit(2);
                    433:                }
                    434:                r = utsname.machine;
                    435: #else
                    436:                r = MACHINE;
                    437: #endif
                    438:        }
                    439:        return r;
1.42      espie     440: }
                    441:
1.75      espie     442: static char *
                    443: figure_out_MACHINE_ARCH()
1.1       deraadt   444: {
1.75      espie     445:        char *r = getenv("MACHINE_ARCH");
                    446:        if (r == NULL) {
                    447: #ifndef MACHINE_ARCH
                    448:                r = "unknown";  /* XXX: no uname -p yet */
                    449: #else
                    450:                r = MACHINE_ARCH;
                    451: #endif
                    452:        }
                    453:        return r;
                    454: }
1.93      miod      455: static char *
                    456: figure_out_MACHINE_CPU()
                    457: {
                    458:        char *r = getenv("MACHINE_CPU");
                    459:        if (r == NULL) {
                    460: #if !defined(MACHINE_CPU) && ! defined(MACHINE_ARCH)
                    461:                r = "unknown";  /* XXX: no uname -p yet */
                    462: #else
                    463: #if defined(MACHINE_CPU)
                    464:                r = MACHINE_CPU;
                    465: #else
                    466:                r = MACHINE_ARCH;
                    467: #endif
                    468: #endif
                    469:        }
                    470:        return r;
                    471: }
1.46      espie     472:
1.75      espie     473: static char *
                    474: figure_out_CURDIR()
                    475: {
                    476:        char *dir, *cwd;
                    477:        struct stat sa, sb;
                    478:
                    479:        /* curdir is cwd... */
                    480:        cwd = dogetcwd();
                    481:        if (cwd == NULL) {
1.1       deraadt   482:                (void)fprintf(stderr, "make: %s.\n", strerror(errno));
                    483:                exit(2);
                    484:        }
                    485:
1.75      espie     486:        if (stat(cwd, &sa) == -1) {
                    487:                (void)fprintf(stderr, "make: %s: %s.\n", cwd, strerror(errno));
                    488:                exit(2);
1.1       deraadt   489:        }
                    490:
1.80      espie     491:        /* ...but we can use the alias $PWD if we can prove it is the same
1.75      espie     492:         * directory */
                    493:        if ((dir = getenv("PWD")) != NULL) {
                    494:                if (stat(dir, &sb) == 0 && sa.st_ino == sb.st_ino &&
1.91      espie     495:                    sa.st_dev == sb.st_dev) {
1.75      espie     496:                        free(cwd);
                    497:                        return estrdup(dir);
1.91      espie     498:                }
1.1       deraadt   499:        }
                    500:
1.75      espie     501:        return cwd;
                    502: }
                    503:
                    504: static char *
                    505: chdir_verify_path(const char *path, struct dirs *d)
                    506: {
                    507:        struct stat sb;
1.9       millert   508:
1.75      espie     509:        if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
                    510:                if (chdir(path)) {
                    511:                        (void)fprintf(stderr, "make warning: %s: %s.\n",
                    512:                              path, strerror(errno));
                    513:                        return NULL;
                    514:                } else {
                    515:                        if (path[0] != '/')
                    516:                                return Str_concat(d->current, path, '/');
                    517:                        else
                    518:                                return estrdup(path);
                    519:                }
1.1       deraadt   520:        }
                    521:
1.75      espie     522:        return NULL;
                    523: }
                    524:
                    525: static char *
                    526: concat_verify(const char *p1, const char *p2, char c, struct dirs *d)
                    527: {
                    528:        char *tmp = Str_concat(p1, p2, c);
                    529:        char *result = chdir_verify_path(tmp, d);
                    530:        free(tmp);
                    531:        return result;
                    532: }
                    533:
                    534: static void
                    535: setup_CURDIR_OBJDIR(struct dirs *d, const char *machine)
                    536: {
                    537:        char *path, *prefix;
1.12      millert   538:
1.75      espie     539:        d->current = figure_out_CURDIR();
                    540:        d->object = NULL;
1.1       deraadt   541:        /*
1.9       millert   542:         * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
                    543:         * exists, change into it and build there.  (If a .${MACHINE} suffix
                    544:         * exists, use that directory instead).
                    545:         * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
                    546:         * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
                    547:         * If all fails, use the current directory to build.
                    548:         *
                    549:         * Once things are initted,
                    550:         * have to add the original directory to the search path,
1.66      jolan     551:         * and modify the paths for the Makefiles appropriately.  The
1.1       deraadt   552:         * current directory is also placed as a variable for make scripts.
                    553:         */
1.75      espie     554:        if ((prefix = getenv("MAKEOBJDIRPREFIX")) != NULL) {
                    555:                d->object = concat_verify(prefix, d->current, 0, d);
                    556:        } else if ((path = getenv("MAKEOBJDIR")) != NULL) {
                    557:                d->object = chdir_verify_path(path, d);
                    558:        } else {
                    559:                path = _PATH_OBJDIR;
                    560:                prefix = _PATH_OBJDIRPREFIX;
                    561:                d->object = concat_verify(path, machine, '.', d);
                    562:                if (!d->object)
                    563:                        d->object=chdir_verify_path(path, d);
                    564:                if (!d->object)
                    565:                        d->object = concat_verify(prefix, d->current, 0, d);
                    566:        }
                    567:        if (d->object == NULL)
                    568:                d->object = d->current;
                    569: }
                    570:
                    571: /*
                    572:  * if the VPATH variable is defined, add its contents to the search path.
                    573:  * Uses the same format as the PATH env variable, i.e.,
                    574:  * <directory>:<directory>:<directory>...
                    575:  */
                    576: static void
                    577: setup_VPATH()
                    578: {
                    579:        if (Var_Value("VPATH") != NULL) {
                    580:                char *vpath;
                    581:
                    582:                vpath = Var_Subst("${VPATH}", NULL, false);
1.79      espie     583:                add_dirpath(defaultPath, vpath);
1.75      espie     584:                (void)free(vpath);
1.1       deraadt   585:        }
1.75      espie     586: }
                    587:
                    588: static void
                    589: read_makefile_list(Lst mk, struct dirs *d)
                    590: {
                    591:        LstNode ln;
                    592:        ln = Lst_Find(mk, ReadMakefile, d);
                    593:        if (ln != NULL)
                    594:                Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
                    595: }
                    596:
                    597: static void
1.89      espie     598: read_all_make_rules(bool noBuiltins, bool read_depend,
                    599:     Lst makefiles, struct dirs *d)
1.75      espie     600: {
                    601:        /*
                    602:         * Read in the built-in rules first, followed by the specified
                    603:         * makefile(s), or the default BSDmakefile, Makefile or
                    604:         * makefile, in that order.
                    605:         */
                    606:        if (!noBuiltins) {
                    607:                LIST sysMkPath;                 /* Path of sys.mk */
                    608:
                    609:                Lst_Init(&sysMkPath);
1.81      espie     610:                Dir_Expand(_PATH_DEFSYSMK, systemIncludePath, &sysMkPath);
1.75      espie     611:                if (Lst_IsEmpty(&sysMkPath))
                    612:                        Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
                    613:
                    614:                read_makefile_list(&sysMkPath, d);
1.1       deraadt   615:        }
                    616:
1.75      espie     617:        if (!Lst_IsEmpty(makefiles)) {
                    618:                read_makefile_list(makefiles, d);
                    619:        } else if (!ReadMakefile("BSDmakefile", d))
                    620:                if (!ReadMakefile("makefile", d))
                    621:                        (void)ReadMakefile("Makefile", d);
                    622:
1.89      espie     623:        /* read a .depend file, if it exists, and we're not building depend */
1.94      espie     624:
1.89      espie     625:        if (read_depend)
                    626:                (void)ReadMakefile(".depend", d);
1.75      espie     627: }
                    628:
                    629:
                    630: int main(int, char **);
                    631: /*-
                    632:  * main --
                    633:  *     The main function, for obvious reasons. Initializes variables
                    634:  *     and a few modules, then parses the arguments give it in the
                    635:  *     environment and on the command line. Reads the system makefile
                    636:  *     followed by either Makefile, makefile or the file given by the
                    637:  *     -f argument. Sets the .MAKEFLAGS PMake variable based on all the
                    638:  *     flags it has received by then uses either the Make or the Compat
                    639:  *     module to create the initial list of targets.
                    640:  *
                    641:  * Results:
                    642:  *     If -q was given, exits -1 if anything was out-of-date. Else it exits
                    643:  *     0.
                    644:  *
                    645:  * Side Effects:
                    646:  *     The program exits when done. Targets are created. etc. etc. etc.
                    647:  */
                    648: int
                    649: main(int argc, char **argv)
                    650: {
                    651:        static LIST targs;      /* target nodes to create */
                    652:        bool outOfDate = true;  /* false if all targets up to date */
                    653:        char *machine = figure_out_MACHINE();
                    654:        char *machine_arch = figure_out_MACHINE_ARCH();
1.93      miod      655:        char *machine_cpu = figure_out_MACHINE_CPU();
1.75      espie     656:        const char *syspath = _PATH_DEFSYSPATH;
                    657:        char *p;
                    658:        static struct dirs d;
1.89      espie     659:        bool read_depend = true;/* false if we don't want to read .depend */
1.75      espie     660:
                    661:        setup_CURDIR_OBJDIR(&d, machine);
                    662:
                    663:        esetenv("PWD", d.object);
1.30      espie     664:        unsetenv("CDPATH");
1.1       deraadt   665:
1.56      espie     666:        Static_Lst_Init(create);
                    667:        Static_Lst_Init(&makefiles);
                    668:        Static_Lst_Init(&varstoprint);
                    669:        Static_Lst_Init(&targs);
1.52      espie     670:
1.48      espie     671:        beSilent = false;               /* Print commands as executed */
                    672:        ignoreErrors = false;           /* Pay attention to non-zero returns */
                    673:        noExecute = false;              /* Execute all commands */
                    674:        keepgoing = false;              /* Stop on error */
                    675:        allPrecious = false;            /* Remove targets when interrupted */
                    676:        queryFlag = false;              /* This is not just a check-run */
                    677:        noBuiltins = false;             /* Read the built-in rules */
                    678:        touchFlag = false;              /* Actually update targets */
1.1       deraadt   679:        debug = 0;                      /* No debug verbosity, please. */
                    680:
1.85      espie     681:        maxJobs = DEFMAXJOBS;
1.48      espie     682:        compatMake = false;             /* No compat mode */
1.9       millert   683:
1.1       deraadt   684:
                    685:        /*
1.48      espie     686:         * Initialize all external modules.
1.1       deraadt   687:         */
1.48      espie     688:        Init();
                    689:
1.75      espie     690:        if (d.object != d.current)
1.79      espie     691:                Dir_AddDir(defaultPath, d.current);
1.76      espie     692:        Var_Set(".CURDIR", d.current);
                    693:        Var_Set(".OBJDIR", d.object);
1.96      espie     694:        Parse_setcurdir(d.current);
1.87      espie     695:        Targ_setdirs(d.current, d.object);
1.1       deraadt   696:
                    697:        /*
                    698:         * Initialize various variables.
                    699:         *      MAKE also gets this name, for compatibility
                    700:         *      .MAKEFLAGS gets set to the empty string just in case.
                    701:         *      MFLAGS also gets initialized empty, for compatibility.
                    702:         */
1.76      espie     703:        Var_Set("MAKE", argv[0]);
                    704:        Var_Set(".MAKE", argv[0]);
                    705:        Var_Set(MAKEFLAGS, "");
                    706:        Var_Set("MFLAGS", "");
                    707:        Var_Set("MACHINE", machine);
                    708:        Var_Set("MACHINE_ARCH", machine_arch);
1.93      miod      709:        Var_Set("MACHINE_CPU", machine_cpu);
1.1       deraadt   710:
                    711:        /*
1.46      espie     712:         * First snag any flags out of the MAKEFLAGS environment variable.
1.1       deraadt   713:         */
                    714:        Main_ParseArgLine(getenv("MAKEFLAGS"));
1.9       millert   715:
1.1       deraadt   716:        MainParseArgs(argc, argv);
1.86      espie     717:
                    718:        /*
1.97    ! espie     719:         * Be compatible if user did not specify -j
1.86      espie     720:         */
1.97    ! espie     721:        if (!forceJobs)
1.86      espie     722:                compatMake = true;
1.30      espie     723:
1.46      espie     724:        /* And set up everything for sub-makes */
1.39      espie     725:        Var_AddCmdline(MAKEFLAGS);
1.46      espie     726:
1.1       deraadt   727:
                    728:        /*
                    729:         * Set up the .TARGETS variable to contain the list of targets to be
                    730:         * created. If none specified, make the variable empty -- the parser
                    731:         * will fill the thing in with the default or .MAIN target.
                    732:         */
1.48      espie     733:        if (!Lst_IsEmpty(create)) {
1.1       deraadt   734:                LstNode ln;
                    735:
1.48      espie     736:                for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) {
1.1       deraadt   737:                        char *name = (char *)Lst_Datum(ln);
                    738:
1.89      espie     739:                        if (strcmp(name, "depend") == 0)
                    740:                                read_depend = false;
                    741:
1.76      espie     742:                        Var_Append(".TARGETS", name);
1.1       deraadt   743:                }
                    744:        } else
1.76      espie     745:                Var_Set(".TARGETS", "");
1.1       deraadt   746:
1.5       niklas    747:
                    748:        /*
                    749:         * If no user-supplied system path was given (through the -m option)
                    750:         * add the directories from the DEFSYSPATH (more than one may be given
                    751:         * as dir1:...:dirn) to the system include path.
                    752:         */
1.81      espie     753:        if (Lst_IsEmpty(systemIncludePath))
                    754:            add_dirpath(systemIncludePath, syspath);
1.5       niklas    755:
1.89      espie     756:        read_all_make_rules(noBuiltins, read_depend, &makefiles, &d);
1.1       deraadt   757:
1.76      espie     758:        Var_Append("MFLAGS", Var_Value(MAKEFLAGS));
1.1       deraadt   759:
1.46      espie     760:        /* Install all the flags into the MAKEFLAGS env variable. */
                    761:        if (((p = Var_Value(MAKEFLAGS)) != NULL) && *p)
1.44      espie     762:                esetenv("MAKEFLAGS", p);
1.1       deraadt   763:
1.75      espie     764:        setup_VPATH();
1.1       deraadt   765:
1.83      espie     766:        process_suffixes_after_makefile_is_read();
1.1       deraadt   767:
1.96      espie     768:        if (dumpData) {
                    769:                dump_data();
                    770:                exit(0);
                    771:        }
1.97    ! espie     772:
        !           773:        /* Print the initial graph, if the user requested it.  */
        !           774:        if (DEBUG(GRAPH1))
        !           775:                dump_data();
        !           776:
1.46      espie     777:        /* Print the values of any variables requested by the user.  */
1.48      espie     778:        if (!Lst_IsEmpty(&varstoprint)) {
1.75      espie     779:                LstNode ln;
1.9       millert   780:
1.80      espie     781:                for (ln = Lst_First(&varstoprint); ln != NULL;
1.75      espie     782:                    ln = Lst_Adv(ln)) {
                    783:                        char *value = Var_Value((char *)Lst_Datum(ln));
1.9       millert   784:
1.75      espie     785:                        printf("%s\n", value ? value : "");
                    786:                }
1.48      espie     787:        } else {
1.80      espie     788:                /* Have now read the entire graph and need to make a list
                    789:                 * of targets to create. If none was given on the command
                    790:                 * line, we consult the parsing module to find the main
1.75      espie     791:                 * target(s) to create.  */
                    792:                if (Lst_IsEmpty(create))
                    793:                        Parse_MainName(&targs);
                    794:                else
                    795:                        Targ_FindList(&targs, create);
                    796:
1.96      espie     797:                Job_Init(maxJobs);
                    798:                /* If the user has defined a .BEGIN target, execute the commands
                    799:                 * attached to it.  */
                    800:                if (!queryFlag)
                    801:                        Job_Begin();
1.75      espie     802:                if (compatMake)
1.80      espie     803:                        /* Compat_Init will take care of creating all the
1.75      espie     804:                         * targets as well as initializing the module.  */
                    805:                        Compat_Run(&targs);
                    806:                else {
                    807:                        /* Traverse the graph, checking on all the targets.  */
                    808:                        outOfDate = Make_Run(&targs);
1.1       deraadt   809:                }
1.9       millert   810:        }
                    811:
1.1       deraadt   812:        /* print the graph now it's been processed if the user requested it */
                    813:        if (DEBUG(GRAPH2))
1.97    ! espie     814:                post_mortem();
1.1       deraadt   815:
                    816:        if (queryFlag && outOfDate)
1.46      espie     817:                return 1;
1.1       deraadt   818:        else
1.46      espie     819:                return 0;
1.1       deraadt   820: }
                    821:
                    822: /*-
                    823:  * ReadMakefile  --
                    824:  *     Open and parse the given makefile.
                    825:  *
                    826:  * Results:
1.48      espie     827:  *     true if ok. false if couldn't open file.
1.1       deraadt   828:  *
                    829:  * Side Effects:
                    830:  *     lots
                    831:  */
1.48      espie     832: static bool
1.75      espie     833: ReadMakefile(void *p, void *q)
1.1       deraadt   834: {
1.64      espie     835:        char *fname = (char *)p;        /* makefile to read */
1.75      espie     836:        struct dirs *d = (struct dirs *)q;
1.1       deraadt   837:        FILE *stream;
1.53      espie     838:        char *name;
1.1       deraadt   839:
                    840:        if (!strcmp(fname, "-")) {
1.76      espie     841:                Var_Set("MAKEFILE", "");
1.37      espie     842:                Parse_File(estrdup("(stdin)"), stdin);
1.1       deraadt   843:        } else {
                    844:                if ((stream = fopen(fname, "r")) != NULL)
                    845:                        goto found;
                    846:                /* if we've chdir'd, rebuild the path name */
1.75      espie     847:                if (d->current != d->object && *fname != '/') {
1.53      espie     848:                        char *path;
                    849:
1.75      espie     850:                        path = Str_concat(d->current, fname, '/');
1.53      espie     851:                        if ((stream = fopen(path, "r")) == NULL)
1.75      espie     852:                                free(path);
1.53      espie     853:                        else {
1.75      espie     854:                                fname = path;
                    855:                                goto found;
1.1       deraadt   856:                        }
                    857:                }
                    858:                /* look in -I and system include directories. */
1.81      espie     859:                name = Dir_FindFile(fname, userIncludePath);
1.1       deraadt   860:                if (!name)
1.81      espie     861:                        name = Dir_FindFile(fname, systemIncludePath);
1.1       deraadt   862:                if (!name || !(stream = fopen(name, "r")))
1.48      espie     863:                        return false;
1.1       deraadt   864:                fname = name;
                    865:                /*
                    866:                 * set the MAKEFILE variable desired by System V fans -- the
                    867:                 * placement of the setting here means it gets set to the last
                    868:                 * makefile specified, as it is set by SysV make.
                    869:                 */
1.76      espie     870: found:         Var_Set("MAKEFILE", fname);
1.1       deraadt   871:                Parse_File(fname, stream);
                    872:        }
1.48      espie     873:        return true;
1.1       deraadt   874: }
                    875:
1.46      espie     876:
1.1       deraadt   877: /*
                    878:  * usage --
                    879:  *     exit with usage message
                    880:  */
                    881: static void
                    882: usage()
                    883: {
                    884:        (void)fprintf(stderr,
1.65      jmc       885: "usage: make [-BeiknPqrSst] [-D variable] [-d flags] [-f makefile]\n\
1.46      espie     886:            [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\
1.65      jmc       887:            [NAME=value] [target ...]\n");
1.1       deraadt   888:        exit(2);
                    889: }
                    890:
                    891: