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

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