1 /* $NetBSD: main.c,v 1.331 2020/08/30 19:56:02 rillig 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.331 2020/08/30 19:56:02 rillig 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.331 2020/08/30 19:56:02 rillig 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 int printVars; /* -[vV] argument */
163 #define COMPAT_VARS 1
164 #define EXPAND_VARS 2
165 static Lst variables; /* list of variables to print
167 int maxJobs; /* -j argument */
168 static int maxJobTokens; /* -j argument */
169 Boolean compatMake; /* -B argument */
170 int debug; /* -d argument */
171 Boolean debugVflag; /* -dV */
172 Boolean noExecute; /* -n flag */
173 Boolean noRecursiveExecute; /* -N flag */
174 Boolean keepgoing; /* -k flag */
175 Boolean queryFlag; /* -q flag */
176 Boolean touchFlag; /* -t flag */
177 Boolean enterFlag; /* -w flag */
178 Boolean enterFlagObj; /* -w and objdir != srcdir */
179 Boolean ignoreErrors; /* -i flag */
180 Boolean beSilent; /* -s flag */
181 Boolean oldVars; /* variable substitution style */
182 Boolean checkEnvFirst; /* -e flag */
183 Boolean parseWarnFatal; /* -W flag */
184 static int jp_0 = -1, jp_1 = -1; /* ends of parent job pipe */
185 Boolean varNoExportEnv; /* -X flag */
186 Boolean doing_depend; /* Set while reading .depend */
187 static Boolean jobsRunning; /* TRUE if the jobs might be running */
188 static const char * tracefile;
189 static void MainParseArgs(int, char **);
190 static int ReadMakefile(const char *);
191 static void usage(void) MAKE_ATTR_DEAD;
192 static void purge_cached_realpaths(void);
194 static Boolean ignorePWD; /* if we use -C, PWD is meaningless */
195 static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
196 char curdir[MAXPATHLEN + 1]; /* Startup directory */
197 char *progname; /* the program name */
198 char *makeDependfile;
204 Boolean forceJobs = FALSE;
207 * On some systems MACHINE is defined as something other than
212 # define MACHINE FORCE_MACHINE
215 extern Lst parseIncPath;
218 * For compatibility with the POSIX version of MAKEFLAGS that includes
219 * all the options with out -, convert flags to -f -l -a -g -s.
222 explode(const char *flags)
231 for (f = flags; *f; f++)
232 if (!isalpha((unsigned char)*f))
236 return bmake_strdup(flags);
239 st = nf = bmake_malloc(len * 3 + 1);
250 parse_debug_options(const char *argvalue)
257 for (modules = argvalue; *modules; ++modules) {
260 debug = ~(0|DEBUG_LINT);
275 debug |= DEBUG_ERROR;
281 if (modules[1] == '1') {
282 debug |= DEBUG_GRAPH1;
285 else if (modules[1] == '2') {
286 debug |= DEBUG_GRAPH2;
289 else if (modules[1] == '3') {
290 debug |= DEBUG_GRAPH3;
313 debug |= DEBUG_SCRIPT;
316 debug |= DEBUG_PARSE;
331 debug |= DEBUG_SHELL;
334 if (debug_file != stdout && debug_file != stderr)
336 if (*++modules == '+') {
341 if (strcmp(modules, "stdout") == 0) {
345 if (strcmp(modules, "stderr") == 0) {
349 len = strlen(modules);
350 fname = bmake_malloc(len + 20);
351 memcpy(fname, modules, len + 1);
352 /* Let the filename be modified by the pid */
353 if (strcmp(fname + len - 3, ".%d") == 0)
354 snprintf(fname + len - 2, 20, "%d", getpid());
355 debug_file = fopen(fname, mode);
357 fprintf(stderr, "Cannot open debug file %s\n",
364 (void)fprintf(stderr,
365 "%s: illegal argument to d option -- %c\n",
372 * Make the debug_file unbuffered, and make
373 * stdout line buffered (unless debugfile == stdout).
375 setvbuf(debug_file, NULL, _IONBF, 0);
376 if (debug_file != stdout) {
377 setvbuf(stdout, NULL, _IOLBF, 0);
382 * does path contain any relative components
385 is_relpath(const char *path)
392 while ((cp = strstr(cp, "/.")) != NULL) {
394 if (cp[0] == '/' || cp[0] == '\0')
396 else if (cp[0] == '.') {
397 if (cp[1] == '/' || cp[1] == '\0')
406 * Parse a given argument vector. Called from main() and from
407 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
409 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
415 * Various global and local flags will be set depending on the flags
419 MainParseArgs(int argc, char **argv)
425 const char *getopt_def;
428 Boolean inOption, dashDash = FALSE;
429 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
431 #define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w"
432 /* Can't actually use getopt(3) because rescanning is not portable */
434 getopt_def = OPTFLAGS;
452 if (c != '-' || dashDash)
457 /* '-' found at some earlier point */
458 getopt_spec = strchr(getopt_def, c);
459 if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') {
460 /* -<something> found, and <something> should have an arg */
464 if(*argvalue == '\0') {
480 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL);
481 Var_Set(MAKE_MODE, "compat", VAR_GLOBAL);
484 if (chdir(argvalue) == -1) {
485 (void)fprintf(stderr,
486 "%s: chdir %s: %s\n",
491 if (getcwd(curdir, MAXPATHLEN) == NULL) {
492 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
495 if (!is_relpath(argvalue) &&
496 stat(argvalue, &sa) != -1 &&
497 stat(curdir, &sb) != -1 &&
498 sa.st_ino == sb.st_ino &&
499 sa.st_dev == sb.st_dev)
500 strncpy(curdir, argvalue, MAXPATHLEN);
504 if (argvalue == NULL || argvalue[0] == 0) goto noarg;
505 Var_Set(argvalue, "1", VAR_GLOBAL);
506 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
507 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
510 if (argvalue == NULL) goto noarg;
511 Parse_AddIncludeDir(argvalue);
512 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
513 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
516 if (argvalue == NULL) goto noarg;
517 if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) {
518 (void)fprintf(stderr,
519 "%s: internal error -- J option malformed (%s)\n",
523 if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
524 (fcntl(jp_1, F_GETFD, 0) < 0)) {
526 (void)fprintf(stderr,
527 "%s: ###### warning -- J descriptors were closed!\n",
535 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL);
536 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
541 noRecursiveExecute = TRUE;
542 Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL);
546 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
549 if (argvalue == NULL) goto noarg;
550 tracefile = bmake_strdup(argvalue);
551 Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL);
552 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
556 if (argvalue == NULL) goto noarg;
557 printVars = c == 'v' ? EXPAND_VARS : COMPAT_VARS;
558 Lst_Append(variables, argvalue);
559 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL);
560 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
563 parseWarnFatal = TRUE;
566 varNoExportEnv = TRUE;
567 Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL);
570 if (argvalue == NULL) goto noarg;
571 /* If '-d-opts' don't pass to children */
572 if (argvalue[0] == '-')
575 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
576 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
578 parse_debug_options(argvalue);
581 checkEnvFirst = TRUE;
582 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
585 if (argvalue == NULL) goto noarg;
586 Lst_Append(makefiles, argvalue);
590 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
593 if (argvalue == NULL) goto noarg;
595 maxJobs = strtol(argvalue, &p, 0);
596 if (*p != '\0' || maxJobs < 1) {
597 (void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n",
601 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
602 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
603 Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL);
604 maxJobTokens = maxJobs;
608 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
611 if (argvalue == NULL) goto noarg;
612 /* look for magic parent directory search string */
613 if (strncmp(".../", argvalue, 4) == 0) {
614 if (!Dir_FindHereOrAbove(curdir, argvalue+4,
615 found_path, sizeof(found_path)))
616 break; /* nothing doing */
617 (void)Dir_AddDir(sysIncPath, found_path);
619 (void)Dir_AddDir(sysIncPath, argvalue);
621 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL);
622 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL);
626 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
630 /* Kind of nonsensical, wot? */
631 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
635 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
639 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
643 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
647 Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
655 fprintf(stderr, "getopt(%s) -> %d (%c)\n",
667 * See if the rest of the arguments are variable assignments and
668 * perform them if so. Else take them to be targets and stuff them
669 * on the end of the "create" list.
671 for (; argc > 1; ++argv, --argc)
672 if (Parse_IsVar(argv[1])) {
673 Parse_DoVar(argv[1], VAR_CMD);
676 Punt("illegal (null) argument.");
677 if (*argv[1] == '-' && !dashDash)
679 Lst_Append(create, bmake_strdup(argv[1]));
684 (void)fprintf(stderr, "%s: option requires an argument -- %c\n",
690 * Main_ParseArgLine --
691 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
692 * is encountered and by main() when reading the .MAKEFLAGS envariable.
693 * Takes a line of arguments and breaks it into its
694 * component words and passes those words and the number of them to the
695 * MainParseArgs function.
696 * The line should have all its leading whitespace removed.
699 * line Line to fracture
705 * Only those that come from the various arguments.
708 Main_ParseArgLine(const char *line)
712 const char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1);
717 for (; *line == ' '; ++line)
725 * $MAKE may simply be naming the make(1) binary
729 if (!(cp = strrchr(line, '/')))
731 if ((cp = strstr(cp, "make")) &&
732 strcmp(cp, "make") == 0)
736 buf = str_concat3(argv0, " ", line);
739 words = Str_Words(buf, TRUE);
740 if (words.words == NULL) {
741 Error("Unterminated quoted string [%s]", buf);
746 MainParseArgs((int)words.len, words.words);
752 Main_SetObjdir(const char *fmt, ...)
756 char buf[MAXPATHLEN + 1];
757 char buf2[MAXPATHLEN + 1];
762 vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
765 if (path[0] != '/') {
766 snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path);
770 /* look for the directory and try to chdir there */
771 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
773 (void)fprintf(stderr, "make warning: %s: %s.\n",
774 path, strerror(errno));
776 snprintf(objdir, sizeof objdir, "%s", path);
777 Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
778 setenv("PWD", objdir, 1);
780 purge_cached_realpaths();
782 if (enterFlag && strcmp(objdir, curdir) != 0)
791 Main_SetVarObjdir(const char *var, const char *suffix)
794 const char *path = Var_Value(var, VAR_CMD, &path_freeIt);
798 if (path == NULL || path[0] == '\0') {
799 bmake_free(path_freeIt);
803 /* expand variable substitutions */
806 if (strchr(path, '$') != 0)
807 xpath = xpath_freeIt = Var_Subst(path, VAR_GLOBAL,
810 (void)Main_SetObjdir("%s%s", xpath, suffix);
812 bmake_free(xpath_freeIt);
813 bmake_free(path_freeIt);
817 /* Read and parse the makefile.
818 * Return TRUE if reading the makefile succeeded, for Lst_Find. */
820 ReadMakefileSucceeded(const void *fname, const void *unused)
822 return ReadMakefile(fname) == 0;
825 /* Read and parse the makefile.
826 * Return TRUE if reading the makefile failed, for Lst_Find. */
828 ReadMakefileFailed(const void *fname, const void *unused)
830 return ReadMakefile(fname) != 0;
834 str2Lst_Append(Lst lp, char *str, const char *sep)
842 for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) {
852 siginfo(int signo MAKE_ATTR_UNUSED)
854 char dir[MAXPATHLEN];
855 char str[2 * MAXPATHLEN];
857 if (getcwd(dir, sizeof(dir)) == NULL)
859 len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir);
861 (void)write(STDERR_FILENO, str, (size_t)len);
866 * Allow makefiles some control over the mode we run in.
869 MakeMode(const char *mode)
871 char *mode_freeIt = NULL;
874 mode = mode_freeIt = Var_Subst("${" MAKE_MODE ":tl}",
875 VAR_GLOBAL, VARE_WANTRES);
877 if (mode[0] != '\0') {
878 if (strstr(mode, "compat")) {
883 if (strstr(mode, "meta"))
884 meta_mode_init(mode);
897 if (printVars == EXPAND_VARS)
902 expandVars = getBoolean(".MAKE.EXPAND_VARIABLES", FALSE);
904 for (ln = Lst_First(variables); ln != NULL; ln = LstNode_Next(ln)) {
905 char *var = LstNode_Datum(ln);
909 if (strchr(var, '$')) {
910 value = p1 = Var_Subst(var, VAR_GLOBAL, VARE_WANTRES);
911 } else if (expandVars) {
913 int len = snprintf(tmp, sizeof(tmp), "${%s}", var);
915 if (len >= (int)sizeof(tmp))
916 Fatal("%s: variable name too big: %s",
918 value = p1 = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
920 value = Var_Value(var, VAR_GLOBAL, &p1);
922 printf("%s\n", value ? value : "");
930 Lst targs; /* target nodes to create -- passed to Make_Init */
931 Boolean outOfDate; /* FALSE if all targets up to date */
934 * Have now read the entire graph and need to make a list of
935 * targets to create. If none was given on the command line,
936 * we consult the parsing module to find the main target(s)
939 if (Lst_IsEmpty(create))
940 targs = Parse_MainName();
942 targs = Targ_FindList(create, TARG_CREATE);
946 * Initialize job module before traversing the graph
947 * now that any .BEGIN and .END targets have been read.
948 * This is done only if the -q flag wasn't given
949 * (to prevent the .BEGIN from being executed should
957 /* Traverse the graph, checking on all the targets */
958 outOfDate = Make_Run(targs);
961 * Compat_Init will take care of creating all the
962 * targets as well as initializing the module.
973 * The main function, for obvious reasons. Initializes variables
974 * and a few modules, then parses the arguments give it in the
975 * environment and on the command line. Reads the system makefile
976 * followed by either Makefile, makefile or the file given by the
977 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
978 * flags it has received by then uses either the Make or the Compat
979 * module to create the initial list of targets.
982 * If -q was given, exits -1 if anything was out-of-date. Else it exits
986 * The program exits when done. Targets are created. etc. etc. etc.
989 main(int argc, char **argv)
991 Boolean outOfDate; /* FALSE if all targets up to date */
994 char mdpath[MAXPATHLEN];
996 const char *machine = FORCE_MACHINE;
998 const char *machine = getenv("MACHINE");
1000 const char *machine_arch = getenv("MACHINE_ARCH");
1001 char *syspath = getenv("MAKESYSPATH");
1002 Lst sysMkPath; /* Path of sys.mk */
1003 char *cp = NULL, *start;
1004 /* avoid faults on read-only strings */
1005 static char defsyspath[] = _PATH_DEFSYSPATH;
1006 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */
1007 struct timeval rightnow; /* to initialize random seed */
1008 struct utsname utsname;
1010 /* default to writing debug to stderr */
1011 debug_file = stderr;
1014 (void)bmake_signal(SIGINFO, siginfo);
1017 * Set the seed to produce a different random sequence
1018 * on each program execution.
1020 gettimeofday(&rightnow, NULL);
1021 srandom(rightnow.tv_sec + rightnow.tv_usec);
1023 if ((progname = strrchr(argv[0], '/')) != NULL)
1027 #if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
1029 * get rid of resource limit on file descriptors
1033 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
1034 rl.rlim_cur != rl.rlim_max) {
1035 rl.rlim_cur = rl.rlim_max;
1036 (void)setrlimit(RLIMIT_NOFILE, &rl);
1041 if (uname(&utsname) == -1) {
1042 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
1048 * Get the name of this type of MACHINE from utsname
1049 * so we can share an executable for similar machines.
1050 * (i.e. m68k: amiga hp300, mac68k, sun3, ...)
1052 * Note that both MACHINE and MACHINE_ARCH are decided at
1057 machine = utsname.machine;
1060 machine = MAKE_MACHINE;
1062 machine = "unknown";
1067 if (!machine_arch) {
1068 #if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL) && defined(CTL_HW) && defined(HW_MACHINE_ARCH)
1069 static char machine_arch_buf[sizeof(utsname.machine)];
1070 int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
1071 size_t len = sizeof(machine_arch_buf);
1073 if (sysctl(mib, __arraycount(mib), machine_arch_buf,
1074 &len, NULL, 0) < 0) {
1075 (void)fprintf(stderr, "%s: sysctl failed (%s).\n", progname,
1080 machine_arch = machine_arch_buf;
1082 #ifndef MACHINE_ARCH
1083 #ifdef MAKE_MACHINE_ARCH
1084 machine_arch = MAKE_MACHINE_ARCH;
1086 machine_arch = "unknown";
1089 machine_arch = MACHINE_ARCH;
1094 myPid = getpid(); /* remember this for vFork() */
1097 * Just in case MAKEOBJDIR wants us to do something tricky.
1099 Var_Init(); /* Initialize the lists of variables for
1100 * parsing arguments */
1101 Var_Set(".MAKE.OS", utsname.sysname, VAR_GLOBAL);
1102 Var_Set("MACHINE", machine, VAR_GLOBAL);
1103 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
1105 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
1107 Var_Set(".newline", "\n", VAR_GLOBAL); /* handy for :@ loops */
1109 * This is the traditional preference for makefiles.
1111 #ifndef MAKEFILE_PREFERENCE_LIST
1112 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
1114 Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST,
1116 Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL);
1118 create = Lst_Init();
1119 makefiles = Lst_Init();
1122 variables = Lst_Init();
1123 beSilent = FALSE; /* Print commands as executed */
1124 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
1125 noExecute = FALSE; /* Execute all commands */
1126 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */
1127 keepgoing = FALSE; /* Stop on error */
1128 allPrecious = FALSE; /* Remove targets when interrupted */
1129 deleteOnError = FALSE; /* Historical default behavior */
1130 queryFlag = FALSE; /* This is not just a check-run */
1131 noBuiltins = FALSE; /* Read the built-in rules */
1132 touchFlag = FALSE; /* Actually update targets */
1133 debug = 0; /* No debug verbosity, please. */
1134 jobsRunning = FALSE;
1136 maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */
1137 maxJobTokens = maxJobs;
1138 compatMake = FALSE; /* No compat mode */
1142 * Initialize the parsing, directory and variable modules to prepare
1143 * for the reading of inclusion paths and variable settings on the
1148 * Initialize various variables.
1149 * MAKE also gets this name, for compatibility
1150 * .MAKEFLAGS gets set to the empty string just in case.
1151 * MFLAGS also gets initialized empty, for compatibility.
1154 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) {
1156 * Leave alone if it is an absolute path, or if it does
1157 * not contain a '/' in which case we need to find it in
1158 * the path, like execvp(3) and the shells do.
1163 * A relative path, canonicalize it.
1165 p1 = cached_realpath(argv[0], mdpath);
1166 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) {
1167 p1 = argv[0]; /* realpath failed */
1170 Var_Set("MAKE", p1, VAR_GLOBAL);
1171 Var_Set(".MAKE", p1, VAR_GLOBAL);
1172 Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
1173 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL);
1174 Var_Set("MFLAGS", "", VAR_GLOBAL);
1175 Var_Set(".ALLTARGETS", "", VAR_GLOBAL);
1176 /* some makefiles need to know this */
1177 Var_Set(MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV, VAR_CMD);
1180 * Set some other useful macros
1185 makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
1188 snprintf(tmp, sizeof(tmp), "%d", makelevel);
1189 Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL);
1190 snprintf(tmp, sizeof(tmp), "%u", myPid);
1191 Var_Set(".MAKE.PID", tmp, VAR_GLOBAL);
1192 snprintf(tmp, sizeof(tmp), "%u", getppid());
1193 Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL);
1195 if (makelevel > 0) {
1197 snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
1198 progname = bmake_strdup(pn);
1207 * First snag any flags out of the MAKE environment variable.
1208 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
1209 * in a different format).
1212 p1 = explode(getenv("MAKEFLAGS"));
1213 Main_ParseArgLine(p1);
1216 Main_ParseArgLine(getenv("MAKE"));
1220 * Find where we are (now).
1221 * We take care of PWD for the automounter below...
1223 if (getcwd(curdir, MAXPATHLEN) == NULL) {
1224 (void)fprintf(stderr, "%s: getcwd: %s.\n",
1225 progname, strerror(errno));
1229 MainParseArgs(argc, argv);
1232 printf("%s: Entering directory `%s'\n", progname, curdir);
1235 * Verify that cwd is sane.
1237 if (stat(curdir, &sa) == -1) {
1238 (void)fprintf(stderr, "%s: %s: %s.\n",
1239 progname, curdir, strerror(errno));
1244 * All this code is so that we know where we are when we start up
1245 * on a different machine with pmake.
1246 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
1247 * since the value of curdir can vary depending on how we got
1248 * here. Ie sitting at a shell prompt (shell that provides $PWD)
1249 * or via subdir.mk in which case its likely a shell which does
1251 * So, to stop it breaking this case only, we ignore PWD if
1252 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
1254 #ifndef NO_PWD_OVERRIDE
1256 char *pwd, *ptmp1 = NULL, *ptmp2 = NULL;
1258 if ((pwd = getenv("PWD")) != NULL &&
1259 Var_Value("MAKEOBJDIRPREFIX", VAR_CMD, &ptmp1) == NULL) {
1260 const char *makeobjdir = Var_Value("MAKEOBJDIR",
1263 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
1264 if (stat(pwd, &sb) == 0 &&
1265 sa.st_ino == sb.st_ino &&
1266 sa.st_dev == sb.st_dev)
1267 (void)strncpy(curdir, pwd, MAXPATHLEN);
1274 Var_Set(".CURDIR", curdir, VAR_GLOBAL);
1277 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that,
1278 * MAKEOBJDIR is set in the environment, try only that value
1279 * and fall back to .CURDIR if it does not exist.
1281 * Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
1282 * and * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none
1283 * of these paths exist, just use .CURDIR.
1285 Dir_InitDir(curdir);
1286 (void)Main_SetObjdir("%s", curdir);
1288 if (!Main_SetVarObjdir("MAKEOBJDIRPREFIX", curdir) &&
1289 !Main_SetVarObjdir("MAKEOBJDIR", "") &&
1290 !Main_SetObjdir("%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
1291 !Main_SetObjdir("%s.%s", _PATH_OBJDIR, machine) &&
1292 !Main_SetObjdir("%s", _PATH_OBJDIR))
1293 (void)Main_SetObjdir("%s%s", _PATH_OBJDIRPREFIX, curdir);
1296 * Initialize archive, target and suffix modules in preparation for
1297 * parsing the makefile(s)
1302 Trace_Init(tracefile);
1307 Trace_Log(MAKESTART, NULL);
1310 * Set up the .TARGETS variable to contain the list of targets to be
1311 * created. If none specified, make the variable empty -- the parser
1312 * will fill the thing in with the default or .MAIN target.
1314 if (!Lst_IsEmpty(create)) {
1317 for (ln = Lst_First(create); ln != NULL; ln = LstNode_Next(ln)) {
1318 char *name = LstNode_Datum(ln);
1319 Var_Append(".TARGETS", name, VAR_GLOBAL);
1322 Var_Set(".TARGETS", "", VAR_GLOBAL);
1326 * If no user-supplied system path was given (through the -m option)
1327 * add the directories from the DEFSYSPATH (more than one may be given
1328 * as dir1:...:dirn) to the system include path.
1330 /* XXX: mismatch: the -m option sets sysIncPath, not syspath */
1331 if (syspath == NULL || syspath[0] == '\0')
1332 syspath = defsyspath;
1334 syspath = bmake_strdup(syspath);
1336 for (start = syspath; *start != '\0'; start = cp) {
1337 for (cp = start; *cp != '\0' && *cp != ':'; cp++)
1342 /* look for magic parent directory search string */
1343 if (strncmp(".../", start, 4) != 0) {
1344 (void)Dir_AddDir(defIncPath, start);
1346 if (Dir_FindHereOrAbove(curdir, start+4,
1347 found_path, sizeof(found_path))) {
1348 (void)Dir_AddDir(defIncPath, found_path);
1352 if (syspath != defsyspath)
1356 * Read in the built-in rules first, followed by the specified
1357 * makefiles, or the default makefile and Makefile, in that order,
1358 * if no makefiles were given on the command line.
1363 sysMkPath = Lst_Init();
1364 Dir_Expand(_PATH_DEFSYSMK,
1365 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath,
1367 if (Lst_IsEmpty(sysMkPath))
1368 Fatal("%s: no system rules (%s).", progname,
1370 ln = Lst_Find(sysMkPath, ReadMakefileSucceeded, NULL);
1372 Fatal("%s: cannot open %s.", progname,
1373 (char *)LstNode_Datum(Lst_First(sysMkPath)));
1376 if (!Lst_IsEmpty(makefiles)) {
1379 ln = Lst_Find(makefiles, ReadMakefileFailed, NULL);
1381 Fatal("%s: cannot open %s.", progname,
1382 (char *)LstNode_Datum(ln));
1384 p1 = Var_Subst("${" MAKEFILE_PREFERENCE "}",
1385 VAR_CMD, VARE_WANTRES);
1386 (void)str2Lst_Append(makefiles, p1, NULL);
1387 (void)Lst_Find(makefiles, ReadMakefileSucceeded, NULL);
1391 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
1392 if (!noBuiltins || !printVars) {
1393 makeDependfile = Var_Subst("${.MAKE.DEPENDFILE:T}",
1394 VAR_CMD, VARE_WANTRES);
1395 doing_depend = TRUE;
1396 (void)ReadMakefile(makeDependfile);
1397 doing_depend = FALSE;
1401 printf("%s: Entering directory `%s'\n", progname, objdir);
1405 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
1408 if (!forceJobs && !compatMake &&
1409 Var_Exists(".MAKE.JOBS", VAR_GLOBAL)) {
1413 value = Var_Subst("${.MAKE.JOBS}", VAR_GLOBAL, VARE_WANTRES);
1414 n = strtol(value, NULL, 0);
1416 (void)fprintf(stderr, "%s: illegal value for .MAKE.JOBS -- must be positive integer!\n",
1421 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
1422 Var_Append(MAKEFLAGS, value, VAR_GLOBAL);
1425 maxJobTokens = maxJobs;
1431 * Be compatible if user did not specify -j and did not explicitly
1432 * turned compatibility on
1434 if (!compatMake && !forceJobs) {
1439 Job_ServerStart(maxJobTokens, jp_0, jp_1);
1442 "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
1443 jp_0, jp_1, maxJobs, maxJobTokens, compatMake ? 1 : 0);
1446 Main_ExportMAKEFLAGS(TRUE); /* initial export */
1450 * For compatibility, look at the directories in the VPATH variable
1451 * and add them to the search path, if the variable is defined. The
1452 * variable's value is in the same format as the PATH envariable, i.e.
1453 * <directory>:<directory>:<directory>...
1455 if (Var_Exists("VPATH", VAR_CMD)) {
1458 * GCC stores string constants in read-only memory, but
1459 * Var_Subst will want to write this thing, so store it
1462 static char VPATH[] = "${VPATH}";
1464 vpath = Var_Subst(VPATH, VAR_CMD, VARE_WANTRES);
1467 /* skip to end of directory */
1468 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
1470 /* Save terminator character so know when to stop */
1473 /* Add directory to search path */
1474 (void)Dir_AddDir(dirSearchPath, path);
1477 } while (savec == ':');
1482 * Now that all search paths have been read for suffixes et al, it's
1483 * time to add the default search path to their lists...
1488 * Propagate attributes through :: dependency lists.
1492 /* print the initial graph, if the user requested it */
1496 /* print the values of any variables requested by the user */
1501 outOfDate = runTargets();
1505 Lst_Free(variables);
1506 Lst_Free(makefiles);
1507 Lst_Destroy(create, free);
1510 /* print the graph now it's been processed if the user requested it */
1514 Trace_Log(MAKEEND, 0);
1517 printf("%s: Leaving directory `%s'\n", progname, objdir);
1519 printf("%s: Leaving directory `%s'\n", progname, curdir);
1533 return outOfDate ? 1 : 0;
1536 /* Open and parse the given makefile, with all its side effects.
1539 * 0 if ok. -1 if couldn't open file.
1542 ReadMakefile(const char *fname)
1545 char *name, *path = NULL;
1547 if (!strcmp(fname, "-")) {
1548 Parse_File(NULL /*stdin*/, -1);
1549 Var_Set("MAKEFILE", "", VAR_INTERNAL);
1551 /* if we've chdir'd, rebuild the path name */
1552 if (strcmp(curdir, objdir) && *fname != '/') {
1553 path = str_concat3(curdir, "/", fname);
1554 fd = open(path, O_RDONLY);
1561 /* If curdir failed, try objdir (ala .depend) */
1562 path = str_concat3(objdir, "/", fname);
1563 fd = open(path, O_RDONLY);
1569 fd = open(fname, O_RDONLY);
1573 /* look in -I and system include directories. */
1574 name = Dir_FindFile(fname, parseIncPath);
1576 name = Dir_FindFile(fname,
1577 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
1578 if (!name || (fd = open(name, O_RDONLY)) == -1) {
1585 * set the MAKEFILE variable desired by System V fans -- the
1586 * placement of the setting here means it gets set to the last
1587 * makefile specified, as it is set by SysV make.
1591 Var_Set("MAKEFILE", fname, VAR_INTERNAL);
1592 Parse_File(fname, fd);
1602 * Execute the command in cmd, and return the output of that command
1603 * in a string. In the output, newlines are replaced with spaces.
1606 * A string containing the output of the command, or the empty string.
1607 * *errfmt returns a format string describing the command failure,
1608 * if any, using a single %s conversion specification.
1611 * The string must be freed by the caller.
1614 Cmd_Exec(const char *cmd, const char **errfmt)
1616 const char *args[4]; /* Args for invoking the shell */
1617 int fds[2]; /* Pipe streams */
1618 int cpid; /* Child PID */
1619 int pid; /* PID from wait() */
1620 WAIT_T status; /* command exit status */
1621 Buffer buf; /* buffer to store the result */
1623 char *res; /* result */
1626 int savederr; /* saved errno */
1633 * Set up arguments for shell
1635 args[0] = shellName;
1641 * Open a pipe for fetching its output
1643 if (pipe(fds) == -1) {
1644 *errfmt = "Couldn't create pipe for \"%s\"";
1651 switch (cpid = vFork()) {
1654 * Close input side of pipe
1656 (void)close(fds[0]);
1659 * Duplicate the output stream to the shell's output, then
1660 * shut the extra thing down. Note we don't fetch the error
1661 * stream...why not? Why?
1663 (void)dup2(fds[1], 1);
1664 (void)close(fds[1]);
1668 (void)execv(shellPath, UNCONST(args));
1673 *errfmt = "Couldn't exec \"%s\"";
1678 * No need for the writing half
1680 (void)close(fds[1]);
1686 char result[BUFSIZ];
1687 bytes_read = read(fds[0], result, sizeof(result));
1689 Buf_AddBytes(&buf, result, (size_t)bytes_read);
1691 while (bytes_read > 0 || (bytes_read == -1 && errno == EINTR));
1692 if (bytes_read == -1)
1696 * Close the input side of the pipe.
1698 (void)close(fds[0]);
1701 * Wait for the process to exit.
1703 while(((pid = waitpid(cpid, &status, 0)) != cpid) && (pid >= 0)) {
1704 JobReapChild(pid, status, FALSE);
1707 res_len = Buf_Size(&buf);
1708 res = Buf_Destroy(&buf, FALSE);
1711 *errfmt = "Couldn't read shell's output for \"%s\"";
1713 if (WIFSIGNALED(status))
1714 *errfmt = "\"%s\" exited on a signal";
1715 else if (WEXITSTATUS(status) != 0)
1716 *errfmt = "\"%s\" returned non-zero status";
1718 /* Convert newlines to spaces. A final newline is just stripped */
1719 if (res_len > 0 && res[res_len - 1] == '\n')
1720 res[res_len - 1] = '\0';
1721 for (cp = res; *cp != '\0'; cp++)
1728 return bmake_strdup("");
1733 * Print an error message given its format.
1739 * The message is printed.
1743 Error(const char *fmt, ...)
1748 err_file = debug_file;
1749 if (err_file == stdout)
1751 (void)fflush(stdout);
1754 fprintf(err_file, "%s: ", progname);
1755 (void)vfprintf(err_file, fmt, ap);
1757 (void)fprintf(err_file, "\n");
1758 (void)fflush(err_file);
1759 if (err_file == stderr)
1767 * Produce a Fatal error message. If jobs are running, waits for them
1778 Fatal(const char *fmt, ...)
1786 (void)fflush(stdout);
1787 (void)vfprintf(stderr, fmt, ap);
1789 (void)fprintf(stderr, "\n");
1790 (void)fflush(stderr);
1792 PrintOnError(NULL, NULL);
1794 if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
1796 Trace_Log(MAKEERROR, 0);
1797 exit(2); /* Not 1 so -q can distinguish error */
1802 * Major exception once jobs are being created. Kills all jobs, prints
1803 * a message and exits.
1809 * All children are killed indiscriminately and the program Lib_Exits
1813 Punt(const char *fmt, ...)
1818 (void)fflush(stdout);
1819 (void)fprintf(stderr, "%s: ", progname);
1820 (void)vfprintf(stderr, fmt, ap);
1822 (void)fprintf(stderr, "\n");
1823 (void)fflush(stderr);
1825 PrintOnError(NULL, NULL);
1832 * Exit without giving a message.
1847 Trace_Log(MAKEERROR, 0);
1848 exit(2); /* Not 1, so -q can distinguish error */
1853 * Called when aborting due to errors in child shell to signal
1864 /* number of errors encountered in Make_Make */
1866 if (dieQuietly(NULL, -1))
1868 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
1873 * Remove a file carefully, avoiding directories.
1876 eunlink(const char *file)
1880 if (lstat(file, &st) == -1)
1883 if (S_ISDIR(st.st_mode)) {
1887 return unlink(file);
1892 * Print why exec failed, avoiding stdio.
1895 execError(const char *af, const char *av)
1899 struct iovec iov[8];
1901 (void)(iov[i].iov_base = UNCONST(s), \
1902 iov[i].iov_len = strlen(iov[i].iov_base), \
1905 #define IOADD(s) (void)write(2, s, strlen(s))
1913 IOADD(") failed (");
1914 IOADD(strerror(errno));
1918 while (writev(2, iov, 8) == -1 && errno == EAGAIN)
1925 * exit with usage message
1931 if ((p = strchr(progname, '[')) != NULL)
1934 (void)fprintf(stderr,
1935 "usage: %s [-BeikNnqrstWwX] \n"
1936 " [-C directory] [-D variable] [-d flags] [-f makefile]\n"
1937 " [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
1938 " [-V variable] [-v variable] [variable=value] [target ...]\n",
1944 * realpath(3) can get expensive, cache results...
1946 static GNode *cached_realpaths = NULL;
1949 get_cached_realpaths(void)
1952 if (!cached_realpaths) {
1953 cached_realpaths = Targ_NewGN("Realpath");
1954 #ifndef DEBUG_REALPATH_CACHE
1955 cached_realpaths->flags = INTERNAL;
1959 return cached_realpaths;
1962 /* purge any relative paths */
1964 purge_cached_realpaths(void)
1966 GNode *cache = get_cached_realpaths();
1967 Hash_Entry *he, *nhe;
1970 he = Hash_EnumFirst(&cache->context, &hs);
1972 nhe = Hash_EnumNext(&hs);
1973 if (he->name[0] != '/') {
1975 fprintf(stderr, "cached_realpath: purging %s\n", he->name);
1976 Hash_DeleteEntry(&cache->context, he);
1983 cached_realpath(const char *pathname, char *resolved)
1989 if (!pathname || !pathname[0])
1992 cache = get_cached_realpaths();
1994 if ((rp = Var_Value(pathname, cache, &cp)) != NULL) {
1996 strlcpy(resolved, rp, MAXPATHLEN);
1997 } else if ((rp = realpath(pathname, resolved)) != NULL) {
1998 Var_Set(pathname, rp, cache);
1999 } /* else should we negative-cache? */
2002 return rp ? resolved : NULL;
2006 PrintAddr(void *a, void *b)
2008 printf("%lx ", (unsigned long) a);
2014 addErrorCMD(void *cmdp, void *gnp MAKE_ATTR_UNUSED)
2017 return 1; /* stop */
2018 Var_Append(".ERROR_CMD", cmdp, VAR_GLOBAL);
2023 * Return true if we should die without noise.
2024 * For example our failing child was a sub-make
2025 * or failure happend elsewhere.
2028 dieQuietly(GNode *gn, int bf)
2030 static int quietly = -1;
2033 if (DEBUG(JOB) || getBoolean(".MAKE.DIE_QUIETLY", 1) == 0)
2038 quietly = (gn) ? ((gn->type & (OP_MAKE)) != 0) : 0;
2044 PrintOnError(GNode *gn, const char *s)
2046 static GNode *en = NULL;
2055 /* we generally want to keep quiet if a sub-make died */
2056 if (dieQuietly(gn, -1))
2062 printf("\n%s: stopped in %s\n", progname, curdir);
2065 return; /* we've been here! */
2068 * We can print this even if there is no .ERROR target.
2070 Var_Set(".ERROR_TARGET", gn->name, VAR_GLOBAL);
2071 Var_Delete(".ERROR_CMD", VAR_GLOBAL);
2072 Lst_ForEach(gn->commands, addErrorCMD, gn);
2074 expr = "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}";
2075 cp = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
2081 * Finally, see if there is a .ERROR target, and run it if so.
2083 en = Targ_FindNode(".ERROR", TARG_NOCREATE);
2085 en->type |= OP_SPECIAL;
2086 Compat_Make(en, en);
2091 Main_ExportMAKEFLAGS(Boolean first)
2093 static Boolean once = TRUE;
2101 expr = "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}";
2102 s = Var_Subst(expr, VAR_CMD, VARE_WANTRES);
2105 setenv("MAKEFLAGS", s, 1);
2107 setenv("MAKE", s, 1);
2115 static char *tmpdir = NULL;
2121 * Honor $TMPDIR but only if it is valid.
2122 * Ensure it ends with /.
2124 tmpdir = Var_Subst("${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL,
2126 if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
2128 tmpdir = bmake_strdup(_PATH_TMP);
2135 * Create and open a temp file using "pattern".
2136 * If "fnamep" is provided set it to a copy of the filename created.
2137 * Otherwise unlink the file once open.
2140 mkTempFile(const char *pattern, char **fnamep)
2142 static char *tmpdir = NULL;
2143 char tfile[MAXPATHLEN];
2149 tmpdir = getTmpdir();
2150 if (pattern[0] == '/') {
2151 snprintf(tfile, sizeof(tfile), "%s", pattern);
2153 snprintf(tfile, sizeof(tfile), "%s%s", tmpdir, pattern);
2155 if ((fd = mkstemp(tfile)) < 0)
2156 Punt("Could not create temporary file %s: %s", tfile, strerror(errno));
2158 *fnamep = bmake_strdup(tfile);
2160 unlink(tfile); /* we just want the descriptor */
2166 * Convert a string representation of a boolean.
2167 * Anything that looks like "No", "False", "Off", "0" etc,
2168 * is FALSE, otherwise TRUE.
2171 s2Boolean(const char *s, Boolean bf)
2175 case '\0': /* not set - the default wins */
2205 * Return a Boolean based on setting of a knob.
2207 * If the knob is not set, the supplied default is the return value.
2208 * If set, anything that looks or smells like "No", "False", "Off", "0" etc,
2209 * is FALSE, otherwise TRUE.
2212 getBoolean(const char *name, Boolean fallback)
2214 char *expr = str_concat3("${", name, ":U:tl}");
2215 char *value = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
2216 Boolean res = s2Boolean(value, fallback);