1 /* $NetBSD: meta.c,v 1.32 2013/06/25 00:20:54 sjg Exp $ */
4 * Implement 'meta' mode.
5 * Adapted from John Birrell's patches to FreeBSD make.
9 * Copyright (c) 2009-2010, Juniper Networks, Inc.
10 * Portions Copyright (c) 2009, John Birrell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/ioctl.h>
43 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
53 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
57 static BuildMon Mybm; /* for compat */
58 static Lst metaBailiwick; /* our scope of control */
59 static Lst metaIgnorePaths; /* paths we deliberately ignore */
61 #ifndef MAKE_META_IGNORE_PATHS
62 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
65 Boolean useMeta = FALSE;
66 static Boolean useFilemon = FALSE;
67 static Boolean writeMeta = FALSE;
68 static Boolean metaEnv = FALSE; /* don't save env unless asked */
69 static Boolean metaVerbose = FALSE;
70 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
71 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
72 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */
74 extern Boolean forceJobs;
75 extern Boolean comatMake;
76 extern char **environ;
78 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
81 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
84 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
87 #if !defined(HAVE_STRSEP)
88 # define strsep(s, d) stresep((s), (d), 0)
92 * Filemon is a kernel module which snoops certain syscalls.
103 * See meta_oodate below - we mainly care about 'E' and 'R'.
105 * We can still use meta mode without filemon, but
106 * the benefits are more limited.
109 # ifndef _PATH_FILEMON
110 # define _PATH_FILEMON "/dev/filemon"
114 * Open the filemon device.
117 filemon_open(BuildMon *pbm)
121 pbm->mon_fd = pbm->filemon_fd = -1;
125 for (retry = 5; retry >= 0; retry--) {
126 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
130 if (pbm->filemon_fd < 0) {
132 warn("Could not open %s", _PATH_FILEMON);
137 * We use a file outside of '.'
138 * to avoid a FreeBSD kernel bug where unlink invalidates
139 * cwd causing getcwd to do a lot more work.
140 * We only care about the descriptor.
142 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
143 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
144 err(1, "Could not set filemon file descriptor!");
146 /* we don't need these once we exec */
147 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
148 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
152 * Read the build monitor output file and write records to the target's
156 filemon_read(FILE *mfp, int fd)
161 /* Check if we're not writing to a meta data file.*/
164 close(fd); /* not interested */
168 (void)lseek(fd, (off_t)0, SEEK_SET);
169 if ((fp = fdopen(fd, "r")) == NULL)
170 err(1, "Could not read build monitor file '%d'", fd);
172 fprintf(mfp, "-- filemon acquired metadata --\n");
174 while (fgets(buf, sizeof(buf), fp)) {
175 fprintf(mfp, "%s", buf);
184 * when realpath() fails,
185 * we use this, to clean up ./ and ../
188 eat_dots(char *buf, size_t bufsz, int dots)
209 cp = strstr(buf, eat);
212 if (dots == 2 && cp > buf) {
215 } while (cp > buf && *cp != '/');
218 strlcpy(cp, cp2, bufsz - (cp - buf));
220 return; /* can't happen? */
227 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
231 char buf[MAXPATHLEN];
232 char cwd[MAXPATHLEN];
236 char *p[4]; /* >= number of possible uses */
241 dname = Var_Value(".OBJDIR", gn, &p[i++]);
243 tname = Var_Value(TARGET, gn, &p[i++]);
245 if (realpath(dname, cwd))
249 * Weed out relative paths from the target file name.
250 * We have to be careful though since if target is a
251 * symlink, the result will be unstable.
252 * So we use realpath() just to get the dirname, and leave the
253 * basename as given to us.
255 if ((cp = strrchr(tname, '/'))) {
256 if (realpath(tname, buf)) {
257 if ((rp = strrchr(buf, '/'))) {
260 if (strcmp(cp, rp) != 0)
261 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
266 * We likely have a directory which is about to be made.
267 * We pretend realpath() succeeded, to have a chance
268 * of generating the same meta file name that we will
271 if (tname[0] == '/') {
272 strlcpy(buf, tname, sizeof(buf));
274 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
276 eat_dots(buf, sizeof(buf), 1); /* ./ */
277 eat_dots(buf, sizeof(buf), 2); /* ../ */
281 /* on some systems dirname may modify its arg */
282 tp = bmake_strdup(tname);
283 if (strcmp(dname, dirname(tp)) == 0)
284 snprintf(mname, mnamelen, "%s.meta", tname);
286 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
289 * Replace path separators in the file name after the
290 * current object directory path.
292 cp = mname + strlen(dname) + 1;
294 while (*cp != '\0') {
301 for (i--; i >= 0; i--) {
309 * Return true if running ${.MAKE}
310 * Bypassed if target is flagged .MAKE
313 is_submake(void *cmdp, void *gnp)
315 static char *p_make = NULL;
322 int rc = 0; /* keep looking */
325 p_make = Var_Value(".MAKE", gn, &cp);
326 p_len = strlen(p_make);
328 cp = strchr(cmd, '$');
330 mp = Var_Subst(NULL, cmd, gn, FALSE);
333 cp2 = strstr(cmd, p_make);
335 switch (cp2[p_len]) {
343 if (cp2 > cmd && rc > 0) {
350 rc = 0; /* no match */
360 typedef struct meta_file_s {
366 printCMD(void *cmdp, void *mfpp)
368 meta_file_t *mfp = mfpp;
372 if (strchr(cmd, '$')) {
373 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
375 fprintf(mfp->fp, "CMD %s\n", cmd);
382 * Certain node types never get a .meta file
384 #define SKIP_META_TYPE(_type) do { \
385 if ((gn->type & __CONCAT(OP_, _type))) { \
387 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
388 gn->name, __STRING(_type)); \
395 meta_create(BuildMon *pbm, GNode *gn)
398 char buf[MAXPATHLEN];
399 char objdir[MAXPATHLEN];
405 char *p[4]; /* >= possible uses */
410 /* This may be a phony node which we don't want meta data for... */
411 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
412 /* Or it may be explicitly flagged as .NOMETA */
413 SKIP_META_TYPE(NOMETA);
414 /* Unless it is explicitly flagged as .META */
415 if (!(gn->type & OP_META)) {
416 SKIP_META_TYPE(PHONY);
417 SKIP_META_TYPE(SPECIAL);
418 SKIP_META_TYPE(MAKE);
425 dname = Var_Value(".OBJDIR", gn, &p[i++]);
426 tname = Var_Value(TARGET, gn, &p[i++]);
428 /* The object directory may not exist. Check it.. */
429 if (stat(dname, &fs) != 0) {
431 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
435 /* Check if there are no commands to execute. */
436 if (Lst_IsEmpty(gn->commands)) {
438 fprintf(debug_file, "Skipping meta for %s: no commands\n",
443 /* make sure these are canonical */
444 if (realpath(dname, objdir))
447 /* If we aren't in the object directory, don't create a meta file. */
448 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
450 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
454 if (!(gn->type & OP_META)) {
455 /* We do not generate .meta files for sub-makes */
456 if (Lst_ForEach(gn->commands, is_submake, gn)) {
458 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
467 /* Describe the target we are building */
468 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
470 fprintf(stdout, "%s\n", mp);
473 /* Get the basename of the target */
474 if ((cp = strrchr(tname, '/')) == NULL) {
482 if (strcmp(cp, makeDependfile) == 0)
486 /* Don't create meta data. */
489 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
492 #ifdef DEBUG_META_MODE
494 fprintf(debug_file, "meta_create: %s\n", fname);
497 if ((mf.fp = fopen(fname, "w")) == NULL)
498 err(1, "Could not open meta file '%s'", fname);
500 fprintf(mf.fp, "# Meta data file %s\n", fname);
504 Lst_ForEach(gn->commands, printCMD, &mf);
506 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
507 fprintf(mf.fp, "TARGET %s\n", tname);
510 for (ptr = environ; *ptr != NULL; ptr++)
511 fprintf(mf.fp, "ENV %s\n", *ptr);
514 fprintf(mf.fp, "-- command output --\n");
517 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
518 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
520 gn->type |= OP_META; /* in case anyone wants to know */
522 gn->type |= OP_SILENT;
525 for (i--; i >= 0; i--) {
548 * Initialization we need before reading makefiles.
554 /* this allows makefiles to test if we have filemon support */
555 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0);
561 * Initialization we need after reading makefiles.
564 meta_mode_init(const char *make_mode)
574 if (strstr(make_mode, "env"))
576 if (strstr(make_mode, "verb"))
578 if (strstr(make_mode, "read"))
580 if (strstr(make_mode, "nofilemon"))
582 if ((cp = strstr(make_mode, "curdirok="))) {
583 metaCurdirOk = boolValue(&cp[9]);
585 if ((cp = strstr(make_mode, "silent="))) {
586 metaSilent = boolValue(&cp[7]);
588 if (strstr(make_mode, "ignore-cmd"))
589 metaIgnoreCMDs = TRUE;
590 /* for backwards compatability */
591 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
592 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
594 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
596 * The default value for MAKE_META_PREFIX
597 * prints the absolute path of the target.
598 * This works be cause :H will generate '.' if there is no /
599 * and :tA will resolve that to cwd.
601 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
606 memset(&Mybm, 0, sizeof(Mybm));
608 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
610 metaBailiwick = Lst_Init(FALSE);
611 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
613 str2Lst_Append(metaBailiwick, cp, NULL);
616 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
618 metaIgnorePaths = Lst_Init(FALSE);
619 Var_Append(MAKE_META_IGNORE_PATHS,
620 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
622 "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL, 0);
624 str2Lst_Append(metaIgnorePaths, cp, NULL);
629 * In each case below we allow for job==NULL
632 meta_job_start(Job *job, GNode *gn)
641 pbm->mfp = meta_create(pbm, gn);
642 #ifdef USE_FILEMON_ONCE
643 /* compat mode we open the filemon dev once per command */
648 if (pbm->mfp != NULL && useFilemon) {
651 pbm->mon_fd = pbm->filemon_fd = -1;
657 * The child calls this before doing anything.
658 * It does not disturb our state.
661 meta_job_child(Job *job)
673 if (pbm->mfp != NULL && useFilemon) {
674 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
675 err(1, "Could not set filemon pid!");
682 meta_job_error(Job *job, GNode *gn, int flags, int status)
684 char cwd[MAXPATHLEN];
694 if (pbm->mfp != NULL) {
695 fprintf(pbm->mfp, "*** Error code %d%s\n",
697 (flags & JOB_IGNERR) ?
701 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
703 getcwd(cwd, sizeof(cwd));
704 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
705 if (pbm && pbm->meta_fname[0]) {
706 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
708 meta_job_finish(job);
712 meta_job_output(Job *job, char *cp, const char *nl)
721 if (pbm->mfp != NULL) {
723 static char *meta_prefix = NULL;
724 static int meta_prefix_len;
729 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
730 if ((cp2 = strchr(meta_prefix, '$')))
731 meta_prefix_len = cp2 - meta_prefix;
733 meta_prefix_len = strlen(meta_prefix);
735 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
736 cp = strchr(cp+1, '\n');
741 fprintf(pbm->mfp, "%s%s", cp, nl);
746 meta_cmd_finish(void *pbmp)
749 BuildMon *pbm = pbmp;
754 if (pbm->filemon_fd >= 0) {
755 close(pbm->filemon_fd);
756 filemon_read(pbm->mfp, pbm->mon_fd);
757 pbm->filemon_fd = pbm->mon_fd = -1;
763 meta_job_finish(Job *job)
772 if (pbm->mfp != NULL) {
773 meta_cmd_finish(pbm);
776 pbm->meta_fname[0] = '\0';
781 * Fetch a full line from fp - growing bufp if needed
782 * Return length in bufp.
785 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
792 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
794 x = o + strlen(&buf[o]);
795 if (buf[x - 1] == '\n')
798 * We need to grow the buffer.
799 * The meta file can give us a clue.
801 if (fstat(fileno(fp), &fs) == 0) {
805 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
807 newsz = ROUNDUP(fs.st_size, BUFSIZ);
809 fprintf(debug_file, "growing buffer %u -> %u\n",
810 (unsigned)bufsz, (unsigned)newsz);
811 p = bmake_realloc(buf, newsz);
814 *szp = bufsz = newsz;
816 if (!fgets(&buf[x], bufsz - x, fp))
817 return x; /* truncated! */
826 prefix_match(void *p, void *q)
828 const char *prefix = p;
829 const char *path = q;
830 size_t n = strlen(prefix);
832 return (0 == strncmp(path, prefix, n));
836 string_match(const void *p, const void *q)
841 return strcmp(p1, p2);
846 * When running with 'meta' functionality, a target can be out-of-date
847 * if any of the references in it's meta data file is more recent.
848 * We have to track the latestdir on a per-process basis.
850 #define LDIR_VNAME_FMT ".meta.%d.ldir"
853 * It is possible that a .meta file is corrupted,
854 * if we detect this we want to reproduce it.
855 * Setting oodate TRUE will have that effect.
857 #define CHECK_VALID_META(p) if (!(p && *p)) { \
858 warnx("%s: %d: malformed", fname, lineno); \
864 meta_oodate(GNode *gn, Boolean oodate)
866 static char *tmpdir = NULL;
867 static char cwd[MAXPATHLEN];
869 char latestdir[MAXPATHLEN];
870 char fname[MAXPATHLEN];
871 char fname1[MAXPATHLEN];
872 char fname2[MAXPATHLEN];
875 static size_t cwdlen = 0;
876 static size_t tmplen = 0;
878 Boolean needOODATE = FALSE;
882 return oodate; /* we're done */
884 missingFiles = Lst_Init(FALSE);
887 * We need to check if the target is out-of-date. This includes
888 * checking if the expanded command has changed. This in turn
889 * requires that all variables are set in the same way that they
890 * would be if the target needs to be re-built.
894 meta_name(gn, fname, sizeof(fname), NULL, NULL);
896 #ifdef DEBUG_META_MODE
898 fprintf(debug_file, "meta_oodate: %s\n", fname);
901 if ((fp = fopen(fname, "r")) != NULL) {
902 static char *buf = NULL;
914 buf = bmake_malloc(bufsz);
918 if (getcwd(cwd, sizeof(cwd)) == NULL)
919 err(1, "Could not get current working directory");
920 cwdlen = strlen(cwd);
924 tmpdir = getTmpdir();
925 tmplen = strlen(tmpdir);
928 /* we want to track all the .meta we read */
929 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
931 ln = Lst_First(gn->commands);
932 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
934 if (buf[x - 1] == '\n')
937 warnx("%s: %d: line truncated at %u", fname, lineno, x);
941 /* Find the start of the build monitor section. */
943 if (strncmp(buf, "-- filemon", 10) == 0) {
947 if (strncmp(buf, "# buildmon", 10) == 0) {
953 /* Delimit the record type. */
955 #ifdef DEBUG_META_MODE
957 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
962 * We are in the 'filemon' output section.
963 * Each record from filemon follows the general form:
968 * <key> is a single letter, denoting the syscall.
969 * <pid> is the process that made the syscall.
970 * <data> is the arguments (of interest).
973 case '#': /* comment */
974 case 'V': /* version */
978 * We need to track pathnames per-process.
980 * Each process run by make, starts off in the 'CWD'
981 * recorded in the .meta file, if it chdirs ('C')
982 * elsewhere we need to track that - but only for
983 * that process. If it forks ('F'), we initialize
984 * the child to have the same cwd as its parent.
986 * We also need to track the 'latestdir' of
987 * interest. This is usually the same as cwd, but
988 * not if a process is reading directories.
990 * Each time we spot a different process ('pid')
991 * we save the current value of 'latestdir' in a
992 * variable qualified by 'lastpid', and
993 * re-initialize 'latestdir' to any pre-saved
994 * value for the current 'pid' and 'CWD' if none.
998 if (pid > 0 && pid != lastpid) {
1003 /* We need to remember this. */
1004 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
1006 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1008 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1010 strlcpy(latestdir, ldir, sizeof(latestdir));
1014 strlcpy(latestdir, cwd, sizeof(latestdir));
1016 /* Skip past the pid. */
1017 if (strsep(&p, " ") == NULL)
1019 #ifdef DEBUG_META_MODE
1021 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
1026 CHECK_VALID_META(p);
1028 /* Process according to record type. */
1030 case 'X': /* eXit */
1031 Var_Delete(ldir_vname, VAR_GLOBAL);
1032 lastpid = 0; /* no need to save ldir_vname */
1035 case 'F': /* [v]Fork */
1042 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1043 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1048 case 'C': /* Chdir */
1049 /* Update the latest directory. */
1050 strlcpy(latestdir, p, sizeof(latestdir));
1053 case 'M': /* renaMe */
1054 if (Lst_IsEmpty(missingFiles))
1056 /* 'L' and 'M' put single quotes around the args */
1061 if ((ep = strchr(p, '\'')))
1065 case 'D': /* unlink */
1066 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1067 /* remove p from the missingFiles list if present */
1068 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1069 char *tp = Lst_Datum(ln);
1070 Lst_Remove(missingFiles, ln);
1072 ln = NULL; /* we're done with it */
1076 case 'L': /* Link */
1077 /* we want the target */
1078 if (strsep(&p, " ") == NULL)
1080 CHECK_VALID_META(p);
1081 /* 'L' and 'M' put single quotes around the args */
1086 if ((ep = strchr(p, '\'')))
1090 case 'W': /* Write */
1092 * If a file we generated within our bailiwick
1093 * but outside of .OBJDIR is missing,
1094 * we need to do it again.
1096 /* ignore non-absolute paths */
1100 if (Lst_IsEmpty(metaBailiwick))
1103 /* ignore cwd - normal dependencies handle those */
1104 if (strncmp(p, cwd, cwdlen) == 0)
1107 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1110 /* tmpdir might be within */
1111 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1114 /* ignore anything containing the string "tmp" */
1115 if ((strstr("tmp", p)))
1118 if (stat(p, &fs) < 0) {
1119 Lst_AtEnd(missingFiles, bmake_strdup(p));
1122 case 'R': /* Read */
1123 case 'E': /* Exec */
1125 * Check for runtime files that can't
1126 * be part of the dependencies because
1127 * they are _expected_ to change.
1130 Lst_ForEach(metaIgnorePaths, prefix_match, p)) {
1131 #ifdef DEBUG_META_MODE
1133 fprintf(debug_file, "meta_oodate: ignoring: %s\n",
1139 if ((cp = strrchr(p, '/'))) {
1142 * We don't normally expect to see this,
1143 * but we do expect it to change.
1145 if (strcmp(cp, makeDependfile) == 0)
1150 * The rest of the record is the file name.
1151 * Check if it's not an absolute path.
1160 sdirs[sdx++] = p; /* done */
1162 if (strcmp(".", p) == 0)
1163 continue; /* no point */
1165 /* Check vs latestdir */
1166 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1167 sdirs[sdx++] = fname1;
1169 if (strcmp(latestdir, cwd) != 0) {
1171 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1172 sdirs[sdx++] = fname2;
1175 sdirs[sdx++] = NULL;
1177 for (sdp = sdirs; *sdp && !found; sdp++) {
1178 #ifdef DEBUG_META_MODE
1180 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1182 if (stat(*sdp, &fs) == 0) {
1188 #ifdef DEBUG_META_MODE
1190 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1192 if (!S_ISDIR(fs.st_mode) &&
1193 fs.st_mtime > gn->mtime) {
1195 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1197 } else if (S_ISDIR(fs.st_mode)) {
1198 /* Update the latest directory. */
1199 realpath(p, latestdir);
1201 } else if (errno == ENOENT && *p == '/' &&
1202 strncmp(p, cwd, cwdlen) != 0) {
1204 * A referenced file outside of CWD is missing.
1205 * We cannot catch every eventuality here...
1208 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1216 } else if (strcmp(buf, "CMD") == 0) {
1218 * Compare the current command with the one in the
1223 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1226 char *cmd = (char *)Lst_Datum(ln);
1227 Boolean hasOODATE = FALSE;
1229 if (strstr(cmd, "$?"))
1231 else if ((cp = strstr(cmd, ".OODATE"))) {
1232 /* check for $[{(].OODATE[:)}] */
1233 if (cp > cmd + 2 && cp[-2] == '$')
1239 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1241 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1243 if ((cp = strchr(cmd, '\n'))) {
1247 * This command contains newlines, we need to
1248 * fetch more from the .meta file before we
1249 * attempt a comparison.
1251 /* first put the newline back at buf[x - 1] */
1254 /* now fetch the next line */
1255 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1259 if (buf[x - 1] != '\n') {
1260 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1263 cp = strchr(++cp, '\n');
1265 if (buf[x - 1] == '\n')
1269 !(gn->type & OP_NOMETA_CMP) &&
1270 strcmp(p, cmd) != 0) {
1272 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1273 if (!metaIgnoreCMDs)
1279 } else if (strcmp(buf, "CWD") == 0) {
1281 * Check if there are extra commands now
1282 * that weren't in the meta data file.
1284 if (!oodate && ln != NULL) {
1286 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1289 if (strcmp(p, cwd) != 0) {
1291 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1298 if (!Lst_IsEmpty(missingFiles)) {
1300 fprintf(debug_file, "%s: missing files: %s...\n",
1301 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1303 Lst_Destroy(missingFiles, (FreeProc *)free);
1306 if ((gn->type & OP_META)) {
1308 fprintf(debug_file, "%s: required but missing\n", fname);
1312 if (oodate && needOODATE) {
1314 * Target uses .OODATE which is empty; or we wouldn't be here.
1315 * We have decided it is oodate, so .OODATE needs to be set.
1316 * All we can sanely do is set it to .ALLSRC.
1318 Var_Delete(OODATE, gn);
1319 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1326 /* support for compat mode */
1328 static int childPipe[2];
1331 meta_compat_start(void)
1333 #ifdef USE_FILEMON_ONCE
1335 * We need to re-open filemon for each cmd.
1337 BuildMon *pbm = &Mybm;
1339 if (pbm->mfp != NULL && useFilemon) {
1342 pbm->mon_fd = pbm->filemon_fd = -1;
1345 if (pipe(childPipe) < 0)
1346 Punt("Cannot create pipe: %s", strerror(errno));
1347 /* Set close-on-exec flag for both */
1348 (void)fcntl(childPipe[0], F_SETFD, 1);
1349 (void)fcntl(childPipe[1], F_SETFD, 1);
1353 meta_compat_child(void)
1355 meta_job_child(NULL);
1356 if (dup2(childPipe[1], 1) < 0 ||
1358 execError("dup2", "pipe");
1364 meta_compat_parent(void)
1369 close(childPipe[1]); /* child side */
1370 fp = fdopen(childPipe[0], "r");
1371 while (fgets(buf, sizeof(buf), fp)) {
1372 meta_job_output(NULL, buf, "");
1378 #endif /* USE_META */