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

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