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

1.46      espie       1: /*     $OpenPackages$ */
1.66    ! jolan       2: /*     $OpenBSD: main.c,v 1.65 2004/04/21 13:17:49 jmc 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,
                    271:                                "make: illegal argument to d option -- %c\n",
                    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.1       deraadt   292:                        maxJobs = atoi(optarg);
1.2       deraadt   293:                        maxLocal = maxJobs;
1.48      espie     294:                        record_option(c, optarg);
1.1       deraadt   295:                        break;
1.15      espie     296:                }
1.5       niklas    297:                case 'm':
1.48      espie     298:                        Dir_AddDir(sysIncPath, optarg);
                    299:                        record_option(c, optarg);
1.5       niklas    300:                        break;
1.58      millert   301:                case -1:
                    302:                        /* Check for variable assignments and targets. */
1.59      millert   303:                        if (argv[optind] != NULL &&
                    304:                            !Parse_DoVar(argv[optind], VAR_CMD)) {
1.58      millert   305:                                if (!*argv[optind])
                    306:                                        Punt("illegal (null) argument.");
1.61      millert   307:                                Lst_AtEnd(create, estrdup(argv[optind]));
1.58      millert   308:                        }
                    309:                        optind++;       /* skip over non-option */
                    310:                        break;
1.1       deraadt   311:                default:
1.39      espie     312:                        posixParseOptLetter(c);
1.1       deraadt   313:                }
                    314:        }
                    315:
1.2       deraadt   316:        /*
                    317:         * Be compatible if user did not specify -j and did not explicitly
1.48      espie     318:         * turn compatibility on
1.2       deraadt   319:         */
                    320:        if (!compatMake && !forceJobs)
1.48      espie     321:                compatMake = true;
1.2       deraadt   322:
1.48      espie     323:        oldVars = true;
1.1       deraadt   324: }
                    325:
                    326: /*-
                    327:  * Main_ParseArgLine --
1.46      espie     328:  *     Used by the parse module when a .MFLAGS or .MAKEFLAGS target
1.1       deraadt   329:  *     is encountered and by main() when reading the .MAKEFLAGS envariable.
                    330:  *     Takes a line of arguments and breaks it into its
1.46      espie     331:  *     component words and passes those words and the number of them to the
1.1       deraadt   332:  *     MainParseArgs function.
                    333:  *     The line should have all its leading whitespace removed.
                    334:  *
                    335:  * Side Effects:
                    336:  *     Only those that come from the various arguments.
                    337:  */
                    338: void
1.64      espie     339: Main_ParseArgLine(const char *line)    /* Line to fracture */
1.1       deraadt   340: {
                    341:        char **argv;                    /* Manufactured argument vector */
                    342:        int argc;                       /* Number of arguments in argv */
1.14      espie     343:        char *args;                     /* Space used by the args */
1.19      espie     344:        char *buf;
1.39      espie     345:        char *argv0;
1.48      espie     346:        const char *s;
1.60      espie     347:        size_t len;
1.1       deraadt   348:
1.46      espie     349:
1.1       deraadt   350:        if (line == NULL)
                    351:                return;
                    352:        for (; *line == ' '; ++line)
                    353:                continue;
                    354:        if (!*line)
                    355:                return;
                    356:
1.39      espie     357:        /* POSIX rule: MAKEFLAGS can hold a set of option letters without
                    358:         * any blanks or dashes. */
                    359:        for (s = line;; s++) {
                    360:                if (*s == '\0') {
1.46      espie     361:                        while (line != s)
1.39      espie     362:                                posixParseOptLetter(*line++);
                    363:                        return;
1.46      espie     364:                }
1.39      espie     365:                if (strchr(OPTLETTERS, *s) == NULL)
                    366:                        break;
                    367:        }
1.46      espie     368:        argv0 = Var_Value(".MAKE");
1.60      espie     369:        len = strlen(line) + strlen(argv0) + 2;
                    370:        buf = emalloc(len);
                    371:        (void)snprintf(buf, len, "%s %s", argv0, line);
1.14      espie     372:
1.46      espie     373:        argv = brk_string(buf, &argc, &args);
1.14      espie     374:        free(buf);
1.1       deraadt   375:        MainParseArgs(argc, argv);
1.14      espie     376:
                    377:        free(args);
                    378:        free(argv);
1.1       deraadt   379: }
                    380:
1.9       millert   381: char *
1.64      espie     382: chdir_verify_path(const char *path)
1.9       millert   383: {
1.46      espie     384:     struct stat sb;
1.9       millert   385:
1.46      espie     386:     if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
                    387:        if (chdir(path)) {
                    388:            (void)fprintf(stderr, "make warning: %s: %s.\n",
                    389:                  path, strerror(errno));
                    390:            return NULL;
                    391:        } else {
1.53      espie     392:            if (path[0] != '/')
                    393:                return Str_concat(curdir, path, '/');
1.46      espie     394:            else
1.53      espie     395:                return estrdup(path);
1.9       millert   396:        }
1.46      espie     397:     }
1.9       millert   398:
1.46      espie     399:     return NULL;
1.9       millert   400: }
                    401:
1.46      espie     402:
1.42      espie     403: /* Add a :-separated path to a Lst of directories.  */
                    404: static void
1.64      espie     405: add_dirpath(Lst l, const char *n)
1.42      espie     406: {
                    407:     const char *start;
                    408:     const char *cp;
                    409:
                    410:     for (start = n;;) {
                    411:        for (cp = start; *cp != '\0' && *cp != ':';)
                    412:            cp++;
1.48      espie     413:        Dir_AddDiri(l, start, cp);
1.42      espie     414:        if (*cp == '\0')
                    415:            break;
                    416:        else
                    417:            start= cp+1;
                    418:     }
                    419: }
                    420:
1.46      espie     421: int main(int, char **);
1.1       deraadt   422: /*-
                    423:  * main --
                    424:  *     The main function, for obvious reasons. Initializes variables
                    425:  *     and a few modules, then parses the arguments give it in the
                    426:  *     environment and on the command line. Reads the system makefile
                    427:  *     followed by either Makefile, makefile or the file given by the
                    428:  *     -f argument. Sets the .MAKEFLAGS PMake variable based on all the
                    429:  *     flags it has received by then uses either the Make or the Compat
                    430:  *     module to create the initial list of targets.
                    431:  *
                    432:  * Results:
                    433:  *     If -q was given, exits -1 if anything was out-of-date. Else it exits
                    434:  *     0.
                    435:  *
                    436:  * Side Effects:
                    437:  *     The program exits when done. Targets are created. etc. etc. etc.
                    438:  */
                    439: int
1.64      espie     440: main(int argc, char **argv)
1.1       deraadt   441: {
1.56      espie     442:        static LIST targs;      /* target nodes to create */
1.48      espie     443:        bool outOfDate = true;  /* false if all targets up to date */
1.1       deraadt   444:        struct stat sb, sa;
1.19      espie     445:        char *p, *path, *pathp, *pwd;
1.53      espie     446:        char *mdpath;
1.46      espie     447:        char *machine = getenv("MACHINE");
1.12      millert   448:        char *machine_arch = getenv("MACHINE_ARCH");
1.48      espie     449:        const char *syspath = _PATH_DEFSYSPATH;
1.46      espie     450:
1.2       deraadt   451: #ifdef RLIMIT_NOFILE
                    452:        /*
                    453:         * get rid of resource limit on file descriptors
                    454:         */
                    455:        {
                    456:                struct rlimit rl;
                    457:                if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
                    458:                    rl.rlim_cur != rl.rlim_max) {
                    459:                        rl.rlim_cur = rl.rlim_max;
1.46      espie     460:                        (void)setrlimit(RLIMIT_NOFILE, &rl);
1.2       deraadt   461:                }
                    462:        }
                    463: #endif
1.1       deraadt   464:        /*
                    465:         * Find where we are and take care of PWD for the automounter...
                    466:         * All this code is so that we know where we are when we start up
                    467:         * on a different machine with pmake.
                    468:         */
1.53      espie     469:        if ((curdir = dogetcwd()) == NULL) {
1.1       deraadt   470:                (void)fprintf(stderr, "make: %s.\n", strerror(errno));
                    471:                exit(2);
                    472:        }
                    473:
                    474:        if (stat(curdir, &sa) == -1) {
                    475:            (void)fprintf(stderr, "make: %s: %s.\n",
                    476:                          curdir, strerror(errno));
                    477:            exit(2);
                    478:        }
                    479:
                    480:        if ((pwd = getenv("PWD")) != NULL) {
                    481:            if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
1.53      espie     482:                sa.st_dev == sb.st_dev) {
                    483:                    free(curdir);
                    484:                    curdir = estrdup(pwd);
                    485:            }
1.1       deraadt   486:        }
                    487:
                    488:        /*
                    489:         * Get the name of this type of MACHINE from utsname
                    490:         * so we can share an executable for similar machines.
                    491:         * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
                    492:         *
1.12      millert   493:         * Note that both MACHINE and MACHINE_ARCH are decided at
                    494:         * run-time.
1.1       deraadt   495:         */
1.12      millert   496:        if (!machine) {
                    497: #ifndef MAKE_BOOTSTRAP
1.9       millert   498:            struct utsname utsname;
                    499:
1.2       deraadt   500:            if (uname(&utsname) == -1) {
1.1       deraadt   501:                    perror("make: uname");
                    502:                    exit(2);
                    503:            }
                    504:            machine = utsname.machine;
1.6       niklas    505: #else
                    506:            machine = MACHINE;
                    507: #endif
1.1       deraadt   508:        }
                    509:
1.12      millert   510:        if (!machine_arch) {
                    511: #ifndef MACHINE_ARCH
                    512:            machine_arch = "unknown";   /* XXX: no uname -p yet */
                    513: #else
                    514:            machine_arch = MACHINE_ARCH;
                    515: #endif
                    516:        }
                    517:
1.1       deraadt   518:        /*
1.9       millert   519:         * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
                    520:         * exists, change into it and build there.  (If a .${MACHINE} suffix
                    521:         * exists, use that directory instead).
                    522:         * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default,
                    523:         * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists.
                    524:         * If all fails, use the current directory to build.
                    525:         *
                    526:         * Once things are initted,
                    527:         * have to add the original directory to the search path,
1.66    ! jolan     528:         * and modify the paths for the Makefiles appropriately.  The
1.1       deraadt   529:         * current directory is also placed as a variable for make scripts.
                    530:         */
1.53      espie     531:        mdpath = NULL;
1.9       millert   532:        if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) {
                    533:                if (!(path = getenv("MAKEOBJDIR"))) {
                    534:                        path = _PATH_OBJDIR;
                    535:                        pathp = _PATH_OBJDIRPREFIX;
1.53      espie     536:                        mdpath = Str_concat(path, machine, '.');
                    537:                        if (!(objdir = chdir_verify_path(mdpath)))
                    538:                                if (!(objdir=chdir_verify_path(path))) {
                    539:                                        free(mdpath);
                    540:                                        mdpath = Str_concat(pathp, curdir, 0);
                    541:                                        if (!(objdir=chdir_verify_path(mdpath)))
1.9       millert   542:                                                objdir = curdir;
                    543:                                }
                    544:                }
1.53      espie     545:                else if (!(objdir = chdir_verify_path(path)))
1.1       deraadt   546:                        objdir = curdir;
                    547:        }
                    548:        else {
1.53      espie     549:                mdpath = Str_concat(pathp, curdir, 0);
                    550:                if (!(objdir = chdir_verify_path(mdpath)))
1.1       deraadt   551:                        objdir = curdir;
                    552:        }
1.53      espie     553:        free(mdpath);
1.1       deraadt   554:
1.44      espie     555:        esetenv("PWD", objdir);
1.30      espie     556:        unsetenv("CDPATH");
1.1       deraadt   557:
1.56      espie     558:        Static_Lst_Init(create);
                    559:        Static_Lst_Init(&makefiles);
                    560:        Static_Lst_Init(&varstoprint);
                    561:        Static_Lst_Init(&targs);
1.52      espie     562:
1.48      espie     563:        beSilent = false;               /* Print commands as executed */
                    564:        ignoreErrors = false;           /* Pay attention to non-zero returns */
                    565:        noExecute = false;              /* Execute all commands */
                    566:        keepgoing = false;              /* Stop on error */
                    567:        allPrecious = false;            /* Remove targets when interrupted */
                    568:        queryFlag = false;              /* This is not just a check-run */
                    569:        noBuiltins = false;             /* Read the built-in rules */
                    570:        touchFlag = false;              /* Actually update targets */
                    571:        usePipes = true;                /* Catch child output in pipes */
1.1       deraadt   572:        debug = 0;                      /* No debug verbosity, please. */
                    573:
1.46      espie     574:        maxLocal = DEFMAXLOCAL;         /* Set default local max concurrency */
1.2       deraadt   575:        maxJobs = maxLocal;
1.48      espie     576:        compatMake = false;             /* No compat mode */
1.9       millert   577:
1.1       deraadt   578:
                    579:        /*
1.48      espie     580:         * Initialize all external modules.
1.1       deraadt   581:         */
1.48      espie     582:        Init();
                    583:
1.1       deraadt   584:        if (objdir != curdir)
1.48      espie     585:                Dir_AddDir(dirSearchPath, curdir);
1.1       deraadt   586:        Var_Set(".CURDIR", curdir, VAR_GLOBAL);
                    587:        Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
                    588:
                    589:        /*
                    590:         * Initialize various variables.
                    591:         *      MAKE also gets this name, for compatibility
                    592:         *      .MAKEFLAGS gets set to the empty string just in case.
                    593:         *      MFLAGS also gets initialized empty, for compatibility.
                    594:         */
                    595:        Var_Set("MAKE", argv[0], VAR_GLOBAL);
1.14      espie     596:        Var_Set(".MAKE", argv[0], VAR_GLOBAL);
1.1       deraadt   597:        Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
                    598:        Var_Set("MFLAGS", "", VAR_GLOBAL);
                    599:        Var_Set("MACHINE", machine, VAR_GLOBAL);
1.12      millert   600:        Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
1.1       deraadt   601:
                    602:        /*
1.46      espie     603:         * First snag any flags out of the MAKEFLAGS environment variable.
1.1       deraadt   604:         */
                    605:        Main_ParseArgLine(getenv("MAKEFLAGS"));
1.9       millert   606:
1.1       deraadt   607:        MainParseArgs(argc, argv);
1.30      espie     608:
1.46      espie     609:        /* And set up everything for sub-makes */
1.39      espie     610:        Var_AddCmdline(MAKEFLAGS);
1.46      espie     611:
1.1       deraadt   612:
1.25      espie     613:        DEFAULT = NULL;
1.1       deraadt   614:
                    615:        /*
                    616:         * Set up the .TARGETS variable to contain the list of targets to be
                    617:         * created. If none specified, make the variable empty -- the parser
                    618:         * will fill the thing in with the default or .MAIN target.
                    619:         */
1.48      espie     620:        if (!Lst_IsEmpty(create)) {
1.1       deraadt   621:                LstNode ln;
                    622:
1.48      espie     623:                for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) {
1.1       deraadt   624:                        char *name = (char *)Lst_Datum(ln);
                    625:
                    626:                        Var_Append(".TARGETS", name, VAR_GLOBAL);
                    627:                }
                    628:        } else
                    629:                Var_Set(".TARGETS", "", VAR_GLOBAL);
                    630:
1.5       niklas    631:
                    632:        /*
                    633:         * If no user-supplied system path was given (through the -m option)
                    634:         * add the directories from the DEFSYSPATH (more than one may be given
                    635:         * as dir1:...:dirn) to the system include path.
                    636:         */
1.48      espie     637:        if (Lst_IsEmpty(sysIncPath))
                    638:            add_dirpath(sysIncPath, syspath);
1.5       niklas    639:
1.1       deraadt   640:        /*
1.5       niklas    641:         * Read in the built-in rules first, followed by the specified
1.48      espie     642:         * makefile(s), or the default BSDmakefile, Makefile or
                    643:         * makefile, in that order.
1.1       deraadt   644:         */
1.5       niklas    645:        if (!noBuiltins) {
                    646:                LstNode ln;
1.46      espie     647:                LIST sysMkPath;                 /* Path of sys.mk */
1.5       niklas    648:
1.33      espie     649:                Lst_Init(&sysMkPath);
1.48      espie     650:                Dir_Expand(_PATH_DEFSYSMK, sysIncPath, &sysMkPath);
1.33      espie     651:                if (Lst_IsEmpty(&sysMkPath))
1.5       niklas    652:                        Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
1.33      espie     653:                ln = Lst_Find(&sysMkPath, ReadMakefile, NULL);
1.25      espie     654:                if (ln != NULL)
1.5       niklas    655:                        Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1.34      espie     656: #ifdef CLEANUP
                    657:                Lst_Destroy(&sysMkPath, (SimpleProc)free);
                    658: #endif
1.5       niklas    659:        }
1.1       deraadt   660:
1.33      espie     661:        if (!Lst_IsEmpty(&makefiles)) {
1.1       deraadt   662:                LstNode ln;
                    663:
1.33      espie     664:                ln = Lst_Find(&makefiles, ReadMakefile, NULL);
1.25      espie     665:                if (ln != NULL)
1.1       deraadt   666:                        Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
1.13      niklas    667:        } else if (!ReadMakefile("BSDmakefile", NULL))
                    668:                if (!ReadMakefile("makefile", NULL))
                    669:                        (void)ReadMakefile("Makefile", NULL);
1.1       deraadt   670:
1.48      espie     671:        /* Always read a .depend file, if it exists. */
1.9       millert   672:        (void)ReadMakefile(".depend", NULL);
1.1       deraadt   673:
1.46      espie     674:        Var_Append("MFLAGS", Var_Value(MAKEFLAGS),
                    675:            VAR_GLOBAL);
1.1       deraadt   676:
1.46      espie     677:        /* Install all the flags into the MAKEFLAGS env variable. */
                    678:        if (((p = Var_Value(MAKEFLAGS)) != NULL) && *p)
1.44      espie     679:                esetenv("MAKEFLAGS", p);
1.1       deraadt   680:
                    681:        /*
                    682:         * For compatibility, look at the directories in the VPATH variable
                    683:         * and add them to the search path, if the variable is defined. The
                    684:         * variable's value is in the same format as the PATH envariable, i.e.
                    685:         * <directory>:<directory>:<directory>...
                    686:         */
1.46      espie     687:        if (Var_Value("VPATH") != NULL) {
                    688:            char *vpath;
1.1       deraadt   689:
1.48      espie     690:            vpath = Var_Subst("${VPATH}", NULL, false);
                    691:            add_dirpath(dirSearchPath, vpath);
1.46      espie     692:            (void)free(vpath);
1.1       deraadt   693:        }
                    694:
1.46      espie     695:        /* Now that all search paths have been read for suffixes et al, it's
                    696:         * time to add the default search path to their lists...  */
1.1       deraadt   697:        Suff_DoPaths();
                    698:
1.46      espie     699:        /* Print the initial graph, if the user requested it.  */
1.1       deraadt   700:        if (DEBUG(GRAPH1))
                    701:                Targ_PrintGraph(1);
                    702:
1.46      espie     703:        /* Print the values of any variables requested by the user.  */
1.48      espie     704:        if (!Lst_IsEmpty(&varstoprint)) {
                    705:            LstNode ln;
1.9       millert   706:
1.48      espie     707:            for (ln = Lst_First(&varstoprint); ln != NULL; ln = Lst_Adv(ln)) {
                    708:                    char *value = Var_Value((char *)Lst_Datum(ln));
1.9       millert   709:
1.48      espie     710:                    printf("%s\n", value ? value : "");
                    711:            }
                    712:        } else {
                    713:            /* Have now read the entire graph and need to make a list of targets
                    714:             * to create. If none was given on the command line, we consult the
                    715:             * parsing module to find the main target(s) to create.  */
                    716:            if (Lst_IsEmpty(create))
1.34      espie     717:                Parse_MainName(&targs);
1.48      espie     718:            else
                    719:                Targ_FindList(&targs, create);
1.1       deraadt   720:
1.48      espie     721:            if (compatMake)
                    722:                /* Compat_Init will take care of creating all the targets as
                    723:                 * well as initializing the module.  */
                    724:                Compat_Run(&targs);
                    725:            else {
1.46      espie     726:                /* Initialize job module before traversing the graph, now that
                    727:                 * any .BEGIN and .END targets have been read.  This is done
1.1       deraadt   728:                 * only if the -q flag wasn't given (to prevent the .BEGIN from
1.46      espie     729:                 * being executed should it exist).  */
1.1       deraadt   730:                if (!queryFlag) {
                    731:                        if (maxLocal == -1)
                    732:                                maxLocal = maxJobs;
                    733:                        Job_Init(maxJobs, maxLocal);
                    734:                }
                    735:
1.46      espie     736:                /* Traverse the graph, checking on all the targets.  */
1.34      espie     737:                outOfDate = Make_Run(&targs);
1.48      espie     738:            }
1.9       millert   739:        }
                    740:
1.52      espie     741: #ifdef CLEANUP
1.34      espie     742:        Lst_Destroy(&targs, NOFREE);
1.48      espie     743:        Lst_Destroy(&varstoprint, NOFREE);
1.33      espie     744:        Lst_Destroy(&makefiles, NOFREE);
1.48      espie     745:        Lst_Destroy(create, (SimpleProc)free);
1.52      espie     746: #endif
1.1       deraadt   747:
                    748:        /* print the graph now it's been processed if the user requested it */
                    749:        if (DEBUG(GRAPH2))
                    750:                Targ_PrintGraph(2);
                    751:
1.53      espie     752: #ifdef CLEANUP
                    753:        if (objdir != curdir)
                    754:            free(objdir);
                    755:        free(curdir);
                    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: