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

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