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

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