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