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

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