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

1.98    ! espie       1: /*     $OpenBSD: main.c,v 1.97 2012/10/02 10:29:31 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':
1.98    ! espie     266:                                        debug |= DEBUG_JOB | DEBUG_KILL;
1.1       deraadt   267:                                        break;
1.87      espie     268:                                case 'J':
1.96      espie     269:                                        /* ignore */
1.98    ! espie     270:                                        break;
        !           271:                                case 'k':
        !           272:                                        debug |= DEBUG_KILL;
1.87      espie     273:                                        break;
1.46      espie     274:                                case 'l':
                    275:                                        debug |= DEBUG_LOUD;
                    276:                                        break;
1.1       deraadt   277:                                case 'm':
                    278:                                        debug |= DEBUG_MAKE;
1.92      espie     279:                                        break;
                    280:                                case 'n':
                    281:                                        debug |= DEBUG_NAME_MATCHING;
1.1       deraadt   282:                                        break;
1.90      espie     283:                                case 'p':
                    284:                                        debug |= DEBUG_PARALLEL;
                    285:                                        break;
1.96      espie     286:                                case 'q':
                    287:                                        debug |= DEBUG_QUICKDEATH;
                    288:                                        break;
1.1       deraadt   289:                                case 's':
                    290:                                        debug |= DEBUG_SUFF;
                    291:                                        break;
                    292:                                case 't':
                    293:                                        debug |= DEBUG_TARG;
                    294:                                        break;
                    295:                                case 'v':
                    296:                                        debug |= DEBUG_VAR;
                    297:                                        break;
                    298:                                default:
                    299:                                        (void)fprintf(stderr,
1.67      jmc       300:                                "make: illegal argument to -d option -- %c\n",
1.1       deraadt   301:                                            *modules);
                    302:                                        usage();
                    303:                                }
1.48      espie     304:                        record_option(c, optarg);
1.1       deraadt   305:                        break;
                    306:                }
                    307:                case 'f':
1.33      espie     308:                        Lst_AtEnd(&makefiles, optarg);
1.1       deraadt   309:                        break;
1.15      espie     310:                case 'j': {
                    311:                   char *endptr;
                    312:
1.48      espie     313:                        forceJobs = true;
1.15      espie     314:                        maxJobs = strtol(optarg, &endptr, 0);
                    315:                        if (endptr == optarg) {
                    316:                                fprintf(stderr,
                    317:                                        "make: illegal argument to -j option -- %s -- not a number\n",
                    318:                                        optarg);
                    319:                                usage();
                    320:                        }
1.48      espie     321:                        record_option(c, optarg);
1.1       deraadt   322:                        break;
1.15      espie     323:                }
1.5       niklas    324:                case 'm':
1.81      espie     325:                        Dir_AddDir(systemIncludePath, optarg);
1.48      espie     326:                        record_option(c, optarg);
1.5       niklas    327:                        break;
1.58      millert   328:                case -1:
                    329:                        /* Check for variable assignments and targets. */
1.59      millert   330:                        if (argv[optind] != NULL &&
1.70      espie     331:                            !Parse_CmdlineVar(argv[optind])) {
1.58      millert   332:                                if (!*argv[optind])
                    333:                                        Punt("illegal (null) argument.");
1.61      millert   334:                                Lst_AtEnd(create, estrdup(argv[optind]));
1.58      millert   335:                        }
                    336:                        optind++;       /* skip over non-option */
                    337:                        break;
1.1       deraadt   338:                default:
1.39      espie     339:                        posixParseOptLetter(c);
1.1       deraadt   340:                }
                    341:        }
                    342:
                    343: }
                    344:
                    345: /*-
                    346:  * Main_ParseArgLine --
1.46      espie     347:  *     Used by the parse module when a .MFLAGS or .MAKEFLAGS target
1.1       deraadt   348:  *     is encountered and by main() when reading the .MAKEFLAGS envariable.
                    349:  *     Takes a line of arguments and breaks it into its
1.46      espie     350:  *     component words and passes those words and the number of them to the
1.1       deraadt   351:  *     MainParseArgs function.
                    352:  *     The line should have all its leading whitespace removed.
                    353:  *
                    354:  * Side Effects:
                    355:  *     Only those that come from the various arguments.
                    356:  */
                    357: void
1.64      espie     358: Main_ParseArgLine(const char *line)    /* Line to fracture */
1.1       deraadt   359: {
                    360:        char **argv;                    /* Manufactured argument vector */
                    361:        int argc;                       /* Number of arguments in argv */
1.14      espie     362:        char *args;                     /* Space used by the args */
1.19      espie     363:        char *buf;
1.39      espie     364:        char *argv0;
1.48      espie     365:        const char *s;
1.60      espie     366:        size_t len;
1.1       deraadt   367:
1.46      espie     368:
1.1       deraadt   369:        if (line == NULL)
                    370:                return;
                    371:        for (; *line == ' '; ++line)
                    372:                continue;
                    373:        if (!*line)
                    374:                return;
                    375:
1.39      espie     376:        /* POSIX rule: MAKEFLAGS can hold a set of option letters without
                    377:         * any blanks or dashes. */
                    378:        for (s = line;; s++) {
                    379:                if (*s == '\0') {
1.46      espie     380:                        while (line != s)
1.39      espie     381:                                posixParseOptLetter(*line++);
                    382:                        return;
1.46      espie     383:                }
1.39      espie     384:                if (strchr(OPTLETTERS, *s) == NULL)
                    385:                        break;
                    386:        }
1.46      espie     387:        argv0 = Var_Value(".MAKE");
1.60      espie     388:        len = strlen(line) + strlen(argv0) + 2;
                    389:        buf = emalloc(len);
                    390:        (void)snprintf(buf, len, "%s %s", argv0, line);
1.14      espie     391:
1.46      espie     392:        argv = brk_string(buf, &argc, &args);
1.14      espie     393:        free(buf);
1.1       deraadt   394:        MainParseArgs(argc, argv);
1.14      espie     395:
                    396:        free(args);
                    397:        free(argv);
1.1       deraadt   398: }
                    399:
1.75      espie     400: /* Add a :-separated path to a Lst of directories.  */
                    401: static void
                    402: add_dirpath(Lst l, const char *n)
1.9       millert   403: {
1.75      espie     404:        const char *start;
                    405:        const char *cp;
1.9       millert   406:
1.75      espie     407:        for (start = n;;) {
                    408:                for (cp = start; *cp != '\0' && *cp != ':';)
                    409:                        cp++;
                    410:                Dir_AddDiri(l, start, cp);
                    411:                if (*cp == '\0')
                    412:                        break;
                    413:                else
                    414:                        start= cp+1;
1.9       millert   415:        }
                    416: }
                    417:
1.75      espie     418: /*
1.80      espie     419:  * Get the name of this type of MACHINE from utsname so we can share an
1.75      espie     420:  * executable for similar machines. (i.e. m68k: amiga hp300, mac68k, sun3, ...)
                    421:  *
1.93      miod      422:  * Note that MACHINE, MACHINE_ARCH and MACHINE_CPU are decided at
1.75      espie     423:  * run-time.
                    424:  */
                    425: static char *
                    426: figure_out_MACHINE()
1.42      espie     427: {
1.75      espie     428:        char *r = getenv("MACHINE");
                    429:        if (r == NULL) {
                    430: #ifndef MAKE_BOOTSTRAP
                    431:                static struct utsname utsname;
1.42      espie     432:
1.75      espie     433:                if (uname(&utsname) == -1) {
                    434:                        perror("make: uname");
                    435:                        exit(2);
                    436:                }
                    437:                r = utsname.machine;
                    438: #else
                    439:                r = MACHINE;
                    440: #endif
                    441:        }
                    442:        return r;
1.42      espie     443: }
                    444:
1.75      espie     445: static char *
                    446: figure_out_MACHINE_ARCH()
1.1       deraadt   447: {
1.75      espie     448:        char *r = getenv("MACHINE_ARCH");
                    449:        if (r == NULL) {
                    450: #ifndef MACHINE_ARCH
                    451:                r = "unknown";  /* XXX: no uname -p yet */
                    452: #else
                    453:                r = MACHINE_ARCH;
                    454: #endif
                    455:        }
                    456:        return r;
                    457: }
1.93      miod      458: static char *
                    459: figure_out_MACHINE_CPU()
                    460: {
                    461:        char *r = getenv("MACHINE_CPU");
                    462:        if (r == NULL) {
                    463: #if !defined(MACHINE_CPU) && ! defined(MACHINE_ARCH)
                    464:                r = "unknown";  /* XXX: no uname -p yet */
                    465: #else
                    466: #if defined(MACHINE_CPU)
                    467:                r = MACHINE_CPU;
                    468: #else
                    469:                r = MACHINE_ARCH;
                    470: #endif
                    471: #endif
                    472:        }
                    473:        return r;
                    474: }
1.46      espie     475:
1.75      espie     476: static char *
                    477: figure_out_CURDIR()
                    478: {
                    479:        char *dir, *cwd;
                    480:        struct stat sa, sb;
                    481:
                    482:        /* curdir is cwd... */
                    483:        cwd = dogetcwd();
                    484:        if (cwd == NULL) {
1.1       deraadt   485:                (void)fprintf(stderr, "make: %s.\n", strerror(errno));
                    486:                exit(2);
                    487:        }
                    488:
1.75      espie     489:        if (stat(cwd, &sa) == -1) {
                    490:                (void)fprintf(stderr, "make: %s: %s.\n", cwd, strerror(errno));
                    491:                exit(2);
1.1       deraadt   492:        }
                    493:
1.80      espie     494:        /* ...but we can use the alias $PWD if we can prove it is the same
1.75      espie     495:         * directory */
                    496:        if ((dir = getenv("PWD")) != NULL) {
                    497:                if (stat(dir, &sb) == 0 && sa.st_ino == sb.st_ino &&
1.91      espie     498:                    sa.st_dev == sb.st_dev) {
1.75      espie     499:                        free(cwd);
                    500:                        return estrdup(dir);
1.91      espie     501:                }
1.1       deraadt   502:        }
                    503:
1.75      espie     504:        return cwd;
                    505: }
                    506:
                    507: static char *
                    508: chdir_verify_path(const char *path, struct dirs *d)
                    509: {
                    510:        struct stat sb;
1.9       millert   511:
1.75      espie     512:        if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
                    513:                if (chdir(path)) {
                    514:                        (void)fprintf(stderr, "make warning: %s: %s.\n",
                    515:                              path, strerror(errno));
                    516:                        return NULL;
                    517:                } else {
                    518:                        if (path[0] != '/')
                    519:                                return Str_concat(d->current, path, '/');
                    520:                        else
                    521:                                return estrdup(path);
                    522:                }
1.1       deraadt   523:        }
                    524:
1.75      espie     525:        return NULL;
                    526: }
                    527:
                    528: static char *
                    529: concat_verify(const char *p1, const char *p2, char c, struct dirs *d)
                    530: {
                    531:        char *tmp = Str_concat(p1, p2, c);
                    532:        char *result = chdir_verify_path(tmp, d);
                    533:        free(tmp);
                    534:        return result;
                    535: }
                    536:
                    537: static void
                    538: setup_CURDIR_OBJDIR(struct dirs *d, const char *machine)
                    539: {
                    540:        char *path, *prefix;
1.12      millert   541:
1.75      espie     542:        d->current = figure_out_CURDIR();
                    543:        d->object = NULL;
1.1       deraadt   544:        /*
1.9       millert   545:         * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
                    546:         * exists, change into it and build there.  (If a .${MACHINE} suffix
                    547:         * exists, use that directory instead).
                    548:         * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
                    549:         * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
                    550:         * If all fails, use the current directory to build.
                    551:         *
                    552:         * Once things are initted,
                    553:         * have to add the original directory to the search path,
1.66      jolan     554:         * and modify the paths for the Makefiles appropriately.  The
1.1       deraadt   555:         * current directory is also placed as a variable for make scripts.
                    556:         */
1.75      espie     557:        if ((prefix = getenv("MAKEOBJDIRPREFIX")) != NULL) {
                    558:                d->object = concat_verify(prefix, d->current, 0, d);
                    559:        } else if ((path = getenv("MAKEOBJDIR")) != NULL) {
                    560:                d->object = chdir_verify_path(path, d);
                    561:        } else {
                    562:                path = _PATH_OBJDIR;
                    563:                prefix = _PATH_OBJDIRPREFIX;
                    564:                d->object = concat_verify(path, machine, '.', d);
                    565:                if (!d->object)
                    566:                        d->object=chdir_verify_path(path, d);
                    567:                if (!d->object)
                    568:                        d->object = concat_verify(prefix, d->current, 0, d);
                    569:        }
                    570:        if (d->object == NULL)
                    571:                d->object = d->current;
                    572: }
                    573:
                    574: /*
                    575:  * if the VPATH variable is defined, add its contents to the search path.
                    576:  * Uses the same format as the PATH env variable, i.e.,
                    577:  * <directory>:<directory>:<directory>...
                    578:  */
                    579: static void
                    580: setup_VPATH()
                    581: {
                    582:        if (Var_Value("VPATH") != NULL) {
                    583:                char *vpath;
                    584:
                    585:                vpath = Var_Subst("${VPATH}", NULL, false);
1.79      espie     586:                add_dirpath(defaultPath, vpath);
1.75      espie     587:                (void)free(vpath);
1.1       deraadt   588:        }
1.75      espie     589: }
                    590:
                    591: static void
                    592: read_makefile_list(Lst mk, struct dirs *d)
                    593: {
                    594:        LstNode ln;
                    595:        ln = Lst_Find(mk, ReadMakefile, d);
                    596:        if (ln != NULL)
                    597:                Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
                    598: }
                    599:
                    600: static void
1.89      espie     601: read_all_make_rules(bool noBuiltins, bool read_depend,
                    602:     Lst makefiles, struct dirs *d)
1.75      espie     603: {
                    604:        /*
                    605:         * Read in the built-in rules first, followed by the specified
                    606:         * makefile(s), or the default BSDmakefile, Makefile or
                    607:         * makefile, in that order.
                    608:         */
                    609:        if (!noBuiltins) {
                    610:                LIST sysMkPath;                 /* Path of sys.mk */
                    611:
                    612:                Lst_Init(&sysMkPath);
1.81      espie     613:                Dir_Expand(_PATH_DEFSYSMK, systemIncludePath, &sysMkPath);
1.75      espie     614:                if (Lst_IsEmpty(&sysMkPath))
                    615:                        Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
                    616:
                    617:                read_makefile_list(&sysMkPath, d);
1.1       deraadt   618:        }
                    619:
1.75      espie     620:        if (!Lst_IsEmpty(makefiles)) {
                    621:                read_makefile_list(makefiles, d);
                    622:        } else if (!ReadMakefile("BSDmakefile", d))
                    623:                if (!ReadMakefile("makefile", d))
                    624:                        (void)ReadMakefile("Makefile", d);
                    625:
1.89      espie     626:        /* read a .depend file, if it exists, and we're not building depend */
1.94      espie     627:
1.89      espie     628:        if (read_depend)
                    629:                (void)ReadMakefile(".depend", d);
1.75      espie     630: }
                    631:
                    632:
                    633: int main(int, char **);
                    634: /*-
                    635:  * main --
                    636:  *     The main function, for obvious reasons. Initializes variables
                    637:  *     and a few modules, then parses the arguments give it in the
                    638:  *     environment and on the command line. Reads the system makefile
                    639:  *     followed by either Makefile, makefile or the file given by the
                    640:  *     -f argument. Sets the .MAKEFLAGS PMake variable based on all the
                    641:  *     flags it has received by then uses either the Make or the Compat
                    642:  *     module to create the initial list of targets.
                    643:  *
                    644:  * Results:
                    645:  *     If -q was given, exits -1 if anything was out-of-date. Else it exits
                    646:  *     0.
                    647:  *
                    648:  * Side Effects:
                    649:  *     The program exits when done. Targets are created. etc. etc. etc.
                    650:  */
                    651: int
                    652: main(int argc, char **argv)
                    653: {
                    654:        static LIST targs;      /* target nodes to create */
                    655:        bool outOfDate = true;  /* false if all targets up to date */
                    656:        char *machine = figure_out_MACHINE();
                    657:        char *machine_arch = figure_out_MACHINE_ARCH();
1.93      miod      658:        char *machine_cpu = figure_out_MACHINE_CPU();
1.75      espie     659:        const char *syspath = _PATH_DEFSYSPATH;
                    660:        char *p;
                    661:        static struct dirs d;
1.89      espie     662:        bool read_depend = true;/* false if we don't want to read .depend */
1.75      espie     663:
                    664:        setup_CURDIR_OBJDIR(&d, machine);
                    665:
                    666:        esetenv("PWD", d.object);
1.30      espie     667:        unsetenv("CDPATH");
1.1       deraadt   668:
1.56      espie     669:        Static_Lst_Init(create);
                    670:        Static_Lst_Init(&makefiles);
                    671:        Static_Lst_Init(&varstoprint);
                    672:        Static_Lst_Init(&targs);
1.52      espie     673:
1.48      espie     674:        beSilent = false;               /* Print commands as executed */
                    675:        ignoreErrors = false;           /* Pay attention to non-zero returns */
                    676:        noExecute = false;              /* Execute all commands */
                    677:        keepgoing = false;              /* Stop on error */
                    678:        allPrecious = false;            /* Remove targets when interrupted */
                    679:        queryFlag = false;              /* This is not just a check-run */
                    680:        noBuiltins = false;             /* Read the built-in rules */
                    681:        touchFlag = false;              /* Actually update targets */
1.1       deraadt   682:        debug = 0;                      /* No debug verbosity, please. */
                    683:
1.85      espie     684:        maxJobs = DEFMAXJOBS;
1.48      espie     685:        compatMake = false;             /* No compat mode */
1.9       millert   686:
1.1       deraadt   687:
                    688:        /*
1.48      espie     689:         * Initialize all external modules.
1.1       deraadt   690:         */
1.48      espie     691:        Init();
                    692:
1.75      espie     693:        if (d.object != d.current)
1.79      espie     694:                Dir_AddDir(defaultPath, d.current);
1.76      espie     695:        Var_Set(".CURDIR", d.current);
                    696:        Var_Set(".OBJDIR", d.object);
1.96      espie     697:        Parse_setcurdir(d.current);
1.87      espie     698:        Targ_setdirs(d.current, d.object);
1.1       deraadt   699:
                    700:        /*
                    701:         * Initialize various variables.
                    702:         *      MAKE also gets this name, for compatibility
                    703:         *      .MAKEFLAGS gets set to the empty string just in case.
                    704:         *      MFLAGS also gets initialized empty, for compatibility.
                    705:         */
1.76      espie     706:        Var_Set("MAKE", argv[0]);
                    707:        Var_Set(".MAKE", argv[0]);
                    708:        Var_Set(MAKEFLAGS, "");
                    709:        Var_Set("MFLAGS", "");
                    710:        Var_Set("MACHINE", machine);
                    711:        Var_Set("MACHINE_ARCH", machine_arch);
1.93      miod      712:        Var_Set("MACHINE_CPU", machine_cpu);
1.1       deraadt   713:
                    714:        /*
1.46      espie     715:         * First snag any flags out of the MAKEFLAGS environment variable.
1.1       deraadt   716:         */
                    717:        Main_ParseArgLine(getenv("MAKEFLAGS"));
1.9       millert   718:
1.1       deraadt   719:        MainParseArgs(argc, argv);
1.86      espie     720:
                    721:        /*
1.97      espie     722:         * Be compatible if user did not specify -j
1.86      espie     723:         */
1.97      espie     724:        if (!forceJobs)
1.86      espie     725:                compatMake = true;
1.30      espie     726:
1.46      espie     727:        /* And set up everything for sub-makes */
1.39      espie     728:        Var_AddCmdline(MAKEFLAGS);
1.46      espie     729:
1.1       deraadt   730:
                    731:        /*
                    732:         * Set up the .TARGETS variable to contain the list of targets to be
                    733:         * created. If none specified, make the variable empty -- the parser
                    734:         * will fill the thing in with the default or .MAIN target.
                    735:         */
1.48      espie     736:        if (!Lst_IsEmpty(create)) {
1.1       deraadt   737:                LstNode ln;
                    738:
1.48      espie     739:                for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) {
1.1       deraadt   740:                        char *name = (char *)Lst_Datum(ln);
                    741:
1.89      espie     742:                        if (strcmp(name, "depend") == 0)
                    743:                                read_depend = false;
                    744:
1.76      espie     745:                        Var_Append(".TARGETS", name);
1.1       deraadt   746:                }
                    747:        } else
1.76      espie     748:                Var_Set(".TARGETS", "");
1.1       deraadt   749:
1.5       niklas    750:
                    751:        /*
                    752:         * If no user-supplied system path was given (through the -m option)
                    753:         * add the directories from the DEFSYSPATH (more than one may be given
                    754:         * as dir1:...:dirn) to the system include path.
                    755:         */
1.81      espie     756:        if (Lst_IsEmpty(systemIncludePath))
                    757:            add_dirpath(systemIncludePath, syspath);
1.5       niklas    758:
1.89      espie     759:        read_all_make_rules(noBuiltins, read_depend, &makefiles, &d);
1.1       deraadt   760:
1.76      espie     761:        Var_Append("MFLAGS", Var_Value(MAKEFLAGS));
1.1       deraadt   762:
1.46      espie     763:        /* Install all the flags into the MAKEFLAGS env variable. */
                    764:        if (((p = Var_Value(MAKEFLAGS)) != NULL) && *p)
1.44      espie     765:                esetenv("MAKEFLAGS", p);
1.1       deraadt   766:
1.75      espie     767:        setup_VPATH();
1.1       deraadt   768:
1.83      espie     769:        process_suffixes_after_makefile_is_read();
1.1       deraadt   770:
1.96      espie     771:        if (dumpData) {
                    772:                dump_data();
                    773:                exit(0);
                    774:        }
1.97      espie     775:
                    776:        /* Print the initial graph, if the user requested it.  */
                    777:        if (DEBUG(GRAPH1))
                    778:                dump_data();
                    779:
1.46      espie     780:        /* Print the values of any variables requested by the user.  */
1.48      espie     781:        if (!Lst_IsEmpty(&varstoprint)) {
1.75      espie     782:                LstNode ln;
1.9       millert   783:
1.80      espie     784:                for (ln = Lst_First(&varstoprint); ln != NULL;
1.75      espie     785:                    ln = Lst_Adv(ln)) {
                    786:                        char *value = Var_Value((char *)Lst_Datum(ln));
1.9       millert   787:
1.75      espie     788:                        printf("%s\n", value ? value : "");
                    789:                }
1.48      espie     790:        } else {
1.80      espie     791:                /* Have now read the entire graph and need to make a list
                    792:                 * of targets to create. If none was given on the command
                    793:                 * line, we consult the parsing module to find the main
1.75      espie     794:                 * target(s) to create.  */
                    795:                if (Lst_IsEmpty(create))
                    796:                        Parse_MainName(&targs);
                    797:                else
                    798:                        Targ_FindList(&targs, create);
                    799:
1.96      espie     800:                Job_Init(maxJobs);
                    801:                /* If the user has defined a .BEGIN target, execute the commands
                    802:                 * attached to it.  */
                    803:                if (!queryFlag)
                    804:                        Job_Begin();
1.75      espie     805:                if (compatMake)
1.80      espie     806:                        /* Compat_Init will take care of creating all the
1.75      espie     807:                         * targets as well as initializing the module.  */
                    808:                        Compat_Run(&targs);
                    809:                else {
                    810:                        /* Traverse the graph, checking on all the targets.  */
                    811:                        outOfDate = Make_Run(&targs);
1.1       deraadt   812:                }
1.9       millert   813:        }
                    814:
1.1       deraadt   815:        /* print the graph now it's been processed if the user requested it */
                    816:        if (DEBUG(GRAPH2))
1.97      espie     817:                post_mortem();
1.1       deraadt   818:
                    819:        if (queryFlag && outOfDate)
1.46      espie     820:                return 1;
1.1       deraadt   821:        else
1.46      espie     822:                return 0;
1.1       deraadt   823: }
                    824:
                    825: /*-
                    826:  * ReadMakefile  --
                    827:  *     Open and parse the given makefile.
                    828:  *
                    829:  * Results:
1.48      espie     830:  *     true if ok. false if couldn't open file.
1.1       deraadt   831:  *
                    832:  * Side Effects:
                    833:  *     lots
                    834:  */
1.48      espie     835: static bool
1.75      espie     836: ReadMakefile(void *p, void *q)
1.1       deraadt   837: {
1.64      espie     838:        char *fname = (char *)p;        /* makefile to read */
1.75      espie     839:        struct dirs *d = (struct dirs *)q;
1.1       deraadt   840:        FILE *stream;
1.53      espie     841:        char *name;
1.1       deraadt   842:
                    843:        if (!strcmp(fname, "-")) {
1.76      espie     844:                Var_Set("MAKEFILE", "");
1.37      espie     845:                Parse_File(estrdup("(stdin)"), stdin);
1.1       deraadt   846:        } else {
                    847:                if ((stream = fopen(fname, "r")) != NULL)
                    848:                        goto found;
                    849:                /* if we've chdir'd, rebuild the path name */
1.75      espie     850:                if (d->current != d->object && *fname != '/') {
1.53      espie     851:                        char *path;
                    852:
1.75      espie     853:                        path = Str_concat(d->current, fname, '/');
1.53      espie     854:                        if ((stream = fopen(path, "r")) == NULL)
1.75      espie     855:                                free(path);
1.53      espie     856:                        else {
1.75      espie     857:                                fname = path;
                    858:                                goto found;
1.1       deraadt   859:                        }
                    860:                }
                    861:                /* look in -I and system include directories. */
1.81      espie     862:                name = Dir_FindFile(fname, userIncludePath);
1.1       deraadt   863:                if (!name)
1.81      espie     864:                        name = Dir_FindFile(fname, systemIncludePath);
1.1       deraadt   865:                if (!name || !(stream = fopen(name, "r")))
1.48      espie     866:                        return false;
1.1       deraadt   867:                fname = name;
                    868:                /*
                    869:                 * set the MAKEFILE variable desired by System V fans -- the
                    870:                 * placement of the setting here means it gets set to the last
                    871:                 * makefile specified, as it is set by SysV make.
                    872:                 */
1.76      espie     873: found:         Var_Set("MAKEFILE", fname);
1.1       deraadt   874:                Parse_File(fname, stream);
                    875:        }
1.48      espie     876:        return true;
1.1       deraadt   877: }
                    878:
1.46      espie     879:
1.1       deraadt   880: /*
                    881:  * usage --
                    882:  *     exit with usage message
                    883:  */
                    884: static void
                    885: usage()
                    886: {
                    887:        (void)fprintf(stderr,
1.65      jmc       888: "usage: make [-BeiknPqrSst] [-D variable] [-d flags] [-f makefile]\n\
1.46      espie     889:            [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\
1.65      jmc       890:            [NAME=value] [target ...]\n");
1.1       deraadt   891:        exit(2);
                    892: }
                    893:
                    894: