meta.c revision 1.197 1 1.197 sjg /* $NetBSD: meta.c,v 1.197 2022/02/08 22:36:02 sjg Exp $ */
2 1.15 sjg
3 1.1 sjg /*
4 1.1 sjg * Implement 'meta' mode.
5 1.1 sjg * Adapted from John Birrell's patches to FreeBSD make.
6 1.1 sjg * --sjg
7 1.1 sjg */
8 1.1 sjg /*
9 1.48 sjg * Copyright (c) 2009-2016, Juniper Networks, Inc.
10 1.1 sjg * Portions Copyright (c) 2009, John Birrell.
11 1.85 rillig *
12 1.1 sjg * Redistribution and use in source and binary forms, with or without
13 1.85 rillig * modification, are permitted provided that the following conditions
14 1.85 rillig * are met:
15 1.1 sjg * 1. Redistributions of source code must retain the above copyright
16 1.85 rillig * notice, this list of conditions and the following disclaimer.
17 1.1 sjg * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 sjg * notice, this list of conditions and the following disclaimer in the
19 1.85 rillig * documentation and/or other materials provided with the distribution.
20 1.85 rillig *
21 1.1 sjg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 1.1 sjg * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 1.1 sjg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 1.1 sjg * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 1.1 sjg * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 sjg * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 1.1 sjg * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 sjg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 sjg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 sjg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.85 rillig * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 sjg */
33 1.3 sjg #if defined(USE_META)
34 1.1 sjg
35 1.1 sjg #ifdef HAVE_CONFIG_H
36 1.1 sjg # include "config.h"
37 1.1 sjg #endif
38 1.1 sjg #include <sys/stat.h>
39 1.1 sjg #include <libgen.h>
40 1.1 sjg #include <errno.h>
41 1.2 sjg #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
42 1.1 sjg #include <err.h>
43 1.2 sjg #endif
44 1.1 sjg
45 1.1 sjg #include "make.h"
46 1.113 rillig #include "dir.h"
47 1.1 sjg #include "job.h"
48 1.1 sjg
49 1.74 riastrad #ifdef USE_FILEMON
50 1.75 riastrad #include "filemon/filemon.h"
51 1.73 maxv #endif
52 1.73 maxv
53 1.1 sjg static BuildMon Mybm; /* for compat */
54 1.155 rillig static StringList metaBailiwick = LST_INIT; /* our scope of control */
55 1.53 christos static char *metaBailiwickStr; /* string storage for the list */
56 1.155 rillig static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
57 1.53 christos static char *metaIgnorePathsStr; /* string storage for the list */
58 1.32 sjg
59 1.32 sjg #ifndef MAKE_META_IGNORE_PATHS
60 1.32 sjg #define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
61 1.32 sjg #endif
62 1.56 sjg #ifndef MAKE_META_IGNORE_PATTERNS
63 1.56 sjg #define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
64 1.56 sjg #endif
65 1.66 sjg #ifndef MAKE_META_IGNORE_FILTER
66 1.66 sjg #define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
67 1.66 sjg #endif
68 1.187 sjg #ifndef MAKE_META_CMP_FILTER
69 1.187 sjg #define MAKE_META_CMP_FILTER ".MAKE.META.CMP_FILTER"
70 1.187 sjg #endif
71 1.1 sjg
72 1.180 rillig bool useMeta = false;
73 1.180 rillig static bool useFilemon = false;
74 1.180 rillig static bool writeMeta = false;
75 1.180 rillig static bool metaMissing = false; /* oodate if missing */
76 1.180 rillig static bool filemonMissing = false; /* oodate if missing */
77 1.180 rillig static bool metaEnv = false; /* don't save env unless asked */
78 1.180 rillig static bool metaVerbose = false;
79 1.180 rillig static bool metaIgnoreCMDs = false; /* ignore CMDs in .meta files */
80 1.180 rillig static bool metaIgnorePatterns = false; /* do we need to do pattern matches */
81 1.180 rillig static bool metaIgnoreFilter = false; /* do we have more complex filtering? */
82 1.187 sjg static bool metaCmpFilter = false; /* do we have CMP_FILTER ? */
83 1.180 rillig static bool metaCurdirOk = false; /* write .meta in .CURDIR Ok? */
84 1.180 rillig static bool metaSilent = false; /* if we have a .meta be SILENT */
85 1.1 sjg
86 1.180 rillig extern bool forceJobs;
87 1.25 sjg extern char **environ;
88 1.1 sjg
89 1.154 rillig #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
90 1.1 sjg
91 1.1 sjg #ifndef N2U
92 1.1 sjg # define N2U(n, u) (((n) + ((u) - 1)) / (u))
93 1.1 sjg #endif
94 1.1 sjg #ifndef ROUNDUP
95 1.1 sjg # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
96 1.1 sjg #endif
97 1.1 sjg
98 1.2 sjg #if !defined(HAVE_STRSEP)
99 1.139 rillig # define strsep(s, d) stresep((s), (d), '\0')
100 1.2 sjg #endif
101 1.2 sjg
102 1.1 sjg /*
103 1.73 maxv * Filemon is a kernel module which snoops certain syscalls.
104 1.73 maxv *
105 1.73 maxv * C chdir
106 1.73 maxv * E exec
107 1.73 maxv * F [v]fork
108 1.73 maxv * L [sym]link
109 1.73 maxv * M rename
110 1.73 maxv * R read
111 1.73 maxv * W write
112 1.73 maxv * S stat
113 1.73 maxv *
114 1.73 maxv * See meta_oodate below - we mainly care about 'E' and 'R'.
115 1.73 maxv *
116 1.85 rillig * We can still use meta mode without filemon, but
117 1.73 maxv * the benefits are more limited.
118 1.73 maxv */
119 1.73 maxv #ifdef USE_FILEMON
120 1.73 maxv
121 1.73 maxv /*
122 1.73 maxv * Open the filemon device.
123 1.73 maxv */
124 1.73 maxv static void
125 1.74 riastrad meta_open_filemon(BuildMon *pbm)
126 1.73 maxv {
127 1.74 riastrad int dupfd;
128 1.74 riastrad
129 1.74 riastrad pbm->mon_fd = -1;
130 1.74 riastrad pbm->filemon = NULL;
131 1.168 rillig if (!useFilemon || pbm->mfp == NULL)
132 1.73 maxv return;
133 1.73 maxv
134 1.74 riastrad pbm->filemon = filemon_open();
135 1.74 riastrad if (pbm->filemon == NULL) {
136 1.180 rillig useFilemon = false;
137 1.75 riastrad warn("Could not open filemon %s", filemon_path());
138 1.73 maxv return;
139 1.73 maxv }
140 1.73 maxv
141 1.73 maxv /*
142 1.73 maxv * We use a file outside of '.'
143 1.73 maxv * to avoid a FreeBSD kernel bug where unlink invalidates
144 1.73 maxv * cwd causing getcwd to do a lot more work.
145 1.73 maxv * We only care about the descriptor.
146 1.73 maxv */
147 1.177 sjg if (!opts.compatMake)
148 1.177 sjg pbm->mon_fd = Job_TempFile("filemon.XXXXXX", NULL, 0);
149 1.177 sjg else
150 1.177 sjg pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL, 0);
151 1.74 riastrad if ((dupfd = dup(pbm->mon_fd)) == -1) {
152 1.184 sjg Punt("Could not dup filemon output: %s", strerror(errno));
153 1.74 riastrad }
154 1.74 riastrad (void)fcntl(dupfd, F_SETFD, FD_CLOEXEC);
155 1.74 riastrad if (filemon_setfd(pbm->filemon, dupfd) == -1) {
156 1.184 sjg Punt("Could not set filemon file descriptor: %s", strerror(errno));
157 1.73 maxv }
158 1.73 maxv /* we don't need these once we exec */
159 1.73 maxv (void)fcntl(pbm->mon_fd, F_SETFD, FD_CLOEXEC);
160 1.73 maxv }
161 1.73 maxv
162 1.73 maxv /*
163 1.73 maxv * Read the build monitor output file and write records to the target's
164 1.73 maxv * metadata file.
165 1.73 maxv */
166 1.73 maxv static int
167 1.73 maxv filemon_read(FILE *mfp, int fd)
168 1.73 maxv {
169 1.73 maxv char buf[BUFSIZ];
170 1.73 maxv int error;
171 1.73 maxv
172 1.73 maxv /* Check if we're not writing to a meta data file.*/
173 1.73 maxv if (mfp == NULL) {
174 1.73 maxv if (fd >= 0)
175 1.73 maxv close(fd); /* not interested */
176 1.73 maxv return 0;
177 1.73 maxv }
178 1.73 maxv /* rewind */
179 1.82 sjg if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
180 1.82 sjg error = errno;
181 1.82 sjg warn("Could not rewind filemon");
182 1.82 sjg fprintf(mfp, "\n");
183 1.82 sjg } else {
184 1.132 rillig ssize_t n;
185 1.124 rillig
186 1.82 sjg error = 0;
187 1.82 sjg fprintf(mfp, "\n-- filemon acquired metadata --\n");
188 1.73 maxv
189 1.138 rillig while ((n = read(fd, buf, sizeof buf)) > 0) {
190 1.124 rillig if ((ssize_t)fwrite(buf, 1, (size_t)n, mfp) < n)
191 1.82 sjg error = EIO;
192 1.82 sjg }
193 1.73 maxv }
194 1.184 sjg if (fflush(mfp) != 0)
195 1.184 sjg Punt("Cannot write filemon data to meta file: %s",
196 1.184 sjg strerror(errno));
197 1.73 maxv if (close(fd) < 0)
198 1.73 maxv error = errno;
199 1.73 maxv return error;
200 1.73 maxv }
201 1.73 maxv #endif
202 1.73 maxv
203 1.73 maxv /*
204 1.8 sjg * when realpath() fails,
205 1.8 sjg * we use this, to clean up ./ and ../
206 1.8 sjg */
207 1.8 sjg static void
208 1.194 rillig eat_dots(char *buf)
209 1.8 sjg {
210 1.194 rillig char *p;
211 1.194 rillig
212 1.194 rillig while ((p = strstr(buf, "/./")) != NULL)
213 1.194 rillig memmove(p, p + 2, strlen(p + 2) + 1);
214 1.85 rillig
215 1.194 rillig while ((p = strstr(buf, "/../")) != NULL) {
216 1.194 rillig char *p2 = p + 3;
217 1.194 rillig if (p > buf) {
218 1.194 rillig do {
219 1.194 rillig p--;
220 1.194 rillig } while (p > buf && *p != '/');
221 1.8 sjg }
222 1.194 rillig if (*p == '/')
223 1.194 rillig memmove(p, p2, strlen(p2) + 1);
224 1.194 rillig else
225 1.194 rillig return; /* can't happen? */
226 1.194 rillig }
227 1.8 sjg }
228 1.8 sjg
229 1.1 sjg static char *
230 1.128 rillig meta_name(char *mname, size_t mnamelen,
231 1.1 sjg const char *dname,
232 1.58 sjg const char *tname,
233 1.58 sjg const char *cwd)
234 1.1 sjg {
235 1.1 sjg char buf[MAXPATHLEN];
236 1.183 rillig char *rp, *cp;
237 1.183 rillig const char *tname_base;
238 1.1 sjg char *tp;
239 1.69 sjg char *dtp;
240 1.69 sjg size_t ldname;
241 1.8 sjg
242 1.1 sjg /*
243 1.1 sjg * Weed out relative paths from the target file name.
244 1.1 sjg * We have to be careful though since if target is a
245 1.1 sjg * symlink, the result will be unstable.
246 1.1 sjg * So we use realpath() just to get the dirname, and leave the
247 1.1 sjg * basename as given to us.
248 1.1 sjg */
249 1.183 rillig if ((tname_base = strrchr(tname, '/')) != NULL) {
250 1.166 rillig if (cached_realpath(tname, buf) != NULL) {
251 1.166 rillig if ((rp = strrchr(buf, '/')) != NULL) {
252 1.1 sjg rp++;
253 1.183 rillig tname_base++;
254 1.183 rillig if (strcmp(tname_base, rp) != 0)
255 1.183 rillig strlcpy(rp, tname_base, sizeof buf - (size_t)(rp - buf));
256 1.1 sjg }
257 1.1 sjg tname = buf;
258 1.8 sjg } else {
259 1.8 sjg /*
260 1.8 sjg * We likely have a directory which is about to be made.
261 1.8 sjg * We pretend realpath() succeeded, to have a chance
262 1.8 sjg * of generating the same meta file name that we will
263 1.8 sjg * next time through.
264 1.8 sjg */
265 1.8 sjg if (tname[0] == '/') {
266 1.138 rillig strlcpy(buf, tname, sizeof buf);
267 1.8 sjg } else {
268 1.138 rillig snprintf(buf, sizeof buf, "%s/%s", cwd, tname);
269 1.8 sjg }
270 1.194 rillig eat_dots(buf);
271 1.8 sjg tname = buf;
272 1.1 sjg }
273 1.1 sjg }
274 1.1 sjg /* on some systems dirname may modify its arg */
275 1.1 sjg tp = bmake_strdup(tname);
276 1.69 sjg dtp = dirname(tp);
277 1.69 sjg if (strcmp(dname, dtp) == 0)
278 1.1 sjg snprintf(mname, mnamelen, "%s.meta", tname);
279 1.1 sjg else {
280 1.69 sjg ldname = strlen(dname);
281 1.69 sjg if (strncmp(dname, dtp, ldname) == 0 && dtp[ldname] == '/')
282 1.69 sjg snprintf(mname, mnamelen, "%s/%s.meta", dname, &tname[ldname+1]);
283 1.69 sjg else
284 1.69 sjg snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
285 1.1 sjg
286 1.1 sjg /*
287 1.1 sjg * Replace path separators in the file name after the
288 1.1 sjg * current object directory path.
289 1.1 sjg */
290 1.1 sjg cp = mname + strlen(dname) + 1;
291 1.1 sjg
292 1.1 sjg while (*cp != '\0') {
293 1.1 sjg if (*cp == '/')
294 1.1 sjg *cp = '_';
295 1.1 sjg cp++;
296 1.1 sjg }
297 1.1 sjg }
298 1.1 sjg free(tp);
299 1.84 rillig return mname;
300 1.1 sjg }
301 1.1 sjg
302 1.1 sjg /*
303 1.1 sjg * Return true if running ${.MAKE}
304 1.1 sjg * Bypassed if target is flagged .MAKE
305 1.1 sjg */
306 1.180 rillig static bool
307 1.149 rillig is_submake(const char *cmd, GNode *gn)
308 1.1 sjg {
309 1.90 rillig static const char *p_make = NULL;
310 1.124 rillig static size_t p_len;
311 1.1 sjg char *mp = NULL;
312 1.190 rillig const char *cp2;
313 1.180 rillig bool rc = false;
314 1.1 sjg
315 1.141 rillig if (p_make == NULL) {
316 1.176 rillig p_make = Var_Value(gn, ".MAKE").str;
317 1.1 sjg p_len = strlen(p_make);
318 1.1 sjg }
319 1.190 rillig if (strchr(cmd, '$') != NULL) {
320 1.117 rillig (void)Var_Subst(cmd, gn, VARE_WANTRES, &mp);
321 1.117 rillig /* TODO: handle errors */
322 1.1 sjg cmd = mp;
323 1.1 sjg }
324 1.1 sjg cp2 = strstr(cmd, p_make);
325 1.139 rillig if (cp2 != NULL) {
326 1.1 sjg switch (cp2[p_len]) {
327 1.1 sjg case '\0':
328 1.1 sjg case ' ':
329 1.1 sjg case '\t':
330 1.1 sjg case '\n':
331 1.180 rillig rc = true;
332 1.1 sjg break;
333 1.1 sjg }
334 1.149 rillig if (cp2 > cmd && rc) {
335 1.1 sjg switch (cp2[-1]) {
336 1.1 sjg case ' ':
337 1.1 sjg case '\t':
338 1.1 sjg case '\n':
339 1.1 sjg break;
340 1.1 sjg default:
341 1.180 rillig rc = false; /* no match */
342 1.1 sjg break;
343 1.1 sjg }
344 1.1 sjg }
345 1.1 sjg }
346 1.44 christos free(mp);
347 1.84 rillig return rc;
348 1.1 sjg }
349 1.1 sjg
350 1.180 rillig static bool
351 1.149 rillig any_is_submake(GNode *gn)
352 1.149 rillig {
353 1.149 rillig StringListNode *ln;
354 1.149 rillig
355 1.153 rillig for (ln = gn->commands.first; ln != NULL; ln = ln->next)
356 1.149 rillig if (is_submake(ln->datum, gn))
357 1.180 rillig return true;
358 1.180 rillig return false;
359 1.149 rillig }
360 1.149 rillig
361 1.119 rillig static void
362 1.178 rillig printCMD(const char *ucmd, FILE *fp, GNode *gn)
363 1.1 sjg {
364 1.178 rillig FStr xcmd = FStr_InitRefer(ucmd);
365 1.1 sjg
366 1.190 rillig Var_Expand(&xcmd, gn, VARE_WANTRES);
367 1.178 rillig fprintf(fp, "CMD %s\n", xcmd.str);
368 1.178 rillig FStr_Done(&xcmd);
369 1.1 sjg }
370 1.1 sjg
371 1.126 rillig static void
372 1.150 rillig printCMDs(GNode *gn, FILE *fp)
373 1.126 rillig {
374 1.162 rillig StringListNode *ln;
375 1.126 rillig
376 1.153 rillig for (ln = gn->commands.first; ln != NULL; ln = ln->next)
377 1.150 rillig printCMD(ln->datum, fp, gn);
378 1.126 rillig }
379 1.126 rillig
380 1.1 sjg /*
381 1.1 sjg * Certain node types never get a .meta file
382 1.1 sjg */
383 1.1 sjg #define SKIP_META_TYPE(_type) do { \
384 1.122 rillig if ((gn->type & __CONCAT(OP_, _type))) { \
385 1.58 sjg if (verbose) { \
386 1.122 rillig debug_printf("Skipping meta for %s: .%s\n", \
387 1.122 rillig gn->name, __STRING(_type)); \
388 1.1 sjg } \
389 1.180 rillig return false; \
390 1.1 sjg } \
391 1.185 rillig } while (false)
392 1.1 sjg
393 1.58 sjg
394 1.58 sjg /*
395 1.58 sjg * Do we need/want a .meta file ?
396 1.58 sjg */
397 1.180 rillig static bool
398 1.157 rillig meta_needed(GNode *gn, const char *dname,
399 1.180 rillig char *objdir_realpath, bool verbose)
400 1.1 sjg {
401 1.143 rillig struct cached_stat cst;
402 1.1 sjg
403 1.58 sjg if (verbose)
404 1.165 rillig verbose = DEBUG(META);
405 1.85 rillig
406 1.1 sjg /* This may be a phony node which we don't want meta data for... */
407 1.1 sjg /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
408 1.1 sjg /* Or it may be explicitly flagged as .NOMETA */
409 1.1 sjg SKIP_META_TYPE(NOMETA);
410 1.1 sjg /* Unless it is explicitly flagged as .META */
411 1.1 sjg if (!(gn->type & OP_META)) {
412 1.1 sjg SKIP_META_TYPE(PHONY);
413 1.1 sjg SKIP_META_TYPE(SPECIAL);
414 1.1 sjg SKIP_META_TYPE(MAKE);
415 1.1 sjg }
416 1.1 sjg
417 1.58 sjg /* Check if there are no commands to execute. */
418 1.153 rillig if (Lst_IsEmpty(&gn->commands)) {
419 1.58 sjg if (verbose)
420 1.122 rillig debug_printf("Skipping meta for %s: no commands\n", gn->name);
421 1.180 rillig return false;
422 1.58 sjg }
423 1.58 sjg if ((gn->type & (OP_META|OP_SUBMAKE)) == OP_SUBMAKE) {
424 1.58 sjg /* OP_SUBMAKE is a bit too aggressive */
425 1.149 rillig if (any_is_submake(gn)) {
426 1.121 rillig DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
427 1.180 rillig return false;
428 1.58 sjg }
429 1.58 sjg }
430 1.58 sjg
431 1.1 sjg /* The object directory may not exist. Check it.. */
432 1.143 rillig if (cached_stat(dname, &cst) != 0) {
433 1.58 sjg if (verbose)
434 1.122 rillig debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
435 1.180 rillig return false;
436 1.1 sjg }
437 1.1 sjg
438 1.1 sjg /* make sure these are canonical */
439 1.166 rillig if (cached_realpath(dname, objdir_realpath) != NULL)
440 1.152 rillig dname = objdir_realpath;
441 1.1 sjg
442 1.1 sjg /* If we aren't in the object directory, don't create a meta file. */
443 1.12 sjg if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
444 1.58 sjg if (verbose)
445 1.122 rillig debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
446 1.122 rillig gn->name);
447 1.180 rillig return false;
448 1.58 sjg }
449 1.180 rillig return true;
450 1.58 sjg }
451 1.58 sjg
452 1.85 rillig
453 1.58 sjg static FILE *
454 1.58 sjg meta_create(BuildMon *pbm, GNode *gn)
455 1.58 sjg {
456 1.150 rillig FILE *fp;
457 1.58 sjg char buf[MAXPATHLEN];
458 1.152 rillig char objdir_realpath[MAXPATHLEN];
459 1.58 sjg char **ptr;
460 1.161 rillig FStr dname;
461 1.58 sjg const char *tname;
462 1.58 sjg char *fname;
463 1.58 sjg const char *cp;
464 1.58 sjg
465 1.150 rillig fp = NULL;
466 1.58 sjg
467 1.176 rillig dname = Var_Value(gn, ".OBJDIR");
468 1.135 rillig tname = GNode_VarTarget(gn);
469 1.58 sjg
470 1.152 rillig /* if this succeeds objdir_realpath is realpath of dname */
471 1.180 rillig if (!meta_needed(gn, dname.str, objdir_realpath, true))
472 1.1 sjg goto out;
473 1.161 rillig dname.str = objdir_realpath;
474 1.1 sjg
475 1.1 sjg if (metaVerbose) {
476 1.1 sjg char *mp;
477 1.1 sjg
478 1.1 sjg /* Describe the target we are building */
479 1.117 rillig (void)Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_WANTRES, &mp);
480 1.117 rillig /* TODO: handle errors */
481 1.146 rillig if (mp[0] != '\0')
482 1.1 sjg fprintf(stdout, "%s\n", mp);
483 1.1 sjg free(mp);
484 1.1 sjg }
485 1.1 sjg /* Get the basename of the target */
486 1.159 rillig cp = str_basename(tname);
487 1.1 sjg
488 1.1 sjg fflush(stdout);
489 1.1 sjg
490 1.1 sjg if (!writeMeta)
491 1.1 sjg /* Don't create meta data. */
492 1.1 sjg goto out;
493 1.1 sjg
494 1.138 rillig fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
495 1.161 rillig dname.str, tname, objdir_realpath);
496 1.1 sjg
497 1.8 sjg #ifdef DEBUG_META_MODE
498 1.121 rillig DEBUG1(META, "meta_create: %s\n", fname);
499 1.8 sjg #endif
500 1.8 sjg
501 1.150 rillig if ((fp = fopen(fname, "w")) == NULL)
502 1.184 sjg Punt("Could not open meta file '%s': %s", fname, strerror(errno));
503 1.1 sjg
504 1.150 rillig fprintf(fp, "# Meta data file %s\n", fname);
505 1.1 sjg
506 1.150 rillig printCMDs(gn, fp);
507 1.1 sjg
508 1.150 rillig fprintf(fp, "CWD %s\n", getcwd(buf, sizeof buf));
509 1.150 rillig fprintf(fp, "TARGET %s\n", tname);
510 1.135 rillig cp = GNode_VarOodate(gn);
511 1.168 rillig if (cp != NULL && *cp != '\0') {
512 1.150 rillig fprintf(fp, "OODATE %s\n", cp);
513 1.76 sjg }
514 1.1 sjg if (metaEnv) {
515 1.1 sjg for (ptr = environ; *ptr != NULL; ptr++)
516 1.150 rillig fprintf(fp, "ENV %s\n", *ptr);
517 1.1 sjg }
518 1.1 sjg
519 1.150 rillig fprintf(fp, "-- command output --\n");
520 1.184 sjg if (fflush(fp) != 0)
521 1.184 sjg Punt("Cannot write expanded command to meta file: %s",
522 1.184 sjg strerror(errno));
523 1.1 sjg
524 1.173 rillig Global_Append(".MAKE.META.FILES", fname);
525 1.173 rillig Global_Append(".MAKE.META.CREATED", fname);
526 1.22 sjg
527 1.22 sjg gn->type |= OP_META; /* in case anyone wants to know */
528 1.22 sjg if (metaSilent) {
529 1.22 sjg gn->type |= OP_SILENT;
530 1.22 sjg }
531 1.1 sjg out:
532 1.161 rillig FStr_Done(&dname);
533 1.1 sjg
534 1.150 rillig return fp;
535 1.1 sjg }
536 1.1 sjg
537 1.180 rillig static bool
538 1.183 rillig boolValue(const char *s)
539 1.12 sjg {
540 1.183 rillig switch (*s) {
541 1.12 sjg case '0':
542 1.12 sjg case 'N':
543 1.12 sjg case 'n':
544 1.12 sjg case 'F':
545 1.12 sjg case 'f':
546 1.180 rillig return false;
547 1.12 sjg }
548 1.180 rillig return true;
549 1.12 sjg }
550 1.1 sjg
551 1.27 sjg /*
552 1.27 sjg * Initialization we need before reading makefiles.
553 1.27 sjg */
554 1.27 sjg void
555 1.30 sjg meta_init(void)
556 1.27 sjg {
557 1.73 maxv #ifdef USE_FILEMON
558 1.73 maxv /* this allows makefiles to test if we have filemon support */
559 1.172 rillig Global_Set(".MAKE.PATH_FILEMON", filemon_path());
560 1.73 maxv #endif
561 1.27 sjg }
562 1.27 sjg
563 1.27 sjg
564 1.58 sjg #define get_mode_bf(bf, token) \
565 1.166 rillig if ((cp = strstr(make_mode, token)) != NULL) \
566 1.138 rillig bf = boolValue(cp + sizeof (token) - 1)
567 1.58 sjg
568 1.27 sjg /*
569 1.27 sjg * Initialization we need after reading makefiles.
570 1.27 sjg */
571 1.1 sjg void
572 1.27 sjg meta_mode_init(const char *make_mode)
573 1.1 sjg {
574 1.180 rillig static bool once = false;
575 1.183 rillig const char *cp;
576 1.1 sjg
577 1.180 rillig useMeta = true;
578 1.180 rillig useFilemon = true;
579 1.180 rillig writeMeta = true;
580 1.1 sjg
581 1.147 rillig if (make_mode != NULL) {
582 1.166 rillig if (strstr(make_mode, "env") != NULL)
583 1.180 rillig metaEnv = true;
584 1.166 rillig if (strstr(make_mode, "verb") != NULL)
585 1.180 rillig metaVerbose = true;
586 1.166 rillig if (strstr(make_mode, "read") != NULL)
587 1.180 rillig writeMeta = false;
588 1.166 rillig if (strstr(make_mode, "nofilemon") != NULL)
589 1.180 rillig useFilemon = false;
590 1.166 rillig if (strstr(make_mode, "ignore-cmd") != NULL)
591 1.180 rillig metaIgnoreCMDs = true;
592 1.58 sjg if (useFilemon)
593 1.58 sjg get_mode_bf(filemonMissing, "missing-filemon=");
594 1.58 sjg get_mode_bf(metaCurdirOk, "curdirok=");
595 1.58 sjg get_mode_bf(metaMissing, "missing-meta=");
596 1.58 sjg get_mode_bf(metaSilent, "silent=");
597 1.1 sjg }
598 1.176 rillig if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
599 1.1 sjg /*
600 1.1 sjg * The default value for MAKE_META_PREFIX
601 1.1 sjg * prints the absolute path of the target.
602 1.1 sjg * This works be cause :H will generate '.' if there is no /
603 1.1 sjg * and :tA will resolve that to cwd.
604 1.1 sjg */
605 1.172 rillig Global_Set(MAKE_META_PREFIX,
606 1.171 rillig "Building ${.TARGET:H:tA}/${.TARGET:T}");
607 1.1 sjg }
608 1.1 sjg if (once)
609 1.1 sjg return;
610 1.180 rillig once = true;
611 1.138 rillig memset(&Mybm, 0, sizeof Mybm);
612 1.17 sjg /*
613 1.17 sjg * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
614 1.17 sjg */
615 1.117 rillig (void)Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
616 1.175 rillig SCOPE_GLOBAL, VARE_WANTRES, &metaBailiwickStr);
617 1.117 rillig /* TODO: handle errors */
618 1.155 rillig str2Lst_Append(&metaBailiwick, metaBailiwickStr);
619 1.32 sjg /*
620 1.32 sjg * We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
621 1.32 sjg */
622 1.173 rillig Global_Append(MAKE_META_IGNORE_PATHS,
623 1.171 rillig "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}");
624 1.117 rillig (void)Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
625 1.175 rillig SCOPE_GLOBAL, VARE_WANTRES, &metaIgnorePathsStr);
626 1.117 rillig /* TODO: handle errors */
627 1.155 rillig str2Lst_Append(&metaIgnorePaths, metaIgnorePathsStr);
628 1.56 sjg
629 1.56 sjg /*
630 1.56 sjg * We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
631 1.56 sjg */
632 1.192 rillig metaIgnorePatterns = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
633 1.192 rillig metaIgnoreFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
634 1.192 rillig metaCmpFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_CMP_FILTER);
635 1.1 sjg }
636 1.1 sjg
637 1.1 sjg /*
638 1.1 sjg * In each case below we allow for job==NULL
639 1.1 sjg */
640 1.1 sjg void
641 1.1 sjg meta_job_start(Job *job, GNode *gn)
642 1.1 sjg {
643 1.1 sjg BuildMon *pbm;
644 1.1 sjg
645 1.1 sjg if (job != NULL) {
646 1.1 sjg pbm = &job->bm;
647 1.1 sjg } else {
648 1.1 sjg pbm = &Mybm;
649 1.1 sjg }
650 1.1 sjg pbm->mfp = meta_create(pbm, gn);
651 1.73 maxv #ifdef USE_FILEMON_ONCE
652 1.73 maxv /* compat mode we open the filemon dev once per command */
653 1.73 maxv if (job == NULL)
654 1.73 maxv return;
655 1.73 maxv #endif
656 1.73 maxv #ifdef USE_FILEMON
657 1.73 maxv if (pbm->mfp != NULL && useFilemon) {
658 1.74 riastrad meta_open_filemon(pbm);
659 1.73 maxv } else {
660 1.74 riastrad pbm->mon_fd = -1;
661 1.74 riastrad pbm->filemon = NULL;
662 1.73 maxv }
663 1.73 maxv #endif
664 1.1 sjg }
665 1.1 sjg
666 1.1 sjg /*
667 1.1 sjg * The child calls this before doing anything.
668 1.1 sjg * It does not disturb our state.
669 1.1 sjg */
670 1.1 sjg void
671 1.1 sjg meta_job_child(Job *job)
672 1.1 sjg {
673 1.73 maxv #ifdef USE_FILEMON
674 1.73 maxv BuildMon *pbm;
675 1.73 maxv
676 1.73 maxv if (job != NULL) {
677 1.73 maxv pbm = &job->bm;
678 1.73 maxv } else {
679 1.73 maxv pbm = &Mybm;
680 1.73 maxv }
681 1.73 maxv if (pbm->mfp != NULL) {
682 1.73 maxv close(fileno(pbm->mfp));
683 1.168 rillig if (useFilemon && pbm->filemon != NULL) {
684 1.73 maxv pid_t pid;
685 1.73 maxv
686 1.73 maxv pid = getpid();
687 1.74 riastrad if (filemon_setpid_child(pbm->filemon, pid) == -1) {
688 1.184 sjg Punt("Could not set filemon pid: %s", strerror(errno));
689 1.73 maxv }
690 1.73 maxv }
691 1.73 maxv }
692 1.73 maxv #endif
693 1.1 sjg }
694 1.1 sjg
695 1.1 sjg void
696 1.74 riastrad meta_job_parent(Job *job, pid_t pid)
697 1.74 riastrad {
698 1.78 sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
699 1.74 riastrad BuildMon *pbm;
700 1.74 riastrad
701 1.74 riastrad if (job != NULL) {
702 1.74 riastrad pbm = &job->bm;
703 1.74 riastrad } else {
704 1.74 riastrad pbm = &Mybm;
705 1.74 riastrad }
706 1.168 rillig if (useFilemon && pbm->filemon != NULL) {
707 1.74 riastrad filemon_setpid_parent(pbm->filemon, pid);
708 1.74 riastrad }
709 1.74 riastrad #endif
710 1.74 riastrad }
711 1.74 riastrad
712 1.74 riastrad int
713 1.74 riastrad meta_job_fd(Job *job)
714 1.74 riastrad {
715 1.78 sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
716 1.74 riastrad BuildMon *pbm;
717 1.74 riastrad
718 1.74 riastrad if (job != NULL) {
719 1.74 riastrad pbm = &job->bm;
720 1.74 riastrad } else {
721 1.74 riastrad pbm = &Mybm;
722 1.74 riastrad }
723 1.168 rillig if (useFilemon && pbm->filemon != NULL) {
724 1.74 riastrad return filemon_readfd(pbm->filemon);
725 1.74 riastrad }
726 1.74 riastrad #endif
727 1.74 riastrad return -1;
728 1.74 riastrad }
729 1.74 riastrad
730 1.74 riastrad int
731 1.74 riastrad meta_job_event(Job *job)
732 1.74 riastrad {
733 1.78 sjg #if defined(USE_FILEMON) && !defined(USE_FILEMON_DEV)
734 1.74 riastrad BuildMon *pbm;
735 1.74 riastrad
736 1.74 riastrad if (job != NULL) {
737 1.74 riastrad pbm = &job->bm;
738 1.74 riastrad } else {
739 1.74 riastrad pbm = &Mybm;
740 1.74 riastrad }
741 1.168 rillig if (useFilemon && pbm->filemon != NULL) {
742 1.74 riastrad return filemon_process(pbm->filemon);
743 1.74 riastrad }
744 1.74 riastrad #endif
745 1.74 riastrad return 0;
746 1.74 riastrad }
747 1.74 riastrad
748 1.74 riastrad void
749 1.180 rillig meta_job_error(Job *job, GNode *gn, bool ignerr, int status)
750 1.1 sjg {
751 1.1 sjg char cwd[MAXPATHLEN];
752 1.1 sjg BuildMon *pbm;
753 1.1 sjg
754 1.1 sjg if (job != NULL) {
755 1.1 sjg pbm = &job->bm;
756 1.141 rillig if (gn == NULL)
757 1.1 sjg gn = job->node;
758 1.52 christos } else {
759 1.1 sjg pbm = &Mybm;
760 1.1 sjg }
761 1.1 sjg if (pbm->mfp != NULL) {
762 1.68 sjg fprintf(pbm->mfp, "\n*** Error code %d%s\n",
763 1.158 rillig status, ignerr ? "(ignored)" : "");
764 1.1 sjg }
765 1.171 rillig if (gn != NULL)
766 1.172 rillig Global_Set(".ERROR_TARGET", GNode_Path(gn));
767 1.138 rillig getcwd(cwd, sizeof cwd);
768 1.172 rillig Global_Set(".ERROR_CWD", cwd);
769 1.139 rillig if (pbm->meta_fname[0] != '\0') {
770 1.172 rillig Global_Set(".ERROR_META_FILE", pbm->meta_fname);
771 1.1 sjg }
772 1.16 sjg meta_job_finish(job);
773 1.1 sjg }
774 1.1 sjg
775 1.1 sjg void
776 1.1 sjg meta_job_output(Job *job, char *cp, const char *nl)
777 1.1 sjg {
778 1.1 sjg BuildMon *pbm;
779 1.85 rillig
780 1.1 sjg if (job != NULL) {
781 1.1 sjg pbm = &job->bm;
782 1.1 sjg } else {
783 1.1 sjg pbm = &Mybm;
784 1.1 sjg }
785 1.1 sjg if (pbm->mfp != NULL) {
786 1.1 sjg if (metaVerbose) {
787 1.1 sjg static char *meta_prefix = NULL;
788 1.124 rillig static size_t meta_prefix_len;
789 1.1 sjg
790 1.141 rillig if (meta_prefix == NULL) {
791 1.1 sjg char *cp2;
792 1.1 sjg
793 1.117 rillig (void)Var_Subst("${" MAKE_META_PREFIX "}",
794 1.175 rillig SCOPE_GLOBAL, VARE_WANTRES, &meta_prefix);
795 1.117 rillig /* TODO: handle errors */
796 1.166 rillig if ((cp2 = strchr(meta_prefix, '$')) != NULL)
797 1.124 rillig meta_prefix_len = (size_t)(cp2 - meta_prefix);
798 1.1 sjg else
799 1.1 sjg meta_prefix_len = strlen(meta_prefix);
800 1.1 sjg }
801 1.1 sjg if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
802 1.164 rillig cp = strchr(cp + 1, '\n');
803 1.164 rillig if (cp == NULL)
804 1.1 sjg return;
805 1.164 rillig cp++;
806 1.1 sjg }
807 1.1 sjg }
808 1.1 sjg fprintf(pbm->mfp, "%s%s", cp, nl);
809 1.1 sjg }
810 1.1 sjg }
811 1.1 sjg
812 1.57 sjg int
813 1.1 sjg meta_cmd_finish(void *pbmp)
814 1.1 sjg {
815 1.57 sjg int error = 0;
816 1.1 sjg BuildMon *pbm = pbmp;
817 1.73 maxv #ifdef USE_FILEMON
818 1.73 maxv int x;
819 1.73 maxv #endif
820 1.1 sjg
821 1.139 rillig if (pbm == NULL)
822 1.1 sjg pbm = &Mybm;
823 1.1 sjg
824 1.73 maxv #ifdef USE_FILEMON
825 1.166 rillig if (pbm->filemon != NULL) {
826 1.74 riastrad while (filemon_process(pbm->filemon) > 0)
827 1.74 riastrad continue;
828 1.184 sjg if (filemon_close(pbm->filemon) == -1) {
829 1.73 maxv error = errno;
830 1.184 sjg Punt("filemon failed: %s", strerror(errno));
831 1.184 sjg }
832 1.73 maxv x = filemon_read(pbm->mfp, pbm->mon_fd);
833 1.73 maxv if (error == 0 && x != 0)
834 1.73 maxv error = x;
835 1.74 riastrad pbm->mon_fd = -1;
836 1.74 riastrad pbm->filemon = NULL;
837 1.140 rillig return error;
838 1.140 rillig }
839 1.73 maxv #endif
840 1.140 rillig
841 1.140 rillig fprintf(pbm->mfp, "\n"); /* ensure end with newline */
842 1.57 sjg return error;
843 1.1 sjg }
844 1.1 sjg
845 1.57 sjg int
846 1.1 sjg meta_job_finish(Job *job)
847 1.1 sjg {
848 1.1 sjg BuildMon *pbm;
849 1.57 sjg int error = 0;
850 1.57 sjg int x;
851 1.1 sjg
852 1.1 sjg if (job != NULL) {
853 1.1 sjg pbm = &job->bm;
854 1.1 sjg } else {
855 1.1 sjg pbm = &Mybm;
856 1.1 sjg }
857 1.1 sjg if (pbm->mfp != NULL) {
858 1.57 sjg error = meta_cmd_finish(pbm);
859 1.57 sjg x = fclose(pbm->mfp);
860 1.57 sjg if (error == 0 && x != 0)
861 1.57 sjg error = errno;
862 1.1 sjg pbm->mfp = NULL;
863 1.6 sjg pbm->meta_fname[0] = '\0';
864 1.1 sjg }
865 1.57 sjg return error;
866 1.1 sjg }
867 1.1 sjg
868 1.53 christos void
869 1.53 christos meta_finish(void)
870 1.53 christos {
871 1.155 rillig Lst_Done(&metaBailiwick);
872 1.53 christos free(metaBailiwickStr);
873 1.155 rillig Lst_Done(&metaIgnorePaths);
874 1.53 christos free(metaIgnorePathsStr);
875 1.53 christos }
876 1.53 christos
877 1.1 sjg /*
878 1.1 sjg * Fetch a full line from fp - growing bufp if needed
879 1.1 sjg * Return length in bufp.
880 1.1 sjg */
881 1.85 rillig static int
882 1.1 sjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
883 1.1 sjg {
884 1.1 sjg char *buf = *bufp;
885 1.1 sjg size_t bufsz = *szp;
886 1.1 sjg struct stat fs;
887 1.1 sjg int x;
888 1.1 sjg
889 1.124 rillig if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
890 1.1 sjg check_newline:
891 1.124 rillig x = o + (int)strlen(&buf[o]);
892 1.1 sjg if (buf[x - 1] == '\n')
893 1.1 sjg return x;
894 1.1 sjg /*
895 1.1 sjg * We need to grow the buffer.
896 1.1 sjg * The meta file can give us a clue.
897 1.1 sjg */
898 1.1 sjg if (fstat(fileno(fp), &fs) == 0) {
899 1.1 sjg size_t newsz;
900 1.1 sjg char *p;
901 1.1 sjg
902 1.124 rillig newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
903 1.1 sjg if (newsz <= bufsz)
904 1.124 rillig newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
905 1.68 sjg if (newsz <= bufsz)
906 1.68 sjg return x; /* truncated */
907 1.160 rillig DEBUG2(META, "growing buffer %u -> %u\n",
908 1.160 rillig (unsigned)bufsz, (unsigned)newsz);
909 1.1 sjg p = bmake_realloc(buf, newsz);
910 1.148 rillig *bufp = buf = p;
911 1.148 rillig *szp = bufsz = newsz;
912 1.148 rillig /* fetch the rest */
913 1.148 rillig if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
914 1.148 rillig return x; /* truncated! */
915 1.148 rillig goto check_newline;
916 1.1 sjg }
917 1.1 sjg }
918 1.1 sjg return 0;
919 1.1 sjg }
920 1.1 sjg
921 1.180 rillig static bool
922 1.149 rillig prefix_match(const char *prefix, const char *path)
923 1.17 sjg {
924 1.17 sjg size_t n = strlen(prefix);
925 1.17 sjg
926 1.84 rillig return strncmp(path, prefix, n) == 0;
927 1.17 sjg }
928 1.17 sjg
929 1.180 rillig static bool
930 1.149 rillig has_any_prefix(const char *path, StringList *prefixes)
931 1.149 rillig {
932 1.149 rillig StringListNode *ln;
933 1.149 rillig
934 1.149 rillig for (ln = prefixes->first; ln != NULL; ln = ln->next)
935 1.149 rillig if (prefix_match(ln->datum, path))
936 1.180 rillig return true;
937 1.180 rillig return false;
938 1.149 rillig }
939 1.149 rillig
940 1.108 rillig /* See if the path equals prefix or starts with "prefix/". */
941 1.180 rillig static bool
942 1.129 rillig path_starts_with(const char *path, const char *prefix)
943 1.64 sjg {
944 1.64 sjg size_t n = strlen(prefix);
945 1.64 sjg
946 1.108 rillig if (strncmp(path, prefix, n) != 0)
947 1.180 rillig return false;
948 1.108 rillig return path[n] == '\0' || path[n] == '/';
949 1.64 sjg }
950 1.64 sjg
951 1.180 rillig static bool
952 1.67 sjg meta_ignore(GNode *gn, const char *p)
953 1.67 sjg {
954 1.67 sjg char fname[MAXPATHLEN];
955 1.67 sjg
956 1.67 sjg if (p == NULL)
957 1.180 rillig return true;
958 1.67 sjg
959 1.67 sjg if (*p == '/') {
960 1.67 sjg cached_realpath(p, fname); /* clean it up */
961 1.155 rillig if (has_any_prefix(fname, &metaIgnorePaths)) {
962 1.67 sjg #ifdef DEBUG_META_MODE
963 1.121 rillig DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
964 1.67 sjg #endif
965 1.180 rillig return true;
966 1.67 sjg }
967 1.67 sjg }
968 1.67 sjg
969 1.67 sjg if (metaIgnorePatterns) {
970 1.97 rillig const char *expr;
971 1.97 rillig char *pm;
972 1.92 rillig
973 1.176 rillig /*
974 1.176 rillig * XXX: This variable is set on a target GNode but is not one of
975 1.176 rillig * the usual local variables. It should be deleted afterwards.
976 1.176 rillig * Ideally it would not be created in the first place, just like
977 1.176 rillig * in a .for loop.
978 1.176 rillig */
979 1.176 rillig Var_Set(gn, ".p.", p);
980 1.92 rillig expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
981 1.117 rillig (void)Var_Subst(expr, gn, VARE_WANTRES, &pm);
982 1.117 rillig /* TODO: handle errors */
983 1.146 rillig if (pm[0] != '\0') {
984 1.67 sjg #ifdef DEBUG_META_MODE
985 1.121 rillig DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
986 1.67 sjg #endif
987 1.67 sjg free(pm);
988 1.180 rillig return true;
989 1.67 sjg }
990 1.67 sjg free(pm);
991 1.67 sjg }
992 1.67 sjg
993 1.67 sjg if (metaIgnoreFilter) {
994 1.67 sjg char *fm;
995 1.67 sjg
996 1.67 sjg /* skip if filter result is empty */
997 1.138 rillig snprintf(fname, sizeof fname,
998 1.67 sjg "${%s:L:${%s:ts:}}",
999 1.67 sjg p, MAKE_META_IGNORE_FILTER);
1000 1.117 rillig (void)Var_Subst(fname, gn, VARE_WANTRES, &fm);
1001 1.117 rillig /* TODO: handle errors */
1002 1.67 sjg if (*fm == '\0') {
1003 1.67 sjg #ifdef DEBUG_META_MODE
1004 1.121 rillig DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
1005 1.67 sjg #endif
1006 1.67 sjg free(fm);
1007 1.180 rillig return true;
1008 1.67 sjg }
1009 1.67 sjg free(fm);
1010 1.67 sjg }
1011 1.180 rillig return false;
1012 1.67 sjg }
1013 1.67 sjg
1014 1.1 sjg /*
1015 1.1 sjg * When running with 'meta' functionality, a target can be out-of-date
1016 1.34 snj * if any of the references in its meta data file is more recent.
1017 1.5 sjg * We have to track the latestdir on a per-process basis.
1018 1.1 sjg */
1019 1.38 sjg #define LCWD_VNAME_FMT ".meta.%d.lcwd"
1020 1.5 sjg #define LDIR_VNAME_FMT ".meta.%d.ldir"
1021 1.5 sjg
1022 1.20 sjg /*
1023 1.20 sjg * It is possible that a .meta file is corrupted,
1024 1.20 sjg * if we detect this we want to reproduce it.
1025 1.180 rillig * Setting oodate true will have that effect.
1026 1.20 sjg */
1027 1.168 rillig #define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
1028 1.196 rillig warnx("%s: %u: malformed", fname, lineno); \
1029 1.180 rillig oodate = true; \
1030 1.20 sjg continue; \
1031 1.20 sjg }
1032 1.20 sjg
1033 1.33 sjg #define DEQUOTE(p) if (*p == '\'') { \
1034 1.33 sjg char *ep; \
1035 1.33 sjg p++; \
1036 1.166 rillig if ((ep = strchr(p, '\'')) != NULL) \
1037 1.33 sjg *ep = '\0'; \
1038 1.33 sjg }
1039 1.33 sjg
1040 1.130 rillig static void
1041 1.130 rillig append_if_new(StringList *list, const char *str)
1042 1.130 rillig {
1043 1.130 rillig StringListNode *ln;
1044 1.130 rillig
1045 1.130 rillig for (ln = list->first; ln != NULL; ln = ln->next)
1046 1.132 rillig if (strcmp(ln->datum, str) == 0)
1047 1.130 rillig return;
1048 1.130 rillig Lst_Append(list, bmake_strdup(str));
1049 1.130 rillig }
1050 1.130 rillig
1051 1.197 sjg /* A "reserved" variable to store the command to be filtered */
1052 1.197 sjg #define META_CMD_FILTER_VAR ".MAKE.cmd_filtered"
1053 1.197 sjg
1054 1.187 sjg static char *
1055 1.197 sjg meta_filter_cmd(GNode *gn, char *s)
1056 1.187 sjg {
1057 1.197 sjg Var_Set(gn, META_CMD_FILTER_VAR, s);
1058 1.197 sjg Var_Subst("${" META_CMD_FILTER_VAR ":${" MAKE_META_CMP_FILTER ":ts:}}", gn, VARE_WANTRES, &s);
1059 1.187 sjg return s;
1060 1.187 sjg }
1061 1.189 rillig
1062 1.187 sjg static int
1063 1.195 sjg meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
1064 1.187 sjg {
1065 1.187 sjg int rc;
1066 1.187 sjg
1067 1.187 sjg rc = strcmp(a, b);
1068 1.195 sjg if (rc == 0 || !filter)
1069 1.187 sjg return rc;
1070 1.197 sjg a = meta_filter_cmd(gn, a);
1071 1.197 sjg b = meta_filter_cmd(gn, b);
1072 1.187 sjg rc = strcmp(a, b);
1073 1.187 sjg free(a);
1074 1.187 sjg free(b);
1075 1.197 sjg Var_Delete(gn, META_CMD_FILTER_VAR);
1076 1.187 sjg return rc;
1077 1.187 sjg }
1078 1.187 sjg
1079 1.180 rillig bool
1080 1.180 rillig meta_oodate(GNode *gn, bool oodate)
1081 1.1 sjg {
1082 1.5 sjg static char *tmpdir = NULL;
1083 1.11 sjg static char cwd[MAXPATHLEN];
1084 1.38 sjg char lcwd_vname[64];
1085 1.5 sjg char ldir_vname[64];
1086 1.38 sjg char lcwd[MAXPATHLEN];
1087 1.1 sjg char latestdir[MAXPATHLEN];
1088 1.1 sjg char fname[MAXPATHLEN];
1089 1.1 sjg char fname1[MAXPATHLEN];
1090 1.5 sjg char fname2[MAXPATHLEN];
1091 1.38 sjg char fname3[MAXPATHLEN];
1092 1.161 rillig FStr dname;
1093 1.58 sjg const char *tname;
1094 1.1 sjg char *p;
1095 1.33 sjg char *link_src;
1096 1.33 sjg char *move_target;
1097 1.11 sjg static size_t cwdlen = 0;
1098 1.7 sjg static size_t tmplen = 0;
1099 1.1 sjg FILE *fp;
1100 1.180 rillig bool needOODATE = false;
1101 1.156 rillig StringList missingFiles;
1102 1.180 rillig bool have_filemon = false;
1103 1.195 sjg bool cmp_filter;
1104 1.58 sjg
1105 1.4 sjg if (oodate)
1106 1.4 sjg return oodate; /* we're done */
1107 1.4 sjg
1108 1.176 rillig dname = Var_Value(gn, ".OBJDIR");
1109 1.135 rillig tname = GNode_VarTarget(gn);
1110 1.58 sjg
1111 1.58 sjg /* if this succeeds fname3 is realpath of dname */
1112 1.180 rillig if (!meta_needed(gn, dname.str, fname3, false))
1113 1.58 sjg goto oodate_out;
1114 1.161 rillig dname.str = fname3;
1115 1.58 sjg
1116 1.156 rillig Lst_Init(&missingFiles);
1117 1.17 sjg
1118 1.1 sjg /*
1119 1.1 sjg * We need to check if the target is out-of-date. This includes
1120 1.1 sjg * checking if the expanded command has changed. This in turn
1121 1.1 sjg * requires that all variables are set in the same way that they
1122 1.1 sjg * would be if the target needs to be re-built.
1123 1.1 sjg */
1124 1.181 rillig GNode_SetLocalVars(gn);
1125 1.1 sjg
1126 1.161 rillig meta_name(fname, sizeof fname, dname.str, tname, dname.str);
1127 1.1 sjg
1128 1.8 sjg #ifdef DEBUG_META_MODE
1129 1.121 rillig DEBUG1(META, "meta_oodate: %s\n", fname);
1130 1.8 sjg #endif
1131 1.8 sjg
1132 1.1 sjg if ((fp = fopen(fname, "r")) != NULL) {
1133 1.1 sjg static char *buf = NULL;
1134 1.1 sjg static size_t bufsz;
1135 1.196 rillig unsigned lineno = 0;
1136 1.5 sjg int lastpid = 0;
1137 1.5 sjg int pid;
1138 1.1 sjg int x;
1139 1.115 rillig StringListNode *cmdNode;
1140 1.143 rillig struct cached_stat cst;
1141 1.1 sjg
1142 1.141 rillig if (buf == NULL) {
1143 1.1 sjg bufsz = 8 * BUFSIZ;
1144 1.1 sjg buf = bmake_malloc(bufsz);
1145 1.1 sjg }
1146 1.5 sjg
1147 1.141 rillig if (cwdlen == 0) {
1148 1.138 rillig if (getcwd(cwd, sizeof cwd) == NULL)
1149 1.11 sjg err(1, "Could not get current working directory");
1150 1.11 sjg cwdlen = strlen(cwd);
1151 1.11 sjg }
1152 1.138 rillig strlcpy(lcwd, cwd, sizeof lcwd);
1153 1.138 rillig strlcpy(latestdir, cwd, sizeof latestdir);
1154 1.11 sjg
1155 1.141 rillig if (tmpdir == NULL) {
1156 1.5 sjg tmpdir = getTmpdir();
1157 1.5 sjg tmplen = strlen(tmpdir);
1158 1.5 sjg }
1159 1.5 sjg
1160 1.1 sjg /* we want to track all the .meta we read */
1161 1.173 rillig Global_Append(".MAKE.META.FILES", fname);
1162 1.1 sjg
1163 1.195 sjg cmp_filter = metaCmpFilter ? metaCmpFilter :
1164 1.195 sjg Var_Exists(gn, MAKE_META_CMP_FILTER);
1165 1.195 sjg
1166 1.153 rillig cmdNode = gn->commands.first;
1167 1.1 sjg while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
1168 1.1 sjg lineno++;
1169 1.1 sjg if (buf[x - 1] == '\n')
1170 1.1 sjg buf[x - 1] = '\0';
1171 1.20 sjg else {
1172 1.196 rillig warnx("%s: %u: line truncated at %u", fname, lineno, x);
1173 1.180 rillig oodate = true;
1174 1.20 sjg break;
1175 1.20 sjg }
1176 1.33 sjg link_src = NULL;
1177 1.33 sjg move_target = NULL;
1178 1.1 sjg /* Find the start of the build monitor section. */
1179 1.58 sjg if (!have_filemon) {
1180 1.1 sjg if (strncmp(buf, "-- filemon", 10) == 0) {
1181 1.180 rillig have_filemon = true;
1182 1.1 sjg continue;
1183 1.1 sjg }
1184 1.1 sjg if (strncmp(buf, "# buildmon", 10) == 0) {
1185 1.180 rillig have_filemon = true;
1186 1.1 sjg continue;
1187 1.1 sjg }
1188 1.85 rillig }
1189 1.1 sjg
1190 1.1 sjg /* Delimit the record type. */
1191 1.1 sjg p = buf;
1192 1.7 sjg #ifdef DEBUG_META_MODE
1193 1.196 rillig DEBUG3(META, "%s: %u: %s\n", fname, lineno, buf);
1194 1.7 sjg #endif
1195 1.1 sjg strsep(&p, " ");
1196 1.58 sjg if (have_filemon) {
1197 1.5 sjg /*
1198 1.5 sjg * We are in the 'filemon' output section.
1199 1.5 sjg * Each record from filemon follows the general form:
1200 1.5 sjg *
1201 1.5 sjg * <key> <pid> <data>
1202 1.5 sjg *
1203 1.5 sjg * Where:
1204 1.5 sjg * <key> is a single letter, denoting the syscall.
1205 1.5 sjg * <pid> is the process that made the syscall.
1206 1.5 sjg * <data> is the arguments (of interest).
1207 1.5 sjg */
1208 1.5 sjg switch(buf[0]) {
1209 1.5 sjg case '#': /* comment */
1210 1.5 sjg case 'V': /* version */
1211 1.5 sjg break;
1212 1.5 sjg default:
1213 1.5 sjg /*
1214 1.5 sjg * We need to track pathnames per-process.
1215 1.5 sjg *
1216 1.170 rillig * Each process run by make starts off in the 'CWD'
1217 1.5 sjg * recorded in the .meta file, if it chdirs ('C')
1218 1.5 sjg * elsewhere we need to track that - but only for
1219 1.5 sjg * that process. If it forks ('F'), we initialize
1220 1.5 sjg * the child to have the same cwd as its parent.
1221 1.5 sjg *
1222 1.5 sjg * We also need to track the 'latestdir' of
1223 1.5 sjg * interest. This is usually the same as cwd, but
1224 1.5 sjg * not if a process is reading directories.
1225 1.5 sjg *
1226 1.5 sjg * Each time we spot a different process ('pid')
1227 1.5 sjg * we save the current value of 'latestdir' in a
1228 1.5 sjg * variable qualified by 'lastpid', and
1229 1.5 sjg * re-initialize 'latestdir' to any pre-saved
1230 1.5 sjg * value for the current 'pid' and 'CWD' if none.
1231 1.5 sjg */
1232 1.20 sjg CHECK_VALID_META(p);
1233 1.5 sjg pid = atoi(p);
1234 1.5 sjg if (pid > 0 && pid != lastpid) {
1235 1.161 rillig FStr ldir;
1236 1.83 rillig
1237 1.5 sjg if (lastpid > 0) {
1238 1.38 sjg /* We need to remember these. */
1239 1.191 rillig Global_Set(lcwd_vname, lcwd);
1240 1.191 rillig Global_Set(ldir_vname, latestdir);
1241 1.5 sjg }
1242 1.138 rillig snprintf(lcwd_vname, sizeof lcwd_vname, LCWD_VNAME_FMT, pid);
1243 1.138 rillig snprintf(ldir_vname, sizeof ldir_vname, LDIR_VNAME_FMT, pid);
1244 1.5 sjg lastpid = pid;
1245 1.176 rillig ldir = Var_Value(SCOPE_GLOBAL, ldir_vname);
1246 1.161 rillig if (ldir.str != NULL) {
1247 1.161 rillig strlcpy(latestdir, ldir.str, sizeof latestdir);
1248 1.161 rillig FStr_Done(&ldir);
1249 1.38 sjg }
1250 1.176 rillig ldir = Var_Value(SCOPE_GLOBAL, lcwd_vname);
1251 1.161 rillig if (ldir.str != NULL) {
1252 1.161 rillig strlcpy(lcwd, ldir.str, sizeof lcwd);
1253 1.161 rillig FStr_Done(&ldir);
1254 1.38 sjg }
1255 1.5 sjg }
1256 1.5 sjg /* Skip past the pid. */
1257 1.5 sjg if (strsep(&p, " ") == NULL)
1258 1.5 sjg continue;
1259 1.7 sjg #ifdef DEBUG_META_MODE
1260 1.7 sjg if (DEBUG(META))
1261 1.196 rillig debug_printf("%s: %u: %d: %c: cwd=%s lcwd=%s ldir=%s\n",
1262 1.122 rillig fname, lineno,
1263 1.122 rillig pid, buf[0], cwd, lcwd, latestdir);
1264 1.7 sjg #endif
1265 1.5 sjg break;
1266 1.5 sjg }
1267 1.5 sjg
1268 1.20 sjg CHECK_VALID_META(p);
1269 1.20 sjg
1270 1.1 sjg /* Process according to record type. */
1271 1.1 sjg switch (buf[0]) {
1272 1.5 sjg case 'X': /* eXit */
1273 1.191 rillig Var_Delete(SCOPE_GLOBAL, lcwd_vname);
1274 1.191 rillig Var_Delete(SCOPE_GLOBAL, ldir_vname);
1275 1.5 sjg lastpid = 0; /* no need to save ldir_vname */
1276 1.5 sjg break;
1277 1.5 sjg
1278 1.5 sjg case 'F': /* [v]Fork */
1279 1.5 sjg {
1280 1.5 sjg char cldir[64];
1281 1.5 sjg int child;
1282 1.5 sjg
1283 1.5 sjg child = atoi(p);
1284 1.5 sjg if (child > 0) {
1285 1.138 rillig snprintf(cldir, sizeof cldir, LCWD_VNAME_FMT, child);
1286 1.191 rillig Global_Set(cldir, lcwd);
1287 1.138 rillig snprintf(cldir, sizeof cldir, LDIR_VNAME_FMT, child);
1288 1.191 rillig Global_Set(cldir, latestdir);
1289 1.38 sjg #ifdef DEBUG_META_MODE
1290 1.38 sjg if (DEBUG(META))
1291 1.122 rillig debug_printf(
1292 1.196 rillig "%s: %u: %d: cwd=%s lcwd=%s ldir=%s\n",
1293 1.122 rillig fname, lineno,
1294 1.122 rillig child, cwd, lcwd, latestdir);
1295 1.38 sjg #endif
1296 1.5 sjg }
1297 1.5 sjg }
1298 1.5 sjg break;
1299 1.1 sjg
1300 1.5 sjg case 'C': /* Chdir */
1301 1.38 sjg /* Update lcwd and latest directory. */
1302 1.138 rillig strlcpy(latestdir, p, sizeof latestdir);
1303 1.138 rillig strlcpy(lcwd, p, sizeof lcwd);
1304 1.191 rillig Global_Set(lcwd_vname, lcwd);
1305 1.191 rillig Global_Set(ldir_vname, lcwd);
1306 1.38 sjg #ifdef DEBUG_META_MODE
1307 1.196 rillig DEBUG4(META, "%s: %u: cwd=%s ldir=%s\n",
1308 1.121 rillig fname, lineno, cwd, lcwd);
1309 1.38 sjg #endif
1310 1.1 sjg break;
1311 1.1 sjg
1312 1.17 sjg case 'M': /* renaMe */
1313 1.33 sjg /*
1314 1.33 sjg * For 'M'oves we want to check
1315 1.33 sjg * the src as for 'R'ead
1316 1.33 sjg * and the target as for 'W'rite.
1317 1.33 sjg */
1318 1.163 rillig {
1319 1.163 rillig char *cp = p; /* save this for a second */
1320 1.163 rillig /* now get target */
1321 1.163 rillig if (strsep(&p, " ") == NULL)
1322 1.163 rillig continue;
1323 1.163 rillig CHECK_VALID_META(p);
1324 1.163 rillig move_target = p;
1325 1.163 rillig p = cp;
1326 1.163 rillig }
1327 1.17 sjg /* 'L' and 'M' put single quotes around the args */
1328 1.33 sjg DEQUOTE(p);
1329 1.33 sjg DEQUOTE(move_target);
1330 1.17 sjg /* FALLTHROUGH */
1331 1.17 sjg case 'D': /* unlink */
1332 1.129 rillig if (*p == '/') {
1333 1.64 sjg /* remove any missingFiles entries that match p */
1334 1.156 rillig StringListNode *ln = missingFiles.first;
1335 1.129 rillig while (ln != NULL) {
1336 1.129 rillig StringListNode *next = ln->next;
1337 1.129 rillig if (path_starts_with(ln->datum, p)) {
1338 1.132 rillig free(ln->datum);
1339 1.156 rillig Lst_Remove(&missingFiles, ln);
1340 1.129 rillig }
1341 1.129 rillig ln = next;
1342 1.17 sjg }
1343 1.17 sjg }
1344 1.33 sjg if (buf[0] == 'M') {
1345 1.33 sjg /* the target of the mv is a file 'W'ritten */
1346 1.33 sjg #ifdef DEBUG_META_MODE
1347 1.121 rillig DEBUG2(META, "meta_oodate: M %s -> %s\n",
1348 1.121 rillig p, move_target);
1349 1.33 sjg #endif
1350 1.33 sjg p = move_target;
1351 1.33 sjg goto check_write;
1352 1.33 sjg }
1353 1.17 sjg break;
1354 1.17 sjg case 'L': /* Link */
1355 1.33 sjg /*
1356 1.33 sjg * For 'L'inks check
1357 1.33 sjg * the src as for 'R'ead
1358 1.33 sjg * and the target as for 'W'rite.
1359 1.33 sjg */
1360 1.33 sjg link_src = p;
1361 1.33 sjg /* now get target */
1362 1.17 sjg if (strsep(&p, " ") == NULL)
1363 1.17 sjg continue;
1364 1.20 sjg CHECK_VALID_META(p);
1365 1.17 sjg /* 'L' and 'M' put single quotes around the args */
1366 1.33 sjg DEQUOTE(p);
1367 1.33 sjg DEQUOTE(link_src);
1368 1.33 sjg #ifdef DEBUG_META_MODE
1369 1.121 rillig DEBUG2(META, "meta_oodate: L %s -> %s\n", link_src, p);
1370 1.33 sjg #endif
1371 1.17 sjg /* FALLTHROUGH */
1372 1.17 sjg case 'W': /* Write */
1373 1.33 sjg check_write:
1374 1.17 sjg /*
1375 1.17 sjg * If a file we generated within our bailiwick
1376 1.17 sjg * but outside of .OBJDIR is missing,
1377 1.85 rillig * we need to do it again.
1378 1.17 sjg */
1379 1.17 sjg /* ignore non-absolute paths */
1380 1.17 sjg if (*p != '/')
1381 1.17 sjg break;
1382 1.17 sjg
1383 1.155 rillig if (Lst_IsEmpty(&metaBailiwick))
1384 1.17 sjg break;
1385 1.17 sjg
1386 1.17 sjg /* ignore cwd - normal dependencies handle those */
1387 1.17 sjg if (strncmp(p, cwd, cwdlen) == 0)
1388 1.17 sjg break;
1389 1.17 sjg
1390 1.155 rillig if (!has_any_prefix(p, &metaBailiwick))
1391 1.17 sjg break;
1392 1.17 sjg
1393 1.17 sjg /* tmpdir might be within */
1394 1.17 sjg if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1395 1.17 sjg break;
1396 1.17 sjg
1397 1.17 sjg /* ignore anything containing the string "tmp" */
1398 1.144 rillig /* XXX: The arguments to strstr must be swapped. */
1399 1.166 rillig if (strstr("tmp", p) != NULL)
1400 1.17 sjg break;
1401 1.17 sjg
1402 1.143 rillig if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
1403 1.143 rillig (link_src == NULL && cached_stat(p, &cst) < 0)) {
1404 1.130 rillig if (!meta_ignore(gn, p))
1405 1.156 rillig append_if_new(&missingFiles, p);
1406 1.17 sjg }
1407 1.17 sjg break;
1408 1.33 sjg check_link_src:
1409 1.33 sjg p = link_src;
1410 1.33 sjg link_src = NULL;
1411 1.33 sjg #ifdef DEBUG_META_MODE
1412 1.121 rillig DEBUG1(META, "meta_oodate: L src %s\n", p);
1413 1.33 sjg #endif
1414 1.33 sjg /* FALLTHROUGH */
1415 1.5 sjg case 'R': /* Read */
1416 1.5 sjg case 'E': /* Exec */
1417 1.1 sjg /*
1418 1.1 sjg * Check for runtime files that can't
1419 1.1 sjg * be part of the dependencies because
1420 1.1 sjg * they are _expected_ to change.
1421 1.1 sjg */
1422 1.67 sjg if (meta_ignore(gn, p))
1423 1.67 sjg break;
1424 1.85 rillig
1425 1.1 sjg /*
1426 1.5 sjg * The rest of the record is the file name.
1427 1.5 sjg * Check if it's not an absolute path.
1428 1.1 sjg */
1429 1.5 sjg {
1430 1.5 sjg char *sdirs[4];
1431 1.5 sjg char **sdp;
1432 1.5 sjg int sdx = 0;
1433 1.180 rillig bool found = false;
1434 1.5 sjg
1435 1.5 sjg if (*p == '/') {
1436 1.5 sjg sdirs[sdx++] = p; /* done */
1437 1.5 sjg } else {
1438 1.5 sjg if (strcmp(".", p) == 0)
1439 1.145 rillig continue; /* no point */
1440 1.5 sjg
1441 1.5 sjg /* Check vs latestdir */
1442 1.138 rillig snprintf(fname1, sizeof fname1, "%s/%s", latestdir, p);
1443 1.5 sjg sdirs[sdx++] = fname1;
1444 1.5 sjg
1445 1.38 sjg if (strcmp(latestdir, lcwd) != 0) {
1446 1.38 sjg /* Check vs lcwd */
1447 1.138 rillig snprintf(fname2, sizeof fname2, "%s/%s", lcwd, p);
1448 1.38 sjg sdirs[sdx++] = fname2;
1449 1.38 sjg }
1450 1.38 sjg if (strcmp(lcwd, cwd) != 0) {
1451 1.5 sjg /* Check vs cwd */
1452 1.138 rillig snprintf(fname3, sizeof fname3, "%s/%s", cwd, p);
1453 1.38 sjg sdirs[sdx++] = fname3;
1454 1.5 sjg }
1455 1.5 sjg }
1456 1.5 sjg sdirs[sdx++] = NULL;
1457 1.1 sjg
1458 1.168 rillig for (sdp = sdirs; *sdp != NULL && !found; sdp++) {
1459 1.7 sjg #ifdef DEBUG_META_MODE
1460 1.196 rillig DEBUG3(META, "%s: %u: looking for: %s\n",
1461 1.121 rillig fname, lineno, *sdp);
1462 1.7 sjg #endif
1463 1.143 rillig if (cached_stat(*sdp, &cst) == 0) {
1464 1.180 rillig found = true;
1465 1.5 sjg p = *sdp;
1466 1.5 sjg }
1467 1.5 sjg }
1468 1.5 sjg if (found) {
1469 1.7 sjg #ifdef DEBUG_META_MODE
1470 1.196 rillig DEBUG3(META, "%s: %u: found: %s\n",
1471 1.121 rillig fname, lineno, p);
1472 1.7 sjg #endif
1473 1.143 rillig if (!S_ISDIR(cst.cst_mode) &&
1474 1.143 rillig cst.cst_mtime > gn->mtime) {
1475 1.196 rillig DEBUG3(META, "%s: %u: file '%s' is newer than the target...\n",
1476 1.121 rillig fname, lineno, p);
1477 1.180 rillig oodate = true;
1478 1.143 rillig } else if (S_ISDIR(cst.cst_mode)) {
1479 1.5 sjg /* Update the latest directory. */
1480 1.59 sjg cached_realpath(p, latestdir);
1481 1.5 sjg }
1482 1.5 sjg } else if (errno == ENOENT && *p == '/' &&
1483 1.5 sjg strncmp(p, cwd, cwdlen) != 0) {
1484 1.5 sjg /*
1485 1.5 sjg * A referenced file outside of CWD is missing.
1486 1.5 sjg * We cannot catch every eventuality here...
1487 1.5 sjg */
1488 1.156 rillig append_if_new(&missingFiles, p);
1489 1.4 sjg }
1490 1.1 sjg }
1491 1.38 sjg if (buf[0] == 'E') {
1492 1.38 sjg /* previous latestdir is no longer relevant */
1493 1.138 rillig strlcpy(latestdir, lcwd, sizeof latestdir);
1494 1.38 sjg }
1495 1.1 sjg break;
1496 1.1 sjg default:
1497 1.1 sjg break;
1498 1.1 sjg }
1499 1.33 sjg if (!oodate && buf[0] == 'L' && link_src != NULL)
1500 1.33 sjg goto check_link_src;
1501 1.5 sjg } else if (strcmp(buf, "CMD") == 0) {
1502 1.1 sjg /*
1503 1.1 sjg * Compare the current command with the one in the
1504 1.1 sjg * meta data file.
1505 1.1 sjg */
1506 1.115 rillig if (cmdNode == NULL) {
1507 1.196 rillig DEBUG2(META, "%s: %u: there were more build commands in the meta data file than there are now...\n",
1508 1.121 rillig fname, lineno);
1509 1.180 rillig oodate = true;
1510 1.1 sjg } else {
1511 1.163 rillig const char *cp;
1512 1.127 rillig char *cmd = cmdNode->datum;
1513 1.180 rillig bool hasOODATE = false;
1514 1.1 sjg
1515 1.166 rillig if (strstr(cmd, "$?") != NULL)
1516 1.180 rillig hasOODATE = true;
1517 1.166 rillig else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
1518 1.29 sjg /* check for $[{(].OODATE[:)}] */
1519 1.29 sjg if (cp > cmd + 2 && cp[-2] == '$')
1520 1.180 rillig hasOODATE = true;
1521 1.29 sjg }
1522 1.29 sjg if (hasOODATE) {
1523 1.180 rillig needOODATE = true;
1524 1.196 rillig DEBUG2(META, "%s: %u: cannot compare command using .OODATE\n",
1525 1.121 rillig fname, lineno);
1526 1.1 sjg }
1527 1.179 rillig (void)Var_Subst(cmd, gn, VARE_UNDEFERR, &cmd);
1528 1.117 rillig /* TODO: handle errors */
1529 1.1 sjg
1530 1.166 rillig if ((cp = strchr(cmd, '\n')) != NULL) {
1531 1.1 sjg int n;
1532 1.1 sjg
1533 1.1 sjg /*
1534 1.1 sjg * This command contains newlines, we need to
1535 1.1 sjg * fetch more from the .meta file before we
1536 1.1 sjg * attempt a comparison.
1537 1.1 sjg */
1538 1.1 sjg /* first put the newline back at buf[x - 1] */
1539 1.1 sjg buf[x - 1] = '\n';
1540 1.1 sjg do {
1541 1.1 sjg /* now fetch the next line */
1542 1.1 sjg if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1543 1.1 sjg break;
1544 1.1 sjg x = n;
1545 1.1 sjg lineno++;
1546 1.1 sjg if (buf[x - 1] != '\n') {
1547 1.196 rillig warnx("%s: %u: line truncated at %u", fname, lineno, x);
1548 1.1 sjg break;
1549 1.1 sjg }
1550 1.164 rillig cp = strchr(cp + 1, '\n');
1551 1.147 rillig } while (cp != NULL);
1552 1.1 sjg if (buf[x - 1] == '\n')
1553 1.1 sjg buf[x - 1] = '\0';
1554 1.1 sjg }
1555 1.139 rillig if (p != NULL &&
1556 1.81 sjg !hasOODATE &&
1557 1.1 sjg !(gn->type & OP_NOMETA_CMP) &&
1558 1.195 sjg (meta_cmd_cmp(gn, p, cmd, cmp_filter) != 0)) {
1559 1.196 rillig DEBUG4(META, "%s: %u: a build command has changed\n%s\nvs\n%s\n",
1560 1.121 rillig fname, lineno, p, cmd);
1561 1.1 sjg if (!metaIgnoreCMDs)
1562 1.180 rillig oodate = true;
1563 1.1 sjg }
1564 1.1 sjg free(cmd);
1565 1.120 rillig cmdNode = cmdNode->next;
1566 1.1 sjg }
1567 1.1 sjg } else if (strcmp(buf, "CWD") == 0) {
1568 1.14 sjg /*
1569 1.14 sjg * Check if there are extra commands now
1570 1.14 sjg * that weren't in the meta data file.
1571 1.14 sjg */
1572 1.115 rillig if (!oodate && cmdNode != NULL) {
1573 1.196 rillig DEBUG2(META, "%s: %u: there are extra build commands now that weren't in the meta data file\n",
1574 1.121 rillig fname, lineno);
1575 1.180 rillig oodate = true;
1576 1.14 sjg }
1577 1.80 sjg CHECK_VALID_META(p);
1578 1.11 sjg if (strcmp(p, cwd) != 0) {
1579 1.196 rillig DEBUG4(META, "%s: %u: the current working directory has changed from '%s' to '%s'\n",
1580 1.121 rillig fname, lineno, p, curdir);
1581 1.180 rillig oodate = true;
1582 1.1 sjg }
1583 1.1 sjg }
1584 1.1 sjg }
1585 1.1 sjg
1586 1.1 sjg fclose(fp);
1587 1.156 rillig if (!Lst_IsEmpty(&missingFiles)) {
1588 1.121 rillig DEBUG2(META, "%s: missing files: %s...\n",
1589 1.156 rillig fname, (char *)missingFiles.first->datum);
1590 1.180 rillig oodate = true;
1591 1.17 sjg }
1592 1.60 sjg if (!oodate && !have_filemon && filemonMissing) {
1593 1.121 rillig DEBUG1(META, "%s: missing filemon data\n", fname);
1594 1.180 rillig oodate = true;
1595 1.21 sjg }
1596 1.60 sjg } else {
1597 1.86 sjg if (writeMeta && (metaMissing || (gn->type & OP_META))) {
1598 1.163 rillig const char *cp = NULL;
1599 1.50 christos
1600 1.60 sjg /* if target is in .CURDIR we do not need a meta file */
1601 1.168 rillig if (gn->path != NULL && (cp = strrchr(gn->path, '/')) != NULL &&
1602 1.168 rillig (cp > gn->path)) {
1603 1.124 rillig if (strncmp(curdir, gn->path, (size_t)(cp - gn->path)) != 0) {
1604 1.60 sjg cp = NULL; /* not in .CURDIR */
1605 1.60 sjg }
1606 1.60 sjg }
1607 1.141 rillig if (cp == NULL) {
1608 1.121 rillig DEBUG1(META, "%s: required but missing\n", fname);
1609 1.180 rillig oodate = true;
1610 1.180 rillig needOODATE = true; /* assume the worst */
1611 1.60 sjg }
1612 1.60 sjg }
1613 1.58 sjg }
1614 1.58 sjg
1615 1.156 rillig Lst_DoneCall(&missingFiles, free);
1616 1.50 christos
1617 1.26 sjg if (oodate && needOODATE) {
1618 1.4 sjg /*
1619 1.26 sjg * Target uses .OODATE which is empty; or we wouldn't be here.
1620 1.26 sjg * We have decided it is oodate, so .OODATE needs to be set.
1621 1.26 sjg * All we can sanely do is set it to .ALLSRC.
1622 1.4 sjg */
1623 1.176 rillig Var_Delete(gn, OODATE);
1624 1.176 rillig Var_Set(gn, OODATE, GNode_VarAllsrc(gn));
1625 1.4 sjg }
1626 1.58 sjg
1627 1.58 sjg oodate_out:
1628 1.161 rillig FStr_Done(&dname);
1629 1.1 sjg return oodate;
1630 1.1 sjg }
1631 1.1 sjg
1632 1.1 sjg /* support for compat mode */
1633 1.1 sjg
1634 1.1 sjg static int childPipe[2];
1635 1.1 sjg
1636 1.1 sjg void
1637 1.1 sjg meta_compat_start(void)
1638 1.1 sjg {
1639 1.73 maxv #ifdef USE_FILEMON_ONCE
1640 1.73 maxv /*
1641 1.73 maxv * We need to re-open filemon for each cmd.
1642 1.73 maxv */
1643 1.73 maxv BuildMon *pbm = &Mybm;
1644 1.85 rillig
1645 1.73 maxv if (pbm->mfp != NULL && useFilemon) {
1646 1.74 riastrad meta_open_filemon(pbm);
1647 1.73 maxv } else {
1648 1.74 riastrad pbm->mon_fd = -1;
1649 1.74 riastrad pbm->filemon = NULL;
1650 1.73 maxv }
1651 1.73 maxv #endif
1652 1.1 sjg if (pipe(childPipe) < 0)
1653 1.1 sjg Punt("Cannot create pipe: %s", strerror(errno));
1654 1.1 sjg /* Set close-on-exec flag for both */
1655 1.42 christos (void)fcntl(childPipe[0], F_SETFD, FD_CLOEXEC);
1656 1.42 christos (void)fcntl(childPipe[1], F_SETFD, FD_CLOEXEC);
1657 1.1 sjg }
1658 1.1 sjg
1659 1.1 sjg void
1660 1.1 sjg meta_compat_child(void)
1661 1.1 sjg {
1662 1.1 sjg meta_job_child(NULL);
1663 1.123 rillig if (dup2(childPipe[1], 1) < 0 || dup2(1, 2) < 0)
1664 1.123 rillig execDie("dup2", "pipe");
1665 1.1 sjg }
1666 1.1 sjg
1667 1.1 sjg void
1668 1.74 riastrad meta_compat_parent(pid_t child)
1669 1.1 sjg {
1670 1.74 riastrad int outfd, metafd, maxfd, nfds;
1671 1.78 sjg char buf[BUFSIZ+1];
1672 1.74 riastrad fd_set readfds;
1673 1.74 riastrad
1674 1.74 riastrad meta_job_parent(NULL, child);
1675 1.1 sjg close(childPipe[1]); /* child side */
1676 1.74 riastrad outfd = childPipe[0];
1677 1.79 sjg #ifdef USE_FILEMON
1678 1.168 rillig metafd = Mybm.filemon != NULL ? filemon_readfd(Mybm.filemon) : -1;
1679 1.79 sjg #else
1680 1.79 sjg metafd = -1;
1681 1.79 sjg #endif
1682 1.74 riastrad maxfd = -1;
1683 1.74 riastrad if (outfd > maxfd)
1684 1.74 riastrad maxfd = outfd;
1685 1.74 riastrad if (metafd > maxfd)
1686 1.74 riastrad maxfd = metafd;
1687 1.74 riastrad
1688 1.74 riastrad while (outfd != -1 || metafd != -1) {
1689 1.74 riastrad FD_ZERO(&readfds);
1690 1.74 riastrad if (outfd != -1) {
1691 1.74 riastrad FD_SET(outfd, &readfds);
1692 1.74 riastrad }
1693 1.74 riastrad if (metafd != -1) {
1694 1.74 riastrad FD_SET(metafd, &readfds);
1695 1.74 riastrad }
1696 1.74 riastrad nfds = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1697 1.74 riastrad if (nfds == -1) {
1698 1.74 riastrad if (errno == EINTR)
1699 1.74 riastrad continue;
1700 1.74 riastrad err(1, "select");
1701 1.74 riastrad }
1702 1.74 riastrad
1703 1.168 rillig if (outfd != -1 && FD_ISSET(outfd, &readfds) != 0) do {
1704 1.74 riastrad /* XXX this is not line-buffered */
1705 1.138 rillig ssize_t nread = read(outfd, buf, sizeof buf - 1);
1706 1.74 riastrad if (nread == -1)
1707 1.74 riastrad err(1, "read");
1708 1.74 riastrad if (nread == 0) {
1709 1.74 riastrad close(outfd);
1710 1.74 riastrad outfd = -1;
1711 1.74 riastrad break;
1712 1.74 riastrad }
1713 1.74 riastrad fwrite(buf, 1, (size_t)nread, stdout);
1714 1.74 riastrad fflush(stdout);
1715 1.78 sjg buf[nread] = '\0';
1716 1.78 sjg meta_job_output(NULL, buf, "");
1717 1.185 rillig } while (false);
1718 1.168 rillig if (metafd != -1 && FD_ISSET(metafd, &readfds) != 0) {
1719 1.74 riastrad if (meta_job_event(NULL) <= 0)
1720 1.74 riastrad metafd = -1;
1721 1.74 riastrad }
1722 1.1 sjg }
1723 1.1 sjg }
1724 1.3 sjg
1725 1.154 rillig #endif /* USE_META */
1726