Home | History | Annotate | Line # | Download | only in make
meta.c revision 1.6
      1  1.1  sjg /*
      2  1.1  sjg  * Implement 'meta' mode.
      3  1.1  sjg  * Adapted from John Birrell's patches to FreeBSD make.
      4  1.1  sjg  * --sjg
      5  1.1  sjg  */
      6  1.1  sjg /*
      7  1.1  sjg  * Copyright (c) 2009-2010, Juniper Networks, Inc.
      8  1.1  sjg  * Portions Copyright (c) 2009, John Birrell.
      9  1.1  sjg  *
     10  1.1  sjg  * Redistribution and use in source and binary forms, with or without
     11  1.1  sjg  * modification, are permitted provided that the following conditions
     12  1.1  sjg  * are met:
     13  1.1  sjg  * 1. Redistributions of source code must retain the above copyright
     14  1.1  sjg  *    notice, this list of conditions and the following disclaimer.
     15  1.1  sjg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  sjg  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  sjg  *    documentation and/or other materials provided with the distribution.
     18  1.1  sjg  *
     19  1.1  sjg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  1.1  sjg  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  1.1  sjg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  1.1  sjg  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  1.1  sjg  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  1.1  sjg  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  1.1  sjg  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1  sjg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  sjg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1  sjg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  1.1  sjg  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  sjg  */
     31  1.3  sjg #if defined(USE_META)
     32  1.1  sjg 
     33  1.1  sjg #ifdef HAVE_CONFIG_H
     34  1.1  sjg # include "config.h"
     35  1.1  sjg #endif
     36  1.1  sjg #include <sys/stat.h>
     37  1.1  sjg #include <sys/ioctl.h>
     38  1.1  sjg #include <fcntl.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.1  sjg #include "job.h"
     47  1.1  sjg 
     48  1.1  sjg #ifdef HAVE_FILEMON_H
     49  1.1  sjg # include <filemon.h>
     50  1.1  sjg #endif
     51  1.1  sjg #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
     52  1.1  sjg # define USE_FILEMON
     53  1.1  sjg #endif
     54  1.1  sjg 
     55  1.1  sjg static BuildMon Mybm;			/* for compat */
     56  1.1  sjg 
     57  1.1  sjg Boolean useMeta = FALSE;
     58  1.1  sjg static Boolean useFilemon = FALSE;
     59  1.1  sjg static Boolean writeMeta = FALSE;
     60  1.1  sjg static Boolean metaEnv = FALSE;		/* don't save env unless asked */
     61  1.1  sjg static Boolean metaVerbose = FALSE;
     62  1.1  sjg static Boolean metaIgnoreCMDs = FALSE;	/* ignore CMDs in .meta files */
     63  1.1  sjg 
     64  1.1  sjg extern Boolean forceJobs;
     65  1.1  sjg extern Boolean comatMake;
     66  1.1  sjg 
     67  1.1  sjg #define	MAKE_META_PREFIX	".MAKE.META.PREFIX"
     68  1.1  sjg 
     69  1.1  sjg #ifndef N2U
     70  1.1  sjg # define N2U(n, u)   (((n) + ((u) - 1)) / (u))
     71  1.1  sjg #endif
     72  1.1  sjg #ifndef ROUNDUP
     73  1.1  sjg # define ROUNDUP(n, u)   (N2U((n), (u)) * (u))
     74  1.1  sjg #endif
     75  1.1  sjg 
     76  1.2  sjg #if !defined(HAVE_STRSEP)
     77  1.2  sjg # define strsep(s, d) stresep((s), (d), 0)
     78  1.2  sjg #endif
     79  1.2  sjg 
     80  1.1  sjg /*
     81  1.1  sjg  * Filemon is a kernel module which snoops certain syscalls.
     82  1.1  sjg  *
     83  1.1  sjg  * C chdir
     84  1.1  sjg  * E exec
     85  1.1  sjg  * F [v]fork
     86  1.1  sjg  * L [sym]link
     87  1.1  sjg  * M rename
     88  1.1  sjg  * R read
     89  1.1  sjg  * W write
     90  1.1  sjg  * S stat
     91  1.1  sjg  *
     92  1.1  sjg  * See meta_oodate below - we mainly care about 'E' and 'R'.
     93  1.1  sjg  *
     94  1.1  sjg  * We can still use meta mode without filemon, but
     95  1.1  sjg  * the benefits are more limited.
     96  1.1  sjg  */
     97  1.1  sjg #ifdef USE_FILEMON
     98  1.1  sjg # ifndef _PATH_FILEMON
     99  1.1  sjg #   define _PATH_FILEMON "/dev/filemon"
    100  1.1  sjg # endif
    101  1.1  sjg 
    102  1.1  sjg /*
    103  1.1  sjg  * Open the filemon device.
    104  1.1  sjg  */
    105  1.1  sjg static void
    106  1.1  sjg filemon_open(BuildMon *pbm)
    107  1.1  sjg {
    108  1.1  sjg     int retry;
    109  1.1  sjg 
    110  1.1  sjg     pbm->mon_fd = pbm->filemon_fd = -1;
    111  1.1  sjg     if (!useFilemon)
    112  1.1  sjg 	return;
    113  1.1  sjg 
    114  1.1  sjg     for (retry = 5; retry >= 0; retry--) {
    115  1.1  sjg 	if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
    116  1.1  sjg 	    break;
    117  1.1  sjg     }
    118  1.1  sjg 
    119  1.1  sjg     if (pbm->filemon_fd < 0) {
    120  1.1  sjg 	useFilemon = FALSE;
    121  1.1  sjg 	warn("Could not open %s", _PATH_FILEMON);
    122  1.1  sjg 	return;
    123  1.1  sjg     }
    124  1.1  sjg 
    125  1.1  sjg     /*
    126  1.1  sjg      * We use a file outside of '.'
    127  1.1  sjg      * to avoid a FreeBSD kernel bug where unlink invalidates
    128  1.1  sjg      * cwd causing getcwd to do a lot more work.
    129  1.1  sjg      * We only care about the descriptor.
    130  1.1  sjg      */
    131  1.1  sjg     pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
    132  1.1  sjg     if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
    133  1.1  sjg 	err(1, "Could not set filemon file descriptor!");
    134  1.1  sjg     }
    135  1.1  sjg     /* we don't need these once we exec */
    136  1.1  sjg     (void)fcntl(pbm->mon_fd, F_SETFD, 1);
    137  1.1  sjg     (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
    138  1.1  sjg }
    139  1.1  sjg 
    140  1.1  sjg /*
    141  1.1  sjg  * Read the build monitor output file and write records to the target's
    142  1.1  sjg  * metadata file.
    143  1.1  sjg  */
    144  1.1  sjg static void
    145  1.1  sjg filemon_read(FILE *mfp, int fd)
    146  1.1  sjg {
    147  1.1  sjg     FILE *fp;
    148  1.1  sjg     char buf[BUFSIZ];
    149  1.1  sjg 
    150  1.1  sjg     /* Check if we're not writing to a meta data file.*/
    151  1.1  sjg     if (mfp == NULL) {
    152  1.1  sjg 	if (fd >= 0)
    153  1.1  sjg 	    close(fd);			/* not interested */
    154  1.1  sjg 	return;
    155  1.1  sjg     }
    156  1.1  sjg     /* rewind */
    157  1.1  sjg     lseek(fd, SEEK_SET, 0);
    158  1.1  sjg     if ((fp = fdopen(fd, "r")) == NULL)
    159  1.1  sjg 	err(1, "Could not read build monitor file '%d'", fd);
    160  1.1  sjg 
    161  1.1  sjg     fprintf(mfp, "-- filemon acquired metadata --\n");
    162  1.1  sjg 
    163  1.1  sjg     while (fgets(buf, sizeof(buf), fp)) {
    164  1.1  sjg 	fprintf(mfp, "%s", buf);
    165  1.1  sjg     }
    166  1.1  sjg     fflush(mfp);
    167  1.1  sjg     clearerr(fp);
    168  1.1  sjg     fclose(fp);
    169  1.1  sjg }
    170  1.1  sjg #endif
    171  1.1  sjg 
    172  1.1  sjg static char *
    173  1.1  sjg meta_name(struct GNode *gn, char *mname, size_t mnamelen,
    174  1.1  sjg 	  const char *dname,
    175  1.1  sjg 	  const char *tname)
    176  1.1  sjg {
    177  1.1  sjg     char buf[MAXPATHLEN];
    178  1.1  sjg     char cwd[MAXPATHLEN];
    179  1.1  sjg     char *rp;
    180  1.1  sjg     char *cp;
    181  1.1  sjg     char *tp;
    182  1.1  sjg     char *p[4];				/* >= number of possible uses */
    183  1.1  sjg     int i;
    184  1.1  sjg 
    185  1.1  sjg     i = 0;
    186  1.1  sjg     if (!dname)
    187  1.1  sjg 	dname = Var_Value(".OBJDIR", gn, &p[i++]);
    188  1.1  sjg     if (!tname)
    189  1.1  sjg 	tname = Var_Value(TARGET, gn, &p[i++]);
    190  1.1  sjg 
    191  1.1  sjg     /*
    192  1.1  sjg      * Weed out relative paths from the target file name.
    193  1.1  sjg      * We have to be careful though since if target is a
    194  1.1  sjg      * symlink, the result will be unstable.
    195  1.1  sjg      * So we use realpath() just to get the dirname, and leave the
    196  1.1  sjg      * basename as given to us.
    197  1.1  sjg      */
    198  1.1  sjg     if ((cp = strrchr(tname, '/'))) {
    199  1.1  sjg 	if (realpath(tname, buf)) {
    200  1.1  sjg 	    if ((rp = strrchr(buf, '/'))) {
    201  1.1  sjg 		rp++;
    202  1.1  sjg 		cp++;
    203  1.1  sjg 		if (strcmp(cp, rp) != 0)
    204  1.1  sjg 		    strlcpy(rp, cp, sizeof(buf) - (rp - buf));
    205  1.1  sjg 	    }
    206  1.1  sjg 	    tname = buf;
    207  1.1  sjg 	}
    208  1.1  sjg     }
    209  1.1  sjg     if (realpath(dname, cwd))
    210  1.1  sjg 	dname = cwd;
    211  1.1  sjg     /* on some systems dirname may modify its arg */
    212  1.1  sjg     tp = bmake_strdup(tname);
    213  1.1  sjg     if (strcmp(dname, dirname(tp)) == 0)
    214  1.1  sjg 	snprintf(mname, mnamelen, "%s.meta", tname);
    215  1.1  sjg     else {
    216  1.1  sjg 	snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
    217  1.1  sjg 
    218  1.1  sjg 	/*
    219  1.1  sjg 	 * Replace path separators in the file name after the
    220  1.1  sjg 	 * current object directory path.
    221  1.1  sjg 	 */
    222  1.1  sjg 	cp = mname + strlen(dname) + 1;
    223  1.1  sjg 
    224  1.1  sjg 	while (*cp != '\0') {
    225  1.1  sjg 	    if (*cp == '/')
    226  1.1  sjg 		*cp = '_';
    227  1.1  sjg 	    cp++;
    228  1.1  sjg 	}
    229  1.1  sjg     }
    230  1.1  sjg     free(tp);
    231  1.1  sjg     for (i--; i >= 0; i--) {
    232  1.1  sjg 	if (p[i])
    233  1.1  sjg 	    free(p[i]);
    234  1.1  sjg     }
    235  1.1  sjg     return (mname);
    236  1.1  sjg }
    237  1.1  sjg 
    238  1.1  sjg /*
    239  1.1  sjg  * Return true if running ${.MAKE}
    240  1.1  sjg  * Bypassed if target is flagged .MAKE
    241  1.1  sjg  */
    242  1.1  sjg static int
    243  1.1  sjg is_submake(void *cmdp, void *gnp)
    244  1.1  sjg {
    245  1.1  sjg     static char *p_make = NULL;
    246  1.1  sjg     static int p_len;
    247  1.1  sjg     char  *cmd = cmdp;
    248  1.1  sjg     GNode *gn = gnp;
    249  1.1  sjg     char *mp = NULL;
    250  1.1  sjg     char *cp;
    251  1.1  sjg     char *cp2;
    252  1.1  sjg     int rc = 0;				/* keep looking */
    253  1.1  sjg 
    254  1.1  sjg     if (!p_make) {
    255  1.1  sjg 	p_make = Var_Value(".MAKE", gn, &cp);
    256  1.1  sjg 	p_len = strlen(p_make);
    257  1.1  sjg     }
    258  1.1  sjg     cp = strchr(cmd, '$');
    259  1.1  sjg     if ((cp)) {
    260  1.1  sjg 	mp = Var_Subst(NULL, cmd, gn, FALSE);
    261  1.1  sjg 	cmd = mp;
    262  1.1  sjg     }
    263  1.1  sjg     cp2 = strstr(cmd, p_make);
    264  1.1  sjg     if ((cp2)) {
    265  1.1  sjg 	switch (cp2[p_len]) {
    266  1.1  sjg 	case '\0':
    267  1.1  sjg 	case ' ':
    268  1.1  sjg 	case '\t':
    269  1.1  sjg 	case '\n':
    270  1.1  sjg 	    rc = 1;
    271  1.1  sjg 	    break;
    272  1.1  sjg 	}
    273  1.1  sjg 	if (cp2 > cmd && rc > 0) {
    274  1.1  sjg 	    switch (cp2[-1]) {
    275  1.1  sjg 	    case ' ':
    276  1.1  sjg 	    case '\t':
    277  1.1  sjg 	    case '\n':
    278  1.1  sjg 		break;
    279  1.1  sjg 	    default:
    280  1.1  sjg 		rc = 0;			/* no match */
    281  1.1  sjg 		break;
    282  1.1  sjg 	    }
    283  1.1  sjg 	}
    284  1.1  sjg     }
    285  1.1  sjg     if (mp)
    286  1.1  sjg 	free(mp);
    287  1.1  sjg     return (rc);
    288  1.1  sjg }
    289  1.1  sjg 
    290  1.1  sjg typedef struct meta_file_s {
    291  1.1  sjg     FILE *fp;
    292  1.1  sjg     GNode *gn;
    293  1.1  sjg } meta_file_t;
    294  1.1  sjg 
    295  1.1  sjg static int
    296  1.1  sjg printCMD(void *cmdp, void *mfpp)
    297  1.1  sjg {
    298  1.1  sjg     meta_file_t *mfp = mfpp;
    299  1.1  sjg     char *cmd = cmdp;
    300  1.1  sjg     char *cp = NULL;
    301  1.1  sjg 
    302  1.1  sjg     if (strchr(cmd, '$')) {
    303  1.1  sjg 	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
    304  1.1  sjg     }
    305  1.1  sjg     fprintf(mfp->fp, "CMD %s\n", cmd);
    306  1.1  sjg     if (cp)
    307  1.1  sjg 	free(cp);
    308  1.1  sjg     return 0;
    309  1.1  sjg }
    310  1.1  sjg 
    311  1.1  sjg /*
    312  1.1  sjg  * Certain node types never get a .meta file
    313  1.1  sjg  */
    314  1.1  sjg #define SKIP_META_TYPE(_type) do { \
    315  1.1  sjg     if ((gn->type & __CONCAT(OP_, _type))) {	\
    316  1.1  sjg 	if (DEBUG(META)) { \
    317  1.1  sjg 	    fprintf(debug_file, "Skipping meta for %s: .%s\n", \
    318  1.1  sjg 		    gn->name, __STRING(_type));		       \
    319  1.1  sjg 	} \
    320  1.1  sjg 	return (NULL); \
    321  1.1  sjg     } \
    322  1.1  sjg } while (0)
    323  1.1  sjg 
    324  1.1  sjg static FILE *
    325  1.1  sjg meta_create(BuildMon *pbm, GNode *gn)
    326  1.1  sjg {
    327  1.1  sjg     extern char **environ;
    328  1.1  sjg     meta_file_t mf;
    329  1.1  sjg     char buf[MAXPATHLEN];
    330  1.1  sjg     char curdir[MAXPATHLEN];
    331  1.1  sjg     char objdir[MAXPATHLEN];
    332  1.1  sjg     char **ptr;
    333  1.1  sjg     const char *cname;
    334  1.1  sjg     const char *dname;
    335  1.1  sjg     const char *tname;
    336  1.1  sjg     char *fname;
    337  1.1  sjg     const char *cp;
    338  1.1  sjg     char *p[4];				/* >= possible uses */
    339  1.1  sjg     int i;
    340  1.1  sjg     struct stat fs;
    341  1.1  sjg 
    342  1.1  sjg 
    343  1.1  sjg     /* This may be a phony node which we don't want meta data for... */
    344  1.1  sjg     /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
    345  1.1  sjg     /* Or it may be explicitly flagged as .NOMETA */
    346  1.1  sjg     SKIP_META_TYPE(NOMETA);
    347  1.1  sjg     /* Unless it is explicitly flagged as .META */
    348  1.1  sjg     if (!(gn->type & OP_META)) {
    349  1.1  sjg 	SKIP_META_TYPE(PHONY);
    350  1.1  sjg 	SKIP_META_TYPE(SPECIAL);
    351  1.1  sjg 	SKIP_META_TYPE(MAKE);
    352  1.1  sjg     }
    353  1.1  sjg 
    354  1.1  sjg     mf.fp = NULL;
    355  1.1  sjg 
    356  1.1  sjg     i = 0;
    357  1.1  sjg 
    358  1.1  sjg     dname = Var_Value(".OBJDIR", gn, &p[i++]);
    359  1.1  sjg     cname = Var_Value(".CURDIR", gn, &p[i++]);
    360  1.1  sjg     tname = Var_Value(TARGET, gn, &p[i++]);
    361  1.1  sjg 
    362  1.1  sjg     /* The object directory may not exist. Check it.. */
    363  1.1  sjg     if (stat(dname, &fs) != 0) {
    364  1.1  sjg 	if (DEBUG(META))
    365  1.1  sjg 	    fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
    366  1.1  sjg 		    gn->name);
    367  1.1  sjg 	goto out;
    368  1.1  sjg     }
    369  1.1  sjg     /* Check if there are no commands to execute. */
    370  1.1  sjg     if (Lst_IsEmpty(gn->commands)) {
    371  1.1  sjg 	if (DEBUG(META))
    372  1.1  sjg 	    fprintf(debug_file, "Skipping meta for %s: no commands\n",
    373  1.1  sjg 		    gn->name);
    374  1.1  sjg 	goto out;
    375  1.1  sjg     }
    376  1.1  sjg 
    377  1.1  sjg     /* make sure these are canonical */
    378  1.1  sjg     if (realpath(dname, objdir))
    379  1.1  sjg 	dname = objdir;
    380  1.1  sjg     if (realpath(cname, curdir))
    381  1.1  sjg 	cname = curdir;
    382  1.1  sjg 
    383  1.1  sjg     /* If we aren't in the object directory, don't create a meta file. */
    384  1.1  sjg     if (strcmp(cname, dname) == 0) {
    385  1.1  sjg 	if (DEBUG(META))
    386  1.1  sjg 	    fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
    387  1.1  sjg 		    gn->name);
    388  1.1  sjg 	goto out;
    389  1.1  sjg     }
    390  1.1  sjg     if (!(gn->type & OP_META)) {
    391  1.1  sjg 	/* We do not generate .meta files for sub-makes */
    392  1.1  sjg 	if (Lst_ForEach(gn->commands, is_submake, gn)) {
    393  1.1  sjg 	    if (DEBUG(META))
    394  1.1  sjg 		fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
    395  1.1  sjg 			gn->name);
    396  1.1  sjg 	    goto out;
    397  1.1  sjg 	}
    398  1.1  sjg     }
    399  1.1  sjg 
    400  1.1  sjg     if (metaVerbose) {
    401  1.1  sjg 	char *mp;
    402  1.1  sjg 
    403  1.1  sjg 	/* Describe the target we are building */
    404  1.1  sjg 	mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
    405  1.1  sjg 	if (*mp)
    406  1.1  sjg 	    fprintf(stdout, "%s\n", mp);
    407  1.1  sjg 	free(mp);
    408  1.1  sjg     }
    409  1.1  sjg     /* Get the basename of the target */
    410  1.1  sjg     if ((cp = strrchr(tname, '/')) == NULL) {
    411  1.1  sjg 	cp = tname;
    412  1.1  sjg     } else {
    413  1.1  sjg 	cp++;
    414  1.1  sjg     }
    415  1.1  sjg 
    416  1.1  sjg     fflush(stdout);
    417  1.1  sjg 
    418  1.1  sjg     if (strcmp(cp, makeDependfile) == 0)
    419  1.1  sjg 	goto out;
    420  1.1  sjg 
    421  1.1  sjg     if (!writeMeta)
    422  1.1  sjg 	/* Don't create meta data. */
    423  1.1  sjg 	goto out;
    424  1.1  sjg 
    425  1.1  sjg     fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
    426  1.1  sjg 		      dname, tname);
    427  1.1  sjg 
    428  1.1  sjg     if ((mf.fp = fopen(fname, "w")) == NULL)
    429  1.1  sjg 	err(1, "Could not open meta file '%s'", fname);
    430  1.1  sjg 
    431  1.1  sjg     fprintf(mf.fp, "# Meta data file %s\n", fname);
    432  1.1  sjg 
    433  1.1  sjg     mf.gn = gn;
    434  1.1  sjg 
    435  1.1  sjg     Lst_ForEach(gn->commands, printCMD, &mf);
    436  1.1  sjg 
    437  1.1  sjg     fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
    438  1.1  sjg     fprintf(mf.fp, "TARGET %s\n", tname);
    439  1.1  sjg 
    440  1.1  sjg     if (metaEnv) {
    441  1.1  sjg 	for (ptr = environ; *ptr != NULL; ptr++)
    442  1.1  sjg 	    fprintf(mf.fp, "ENV %s\n", *ptr);
    443  1.1  sjg     }
    444  1.1  sjg 
    445  1.1  sjg     fprintf(mf.fp, "-- command output --\n");
    446  1.1  sjg     fflush(mf.fp);
    447  1.1  sjg 
    448  1.1  sjg     Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
    449  1.1  sjg     Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
    450  1.1  sjg 
    451  1.1  sjg  out:
    452  1.1  sjg     for (i--; i >= 0; i--) {
    453  1.1  sjg 	if (p[i])
    454  1.1  sjg 	    free(p[i]);
    455  1.1  sjg     }
    456  1.1  sjg 
    457  1.1  sjg     return (mf.fp);
    458  1.1  sjg }
    459  1.1  sjg 
    460  1.1  sjg 
    461  1.1  sjg void
    462  1.1  sjg meta_init(const char *make_mode)
    463  1.1  sjg {
    464  1.1  sjg     static int once = 0;
    465  1.1  sjg 
    466  1.1  sjg     useMeta = TRUE;
    467  1.1  sjg     useFilemon = TRUE;
    468  1.1  sjg     writeMeta = TRUE;
    469  1.1  sjg 
    470  1.1  sjg     if (make_mode) {
    471  1.1  sjg 	if (strstr(make_mode, "env"))
    472  1.1  sjg 	    metaEnv = TRUE;
    473  1.1  sjg 	if (strstr(make_mode, "verb"))
    474  1.1  sjg 	    metaVerbose = TRUE;
    475  1.1  sjg 	if (strstr(make_mode, "read"))
    476  1.1  sjg 	    writeMeta = FALSE;
    477  1.1  sjg 	if (strstr(make_mode, "nofilemon"))
    478  1.1  sjg 	    useFilemon = FALSE;
    479  1.1  sjg 	if (strstr(make_mode, "ignore-cmd"))
    480  1.1  sjg 	    metaIgnoreCMDs = TRUE;
    481  1.1  sjg 	/* for backwards compatability */
    482  1.1  sjg 	Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
    483  1.1  sjg 	Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
    484  1.1  sjg     }
    485  1.1  sjg     if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
    486  1.1  sjg 	/*
    487  1.1  sjg 	 * The default value for MAKE_META_PREFIX
    488  1.1  sjg 	 * prints the absolute path of the target.
    489  1.1  sjg 	 * This works be cause :H will generate '.' if there is no /
    490  1.1  sjg 	 * and :tA will resolve that to cwd.
    491  1.1  sjg 	 */
    492  1.1  sjg 	Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
    493  1.1  sjg     }
    494  1.1  sjg     if (once)
    495  1.1  sjg 	return;
    496  1.1  sjg     once = 1;
    497  1.1  sjg     memset(&Mybm, 0, sizeof(Mybm));
    498  1.1  sjg }
    499  1.1  sjg 
    500  1.1  sjg /*
    501  1.1  sjg  * In each case below we allow for job==NULL
    502  1.1  sjg  */
    503  1.1  sjg void
    504  1.1  sjg meta_job_start(Job *job, GNode *gn)
    505  1.1  sjg {
    506  1.1  sjg     BuildMon *pbm;
    507  1.1  sjg 
    508  1.1  sjg     if (job != NULL) {
    509  1.1  sjg 	pbm = &job->bm;
    510  1.1  sjg     } else {
    511  1.1  sjg 	pbm = &Mybm;
    512  1.1  sjg     }
    513  1.1  sjg     pbm->mfp = meta_create(pbm, gn);
    514  1.1  sjg #ifdef USE_FILEMON_ONCE
    515  1.1  sjg     /* compat mode we open the filemon dev once per command */
    516  1.1  sjg     if (job == NULL)
    517  1.1  sjg 	return;
    518  1.1  sjg #endif
    519  1.1  sjg #ifdef USE_FILEMON
    520  1.1  sjg     if (pbm->mfp != NULL && useFilemon) {
    521  1.1  sjg 	filemon_open(pbm);
    522  1.1  sjg     } else {
    523  1.1  sjg 	pbm->mon_fd = pbm->filemon_fd = -1;
    524  1.1  sjg     }
    525  1.1  sjg #endif
    526  1.1  sjg }
    527  1.1  sjg 
    528  1.1  sjg /*
    529  1.1  sjg  * The child calls this before doing anything.
    530  1.1  sjg  * It does not disturb our state.
    531  1.1  sjg  */
    532  1.1  sjg void
    533  1.1  sjg meta_job_child(Job *job)
    534  1.1  sjg {
    535  1.1  sjg #ifdef USE_FILEMON
    536  1.1  sjg     BuildMon *pbm;
    537  1.1  sjg     pid_t pid;
    538  1.1  sjg 
    539  1.1  sjg     if (job != NULL) {
    540  1.1  sjg 	pbm = &job->bm;
    541  1.1  sjg     } else {
    542  1.1  sjg 	pbm = &Mybm;
    543  1.1  sjg     }
    544  1.1  sjg     pid = getpid();
    545  1.1  sjg     if (pbm->mfp != NULL && useFilemon) {
    546  1.1  sjg 	if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
    547  1.1  sjg 	    err(1, "Could not set filemon pid!");
    548  1.1  sjg 	}
    549  1.1  sjg     }
    550  1.1  sjg #endif
    551  1.1  sjg }
    552  1.1  sjg 
    553  1.1  sjg void
    554  1.1  sjg meta_job_error(Job *job, GNode *gn, int flags, int status)
    555  1.1  sjg {
    556  1.1  sjg     char cwd[MAXPATHLEN];
    557  1.1  sjg     BuildMon *pbm;
    558  1.1  sjg 
    559  1.1  sjg     if (job != NULL) {
    560  1.1  sjg 	pbm = &job->bm;
    561  1.1  sjg     } else {
    562  1.1  sjg 	if (!gn)
    563  1.1  sjg 	    gn = job->node;
    564  1.1  sjg 	pbm = &Mybm;
    565  1.1  sjg     }
    566  1.1  sjg     if (pbm->mfp != NULL) {
    567  1.1  sjg 	fprintf(pbm->mfp, "*** Error code %d%s\n",
    568  1.1  sjg 		status,
    569  1.1  sjg 		(flags & JOB_IGNERR) ?
    570  1.1  sjg 		"(ignored)" : "");
    571  1.1  sjg     }
    572  1.1  sjg     if (gn) {
    573  1.1  sjg 	Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
    574  1.1  sjg     }
    575  1.1  sjg     getcwd(cwd, sizeof(cwd));
    576  1.1  sjg     Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
    577  1.6  sjg     if (pbm && pbm->meta_fname[0]) {
    578  1.1  sjg 	Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
    579  1.1  sjg     }
    580  1.1  sjg }
    581  1.1  sjg 
    582  1.1  sjg void
    583  1.1  sjg meta_job_output(Job *job, char *cp, const char *nl)
    584  1.1  sjg {
    585  1.1  sjg     BuildMon *pbm;
    586  1.1  sjg 
    587  1.1  sjg     if (job != NULL) {
    588  1.1  sjg 	pbm = &job->bm;
    589  1.1  sjg     } else {
    590  1.1  sjg 	pbm = &Mybm;
    591  1.1  sjg     }
    592  1.1  sjg     if (pbm->mfp != NULL) {
    593  1.1  sjg 	if (metaVerbose) {
    594  1.1  sjg 	    static char *meta_prefix = NULL;
    595  1.1  sjg 	    static int meta_prefix_len;
    596  1.1  sjg 
    597  1.1  sjg 	    if (!meta_prefix) {
    598  1.1  sjg 		char *cp2;
    599  1.1  sjg 
    600  1.1  sjg 		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
    601  1.1  sjg 		if ((cp2 = strchr(meta_prefix, '$')))
    602  1.1  sjg 		    meta_prefix_len = cp2 - meta_prefix;
    603  1.1  sjg 		else
    604  1.1  sjg 		    meta_prefix_len = strlen(meta_prefix);
    605  1.1  sjg 	    }
    606  1.1  sjg 	    if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
    607  1.1  sjg 		cp = strchr(cp+1, '\n');
    608  1.1  sjg 		if (!cp++)
    609  1.1  sjg 		    return;
    610  1.1  sjg 	    }
    611  1.1  sjg 	}
    612  1.1  sjg 	fprintf(pbm->mfp, "%s%s", cp, nl);
    613  1.1  sjg     }
    614  1.1  sjg }
    615  1.1  sjg 
    616  1.1  sjg void
    617  1.1  sjg meta_cmd_finish(void *pbmp)
    618  1.1  sjg {
    619  1.1  sjg #ifdef USE_FILEMON
    620  1.1  sjg     BuildMon *pbm = pbmp;
    621  1.1  sjg 
    622  1.1  sjg     if (!pbm)
    623  1.1  sjg 	pbm = &Mybm;
    624  1.1  sjg 
    625  1.1  sjg     if (pbm->filemon_fd >= 0) {
    626  1.1  sjg 	close(pbm->filemon_fd);
    627  1.1  sjg 	filemon_read(pbm->mfp, pbm->mon_fd);
    628  1.1  sjg 	pbm->filemon_fd = pbm->mon_fd = -1;
    629  1.1  sjg     }
    630  1.1  sjg #endif
    631  1.1  sjg }
    632  1.1  sjg 
    633  1.1  sjg void
    634  1.1  sjg meta_job_finish(Job *job)
    635  1.1  sjg {
    636  1.1  sjg     BuildMon *pbm;
    637  1.1  sjg 
    638  1.1  sjg     if (job != NULL) {
    639  1.1  sjg 	pbm = &job->bm;
    640  1.1  sjg     } else {
    641  1.1  sjg 	pbm = &Mybm;
    642  1.1  sjg     }
    643  1.1  sjg     if (pbm->mfp != NULL) {
    644  1.1  sjg 	meta_cmd_finish(pbm);
    645  1.1  sjg 	fclose(pbm->mfp);
    646  1.1  sjg 	pbm->mfp = NULL;
    647  1.6  sjg 	pbm->meta_fname[0] = '\0';
    648  1.1  sjg     }
    649  1.1  sjg }
    650  1.1  sjg 
    651  1.1  sjg /*
    652  1.1  sjg  * Fetch a full line from fp - growing bufp if needed
    653  1.1  sjg  * Return length in bufp.
    654  1.1  sjg  */
    655  1.1  sjg static int
    656  1.1  sjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
    657  1.1  sjg {
    658  1.1  sjg     char *buf = *bufp;
    659  1.1  sjg     size_t bufsz = *szp;
    660  1.1  sjg     struct stat fs;
    661  1.1  sjg     int x;
    662  1.1  sjg 
    663  1.1  sjg     if (fgets(&buf[o], bufsz - o, fp) != NULL) {
    664  1.1  sjg     check_newline:
    665  1.1  sjg 	x = o + strlen(&buf[o]);
    666  1.1  sjg 	if (buf[x - 1] == '\n')
    667  1.1  sjg 	    return x;
    668  1.1  sjg 	/*
    669  1.1  sjg 	 * We need to grow the buffer.
    670  1.1  sjg 	 * The meta file can give us a clue.
    671  1.1  sjg 	 */
    672  1.1  sjg 	if (fstat(fileno(fp), &fs) == 0) {
    673  1.1  sjg 	    size_t newsz;
    674  1.1  sjg 	    char *p;
    675  1.1  sjg 
    676  1.1  sjg 	    newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
    677  1.1  sjg 	    if (newsz <= bufsz)
    678  1.1  sjg 		newsz = ROUNDUP(fs.st_size, BUFSIZ);
    679  1.1  sjg 	    if (DEBUG(META))
    680  1.1  sjg 		fprintf(debug_file, "growing buffer %u -> %u\n",
    681  1.1  sjg 			bufsz, newsz);
    682  1.1  sjg 	    p = bmake_realloc(buf, newsz);
    683  1.1  sjg 	    if (p) {
    684  1.1  sjg 		*bufp = buf = p;
    685  1.1  sjg 		*szp = bufsz = newsz;
    686  1.1  sjg 		/* fetch the rest */
    687  1.1  sjg 		if (!fgets(&buf[x], bufsz - x, fp))
    688  1.1  sjg 		    return x;		/* truncated! */
    689  1.1  sjg 		goto check_newline;
    690  1.1  sjg 	    }
    691  1.1  sjg 	}
    692  1.1  sjg     }
    693  1.1  sjg     return 0;
    694  1.1  sjg }
    695  1.1  sjg 
    696  1.1  sjg /*
    697  1.1  sjg  * When running with 'meta' functionality, a target can be out-of-date
    698  1.1  sjg  * if any of the references in it's meta data file is more recent.
    699  1.5  sjg  * We have to track the latestdir on a per-process basis.
    700  1.1  sjg  */
    701  1.5  sjg #define LDIR_VNAME_FMT ".meta.%d.ldir"
    702  1.5  sjg 
    703  1.1  sjg Boolean
    704  1.1  sjg meta_oodate(GNode *gn, Boolean oodate)
    705  1.1  sjg {
    706  1.5  sjg     static char *tmpdir = NULL;
    707  1.5  sjg     char ldir_vname[64];
    708  1.5  sjg     char cwd[MAXPATHLEN];
    709  1.1  sjg     char latestdir[MAXPATHLEN];
    710  1.1  sjg     char fname[MAXPATHLEN];
    711  1.1  sjg     char fname1[MAXPATHLEN];
    712  1.5  sjg     char fname2[MAXPATHLEN];
    713  1.1  sjg     char *p;
    714  1.1  sjg     char *cp;
    715  1.5  sjg     size_t cwdlen;
    716  1.5  sjg     size_t tmplen = 0;
    717  1.1  sjg     FILE *fp;
    718  1.1  sjg     Boolean ignoreOODATE = FALSE;
    719  1.1  sjg 
    720  1.4  sjg     if (oodate)
    721  1.4  sjg 	return oodate;		/* we're done */
    722  1.4  sjg 
    723  1.1  sjg     /*
    724  1.1  sjg      * We need to check if the target is out-of-date. This includes
    725  1.1  sjg      * checking if the expanded command has changed. This in turn
    726  1.1  sjg      * requires that all variables are set in the same way that they
    727  1.1  sjg      * would be if the target needs to be re-built.
    728  1.1  sjg      */
    729  1.1  sjg     Make_DoAllVar(gn);
    730  1.1  sjg 
    731  1.5  sjg     if (getcwd(cwd, sizeof(cwd)) == NULL)
    732  1.1  sjg 	err(1, "Could not get current working directory");
    733  1.1  sjg 
    734  1.1  sjg     meta_name(gn, fname, sizeof(fname), NULL, NULL);
    735  1.1  sjg 
    736  1.1  sjg     if ((fp = fopen(fname, "r")) != NULL) {
    737  1.1  sjg 	static char *buf = NULL;
    738  1.1  sjg 	static size_t bufsz;
    739  1.1  sjg 	int lineno = 0;
    740  1.5  sjg 	int lastpid = 0;
    741  1.5  sjg 	int pid;
    742  1.1  sjg 	int f = 0;
    743  1.1  sjg 	int x;
    744  1.1  sjg 	LstNode ln;
    745  1.1  sjg 	struct stat fs;
    746  1.1  sjg 
    747  1.1  sjg 	if (!buf) {
    748  1.1  sjg 	    bufsz = 8 * BUFSIZ;
    749  1.1  sjg 	    buf = bmake_malloc(bufsz);
    750  1.1  sjg 	}
    751  1.5  sjg 
    752  1.5  sjg 	if (!tmpdir) {
    753  1.5  sjg 	    tmpdir = getTmpdir();
    754  1.5  sjg 	    tmplen = strlen(tmpdir);
    755  1.5  sjg 	}
    756  1.5  sjg 
    757  1.1  sjg 	/* we want to track all the .meta we read */
    758  1.1  sjg 	Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
    759  1.1  sjg 
    760  1.5  sjg 	cwdlen = strlen(cwd);
    761  1.5  sjg 
    762  1.1  sjg 	ln = Lst_First(gn->commands);
    763  1.1  sjg 	while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
    764  1.1  sjg 	    lineno++;
    765  1.1  sjg 	    if (buf[x - 1] == '\n')
    766  1.1  sjg 		buf[x - 1] = '\0';
    767  1.1  sjg 	    else
    768  1.1  sjg 		warnx("%s: %d: line truncated at %u", fname, lineno, x);
    769  1.1  sjg 
    770  1.1  sjg 	    /* Find the start of the build monitor section. */
    771  1.1  sjg 	    if (!f) {
    772  1.1  sjg 		if (strncmp(buf, "-- filemon", 10) == 0) {
    773  1.1  sjg 		    f = 1;
    774  1.1  sjg 		    continue;
    775  1.1  sjg 		}
    776  1.1  sjg 		if (strncmp(buf, "# buildmon", 10) == 0) {
    777  1.1  sjg 		    f = 1;
    778  1.1  sjg 		    continue;
    779  1.1  sjg 		}
    780  1.1  sjg 	    }
    781  1.1  sjg 
    782  1.1  sjg 	    /* Delimit the record type. */
    783  1.1  sjg 	    p = buf;
    784  1.1  sjg 	    strsep(&p, " ");
    785  1.1  sjg 	    if (f) {
    786  1.5  sjg 		/*
    787  1.5  sjg 		 * We are in the 'filemon' output section.
    788  1.5  sjg 		 * Each record from filemon follows the general form:
    789  1.5  sjg 		 *
    790  1.5  sjg 		 * <key> <pid> <data>
    791  1.5  sjg 		 *
    792  1.5  sjg 		 * Where:
    793  1.5  sjg 		 * <key> is a single letter, denoting the syscall.
    794  1.5  sjg 		 * <pid> is the process that made the syscall.
    795  1.5  sjg 		 * <data> is the arguments (of interest).
    796  1.5  sjg 		 */
    797  1.5  sjg 		switch(buf[0]) {
    798  1.5  sjg 		case '#':		/* comment */
    799  1.5  sjg 		case 'V':		/* version */
    800  1.5  sjg 		    break;
    801  1.5  sjg 		default:
    802  1.5  sjg 		    /*
    803  1.5  sjg 		     * We need to track pathnames per-process.
    804  1.5  sjg 		     *
    805  1.5  sjg 		     * Each process run by make, starts off in the 'CWD'
    806  1.5  sjg 		     * recorded in the .meta file, if it chdirs ('C')
    807  1.5  sjg 		     * elsewhere we need to track that - but only for
    808  1.5  sjg 		     * that process.  If it forks ('F'), we initialize
    809  1.5  sjg 		     * the child to have the same cwd as its parent.
    810  1.5  sjg 		     *
    811  1.5  sjg 		     * We also need to track the 'latestdir' of
    812  1.5  sjg 		     * interest.  This is usually the same as cwd, but
    813  1.5  sjg 		     * not if a process is reading directories.
    814  1.5  sjg 		     *
    815  1.5  sjg 		     * Each time we spot a different process ('pid')
    816  1.5  sjg 		     * we save the current value of 'latestdir' in a
    817  1.5  sjg 		     * variable qualified by 'lastpid', and
    818  1.5  sjg 		     * re-initialize 'latestdir' to any pre-saved
    819  1.5  sjg 		     * value for the current 'pid' and 'CWD' if none.
    820  1.5  sjg 		     */
    821  1.5  sjg 		    pid = atoi(p);
    822  1.5  sjg 		    if (pid > 0 && pid != lastpid) {
    823  1.5  sjg 			char *ldir;
    824  1.5  sjg 			char *tp;
    825  1.5  sjg 
    826  1.5  sjg 			if (lastpid > 0) {
    827  1.5  sjg 			    /* We need to remember this. */
    828  1.5  sjg 			    Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
    829  1.5  sjg 			}
    830  1.5  sjg 			snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
    831  1.5  sjg 			lastpid = pid;
    832  1.5  sjg 			ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
    833  1.5  sjg 			if (ldir) {
    834  1.5  sjg 			    strlcpy(latestdir, ldir, sizeof(latestdir));
    835  1.5  sjg 			    if (tp)
    836  1.5  sjg 				free(tp);
    837  1.5  sjg 			} else
    838  1.5  sjg 			    strlcpy(latestdir, cwd, sizeof(latestdir));
    839  1.5  sjg 		    }
    840  1.5  sjg 		    /* Skip past the pid. */
    841  1.5  sjg 		    if (strsep(&p, " ") == NULL)
    842  1.5  sjg 			continue;
    843  1.5  sjg 		    break;
    844  1.5  sjg 		}
    845  1.5  sjg 
    846  1.1  sjg 		/* Process according to record type. */
    847  1.1  sjg 		switch (buf[0]) {
    848  1.5  sjg 		case 'X':		/* eXit */
    849  1.5  sjg 		    Var_Delete(ldir_vname, VAR_GLOBAL);
    850  1.5  sjg 		    lastpid = 0;	/* no need to save ldir_vname */
    851  1.5  sjg 		    break;
    852  1.5  sjg 
    853  1.5  sjg 		case 'F':		/* [v]Fork */
    854  1.5  sjg 		    {
    855  1.5  sjg 			char cldir[64];
    856  1.5  sjg 			int child;
    857  1.5  sjg 
    858  1.5  sjg 			child = atoi(p);
    859  1.5  sjg 			if (child > 0) {
    860  1.5  sjg 			    snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
    861  1.5  sjg 			    Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
    862  1.5  sjg 			}
    863  1.5  sjg 		    }
    864  1.5  sjg 		    break;
    865  1.1  sjg 
    866  1.5  sjg 		case 'C':		/* Chdir */
    867  1.1  sjg 		    /* Update the latest directory. */
    868  1.1  sjg 		    strlcpy(latestdir, p, sizeof(latestdir));
    869  1.1  sjg 		    break;
    870  1.1  sjg 
    871  1.5  sjg 		case 'R':		/* Read */
    872  1.5  sjg 		case 'E':		/* Exec */
    873  1.1  sjg 		    /*
    874  1.1  sjg 		     * Check for runtime files that can't
    875  1.1  sjg 		     * be part of the dependencies because
    876  1.1  sjg 		     * they are _expected_ to change.
    877  1.1  sjg 		     */
    878  1.5  sjg 		    if (strncmp(p, "/tmp/", 5) == 0 ||
    879  1.5  sjg 			strncmp(p, tmpdir, tmplen) == 0)
    880  1.5  sjg 			break;
    881  1.5  sjg 
    882  1.1  sjg 		    if (strncmp(p, "/var/", 5) == 0)
    883  1.1  sjg 			break;
    884  1.1  sjg 
    885  1.1  sjg 		    /* Ignore device files. */
    886  1.1  sjg 		    if (strncmp(p, "/dev/", 5) == 0)
    887  1.1  sjg 			break;
    888  1.1  sjg 
    889  1.1  sjg 		    /* Ignore /etc/ files. */
    890  1.1  sjg 		    if (strncmp(p, "/etc/", 5) == 0)
    891  1.1  sjg 			break;
    892  1.1  sjg 
    893  1.1  sjg 		    /*
    894  1.5  sjg 		     * The rest of the record is the file name.
    895  1.5  sjg 		     * Check if it's not an absolute path.
    896  1.1  sjg 		     */
    897  1.5  sjg 		    {
    898  1.5  sjg 			char *sdirs[4];
    899  1.5  sjg 			char **sdp;
    900  1.5  sjg 			int sdx = 0;
    901  1.5  sjg 			int found = 0;
    902  1.5  sjg 
    903  1.5  sjg 			if (*p == '/') {
    904  1.5  sjg 			    sdirs[sdx++] = p; /* done */
    905  1.5  sjg 			} else {
    906  1.5  sjg 			    if (strcmp(".", p) == 0)
    907  1.5  sjg 				continue;  /* no point */
    908  1.5  sjg 
    909  1.5  sjg 			    /* Check vs latestdir */
    910  1.5  sjg 			    snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
    911  1.5  sjg 			    sdirs[sdx++] = fname1;
    912  1.5  sjg 
    913  1.5  sjg 			    if (strcmp(latestdir, cwd) != 0) {
    914  1.5  sjg 				/* Check vs cwd */
    915  1.5  sjg 				snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
    916  1.5  sjg 				sdirs[sdx++] = fname2;
    917  1.5  sjg 			    }
    918  1.5  sjg 			}
    919  1.5  sjg 			sdirs[sdx++] = NULL;
    920  1.1  sjg 
    921  1.5  sjg 			for (sdp = sdirs; *sdp && !found; sdp++) {
    922  1.5  sjg 			    if (stat(*sdp, &fs) == 0) {
    923  1.5  sjg 				found = 1;
    924  1.5  sjg 				p = *sdp;
    925  1.5  sjg 			    }
    926  1.5  sjg 			}
    927  1.5  sjg 			if (found) {
    928  1.5  sjg 			    if (!S_ISDIR(fs.st_mode) &&
    929  1.5  sjg 				fs.st_mtime > gn->mtime) {
    930  1.5  sjg 				if (DEBUG(META))
    931  1.5  sjg 				    fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
    932  1.5  sjg 				oodate = TRUE;
    933  1.5  sjg 			    } else if (S_ISDIR(fs.st_mode)) {
    934  1.5  sjg 				/* Update the latest directory. */
    935  1.5  sjg 				realpath(p, latestdir);
    936  1.5  sjg 			    }
    937  1.5  sjg 			} else if (errno == ENOENT && *p == '/' &&
    938  1.5  sjg 				   strncmp(p, cwd, cwdlen) != 0) {
    939  1.5  sjg 			    /*
    940  1.5  sjg 			     * A referenced file outside of CWD is missing.
    941  1.5  sjg 			     * We cannot catch every eventuality here...
    942  1.5  sjg 			     */
    943  1.4  sjg 			    if (DEBUG(META))
    944  1.5  sjg 				fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
    945  1.4  sjg 			    oodate = TRUE;
    946  1.4  sjg 			}
    947  1.1  sjg 		    }
    948  1.1  sjg 		    break;
    949  1.1  sjg 		default:
    950  1.1  sjg 		    break;
    951  1.1  sjg 		}
    952  1.5  sjg 	    } else if (strcmp(buf, "CMD") == 0) {
    953  1.1  sjg 		/*
    954  1.1  sjg 		 * Compare the current command with the one in the
    955  1.1  sjg 		 * meta data file.
    956  1.1  sjg 		 */
    957  1.1  sjg 		if (ln == NULL) {
    958  1.1  sjg 		    if (DEBUG(META))
    959  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);
    960  1.1  sjg 		    oodate = TRUE;
    961  1.1  sjg 		} else {
    962  1.1  sjg 		    char *cmd = (char *)Lst_Datum(ln);
    963  1.1  sjg 
    964  1.1  sjg 		    if (!ignoreOODATE) {
    965  1.1  sjg 			if (strstr(cmd, "$?"))
    966  1.1  sjg 			    ignoreOODATE = TRUE;
    967  1.1  sjg 			else if ((cp = strstr(cmd, ".OODATE"))) {
    968  1.1  sjg 			    /* check for $[{(].OODATE[)}] */
    969  1.1  sjg 			    if (cp > cmd + 2 && cp[-2] == '$')
    970  1.1  sjg 				ignoreOODATE = TRUE;
    971  1.1  sjg 			}
    972  1.1  sjg 			if (ignoreOODATE && DEBUG(META))
    973  1.1  sjg 			    fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
    974  1.1  sjg 		    }
    975  1.1  sjg 		    cmd = Var_Subst(NULL, cmd, gn, TRUE);
    976  1.1  sjg 
    977  1.1  sjg 		    if ((cp = strchr(cmd, '\n'))) {
    978  1.1  sjg 			int n;
    979  1.1  sjg 
    980  1.1  sjg 			/*
    981  1.1  sjg 			 * This command contains newlines, we need to
    982  1.1  sjg 			 * fetch more from the .meta file before we
    983  1.1  sjg 			 * attempt a comparison.
    984  1.1  sjg 			 */
    985  1.1  sjg 			/* first put the newline back at buf[x - 1] */
    986  1.1  sjg 			buf[x - 1] = '\n';
    987  1.1  sjg 			do {
    988  1.1  sjg 			    /* now fetch the next line */
    989  1.1  sjg 			    if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
    990  1.1  sjg 				break;
    991  1.1  sjg 			    x = n;
    992  1.1  sjg 			    lineno++;
    993  1.1  sjg 			    if (buf[x - 1] != '\n') {
    994  1.1  sjg 				warnx("%s: %d: line truncated at %u", fname, lineno, x);
    995  1.1  sjg 				break;
    996  1.1  sjg 			    }
    997  1.1  sjg 			    cp = strchr(++cp, '\n');
    998  1.1  sjg 			} while (cp);
    999  1.1  sjg 			if (buf[x - 1] == '\n')
   1000  1.1  sjg 			    buf[x - 1] = '\0';
   1001  1.1  sjg 		    }
   1002  1.1  sjg 		    if (!ignoreOODATE &&
   1003  1.1  sjg 			!(gn->type & OP_NOMETA_CMP) &&
   1004  1.1  sjg 			strcmp(p, cmd) != 0) {
   1005  1.1  sjg 			if (DEBUG(META))
   1006  1.1  sjg 			    fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
   1007  1.1  sjg 			if (!metaIgnoreCMDs)
   1008  1.1  sjg 			    oodate = TRUE;
   1009  1.1  sjg 		    }
   1010  1.1  sjg 		    free(cmd);
   1011  1.1  sjg 		    ln = Lst_Succ(ln);
   1012  1.1  sjg 		}
   1013  1.1  sjg 	    } else if (strcmp(buf, "CWD") == 0) {
   1014  1.1  sjg 		char curdir[MAXPATHLEN];
   1015  1.1  sjg 		if (strcmp(p, getcwd(curdir, sizeof(curdir))) != 0) {
   1016  1.1  sjg 		    if (DEBUG(META))
   1017  1.1  sjg 			fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
   1018  1.1  sjg 		    oodate = TRUE;
   1019  1.1  sjg 		}
   1020  1.1  sjg 	    }
   1021  1.1  sjg 	}
   1022  1.1  sjg 
   1023  1.1  sjg 	/*
   1024  1.1  sjg 	 * Check if there are extra commands now
   1025  1.1  sjg 	 * that weren't in the meta data file.
   1026  1.1  sjg 	 */
   1027  1.1  sjg 	if (!oodate && ln != NULL) {
   1028  1.1  sjg 	    if (DEBUG(META))
   1029  1.1  sjg 		fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
   1030  1.1  sjg 	    oodate = TRUE;
   1031  1.1  sjg 	}
   1032  1.1  sjg 
   1033  1.1  sjg 	fclose(fp);
   1034  1.1  sjg     }
   1035  1.4  sjg     if (oodate && ignoreOODATE) {
   1036  1.4  sjg 	/*
   1037  1.4  sjg 	 * Target uses .OODATE, so we need to re-compute it.
   1038  1.4  sjg 	 * We need to clean up what Make_DoAllVar() did.
   1039  1.4  sjg 	 */
   1040  1.4  sjg 	Var_Delete(ALLSRC, gn);
   1041  1.4  sjg 	Var_Delete(OODATE, gn);
   1042  1.4  sjg 	gn->flags &= ~DONE_ALLSRC;
   1043  1.4  sjg     }
   1044  1.1  sjg     return oodate;
   1045  1.1  sjg }
   1046  1.1  sjg 
   1047  1.1  sjg /* support for compat mode */
   1048  1.1  sjg 
   1049  1.1  sjg static int childPipe[2];
   1050  1.1  sjg 
   1051  1.1  sjg void
   1052  1.1  sjg meta_compat_start(void)
   1053  1.1  sjg {
   1054  1.1  sjg #ifdef USE_FILEMON_ONCE
   1055  1.1  sjg     /*
   1056  1.1  sjg      * We need to re-open filemon for each cmd.
   1057  1.1  sjg      */
   1058  1.1  sjg     BuildMon *pbm = &Mybm;
   1059  1.1  sjg 
   1060  1.1  sjg     if (pbm->mfp != NULL && useFilemon) {
   1061  1.1  sjg 	filemon_open(pbm);
   1062  1.1  sjg     } else {
   1063  1.1  sjg 	pbm->mon_fd = pbm->filemon_fd = -1;
   1064  1.1  sjg     }
   1065  1.1  sjg #endif
   1066  1.1  sjg     if (pipe(childPipe) < 0)
   1067  1.1  sjg 	Punt("Cannot create pipe: %s", strerror(errno));
   1068  1.1  sjg     /* Set close-on-exec flag for both */
   1069  1.1  sjg     (void)fcntl(childPipe[0], F_SETFD, 1);
   1070  1.1  sjg     (void)fcntl(childPipe[1], F_SETFD, 1);
   1071  1.1  sjg }
   1072  1.1  sjg 
   1073  1.1  sjg void
   1074  1.1  sjg meta_compat_child(void)
   1075  1.1  sjg {
   1076  1.1  sjg     meta_job_child(NULL);
   1077  1.1  sjg     if (dup2(childPipe[1], 1) < 0 ||
   1078  1.1  sjg 	dup2(1, 2) < 0) {
   1079  1.1  sjg 	execError("dup2", "pipe");
   1080  1.1  sjg 	_exit(1);
   1081  1.1  sjg     }
   1082  1.1  sjg }
   1083  1.1  sjg 
   1084  1.1  sjg void
   1085  1.1  sjg meta_compat_parent(void)
   1086  1.1  sjg {
   1087  1.1  sjg     FILE *fp;
   1088  1.1  sjg     char buf[BUFSIZ];
   1089  1.1  sjg 
   1090  1.1  sjg     close(childPipe[1]);			/* child side */
   1091  1.1  sjg     fp = fdopen(childPipe[0], "r");
   1092  1.1  sjg     while (fgets(buf, sizeof(buf), fp)) {
   1093  1.1  sjg 	meta_job_output(NULL, buf, "");
   1094  1.1  sjg 	printf("%s", buf);
   1095  1.1  sjg     }
   1096  1.1  sjg     fclose(fp);
   1097  1.1  sjg }
   1098  1.3  sjg 
   1099  1.3  sjg #endif	/* USE_META */
   1100