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

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