Major kernel build infrastructure changes, part 2/2 (user).
[dragonfly.git] / usr.bin / make / main.c
1 /*-
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * @(#) Copyright (c) 1988, 1989, 1990, 1993 The Regents of the University of California.  All rights reserved.
39  * @(#)main.c   8.3 (Berkeley) 3/19/94
40  * $FreeBSD: src/usr.bin/make/main.c,v 1.118 2005/02/13 13:33:56 harti Exp $
41  * $DragonFly: src/usr.bin/make/main.c,v 1.145 2006/11/07 06:57:02 dillon Exp $
42  */
43
44 /*
45  * main.c
46  *      The main file for this entire program. Exit routines etc
47  *      reside here.
48  *
49  * Utility functions defined in this file:
50  *      Main_ParseArgLine
51  *                      Takes a line of arguments, breaks them and
52  *                      treats them as if they were given when first
53  *                      invoked. Used by the parse module to implement
54  *                      the .MFLAGS target.
55  */
56
57 #ifndef MACHINE
58 #include <sys/utsname.h>
59 #endif
60 #include <sys/param.h>
61 #include <sys/stat.h>
62 #include <sys/sysctl.h>
63 #include <sys/time.h>
64 #include <sys/queue.h>
65 #include <sys/resource.h>
66 #include <sys/wait.h>
67 #include <err.h>
68 #include <errno.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72
73 #include "arch.h"
74 #include "buf.h"
75 #include "config.h"
76 #include "dir.h"
77 #include "globals.h"
78 #include "job.h"
79 #include "make.h"
80 #include "parse.h"
81 #include "pathnames.h"
82 #include "shell.h"
83 #include "str.h"
84 #include "suff.h"
85 #include "targ.h"
86 #include "util.h"
87 #include "var.h"
88
89 extern char **environ;  /* XXX what header declares this variable? */
90
91 /*
92  * DEFMAXJOBS
93  *      This control the default concurrency. On no occasion will more
94  *      than DEFMAXJOBS targets be created at once.
95  */
96 #define DEFMAXJOBS      1
97
98 typedef struct CLI {
99         /** ordered list of makefiles to read */
100         Lst makefiles;
101
102         /** list of variables to print */
103         Lst variables;
104
105         /** Targets to be made */
106         Lst create;
107
108         /** directories to search when looking for includes */
109         struct Path     parseIncPath;
110
111         /** directories to search when looking for system includes */
112         struct Path     sysIncPath;
113
114         bool    expandVars;     /* fully expand printed variables */
115         bool    builtins;       /* -r flag */
116         bool    forceJobs;      /* -j argument given */
117
118         /**
119          * -q flag:
120          * true if we aren't supposed to really make anything, just
121          * see if the targets are out-of-date
122          */
123         bool            queryFlag;
124 } CLI;
125
126 /* (-E) vars to override from env */
127 Lst envFirstVars = Lst_Initializer(envFirstVars);
128
129 bool            allPrecious;    /* .PRECIOUS given on line by itself */
130 bool            beSilent;       /* -s flag */
131 bool            beVerbose;      /* -v flag */
132 bool            compatMake;     /* -B argument */
133 int             debug;          /* -d flag */
134 bool            ignoreErrors;   /* -i flag */
135 int             jobLimit;       /* -j argument */
136 bool            jobsRunning;    /* true if the jobs might be running */
137 bool            keepgoing;      /* -k flag */
138 bool            noExecute;      /* -n flag */
139 bool            touchFlag;      /* -t flag */
140 bool            usePipes;       /* !-P flag */
141 uint32_t        warn_cmd;       /* command line warning flags */
142 uint32_t        warn_flags;     /* actual warning flags */
143 uint32_t        warn_nocmd;     /* command line no-warning flags */
144
145 time_t          now;            /* Time at start of make */
146 struct GNode    *DEFAULT;       /* .DEFAULT node */
147
148
149 /**
150  * Exit with usage message.
151  */
152 static void
153 usage(void)
154 {
155         fprintf(stderr,
156             "usage: make [-BPSXeiknqrstv] [-C directory] [-D variable]\n"
157             "\t[-d flags] [-E variable] [-f makefile] [-I directory]\n"
158             "\t[-j max_jobs] [-m directory] [-V variable]\n"
159             "\t[variable=value] [target ...]\n");
160         exit(2);
161 }
162
163 /**
164  * MFLAGS_append
165  *      Append a flag with an optional argument to MAKEFLAGS and MFLAGS
166  */
167 static void
168 MFLAGS_append(const char *flag, char *arg)
169 {
170         char *str;
171
172         Var_Append(".MAKEFLAGS", flag, VAR_GLOBAL);
173         if (arg != NULL) {
174                 str = MAKEFLAGS_quote(arg);
175                 Var_Append(".MAKEFLAGS", str, VAR_GLOBAL);
176                 free(str);
177         }
178
179         Var_Append("MFLAGS", flag, VAR_GLOBAL);
180         if (arg != NULL) {
181                 str = MAKEFLAGS_quote(arg);
182                 Var_Append("MFLAGS", str, VAR_GLOBAL);
183                 free(str);
184         }
185 }
186
187 /**
188  * Open and parse the given makefile.
189  *
190  * Results:
191  *      true if ok. false if couldn't open file.
192  */
193 static bool
194 ReadMakefile(Parser *parser, CLI *cli, const char file[], const char curdir[], const char objdir[])
195 {
196         char    path[MAXPATHLEN];
197         FILE    *stream;
198         char    *fname;
199         char    *name;
200
201         if (!strcmp(file, "-")) {
202                 Parse_File(parser, cli, "(stdin)", stdin);
203                 Var_SetGlobal("MAKEFILE", "");
204                 return (true);
205         }
206
207         if (strcmp(curdir, objdir) == 0 || file[0] == '/') {
208                 strcpy(path, file);
209         } else {
210                 /*
211                  * we've chdir'd, rebuild the path name
212                  */
213                 snprintf(path, MAXPATHLEN, "%s/%s", curdir, file);
214         }
215 #if THIS_BREAKS_THINGS
216         /*
217          * XXX The realpath stuff breaks relative includes
218          * XXX in some cases.   The problem likely is in
219          * XXX parse.c where it does special things in
220          * XXX ParseDoInclude if the file is relateive
221          * XXX or absolute and not a system file.  There
222          * XXX it assumes that if the current file that's
223          * XXX being included is absolute, that any files
224          * XXX that it includes shouldn't do the -I path
225          * XXX stuff, which is inconsistant with historical
226          * XXX behavior.  However, I can't pentrate the mists
227          * XXX further, so I'm putting this workaround in
228          * XXX here until such time as the underlying bug
229          * XXX can be fixed.
230          */
231         if (realpath(path, path) == NULL) {
232                 stream = NULL;
233         } else {
234                 stream = fopen(path, "r");
235         }
236 #else
237         stream = fopen(path, "r");
238 #endif
239         if (stream != NULL) {
240                 if (strcmp(file, ".depend") != 0)
241                         Var_SetGlobal("MAKEFILE", file);
242                 Parse_File(parser, cli, path, stream);
243                 fclose(stream);
244                 return (true);
245         }
246
247         /* look in -I and system include directories. */
248         fname = estrdup(file);
249         name = NULL;
250         if (name == NULL)
251                 name = Path_FindFile(fname, &cli->parseIncPath);
252         if (name == NULL)
253                 name = Path_FindFile(fname, &cli->sysIncPath);
254
255         if (name != NULL) {
256                 stream = fopen(name, "r");
257                 if (stream != NULL) {
258                         /*
259                          * set the MAKEFILE variable desired by System V fans
260                          * -- the placement of the setting here means it gets
261                          * set to the last makefile specified, as it is set
262                          * by SysV make.
263                          */
264                         if (strcmp(file, ".depend") != 0)
265                                 Var_SetGlobal("MAKEFILE", name);
266                         Parse_File(parser, cli, name, stream);
267                         fclose(stream);
268                         return (true);
269                 }
270         }
271
272         return (false); /* no makefile found */
273 }
274
275 /**
276  * Read in the built-in rules first, followed by the specified
277  * makefiles or the one of the default makefiles.  Finally .depend
278  * makefile.
279  */
280 static void
281 ReadInputFiles(Parser *parser, CLI *cli, const char curdir[], const char objdir[])
282 {
283         if (cli->builtins) {
284                 char    defsysmk[] = PATH_DEFSYSMK;     /* Path of sys.mk */
285                 Lst     sysMkPath = Lst_Initializer(sysMkPath);
286                 LstNode *ln;
287
288                 Path_Expand(defsysmk, &cli->sysIncPath, &sysMkPath);
289                 if (Lst_IsEmpty(&sysMkPath))
290                         Fatal("make: no system rules (%s).", PATH_DEFSYSMK);
291
292                 LST_FOREACH(ln, &sysMkPath) {
293                         char *name = Lst_Datum(ln);
294                         if (!ReadMakefile(parser, cli, name, curdir, objdir))
295                                 Fatal("make: cannot open %s.", name);
296                 }
297                 Lst_Destroy(&sysMkPath, free);
298         }
299
300         if (!Lst_IsEmpty(&cli->makefiles)) {
301                 LstNode *ln;
302
303                 LST_FOREACH(ln, &cli->makefiles) {
304                         char *name = Lst_Datum(ln);
305                         if (!ReadMakefile(parser, cli, name, curdir, objdir))
306                                 Fatal("make: cannot open %s.", name);
307                 }
308         } else if (ReadMakefile(parser, cli, "BSDmakefile", curdir, objdir)) {
309                 /* read BSDmakefile */
310         } else if (ReadMakefile(parser, cli, "makefile", curdir, objdir)) {
311                 /* read makefile */
312         } else if (ReadMakefile(parser, cli, "Makefile", curdir, objdir)) {
313                 /* read Makefile */
314         } else {
315                 /* No Makefile found */
316         }
317
318         ReadMakefile(parser, cli, ".depend", curdir, objdir);
319 }
320
321 /**
322  * Main_ParseWarn
323  *
324  *      Handle argument to warning option.
325  */
326 int
327 Main_ParseWarn(const char *arg, int iscmd)
328 {
329         int i, neg;
330
331         static const struct {
332                 const char      *option;
333                 uint32_t        flag;
334         } options[] = {
335                 { "dirsyntax",  WARN_DIRSYNTAX },
336                 { NULL,         0 }
337         };
338
339         neg = 0;
340         if (arg[0] == 'n' && arg[1] == 'o') {
341                 neg = 1;
342                 arg += 2;
343         }
344
345         for (i = 0; options[i].option != NULL; i++)
346                 if (strcmp(arg, options[i].option) == 0)
347                         break;
348
349         if (options[i].option == NULL)
350                 /* unknown option */
351                 return (-1);
352
353         if (iscmd) {
354                 if (!neg) {
355                         warn_cmd |= options[i].flag;
356                         warn_nocmd &= ~options[i].flag;
357                         warn_flags |= options[i].flag;
358                 } else {
359                         warn_nocmd |= options[i].flag;
360                         warn_cmd &= ~options[i].flag;
361                         warn_flags &= ~options[i].flag;
362                 }
363         } else {
364                 if (!neg) {
365                         warn_flags |= (options[i].flag & ~warn_nocmd);
366                 } else {
367                         warn_flags &= ~(options[i].flag | warn_cmd);
368                 }
369         }
370         return (0);
371 }
372
373 /**
374  * MainParseArgs
375  *      Parse a given argument vector. Called from main() and from
376  *      Main_ParseArgLine() when the .MAKEFLAGS target is used.
377  *
378  *      XXX: Deal with command line overriding .MAKEFLAGS in makefile
379  *
380  * Side Effects:
381  *      Various global and local flags will be set depending on the flags
382  *      given
383  */
384 static void
385 MainParseArgs(CLI *cli, int argc, char **argv)
386 {
387         int c;
388         bool    found_dd = false;
389
390 rearg:
391         optind = 1;     /* since we're called more than once */
392         optreset = 1;
393 #define OPTFLAGS "ABC:D:E:I:PSV:Xd:ef:ij:km:nqrstvx:"
394         for (;;) {
395                 if ((optind < argc) && strcmp(argv[optind], "--") == 0) {
396                         found_dd = true;
397                 }
398                 if ((c = getopt(argc, argv, OPTFLAGS)) == -1) {
399                         break;
400                 }
401                 switch(c) {
402
403                 case 'A':
404                         arch_fatal = false;
405                         MFLAGS_append("-A", NULL);
406                         break;
407                 case 'C':
408                         if (chdir(optarg) == -1)
409                                 err(1, "chdir %s", optarg);
410                         break;
411                 case 'D':
412                         Var_SetGlobal(optarg, "1");
413                         MFLAGS_append("-D", optarg);
414                         break;
415                 case 'I':
416                         Path_AddDir(&cli->parseIncPath, optarg);
417                         MFLAGS_append("-I", optarg);
418                         break;
419                 case 'V':
420                         Lst_AtEnd(&cli->variables, estrdup(optarg));
421                         MFLAGS_append("-V", optarg);
422                         break;
423                 case 'X':
424                         cli->expandVars = false;
425                         break;
426                 case 'B':
427                         compatMake = true;
428                         MFLAGS_append("-B", NULL);
429                         unsetenv("MAKE_JOBS_FIFO");
430                         break;
431                 case 'P':
432                         usePipes = false;
433                         MFLAGS_append("-P", NULL);
434                         break;
435                 case 'S':
436                         keepgoing = false;
437                         MFLAGS_append("-S", NULL);
438                         break;
439                 case 'd': {
440                         char *modules = optarg;
441
442                         for (; *modules; ++modules)
443                                 switch (*modules) {
444                                 case 'A':
445                                         debug = ~0;
446                                         break;
447                                 case 'a':
448                                         debug |= DEBUG_ARCH;
449                                         break;
450                                 case 'c':
451                                         debug |= DEBUG_COND;
452                                         break;
453                                 case 'd':
454                                         debug |= DEBUG_DIR;
455                                         break;
456                                 case 'f':
457                                         debug |= DEBUG_FOR;
458                                         break;
459                                 case 'g':
460                                         if (modules[1] == '1') {
461                                                 debug |= DEBUG_GRAPH1;
462                                                 ++modules;
463                                         }
464                                         else if (modules[1] == '2') {
465                                                 debug |= DEBUG_GRAPH2;
466                                                 ++modules;
467                                         }
468                                         break;
469                                 case 'j':
470                                         debug |= DEBUG_JOB;
471                                         break;
472                                 case 'l':
473                                         debug |= DEBUG_LOUD;
474                                         break;
475                                 case 'm':
476                                         debug |= DEBUG_MAKE;
477                                         break;
478                                 case 's':
479                                         debug |= DEBUG_SUFF;
480                                         break;
481                                 case 't':
482                                         debug |= DEBUG_TARG;
483                                         break;
484                                 case 'v':
485                                         debug |= DEBUG_VAR;
486                                         break;
487                                 default:
488                                         warnx("illegal argument to d option "
489                                             "-- %c", *modules);
490                                         usage();
491                                 }
492                         MFLAGS_append("-d", optarg);
493                         break;
494                 }
495                 case 'E':
496                         Lst_AtEnd(&envFirstVars, estrdup(optarg));
497                         MFLAGS_append("-E", optarg);
498                         break;
499                 case 'e':
500                         checkEnvFirst = true;
501                         MFLAGS_append("-e", NULL);
502                         break;
503                 case 'f':
504                         Lst_AtEnd(&cli->makefiles, estrdup(optarg));
505                         break;
506                 case 'i':
507                         ignoreErrors = true;
508                         MFLAGS_append("-i", NULL);
509                         break;
510                 case 'j': {
511                         char *endptr;
512
513                         cli->forceJobs = true;
514                         jobLimit = strtol(optarg, &endptr, 10);
515                         if (jobLimit <= 0 || *endptr != '\0') {
516                                 warnx("illegal number, -j argument -- %s",
517                                     optarg);
518                                 usage();
519                         }
520                         MFLAGS_append("-j", optarg);
521                         break;
522                 }
523                 case 'k':
524                         keepgoing = true;
525                         MFLAGS_append("-k", NULL);
526                         break;
527                 case 'm':
528                         Path_AddDir(&cli->sysIncPath, optarg);
529                         MFLAGS_append("-m", optarg);
530                         break;
531                 case 'n':
532                         noExecute = true;
533                         MFLAGS_append("-n", NULL);
534                         break;
535                 case 'q':
536                         cli->queryFlag = true;
537                         /* Kind of nonsensical, wot? */
538                         MFLAGS_append("-q", NULL);
539                         break;
540                 case 'r':
541                         cli->builtins = false;
542                         MFLAGS_append("-r", NULL);
543                         break;
544                 case 's':
545                         beSilent = true;
546                         MFLAGS_append("-s", NULL);
547                         break;
548                 case 't':
549                         touchFlag = true;
550                         MFLAGS_append("-t", NULL);
551                         break;
552                 case 'v':
553                         beVerbose = true;
554                         MFLAGS_append("-v", NULL);
555                         break;
556                 case 'x':
557                         if (Main_ParseWarn(optarg, 1) != -1)
558                                 MFLAGS_append("-x", optarg);
559                         break;
560                 default:
561                 case '?':
562                         usage();
563                 }
564         }
565         argv += optind;
566         argc -= optind;
567
568         oldVars = true;
569
570         /*
571          * Parse the rest of the arguments.
572          *      o Check for variable assignments and perform them if so.
573          *      o Check for more flags and restart getopt if so.
574          *      o Anything else is taken to be a target and added
575          *        to the end of the "create" list.
576          */
577         for (; *argv != NULL; ++argv, --argc) {
578                 if (Parse_IsVar(*argv)) {
579                         char *ptr = MAKEFLAGS_quote(*argv);
580
581                         Var_Append(".MAKEFLAGS", ptr, VAR_GLOBAL);
582                         Parse_DoVar(*argv, VAR_CMD);
583                         free(ptr);
584
585                 } else if ((*argv)[0] == '-') {
586                         if ((*argv)[1] == '\0') {
587                                 /*
588                                  * (*argv) is a single dash, so we
589                                  * just ignore it.
590                                  */
591                         } else if (found_dd) {
592                                 /*
593                                  * Double dash has been found, ignore
594                                  * any more options.  But what do we do
595                                  * with it?  For now treat it like a target.
596                                  */
597                                 Lst_AtEnd(&cli->create, estrdup(*argv));
598                         } else {
599                                 /*
600                                  * (*argv) is a -flag, so backup argv and
601                                  * argc.  getopt() expects options to start
602                                  * in the 2nd position.
603                                  */
604                                 argc++;
605                                 argv--;
606                                 goto rearg;
607                         }
608
609                 } else if ((*argv)[0] == '\0') {
610                         Punt("illegal (null) argument.");
611
612                 } else {
613                         Lst_AtEnd(&cli->create, estrdup(*argv));
614                 }
615         }
616 }
617
618 /**
619  * Main_ParseArgLine
620  *      Used by the parse module when a .MFLAGS or .MAKEFLAGS target
621  *      is encountered and by main() when reading the .MAKEFLAGS envariable.
622  *      Takes a line of arguments and breaks it into its
623  *      component words and passes those words and the number of them to the
624  *      MainParseArgs function.
625  *      The line should have all its leading whitespace removed.
626  *
627  * Side Effects:
628  *      Only those that come from the various arguments.
629  */
630 void
631 Main_ParseArgLine(CLI *cli, const char line[], int mflags)
632 {
633         ArgArray        aa;
634
635         if (mflags) {
636                 MAKEFLAGS_break(&aa, line);
637         } else {
638                 brk_string(&aa, line, true);
639         }
640         MainParseArgs(cli, aa.argc, aa.argv);
641         ArgArray_Done(&aa);
642 }
643
644 /**
645  * Try to change the current working directory to path, and return
646  * the whole path using getcwd().
647  *
648  * @note for amd managed mount points we really should use pawd(1).
649  */
650 static int
651 CheckDir(const char path[], char newdir[])
652 {
653         struct stat sb;
654
655         /*
656          * Check if the path is a directory.  If not fail without reporting
657          * an error.
658          */
659         if (stat(path, &sb) < 0) {
660                 return (0);
661         }
662         if (S_ISDIR(sb.st_mode) == 0) {
663                 return (0);
664         }
665
666         /*
667          * The path refers to a directory, so we try to change into it. If we
668          * fail, or we fail to obtain the path from root to the directory,
669          * then report an error and fail.
670          */
671         if (chdir(path) < 0) {
672                 warn("warning: %s", path);
673                 return (0);
674         }
675         if (getcwd(newdir, MAXPATHLEN) == NULL) {
676                 warn("warning: %s", path);
677                 return (0);
678         }
679
680         /*
681          * Directory in path is accessable, newdir should now contain the
682          * path to it.
683          */
684         return (1);
685 }
686
687 /**
688  * Determine location of the object directory.
689  */
690 static void
691 FindObjDir(const char machine[], char curdir[], char objdir[])
692 {
693         struct stat     sa;
694         char            newdir[MAXPATHLEN];
695         char            mdpath[MAXPATHLEN];
696         const char      *env;
697
698         /*
699          * Find a path to where we are... [-C directory] might have changed
700          * our current directory.
701          */
702         if (getcwd(curdir, MAXPATHLEN) == NULL)
703                 err(2, NULL);
704
705         if (stat(curdir, &sa) == -1)
706                 err(2, "%s", curdir);
707
708         /*
709          * The object directory location is determined using the
710          * following order of preference:
711          *
712          *      1. MAKEOBJDIRPREFIX`cwd`
713          *      2. MAKEOBJDIR
714          *      3. PATH_OBJDIR.${MACHINE}
715          *      4. PATH_OBJDIR
716          *      5. PATH_OBJDIRPREFIX`cwd`
717          *
718          * If one of the first two fails, use the current directory.
719          * If the remaining three all fail, use the current directory.
720          */
721         if ((env = getenv("MAKEOBJDIRPREFIX")) != NULL) {
722                 snprintf(mdpath, MAXPATHLEN, "%s%s", env, curdir);
723                 if (CheckDir(mdpath, newdir)) {
724                         strcpy(objdir, newdir);
725                         return;
726                 }
727                 strcpy(objdir, curdir);
728                 return;
729         }
730
731         if ((env = getenv("MAKEOBJDIR")) != NULL) {
732                 if (CheckDir(env, newdir)) {
733                         strcpy(objdir, newdir);
734                         return;
735                 }
736                 strcpy(objdir, curdir);
737                 return;
738         }
739
740         snprintf(mdpath, MAXPATHLEN, "%s.%s", PATH_OBJDIR, machine);
741         if (CheckDir(mdpath, newdir)) {
742                 strcpy(objdir, newdir);
743                 return;
744         }
745
746         if (CheckDir(PATH_OBJDIR, newdir)) {
747                 strcpy(objdir, newdir);
748                 return;
749         }
750
751         snprintf(mdpath, MAXPATHLEN, "%s%s", PATH_OBJDIRPREFIX, curdir);
752         if (CheckDir(mdpath, newdir)) {
753                 strcpy(objdir, newdir);
754                 return;
755         }
756
757         strcpy(objdir, curdir);
758 }
759
760 /**
761  * Initialize various make variables.
762  *      MAKE also gets this name, for compatibility
763  *      .MAKEFLAGS gets set to the empty string just in case.
764  *      MFLAGS also gets initialized empty, for compatibility.
765  */
766 static void
767 InitVariables(const char progname[])
768 {
769         const char      *machine;
770         const char      *machine_arch;
771         const char      *machine_cpu;
772         char buf[256];
773         size_t bufsiz;
774
775         Var_SetGlobal("MAKE", progname);
776         Var_SetGlobal(".MAKEFLAGS", "");
777         Var_SetGlobal("MFLAGS", "");
778
779         Var_SetGlobal(".DIRECTIVE_MAKEENV", "YES");
780         Var_SetGlobal(".ST_EXPORTVAR", "YES");
781 #ifdef MAKE_VERSION
782         Var_SetGlobal("MAKE_VERSION", MAKE_VERSION);
783 #endif
784
785         /*
786          * The make program defines MACHINE and MACHINE_ARCH.  These
787          * parameters are taken from the running system but can be
788          * overridden by environment variables.
789          *
790          * MACHINE      - This is the platform, e.g. "vkernel", "i386", "sun",
791          *                and so forth.
792          *
793          * MACHINE_ARCH - This is the cpu architecture, for example "i386".
794          *                Several different platforms may use the same
795          *                cpu architecture.
796          *
797          * MACHINE_CPU  - This is the minimum supported cpu revision based on
798          *                the target cpu architecture.  e.g. "i486", "i586",
799          *                etc.  NOTE: This field will often be "i386" even 
800          *                for kernels built without I386_CPU.
801          *
802          * In some, but not all cases, MACHINE == MACHINE_ARCH.  A virtual
803          * kernel build, for example, would set MACHINE to "vkernel" and
804          * MACHINE_ARCH to the cpu architecture.
805          */
806         if ((machine = getenv("MACHINE")) == NULL) {
807                 bufsiz = sizeof(buf);
808                 if (sysctlbyname("hw.machine", buf, &bufsiz, NULL, 0) < 0)
809                         machine = "unknown";
810                 else
811                         machine = strdup(buf);
812         }
813
814         if ((machine_arch = getenv("MACHINE_ARCH")) == NULL) {
815                 bufsiz = sizeof(buf);
816                 if (sysctlbyname("hw.machine_arch", buf, &bufsiz, NULL, 0) < 0)
817                         machine_arch = "unknown";
818                 else
819                         machine_arch = strdup(buf);
820         }
821
822         /*
823          * Only newer kernels have machine_cpu.  If not otherwise known set
824          * the minimum supported cpu architecture to the machine architecture.
825          */
826         if ((machine_cpu = getenv("MACHINE_CPU")) == NULL) {
827                 bufsiz = sizeof(buf);
828                 if (sysctlbyname("hw.machine_cpu", buf, &bufsiz, NULL, 0) < 0)
829                         machine_cpu = machine_arch;
830                 else
831                         machine_cpu = strdup(buf);
832         }
833
834         Var_SetGlobal("MACHINE", machine);
835         Var_SetGlobal("MACHINE_ARCH", machine_arch);
836         Var_SetGlobal("MACHINE_CPU", machine_cpu);
837 }
838
839 /**
840  * Build targets given in the command line or if none were given
841  * use the main target determined by the parsing module.
842  */
843 static int
844 BuildStuff(CLI *cli)
845 {
846         int     status;
847         Lst     targs = Lst_Initializer(targs);
848
849         if (Lst_IsEmpty(&cli->create))
850                 Parse_MainName(&targs);
851         else
852                 Targ_FindList(&targs, &cli->create, TARG_CREATE);
853
854         /* Traverse the graph, checking on all the targets */
855         if (compatMake) {
856                 Sig_Init(true);
857                 status = Compat_Run(&targs, cli->queryFlag);
858         } else {
859                 Sig_Init(false);
860                 status = Make_Run(&targs, cli->queryFlag);
861         }
862
863         Lst_Destroy(&targs, NOFREE);
864         return (status);
865 }
866
867 /**
868  * main
869  *      The main function, for obvious reasons. Initializes variables
870  *      and a few modules, then parses the arguments give it in the
871  *      environment and on the command line. Reads the system makefile
872  *      followed by either Makefile, makefile or the file given by the
873  *      -f argument. Sets the .MAKEFLAGS PMake variable based on all the
874  *      flags it has received by then uses either the Make or the Compat
875  *      module to create the initial list of targets.
876  *
877  * Results:
878  *      If -q was given, exits -1 if anything was out-of-date. Else it exits
879  *      0.
880  *
881  * Side Effects:
882  *      The program exits when done. Targets are created. etc. etc. etc.
883  */
884 int
885 main(int argc, char **argv)
886 {
887         CLI             cli;
888         Parser          parser;
889         Shell           *shell;
890         int             status;         /* exit status */
891         char            curdir[MAXPATHLEN];     /* startup directory */
892         char            objdir[MAXPATHLEN];     /* where we chdir'ed to */
893         const char      *make_flags;
894
895         /*------------------------------------------------------------*
896          * This section initializes stuff that require no input.
897          *------------------------------------------------------------*/
898         /*
899          * Initialize program globals.
900          */
901         beSilent = false;               /* Print commands as executed */
902         ignoreErrors = false;           /* Pay attention to non-zero returns */
903         noExecute = false;              /* Execute all commands */
904         keepgoing = false;              /* Stop on error */
905         allPrecious = false;            /* Remove targets when interrupted */
906         touchFlag = false;              /* Actually update targets */
907         usePipes = true;                /* Catch child output in pipes */
908         debug = 0;                      /* No debug verbosity, please. */
909         jobsRunning = false;
910
911         jobLimit = DEFMAXJOBS;
912         compatMake = false;             /* No compat mode */
913
914         /*
915          * Initialize program flags.
916          */
917         Lst_Init(&cli.makefiles);
918         Lst_Init(&cli.variables);
919         Lst_Init(&cli.create);
920         TAILQ_INIT(&cli.parseIncPath);
921         TAILQ_INIT(&cli.sysIncPath);
922
923         cli.expandVars = true;
924         cli.builtins = true;            /* Read the built-in rules */
925         cli.queryFlag = false;
926         cli.forceJobs = false;
927
928         shell = Shell_Match(DEFSHELLNAME);
929
930         /*
931          * Initialize the various modules.
932          */
933         Proc_Init();
934         commandShell = shell;
935         Targ_Init();
936         Suff_Init();
937         Dir_Init();
938
939         /*------------------------------------------------------------*
940          * This section initializes stuff that depend on things
941          * in the enviornment, command line, or a input file.
942          *------------------------------------------------------------*/
943         Var_Init(environ);
944
945         InitVariables(argv[0]);
946
947         /*
948          * First snag things out of the MAKEFLAGS environment
949          * variable.  Then parse the command line arguments.
950          */
951         if ((make_flags = getenv("MAKEFLAGS")) != NULL) {
952                 Main_ParseArgLine(&cli, make_flags, 1);
953         }
954         MainParseArgs(&cli, argc, argv);
955
956         FindObjDir(Var_Value("MACHINE", VAR_GLOBAL), curdir, objdir);
957         Var_SetGlobal(".CURDIR", curdir);
958         Var_SetGlobal(".OBJDIR", objdir);
959
960         /*
961          * Set up the .TARGETS variable to contain the list of targets to be
962          * created. If none specified, make the variable empty -- the parser
963          * will fill the thing in with the default or .MAIN target.
964          */
965         if (Lst_IsEmpty(&cli.create)) {
966                 Var_SetGlobal(".TARGETS", "");
967         } else {
968                 LstNode *ln;
969
970                 LST_FOREACH(ln, &cli.create) {
971                         char *name = Lst_Datum(ln);
972
973                         Var_Append(".TARGETS", name, VAR_GLOBAL);
974                 }
975         }
976
977         Dir_CurObj(curdir, objdir);
978
979         /*
980          * If no user-supplied system path was given (through the -m option)
981          * add the directories from the DEFSYSPATH (more than one may be given
982          * as dir1:...:dirn) to the system include path.
983          */
984         if (TAILQ_EMPTY(&cli.sysIncPath)) {
985                 char syspath[] = PATH_DEFSYSPATH;
986                 char *start = syspath;
987                 char *cp;
988
989                 while ((cp = strsep(&start, ":")) != NULL) {
990                         Path_AddDir(&cli.sysIncPath, cp);
991                 }
992         }
993
994         if (getenv("MAKE_JOBS_FIFO") != NULL)
995                 cli.forceJobs = true;
996
997         /*
998          * Be compatible if user did not specify -j and did not explicitly
999          * turned compatibility on
1000          */
1001         if (compatMake == false && cli.forceJobs == false)
1002                 compatMake = true;
1003
1004         DEFAULT = NULL;
1005         time(&now);
1006
1007         parser.create = &cli.create;
1008         parser.parseIncPath = &cli.parseIncPath;
1009         parser.sysIncPath = &cli.sysIncPath;
1010
1011         ReadInputFiles(&parser, &cli, curdir, objdir);
1012
1013         /*------------------------------------------------------------*
1014          * We are finished processing inputs.
1015          *------------------------------------------------------------*/
1016
1017         /* Install all the flags into the MAKE envariable. */
1018         {
1019                 const char *p;
1020
1021                 p = Var_Value(".MAKEFLAGS", VAR_GLOBAL);
1022                 if (p != NULL && *p != '\0') {
1023                         if (setenv("MAKEFLAGS", p, 1) == -1)
1024                                 Punt("setenv: MAKEFLAGS: can't allocate memory");
1025                 }
1026         }
1027
1028         /*
1029          * For compatibility, look at the directories in the VPATH variable
1030          * and add them to the search path, if the variable is defined. The
1031          * variable's value is in the same format as the PATH envariable, i.e.
1032          * <directory>:<directory>:<directory>...
1033          */
1034         if (Var_Value("VPATH", VAR_CMD) != NULL) {
1035                 Buffer  *buf = Var_Subst("${VPATH}", VAR_CMD, false);
1036                 char    *start = Buf_Data(buf);
1037                 char    *cp;
1038
1039                 while ((cp = strsep(&start, ":")) != NULL) {
1040                         Path_AddDir(&dirSearchPath, cp);
1041                 }
1042
1043                 Buf_Destroy(buf, true);
1044         }
1045
1046         /*
1047          * Now that all search paths have been read for suffixes et al, it's
1048          * time to add the default search path to their lists...
1049          */
1050         Suff_DoPaths();
1051
1052         /* print the initial graph, if the user requested it */
1053         if (DEBUG(GRAPH1))
1054                 Targ_PrintGraph(1);
1055
1056         if (Lst_IsEmpty(&cli.variables)) {
1057                 status = BuildStuff(&cli);
1058         } else {
1059                 /* Print the values of variables requested by the user. */
1060                 Var_Print(&cli.variables, cli.expandVars);
1061
1062                 /*
1063                  * XXX
1064                  * This should be a "don't care", we do not check
1065                  * the status of any files.  It might make sense to
1066                  * modify Var_Print() to indicate that one of the
1067                  * requested variables did not exist, and use that
1068                  * as the return status.
1069                  * XXX
1070                  */
1071                 status = cli.queryFlag ? 1 : 0;
1072         }
1073
1074         /* print the graph now it's been processed if the user requested it */
1075         if (DEBUG(GRAPH2))
1076                 Targ_PrintGraph(2);
1077
1078 #if 0
1079         TAILQ_DESTROY(&cli.sysIncPath);
1080         TAILQ_DESTROY(&cli.parseIncPath);
1081 #endif
1082         Lst_Destroy(&cli.create, free);
1083         Lst_Destroy(&cli.variables, free);
1084         Lst_Destroy(&cli.makefiles, free);
1085
1086         return (status);
1087 }
1088