1 /* $NetBSD: meta.c,v 1.29 2013/03/31 05:49:51 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 * Initialization we need before reading makefiles.
549 /* this allows makefiles to test if we have filemon support */
550 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0);
556 * Initialization we need after reading makefiles.
559 meta_mode_init(const char *make_mode)
569 if (strstr(make_mode, "env"))
571 if (strstr(make_mode, "verb"))
573 if (strstr(make_mode, "read"))
575 if (strstr(make_mode, "nofilemon"))
577 if ((cp = strstr(make_mode, "curdirok="))) {
578 metaCurdirOk = boolValue(&cp[9]);
580 if ((cp = strstr(make_mode, "silent="))) {
581 metaSilent = boolValue(&cp[7]);
583 if (strstr(make_mode, "ignore-cmd"))
584 metaIgnoreCMDs = TRUE;
585 /* for backwards compatability */
586 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
587 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
589 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
591 * The default value for MAKE_META_PREFIX
592 * prints the absolute path of the target.
593 * This works be cause :H will generate '.' if there is no /
594 * and :tA will resolve that to cwd.
596 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
601 memset(&Mybm, 0, sizeof(Mybm));
603 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
605 metaBailiwick = Lst_Init(FALSE);
606 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
608 str2Lst_Append(metaBailiwick, cp, NULL);
613 * In each case below we allow for job==NULL
616 meta_job_start(Job *job, GNode *gn)
625 pbm->mfp = meta_create(pbm, gn);
626 #ifdef USE_FILEMON_ONCE
627 /* compat mode we open the filemon dev once per command */
632 if (pbm->mfp != NULL && useFilemon) {
635 pbm->mon_fd = pbm->filemon_fd = -1;
641 * The child calls this before doing anything.
642 * It does not disturb our state.
645 meta_job_child(Job *job)
657 if (pbm->mfp != NULL && useFilemon) {
658 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
659 err(1, "Could not set filemon pid!");
666 meta_job_error(Job *job, GNode *gn, int flags, int status)
668 char cwd[MAXPATHLEN];
678 if (pbm->mfp != NULL) {
679 fprintf(pbm->mfp, "*** Error code %d%s\n",
681 (flags & JOB_IGNERR) ?
685 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
687 getcwd(cwd, sizeof(cwd));
688 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
689 if (pbm && pbm->meta_fname[0]) {
690 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
692 meta_job_finish(job);
696 meta_job_output(Job *job, char *cp, const char *nl)
705 if (pbm->mfp != NULL) {
707 static char *meta_prefix = NULL;
708 static int meta_prefix_len;
713 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
714 if ((cp2 = strchr(meta_prefix, '$')))
715 meta_prefix_len = cp2 - meta_prefix;
717 meta_prefix_len = strlen(meta_prefix);
719 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
720 cp = strchr(cp+1, '\n');
725 fprintf(pbm->mfp, "%s%s", cp, nl);
730 meta_cmd_finish(void *pbmp)
733 BuildMon *pbm = pbmp;
738 if (pbm->filemon_fd >= 0) {
739 close(pbm->filemon_fd);
740 filemon_read(pbm->mfp, pbm->mon_fd);
741 pbm->filemon_fd = pbm->mon_fd = -1;
747 meta_job_finish(Job *job)
756 if (pbm->mfp != NULL) {
757 meta_cmd_finish(pbm);
760 pbm->meta_fname[0] = '\0';
765 * Fetch a full line from fp - growing bufp if needed
766 * Return length in bufp.
769 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
776 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
778 x = o + strlen(&buf[o]);
779 if (buf[x - 1] == '\n')
782 * We need to grow the buffer.
783 * The meta file can give us a clue.
785 if (fstat(fileno(fp), &fs) == 0) {
789 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
791 newsz = ROUNDUP(fs.st_size, BUFSIZ);
793 fprintf(debug_file, "growing buffer %u -> %u\n",
794 (unsigned)bufsz, (unsigned)newsz);
795 p = bmake_realloc(buf, newsz);
798 *szp = bufsz = newsz;
800 if (!fgets(&buf[x], bufsz - x, fp))
801 return x; /* truncated! */
810 prefix_match(void *p, void *q)
812 const char *prefix = p;
813 const char *path = q;
814 size_t n = strlen(prefix);
816 return (0 == strncmp(path, prefix, n));
820 string_match(const void *p, const void *q)
825 return strcmp(p1, p2);
830 * When running with 'meta' functionality, a target can be out-of-date
831 * if any of the references in it's meta data file is more recent.
832 * We have to track the latestdir on a per-process basis.
834 #define LDIR_VNAME_FMT ".meta.%d.ldir"
837 * It is possible that a .meta file is corrupted,
838 * if we detect this we want to reproduce it.
839 * Setting oodate TRUE will have that effect.
841 #define CHECK_VALID_META(p) if (!(p && *p)) { \
842 warnx("%s: %d: malformed", fname, lineno); \
848 meta_oodate(GNode *gn, Boolean oodate)
850 static char *tmpdir = NULL;
851 static char cwd[MAXPATHLEN];
853 char latestdir[MAXPATHLEN];
854 char fname[MAXPATHLEN];
855 char fname1[MAXPATHLEN];
856 char fname2[MAXPATHLEN];
859 static size_t cwdlen = 0;
860 static size_t tmplen = 0;
862 Boolean needOODATE = FALSE;
866 return oodate; /* we're done */
868 missingFiles = Lst_Init(FALSE);
871 * We need to check if the target is out-of-date. This includes
872 * checking if the expanded command has changed. This in turn
873 * requires that all variables are set in the same way that they
874 * would be if the target needs to be re-built.
878 meta_name(gn, fname, sizeof(fname), NULL, NULL);
880 #ifdef DEBUG_META_MODE
882 fprintf(debug_file, "meta_oodate: %s\n", fname);
885 if ((fp = fopen(fname, "r")) != NULL) {
886 static char *buf = NULL;
898 buf = bmake_malloc(bufsz);
902 if (getcwd(cwd, sizeof(cwd)) == NULL)
903 err(1, "Could not get current working directory");
904 cwdlen = strlen(cwd);
908 tmpdir = getTmpdir();
909 tmplen = strlen(tmpdir);
912 /* we want to track all the .meta we read */
913 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
915 ln = Lst_First(gn->commands);
916 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
918 if (buf[x - 1] == '\n')
921 warnx("%s: %d: line truncated at %u", fname, lineno, x);
925 /* Find the start of the build monitor section. */
927 if (strncmp(buf, "-- filemon", 10) == 0) {
931 if (strncmp(buf, "# buildmon", 10) == 0) {
937 /* Delimit the record type. */
939 #ifdef DEBUG_META_MODE
941 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
946 * We are in the 'filemon' output section.
947 * Each record from filemon follows the general form:
952 * <key> is a single letter, denoting the syscall.
953 * <pid> is the process that made the syscall.
954 * <data> is the arguments (of interest).
957 case '#': /* comment */
958 case 'V': /* version */
962 * We need to track pathnames per-process.
964 * Each process run by make, starts off in the 'CWD'
965 * recorded in the .meta file, if it chdirs ('C')
966 * elsewhere we need to track that - but only for
967 * that process. If it forks ('F'), we initialize
968 * the child to have the same cwd as its parent.
970 * We also need to track the 'latestdir' of
971 * interest. This is usually the same as cwd, but
972 * not if a process is reading directories.
974 * Each time we spot a different process ('pid')
975 * we save the current value of 'latestdir' in a
976 * variable qualified by 'lastpid', and
977 * re-initialize 'latestdir' to any pre-saved
978 * value for the current 'pid' and 'CWD' if none.
982 if (pid > 0 && pid != lastpid) {
987 /* We need to remember this. */
988 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
990 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
992 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
994 strlcpy(latestdir, ldir, sizeof(latestdir));
998 strlcpy(latestdir, cwd, sizeof(latestdir));
1000 /* Skip past the pid. */
1001 if (strsep(&p, " ") == NULL)
1003 #ifdef DEBUG_META_MODE
1005 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
1010 CHECK_VALID_META(p);
1012 /* Process according to record type. */
1014 case 'X': /* eXit */
1015 Var_Delete(ldir_vname, VAR_GLOBAL);
1016 lastpid = 0; /* no need to save ldir_vname */
1019 case 'F': /* [v]Fork */
1026 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1027 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1032 case 'C': /* Chdir */
1033 /* Update the latest directory. */
1034 strlcpy(latestdir, p, sizeof(latestdir));
1037 case 'M': /* renaMe */
1038 if (Lst_IsEmpty(missingFiles))
1040 /* 'L' and 'M' put single quotes around the args */
1045 if ((ep = strchr(p, '\'')))
1049 case 'D': /* unlink */
1050 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1051 /* remove p from the missingFiles list if present */
1052 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1053 char *tp = Lst_Datum(ln);
1054 Lst_Remove(missingFiles, ln);
1056 ln = NULL; /* we're done with it */
1060 case 'L': /* Link */
1061 /* we want the target */
1062 if (strsep(&p, " ") == NULL)
1064 CHECK_VALID_META(p);
1065 /* 'L' and 'M' put single quotes around the args */
1070 if ((ep = strchr(p, '\'')))
1074 case 'W': /* Write */
1076 * If a file we generated within our bailiwick
1077 * but outside of .OBJDIR is missing,
1078 * we need to do it again.
1080 /* ignore non-absolute paths */
1084 if (Lst_IsEmpty(metaBailiwick))
1087 /* ignore cwd - normal dependencies handle those */
1088 if (strncmp(p, cwd, cwdlen) == 0)
1091 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1094 /* tmpdir might be within */
1095 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1098 /* ignore anything containing the string "tmp" */
1099 if ((strstr("tmp", p)))
1102 if (stat(p, &fs) < 0) {
1103 Lst_AtEnd(missingFiles, bmake_strdup(p));
1106 case 'R': /* Read */
1107 case 'E': /* Exec */
1109 * Check for runtime files that can't
1110 * be part of the dependencies because
1111 * they are _expected_ to change.
1113 if (strncmp(p, "/tmp/", 5) == 0 ||
1114 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
1117 if (strncmp(p, "/var/", 5) == 0)
1120 /* Ignore device files. */
1121 if (strncmp(p, "/dev/", 5) == 0)
1124 /* Ignore /etc/ files. */
1125 if (strncmp(p, "/etc/", 5) == 0)
1128 if ((cp = strrchr(p, '/'))) {
1131 * We don't normally expect to see this,
1132 * but we do expect it to change.
1134 if (strcmp(cp, makeDependfile) == 0)
1139 * The rest of the record is the file name.
1140 * Check if it's not an absolute path.
1149 sdirs[sdx++] = p; /* done */
1151 if (strcmp(".", p) == 0)
1152 continue; /* no point */
1154 /* Check vs latestdir */
1155 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1156 sdirs[sdx++] = fname1;
1158 if (strcmp(latestdir, cwd) != 0) {
1160 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1161 sdirs[sdx++] = fname2;
1164 sdirs[sdx++] = NULL;
1166 for (sdp = sdirs; *sdp && !found; sdp++) {
1167 #ifdef DEBUG_META_MODE
1169 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1171 if (stat(*sdp, &fs) == 0) {
1177 #ifdef DEBUG_META_MODE
1179 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1181 if (!S_ISDIR(fs.st_mode) &&
1182 fs.st_mtime > gn->mtime) {
1184 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1186 } else if (S_ISDIR(fs.st_mode)) {
1187 /* Update the latest directory. */
1188 realpath(p, latestdir);
1190 } else if (errno == ENOENT && *p == '/' &&
1191 strncmp(p, cwd, cwdlen) != 0) {
1193 * A referenced file outside of CWD is missing.
1194 * We cannot catch every eventuality here...
1197 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1205 } else if (strcmp(buf, "CMD") == 0) {
1207 * Compare the current command with the one in the
1212 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1215 char *cmd = (char *)Lst_Datum(ln);
1216 Boolean hasOODATE = FALSE;
1218 if (strstr(cmd, "$?"))
1220 else if ((cp = strstr(cmd, ".OODATE"))) {
1221 /* check for $[{(].OODATE[:)}] */
1222 if (cp > cmd + 2 && cp[-2] == '$')
1228 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1230 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1232 if ((cp = strchr(cmd, '\n'))) {
1236 * This command contains newlines, we need to
1237 * fetch more from the .meta file before we
1238 * attempt a comparison.
1240 /* first put the newline back at buf[x - 1] */
1243 /* now fetch the next line */
1244 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1248 if (buf[x - 1] != '\n') {
1249 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1252 cp = strchr(++cp, '\n');
1254 if (buf[x - 1] == '\n')
1258 !(gn->type & OP_NOMETA_CMP) &&
1259 strcmp(p, cmd) != 0) {
1261 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1262 if (!metaIgnoreCMDs)
1268 } else if (strcmp(buf, "CWD") == 0) {
1270 * Check if there are extra commands now
1271 * that weren't in the meta data file.
1273 if (!oodate && ln != NULL) {
1275 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1278 if (strcmp(p, cwd) != 0) {
1280 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1287 if (!Lst_IsEmpty(missingFiles)) {
1289 fprintf(debug_file, "%s: missing files: %s...\n",
1290 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1292 Lst_Destroy(missingFiles, (FreeProc *)free);
1295 if ((gn->type & OP_META)) {
1297 fprintf(debug_file, "%s: required but missing\n", fname);
1301 if (oodate && needOODATE) {
1303 * Target uses .OODATE which is empty; or we wouldn't be here.
1304 * We have decided it is oodate, so .OODATE needs to be set.
1305 * All we can sanely do is set it to .ALLSRC.
1307 Var_Delete(OODATE, gn);
1308 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1315 /* support for compat mode */
1317 static int childPipe[2];
1320 meta_compat_start(void)
1322 #ifdef USE_FILEMON_ONCE
1324 * We need to re-open filemon for each cmd.
1326 BuildMon *pbm = &Mybm;
1328 if (pbm->mfp != NULL && useFilemon) {
1331 pbm->mon_fd = pbm->filemon_fd = -1;
1334 if (pipe(childPipe) < 0)
1335 Punt("Cannot create pipe: %s", strerror(errno));
1336 /* Set close-on-exec flag for both */
1337 (void)fcntl(childPipe[0], F_SETFD, 1);
1338 (void)fcntl(childPipe[1], F_SETFD, 1);
1342 meta_compat_child(void)
1344 meta_job_child(NULL);
1345 if (dup2(childPipe[1], 1) < 0 ||
1347 execError("dup2", "pipe");
1353 meta_compat_parent(void)
1358 close(childPipe[1]); /* child side */
1359 fp = fdopen(childPipe[0], "r");
1360 while (fgets(buf, sizeof(buf), fp)) {
1361 meta_job_output(NULL, buf, "");
1367 #endif /* USE_META */