Home | History | Annotate | Line # | Download | only in make
meta.c revision 1.177
      1  1.177       sjg /*      $NetBSD: meta.c,v 1.177 2021/02/05 19:19:17 sjg Exp $ */
      2   1.15       sjg 
      3    1.1       sjg /*
      4    1.1       sjg  * Implement 'meta' mode.
      5    1.1       sjg  * Adapted from John Birrell's patches to FreeBSD make.
      6    1.1       sjg  * --sjg
      7    1.1       sjg  */
      8    1.1       sjg /*
      9   1.48       sjg  * Copyright (c) 2009-2016, Juniper Networks, Inc.
     10    1.1       sjg  * Portions Copyright (c) 2009, John Birrell.
     11   1.85    rillig  *
     12    1.1       sjg  * Redistribution and use in source and binary forms, with or without
     13   1.85    rillig  * modification, are permitted provided that the following conditions
     14   1.85    rillig  * are met:
     15    1.1       sjg  * 1. Redistributions of source code must retain the above copyright
     16   1.85    rillig  *    notice, this list of conditions and the following disclaimer.
     17    1.1       sjg  * 2. Redistributions in binary form must reproduce the above copyright
     18    1.1       sjg  *    notice, this list of conditions and the following disclaimer in the
     19   1.85    rillig  *    documentation and/or other materials provided with the distribution.
     20   1.85    rillig  *
     21    1.1       sjg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     22    1.1       sjg  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     23    1.1       sjg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     24    1.1       sjg  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     25    1.1       sjg  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26    1.1       sjg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27    1.1       sjg  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28    1.1       sjg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29    1.1       sjg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30    1.1       sjg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31   1.85    rillig  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32    1.1       sjg  */
     33    1.3       sjg #if defined(USE_META)
     34    1.1       sjg 
     35    1.1       sjg #ifdef HAVE_CONFIG_H
     36    1.1       sjg # include "config.h"
     37    1.1       sjg #endif
     38    1.1       sjg #include <sys/stat.h>
     39    1.1       sjg #include <libgen.h>
     40    1.1       sjg #include <errno.h>
     41    1.2       sjg #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
     42    1.1       sjg #include <err.h>
     43    1.2       sjg #endif
     44    1.1       sjg 
     45    1.1       sjg #include "make.h"
     46  1.113    rillig #include "dir.h"
     47    1.1       sjg #include "job.h"
     48    1.1       sjg 
     49   1.74  riastrad #ifdef USE_FILEMON
     50   1.75  riastrad #include "filemon/filemon.h"
     51   1.73      maxv #endif
     52   1.73      maxv 
     53    1.1       sjg static BuildMon Mybm;			/* for compat */
     54  1.155    rillig static StringList metaBailiwick = LST_INIT; /* our scope of control */
     55   1.53  christos static char *metaBailiwickStr;		/* string storage for the list */
     56  1.155    rillig static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
     57   1.53  christos static char *metaIgnorePathsStr;	/* string storage for the list */
     58   1.32       sjg 
     59   1.32       sjg #ifndef MAKE_META_IGNORE_PATHS
     60   1.32       sjg #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
     61   1.32       sjg #endif
     62   1.56       sjg #ifndef MAKE_META_IGNORE_PATTERNS
     63   1.56       sjg #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
     64   1.56       sjg #endif
     65   1.66       sjg #ifndef MAKE_META_IGNORE_FILTER
     66   1.66       sjg #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
     67   1.66       sjg #endif
     68    1.1       sjg 
     69    1.1       sjg Boolean useMeta = FALSE;
     70    1.1       sjg static Boolean useFilemon = FALSE;
     71    1.1       sjg static Boolean writeMeta = FALSE;
     72   1.58       sjg static Boolean metaMissing = FALSE;	/* oodate if missing */
     73   1.58       sjg static Boolean filemonMissing = FALSE;	/* oodate if missing */
     74    1.1       sjg static Boolean metaEnv = FALSE;		/* don't save env unless asked */
     75    1.1       sjg static Boolean metaVerbose = FALSE;
     76    1.1       sjg static Boolean metaIgnoreCMDs = FALSE;	/* ignore CMDs in .meta files */
     77   1.56       sjg static Boolean metaIgnorePatterns = FALSE; /* do we need to do pattern matches */
     78  1.145    rillig static Boolean metaIgnoreFilter = FALSE; /* do we have more complex filtering? */
     79   1.12       sjg static Boolean metaCurdirOk = FALSE;	/* write .meta in .CURDIR Ok? */
     80   1.22       sjg static Boolean metaSilent = FALSE;	/* if we have a .meta be SILENT */
     81    1.1       sjg 
     82    1.1       sjg extern Boolean forceJobs;
     83   1.25       sjg extern char    **environ;
     84    1.1       sjg 
     85  1.154    rillig #define MAKE_META_PREFIX	".MAKE.META.PREFIX"
     86    1.1       sjg 
     87    1.1       sjg #ifndef N2U
     88    1.1       sjg # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
     89    1.1       sjg #endif
     90    1.1       sjg #ifndef ROUNDUP
     91    1.1       sjg # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
     92    1.1       sjg #endif
     93    1.1       sjg 
     94    1.2       sjg #if !defined(HAVE_STRSEP)
     95  1.139    rillig # define strsep(s, d) stresep((s), (d), '\0')
     96    1.2       sjg #endif
     97    1.2       sjg 
     98    1.1       sjg /*
     99   1.73      maxv  * Filemon is a kernel module which snoops certain syscalls.
    100   1.73      maxv  *
    101   1.73      maxv  * C chdir
    102   1.73      maxv  * E exec
    103   1.73      maxv  * F [v]fork
    104   1.73      maxv  * L [sym]link
    105   1.73      maxv  * M rename
    106   1.73      maxv  * R read
    107   1.73      maxv  * W write
    108   1.73      maxv  * S stat
    109   1.73      maxv  *
    110   1.73      maxv  * See meta_oodate below - we mainly care about 'E' and 'R'.
    111   1.73      maxv  *
    112   1.85    rillig  * We can still use meta mode without filemon, but
    113   1.73      maxv  * the benefits are more limited.
    114   1.73      maxv  */
    115   1.73      maxv #ifdef USE_FILEMON
    116   1.73      maxv 
    117   1.73      maxv /*
    118   1.73      maxv  * Open the filemon device.
    119   1.73      maxv  */
    120   1.73      maxv static void
    121   1.74  riastrad meta_open_filemon(BuildMon *pbm)
    122   1.73      maxv {
    123   1.74  riastrad     int dupfd;
    124   1.74  riastrad 
    125   1.74  riastrad     pbm->mon_fd = -1;
    126   1.74  riastrad     pbm->filemon = NULL;
    127  1.168    rillig     if (!useFilemon || pbm->mfp == NULL)
    128   1.73      maxv 	return;
    129   1.73      maxv 
    130   1.74  riastrad     pbm->filemon = filemon_open();
    131   1.74  riastrad     if (pbm->filemon == NULL) {
    132   1.73      maxv 	useFilemon = FALSE;
    133   1.75  riastrad 	warn("Could not open filemon %s", filemon_path());
    134   1.73      maxv 	return;
    135   1.73      maxv     }
    136   1.73      maxv 
    137   1.73      maxv     /*
    138   1.73      maxv      * We use a file outside of '.'
    139   1.73      maxv      * to avoid a FreeBSD kernel bug where unlink invalidates
    140   1.73      maxv      * cwd causing getcwd to do a lot more work.
    141   1.73      maxv      * We only care about the descriptor.
    142   1.73      maxv      */
    143  1.177       sjg     if (!opts.compatMake)
    144  1.177       sjg 	pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
    145  1.177       sjg     else
    146  1.177       sjg 	pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
    147   1.74  riastrad     if ((dupfd = dup(pbm->mon_fd)) == -1) {
    148   1.74  riastrad 	err(1, "Could not dup filemon output!");
    149   1.74  riastrad     }
    150   1.74  riastrad     (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
    151   1.74  riastrad     if (filemon_setfd(pbm->filemon, dupfd) == -1) {
    152   1.73      maxv 	err(1, "Could not set filemon file descriptor!");
    153   1.73      maxv     }
    154   1.73      maxv     /* we don't need these once we exec */
    155   1.73      maxv     (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
    156   1.73      maxv }
    157   1.73      maxv 
    158   1.73      maxv /*
    159   1.73      maxv  * Read the build monitor output file and write records to the target's
    160   1.73      maxv  * metadata file.
    161   1.73      maxv  */
    162   1.73      maxv static int
    163   1.73      maxv filemon_read(FILE *mfp, int fd)
    164   1.73      maxv {
    165   1.73      maxv     char buf[BUFSIZ];
    166   1.73      maxv     int error;
    167   1.73      maxv 
    168   1.73      maxv     /* Check if we're not writing to a meta data file.*/
    169   1.73      maxv     if (mfp == NULL) {
    170   1.73      maxv 	if (fd >= 0)
    171   1.73      maxv 	    close(fd);			/* not interested */
    172   1.73      maxv 	return 0;
    173   1.73      maxv     }
    174   1.73      maxv     /* rewind */
    175   1.82       sjg     if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
    176   1.82       sjg 	error = errno;
    177   1.82       sjg 	warn("Could not rewind filemon");
    178   1.82       sjg 	fprintf(mfp, "\n");
    179   1.82       sjg     } else {
    180  1.132    rillig 	ssize_t n;
    181  1.124    rillig 
    182   1.82       sjg 	error = 0;
    183   1.82       sjg 	fprintf(mfp, "\n-- filemon acquired metadata --\n");
    184   1.73      maxv 
    185  1.138    rillig 	while ((n = read(fd, buf, sizeof buf)) > 0) {
    186  1.124    rillig 	    if ((ssize_t)fwrite(buf, 1, (size_t)n, mfp) < n)
    187   1.82       sjg 		error = EIO;
    188   1.82       sjg 	}
    189   1.73      maxv     }
    190   1.73      maxv     fflush(mfp);
    191   1.73      maxv     if (close(fd) < 0)
    192   1.73      maxv 	error = errno;
    193   1.73      maxv     return error;
    194   1.73      maxv }
    195   1.73      maxv #endif
    196   1.73      maxv 
    197   1.73      maxv /*
    198    1.8       sjg  * when realpath() fails,
    199    1.8       sjg  * we use this, to clean up ./ and ../
    200    1.8       sjg  */
    201    1.8       sjg static void
    202    1.8       sjg eat_dots(char *buf, size_t bufsz, int dots)
    203    1.8       sjg {
    204    1.8       sjg     char *cp;
    205    1.8       sjg     char *cp2;
    206    1.8       sjg     const char *eat;
    207    1.8       sjg     size_t eatlen;
    208    1.8       sjg 
    209    1.8       sjg     switch (dots) {
    210    1.8       sjg     case 1:
    211    1.8       sjg 	eat = "/./";
    212    1.8       sjg 	eatlen = 2;
    213    1.8       sjg 	break;
    214    1.8       sjg     case 2:
    215    1.8       sjg 	eat = "/../";
    216    1.8       sjg 	eatlen = 3;
    217    1.8       sjg 	break;
    218    1.8       sjg     default:
    219    1.8       sjg 	return;
    220    1.8       sjg     }
    221   1.85    rillig 
    222    1.8       sjg     do {
    223    1.8       sjg 	cp = strstr(buf, eat);
    224  1.147    rillig 	if (cp != NULL) {
    225    1.8       sjg 	    cp2 = cp + eatlen;
    226    1.8       sjg 	    if (dots == 2 && cp > buf) {
    227    1.8       sjg 		do {
    228    1.8       sjg 		    cp--;
    229    1.8       sjg 		} while (cp > buf && *cp != '/');
    230    1.8       sjg 	    }
    231    1.8       sjg 	    if (*cp == '/') {
    232  1.124    rillig 		strlcpy(cp, cp2, bufsz - (size_t)(cp - buf));
    233    1.8       sjg 	    } else {
    234    1.8       sjg 		return;			/* can't happen? */
    235    1.8       sjg 	    }
    236    1.8       sjg 	}
    237  1.147    rillig     } while (cp != NULL);
    238    1.8       sjg }
    239    1.8       sjg 
    240    1.1       sjg static char *
    241  1.128    rillig meta_name(char *mname, size_t mnamelen,
    242    1.1       sjg 	  const char *dname,
    243   1.58       sjg 	  const char *tname,
    244   1.58       sjg 	  const char *cwd)
    245    1.1       sjg {
    246    1.1       sjg     char buf[MAXPATHLEN];
    247    1.1       sjg     char *rp;
    248    1.1       sjg     char *cp;
    249    1.1       sjg     char *tp;
    250   1.69       sjg     char *dtp;
    251   1.69       sjg     size_t ldname;
    252    1.8       sjg 
    253    1.1       sjg     /*
    254    1.1       sjg      * Weed out relative paths from the target file name.
    255    1.1       sjg      * We have to be careful though since if target is a
    256    1.1       sjg      * symlink, the result will be unstable.
    257    1.1       sjg      * So we use realpath() just to get the dirname, and leave the
    258    1.1       sjg      * basename as given to us.
    259    1.1       sjg      */
    260  1.166    rillig     if ((cp = strrchr(tname, '/')) != NULL) {
    261  1.166    rillig 	if (cached_realpath(tname, buf) != NULL) {
    262  1.166    rillig 	    if ((rp = strrchr(buf, '/')) != NULL) {
    263    1.1       sjg 		rp++;
    264    1.1       sjg 		cp++;
    265    1.1       sjg 		if (strcmp(cp, rp) != 0)
    266  1.124    rillig 		    strlcpy(rp, cp, sizeof buf - (size_t)(rp - buf));
    267    1.1       sjg 	    }
    268    1.1       sjg 	    tname = buf;
    269    1.8       sjg 	} else {
    270    1.8       sjg 	    /*
    271    1.8       sjg 	     * We likely have a directory which is about to be made.
    272    1.8       sjg 	     * We pretend realpath() succeeded, to have a chance
    273    1.8       sjg 	     * of generating the same meta file name that we will
    274    1.8       sjg 	     * next time through.
    275    1.8       sjg 	     */
    276    1.8       sjg 	    if (tname[0] == '/') {
    277  1.138    rillig 		strlcpy(buf, tname, sizeof buf);
    278    1.8       sjg 	    } else {
    279  1.138    rillig 		snprintf(buf, sizeof buf, "%s/%s", cwd, tname);
    280    1.8       sjg 	    }
    281  1.138    rillig 	    eat_dots(buf, sizeof buf, 1);	/* ./ */
    282  1.138    rillig 	    eat_dots(buf, sizeof buf, 2);	/* ../ */
    283    1.8       sjg 	    tname = buf;
    284    1.1       sjg 	}
    285    1.1       sjg     }
    286    1.1       sjg     /* on some systems dirname may modify its arg */
    287    1.1       sjg     tp = bmake_strdup(tname);
    288   1.69       sjg     dtp = dirname(tp);
    289   1.69       sjg     if (strcmp(dname, dtp) == 0)
    290    1.1       sjg 	snprintf(mname, mnamelen, "%s.meta", tname);
    291    1.1       sjg     else {
    292   1.69       sjg 	ldname = strlen(dname);
    293   1.69       sjg 	if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
    294   1.69       sjg 	    snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
    295   1.69       sjg 	else
    296   1.69       sjg 	    snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
    297    1.1       sjg 
    298    1.1       sjg 	/*
    299    1.1       sjg 	 * Replace path separators in the file name after the
    300    1.1       sjg 	 * current object directory path.
    301    1.1       sjg 	 */
    302    1.1       sjg 	cp = mname + strlen(dname) + 1;
    303    1.1       sjg 
    304    1.1       sjg 	while (*cp != '\0') {
    305    1.1       sjg 	    if (*cp == '/')
    306    1.1       sjg 		*cp = '_';
    307    1.1       sjg 	    cp++;
    308    1.1       sjg 	}
    309    1.1       sjg     }
    310    1.1       sjg     free(tp);
    311   1.84    rillig     return mname;
    312    1.1       sjg }
    313    1.1       sjg 
    314    1.1       sjg /*
    315    1.1       sjg  * Return true if running ${.MAKE}
    316    1.1       sjg  * Bypassed if target is flagged .MAKE
    317    1.1       sjg  */
    318  1.149    rillig static Boolean
    319  1.149    rillig is_submake(const char *cmd, GNode *gn)
    320    1.1       sjg {
    321   1.90    rillig     static const char *p_make = NULL;
    322  1.124    rillig     static size_t p_len;
    323    1.1       sjg     char *mp = NULL;
    324    1.1       sjg     char *cp;
    325    1.1       sjg     char *cp2;
    326  1.149    rillig     Boolean rc = FALSE;
    327    1.1       sjg 
    328  1.141    rillig     if (p_make == NULL) {
    329  1.176    rillig 	p_make = Var_Value(gn, ".MAKE").str;
    330    1.1       sjg 	p_len = strlen(p_make);
    331    1.1       sjg     }
    332    1.1       sjg     cp = strchr(cmd, '$');
    333  1.166    rillig     if (cp != NULL) {
    334  1.117    rillig 	(void)Var_Subst(cmd, gn, VARE_WANTRES, &mp);
    335  1.117    rillig 	/* TODO: handle errors */
    336    1.1       sjg 	cmd = mp;
    337    1.1       sjg     }
    338    1.1       sjg     cp2 = strstr(cmd, p_make);
    339  1.139    rillig     if (cp2 != NULL) {
    340    1.1       sjg 	switch (cp2[p_len]) {
    341    1.1       sjg 	case '\0':
    342    1.1       sjg 	case ' ':
    343    1.1       sjg 	case '\t':
    344    1.1       sjg 	case '\n':
    345  1.149    rillig 	    rc = TRUE;
    346    1.1       sjg 	    break;
    347    1.1       sjg 	}
    348  1.149    rillig 	if (cp2 > cmd && rc) {
    349    1.1       sjg 	    switch (cp2[-1]) {
    350    1.1       sjg 	    case ' ':
    351    1.1       sjg 	    case '\t':
    352    1.1       sjg 	    case '\n':
    353    1.1       sjg 		break;
    354    1.1       sjg 	    default:
    355  1.149    rillig 		rc = FALSE;		/* no match */
    356    1.1       sjg 		break;
    357    1.1       sjg 	    }
    358    1.1       sjg 	}
    359    1.1       sjg     }
    360   1.44  christos     free(mp);
    361   1.84    rillig     return rc;
    362    1.1       sjg }
    363    1.1       sjg 
    364  1.149    rillig static Boolean
    365  1.149    rillig any_is_submake(GNode *gn)
    366  1.149    rillig {
    367  1.149    rillig     StringListNode *ln;
    368  1.149    rillig 
    369  1.153    rillig     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
    370  1.149    rillig 	if (is_submake(ln->datum, gn))
    371  1.149    rillig 	    return TRUE;
    372  1.149    rillig     return FALSE;
    373  1.149    rillig }
    374  1.149    rillig 
    375  1.119    rillig static void
    376  1.150    rillig printCMD(const char *cmd, FILE *fp, GNode *gn)
    377    1.1       sjg {
    378  1.111    rillig     char *cmd_freeIt = NULL;
    379    1.1       sjg 
    380  1.166    rillig     if (strchr(cmd, '$') != NULL) {
    381  1.150    rillig 	(void)Var_Subst(cmd, gn, VARE_WANTRES, &cmd_freeIt);
    382  1.117    rillig 	/* TODO: handle errors */
    383  1.117    rillig 	cmd = cmd_freeIt;
    384    1.1       sjg     }
    385  1.150    rillig     fprintf(fp, "CMD %s\n", cmd);
    386  1.111    rillig     free(cmd_freeIt);
    387    1.1       sjg }
    388    1.1       sjg 
    389  1.126    rillig static void
    390  1.150    rillig printCMDs(GNode *gn, FILE *fp)
    391  1.126    rillig {
    392  1.162    rillig     StringListNode *ln;
    393  1.126    rillig 
    394  1.153    rillig     for (ln = gn->commands.first; ln != NULL; ln = ln->next)
    395  1.150    rillig 	printCMD(ln->datum, fp, gn);
    396  1.126    rillig }
    397  1.126    rillig 
    398    1.1       sjg /*
    399    1.1       sjg  * Certain node types never get a .meta file
    400    1.1       sjg  */
    401    1.1       sjg #define SKIP_META_TYPE(_type) do { \
    402  1.122    rillig     if ((gn->type & __CONCAT(OP_, _type))) { \
    403   1.58       sjg 	if (verbose) { \
    404  1.122    rillig 	    debug_printf("Skipping meta for %s: .%s\n", \
    405  1.122    rillig 		    gn->name, __STRING(_type)); \
    406    1.1       sjg 	} \
    407   1.58       sjg 	return FALSE; \
    408    1.1       sjg     } \
    409  1.169    rillig } while (/*CONSTCOND*/FALSE)
    410    1.1       sjg 
    411   1.58       sjg 
    412   1.58       sjg /*
    413   1.58       sjg  * Do we need/want a .meta file ?
    414   1.58       sjg  */
    415   1.58       sjg static Boolean
    416  1.157    rillig meta_needed(GNode *gn, const char *dname,
    417  1.152    rillig 	    char *objdir_realpath, Boolean verbose)
    418    1.1       sjg {
    419  1.143    rillig     struct cached_stat cst;
    420    1.1       sjg 
    421   1.58       sjg     if (verbose)
    422  1.165    rillig 	verbose = DEBUG(META);
    423   1.85    rillig 
    424    1.1       sjg     /* This may be a phony node which we don't want meta data for... */
    425    1.1       sjg     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
    426    1.1       sjg     /* Or it may be explicitly flagged as .NOMETA */
    427    1.1       sjg     SKIP_META_TYPE(NOMETA);
    428    1.1       sjg     /* Unless it is explicitly flagged as .META */
    429    1.1       sjg     if (!(gn->type & OP_META)) {
    430    1.1       sjg 	SKIP_META_TYPE(PHONY);
    431    1.1       sjg 	SKIP_META_TYPE(SPECIAL);
    432    1.1       sjg 	SKIP_META_TYPE(MAKE);
    433    1.1       sjg     }
    434    1.1       sjg 
    435   1.58       sjg     /* Check if there are no commands to execute. */
    436  1.153    rillig     if (Lst_IsEmpty(&gn->commands)) {
    437   1.58       sjg 	if (verbose)
    438  1.122    rillig 	    debug_printf("Skipping meta for %s: no commands\n", gn->name);
    439   1.58       sjg 	return FALSE;
    440   1.58       sjg     }
    441   1.58       sjg     if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
    442   1.58       sjg 	/* OP_SUBMAKE is a bit too aggressive */
    443  1.149    rillig 	if (any_is_submake(gn)) {
    444  1.121    rillig 	    DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
    445   1.58       sjg 	    return FALSE;
    446   1.58       sjg 	}
    447   1.58       sjg     }
    448   1.58       sjg 
    449    1.1       sjg     /* The object directory may not exist. Check it.. */
    450  1.143    rillig     if (cached_stat(dname, &cst) != 0) {
    451   1.58       sjg 	if (verbose)
    452  1.122    rillig 	    debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
    453   1.58       sjg 	return FALSE;
    454    1.1       sjg     }
    455    1.1       sjg 
    456    1.1       sjg     /* make sure these are canonical */
    457  1.166    rillig     if (cached_realpath(dname, objdir_realpath) != NULL)
    458  1.152    rillig 	dname = objdir_realpath;
    459    1.1       sjg 
    460    1.1       sjg     /* If we aren't in the object directory, don't create a meta file. */
    461   1.12       sjg     if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
    462   1.58       sjg 	if (verbose)
    463  1.122    rillig 	    debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
    464  1.122    rillig 			 gn->name);
    465   1.58       sjg 	return FALSE;
    466   1.58       sjg     }
    467   1.58       sjg     return TRUE;
    468   1.58       sjg }
    469   1.58       sjg 
    470   1.85    rillig 
    471   1.58       sjg static FILE *
    472   1.58       sjg meta_create(BuildMon *pbm, GNode *gn)
    473   1.58       sjg {
    474  1.150    rillig     FILE *fp;
    475   1.58       sjg     char buf[MAXPATHLEN];
    476  1.152    rillig     char objdir_realpath[MAXPATHLEN];
    477   1.58       sjg     char **ptr;
    478  1.161    rillig     FStr dname;
    479   1.58       sjg     const char *tname;
    480   1.58       sjg     char *fname;
    481   1.58       sjg     const char *cp;
    482   1.58       sjg 
    483  1.150    rillig     fp = NULL;
    484   1.58       sjg 
    485  1.176    rillig     dname = Var_Value(gn, ".OBJDIR");
    486  1.135    rillig     tname = GNode_VarTarget(gn);
    487   1.58       sjg 
    488  1.152    rillig     /* if this succeeds objdir_realpath is realpath of dname */
    489  1.161    rillig     if (!meta_needed(gn, dname.str, objdir_realpath, TRUE))
    490    1.1       sjg 	goto out;
    491  1.161    rillig     dname.str = objdir_realpath;
    492    1.1       sjg 
    493    1.1       sjg     if (metaVerbose) {
    494    1.1       sjg 	char *mp;
    495    1.1       sjg 
    496    1.1       sjg 	/* Describe the target we are building */
    497  1.117    rillig 	(void)Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_WANTRES, &mp);
    498  1.117    rillig 	/* TODO: handle errors */
    499  1.146    rillig 	if (mp[0] != '\0')
    500    1.1       sjg 	    fprintf(stdout, "%s\n", mp);
    501    1.1       sjg 	free(mp);
    502    1.1       sjg     }
    503    1.1       sjg     /* Get the basename of the target */
    504  1.159    rillig     cp = str_basename(tname);
    505    1.1       sjg 
    506    1.1       sjg     fflush(stdout);
    507    1.1       sjg 
    508    1.1       sjg     if (!writeMeta)
    509    1.1       sjg 	/* Don't create meta data. */
    510    1.1       sjg 	goto out;
    511    1.1       sjg 
    512  1.138    rillig     fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
    513  1.161    rillig 		      dname.str, tname, objdir_realpath);
    514    1.1       sjg 
    515    1.8       sjg #ifdef DEBUG_META_MODE
    516  1.121    rillig     DEBUG1(META, "meta_create: %s\n", fname);
    517    1.8       sjg #endif
    518    1.8       sjg 
    519  1.150    rillig     if ((fp = fopen(fname, "w")) == NULL)
    520    1.1       sjg 	err(1, "Could not open meta file '%s'", fname);
    521    1.1       sjg 
    522  1.150    rillig     fprintf(fp, "# Meta data file %s\n", fname);
    523    1.1       sjg 
    524  1.150    rillig     printCMDs(gn, fp);
    525    1.1       sjg 
    526  1.150    rillig     fprintf(fp, "CWD %s\n", getcwd(buf, sizeof buf));
    527  1.150    rillig     fprintf(fp, "TARGET %s\n", tname);
    528  1.135    rillig     cp = GNode_VarOodate(gn);
    529  1.168    rillig     if (cp != NULL && *cp != '\0') {
    530  1.150    rillig 	fprintf(fp, "OODATE %s\n", cp);
    531   1.76       sjg     }
    532    1.1       sjg     if (metaEnv) {
    533    1.1       sjg 	for (ptr = environ; *ptr != NULL; ptr++)
    534  1.150    rillig 	    fprintf(fp, "ENV %s\n", *ptr);
    535    1.1       sjg     }
    536    1.1       sjg 
    537  1.150    rillig     fprintf(fp, "-- command output --\n");
    538  1.150    rillig     fflush(fp);
    539    1.1       sjg 
    540  1.173    rillig     Global_Append(".MAKE.META.FILES", fname);
    541  1.173    rillig     Global_Append(".MAKE.META.CREATED", fname);
    542   1.22       sjg 
    543   1.22       sjg     gn->type |= OP_META;		/* in case anyone wants to know */
    544   1.22       sjg     if (metaSilent) {
    545   1.22       sjg 	    gn->type |= OP_SILENT;
    546   1.22       sjg     }
    547    1.1       sjg  out:
    548  1.161    rillig     FStr_Done(&dname);
    549    1.1       sjg 
    550  1.150    rillig     return fp;
    551    1.1       sjg }
    552    1.1       sjg 
    553   1.12       sjg static Boolean
    554   1.12       sjg boolValue(char *s)
    555   1.12       sjg {
    556   1.12       sjg     switch(*s) {
    557   1.12       sjg     case '0':
    558   1.12       sjg     case 'N':
    559   1.12       sjg     case 'n':
    560   1.12       sjg     case 'F':
    561   1.12       sjg     case 'f':
    562   1.12       sjg 	return FALSE;
    563   1.12       sjg     }
    564   1.12       sjg     return TRUE;
    565   1.12       sjg }
    566    1.1       sjg 
    567   1.27       sjg /*
    568   1.27       sjg  * Initialization we need before reading makefiles.
    569   1.27       sjg  */
    570   1.27       sjg void
    571   1.30       sjg meta_init(void)
    572   1.27       sjg {
    573   1.73      maxv #ifdef USE_FILEMON
    574   1.73      maxv 	/* this allows makefiles to test if we have filemon support */
    575  1.172    rillig 	Global_Set(".MAKE.PATH_FILEMON", filemon_path());
    576   1.73      maxv #endif
    577   1.27       sjg }
    578   1.27       sjg 
    579   1.27       sjg 
    580   1.58       sjg #define get_mode_bf(bf, token) \
    581  1.166    rillig     if ((cp = strstr(make_mode, token)) != NULL) \
    582  1.138    rillig 	bf = boolValue(cp + sizeof (token) - 1)
    583   1.58       sjg 
    584   1.27       sjg /*
    585   1.27       sjg  * Initialization we need after reading makefiles.
    586   1.27       sjg  */
    587    1.1       sjg void
    588   1.27       sjg meta_mode_init(const char *make_mode)
    589    1.1       sjg {
    590  1.147    rillig     static Boolean once = FALSE;
    591   1.12       sjg     char *cp;
    592  1.161    rillig     FStr value;
    593    1.1       sjg 
    594    1.1       sjg     useMeta = TRUE;
    595    1.1       sjg     useFilemon = TRUE;
    596    1.1       sjg     writeMeta = TRUE;
    597    1.1       sjg 
    598  1.147    rillig     if (make_mode != NULL) {
    599  1.166    rillig 	if (strstr(make_mode, "env") != NULL)
    600    1.1       sjg 	    metaEnv = TRUE;
    601  1.166    rillig 	if (strstr(make_mode, "verb") != NULL)
    602    1.1       sjg 	    metaVerbose = TRUE;
    603  1.166    rillig 	if (strstr(make_mode, "read") != NULL)
    604    1.1       sjg 	    writeMeta = FALSE;
    605  1.166    rillig 	if (strstr(make_mode, "nofilemon") != NULL)
    606    1.1       sjg 	    useFilemon = FALSE;
    607  1.166    rillig 	if (strstr(make_mode, "ignore-cmd") != NULL)
    608    1.1       sjg 	    metaIgnoreCMDs = TRUE;
    609   1.58       sjg 	if (useFilemon)
    610   1.58       sjg 	    get_mode_bf(filemonMissing, "missing-filemon=");
    611   1.58       sjg 	get_mode_bf(metaCurdirOk, "curdirok=");
    612   1.58       sjg 	get_mode_bf(metaMissing, "missing-meta=");
    613   1.58       sjg 	get_mode_bf(metaSilent, "silent=");
    614    1.1       sjg     }
    615  1.176    rillig     if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
    616    1.1       sjg 	/*
    617    1.1       sjg 	 * The default value for MAKE_META_PREFIX
    618    1.1       sjg 	 * prints the absolute path of the target.
    619    1.1       sjg 	 * This works be cause :H will generate '.' if there is no /
    620    1.1       sjg 	 * and :tA will resolve that to cwd.
    621    1.1       sjg 	 */
    622  1.172    rillig 	Global_Set(MAKE_META_PREFIX,
    623  1.171    rillig 	    "Building ${.TARGET:H:tA}/${.TARGET:T}");
    624    1.1       sjg     }
    625    1.1       sjg     if (once)
    626    1.1       sjg 	return;
    627  1.147    rillig     once = TRUE;
    628  1.138    rillig     memset(&Mybm, 0, sizeof Mybm);
    629   1.17       sjg     /*
    630   1.17       sjg      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
    631   1.17       sjg      */
    632  1.117    rillig     (void)Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
    633  1.175    rillig 		    SCOPE_GLOBAL, VARE_WANTRES, &metaBailiwickStr);
    634  1.117    rillig     /* TODO: handle errors */
    635  1.155    rillig     str2Lst_Append(&metaBailiwick, metaBailiwickStr);
    636   1.32       sjg     /*
    637   1.32       sjg      * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
    638   1.32       sjg      */
    639  1.173    rillig     Global_Append(MAKE_META_IGNORE_PATHS,
    640  1.171    rillig 	       "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}");
    641  1.117    rillig     (void)Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
    642  1.175    rillig 		    SCOPE_GLOBAL, VARE_WANTRES, &metaIgnorePathsStr);
    643  1.117    rillig     /* TODO: handle errors */
    644  1.155    rillig     str2Lst_Append(&metaIgnorePaths, metaIgnorePathsStr);
    645   1.56       sjg 
    646   1.56       sjg     /*
    647   1.56       sjg      * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
    648   1.56       sjg      */
    649  1.176    rillig     value = Var_Value(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
    650  1.161    rillig     if (value.str != NULL) {
    651   1.56       sjg 	metaIgnorePatterns = TRUE;
    652  1.161    rillig 	FStr_Done(&value);
    653   1.56       sjg     }
    654  1.176    rillig     value = Var_Value(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
    655  1.161    rillig     if (value.str != NULL) {
    656   1.66       sjg 	metaIgnoreFilter = TRUE;
    657  1.161    rillig 	FStr_Done(&value);
    658   1.66       sjg     }
    659    1.1       sjg }
    660    1.1       sjg 
    661    1.1       sjg /*
    662    1.1       sjg  * In each case below we allow for job==NULL
    663    1.1       sjg  */
    664    1.1       sjg void
    665    1.1       sjg meta_job_start(Job *job, GNode *gn)
    666    1.1       sjg {
    667    1.1       sjg     BuildMon *pbm;
    668    1.1       sjg 
    669    1.1       sjg     if (job != NULL) {
    670    1.1       sjg 	pbm = &job->bm;
    671    1.1       sjg     } else {
    672    1.1       sjg 	pbm = &Mybm;
    673    1.1       sjg     }
    674    1.1       sjg     pbm->mfp = meta_create(pbm, gn);
    675   1.73      maxv #ifdef USE_FILEMON_ONCE
    676   1.73      maxv     /* compat mode we open the filemon dev once per command */
    677   1.73      maxv     if (job == NULL)
    678   1.73      maxv 	return;
    679   1.73      maxv #endif
    680   1.73      maxv #ifdef USE_FILEMON
    681   1.73      maxv     if (pbm->mfp != NULL && useFilemon) {
    682   1.74  riastrad 	meta_open_filemon(pbm);
    683   1.73      maxv     } else {
    684   1.74  riastrad 	pbm->mon_fd = -1;
    685   1.74  riastrad 	pbm->filemon = NULL;
    686   1.73      maxv     }
    687   1.73      maxv #endif
    688    1.1       sjg }
    689    1.1       sjg 
    690    1.1       sjg /*
    691    1.1       sjg  * The child calls this before doing anything.
    692    1.1       sjg  * It does not disturb our state.
    693    1.1       sjg  */
    694    1.1       sjg void
    695    1.1       sjg meta_job_child(Job *job)
    696    1.1       sjg {
    697   1.73      maxv #ifdef USE_FILEMON
    698   1.73      maxv     BuildMon *pbm;
    699   1.73      maxv 
    700   1.73      maxv     if (job != NULL) {
    701   1.73      maxv 	pbm = &job->bm;
    702   1.73      maxv     } else {
    703   1.73      maxv 	pbm = &Mybm;
    704   1.73      maxv     }
    705   1.73      maxv     if (pbm->mfp != NULL) {
    706   1.73      maxv 	close(fileno(pbm->mfp));
    707  1.168    rillig 	if (useFilemon && pbm->filemon != NULL) {
    708   1.73      maxv 	    pid_t pid;
    709   1.73      maxv 
    710   1.73      maxv 	    pid = getpid();
    711   1.74  riastrad 	    if (filemon_setpid_child(pbm->filemon, pid) == -1) {
    712   1.73      maxv 		err(1, "Could not set filemon pid!");
    713   1.73      maxv 	    }
    714   1.73      maxv 	}
    715   1.73      maxv     }
    716   1.73      maxv #endif
    717    1.1       sjg }
    718    1.1       sjg 
    719    1.1       sjg void
    720   1.74  riastrad meta_job_parent(Job *job, pid_t pid)
    721   1.74  riastrad {
    722   1.78       sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
    723   1.74  riastrad     BuildMon *pbm;
    724   1.74  riastrad 
    725   1.74  riastrad     if (job != NULL) {
    726   1.74  riastrad 	pbm = &job->bm;
    727   1.74  riastrad     } else {
    728   1.74  riastrad 	pbm = &Mybm;
    729   1.74  riastrad     }
    730  1.168    rillig     if (useFilemon && pbm->filemon != NULL) {
    731   1.74  riastrad 	filemon_setpid_parent(pbm->filemon, pid);
    732   1.74  riastrad     }
    733   1.74  riastrad #endif
    734   1.74  riastrad }
    735   1.74  riastrad 
    736   1.74  riastrad int
    737   1.74  riastrad meta_job_fd(Job *job)
    738   1.74  riastrad {
    739   1.78       sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
    740   1.74  riastrad     BuildMon *pbm;
    741   1.74  riastrad 
    742   1.74  riastrad     if (job != NULL) {
    743   1.74  riastrad 	pbm = &job->bm;
    744   1.74  riastrad     } else {
    745   1.74  riastrad 	pbm = &Mybm;
    746   1.74  riastrad     }
    747  1.168    rillig     if (useFilemon && pbm->filemon != NULL) {
    748   1.74  riastrad 	return filemon_readfd(pbm->filemon);
    749   1.74  riastrad     }
    750   1.74  riastrad #endif
    751   1.74  riastrad     return -1;
    752   1.74  riastrad }
    753   1.74  riastrad 
    754   1.74  riastrad int
    755   1.74  riastrad meta_job_event(Job *job)
    756   1.74  riastrad {
    757   1.78       sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
    758   1.74  riastrad     BuildMon *pbm;
    759   1.74  riastrad 
    760   1.74  riastrad     if (job != NULL) {
    761   1.74  riastrad 	pbm = &job->bm;
    762   1.74  riastrad     } else {
    763   1.74  riastrad 	pbm = &Mybm;
    764   1.74  riastrad     }
    765  1.168    rillig     if (useFilemon && pbm->filemon != NULL) {
    766   1.74  riastrad 	return filemon_process(pbm->filemon);
    767   1.74  riastrad     }
    768   1.74  riastrad #endif
    769   1.74  riastrad     return 0;
    770   1.74  riastrad }
    771   1.74  riastrad 
    772   1.74  riastrad void
    773  1.158    rillig meta_job_error(Job *job, GNode *gn, Boolean ignerr, int status)
    774    1.1       sjg {
    775    1.1       sjg     char cwd[MAXPATHLEN];
    776    1.1       sjg     BuildMon *pbm;
    777    1.1       sjg 
    778    1.1       sjg     if (job != NULL) {
    779    1.1       sjg 	pbm = &job->bm;
    780  1.141    rillig 	if (gn == NULL)
    781    1.1       sjg 	    gn = job->node;
    782   1.52  christos     } else {
    783    1.1       sjg 	pbm = &Mybm;
    784    1.1       sjg     }
    785    1.1       sjg     if (pbm->mfp != NULL) {
    786   1.68       sjg 	fprintf(pbm->mfp, "\n*** Error code %d%s\n",
    787  1.158    rillig 		status, ignerr ? "(ignored)" : "");
    788    1.1       sjg     }
    789  1.171    rillig     if (gn != NULL)
    790  1.172    rillig 	Global_Set(".ERROR_TARGET", GNode_Path(gn));
    791  1.138    rillig     getcwd(cwd, sizeof cwd);
    792  1.172    rillig     Global_Set(".ERROR_CWD", cwd);
    793  1.139    rillig     if (pbm->meta_fname[0] != '\0') {
    794  1.172    rillig 	Global_Set(".ERROR_META_FILE", pbm->meta_fname);
    795    1.1       sjg     }
    796   1.16       sjg     meta_job_finish(job);
    797    1.1       sjg }
    798    1.1       sjg 
    799    1.1       sjg void
    800    1.1       sjg meta_job_output(Job *job, char *cp, const char *nl)
    801    1.1       sjg {
    802    1.1       sjg     BuildMon *pbm;
    803   1.85    rillig 
    804    1.1       sjg     if (job != NULL) {
    805    1.1       sjg 	pbm = &job->bm;
    806    1.1       sjg     } else {
    807    1.1       sjg 	pbm = &Mybm;
    808    1.1       sjg     }
    809    1.1       sjg     if (pbm->mfp != NULL) {
    810    1.1       sjg 	if (metaVerbose) {
    811    1.1       sjg 	    static char *meta_prefix = NULL;
    812  1.124    rillig 	    static size_t meta_prefix_len;
    813    1.1       sjg 
    814  1.141    rillig 	    if (meta_prefix == NULL) {
    815    1.1       sjg 		char *cp2;
    816    1.1       sjg 
    817  1.117    rillig 		(void)Var_Subst("${" MAKE_META_PREFIX "}",
    818  1.175    rillig 				SCOPE_GLOBAL, VARE_WANTRES, &meta_prefix);
    819  1.117    rillig 		/* TODO: handle errors */
    820  1.166    rillig 		if ((cp2 = strchr(meta_prefix, '$')) != NULL)
    821  1.124    rillig 		    meta_prefix_len = (size_t)(cp2 - meta_prefix);
    822    1.1       sjg 		else
    823    1.1       sjg 		    meta_prefix_len = strlen(meta_prefix);
    824    1.1       sjg 	    }
    825    1.1       sjg 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
    826  1.164    rillig 		cp = strchr(cp + 1, '\n');
    827  1.164    rillig 		if (cp == NULL)
    828    1.1       sjg 		    return;
    829  1.164    rillig 		cp++;
    830    1.1       sjg 	    }
    831    1.1       sjg 	}
    832    1.1       sjg 	fprintf(pbm->mfp, "%s%s", cp, nl);
    833    1.1       sjg     }
    834    1.1       sjg }
    835    1.1       sjg 
    836   1.57       sjg int
    837    1.1       sjg meta_cmd_finish(void *pbmp)
    838    1.1       sjg {
    839   1.57       sjg     int error = 0;
    840    1.1       sjg     BuildMon *pbm = pbmp;
    841   1.73      maxv #ifdef USE_FILEMON
    842   1.73      maxv     int x;
    843   1.73      maxv #endif
    844    1.1       sjg 
    845  1.139    rillig     if (pbm == NULL)
    846    1.1       sjg 	pbm = &Mybm;
    847    1.1       sjg 
    848   1.73      maxv #ifdef USE_FILEMON
    849  1.166    rillig     if (pbm->filemon != NULL) {
    850   1.74  riastrad 	while (filemon_process(pbm->filemon) > 0)
    851   1.74  riastrad 	    continue;
    852   1.74  riastrad 	if (filemon_close(pbm->filemon) == -1)
    853   1.73      maxv 	    error = errno;
    854   1.73      maxv 	x = filemon_read(pbm->mfp, pbm->mon_fd);
    855   1.73      maxv 	if (error == 0 && x != 0)
    856   1.73      maxv 	    error = x;
    857   1.74  riastrad 	pbm->mon_fd = -1;
    858   1.74  riastrad 	pbm->filemon = NULL;
    859  1.140    rillig 	return error;
    860  1.140    rillig     }
    861   1.73      maxv #endif
    862  1.140    rillig 
    863  1.140    rillig     fprintf(pbm->mfp, "\n");	/* ensure end with newline */
    864   1.57       sjg     return error;
    865    1.1       sjg }
    866    1.1       sjg 
    867   1.57       sjg int
    868    1.1       sjg meta_job_finish(Job *job)
    869    1.1       sjg {
    870    1.1       sjg     BuildMon *pbm;
    871   1.57       sjg     int error = 0;
    872   1.57       sjg     int x;
    873    1.1       sjg 
    874    1.1       sjg     if (job != NULL) {
    875    1.1       sjg 	pbm = &job->bm;
    876    1.1       sjg     } else {
    877    1.1       sjg 	pbm = &Mybm;
    878    1.1       sjg     }
    879    1.1       sjg     if (pbm->mfp != NULL) {
    880   1.57       sjg 	error = meta_cmd_finish(pbm);
    881   1.57       sjg 	x = fclose(pbm->mfp);
    882   1.57       sjg 	if (error == 0 && x != 0)
    883   1.57       sjg 	    error = errno;
    884    1.1       sjg 	pbm->mfp = NULL;
    885    1.6       sjg 	pbm->meta_fname[0] = '\0';
    886    1.1       sjg     }
    887   1.57       sjg     return error;
    888    1.1       sjg }
    889    1.1       sjg 
    890   1.53  christos void
    891   1.53  christos meta_finish(void)
    892   1.53  christos {
    893  1.155    rillig     Lst_Done(&metaBailiwick);
    894   1.53  christos     free(metaBailiwickStr);
    895  1.155    rillig     Lst_Done(&metaIgnorePaths);
    896   1.53  christos     free(metaIgnorePathsStr);
    897   1.53  christos }
    898   1.53  christos 
    899    1.1       sjg /*
    900    1.1       sjg  * Fetch a full line from fp - growing bufp if needed
    901    1.1       sjg  * Return length in bufp.
    902    1.1       sjg  */
    903   1.85    rillig static int
    904    1.1       sjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
    905    1.1       sjg {
    906    1.1       sjg     char *buf = *bufp;
    907    1.1       sjg     size_t bufsz = *szp;
    908    1.1       sjg     struct stat fs;
    909    1.1       sjg     int x;
    910    1.1       sjg 
    911  1.124    rillig     if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
    912    1.1       sjg     check_newline:
    913  1.124    rillig 	x = o + (int)strlen(&buf[o]);
    914    1.1       sjg 	if (buf[x - 1] == '\n')
    915    1.1       sjg 	    return x;
    916    1.1       sjg 	/*
    917    1.1       sjg 	 * We need to grow the buffer.
    918    1.1       sjg 	 * The meta file can give us a clue.
    919    1.1       sjg 	 */
    920    1.1       sjg 	if (fstat(fileno(fp), &fs) == 0) {
    921    1.1       sjg 	    size_t newsz;
    922    1.1       sjg 	    char *p;
    923    1.1       sjg 
    924  1.124    rillig 	    newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
    925    1.1       sjg 	    if (newsz <= bufsz)
    926  1.124    rillig 		newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
    927   1.68       sjg 	    if (newsz <= bufsz)
    928   1.68       sjg 		return x;		/* truncated */
    929  1.160    rillig 	    DEBUG2(META, "growing buffer %u -> %u\n",
    930  1.160    rillig 		   (unsigned)bufsz, (unsigned)newsz);
    931    1.1       sjg 	    p = bmake_realloc(buf, newsz);
    932  1.148    rillig 	    *bufp = buf = p;
    933  1.148    rillig 	    *szp = bufsz = newsz;
    934  1.148    rillig 	    /* fetch the rest */
    935  1.148    rillig 	    if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
    936  1.148    rillig 		return x;		/* truncated! */
    937  1.148    rillig 	    goto check_newline;
    938    1.1       sjg 	}
    939    1.1       sjg     }
    940    1.1       sjg     return 0;
    941    1.1       sjg }
    942    1.1       sjg 
    943  1.149    rillig static Boolean
    944  1.149    rillig prefix_match(const char *prefix, const char *path)
    945   1.17       sjg {
    946   1.17       sjg     size_t n = strlen(prefix);
    947   1.17       sjg 
    948   1.84    rillig     return strncmp(path, prefix, n) == 0;
    949   1.17       sjg }
    950   1.17       sjg 
    951  1.149    rillig static Boolean
    952  1.149    rillig has_any_prefix(const char *path, StringList *prefixes)
    953  1.149    rillig {
    954  1.149    rillig     StringListNode *ln;
    955  1.149    rillig 
    956  1.149    rillig     for (ln = prefixes->first; ln != NULL; ln = ln->next)
    957  1.149    rillig 	if (prefix_match(ln->datum, path))
    958  1.149    rillig 	    return TRUE;
    959  1.149    rillig     return FALSE;
    960  1.149    rillig }
    961  1.149    rillig 
    962  1.108    rillig /* See if the path equals prefix or starts with "prefix/". */
    963  1.108    rillig static Boolean
    964  1.129    rillig path_starts_with(const char *path, const char *prefix)
    965   1.64       sjg {
    966   1.64       sjg     size_t n = strlen(prefix);
    967   1.64       sjg 
    968  1.108    rillig     if (strncmp(path, prefix, n) != 0)
    969  1.114    rillig 	return FALSE;
    970  1.108    rillig     return path[n] == '\0' || path[n] == '/';
    971   1.64       sjg }
    972   1.64       sjg 
    973  1.168    rillig static Boolean
    974   1.67       sjg meta_ignore(GNode *gn, const char *p)
    975   1.67       sjg {
    976   1.67       sjg     char fname[MAXPATHLEN];
    977   1.67       sjg 
    978   1.67       sjg     if (p == NULL)
    979   1.67       sjg 	return TRUE;
    980   1.67       sjg 
    981   1.67       sjg     if (*p == '/') {
    982   1.67       sjg 	cached_realpath(p, fname); /* clean it up */
    983  1.155    rillig 	if (has_any_prefix(fname, &metaIgnorePaths)) {
    984   1.67       sjg #ifdef DEBUG_META_MODE
    985  1.121    rillig 	    DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
    986   1.67       sjg #endif
    987   1.67       sjg 	    return TRUE;
    988   1.67       sjg 	}
    989   1.67       sjg     }
    990   1.67       sjg 
    991   1.67       sjg     if (metaIgnorePatterns) {
    992   1.97    rillig 	const char *expr;
    993   1.97    rillig 	char *pm;
    994   1.92    rillig 
    995  1.176    rillig 	/*
    996  1.176    rillig 	 * XXX: This variable is set on a target GNode but is not one of
    997  1.176    rillig 	 * the usual local variables.  It should be deleted afterwards.
    998  1.176    rillig 	 * Ideally it would not be created in the first place, just like
    999  1.176    rillig 	 * in a .for loop.
   1000  1.176    rillig 	 */
   1001  1.176    rillig 	Var_Set(gn, ".p.", p);
   1002   1.92    rillig 	expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
   1003  1.117    rillig 	(void)Var_Subst(expr, gn, VARE_WANTRES, &pm);
   1004  1.117    rillig 	/* TODO: handle errors */
   1005  1.146    rillig 	if (pm[0] != '\0') {
   1006   1.67       sjg #ifdef DEBUG_META_MODE
   1007  1.121    rillig 	    DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
   1008   1.67       sjg #endif
   1009   1.67       sjg 	    free(pm);
   1010   1.67       sjg 	    return TRUE;
   1011   1.67       sjg 	}
   1012   1.67       sjg 	free(pm);
   1013   1.67       sjg     }
   1014   1.67       sjg 
   1015   1.67       sjg     if (metaIgnoreFilter) {
   1016   1.67       sjg 	char *fm;
   1017   1.67       sjg 
   1018   1.67       sjg 	/* skip if filter result is empty */
   1019  1.138    rillig 	snprintf(fname, sizeof fname,
   1020   1.67       sjg 		 "${%s:L:${%s:ts:}}",
   1021   1.67       sjg 		 p, MAKE_META_IGNORE_FILTER);
   1022  1.117    rillig 	(void)Var_Subst(fname, gn, VARE_WANTRES, &fm);
   1023  1.117    rillig 	/* TODO: handle errors */
   1024   1.67       sjg 	if (*fm == '\0') {
   1025   1.67       sjg #ifdef DEBUG_META_MODE
   1026  1.121    rillig 	    DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
   1027   1.67       sjg #endif
   1028   1.67       sjg 	    free(fm);
   1029   1.67       sjg 	    return TRUE;
   1030   1.67       sjg 	}
   1031   1.67       sjg 	free(fm);
   1032   1.67       sjg     }
   1033   1.67       sjg     return FALSE;
   1034   1.67       sjg }
   1035   1.67       sjg 
   1036    1.1       sjg /*
   1037    1.1       sjg  * When running with 'meta' functionality, a target can be out-of-date
   1038   1.34       snj  * if any of the references in its meta data file is more recent.
   1039    1.5       sjg  * We have to track the latestdir on a per-process basis.
   1040    1.1       sjg  */
   1041   1.38       sjg #define LCWD_VNAME_FMT ".meta.%d.lcwd"
   1042    1.5       sjg #define LDIR_VNAME_FMT ".meta.%d.ldir"
   1043    1.5       sjg 
   1044   1.20       sjg /*
   1045   1.20       sjg  * It is possible that a .meta file is corrupted,
   1046   1.20       sjg  * if we detect this we want to reproduce it.
   1047   1.20       sjg  * Setting oodate TRUE will have that effect.
   1048   1.20       sjg  */
   1049  1.168    rillig #define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
   1050   1.20       sjg     warnx("%s: %d: malformed", fname, lineno); \
   1051   1.20       sjg     oodate = TRUE; \
   1052   1.20       sjg     continue; \
   1053   1.20       sjg     }
   1054   1.20       sjg 
   1055   1.33       sjg #define DEQUOTE(p) if (*p == '\'') {	\
   1056   1.33       sjg     char *ep; \
   1057   1.33       sjg     p++; \
   1058  1.166    rillig     if ((ep = strchr(p, '\'')) != NULL) \
   1059   1.33       sjg 	*ep = '\0'; \
   1060   1.33       sjg     }
   1061   1.33       sjg 
   1062  1.130    rillig static void
   1063  1.130    rillig append_if_new(StringList *list, const char *str)
   1064  1.130    rillig {
   1065  1.130    rillig     StringListNode *ln;
   1066  1.130    rillig 
   1067  1.130    rillig     for (ln = list->first; ln != NULL; ln = ln->next)
   1068  1.132    rillig 	if (strcmp(ln->datum, str) == 0)
   1069  1.130    rillig 	    return;
   1070  1.130    rillig     Lst_Append(list, bmake_strdup(str));
   1071  1.130    rillig }
   1072  1.130    rillig 
   1073    1.1       sjg Boolean
   1074    1.1       sjg meta_oodate(GNode *gn, Boolean oodate)
   1075    1.1       sjg {
   1076    1.5       sjg     static char *tmpdir = NULL;
   1077   1.11       sjg     static char cwd[MAXPATHLEN];
   1078   1.38       sjg     char lcwd_vname[64];
   1079    1.5       sjg     char ldir_vname[64];
   1080   1.38       sjg     char lcwd[MAXPATHLEN];
   1081    1.1       sjg     char latestdir[MAXPATHLEN];
   1082    1.1       sjg     char fname[MAXPATHLEN];
   1083    1.1       sjg     char fname1[MAXPATHLEN];
   1084    1.5       sjg     char fname2[MAXPATHLEN];
   1085   1.38       sjg     char fname3[MAXPATHLEN];
   1086  1.161    rillig     FStr dname;
   1087   1.58       sjg     const char *tname;
   1088    1.1       sjg     char *p;
   1089   1.33       sjg     char *link_src;
   1090   1.33       sjg     char *move_target;
   1091   1.11       sjg     static size_t cwdlen = 0;
   1092    1.7       sjg     static size_t tmplen = 0;
   1093    1.1       sjg     FILE *fp;
   1094   1.26       sjg     Boolean needOODATE = FALSE;
   1095  1.156    rillig     StringList missingFiles;
   1096  1.141    rillig     Boolean have_filemon = FALSE;
   1097   1.58       sjg 
   1098    1.4       sjg     if (oodate)
   1099    1.4       sjg 	return oodate;		/* we're done */
   1100    1.4       sjg 
   1101  1.176    rillig     dname = Var_Value(gn, ".OBJDIR");
   1102  1.135    rillig     tname = GNode_VarTarget(gn);
   1103   1.58       sjg 
   1104   1.58       sjg     /* if this succeeds fname3 is realpath of dname */
   1105  1.161    rillig     if (!meta_needed(gn, dname.str, fname3, FALSE))
   1106   1.58       sjg 	goto oodate_out;
   1107  1.161    rillig     dname.str = fname3;
   1108   1.58       sjg 
   1109  1.156    rillig     Lst_Init(&missingFiles);
   1110   1.17       sjg 
   1111    1.1       sjg     /*
   1112    1.1       sjg      * We need to check if the target is out-of-date. This includes
   1113    1.1       sjg      * checking if the expanded command has changed. This in turn
   1114    1.1       sjg      * requires that all variables are set in the same way that they
   1115    1.1       sjg      * would be if the target needs to be re-built.
   1116    1.1       sjg      */
   1117    1.1       sjg     Make_DoAllVar(gn);
   1118    1.1       sjg 
   1119  1.161    rillig     meta_name(fname, sizeof fname, dname.str, tname, dname.str);
   1120    1.1       sjg 
   1121    1.8       sjg #ifdef DEBUG_META_MODE
   1122  1.121    rillig     DEBUG1(META, "meta_oodate: %s\n", fname);
   1123    1.8       sjg #endif
   1124    1.8       sjg 
   1125    1.1       sjg     if ((fp = fopen(fname, "r")) != NULL) {
   1126    1.1       sjg 	static char *buf = NULL;
   1127    1.1       sjg 	static size_t bufsz;
   1128    1.1       sjg 	int lineno = 0;
   1129    1.5       sjg 	int lastpid = 0;
   1130    1.5       sjg 	int pid;
   1131    1.1       sjg 	int x;
   1132  1.115    rillig 	StringListNode *cmdNode;
   1133  1.143    rillig 	struct cached_stat cst;
   1134    1.1       sjg 
   1135  1.141    rillig 	if (buf == NULL) {
   1136    1.1       sjg 	    bufsz = 8 * BUFSIZ;
   1137    1.1       sjg 	    buf = bmake_malloc(bufsz);
   1138    1.1       sjg 	}
   1139    1.5       sjg 
   1140  1.141    rillig 	if (cwdlen == 0) {
   1141  1.138    rillig 	    if (getcwd(cwd, sizeof cwd) == NULL)
   1142   1.11       sjg 		err(1, "Could not get current working directory");
   1143   1.11       sjg 	    cwdlen = strlen(cwd);
   1144   1.11       sjg 	}
   1145  1.138    rillig 	strlcpy(lcwd, cwd, sizeof lcwd);
   1146  1.138    rillig 	strlcpy(latestdir, cwd, sizeof latestdir);
   1147   1.11       sjg 
   1148  1.141    rillig 	if (tmpdir == NULL) {
   1149    1.5       sjg 	    tmpdir = getTmpdir();
   1150    1.5       sjg 	    tmplen = strlen(tmpdir);
   1151    1.5       sjg 	}
   1152    1.5       sjg 
   1153    1.1       sjg 	/* we want to track all the .meta we read */
   1154  1.173    rillig 	Global_Append(".MAKE.META.FILES", fname);
   1155    1.1       sjg 
   1156  1.153    rillig 	cmdNode = gn->commands.first;
   1157    1.1       sjg 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
   1158    1.1       sjg 	    lineno++;
   1159    1.1       sjg 	    if (buf[x - 1] == '\n')
   1160    1.1       sjg 		buf[x - 1] = '\0';
   1161   1.20       sjg 	    else {
   1162    1.1       sjg 		warnx("%s: %d: line truncated at %u", fname, lineno, x);
   1163   1.20       sjg 		oodate = TRUE;
   1164   1.20       sjg 		break;
   1165   1.20       sjg 	    }
   1166   1.33       sjg 	    link_src = NULL;
   1167   1.33       sjg 	    move_target = NULL;
   1168    1.1       sjg 	    /* Find the start of the build monitor section. */
   1169   1.58       sjg 	    if (!have_filemon) {
   1170    1.1       sjg 		if (strncmp(buf, "-- filemon", 10) == 0) {
   1171   1.58       sjg 		    have_filemon = TRUE;
   1172    1.1       sjg 		    continue;
   1173    1.1       sjg 		}
   1174    1.1       sjg 		if (strncmp(buf, "# buildmon", 10) == 0) {
   1175   1.58       sjg 		    have_filemon = TRUE;
   1176    1.1       sjg 		    continue;
   1177    1.1       sjg 		}
   1178   1.85    rillig 	    }
   1179    1.1       sjg 
   1180    1.1       sjg 	    /* Delimit the record type. */
   1181    1.1       sjg 	    p = buf;
   1182    1.7       sjg #ifdef DEBUG_META_MODE
   1183  1.121    rillig 	    DEBUG3(META, "%s: %d: %s\n", fname, lineno, buf);
   1184    1.7       sjg #endif
   1185    1.1       sjg 	    strsep(&p, " ");
   1186   1.58       sjg 	    if (have_filemon) {
   1187    1.5       sjg 		/*
   1188    1.5       sjg 		 * We are in the 'filemon' output section.
   1189    1.5       sjg 		 * Each record from filemon follows the general form:
   1190    1.5       sjg 		 *
   1191    1.5       sjg 		 * <key> <pid> <data>
   1192    1.5       sjg 		 *
   1193    1.5       sjg 		 * Where:
   1194    1.5       sjg 		 * <key> is a single letter, denoting the syscall.
   1195    1.5       sjg 		 * <pid> is the process that made the syscall.
   1196    1.5       sjg 		 * <data> is the arguments (of interest).
   1197    1.5       sjg 		 */
   1198    1.5       sjg 		switch(buf[0]) {
   1199    1.5       sjg 		case '#':		/* comment */
   1200    1.5       sjg 		case 'V':		/* version */
   1201    1.5       sjg 		    break;
   1202    1.5       sjg 		default:
   1203    1.5       sjg 		    /*
   1204    1.5       sjg 		     * We need to track pathnames per-process.
   1205    1.5       sjg 		     *
   1206  1.170    rillig 		     * Each process run by make starts off in the 'CWD'
   1207    1.5       sjg 		     * recorded in the .meta file, if it chdirs ('C')
   1208    1.5       sjg 		     * elsewhere we need to track that - but only for
   1209    1.5       sjg 		     * that process.  If it forks ('F'), we initialize
   1210    1.5       sjg 		     * the child to have the same cwd as its parent.
   1211    1.5       sjg 		     *
   1212    1.5       sjg 		     * We also need to track the 'latestdir' of
   1213    1.5       sjg 		     * interest.  This is usually the same as cwd, but
   1214    1.5       sjg 		     * not if a process is reading directories.
   1215    1.5       sjg 		     *
   1216    1.5       sjg 		     * Each time we spot a different process ('pid')
   1217    1.5       sjg 		     * we save the current value of 'latestdir' in a
   1218    1.5       sjg 		     * variable qualified by 'lastpid', and
   1219    1.5       sjg 		     * re-initialize 'latestdir' to any pre-saved
   1220    1.5       sjg 		     * value for the current 'pid' and 'CWD' if none.
   1221    1.5       sjg 		     */
   1222   1.20       sjg 		    CHECK_VALID_META(p);
   1223    1.5       sjg 		    pid = atoi(p);
   1224    1.5       sjg 		    if (pid > 0 && pid != lastpid) {
   1225  1.161    rillig 			FStr ldir;
   1226   1.83    rillig 
   1227    1.5       sjg 			if (lastpid > 0) {
   1228   1.38       sjg 			    /* We need to remember these. */
   1229  1.171    rillig 			    Global_SetExpand(lcwd_vname, lcwd);
   1230  1.171    rillig 			    Global_SetExpand(ldir_vname, latestdir);
   1231    1.5       sjg 			}
   1232  1.138    rillig 			snprintf(lcwd_vname, sizeof lcwd_vname, LCWD_VNAME_FMT, pid);
   1233  1.138    rillig 			snprintf(ldir_vname, sizeof ldir_vname, LDIR_VNAME_FMT, pid);
   1234    1.5       sjg 			lastpid = pid;
   1235  1.176    rillig 			ldir = Var_Value(SCOPE_GLOBAL, ldir_vname);
   1236  1.161    rillig 			if (ldir.str != NULL) {
   1237  1.161    rillig 			    strlcpy(latestdir, ldir.str, sizeof latestdir);
   1238  1.161    rillig 			    FStr_Done(&ldir);
   1239   1.38       sjg 			}
   1240  1.176    rillig 			ldir = Var_Value(SCOPE_GLOBAL, lcwd_vname);
   1241  1.161    rillig 			if (ldir.str != NULL) {
   1242  1.161    rillig 			    strlcpy(lcwd, ldir.str, sizeof lcwd);
   1243  1.161    rillig 			    FStr_Done(&ldir);
   1244   1.38       sjg 			}
   1245    1.5       sjg 		    }
   1246    1.5       sjg 		    /* Skip past the pid. */
   1247    1.5       sjg 		    if (strsep(&p, " ") == NULL)
   1248    1.5       sjg 			continue;
   1249    1.7       sjg #ifdef DEBUG_META_MODE
   1250    1.7       sjg 		    if (DEBUG(META))
   1251  1.122    rillig 			debug_printf("%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
   1252  1.122    rillig 				     fname, lineno,
   1253  1.122    rillig 				     pid, buf[0], cwd, lcwd, latestdir);
   1254    1.7       sjg #endif
   1255    1.5       sjg 		    break;
   1256    1.5       sjg 		}
   1257    1.5       sjg 
   1258   1.20       sjg 		CHECK_VALID_META(p);
   1259   1.20       sjg 
   1260    1.1       sjg 		/* Process according to record type. */
   1261    1.1       sjg 		switch (buf[0]) {
   1262    1.5       sjg 		case 'X':		/* eXit */
   1263  1.176    rillig 		    Var_DeleteExpand(SCOPE_GLOBAL, lcwd_vname);
   1264  1.176    rillig 		    Var_DeleteExpand(SCOPE_GLOBAL, ldir_vname);
   1265    1.5       sjg 		    lastpid = 0;	/* no need to save ldir_vname */
   1266    1.5       sjg 		    break;
   1267    1.5       sjg 
   1268    1.5       sjg 		case 'F':		/* [v]Fork */
   1269    1.5       sjg 		    {
   1270    1.5       sjg 			char cldir[64];
   1271    1.5       sjg 			int child;
   1272    1.5       sjg 
   1273    1.5       sjg 			child = atoi(p);
   1274    1.5       sjg 			if (child > 0) {
   1275  1.138    rillig 			    snprintf(cldir, sizeof cldir, LCWD_VNAME_FMT, child);
   1276  1.171    rillig 			    Global_SetExpand(cldir, lcwd);
   1277  1.138    rillig 			    snprintf(cldir, sizeof cldir, LDIR_VNAME_FMT, child);
   1278  1.171    rillig 			    Global_SetExpand(cldir, latestdir);
   1279   1.38       sjg #ifdef DEBUG_META_MODE
   1280   1.38       sjg 			    if (DEBUG(META))
   1281  1.122    rillig 				debug_printf(
   1282  1.122    rillig 					"%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
   1283  1.122    rillig 					fname, lineno,
   1284  1.122    rillig 					child, cwd, lcwd, latestdir);
   1285   1.38       sjg #endif
   1286    1.5       sjg 			}
   1287    1.5       sjg 		    }
   1288    1.5       sjg 		    break;
   1289    1.1       sjg 
   1290    1.5       sjg 		case 'C':		/* Chdir */
   1291   1.38       sjg 		    /* Update lcwd and latest directory. */
   1292  1.138    rillig 		    strlcpy(latestdir, p, sizeof latestdir);
   1293  1.138    rillig 		    strlcpy(lcwd, p, sizeof lcwd);
   1294  1.171    rillig 		    Global_SetExpand(lcwd_vname, lcwd);
   1295  1.171    rillig 		    Global_SetExpand(ldir_vname, lcwd);
   1296   1.38       sjg #ifdef DEBUG_META_MODE
   1297  1.121    rillig 		    DEBUG4(META, "%s: %d: cwd=%s ldir=%s\n",
   1298  1.121    rillig 			   fname, lineno, cwd, lcwd);
   1299   1.38       sjg #endif
   1300    1.1       sjg 		    break;
   1301    1.1       sjg 
   1302   1.17       sjg 		case 'M':		/* renaMe */
   1303   1.33       sjg 		    /*
   1304   1.33       sjg 		     * For 'M'oves we want to check
   1305   1.33       sjg 		     * the src as for 'R'ead
   1306   1.33       sjg 		     * and the target as for 'W'rite.
   1307   1.33       sjg 		     */
   1308  1.163    rillig 		    {
   1309  1.163    rillig 			char *cp = p;	/* save this for a second */
   1310  1.163    rillig 			/* now get target */
   1311  1.163    rillig 			if (strsep(&p, " ") == NULL)
   1312  1.163    rillig 			    continue;
   1313  1.163    rillig 			CHECK_VALID_META(p);
   1314  1.163    rillig 			move_target = p;
   1315  1.163    rillig 			p = cp;
   1316  1.163    rillig 		    }
   1317   1.17       sjg 		    /* 'L' and 'M' put single quotes around the args */
   1318   1.33       sjg 		    DEQUOTE(p);
   1319   1.33       sjg 		    DEQUOTE(move_target);
   1320   1.17       sjg 		    /* FALLTHROUGH */
   1321   1.17       sjg 		case 'D':		/* unlink */
   1322  1.129    rillig 		    if (*p == '/') {
   1323   1.64       sjg 			/* remove any missingFiles entries that match p */
   1324  1.156    rillig 			StringListNode *ln = missingFiles.first;
   1325  1.129    rillig 			while (ln != NULL) {
   1326  1.129    rillig 			    StringListNode *next = ln->next;
   1327  1.129    rillig 			    if (path_starts_with(ln->datum, p)) {
   1328  1.132    rillig 				free(ln->datum);
   1329  1.156    rillig 				Lst_Remove(&missingFiles, ln);
   1330  1.129    rillig 			    }
   1331  1.129    rillig 			    ln = next;
   1332   1.17       sjg 			}
   1333   1.17       sjg 		    }
   1334   1.33       sjg 		    if (buf[0] == 'M') {
   1335   1.33       sjg 			/* the target of the mv is a file 'W'ritten */
   1336   1.33       sjg #ifdef DEBUG_META_MODE
   1337  1.121    rillig 			DEBUG2(META, "meta_oodate: M %s -> %s\n",
   1338  1.121    rillig 			       p, move_target);
   1339   1.33       sjg #endif
   1340   1.33       sjg 			p = move_target;
   1341   1.33       sjg 			goto check_write;
   1342   1.33       sjg 		    }
   1343   1.17       sjg 		    break;
   1344   1.17       sjg 		case 'L':		/* Link */
   1345   1.33       sjg 		    /*
   1346   1.33       sjg 		     * For 'L'inks check
   1347   1.33       sjg 		     * the src as for 'R'ead
   1348   1.33       sjg 		     * and the target as for 'W'rite.
   1349   1.33       sjg 		     */
   1350   1.33       sjg 		    link_src = p;
   1351   1.33       sjg 		    /* now get target */
   1352   1.17       sjg 		    if (strsep(&p, " ") == NULL)
   1353   1.17       sjg 			continue;
   1354   1.20       sjg 		    CHECK_VALID_META(p);
   1355   1.17       sjg 		    /* 'L' and 'M' put single quotes around the args */
   1356   1.33       sjg 		    DEQUOTE(p);
   1357   1.33       sjg 		    DEQUOTE(link_src);
   1358   1.33       sjg #ifdef DEBUG_META_MODE
   1359  1.121    rillig 		    DEBUG2(META, "meta_oodate: L %s -> %s\n", link_src, p);
   1360   1.33       sjg #endif
   1361   1.17       sjg 		    /* FALLTHROUGH */
   1362   1.17       sjg 		case 'W':		/* Write */
   1363   1.33       sjg 		check_write:
   1364   1.17       sjg 		    /*
   1365   1.17       sjg 		     * If a file we generated within our bailiwick
   1366   1.17       sjg 		     * but outside of .OBJDIR is missing,
   1367   1.85    rillig 		     * we need to do it again.
   1368   1.17       sjg 		     */
   1369   1.17       sjg 		    /* ignore non-absolute paths */
   1370   1.17       sjg 		    if (*p != '/')
   1371   1.17       sjg 			break;
   1372   1.17       sjg 
   1373  1.155    rillig 		    if (Lst_IsEmpty(&metaBailiwick))
   1374   1.17       sjg 			break;
   1375   1.17       sjg 
   1376   1.17       sjg 		    /* ignore cwd - normal dependencies handle those */
   1377   1.17       sjg 		    if (strncmp(p, cwd, cwdlen) == 0)
   1378   1.17       sjg 			break;
   1379   1.17       sjg 
   1380  1.155    rillig 		    if (!has_any_prefix(p, &metaBailiwick))
   1381   1.17       sjg 			break;
   1382   1.17       sjg 
   1383   1.17       sjg 		    /* tmpdir might be within */
   1384   1.17       sjg 		    if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
   1385   1.17       sjg 			break;
   1386   1.17       sjg 
   1387   1.17       sjg 		    /* ignore anything containing the string "tmp" */
   1388  1.144    rillig 		    /* XXX: The arguments to strstr must be swapped. */
   1389  1.166    rillig 		    if (strstr("tmp", p) != NULL)
   1390   1.17       sjg 			break;
   1391   1.17       sjg 
   1392  1.143    rillig 		    if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
   1393  1.143    rillig 			(link_src == NULL && cached_stat(p, &cst) < 0)) {
   1394  1.130    rillig 			if (!meta_ignore(gn, p))
   1395  1.156    rillig 			    append_if_new(&missingFiles, p);
   1396   1.17       sjg 		    }
   1397   1.17       sjg 		    break;
   1398   1.33       sjg 		check_link_src:
   1399   1.33       sjg 		    p = link_src;
   1400   1.33       sjg 		    link_src = NULL;
   1401   1.33       sjg #ifdef DEBUG_META_MODE
   1402  1.121    rillig 		    DEBUG1(META, "meta_oodate: L src %s\n", p);
   1403   1.33       sjg #endif
   1404   1.33       sjg 		    /* FALLTHROUGH */
   1405    1.5       sjg 		case 'R':		/* Read */
   1406    1.5       sjg 		case 'E':		/* Exec */
   1407    1.1       sjg 		    /*
   1408    1.1       sjg 		     * Check for runtime files that can't
   1409    1.1       sjg 		     * be part of the dependencies because
   1410    1.1       sjg 		     * they are _expected_ to change.
   1411    1.1       sjg 		     */
   1412   1.67       sjg 		    if (meta_ignore(gn, p))
   1413   1.67       sjg 			break;
   1414   1.85    rillig 
   1415    1.1       sjg 		    /*
   1416    1.5       sjg 		     * The rest of the record is the file name.
   1417    1.5       sjg 		     * Check if it's not an absolute path.
   1418    1.1       sjg 		     */
   1419    1.5       sjg 		    {
   1420    1.5       sjg 			char *sdirs[4];
   1421    1.5       sjg 			char **sdp;
   1422    1.5       sjg 			int sdx = 0;
   1423  1.147    rillig 			Boolean found = FALSE;
   1424    1.5       sjg 
   1425    1.5       sjg 			if (*p == '/') {
   1426    1.5       sjg 			    sdirs[sdx++] = p; /* done */
   1427    1.5       sjg 			} else {
   1428    1.5       sjg 			    if (strcmp(".", p) == 0)
   1429  1.145    rillig 				continue; /* no point */
   1430    1.5       sjg 
   1431    1.5       sjg 			    /* Check vs latestdir */
   1432  1.138    rillig 			    snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p);
   1433    1.5       sjg 			    sdirs[sdx++] = fname1;
   1434    1.5       sjg 
   1435   1.38       sjg 			    if (strcmp(latestdir, lcwd) != 0) {
   1436   1.38       sjg 				/* Check vs lcwd */
   1437  1.138    rillig 				snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p);
   1438   1.38       sjg 				sdirs[sdx++] = fname2;
   1439   1.38       sjg 			    }
   1440   1.38       sjg 			    if (strcmp(lcwd, cwd) != 0) {
   1441    1.5       sjg 				/* Check vs cwd */
   1442  1.138    rillig 				snprintf(fname3, sizeof fname3, "%s/%s", cwd, p);
   1443   1.38       sjg 				sdirs[sdx++] = fname3;
   1444    1.5       sjg 			    }
   1445    1.5       sjg 			}
   1446    1.5       sjg 			sdirs[sdx++] = NULL;
   1447    1.1       sjg 
   1448  1.168    rillig 			for (sdp = sdirs; *sdp != NULL && !found; sdp++) {
   1449    1.7       sjg #ifdef DEBUG_META_MODE
   1450  1.121    rillig 			    DEBUG3(META, "%s: %d: looking for: %s\n",
   1451  1.121    rillig 				   fname, lineno, *sdp);
   1452    1.7       sjg #endif
   1453  1.143    rillig 			    if (cached_stat(*sdp, &cst) == 0) {
   1454  1.147    rillig 				found = TRUE;
   1455    1.5       sjg 				p = *sdp;
   1456    1.5       sjg 			    }
   1457    1.5       sjg 			}
   1458    1.5       sjg 			if (found) {
   1459    1.7       sjg #ifdef DEBUG_META_MODE
   1460  1.121    rillig 			    DEBUG3(META, "%s: %d: found: %s\n",
   1461  1.121    rillig 				   fname, lineno, p);
   1462    1.7       sjg #endif
   1463  1.143    rillig 			    if (!S_ISDIR(cst.cst_mode) &&
   1464  1.143    rillig 				cst.cst_mtime > gn->mtime) {
   1465  1.121    rillig 				DEBUG3(META, "%s: %d: file '%s' is newer than the target...\n",
   1466  1.121    rillig 				       fname, lineno, p);
   1467    1.5       sjg 				oodate = TRUE;
   1468  1.143    rillig 			    } else if (S_ISDIR(cst.cst_mode)) {
   1469    1.5       sjg 				/* Update the latest directory. */
   1470   1.59       sjg 				cached_realpath(p, latestdir);
   1471    1.5       sjg 			    }
   1472    1.5       sjg 			} else if (errno == ENOENT && *p == '/' &&
   1473    1.5       sjg 				   strncmp(p, cwd, cwdlen) != 0) {
   1474    1.5       sjg 			    /*
   1475    1.5       sjg 			     * A referenced file outside of CWD is missing.
   1476    1.5       sjg 			     * We cannot catch every eventuality here...
   1477    1.5       sjg 			     */
   1478  1.156    rillig 			    append_if_new(&missingFiles, p);
   1479    1.4       sjg 			}
   1480    1.1       sjg 		    }
   1481   1.38       sjg 		    if (buf[0] == 'E') {
   1482   1.38       sjg 			/* previous latestdir is no longer relevant */
   1483  1.138    rillig 			strlcpy(latestdir, lcwd, sizeof latestdir);
   1484   1.38       sjg 		    }
   1485    1.1       sjg 		    break;
   1486    1.1       sjg 		default:
   1487    1.1       sjg 		    break;
   1488    1.1       sjg 		}
   1489   1.33       sjg 		if (!oodate && buf[0] == 'L' && link_src != NULL)
   1490   1.33       sjg 		    goto check_link_src;
   1491    1.5       sjg 	    } else if (strcmp(buf, "CMD") == 0) {
   1492    1.1       sjg 		/*
   1493    1.1       sjg 		 * Compare the current command with the one in the
   1494    1.1       sjg 		 * meta data file.
   1495    1.1       sjg 		 */
   1496  1.115    rillig 		if (cmdNode == NULL) {
   1497  1.121    rillig 		    DEBUG2(META, "%s: %d: there were more build commands in the meta data file than there are now...\n",
   1498  1.121    rillig 			   fname, lineno);
   1499    1.1       sjg 		    oodate = TRUE;
   1500    1.1       sjg 		} else {
   1501  1.163    rillig 		    const char *cp;
   1502  1.127    rillig 		    char *cmd = cmdNode->datum;
   1503   1.29       sjg 		    Boolean hasOODATE = FALSE;
   1504    1.1       sjg 
   1505  1.166    rillig 		    if (strstr(cmd, "$?") != NULL)
   1506   1.29       sjg 			hasOODATE = TRUE;
   1507  1.166    rillig 		    else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
   1508   1.29       sjg 			/* check for $[{(].OODATE[:)}] */
   1509   1.29       sjg 			if (cp > cmd + 2 && cp[-2] == '$')
   1510   1.29       sjg 			    hasOODATE = TRUE;
   1511   1.29       sjg 		    }
   1512   1.29       sjg 		    if (hasOODATE) {
   1513   1.29       sjg 			needOODATE = TRUE;
   1514  1.121    rillig 			DEBUG2(META, "%s: %d: cannot compare command using .OODATE\n",
   1515  1.121    rillig 			       fname, lineno);
   1516    1.1       sjg 		    }
   1517  1.117    rillig 		    (void)Var_Subst(cmd, gn, VARE_WANTRES|VARE_UNDEFERR, &cmd);
   1518  1.117    rillig 		    /* TODO: handle errors */
   1519    1.1       sjg 
   1520  1.166    rillig 		    if ((cp = strchr(cmd, '\n')) != NULL) {
   1521    1.1       sjg 			int n;
   1522    1.1       sjg 
   1523    1.1       sjg 			/*
   1524    1.1       sjg 			 * This command contains newlines, we need to
   1525    1.1       sjg 			 * fetch more from the .meta file before we
   1526    1.1       sjg 			 * attempt a comparison.
   1527    1.1       sjg 			 */
   1528    1.1       sjg 			/* first put the newline back at buf[x - 1] */
   1529    1.1       sjg 			buf[x - 1] = '\n';
   1530    1.1       sjg 			do {
   1531    1.1       sjg 			    /* now fetch the next line */
   1532    1.1       sjg 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
   1533    1.1       sjg 				break;
   1534    1.1       sjg 			    x = n;
   1535    1.1       sjg 			    lineno++;
   1536    1.1       sjg 			    if (buf[x - 1] != '\n') {
   1537    1.1       sjg 				warnx("%s: %d: line truncated at %u", fname, lineno, x);
   1538    1.1       sjg 				break;
   1539    1.1       sjg 			    }
   1540  1.164    rillig 			    cp = strchr(cp + 1, '\n');
   1541  1.147    rillig 			} while (cp != NULL);
   1542    1.1       sjg 			if (buf[x - 1] == '\n')
   1543    1.1       sjg 			    buf[x - 1] = '\0';
   1544    1.1       sjg 		    }
   1545  1.139    rillig 		    if (p != NULL &&
   1546   1.81       sjg 			!hasOODATE &&
   1547    1.1       sjg 			!(gn->type & OP_NOMETA_CMP) &&
   1548  1.139    rillig 			(strcmp(p, cmd) != 0)) {
   1549  1.121    rillig 			DEBUG4(META, "%s: %d: a build command has changed\n%s\nvs\n%s\n",
   1550  1.121    rillig 			       fname, lineno, p, cmd);
   1551    1.1       sjg 			if (!metaIgnoreCMDs)
   1552    1.1       sjg 			    oodate = TRUE;
   1553    1.1       sjg 		    }
   1554    1.1       sjg 		    free(cmd);
   1555  1.120    rillig 		    cmdNode = cmdNode->next;
   1556    1.1       sjg 		}
   1557    1.1       sjg 	    } else if (strcmp(buf, "CWD") == 0) {
   1558   1.14       sjg 		/*
   1559   1.14       sjg 		 * Check if there are extra commands now
   1560   1.14       sjg 		 * that weren't in the meta data file.
   1561   1.14       sjg 		 */
   1562  1.115    rillig 		if (!oodate && cmdNode != NULL) {
   1563  1.121    rillig 		    DEBUG2(META, "%s: %d: there are extra build commands now that weren't in the meta data file\n",
   1564  1.121    rillig 			   fname, lineno);
   1565   1.14       sjg 		    oodate = TRUE;
   1566   1.14       sjg 		}
   1567   1.80       sjg 		CHECK_VALID_META(p);
   1568   1.11       sjg 		if (strcmp(p, cwd) != 0) {
   1569  1.121    rillig 		    DEBUG4(META, "%s: %d: the current working directory has changed from '%s' to '%s'\n",
   1570  1.121    rillig 			   fname, lineno, p, curdir);
   1571    1.1       sjg 		    oodate = TRUE;
   1572    1.1       sjg 		}
   1573    1.1       sjg 	    }
   1574    1.1       sjg 	}
   1575    1.1       sjg 
   1576    1.1       sjg 	fclose(fp);
   1577  1.156    rillig 	if (!Lst_IsEmpty(&missingFiles)) {
   1578  1.121    rillig 	    DEBUG2(META, "%s: missing files: %s...\n",
   1579  1.156    rillig 		   fname, (char *)missingFiles.first->datum);
   1580   1.17       sjg 	    oodate = TRUE;
   1581   1.17       sjg 	}
   1582   1.60       sjg 	if (!oodate && !have_filemon && filemonMissing) {
   1583  1.121    rillig 	    DEBUG1(META, "%s: missing filemon data\n", fname);
   1584   1.21       sjg 	    oodate = TRUE;
   1585   1.21       sjg 	}
   1586   1.60       sjg     } else {
   1587   1.86       sjg 	if (writeMeta && (metaMissing || (gn->type & OP_META))) {
   1588  1.163    rillig 	    const char *cp = NULL;
   1589   1.50  christos 
   1590   1.60       sjg 	    /* if target is in .CURDIR we do not need a meta file */
   1591  1.168    rillig 	    if (gn->path != NULL && (cp = strrchr(gn->path, '/')) != NULL &&
   1592  1.168    rillig 		(cp > gn->path)) {
   1593  1.124    rillig 		if (strncmp(curdir, gn->path, (size_t)(cp - gn->path)) != 0) {
   1594   1.60       sjg 		    cp = NULL;		/* not in .CURDIR */
   1595   1.60       sjg 		}
   1596   1.60       sjg 	    }
   1597  1.141    rillig 	    if (cp == NULL) {
   1598  1.121    rillig 		DEBUG1(META, "%s: required but missing\n", fname);
   1599   1.60       sjg 		oodate = TRUE;
   1600   1.62       sjg 		needOODATE = TRUE;	/* assume the worst */
   1601   1.60       sjg 	    }
   1602   1.60       sjg 	}
   1603   1.58       sjg     }
   1604   1.58       sjg 
   1605  1.156    rillig     Lst_DoneCall(&missingFiles, free);
   1606   1.50  christos 
   1607   1.26       sjg     if (oodate && needOODATE) {
   1608    1.4       sjg 	/*
   1609   1.26       sjg 	 * Target uses .OODATE which is empty; or we wouldn't be here.
   1610   1.26       sjg 	 * We have decided it is oodate, so .OODATE needs to be set.
   1611   1.26       sjg 	 * All we can sanely do is set it to .ALLSRC.
   1612    1.4       sjg 	 */
   1613  1.176    rillig 	Var_Delete(gn, OODATE);
   1614  1.176    rillig 	Var_Set(gn, OODATE, GNode_VarAllsrc(gn));
   1615    1.4       sjg     }
   1616   1.58       sjg 
   1617   1.58       sjg  oodate_out:
   1618  1.161    rillig     FStr_Done(&dname);
   1619    1.1       sjg     return oodate;
   1620    1.1       sjg }
   1621    1.1       sjg 
   1622    1.1       sjg /* support for compat mode */
   1623    1.1       sjg 
   1624    1.1       sjg static int childPipe[2];
   1625    1.1       sjg 
   1626    1.1       sjg void
   1627    1.1       sjg meta_compat_start(void)
   1628    1.1       sjg {
   1629   1.73      maxv #ifdef USE_FILEMON_ONCE
   1630   1.73      maxv     /*
   1631   1.73      maxv      * We need to re-open filemon for each cmd.
   1632   1.73      maxv      */
   1633   1.73      maxv     BuildMon *pbm = &Mybm;
   1634   1.85    rillig 
   1635   1.73      maxv     if (pbm->mfp != NULL && useFilemon) {
   1636   1.74  riastrad 	meta_open_filemon(pbm);
   1637   1.73      maxv     } else {
   1638   1.74  riastrad 	pbm->mon_fd = -1;
   1639   1.74  riastrad 	pbm->filemon = NULL;
   1640   1.73      maxv     }
   1641   1.73      maxv #endif
   1642    1.1       sjg     if (pipe(childPipe) < 0)
   1643    1.1       sjg 	Punt("Cannot create pipe: %s", strerror(errno));
   1644    1.1       sjg     /* Set close-on-exec flag for both */
   1645   1.42  christos     (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
   1646   1.42  christos     (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
   1647    1.1       sjg }
   1648    1.1       sjg 
   1649    1.1       sjg void
   1650    1.1       sjg meta_compat_child(void)
   1651    1.1       sjg {
   1652    1.1       sjg     meta_job_child(NULL);
   1653  1.123    rillig     if (dup2(childPipe[1], 1) < 0 || dup2(1, 2) < 0)
   1654  1.123    rillig 	execDie("dup2", "pipe");
   1655    1.1       sjg }
   1656    1.1       sjg 
   1657    1.1       sjg void
   1658   1.74  riastrad meta_compat_parent(pid_t child)
   1659    1.1       sjg {
   1660   1.74  riastrad     int outfd, metafd, maxfd, nfds;
   1661   1.78       sjg     char buf[BUFSIZ+1];
   1662   1.74  riastrad     fd_set readfds;
   1663   1.74  riastrad 
   1664   1.74  riastrad     meta_job_parent(NULL, child);
   1665    1.1       sjg     close(childPipe[1]);			/* child side */
   1666   1.74  riastrad     outfd = childPipe[0];
   1667   1.79       sjg #ifdef USE_FILEMON
   1668  1.168    rillig     metafd = Mybm.filemon != NULL ? filemon_readfd(Mybm.filemon) : -1;
   1669   1.79       sjg #else
   1670   1.79       sjg     metafd = -1;
   1671   1.79       sjg #endif
   1672   1.74  riastrad     maxfd = -1;
   1673   1.74  riastrad     if (outfd > maxfd)
   1674   1.74  riastrad 	    maxfd = outfd;
   1675   1.74  riastrad     if (metafd > maxfd)
   1676   1.74  riastrad 	    maxfd = metafd;
   1677   1.74  riastrad 
   1678   1.74  riastrad     while (outfd != -1 || metafd != -1) {
   1679   1.74  riastrad 	FD_ZERO(&readfds);
   1680   1.74  riastrad 	if (outfd != -1) {
   1681   1.74  riastrad 	    FD_SET(outfd, &readfds);
   1682   1.74  riastrad 	}
   1683   1.74  riastrad 	if (metafd != -1) {
   1684   1.74  riastrad 	    FD_SET(metafd, &readfds);
   1685   1.74  riastrad 	}
   1686   1.74  riastrad 	nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
   1687   1.74  riastrad 	if (nfds == -1) {
   1688   1.74  riastrad 	    if (errno == EINTR)
   1689   1.74  riastrad 		continue;
   1690   1.74  riastrad 	    err(1, "select");
   1691   1.74  riastrad 	}
   1692   1.74  riastrad 
   1693  1.168    rillig 	if (outfd != -1 && FD_ISSET(outfd, &readfds) != 0) do {
   1694   1.74  riastrad 	    /* XXX this is not line-buffered */
   1695  1.138    rillig 	    ssize_t nread = read(outfd, buf, sizeof buf - 1);
   1696   1.74  riastrad 	    if (nread == -1)
   1697   1.74  riastrad 		err(1, "read");
   1698   1.74  riastrad 	    if (nread == 0) {
   1699   1.74  riastrad 		close(outfd);
   1700   1.74  riastrad 		outfd = -1;
   1701   1.74  riastrad 		break;
   1702   1.74  riastrad 	    }
   1703   1.74  riastrad 	    fwrite(buf, 1, (size_t)nread, stdout);
   1704   1.74  riastrad 	    fflush(stdout);
   1705   1.78       sjg 	    buf[nread] = '\0';
   1706   1.78       sjg 	    meta_job_output(NULL, buf, "");
   1707  1.169    rillig 	} while (/*CONSTCOND*/FALSE);
   1708  1.168    rillig 	if (metafd != -1 && FD_ISSET(metafd, &readfds) != 0) {
   1709   1.74  riastrad 	    if (meta_job_event(NULL) <= 0)
   1710   1.74  riastrad 		metafd = -1;
   1711   1.74  riastrad 	}
   1712    1.1       sjg     }
   1713    1.1       sjg }
   1714    1.3       sjg 
   1715  1.154    rillig #endif /* USE_META */
   1716