meta.c revision 1.21 1 1.21 sjg /* $NetBSD: meta.c,v 1.21 2011/08/18 00:00:21 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.1 sjg * Copyright (c) 2009-2010, Juniper Networks, Inc.
10 1.1 sjg * Portions Copyright (c) 2009, John Birrell.
11 1.1 sjg *
12 1.1 sjg * Redistribution and use in source and binary forms, with or without
13 1.1 sjg * modification, are permitted provided that the following conditions
14 1.1 sjg * are met:
15 1.1 sjg * 1. Redistributions of source code must retain the above copyright
16 1.1 sjg * 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.1 sjg * documentation and/or other materials provided with the distribution.
20 1.1 sjg *
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.1 sjg * 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 <sys/ioctl.h>
40 1.1 sjg #include <fcntl.h>
41 1.1 sjg #include <libgen.h>
42 1.1 sjg #include <errno.h>
43 1.2 sjg #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
44 1.1 sjg #include <err.h>
45 1.2 sjg #endif
46 1.1 sjg
47 1.1 sjg #include "make.h"
48 1.1 sjg #include "job.h"
49 1.1 sjg
50 1.1 sjg #ifdef HAVE_FILEMON_H
51 1.1 sjg # include <filemon.h>
52 1.1 sjg #endif
53 1.1 sjg #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
54 1.1 sjg # define USE_FILEMON
55 1.1 sjg #endif
56 1.1 sjg
57 1.1 sjg static BuildMon Mybm; /* for compat */
58 1.17 sjg static Lst metaBailiwick; /* our scope of control */
59 1.1 sjg
60 1.1 sjg Boolean useMeta = FALSE;
61 1.1 sjg static Boolean useFilemon = FALSE;
62 1.1 sjg static Boolean writeMeta = FALSE;
63 1.1 sjg static Boolean metaEnv = FALSE; /* don't save env unless asked */
64 1.1 sjg static Boolean metaVerbose = FALSE;
65 1.1 sjg static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
66 1.12 sjg static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
67 1.1 sjg
68 1.1 sjg extern Boolean forceJobs;
69 1.1 sjg extern Boolean comatMake;
70 1.1 sjg
71 1.1 sjg #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
72 1.1 sjg
73 1.1 sjg #ifndef N2U
74 1.1 sjg # define N2U(n, u) (((n) + ((u) - 1)) / (u))
75 1.1 sjg #endif
76 1.1 sjg #ifndef ROUNDUP
77 1.1 sjg # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
78 1.1 sjg #endif
79 1.1 sjg
80 1.2 sjg #if !defined(HAVE_STRSEP)
81 1.2 sjg # define strsep(s, d) stresep((s), (d), 0)
82 1.2 sjg #endif
83 1.2 sjg
84 1.1 sjg /*
85 1.1 sjg * Filemon is a kernel module which snoops certain syscalls.
86 1.1 sjg *
87 1.1 sjg * C chdir
88 1.1 sjg * E exec
89 1.1 sjg * F [v]fork
90 1.1 sjg * L [sym]link
91 1.1 sjg * M rename
92 1.1 sjg * R read
93 1.1 sjg * W write
94 1.1 sjg * S stat
95 1.1 sjg *
96 1.1 sjg * See meta_oodate below - we mainly care about 'E' and 'R'.
97 1.1 sjg *
98 1.1 sjg * We can still use meta mode without filemon, but
99 1.1 sjg * the benefits are more limited.
100 1.1 sjg */
101 1.1 sjg #ifdef USE_FILEMON
102 1.1 sjg # ifndef _PATH_FILEMON
103 1.1 sjg # define _PATH_FILEMON "/dev/filemon"
104 1.1 sjg # endif
105 1.1 sjg
106 1.1 sjg /*
107 1.1 sjg * Open the filemon device.
108 1.1 sjg */
109 1.1 sjg static void
110 1.1 sjg filemon_open(BuildMon *pbm)
111 1.1 sjg {
112 1.1 sjg int retry;
113 1.1 sjg
114 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
115 1.1 sjg if (!useFilemon)
116 1.1 sjg return;
117 1.1 sjg
118 1.1 sjg for (retry = 5; retry >= 0; retry--) {
119 1.1 sjg if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
120 1.1 sjg break;
121 1.1 sjg }
122 1.1 sjg
123 1.1 sjg if (pbm->filemon_fd < 0) {
124 1.1 sjg useFilemon = FALSE;
125 1.1 sjg warn("Could not open %s", _PATH_FILEMON);
126 1.1 sjg return;
127 1.1 sjg }
128 1.1 sjg
129 1.1 sjg /*
130 1.1 sjg * We use a file outside of '.'
131 1.1 sjg * to avoid a FreeBSD kernel bug where unlink invalidates
132 1.1 sjg * cwd causing getcwd to do a lot more work.
133 1.1 sjg * We only care about the descriptor.
134 1.1 sjg */
135 1.1 sjg pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
136 1.1 sjg if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
137 1.1 sjg err(1, "Could not set filemon file descriptor!");
138 1.1 sjg }
139 1.1 sjg /* we don't need these once we exec */
140 1.1 sjg (void)fcntl(pbm->mon_fd, F_SETFD, 1);
141 1.1 sjg (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
142 1.1 sjg }
143 1.1 sjg
144 1.1 sjg /*
145 1.1 sjg * Read the build monitor output file and write records to the target's
146 1.1 sjg * metadata file.
147 1.1 sjg */
148 1.1 sjg static void
149 1.1 sjg filemon_read(FILE *mfp, int fd)
150 1.1 sjg {
151 1.1 sjg FILE *fp;
152 1.1 sjg char buf[BUFSIZ];
153 1.1 sjg
154 1.1 sjg /* Check if we're not writing to a meta data file.*/
155 1.1 sjg if (mfp == NULL) {
156 1.1 sjg if (fd >= 0)
157 1.1 sjg close(fd); /* not interested */
158 1.1 sjg return;
159 1.1 sjg }
160 1.1 sjg /* rewind */
161 1.1 sjg lseek(fd, SEEK_SET, 0);
162 1.1 sjg if ((fp = fdopen(fd, "r")) == NULL)
163 1.1 sjg err(1, "Could not read build monitor file '%d'", fd);
164 1.1 sjg
165 1.1 sjg fprintf(mfp, "-- filemon acquired metadata --\n");
166 1.1 sjg
167 1.1 sjg while (fgets(buf, sizeof(buf), fp)) {
168 1.1 sjg fprintf(mfp, "%s", buf);
169 1.1 sjg }
170 1.1 sjg fflush(mfp);
171 1.1 sjg clearerr(fp);
172 1.1 sjg fclose(fp);
173 1.1 sjg }
174 1.1 sjg #endif
175 1.1 sjg
176 1.8 sjg /*
177 1.8 sjg * when realpath() fails,
178 1.8 sjg * we use this, to clean up ./ and ../
179 1.8 sjg */
180 1.8 sjg static void
181 1.8 sjg eat_dots(char *buf, size_t bufsz, int dots)
182 1.8 sjg {
183 1.8 sjg char *cp;
184 1.8 sjg char *cp2;
185 1.8 sjg const char *eat;
186 1.8 sjg size_t eatlen;
187 1.8 sjg
188 1.8 sjg switch (dots) {
189 1.8 sjg case 1:
190 1.8 sjg eat = "/./";
191 1.8 sjg eatlen = 2;
192 1.8 sjg break;
193 1.8 sjg case 2:
194 1.8 sjg eat = "/../";
195 1.8 sjg eatlen = 3;
196 1.8 sjg break;
197 1.8 sjg default:
198 1.8 sjg return;
199 1.8 sjg }
200 1.8 sjg
201 1.8 sjg do {
202 1.8 sjg cp = strstr(buf, eat);
203 1.8 sjg if (cp) {
204 1.8 sjg cp2 = cp + eatlen;
205 1.8 sjg if (dots == 2 && cp > buf) {
206 1.8 sjg do {
207 1.8 sjg cp--;
208 1.8 sjg } while (cp > buf && *cp != '/');
209 1.8 sjg }
210 1.8 sjg if (*cp == '/') {
211 1.8 sjg strlcpy(cp, cp2, bufsz - (cp - buf));
212 1.8 sjg } else {
213 1.8 sjg return; /* can't happen? */
214 1.8 sjg }
215 1.8 sjg }
216 1.8 sjg } while (cp);
217 1.8 sjg }
218 1.8 sjg
219 1.1 sjg static char *
220 1.1 sjg meta_name(struct GNode *gn, char *mname, size_t mnamelen,
221 1.1 sjg const char *dname,
222 1.1 sjg const char *tname)
223 1.1 sjg {
224 1.1 sjg char buf[MAXPATHLEN];
225 1.1 sjg char cwd[MAXPATHLEN];
226 1.1 sjg char *rp;
227 1.1 sjg char *cp;
228 1.1 sjg char *tp;
229 1.1 sjg char *p[4]; /* >= number of possible uses */
230 1.1 sjg int i;
231 1.1 sjg
232 1.1 sjg i = 0;
233 1.1 sjg if (!dname)
234 1.1 sjg dname = Var_Value(".OBJDIR", gn, &p[i++]);
235 1.1 sjg if (!tname)
236 1.1 sjg tname = Var_Value(TARGET, gn, &p[i++]);
237 1.1 sjg
238 1.8 sjg if (realpath(dname, cwd))
239 1.8 sjg dname = cwd;
240 1.8 sjg
241 1.1 sjg /*
242 1.1 sjg * Weed out relative paths from the target file name.
243 1.1 sjg * We have to be careful though since if target is a
244 1.1 sjg * symlink, the result will be unstable.
245 1.1 sjg * So we use realpath() just to get the dirname, and leave the
246 1.1 sjg * basename as given to us.
247 1.1 sjg */
248 1.1 sjg if ((cp = strrchr(tname, '/'))) {
249 1.1 sjg if (realpath(tname, buf)) {
250 1.1 sjg if ((rp = strrchr(buf, '/'))) {
251 1.1 sjg rp++;
252 1.1 sjg cp++;
253 1.1 sjg if (strcmp(cp, rp) != 0)
254 1.1 sjg strlcpy(rp, cp, sizeof(buf) - (rp - buf));
255 1.1 sjg }
256 1.1 sjg tname = buf;
257 1.8 sjg } else {
258 1.8 sjg /*
259 1.8 sjg * We likely have a directory which is about to be made.
260 1.8 sjg * We pretend realpath() succeeded, to have a chance
261 1.8 sjg * of generating the same meta file name that we will
262 1.8 sjg * next time through.
263 1.8 sjg */
264 1.8 sjg if (tname[0] == '/') {
265 1.8 sjg strlcpy(buf, tname, sizeof(buf));
266 1.8 sjg } else {
267 1.8 sjg snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
268 1.8 sjg }
269 1.8 sjg eat_dots(buf, sizeof(buf), 1); /* ./ */
270 1.8 sjg eat_dots(buf, sizeof(buf), 2); /* ../ */
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.1 sjg if (strcmp(dname, dirname(tp)) == 0)
277 1.1 sjg snprintf(mname, mnamelen, "%s.meta", tname);
278 1.1 sjg else {
279 1.1 sjg snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
280 1.1 sjg
281 1.1 sjg /*
282 1.1 sjg * Replace path separators in the file name after the
283 1.1 sjg * current object directory path.
284 1.1 sjg */
285 1.1 sjg cp = mname + strlen(dname) + 1;
286 1.1 sjg
287 1.1 sjg while (*cp != '\0') {
288 1.1 sjg if (*cp == '/')
289 1.1 sjg *cp = '_';
290 1.1 sjg cp++;
291 1.1 sjg }
292 1.1 sjg }
293 1.1 sjg free(tp);
294 1.1 sjg for (i--; i >= 0; i--) {
295 1.1 sjg if (p[i])
296 1.1 sjg free(p[i]);
297 1.1 sjg }
298 1.1 sjg return (mname);
299 1.1 sjg }
300 1.1 sjg
301 1.1 sjg /*
302 1.1 sjg * Return true if running ${.MAKE}
303 1.1 sjg * Bypassed if target is flagged .MAKE
304 1.1 sjg */
305 1.1 sjg static int
306 1.1 sjg is_submake(void *cmdp, void *gnp)
307 1.1 sjg {
308 1.1 sjg static char *p_make = NULL;
309 1.1 sjg static int p_len;
310 1.1 sjg char *cmd = cmdp;
311 1.1 sjg GNode *gn = gnp;
312 1.1 sjg char *mp = NULL;
313 1.1 sjg char *cp;
314 1.1 sjg char *cp2;
315 1.1 sjg int rc = 0; /* keep looking */
316 1.1 sjg
317 1.1 sjg if (!p_make) {
318 1.1 sjg p_make = Var_Value(".MAKE", gn, &cp);
319 1.1 sjg p_len = strlen(p_make);
320 1.1 sjg }
321 1.1 sjg cp = strchr(cmd, '$');
322 1.1 sjg if ((cp)) {
323 1.1 sjg mp = Var_Subst(NULL, cmd, gn, FALSE);
324 1.1 sjg cmd = mp;
325 1.1 sjg }
326 1.1 sjg cp2 = strstr(cmd, p_make);
327 1.1 sjg if ((cp2)) {
328 1.1 sjg switch (cp2[p_len]) {
329 1.1 sjg case '\0':
330 1.1 sjg case ' ':
331 1.1 sjg case '\t':
332 1.1 sjg case '\n':
333 1.1 sjg rc = 1;
334 1.1 sjg break;
335 1.1 sjg }
336 1.1 sjg if (cp2 > cmd && rc > 0) {
337 1.1 sjg switch (cp2[-1]) {
338 1.1 sjg case ' ':
339 1.1 sjg case '\t':
340 1.1 sjg case '\n':
341 1.1 sjg break;
342 1.1 sjg default:
343 1.1 sjg rc = 0; /* no match */
344 1.1 sjg break;
345 1.1 sjg }
346 1.1 sjg }
347 1.1 sjg }
348 1.1 sjg if (mp)
349 1.1 sjg free(mp);
350 1.1 sjg return (rc);
351 1.1 sjg }
352 1.1 sjg
353 1.1 sjg typedef struct meta_file_s {
354 1.1 sjg FILE *fp;
355 1.1 sjg GNode *gn;
356 1.1 sjg } meta_file_t;
357 1.1 sjg
358 1.1 sjg static int
359 1.1 sjg printCMD(void *cmdp, void *mfpp)
360 1.1 sjg {
361 1.1 sjg meta_file_t *mfp = mfpp;
362 1.1 sjg char *cmd = cmdp;
363 1.1 sjg char *cp = NULL;
364 1.1 sjg
365 1.1 sjg if (strchr(cmd, '$')) {
366 1.1 sjg cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
367 1.1 sjg }
368 1.1 sjg fprintf(mfp->fp, "CMD %s\n", cmd);
369 1.1 sjg if (cp)
370 1.1 sjg free(cp);
371 1.1 sjg return 0;
372 1.1 sjg }
373 1.1 sjg
374 1.1 sjg /*
375 1.1 sjg * Certain node types never get a .meta file
376 1.1 sjg */
377 1.1 sjg #define SKIP_META_TYPE(_type) do { \
378 1.1 sjg if ((gn->type & __CONCAT(OP_, _type))) { \
379 1.1 sjg if (DEBUG(META)) { \
380 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .%s\n", \
381 1.1 sjg gn->name, __STRING(_type)); \
382 1.1 sjg } \
383 1.1 sjg return (NULL); \
384 1.1 sjg } \
385 1.1 sjg } while (0)
386 1.1 sjg
387 1.1 sjg static FILE *
388 1.1 sjg meta_create(BuildMon *pbm, GNode *gn)
389 1.1 sjg {
390 1.1 sjg extern char **environ;
391 1.1 sjg meta_file_t mf;
392 1.1 sjg char buf[MAXPATHLEN];
393 1.1 sjg char objdir[MAXPATHLEN];
394 1.1 sjg char **ptr;
395 1.1 sjg const char *dname;
396 1.1 sjg const char *tname;
397 1.1 sjg char *fname;
398 1.1 sjg const char *cp;
399 1.1 sjg char *p[4]; /* >= possible uses */
400 1.1 sjg int i;
401 1.1 sjg struct stat fs;
402 1.1 sjg
403 1.1 sjg
404 1.1 sjg /* This may be a phony node which we don't want meta data for... */
405 1.1 sjg /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
406 1.1 sjg /* Or it may be explicitly flagged as .NOMETA */
407 1.1 sjg SKIP_META_TYPE(NOMETA);
408 1.1 sjg /* Unless it is explicitly flagged as .META */
409 1.1 sjg if (!(gn->type & OP_META)) {
410 1.1 sjg SKIP_META_TYPE(PHONY);
411 1.1 sjg SKIP_META_TYPE(SPECIAL);
412 1.1 sjg SKIP_META_TYPE(MAKE);
413 1.1 sjg }
414 1.1 sjg
415 1.1 sjg mf.fp = NULL;
416 1.1 sjg
417 1.1 sjg i = 0;
418 1.1 sjg
419 1.1 sjg dname = Var_Value(".OBJDIR", gn, &p[i++]);
420 1.1 sjg tname = Var_Value(TARGET, gn, &p[i++]);
421 1.1 sjg
422 1.1 sjg /* The object directory may not exist. Check it.. */
423 1.1 sjg if (stat(dname, &fs) != 0) {
424 1.1 sjg if (DEBUG(META))
425 1.1 sjg fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
426 1.1 sjg gn->name);
427 1.1 sjg goto out;
428 1.1 sjg }
429 1.1 sjg /* Check if there are no commands to execute. */
430 1.1 sjg if (Lst_IsEmpty(gn->commands)) {
431 1.1 sjg if (DEBUG(META))
432 1.1 sjg fprintf(debug_file, "Skipping meta for %s: no commands\n",
433 1.1 sjg gn->name);
434 1.1 sjg goto out;
435 1.1 sjg }
436 1.1 sjg
437 1.1 sjg /* make sure these are canonical */
438 1.1 sjg if (realpath(dname, objdir))
439 1.1 sjg dname = objdir;
440 1.1 sjg
441 1.1 sjg /* If we aren't in the object directory, don't create a meta file. */
442 1.12 sjg if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
443 1.1 sjg if (DEBUG(META))
444 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
445 1.1 sjg gn->name);
446 1.1 sjg goto out;
447 1.1 sjg }
448 1.1 sjg if (!(gn->type & OP_META)) {
449 1.1 sjg /* We do not generate .meta files for sub-makes */
450 1.1 sjg if (Lst_ForEach(gn->commands, is_submake, gn)) {
451 1.1 sjg if (DEBUG(META))
452 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
453 1.1 sjg gn->name);
454 1.1 sjg goto out;
455 1.1 sjg }
456 1.1 sjg }
457 1.1 sjg
458 1.1 sjg if (metaVerbose) {
459 1.1 sjg char *mp;
460 1.1 sjg
461 1.1 sjg /* Describe the target we are building */
462 1.1 sjg mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
463 1.1 sjg if (*mp)
464 1.1 sjg fprintf(stdout, "%s\n", mp);
465 1.1 sjg free(mp);
466 1.1 sjg }
467 1.1 sjg /* Get the basename of the target */
468 1.1 sjg if ((cp = strrchr(tname, '/')) == NULL) {
469 1.1 sjg cp = tname;
470 1.1 sjg } else {
471 1.1 sjg cp++;
472 1.1 sjg }
473 1.1 sjg
474 1.1 sjg fflush(stdout);
475 1.1 sjg
476 1.1 sjg if (strcmp(cp, makeDependfile) == 0)
477 1.1 sjg goto out;
478 1.1 sjg
479 1.1 sjg if (!writeMeta)
480 1.1 sjg /* Don't create meta data. */
481 1.1 sjg goto out;
482 1.1 sjg
483 1.1 sjg fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
484 1.1 sjg dname, tname);
485 1.1 sjg
486 1.8 sjg #ifdef DEBUG_META_MODE
487 1.8 sjg if (DEBUG(META))
488 1.8 sjg fprintf(debug_file, "meta_create: %s\n", fname);
489 1.8 sjg #endif
490 1.8 sjg
491 1.1 sjg if ((mf.fp = fopen(fname, "w")) == NULL)
492 1.1 sjg err(1, "Could not open meta file '%s'", fname);
493 1.1 sjg
494 1.1 sjg fprintf(mf.fp, "# Meta data file %s\n", fname);
495 1.1 sjg
496 1.1 sjg mf.gn = gn;
497 1.1 sjg
498 1.1 sjg Lst_ForEach(gn->commands, printCMD, &mf);
499 1.1 sjg
500 1.1 sjg fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
501 1.1 sjg fprintf(mf.fp, "TARGET %s\n", tname);
502 1.1 sjg
503 1.1 sjg if (metaEnv) {
504 1.1 sjg for (ptr = environ; *ptr != NULL; ptr++)
505 1.1 sjg fprintf(mf.fp, "ENV %s\n", *ptr);
506 1.1 sjg }
507 1.1 sjg
508 1.1 sjg fprintf(mf.fp, "-- command output --\n");
509 1.1 sjg fflush(mf.fp);
510 1.1 sjg
511 1.1 sjg Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
512 1.1 sjg Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
513 1.1 sjg
514 1.1 sjg out:
515 1.1 sjg for (i--; i >= 0; i--) {
516 1.1 sjg if (p[i])
517 1.1 sjg free(p[i]);
518 1.1 sjg }
519 1.1 sjg
520 1.1 sjg return (mf.fp);
521 1.1 sjg }
522 1.1 sjg
523 1.12 sjg static Boolean
524 1.12 sjg boolValue(char *s)
525 1.12 sjg {
526 1.12 sjg switch(*s) {
527 1.12 sjg case '0':
528 1.12 sjg case 'N':
529 1.12 sjg case 'n':
530 1.12 sjg case 'F':
531 1.12 sjg case 'f':
532 1.12 sjg return FALSE;
533 1.12 sjg }
534 1.12 sjg return TRUE;
535 1.12 sjg }
536 1.1 sjg
537 1.1 sjg void
538 1.1 sjg meta_init(const char *make_mode)
539 1.1 sjg {
540 1.1 sjg static int once = 0;
541 1.12 sjg char *cp;
542 1.1 sjg
543 1.1 sjg useMeta = TRUE;
544 1.1 sjg useFilemon = TRUE;
545 1.1 sjg writeMeta = TRUE;
546 1.1 sjg
547 1.1 sjg if (make_mode) {
548 1.1 sjg if (strstr(make_mode, "env"))
549 1.1 sjg metaEnv = TRUE;
550 1.1 sjg if (strstr(make_mode, "verb"))
551 1.1 sjg metaVerbose = TRUE;
552 1.1 sjg if (strstr(make_mode, "read"))
553 1.1 sjg writeMeta = FALSE;
554 1.1 sjg if (strstr(make_mode, "nofilemon"))
555 1.1 sjg useFilemon = FALSE;
556 1.13 sjg if ((cp = strstr(make_mode, "curdirok="))) {
557 1.13 sjg metaCurdirOk = boolValue(&cp[9]);
558 1.12 sjg }
559 1.1 sjg if (strstr(make_mode, "ignore-cmd"))
560 1.1 sjg metaIgnoreCMDs = TRUE;
561 1.1 sjg /* for backwards compatability */
562 1.1 sjg Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
563 1.1 sjg Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
564 1.1 sjg }
565 1.1 sjg if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
566 1.1 sjg /*
567 1.1 sjg * The default value for MAKE_META_PREFIX
568 1.1 sjg * prints the absolute path of the target.
569 1.1 sjg * This works be cause :H will generate '.' if there is no /
570 1.1 sjg * and :tA will resolve that to cwd.
571 1.1 sjg */
572 1.1 sjg Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
573 1.1 sjg }
574 1.1 sjg if (once)
575 1.1 sjg return;
576 1.1 sjg once = 1;
577 1.1 sjg memset(&Mybm, 0, sizeof(Mybm));
578 1.17 sjg /*
579 1.17 sjg * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
580 1.17 sjg */
581 1.17 sjg metaBailiwick = Lst_Init(FALSE);
582 1.17 sjg cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
583 1.17 sjg if (cp) {
584 1.17 sjg str2Lst_Append(metaBailiwick, cp, NULL);
585 1.17 sjg }
586 1.1 sjg }
587 1.1 sjg
588 1.1 sjg /*
589 1.1 sjg * In each case below we allow for job==NULL
590 1.1 sjg */
591 1.1 sjg void
592 1.1 sjg meta_job_start(Job *job, GNode *gn)
593 1.1 sjg {
594 1.1 sjg BuildMon *pbm;
595 1.1 sjg
596 1.1 sjg if (job != NULL) {
597 1.1 sjg pbm = &job->bm;
598 1.1 sjg } else {
599 1.1 sjg pbm = &Mybm;
600 1.1 sjg }
601 1.1 sjg pbm->mfp = meta_create(pbm, gn);
602 1.1 sjg #ifdef USE_FILEMON_ONCE
603 1.1 sjg /* compat mode we open the filemon dev once per command */
604 1.1 sjg if (job == NULL)
605 1.1 sjg return;
606 1.1 sjg #endif
607 1.1 sjg #ifdef USE_FILEMON
608 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
609 1.1 sjg filemon_open(pbm);
610 1.1 sjg } else {
611 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
612 1.1 sjg }
613 1.1 sjg #endif
614 1.1 sjg }
615 1.1 sjg
616 1.1 sjg /*
617 1.1 sjg * The child calls this before doing anything.
618 1.1 sjg * It does not disturb our state.
619 1.1 sjg */
620 1.1 sjg void
621 1.1 sjg meta_job_child(Job *job)
622 1.1 sjg {
623 1.1 sjg #ifdef USE_FILEMON
624 1.1 sjg BuildMon *pbm;
625 1.1 sjg pid_t pid;
626 1.1 sjg
627 1.1 sjg if (job != NULL) {
628 1.1 sjg pbm = &job->bm;
629 1.1 sjg } else {
630 1.1 sjg pbm = &Mybm;
631 1.1 sjg }
632 1.1 sjg pid = getpid();
633 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
634 1.1 sjg if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
635 1.1 sjg err(1, "Could not set filemon pid!");
636 1.1 sjg }
637 1.1 sjg }
638 1.1 sjg #endif
639 1.1 sjg }
640 1.1 sjg
641 1.1 sjg void
642 1.1 sjg meta_job_error(Job *job, GNode *gn, int flags, int status)
643 1.1 sjg {
644 1.1 sjg char cwd[MAXPATHLEN];
645 1.1 sjg BuildMon *pbm;
646 1.1 sjg
647 1.1 sjg if (job != NULL) {
648 1.1 sjg pbm = &job->bm;
649 1.1 sjg } else {
650 1.1 sjg if (!gn)
651 1.1 sjg gn = job->node;
652 1.1 sjg pbm = &Mybm;
653 1.1 sjg }
654 1.1 sjg if (pbm->mfp != NULL) {
655 1.1 sjg fprintf(pbm->mfp, "*** Error code %d%s\n",
656 1.1 sjg status,
657 1.1 sjg (flags & JOB_IGNERR) ?
658 1.1 sjg "(ignored)" : "");
659 1.1 sjg }
660 1.1 sjg if (gn) {
661 1.1 sjg Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
662 1.1 sjg }
663 1.1 sjg getcwd(cwd, sizeof(cwd));
664 1.1 sjg Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
665 1.6 sjg if (pbm && pbm->meta_fname[0]) {
666 1.1 sjg Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
667 1.1 sjg }
668 1.16 sjg meta_job_finish(job);
669 1.1 sjg }
670 1.1 sjg
671 1.1 sjg void
672 1.1 sjg meta_job_output(Job *job, char *cp, const char *nl)
673 1.1 sjg {
674 1.1 sjg BuildMon *pbm;
675 1.1 sjg
676 1.1 sjg if (job != NULL) {
677 1.1 sjg pbm = &job->bm;
678 1.1 sjg } else {
679 1.1 sjg pbm = &Mybm;
680 1.1 sjg }
681 1.1 sjg if (pbm->mfp != NULL) {
682 1.1 sjg if (metaVerbose) {
683 1.1 sjg static char *meta_prefix = NULL;
684 1.1 sjg static int meta_prefix_len;
685 1.1 sjg
686 1.1 sjg if (!meta_prefix) {
687 1.1 sjg char *cp2;
688 1.1 sjg
689 1.1 sjg meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
690 1.1 sjg if ((cp2 = strchr(meta_prefix, '$')))
691 1.1 sjg meta_prefix_len = cp2 - meta_prefix;
692 1.1 sjg else
693 1.1 sjg meta_prefix_len = strlen(meta_prefix);
694 1.1 sjg }
695 1.1 sjg if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
696 1.1 sjg cp = strchr(cp+1, '\n');
697 1.1 sjg if (!cp++)
698 1.1 sjg return;
699 1.1 sjg }
700 1.1 sjg }
701 1.1 sjg fprintf(pbm->mfp, "%s%s", cp, nl);
702 1.1 sjg }
703 1.1 sjg }
704 1.1 sjg
705 1.1 sjg void
706 1.1 sjg meta_cmd_finish(void *pbmp)
707 1.1 sjg {
708 1.1 sjg #ifdef USE_FILEMON
709 1.1 sjg BuildMon *pbm = pbmp;
710 1.1 sjg
711 1.1 sjg if (!pbm)
712 1.1 sjg pbm = &Mybm;
713 1.1 sjg
714 1.1 sjg if (pbm->filemon_fd >= 0) {
715 1.1 sjg close(pbm->filemon_fd);
716 1.1 sjg filemon_read(pbm->mfp, pbm->mon_fd);
717 1.1 sjg pbm->filemon_fd = pbm->mon_fd = -1;
718 1.1 sjg }
719 1.1 sjg #endif
720 1.1 sjg }
721 1.1 sjg
722 1.1 sjg void
723 1.1 sjg meta_job_finish(Job *job)
724 1.1 sjg {
725 1.1 sjg BuildMon *pbm;
726 1.1 sjg
727 1.1 sjg if (job != NULL) {
728 1.1 sjg pbm = &job->bm;
729 1.1 sjg } else {
730 1.1 sjg pbm = &Mybm;
731 1.1 sjg }
732 1.1 sjg if (pbm->mfp != NULL) {
733 1.1 sjg meta_cmd_finish(pbm);
734 1.1 sjg fclose(pbm->mfp);
735 1.1 sjg pbm->mfp = NULL;
736 1.6 sjg pbm->meta_fname[0] = '\0';
737 1.1 sjg }
738 1.1 sjg }
739 1.1 sjg
740 1.1 sjg /*
741 1.1 sjg * Fetch a full line from fp - growing bufp if needed
742 1.1 sjg * Return length in bufp.
743 1.1 sjg */
744 1.1 sjg static int
745 1.1 sjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
746 1.1 sjg {
747 1.1 sjg char *buf = *bufp;
748 1.1 sjg size_t bufsz = *szp;
749 1.1 sjg struct stat fs;
750 1.1 sjg int x;
751 1.1 sjg
752 1.1 sjg if (fgets(&buf[o], bufsz - o, fp) != NULL) {
753 1.1 sjg check_newline:
754 1.1 sjg x = o + strlen(&buf[o]);
755 1.1 sjg if (buf[x - 1] == '\n')
756 1.1 sjg return x;
757 1.1 sjg /*
758 1.1 sjg * We need to grow the buffer.
759 1.1 sjg * The meta file can give us a clue.
760 1.1 sjg */
761 1.1 sjg if (fstat(fileno(fp), &fs) == 0) {
762 1.1 sjg size_t newsz;
763 1.1 sjg char *p;
764 1.1 sjg
765 1.1 sjg newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
766 1.1 sjg if (newsz <= bufsz)
767 1.1 sjg newsz = ROUNDUP(fs.st_size, BUFSIZ);
768 1.1 sjg if (DEBUG(META))
769 1.19 sjg fprintf(debug_file, "growing buffer %zu -> %zu\n",
770 1.19 sjg bufsz, newsz);
771 1.1 sjg p = bmake_realloc(buf, newsz);
772 1.1 sjg if (p) {
773 1.1 sjg *bufp = buf = p;
774 1.1 sjg *szp = bufsz = newsz;
775 1.1 sjg /* fetch the rest */
776 1.1 sjg if (!fgets(&buf[x], bufsz - x, fp))
777 1.1 sjg return x; /* truncated! */
778 1.1 sjg goto check_newline;
779 1.1 sjg }
780 1.1 sjg }
781 1.1 sjg }
782 1.1 sjg return 0;
783 1.1 sjg }
784 1.1 sjg
785 1.17 sjg static int
786 1.17 sjg prefix_match(void *p, void *q)
787 1.17 sjg {
788 1.17 sjg const char *prefix = p;
789 1.17 sjg const char *path = q;
790 1.17 sjg size_t n = strlen(prefix);
791 1.17 sjg
792 1.17 sjg return (0 == strncmp(path, prefix, n));
793 1.17 sjg }
794 1.17 sjg
795 1.17 sjg static int
796 1.17 sjg string_match(const void *p, const void *q)
797 1.17 sjg {
798 1.17 sjg const char *p1 = p;
799 1.17 sjg const char *p2 = q;
800 1.17 sjg
801 1.17 sjg return strcmp(p1, p2);
802 1.17 sjg }
803 1.17 sjg
804 1.17 sjg
805 1.1 sjg /*
806 1.1 sjg * When running with 'meta' functionality, a target can be out-of-date
807 1.1 sjg * if any of the references in it's meta data file is more recent.
808 1.5 sjg * We have to track the latestdir on a per-process basis.
809 1.1 sjg */
810 1.5 sjg #define LDIR_VNAME_FMT ".meta.%d.ldir"
811 1.5 sjg
812 1.20 sjg /*
813 1.20 sjg * It is possible that a .meta file is corrupted,
814 1.20 sjg * if we detect this we want to reproduce it.
815 1.20 sjg * Setting oodate TRUE will have that effect.
816 1.20 sjg */
817 1.20 sjg #define CHECK_VALID_META(p) if (!(p && *p)) { \
818 1.20 sjg warnx("%s: %d: malformed", fname, lineno); \
819 1.20 sjg oodate = TRUE; \
820 1.20 sjg continue; \
821 1.20 sjg }
822 1.20 sjg
823 1.1 sjg Boolean
824 1.1 sjg meta_oodate(GNode *gn, Boolean oodate)
825 1.1 sjg {
826 1.5 sjg static char *tmpdir = NULL;
827 1.11 sjg static char cwd[MAXPATHLEN];
828 1.5 sjg char ldir_vname[64];
829 1.1 sjg char latestdir[MAXPATHLEN];
830 1.1 sjg char fname[MAXPATHLEN];
831 1.1 sjg char fname1[MAXPATHLEN];
832 1.5 sjg char fname2[MAXPATHLEN];
833 1.1 sjg char *p;
834 1.1 sjg char *cp;
835 1.11 sjg static size_t cwdlen = 0;
836 1.7 sjg static size_t tmplen = 0;
837 1.1 sjg FILE *fp;
838 1.1 sjg Boolean ignoreOODATE = FALSE;
839 1.17 sjg Lst missingFiles;
840 1.17 sjg
841 1.4 sjg if (oodate)
842 1.4 sjg return oodate; /* we're done */
843 1.4 sjg
844 1.17 sjg missingFiles = Lst_Init(FALSE);
845 1.17 sjg
846 1.1 sjg /*
847 1.1 sjg * We need to check if the target is out-of-date. This includes
848 1.1 sjg * checking if the expanded command has changed. This in turn
849 1.1 sjg * requires that all variables are set in the same way that they
850 1.1 sjg * would be if the target needs to be re-built.
851 1.1 sjg */
852 1.1 sjg Make_DoAllVar(gn);
853 1.1 sjg
854 1.1 sjg meta_name(gn, fname, sizeof(fname), NULL, NULL);
855 1.1 sjg
856 1.8 sjg #ifdef DEBUG_META_MODE
857 1.8 sjg if (DEBUG(META))
858 1.8 sjg fprintf(debug_file, "meta_oodate: %s\n", fname);
859 1.8 sjg #endif
860 1.8 sjg
861 1.1 sjg if ((fp = fopen(fname, "r")) != NULL) {
862 1.1 sjg static char *buf = NULL;
863 1.1 sjg static size_t bufsz;
864 1.1 sjg int lineno = 0;
865 1.5 sjg int lastpid = 0;
866 1.5 sjg int pid;
867 1.1 sjg int f = 0;
868 1.1 sjg int x;
869 1.1 sjg LstNode ln;
870 1.1 sjg struct stat fs;
871 1.1 sjg
872 1.1 sjg if (!buf) {
873 1.1 sjg bufsz = 8 * BUFSIZ;
874 1.1 sjg buf = bmake_malloc(bufsz);
875 1.1 sjg }
876 1.5 sjg
877 1.11 sjg if (!cwdlen) {
878 1.11 sjg if (getcwd(cwd, sizeof(cwd)) == NULL)
879 1.11 sjg err(1, "Could not get current working directory");
880 1.11 sjg cwdlen = strlen(cwd);
881 1.11 sjg }
882 1.11 sjg
883 1.5 sjg if (!tmpdir) {
884 1.5 sjg tmpdir = getTmpdir();
885 1.5 sjg tmplen = strlen(tmpdir);
886 1.5 sjg }
887 1.5 sjg
888 1.1 sjg /* we want to track all the .meta we read */
889 1.1 sjg Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
890 1.1 sjg
891 1.1 sjg ln = Lst_First(gn->commands);
892 1.1 sjg while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
893 1.1 sjg lineno++;
894 1.1 sjg if (buf[x - 1] == '\n')
895 1.1 sjg buf[x - 1] = '\0';
896 1.20 sjg else {
897 1.1 sjg warnx("%s: %d: line truncated at %u", fname, lineno, x);
898 1.20 sjg oodate = TRUE;
899 1.20 sjg break;
900 1.20 sjg }
901 1.1 sjg /* Find the start of the build monitor section. */
902 1.1 sjg if (!f) {
903 1.1 sjg if (strncmp(buf, "-- filemon", 10) == 0) {
904 1.1 sjg f = 1;
905 1.1 sjg continue;
906 1.1 sjg }
907 1.1 sjg if (strncmp(buf, "# buildmon", 10) == 0) {
908 1.1 sjg f = 1;
909 1.1 sjg continue;
910 1.1 sjg }
911 1.1 sjg }
912 1.1 sjg
913 1.1 sjg /* Delimit the record type. */
914 1.1 sjg p = buf;
915 1.7 sjg #ifdef DEBUG_META_MODE
916 1.7 sjg if (DEBUG(META))
917 1.7 sjg fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
918 1.7 sjg #endif
919 1.1 sjg strsep(&p, " ");
920 1.1 sjg if (f) {
921 1.5 sjg /*
922 1.5 sjg * We are in the 'filemon' output section.
923 1.5 sjg * Each record from filemon follows the general form:
924 1.5 sjg *
925 1.5 sjg * <key> <pid> <data>
926 1.5 sjg *
927 1.5 sjg * Where:
928 1.5 sjg * <key> is a single letter, denoting the syscall.
929 1.5 sjg * <pid> is the process that made the syscall.
930 1.5 sjg * <data> is the arguments (of interest).
931 1.5 sjg */
932 1.5 sjg switch(buf[0]) {
933 1.5 sjg case '#': /* comment */
934 1.5 sjg case 'V': /* version */
935 1.5 sjg break;
936 1.5 sjg default:
937 1.5 sjg /*
938 1.5 sjg * We need to track pathnames per-process.
939 1.5 sjg *
940 1.5 sjg * Each process run by make, starts off in the 'CWD'
941 1.5 sjg * recorded in the .meta file, if it chdirs ('C')
942 1.5 sjg * elsewhere we need to track that - but only for
943 1.5 sjg * that process. If it forks ('F'), we initialize
944 1.5 sjg * the child to have the same cwd as its parent.
945 1.5 sjg *
946 1.5 sjg * We also need to track the 'latestdir' of
947 1.5 sjg * interest. This is usually the same as cwd, but
948 1.5 sjg * not if a process is reading directories.
949 1.5 sjg *
950 1.5 sjg * Each time we spot a different process ('pid')
951 1.5 sjg * we save the current value of 'latestdir' in a
952 1.5 sjg * variable qualified by 'lastpid', and
953 1.5 sjg * re-initialize 'latestdir' to any pre-saved
954 1.5 sjg * value for the current 'pid' and 'CWD' if none.
955 1.5 sjg */
956 1.20 sjg CHECK_VALID_META(p);
957 1.5 sjg pid = atoi(p);
958 1.5 sjg if (pid > 0 && pid != lastpid) {
959 1.5 sjg char *ldir;
960 1.5 sjg char *tp;
961 1.5 sjg
962 1.5 sjg if (lastpid > 0) {
963 1.5 sjg /* We need to remember this. */
964 1.5 sjg Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
965 1.5 sjg }
966 1.5 sjg snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
967 1.5 sjg lastpid = pid;
968 1.5 sjg ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
969 1.5 sjg if (ldir) {
970 1.5 sjg strlcpy(latestdir, ldir, sizeof(latestdir));
971 1.5 sjg if (tp)
972 1.5 sjg free(tp);
973 1.5 sjg } else
974 1.5 sjg strlcpy(latestdir, cwd, sizeof(latestdir));
975 1.5 sjg }
976 1.5 sjg /* Skip past the pid. */
977 1.5 sjg if (strsep(&p, " ") == NULL)
978 1.5 sjg continue;
979 1.7 sjg #ifdef DEBUG_META_MODE
980 1.7 sjg if (DEBUG(META))
981 1.7 sjg fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
982 1.7 sjg #endif
983 1.5 sjg break;
984 1.5 sjg }
985 1.5 sjg
986 1.20 sjg CHECK_VALID_META(p);
987 1.20 sjg
988 1.1 sjg /* Process according to record type. */
989 1.1 sjg switch (buf[0]) {
990 1.5 sjg case 'X': /* eXit */
991 1.5 sjg Var_Delete(ldir_vname, VAR_GLOBAL);
992 1.5 sjg lastpid = 0; /* no need to save ldir_vname */
993 1.5 sjg break;
994 1.5 sjg
995 1.5 sjg case 'F': /* [v]Fork */
996 1.5 sjg {
997 1.5 sjg char cldir[64];
998 1.5 sjg int child;
999 1.5 sjg
1000 1.5 sjg child = atoi(p);
1001 1.5 sjg if (child > 0) {
1002 1.5 sjg snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1003 1.5 sjg Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1004 1.5 sjg }
1005 1.5 sjg }
1006 1.5 sjg break;
1007 1.1 sjg
1008 1.5 sjg case 'C': /* Chdir */
1009 1.1 sjg /* Update the latest directory. */
1010 1.1 sjg strlcpy(latestdir, p, sizeof(latestdir));
1011 1.1 sjg break;
1012 1.1 sjg
1013 1.17 sjg case 'M': /* renaMe */
1014 1.17 sjg if (Lst_IsEmpty(missingFiles))
1015 1.17 sjg break;
1016 1.17 sjg /* 'L' and 'M' put single quotes around the args */
1017 1.17 sjg if (*p == '\'') {
1018 1.17 sjg char *ep;
1019 1.17 sjg
1020 1.17 sjg p++;
1021 1.17 sjg if ((ep = strchr(p, '\'')))
1022 1.17 sjg *ep = '\0';
1023 1.17 sjg }
1024 1.17 sjg /* FALLTHROUGH */
1025 1.17 sjg case 'D': /* unlink */
1026 1.17 sjg if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1027 1.17 sjg /* remove p from the missingFiles list if present */
1028 1.17 sjg if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1029 1.17 sjg char *tp = Lst_Datum(ln);
1030 1.17 sjg Lst_Remove(missingFiles, ln);
1031 1.17 sjg free(tp);
1032 1.17 sjg }
1033 1.17 sjg }
1034 1.17 sjg break;
1035 1.17 sjg case 'L': /* Link */
1036 1.17 sjg /* we want the target */
1037 1.17 sjg if (strsep(&p, " ") == NULL)
1038 1.17 sjg continue;
1039 1.20 sjg CHECK_VALID_META(p);
1040 1.17 sjg /* 'L' and 'M' put single quotes around the args */
1041 1.17 sjg if (*p == '\'') {
1042 1.17 sjg char *ep;
1043 1.17 sjg
1044 1.17 sjg p++;
1045 1.17 sjg if ((ep = strchr(p, '\'')))
1046 1.17 sjg *ep = '\0';
1047 1.17 sjg }
1048 1.17 sjg /* FALLTHROUGH */
1049 1.17 sjg case 'W': /* Write */
1050 1.17 sjg /*
1051 1.17 sjg * If a file we generated within our bailiwick
1052 1.17 sjg * but outside of .OBJDIR is missing,
1053 1.17 sjg * we need to do it again.
1054 1.17 sjg */
1055 1.17 sjg /* ignore non-absolute paths */
1056 1.17 sjg if (*p != '/')
1057 1.17 sjg break;
1058 1.17 sjg
1059 1.17 sjg if (Lst_IsEmpty(metaBailiwick))
1060 1.17 sjg break;
1061 1.17 sjg
1062 1.17 sjg /* ignore cwd - normal dependencies handle those */
1063 1.17 sjg if (strncmp(p, cwd, cwdlen) == 0)
1064 1.17 sjg break;
1065 1.17 sjg
1066 1.17 sjg if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1067 1.17 sjg break;
1068 1.17 sjg
1069 1.17 sjg /* tmpdir might be within */
1070 1.17 sjg if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1071 1.17 sjg break;
1072 1.17 sjg
1073 1.17 sjg /* ignore anything containing the string "tmp" */
1074 1.17 sjg if ((strstr("tmp", p)))
1075 1.17 sjg break;
1076 1.17 sjg
1077 1.17 sjg if (stat(p, &fs) < 0) {
1078 1.17 sjg Lst_AtEnd(missingFiles, bmake_strdup(p));
1079 1.17 sjg }
1080 1.17 sjg break;
1081 1.5 sjg case 'R': /* Read */
1082 1.5 sjg case 'E': /* Exec */
1083 1.1 sjg /*
1084 1.1 sjg * Check for runtime files that can't
1085 1.1 sjg * be part of the dependencies because
1086 1.1 sjg * they are _expected_ to change.
1087 1.1 sjg */
1088 1.5 sjg if (strncmp(p, "/tmp/", 5) == 0 ||
1089 1.7 sjg (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
1090 1.5 sjg break;
1091 1.5 sjg
1092 1.1 sjg if (strncmp(p, "/var/", 5) == 0)
1093 1.1 sjg break;
1094 1.1 sjg
1095 1.1 sjg /* Ignore device files. */
1096 1.1 sjg if (strncmp(p, "/dev/", 5) == 0)
1097 1.1 sjg break;
1098 1.1 sjg
1099 1.1 sjg /* Ignore /etc/ files. */
1100 1.1 sjg if (strncmp(p, "/etc/", 5) == 0)
1101 1.1 sjg break;
1102 1.1 sjg
1103 1.1 sjg /*
1104 1.5 sjg * The rest of the record is the file name.
1105 1.5 sjg * Check if it's not an absolute path.
1106 1.1 sjg */
1107 1.5 sjg {
1108 1.5 sjg char *sdirs[4];
1109 1.5 sjg char **sdp;
1110 1.5 sjg int sdx = 0;
1111 1.5 sjg int found = 0;
1112 1.5 sjg
1113 1.5 sjg if (*p == '/') {
1114 1.5 sjg sdirs[sdx++] = p; /* done */
1115 1.5 sjg } else {
1116 1.5 sjg if (strcmp(".", p) == 0)
1117 1.5 sjg continue; /* no point */
1118 1.5 sjg
1119 1.5 sjg /* Check vs latestdir */
1120 1.5 sjg snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1121 1.5 sjg sdirs[sdx++] = fname1;
1122 1.5 sjg
1123 1.5 sjg if (strcmp(latestdir, cwd) != 0) {
1124 1.5 sjg /* Check vs cwd */
1125 1.5 sjg snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1126 1.5 sjg sdirs[sdx++] = fname2;
1127 1.5 sjg }
1128 1.5 sjg }
1129 1.5 sjg sdirs[sdx++] = NULL;
1130 1.1 sjg
1131 1.5 sjg for (sdp = sdirs; *sdp && !found; sdp++) {
1132 1.7 sjg #ifdef DEBUG_META_MODE
1133 1.7 sjg if (DEBUG(META))
1134 1.7 sjg fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1135 1.7 sjg #endif
1136 1.5 sjg if (stat(*sdp, &fs) == 0) {
1137 1.5 sjg found = 1;
1138 1.5 sjg p = *sdp;
1139 1.5 sjg }
1140 1.5 sjg }
1141 1.5 sjg if (found) {
1142 1.7 sjg #ifdef DEBUG_META_MODE
1143 1.7 sjg if (DEBUG(META))
1144 1.7 sjg fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1145 1.7 sjg #endif
1146 1.5 sjg if (!S_ISDIR(fs.st_mode) &&
1147 1.5 sjg fs.st_mtime > gn->mtime) {
1148 1.5 sjg if (DEBUG(META))
1149 1.5 sjg fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1150 1.5 sjg oodate = TRUE;
1151 1.5 sjg } else if (S_ISDIR(fs.st_mode)) {
1152 1.5 sjg /* Update the latest directory. */
1153 1.5 sjg realpath(p, latestdir);
1154 1.5 sjg }
1155 1.5 sjg } else if (errno == ENOENT && *p == '/' &&
1156 1.5 sjg strncmp(p, cwd, cwdlen) != 0) {
1157 1.5 sjg /*
1158 1.5 sjg * A referenced file outside of CWD is missing.
1159 1.5 sjg * We cannot catch every eventuality here...
1160 1.5 sjg */
1161 1.4 sjg if (DEBUG(META))
1162 1.5 sjg fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1163 1.4 sjg oodate = TRUE;
1164 1.4 sjg }
1165 1.1 sjg }
1166 1.1 sjg break;
1167 1.1 sjg default:
1168 1.1 sjg break;
1169 1.1 sjg }
1170 1.5 sjg } else if (strcmp(buf, "CMD") == 0) {
1171 1.1 sjg /*
1172 1.1 sjg * Compare the current command with the one in the
1173 1.1 sjg * meta data file.
1174 1.1 sjg */
1175 1.1 sjg if (ln == NULL) {
1176 1.1 sjg if (DEBUG(META))
1177 1.1 sjg fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1178 1.1 sjg oodate = TRUE;
1179 1.1 sjg } else {
1180 1.1 sjg char *cmd = (char *)Lst_Datum(ln);
1181 1.1 sjg
1182 1.1 sjg if (!ignoreOODATE) {
1183 1.1 sjg if (strstr(cmd, "$?"))
1184 1.1 sjg ignoreOODATE = TRUE;
1185 1.1 sjg else if ((cp = strstr(cmd, ".OODATE"))) {
1186 1.1 sjg /* check for $[{(].OODATE[)}] */
1187 1.1 sjg if (cp > cmd + 2 && cp[-2] == '$')
1188 1.1 sjg ignoreOODATE = TRUE;
1189 1.1 sjg }
1190 1.1 sjg if (ignoreOODATE && DEBUG(META))
1191 1.1 sjg fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1192 1.1 sjg }
1193 1.1 sjg cmd = Var_Subst(NULL, cmd, gn, TRUE);
1194 1.1 sjg
1195 1.1 sjg if ((cp = strchr(cmd, '\n'))) {
1196 1.1 sjg int n;
1197 1.1 sjg
1198 1.1 sjg /*
1199 1.1 sjg * This command contains newlines, we need to
1200 1.1 sjg * fetch more from the .meta file before we
1201 1.1 sjg * attempt a comparison.
1202 1.1 sjg */
1203 1.1 sjg /* first put the newline back at buf[x - 1] */
1204 1.1 sjg buf[x - 1] = '\n';
1205 1.1 sjg do {
1206 1.1 sjg /* now fetch the next line */
1207 1.1 sjg if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1208 1.1 sjg break;
1209 1.1 sjg x = n;
1210 1.1 sjg lineno++;
1211 1.1 sjg if (buf[x - 1] != '\n') {
1212 1.1 sjg warnx("%s: %d: line truncated at %u", fname, lineno, x);
1213 1.1 sjg break;
1214 1.1 sjg }
1215 1.1 sjg cp = strchr(++cp, '\n');
1216 1.1 sjg } while (cp);
1217 1.1 sjg if (buf[x - 1] == '\n')
1218 1.1 sjg buf[x - 1] = '\0';
1219 1.1 sjg }
1220 1.1 sjg if (!ignoreOODATE &&
1221 1.1 sjg !(gn->type & OP_NOMETA_CMP) &&
1222 1.1 sjg strcmp(p, cmd) != 0) {
1223 1.1 sjg if (DEBUG(META))
1224 1.1 sjg fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1225 1.1 sjg if (!metaIgnoreCMDs)
1226 1.1 sjg oodate = TRUE;
1227 1.1 sjg }
1228 1.1 sjg free(cmd);
1229 1.1 sjg ln = Lst_Succ(ln);
1230 1.1 sjg }
1231 1.1 sjg } else if (strcmp(buf, "CWD") == 0) {
1232 1.14 sjg /*
1233 1.14 sjg * Check if there are extra commands now
1234 1.14 sjg * that weren't in the meta data file.
1235 1.14 sjg */
1236 1.14 sjg if (!oodate && ln != NULL) {
1237 1.14 sjg if (DEBUG(META))
1238 1.14 sjg fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1239 1.14 sjg oodate = TRUE;
1240 1.14 sjg }
1241 1.11 sjg if (strcmp(p, cwd) != 0) {
1242 1.1 sjg if (DEBUG(META))
1243 1.1 sjg fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1244 1.1 sjg oodate = TRUE;
1245 1.1 sjg }
1246 1.1 sjg }
1247 1.1 sjg }
1248 1.1 sjg
1249 1.1 sjg fclose(fp);
1250 1.17 sjg if (!Lst_IsEmpty(missingFiles)) {
1251 1.17 sjg if (DEBUG(META))
1252 1.17 sjg fprintf(debug_file, "%s: missing files: %s...\n",
1253 1.17 sjg fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1254 1.17 sjg oodate = TRUE;
1255 1.17 sjg Lst_Destroy(missingFiles, (FreeProc *)free);
1256 1.17 sjg }
1257 1.21 sjg } else {
1258 1.21 sjg if ((gn->type & OP_META)) {
1259 1.21 sjg if (DEBUG(META))
1260 1.21 sjg fprintf(debug_file, "%s: required but missing\n", fname);
1261 1.21 sjg oodate = TRUE;
1262 1.21 sjg }
1263 1.1 sjg }
1264 1.4 sjg if (oodate && ignoreOODATE) {
1265 1.4 sjg /*
1266 1.4 sjg * Target uses .OODATE, so we need to re-compute it.
1267 1.4 sjg * We need to clean up what Make_DoAllVar() did.
1268 1.4 sjg */
1269 1.4 sjg Var_Delete(ALLSRC, gn);
1270 1.4 sjg Var_Delete(OODATE, gn);
1271 1.4 sjg gn->flags &= ~DONE_ALLSRC;
1272 1.4 sjg }
1273 1.1 sjg return oodate;
1274 1.1 sjg }
1275 1.1 sjg
1276 1.1 sjg /* support for compat mode */
1277 1.1 sjg
1278 1.1 sjg static int childPipe[2];
1279 1.1 sjg
1280 1.1 sjg void
1281 1.1 sjg meta_compat_start(void)
1282 1.1 sjg {
1283 1.1 sjg #ifdef USE_FILEMON_ONCE
1284 1.1 sjg /*
1285 1.1 sjg * We need to re-open filemon for each cmd.
1286 1.1 sjg */
1287 1.1 sjg BuildMon *pbm = &Mybm;
1288 1.1 sjg
1289 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
1290 1.1 sjg filemon_open(pbm);
1291 1.1 sjg } else {
1292 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
1293 1.1 sjg }
1294 1.1 sjg #endif
1295 1.1 sjg if (pipe(childPipe) < 0)
1296 1.1 sjg Punt("Cannot create pipe: %s", strerror(errno));
1297 1.1 sjg /* Set close-on-exec flag for both */
1298 1.1 sjg (void)fcntl(childPipe[0], F_SETFD, 1);
1299 1.1 sjg (void)fcntl(childPipe[1], F_SETFD, 1);
1300 1.1 sjg }
1301 1.1 sjg
1302 1.1 sjg void
1303 1.1 sjg meta_compat_child(void)
1304 1.1 sjg {
1305 1.1 sjg meta_job_child(NULL);
1306 1.1 sjg if (dup2(childPipe[1], 1) < 0 ||
1307 1.1 sjg dup2(1, 2) < 0) {
1308 1.1 sjg execError("dup2", "pipe");
1309 1.1 sjg _exit(1);
1310 1.1 sjg }
1311 1.1 sjg }
1312 1.1 sjg
1313 1.1 sjg void
1314 1.1 sjg meta_compat_parent(void)
1315 1.1 sjg {
1316 1.1 sjg FILE *fp;
1317 1.1 sjg char buf[BUFSIZ];
1318 1.1 sjg
1319 1.1 sjg close(childPipe[1]); /* child side */
1320 1.1 sjg fp = fdopen(childPipe[0], "r");
1321 1.1 sjg while (fgets(buf, sizeof(buf), fp)) {
1322 1.1 sjg meta_job_output(NULL, buf, "");
1323 1.1 sjg printf("%s", buf);
1324 1.1 sjg }
1325 1.1 sjg fclose(fp);
1326 1.1 sjg }
1327 1.3 sjg
1328 1.3 sjg #endif /* USE_META */
1329