1 /* $NetBSD: main.c,v 1.264 2017/04/20 03:57:27 sjg Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid[] = "$NetBSD: main.c,v 1.264 2017/04/20 03:57:27 sjg Exp $";
74 #include <sys/cdefs.h>
76 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\
77 The Regents of the University of California. All rights reserved.");
82 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
84 __RCSID("$NetBSD: main.c,v 1.264 2017/04/20 03:57:27 sjg Exp $");
91 * The main file for this entire program. Exit routines etc
94 * Utility functions defined in this file:
95 * Main_ParseArgLine Takes a line of arguments, breaks them and
96 * treats them as if they were given when first
97 * invoked. Used by the parse module to implement
100 * Error Print a tagged error message. The global
101 * MAKE variable must have been defined. This
102 * takes a format string and optional arguments
105 * Fatal Print an error message and exit. Also takes
106 * a format string and arguments for it.
108 * Punt Aborts all jobs and exits with a message. Also
109 * takes a format string and arguments for it.
111 * Finish Finish things up by printing the number of
112 * errors which occurred, as passed to it, and
116 #include <sys/types.h>
117 #include <sys/time.h>
118 #include <sys/param.h>
119 #include <sys/resource.h>
120 #include <sys/stat.h>
121 #if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL)
122 #include <sys/sysctl.h>
124 #include <sys/utsname.h>
139 #include "pathnames.h"
147 #define DEFMAXLOCAL DEFMAXJOBS
148 #endif /* DEFMAXLOCAL */
151 # define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
154 Lst create; /* Targets to be made */
155 time_t now; /* Time at start of make */
156 GNode *DEFAULT; /* .DEFAULT node */
157 Boolean allPrecious; /* .PRECIOUS given on line by itself */
158 Boolean deleteOnError; /* .DELETE_ON_ERROR: set */
160 static Boolean noBuiltins; /* -r flag */
161 static Lst makefiles; /* ordered list of makefiles to read */
162 static Boolean printVars; /* print value of one or more vars */
163 static Lst variables; /* list of variables to print */
164 int maxJobs; /* -j argument */
165 static int maxJobTokens; /* -j argument */
166 Boolean compatMake; /* -B argument */
167 int debug; /* -d argument */
168 Boolean debugVflag; /* -dV */
169 Boolean noExecute; /* -n flag */
170 Boolean noRecursiveExecute; /* -N flag */
171 Boolean keepgoing; /* -k flag */
172 Boolean queryFlag; /* -q flag */
173 Boolean touchFlag; /* -t flag */
174 Boolean enterFlag; /* -w flag */
175 Boolean enterFlagObj; /* -w and objdir != srcdir */
176 Boolean ignoreErrors; /* -i flag */
177 Boolean beSilent; /* -s flag */
178 Boolean oldVars; /* variable substitution style */
179 Boolean checkEnvFirst; /* -e flag */
180 Boolean parseWarnFatal; /* -W flag */
181 Boolean jobServer; /* -J flag */
182 static int jp_0 = -1, jp_1 = -1; /* ends of parent job pipe */
183 Boolean varNoExportEnv; /* -X flag */
184 Boolean doing_depend; /* Set while reading .depend */
185 static Boolean jobsRunning; /* TRUE if the jobs might be running */
186 static const char * tracefile;
187 static void MainParseArgs(int, char **);
188 static int ReadMakefile(const void *, const void *);
189 static void usage(void) MAKE_ATTR_DEAD;
190 static void purge_cached_realpaths(void);
192 static Boolean ignorePWD; /* if we use -C, PWD is meaningless */
193 static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
194 char curdir[MAXPATHLEN + 1]; /* Startup directory */
195 char *progname; /* the program name */
196 char *makeDependfile;
200 Boolean forceJobs = FALSE;
203 * On some systems MACHINE is defined as something other than
208 # define MACHINE FORCE_MACHINE
211 extern Lst parseIncPath;
214 * For compatibility with the POSIX version of MAKEFLAGS that includes
215 * all the options with out -, convert flags to -f -l -a -g -s.
218 explode(const char *flags)
227 for (f = flags; *f; f++)
228 if (!isalpha((unsigned char)*f))
232 return bmake_strdup(flags);
235 st = nf = bmake_malloc(len * 3 + 1);
246 parse_debug_options(const char *argvalue)
253 for (modules = argvalue; *modules; ++modules) {
271 debug |= DEBUG_ERROR;
277 if (modules[1] == '1') {
278 debug |= DEBUG_GRAPH1;
281 else if (modules[1] == '2') {
282 debug |= DEBUG_GRAPH2;
285 else if (modules[1] == '3') {
286 debug |= DEBUG_GRAPH3;
303 debug |= DEBUG_SCRIPT;
306 debug |= DEBUG_PARSE;
321 debug |= DEBUG_SHELL;
324 if (debug_file != stdout && debug_file != stderr)
326 if (*++modules == '+') {
331 if (strcmp(modules, "stdout") == 0) {
335 if (strcmp(modules, "stderr") == 0) {
339 len = strlen(modules);
340 fname = bmake_malloc(len + 20);
341 memcpy(fname, modules, len + 1);
342 /* Let the filename be modified by the pid */
343 if (strcmp(fname + len - 3, ".%d") == 0)
344 snprintf(fname + len - 2, 20, "%d", getpid());
345 debug_file = fopen(fname, mode);
347 fprintf(stderr, "Cannot open debug file %s\n",
354 (void)fprintf(stderr,
355 "%s: illegal argument to d option -- %c\n",
362 * Make the debug_file unbuffered, and make
363 * stdout line buffered (unless debugfile == stdout).
365 setvbuf(debug_file, NULL, _IONBF, 0);
366 if (debug_file != stdout) {
367 setvbuf(stdout, NULL, _IOLBF, 0);
372 * does path contain any relative components
375 is_relpath(const char *path)
383 cp = strstr(cp, "/.");
387 if (cp[0] == '/' || cp[0] == '\0')
389 else if (cp[0] == '.') {
390 if (cp[1] == '/' || cp[1] == '\0')
399 * Parse a given argument vector. Called from main() and from
400 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
402 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
408 * Various global and local flags will be set depending on the flags
412 MainParseArgs(int argc, char **argv)
418 const char *getopt_def;
421 Boolean inOption, dashDash = FALSE;
422 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
424 #define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstw"
425 /* Can't actually use getopt(3) because rescanning is not portable */
427 getopt_def = OPTFLAGS;
445 if (c != '-' || dashDash)
450 /* '-' found at some earlier point */
451 getopt_spec = strchr(getopt_def, c);
452 if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') {
453 /* -<something> found, and <something> should have an arg */
457 if(*argvalue == '\0') {
473 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
474 Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0);
477 if (chdir(argvalue) == -1) {
478 (void)fprintf(stderr,
479 "%s: chdir %s: %s\n",
484 if (getcwd(curdir, MAXPATHLEN) == NULL) {
485 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
488 if (!is_relpath(argvalue) &&
489 stat(argvalue, &sa) != -1 &&
490 stat(curdir, &sb) != -1 &&
491 sa.st_ino == sb.st_ino &&
492 sa.st_dev == sb.st_dev)
493 strncpy(curdir, argvalue, MAXPATHLEN);
497 if (argvalue == NULL || argvalue[0] == 0) goto noarg;
498 Var_Set(argvalue, "1", VAR_GLOBAL, 0);
499 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
500 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
503 if (argvalue == NULL) goto noarg;
504 Parse_AddIncludeDir(argvalue);
505 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
506 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
509 if (argvalue == NULL) goto noarg;
510 if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) {
511 (void)fprintf(stderr,
512 "%s: internal error -- J option malformed (%s)\n",
516 if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
517 (fcntl(jp_1, F_GETFD, 0) < 0)) {
519 (void)fprintf(stderr,
520 "%s: ###### warning -- J descriptors were closed!\n",
528 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
529 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
535 noRecursiveExecute = TRUE;
536 Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL);
540 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
543 if (argvalue == NULL) goto noarg;
544 tracefile = bmake_strdup(argvalue);
545 Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL);
546 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
549 if (argvalue == NULL) goto noarg;
551 (void)Lst_AtEnd(variables, argvalue);
552 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
553 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
556 parseWarnFatal = TRUE;
559 varNoExportEnv = TRUE;
560 Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL);
563 if (argvalue == NULL) goto noarg;
564 /* If '-d-opts' don't pass to children */
565 if (argvalue[0] == '-')
568 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
569 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
571 parse_debug_options(argvalue);
574 checkEnvFirst = TRUE;
575 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
578 if (argvalue == NULL) goto noarg;
579 (void)Lst_AtEnd(makefiles, argvalue);
583 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
586 if (argvalue == NULL) goto noarg;
588 maxJobs = strtol(argvalue, &p, 0);
589 if (*p != '\0' || maxJobs < 1) {
590 (void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n",
594 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
595 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
596 Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0);
597 maxJobTokens = maxJobs;
601 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
604 if (argvalue == NULL) goto noarg;
605 /* look for magic parent directory search string */
606 if (strncmp(".../", argvalue, 4) == 0) {
607 if (!Dir_FindHereOrAbove(curdir, argvalue+4,
608 found_path, sizeof(found_path)))
609 break; /* nothing doing */
610 (void)Dir_AddDir(sysIncPath, found_path);
612 (void)Dir_AddDir(sysIncPath, argvalue);
614 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
615 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
619 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
623 /* Kind of nonsensical, wot? */
624 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
628 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
632 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
636 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
640 Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
648 fprintf(stderr, "getopt(%s) -> %d (%c)\n",
660 * See if the rest of the arguments are variable assignments and
661 * perform them if so. Else take them to be targets and stuff them
662 * on the end of the "create" list.
664 for (; argc > 1; ++argv, --argc)
665 if (Parse_IsVar(argv[1])) {
666 Parse_DoVar(argv[1], VAR_CMD);
669 Punt("illegal (null) argument.");
670 if (*argv[1] == '-' && !dashDash)
672 (void)Lst_AtEnd(create, bmake_strdup(argv[1]));
677 (void)fprintf(stderr, "%s: option requires an argument -- %c\n",
683 * Main_ParseArgLine --
684 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
685 * is encountered and by main() when reading the .MAKEFLAGS envariable.
686 * Takes a line of arguments and breaks it into its
687 * component words and passes those words and the number of them to the
688 * MainParseArgs function.
689 * The line should have all its leading whitespace removed.
692 * line Line to fracture
698 * Only those that come from the various arguments.
701 Main_ParseArgLine(const char *line)
703 char **argv; /* Manufactured argument vector */
704 int argc; /* Number of arguments in argv */
705 char *args; /* Space used by the args */
707 char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1);
712 for (; *line == ' '; ++line)
720 * $MAKE may simply be naming the make(1) binary
724 if (!(cp = strrchr(line, '/')))
726 if ((cp = strstr(cp, "make")) &&
727 strcmp(cp, "make") == 0)
731 buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2);
732 (void)snprintf(buf, len, "%s %s", argv0, line);
735 argv = brk_string(buf, &argc, TRUE, &args);
737 Error("Unterminated quoted string [%s]", buf);
742 MainParseArgs(argc, argv);
749 Main_SetObjdir(const char *fmt, ...)
753 char buf[MAXPATHLEN + 1];
758 vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
761 if (path[0] != '/') {
762 char buf2[MAXPATHLEN + 1];
764 snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path);
768 /* look for the directory and try to chdir there */
769 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
771 (void)fprintf(stderr, "make warning: %s: %s.\n",
772 path, strerror(errno));
774 strncpy(objdir, path, MAXPATHLEN);
775 Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0);
776 setenv("PWD", objdir, 1);
778 purge_cached_realpaths();
780 if (enterFlag && strcmp(objdir, curdir) != 0)
789 Main_SetVarObjdir(const char *var, const char *suffix)
791 char *p, *path, *xpath;
793 if ((path = Var_Value(var, VAR_CMD, &p)) == NULL)
796 /* expand variable substitutions */
797 if (strchr(path, '$') != 0)
798 xpath = Var_Subst(NULL, path, VAR_GLOBAL, VARF_WANTRES);
802 (void)Main_SetObjdir("%s%s", xpath, suffix);
811 * ReadAllMakefiles --
812 * wrapper around ReadMakefile() to read all.
815 * TRUE if ok, FALSE on error
818 ReadAllMakefiles(const void *p, const void *q)
820 return (ReadMakefile(p, q) == 0);
824 str2Lst_Append(Lst lp, char *str, const char *sep)
832 for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) {
833 (void)Lst_AtEnd(lp, cp);
842 siginfo(int signo MAKE_ATTR_UNUSED)
844 char dir[MAXPATHLEN];
845 char str[2 * MAXPATHLEN];
847 if (getcwd(dir, sizeof(dir)) == NULL)
849 len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir);
851 (void)write(STDERR_FILENO, str, (size_t)len);
856 * Allow makefiles some control over the mode we run in.
859 MakeMode(const char *mode)
864 mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}",
865 VAR_GLOBAL, VARF_WANTRES);
868 if (strstr(mode, "compat")) {
873 if (strstr(mode, "meta"))
874 meta_mode_init(mode);
883 * The main function, for obvious reasons. Initializes variables
884 * and a few modules, then parses the arguments give it in the
885 * environment and on the command line. Reads the system makefile
886 * followed by either Makefile, makefile or the file given by the
887 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
888 * flags it has received by then uses either the Make or the Compat
889 * module to create the initial list of targets.
892 * If -q was given, exits -1 if anything was out-of-date. Else it exits
896 * The program exits when done. Targets are created. etc. etc. etc.
899 main(int argc, char **argv)
901 Lst targs; /* target nodes to create -- passed to Make_Init */
902 Boolean outOfDate = FALSE; /* FALSE if all targets up to date */
905 char mdpath[MAXPATHLEN];
907 const char *machine = FORCE_MACHINE;
909 const char *machine = getenv("MACHINE");
911 const char *machine_arch = getenv("MACHINE_ARCH");
912 char *syspath = getenv("MAKESYSPATH");
913 Lst sysMkPath; /* Path of sys.mk */
914 char *cp = NULL, *start;
915 /* avoid faults on read-only strings */
916 static char defsyspath[] = _PATH_DEFSYSPATH;
917 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
918 struct timeval rightnow; /* to initialize random seed */
919 struct utsname utsname;
921 /* default to writing debug to stderr */
925 (void)bmake_signal(SIGINFO, siginfo);
928 * Set the seed to produce a different random sequence
929 * on each program execution.
931 gettimeofday(&rightnow, NULL);
932 srandom(rightnow.tv_sec + rightnow.tv_usec);
934 if ((progname = strrchr(argv[0], '/')) != NULL)
938 #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
940 * get rid of resource limit on file descriptors
944 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
945 rl.rlim_cur != rl.rlim_max) {
946 rl.rlim_cur = rl.rlim_max;
947 (void)setrlimit(RLIMIT_NOFILE, &rl);
952 if (uname(&utsname) == -1) {
953 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
959 * Get the name of this type of MACHINE from utsname
960 * so we can share an executable for similar machines.
961 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
963 * Note that both MACHINE and MACHINE_ARCH are decided at
968 machine = utsname.machine;
971 machine = MAKE_MACHINE;
979 #if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL) && defined(CTL_HW) && defined(HW_MACHINE_ARCH)
980 static char machine_arch_buf[sizeof(utsname.machine)];
981 int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
982 size_t len = sizeof(machine_arch_buf);
984 if (sysctl(mib, __arraycount(mib), machine_arch_buf,
985 &len, NULL, 0) < 0) {
986 (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
991 machine_arch = machine_arch_buf;
994 #ifdef MAKE_MACHINE_ARCH
995 machine_arch = MAKE_MACHINE_ARCH;
997 machine_arch = "unknown";
1000 machine_arch = MACHINE_ARCH;
1005 myPid = getpid(); /* remember this for vFork() */
1008 * Just in case MAKEOBJDIR wants us to do something tricky.
1010 Var_Init(); /* Initialize the lists of variables for
1011 * parsing arguments */
1012 Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL, 0);
1013 Var_Set("MACHINE", machine, VAR_GLOBAL, 0);
1014 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0);
1016 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0);
1018 Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */
1020 * This is the traditional preference for makefiles.
1022 #ifndef MAKEFILE_PREFERENCE_LIST
1023 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
1025 Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
1027 Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0);
1029 create = Lst_Init(FALSE);
1030 makefiles = Lst_Init(FALSE);
1033 variables = Lst_Init(FALSE);
1034 beSilent = FALSE; /* Print commands as executed */
1035 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
1036 noExecute = FALSE; /* Execute all commands */
1037 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */
1038 keepgoing = FALSE; /* Stop on error */
1039 allPrecious = FALSE; /* Remove targets when interrupted */
1040 deleteOnError = FALSE; /* Historical default behavior */
1041 queryFlag = FALSE; /* This is not just a check-run */
1042 noBuiltins = FALSE; /* Read the built-in rules */
1043 touchFlag = FALSE; /* Actually update targets */
1044 debug = 0; /* No debug verbosity, please. */
1045 jobsRunning = FALSE;
1047 maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */
1048 maxJobTokens = maxJobs;
1049 compatMake = FALSE; /* No compat mode */
1053 * Initialize the parsing, directory and variable modules to prepare
1054 * for the reading of inclusion paths and variable settings on the
1059 * Initialize various variables.
1060 * MAKE also gets this name, for compatibility
1061 * .MAKEFLAGS gets set to the empty string just in case.
1062 * MFLAGS also gets initialized empty, for compatibility.
1065 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
1067 * Leave alone if it is an absolute path, or if it does
1068 * not contain a '/' in which case we need to find it in
1069 * the path, like execvp(3) and the shells do.
1074 * A relative path, canonicalize it.
1076 p1 = cached_realpath(argv[0], mdpath);
1077 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
1078 p1 = argv[0]; /* realpath failed */
1081 Var_Set("MAKE", p1, VAR_GLOBAL, 0);
1082 Var_Set(".MAKE", p1, VAR_GLOBAL, 0);
1083 Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0);
1084 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0);
1085 Var_Set("MFLAGS", "", VAR_GLOBAL, 0);
1086 Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0);
1087 /* some makefiles need to know this */
1088 Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD, 0);
1091 * Set some other useful macros
1096 makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
1099 snprintf(tmp, sizeof(tmp), "%d", makelevel);
1100 Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL, 0);
1101 snprintf(tmp, sizeof(tmp), "%u", myPid);
1102 Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
1103 snprintf(tmp, sizeof(tmp), "%u", getppid());
1104 Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
1106 if (makelevel > 0) {
1108 snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
1109 progname = bmake_strdup(pn);
1115 Dir_Init(NULL); /* Dir_* safe to call from MainParseArgs */
1118 * First snag any flags out of the MAKE environment variable.
1119 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
1120 * in a different format).
1123 p1 = explode(getenv("MAKEFLAGS"));
1124 Main_ParseArgLine(p1);
1127 Main_ParseArgLine(getenv("MAKE"));
1131 * Find where we are (now).
1132 * We take care of PWD for the automounter below...
1134 if (getcwd(curdir, MAXPATHLEN) == NULL) {
1135 (void)fprintf(stderr, "%s: getcwd: %s.\n",
1136 progname, strerror(errno));
1140 MainParseArgs(argc, argv);
1143 printf("%s: Entering directory `%s'\n", progname, curdir);
1146 * Verify that cwd is sane.
1148 if (stat(curdir, &sa) == -1) {
1149 (void)fprintf(stderr, "%s: %s: %s.\n",
1150 progname, curdir, strerror(errno));
1155 * All this code is so that we know where we are when we start up
1156 * on a different machine with pmake.
1157 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
1158 * since the value of curdir can vary depending on how we got
1159 * here. Ie sitting at a shell prompt (shell that provides $PWD)
1160 * or via subdir.mk in which case its likely a shell which does
1162 * So, to stop it breaking this case only, we ignore PWD if
1163 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
1165 #ifndef NO_PWD_OVERRIDE
1167 char *pwd, *ptmp1 = NULL, *ptmp2 = NULL;
1169 if ((pwd = getenv("PWD")) != NULL &&
1170 Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &ptmp1) == NULL) {
1171 const char *makeobjdir = Var_Value("MAKEOBJDIR",
1174 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
1175 if (stat(pwd, &sb) == 0 &&
1176 sa.st_ino == sb.st_ino &&
1177 sa.st_dev == sb.st_dev)
1178 (void)strncpy(curdir, pwd, MAXPATHLEN);
1185 Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
1188 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that,
1189 * MAKEOBJDIR is set in the environment, try only that value
1190 * and fall back to .CURDIR if it does not exist.
1192 * Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
1193 * and * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none
1194 * of these paths exist, just use .CURDIR.
1197 (void)Main_SetObjdir("%s", curdir);
1199 if (!Main_SetVarObjdir("MAKEOBJDIRPREFIX", curdir) &&
1200 !Main_SetVarObjdir("MAKEOBJDIR", "") &&
1201 !Main_SetObjdir("%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
1202 !Main_SetObjdir("%s.%s", _PATH_OBJDIR, machine) &&
1203 !Main_SetObjdir("%s", _PATH_OBJDIR))
1204 (void)Main_SetObjdir("%s%s", _PATH_OBJDIRPREFIX, curdir);
1207 * Initialize archive, target and suffix modules in preparation for
1208 * parsing the makefile(s)
1213 Trace_Init(tracefile);
1218 Trace_Log(MAKESTART, NULL);
1221 * Set up the .TARGETS variable to contain the list of targets to be
1222 * created. If none specified, make the variable empty -- the parser
1223 * will fill the thing in with the default or .MAIN target.
1225 if (!Lst_IsEmpty(create)) {
1228 for (ln = Lst_First(create); ln != NULL;
1229 ln = Lst_Succ(ln)) {
1230 char *name = (char *)Lst_Datum(ln);
1232 Var_Append(".TARGETS", name, VAR_GLOBAL);
1235 Var_Set(".TARGETS", "", VAR_GLOBAL, 0);
1239 * If no user-supplied system path was given (through the -m option)
1240 * add the directories from the DEFSYSPATH (more than one may be given
1241 * as dir1:...:dirn) to the system include path.
1243 if (syspath == NULL || *syspath == '\0')
1244 syspath = defsyspath;
1246 syspath = bmake_strdup(syspath);
1248 for (start = syspath; *start != '\0'; start = cp) {
1249 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1254 /* look for magic parent directory search string */
1255 if (strncmp(".../", start, 4) != 0) {
1256 (void)Dir_AddDir(defIncPath, start);
1258 if (Dir_FindHereOrAbove(curdir, start+4,
1259 found_path, sizeof(found_path))) {
1260 (void)Dir_AddDir(defIncPath, found_path);
1264 if (syspath != defsyspath)
1268 * Read in the built-in rules first, followed by the specified
1269 * makefile, if it was (makefile != NULL), or the default
1270 * makefile and Makefile, in that order, if it wasn't.
1275 sysMkPath = Lst_Init(FALSE);
1276 Dir_Expand(_PATH_DEFSYSMK,
1277 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
1279 if (Lst_IsEmpty(sysMkPath))
1280 Fatal("%s: no system rules (%s).", progname,
1282 ln = Lst_Find(sysMkPath, NULL, ReadMakefile);
1284 Fatal("%s: cannot open %s.", progname,
1285 (char *)Lst_Datum(ln));
1288 if (!Lst_IsEmpty(makefiles)) {
1291 ln = Lst_Find(makefiles, NULL, ReadAllMakefiles);
1293 Fatal("%s: cannot open %s.", progname,
1294 (char *)Lst_Datum(ln));
1296 p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}",
1297 VAR_CMD, VARF_WANTRES);
1299 (void)str2Lst_Append(makefiles, p1, NULL);
1300 (void)Lst_Find(makefiles, NULL, ReadMakefile);
1305 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
1306 if (!noBuiltins || !printVars) {
1307 makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}",
1308 VAR_CMD, VARF_WANTRES);
1309 doing_depend = TRUE;
1310 (void)ReadMakefile(makeDependfile, NULL);
1311 doing_depend = FALSE;
1315 printf("%s: Entering directory `%s'\n", progname, objdir);
1319 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
1322 if (!forceJobs && !compatMake &&
1323 Var_Exists(".MAKE.JOBS", VAR_GLOBAL)) {
1327 value = Var_Subst(NULL, "${.MAKE.JOBS}", VAR_GLOBAL, VARF_WANTRES);
1328 n = strtol(value, NULL, 0);
1330 (void)fprintf(stderr, "%s: illegal value for .MAKE.JOBS -- must be positive integer!\n",
1335 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
1336 Var_Append(MAKEFLAGS, value, VAR_GLOBAL);
1339 maxJobTokens = maxJobs;
1345 * Be compatible if user did not specify -j and did not explicitly
1346 * turned compatibility on
1348 if (!compatMake && !forceJobs) {
1353 Job_ServerStart(maxJobTokens, jp_0, jp_1);
1355 fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1356 jp_0, jp_1, maxJobs, maxJobTokens, compatMake);
1359 Main_ExportMAKEFLAGS(TRUE); /* initial export */
1362 * For compatibility, look at the directories in the VPATH variable
1363 * and add them to the search path, if the variable is defined. The
1364 * variable's value is in the same format as the PATH envariable, i.e.
1365 * <directory>:<directory>:<directory>...
1367 if (Var_Exists("VPATH", VAR_CMD)) {
1370 * GCC stores string constants in read-only memory, but
1371 * Var_Subst will want to write this thing, so store it
1374 static char VPATH[] = "${VPATH}";
1376 vpath = Var_Subst(NULL, VPATH, VAR_CMD, VARF_WANTRES);
1379 /* skip to end of directory */
1380 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
1382 /* Save terminator character so know when to stop */
1385 /* Add directory to search path */
1386 (void)Dir_AddDir(dirSearchPath, path);
1389 } while (savec == ':');
1394 * Now that all search paths have been read for suffixes et al, it's
1395 * time to add the default search path to their lists...
1400 * Propagate attributes through :: dependency lists.
1404 /* print the initial graph, if the user requested it */
1408 /* print the values of any variables requested by the user */
1416 expandVars = getBoolean(".MAKE.EXPAND_VARIABLES", FALSE);
1417 for (ln = Lst_First(variables); ln != NULL;
1418 ln = Lst_Succ(ln)) {
1419 char *var = (char *)Lst_Datum(ln);
1422 if (strchr(var, '$')) {
1423 value = p1 = Var_Subst(NULL, var, VAR_GLOBAL,
1425 } else if (expandVars) {
1428 if (snprintf(tmp, sizeof(tmp), "${%s}", var) >= (int)(sizeof(tmp)))
1429 Fatal("%s: variable name too big: %s",
1431 value = p1 = Var_Subst(NULL, tmp, VAR_GLOBAL,
1434 value = Var_Value(var, VAR_GLOBAL, &p1);
1436 printf("%s\n", value ? value : "");
1441 * Have now read the entire graph and need to make a list of
1442 * targets to create. If none was given on the command line,
1443 * we consult the parsing module to find the main target(s)
1446 if (Lst_IsEmpty(create))
1447 targs = Parse_MainName();
1449 targs = Targ_FindList(create, TARG_CREATE);
1453 * Initialize job module before traversing the graph
1454 * now that any .BEGIN and .END targets have been read.
1455 * This is done only if the -q flag wasn't given
1456 * (to prevent the .BEGIN from being executed should
1464 /* Traverse the graph, checking on all the targets */
1465 outOfDate = Make_Run(targs);
1468 * Compat_Init will take care of creating all the
1469 * targets as well as initializing the module.
1476 Lst_Destroy(targs, NULL);
1477 Lst_Destroy(variables, NULL);
1478 Lst_Destroy(makefiles, NULL);
1479 Lst_Destroy(create, (FreeProc *)free);
1482 /* print the graph now it's been processed if the user requested it */
1486 Trace_Log(MAKEEND, 0);
1489 printf("%s: Leaving directory `%s'\n", progname, objdir);
1491 printf("%s: Leaving directory `%s'\n", progname, curdir);
1505 return outOfDate ? 1 : 0;
1510 * Open and parse the given makefile.
1513 * 0 if ok. -1 if couldn't open file.
1519 ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
1521 const char *fname = p; /* makefile to read */
1523 size_t len = MAXPATHLEN;
1524 char *name, *path = bmake_malloc(len);
1526 if (!strcmp(fname, "-")) {
1527 Parse_File(NULL /*stdin*/, -1);
1528 Var_Set("MAKEFILE", "", VAR_INTERNAL, 0);
1530 /* if we've chdir'd, rebuild the path name */
1531 if (strcmp(curdir, objdir) && *fname != '/') {
1532 size_t plen = strlen(curdir) + strlen(fname) + 2;
1534 path = bmake_realloc(path, len = 2 * plen);
1536 (void)snprintf(path, len, "%s/%s", curdir, fname);
1537 fd = open(path, O_RDONLY);
1543 /* If curdir failed, try objdir (ala .depend) */
1544 plen = strlen(objdir) + strlen(fname) + 2;
1546 path = bmake_realloc(path, len = 2 * plen);
1547 (void)snprintf(path, len, "%s/%s", objdir, fname);
1548 fd = open(path, O_RDONLY);
1554 fd = open(fname, O_RDONLY);
1558 /* look in -I and system include directories. */
1559 name = Dir_FindFile(fname, parseIncPath);
1561 name = Dir_FindFile(fname,
1562 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
1563 if (!name || (fd = open(name, O_RDONLY)) == -1) {
1570 * set the MAKEFILE variable desired by System V fans -- the
1571 * placement of the setting here means it gets set to the last
1572 * makefile specified, as it is set by SysV make.
1576 Var_Set("MAKEFILE", fname, VAR_INTERNAL, 0);
1577 Parse_File(fname, fd);
1587 * Execute the command in cmd, and return the output of that command
1591 * A string containing the output of the command, or the empty string
1592 * If errnum is not NULL, it contains the reason for the command failure
1595 * The string must be freed by the caller.
1598 Cmd_Exec(const char *cmd, const char **errnum)
1600 const char *args[4]; /* Args for invoking the shell */
1601 int fds[2]; /* Pipe streams */
1602 int cpid; /* Child PID */
1603 int pid; /* PID from wait() */
1604 char *res; /* result */
1605 WAIT_T status; /* command exit status */
1606 Buffer buf; /* buffer to store the result */
1608 int cc; /* bytes read, or -1 */
1609 int savederr; /* saved errno */
1617 * Set up arguments for shell
1619 args[0] = shellName;
1625 * Open a pipe for fetching its output
1627 if (pipe(fds) == -1) {
1628 *errnum = "Couldn't create pipe for \"%s\"";
1635 switch (cpid = vFork()) {
1638 * Close input side of pipe
1640 (void)close(fds[0]);
1643 * Duplicate the output stream to the shell's output, then
1644 * shut the extra thing down. Note we don't fetch the error
1645 * stream...why not? Why?
1647 (void)dup2(fds[1], 1);
1648 (void)close(fds[1]);
1652 (void)execv(shellPath, UNCONST(args));
1657 *errnum = "Couldn't exec \"%s\"";
1662 * No need for the writing half
1664 (void)close(fds[1]);
1670 char result[BUFSIZ];
1671 cc = read(fds[0], result, sizeof(result));
1673 Buf_AddBytes(&buf, cc, result);
1675 while (cc > 0 || (cc == -1 && errno == EINTR));
1680 * Close the input side of the pipe.
1682 (void)close(fds[0]);
1685 * Wait for the process to exit.
1687 while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
1688 JobReapChild(pid, status, FALSE);
1691 cc = Buf_Size(&buf);
1692 res = Buf_Destroy(&buf, FALSE);
1695 *errnum = "Couldn't read shell's output for \"%s\"";
1697 if (WIFSIGNALED(status))
1698 *errnum = "\"%s\" exited on a signal";
1699 else if (WEXITSTATUS(status) != 0)
1700 *errnum = "\"%s\" returned non-zero status";
1703 * Null-terminate the result, convert newlines to spaces and
1704 * install it in the variable.
1709 if (cc > 0 && *--cp == '\n') {
1711 * A final newline is just stripped
1725 res = bmake_malloc(1);
1732 * Print an error message given its format.
1738 * The message is printed.
1742 Error(const char *fmt, ...)
1747 err_file = debug_file;
1748 if (err_file == stdout)
1750 (void)fflush(stdout);
1753 fprintf(err_file, "%s: ", progname);
1754 (void)vfprintf(err_file, fmt, ap);
1756 (void)fprintf(err_file, "\n");
1757 (void)fflush(err_file);
1758 if (err_file == stderr)
1766 * Produce a Fatal error message. If jobs are running, waits for them
1777 Fatal(const char *fmt, ...)
1785 (void)fflush(stdout);
1786 (void)vfprintf(stderr, fmt, ap);
1788 (void)fprintf(stderr, "\n");
1789 (void)fflush(stderr);
1791 PrintOnError(NULL, NULL);
1793 if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
1795 Trace_Log(MAKEERROR, 0);
1796 exit(2); /* Not 1 so -q can distinguish error */
1801 * Major exception once jobs are being created. Kills all jobs, prints
1802 * a message and exits.
1808 * All children are killed indiscriminately and the program Lib_Exits
1812 Punt(const char *fmt, ...)
1817 (void)fflush(stdout);
1818 (void)fprintf(stderr, "%s: ", progname);
1819 (void)vfprintf(stderr, fmt, ap);
1821 (void)fprintf(stderr, "\n");
1822 (void)fflush(stderr);
1824 PrintOnError(NULL, NULL);
1831 * Exit without giving a message.
1846 Trace_Log(MAKEERROR, 0);
1847 exit(2); /* Not 1, so -q can distinguish error */
1852 * Called when aborting due to errors in child shell to signal
1863 /* number of errors encountered in Make_Make */
1865 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
1870 * Remove a file carefully, avoiding directories.
1873 eunlink(const char *file)
1877 if (lstat(file, &st) == -1)
1880 if (S_ISDIR(st.st_mode)) {
1884 return unlink(file);
1889 * Print why exec failed, avoiding stdio.
1892 execError(const char *af, const char *av)
1896 struct iovec iov[8];
1898 (void)(iov[i].iov_base = UNCONST(s), \
1899 iov[i].iov_len = strlen(iov[i].iov_base), \
1902 #define IOADD(s) (void)write(2, s, strlen(s))
1910 IOADD(") failed (");
1911 IOADD(strerror(errno));
1915 while (writev(2, iov, 8) == -1 && errno == EAGAIN)
1922 * exit with usage message
1928 if ((p = strchr(progname, '[')) != NULL)
1931 (void)fprintf(stderr,
1932 "usage: %s [-BeikNnqrstWwX] \n\
1933 [-C directory] [-D variable] [-d flags] [-f makefile]\n\
1934 [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n\
1935 [-V variable] [variable=value] [target ...]\n", progname);
1940 * realpath(3) can get expensive, cache results...
1942 static GNode *cached_realpaths = NULL;
1945 get_cached_realpaths(void)
1948 if (!cached_realpaths) {
1949 cached_realpaths = Targ_NewGN("Realpath");
1950 #ifndef DEBUG_REALPATH_CACHE
1951 cached_realpaths->flags = INTERNAL;
1955 return cached_realpaths;
1958 /* purge any relative paths */
1960 purge_cached_realpaths(void)
1962 GNode *cache = get_cached_realpaths();
1963 Hash_Entry *he, *nhe;
1966 he = Hash_EnumFirst(&cache->context, &hs);
1968 nhe = Hash_EnumNext(&hs);
1969 if (he->name[0] != '/') {
1971 fprintf(stderr, "cached_realpath: purging %s\n", he->name);
1972 Hash_DeleteEntry(&cache->context, he);
1979 cached_realpath(const char *pathname, char *resolved)
1984 if (!pathname || !pathname[0])
1987 cache = get_cached_realpaths();
1989 if ((rp = Var_Value(pathname, cache, &cp)) != NULL) {
1991 strlcpy(resolved, rp, MAXPATHLEN);
1992 } else if ((rp = realpath(pathname, resolved)) != NULL) {
1993 Var_Set(pathname, rp, cache, 0);
1996 return rp ? resolved : NULL;
2000 PrintAddr(void *a, void *b)
2002 printf("%lx ", (unsigned long) a);
2008 addErrorCMD(void *cmdp, void *gnp MAKE_ATTR_UNUSED)
2011 return 1; /* stop */
2012 Var_Append(".ERROR_CMD", cmdp, VAR_GLOBAL);
2017 PrintOnError(GNode *gn, const char *s)
2019 static GNode *en = NULL;
2026 printf("\n%s: stopped in %s\n", progname, curdir);
2029 return; /* we've been here! */
2032 * We can print this even if there is no .ERROR target.
2034 Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL, 0);
2035 Var_Delete(".ERROR_CMD", VAR_GLOBAL);
2036 Lst_ForEach(gn->commands, addErrorCMD, gn);
2038 strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
2040 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
2049 * Finally, see if there is a .ERROR target, and run it if so.
2051 en = Targ_FindNode(".ERROR", TARG_NOCREATE);
2053 en->type |= OP_SPECIAL;
2054 Compat_Make(en, en);
2059 Main_ExportMAKEFLAGS(Boolean first)
2061 static int once = 1;
2069 strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
2071 s = Var_Subst(NULL, tmp, VAR_CMD, VARF_WANTRES);
2074 setenv("MAKEFLAGS", s, 1);
2076 setenv("MAKE", s, 1);
2084 static char *tmpdir = NULL;
2090 * Honor $TMPDIR but only if it is valid.
2091 * Ensure it ends with /.
2093 tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL,
2095 if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
2097 tmpdir = bmake_strdup(_PATH_TMP);
2104 * Create and open a temp file using "pattern".
2105 * If "fnamep" is provided set it to a copy of the filename created.
2106 * Otherwise unlink the file once open.
2109 mkTempFile(const char *pattern, char **fnamep)
2111 static char *tmpdir = NULL;
2112 char tfile[MAXPATHLEN];
2118 tmpdir = getTmpdir();
2119 if (pattern[0] == '/') {
2120 snprintf(tfile, sizeof(tfile), "%s", pattern);
2122 snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
2124 if ((fd = mkstemp(tfile)) < 0)
2125 Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
2127 *fnamep = bmake_strdup(tfile);
2129 unlink(tfile); /* we just want the descriptor */
2135 * Convert a string representation of a boolean.
2136 * Anything that looks like "No", "False", "Off", "0" etc,
2137 * is FALSE, otherwise TRUE.
2140 s2Boolean(const char *s, Boolean bf)
2144 case '\0': /* not set - the default wins */
2174 * Return a Boolean based on setting of a knob.
2176 * If the knob is not set, the supplied default is the return value.
2177 * If set, anything that looks or smells like "No", "False", "Off", "0" etc,
2178 * is FALSE, otherwise TRUE.
2181 getBoolean(const char *name, Boolean bf)
2186 if (snprintf(tmp, sizeof(tmp), "${%s:U:tl}", name) < (int)(sizeof(tmp))) {
2187 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
2190 bf = s2Boolean(cp, bf);