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

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