1 /* $NetBSD: meta.c,v 1.54 2016/03/11 07:01:21 sjg Exp $ */
4 * Implement 'meta' mode.
5 * Adapted from John Birrell's patches to FreeBSD make.
9 * Copyright (c) 2009-2016, 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>
42 #elif !defined(HAVE_DIRNAME)
43 char * dirname(char *);
46 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
56 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
60 static BuildMon Mybm; /* for compat */
61 static Lst metaBailiwick; /* our scope of control */
62 static char *metaBailiwickStr; /* string storage for the list */
63 static Lst metaIgnorePaths; /* paths we deliberately ignore */
64 static char *metaIgnorePathsStr; /* string storage for the list */
66 #ifndef MAKE_META_IGNORE_PATHS
67 #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
70 Boolean useMeta = FALSE;
71 static Boolean useFilemon = FALSE;
72 static Boolean writeMeta = FALSE;
73 static Boolean metaEnv = FALSE; /* don't save env unless asked */
74 static Boolean metaVerbose = FALSE;
75 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
76 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
77 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */
79 extern Boolean forceJobs;
80 extern Boolean comatMake;
81 extern char **environ;
83 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
86 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
89 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
92 #if !defined(HAVE_STRSEP)
93 # define strsep(s, d) stresep((s), (d), 0)
97 * Filemon is a kernel module which snoops certain syscalls.
108 * See meta_oodate below - we mainly care about 'E' and 'R'.
110 * We can still use meta mode without filemon, but
111 * the benefits are more limited.
114 # ifndef _PATH_FILEMON
115 # define _PATH_FILEMON "/dev/filemon"
119 * Open the filemon device.
122 filemon_open(BuildMon *pbm)
126 pbm->mon_fd = pbm->filemon_fd = -1;
130 for (retry = 5; retry >= 0; retry--) {
131 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
135 if (pbm->filemon_fd < 0) {
137 warn("Could not open %s", _PATH_FILEMON);
142 * We use a file outside of '.'
143 * to avoid a FreeBSD kernel bug where unlink invalidates
144 * cwd causing getcwd to do a lot more work.
145 * We only care about the descriptor.
147 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
148 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
149 err(1, "Could not set filemon file descriptor!");
151 /* we don't need these once we exec */
152 (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
153 (void)fcntl(pbm->filemon_fd, F_SETFD, FD_CLOEXEC);
157 * Read the build monitor output file and write records to the target's
161 filemon_read(FILE *mfp, int fd)
166 /* Check if we're not writing to a meta data file.*/
169 close(fd); /* not interested */
173 (void)lseek(fd, (off_t)0, SEEK_SET);
175 fprintf(mfp, "\n-- filemon acquired metadata --\n");
177 while ((n = read(fd, buf, sizeof(buf))) > 0) {
178 fwrite(buf, 1, n, mfp);
186 * when realpath() fails,
187 * we use this, to clean up ./ and ../
190 eat_dots(char *buf, size_t bufsz, int dots)
211 cp = strstr(buf, eat);
214 if (dots == 2 && cp > buf) {
217 } while (cp > buf && *cp != '/');
220 strlcpy(cp, cp2, bufsz - (cp - buf));
222 return; /* can't happen? */
229 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
233 char buf[MAXPATHLEN];
234 char cwd[MAXPATHLEN];
238 char *p[4]; /* >= number of possible uses */
243 dname = Var_Value(".OBJDIR", gn, &p[i++]);
245 tname = Var_Value(TARGET, gn, &p[i++]);
247 if (realpath(dname, cwd))
251 * Weed out relative paths from the target file name.
252 * We have to be careful though since if target is a
253 * symlink, the result will be unstable.
254 * So we use realpath() just to get the dirname, and leave the
255 * basename as given to us.
257 if ((cp = strrchr(tname, '/'))) {
258 if (realpath(tname, buf)) {
259 if ((rp = strrchr(buf, '/'))) {
262 if (strcmp(cp, rp) != 0)
263 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
268 * We likely have a directory which is about to be made.
269 * We pretend realpath() succeeded, to have a chance
270 * of generating the same meta file name that we will
273 if (tname[0] == '/') {
274 strlcpy(buf, tname, sizeof(buf));
276 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
278 eat_dots(buf, sizeof(buf), 1); /* ./ */
279 eat_dots(buf, sizeof(buf), 2); /* ../ */
283 /* on some systems dirname may modify its arg */
284 tp = bmake_strdup(tname);
285 if (strcmp(dname, dirname(tp)) == 0)
286 snprintf(mname, mnamelen, "%s.meta", tname);
288 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
291 * Replace path separators in the file name after the
292 * current object directory path.
294 cp = mname + strlen(dname) + 1;
296 while (*cp != '\0') {
303 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, VARF_WANTRES);
334 cp2 = strstr(cmd, p_make);
336 switch (cp2[p_len]) {
344 if (cp2 > cmd && rc > 0) {
351 rc = 0; /* no match */
360 typedef struct meta_file_s {
366 printCMD(void *cmdp, void *mfpp)
368 meta_file_t *mfp = mfpp;
372 if (strchr(cmd, '$')) {
373 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, VARF_WANTRES);
375 fprintf(mfp->fp, "CMD %s\n", cmd);
381 * Certain node types never get a .meta file
383 #define SKIP_META_TYPE(_type) do { \
384 if ((gn->type & __CONCAT(OP_, _type))) { \
386 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
387 gn->name, __STRING(_type)); \
394 meta_create(BuildMon *pbm, GNode *gn)
397 char buf[MAXPATHLEN];
398 char objdir[MAXPATHLEN];
404 char *p[4]; /* >= possible uses */
409 /* This may be a phony node which we don't want meta data for... */
410 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
411 /* Or it may be explicitly flagged as .NOMETA */
412 SKIP_META_TYPE(NOMETA);
413 /* Unless it is explicitly flagged as .META */
414 if (!(gn->type & OP_META)) {
415 SKIP_META_TYPE(PHONY);
416 SKIP_META_TYPE(SPECIAL);
417 SKIP_META_TYPE(MAKE);
424 dname = Var_Value(".OBJDIR", gn, &p[i++]);
425 tname = Var_Value(TARGET, gn, &p[i++]);
427 /* The object directory may not exist. Check it.. */
428 if (stat(dname, &fs) != 0) {
430 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
434 /* Check if there are no commands to execute. */
435 if (Lst_IsEmpty(gn->commands)) {
437 fprintf(debug_file, "Skipping meta for %s: no commands\n",
442 /* make sure these are canonical */
443 if (realpath(dname, objdir))
446 /* If we aren't in the object directory, don't create a meta file. */
447 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
449 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
453 if (!(gn->type & OP_META)) {
454 /* We do not generate .meta files for sub-makes */
455 if (Lst_ForEach(gn->commands, is_submake, gn)) {
457 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
466 /* Describe the target we are building */
467 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, VARF_WANTRES);
469 fprintf(stdout, "%s\n", mp);
472 /* Get the basename of the target */
473 if ((cp = strrchr(tname, '/')) == NULL) {
482 /* Don't create meta data. */
485 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
488 #ifdef DEBUG_META_MODE
490 fprintf(debug_file, "meta_create: %s\n", fname);
493 if ((mf.fp = fopen(fname, "w")) == NULL)
494 err(1, "Could not open meta file '%s'", fname);
496 fprintf(mf.fp, "# Meta data file %s\n", fname);
500 Lst_ForEach(gn->commands, printCMD, &mf);
502 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
503 fprintf(mf.fp, "TARGET %s\n", tname);
506 for (ptr = environ; *ptr != NULL; ptr++)
507 fprintf(mf.fp, "ENV %s\n", *ptr);
510 fprintf(mf.fp, "-- command output --\n");
513 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
514 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
516 gn->type |= OP_META; /* in case anyone wants to know */
518 gn->type |= OP_SILENT;
521 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 metaBailiwickStr = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}",
607 VAR_GLOBAL, VARF_WANTRES);
608 if (metaBailiwickStr) {
609 str2Lst_Append(metaBailiwick, metaBailiwickStr, NULL);
612 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
614 metaIgnorePaths = Lst_Init(FALSE);
615 Var_Append(MAKE_META_IGNORE_PATHS,
616 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
617 metaIgnorePathsStr = Var_Subst(NULL,
618 "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL,
620 if (metaIgnorePathsStr) {
621 str2Lst_Append(metaIgnorePaths, metaIgnorePathsStr, 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->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 "}",
731 VAR_GLOBAL, VARF_WANTRES);
732 if ((cp2 = strchr(meta_prefix, '$')))
733 meta_prefix_len = cp2 - meta_prefix;
735 meta_prefix_len = strlen(meta_prefix);
737 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
738 cp = strchr(cp+1, '\n');
743 fprintf(pbm->mfp, "%s%s", cp, nl);
748 meta_cmd_finish(void *pbmp)
751 BuildMon *pbm = pbmp;
756 if (pbm->filemon_fd >= 0) {
757 close(pbm->filemon_fd);
758 filemon_read(pbm->mfp, pbm->mon_fd);
759 pbm->filemon_fd = pbm->mon_fd = -1;
765 meta_job_finish(Job *job)
774 if (pbm->mfp != NULL) {
775 meta_cmd_finish(pbm);
778 pbm->meta_fname[0] = '\0';
785 Lst_Destroy(metaBailiwick, NULL);
786 free(metaBailiwickStr);
787 Lst_Destroy(metaIgnorePaths, NULL);
788 free(metaIgnorePathsStr);
792 * Fetch a full line from fp - growing bufp if needed
793 * Return length in bufp.
796 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
803 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
805 x = o + strlen(&buf[o]);
806 if (buf[x - 1] == '\n')
809 * We need to grow the buffer.
810 * The meta file can give us a clue.
812 if (fstat(fileno(fp), &fs) == 0) {
816 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
818 newsz = ROUNDUP(fs.st_size, BUFSIZ);
820 fprintf(debug_file, "growing buffer %u -> %u\n",
821 (unsigned)bufsz, (unsigned)newsz);
822 p = bmake_realloc(buf, newsz);
825 *szp = bufsz = newsz;
827 if (!fgets(&buf[x], bufsz - x, fp))
828 return x; /* truncated! */
837 prefix_match(void *p, void *q)
839 const char *prefix = p;
840 const char *path = q;
841 size_t n = strlen(prefix);
843 return (0 == strncmp(path, prefix, n));
847 string_match(const void *p, const void *q)
852 return strcmp(p1, p2);
857 * When running with 'meta' functionality, a target can be out-of-date
858 * if any of the references in its meta data file is more recent.
859 * We have to track the latestdir on a per-process basis.
861 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
862 #define LDIR_VNAME_FMT ".meta.%d.ldir"
865 * It is possible that a .meta file is corrupted,
866 * if we detect this we want to reproduce it.
867 * Setting oodate TRUE will have that effect.
869 #define CHECK_VALID_META(p) if (!(p && *p)) { \
870 warnx("%s: %d: malformed", fname, lineno); \
875 #define DEQUOTE(p) if (*p == '\'') { \
878 if ((ep = strchr(p, '\''))) \
883 meta_oodate(GNode *gn, Boolean oodate)
885 static char *tmpdir = NULL;
886 static char cwd[MAXPATHLEN];
889 char lcwd[MAXPATHLEN];
890 char latestdir[MAXPATHLEN];
891 char fname[MAXPATHLEN];
892 char fname1[MAXPATHLEN];
893 char fname2[MAXPATHLEN];
894 char fname3[MAXPATHLEN];
899 static size_t cwdlen = 0;
900 static size_t tmplen = 0;
902 Boolean needOODATE = FALSE;
906 return oodate; /* we're done */
908 missingFiles = Lst_Init(FALSE);
911 * We need to check if the target is out-of-date. This includes
912 * checking if the expanded command has changed. This in turn
913 * requires that all variables are set in the same way that they
914 * would be if the target needs to be re-built.
918 meta_name(gn, fname, sizeof(fname), NULL, NULL);
920 #ifdef DEBUG_META_MODE
922 fprintf(debug_file, "meta_oodate: %s\n", fname);
925 if ((fp = fopen(fname, "r")) != NULL) {
926 static char *buf = NULL;
938 buf = bmake_malloc(bufsz);
942 if (getcwd(cwd, sizeof(cwd)) == NULL)
943 err(1, "Could not get current working directory");
944 cwdlen = strlen(cwd);
946 strlcpy(lcwd, cwd, sizeof(lcwd));
947 strlcpy(latestdir, cwd, sizeof(latestdir));
950 tmpdir = getTmpdir();
951 tmplen = strlen(tmpdir);
954 /* we want to track all the .meta we read */
955 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
957 ln = Lst_First(gn->commands);
958 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
960 if (buf[x - 1] == '\n')
963 warnx("%s: %d: line truncated at %u", fname, lineno, x);
969 /* Find the start of the build monitor section. */
971 if (strncmp(buf, "-- filemon", 10) == 0) {
975 if (strncmp(buf, "# buildmon", 10) == 0) {
981 /* Delimit the record type. */
983 #ifdef DEBUG_META_MODE
985 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
990 * We are in the 'filemon' output section.
991 * Each record from filemon follows the general form:
996 * <key> is a single letter, denoting the syscall.
997 * <pid> is the process that made the syscall.
998 * <data> is the arguments (of interest).
1001 case '#': /* comment */
1002 case 'V': /* version */
1006 * We need to track pathnames per-process.
1008 * Each process run by make, starts off in the 'CWD'
1009 * recorded in the .meta file, if it chdirs ('C')
1010 * elsewhere we need to track that - but only for
1011 * that process. If it forks ('F'), we initialize
1012 * the child to have the same cwd as its parent.
1014 * We also need to track the 'latestdir' of
1015 * interest. This is usually the same as cwd, but
1016 * not if a process is reading directories.
1018 * Each time we spot a different process ('pid')
1019 * we save the current value of 'latestdir' in a
1020 * variable qualified by 'lastpid', and
1021 * re-initialize 'latestdir' to any pre-saved
1022 * value for the current 'pid' and 'CWD' if none.
1024 CHECK_VALID_META(p);
1026 if (pid > 0 && pid != lastpid) {
1031 /* We need to remember these. */
1032 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1033 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
1035 snprintf(lcwd_vname, sizeof(lcwd_vname), LCWD_VNAME_FMT, pid);
1036 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
1038 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
1040 strlcpy(latestdir, ldir, sizeof(latestdir));
1043 ldir = Var_Value(lcwd_vname, VAR_GLOBAL, &tp);
1045 strlcpy(lcwd, ldir, sizeof(lcwd));
1049 /* Skip past the pid. */
1050 if (strsep(&p, " ") == NULL)
1052 #ifdef DEBUG_META_MODE
1054 fprintf(debug_file, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1056 pid, buf[0], cwd, lcwd, latestdir);
1061 CHECK_VALID_META(p);
1063 /* Process according to record type. */
1065 case 'X': /* eXit */
1066 Var_Delete(lcwd_vname, VAR_GLOBAL);
1067 Var_Delete(ldir_vname, VAR_GLOBAL);
1068 lastpid = 0; /* no need to save ldir_vname */
1071 case 'F': /* [v]Fork */
1078 snprintf(cldir, sizeof(cldir), LCWD_VNAME_FMT, child);
1079 Var_Set(cldir, lcwd, VAR_GLOBAL, 0);
1080 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1081 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1082 #ifdef DEBUG_META_MODE
1084 fprintf(debug_file, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1086 child, cwd, lcwd, latestdir);
1092 case 'C': /* Chdir */
1093 /* Update lcwd and latest directory. */
1094 strlcpy(latestdir, p, sizeof(latestdir));
1095 strlcpy(lcwd, p, sizeof(lcwd));
1096 Var_Set(lcwd_vname, lcwd, VAR_GLOBAL, 0);
1097 Var_Set(ldir_vname, lcwd, VAR_GLOBAL, 0);
1098 #ifdef DEBUG_META_MODE
1100 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, lcwd);
1104 case 'M': /* renaMe */
1106 * For 'M'oves we want to check
1107 * the src as for 'R'ead
1108 * and the target as for 'W'rite.
1110 cp = p; /* save this for a second */
1111 /* now get target */
1112 if (strsep(&p, " ") == NULL)
1114 CHECK_VALID_META(p);
1117 /* 'L' and 'M' put single quotes around the args */
1119 DEQUOTE(move_target);
1121 case 'D': /* unlink */
1122 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1123 /* remove p from the missingFiles list if present */
1124 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1125 char *tp = Lst_Datum(ln);
1126 Lst_Remove(missingFiles, ln);
1128 ln = NULL; /* we're done with it */
1131 if (buf[0] == 'M') {
1132 /* the target of the mv is a file 'W'ritten */
1133 #ifdef DEBUG_META_MODE
1135 fprintf(debug_file, "meta_oodate: M %s -> %s\n",
1142 case 'L': /* Link */
1145 * the src as for 'R'ead
1146 * and the target as for 'W'rite.
1149 /* now get target */
1150 if (strsep(&p, " ") == NULL)
1152 CHECK_VALID_META(p);
1153 /* 'L' and 'M' put single quotes around the args */
1156 #ifdef DEBUG_META_MODE
1158 fprintf(debug_file, "meta_oodate: L %s -> %s\n",
1162 case 'W': /* Write */
1165 * If a file we generated within our bailiwick
1166 * but outside of .OBJDIR is missing,
1167 * we need to do it again.
1169 /* ignore non-absolute paths */
1173 if (Lst_IsEmpty(metaBailiwick))
1176 /* ignore cwd - normal dependencies handle those */
1177 if (strncmp(p, cwd, cwdlen) == 0)
1180 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1183 /* tmpdir might be within */
1184 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1187 /* ignore anything containing the string "tmp" */
1188 if ((strstr("tmp", p)))
1191 if ((link_src != NULL && lstat(p, &fs) < 0) ||
1192 (link_src == NULL && stat(p, &fs) < 0)) {
1193 if (Lst_Find(missingFiles, p, string_match) == NULL)
1194 Lst_AtEnd(missingFiles, bmake_strdup(p));
1200 #ifdef DEBUG_META_MODE
1202 fprintf(debug_file, "meta_oodate: L src %s\n", p);
1205 case 'R': /* Read */
1206 case 'E': /* Exec */
1208 * Check for runtime files that can't
1209 * be part of the dependencies because
1210 * they are _expected_ to change.
1213 Lst_ForEach(metaIgnorePaths, prefix_match, p)) {
1214 #ifdef DEBUG_META_MODE
1216 fprintf(debug_file, "meta_oodate: ignoring: %s\n",
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...
1285 if (Lst_Find(missingFiles, p, string_match) == NULL)
1286 Lst_AtEnd(missingFiles, bmake_strdup(p));
1289 if (buf[0] == 'E') {
1290 /* previous latestdir is no longer relevant */
1291 strlcpy(latestdir, lcwd, sizeof(latestdir));
1297 if (!oodate && buf[0] == 'L' && link_src != NULL)
1298 goto check_link_src;
1299 } else if (strcmp(buf, "CMD") == 0) {
1301 * Compare the current command with the one in the
1306 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1309 char *cmd = (char *)Lst_Datum(ln);
1310 Boolean hasOODATE = FALSE;
1312 if (strstr(cmd, "$?"))
1314 else if ((cp = strstr(cmd, ".OODATE"))) {
1315 /* check for $[{(].OODATE[:)}] */
1316 if (cp > cmd + 2 && cp[-2] == '$')
1322 fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
1324 cmd = Var_Subst(NULL, cmd, gn, VARF_WANTRES|VARF_UNDEFERR);
1326 if ((cp = strchr(cmd, '\n'))) {
1330 * This command contains newlines, we need to
1331 * fetch more from the .meta file before we
1332 * attempt a comparison.
1334 /* first put the newline back at buf[x - 1] */
1337 /* now fetch the next line */
1338 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1342 if (buf[x - 1] != '\n') {
1343 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1346 cp = strchr(++cp, '\n');
1348 if (buf[x - 1] == '\n')
1352 !(gn->type & OP_NOMETA_CMP) &&
1353 strcmp(p, cmd) != 0) {
1355 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1356 if (!metaIgnoreCMDs)
1362 } else if (strcmp(buf, "CWD") == 0) {
1364 * Check if there are extra commands now
1365 * that weren't in the meta data file.
1367 if (!oodate && ln != NULL) {
1369 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1372 if (strcmp(p, cwd) != 0) {
1374 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1381 if (!Lst_IsEmpty(missingFiles)) {
1383 fprintf(debug_file, "%s: missing files: %s...\n",
1384 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1388 if ((gn->type & OP_META)) {
1390 fprintf(debug_file, "%s: required but missing\n", fname);
1395 Lst_Destroy(missingFiles, (FreeProc *)free);
1397 if (oodate && needOODATE) {
1399 * Target uses .OODATE which is empty; or we wouldn't be here.
1400 * We have decided it is oodate, so .OODATE needs to be set.
1401 * All we can sanely do is set it to .ALLSRC.
1403 Var_Delete(OODATE, gn);
1404 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, FD_CLOEXEC);
1433 (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
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 */