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