2021-06-25 18:16:24 +00:00
|
|
|
/* $NetBSD: meta.c,v 1.181 2021/04/04 10:05:08 rillig Exp $ */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implement 'meta' mode.
|
|
|
|
* Adapted from John Birrell's patches to FreeBSD make.
|
|
|
|
* --sjg
|
|
|
|
*/
|
|
|
|
/*
|
2016-03-11 00:37:02 +00:00
|
|
|
* Copyright (c) 2009-2016, Juniper Networks, Inc.
|
2012-06-08 21:57:36 +00:00
|
|
|
* Portions Copyright (c) 2009, John Birrell.
|
2020-07-08 18:32:15 +00:00
|
|
|
*
|
2012-06-08 21:57:36 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2020-07-08 18:32:15 +00:00
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2012-06-08 21:57:36 +00:00
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2020-07-08 18:32:15 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2012-06-08 21:57:36 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
2020-07-08 18:32:15 +00:00
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2012-06-08 21:57:36 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT
|
|
|
|
* OWNER 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
|
2020-07-08 18:32:15 +00:00
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
|
|
|
#if defined(USE_META)
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
2015-12-07 22:51:25 +00:00
|
|
|
#ifdef HAVE_LIBGEN_H
|
2012-06-08 21:57:36 +00:00
|
|
|
#include <libgen.h>
|
2015-12-07 22:51:25 +00:00
|
|
|
#elif !defined(HAVE_DIRNAME)
|
|
|
|
char * dirname(char *);
|
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
|
|
|
|
#include <err.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "make.h"
|
2020-09-05 16:11:04 +00:00
|
|
|
#include "dir.h"
|
2012-06-08 21:57:36 +00:00
|
|
|
#include "job.h"
|
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
#ifdef USE_FILEMON
|
|
|
|
#include "filemon/filemon.h"
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static BuildMon Mybm; /* for compat */
|
2021-01-14 01:24:34 +00:00
|
|
|
static StringList metaBailiwick = LST_INIT; /* our scope of control */
|
2016-03-11 00:37:02 +00:00
|
|
|
static char *metaBailiwickStr; /* string storage for the list */
|
2021-01-14 01:24:34 +00:00
|
|
|
static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
|
2016-03-11 00:37:02 +00:00
|
|
|
static char *metaIgnorePathsStr; /* string storage for the list */
|
2013-08-02 05:23:57 +00:00
|
|
|
|
|
|
|
#ifndef MAKE_META_IGNORE_PATHS
|
|
|
|
#define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
|
|
|
|
#endif
|
2016-05-20 16:19:56 +00:00
|
|
|
#ifndef MAKE_META_IGNORE_PATTERNS
|
|
|
|
#define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
|
|
|
|
#endif
|
2016-09-08 23:49:33 +00:00
|
|
|
#ifndef MAKE_META_IGNORE_FILTER
|
|
|
|
#define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
|
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool useMeta = false;
|
|
|
|
static bool useFilemon = false;
|
|
|
|
static bool writeMeta = false;
|
|
|
|
static bool metaMissing = false; /* oodate if missing */
|
|
|
|
static bool filemonMissing = false; /* oodate if missing */
|
|
|
|
static bool metaEnv = false; /* don't save env unless asked */
|
|
|
|
static bool metaVerbose = false;
|
|
|
|
static bool metaIgnoreCMDs = false; /* ignore CMDs in .meta files */
|
|
|
|
static bool metaIgnorePatterns = false; /* do we need to do pattern matches */
|
|
|
|
static bool metaIgnoreFilter = false; /* do we have more complex filtering? */
|
|
|
|
static bool metaCurdirOk = false; /* write .meta in .CURDIR Ok? */
|
|
|
|
static bool metaSilent = false; /* if we have a .meta be SILENT */
|
|
|
|
|
|
|
|
extern bool forceJobs;
|
2012-07-06 00:03:45 +00:00
|
|
|
extern char **environ;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
#define MAKE_META_PREFIX ".MAKE.META.PREFIX"
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifndef N2U
|
|
|
|
# define N2U(n, u) (((n) + ((u) - 1)) / (u))
|
|
|
|
#endif
|
|
|
|
#ifndef ROUNDUP
|
|
|
|
# define ROUNDUP(n, u) (N2U((n), (u)) * (u))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(HAVE_STRSEP)
|
2020-11-20 03:54:37 +00:00
|
|
|
# define strsep(s, d) stresep((s), (d), '\0')
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Filemon is a kernel module which snoops certain syscalls.
|
|
|
|
*
|
|
|
|
* C chdir
|
|
|
|
* E exec
|
|
|
|
* F [v]fork
|
|
|
|
* L [sym]link
|
|
|
|
* M rename
|
|
|
|
* R read
|
|
|
|
* W write
|
|
|
|
* S stat
|
|
|
|
*
|
|
|
|
* See meta_oodate below - we mainly care about 'E' and 'R'.
|
|
|
|
*
|
2020-07-08 18:32:15 +00:00
|
|
|
* We can still use meta mode without filemon, but
|
2012-06-08 21:57:36 +00:00
|
|
|
* the benefits are more limited.
|
|
|
|
*/
|
|
|
|
#ifdef USE_FILEMON
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the filemon device.
|
|
|
|
*/
|
|
|
|
static void
|
2020-05-20 19:34:48 +00:00
|
|
|
meta_open_filemon(BuildMon *pbm)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-05-20 19:34:48 +00:00
|
|
|
int dupfd;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
pbm->mon_fd = -1;
|
|
|
|
pbm->filemon = NULL;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!useFilemon || pbm->mfp == NULL)
|
2020-05-20 19:34:48 +00:00
|
|
|
return;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
pbm->filemon = filemon_open();
|
|
|
|
if (pbm->filemon == NULL) {
|
2021-06-25 18:16:24 +00:00
|
|
|
useFilemon = false;
|
2020-05-20 19:34:48 +00:00
|
|
|
warn("Could not open filemon %s", filemon_path());
|
2012-06-08 21:57:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use a file outside of '.'
|
|
|
|
* to avoid a FreeBSD kernel bug where unlink invalidates
|
|
|
|
* cwd causing getcwd to do a lot more work.
|
|
|
|
* We only care about the descriptor.
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
if (!opts.compatMake)
|
|
|
|
pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
|
|
|
|
else
|
|
|
|
pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
|
2020-05-20 19:34:48 +00:00
|
|
|
if ((dupfd = dup(pbm->mon_fd)) == -1) {
|
|
|
|
err(1, "Could not dup filemon output!");
|
|
|
|
}
|
|
|
|
(void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
|
|
|
|
if (filemon_setfd(pbm->filemon, dupfd) == -1) {
|
2012-06-08 21:57:36 +00:00
|
|
|
err(1, "Could not set filemon file descriptor!");
|
|
|
|
}
|
|
|
|
/* we don't need these once we exec */
|
2016-03-11 00:37:02 +00:00
|
|
|
(void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the build monitor output file and write records to the target's
|
|
|
|
* metadata file.
|
|
|
|
*/
|
2016-05-20 16:19:56 +00:00
|
|
|
static int
|
2012-06-08 21:57:36 +00:00
|
|
|
filemon_read(FILE *mfp, int fd)
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ];
|
2016-05-20 16:19:56 +00:00
|
|
|
int error;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/* Check if we're not writing to a meta data file.*/
|
|
|
|
if (mfp == NULL) {
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd); /* not interested */
|
2016-05-20 16:19:56 +00:00
|
|
|
return 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
/* rewind */
|
2020-07-01 02:13:04 +00:00
|
|
|
if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
|
|
|
|
error = errno;
|
|
|
|
warn("Could not rewind filemon");
|
|
|
|
fprintf(mfp, "\n");
|
|
|
|
} else {
|
2020-11-07 19:39:21 +00:00
|
|
|
ssize_t n;
|
|
|
|
|
2020-07-01 02:13:04 +00:00
|
|
|
error = 0;
|
|
|
|
fprintf(mfp, "\n-- filemon acquired metadata --\n");
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
while ((n = read(fd, buf, sizeof buf)) > 0) {
|
2020-11-07 19:39:21 +00:00
|
|
|
if ((ssize_t)fwrite(buf, 1, (size_t)n, mfp) < n)
|
2020-07-01 02:13:04 +00:00
|
|
|
error = EIO;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
fflush(mfp);
|
2016-05-20 16:19:56 +00:00
|
|
|
if (close(fd) < 0)
|
|
|
|
error = errno;
|
|
|
|
return error;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* when realpath() fails,
|
|
|
|
* we use this, to clean up ./ and ../
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
eat_dots(char *buf, size_t bufsz, int dots)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
char *cp2;
|
|
|
|
const char *eat;
|
|
|
|
size_t eatlen;
|
|
|
|
|
|
|
|
switch (dots) {
|
|
|
|
case 1:
|
|
|
|
eat = "/./";
|
|
|
|
eatlen = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
eat = "/../";
|
|
|
|
eatlen = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
do {
|
|
|
|
cp = strstr(buf, eat);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cp != NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
cp2 = cp + eatlen;
|
|
|
|
if (dots == 2 && cp > buf) {
|
|
|
|
do {
|
|
|
|
cp--;
|
|
|
|
} while (cp > buf && *cp != '/');
|
|
|
|
}
|
|
|
|
if (*cp == '/') {
|
2020-11-07 19:39:21 +00:00
|
|
|
strlcpy(cp, cp2, bufsz - (size_t)(cp - buf));
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
|
|
|
return; /* can't happen? */
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
} while (cp != NULL);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2017-03-06 19:54:54 +00:00
|
|
|
meta_name(char *mname, size_t mnamelen,
|
2012-06-08 21:57:36 +00:00
|
|
|
const char *dname,
|
2016-06-05 15:57:18 +00:00
|
|
|
const char *tname,
|
|
|
|
const char *cwd)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
char buf[MAXPATHLEN];
|
|
|
|
char *rp;
|
|
|
|
char *cp;
|
|
|
|
char *tp;
|
2017-11-01 23:16:32 +00:00
|
|
|
char *dtp;
|
|
|
|
size_t ldname;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Weed out relative paths from the target file name.
|
|
|
|
* We have to be careful though since if target is a
|
|
|
|
* symlink, the result will be unstable.
|
|
|
|
* So we use realpath() just to get the dirname, and leave the
|
|
|
|
* basename as given to us.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((cp = strrchr(tname, '/')) != NULL) {
|
|
|
|
if (cached_realpath(tname, buf) != NULL) {
|
|
|
|
if ((rp = strrchr(buf, '/')) != NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
rp++;
|
|
|
|
cp++;
|
|
|
|
if (strcmp(cp, rp) != 0)
|
2020-11-07 19:39:21 +00:00
|
|
|
strlcpy(rp, cp, sizeof buf - (size_t)(rp - buf));
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
tname = buf;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We likely have a directory which is about to be made.
|
|
|
|
* We pretend realpath() succeeded, to have a chance
|
|
|
|
* of generating the same meta file name that we will
|
|
|
|
* next time through.
|
|
|
|
*/
|
|
|
|
if (tname[0] == '/') {
|
2020-11-20 03:54:37 +00:00
|
|
|
strlcpy(buf, tname, sizeof buf);
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(buf, sizeof buf, "%s/%s", cwd, tname);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
eat_dots(buf, sizeof buf, 1); /* ./ */
|
|
|
|
eat_dots(buf, sizeof buf, 2); /* ../ */
|
2012-06-08 21:57:36 +00:00
|
|
|
tname = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* on some systems dirname may modify its arg */
|
|
|
|
tp = bmake_strdup(tname);
|
2017-11-01 23:16:32 +00:00
|
|
|
dtp = dirname(tp);
|
|
|
|
if (strcmp(dname, dtp) == 0)
|
2012-06-08 21:57:36 +00:00
|
|
|
snprintf(mname, mnamelen, "%s.meta", tname);
|
|
|
|
else {
|
2017-11-01 23:16:32 +00:00
|
|
|
ldname = strlen(dname);
|
|
|
|
if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
|
|
|
|
snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
|
|
|
|
else
|
|
|
|
snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace path separators in the file name after the
|
|
|
|
* current object directory path.
|
|
|
|
*/
|
|
|
|
cp = mname + strlen(dname) + 1;
|
|
|
|
|
|
|
|
while (*cp != '\0') {
|
|
|
|
if (*cp == '/')
|
|
|
|
*cp = '_';
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(tp);
|
2020-07-08 18:32:15 +00:00
|
|
|
return mname;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if running ${.MAKE}
|
|
|
|
* Bypassed if target is flagged .MAKE
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2021-01-14 01:24:34 +00:00
|
|
|
is_submake(const char *cmd, GNode *gn)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-09-05 16:11:04 +00:00
|
|
|
static const char *p_make = NULL;
|
2020-11-07 19:39:21 +00:00
|
|
|
static size_t p_len;
|
2012-06-08 21:57:36 +00:00
|
|
|
char *mp = NULL;
|
|
|
|
char *cp;
|
|
|
|
char *cp2;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool rc = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (p_make == NULL) {
|
2021-02-11 01:51:11 +00:00
|
|
|
p_make = Var_Value(gn, ".MAKE").str;
|
2012-06-08 21:57:36 +00:00
|
|
|
p_len = strlen(p_make);
|
|
|
|
}
|
|
|
|
cp = strchr(cmd, '$');
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cp != NULL) {
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst(cmd, gn, VARE_WANTRES, &mp);
|
|
|
|
/* TODO: handle errors */
|
2012-06-08 21:57:36 +00:00
|
|
|
cmd = mp;
|
|
|
|
}
|
|
|
|
cp2 = strstr(cmd, p_make);
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cp2 != NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
switch (cp2[p_len]) {
|
|
|
|
case '\0':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
2021-06-25 18:16:24 +00:00
|
|
|
rc = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cp2 > cmd && rc) {
|
2012-06-08 21:57:36 +00:00
|
|
|
switch (cp2[-1]) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-25 18:16:24 +00:00
|
|
|
rc = false; /* no match */
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 00:37:02 +00:00
|
|
|
free(mp);
|
2020-07-08 18:32:15 +00:00
|
|
|
return rc;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2021-01-14 01:24:34 +00:00
|
|
|
any_is_submake(GNode *gn)
|
|
|
|
{
|
|
|
|
StringListNode *ln;
|
|
|
|
|
|
|
|
for (ln = gn->commands.first; ln != NULL; ln = ln->next)
|
|
|
|
if (is_submake(ln->datum, gn))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
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
|
|
|
printCMD(const char *ucmd, FILE *fp, GNode *gn)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
FStr xcmd = FStr_InitRefer(ucmd);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
if (strchr(ucmd, '$') != NULL) {
|
|
|
|
char *expanded;
|
|
|
|
(void)Var_Subst(ucmd, gn, VARE_WANTRES, &expanded);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2021-06-25 18:16:24 +00:00
|
|
|
xcmd = FStr_InitOwn(expanded);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
|
|
|
|
fprintf(fp, "CMD %s\n", xcmd.str);
|
|
|
|
FStr_Done(&xcmd);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-14 01:24:34 +00:00
|
|
|
printCMDs(GNode *gn, FILE *fp)
|
2020-11-07 19:39:21 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
StringListNode *ln;
|
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)
|
|
|
|
printCMD(ln->datum, fp, gn);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Certain node types never get a .meta file
|
|
|
|
*/
|
|
|
|
#define SKIP_META_TYPE(_type) do { \
|
2020-11-07 19:39:21 +00:00
|
|
|
if ((gn->type & __CONCAT(OP_, _type))) { \
|
2016-06-05 15:57:18 +00:00
|
|
|
if (verbose) { \
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf("Skipping meta for %s: .%s\n", \
|
|
|
|
gn->name, __STRING(_type)); \
|
2012-06-08 21:57:36 +00:00
|
|
|
} \
|
2021-06-25 18:16:24 +00:00
|
|
|
return false; \
|
2012-06-08 21:57:36 +00:00
|
|
|
} \
|
2021-06-25 18:16:24 +00:00
|
|
|
} while (/*CONSTCOND*/false)
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2016-06-05 15:57:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do we need/want a .meta file ?
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2017-03-06 19:54:54 +00:00
|
|
|
meta_needed(GNode *gn, const char *dname,
|
2021-06-25 18:16:24 +00:00
|
|
|
char *objdir_realpath, bool verbose)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-11-20 03:54:37 +00:00
|
|
|
struct cached_stat cst;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2016-06-05 15:57:18 +00:00
|
|
|
if (verbose)
|
|
|
|
verbose = DEBUG(META);
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/* This may be a phony node which we don't want meta data for... */
|
|
|
|
/* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
|
|
|
|
/* Or it may be explicitly flagged as .NOMETA */
|
|
|
|
SKIP_META_TYPE(NOMETA);
|
|
|
|
/* Unless it is explicitly flagged as .META */
|
|
|
|
if (!(gn->type & OP_META)) {
|
|
|
|
SKIP_META_TYPE(PHONY);
|
|
|
|
SKIP_META_TYPE(SPECIAL);
|
|
|
|
SKIP_META_TYPE(MAKE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there are no commands to execute. */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (Lst_IsEmpty(&gn->commands)) {
|
2016-06-05 15:57:18 +00:00
|
|
|
if (verbose)
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf("Skipping meta for %s: no commands\n", gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2016-06-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
|
|
|
|
/* OP_SUBMAKE is a bit too aggressive */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (any_is_submake(gn)) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2016-06-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The object directory may not exist. Check it.. */
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cached_stat(dname, &cst) != 0) {
|
2016-06-05 15:57:18 +00:00
|
|
|
if (verbose)
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure these are canonical */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cached_realpath(dname, objdir_realpath) != NULL)
|
|
|
|
dname = objdir_realpath;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/* If we aren't in the object directory, don't create a meta file. */
|
|
|
|
if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
|
2016-06-05 15:57:18 +00:00
|
|
|
if (verbose)
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
|
|
|
|
gn->name);
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-06-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2016-06-05 15:57:18 +00:00
|
|
|
static FILE *
|
|
|
|
meta_create(BuildMon *pbm, GNode *gn)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
FILE *fp;
|
2016-06-05 15:57:18 +00:00
|
|
|
char buf[MAXPATHLEN];
|
2021-01-14 01:24:34 +00:00
|
|
|
char objdir_realpath[MAXPATHLEN];
|
2016-06-05 15:57:18 +00:00
|
|
|
char **ptr;
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr dname;
|
2016-06-05 15:57:18 +00:00
|
|
|
const char *tname;
|
|
|
|
char *fname;
|
|
|
|
const char *cp;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
fp = NULL;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
dname = Var_Value(gn, ".OBJDIR");
|
2020-11-07 19:39:21 +00:00
|
|
|
tname = GNode_VarTarget(gn);
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/* if this succeeds objdir_realpath is realpath of dname */
|
2021-06-25 18:16:24 +00:00
|
|
|
if (!meta_needed(gn, dname.str, objdir_realpath, true))
|
2016-06-05 15:57:18 +00:00
|
|
|
goto out;
|
2021-01-14 01:24:34 +00:00
|
|
|
dname.str = objdir_realpath;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
if (metaVerbose) {
|
|
|
|
char *mp;
|
|
|
|
|
|
|
|
/* Describe the target we are building */
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_WANTRES, &mp);
|
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (mp[0] != '\0')
|
2012-06-08 21:57:36 +00:00
|
|
|
fprintf(stdout, "%s\n", mp);
|
|
|
|
free(mp);
|
|
|
|
}
|
|
|
|
/* Get the basename of the target */
|
2021-01-14 01:24:34 +00:00
|
|
|
cp = str_basename(tname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (!writeMeta)
|
|
|
|
/* Don't create meta data. */
|
|
|
|
goto out;
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
|
2021-01-14 01:24:34 +00:00
|
|
|
dname.str, tname, objdir_realpath);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_create: %s\n", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((fp = fopen(fname, "w")) == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
err(1, "Could not open meta file '%s'", fname);
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
fprintf(fp, "# Meta data file %s\n", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
printCMDs(gn, fp);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
fprintf(fp, "CWD %s\n", getcwd(buf, sizeof buf));
|
|
|
|
fprintf(fp, "TARGET %s\n", tname);
|
2020-11-07 19:39:21 +00:00
|
|
|
cp = GNode_VarOodate(gn);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (cp != NULL && *cp != '\0') {
|
|
|
|
fprintf(fp, "OODATE %s\n", cp);
|
2020-05-20 19:34:48 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
if (metaEnv) {
|
|
|
|
for (ptr = environ; *ptr != NULL; ptr++)
|
2021-01-14 01:24:34 +00:00
|
|
|
fprintf(fp, "ENV %s\n", *ptr);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
fprintf(fp, "-- command output --\n");
|
|
|
|
fflush(fp);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(".MAKE.META.FILES", fname);
|
|
|
|
Global_Append(".MAKE.META.CREATED", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
gn->type |= OP_META; /* in case anyone wants to know */
|
|
|
|
if (metaSilent) {
|
|
|
|
gn->type |= OP_SILENT;
|
|
|
|
}
|
|
|
|
out:
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&dname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
return fp;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2012-06-08 21:57:36 +00:00
|
|
|
boolValue(char *s)
|
|
|
|
{
|
|
|
|
switch(*s) {
|
|
|
|
case '0':
|
|
|
|
case 'N':
|
|
|
|
case 'n':
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-01 21:12:55 +00:00
|
|
|
/*
|
|
|
|
* Initialization we need before reading makefiles.
|
|
|
|
*/
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
2013-05-20 22:35:32 +00:00
|
|
|
meta_init(void)
|
2013-04-01 21:12:55 +00:00
|
|
|
{
|
|
|
|
#ifdef USE_FILEMON
|
|
|
|
/* this allows makefiles to test if we have filemon support */
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".MAKE.PATH_FILEMON", filemon_path());
|
2013-04-01 21:12:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-05 15:57:18 +00:00
|
|
|
#define get_mode_bf(bf, token) \
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((cp = strstr(make_mode, token)) != NULL) \
|
2020-11-20 03:54:37 +00:00
|
|
|
bf = boolValue(cp + sizeof (token) - 1)
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2013-04-01 21:12:55 +00:00
|
|
|
/*
|
|
|
|
* Initialization we need after reading makefiles.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
meta_mode_init(const char *make_mode)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool once = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
char *cp;
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr value;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
useMeta = true;
|
|
|
|
useFilemon = true;
|
|
|
|
writeMeta = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (make_mode != NULL) {
|
|
|
|
if (strstr(make_mode, "env") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
metaEnv = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr(make_mode, "verb") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
metaVerbose = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr(make_mode, "read") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
writeMeta = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr(make_mode, "nofilemon") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
useFilemon = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr(make_mode, "ignore-cmd") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
metaIgnoreCMDs = true;
|
2016-06-05 15:57:18 +00:00
|
|
|
if (useFilemon)
|
|
|
|
get_mode_bf(filemonMissing, "missing-filemon=");
|
|
|
|
get_mode_bf(metaCurdirOk, "curdirok=");
|
|
|
|
get_mode_bf(metaMissing, "missing-meta=");
|
|
|
|
get_mode_bf(metaSilent, "silent=");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* The default value for MAKE_META_PREFIX
|
|
|
|
* prints the absolute path of the target.
|
|
|
|
* This works be cause :H will generate '.' if there is no /
|
|
|
|
* and :tA will resolve that to cwd.
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(MAKE_META_PREFIX,
|
|
|
|
"Building ${.TARGET:H:tA}/${.TARGET:T}");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
if (once)
|
|
|
|
return;
|
2021-06-25 18:16:24 +00:00
|
|
|
once = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
memset(&Mybm, 0, sizeof Mybm);
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &metaBailiwickStr);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
str2Lst_Append(&metaBailiwick, metaBailiwickStr);
|
2013-08-02 05:23:57 +00:00
|
|
|
/*
|
|
|
|
* We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(MAKE_META_IGNORE_PATHS,
|
|
|
|
"/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}");
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &metaIgnorePathsStr);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
str2Lst_Append(&metaIgnorePaths, metaIgnorePathsStr);
|
2016-05-20 16:19:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
|
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
value = Var_Value(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (value.str != NULL) {
|
2021-06-25 18:16:24 +00:00
|
|
|
metaIgnorePatterns = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&value);
|
2016-05-20 16:19:56 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
value = Var_Value(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (value.str != NULL) {
|
2021-06-25 18:16:24 +00:00
|
|
|
metaIgnoreFilter = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&value);
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In each case below we allow for job==NULL
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
meta_job_start(Job *job, GNode *gn)
|
|
|
|
{
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
|
|
|
pbm->mfp = meta_create(pbm, gn);
|
|
|
|
#ifdef USE_FILEMON_ONCE
|
|
|
|
/* compat mode we open the filemon dev once per command */
|
|
|
|
if (job == NULL)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_FILEMON
|
|
|
|
if (pbm->mfp != NULL && useFilemon) {
|
2020-05-20 19:34:48 +00:00
|
|
|
meta_open_filemon(pbm);
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-05-20 19:34:48 +00:00
|
|
|
pbm->mon_fd = -1;
|
|
|
|
pbm->filemon = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The child calls this before doing anything.
|
|
|
|
* It does not disturb our state.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
meta_job_child(Job *job)
|
|
|
|
{
|
|
|
|
#ifdef USE_FILEMON
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
2015-04-20 03:45:54 +00:00
|
|
|
if (pbm->mfp != NULL) {
|
|
|
|
close(fileno(pbm->mfp));
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useFilemon && pbm->filemon != NULL) {
|
2015-04-20 03:45:54 +00:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pid = getpid();
|
2020-05-20 19:34:48 +00:00
|
|
|
if (filemon_setpid_child(pbm->filemon, pid) == -1) {
|
2015-04-20 03:45:54 +00:00
|
|
|
err(1, "Could not set filemon pid!");
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:34:48 +00:00
|
|
|
void
|
|
|
|
meta_job_parent(Job *job, pid_t pid)
|
|
|
|
{
|
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useFilemon && pbm->filemon != NULL) {
|
2020-05-20 19:34:48 +00:00
|
|
|
filemon_setpid_parent(pbm->filemon, pid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
meta_job_fd(Job *job)
|
|
|
|
{
|
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useFilemon && pbm->filemon != NULL) {
|
2020-05-20 19:34:48 +00:00
|
|
|
return filemon_readfd(pbm->filemon);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
meta_job_event(Job *job)
|
|
|
|
{
|
|
|
|
#if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
if (useFilemon && pbm->filemon != NULL) {
|
2020-05-20 19:34:48 +00:00
|
|
|
return filemon_process(pbm->filemon);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
void
|
2021-06-25 18:16:24 +00:00
|
|
|
meta_job_error(Job *job, GNode *gn, bool ignerr, int status)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
char cwd[MAXPATHLEN];
|
|
|
|
BuildMon *pbm;
|
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
2020-11-20 03:54:37 +00:00
|
|
|
if (gn == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
gn = job->node;
|
2016-03-11 00:37:02 +00:00
|
|
|
} else {
|
2012-06-08 21:57:36 +00:00
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
|
|
|
if (pbm->mfp != NULL) {
|
2017-07-15 18:43:39 +00:00
|
|
|
fprintf(pbm->mfp, "\n*** Error code %d%s\n",
|
2021-01-14 01:24:34 +00:00
|
|
|
status, ignerr ? "(ignored)" : "");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
if (gn != NULL)
|
|
|
|
Global_Set(".ERROR_TARGET", GNode_Path(gn));
|
2020-11-20 03:54:37 +00:00
|
|
|
getcwd(cwd, sizeof cwd);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".ERROR_CWD", cwd);
|
2020-11-20 03:54:37 +00:00
|
|
|
if (pbm->meta_fname[0] != '\0') {
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Set(".ERROR_META_FILE", pbm->meta_fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
meta_job_finish(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
meta_job_output(Job *job, char *cp, const char *nl)
|
|
|
|
{
|
|
|
|
BuildMon *pbm;
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
|
|
|
if (pbm->mfp != NULL) {
|
|
|
|
if (metaVerbose) {
|
|
|
|
static char *meta_prefix = NULL;
|
2020-11-07 19:39:21 +00:00
|
|
|
static size_t meta_prefix_len;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (meta_prefix == NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
char *cp2;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst("${" MAKE_META_PREFIX "}",
|
2021-02-11 01:51:11 +00:00
|
|
|
SCOPE_GLOBAL, VARE_WANTRES, &meta_prefix);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((cp2 = strchr(meta_prefix, '$')) != NULL)
|
2020-11-07 19:39:21 +00:00
|
|
|
meta_prefix_len = (size_t)(cp2 - meta_prefix);
|
2012-06-08 21:57:36 +00:00
|
|
|
else
|
|
|
|
meta_prefix_len = strlen(meta_prefix);
|
|
|
|
}
|
|
|
|
if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
|
2021-01-14 01:24:34 +00:00
|
|
|
cp = strchr(cp + 1, '\n');
|
|
|
|
if (cp == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
return;
|
2021-01-14 01:24:34 +00:00
|
|
|
cp++;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(pbm->mfp, "%s%s", cp, nl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:19:56 +00:00
|
|
|
int
|
2012-06-08 21:57:36 +00:00
|
|
|
meta_cmd_finish(void *pbmp)
|
|
|
|
{
|
2016-05-20 16:19:56 +00:00
|
|
|
int error = 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
BuildMon *pbm = pbmp;
|
2018-03-01 23:45:44 +00:00
|
|
|
#ifdef USE_FILEMON
|
2016-05-20 16:19:56 +00:00
|
|
|
int x;
|
2018-03-01 23:45:44 +00:00
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (pbm == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
pbm = &Mybm;
|
|
|
|
|
2017-07-15 18:43:39 +00:00
|
|
|
#ifdef USE_FILEMON
|
2021-01-14 01:24:34 +00:00
|
|
|
if (pbm->filemon != NULL) {
|
2020-05-20 19:34:48 +00:00
|
|
|
while (filemon_process(pbm->filemon) > 0)
|
|
|
|
continue;
|
|
|
|
if (filemon_close(pbm->filemon) == -1)
|
2016-05-20 16:19:56 +00:00
|
|
|
error = errno;
|
|
|
|
x = filemon_read(pbm->mfp, pbm->mon_fd);
|
|
|
|
if (error == 0 && x != 0)
|
|
|
|
error = x;
|
2020-05-20 19:34:48 +00:00
|
|
|
pbm->mon_fd = -1;
|
|
|
|
pbm->filemon = NULL;
|
2020-11-20 03:54:37 +00:00
|
|
|
return error;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
|
|
|
|
fprintf(pbm->mfp, "\n"); /* ensure end with newline */
|
2016-05-20 16:19:56 +00:00
|
|
|
return error;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:19:56 +00:00
|
|
|
int
|
2012-06-08 21:57:36 +00:00
|
|
|
meta_job_finish(Job *job)
|
|
|
|
{
|
|
|
|
BuildMon *pbm;
|
2016-05-20 16:19:56 +00:00
|
|
|
int error = 0;
|
|
|
|
int x;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
if (job != NULL) {
|
|
|
|
pbm = &job->bm;
|
|
|
|
} else {
|
|
|
|
pbm = &Mybm;
|
|
|
|
}
|
|
|
|
if (pbm->mfp != NULL) {
|
2016-05-20 16:19:56 +00:00
|
|
|
error = meta_cmd_finish(pbm);
|
|
|
|
x = fclose(pbm->mfp);
|
|
|
|
if (error == 0 && x != 0)
|
|
|
|
error = errno;
|
2012-06-08 21:57:36 +00:00
|
|
|
pbm->mfp = NULL;
|
|
|
|
pbm->meta_fname[0] = '\0';
|
|
|
|
}
|
2016-05-20 16:19:56 +00:00
|
|
|
return error;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 00:37:02 +00:00
|
|
|
void
|
|
|
|
meta_finish(void)
|
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Done(&metaBailiwick);
|
2016-03-11 00:37:02 +00:00
|
|
|
free(metaBailiwickStr);
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Done(&metaIgnorePaths);
|
2016-03-11 00:37:02 +00:00
|
|
|
free(metaIgnorePathsStr);
|
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* Fetch a full line from fp - growing bufp if needed
|
|
|
|
* Return length in bufp.
|
|
|
|
*/
|
2020-07-08 18:32:15 +00:00
|
|
|
static int
|
2012-06-08 21:57:36 +00:00
|
|
|
fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
|
|
|
|
{
|
|
|
|
char *buf = *bufp;
|
|
|
|
size_t bufsz = *szp;
|
|
|
|
struct stat fs;
|
|
|
|
int x;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
check_newline:
|
2020-11-07 19:39:21 +00:00
|
|
|
x = o + (int)strlen(&buf[o]);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (buf[x - 1] == '\n')
|
|
|
|
return x;
|
|
|
|
/*
|
|
|
|
* We need to grow the buffer.
|
|
|
|
* The meta file can give us a clue.
|
|
|
|
*/
|
|
|
|
if (fstat(fileno(fp), &fs) == 0) {
|
|
|
|
size_t newsz;
|
|
|
|
char *p;
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (newsz <= bufsz)
|
2020-11-07 19:39:21 +00:00
|
|
|
newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
|
2017-07-15 18:43:39 +00:00
|
|
|
if (newsz <= bufsz)
|
|
|
|
return x; /* truncated */
|
2021-01-14 01:24:34 +00:00
|
|
|
DEBUG2(META, "growing buffer %u -> %u\n",
|
|
|
|
(unsigned)bufsz, (unsigned)newsz);
|
2012-06-08 21:57:36 +00:00
|
|
|
p = bmake_realloc(buf, newsz);
|
2021-01-14 01:24:34 +00:00
|
|
|
*bufp = buf = p;
|
|
|
|
*szp = bufsz = newsz;
|
|
|
|
/* fetch the rest */
|
|
|
|
if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
|
|
|
|
return x; /* truncated! */
|
|
|
|
goto check_newline;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2021-01-14 01:24:34 +00:00
|
|
|
prefix_match(const char *prefix, const char *path)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
size_t n = strlen(prefix);
|
|
|
|
|
2020-07-08 18:32:15 +00:00
|
|
|
return strncmp(path, prefix, n) == 0;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2021-01-14 01:24:34 +00:00
|
|
|
has_any_prefix(const char *path, StringList *prefixes)
|
|
|
|
{
|
|
|
|
StringListNode *ln;
|
|
|
|
|
|
|
|
for (ln = prefixes->first; ln != NULL; ln = ln->next)
|
|
|
|
if (prefix_match(ln->datum, path))
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2021-01-14 01:24:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
/* See if the path equals prefix or starts with "prefix/". */
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2020-11-07 19:39:21 +00:00
|
|
|
path_starts_with(const char *path, const char *prefix)
|
2016-09-08 23:49:33 +00:00
|
|
|
{
|
|
|
|
size_t n = strlen(prefix);
|
|
|
|
|
2020-09-05 16:11:04 +00:00
|
|
|
if (strncmp(path, prefix, n) != 0)
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2020-09-05 16:11:04 +00:00
|
|
|
return path[n] == '\0' || path[n] == '/';
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
static bool
|
2016-09-08 23:49:33 +00:00
|
|
|
meta_ignore(GNode *gn, const char *p)
|
|
|
|
{
|
|
|
|
char fname[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (p == NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-09-08 23:49:33 +00:00
|
|
|
|
|
|
|
if (*p == '/') {
|
|
|
|
cached_realpath(p, fname); /* clean it up */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (has_any_prefix(fname, &metaIgnorePaths)) {
|
2016-09-08 23:49:33 +00:00
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
|
2016-09-08 23:49:33 +00:00
|
|
|
#endif
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metaIgnorePatterns) {
|
2020-09-05 16:11:04 +00:00
|
|
|
const char *expr;
|
2016-09-08 23:49:33 +00:00
|
|
|
char *pm;
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
/*
|
|
|
|
* XXX: This variable is set on a target GNode but is not one of
|
|
|
|
* the usual local variables. It should be deleted afterwards.
|
|
|
|
* Ideally it would not be created in the first place, just like
|
|
|
|
* in a .for loop.
|
|
|
|
*/
|
|
|
|
Var_Set(gn, ".p.", p);
|
2020-09-05 16:11:04 +00:00
|
|
|
expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst(expr, gn, VARE_WANTRES, &pm);
|
|
|
|
/* TODO: handle errors */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (pm[0] != '\0') {
|
2016-09-08 23:49:33 +00:00
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
|
2016-09-08 23:49:33 +00:00
|
|
|
#endif
|
|
|
|
free(pm);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
|
|
|
free(pm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metaIgnoreFilter) {
|
|
|
|
char *fm;
|
|
|
|
|
|
|
|
/* skip if filter result is empty */
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(fname, sizeof fname,
|
2016-09-08 23:49:33 +00:00
|
|
|
"${%s:L:${%s:ts:}}",
|
|
|
|
p, MAKE_META_IGNORE_FILTER);
|
2020-11-07 19:39:21 +00:00
|
|
|
(void)Var_Subst(fname, gn, VARE_WANTRES, &fm);
|
|
|
|
/* TODO: handle errors */
|
2016-09-08 23:49:33 +00:00
|
|
|
if (*fm == '\0') {
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
|
2016-09-08 23:49:33 +00:00
|
|
|
#endif
|
|
|
|
free(fm);
|
2021-06-25 18:16:24 +00:00
|
|
|
return true;
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
|
|
|
free(fm);
|
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
return false;
|
2016-09-08 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* When running with 'meta' functionality, a target can be out-of-date
|
2015-04-20 03:45:54 +00:00
|
|
|
* if any of the references in its meta data file is more recent.
|
2012-06-08 21:57:36 +00:00
|
|
|
* We have to track the latestdir on a per-process basis.
|
|
|
|
*/
|
2015-04-20 03:45:54 +00:00
|
|
|
#define LCWD_VNAME_FMT ".meta.%d.lcwd"
|
2012-06-08 21:57:36 +00:00
|
|
|
#define LDIR_VNAME_FMT ".meta.%d.ldir"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible that a .meta file is corrupted,
|
|
|
|
* if we detect this we want to reproduce it.
|
2021-06-25 18:16:24 +00:00
|
|
|
* Setting oodate true will have that effect.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
#define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
|
2012-06-08 21:57:36 +00:00
|
|
|
warnx("%s: %d: malformed", fname, lineno); \
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true; \
|
2012-06-08 21:57:36 +00:00
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:40:17 +00:00
|
|
|
#define DEQUOTE(p) if (*p == '\'') { \
|
|
|
|
char *ep; \
|
|
|
|
p++; \
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((ep = strchr(p, '\'')) != NULL) \
|
2014-01-26 07:40:17 +00:00
|
|
|
*ep = '\0'; \
|
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static void
|
|
|
|
append_if_new(StringList *list, const char *str)
|
|
|
|
{
|
|
|
|
StringListNode *ln;
|
|
|
|
|
|
|
|
for (ln = list->first; ln != NULL; ln = ln->next)
|
|
|
|
if (strcmp(ln->datum, str) == 0)
|
|
|
|
return;
|
|
|
|
Lst_Append(list, bmake_strdup(str));
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:16:24 +00:00
|
|
|
bool
|
|
|
|
meta_oodate(GNode *gn, bool oodate)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
|
|
|
static char *tmpdir = NULL;
|
|
|
|
static char cwd[MAXPATHLEN];
|
2015-04-20 03:45:54 +00:00
|
|
|
char lcwd_vname[64];
|
2012-06-08 21:57:36 +00:00
|
|
|
char ldir_vname[64];
|
2015-04-20 03:45:54 +00:00
|
|
|
char lcwd[MAXPATHLEN];
|
2012-06-08 21:57:36 +00:00
|
|
|
char latestdir[MAXPATHLEN];
|
|
|
|
char fname[MAXPATHLEN];
|
|
|
|
char fname1[MAXPATHLEN];
|
|
|
|
char fname2[MAXPATHLEN];
|
2015-04-20 03:45:54 +00:00
|
|
|
char fname3[MAXPATHLEN];
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr dname;
|
2016-06-05 15:57:18 +00:00
|
|
|
const char *tname;
|
2012-06-08 21:57:36 +00:00
|
|
|
char *p;
|
2014-01-26 07:40:17 +00:00
|
|
|
char *link_src;
|
|
|
|
char *move_target;
|
2012-06-08 21:57:36 +00:00
|
|
|
static size_t cwdlen = 0;
|
|
|
|
static size_t tmplen = 0;
|
|
|
|
FILE *fp;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool needOODATE = false;
|
2021-01-14 01:24:34 +00:00
|
|
|
StringList missingFiles;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool have_filemon = false;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
if (oodate)
|
|
|
|
return oodate; /* we're done */
|
|
|
|
|
2021-02-11 01:51:11 +00:00
|
|
|
dname = Var_Value(gn, ".OBJDIR");
|
2020-11-07 19:39:21 +00:00
|
|
|
tname = GNode_VarTarget(gn);
|
2016-06-05 15:57:18 +00:00
|
|
|
|
|
|
|
/* if this succeeds fname3 is realpath of dname */
|
2021-06-25 18:16:24 +00:00
|
|
|
if (!meta_needed(gn, dname.str, fname3, false))
|
2016-06-05 15:57:18 +00:00
|
|
|
goto oodate_out;
|
2021-01-14 01:24:34 +00:00
|
|
|
dname.str = fname3;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Init(&missingFiles);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to check if the target is out-of-date. This includes
|
|
|
|
* checking if the expanded command has changed. This in turn
|
|
|
|
* requires that all variables are set in the same way that they
|
|
|
|
* would be if the target needs to be re-built.
|
|
|
|
*/
|
2021-06-25 18:16:24 +00:00
|
|
|
GNode_SetLocalVars(gn);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
meta_name(fname, sizeof fname, dname.str, tname, dname.str);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_oodate: %s\n", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((fp = fopen(fname, "r")) != NULL) {
|
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t bufsz;
|
|
|
|
int lineno = 0;
|
|
|
|
int lastpid = 0;
|
|
|
|
int pid;
|
|
|
|
int x;
|
2020-11-07 19:39:21 +00:00
|
|
|
StringListNode *cmdNode;
|
2020-11-20 03:54:37 +00:00
|
|
|
struct cached_stat cst;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (buf == NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
bufsz = 8 * BUFSIZ;
|
|
|
|
buf = bmake_malloc(bufsz);
|
|
|
|
}
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cwdlen == 0) {
|
|
|
|
if (getcwd(cwd, sizeof cwd) == NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
err(1, "Could not get current working directory");
|
|
|
|
cwdlen = strlen(cwd);
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
strlcpy(lcwd, cwd, sizeof lcwd);
|
|
|
|
strlcpy(latestdir, cwd, sizeof latestdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if (tmpdir == NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
tmpdir = getTmpdir();
|
|
|
|
tmplen = strlen(tmpdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we want to track all the .meta we read */
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_Append(".MAKE.META.FILES", fname);
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
cmdNode = gn->commands.first;
|
2012-06-08 21:57:36 +00:00
|
|
|
while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
|
|
|
|
lineno++;
|
|
|
|
if (buf[x - 1] == '\n')
|
|
|
|
buf[x - 1] = '\0';
|
|
|
|
else {
|
|
|
|
warnx("%s: %d: line truncated at %u", fname, lineno, x);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-01-26 07:40:17 +00:00
|
|
|
link_src = NULL;
|
|
|
|
move_target = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
/* Find the start of the build monitor section. */
|
2016-06-05 15:57:18 +00:00
|
|
|
if (!have_filemon) {
|
2012-06-08 21:57:36 +00:00
|
|
|
if (strncmp(buf, "-- filemon", 10) == 0) {
|
2021-06-25 18:16:24 +00:00
|
|
|
have_filemon = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp(buf, "# buildmon", 10) == 0) {
|
2021-06-25 18:16:24 +00:00
|
|
|
have_filemon = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-08 18:32:15 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/* Delimit the record type. */
|
|
|
|
p = buf;
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG3(META, "%s: %d: %s\n", fname, lineno, buf);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
strsep(&p, " ");
|
2016-06-05 15:57:18 +00:00
|
|
|
if (have_filemon) {
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* We are in the 'filemon' output section.
|
|
|
|
* Each record from filemon follows the general form:
|
|
|
|
*
|
|
|
|
* <key> <pid> <data>
|
|
|
|
*
|
|
|
|
* Where:
|
|
|
|
* <key> is a single letter, denoting the syscall.
|
|
|
|
* <pid> is the process that made the syscall.
|
|
|
|
* <data> is the arguments (of interest).
|
|
|
|
*/
|
|
|
|
switch(buf[0]) {
|
|
|
|
case '#': /* comment */
|
|
|
|
case 'V': /* version */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* We need to track pathnames per-process.
|
|
|
|
*
|
2021-02-11 01:51:11 +00:00
|
|
|
* Each process run by make starts off in the 'CWD'
|
2012-06-08 21:57:36 +00:00
|
|
|
* recorded in the .meta file, if it chdirs ('C')
|
|
|
|
* elsewhere we need to track that - but only for
|
|
|
|
* that process. If it forks ('F'), we initialize
|
|
|
|
* the child to have the same cwd as its parent.
|
|
|
|
*
|
|
|
|
* We also need to track the 'latestdir' of
|
|
|
|
* interest. This is usually the same as cwd, but
|
|
|
|
* not if a process is reading directories.
|
|
|
|
*
|
|
|
|
* Each time we spot a different process ('pid')
|
|
|
|
* we save the current value of 'latestdir' in a
|
|
|
|
* variable qualified by 'lastpid', and
|
|
|
|
* re-initialize 'latestdir' to any pre-saved
|
|
|
|
* value for the current 'pid' and 'CWD' if none.
|
|
|
|
*/
|
|
|
|
CHECK_VALID_META(p);
|
|
|
|
pid = atoi(p);
|
|
|
|
if (pid > 0 && pid != lastpid) {
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr ldir;
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
if (lastpid > 0) {
|
2015-04-20 03:45:54 +00:00
|
|
|
/* We need to remember these. */
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_SetExpand(lcwd_vname, lcwd);
|
|
|
|
Global_SetExpand(ldir_vname, latestdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(lcwd_vname, sizeof lcwd_vname, LCWD_VNAME_FMT, pid);
|
|
|
|
snprintf(ldir_vname, sizeof ldir_vname, LDIR_VNAME_FMT, pid);
|
2012-06-08 21:57:36 +00:00
|
|
|
lastpid = pid;
|
2021-02-11 01:51:11 +00:00
|
|
|
ldir = Var_Value(SCOPE_GLOBAL, ldir_vname);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (ldir.str != NULL) {
|
|
|
|
strlcpy(latestdir, ldir.str, sizeof latestdir);
|
|
|
|
FStr_Done(&ldir);
|
2015-04-20 03:45:54 +00:00
|
|
|
}
|
2021-02-11 01:51:11 +00:00
|
|
|
ldir = Var_Value(SCOPE_GLOBAL, lcwd_vname);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (ldir.str != NULL) {
|
|
|
|
strlcpy(lcwd, ldir.str, sizeof lcwd);
|
|
|
|
FStr_Done(&ldir);
|
2015-04-20 03:45:54 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
/* Skip past the pid. */
|
|
|
|
if (strsep(&p, " ") == NULL)
|
|
|
|
continue;
|
|
|
|
#ifdef DEBUG_META_MODE
|
|
|
|
if (DEBUG(META))
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf("%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
|
|
|
|
fname, lineno,
|
|
|
|
pid, buf[0], cwd, lcwd, latestdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_VALID_META(p);
|
|
|
|
|
|
|
|
/* Process according to record type. */
|
|
|
|
switch (buf[0]) {
|
|
|
|
case 'X': /* eXit */
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_DeleteExpand(SCOPE_GLOBAL, lcwd_vname);
|
|
|
|
Var_DeleteExpand(SCOPE_GLOBAL, ldir_vname);
|
2012-06-08 21:57:36 +00:00
|
|
|
lastpid = 0; /* no need to save ldir_vname */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F': /* [v]Fork */
|
|
|
|
{
|
|
|
|
char cldir[64];
|
|
|
|
int child;
|
|
|
|
|
|
|
|
child = atoi(p);
|
|
|
|
if (child > 0) {
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(cldir, sizeof cldir, LCWD_VNAME_FMT, child);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_SetExpand(cldir, lcwd);
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(cldir, sizeof cldir, LDIR_VNAME_FMT, child);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_SetExpand(cldir, latestdir);
|
2015-04-20 03:45:54 +00:00
|
|
|
#ifdef DEBUG_META_MODE
|
|
|
|
if (DEBUG(META))
|
2020-11-07 19:39:21 +00:00
|
|
|
debug_printf(
|
|
|
|
"%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
|
|
|
|
fname, lineno,
|
|
|
|
child, cwd, lcwd, latestdir);
|
2015-04-20 03:45:54 +00:00
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C': /* Chdir */
|
2015-04-20 03:45:54 +00:00
|
|
|
/* Update lcwd and latest directory. */
|
2020-11-20 03:54:37 +00:00
|
|
|
strlcpy(latestdir, p, sizeof latestdir);
|
|
|
|
strlcpy(lcwd, p, sizeof lcwd);
|
2021-02-11 01:51:11 +00:00
|
|
|
Global_SetExpand(lcwd_vname, lcwd);
|
|
|
|
Global_SetExpand(ldir_vname, lcwd);
|
2015-04-20 03:45:54 +00:00
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG4(META, "%s: %d: cwd=%s ldir=%s\n",
|
|
|
|
fname, lineno, cwd, lcwd);
|
2015-04-20 03:45:54 +00:00
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M': /* renaMe */
|
2014-01-26 07:40:17 +00:00
|
|
|
/*
|
|
|
|
* For 'M'oves we want to check
|
|
|
|
* the src as for 'R'ead
|
|
|
|
* and the target as for 'W'rite.
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
{
|
|
|
|
char *cp = p; /* save this for a second */
|
|
|
|
/* now get target */
|
|
|
|
if (strsep(&p, " ") == NULL)
|
|
|
|
continue;
|
|
|
|
CHECK_VALID_META(p);
|
|
|
|
move_target = p;
|
|
|
|
p = cp;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
/* 'L' and 'M' put single quotes around the args */
|
2014-01-26 07:40:17 +00:00
|
|
|
DEQUOTE(p);
|
|
|
|
DEQUOTE(move_target);
|
2012-06-08 21:57:36 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'D': /* unlink */
|
2020-11-07 19:39:21 +00:00
|
|
|
if (*p == '/') {
|
2016-09-08 23:49:33 +00:00
|
|
|
/* remove any missingFiles entries that match p */
|
2021-01-14 01:24:34 +00:00
|
|
|
StringListNode *ln = missingFiles.first;
|
2020-11-07 19:39:21 +00:00
|
|
|
while (ln != NULL) {
|
|
|
|
StringListNode *next = ln->next;
|
|
|
|
if (path_starts_with(ln->datum, p)) {
|
|
|
|
free(ln->datum);
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Remove(&missingFiles, ln);
|
2020-11-07 19:39:21 +00:00
|
|
|
}
|
|
|
|
ln = next;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 07:40:17 +00:00
|
|
|
if (buf[0] == 'M') {
|
|
|
|
/* the target of the mv is a file 'W'ritten */
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG2(META, "meta_oodate: M %s -> %s\n",
|
|
|
|
p, move_target);
|
2014-01-26 07:40:17 +00:00
|
|
|
#endif
|
|
|
|
p = move_target;
|
|
|
|
goto check_write;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
case 'L': /* Link */
|
2014-01-26 07:40:17 +00:00
|
|
|
/*
|
|
|
|
* For 'L'inks check
|
|
|
|
* the src as for 'R'ead
|
|
|
|
* and the target as for 'W'rite.
|
|
|
|
*/
|
|
|
|
link_src = p;
|
|
|
|
/* now get target */
|
2012-06-08 21:57:36 +00:00
|
|
|
if (strsep(&p, " ") == NULL)
|
|
|
|
continue;
|
|
|
|
CHECK_VALID_META(p);
|
|
|
|
/* 'L' and 'M' put single quotes around the args */
|
2014-01-26 07:40:17 +00:00
|
|
|
DEQUOTE(p);
|
|
|
|
DEQUOTE(link_src);
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG2(META, "meta_oodate: L %s -> %s\n", link_src, p);
|
2014-01-26 07:40:17 +00:00
|
|
|
#endif
|
2012-06-08 21:57:36 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'W': /* Write */
|
2014-01-26 07:40:17 +00:00
|
|
|
check_write:
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* If a file we generated within our bailiwick
|
|
|
|
* but outside of .OBJDIR is missing,
|
2020-07-08 18:32:15 +00:00
|
|
|
* we need to do it again.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
|
|
|
/* ignore non-absolute paths */
|
|
|
|
if (*p != '/')
|
|
|
|
break;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (Lst_IsEmpty(&metaBailiwick))
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* ignore cwd - normal dependencies handle those */
|
|
|
|
if (strncmp(p, cwd, cwdlen) == 0)
|
|
|
|
break;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!has_any_prefix(p, &metaBailiwick))
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* tmpdir might be within */
|
|
|
|
if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* ignore anything containing the string "tmp" */
|
2020-11-20 03:54:37 +00:00
|
|
|
/* XXX: The arguments to strstr must be swapped. */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr("tmp", p) != NULL)
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
|
2020-11-20 03:54:37 +00:00
|
|
|
if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
|
|
|
|
(link_src == NULL && cached_stat(p, &cst) < 0)) {
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!meta_ignore(gn, p))
|
2021-01-14 01:24:34 +00:00
|
|
|
append_if_new(&missingFiles, p);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-01-26 07:40:17 +00:00
|
|
|
check_link_src:
|
|
|
|
p = link_src;
|
|
|
|
link_src = NULL;
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "meta_oodate: L src %s\n", p);
|
2014-01-26 07:40:17 +00:00
|
|
|
#endif
|
|
|
|
/* FALLTHROUGH */
|
2012-06-08 21:57:36 +00:00
|
|
|
case 'R': /* Read */
|
|
|
|
case 'E': /* Exec */
|
|
|
|
/*
|
|
|
|
* Check for runtime files that can't
|
|
|
|
* be part of the dependencies because
|
|
|
|
* they are _expected_ to change.
|
|
|
|
*/
|
2016-09-08 23:49:33 +00:00
|
|
|
if (meta_ignore(gn, p))
|
|
|
|
break;
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
|
|
|
* The rest of the record is the file name.
|
|
|
|
* Check if it's not an absolute path.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
char *sdirs[4];
|
|
|
|
char **sdp;
|
|
|
|
int sdx = 0;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool found = false;
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
if (*p == '/') {
|
|
|
|
sdirs[sdx++] = p; /* done */
|
|
|
|
} else {
|
|
|
|
if (strcmp(".", p) == 0)
|
2021-01-14 01:24:34 +00:00
|
|
|
continue; /* no point */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
|
|
|
/* Check vs latestdir */
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p);
|
2012-06-08 21:57:36 +00:00
|
|
|
sdirs[sdx++] = fname1;
|
|
|
|
|
2015-04-20 03:45:54 +00:00
|
|
|
if (strcmp(latestdir, lcwd) != 0) {
|
|
|
|
/* Check vs lcwd */
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p);
|
2012-06-08 21:57:36 +00:00
|
|
|
sdirs[sdx++] = fname2;
|
|
|
|
}
|
2015-04-20 03:45:54 +00:00
|
|
|
if (strcmp(lcwd, cwd) != 0) {
|
|
|
|
/* Check vs cwd */
|
2020-11-20 03:54:37 +00:00
|
|
|
snprintf(fname3, sizeof fname3, "%s/%s", cwd, p);
|
2015-04-20 03:45:54 +00:00
|
|
|
sdirs[sdx++] = fname3;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
sdirs[sdx++] = NULL;
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (sdp = sdirs; *sdp != NULL && !found; sdp++) {
|
2012-06-08 21:57:36 +00:00
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG3(META, "%s: %d: looking for: %s\n",
|
|
|
|
fname, lineno, *sdp);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cached_stat(*sdp, &cst) == 0) {
|
2021-06-25 18:16:24 +00:00
|
|
|
found = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
p = *sdp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
#ifdef DEBUG_META_MODE
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG3(META, "%s: %d: found: %s\n",
|
|
|
|
fname, lineno, p);
|
2012-06-08 21:57:36 +00:00
|
|
|
#endif
|
2020-11-20 03:54:37 +00:00
|
|
|
if (!S_ISDIR(cst.cst_mode) &&
|
|
|
|
cst.cst_mtime > gn->mtime) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG3(META, "%s: %d: file '%s' is newer than the target...\n",
|
|
|
|
fname, lineno, p);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2020-11-20 03:54:37 +00:00
|
|
|
} else if (S_ISDIR(cst.cst_mode)) {
|
2012-06-08 21:57:36 +00:00
|
|
|
/* Update the latest directory. */
|
2016-06-05 15:57:18 +00:00
|
|
|
cached_realpath(p, latestdir);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
} else if (errno == ENOENT && *p == '/' &&
|
|
|
|
strncmp(p, cwd, cwdlen) != 0) {
|
|
|
|
/*
|
|
|
|
* A referenced file outside of CWD is missing.
|
|
|
|
* We cannot catch every eventuality here...
|
|
|
|
*/
|
2021-01-14 01:24:34 +00:00
|
|
|
append_if_new(&missingFiles, p);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 03:45:54 +00:00
|
|
|
if (buf[0] == 'E') {
|
|
|
|
/* previous latestdir is no longer relevant */
|
2020-11-20 03:54:37 +00:00
|
|
|
strlcpy(latestdir, lcwd, sizeof latestdir);
|
2015-04-20 03:45:54 +00:00
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-01-26 07:40:17 +00:00
|
|
|
if (!oodate && buf[0] == 'L' && link_src != NULL)
|
|
|
|
goto check_link_src;
|
2012-06-08 21:57:36 +00:00
|
|
|
} else if (strcmp(buf, "CMD") == 0) {
|
|
|
|
/*
|
|
|
|
* Compare the current command with the one in the
|
|
|
|
* meta data file.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
if (cmdNode == NULL) {
|
|
|
|
DEBUG2(META, "%s: %d: there were more build commands in the meta data file than there are now...\n",
|
|
|
|
fname, lineno);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *cp;
|
2020-11-07 19:39:21 +00:00
|
|
|
char *cmd = cmdNode->datum;
|
2021-06-25 18:16:24 +00:00
|
|
|
bool hasOODATE = false;
|
2013-04-01 21:12:55 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (strstr(cmd, "$?") != NULL)
|
2021-06-25 18:16:24 +00:00
|
|
|
hasOODATE = true;
|
2021-01-14 01:24:34 +00:00
|
|
|
else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
|
2013-04-01 21:12:55 +00:00
|
|
|
/* check for $[{(].OODATE[:)}] */
|
|
|
|
if (cp > cmd + 2 && cp[-2] == '$')
|
2021-06-25 18:16:24 +00:00
|
|
|
hasOODATE = true;
|
2013-04-01 21:12:55 +00:00
|
|
|
}
|
|
|
|
if (hasOODATE) {
|
2021-06-25 18:16:24 +00:00
|
|
|
needOODATE = true;
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG2(META, "%s: %d: cannot compare command using .OODATE\n",
|
|
|
|
fname, lineno);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2021-06-25 18:16:24 +00:00
|
|
|
(void)Var_Subst(cmd, gn, VARE_UNDEFERR, &cmd);
|
2020-11-07 19:39:21 +00:00
|
|
|
/* TODO: handle errors */
|
2012-06-08 21:57:36 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if ((cp = strchr(cmd, '\n')) != NULL) {
|
2012-06-08 21:57:36 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This command contains newlines, we need to
|
|
|
|
* fetch more from the .meta file before we
|
|
|
|
* attempt a comparison.
|
|
|
|
*/
|
|
|
|
/* first put the newline back at buf[x - 1] */
|
|
|
|
buf[x - 1] = '\n';
|
|
|
|
do {
|
|
|
|
/* now fetch the next line */
|
|
|
|
if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
|
|
|
|
break;
|
|
|
|
x = n;
|
|
|
|
lineno++;
|
|
|
|
if (buf[x - 1] != '\n') {
|
|
|
|
warnx("%s: %d: line truncated at %u", fname, lineno, x);
|
|
|
|
break;
|
|
|
|
}
|
2021-01-14 01:24:34 +00:00
|
|
|
cp = strchr(cp + 1, '\n');
|
|
|
|
} while (cp != NULL);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (buf[x - 1] == '\n')
|
|
|
|
buf[x - 1] = '\0';
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
if (p != NULL &&
|
2020-05-20 19:34:48 +00:00
|
|
|
!hasOODATE &&
|
2012-06-08 21:57:36 +00:00
|
|
|
!(gn->type & OP_NOMETA_CMP) &&
|
2020-11-20 03:54:37 +00:00
|
|
|
(strcmp(p, cmd) != 0)) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG4(META, "%s: %d: a build command has changed\n%s\nvs\n%s\n",
|
|
|
|
fname, lineno, p, cmd);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (!metaIgnoreCMDs)
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
free(cmd);
|
2020-11-07 19:39:21 +00:00
|
|
|
cmdNode = cmdNode->next;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
} else if (strcmp(buf, "CWD") == 0) {
|
|
|
|
/*
|
|
|
|
* Check if there are extra commands now
|
|
|
|
* that weren't in the meta data file.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
if (!oodate && cmdNode != NULL) {
|
|
|
|
DEBUG2(META, "%s: %d: there are extra build commands now that weren't in the meta data file\n",
|
|
|
|
fname, lineno);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2020-05-20 19:34:48 +00:00
|
|
|
CHECK_VALID_META(p);
|
2012-06-08 21:57:36 +00:00
|
|
|
if (strcmp(p, cwd) != 0) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG4(META, "%s: %d: the current working directory has changed from '%s' to '%s'\n",
|
|
|
|
fname, lineno, p, curdir);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (!Lst_IsEmpty(&missingFiles)) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG2(META, "%s: missing files: %s...\n",
|
2021-01-14 01:24:34 +00:00
|
|
|
fname, (char *)missingFiles.first->datum);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2016-06-05 15:57:18 +00:00
|
|
|
if (!oodate && !have_filemon && filemonMissing) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "%s: missing filemon data\n", fname);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2016-06-05 15:57:18 +00:00
|
|
|
} else {
|
2020-07-11 22:45:05 +00:00
|
|
|
if (writeMeta && (metaMissing || (gn->type & OP_META))) {
|
2021-01-14 01:24:34 +00:00
|
|
|
const char *cp = NULL;
|
2016-06-05 15:57:18 +00:00
|
|
|
|
|
|
|
/* if target is in .CURDIR we do not need a meta file */
|
2021-01-14 01:24:34 +00:00
|
|
|
if (gn->path != NULL && (cp = strrchr(gn->path, '/')) != NULL &&
|
|
|
|
(cp > gn->path)) {
|
2020-11-07 19:39:21 +00:00
|
|
|
if (strncmp(curdir, gn->path, (size_t)(cp - gn->path)) != 0) {
|
2016-06-05 15:57:18 +00:00
|
|
|
cp = NULL; /* not in .CURDIR */
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 03:54:37 +00:00
|
|
|
if (cp == NULL) {
|
2020-11-07 19:39:21 +00:00
|
|
|
DEBUG1(META, "%s: required but missing\n", fname);
|
2021-06-25 18:16:24 +00:00
|
|
|
oodate = true;
|
|
|
|
needOODATE = true; /* assume the worst */
|
2016-06-05 15:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2016-03-11 00:37:02 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_DoneCall(&missingFiles, free);
|
2016-03-11 00:37:02 +00:00
|
|
|
|
2013-01-31 16:44:23 +00:00
|
|
|
if (oodate && needOODATE) {
|
2012-06-08 21:57:36 +00:00
|
|
|
/*
|
2013-01-31 16:44:23 +00:00
|
|
|
* Target uses .OODATE which is empty; or we wouldn't be here.
|
|
|
|
* We have decided it is oodate, so .OODATE needs to be set.
|
|
|
|
* All we can sanely do is set it to .ALLSRC.
|
2012-06-08 21:57:36 +00:00
|
|
|
*/
|
2021-02-11 01:51:11 +00:00
|
|
|
Var_Delete(gn, OODATE);
|
|
|
|
Var_Set(gn, OODATE, GNode_VarAllsrc(gn));
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
2016-06-05 15:57:18 +00:00
|
|
|
|
|
|
|
oodate_out:
|
2021-01-14 01:24:34 +00:00
|
|
|
FStr_Done(&dname);
|
2012-06-08 21:57:36 +00:00
|
|
|
return oodate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* support for compat mode */
|
|
|
|
|
|
|
|
static int childPipe[2];
|
|
|
|
|
|
|
|
void
|
|
|
|
meta_compat_start(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_FILEMON_ONCE
|
|
|
|
/*
|
|
|
|
* We need to re-open filemon for each cmd.
|
|
|
|
*/
|
|
|
|
BuildMon *pbm = &Mybm;
|
2020-07-08 18:32:15 +00:00
|
|
|
|
2012-06-08 21:57:36 +00:00
|
|
|
if (pbm->mfp != NULL && useFilemon) {
|
2020-05-20 19:34:48 +00:00
|
|
|
meta_open_filemon(pbm);
|
2012-06-08 21:57:36 +00:00
|
|
|
} else {
|
2020-05-20 19:34:48 +00:00
|
|
|
pbm->mon_fd = -1;
|
|
|
|
pbm->filemon = NULL;
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (pipe(childPipe) < 0)
|
|
|
|
Punt("Cannot create pipe: %s", strerror(errno));
|
|
|
|
/* Set close-on-exec flag for both */
|
2016-03-11 00:37:02 +00:00
|
|
|
(void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
|
|
|
|
(void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
meta_compat_child(void)
|
|
|
|
{
|
|
|
|
meta_job_child(NULL);
|
2020-11-07 19:39:21 +00:00
|
|
|
if (dup2(childPipe[1], 1) < 0 || dup2(1, 2) < 0)
|
|
|
|
execDie("dup2", "pipe");
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-20 19:34:48 +00:00
|
|
|
meta_compat_parent(pid_t child)
|
2012-06-08 21:57:36 +00:00
|
|
|
{
|
2020-05-20 19:34:48 +00:00
|
|
|
int outfd, metafd, maxfd, nfds;
|
|
|
|
char buf[BUFSIZ+1];
|
|
|
|
fd_set readfds;
|
|
|
|
|
|
|
|
meta_job_parent(NULL, child);
|
2012-06-08 21:57:36 +00:00
|
|
|
close(childPipe[1]); /* child side */
|
2020-05-20 19:34:48 +00:00
|
|
|
outfd = childPipe[0];
|
|
|
|
#ifdef USE_FILEMON
|
2021-01-14 01:24:34 +00:00
|
|
|
metafd = Mybm.filemon != NULL ? filemon_readfd(Mybm.filemon) : -1;
|
2020-05-20 19:34:48 +00:00
|
|
|
#else
|
|
|
|
metafd = -1;
|
|
|
|
#endif
|
|
|
|
maxfd = -1;
|
|
|
|
if (outfd > maxfd)
|
|
|
|
maxfd = outfd;
|
|
|
|
if (metafd > maxfd)
|
|
|
|
maxfd = metafd;
|
|
|
|
|
|
|
|
while (outfd != -1 || metafd != -1) {
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
if (outfd != -1) {
|
|
|
|
FD_SET(outfd, &readfds);
|
|
|
|
}
|
|
|
|
if (metafd != -1) {
|
|
|
|
FD_SET(metafd, &readfds);
|
|
|
|
}
|
|
|
|
nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
|
|
|
|
if (nfds == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
err(1, "select");
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (outfd != -1 && FD_ISSET(outfd, &readfds) != 0) do {
|
2020-05-20 19:34:48 +00:00
|
|
|
/* XXX this is not line-buffered */
|
2020-11-20 03:54:37 +00:00
|
|
|
ssize_t nread = read(outfd, buf, sizeof buf - 1);
|
2020-05-20 19:34:48 +00:00
|
|
|
if (nread == -1)
|
|
|
|
err(1, "read");
|
|
|
|
if (nread == 0) {
|
|
|
|
close(outfd);
|
|
|
|
outfd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fwrite(buf, 1, (size_t)nread, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
buf[nread] = '\0';
|
|
|
|
meta_job_output(NULL, buf, "");
|
2021-06-25 18:16:24 +00:00
|
|
|
} while (/*CONSTCOND*/false);
|
2021-01-14 01:24:34 +00:00
|
|
|
if (metafd != -1 && FD_ISSET(metafd, &readfds) != 0) {
|
2020-05-20 19:34:48 +00:00
|
|
|
if (meta_job_event(NULL) <= 0)
|
|
|
|
metafd = -1;
|
|
|
|
}
|
2012-06-08 21:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
#endif /* USE_META */
|