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

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