1 /* $NetBSD: meta.c,v 1.41 2015/11/30 23:37:56 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 #elif !defined(HAVE_DIRNAME)
44 char * dirname(char *);
47 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
57 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
61 static BuildMon Mybm; /* for compat */
62 static Lst metaBailiwick; /* our scope of control */
63 static Lst metaIgnorePaths; /* paths we deliberately ignore */
65 #ifndef MAKE_META_IGNORE_PATHS
66 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
69 Boolean useMeta = FALSE;
70 static Boolean useFilemon = FALSE;
71 static Boolean writeMeta = FALSE;
72 static Boolean metaEnv = FALSE; /* don't save env unless asked */
73 static Boolean metaVerbose = FALSE;
74 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
75 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
76 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */
78 extern Boolean forceJobs;
79 extern Boolean comatMake;
80 extern char **environ;
82 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
85 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
88 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
91 #if !defined(HAVE_STRSEP)
92 # define strsep(s, d) stresep((s), (d), 0)
96 * Filemon is a kernel module which snoops certain syscalls.
107 * See meta_oodate below - we mainly care about 'E' and 'R'.
109 * We can still use meta mode without filemon, but
110 * the benefits are more limited.
113 # ifndef _PATH_FILEMON
114 # define _PATH_FILEMON "/dev/filemon"
118 * Open the filemon device.
121 filemon_open(BuildMon *pbm)
125 pbm->mon_fd = pbm->filemon_fd = -1;
129 for (retry = 5; retry >= 0; retry--) {
130 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
134 if (pbm->filemon_fd < 0) {
136 warn("Could not open %s", _PATH_FILEMON);
141 * We use a file outside of '.'
142 * to avoid a FreeBSD kernel bug where unlink invalidates
143 * cwd causing getcwd to do a lot more work.
144 * We only care about the descriptor.
146 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
147 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
148 err(1, "Could not set filemon file descriptor!");
150 /* we don't need these once we exec */
151 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
152 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
156 * Read the build monitor output file and write records to the target's
160 filemon_read(FILE *mfp, int fd)
165 /* Check if we're not writing to a meta data file.*/
168 close(fd); /* not interested */
172 (void)lseek(fd, (off_t)0, SEEK_SET);
174 fprintf(mfp, "\n-- filemon acquired metadata --\n");
176 while ((n = read(fd, buf, sizeof(buf))) > 0) {
177 fwrite(buf, 1, n, mfp);
185 * when realpath() fails,
186 * we use this, to clean up ./ and ../
189 eat_dots(char *buf, size_t bufsz, int dots)
210 cp = strstr(buf, eat);
213 if (dots == 2 && cp > buf) {
216 } while (cp > buf && *cp != '/');
219 strlcpy(cp, cp2, bufsz - (cp - buf));
221 return; /* can't happen? */
228 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
232 char buf[MAXPATHLEN];
233 char cwd[MAXPATHLEN];
237 char *p[4]; /* >= number of possible uses */
242 dname = Var_Value(".OBJDIR", gn, &p[i++]);
244 tname = Var_Value(TARGET, gn, &p[i++]);
246 if (realpath(dname, cwd))
250 * Weed out relative paths from the target file name.
251 * We have to be careful though since if target is a
252 * symlink, the result will be unstable.
253 * So we use realpath() just to get the dirname, and leave the
254 * basename as given to us.
256 if ((cp = strrchr(tname, '/'))) {
257 if (realpath(tname, buf)) {
258 if ((rp = strrchr(buf, '/'))) {
261 if (strcmp(cp, rp) != 0)
262 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
267 * We likely have a directory which is about to be made.
268 * We pretend realpath() succeeded, to have a chance
269 * of generating the same meta file name that we will
272 if (tname[0] == '/') {
273 strlcpy(buf, tname, sizeof(buf));
275 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
277 eat_dots(buf, sizeof(buf), 1); /* ./ */
278 eat_dots(buf, sizeof(buf), 2); /* ../ */
282 /* on some systems dirname may modify its arg */
283 tp = bmake_strdup(tname);
284 if (strcmp(dname, dirname(tp)) == 0)
285 snprintf(mname, mnamelen, "%s.meta", tname);
287 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
290 * Replace path separators in the file name after the
291 * current object directory path.
293 cp = mname + strlen(dname) + 1;
295 while (*cp != '\0') {
302 for (i--; i >= 0; i--) {
310 * Return true if running ${.MAKE}
311 * Bypassed if target is flagged .MAKE
314 is_submake(void *cmdp, void *gnp)
316 static char *p_make = NULL;
323 int rc = 0; /* keep looking */
326 p_make = Var_Value(".MAKE", gn, &cp);
327 p_len = strlen(p_make);
329 cp = strchr(cmd, '$');
331 mp = Var_Subst(NULL, cmd, gn, FALSE, TRUE);
334 cp2 = strstr(cmd, p_make);
336 switch (cp2[p_len]) {
344 if (cp2 > cmd && rc > 0) {
351 rc = 0; /* no match */
361 typedef struct meta_file_s {
367 printCMD(void *cmdp, void *mfpp)
369 meta_file_t *mfp = mfpp;
373 if (strchr(cmd, '$')) {
374 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE, TRUE);
376 fprintf(mfp->fp, "CMD %s\n", cmd);
383 * Certain node types never get a .meta file
385 #define SKIP_META_TYPE(_type) do { \
386 if ((gn->type & __CONCAT(OP_, _type))) { \
388 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
389 gn->name, __STRING(_type)); \
396 meta_create(BuildMon *pbm, GNode *gn)
399 char buf[MAXPATHLEN];
400 char objdir[MAXPATHLEN];
406 char *p[4]; /* >= possible uses */
411 /* This may be a phony node which we don't want meta data for... */
412 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
413 /* Or it may be explicitly flagged as .NOMETA */
414 SKIP_META_TYPE(NOMETA);
415 /* Unless it is explicitly flagged as .META */
416 if (!(gn->type & OP_META)) {
417 SKIP_META_TYPE(PHONY);
418 SKIP_META_TYPE(SPECIAL);
419 SKIP_META_TYPE(MAKE);
426 dname = Var_Value(".OBJDIR", gn, &p[i++]);
427 tname = Var_Value(TARGET, gn, &p[i++]);
429 /* The object directory may not exist. Check it.. */
430 if (stat(dname, &fs) != 0) {
432 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
436 /* Check if there are no commands to execute. */
437 if (Lst_IsEmpty(gn->commands)) {
439 fprintf(debug_file, "Skipping meta for %s: no commands\n",
444 /* make sure these are canonical */
445 if (realpath(dname, objdir))
448 /* If we aren't in the object directory, don't create a meta file. */
449 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
451 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
455 if (!(gn->type & OP_META)) {
456 /* We do not generate .meta files for sub-makes */
457 if (Lst_ForEach(gn->commands, is_submake, gn)) {
459 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
468 /* Describe the target we are building */
469 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, FALSE, TRUE);
471 fprintf(stdout, "%s\n", mp);
474 /* Get the basename of the target */
475 if ((cp = strrchr(tname, '/')) == NULL) {
483 if (strcmp(cp, makeDependfile) == 0)
487 /* Don't create meta data. */
490 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
493 #ifdef DEBUG_META_MODE
495 fprintf(debug_file, "meta_create: %s\n", fname);
498 if ((mf.fp = fopen(fname, "w")) == NULL)
499 err(1, "Could not open meta file '%s'", fname);
501 fprintf(mf.fp, "# Meta data file %s\n", fname);
505 Lst_ForEach(gn->commands, printCMD, &mf);
507 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
508 fprintf(mf.fp, "TARGET %s\n", tname);
511 for (ptr = environ; *ptr != NULL; ptr++)
512 fprintf(mf.fp, "ENV %s\n", *ptr);
515 fprintf(mf.fp, "-- command output --\n");
518 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
519 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
521 gn->type |= OP_META; /* in case anyone wants to know */
523 gn->type |= OP_SILENT;
526 for (i--; i >= 0; i--) {
549 * Initialization we need before reading makefiles.
555 /* this allows makefiles to test if we have filemon support */
556 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON, VAR_GLOBAL, 0);
562 * Initialization we need after reading makefiles.
565 meta_mode_init(const char *make_mode)
575 if (strstr(make_mode, "env"))
577 if (strstr(make_mode, "verb"))
579 if (strstr(make_mode, "read"))
581 if (strstr(make_mode, "nofilemon"))
583 if ((cp = strstr(make_mode, "curdirok="))) {
584 metaCurdirOk = boolValue(&cp[9]);
586 if ((cp = strstr(make_mode, "silent="))) {
587 metaSilent = boolValue(&cp[7]);
589 if (strstr(make_mode, "ignore-cmd"))
590 metaIgnoreCMDs = TRUE;
591 /* for backwards compatability */
592 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
593 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
595 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
597 * The default value for MAKE_META_PREFIX
598 * prints the absolute path of the target.
599 * This works be cause :H will generate '.' if there is no /
600 * and :tA will resolve that to cwd.
602 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
607 memset(&Mybm, 0, sizeof(Mybm));
609 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
611 metaBailiwick = Lst_Init(FALSE);
612 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL,
615 str2Lst_Append(metaBailiwick, cp, NULL);
618 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
620 metaIgnorePaths = Lst_Init(FALSE);
621 Var_Append(MAKE_META_IGNORE_PATHS,
622 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
624 "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL,
627 str2Lst_Append(metaIgnorePaths, cp, NULL);
632 * In each case below we allow for job==NULL
635 meta_job_start(Job *job, GNode *gn)
644 pbm->mfp = meta_create(pbm, gn);
645 #ifdef USE_FILEMON_ONCE
646 /* compat mode we open the filemon dev once per command */
651 if (pbm->mfp != NULL && useFilemon) {
654 pbm->mon_fd = pbm->filemon_fd = -1;
660 * The child calls this before doing anything.
661 * It does not disturb our state.
664 meta_job_child(Job *job)
674 if (pbm->mfp != NULL) {
675 close(fileno(pbm->mfp));
680 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
681 err(1, "Could not set filemon pid!");
689 meta_job_error(Job *job, GNode *gn, int flags, int status)
691 char cwd[MAXPATHLEN];
701 if (pbm->mfp != NULL) {
702 fprintf(pbm->mfp, "*** Error code %d%s\n",
704 (flags & JOB_IGNERR) ?
708 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
710 getcwd(cwd, sizeof(cwd));
711 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
712 if (pbm && pbm->meta_fname[0]) {
713 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
715 meta_job_finish(job);
719 meta_job_output(Job *job, char *cp, const char *nl)
728 if (pbm->mfp != NULL) {
730 static char *meta_prefix = NULL;
731 static int meta_prefix_len;
736 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}",
737 VAR_GLOBAL, FALSE, TRUE);
738 if ((cp2 = strchr(meta_prefix, '$')))
739 meta_prefix_len = cp2 - meta_prefix;
741 meta_prefix_len = strlen(meta_prefix);
743 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
744 cp = strchr(cp+1, '\n');
749 fprintf(pbm->mfp, "%s%s", cp, nl);
754 meta_cmd_finish(void *pbmp)
757 BuildMon *pbm = pbmp;
762 if (pbm->filemon_fd >= 0) {
763 close(pbm->filemon_fd);
764 filemon_read(pbm->mfp, pbm->mon_fd);
765 pbm->filemon_fd = pbm->mon_fd = -1;
771 meta_job_finish(Job *job)
780 if (pbm->mfp != NULL) {
781 meta_cmd_finish(pbm);
784 pbm->meta_fname[0] = '\0';
789 * Fetch a full line from fp - growing bufp if needed
790 * Return length in bufp.
793 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
800 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
802 x = o + strlen(&buf[o]);
803 if (buf[x - 1] == '\n')
806 * We need to grow the buffer.
807 * The meta file can give us a clue.
809 if (fstat(fileno(fp), &fs) == 0) {
813 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
815 newsz = ROUNDUP(fs.st_size, BUFSIZ);
817 fprintf(debug_file, "growing buffer %u -> %u\n",
818 (unsigned)bufsz, (unsigned)newsz);
819 p = bmake_realloc(buf, newsz);
822 *szp = bufsz = newsz;
824 if (!fgets(&buf[x], bufsz - x, fp))
825 return x; /* truncated! */
834 prefix_match(void *p, void *q)
836 const char *prefix = p;
837 const char *path = q;
838 size_t n = strlen(prefix);
840 return (0 == strncmp(path, prefix, n));
844 string_match(const void *p, const void *q)
849 return strcmp(p1, p2);
854 * When running with 'meta' functionality, a target can be out-of-date
855 * if any of the references in its meta data file is more recent.
856 * We have to track the latestdir on a per-process basis.
858 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
859 #define LDIR_VNAME_FMT ".meta.%d.ldir"
862 * It is possible that a .meta file is corrupted,
863 * if we detect this we want to reproduce it.
864 * Setting oodate TRUE will have that effect.
866 #define CHECK_VALID_META(p) if (!(p && *p)) { \
867 warnx("%s: %d: malformed", fname, lineno); \
872 #define DEQUOTE(p) if (*p == '\'') { \
875 if ((ep = strchr(p, '\''))) \
880 meta_oodate(GNode *gn, Boolean oodate)
882 static char *tmpdir = NULL;
883 static char cwd[MAXPATHLEN];
886 char lcwd[MAXPATHLEN];
887 char latestdir[MAXPATHLEN];
888 char fname[MAXPATHLEN];
889 char fname1[MAXPATHLEN];
890 char fname2[MAXPATHLEN];
891 char fname3[MAXPATHLEN];
896 static size_t cwdlen = 0;
897 static size_t tmplen = 0;
899 Boolean needOODATE = FALSE;
903 return oodate; /* we're done */
905 missingFiles = Lst_Init(FALSE);
908 * We need to check if the target is out-of-date. This includes
909 * checking if the expanded command has changed. This in turn
910 * requires that all variables are set in the same way that they
911 * would be if the target needs to be re-built.
915 meta_name(gn, fname, sizeof(fname), NULL, NULL);
917 #ifdef DEBUG_META_MODE
919 fprintf(debug_file, "meta_oodate: %s\n", fname);
922 if ((fp = fopen(fname, "r")) != NULL) {
923 static char *buf = NULL;
935 buf = bmake_malloc(bufsz);
939 if (getcwd(cwd, sizeof(cwd)) == NULL)
940 err(1, "Could not get current working directory");
941 cwdlen = strlen(cwd);
943 strlcpy(lcwd, cwd, sizeof(lcwd));
944 strlcpy(latestdir, cwd, sizeof(latestdir));
947 tmpdir = getTmpdir();
948 tmplen = strlen(tmpdir);
951 /* we want to track all the .meta we read */
952 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
954 ln = Lst_First(gn->commands);
955 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
957 if (buf[x - 1] == '\n')
960 warnx("%s: %d: line truncated at %u", fname, lineno, x);
966 /* Find the start of the build monitor section. */
968 if (strncmp(buf, "-- filemon", 10) == 0) {
972 if (strncmp(buf, "# buildmon", 10) == 0) {
978 /* Delimit the record type. */
980 #ifdef DEBUG_META_MODE
982 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
987 * We are in the 'filemon' output section.
988 * Each record from filemon follows the general form:
993 * <key> is a single letter, denoting the syscall.
994 * <pid> is the process that made the syscall.
995 * <data> is the arguments (of interest).
998 case '#': /* comment */
999 case 'V': /* version */
1003 * We need to track pathnames per-process.
1005 * Each process run by make, starts off in the 'CWD'
1006 * recorded in the .meta file, if it chdirs ('C')
1007 * elsewhere we need to track that - but only for
1008 * that process. If it forks ('F'), we initialize
1009 * the child to have the same cwd as its parent.
1011 * We also need to track the 'latestdir' of
1012 * interest. This is usually the same as cwd, but
1013 * not if a process is reading directories.
1015 * Each time we spot a different process ('pid')
1016 * we save the current value of 'latestdir' in a
1017 * variable qualified by 'lastpid', and
1018 * re-initialize 'latestdir' to any pre-saved
1019 * value for the current 'pid' and 'CWD' if none.
1021 CHECK_VALID_META(p);
1023 if (pid > 0 && pid != lastpid) {
1028 /* We need to remember these. */
1029 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1030 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
1032 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1033 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1035 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1037 strlcpy(latestdir, ldir, sizeof(latestdir));
1041 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1043 strlcpy(lcwd, ldir, sizeof(lcwd));
1048 /* Skip past the pid. */
1049 if (strsep(&p, " ") == NULL)
1051 #ifdef DEBUG_META_MODE
1053 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1055 pid, buf[0], cwd, lcwd, latestdir);
1060 CHECK_VALID_META(p);
1062 /* Process according to record type. */
1064 case 'X': /* eXit */
1065 Var_Delete(lcwd_vname, VAR_GLOBAL);
1066 Var_Delete(ldir_vname, VAR_GLOBAL);
1067 lastpid = 0; /* no need to save ldir_vname */
1070 case 'F': /* [v]Fork */
1077 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1078 Var_Set(cldir, lcwd, VAR_GLOBAL, 0);
1079 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1080 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1081 #ifdef DEBUG_META_MODE
1083 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1085 child, cwd, lcwd, latestdir);
1091 case 'C': /* Chdir */
1092 /* Update lcwd and latest directory. */
1093 strlcpy(latestdir, p, sizeof(latestdir));
1094 strlcpy(lcwd, p, sizeof(lcwd));
1095 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1096 Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0);
1097 #ifdef DEBUG_META_MODE
1099 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1103 case 'M': /* renaMe */
1105 * For 'M'oves we want to check
1106 * the src as for 'R'ead
1107 * and the target as for 'W'rite.
1109 cp = p; /* save this for a second */
1110 /* now get target */
1111 if (strsep(&p, " ") == NULL)
1113 CHECK_VALID_META(p);
1116 /* 'L' and 'M' put single quotes around the args */
1118 DEQUOTE(move_target);
1120 case 'D': /* unlink */
1121 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1122 /* remove p from the missingFiles list if present */
1123 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1124 char *tp = Lst_Datum(ln);
1125 Lst_Remove(missingFiles, ln);
1127 ln = NULL; /* we're done with it */
1130 if (buf[0] == 'M') {
1131 /* the target of the mv is a file 'W'ritten */
1132 #ifdef DEBUG_META_MODE
1134 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1141 case 'L': /* Link */
1144 * the src as for 'R'ead
1145 * and the target as for 'W'rite.
1148 /* now get target */
1149 if (strsep(&p, " ") == NULL)
1151 CHECK_VALID_META(p);
1152 /* 'L' and 'M' put single quotes around the args */
1155 #ifdef DEBUG_META_MODE
1157 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1161 case 'W': /* Write */
1164 * If a file we generated within our bailiwick
1165 * but outside of .OBJDIR is missing,
1166 * we need to do it again.
1168 /* ignore non-absolute paths */
1172 if (Lst_IsEmpty(metaBailiwick))
1175 /* ignore cwd - normal dependencies handle those */
1176 if (strncmp(p, cwd, cwdlen) == 0)
1179 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1182 /* tmpdir might be within */
1183 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1186 /* ignore anything containing the string "tmp" */
1187 if ((strstr("tmp", p)))
1190 if ((link_src != NULL && lstat(p, &fs) < 0) ||
1191 (link_src == NULL && stat(p, &fs) < 0)) {
1192 Lst_AtEnd(missingFiles, bmake_strdup(p));
1198 #ifdef DEBUG_META_MODE
1200 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1203 case 'R': /* Read */
1204 case 'E': /* Exec */
1206 * Check for runtime files that can't
1207 * be part of the dependencies because
1208 * they are _expected_ to change.
1211 Lst_ForEach(metaIgnorePaths, prefix_match, p)) {
1212 #ifdef DEBUG_META_MODE
1214 fprintf(debug_file, "meta_oodate: ignoring: %s\n",
1221 * The rest of the record is the file name.
1222 * Check if it's not an absolute path.
1231 sdirs[sdx++] = p; /* done */
1233 if (strcmp(".", p) == 0)
1234 continue; /* no point */
1236 /* Check vs latestdir */
1237 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1238 sdirs[sdx++] = fname1;
1240 if (strcmp(latestdir, lcwd) != 0) {
1242 snprintf(fname2, sizeof(fname2), "%s/%s", lcwd, p);
1243 sdirs[sdx++] = fname2;
1245 if (strcmp(lcwd, cwd) != 0) {
1247 snprintf(fname3, sizeof(fname3), "%s/%s", cwd, p);
1248 sdirs[sdx++] = fname3;
1251 sdirs[sdx++] = NULL;
1253 for (sdp = sdirs; *sdp && !found; sdp++) {
1254 #ifdef DEBUG_META_MODE
1256 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1258 if (stat(*sdp, &fs) == 0) {
1264 #ifdef DEBUG_META_MODE
1266 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1268 if (!S_ISDIR(fs.st_mode) &&
1269 fs.st_mtime > gn->mtime) {
1271 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1273 } else if (S_ISDIR(fs.st_mode)) {
1274 /* Update the latest directory. */
1275 realpath(p, latestdir);
1277 } else if (errno == ENOENT && *p == '/' &&
1278 strncmp(p, cwd, cwdlen) != 0) {
1280 * A referenced file outside of CWD is missing.
1281 * We cannot catch every eventuality here...
1284 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1288 if (buf[0] == 'E') {
1289 /* previous latestdir is no longer relevant */
1290 strlcpy(latestdir, lcwd, sizeof(latestdir));
1296 if (!oodate && buf[0] == 'L' && link_src != NULL)
1297 goto check_link_src;
1298 } else if (strcmp(buf, "CMD") == 0) {
1300 * Compare the current command with the one in the
1305 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1308 char *cmd = (char *)Lst_Datum(ln);
1309 Boolean hasOODATE = FALSE;
1311 if (strstr(cmd, "$?"))
1313 else if ((cp = strstr(cmd, ".OODATE"))) {
1314 /* check for $[{(].OODATE[:)}] */
1315 if (cp > cmd + 2 && cp[-2] == '$')
1321 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1323 cmd = Var_Subst(NULL, cmd, gn, TRUE, TRUE);
1325 if ((cp = strchr(cmd, '\n'))) {
1329 * This command contains newlines, we need to
1330 * fetch more from the .meta file before we
1331 * attempt a comparison.
1333 /* first put the newline back at buf[x - 1] */
1336 /* now fetch the next line */
1337 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1341 if (buf[x - 1] != '\n') {
1342 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1345 cp = strchr(++cp, '\n');
1347 if (buf[x - 1] == '\n')
1351 !(gn->type & OP_NOMETA_CMP) &&
1352 strcmp(p, cmd) != 0) {
1354 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1355 if (!metaIgnoreCMDs)
1361 } else if (strcmp(buf, "CWD") == 0) {
1363 * Check if there are extra commands now
1364 * that weren't in the meta data file.
1366 if (!oodate && ln != NULL) {
1368 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1371 if (strcmp(p, cwd) != 0) {
1373 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1380 if (!Lst_IsEmpty(missingFiles)) {
1382 fprintf(debug_file, "%s: missing files: %s...\n",
1383 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1385 Lst_Destroy(missingFiles, (FreeProc *)free);
1388 if ((gn->type & OP_META)) {
1390 fprintf(debug_file, "%s: required but missing\n", fname);
1394 if (oodate && needOODATE) {
1396 * Target uses .OODATE which is empty; or we wouldn't be here.
1397 * We have decided it is oodate, so .OODATE needs to be set.
1398 * All we can sanely do is set it to .ALLSRC.
1400 Var_Delete(OODATE, gn);
1401 Var_Set(OODATE, Var_Value(ALLSRC, gn, &cp), gn, 0);
1408 /* support for compat mode */
1410 static int childPipe[2];
1413 meta_compat_start(void)
1415 #ifdef USE_FILEMON_ONCE
1417 * We need to re-open filemon for each cmd.
1419 BuildMon *pbm = &Mybm;
1421 if (pbm->mfp != NULL && useFilemon) {
1424 pbm->mon_fd = pbm->filemon_fd = -1;
1427 if (pipe(childPipe) < 0)
1428 Punt("Cannot create pipe: %s", strerror(errno));
1429 /* Set close-on-exec flag for both */
1430 (void)fcntl(childPipe[0], F_SETFD, 1);
1431 (void)fcntl(childPipe[1], F_SETFD, 1);
1435 meta_compat_child(void)
1437 meta_job_child(NULL);
1438 if (dup2(childPipe[1], 1) < 0 ||
1440 execError("dup2", "pipe");
1446 meta_compat_parent(void)
1451 close(childPipe[1]); /* child side */
1452 fp = fdopen(childPipe[0], "r");
1453 while (fgets(buf, sizeof(buf), fp)) {
1454 meta_job_output(NULL, buf, "");
1460 #endif /* USE_META */