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

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