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

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