/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* The main file for this entire program. Exit routines etc. reside here.
*
* Utility functions defined in this file:
*
* Main_ParseArgLine
* Parse and process command line arguments from a
* single string. Used to implement the special targets
* .MFLAGS and .MAKEFLAGS.
*
* Error Print a tagged error message.
*
* Fatal Print an error message and exit.
*
* Punt Abort all jobs and exit with a message.
*/
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: main.c,v 1.659 2025/06/13 05:41:36 rillig Exp $");
#if defined(MAKE_NATIVE)
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
"The Regents of the University of California. "
"All rights reserved.");
#endif
CmdOpts opts;
time_t now; /* Time at start of make */
GNode *defaultNode; /* .DEFAULT node */
bool allPrecious; /* .PRECIOUS given on a line by itself */
bool deleteOnError; /* .DELETE_ON_ERROR: set */
static int tokenPoolReader = -1, tokenPoolWriter = -1;
bool doing_depend; /* Set while reading .depend */
static bool jobsRunning; /* true if the jobs might be running */
static const char *tracefile;
static bool ReadMakefile(const char *);
static void purge_relative_cached_realpaths(void);
static bool ignorePWD; /* if we use -C, PWD is meaningless */
static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
char curdir[MAXPATHLEN + 1]; /* Startup directory */
const char *progname;
char *makeDependfile;
pid_t myPid;
int makelevel;
/*
* For compatibility with the POSIX version of MAKEFLAGS that includes
* all the options without '-', convert 'flags' to '-f -l -a -g -s '.
*/
static char *
explode(const char *flags)
{
char *exploded, *ep;
const char *p;
if (flags == NULL)
return NULL;
for (p = flags; *p != '\0'; p++)
if (!ch_isalpha(*p))
return bmake_strdup(flags);
len = strlen(arg);
fname = bmake_malloc(len + 20);
memcpy(fname, arg, len + 1);
/* Replace the trailing '%d' after '.%d' with the pid. */
if (len >= 3 && memcmp(fname + len - 3, ".%d", 3) == 0)
snprintf(fname + len - 2, 20, "%d", getpid());
opts.debug_file = fopen(fname, mode);
if (opts.debug_file == NULL) {
fprintf(stderr, "Cannot open debug file \"%s\"\n", fname);
exit(2);
}
free(fname);
}
static bool
MainParseOption(char c, const char *argvalue)
{
switch (c) {
case '\0':
break;
case 'B':
opts.compatMake = true;
Global_Append(MAKEFLAGS, "-B");
Global_Set(".MAKE.MODE", "compat");
break;
case 'C':
MainParseArgChdir(argvalue);
break;
case 'D':
if (argvalue[0] == '\0')
return false;
Var_SetExpand(SCOPE_GLOBAL, argvalue, "1");
Global_Append(MAKEFLAGS, "-D");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'I':
SearchPath_Add(parseIncPath, argvalue);
Global_Append(MAKEFLAGS, "-I");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'J':
MainParseArgJobsInternal(argvalue);
break;
case 'N':
opts.noExecute = true;
opts.noRecursiveExecute = true;
Global_Append(MAKEFLAGS, "-N");
break;
case 'S':
opts.keepgoing = false;
Global_Append(MAKEFLAGS, "-S");
break;
case 'T':
tracefile = bmake_strdup(argvalue);
Global_Append(MAKEFLAGS, "-T");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'V':
case 'v':
opts.printVars = c == 'v' ? PVM_EXPANDED : PVM_UNEXPANDED;
Lst_Append(&opts.variables, bmake_strdup(argvalue));
/* XXX: Why always -V? */
Global_Append(MAKEFLAGS, "-V");
Global_Append(MAKEFLAGS, argvalue);
break;
case 'W':
opts.parseWarnFatal = true;
/* XXX: why no Global_Append? */
break;
case 'X':
opts.varNoExportEnv = true;
Global_Append(MAKEFLAGS, "-X");
break;
case 'd':
/* If '-d-opts' don't pass to children */
if (argvalue[0] == '-')
argvalue++;
else {
Global_Append(MAKEFLAGS, "-d");
Global_Append(MAKEFLAGS, argvalue);
}
MainParseArgDebug(argvalue);
break;
case 'e':
opts.checkEnvFirst = true;
Global_Append(MAKEFLAGS, "-e");
break;
case 'f':
Lst_Append(&opts.makefiles, bmake_strdup(argvalue));
break;
case 'i':
opts.ignoreErrors = true;
Global_Append(MAKEFLAGS, "-i");
break;
case 'j':
MainParseArgJobs(argvalue);
break;
case 'k':
opts.keepgoing = true;
Global_Append(MAKEFLAGS, "-k");
break;
case 'm':
MainParseArgSysInc(argvalue);
/* XXX: why no Var_Append? */
break;
case 'n':
opts.noExecute = true;
Global_Append(MAKEFLAGS, "-n");
break;
case 'q':
opts.query = true;
/* Kind of nonsensical, wot? */
Global_Append(MAKEFLAGS, "-q");
break;
case 'r':
opts.noBuiltins = true;
Global_Append(MAKEFLAGS, "-r");
break;
case 's':
opts.silent = true;
Global_Append(MAKEFLAGS, "-s");
break;
case 't':
opts.touch = true;
Global_Append(MAKEFLAGS, "-t");
break;
case 'w':
opts.enterFlag = true;
Global_Append(MAKEFLAGS, "-w");
break;
default:
usage();
}
return true;
}
/*
* Parse the given arguments. Called from main() and from
* Main_ParseArgLine() when the .MAKEFLAGS target is used.
*
* The arguments must be treated as read-only and will be freed after the
* call.
*
* XXX: Deal with command line overriding .MAKEFLAGS in makefile
*/
static void
MainParseArgs(int argc, char **argv)
{
char c;
int arginc;
char *argvalue;
char *optscan;
bool inOption, dashDash = false;
const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
/* Can't actually use getopt(3) because rescanning is not portable */
rearg:
inOption = false;
optscan = NULL;
while (argc > 1) {
const char *optspec;
if (!inOption)
optscan = argv[1];
c = *optscan++;
arginc = 0;
if (inOption) {
if (c == '\0') {
argv++;
argc--;
inOption = false;
continue;
}
} else {
if (c != '-' || dashDash)
break;
inOption = true;
c = *optscan++;
}
/* '-' found at some earlier point */
optspec = strchr(optspecs, c);
if (c != '\0' && optspec != NULL && optspec[1] == ':') {
/*
* -<something> found, and <something> should have an
* argument
*/
inOption = false;
arginc = 1;
argvalue = optscan;
if (*argvalue == '\0') {
if (argc < 3)
goto noarg;
argvalue = argv[2];
arginc = 2;
}
} else {
argvalue = NULL;
}
switch (c) {
case '\0':
arginc = 1;
inOption = false;
break;
case '-':
dashDash = true;
break;
default:
if (!MainParseOption(c, argvalue))
goto noarg;
}
argv += arginc;
argc -= arginc;
}
/*
* See if the rest of the arguments are variable assignments and
* perform them if so. Else take them to be targets and stuff them
* on the end of the "create" list.
*/
for (; argc > 1; argv++, argc--) {
if (!Parse_VarAssign(argv[1], false, SCOPE_CMDLINE)) {
if (argv[1][0] == '\0')
Punt("illegal (null) argument.");
if (argv[1][0] == '-' && !dashDash)
goto rearg;
Lst_Append(&opts.create, bmake_strdup(argv[1]));
}
}
/*
* Break a line of arguments into words and parse them.
*
* Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
* by main() when reading the MAKEFLAGS environment variable.
*/
void
Main_ParseArgLine(const char *line)
{
Words words;
char *buf;
const char *p;
if (line == NULL)
return;
for (p = line; *p == ' '; p++)
continue;
if (p[0] == '\0')
return;
/*
* Splits str into words (in-place, modifying it), adding them to the list.
* The string must be kept alive as long as the list.
*/
void
AppendWords(StringList *lp, char *str)
{
char *p;
const char *sep = " \t";
for (p = strtok(str, sep); p != NULL; p = strtok(NULL, sep))
Lst_Append(lp, p);
}
#ifdef SIGINFO
static void
siginfo(int signo MAKE_ATTR_UNUSED)
{
char dir[MAXPATHLEN];
char str[2 * MAXPATHLEN];
int len;
if (getcwd(dir, sizeof dir) == NULL)
return;
len = snprintf(str, sizeof str, "%s: Working in: %s\n", progname, dir);
if (len > 0)
(void)write(STDERR_FILENO, str, (size_t)len);
}
#endif
/* Allow makefiles some control over the mode we run in. */
static void
MakeMode(void)
{
char *mode = Var_Subst("${.MAKE.MODE:tl}", SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
if (mode[0] != '\0') {
if (strstr(mode, "compat") != NULL) {
opts.compatMake = true;
forceJobs = false;
}
#if USE_META
if (strstr(mode, "meta") != NULL)
meta_mode_init(mode);
#endif
if (strstr(mode, "randomize-targets") != NULL)
opts.randomizeTargets = true;
}
/*
* Return a bool based on a variable.
*
* If the knob is not set, return the fallback.
* If set, anything that looks or smells like "No", "False", "Off", "0", etc.
* is false, otherwise true.
*/
bool
GetBooleanExpr(const char *expr, bool fallback)
{
char *value;
bool res;
value = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
res = ParseBoolean(value, fallback);
free(value);
return res;
}
/*
* Set up the .TARGETS variable to contain the list of targets to be created.
* If none specified, make the variable empty for now, the parser will fill
* in the default or .MAIN target later.
*/
static void
InitVarTargets(void)
{
StringListNode *ln;
if (Lst_IsEmpty(&opts.create)) {
Global_Set(".TARGETS", "");
return;
}
#ifndef NO_PWD_OVERRIDE
/*
* Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
* since the value of curdir can vary depending on how we got
* here. That is, sitting at a shell prompt (shell that provides $PWD)
* or via subdir.mk, in which case it's likely a shell which does
* not provide it.
*
* So, to stop it breaking this case only, we ignore PWD if
* MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains an expression.
*/
static void
HandlePWD(const struct stat *curdir_st)
{
char *pwd;
FStr makeobjdir;
struct stat pwd_st;
if (ignorePWD || (pwd = getenv("PWD")) == NULL)
return;
if (Var_Exists(SCOPE_CMDLINE, "MAKEOBJDIRPREFIX"))
return;
/*
* Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, MAKEOBJDIR is set
* in the environment, try only that value and fall back to .CURDIR if it
* does not exist.
*
* Otherwise, try _PATH_OBJDIR.MACHINE-MACHINE_ARCH, _PATH_OBJDIR.MACHINE,
* and finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none of these
* paths exist, just use .CURDIR.
*/
static void
InitObjdir(const char *machine, const char *machine_arch)
{
bool writable;
/*
* Initialize MAKE and .MAKE to the path of the executable, so that it can be
* found by execvp(3) and the shells, even after a chdir.
*
* If it's a relative path and contains a '/', resolve it to an absolute path.
* Otherwise keep it as is, assuming it will be found in the PATH.
*/
static void
InitVarMake(const char *argv0)
{
const char *make = argv0;
char pathbuf[MAXPATHLEN];
if (argv0[0] != '/' && strchr(argv0, '/') != NULL) {
const char *abspath = cached_realpath(argv0, pathbuf);
struct stat st;
if (abspath != NULL && abspath[0] == '/' &&
stat(make, &st) == 0)
make = abspath;
}
/*
* Add the directories from the colon-separated syspath to defSysIncPath.
* After returning, the contents of syspath is unspecified.
*/
static void
InitDefSysIncPath(char *syspath)
{
static char defsyspath[] = _PATH_DEFSYSPATH;
char *start, *p;
/*
* If no user-supplied system path was given (through the -m option)
* add the directories from the DEFSYSPATH (more than one may be given
* as dir1:...:dirn) to the system include path.
*/
if (syspath == NULL || syspath[0] == '\0')
syspath = defsyspath;
else
syspath = bmake_strdup(syspath);
SearchPath_Expand(
Lst_IsEmpty(&sysIncPath->dirs) ? defSysIncPath : sysIncPath,
_PATH_DEFSYSMK,
&sysMkFiles);
if (Lst_IsEmpty(&sysMkFiles))
Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK);
for (ln = sysMkFiles.first; ln != NULL; ln = ln->next)
if (ReadMakefile(ln->datum))
break;
if (ln == NULL)
Fatal("%s: cannot open %s.",
progname, (const char *)sysMkFiles.first->datum);
Lst_DoneFree(&sysMkFiles);
}
static void
InitMaxJobs(void)
{
char *value;
int n;
if (bogusJflag && !opts.compatMake) {
opts.compatMake = true;
Parse_Error(PARSE_WARNING,
"internal option \"-J\" in \"%s\" "
"refers to unopened file descriptors; "
"falling back to compat mode.\n"
"\t"
"To run the target even in -n mode, "
"add the .MAKE pseudo-source to the target.\n"
"\t"
"To run the target in default mode only, "
"add a ${:D make} marker to a target's command. "
"(This marker expression expands to an empty string.)\n"
"\t"
"To make the sub-make run in compat mode, add -B to "
"its invocation.\n"
"\t"
"To make the sub-make independent from the parent make, "
"unset the MAKEFLAGS environment variable in the "
"target's commands.",
curdir);
PrintStackTrace(true);
return;
}
if (forceJobs || opts.compatMake ||
!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOBS"))
return;
value = Var_Subst("${.MAKE.JOBS}", SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
n = (int)strtol(value, NULL, 0);
if (n < 1) {
(void)fprintf(stderr,
"%s: illegal value for .MAKE.JOBS "
"-- must be positive integer!\n",
progname);
exit(2); /* Not 1 so -q can distinguish error */
}
if (n != opts.maxJobs) {
Global_Append(MAKEFLAGS, "-j");
Global_Append(MAKEFLAGS, value);
}
/*
* For compatibility, look at the directories in the VPATH variable
* and add them to the search path, if the variable is defined. The
* variable's value is in the same format as the PATH environment
* variable, i.e. <directory>:<directory>:<directory>...
*/
static void
InitVpath(void)
{
char *vpath, savec, *path;
if (!Var_Exists(SCOPE_CMDLINE, "VPATH"))
return;
vpath = Var_Subst("${VPATH}", SCOPE_CMDLINE, VARE_EVAL);
/* TODO: handle errors */
path = vpath;
do {
char *p;
/* skip to end of directory */
for (p = path; *p != ':' && *p != '\0'; p++)
continue;
/* Save terminator character so know when to stop */
savec = *p;
*p = '\0';
/* Add directory to search path */
(void)SearchPath_Add(&dirSearchPath, path);
*p = savec;
path = p + 1;
} while (savec == ':');
free(vpath);
}
/* Just in case MAKEOBJDIR wants us to do something tricky. */
Targ_Init();
Global_Set_ReadOnly(".MAKE.OS", utsname.sysname);
Global_Set("MACHINE", machine);
Global_Set("MACHINE_ARCH", machine_arch);
#ifdef MAKE_VERSION
Global_Set("MAKE_VERSION", MAKE_VERSION);
#endif
Global_Set_ReadOnly(".newline", "\n");
#ifndef MAKEFILE_PREFERENCE_LIST
/* This is the traditional preference for makefiles. */
# define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
#endif
Global_Set(".MAKE.MAKEFILE_PREFERENCE", MAKEFILE_PREFERENCE_LIST);
Global_Set(".MAKE.DEPENDFILE", ".depend");
/* Tell makefiles like jobs.mk whether we support -jC */
#ifdef _SC_NPROCESSORS_ONLN
Global_Set_ReadOnly(".MAKE.JOBS.C", "yes");
#else
Global_Set_ReadOnly(".MAKE.JOBS.C", "no");
#endif
/*
* Initialize the parsing, directory and variable modules to prepare
* for the reading of inclusion paths and variable settings on the
* command line
*/
/*
* Initialize various variables.
* MAKE also gets this name, for compatibility
* .MAKEFLAGS gets set to the empty string just in case.
* MFLAGS also gets initialized empty, for compatibility.
*/
Parse_Init();
InitVarMake(argv[0]);
Global_Set(MAKEFLAGS, "");
Global_Set(".MAKEOVERRIDES", "");
Global_Set("MFLAGS", "");
Global_Set(".ALLTARGETS", "");
Global_Set_ReadOnly(".MAKE.LEVEL.ENV", MAKE_LEVEL_ENV);
/* Set some other useful variables. */
{
char buf[64];
const char *ep = getenv(MAKE_LEVEL_ENV);
/*
* Read the system makefile followed by either makefile, Makefile or the
* files given by the -f option. Exit on parse errors.
*/
static void
main_ReadFiles(void)
{
if (Lst_IsEmpty(&sysIncPath->dirs))
SearchPath_AddAll(sysIncPath, defSysIncPath);
Dir_SetSYSPATH();
if (!opts.noBuiltins)
ReadBuiltinRules();
posix_state = PS_MAYBE_NEXT_LINE;
if (!Lst_IsEmpty(&opts.makefiles))
ReadAllMakefiles(&opts.makefiles);
else
ReadFirstDefaultMakefile();
}
if (opts.printVars == PVM_NONE)
Main_ExportMAKEFLAGS(true); /* initial export */
InitVpath();
/*
* Now that all search paths have been read for suffixes et al, it's
* time to add the default search path to their lists...
*/
Suff_ExtendPaths();
/*
* Propagate attributes through :: dependency lists.
*/
Targ_Propagate();
/* print the initial graph, if the user requested it */
if (DEBUG(GRAPH1))
Targ_PrintGraph(1);
}
/*
* Make the targets, or print variables.
* Return whether any of the targets is out-of-date.
*/
static bool
main_Run(void)
{
if (opts.printVars != PVM_NONE) {
PrintVariables();
return false;
} else
return MakeTargets();
}
/* Clean up after making the targets. */
static void
main_CleanUp(void)
{
#ifdef CLEANUP
Lst_DoneFree(&opts.variables);
Lst_DoneFree(&opts.makefiles);
Lst_DoneFree(&opts.create);
#endif
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
Trace_Log(MAKEEND, NULL);
if (enterFlagObj)
printf("%s: Leaving directory `%s'\n", progname, objdir);
if (opts.enterFlag)
printf("%s: Leaving directory `%s'\n", progname, curdir);
/*
* Open and parse the given makefile, with all its side effects.
* Return false if the file could not be opened.
*/
static bool
ReadMakefile(const char *fname)
{
int fd;
char *name, *path = NULL;
if (strcmp(fname, "-") == 0) {
Parse_File("(stdin)", -1);
Var_Set(SCOPE_INTERNAL, "MAKEFILE", "");
} else {
if (strncmp(fname, ".../", 4) == 0) {
name = Dir_FindHereOrAbove(curdir, fname + 4);
if (name != NULL) {
/* Dir_FindHereOrAbove returns dirname */
path = str_concat3(name, "/",
str_basename(fname));
free(name);
fd = open(path, O_RDONLY);
if (fd != -1) {
fname = path;
goto found;
}
}
}
/* if we've chdir'd, rebuild the path name */
if (strcmp(curdir, objdir) != 0 && *fname != '/') {
path = str_concat3(curdir, "/", fname);
fd = open(path, O_RDONLY);
if (fd != -1) {
fname = path;
goto found;
}
free(path);
/* If curdir failed, try objdir (ala .depend) */
path = str_concat3(objdir, "/", fname);
fd = open(path, O_RDONLY);
if (fd != -1) {
fname = path;
goto found;
}
} else {
fd = open(fname, O_RDONLY);
if (fd != -1)
goto found;
}
/* look in -I and system include directories. */
name = Dir_FindFile(fname, parseIncPath);
if (name == NULL) {
SearchPath *sysInc = Lst_IsEmpty(&sysIncPath->dirs)
? defSysIncPath : sysIncPath;
name = Dir_FindFile(fname, sysInc);
}
if (name == NULL || (fd = open(name, O_RDONLY)) == -1) {
free(name);
free(path);
return false;
}
fname = name;
/*
* set the MAKEFILE variable desired by System V fans -- the
* placement of the setting here means it gets set to the last
* makefile specified, as it is set by SysV make.
*/
found:
if (!doing_depend)
Var_Set(SCOPE_INTERNAL, "MAKEFILE", fname);
Parse_File(fname, fd);
}
free(path);
return true;
}
/* populate av for Cmd_Exec and Compat_RunCommand */
int
Cmd_Argv(const char *cmd, size_t cmd_len, const char **av, size_t avsz,
char *cmd_file, size_t cmd_filesz, bool eflag, bool xflag)
{
int ac = 0;
int cmd_fd = -1;
if (shellPath == NULL)
Shell_Init();
if (cmd_file != NULL) {
if (cmd_len == 0)
cmd_len = strlen(cmd);
if (cmd_len > MAKE_CMDLEN_LIMIT) {
cmd_fd = mkTempFile(NULL, cmd_file, cmd_filesz);
if (cmd_fd >= 0) {
ssize_t n;
/* The following works for any of the builtin shell specs. */
av[ac++] = shellPath;
if (eflag)
av[ac++] = shellErrFlag;
if (cmd_fd >= 0) {
if (xflag)
av[ac++] = "-x";
av[ac++] = cmd_file;
} else {
av[ac++] = xflag ? "-xc" : "-c";
av[ac++] = cmd;
}
av[ac] = NULL;
return ac;
}
/*
* Execute the command in cmd, and return its output (only stdout, not
* stderr, possibly empty). In the output, replace newlines with spaces.
*/
char *
Cmd_Exec(const char *cmd, char **error)
{
const char *args[4]; /* Arguments for invoking the shell */
int pipefds[2];
int cpid; /* Child PID */
int pid; /* PID from wait() */
int status; /* command exit status */
Buffer buf; /* buffer to store the result */
ssize_t bytes_read;
char *output;
char *p;
int saved_errno;
char cmd_file[MAXPATHLEN];
DEBUG1(VAR, "Capturing the output of command \"%s\"\n", cmd);
if (Buf_EndsWith(&buf, '\n'))
buf.data[buf.len - 1] = '\0';
output = Buf_DoneData(&buf);
for (p = output; *p != '\0'; p++)
if (*p == '\n')
*p = ' ';
if (WIFSIGNALED(status))
*error = str_concat3("\"", cmd, "\" exited on a signal");
else if (WEXITSTATUS(status) != 0) {
Buffer errBuf;
Buf_Init(&errBuf);
Buf_AddStr(&errBuf, "Command \"");
Buf_AddStr(&errBuf, cmd);
Buf_AddStr(&errBuf, "\" exited with status ");
Buf_AddInt(&errBuf, WEXITSTATUS(status));
*error = Buf_DoneData(&errBuf);
} else if (saved_errno != 0)
*error = str_concat3(
"Couldn't read shell's output for \"", cmd, "\"");
else
*error = NULL;
if (cmd_file[0] != '\0')
unlink(cmd_file);
return output;
}
/*
* Print a printf-style error message.
*
* In default mode, this error message has no consequences, for compatibility
* reasons, in particular it does not affect the exit status. Only in lint
* mode (-dL) it does.
*/
void
Error(const char *fmt, ...)
{
va_list ap;
FILE *f;
f = opts.debug_file;
if (f == stdout)
f = stderr;
(void)fflush(stdout);
for (;;) {
fprintf(f, "%s: ", progname);
va_start(ap, fmt);
(void)vfprintf(f, fmt, ap);
va_end(ap);
(void)fprintf(f, "\n");
(void)fflush(f);
if (f == stderr)
break;
f = stderr;
}
main_errors++;
}
/*
* Wait for any running jobs to finish, then produce an error message,
* finally exit immediately.
*
* Exiting immediately differs from Parse_Error, which exits only after the
* current top-level makefile has been parsed completely.
*/
void
Fatal(const char *fmt, ...)
{
va_list ap;
/* Exit without giving a message. */
void
DieHorribly(void)
{
if (jobsRunning)
Job_AbortAll();
if (DEBUG(GRAPH2))
Targ_PrintGraph(2);
Trace_Log(MAKEERROR, NULL);
exit(2); /* Not 1 so -q can distinguish error */
}
int
unlink_file(const char *file)
{
struct stat st;
if (lstat(file, &st) == -1)
return -1;
if (S_ISDIR(st.st_mode)) {
/*
* POSIX says for unlink: "The path argument shall not name
* a directory unless [...]".
*/
errno = EISDIR;
return -1;
}
return unlink(file);
}
while (n > 0) {
ssize_t written = write(fd, mem, n);
/* XXX: Should this EAGAIN be EINTR? */
if (written == -1 && errno == EAGAIN)
continue;
if (written == -1)
break;
mem += written;
n -= (size_t)written;
}
}
/*
* Return true if we should die without noise.
* For example our failing child was a sub-make or failure happened elsewhere.
*/
bool
shouldDieQuietly(GNode *gn, int bf)
{
static int quietly = -1;
/*
* We can print this even if there is no .ERROR target.
*/
snprintf(sts, sizeof(sts), "%d", gn->exit_status);
Global_Set(".ERROR_EXIT", sts);
Global_Set(".ERROR_TARGET", gn->name);
Global_Delete(".ERROR_CMD");
if (cmd == NULL)
break;
Global_Append(".ERROR_CMD", cmd);
}
}
/*
* Print some helpful information in case of an error.
* The caller should exit soon after calling this function.
*/
void
PrintOnError(GNode *gn, const char *msg)
{
static GNode *errorNode = NULL;
StringListNode *ln;
if (DEBUG(HASH)) {
Targ_Stats();
Var_Stats();
}
if (errorNode != NULL)
return; /* we've been here! */
/*
* Finally, see if there is a .ERROR target, and run it if so.
*/
errorNode = Targ_FindNode(".ERROR");
if (errorNode != NULL) {
errorNode->type |= OP_SPECIAL;
Compat_Make(errorNode, errorNode);
}
}
/*
* Create and open a temp file using "pattern".
* If tfile is provided, set it to a copy of the filename created.
* Otherwise unlink the file once open.
*/
int
mkTempFile(const char *pattern, char *tfile, size_t tfile_sz)
{
static char *tmpdir = NULL;
char tbuf[MAXPATHLEN];
int fd;
if (pattern == NULL)
pattern = "makeXXXXXX";
if (tmpdir == NULL)
tmpdir = getTmpdir();
if (tfile == NULL) {
tfile = tbuf;
tfile_sz = sizeof tbuf;
}
if ((fd = mkstemp(tfile)) < 0)
Punt("mkstemp %s: %s", tfile, strerror(errno));
if (tfile == tbuf)
unlink(tfile); /* we just want the descriptor */
return fd;
}
/*
* Convert a string representation of a boolean into a boolean value.
* Anything that looks like "No", "False", "Off", "0" etc. is false,
* the empty string is the fallback, everything else is true.
*/
bool
ParseBoolean(const char *s, bool fallback)
{
char ch = ch_tolower(s[0]);
if (ch == '\0')
return fallback;
if (ch == '0' || ch == 'f' || ch == 'n')
return false;
if (ch == 'o')
return ch_tolower(s[1]) != 'f';
return true;
}