1 /* $NetBSD: meta.c,v 1.67 2016/08/17 15:52:42 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"
69 #ifndef MAKE_META_IGNORE_PATTERNS
70 #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
72 #ifndef MAKE_META_IGNORE_FILTER
73 #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
76 Boolean useMeta
= FALSE
;
77 static Boolean useFilemon
= FALSE
;
78 static Boolean writeMeta
= FALSE
;
79 static Boolean metaMissing
= FALSE
; /* oodate if missing */
80 static Boolean filemonMissing
= FALSE
; /* oodate if missing */
81 static Boolean metaEnv
= FALSE
; /* don't save env unless asked */
82 static Boolean metaVerbose
= FALSE
;
83 static Boolean metaIgnoreCMDs
= FALSE
; /* ignore CMDs in .meta files */
84 static Boolean metaIgnorePatterns
= FALSE
; /* do we need to do pattern matches */
85 static Boolean metaIgnoreFilter
= FALSE
; /* do we have more complex filtering? */
86 static Boolean metaCurdirOk
= FALSE
; /* write .meta in .CURDIR Ok? */
87 static Boolean metaSilent
= FALSE
; /* if we have a .meta be SILENT */
89 extern Boolean forceJobs
;
90 extern Boolean comatMake
;
91 extern char **environ
;
93 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
96 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
99 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
102 #if !defined(HAVE_STRSEP)
103 # define strsep(s, d) stresep((s), (d), 0)
107 * Filemon is a kernel module which snoops certain syscalls.
118 * See meta_oodate below - we mainly care about 'E' and 'R'.
120 * We can still use meta mode without filemon, but
121 * the benefits are more limited.
124 # ifndef _PATH_FILEMON
125 # define _PATH_FILEMON "/dev/filemon"
129 * Open the filemon device.
132 filemon_open(BuildMon
*pbm
)
136 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
140 for (retry
= 5; retry
>= 0; retry
--) {
141 if ((pbm
->filemon_fd
= open(_PATH_FILEMON
, O_RDWR
)) >= 0)
145 if (pbm
->filemon_fd
< 0) {
147 warn("Could not open %s", _PATH_FILEMON
);
152 * We use a file outside of '.'
153 * to avoid a FreeBSD kernel bug where unlink invalidates
154 * cwd causing getcwd to do a lot more work.
155 * We only care about the descriptor.
157 pbm
->mon_fd
= mkTempFile("filemon.XXXXXX", NULL
);
158 if (ioctl(pbm
->filemon_fd
, FILEMON_SET_FD
, &pbm
->mon_fd
) < 0) {
159 err(1, "Could not set filemon file descriptor!");
161 /* we don't need these once we exec */
162 (void)fcntl(pbm
->mon_fd
, F_SETFD
, FD_CLOEXEC
);
163 (void)fcntl(pbm
->filemon_fd
, F_SETFD
, FD_CLOEXEC
);
167 * Read the build monitor output file and write records to the target's
171 filemon_read(FILE *mfp
, int fd
)
177 /* Check if we're not writing to a meta data file.*/
180 close(fd
); /* not interested */
184 (void)lseek(fd
, (off_t
)0, SEEK_SET
);
187 fprintf(mfp
, "\n-- filemon acquired metadata --\n");
189 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0) {
190 if ((int)fwrite(buf
, 1, n
, mfp
) < n
)
201 * when realpath() fails,
202 * we use this, to clean up ./ and ../
205 eat_dots(char *buf
, size_t bufsz
, int dots
)
226 cp
= strstr(buf
, eat
);
229 if (dots
== 2 && cp
> buf
) {
232 } while (cp
> buf
&& *cp
!= '/');
235 strlcpy(cp
, cp2
, bufsz
- (cp
- buf
));
237 return; /* can't happen? */
244 meta_name(struct GNode __unused
*gn
, char *mname
, size_t mnamelen
,
249 char buf
[MAXPATHLEN
];
255 * Weed out relative paths from the target file name.
256 * We have to be careful though since if target is a
257 * symlink, the result will be unstable.
258 * So we use realpath() just to get the dirname, and leave the
259 * basename as given to us.
261 if ((cp
= strrchr(tname
, '/'))) {
262 if (cached_realpath(tname
, buf
)) {
263 if ((rp
= strrchr(buf
, '/'))) {
266 if (strcmp(cp
, rp
) != 0)
267 strlcpy(rp
, cp
, sizeof(buf
) - (rp
- buf
));
272 * We likely have a directory which is about to be made.
273 * We pretend realpath() succeeded, to have a chance
274 * of generating the same meta file name that we will
277 if (tname
[0] == '/') {
278 strlcpy(buf
, tname
, sizeof(buf
));
280 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, tname
);
282 eat_dots(buf
, sizeof(buf
), 1); /* ./ */
283 eat_dots(buf
, sizeof(buf
), 2); /* ../ */
287 /* on some systems dirname may modify its arg */
288 tp
= bmake_strdup(tname
);
289 if (strcmp(dname
, dirname(tp
)) == 0)
290 snprintf(mname
, mnamelen
, "%s.meta", tname
);
292 snprintf(mname
, mnamelen
, "%s/%s.meta", dname
, tname
);
295 * Replace path separators in the file name after the
296 * current object directory path.
298 cp
= mname
+ strlen(dname
) + 1;
300 while (*cp
!= '\0') {
311 * Return true if running ${.MAKE}
312 * Bypassed if target is flagged .MAKE
315 is_submake(void *cmdp
, void *gnp
)
317 static char *p_make
= NULL
;
324 int rc
= 0; /* keep looking */
327 p_make
= Var_Value(".MAKE", gn
, &cp
);
328 p_len
= strlen(p_make
);
330 cp
= strchr(cmd
, '$');
332 mp
= Var_Subst(NULL
, cmd
, gn
, VARF_WANTRES
);
335 cp2
= strstr(cmd
, p_make
);
337 switch (cp2
[p_len
]) {
345 if (cp2
> cmd
&& rc
> 0) {
352 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
, VARF_WANTRES
);
376 fprintf(mfp
->fp
, "CMD %s\n", cmd
);
382 * Certain node types never get a .meta file
384 #define SKIP_META_TYPE(_type) do { \
385 if ((gn->type & __CONCAT(OP_, _type))) { \
387 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
388 gn->name, __STRING(_type)); \
396 * Do we need/want a .meta file ?
399 meta_needed(GNode
*gn
, const char *dname
, const char __unused
*tname
,
400 char *objdir
, int verbose
)
405 verbose
= DEBUG(META
);
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
);
418 /* Check if there are no commands to execute. */
419 if (Lst_IsEmpty(gn
->commands
)) {
421 fprintf(debug_file
, "Skipping meta for %s: no commands\n",
425 if ((gn
->type
& (OP_META
|OP_SUBMAKE
)) == OP_SUBMAKE
) {
426 /* OP_SUBMAKE is a bit too aggressive */
427 if (Lst_ForEach(gn
->commands
, is_submake
, gn
)) {
429 fprintf(debug_file
, "Skipping meta for %s: .SUBMAKE\n",
435 /* The object directory may not exist. Check it.. */
436 if (cached_stat(dname
, &fs
) != 0) {
438 fprintf(debug_file
, "Skipping meta for %s: no .OBJDIR\n",
443 /* make sure these are canonical */
444 if (cached_realpath(dname
, objdir
))
447 /* If we aren't in the object directory, don't create a meta file. */
448 if (!metaCurdirOk
&& strcmp(curdir
, dname
) == 0) {
450 fprintf(debug_file
, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
459 meta_create(BuildMon
*pbm
, GNode
*gn
)
462 char buf
[MAXPATHLEN
];
463 char objdir
[MAXPATHLEN
];
469 char *p
[4]; /* >= possible uses */
475 dname
= Var_Value(".OBJDIR", gn
, &p
[i
++]);
476 tname
= Var_Value(TARGET
, gn
, &p
[i
++]);
478 /* if this succeeds objdir is realpath of dname */
479 if (!meta_needed(gn
, dname
, tname
, objdir
, TRUE
))
486 /* Describe the target we are building */
487 mp
= Var_Subst(NULL
, "${" MAKE_META_PREFIX
"}", gn
, VARF_WANTRES
);
489 fprintf(stdout
, "%s\n", mp
);
492 /* Get the basename of the target */
493 if ((cp
= strrchr(tname
, '/')) == NULL
) {
502 /* Don't create meta data. */
505 fname
= meta_name(gn
, pbm
->meta_fname
, sizeof(pbm
->meta_fname
),
506 dname
, tname
, objdir
);
508 #ifdef DEBUG_META_MODE
510 fprintf(debug_file
, "meta_create: %s\n", fname
);
513 if ((mf
.fp
= fopen(fname
, "w")) == NULL
)
514 err(1, "Could not open meta file '%s'", fname
);
516 fprintf(mf
.fp
, "# Meta data file %s\n", fname
);
520 Lst_ForEach(gn
->commands
, printCMD
, &mf
);
522 fprintf(mf
.fp
, "CWD %s\n", getcwd(buf
, sizeof(buf
)));
523 fprintf(mf
.fp
, "TARGET %s\n", tname
);
526 for (ptr
= environ
; *ptr
!= NULL
; ptr
++)
527 fprintf(mf
.fp
, "ENV %s\n", *ptr
);
530 fprintf(mf
.fp
, "-- command output --\n");
533 Var_Append(".MAKE.META.FILES", fname
, VAR_GLOBAL
);
534 Var_Append(".MAKE.META.CREATED", fname
, VAR_GLOBAL
);
536 gn
->type
|= OP_META
; /* in case anyone wants to know */
538 gn
->type
|= OP_SILENT
;
541 for (i
--; i
>= 0; i
--) {
563 * Initialization we need before reading makefiles.
569 /* this allows makefiles to test if we have filemon support */
570 Var_Set(".MAKE.PATH_FILEMON", _PATH_FILEMON
, VAR_GLOBAL
, 0);
575 #define get_mode_bf(bf, token) \
576 if ((cp = strstr(make_mode, token))) \
577 bf = boolValue(&cp[sizeof(token) - 1])
580 * Initialization we need after reading makefiles.
583 meta_mode_init(const char *make_mode
)
593 if (strstr(make_mode
, "env"))
595 if (strstr(make_mode
, "verb"))
597 if (strstr(make_mode
, "read"))
599 if (strstr(make_mode
, "nofilemon"))
601 if (strstr(make_mode
, "ignore-cmd"))
602 metaIgnoreCMDs
= TRUE
;
604 get_mode_bf(filemonMissing
, "missing-filemon=");
605 get_mode_bf(metaCurdirOk
, "curdirok=");
606 get_mode_bf(metaMissing
, "missing-meta=");
607 get_mode_bf(metaSilent
, "silent=");
609 if (metaVerbose
&& !Var_Exists(MAKE_META_PREFIX
, VAR_GLOBAL
)) {
611 * The default value for MAKE_META_PREFIX
612 * prints the absolute path of the target.
613 * This works be cause :H will generate '.' if there is no /
614 * and :tA will resolve that to cwd.
616 Var_Set(MAKE_META_PREFIX
, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL
, 0);
621 memset(&Mybm
, 0, sizeof(Mybm
));
623 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
625 metaBailiwick
= Lst_Init(FALSE
);
626 metaBailiwickStr
= Var_Subst(NULL
, "${.MAKE.META.BAILIWICK:O:u:tA}",
627 VAR_GLOBAL
, VARF_WANTRES
);
628 if (metaBailiwickStr
) {
629 str2Lst_Append(metaBailiwick
, metaBailiwickStr
, NULL
);
632 * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
634 metaIgnorePaths
= Lst_Init(FALSE
);
635 Var_Append(MAKE_META_IGNORE_PATHS
,
636 "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL
);
637 metaIgnorePathsStr
= Var_Subst(NULL
,
638 "${" MAKE_META_IGNORE_PATHS
":O:u:tA}", VAR_GLOBAL
,
640 if (metaIgnorePathsStr
) {
641 str2Lst_Append(metaIgnorePaths
, metaIgnorePathsStr
, NULL
);
645 * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
648 if (Var_Value(MAKE_META_IGNORE_PATTERNS
, VAR_GLOBAL
, &cp
)) {
649 metaIgnorePatterns
= TRUE
;
653 if (Var_Value(MAKE_META_IGNORE_FILTER
, VAR_GLOBAL
, &cp
)) {
654 metaIgnoreFilter
= TRUE
;
660 * In each case below we allow for job==NULL
663 meta_job_start(Job
*job
, GNode
*gn
)
672 pbm
->mfp
= meta_create(pbm
, gn
);
673 #ifdef USE_FILEMON_ONCE
674 /* compat mode we open the filemon dev once per command */
679 if (pbm
->mfp
!= NULL
&& useFilemon
) {
682 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
688 * The child calls this before doing anything.
689 * It does not disturb our state.
692 meta_job_child(Job __unused
*job
)
702 if (pbm
->mfp
!= NULL
) {
703 close(fileno(pbm
->mfp
));
708 if (ioctl(pbm
->filemon_fd
, FILEMON_SET_PID
, &pid
) < 0) {
709 err(1, "Could not set filemon pid!");
717 meta_job_error(Job
*job
, GNode
*gn
, int flags
, int status
)
719 char cwd
[MAXPATHLEN
];
729 if (pbm
->mfp
!= NULL
) {
730 fprintf(pbm
->mfp
, "*** Error code %d%s\n",
732 (flags
& JOB_IGNERR
) ?
736 Var_Set(".ERROR_TARGET", gn
->path
? gn
->path
: gn
->name
, VAR_GLOBAL
, 0);
738 getcwd(cwd
, sizeof(cwd
));
739 Var_Set(".ERROR_CWD", cwd
, VAR_GLOBAL
, 0);
740 if (pbm
->meta_fname
[0]) {
741 Var_Set(".ERROR_META_FILE", pbm
->meta_fname
, VAR_GLOBAL
, 0);
743 meta_job_finish(job
);
747 meta_job_output(Job
*job
, char *cp
, const char *nl
)
756 if (pbm
->mfp
!= NULL
) {
758 static char *meta_prefix
= NULL
;
759 static int meta_prefix_len
;
764 meta_prefix
= Var_Subst(NULL
, "${" MAKE_META_PREFIX
"}",
765 VAR_GLOBAL
, VARF_WANTRES
);
766 if ((cp2
= strchr(meta_prefix
, '$')))
767 meta_prefix_len
= cp2
- meta_prefix
;
769 meta_prefix_len
= strlen(meta_prefix
);
771 if (strncmp(cp
, meta_prefix
, meta_prefix_len
) == 0) {
772 cp
= strchr(cp
+1, '\n');
777 fprintf(pbm
->mfp
, "%s%s", cp
, nl
);
782 meta_cmd_finish(void __unused
*pbmp
)
786 BuildMon
*pbm
= pbmp
;
792 if (pbm
->filemon_fd
>= 0) {
793 if (close(pbm
->filemon_fd
) < 0)
795 x
= filemon_read(pbm
->mfp
, pbm
->mon_fd
);
796 if (error
== 0 && x
!= 0)
798 pbm
->filemon_fd
= pbm
->mon_fd
= -1;
805 meta_job_finish(Job
*job
)
816 if (pbm
->mfp
!= NULL
) {
817 error
= meta_cmd_finish(pbm
);
818 x
= fclose(pbm
->mfp
);
819 if (error
== 0 && x
!= 0)
822 pbm
->meta_fname
[0] = '\0';
830 Lst_Destroy(metaBailiwick
, NULL
);
831 free(metaBailiwickStr
);
832 Lst_Destroy(metaIgnorePaths
, NULL
);
833 free(metaIgnorePathsStr
);
837 * Fetch a full line from fp - growing bufp if needed
838 * Return length in bufp.
841 fgetLine(char **bufp
, size_t *szp
, int o
, FILE *fp
)
848 if (fgets(&buf
[o
], bufsz
- o
, fp
) != NULL
) {
850 x
= o
+ strlen(&buf
[o
]);
851 if (buf
[x
- 1] == '\n')
854 * We need to grow the buffer.
855 * The meta file can give us a clue.
857 if (fstat(fileno(fp
), &fs
) == 0) {
861 newsz
= ROUNDUP((fs
.st_size
/ 2), BUFSIZ
);
863 newsz
= ROUNDUP(fs
.st_size
, BUFSIZ
);
865 fprintf(debug_file
, "growing buffer %u -> %u\n",
866 (unsigned)bufsz
, (unsigned)newsz
);
867 p
= bmake_realloc(buf
, newsz
);
870 *szp
= bufsz
= newsz
;
872 if (!fgets(&buf
[x
], bufsz
- x
, fp
))
873 return x
; /* truncated! */
881 /* Lst_ForEach wants 1 to stop search */
883 prefix_match(void *p
, void *q
)
885 const char *prefix
= p
;
886 const char *path
= q
;
887 size_t n
= strlen(prefix
);
889 return (0 == strncmp(path
, prefix
, n
));
893 * looking for exact or prefix/ match to
894 * Lst_Find wants 0 to stop search
897 path_match(const void *p
, const void *q
)
899 const char *prefix
= q
;
900 const char *path
= p
;
901 size_t n
= strlen(prefix
);
904 if ((rc
= strncmp(path
, prefix
, n
)) == 0) {
917 /* Lst_Find wants 0 to stop search */
919 string_match(const void *p
, const void *q
)
924 return strcmp(p1
, p2
);
929 meta_ignore(GNode
*gn
, const char *p
)
931 char fname
[MAXPATHLEN
];
937 cached_realpath(p
, fname
); /* clean it up */
938 if (Lst_ForEach(metaIgnorePaths
, prefix_match
, fname
)) {
939 #ifdef DEBUG_META_MODE
941 fprintf(debug_file
, "meta_oodate: ignoring path: %s\n",
948 if (metaIgnorePatterns
) {
951 snprintf(fname
, sizeof(fname
),
952 "${%s:@m@${%s:L:M$m}@}",
953 MAKE_META_IGNORE_PATTERNS
, p
);
954 pm
= Var_Subst(NULL
, fname
, gn
, VARF_WANTRES
);
956 #ifdef DEBUG_META_MODE
958 fprintf(debug_file
, "meta_oodate: ignoring pattern: %s\n",
967 if (metaIgnoreFilter
) {
970 /* skip if filter result is empty */
971 snprintf(fname
, sizeof(fname
),
973 p
, MAKE_META_IGNORE_FILTER
);
974 fm
= Var_Subst(NULL
, fname
, gn
, VARF_WANTRES
);
976 #ifdef DEBUG_META_MODE
978 fprintf(debug_file
, "meta_oodate: ignoring filtered: %s\n",
990 * When running with 'meta' functionality, a target can be out-of-date
991 * if any of the references in its meta data file is more recent.
992 * We have to track the latestdir on a per-process basis.
994 #define LCWD_VNAME_FMT ".meta.%d.lcwd"
995 #define LDIR_VNAME_FMT ".meta.%d.ldir"
998 * It is possible that a .meta file is corrupted,
999 * if we detect this we want to reproduce it.
1000 * Setting oodate TRUE will have that effect.
1002 #define CHECK_VALID_META(p) if (!(p && *p)) { \
1003 warnx("%s: %d: malformed", fname, lineno); \
1008 #define DEQUOTE(p) if (*p == '\'') { \
1011 if ((ep = strchr(p, '\''))) \
1016 meta_oodate(GNode
*gn
, Boolean oodate
)
1018 static char *tmpdir
= NULL
;
1019 static char cwd
[MAXPATHLEN
];
1020 char lcwd_vname
[64];
1021 char ldir_vname
[64];
1022 char lcwd
[MAXPATHLEN
];
1023 char latestdir
[MAXPATHLEN
];
1024 char fname
[MAXPATHLEN
];
1025 char fname1
[MAXPATHLEN
];
1026 char fname2
[MAXPATHLEN
];
1027 char fname3
[MAXPATHLEN
];
1034 static size_t cwdlen
= 0;
1035 static size_t tmplen
= 0;
1037 Boolean needOODATE
= FALSE
;
1039 char *pa
[4]; /* >= possible uses */
1041 int have_filemon
= FALSE
;
1044 return oodate
; /* we're done */
1048 dname
= Var_Value(".OBJDIR", gn
, &pa
[i
++]);
1049 tname
= Var_Value(TARGET
, gn
, &pa
[i
++]);
1051 /* if this succeeds fname3 is realpath of dname */
1052 if (!meta_needed(gn
, dname
, tname
, fname3
, FALSE
))
1056 missingFiles
= Lst_Init(FALSE
);
1059 * We need to check if the target is out-of-date. This includes
1060 * checking if the expanded command has changed. This in turn
1061 * requires that all variables are set in the same way that they
1062 * would be if the target needs to be re-built.
1066 meta_name(gn
, fname
, sizeof(fname
), dname
, tname
, dname
);
1068 #ifdef DEBUG_META_MODE
1070 fprintf(debug_file
, "meta_oodate: %s\n", fname
);
1073 if ((fp
= fopen(fname
, "r")) != NULL
) {
1074 static char *buf
= NULL
;
1075 static size_t bufsz
;
1085 buf
= bmake_malloc(bufsz
);
1089 if (getcwd(cwd
, sizeof(cwd
)) == NULL
)
1090 err(1, "Could not get current working directory");
1091 cwdlen
= strlen(cwd
);
1093 strlcpy(lcwd
, cwd
, sizeof(lcwd
));
1094 strlcpy(latestdir
, cwd
, sizeof(latestdir
));
1097 tmpdir
= getTmpdir();
1098 tmplen
= strlen(tmpdir
);
1101 /* we want to track all the .meta we read */
1102 Var_Append(".MAKE.META.FILES", fname
, VAR_GLOBAL
);
1104 ln
= Lst_First(gn
->commands
);
1105 while (!oodate
&& (x
= fgetLine(&buf
, &bufsz
, 0, fp
)) > 0) {
1107 if (buf
[x
- 1] == '\n')
1110 warnx("%s: %d: line truncated at %u", fname
, lineno
, x
);
1116 /* Find the start of the build monitor section. */
1117 if (!have_filemon
) {
1118 if (strncmp(buf
, "-- filemon", 10) == 0) {
1119 have_filemon
= TRUE
;
1122 if (strncmp(buf
, "# buildmon", 10) == 0) {
1123 have_filemon
= TRUE
;
1128 /* Delimit the record type. */
1130 #ifdef DEBUG_META_MODE
1132 fprintf(debug_file
, "%s: %d: %s\n", fname
, lineno
, buf
);
1137 * We are in the 'filemon' output section.
1138 * Each record from filemon follows the general form:
1140 * <key> <pid> <data>
1143 * <key> is a single letter, denoting the syscall.
1144 * <pid> is the process that made the syscall.
1145 * <data> is the arguments (of interest).
1148 case '#': /* comment */
1149 case 'V': /* version */
1153 * We need to track pathnames per-process.
1155 * Each process run by make, starts off in the 'CWD'
1156 * recorded in the .meta file, if it chdirs ('C')
1157 * elsewhere we need to track that - but only for
1158 * that process. If it forks ('F'), we initialize
1159 * the child to have the same cwd as its parent.
1161 * We also need to track the 'latestdir' of
1162 * interest. This is usually the same as cwd, but
1163 * not if a process is reading directories.
1165 * Each time we spot a different process ('pid')
1166 * we save the current value of 'latestdir' in a
1167 * variable qualified by 'lastpid', and
1168 * re-initialize 'latestdir' to any pre-saved
1169 * value for the current 'pid' and 'CWD' if none.
1171 CHECK_VALID_META(p
);
1173 if (pid
> 0 && pid
!= lastpid
) {
1178 /* We need to remember these. */
1179 Var_Set(lcwd_vname
, lcwd
, VAR_GLOBAL
, 0);
1180 Var_Set(ldir_vname
, latestdir
, VAR_GLOBAL
, 0);
1182 snprintf(lcwd_vname
, sizeof(lcwd_vname
), LCWD_VNAME_FMT
, pid
);
1183 snprintf(ldir_vname
, sizeof(ldir_vname
), LDIR_VNAME_FMT
, pid
);
1185 ldir
= Var_Value(ldir_vname
, VAR_GLOBAL
, &tp
);
1187 strlcpy(latestdir
, ldir
, sizeof(latestdir
));
1190 ldir
= Var_Value(lcwd_vname
, VAR_GLOBAL
, &tp
);
1192 strlcpy(lcwd
, ldir
, sizeof(lcwd
));
1196 /* Skip past the pid. */
1197 if (strsep(&p
, " ") == NULL
)
1199 #ifdef DEBUG_META_MODE
1201 fprintf(debug_file
, "%s: %d: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1203 pid
, buf
[0], cwd
, lcwd
, latestdir
);
1208 CHECK_VALID_META(p
);
1210 /* Process according to record type. */
1212 case 'X': /* eXit */
1213 Var_Delete(lcwd_vname
, VAR_GLOBAL
);
1214 Var_Delete(ldir_vname
, VAR_GLOBAL
);
1215 lastpid
= 0; /* no need to save ldir_vname */
1218 case 'F': /* [v]Fork */
1225 snprintf(cldir
, sizeof(cldir
), LCWD_VNAME_FMT
, child
);
1226 Var_Set(cldir
, lcwd
, VAR_GLOBAL
, 0);
1227 snprintf(cldir
, sizeof(cldir
), LDIR_VNAME_FMT
, child
);
1228 Var_Set(cldir
, latestdir
, VAR_GLOBAL
, 0);
1229 #ifdef DEBUG_META_MODE
1231 fprintf(debug_file
, "%s: %d: %d: cwd=%s lcwd=%s ldir=%s\n",
1233 child
, cwd
, lcwd
, latestdir
);
1239 case 'C': /* Chdir */
1240 /* Update lcwd and latest directory. */
1241 strlcpy(latestdir
, p
, sizeof(latestdir
));
1242 strlcpy(lcwd
, p
, sizeof(lcwd
));
1243 Var_Set(lcwd_vname
, lcwd
, VAR_GLOBAL
, 0);
1244 Var_Set(ldir_vname
, lcwd
, VAR_GLOBAL
, 0);
1245 #ifdef DEBUG_META_MODE
1247 fprintf(debug_file
, "%s: %d: cwd=%s ldir=%s\n", fname
, lineno
, cwd
, lcwd
);
1251 case 'M': /* renaMe */
1253 * For 'M'oves we want to check
1254 * the src as for 'R'ead
1255 * and the target as for 'W'rite.
1257 cp
= p
; /* save this for a second */
1258 /* now get target */
1259 if (strsep(&p
, " ") == NULL
)
1261 CHECK_VALID_META(p
);
1264 /* 'L' and 'M' put single quotes around the args */
1266 DEQUOTE(move_target
);
1268 case 'D': /* unlink */
1269 if (*p
== '/' && !Lst_IsEmpty(missingFiles
)) {
1270 /* remove any missingFiles entries that match p */
1271 if ((ln
= Lst_Find(missingFiles
, p
,
1272 path_match
)) != NULL
) {
1277 nln
= Lst_FindFrom(missingFiles
, Lst_Succ(ln
),
1280 Lst_Remove(missingFiles
, ln
);
1282 } while ((ln
= nln
) != NULL
);
1285 if (buf
[0] == 'M') {
1286 /* the target of the mv is a file 'W'ritten */
1287 #ifdef DEBUG_META_MODE
1289 fprintf(debug_file
, "meta_oodate: M %s -> %s\n",
1296 case 'L': /* Link */
1299 * the src as for 'R'ead
1300 * and the target as for 'W'rite.
1303 /* now get target */
1304 if (strsep(&p
, " ") == NULL
)
1306 CHECK_VALID_META(p
);
1307 /* 'L' and 'M' put single quotes around the args */
1310 #ifdef DEBUG_META_MODE
1312 fprintf(debug_file
, "meta_oodate: L %s -> %s\n",
1316 case 'W': /* Write */
1319 * If a file we generated within our bailiwick
1320 * but outside of .OBJDIR is missing,
1321 * we need to do it again.
1323 /* ignore non-absolute paths */
1327 if (Lst_IsEmpty(metaBailiwick
))
1330 /* ignore cwd - normal dependencies handle those */
1331 if (strncmp(p
, cwd
, cwdlen
) == 0)
1334 if (!Lst_ForEach(metaBailiwick
, prefix_match
, p
))
1337 /* tmpdir might be within */
1338 if (tmplen
> 0 && strncmp(p
, tmpdir
, tmplen
) == 0)
1341 /* ignore anything containing the string "tmp" */
1342 if ((strstr("tmp", p
)))
1345 if ((link_src
!= NULL
&& cached_lstat(p
, &fs
) < 0) ||
1346 (link_src
== NULL
&& cached_stat(p
, &fs
) < 0)) {
1347 if (!meta_ignore(gn
, p
)) {
1348 if (Lst_Find(missingFiles
, p
, string_match
) == NULL
)
1349 Lst_AtEnd(missingFiles
, bmake_strdup(p
));
1356 #ifdef DEBUG_META_MODE
1358 fprintf(debug_file
, "meta_oodate: L src %s\n", p
);
1361 case 'R': /* Read */
1362 case 'E': /* Exec */
1364 * Check for runtime files that can't
1365 * be part of the dependencies because
1366 * they are _expected_ to change.
1368 if (meta_ignore(gn
, p
))
1372 * The rest of the record is the file name.
1373 * Check if it's not an absolute path.
1382 sdirs
[sdx
++] = p
; /* done */
1384 if (strcmp(".", p
) == 0)
1385 continue; /* no point */
1387 /* Check vs latestdir */
1388 snprintf(fname1
, sizeof(fname1
), "%s/%s", latestdir
, p
);
1389 sdirs
[sdx
++] = fname1
;
1391 if (strcmp(latestdir
, lcwd
) != 0) {
1393 snprintf(fname2
, sizeof(fname2
), "%s/%s", lcwd
, p
);
1394 sdirs
[sdx
++] = fname2
;
1396 if (strcmp(lcwd
, cwd
) != 0) {
1398 snprintf(fname3
, sizeof(fname3
), "%s/%s", cwd
, p
);
1399 sdirs
[sdx
++] = fname3
;
1402 sdirs
[sdx
++] = NULL
;
1404 for (sdp
= sdirs
; *sdp
&& !found
; sdp
++) {
1405 #ifdef DEBUG_META_MODE
1407 fprintf(debug_file
, "%s: %d: looking for: %s\n", fname
, lineno
, *sdp
);
1409 if (cached_stat(*sdp
, &fs
) == 0) {
1415 #ifdef DEBUG_META_MODE
1417 fprintf(debug_file
, "%s: %d: found: %s\n", fname
, lineno
, p
);
1419 if (!S_ISDIR(fs
.st_mode
) &&
1420 fs
.st_mtime
> gn
->mtime
) {
1422 fprintf(debug_file
, "%s: %d: file '%s' is newer than the target...\n", fname
, lineno
, p
);
1424 } else if (S_ISDIR(fs
.st_mode
)) {
1425 /* Update the latest directory. */
1426 cached_realpath(p
, latestdir
);
1428 } else if (errno
== ENOENT
&& *p
== '/' &&
1429 strncmp(p
, cwd
, cwdlen
) != 0) {
1431 * A referenced file outside of CWD is missing.
1432 * We cannot catch every eventuality here...
1434 if (Lst_Find(missingFiles
, p
, string_match
) == NULL
)
1435 Lst_AtEnd(missingFiles
, bmake_strdup(p
));
1438 if (buf
[0] == 'E') {
1439 /* previous latestdir is no longer relevant */
1440 strlcpy(latestdir
, lcwd
, sizeof(latestdir
));
1446 if (!oodate
&& buf
[0] == 'L' && link_src
!= NULL
)
1447 goto check_link_src
;
1448 } else if (strcmp(buf
, "CMD") == 0) {
1450 * Compare the current command with the one in the
1455 fprintf(debug_file
, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname
, lineno
);
1458 char *cmd
= (char *)Lst_Datum(ln
);
1459 Boolean hasOODATE
= FALSE
;
1461 if (strstr(cmd
, "$?"))
1463 else if ((cp
= strstr(cmd
, ".OODATE"))) {
1464 /* check for $[{(].OODATE[:)}] */
1465 if (cp
> cmd
+ 2 && cp
[-2] == '$')
1471 fprintf(debug_file
, "%s: %d: cannot compare command using .OODATE\n", fname
, lineno
);
1473 cmd
= Var_Subst(NULL
, cmd
, gn
, VARF_WANTRES
|VARF_UNDEFERR
);
1475 if ((cp
= strchr(cmd
, '\n'))) {
1479 * This command contains newlines, we need to
1480 * fetch more from the .meta file before we
1481 * attempt a comparison.
1483 /* first put the newline back at buf[x - 1] */
1486 /* now fetch the next line */
1487 if ((n
= fgetLine(&buf
, &bufsz
, x
, fp
)) <= 0)
1491 if (buf
[x
- 1] != '\n') {
1492 warnx("%s: %d: line truncated at %u", fname
, lineno
, x
);
1495 cp
= strchr(++cp
, '\n');
1497 if (buf
[x
- 1] == '\n')
1501 !(gn
->type
& OP_NOMETA_CMP
) &&
1502 strcmp(p
, cmd
) != 0) {
1504 fprintf(debug_file
, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname
, lineno
, p
, cmd
);
1505 if (!metaIgnoreCMDs
)
1511 } else if (strcmp(buf
, "CWD") == 0) {
1513 * Check if there are extra commands now
1514 * that weren't in the meta data file.
1516 if (!oodate
&& ln
!= NULL
) {
1518 fprintf(debug_file
, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname
, lineno
);
1521 if (strcmp(p
, cwd
) != 0) {
1523 fprintf(debug_file
, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname
, lineno
, p
, curdir
);
1530 if (!Lst_IsEmpty(missingFiles
)) {
1532 fprintf(debug_file
, "%s: missing files: %s...\n",
1533 fname
, (char *)Lst_Datum(Lst_First(missingFiles
)));
1536 if (!oodate
&& !have_filemon
&& filemonMissing
) {
1538 fprintf(debug_file
, "%s: missing filemon data\n", fname
);
1542 if (writeMeta
&& metaMissing
) {
1545 /* if target is in .CURDIR we do not need a meta file */
1546 if (gn
->path
&& (cp
= strrchr(gn
->path
, '/')) && cp
> gn
->path
) {
1547 if (strncmp(curdir
, gn
->path
, (cp
- gn
->path
)) != 0) {
1548 cp
= NULL
; /* not in .CURDIR */
1553 fprintf(debug_file
, "%s: required but missing\n", fname
);
1555 needOODATE
= TRUE
; /* assume the worst */
1560 Lst_Destroy(missingFiles
, (FreeProc
*)free
);
1562 if (oodate
&& needOODATE
) {
1564 * Target uses .OODATE which is empty; or we wouldn't be here.
1565 * We have decided it is oodate, so .OODATE needs to be set.
1566 * All we can sanely do is set it to .ALLSRC.
1568 Var_Delete(OODATE
, gn
);
1569 Var_Set(OODATE
, Var_Value(ALLSRC
, gn
, &cp
), gn
, 0);
1574 for (i
--; i
>= 0; i
--) {
1580 /* support for compat mode */
1582 static int childPipe
[2];
1585 meta_compat_start(void)
1587 #ifdef USE_FILEMON_ONCE
1589 * We need to re-open filemon for each cmd.
1591 BuildMon
*pbm
= &Mybm
;
1593 if (pbm
->mfp
!= NULL
&& useFilemon
) {
1596 pbm
->mon_fd
= pbm
->filemon_fd
= -1;
1599 if (pipe(childPipe
) < 0)
1600 Punt("Cannot create pipe: %s", strerror(errno
));
1601 /* Set close-on-exec flag for both */
1602 (void)fcntl(childPipe
[0], F_SETFD
, FD_CLOEXEC
);
1603 (void)fcntl(childPipe
[1], F_SETFD
, FD_CLOEXEC
);
1607 meta_compat_child(void)
1609 meta_job_child(NULL
);
1610 if (dup2(childPipe
[1], 1) < 0 ||
1612 execError("dup2", "pipe");
1618 meta_compat_parent(void)
1623 close(childPipe
[1]); /* child side */
1624 fp
= fdopen(childPipe
[0], "r");
1625 while (fgets(buf
, sizeof(buf
), fp
)) {
1626 meta_job_output(NULL
, buf
, "");
1633 #endif /* USE_META */