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

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