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

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