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