1 /* $NetBSD: meta.c,v 1.26 2013/01/19 04:23:37 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 */
60 Boolean useMeta = FALSE;
61 static Boolean useFilemon = FALSE;
62 static Boolean writeMeta = FALSE;
63 static Boolean metaEnv = FALSE; /* don't save env unless asked */
64 static Boolean metaVerbose = FALSE;
65 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
66 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
67 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */
69 extern Boolean forceJobs;
70 extern Boolean comatMake;
71 extern char **environ;
73 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
76 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
79 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
82 #if !defined(HAVE_STRSEP)
83 # define strsep(s, d) stresep((s), (d), 0)
87 * Filemon is a kernel module which snoops certain syscalls.
98 * See meta_oodate below - we mainly care about 'E' and 'R'.
100 * We can still use meta mode without filemon, but
101 * the benefits are more limited.
104 # ifndef _PATH_FILEMON
105 # define _PATH_FILEMON "/dev/filemon"
109 * Open the filemon device.
112 filemon_open(BuildMon *pbm)
116 pbm->mon_fd = pbm->filemon_fd = -1;
120 for (retry = 5; retry >= 0; retry--) {
121 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
125 if (pbm->filemon_fd < 0) {
127 warn("Could not open %s", _PATH_FILEMON);
132 * We use a file outside of '.'
133 * to avoid a FreeBSD kernel bug where unlink invalidates
134 * cwd causing getcwd to do a lot more work.
135 * We only care about the descriptor.
137 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
138 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
139 err(1, "Could not set filemon file descriptor!");
141 /* we don't need these once we exec */
142 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
143 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
147 * Read the build monitor output file and write records to the target's
151 filemon_read(FILE *mfp, int fd)
156 /* Check if we're not writing to a meta data file.*/
159 close(fd); /* not interested */
163 (void)lseek(fd, (off_t)0, SEEK_SET);
164 if ((fp = fdopen(fd, "r")) == NULL)
165 err(1, "Could not read build monitor file '%d'", fd);
167 fprintf(mfp, "-- filemon acquired metadata --\n");
169 while (fgets(buf, sizeof(buf), fp)) {
170 fprintf(mfp, "%s", buf);
179 * when realpath() fails,
180 * we use this, to clean up ./ and ../
183 eat_dots(char *buf, size_t bufsz, int dots)
204 cp = strstr(buf, eat);
207 if (dots == 2 && cp > buf) {
210 } while (cp > buf && *cp != '/');
213 strlcpy(cp, cp2, bufsz - (cp - buf));
215 return; /* can't happen? */
222 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
226 char buf[MAXPATHLEN];
227 char cwd[MAXPATHLEN];
231 char *p[4]; /* >= number of possible uses */
236 dname = Var_Value(".OBJDIR", gn, &p[i++]);
238 tname = Var_Value(TARGET, gn, &p[i++]);
240 if (realpath(dname, cwd))
244 * Weed out relative paths from the target file name.
245 * We have to be careful though since if target is a
246 * symlink, the result will be unstable.
247 * So we use realpath() just to get the dirname, and leave the
248 * basename as given to us.
250 if ((cp = strrchr(tname, '/'))) {
251 if (realpath(tname, buf)) {
252 if ((rp = strrchr(buf, '/'))) {
255 if (strcmp(cp, rp) != 0)
256 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
261 * We likely have a directory which is about to be made.
262 * We pretend realpath() succeeded, to have a chance
263 * of generating the same meta file name that we will
266 if (tname[0] == '/') {
267 strlcpy(buf, tname, sizeof(buf));
269 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
271 eat_dots(buf, sizeof(buf), 1); /* ./ */
272 eat_dots(buf, sizeof(buf), 2); /* ../ */
276 /* on some systems dirname may modify its arg */
277 tp = bmake_strdup(tname);
278 if (strcmp(dname, dirname(tp)) == 0)
279 snprintf(mname, mnamelen, "%s.meta", tname);
281 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
284 * Replace path separators in the file name after the
285 * current object directory path.
287 cp = mname + strlen(dname) + 1;
289 while (*cp != '\0') {
296 for (i--; i >= 0; i--) {
304 * Return true if running ${.MAKE}
305 * Bypassed if target is flagged .MAKE
308 is_submake(void *cmdp, void *gnp)
310 static char *p_make = NULL;
317 int rc = 0; /* keep looking */
320 p_make = Var_Value(".MAKE", gn, &cp);
321 p_len = strlen(p_make);
323 cp = strchr(cmd, '$');
325 mp = Var_Subst(NULL, cmd, gn, FALSE);
328 cp2 = strstr(cmd, p_make);
330 switch (cp2[p_len]) {
338 if (cp2 > cmd && rc > 0) {
345 rc = 0; /* no match */
355 typedef struct meta_file_s {
361 printCMD(void *cmdp, void *mfpp)
363 meta_file_t *mfp = mfpp;
367 if (strchr(cmd, '$')) {
368 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
370 fprintf(mfp->fp, "CMD %s\n", cmd);
377 * Certain node types never get a .meta file
379 #define SKIP_META_TYPE(_type) do { \
380 if ((gn->type & __CONCAT(OP_, _type))) { \
382 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
383 gn->name, __STRING(_type)); \
390 meta_create(BuildMon *pbm, GNode *gn)
393 char buf[MAXPATHLEN];
394 char objdir[MAXPATHLEN];
400 char *p[4]; /* >= possible uses */
405 /* This may be a phony node which we don't want meta data for... */
406 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
407 /* Or it may be explicitly flagged as .NOMETA */
408 SKIP_META_TYPE(NOMETA);
409 /* Unless it is explicitly flagged as .META */
410 if (!(gn->type & OP_META)) {
411 SKIP_META_TYPE(PHONY);
412 SKIP_META_TYPE(SPECIAL);
413 SKIP_META_TYPE(MAKE);
420 dname = Var_Value(".OBJDIR", gn, &p[i++]);
421 tname = Var_Value(TARGET, gn, &p[i++]);
423 /* The object directory may not exist. Check it.. */
424 if (stat(dname, &fs) != 0) {
426 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
430 /* Check if there are no commands to execute. */
431 if (Lst_IsEmpty(gn->commands)) {
433 fprintf(debug_file, "Skipping meta for %s: no commands\n",
438 /* make sure these are canonical */
439 if (realpath(dname, objdir))
442 /* If we aren't in the object directory, don't create a meta file. */
443 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
445 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
449 if (!(gn->type & OP_META)) {
450 /* We do not generate .meta files for sub-makes */
451 if (Lst_ForEach(gn->commands, is_submake, gn)) {
453 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
462 /* Describe the target we are building */
463 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
465 fprintf(stdout, "%s\n", mp);
468 /* Get the basename of the target */
469 if ((cp = strrchr(tname, '/')) == NULL) {
477 if (strcmp(cp, makeDependfile) == 0)
481 /* Don't create meta data. */
484 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
487 #ifdef DEBUG_META_MODE
489 fprintf(debug_file, "meta_create: %s\n", fname);
492 if ((mf.fp = fopen(fname, "w")) == NULL)
493 err(1, "Could not open meta file '%s'", fname);
495 fprintf(mf.fp, "# Meta data file %s\n", fname);
499 Lst_ForEach(gn->commands, printCMD, &mf);
501 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
502 fprintf(mf.fp, "TARGET %s\n", tname);
505 for (ptr = environ; *ptr != NULL; ptr++)
506 fprintf(mf.fp, "ENV %s\n", *ptr);
509 fprintf(mf.fp, "-- command output --\n");
512 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
513 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
515 gn->type |= OP_META; /* in case anyone wants to know */
517 gn->type |= OP_SILENT;
520 for (i--; i >= 0; i--) {
543 meta_init(const char *make_mode)
553 if (strstr(make_mode, "env"))
555 if (strstr(make_mode, "verb"))
557 if (strstr(make_mode, "read"))
559 if (strstr(make_mode, "nofilemon"))
561 if ((cp = strstr(make_mode, "curdirok="))) {
562 metaCurdirOk = boolValue(&cp[9]);
564 if ((cp = strstr(make_mode, "silent="))) {
565 metaSilent = boolValue(&cp[7]);
567 if (strstr(make_mode, "ignore-cmd"))
568 metaIgnoreCMDs = TRUE;
569 /* for backwards compatability */
570 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
571 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
573 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
575 * The default value for MAKE_META_PREFIX
576 * prints the absolute path of the target.
577 * This works be cause :H will generate '.' if there is no /
578 * and :tA will resolve that to cwd.
580 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
585 memset(&Mybm, 0, sizeof(Mybm));
587 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
589 metaBailiwick = Lst_Init(FALSE);
590 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
592 str2Lst_Append(metaBailiwick, cp, NULL);
597 * In each case below we allow for job==NULL
600 meta_job_start(Job *job, GNode *gn)
609 pbm->mfp = meta_create(pbm, gn);
610 #ifdef USE_FILEMON_ONCE
611 /* compat mode we open the filemon dev once per command */
616 if (pbm->mfp != NULL && useFilemon) {
619 pbm->mon_fd = pbm->filemon_fd = -1;
625 * The child calls this before doing anything.
626 * It does not disturb our state.
629 meta_job_child(Job *job)
641 if (pbm->mfp != NULL && useFilemon) {
642 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
643 err(1, "Could not set filemon pid!");
650 meta_job_error(Job *job, GNode *gn, int flags, int status)
652 char cwd[MAXPATHLEN];
662 if (pbm->mfp != NULL) {
663 fprintf(pbm->mfp, "*** Error code %d%s\n",
665 (flags & JOB_IGNERR) ?
669 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
671 getcwd(cwd, sizeof(cwd));
672 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
673 if (pbm && pbm->meta_fname[0]) {
674 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
676 meta_job_finish(job);
680 meta_job_output(Job *job, char *cp, const char *nl)
689 if (pbm->mfp != NULL) {
691 static char *meta_prefix = NULL;
692 static int meta_prefix_len;
697 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
698 if ((cp2 = strchr(meta_prefix, '$')))
699 meta_prefix_len = cp2 - meta_prefix;
701 meta_prefix_len = strlen(meta_prefix);
703 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
704 cp = strchr(cp+1, '\n');
709 fprintf(pbm->mfp, "%s%s", cp, nl);
714 meta_cmd_finish(void *pbmp)
717 BuildMon *pbm = pbmp;
722 if (pbm->filemon_fd >= 0) {
723 close(pbm->filemon_fd);
724 filemon_read(pbm->mfp, pbm->mon_fd);
725 pbm->filemon_fd = pbm->mon_fd = -1;
731 meta_job_finish(Job *job)
740 if (pbm->mfp != NULL) {
741 meta_cmd_finish(pbm);
744 pbm->meta_fname[0] = '\0';
749 * Fetch a full line from fp - growing bufp if needed
750 * Return length in bufp.
753 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
760 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
762 x = o + strlen(&buf[o]);
763 if (buf[x - 1] == '\n')
766 * We need to grow the buffer.
767 * The meta file can give us a clue.
769 if (fstat(fileno(fp), &fs) == 0) {
773 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
775 newsz = ROUNDUP(fs.st_size, BUFSIZ);
777 fprintf(debug_file, "growing buffer %u -> %u\n",
778 (unsigned)bufsz, (unsigned)newsz);
779 p = bmake_realloc(buf, newsz);
782 *szp = bufsz = newsz;
784 if (!fgets(&buf[x], bufsz - x, fp))
785 return x; /* truncated! */
794 prefix_match(void *p, void *q)
796 const char *prefix = p;
797 const char *path = q;
798 size_t n = strlen(prefix);
800 return (0 == strncmp(path, prefix, n));
804 string_match(const void *p, const void *q)
809 return strcmp(p1, p2);
814 * When running with 'meta' functionality, a target can be out-of-date
815 * if any of the references in it's meta data file is more recent.
816 * We have to track the latestdir on a per-process basis.
818 #define LDIR_VNAME_FMT ".meta.%d.ldir"
821 * It is possible that a .meta file is corrupted,
822 * if we detect this we want to reproduce it.
823 * Setting oodate TRUE will have that effect.
825 #define CHECK_VALID_META(p) if (!(p && *p)) { \
826 warnx("%s: %d: malformed", fname, lineno); \
832 meta_oodate(GNode *gn, Boolean oodate)
834 static char *tmpdir = NULL;
835 static char cwd[MAXPATHLEN];
837 char latestdir[MAXPATHLEN];
838 char fname[MAXPATHLEN];
839 char fname1[MAXPATHLEN];
840 char fname2[MAXPATHLEN];
843 static size_t cwdlen = 0;
844 static size_t tmplen = 0;
846 Boolean needOODATE = FALSE;
850 return oodate; /* we're done */
852 missingFiles = Lst_Init(FALSE);
855 * We need to check if the target is out-of-date. This includes
856 * checking if the expanded command has changed. This in turn
857 * requires that all variables are set in the same way that they
858 * would be if the target needs to be re-built.
862 meta_name(gn, fname, sizeof(fname), NULL, NULL);
864 #ifdef DEBUG_META_MODE
866 fprintf(debug_file, "meta_oodate: %s\n", fname);
869 if ((fp = fopen(fname, "r")) != NULL) {
870 static char *buf = NULL;
882 buf = bmake_malloc(bufsz);
886 if (getcwd(cwd, sizeof(cwd)) == NULL)
887 err(1, "Could not get current working directory");
888 cwdlen = strlen(cwd);
892 tmpdir = getTmpdir();
893 tmplen = strlen(tmpdir);
896 /* we want to track all the .meta we read */
897 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
899 ln = Lst_First(gn->commands);
900 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
902 if (buf[x - 1] == '\n')
905 warnx("%s: %d: line truncated at %u", fname, lineno, x);
909 /* Find the start of the build monitor section. */
911 if (strncmp(buf, "-- filemon", 10) == 0) {
915 if (strncmp(buf, "# buildmon", 10) == 0) {
921 /* Delimit the record type. */
923 #ifdef DEBUG_META_MODE
925 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
930 * We are in the 'filemon' output section.
931 * Each record from filemon follows the general form:
936 * <key> is a single letter, denoting the syscall.
937 * <pid> is the process that made the syscall.
938 * <data> is the arguments (of interest).
941 case '#': /* comment */
942 case 'V': /* version */
946 * We need to track pathnames per-process.
948 * Each process run by make, starts off in the 'CWD'
949 * recorded in the .meta file, if it chdirs ('C')
950 * elsewhere we need to track that - but only for
951 * that process. If it forks ('F'), we initialize
952 * the child to have the same cwd as its parent.
954 * We also need to track the 'latestdir' of
955 * interest. This is usually the same as cwd, but
956 * not if a process is reading directories.
958 * Each time we spot a different process ('pid')
959 * we save the current value of 'latestdir' in a
960 * variable qualified by 'lastpid', and
961 * re-initialize 'latestdir' to any pre-saved
962 * value for the current 'pid' and 'CWD' if none.
966 if (pid > 0 && pid != lastpid) {
971 /* We need to remember this. */
972 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
974 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
976 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
978 strlcpy(latestdir, ldir, sizeof(latestdir));
982 strlcpy(latestdir, cwd, sizeof(latestdir));
984 /* Skip past the pid. */
985 if (strsep(&p, " ") == NULL)
987 #ifdef DEBUG_META_MODE
989 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
996 /* Process according to record type. */
999 Var_Delete(ldir_vname, VAR_GLOBAL);
1000 lastpid = 0; /* no need to save ldir_vname */
1003 case 'F': /* [v]Fork */
1010 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1011 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1016 case 'C': /* Chdir */
1017 /* Update the latest directory. */
1018 strlcpy(latestdir, p, sizeof(latestdir));
1021 case 'M': /* renaMe */
1022 if (Lst_IsEmpty(missingFiles))
1024 /* 'L' and 'M' put single quotes around the args */
1029 if ((ep = strchr(p, '\'')))
1033 case 'D': /* unlink */
1034 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1035 /* remove p from the missingFiles list if present */
1036 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1037 char *tp = Lst_Datum(ln);
1038 Lst_Remove(missingFiles, ln);
1043 case 'L': /* Link */
1044 /* we want the target */
1045 if (strsep(&p, " ") == NULL)
1047 CHECK_VALID_META(p);
1048 /* 'L' and 'M' put single quotes around the args */
1053 if ((ep = strchr(p, '\'')))
1057 case 'W': /* Write */
1059 * If a file we generated within our bailiwick
1060 * but outside of .OBJDIR is missing,
1061 * we need to do it again.
1063 /* ignore non-absolute paths */
1067 if (Lst_IsEmpty(metaBailiwick))
1070 /* ignore cwd - normal dependencies handle those */
1071 if (strncmp(p, cwd, cwdlen) == 0)
1074 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1077 /* tmpdir might be within */
1078 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1081 /* ignore anything containing the string "tmp" */
1082 if ((strstr("tmp", p)))
1085 if (stat(p, &fs) < 0) {
1086 Lst_AtEnd(missingFiles, bmake_strdup(p));
1089 case 'R': /* Read */
1090 case 'E': /* Exec */
1092 * Check for runtime files that can't
1093 * be part of the dependencies because
1094 * they are _expected_ to change.
1096 if (strncmp(p, "/tmp/", 5) == 0 ||
1097 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
1100 if (strncmp(p, "/var/", 5) == 0)
1103 /* Ignore device files. */
1104 if (strncmp(p, "/dev/", 5) == 0)
1107 /* Ignore /etc/ files. */
1108 if (strncmp(p, "/etc/", 5) == 0)
1111 if ((cp = strrchr(p, '/'))) {
1114 * We don't normally expect to see this,
1115 * but we do expect it to change.
1117 if (strcmp(cp, makeDependfile) == 0)
1122 * The rest of the record is the file name.
1123 * Check if it's not an absolute path.
1132 sdirs[sdx++] = p; /* done */
1134 if (strcmp(".", p) == 0)
1135 continue; /* no point */
1137 /* Check vs latestdir */
1138 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1139 sdirs[sdx++] = fname1;
1141 if (strcmp(latestdir, cwd) != 0) {
1143 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1144 sdirs[sdx++] = fname2;
1147 sdirs[sdx++] = NULL;
1149 for (sdp = sdirs; *sdp && !found; sdp++) {
1150 #ifdef DEBUG_META_MODE
1152 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1154 if (stat(*sdp, &fs) == 0) {
1160 #ifdef DEBUG_META_MODE
1162 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1164 if (!S_ISDIR(fs.st_mode) &&
1165 fs.st_mtime > gn->mtime) {
1167 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1169 } else if (S_ISDIR(fs.st_mode)) {
1170 /* Update the latest directory. */
1171 realpath(p, latestdir);
1173 } else if (errno == ENOENT && *p == '/' &&
1174 strncmp(p, cwd, cwdlen) != 0) {
1176 * A referenced file outside of CWD is missing.
1177 * We cannot catch every eventuality here...
1180 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1188 } else if (strcmp(buf, "CMD") == 0) {
1190 * Compare the current command with the one in the
1195 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1198 char *cmd = (char *)Lst_Datum(ln);
1201 if (strstr(cmd, "$?"))
1203 else if ((cp = strstr(cmd, ".OODATE"))) {
1204 /* check for $[{(].OODATE[)}] */
1205 if (cp > cmd + 2 && cp[-2] == '$')
1208 if (needOODATE && DEBUG(META))
1209 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1211 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1213 if ((cp = strchr(cmd, '\n'))) {
1217 * This command contains newlines, we need to
1218 * fetch more from the .meta file before we
1219 * attempt a comparison.
1221 /* first put the newline back at buf[x - 1] */
1224 /* now fetch the next line */
1225 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1229 if (buf[x - 1] != '\n') {
1230 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1233 cp = strchr(++cp, '\n');
1235 if (buf[x - 1] == '\n')
1239 !(gn->type & OP_NOMETA_CMP) &&
1240 strcmp(p, cmd) != 0) {
1242 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1243 if (!metaIgnoreCMDs)
1249 } else if (strcmp(buf, "CWD") == 0) {
1251 * Check if there are extra commands now
1252 * that weren't in the meta data file.
1254 if (!oodate && ln != NULL) {
1256 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1259 if (strcmp(p, cwd) != 0) {
1261 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1268 if (!Lst_IsEmpty(missingFiles)) {
1270 fprintf(debug_file, "%s: missing files: %s...\n",
1271 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1273 Lst_Destroy(missingFiles, (FreeProc *)free);
1276 if ((gn->type & OP_META)) {
1278 fprintf(debug_file, "%s: required but missing\n", fname);
1282 if (oodate && needOODATE) {
1284 * Target uses .OODATE which is empty; or we wouldn't be here.
1285 * We have decided it is oodate, so .OODATE needs to be set.
1286 * All we can sanely do is set it to .ALLSRC.
1288 Var_Delete(OODATE, gn);
1289 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1296 /* support for compat mode */
1298 static int childPipe[2];
1301 meta_compat_start(void)
1303 #ifdef USE_FILEMON_ONCE
1305 * We need to re-open filemon for each cmd.
1307 BuildMon *pbm = &Mybm;
1309 if (pbm->mfp != NULL && useFilemon) {
1312 pbm->mon_fd = pbm->filemon_fd = -1;
1315 if (pipe(childPipe) < 0)
1316 Punt("Cannot create pipe: %s", strerror(errno));
1317 /* Set close-on-exec flag for both */
1318 (void)fcntl(childPipe[0], F_SETFD, 1);
1319 (void)fcntl(childPipe[1], F_SETFD, 1);
1323 meta_compat_child(void)
1325 meta_job_child(NULL);
1326 if (dup2(childPipe[1], 1) < 0 ||
1328 execError("dup2", "pipe");
1334 meta_compat_parent(void)
1339 close(childPipe[1]); /* child side */
1340 fp = fdopen(childPipe[0], "r");
1341 while (fgets(buf, sizeof(buf), fp)) {
1342 meta_job_output(NULL, buf, "");
1348 #endif /* USE_META */