2021-06-25 18:16:24 +00:00
|
|
|
/* $NetBSD: main.c,v 1.540 2021/06/18 12:54:17 rillig Exp $ */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* The main file for this entire program. Exit routines etc. reside here.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Utility functions defined in this file:
|
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Main_ParseArgLine
|
|
|
|
* Parse and process command line arguments from a
|
|
|
|
* single string. Used to implement the special targets
|
|
|
|
* .MFLAGS and .MAKEFLAGS.
|
2020-11-20 03:54:37 +00:00
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Error Print a tagged error message.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Fatal Print an error message and exit.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Punt Abort all jobs and exit with a message.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Finish Finish things up by printing the number of errors
|
|
|
|
* that occurred, and exit.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/stat.h>
|
2014-01-26 07:40:17 +00:00
|
|
|
#if defined(MAKE_NATIVE) && defined(HAVE_SYSCTL)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include "wait.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2013-08-02 05:23:57 +00:00
|
|
|
#include <signal.h>
|
2012-06-08 21:57:36 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "make.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "job.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "trace.h"
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
|
2021-06-25 18:16:24 +00:00
|
|
|
MAKE_RCSID("$NetBSD: main.c,v 1.540 2021/06/18 12:54:17 rillig Exp $");
|
2020-11-07 19:39:21 +00:00
|
|
|
#if defined(MAKE_NATIVE) && !defined(lint)
|
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
|
|
|
|
"The Regents of the University of California. "
|
|
|
|
"All rights reserved.");
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-26 07:40:17 +00:00
|
|
|
#ifndef __arraycount
|
|
|
|
# define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
|
|
|
|
#endif
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
CmdOpts opts;
|
2020-11-20 03:54:37 +00:00
|
|
|
time_t now; /* Time at start of make */
|
|
|
|
GNode *defaultNode; /* .DEFAULT node */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool allPrecious; /* .PRECIOUS given on line by itself */
|
|
|
|
bool deleteOnError; /* .DELETE_ON_ERROR: set */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
static int maxJobTokens; /* -j argument */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool enterFlagObj; /* -w and objdir != srcdir */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
static int jp_0 = -1, jp_1 = -1; /* ends of parent job pipe */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool doing_depend; /* Set while reading .depend */
|
|
|
|
static bool jobsRunning; /* true if the jobs might be running */
|
2020-11-20 03:54:37 +00:00
|
|
|
static const char *tracefile;
|
|
|
|
static int ReadMakefile(const char *);
|
|
|
|
static void purge_relative_cached_realpaths(void);
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool ignorePWD; /* if we use -C, PWD is meaningless */
|
2020-11-20 03:54:37 +00:00
|
|
|
static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */
|
|
|
|
char curdir[MAXPATHLEN + 1]; /* Startup directory */
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *progname;
|
2012-06-08 21:57:36 +00:00
|
|
|
char *makeDependfile;
|
|
|
|
pid_t myPid;
|
2013-08-02 05:23:57 +00:00
|
|
|
int makelevel;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool forceJobs = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
static int main_errors = 0;
|
2020-11-20 03:54:37 +00:00
|
|
|
static HashTable cached_realpaths;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2013-08-02 05:23:57 +00:00
|
|
|
/*
|
|
|
|
* For compatibility with the POSIX version of MAKEFLAGS that includes
|
2020-11-20 03:54:37 +00:00
|
|
|
* all the options without '-', convert 'flags' to '-f -l -a -g -s'.
|
2013-08-02 05:23:57 +00:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
explode(const char *flags)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
size_t len;
|
|
|
|
char *nf, *st;
|
|
|
|
const char *f;
|
2013-08-02 05:23:57 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (flags == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (f = flags; *f != '\0'; f++)
|
2020-11-20 03:54:37 +00:00
|
|
|
if (!ch_isalpha(*f))
|
|
|
|
break;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (*f != '\0')
|
2020-11-20 03:54:37 +00:00
|
|
|
return bmake_strdup(flags);
|
2013-08-02 05:23:57 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
len = strlen(flags);
|
|
|
|
st = nf = bmake_malloc(len * 3 + 1);
|
2021-01-14 01:24:34 +00:00
|
|
|
while (*flags != '\0') {
|
2020-11-20 03:54:37 +00:00
|
|
|
*nf++ = '-';
|
|
|
|
*nf++ = *flags++;
|
|
|
|
*nf++ = ' ';
|
|
|
|
}
|
|
|
|
*nf = '\0';
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* usage --
|
|
|
|
* exit with usage message
|
|
|
|
*/
|
|
|
|
MAKE_ATTR_DEAD static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
size_t prognameLen = strcspn(progname, "[");
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: %.*s [-BeikNnqrSstWwX]\n"
|
|
|
|
" [-C directory] [-D variable] [-d flags] [-f makefile]\n"
|
|
|
|
" [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n"
|
|
|
|
" [-V variable] [-v variable] [variable=value] [target ...]\n",
|
|
|
|
(int)prognameLen, progname);
|
|
|
|
exit(2);
|
2013-08-02 05:23:57 +00:00
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
2021-02-11 01:51:11 +00:00
|
|
|
MainParseArgDebugFile(const char *arg)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *mode;
|
|
|
|
size_t len;
|
|
|
|
char *fname;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (opts.debug_file != stdout && opts.debug_file != stderr)
|
|
|
|
fclose(opts.debug_file);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
if (*arg == '+') {
|
|
|
|
arg++;
|
2020-11-20 03:54:37 +00:00
|
|
|
mode = "a";
|
|
|
|
} else
|
|
|
|
mode = "w";
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
if (strcmp(arg, "stdout") == 0) {
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.debug_file = stdout;
|
|
|
|
return;
|
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
if (strcmp(arg, "stderr") == 0) {
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.debug_file = stderr;
|
|
|
|
return;
|
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
len = strlen(arg);
|
2020-11-20 03:54:37 +00:00
|
|
|
fname = bmake_malloc(len + 20);
|
2021-02-11 01:51:11 +00:00
|
|
|
memcpy(fname, arg, len + 1);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* Let the filename be modified by the pid */
|
|
|
|
if (strcmp(fname + len - 3, ".%d") == 0)
|
|
|
|
snprintf(fname + len - 2, 20, "%d", getpid());
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.debug_file = fopen(fname, mode);
|
|
|
|
if (opts.debug_file == NULL) {
|
|
|
|
fprintf(stderr, "Cannot open debug file %s\n",
|
|
|
|
fname);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
free(fname);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
2021-02-11 01:51:11 +00:00
|
|
|
MainParseArgDebug(const char *argvalue)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
const char *modules;
|
2020-11-20 03:54:37 +00:00
|
|
|
DebugFlags debug = opts.debug;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
for (modules = argvalue; *modules != '\0'; modules++) {
|
2012-06-08 21:57:36 +00:00
|
|
|
switch (*modules) {
|
2020-11-07 19:39:21 +00:00
|
|
|
case '0': /* undocumented, only intended for tests */
|
2020-11-20 03:54:37 +00:00
|
|
|
debug = DEBUG_NONE;
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
case 'A':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug = DEBUG_ALL;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_ARCH;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_CWD;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_COND;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_DIR;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_ERROR;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_FOR;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
if (modules[1] == '1') {
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_GRAPH1;
|
|
|
|
modules++;
|
|
|
|
} else if (modules[1] == '2') {
|
|
|
|
debug |= DEBUG_GRAPH2;
|
|
|
|
modules++;
|
|
|
|
} else if (modules[1] == '3') {
|
|
|
|
debug |= DEBUG_GRAPH3;
|
|
|
|
modules++;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-09-05 16:11:04 +00:00
|
|
|
case 'h':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_HASH;
|
2020-09-05 16:11:04 +00:00
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
case 'j':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_JOB;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
2020-09-05 16:11:04 +00:00
|
|
|
case 'L':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.strict = true;
|
2020-09-05 16:11:04 +00:00
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
case 'l':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_LOUD;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_META;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_MAKE;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_SCRIPT;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_PARSE;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_SUFF;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_TARG;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
2012-09-11 02:56:02 +00:00
|
|
|
case 'V':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.debugVflag = true;
|
2012-09-11 02:56:02 +00:00
|
|
|
break;
|
2012-06-08 21:57:36 +00:00
|
|
|
case 'v':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_VAR;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
2020-11-20 03:54:37 +00:00
|
|
|
debug |= DEBUG_SHELL;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'F':
|
2021-02-11 01:51:11 +00:00
|
|
|
MainParseArgDebugFile(modules + 1);
|
2012-06-08 21:57:36 +00:00
|
|
|
goto debug_setbuf;
|
|
|
|
default:
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"%s: illegal argument to d option -- %c\n",
|
|
|
|
progname, *modules);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
debug_setbuf:
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.debug = debug;
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* Make the debug_file unbuffered, and make
|
|
|
|
* stdout line buffered (unless debugfile == stdout).
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
setvbuf(opts.debug_file, NULL, _IONBF, 0);
|
|
|
|
if (opts.debug_file != stdout) {
|
2012-06-08 21:57:36 +00:00
|
|
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Is path relative, or does it contain any relative component "." or ".."? */
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2021-02-11 01:51:11 +00:00
|
|
|
IsRelativePath(const char *path)
|
2017-04-20 23:12:37 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
const char *p;
|
2017-04-20 23:12:37 +00:00
|
|
|
|
|
|
|
if (path[0] != '/')
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
|
|
|
p = path;
|
|
|
|
while ((p = strstr(p, "/.")) != NULL) {
|
|
|
|
p += 2;
|
|
|
|
if (*p == '.')
|
|
|
|
p++;
|
|
|
|
if (*p == '/' || *p == '\0')
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-04-20 23:12:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
MainParseArgChdir(const char *argvalue)
|
|
|
|
{
|
|
|
|
struct stat sa, sb;
|
|
|
|
|
|
|
|
if (chdir(argvalue) == -1) {
|
|
|
|
(void)fprintf(stderr, "%s: chdir %s: %s\n",
|
|
|
|
progname, argvalue, strerror(errno));
|
2021-01-14 01:24:34 +00:00
|
|
|
exit(2); /* Not 1 so -q can distinguish error */
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
if (getcwd(curdir, MAXPATHLEN) == NULL) {
|
|
|
|
(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
|
|
|
|
exit(2);
|
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
if (!IsRelativePath(argvalue) &&
|
2020-11-07 19:39:21 +00:00
|
|
|
stat(argvalue, &sa) != -1 &&
|
|
|
|
stat(curdir, &sb) != -1 &&
|
|
|
|
sa.st_ino == sb.st_ino &&
|
|
|
|
sa.st_dev == sb.st_dev)
|
|
|
|
strncpy(curdir, argvalue, MAXPATHLEN);
|
2021-06-25 18:16:24 +00:00
|
|
|
ignorePWD = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
MainParseArgJobsInternal(const char *argvalue)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
char end;
|
|
|
|
if (sscanf(argvalue, "%d,%d%c", &jp_0, &jp_1, &end) != 2) {
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)fprintf(stderr,
|
2020-11-20 03:54:37 +00:00
|
|
|
"%s: internal error -- J option malformed (%s)\n",
|
|
|
|
progname, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if ((fcntl(jp_0, F_GETFD, 0) < 0) ||
|
|
|
|
(fcntl(jp_1, F_GETFD, 0) < 0)) {
|
|
|
|
#if 0
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"%s: ###### warning -- J descriptors were closed!\n",
|
|
|
|
progname);
|
|
|
|
exit(2);
|
|
|
|
#endif
|
|
|
|
jp_0 = -1;
|
|
|
|
jp_1 = -1;
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.compatMake = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
} else {
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-J");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
MainParseArgJobs(const char *argvalue)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
forceJobs = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
opts.maxJobs = (int)strtol(argvalue, &p, 0);
|
|
|
|
if (*p != '\0' || opts.maxJobs < 1) {
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"%s: illegal argument to -j -- must be positive integer!\n",
|
|
|
|
progname);
|
2021-01-14 01:24:34 +00:00
|
|
|
exit(2); /* Not 1 so -q can distinguish error */
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-j");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
|
|
|
Global_Set(".MAKE.JOBS", argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
maxJobTokens = opts.maxJobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
MainParseArgSysInc(const char *argvalue)
|
|
|
|
{
|
|
|
|
/* look for magic parent directory search string */
|
|
|
|
if (strncmp(".../", argvalue, 4) == 0) {
|
|
|
|
char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
|
|
|
|
if (found_path == NULL)
|
|
|
|
return;
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)SearchPath_Add(sysIncPath, found_path);
|
2020-11-07 19:39:21 +00:00
|
|
|
free(found_path);
|
|
|
|
} else {
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)SearchPath_Add(sysIncPath, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-m");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2020-11-07 19:39:21 +00:00
|
|
|
MainParseArg(char c, const char *argvalue)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case 'B':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.compatMake = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-B");
|
|
|
|
Global_Set(MAKE_MODE, "compat");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
MainParseArgChdir(argvalue);
|
|
|
|
break;
|
|
|
|
case 'D':
|
2021-06-25 18:16:24 +00:00
|
|
|
if (argvalue[0] == '\0') return false;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_SetExpand(argvalue, "1");
|
|
|
|
Global_Append(MAKEFLAGS, "-D");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
Parse_AddIncludeDir(argvalue);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-I");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
MainParseArgJobsInternal(argvalue);
|
|
|
|
break;
|
|
|
|
case 'N':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.noExecute = true;
|
|
|
|
opts.noRecursiveExecute = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-N");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.keepgoing = false;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-S");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
tracefile = bmake_strdup(argvalue);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-T");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
case 'v':
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.printVars = c == 'v' ? PVM_EXPANDED : PVM_UNEXPANDED;
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Append(&opts.variables, bmake_strdup(argvalue));
|
2020-11-07 19:39:21 +00:00
|
|
|
/* XXX: Why always -V? */
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-V");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'W':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.parseWarnFatal = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
/* XXX: why no Var_Append? */
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'X':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.varNoExportEnv = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-X");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
/* If '-d-opts' don't pass to children */
|
|
|
|
if (argvalue[0] == '-')
|
|
|
|
argvalue++;
|
|
|
|
else {
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-d");
|
|
|
|
Global_Append(MAKEFLAGS, argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
MainParseArgDebug(argvalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.checkEnvFirst = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-e");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Append(&opts.makefiles, bmake_strdup(argvalue));
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.ignoreErrors = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-i");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
MainParseArgJobs(argvalue);
|
|
|
|
break;
|
|
|
|
case 'k':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.keepgoing = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-k");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
MainParseArgSysInc(argvalue);
|
2020-11-20 03:54:37 +00:00
|
|
|
/* XXX: why no Var_Append? */
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.noExecute = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-n");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'q':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.queryFlag = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Kind of nonsensical, wot? */
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-q");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.noBuiltins = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-r");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.beSilent = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-s");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.touchFlag = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-t");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.enterFlag = true;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-w");
|
2020-11-07 19:39:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Parse the given arguments. Called from main() and from
|
2020-11-07 19:39:21 +00:00
|
|
|
* Main_ParseArgLine() when the .MAKEFLAGS target is used.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* The arguments must be treated as read-only and will be freed after the
|
|
|
|
* call.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* XXX: Deal with command line overriding .MAKEFLAGS in makefile
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
static void
|
|
|
|
MainParseArgs(int argc, char **argv)
|
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
char c;
|
2012-06-08 21:57:36 +00:00
|
|
|
int arginc;
|
|
|
|
char *argvalue;
|
|
|
|
char *optscan;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool inOption, dashDash = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
const char *optspecs = "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstv:w";
|
2012-06-08 21:57:36 +00:00
|
|
|
/* Can't actually use getopt(3) because rescanning is not portable */
|
|
|
|
|
2020-07-08 18:32:15 +00:00
|
|
|
rearg:
|
2021-06-25 18:16:24 +00:00
|
|
|
inOption = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
optscan = NULL;
|
2020-11-07 19:39:21 +00:00
|
|
|
while (argc > 1) {
|
|
|
|
const char *optspec;
|
|
|
|
if (!inOption)
|
2012-06-08 21:57:36 +00:00
|
|
|
optscan = argv[1];
|
|
|
|
c = *optscan++;
|
|
|
|
arginc = 0;
|
2020-11-07 19:39:21 +00:00
|
|
|
if (inOption) {
|
|
|
|
if (c == '\0') {
|
2020-11-20 03:54:37 +00:00
|
|
|
argv++;
|
|
|
|
argc--;
|
2021-06-25 18:16:24 +00:00
|
|
|
inOption = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (c != '-' || dashDash)
|
|
|
|
break;
|
2021-06-25 18:16:24 +00:00
|
|
|
inOption = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
c = *optscan++;
|
|
|
|
}
|
|
|
|
/* '-' found at some earlier point */
|
2020-11-07 19:39:21 +00:00
|
|
|
optspec = strchr(optspecs, c);
|
|
|
|
if (c != '\0' && optspec != NULL && optspec[1] == ':') {
|
2012-06-08 21:57:36 +00:00
|
|
|
/* -<something> found, and <something> should have an arg */
|
2021-06-25 18:16:24 +00:00
|
|
|
inOption = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
arginc = 1;
|
|
|
|
argvalue = optscan;
|
2020-11-07 19:39:21 +00:00
|
|
|
if (*argvalue == '\0') {
|
2012-06-08 21:57:36 +00:00
|
|
|
if (argc < 3)
|
|
|
|
goto noarg;
|
|
|
|
argvalue = argv[2];
|
|
|
|
arginc = 2;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-08 18:32:15 +00:00
|
|
|
argvalue = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
switch (c) {
|
2012-06-08 21:57:36 +00:00
|
|
|
case '\0':
|
|
|
|
arginc = 1;
|
2021-06-25 18:16:24 +00:00
|
|
|
inOption = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
2021-06-25 18:16:24 +00:00
|
|
|
dashDash = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!MainParseArg(c, argvalue))
|
|
|
|
goto noarg;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
for (; argc > 1; argv++, argc--) {
|
2020-11-07 19:39:21 +00:00
|
|
|
VarAssign var;
|
|
|
|
if (Parse_IsVar(argv[1], &var)) {
|
2021-06-25 18:16:24 +00:00
|
|
|
Parse_Var(&var, SCOPE_CMDLINE);
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-11-20 03:54:37 +00:00
|
|
|
if (argv[1][0] == '\0')
|
2012-06-08 21:57:36 +00:00
|
|
|
Punt("illegal (null) argument.");
|
2020-11-20 03:54:37 +00:00
|
|
|
if (argv[1][0] == '-' && !dashDash)
|
2012-06-08 21:57:36 +00:00
|
|
|
goto rearg;
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Append(&opts.create, bmake_strdup(argv[1]));
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
noarg:
|
|
|
|
(void)fprintf(stderr, "%s: option requires an argument -- %c\n",
|
|
|
|
progname, c);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Break a line of arguments into words and parse them.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* Used when a .MFLAGS or .MAKEFLAGS target is encountered during parsing and
|
2021-01-14 01:24:34 +00:00
|
|
|
* by main() when reading the MAKEFLAGS environment variable.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Main_ParseArgLine(const char *line)
|
|
|
|
{
|
2020-09-05 16:11:04 +00:00
|
|
|
Words words;
|
|
|
|
char *buf;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
if (line == NULL)
|
|
|
|
return;
|
2021-01-14 01:24:34 +00:00
|
|
|
/* XXX: don't use line as an iterator variable */
|
2021-02-11 01:51:11 +00:00
|
|
|
for (; *line == ' '; line++)
|
2012-06-08 21:57:36 +00:00
|
|
|
continue;
|
2020-11-20 03:54:37 +00:00
|
|
|
if (line[0] == '\0')
|
2012-06-08 21:57:36 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#ifndef POSIX
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* $MAKE may simply be naming the make(1) binary
|
|
|
|
*/
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (!(cp = strrchr(line, '/')))
|
|
|
|
cp = line;
|
|
|
|
if ((cp = strstr(cp, "make")) &&
|
|
|
|
strcmp(cp, "make") == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
{
|
2021-02-11 01:51:11 +00:00
|
|
|
FStr argv0 = Var_Value(SCOPE_GLOBAL, ".MAKE");
|
2021-01-14 01:24:34 +00:00
|
|
|
buf = str_concat3(argv0.str, " ", line);
|
|
|
|
FStr_Done(&argv0);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
words = Str_Words(buf, true);
|
2020-09-05 16:11:04 +00:00
|
|
|
if (words.words == NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
Error("Unterminated quoted string [%s]", buf);
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(buf);
|
2020-09-05 16:11:04 +00:00
|
|
|
MainParseArgs((int)words.len, words.words);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
Words_Free(words);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
|
|
|
Main_SetObjdir(bool writable, const char *fmt, ...)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
2017-04-15 00:51:18 +00:00
|
|
|
char *path;
|
|
|
|
char buf[MAXPATHLEN + 1];
|
2017-05-10 22:40:27 +00:00
|
|
|
char buf2[MAXPATHLEN + 1];
|
2021-06-25 18:16:24 +00:00
|
|
|
bool rc = false;
|
2016-12-19 21:50:59 +00:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2017-04-15 00:51:18 +00:00
|
|
|
vsnprintf(path = buf, MAXPATHLEN, fmt, ap);
|
2016-12-19 21:50:59 +00:00
|
|
|
va_end(ap);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
if (path[0] != '/') {
|
2017-04-20 23:12:37 +00:00
|
|
|
snprintf(buf2, MAXPATHLEN, "%s/%s", curdir, path);
|
|
|
|
path = buf2;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* look for the directory and try to chdir there */
|
|
|
|
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
|
2020-11-20 03:54:37 +00:00
|
|
|
if ((writable && access(path, W_OK) != 0) ||
|
|
|
|
(chdir(path) != 0)) {
|
|
|
|
(void)fprintf(stderr, "%s warning: %s: %s.\n",
|
|
|
|
progname, path, strerror(errno));
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-09-05 16:11:04 +00:00
|
|
|
snprintf(objdir, sizeof objdir, "%s", path);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".OBJDIR", objdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
setenv("PWD", objdir, 1);
|
|
|
|
Dir_InitDot();
|
2020-11-20 03:54:37 +00:00
|
|
|
purge_relative_cached_realpaths();
|
2021-06-25 18:16:24 +00:00
|
|
|
rc = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
if (opts.enterFlag && strcmp(objdir, curdir) != 0)
|
2021-06-25 18:16:24 +00:00
|
|
|
enterFlagObj = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
|
|
|
SetVarObjdir(bool writable, const char *var, const char *suffix)
|
2016-12-19 21:50:59 +00:00
|
|
|
{
|
2021-02-11 01:51:11 +00:00
|
|
|
FStr path = Var_Value(SCOPE_CMDLINE, var);
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr xpath;
|
2017-04-15 00:51:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (path.str == NULL || path.str[0] == '\0') {
|
|
|
|
FStr_Done(&path);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
2016-12-19 21:50:59 +00:00
|
|
|
|
2017-04-15 00:51:18 +00:00
|
|
|
/* expand variable substitutions */
|
2021-01-14 01:24:34 +00:00
|
|
|
xpath = FStr_InitRefer(path.str);
|
|
|
|
if (strchr(path.str, '$') != 0) {
|
|
|
|
char *expanded;
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst(path.str, SCOPE_GLOBAL, VARE_WANTRES, &expanded);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
xpath = FStr_InitOwn(expanded);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2017-04-15 00:51:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)Main_SetObjdir(writable, "%s%s", xpath.str, suffix);
|
2017-04-15 00:51:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&xpath);
|
|
|
|
FStr_Done(&path);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-12-19 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Splits str into words, adding them to the list.
|
|
|
|
* The string must be kept alive as long as the list.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
int
|
2020-11-20 03:54:37 +00:00
|
|
|
str2Lst_Append(StringList *lp, char *str)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
char *cp;
|
|
|
|
int n;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *sep = " \t";
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (n = 0, cp = strtok(str, sep); cp != NULL; cp = strtok(NULL, sep)) {
|
2020-11-20 03:54:37 +00:00
|
|
|
Lst_Append(lp, cp);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static void
|
2012-06-25 22:20:51 +00:00
|
|
|
siginfo(int signo MAKE_ATTR_UNUSED)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
char dir[MAXPATHLEN];
|
|
|
|
char str[2 * MAXPATHLEN];
|
|
|
|
int len;
|
2020-11-20 03:54:37 +00:00
|
|
|
if (getcwd(dir, sizeof dir) == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
return;
|
2020-11-20 03:54:37 +00:00
|
|
|
len = snprintf(str, sizeof str, "%s: Working in: %s\n", progname, dir);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (len > 0)
|
|
|
|
(void)write(STDERR_FILENO, str, (size_t)len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* Allow makefiles some control over the mode we run in. */
|
|
|
|
static void
|
|
|
|
MakeMode(void)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-02-11 01:51:11 +00:00
|
|
|
char *mode;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst("${" MAKE_MODE ":tl}", SCOPE_GLOBAL, VARE_WANTRES, &mode);
|
|
|
|
/* TODO: handle errors */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
if (mode[0] != '\0') {
|
|
|
|
if (strstr(mode, "compat") != NULL) {
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.compatMake = true;
|
|
|
|
forceJobs = false;
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#if USE_META
|
2021-02-11 01:51:11 +00:00
|
|
|
if (strstr(mode, "meta") != NULL)
|
|
|
|
meta_mode_init(mode);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2016-03-11 00:37:02 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
free(mode);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
2021-06-25 18:16:24 +00:00
|
|
|
PrintVar(const char *varname, bool expandVars)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strchr(varname, '$') != NULL) {
|
2020-11-07 19:39:21 +00:00
|
|
|
char *evalue;
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst(varname, SCOPE_GLOBAL, VARE_WANTRES, &evalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
printf("%s\n", evalue);
|
|
|
|
bmake_free(evalue);
|
|
|
|
|
|
|
|
} else if (expandVars) {
|
|
|
|
char *expr = str_concat3("${", varname, "}");
|
|
|
|
char *evalue;
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &evalue);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
free(expr);
|
|
|
|
printf("%s\n", evalue);
|
|
|
|
bmake_free(evalue);
|
|
|
|
|
|
|
|
} else {
|
2021-02-11 01:51:11 +00:00
|
|
|
FStr value = Var_Value(SCOPE_GLOBAL, varname);
|
2021-01-14 01:24:34 +00:00
|
|
|
printf("%s\n", value.str != NULL ? value.str : "");
|
|
|
|
FStr_Done(&value);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/*
|
2021-06-25 18:16:24 +00:00
|
|
|
* Return a bool based on a variable.
|
2020-11-20 03:54:37 +00:00
|
|
|
*
|
|
|
|
* If the knob is not set, return the fallback.
|
|
|
|
* If set, anything that looks or smells like "No", "False", "Off", "0", etc.
|
2021-06-25 18:16:24 +00:00
|
|
|
* is false, otherwise true.
|
2020-11-20 03:54:37 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
|
|
|
GetBooleanExpr(const char *expr, bool fallback)
|
2020-11-20 03:54:37 +00:00
|
|
|
{
|
|
|
|
char *value;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool res;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &value);
|
2020-11-20 03:54:37 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
res = ParseBoolean(value, fallback);
|
|
|
|
free(value);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-07-15 18:43:39 +00:00
|
|
|
static void
|
|
|
|
doPrintVars(void)
|
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
StringListNode *ln;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool expandVars;
|
2017-07-15 18:43:39 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (opts.printVars == PVM_EXPANDED)
|
2021-06-25 18:16:24 +00:00
|
|
|
expandVars = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
else if (opts.debugVflag)
|
2021-06-25 18:16:24 +00:00
|
|
|
expandVars = false;
|
2017-07-15 18:43:39 +00:00
|
|
|
else
|
2021-06-25 18:16:24 +00:00
|
|
|
expandVars = GetBooleanExpr("${.MAKE.EXPAND_VARIABLES}",
|
|
|
|
false);
|
2017-07-15 18:43:39 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = opts.variables.first; ln != NULL; ln = ln->next) {
|
2020-11-07 19:39:21 +00:00
|
|
|
const char *varname = ln->datum;
|
|
|
|
PrintVar(varname, expandVars);
|
2017-07-15 18:43:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2017-07-15 18:43:39 +00:00
|
|
|
runTargets(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
GNodeList targs = LST_INIT; /* target nodes to create */
|
2021-06-25 18:16:24 +00:00
|
|
|
bool outOfDate; /* false if all targets up to date */
|
2017-07-15 18:43:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Have now read the entire graph and need to make a list of
|
|
|
|
* targets to create. If none was given on the command line,
|
|
|
|
* we consult the parsing module to find the main target(s)
|
|
|
|
* to create.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
if (Lst_IsEmpty(&opts.create))
|
|
|
|
Parse_MainName(&targs);
|
2017-07-15 18:43:39 +00:00
|
|
|
else
|
2021-01-14 01:24:34 +00:00
|
|
|
Targ_FindList(&targs, &opts.create);
|
2017-07-15 18:43:39 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!opts.compatMake) {
|
2017-07-15 18:43:39 +00:00
|
|
|
/*
|
|
|
|
* Initialize job module before traversing the graph
|
|
|
|
* now that any .BEGIN and .END targets have been read.
|
|
|
|
* This is done only if the -q flag wasn't given
|
|
|
|
* (to prevent the .BEGIN from being executed should
|
|
|
|
* it exist).
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!opts.queryFlag) {
|
2017-07-15 18:43:39 +00:00
|
|
|
Job_Init();
|
2021-06-25 18:16:24 +00:00
|
|
|
jobsRunning = true;
|
2017-07-15 18:43:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Traverse the graph, checking on all the targets */
|
2021-01-14 01:24:34 +00:00
|
|
|
outOfDate = Make_Run(&targs);
|
2020-11-07 19:39:21 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Compat_Init will take care of creating all the
|
|
|
|
* targets as well as initializing the module.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
Compat_Run(&targs);
|
2021-06-25 18:16:24 +00:00
|
|
|
outOfDate = false;
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
Lst_Done(&targs); /* Don't free the targets themselves. */
|
2020-11-07 19:39:21 +00:00
|
|
|
return outOfDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the .TARGETS variable to contain the list of targets to be
|
|
|
|
* created. If none specified, make the variable empty -- the parser
|
|
|
|
* will fill the thing in with the default or .MAIN target.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
InitVarTargets(void)
|
|
|
|
{
|
|
|
|
StringListNode *ln;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (Lst_IsEmpty(&opts.create)) {
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".TARGETS", "");
|
2020-11-07 19:39:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = opts.create.first; ln != NULL; ln = ln->next) {
|
2021-02-11 01:51:11 +00:00
|
|
|
const char *name = ln->datum;
|
|
|
|
Global_Append(".TARGETS", name);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitRandom(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
srandom((unsigned int)(tv.tv_sec + tv.tv_usec));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2021-02-11 01:51:11 +00:00
|
|
|
InitVarMachine(const struct utsname *utsname MAKE_ATTR_UNUSED)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
|
|
|
#ifdef FORCE_MACHINE
|
2020-11-20 03:54:37 +00:00
|
|
|
return FORCE_MACHINE;
|
2020-11-07 19:39:21 +00:00
|
|
|
#else
|
|
|
|
const char *machine = getenv("MACHINE");
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (machine != NULL)
|
|
|
|
return machine;
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
#if defined(MAKE_NATIVE)
|
2020-11-07 19:39:21 +00:00
|
|
|
return utsname->machine;
|
2020-11-20 03:54:37 +00:00
|
|
|
#elif defined(MAKE_MACHINE)
|
2020-11-07 19:39:21 +00:00
|
|
|
return MAKE_MACHINE;
|
|
|
|
#else
|
|
|
|
return "unknown";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2020-11-20 03:54:37 +00:00
|
|
|
InitVarMachineArch(void)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
#ifdef FORCE_MACHINE_ARCH
|
|
|
|
return FORCE_MACHINE_ARCH;
|
|
|
|
#else
|
2020-11-07 19:39:21 +00:00
|
|
|
const char *env = getenv("MACHINE_ARCH");
|
|
|
|
if (env != NULL)
|
|
|
|
return env;
|
|
|
|
|
|
|
|
#if defined(MAKE_NATIVE) && defined(CTL_HW)
|
|
|
|
{
|
|
|
|
struct utsname utsname;
|
2020-11-20 03:54:37 +00:00
|
|
|
static char machine_arch_buf[sizeof utsname.machine];
|
2020-11-07 19:39:21 +00:00
|
|
|
const int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
|
2020-11-20 03:54:37 +00:00
|
|
|
size_t len = sizeof machine_arch_buf;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (sysctl(mib, (unsigned int)__arraycount(mib),
|
|
|
|
machine_arch_buf, &len, NULL, 0) < 0) {
|
|
|
|
(void)fprintf(stderr, "%s: sysctl failed (%s).\n",
|
|
|
|
progname, strerror(errno));
|
|
|
|
exit(2);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return machine_arch_buf;
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
#elif defined(MACHINE_ARCH)
|
|
|
|
return MACHINE_ARCH;
|
|
|
|
#elif defined(MAKE_MACHINE_ARCH)
|
2020-11-07 19:39:21 +00:00
|
|
|
return MAKE_MACHINE_ARCH;
|
|
|
|
#else
|
|
|
|
return "unknown";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_PWD_OVERRIDE
|
|
|
|
/*
|
|
|
|
* All this code is so that we know where we are when we start up
|
|
|
|
* on a different machine with pmake.
|
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* XXX: Make no longer has "local" and "remote" mode. Is this code still
|
|
|
|
* necessary?
|
|
|
|
*
|
2020-11-07 19:39:21 +00:00
|
|
|
* Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
|
|
|
|
* since the value of curdir can vary depending on how we got
|
|
|
|
* here. Ie sitting at a shell prompt (shell that provides $PWD)
|
|
|
|
* or via subdir.mk in which case its 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 a variable expression.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
HandlePWD(const struct stat *curdir_st)
|
|
|
|
{
|
|
|
|
char *pwd;
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr prefix, makeobjdir;
|
2020-11-07 19:39:21 +00:00
|
|
|
struct stat pwd_st;
|
|
|
|
|
|
|
|
if (ignorePWD || (pwd = getenv("PWD")) == NULL)
|
|
|
|
return;
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
prefix = Var_Value(SCOPE_CMDLINE, "MAKEOBJDIRPREFIX");
|
2021-01-14 01:24:34 +00:00
|
|
|
if (prefix.str != NULL) {
|
|
|
|
FStr_Done(&prefix);
|
2020-11-07 19:39:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
makeobjdir = Var_Value(SCOPE_CMDLINE, "MAKEOBJDIR");
|
2021-01-14 01:24:34 +00:00
|
|
|
if (makeobjdir.str != NULL && strchr(makeobjdir.str, '$') != NULL)
|
2020-11-07 19:39:21 +00:00
|
|
|
goto ignore_pwd;
|
|
|
|
|
|
|
|
if (stat(pwd, &pwd_st) == 0 &&
|
|
|
|
curdir_st->st_ino == pwd_st.st_ino &&
|
|
|
|
curdir_st->st_dev == pwd_st.st_dev)
|
|
|
|
(void)strncpy(curdir, pwd, MAXPATHLEN);
|
|
|
|
|
|
|
|
ignore_pwd:
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&makeobjdir);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
bool writable;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Dir_InitCur(curdir);
|
2021-06-25 18:16:24 +00:00
|
|
|
writable = GetBooleanExpr("${MAKE_OBJDIR_CHECK_WRITABLE}", true);
|
|
|
|
(void)Main_SetObjdir(false, "%s", curdir);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
if (!SetVarObjdir(writable, "MAKEOBJDIRPREFIX", curdir) &&
|
|
|
|
!SetVarObjdir(writable, "MAKEOBJDIR", "") &&
|
|
|
|
!Main_SetObjdir(writable, "%s.%s-%s", _PATH_OBJDIR, machine, machine_arch) &&
|
|
|
|
!Main_SetObjdir(writable, "%s.%s", _PATH_OBJDIR, machine) &&
|
|
|
|
!Main_SetObjdir(writable, "%s", _PATH_OBJDIR))
|
|
|
|
(void)Main_SetObjdir(writable, "%s%s", _PATH_OBJDIRPREFIX, curdir);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get rid of resource limit on file descriptors */
|
|
|
|
static void
|
|
|
|
UnlimitFiles(void)
|
|
|
|
{
|
|
|
|
#if defined(MAKE_NATIVE) || (defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE))
|
|
|
|
struct rlimit rl;
|
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) != -1 &&
|
|
|
|
rl.rlim_cur != rl.rlim_max) {
|
|
|
|
rl.rlim_cur = rl.rlim_max;
|
|
|
|
(void)setrlimit(RLIMIT_NOFILE, &rl);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CmdOpts_Init(void)
|
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.compatMake = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
opts.debug = DEBUG_NONE;
|
2021-02-11 01:51:11 +00:00
|
|
|
/* opts.debug_file has already been initialized earlier */
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.strict = false;
|
|
|
|
opts.debugVflag = false;
|
|
|
|
opts.checkEnvFirst = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Init(&opts.makefiles);
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.ignoreErrors = false; /* Pay attention to non-zero returns */
|
2021-01-14 01:24:34 +00:00
|
|
|
opts.maxJobs = 1;
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.keepgoing = false; /* Stop on error */
|
|
|
|
opts.noRecursiveExecute = false; /* Execute all .MAKE targets */
|
|
|
|
opts.noExecute = false; /* Execute all commands */
|
|
|
|
opts.queryFlag = false;
|
|
|
|
opts.noBuiltins = false; /* Read the built-in rules */
|
|
|
|
opts.beSilent = false; /* Print commands as executed */
|
|
|
|
opts.touchFlag = false;
|
2020-11-20 03:54:37 +00:00
|
|
|
opts.printVars = PVM_NONE;
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Init(&opts.variables);
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.parseWarnFatal = false;
|
|
|
|
opts.enterFlag = false;
|
|
|
|
opts.varNoExportEnv = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Init(&opts.create);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Initialize MAKE and .MAKE to the path of the executable, so that it can be
|
2020-11-07 19:39:21 +00:00
|
|
|
* 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.
|
2021-01-14 01:24:34 +00:00
|
|
|
* Otherwise keep it as is, assuming it will be found in the PATH.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
InitVarMake(const char *argv0)
|
|
|
|
{
|
|
|
|
const char *make = argv0;
|
|
|
|
|
|
|
|
if (argv0[0] != '/' && strchr(argv0, '/') != NULL) {
|
|
|
|
char pathbuf[MAXPATHLEN];
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *abspath = cached_realpath(argv0, pathbuf);
|
2020-11-07 19:39:21 +00:00
|
|
|
struct stat st;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (abspath != NULL && abspath[0] == '/' &&
|
|
|
|
stat(make, &st) == 0)
|
|
|
|
make = abspath;
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set("MAKE", make);
|
|
|
|
Global_Set(".MAKE", make);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Add the directories from the colon-separated syspath to defSysIncPath.
|
|
|
|
* After returning, the contents of syspath is unspecified.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
InitDefSysIncPath(char *syspath)
|
|
|
|
{
|
|
|
|
static char defsyspath[] = _PATH_DEFSYSPATH;
|
|
|
|
char *start, *cp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
for (start = syspath; *start != '\0'; start = cp) {
|
|
|
|
for (cp = start; *cp != '\0' && *cp != ':'; cp++)
|
|
|
|
continue;
|
2020-11-20 03:54:37 +00:00
|
|
|
if (*cp == ':')
|
2020-11-07 19:39:21 +00:00
|
|
|
*cp++ = '\0';
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* look for magic parent directory search string */
|
2020-11-20 03:54:37 +00:00
|
|
|
if (strncmp(start, ".../", 4) == 0) {
|
2020-11-07 19:39:21 +00:00
|
|
|
char *dir = Dir_FindHereOrAbove(curdir, start + 4);
|
|
|
|
if (dir != NULL) {
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)SearchPath_Add(defSysIncPath, dir);
|
2020-11-07 19:39:21 +00:00
|
|
|
free(dir);
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
} else {
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)SearchPath_Add(defSysIncPath, start);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syspath != defsyspath)
|
|
|
|
free(syspath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadBuiltinRules(void)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
StringListNode *ln;
|
2021-02-11 01:51:11 +00:00
|
|
|
StringList sysMkFiles = LST_INIT;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
SearchPath_Expand(
|
|
|
|
Lst_IsEmpty(&sysIncPath->dirs) ? defSysIncPath : sysIncPath,
|
|
|
|
_PATH_DEFSYSMK,
|
|
|
|
&sysMkFiles);
|
|
|
|
if (Lst_IsEmpty(&sysMkFiles))
|
2020-11-07 19:39:21 +00:00
|
|
|
Fatal("%s: no system rules (%s).", progname, _PATH_DEFSYSMK);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
for (ln = sysMkFiles.first; ln != NULL; ln = ln->next)
|
2020-11-20 03:54:37 +00:00
|
|
|
if (ReadMakefile(ln->datum) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ln == NULL)
|
|
|
|
Fatal("%s: cannot open %s.",
|
2021-02-11 01:51:11 +00:00
|
|
|
progname, (const char *)sysMkFiles.first->datum);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/* Free the list nodes but not the actual filenames since these may
|
|
|
|
* still be used in GNodes. */
|
|
|
|
Lst_Done(&sysMkFiles);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitMaxJobs(void)
|
|
|
|
{
|
|
|
|
char *value;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (forceJobs || opts.compatMake ||
|
2021-02-11 01:51:11 +00:00
|
|
|
!Var_Exists(SCOPE_GLOBAL, ".MAKE.JOBS"))
|
2020-11-07 19:39:21 +00:00
|
|
|
return;
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst("${.MAKE.JOBS}", SCOPE_GLOBAL, VARE_WANTRES, &value);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
n = (int)strtol(value, NULL, 0);
|
|
|
|
if (n < 1) {
|
|
|
|
(void)fprintf(stderr,
|
2020-11-20 03:54:37 +00:00
|
|
|
"%s: illegal value for .MAKE.JOBS "
|
|
|
|
"-- must be positive integer!\n",
|
|
|
|
progname);
|
2021-01-14 01:24:34 +00:00
|
|
|
exit(2); /* Not 1 so -q can distinguish error */
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n != opts.maxJobs) {
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKEFLAGS, "-j");
|
|
|
|
Global_Append(MAKEFLAGS, value);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.maxJobs = n;
|
|
|
|
maxJobTokens = opts.maxJobs;
|
2021-06-25 18:16:24 +00:00
|
|
|
forceJobs = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
free(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;
|
2021-02-11 01:51:11 +00:00
|
|
|
if (!Var_Exists(SCOPE_CMDLINE, "VPATH"))
|
2020-11-07 19:39:21 +00:00
|
|
|
return;
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst("${VPATH}", SCOPE_CMDLINE, VARE_WANTRES, &vpath);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
path = vpath;
|
|
|
|
do {
|
|
|
|
char *cp;
|
|
|
|
/* skip to end of directory */
|
|
|
|
for (cp = path; *cp != ':' && *cp != '\0'; cp++)
|
|
|
|
continue;
|
|
|
|
/* Save terminator character so know when to stop */
|
|
|
|
savec = *cp;
|
|
|
|
*cp = '\0';
|
|
|
|
/* Add directory to search path */
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)SearchPath_Add(&dirSearchPath, path);
|
2020-11-07 19:39:21 +00:00
|
|
|
*cp = savec;
|
|
|
|
path = cp + 1;
|
|
|
|
} while (savec == ':');
|
|
|
|
free(vpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-20 03:54:37 +00:00
|
|
|
ReadAllMakefiles(StringList *makefiles)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
StringListNode *ln;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
for (ln = makefiles->first; ln != NULL; ln = ln->next) {
|
|
|
|
const char *fname = ln->datum;
|
|
|
|
if (ReadMakefile(fname) != 0)
|
|
|
|
Fatal("%s: cannot open %s.", progname, fname);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-11-20 03:54:37 +00:00
|
|
|
ReadFirstDefaultMakefile(void)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
StringListNode *ln;
|
|
|
|
char *prefs;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
(void)Var_Subst("${" MAKE_MAKEFILE_PREFERENCE "}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_CMDLINE, VARE_WANTRES, &prefs);
|
2020-11-20 03:54:37 +00:00
|
|
|
/* TODO: handle errors */
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* XXX: This should use a local list instead of opts.makefiles
|
|
|
|
* since these makefiles do not come from the command line. They
|
|
|
|
* also have different semantics in that only the first file that
|
|
|
|
* is found is processed. See ReadAllMakefiles. */
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)str2Lst_Append(&opts.makefiles, prefs);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = opts.makefiles.first; ln != NULL; ln = ln->next)
|
2020-11-20 03:54:37 +00:00
|
|
|
if (ReadMakefile(ln->datum) == 0)
|
|
|
|
break;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
free(prefs);
|
2017-07-15 18:43:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Initialize variables such as MAKE, MACHINE, .MAKEFLAGS.
|
2020-11-20 03:54:37 +00:00
|
|
|
* Initialize a few modules.
|
2021-01-14 01:24:34 +00:00
|
|
|
* Parse the arguments from MAKEFLAGS and the command line.
|
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
static void
|
|
|
|
main_Init(int argc, char **argv)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
struct stat sa;
|
|
|
|
const char *machine;
|
|
|
|
const char *machine_arch;
|
2012-06-08 21:57:36 +00:00
|
|
|
char *syspath = getenv("MAKESYSPATH");
|
|
|
|
struct utsname utsname;
|
|
|
|
|
|
|
|
/* default to writing debug to stderr */
|
2020-11-07 19:39:21 +00:00
|
|
|
opts.debug_file = stderr;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
HashTable_Init(&cached_realpaths);
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef SIGINFO
|
|
|
|
(void)bmake_signal(SIGINFO, siginfo);
|
|
|
|
#endif
|
2020-11-07 19:39:21 +00:00
|
|
|
|
|
|
|
InitRandom();
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
progname = str_basename(argv[0]);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
|
|
|
UnlimitFiles();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2013-04-01 21:12:55 +00:00
|
|
|
if (uname(&utsname) == -1) {
|
2020-11-20 03:54:37 +00:00
|
|
|
(void)fprintf(stderr, "%s: uname failed (%s).\n", progname,
|
|
|
|
strerror(errno));
|
|
|
|
exit(2);
|
2013-04-01 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* Get the name of this type of MACHINE from utsname
|
|
|
|
* so we can share an executable for similar machines.
|
|
|
|
* (i.e. m68k: amiga hp300, mac68k, sun3, ...)
|
|
|
|
*
|
|
|
|
* Note that both MACHINE and MACHINE_ARCH are decided at
|
|
|
|
* run-time.
|
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
machine = InitVarMachine(&utsname);
|
|
|
|
machine_arch = InitVarMachineArch();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
myPid = getpid(); /* remember this for vFork() */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Just in case MAKEOBJDIR wants us to do something tricky.
|
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
Targ_Init();
|
|
|
|
Var_Init();
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.OS", utsname.sysname);
|
|
|
|
Global_Set("MACHINE", machine);
|
|
|
|
Global_Set("MACHINE_ARCH", machine_arch);
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef MAKE_VERSION
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set("MAKE_VERSION", MAKE_VERSION);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".newline", "\n"); /* handy for :@ loops */
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* This is the traditional preference for makefiles.
|
|
|
|
*/
|
|
|
|
#ifndef MAKEFILE_PREFERENCE_LIST
|
|
|
|
# define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
|
|
|
|
#endif
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(MAKE_MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST);
|
|
|
|
Global_Set(MAKE_DEPENDFILE, ".depend");
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
CmdOpts_Init();
|
2021-06-25 18:16:24 +00:00
|
|
|
allPrecious = false; /* Remove targets when interrupted */
|
|
|
|
deleteOnError = false; /* Historical default behavior */
|
|
|
|
jobsRunning = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
maxJobTokens = opts.maxJobs;
|
2021-06-25 18:16:24 +00:00
|
|
|
ignorePWD = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
2020-11-07 19:39:21 +00:00
|
|
|
InitVarMake(argv[0]);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(MAKEFLAGS, "");
|
|
|
|
Global_Set(MAKEOVERRIDES, "");
|
|
|
|
Global_Set("MFLAGS", "");
|
|
|
|
Global_Set(".ALLTARGETS", "");
|
2013-08-02 05:23:57 +00:00
|
|
|
/* some makefiles need to know this */
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Set(SCOPE_CMDLINE, MAKE_LEVEL ".ENV", MAKE_LEVEL_ENV);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* Set some other useful variables. */
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
char tmp[64], *ep = getenv(MAKE_LEVEL_ENV);
|
|
|
|
|
|
|
|
makelevel = ep != NULL && ep[0] != '\0' ? atoi(ep) : 0;
|
|
|
|
if (makelevel < 0)
|
|
|
|
makelevel = 0;
|
|
|
|
snprintf(tmp, sizeof tmp, "%d", makelevel);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(MAKE_LEVEL, tmp);
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(tmp, sizeof tmp, "%u", myPid);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.PID", tmp);
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(tmp, sizeof tmp, "%u", getppid());
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.PPID", tmp);
|
2021-01-14 01:24:34 +00:00
|
|
|
snprintf(tmp, sizeof tmp, "%u", getuid());
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.UID", tmp);
|
2021-01-14 01:24:34 +00:00
|
|
|
snprintf(tmp, sizeof tmp, "%u", getgid());
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.GID", tmp);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2013-08-02 05:23:57 +00:00
|
|
|
if (makelevel > 0) {
|
|
|
|
char pn[1024];
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(pn, sizeof pn, "%s[%d]", progname, makelevel);
|
2013-08-02 05:23:57 +00:00
|
|
|
progname = bmake_strdup(pn);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2013-04-01 21:12:55 +00:00
|
|
|
#ifdef USE_META
|
|
|
|
meta_init();
|
|
|
|
#endif
|
2020-09-05 16:11:04 +00:00
|
|
|
Dir_Init();
|
2017-03-06 19:54:54 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* First snag any flags out of the MAKE environment variable.
|
|
|
|
* (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
|
|
|
|
* in a different format).
|
|
|
|
*/
|
|
|
|
#ifdef POSIX
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
char *p1 = explode(getenv("MAKEFLAGS"));
|
|
|
|
Main_ParseArgLine(p1);
|
|
|
|
free(p1);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
|
|
|
Main_ParseArgLine(getenv("MAKE"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find where we are (now).
|
|
|
|
* We take care of PWD for the automounter below...
|
|
|
|
*/
|
|
|
|
if (getcwd(curdir, MAXPATHLEN) == NULL) {
|
|
|
|
(void)fprintf(stderr, "%s: getcwd: %s.\n",
|
|
|
|
progname, strerror(errno));
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainParseArgs(argc, argv);
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (opts.enterFlag)
|
2013-08-02 05:23:57 +00:00
|
|
|
printf("%s: Entering directory `%s'\n", progname, curdir);
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* Verify that cwd is sane.
|
|
|
|
*/
|
|
|
|
if (stat(curdir, &sa) == -1) {
|
2020-11-20 03:54:37 +00:00
|
|
|
(void)fprintf(stderr, "%s: %s: %s.\n",
|
|
|
|
progname, curdir, strerror(errno));
|
|
|
|
exit(2);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_PWD_OVERRIDE
|
2020-11-07 19:39:21 +00:00
|
|
|
HandlePWD(&sa);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".CURDIR", curdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
InitObjdir(machine, machine_arch);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize archive, target and suffix modules in preparation for
|
|
|
|
* parsing the makefile(s)
|
|
|
|
*/
|
|
|
|
Arch_Init();
|
|
|
|
Suff_Init();
|
|
|
|
Trace_Init(tracefile);
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
defaultNode = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
(void)time(&now);
|
|
|
|
|
|
|
|
Trace_Log(MAKESTART, NULL);
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
InitVarTargets();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
InitDefSysIncPath(syspath);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Read the system makefile followed by either makefile, Makefile or the
|
|
|
|
* files given by the -f option. Exit on parse errors.
|
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
static void
|
|
|
|
main_ReadFiles(void)
|
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!opts.noBuiltins)
|
|
|
|
ReadBuiltinRules();
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!Lst_IsEmpty(&opts.makefiles))
|
|
|
|
ReadAllMakefiles(&opts.makefiles);
|
2020-11-20 03:54:37 +00:00
|
|
|
else
|
|
|
|
ReadFirstDefaultMakefile();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the dependency graph. */
|
|
|
|
static void
|
|
|
|
main_PrepareMaking(void)
|
|
|
|
{
|
2012-06-08 21:57:36 +00:00
|
|
|
/* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
|
2020-11-20 03:54:37 +00:00
|
|
|
if (!opts.noBuiltins || opts.printVars == PVM_NONE) {
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)Var_Subst("${.MAKE.DEPENDFILE}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_CMDLINE, VARE_WANTRES, &makeDependfile);
|
2020-11-20 03:54:37 +00:00
|
|
|
if (makeDependfile[0] != '\0') {
|
|
|
|
/* TODO: handle errors */
|
2021-06-25 18:16:24 +00:00
|
|
|
doing_depend = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
(void)ReadMakefile(makeDependfile);
|
2021-06-25 18:16:24 +00:00
|
|
|
doing_depend = false;
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 22:14:23 +00:00
|
|
|
if (enterFlagObj)
|
|
|
|
printf("%s: Entering directory `%s'\n", progname, objdir);
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
MakeMode();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2021-02-11 01:51:11 +00:00
|
|
|
FStr makeflags = Var_Value(SCOPE_GLOBAL, MAKEFLAGS);
|
|
|
|
Global_Append("MFLAGS", makeflags.str);
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&makeflags);
|
2016-05-20 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
InitMaxJobs();
|
|
|
|
|
2016-05-20 16:19:56 +00:00
|
|
|
/*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Be compatible if the user did not specify -j and did not explicitly
|
|
|
|
* turn compatibility on.
|
2016-05-20 16:19:56 +00:00
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
if (!opts.compatMake && !forceJobs)
|
2021-06-25 18:16:24 +00:00
|
|
|
opts.compatMake = true;
|
2016-05-20 16:19:56 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!opts.compatMake)
|
2020-11-20 03:54:37 +00:00
|
|
|
Job_ServerStart(maxJobTokens, jp_0, jp_1);
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG5(JOB, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n",
|
2020-11-20 03:54:37 +00:00
|
|
|
jp_0, jp_1, opts.maxJobs, maxJobTokens, opts.compatMake ? 1 : 0);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (opts.printVars == PVM_NONE)
|
2021-06-25 18:16:24 +00:00
|
|
|
Main_ExportMAKEFLAGS(true); /* initial export */
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
InitVpath();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that all search paths have been read for suffixes et al, it's
|
|
|
|
* time to add the default search path to their lists...
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
Suff_ExtendPaths();
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Propagate attributes through :: dependency lists.
|
|
|
|
*/
|
|
|
|
Targ_Propagate();
|
|
|
|
|
|
|
|
/* print the initial graph, if the user requested it */
|
|
|
|
if (DEBUG(GRAPH1))
|
|
|
|
Targ_PrintGraph(1);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Make the targets.
|
2020-11-20 03:54:37 +00:00
|
|
|
* If the -v or -V options are given, print variables instead.
|
2021-01-14 01:24:34 +00:00
|
|
|
* Return whether any of the targets is out-of-date.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2020-11-20 03:54:37 +00:00
|
|
|
main_Run(void)
|
|
|
|
{
|
|
|
|
if (opts.printVars != PVM_NONE) {
|
|
|
|
/* print the values of any variables requested by the user */
|
2017-07-15 18:43:39 +00:00
|
|
|
doPrintVars();
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-11-20 03:54:37 +00:00
|
|
|
return runTargets();
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* Clean up after making the targets. */
|
|
|
|
static void
|
|
|
|
main_CleanUp(void)
|
|
|
|
{
|
|
|
|
#ifdef CLEANUP
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_DoneCall(&opts.variables, free);
|
|
|
|
/*
|
|
|
|
* Don't free the actual strings from opts.makefiles, they may be
|
|
|
|
* used in GNodes.
|
|
|
|
*/
|
|
|
|
Lst_Done(&opts.makefiles);
|
|
|
|
Lst_DoneCall(&opts.create, free);
|
2020-11-20 03:54:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print the graph now it's been processed if the user requested it */
|
|
|
|
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);
|
|
|
|
|
|
|
|
#ifdef USE_META
|
|
|
|
meta_finish();
|
|
|
|
#endif
|
|
|
|
Suff_End();
|
|
|
|
Targ_End();
|
|
|
|
Arch_End();
|
|
|
|
Var_End();
|
|
|
|
Parse_End();
|
|
|
|
Dir_End();
|
|
|
|
Job_End();
|
|
|
|
Trace_End();
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* Determine the exit code. */
|
|
|
|
static int
|
2021-06-25 18:16:24 +00:00
|
|
|
main_Exit(bool outOfDate)
|
2020-11-20 03:54:37 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (opts.strict && (main_errors > 0 || Parse_GetFatals() > 0))
|
2020-11-20 03:54:37 +00:00
|
|
|
return 2; /* Not 1 so -q can distinguish error */
|
2012-06-08 21:57:36 +00:00
|
|
|
return outOfDate ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
bool outOfDate;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
main_Init(argc, argv);
|
|
|
|
main_ReadFiles();
|
|
|
|
main_PrepareMaking();
|
|
|
|
outOfDate = main_Run();
|
|
|
|
main_CleanUp();
|
|
|
|
return main_Exit(outOfDate);
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Open and parse the given makefile, with all its side effects.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* 0 if ok. -1 if couldn't open file.
|
|
|
|
*/
|
|
|
|
static int
|
2020-09-05 16:11:04 +00:00
|
|
|
ReadMakefile(const char *fname)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2020-09-05 16:11:04 +00:00
|
|
|
char *name, *path = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (strcmp(fname, "-") == 0) {
|
2012-06-08 21:57:36 +00:00
|
|
|
Parse_File(NULL /*stdin*/, -1);
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Set(SCOPE_INTERNAL, "MAKEFILE", "");
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
|
|
|
/* if we've chdir'd, rebuild the path name */
|
2020-11-20 03:54:37 +00:00
|
|
|
if (strcmp(curdir, objdir) != 0 && *fname != '/') {
|
2020-09-05 16:11:04 +00:00
|
|
|
path = str_concat3(curdir, "/", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd != -1) {
|
|
|
|
fname = path;
|
|
|
|
goto found;
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
free(path);
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/* If curdir failed, try objdir (ala .depend) */
|
2020-09-05 16:11:04 +00:00
|
|
|
path = str_concat3(objdir, "/", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
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);
|
2020-11-20 03:54:37 +00:00
|
|
|
if (name == NULL) {
|
2021-02-11 01:51:11 +00:00
|
|
|
SearchPath *sysInc = Lst_IsEmpty(&sysIncPath->dirs)
|
2020-11-07 19:39:21 +00:00
|
|
|
? defSysIncPath : sysIncPath;
|
|
|
|
name = Dir_FindFile(fname, sysInc);
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
if (name == NULL || (fd = open(name, O_RDONLY)) == -1) {
|
2016-03-11 00:37:02 +00:00
|
|
|
free(name);
|
2012-06-08 21:57:36 +00:00
|
|
|
free(path);
|
2020-07-08 18:32:15 +00:00
|
|
|
return -1;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
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)
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Set(SCOPE_INTERNAL, "MAKEFILE", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
Parse_File(fname, fd);
|
|
|
|
}
|
|
|
|
free(path);
|
2020-07-08 18:32:15 +00:00
|
|
|
return 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
2012-06-08 21:57:36 +00:00
|
|
|
* Cmd_Exec --
|
|
|
|
* Execute the command in cmd, and return the output of that command
|
2020-09-05 16:11:04 +00:00
|
|
|
* in a string. In the output, newlines are replaced with spaces.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Results:
|
2020-09-05 16:11:04 +00:00
|
|
|
* A string containing the output of the command, or the empty string.
|
|
|
|
* *errfmt returns a format string describing the command failure,
|
|
|
|
* if any, using a single %s conversion specification.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The string must be freed by the caller.
|
|
|
|
*/
|
|
|
|
char *
|
2020-09-05 16:11:04 +00:00
|
|
|
Cmd_Exec(const char *cmd, const char **errfmt)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *args[4]; /* Args for invoking the shell */
|
2021-01-14 01:24:34 +00:00
|
|
|
int pipefds[2];
|
2020-11-20 03:54:37 +00:00
|
|
|
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 *res; /* result */
|
|
|
|
size_t res_len;
|
|
|
|
char *cp;
|
|
|
|
int savederr; /* saved errno */
|
|
|
|
|
|
|
|
*errfmt = NULL;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (shellName == NULL)
|
2020-11-20 03:54:37 +00:00
|
|
|
Shell_Init();
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Set up arguments for shell
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2020-11-20 03:54:37 +00:00
|
|
|
args[0] = shellName;
|
|
|
|
args[1] = "-c";
|
|
|
|
args[2] = cmd;
|
|
|
|
args[3] = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Open a pipe for fetching its output
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
if (pipe(pipefds) == -1) {
|
2020-11-20 03:54:37 +00:00
|
|
|
*errfmt = "Couldn't create pipe for \"%s\"";
|
|
|
|
goto bad;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Var_ReexportVars();
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Fork
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
switch (cpid = vfork()) {
|
2020-11-20 03:54:37 +00:00
|
|
|
case 0:
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)close(pipefds[0]); /* Close input side of pipe */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/*
|
|
|
|
* Duplicate the output stream to the shell's output, then
|
|
|
|
* shut the extra thing down. Note we don't fetch the error
|
|
|
|
* stream...why not? Why?
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)dup2(pipefds[1], 1);
|
|
|
|
(void)close(pipefds[1]);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
(void)execv(shellPath, UNCONST(args));
|
|
|
|
_exit(1);
|
|
|
|
/*NOTREACHED*/
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
case -1:
|
|
|
|
*errfmt = "Couldn't exec \"%s\"";
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
default:
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)close(pipefds[1]); /* No need for the writing half */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
savederr = 0;
|
|
|
|
Buf_Init(&buf);
|
|
|
|
|
|
|
|
do {
|
|
|
|
char result[BUFSIZ];
|
2021-01-14 01:24:34 +00:00
|
|
|
bytes_read = read(pipefds[0], result, sizeof result);
|
2020-11-20 03:54:37 +00:00
|
|
|
if (bytes_read > 0)
|
|
|
|
Buf_AddBytes(&buf, result, (size_t)bytes_read);
|
|
|
|
} while (bytes_read > 0 ||
|
|
|
|
(bytes_read == -1 && errno == EINTR));
|
|
|
|
if (bytes_read == -1)
|
|
|
|
savederr = errno;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
(void)close(pipefds[0]); /* Close the input side of the pipe. */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
/* Wait for the process to exit. */
|
|
|
|
while ((pid = waitpid(cpid, &status, 0)) != cpid && pid >= 0)
|
2021-06-25 18:16:24 +00:00
|
|
|
JobReapChild(pid, status, false);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
res_len = buf.len;
|
|
|
|
res = Buf_DoneData(&buf);
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
if (savederr != 0)
|
|
|
|
*errfmt = "Couldn't read shell's output for \"%s\"";
|
|
|
|
|
|
|
|
if (WIFSIGNALED(status))
|
|
|
|
*errfmt = "\"%s\" exited on a signal";
|
|
|
|
else if (WEXITSTATUS(status) != 0)
|
|
|
|
*errfmt = "\"%s\" returned non-zero status";
|
|
|
|
|
|
|
|
/* Convert newlines to spaces. A final newline is just stripped */
|
|
|
|
if (res_len > 0 && res[res_len - 1] == '\n')
|
|
|
|
res[res_len - 1] = '\0';
|
|
|
|
for (cp = res; *cp != '\0'; cp++)
|
|
|
|
if (*cp == '\n')
|
|
|
|
*cp = ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
2012-06-08 21:57:36 +00:00
|
|
|
bad:
|
2020-11-20 03:54:37 +00:00
|
|
|
return bmake_strdup("");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Print a printf-style error message.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-20 03:54:37 +00:00
|
|
|
* In default mode, this error message has no consequences, in particular it
|
2021-01-14 01:24:34 +00:00
|
|
|
* does not affect the exit status. Only in lint mode (-dL) it does.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Error(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
FILE *err_file;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
err_file = opts.debug_file;
|
2012-06-08 21:57:36 +00:00
|
|
|
if (err_file == stdout)
|
|
|
|
err_file = stderr;
|
|
|
|
(void)fflush(stdout);
|
|
|
|
for (;;) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(err_file, "%s: ", progname);
|
|
|
|
(void)vfprintf(err_file, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
(void)fprintf(err_file, "\n");
|
|
|
|
(void)fflush(err_file);
|
|
|
|
if (err_file == stderr)
|
|
|
|
break;
|
|
|
|
err_file = stderr;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
main_errors++;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Wait for any running jobs to finish, then produce an error message,
|
2020-11-20 03:54:37 +00:00
|
|
|
* finally exit immediately.
|
2012-06-08 21:57:36 +00:00
|
|
|
*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Exiting immediately differs from Parse_Error, which exits only after the
|
2021-01-14 01:24:34 +00:00
|
|
|
* current top-level makefile has been parsed completely.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Fatal(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (jobsRunning)
|
|
|
|
Job_Wait();
|
|
|
|
|
|
|
|
(void)fflush(stdout);
|
2020-11-20 03:54:37 +00:00
|
|
|
va_start(ap, fmt);
|
2012-06-08 21:57:36 +00:00
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
(void)fprintf(stderr, "\n");
|
|
|
|
(void)fflush(stderr);
|
|
|
|
|
|
|
|
PrintOnError(NULL, NULL);
|
|
|
|
|
|
|
|
if (DEBUG(GRAPH2) || DEBUG(GRAPH3))
|
|
|
|
Targ_PrintGraph(2);
|
2020-11-20 03:54:37 +00:00
|
|
|
Trace_Log(MAKEERROR, NULL);
|
2012-06-08 21:57:36 +00:00
|
|
|
exit(2); /* Not 1 so -q can distinguish error */
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Major exception once jobs are being created.
|
|
|
|
* Kills all jobs, prints a message and exits.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
Punt(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void)fflush(stdout);
|
|
|
|
(void)fprintf(stderr, "%s: ", progname);
|
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
(void)fprintf(stderr, "\n");
|
|
|
|
(void)fflush(stderr);
|
|
|
|
|
|
|
|
PrintOnError(NULL, NULL);
|
|
|
|
|
|
|
|
DieHorribly();
|
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Exit without giving a message. */
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
|
|
|
DieHorribly(void)
|
|
|
|
{
|
|
|
|
if (jobsRunning)
|
|
|
|
Job_AbortAll();
|
|
|
|
if (DEBUG(GRAPH2))
|
|
|
|
Targ_PrintGraph(2);
|
2020-11-20 03:54:37 +00:00
|
|
|
Trace_Log(MAKEERROR, NULL);
|
2021-01-14 01:24:34 +00:00
|
|
|
exit(2); /* Not 1 so -q can distinguish error */
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Called when aborting due to errors in child shell to signal abnormal exit.
|
2020-11-07 19:39:21 +00:00
|
|
|
* The program exits.
|
2021-01-14 01:24:34 +00:00
|
|
|
* Errors is the number of errors encountered in Make_Make.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Finish(int errs)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
if (shouldDieQuietly(NULL, -1))
|
2020-07-01 02:13:04 +00:00
|
|
|
exit(2);
|
2020-11-07 19:39:21 +00:00
|
|
|
Fatal("%d error%s", errs, errs == 1 ? "" : "s");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-04-01 21:12:55 +00:00
|
|
|
* eunlink --
|
2012-06-08 21:57:36 +00:00
|
|
|
* Remove a file carefully, avoiding directories.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
eunlink(const char *file)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (lstat(file, &st) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return unlink(file);
|
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
write_all(int fd, const void *data, size_t n)
|
|
|
|
{
|
|
|
|
const char *mem = data;
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
ssize_t written = write(fd, mem, n);
|
|
|
|
if (written == -1 && errno == EAGAIN)
|
|
|
|
continue;
|
|
|
|
if (written == -1)
|
|
|
|
break;
|
|
|
|
mem += written;
|
|
|
|
n -= (size_t)written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2020-11-07 19:39:21 +00:00
|
|
|
* execDie --
|
2012-06-08 21:57:36 +00:00
|
|
|
* Print why exec failed, avoiding stdio.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
void MAKE_ATTR_DEAD
|
|
|
|
execDie(const char *af, const char *av)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-07 19:39:21 +00:00
|
|
|
Buffer buf;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
Buf_Init(&buf);
|
2020-11-07 19:39:21 +00:00
|
|
|
Buf_AddStr(&buf, progname);
|
|
|
|
Buf_AddStr(&buf, ": ");
|
|
|
|
Buf_AddStr(&buf, af);
|
|
|
|
Buf_AddStr(&buf, "(");
|
|
|
|
Buf_AddStr(&buf, av);
|
|
|
|
Buf_AddStr(&buf, ") failed (");
|
|
|
|
Buf_AddStr(&buf, strerror(errno));
|
|
|
|
Buf_AddStr(&buf, ")\n");
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
write_all(STDERR_FILENO, buf.data, buf.len);
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
Buf_Done(&buf);
|
2020-11-07 19:39:21 +00:00
|
|
|
_exit(1);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 23:12:37 +00:00
|
|
|
/* purge any relative paths */
|
|
|
|
static void
|
2020-11-20 03:54:37 +00:00
|
|
|
purge_relative_cached_realpaths(void)
|
2017-04-20 23:12:37 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
HashEntry *he, *nhe;
|
|
|
|
HashIter hi;
|
|
|
|
|
|
|
|
HashIter_Init(&hi, &cached_realpaths);
|
|
|
|
he = HashIter_Next(&hi);
|
|
|
|
while (he != NULL) {
|
|
|
|
nhe = HashIter_Next(&hi);
|
|
|
|
if (he->key[0] != '/') {
|
|
|
|
DEBUG1(DIR, "cached_realpath: purging %s\n", he->key);
|
|
|
|
HashTable_DeleteEntry(&cached_realpaths, he);
|
|
|
|
/* XXX: What about the allocated he->value? Either
|
|
|
|
* free them or document why they cannot be freed. */
|
|
|
|
}
|
|
|
|
he = nhe;
|
|
|
|
}
|
2017-04-20 23:12:37 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 15:57:18 +00:00
|
|
|
char *
|
|
|
|
cached_realpath(const char *pathname, char *resolved)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *rp;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (pathname == NULL || pathname[0] == '\0')
|
|
|
|
return NULL;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
rp = HashTable_FindValue(&cached_realpaths, pathname);
|
|
|
|
if (rp != NULL) {
|
|
|
|
/* a hit */
|
|
|
|
strncpy(resolved, rp, MAXPATHLEN);
|
|
|
|
resolved[MAXPATHLEN - 1] = '\0';
|
|
|
|
return resolved;
|
|
|
|
}
|
2017-04-20 23:12:37 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
rp = realpath(pathname, resolved);
|
|
|
|
if (rp != NULL) {
|
|
|
|
HashTable_Set(&cached_realpaths, pathname, bmake_strdup(rp));
|
|
|
|
DEBUG2(DIR, "cached_realpath: %s -> %s\n", pathname, rp);
|
|
|
|
return resolved;
|
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/* should we negative-cache? */
|
|
|
|
return NULL;
|
2016-06-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 02:13:04 +00:00
|
|
|
/*
|
|
|
|
* Return true if we should die without noise.
|
2020-11-20 03:54:37 +00:00
|
|
|
* For example our failing child was a sub-make or failure happened elsewhere.
|
2020-07-01 02:13:04 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
2020-11-20 03:54:37 +00:00
|
|
|
shouldDieQuietly(GNode *gn, int bf)
|
2020-07-01 02:13:04 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
static int quietly = -1;
|
|
|
|
|
|
|
|
if (quietly < 0) {
|
2021-06-25 18:16:24 +00:00
|
|
|
if (DEBUG(JOB) ||
|
|
|
|
!GetBooleanExpr("${.MAKE.DIE_QUIETLY}", true))
|
2020-11-20 03:54:37 +00:00
|
|
|
quietly = 0;
|
|
|
|
else if (bf >= 0)
|
|
|
|
quietly = bf;
|
|
|
|
else
|
2021-01-14 01:24:34 +00:00
|
|
|
quietly = (gn != NULL && (gn->type & OP_MAKE)) ? 1 : 0;
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
return quietly != 0;
|
2020-07-01 02:13:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
SetErrorVars(GNode *gn)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
StringListNode *ln;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
/*
|
|
|
|
* We can print this even if there is no .ERROR target.
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".ERROR_TARGET", gn->name);
|
|
|
|
Global_Delete(".ERROR_CMD");
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *cmd = ln->datum;
|
2020-11-07 19:39:21 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cmd == NULL)
|
|
|
|
break;
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(".ERROR_CMD", cmd);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Print some helpful information in case of an error.
|
|
|
|
* The caller should exit soon after calling this function.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
PrintOnError(GNode *gn, const char *msg)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
static GNode *errorNode = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (DEBUG(HASH)) {
|
|
|
|
Targ_Stats();
|
|
|
|
Var_Stats();
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (errorNode != NULL)
|
|
|
|
return; /* we've been here! */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
if (msg != NULL)
|
|
|
|
printf("%s", msg);
|
|
|
|
printf("\n%s: stopped in %s\n", progname, curdir);
|
2020-07-01 02:13:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* we generally want to keep quiet if a sub-make died */
|
|
|
|
if (shouldDieQuietly(gn, -1))
|
|
|
|
return;
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
if (gn != NULL)
|
|
|
|
SetErrorVars(gn);
|
|
|
|
|
|
|
|
{
|
|
|
|
char *errorVarsValues;
|
|
|
|
(void)Var_Subst("${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &errorVarsValues);
|
2020-11-20 03:54:37 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
printf("%s", errorVarsValues);
|
|
|
|
free(errorVarsValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-25 18:16:24 +00:00
|
|
|
Main_ExportMAKEFLAGS(bool first)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool once = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
const char *expr;
|
|
|
|
char *s;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (once != first)
|
|
|
|
return;
|
2021-06-25 18:16:24 +00:00
|
|
|
once = false;
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
expr = "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}";
|
2021-02-11 01:51:11 +00:00
|
|
|
(void)Var_Subst(expr, SCOPE_CMDLINE, VARE_WANTRES, &s);
|
2020-11-20 03:54:37 +00:00
|
|
|
/* TODO: handle errors */
|
|
|
|
if (s[0] != '\0') {
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef POSIX
|
2020-11-20 03:54:37 +00:00
|
|
|
setenv("MAKEFLAGS", s, 1);
|
2012-06-08 21:57:36 +00:00
|
|
|
#else
|
2020-11-20 03:54:37 +00:00
|
|
|
setenv("MAKE", s, 1);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
getTmpdir(void)
|
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
static char *tmpdir = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (tmpdir != NULL)
|
|
|
|
return tmpdir;
|
|
|
|
|
|
|
|
/* Honor $TMPDIR but only if it is valid. Ensure it ends with '/'. */
|
2021-06-25 18:16:24 +00:00
|
|
|
(void)Var_Subst("${TMPDIR:tA:U" _PATH_TMP ":S,/$,,W}/",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &tmpdir);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2020-11-20 03:54:37 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
|
2020-11-20 03:54:37 +00:00
|
|
|
free(tmpdir);
|
|
|
|
tmpdir = bmake_strdup(_PATH_TMP);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
return tmpdir;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create and open a temp file using "pattern".
|
2020-11-07 19:39:21 +00:00
|
|
|
* If out_fname is provided, set it to a copy of the filename created.
|
2012-06-08 21:57:36 +00:00
|
|
|
* Otherwise unlink the file once open.
|
|
|
|
*/
|
|
|
|
int
|
2021-02-11 01:51:11 +00:00
|
|
|
mkTempFile(const char *pattern, char *tfile, size_t tfile_sz)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
static char *tmpdir = NULL;
|
2021-02-11 01:51:11 +00:00
|
|
|
char tbuf[MAXPATHLEN];
|
2020-11-20 03:54:37 +00:00
|
|
|
int fd;
|
2012-09-11 02:56:02 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (pattern == NULL)
|
|
|
|
pattern = TMPPAT;
|
|
|
|
if (tmpdir == NULL)
|
|
|
|
tmpdir = getTmpdir();
|
2021-02-11 01:51:11 +00:00
|
|
|
if (tfile == NULL) {
|
|
|
|
tfile = tbuf;
|
|
|
|
tfile_sz = sizeof tbuf;
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
if (pattern[0] == '/') {
|
2021-02-11 01:51:11 +00:00
|
|
|
snprintf(tfile, tfile_sz, "%s", pattern);
|
2020-11-20 03:54:37 +00:00
|
|
|
} else {
|
2021-02-11 01:51:11 +00:00
|
|
|
snprintf(tfile, tfile_sz, "%s%s", tmpdir, pattern);
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
if ((fd = mkstemp(tfile)) < 0)
|
|
|
|
Punt("Could not create temporary file %s: %s", tfile,
|
|
|
|
strerror(errno));
|
2021-02-11 01:51:11 +00:00
|
|
|
if (tfile == tbuf) {
|
2021-01-14 01:24:34 +00:00
|
|
|
unlink(tfile); /* we just want the descriptor */
|
2020-11-20 03:54:37 +00:00
|
|
|
}
|
|
|
|
return fd;
|
2016-03-11 00:37:02 +00:00
|
|
|
}
|
2012-09-11 02:56:02 +00:00
|
|
|
|
|
|
|
/*
|
2020-11-20 03:54:37 +00:00
|
|
|
* Convert a string representation of a boolean into a boolean value.
|
2021-06-25 18:16:24 +00:00
|
|
|
* Anything that looks like "No", "False", "Off", "0" etc. is false,
|
|
|
|
* the empty string is the fallback, everything else is true.
|
2012-09-11 02:56:02 +00:00
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
|
|
|
ParseBoolean(const char *s, bool fallback)
|
2012-09-11 02:56:02 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
char ch = ch_tolower(s[0]);
|
|
|
|
if (ch == '\0')
|
|
|
|
return fallback;
|
|
|
|
if (ch == '0' || ch == 'f' || ch == 'n')
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2020-11-20 03:54:37 +00:00
|
|
|
if (ch == 'o')
|
|
|
|
return ch_tolower(s[1]) != 'f';
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2012-09-11 02:56:02 +00:00
|
|
|
}
|