1 /* $NetBSD: meta.c,v 1.38 2015/04/11 05:24:30 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);
170 fprintf(mfp, "\n-- filemon acquired metadata --\n");
172 while ((n = read(fd, buf, sizeof(buf))) > 0) {
173 fwrite(buf, 1, n, mfp);
181 * when realpath() fails,
182 * we use this, to clean up ./ and ../
185 eat_dots(char *buf, size_t bufsz, int dots)
206 cp = strstr(buf, eat);
209 if (dots == 2 && cp > buf) {
212 } while (cp > buf && *cp != '/');
215 strlcpy(cp, cp2, bufsz - (cp - buf));
217 return; /* can't happen? */
224 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
228 char buf[MAXPATHLEN];
229 char cwd[MAXPATHLEN];
233 char *p[4]; /* >= number of possible uses */
238 dname = Var_Value(".OBJDIR", gn, &p[i++]);
240 tname = Var_Value(TARGET, gn, &p[i++]);
242 if (realpath(dname, cwd))
246 * Weed out relative paths from the target file name.
247 * We have to be careful though since if target is a
248 * symlink, the result will be unstable.
249 * So we use realpath() just to get the dirname, and leave the
250 * basename as given to us.
252 if ((cp = strrchr(tname, '/'))) {
253 if (realpath(tname, buf)) {
254 if ((rp = strrchr(buf, '/'))) {
257 if (strcmp(cp, rp) != 0)
258 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
263 * We likely have a directory which is about to be made.
264 * We pretend realpath() succeeded, to have a chance
265 * of generating the same meta file name that we will
268 if (tname[0] == '/') {
269 strlcpy(buf, tname, sizeof(buf));
271 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
273 eat_dots(buf, sizeof(buf), 1); /* ./ */
274 eat_dots(buf, sizeof(buf), 2); /* ../ */
278 /* on some systems dirname may modify its arg */
279 tp = bmake_strdup(tname);
280 if (strcmp(dname, dirname(tp)) == 0)
281 snprintf(mname, mnamelen, "%s.meta", tname);
283 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
286 * Replace path separators in the file name after the
287 * current object directory path.
289 cp = mname + strlen(dname) + 1;
291 while (*cp != '\0') {
298 for (i--; i >= 0; i--) {
306 * Return true if running ${.MAKE}
307 * Bypassed if target is flagged .MAKE
310 is_submake(void *cmdp, void *gnp)
312 static char *p_make = NULL;
319 int rc = 0; /* keep looking */
322 p_make = Var_Value(".MAKE", gn, &cp);
323 p_len = strlen(p_make);
325 cp = strchr(cmd, '$');
327 mp = Var_Subst(NULL, cmd, gn, FALSE);
330 cp2 = strstr(cmd, p_make);
332 switch (cp2[p_len]) {
340 if (cp2 > cmd && rc > 0) {
347 rc = 0; /* no match */
357 typedef struct meta_file_s {
363 printCMD(void *cmdp, void *mfpp)
365 meta_file_t *mfp = mfpp;
369 if (strchr(cmd, '$')) {
370 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
372 fprintf(mfp->fp, "CMD %s\n", cmd);
379 * Certain node types never get a .meta file
381 #define SKIP_META_TYPE(_type) do { \
382 if ((gn->type & __CONCAT(OP_, _type))) { \
384 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
385 gn->name, __STRING(_type)); \
392 meta_create(BuildMon *pbm, GNode *gn)
395 char buf[MAXPATHLEN];
396 char objdir[MAXPATHLEN];
402 char *p[4]; /* >= possible uses */
407 /* This may be a phony node which we don't want meta data for... */
408 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
409 /* Or it may be explicitly flagged as .NOMETA */
410 SKIP_META_TYPE(NOMETA);
411 /* Unless it is explicitly flagged as .META */
412 if (!(gn->type & OP_META)) {
413 SKIP_META_TYPE(PHONY);
414 SKIP_META_TYPE(SPECIAL);
415 SKIP_META_TYPE(MAKE);
422 dname = Var_Value(".OBJDIR", gn, &p[i++]);
423 tname = Var_Value(TARGET, gn, &p[i++]);
425 /* The object directory may not exist. Check it.. */
426 if (stat(dname, &fs) != 0) {
428 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
432 /* Check if there are no commands to execute. */
433 if (Lst_IsEmpty(gn->commands)) {
435 fprintf(debug_file, "Skipping meta for %s: no commands\n",
440 /* make sure these are canonical */
441 if (realpath(dname, objdir))
444 /* If we aren't in the object directory, don't create a meta file. */
445 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
447 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
451 if (!(gn->type & OP_META)) {
452 /* We do not generate .meta files for sub-makes */
453 if (Lst_ForEach(gn->commands, is_submake, gn)) {
455 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
464 /* Describe the target we are building */
465 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
467 fprintf(stdout, "%s\n", mp);
470 /* Get the basename of the target */
471 if ((cp = strrchr(tname, '/')) == NULL) {
479 if (strcmp(cp, makeDependfile) == 0)
483 /* Don't create meta data. */
486 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
489 #ifdef DEBUG_META_MODE
491 fprintf(debug_file, "meta_create: %s\n", fname);
494 if ((mf.fp = fopen(fname, "w")) == NULL)
495 err(1, "Could not open meta file '%s'", fname);
497 fprintf(mf.fp, "# Meta data file %s\n", fname);
501 Lst_ForEach(gn->commands, printCMD, &mf);
503 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
504 fprintf(mf.fp, "TARGET %s\n", tname);
507 for (ptr = environ; *ptr != NULL; ptr++)
508 fprintf(mf.fp, "ENV %s\n", *ptr);
511 fprintf(mf.fp, "-- command output --\n");
514 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
515 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
517 gn->type |= OP_META; /* in case anyone wants to know */
519 gn->type |= OP_SILENT;
522 for (i--; i >= 0; i--) {
545 * Initialization we need before reading makefiles.
551 /* this allows makefiles to test if we have filemon support */
552 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0);
558 * Initialization we need after reading makefiles.
561 meta_mode_init(const char *make_mode)
571 if (strstr(make_mode, "env"))
573 if (strstr(make_mode, "verb"))
575 if (strstr(make_mode, "read"))
577 if (strstr(make_mode, "nofilemon"))
579 if ((cp = strstr(make_mode, "curdirok="))) {
580 metaCurdirOk = boolValue(&cp[9]);
582 if ((cp = strstr(make_mode, "silent="))) {
583 metaSilent = boolValue(&cp[7]);
585 if (strstr(make_mode, "ignore-cmd"))
586 metaIgnoreCMDs = TRUE;
587 /* for backwards compatability */
588 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
589 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
591 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
593 * The default value for MAKE_META_PREFIX
594 * prints the absolute path of the target.
595 * This works be cause :H will generate '.' if there is no /
596 * and :tA will resolve that to cwd.
598 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
603 memset(&Mybm, 0, sizeof(Mybm));
605 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
607 metaBailiwick = Lst_Init(FALSE);
608 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
610 str2Lst_Append(metaBailiwick, cp, NULL);
613 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
615 metaIgnorePaths = Lst_Init(FALSE);
616 Var_Append(MAKE_META_IGNORE_PATHS,
617 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
619 "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL, 0);
621 str2Lst_Append(metaIgnorePaths, cp, NULL);
626 * In each case below we allow for job==NULL
629 meta_job_start(Job *job, GNode *gn)
638 pbm->mfp = meta_create(pbm, gn);
639 #ifdef USE_FILEMON_ONCE
640 /* compat mode we open the filemon dev once per command */
645 if (pbm->mfp != NULL && useFilemon) {
648 pbm->mon_fd = pbm->filemon_fd = -1;
654 * The child calls this before doing anything.
655 * It does not disturb our state.
658 meta_job_child(Job *job)
668 if (pbm->mfp != NULL) {
669 close(fileno(pbm->mfp));
674 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
675 err(1, "Could not set filemon pid!");
683 meta_job_error(Job *job, GNode *gn, int flags, int status)
685 char cwd[MAXPATHLEN];
695 if (pbm->mfp != NULL) {
696 fprintf(pbm->mfp, "*** Error code %d%s\n",
698 (flags & JOB_IGNERR) ?
702 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
704 getcwd(cwd, sizeof(cwd));
705 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
706 if (pbm && pbm->meta_fname[0]) {
707 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
709 meta_job_finish(job);
713 meta_job_output(Job *job, char *cp, const char *nl)
722 if (pbm->mfp != NULL) {
724 static char *meta_prefix = NULL;
725 static int meta_prefix_len;
730 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
731 if ((cp2 = strchr(meta_prefix, '$')))
732 meta_prefix_len = cp2 - meta_prefix;
734 meta_prefix_len = strlen(meta_prefix);
736 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
737 cp = strchr(cp+1, '\n');
742 fprintf(pbm->mfp, "%s%s", cp, nl);
747 meta_cmd_finish(void *pbmp)
750 BuildMon *pbm = pbmp;
755 if (pbm->filemon_fd >= 0) {
756 close(pbm->filemon_fd);
757 filemon_read(pbm->mfp, pbm->mon_fd);
758 pbm->filemon_fd = pbm->mon_fd = -1;
764 meta_job_finish(Job *job)
773 if (pbm->mfp != NULL) {
774 meta_cmd_finish(pbm);
777 pbm->meta_fname[0] = '\0';
782 * Fetch a full line from fp - growing bufp if needed
783 * Return length in bufp.
786 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
793 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
795 x = o + strlen(&buf[o]);
796 if (buf[x - 1] == '\n')
799 * We need to grow the buffer.
800 * The meta file can give us a clue.
802 if (fstat(fileno(fp), &fs) == 0) {
806 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
808 newsz = ROUNDUP(fs.st_size, BUFSIZ);
810 fprintf(debug_file, "growing buffer %u -> %u\n",
811 (unsigned)bufsz, (unsigned)newsz);
812 p = bmake_realloc(buf, newsz);
815 *szp = bufsz = newsz;
817 if (!fgets(&buf[x], bufsz - x, fp))
818 return x; /* truncated! */
827 prefix_match(void *p, void *q)
829 const char *prefix = p;
830 const char *path = q;
831 size_t n = strlen(prefix);
833 return (0 == strncmp(path, prefix, n));
837 string_match(const void *p, const void *q)
842 return strcmp(p1, p2);
847 * When running with 'meta' functionality, a target can be out-of-date
848 * if any of the references in its meta data file is more recent.
849 * We have to track the latestdir on a per-process basis.
851 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
852 #define LDIR_VNAME_FMT ".meta.%d.ldir"
855 * It is possible that a .meta file is corrupted,
856 * if we detect this we want to reproduce it.
857 * Setting oodate TRUE will have that effect.
859 #define CHECK_VALID_META(p) if (!(p && *p)) { \
860 warnx("%s: %d: malformed", fname, lineno); \
865 #define DEQUOTE(p) if (*p == '\'') { \
868 if ((ep = strchr(p, '\''))) \
873 meta_oodate(GNode *gn, Boolean oodate)
875 static char *tmpdir = NULL;
876 static char cwd[MAXPATHLEN];
879 char lcwd[MAXPATHLEN];
880 char latestdir[MAXPATHLEN];
881 char fname[MAXPATHLEN];
882 char fname1[MAXPATHLEN];
883 char fname2[MAXPATHLEN];
884 char fname3[MAXPATHLEN];
889 static size_t cwdlen = 0;
890 static size_t tmplen = 0;
892 Boolean needOODATE = FALSE;
896 return oodate; /* we're done */
898 missingFiles = Lst_Init(FALSE);
901 * We need to check if the target is out-of-date. This includes
902 * checking if the expanded command has changed. This in turn
903 * requires that all variables are set in the same way that they
904 * would be if the target needs to be re-built.
908 meta_name(gn, fname, sizeof(fname), NULL, NULL);
910 #ifdef DEBUG_META_MODE
912 fprintf(debug_file, "meta_oodate: %s\n", fname);
915 if ((fp = fopen(fname, "r")) != NULL) {
916 static char *buf = NULL;
928 buf = bmake_malloc(bufsz);
932 if (getcwd(cwd, sizeof(cwd)) == NULL)
933 err(1, "Could not get current working directory");
934 cwdlen = strlen(cwd);
936 strlcpy(lcwd, cwd, sizeof(lcwd));
937 strlcpy(latestdir, cwd, sizeof(latestdir));
940 tmpdir = getTmpdir();
941 tmplen = strlen(tmpdir);
944 /* we want to track all the .meta we read */
945 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
947 ln = Lst_First(gn->commands);
948 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
950 if (buf[x - 1] == '\n')
953 warnx("%s: %d: line truncated at %u", fname, lineno, x);
959 /* Find the start of the build monitor section. */
961 if (strncmp(buf, "-- filemon", 10) == 0) {
965 if (strncmp(buf, "# buildmon", 10) == 0) {
971 /* Delimit the record type. */
973 #ifdef DEBUG_META_MODE
975 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
980 * We are in the 'filemon' output section.
981 * Each record from filemon follows the general form:
986 * <key> is a single letter, denoting the syscall.
987 * <pid> is the process that made the syscall.
988 * <data> is the arguments (of interest).
991 case '#': /* comment */
992 case 'V': /* version */
996 * We need to track pathnames per-process.
998 * Each process run by make, starts off in the 'CWD'
999 * recorded in the .meta file, if it chdirs ('C')
1000 * elsewhere we need to track that - but only for
1001 * that process. If it forks ('F'), we initialize
1002 * the child to have the same cwd as its parent.
1004 * We also need to track the 'latestdir' of
1005 * interest. This is usually the same as cwd, but
1006 * not if a process is reading directories.
1008 * Each time we spot a different process ('pid')
1009 * we save the current value of 'latestdir' in a
1010 * variable qualified by 'lastpid', and
1011 * re-initialize 'latestdir' to any pre-saved
1012 * value for the current 'pid' and 'CWD' if none.
1014 CHECK_VALID_META(p);
1016 if (pid > 0 && pid != lastpid) {
1021 /* We need to remember these. */
1022 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1023 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
1025 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1026 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1028 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1030 strlcpy(latestdir, ldir, sizeof(latestdir));
1034 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1036 strlcpy(lcwd, ldir, sizeof(lcwd));
1041 /* Skip past the pid. */
1042 if (strsep(&p, " ") == NULL)
1044 #ifdef DEBUG_META_MODE
1046 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1048 pid, buf[0], cwd, lcwd, latestdir);
1053 CHECK_VALID_META(p);
1055 /* Process according to record type. */
1057 case 'X': /* eXit */
1058 Var_Delete(lcwd_vname, VAR_GLOBAL);
1059 Var_Delete(ldir_vname, VAR_GLOBAL);
1060 lastpid = 0; /* no need to save ldir_vname */
1063 case 'F': /* [v]Fork */
1070 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1071 Var_Set(cldir, lcwd, VAR_GLOBAL, 0);
1072 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1073 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1074 #ifdef DEBUG_META_MODE
1076 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1078 child, cwd, lcwd, latestdir);
1084 case 'C': /* Chdir */
1085 /* Update lcwd and latest directory. */
1086 strlcpy(latestdir, p, sizeof(latestdir));
1087 strlcpy(lcwd, p, sizeof(lcwd));
1088 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1089 Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0);
1090 #ifdef DEBUG_META_MODE
1092 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1096 case 'M': /* renaMe */
1098 * For 'M'oves we want to check
1099 * the src as for 'R'ead
1100 * and the target as for 'W'rite.
1102 cp = p; /* save this for a second */
1103 /* now get target */
1104 if (strsep(&p, " ") == NULL)
1106 CHECK_VALID_META(p);
1109 /* 'L' and 'M' put single quotes around the args */
1111 DEQUOTE(move_target);
1113 case 'D': /* unlink */
1114 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1115 /* remove p from the missingFiles list if present */
1116 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1117 char *tp = Lst_Datum(ln);
1118 Lst_Remove(missingFiles, ln);
1120 ln = NULL; /* we're done with it */
1123 if (buf[0] == 'M') {
1124 /* the target of the mv is a file 'W'ritten */
1125 #ifdef DEBUG_META_MODE
1127 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1134 case 'L': /* Link */
1137 * the src as for 'R'ead
1138 * and the target as for 'W'rite.
1141 /* now get target */
1142 if (strsep(&p, " ") == NULL)
1144 CHECK_VALID_META(p);
1145 /* 'L' and 'M' put single quotes around the args */
1148 #ifdef DEBUG_META_MODE
1150 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1154 case 'W': /* Write */
1157 * If a file we generated within our bailiwick
1158 * but outside of .OBJDIR is missing,
1159 * we need to do it again.
1161 /* ignore non-absolute paths */
1165 if (Lst_IsEmpty(metaBailiwick))
1168 /* ignore cwd - normal dependencies handle those */
1169 if (strncmp(p, cwd, cwdlen) == 0)
1172 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1175 /* tmpdir might be within */
1176 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1179 /* ignore anything containing the string "tmp" */
1180 if ((strstr("tmp", p)))
1183 if (stat(p, &fs) < 0) {
1184 Lst_AtEnd(missingFiles, bmake_strdup(p));
1190 #ifdef DEBUG_META_MODE
1192 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1195 case 'R': /* Read */
1196 case 'E': /* Exec */
1198 * Check for runtime files that can't
1199 * be part of the dependencies because
1200 * they are _expected_ to change.
1203 Lst_ForEach(metaIgnorePaths, prefix_match, p)) {
1204 #ifdef DEBUG_META_MODE
1206 fprintf(debug_file, "meta_oodate: ignoring: %s\n",
1212 if ((cp = strrchr(p, '/'))) {
1215 * We don't normally expect to see this,
1216 * but we do expect it to change.
1218 if (strcmp(cp, makeDependfile) == 0)
1223 * The rest of the record is the file name.
1224 * Check if it's not an absolute path.
1233 sdirs[sdx++] = p; /* done */
1235 if (strcmp(".", p) == 0)
1236 continue; /* no point */
1238 /* Check vs latestdir */
1239 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1240 sdirs[sdx++] = fname1;
1242 if (strcmp(latestdir, lcwd) != 0) {
1244 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1245 sdirs[sdx++] = fname2;
1247 if (strcmp(lcwd, cwd) != 0) {
1249 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1250 sdirs[sdx++] = fname3;
1253 sdirs[sdx++] = NULL;
1255 for (sdp = sdirs; *sdp && !found; sdp++) {
1256 #ifdef DEBUG_META_MODE
1258 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1260 if (stat(*sdp, &fs) == 0) {
1266 #ifdef DEBUG_META_MODE
1268 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1270 if (!S_ISDIR(fs.st_mode) &&
1271 fs.st_mtime > gn->mtime) {
1273 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1275 } else if (S_ISDIR(fs.st_mode)) {
1276 /* Update the latest directory. */
1277 realpath(p, latestdir);
1279 } else if (errno == ENOENT && *p == '/' &&
1280 strncmp(p, cwd, cwdlen) != 0) {
1282 * A referenced file outside of CWD is missing.
1283 * We cannot catch every eventuality here...
1286 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1290 if (buf[0] == 'E') {
1291 /* previous latestdir is no longer relevant */
1292 strlcpy(latestdir, lcwd, sizeof(latestdir));
1298 if (!oodate && buf[0] == 'L' && link_src != NULL)
1299 goto check_link_src;
1300 } else if (strcmp(buf, "CMD") == 0) {
1302 * Compare the current command with the one in the
1307 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1310 char *cmd = (char *)Lst_Datum(ln);
1311 Boolean hasOODATE = FALSE;
1313 if (strstr(cmd, "$?"))
1315 else if ((cp = strstr(cmd, ".OODATE"))) {
1316 /* check for $[{(].OODATE[:)}] */
1317 if (cp > cmd + 2 && cp[-2] == '$')
1323 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1325 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1327 if ((cp = strchr(cmd, '\n'))) {
1331 * This command contains newlines, we need to
1332 * fetch more from the .meta file before we
1333 * attempt a comparison.
1335 /* first put the newline back at buf[x - 1] */
1338 /* now fetch the next line */
1339 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1343 if (buf[x - 1] != '\n') {
1344 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1347 cp = strchr(++cp, '\n');
1349 if (buf[x - 1] == '\n')
1353 !(gn->type & OP_NOMETA_CMP) &&
1354 strcmp(p, cmd) != 0) {
1356 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1357 if (!metaIgnoreCMDs)
1363 } else if (strcmp(buf, "CWD") == 0) {
1365 * Check if there are extra commands now
1366 * that weren't in the meta data file.
1368 if (!oodate && ln != NULL) {
1370 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1373 if (strcmp(p, cwd) != 0) {
1375 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1382 if (!Lst_IsEmpty(missingFiles)) {
1384 fprintf(debug_file, "%s: missing files: %s...\n",
1385 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1387 Lst_Destroy(missingFiles, (FreeProc *)free);
1390 if ((gn->type & OP_META)) {
1392 fprintf(debug_file, "%s: required but missing\n", fname);
1396 if (oodate && needOODATE) {
1398 * Target uses .OODATE which is empty; or we wouldn't be here.
1399 * We have decided it is oodate, so .OODATE needs to be set.
1400 * All we can sanely do is set it to .ALLSRC.
1402 Var_Delete(OODATE, gn);
1403 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1410 /* support for compat mode */
1412 static int childPipe[2];
1415 meta_compat_start(void)
1417 #ifdef USE_FILEMON_ONCE
1419 * We need to re-open filemon for each cmd.
1421 BuildMon *pbm = &Mybm;
1423 if (pbm->mfp != NULL && useFilemon) {
1426 pbm->mon_fd = pbm->filemon_fd = -1;
1429 if (pipe(childPipe) < 0)
1430 Punt("Cannot create pipe: %s", strerror(errno));
1431 /* Set close-on-exec flag for both */
1432 (void)fcntl(childPipe[0], F_SETFD, 1);
1433 (void)fcntl(childPipe[1], F_SETFD, 1);
1437 meta_compat_child(void)
1439 meta_job_child(NULL);
1440 if (dup2(childPipe[1], 1) < 0 ||
1442 execError("dup2", "pipe");
1448 meta_compat_parent(void)
1453 close(childPipe[1]); /* child side */
1454 fp = fdopen(childPipe[0], "r");
1455 while (fgets(buf, sizeof(buf), fp)) {
1456 meta_job_output(NULL, buf, "");
1462 #endif /* USE_META */