meta.c revision 1.1 1 1.1 sjg /*
2 1.1 sjg * Implement 'meta' mode.
3 1.1 sjg * Adapted from John Birrell's patches to FreeBSD make.
4 1.1 sjg * --sjg
5 1.1 sjg */
6 1.1 sjg /*
7 1.1 sjg * Copyright (c) 2009-2010, Juniper Networks, Inc.
8 1.1 sjg * Portions Copyright (c) 2009, John Birrell.
9 1.1 sjg *
10 1.1 sjg * Redistribution and use in source and binary forms, with or without
11 1.1 sjg * modification, are permitted provided that the following conditions
12 1.1 sjg * are met:
13 1.1 sjg * 1. Redistributions of source code must retain the above copyright
14 1.1 sjg * notice, this list of conditions and the following disclaimer.
15 1.1 sjg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 sjg * notice, this list of conditions and the following disclaimer in the
17 1.1 sjg * documentation and/or other materials provided with the distribution.
18 1.1 sjg *
19 1.1 sjg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1 sjg * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 sjg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 1.1 sjg * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 1.1 sjg * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 1.1 sjg * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 1.1 sjg * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 sjg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 sjg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 sjg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 1.1 sjg * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 sjg */
31 1.1 sjg
32 1.1 sjg #ifdef HAVE_CONFIG_H
33 1.1 sjg # include "config.h"
34 1.1 sjg #endif
35 1.1 sjg #include <sys/stat.h>
36 1.1 sjg #include <sys/ioctl.h>
37 1.1 sjg #include <fcntl.h>
38 1.1 sjg #include <libgen.h>
39 1.1 sjg #include <errno.h>
40 1.1 sjg #include <err.h>
41 1.1 sjg
42 1.1 sjg #include "make.h"
43 1.1 sjg #include "job.h"
44 1.1 sjg
45 1.1 sjg #ifdef HAVE_FILEMON_H
46 1.1 sjg # include <filemon.h>
47 1.1 sjg #endif
48 1.1 sjg #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
49 1.1 sjg # define USE_FILEMON
50 1.1 sjg #endif
51 1.1 sjg
52 1.1 sjg static BuildMon Mybm; /* for compat */
53 1.1 sjg
54 1.1 sjg Boolean useMeta = FALSE;
55 1.1 sjg static Boolean useFilemon = FALSE;
56 1.1 sjg static Boolean writeMeta = FALSE;
57 1.1 sjg static Boolean metaEnv = FALSE; /* don't save env unless asked */
58 1.1 sjg static Boolean metaVerbose = FALSE;
59 1.1 sjg static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
60 1.1 sjg
61 1.1 sjg extern Boolean forceJobs;
62 1.1 sjg extern Boolean comatMake;
63 1.1 sjg
64 1.1 sjg #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
65 1.1 sjg
66 1.1 sjg #ifndef N2U
67 1.1 sjg # define N2U(n, u) (((n) + ((u) - 1)) / (u))
68 1.1 sjg #endif
69 1.1 sjg #ifndef ROUNDUP
70 1.1 sjg # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
71 1.1 sjg #endif
72 1.1 sjg
73 1.1 sjg /*
74 1.1 sjg * Filemon is a kernel module which snoops certain syscalls.
75 1.1 sjg *
76 1.1 sjg * C chdir
77 1.1 sjg * E exec
78 1.1 sjg * F [v]fork
79 1.1 sjg * L [sym]link
80 1.1 sjg * M rename
81 1.1 sjg * R read
82 1.1 sjg * W write
83 1.1 sjg * S stat
84 1.1 sjg *
85 1.1 sjg * See meta_oodate below - we mainly care about 'E' and 'R'.
86 1.1 sjg *
87 1.1 sjg * We can still use meta mode without filemon, but
88 1.1 sjg * the benefits are more limited.
89 1.1 sjg */
90 1.1 sjg #ifdef USE_FILEMON
91 1.1 sjg # ifndef _PATH_FILEMON
92 1.1 sjg # define _PATH_FILEMON "/dev/filemon"
93 1.1 sjg # endif
94 1.1 sjg
95 1.1 sjg /*
96 1.1 sjg * Open the filemon device.
97 1.1 sjg */
98 1.1 sjg static void
99 1.1 sjg filemon_open(BuildMon *pbm)
100 1.1 sjg {
101 1.1 sjg int retry;
102 1.1 sjg
103 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
104 1.1 sjg if (!useFilemon)
105 1.1 sjg return;
106 1.1 sjg
107 1.1 sjg for (retry = 5; retry >= 0; retry--) {
108 1.1 sjg if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
109 1.1 sjg break;
110 1.1 sjg }
111 1.1 sjg
112 1.1 sjg if (pbm->filemon_fd < 0) {
113 1.1 sjg useFilemon = FALSE;
114 1.1 sjg warn("Could not open %s", _PATH_FILEMON);
115 1.1 sjg return;
116 1.1 sjg }
117 1.1 sjg
118 1.1 sjg /*
119 1.1 sjg * We use a file outside of '.'
120 1.1 sjg * to avoid a FreeBSD kernel bug where unlink invalidates
121 1.1 sjg * cwd causing getcwd to do a lot more work.
122 1.1 sjg * We only care about the descriptor.
123 1.1 sjg */
124 1.1 sjg pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
125 1.1 sjg if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
126 1.1 sjg err(1, "Could not set filemon file descriptor!");
127 1.1 sjg }
128 1.1 sjg /* we don't need these once we exec */
129 1.1 sjg (void)fcntl(pbm->mon_fd, F_SETFD, 1);
130 1.1 sjg (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
131 1.1 sjg }
132 1.1 sjg
133 1.1 sjg /*
134 1.1 sjg * Read the build monitor output file and write records to the target's
135 1.1 sjg * metadata file.
136 1.1 sjg */
137 1.1 sjg static void
138 1.1 sjg filemon_read(FILE *mfp, int fd)
139 1.1 sjg {
140 1.1 sjg FILE *fp;
141 1.1 sjg char buf[BUFSIZ];
142 1.1 sjg
143 1.1 sjg /* Check if we're not writing to a meta data file.*/
144 1.1 sjg if (mfp == NULL) {
145 1.1 sjg if (fd >= 0)
146 1.1 sjg close(fd); /* not interested */
147 1.1 sjg return;
148 1.1 sjg }
149 1.1 sjg /* rewind */
150 1.1 sjg lseek(fd, SEEK_SET, 0);
151 1.1 sjg if ((fp = fdopen(fd, "r")) == NULL)
152 1.1 sjg err(1, "Could not read build monitor file '%d'", fd);
153 1.1 sjg
154 1.1 sjg fprintf(mfp, "-- filemon acquired metadata --\n");
155 1.1 sjg
156 1.1 sjg while (fgets(buf, sizeof(buf), fp)) {
157 1.1 sjg fprintf(mfp, "%s", buf);
158 1.1 sjg }
159 1.1 sjg fflush(mfp);
160 1.1 sjg clearerr(fp);
161 1.1 sjg fclose(fp);
162 1.1 sjg }
163 1.1 sjg #endif
164 1.1 sjg
165 1.1 sjg static char *
166 1.1 sjg meta_name(struct GNode *gn, char *mname, size_t mnamelen,
167 1.1 sjg const char *dname,
168 1.1 sjg const char *tname)
169 1.1 sjg {
170 1.1 sjg char buf[MAXPATHLEN];
171 1.1 sjg char cwd[MAXPATHLEN];
172 1.1 sjg char *rp;
173 1.1 sjg char *cp;
174 1.1 sjg char *tp;
175 1.1 sjg char *p[4]; /* >= number of possible uses */
176 1.1 sjg int i;
177 1.1 sjg
178 1.1 sjg i = 0;
179 1.1 sjg if (!dname)
180 1.1 sjg dname = Var_Value(".OBJDIR", gn, &p[i++]);
181 1.1 sjg if (!tname)
182 1.1 sjg tname = Var_Value(TARGET, gn, &p[i++]);
183 1.1 sjg
184 1.1 sjg /*
185 1.1 sjg * Weed out relative paths from the target file name.
186 1.1 sjg * We have to be careful though since if target is a
187 1.1 sjg * symlink, the result will be unstable.
188 1.1 sjg * So we use realpath() just to get the dirname, and leave the
189 1.1 sjg * basename as given to us.
190 1.1 sjg */
191 1.1 sjg if ((cp = strrchr(tname, '/'))) {
192 1.1 sjg if (realpath(tname, buf)) {
193 1.1 sjg if ((rp = strrchr(buf, '/'))) {
194 1.1 sjg rp++;
195 1.1 sjg cp++;
196 1.1 sjg if (strcmp(cp, rp) != 0)
197 1.1 sjg strlcpy(rp, cp, sizeof(buf) - (rp - buf));
198 1.1 sjg }
199 1.1 sjg tname = buf;
200 1.1 sjg }
201 1.1 sjg }
202 1.1 sjg if (realpath(dname, cwd))
203 1.1 sjg dname = cwd;
204 1.1 sjg /* on some systems dirname may modify its arg */
205 1.1 sjg tp = bmake_strdup(tname);
206 1.1 sjg if (strcmp(dname, dirname(tp)) == 0)
207 1.1 sjg snprintf(mname, mnamelen, "%s.meta", tname);
208 1.1 sjg else {
209 1.1 sjg snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
210 1.1 sjg
211 1.1 sjg /*
212 1.1 sjg * Replace path separators in the file name after the
213 1.1 sjg * current object directory path.
214 1.1 sjg */
215 1.1 sjg cp = mname + strlen(dname) + 1;
216 1.1 sjg
217 1.1 sjg while (*cp != '\0') {
218 1.1 sjg if (*cp == '/')
219 1.1 sjg *cp = '_';
220 1.1 sjg cp++;
221 1.1 sjg }
222 1.1 sjg }
223 1.1 sjg free(tp);
224 1.1 sjg for (i--; i >= 0; i--) {
225 1.1 sjg if (p[i])
226 1.1 sjg free(p[i]);
227 1.1 sjg }
228 1.1 sjg return (mname);
229 1.1 sjg }
230 1.1 sjg
231 1.1 sjg /*
232 1.1 sjg * Return true if running ${.MAKE}
233 1.1 sjg * Bypassed if target is flagged .MAKE
234 1.1 sjg */
235 1.1 sjg static int
236 1.1 sjg is_submake(void *cmdp, void *gnp)
237 1.1 sjg {
238 1.1 sjg static char *p_make = NULL;
239 1.1 sjg static int p_len;
240 1.1 sjg char *cmd = cmdp;
241 1.1 sjg GNode *gn = gnp;
242 1.1 sjg char *mp = NULL;
243 1.1 sjg char *cp;
244 1.1 sjg char *cp2;
245 1.1 sjg int rc = 0; /* keep looking */
246 1.1 sjg
247 1.1 sjg if (!p_make) {
248 1.1 sjg p_make = Var_Value(".MAKE", gn, &cp);
249 1.1 sjg p_len = strlen(p_make);
250 1.1 sjg }
251 1.1 sjg cp = strchr(cmd, '$');
252 1.1 sjg if ((cp)) {
253 1.1 sjg mp = Var_Subst(NULL, cmd, gn, FALSE);
254 1.1 sjg cmd = mp;
255 1.1 sjg }
256 1.1 sjg cp2 = strstr(cmd, p_make);
257 1.1 sjg if ((cp2)) {
258 1.1 sjg switch (cp2[p_len]) {
259 1.1 sjg case '\0':
260 1.1 sjg case ' ':
261 1.1 sjg case '\t':
262 1.1 sjg case '\n':
263 1.1 sjg rc = 1;
264 1.1 sjg break;
265 1.1 sjg }
266 1.1 sjg if (cp2 > cmd && rc > 0) {
267 1.1 sjg switch (cp2[-1]) {
268 1.1 sjg case ' ':
269 1.1 sjg case '\t':
270 1.1 sjg case '\n':
271 1.1 sjg break;
272 1.1 sjg default:
273 1.1 sjg rc = 0; /* no match */
274 1.1 sjg break;
275 1.1 sjg }
276 1.1 sjg }
277 1.1 sjg }
278 1.1 sjg if (mp)
279 1.1 sjg free(mp);
280 1.1 sjg return (rc);
281 1.1 sjg }
282 1.1 sjg
283 1.1 sjg typedef struct meta_file_s {
284 1.1 sjg FILE *fp;
285 1.1 sjg GNode *gn;
286 1.1 sjg } meta_file_t;
287 1.1 sjg
288 1.1 sjg static int
289 1.1 sjg printCMD(void *cmdp, void *mfpp)
290 1.1 sjg {
291 1.1 sjg meta_file_t *mfp = mfpp;
292 1.1 sjg char *cmd = cmdp;
293 1.1 sjg char *cp = NULL;
294 1.1 sjg
295 1.1 sjg if (strchr(cmd, '$')) {
296 1.1 sjg cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
297 1.1 sjg }
298 1.1 sjg fprintf(mfp->fp, "CMD %s\n", cmd);
299 1.1 sjg if (cp)
300 1.1 sjg free(cp);
301 1.1 sjg return 0;
302 1.1 sjg }
303 1.1 sjg
304 1.1 sjg /*
305 1.1 sjg * Certain node types never get a .meta file
306 1.1 sjg */
307 1.1 sjg #define SKIP_META_TYPE(_type) do { \
308 1.1 sjg if ((gn->type & __CONCAT(OP_, _type))) { \
309 1.1 sjg if (DEBUG(META)) { \
310 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .%s\n", \
311 1.1 sjg gn->name, __STRING(_type)); \
312 1.1 sjg } \
313 1.1 sjg return (NULL); \
314 1.1 sjg } \
315 1.1 sjg } while (0)
316 1.1 sjg
317 1.1 sjg static FILE *
318 1.1 sjg meta_create(BuildMon *pbm, GNode *gn)
319 1.1 sjg {
320 1.1 sjg extern char **environ;
321 1.1 sjg meta_file_t mf;
322 1.1 sjg char buf[MAXPATHLEN];
323 1.1 sjg char curdir[MAXPATHLEN];
324 1.1 sjg char objdir[MAXPATHLEN];
325 1.1 sjg char **ptr;
326 1.1 sjg const char *cname;
327 1.1 sjg const char *dname;
328 1.1 sjg const char *tname;
329 1.1 sjg char *fname;
330 1.1 sjg const char *cp;
331 1.1 sjg char *p[4]; /* >= possible uses */
332 1.1 sjg int i;
333 1.1 sjg struct stat fs;
334 1.1 sjg
335 1.1 sjg
336 1.1 sjg /* This may be a phony node which we don't want meta data for... */
337 1.1 sjg /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
338 1.1 sjg /* Or it may be explicitly flagged as .NOMETA */
339 1.1 sjg SKIP_META_TYPE(NOMETA);
340 1.1 sjg /* Unless it is explicitly flagged as .META */
341 1.1 sjg if (!(gn->type & OP_META)) {
342 1.1 sjg SKIP_META_TYPE(PHONY);
343 1.1 sjg SKIP_META_TYPE(SPECIAL);
344 1.1 sjg SKIP_META_TYPE(MAKE);
345 1.1 sjg }
346 1.1 sjg
347 1.1 sjg mf.fp = NULL;
348 1.1 sjg
349 1.1 sjg i = 0;
350 1.1 sjg
351 1.1 sjg dname = Var_Value(".OBJDIR", gn, &p[i++]);
352 1.1 sjg cname = Var_Value(".CURDIR", gn, &p[i++]);
353 1.1 sjg tname = Var_Value(TARGET, gn, &p[i++]);
354 1.1 sjg
355 1.1 sjg /* The object directory may not exist. Check it.. */
356 1.1 sjg if (stat(dname, &fs) != 0) {
357 1.1 sjg if (DEBUG(META))
358 1.1 sjg fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
359 1.1 sjg gn->name);
360 1.1 sjg goto out;
361 1.1 sjg }
362 1.1 sjg /* Check if there are no commands to execute. */
363 1.1 sjg if (Lst_IsEmpty(gn->commands)) {
364 1.1 sjg if (DEBUG(META))
365 1.1 sjg fprintf(debug_file, "Skipping meta for %s: no commands\n",
366 1.1 sjg gn->name);
367 1.1 sjg goto out;
368 1.1 sjg }
369 1.1 sjg
370 1.1 sjg /* make sure these are canonical */
371 1.1 sjg if (realpath(dname, objdir))
372 1.1 sjg dname = objdir;
373 1.1 sjg if (realpath(cname, curdir))
374 1.1 sjg cname = curdir;
375 1.1 sjg
376 1.1 sjg /* If we aren't in the object directory, don't create a meta file. */
377 1.1 sjg if (strcmp(cname, dname) == 0) {
378 1.1 sjg if (DEBUG(META))
379 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
380 1.1 sjg gn->name);
381 1.1 sjg goto out;
382 1.1 sjg }
383 1.1 sjg if (!(gn->type & OP_META)) {
384 1.1 sjg /* We do not generate .meta files for sub-makes */
385 1.1 sjg if (Lst_ForEach(gn->commands, is_submake, gn)) {
386 1.1 sjg if (DEBUG(META))
387 1.1 sjg fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
388 1.1 sjg gn->name);
389 1.1 sjg goto out;
390 1.1 sjg }
391 1.1 sjg }
392 1.1 sjg
393 1.1 sjg if (metaVerbose) {
394 1.1 sjg char *mp;
395 1.1 sjg
396 1.1 sjg /* Describe the target we are building */
397 1.1 sjg mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
398 1.1 sjg if (*mp)
399 1.1 sjg fprintf(stdout, "%s\n", mp);
400 1.1 sjg free(mp);
401 1.1 sjg }
402 1.1 sjg /* Get the basename of the target */
403 1.1 sjg if ((cp = strrchr(tname, '/')) == NULL) {
404 1.1 sjg cp = tname;
405 1.1 sjg } else {
406 1.1 sjg cp++;
407 1.1 sjg }
408 1.1 sjg
409 1.1 sjg fflush(stdout);
410 1.1 sjg
411 1.1 sjg if (strcmp(cp, makeDependfile) == 0)
412 1.1 sjg goto out;
413 1.1 sjg
414 1.1 sjg if (!writeMeta)
415 1.1 sjg /* Don't create meta data. */
416 1.1 sjg goto out;
417 1.1 sjg
418 1.1 sjg fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
419 1.1 sjg dname, tname);
420 1.1 sjg
421 1.1 sjg if ((mf.fp = fopen(fname, "w")) == NULL)
422 1.1 sjg err(1, "Could not open meta file '%s'", fname);
423 1.1 sjg
424 1.1 sjg fprintf(mf.fp, "# Meta data file %s\n", fname);
425 1.1 sjg
426 1.1 sjg mf.gn = gn;
427 1.1 sjg
428 1.1 sjg Lst_ForEach(gn->commands, printCMD, &mf);
429 1.1 sjg
430 1.1 sjg fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
431 1.1 sjg fprintf(mf.fp, "TARGET %s\n", tname);
432 1.1 sjg
433 1.1 sjg if (metaEnv) {
434 1.1 sjg for (ptr = environ; *ptr != NULL; ptr++)
435 1.1 sjg fprintf(mf.fp, "ENV %s\n", *ptr);
436 1.1 sjg }
437 1.1 sjg
438 1.1 sjg fprintf(mf.fp, "-- command output --\n");
439 1.1 sjg fflush(mf.fp);
440 1.1 sjg
441 1.1 sjg Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
442 1.1 sjg Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
443 1.1 sjg
444 1.1 sjg out:
445 1.1 sjg for (i--; i >= 0; i--) {
446 1.1 sjg if (p[i])
447 1.1 sjg free(p[i]);
448 1.1 sjg }
449 1.1 sjg
450 1.1 sjg return (mf.fp);
451 1.1 sjg }
452 1.1 sjg
453 1.1 sjg
454 1.1 sjg void
455 1.1 sjg meta_init(const char *make_mode)
456 1.1 sjg {
457 1.1 sjg static int once = 0;
458 1.1 sjg
459 1.1 sjg useMeta = TRUE;
460 1.1 sjg useFilemon = TRUE;
461 1.1 sjg writeMeta = TRUE;
462 1.1 sjg
463 1.1 sjg if (make_mode) {
464 1.1 sjg if (strstr(make_mode, "env"))
465 1.1 sjg metaEnv = TRUE;
466 1.1 sjg if (strstr(make_mode, "verb"))
467 1.1 sjg metaVerbose = TRUE;
468 1.1 sjg if (strstr(make_mode, "read"))
469 1.1 sjg writeMeta = FALSE;
470 1.1 sjg if (strstr(make_mode, "nofilemon"))
471 1.1 sjg useFilemon = FALSE;
472 1.1 sjg if (strstr(make_mode, "ignore-cmd"))
473 1.1 sjg metaIgnoreCMDs = TRUE;
474 1.1 sjg /* for backwards compatability */
475 1.1 sjg Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
476 1.1 sjg Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
477 1.1 sjg }
478 1.1 sjg if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
479 1.1 sjg /*
480 1.1 sjg * The default value for MAKE_META_PREFIX
481 1.1 sjg * prints the absolute path of the target.
482 1.1 sjg * This works be cause :H will generate '.' if there is no /
483 1.1 sjg * and :tA will resolve that to cwd.
484 1.1 sjg */
485 1.1 sjg Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
486 1.1 sjg }
487 1.1 sjg if (once)
488 1.1 sjg return;
489 1.1 sjg once = 1;
490 1.1 sjg memset(&Mybm, 0, sizeof(Mybm));
491 1.1 sjg }
492 1.1 sjg
493 1.1 sjg /*
494 1.1 sjg * In each case below we allow for job==NULL
495 1.1 sjg */
496 1.1 sjg void
497 1.1 sjg meta_job_start(Job *job, GNode *gn)
498 1.1 sjg {
499 1.1 sjg BuildMon *pbm;
500 1.1 sjg
501 1.1 sjg if (job != NULL) {
502 1.1 sjg pbm = &job->bm;
503 1.1 sjg } else {
504 1.1 sjg pbm = &Mybm;
505 1.1 sjg }
506 1.1 sjg pbm->mfp = meta_create(pbm, gn);
507 1.1 sjg #ifdef USE_FILEMON_ONCE
508 1.1 sjg /* compat mode we open the filemon dev once per command */
509 1.1 sjg if (job == NULL)
510 1.1 sjg return;
511 1.1 sjg #endif
512 1.1 sjg #ifdef USE_FILEMON
513 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
514 1.1 sjg filemon_open(pbm);
515 1.1 sjg } else {
516 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
517 1.1 sjg }
518 1.1 sjg #endif
519 1.1 sjg }
520 1.1 sjg
521 1.1 sjg /*
522 1.1 sjg * The child calls this before doing anything.
523 1.1 sjg * It does not disturb our state.
524 1.1 sjg */
525 1.1 sjg void
526 1.1 sjg meta_job_child(Job *job)
527 1.1 sjg {
528 1.1 sjg #ifdef USE_FILEMON
529 1.1 sjg BuildMon *pbm;
530 1.1 sjg pid_t pid;
531 1.1 sjg
532 1.1 sjg if (job != NULL) {
533 1.1 sjg pbm = &job->bm;
534 1.1 sjg } else {
535 1.1 sjg pbm = &Mybm;
536 1.1 sjg }
537 1.1 sjg pid = getpid();
538 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
539 1.1 sjg if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
540 1.1 sjg err(1, "Could not set filemon pid!");
541 1.1 sjg }
542 1.1 sjg }
543 1.1 sjg #endif
544 1.1 sjg }
545 1.1 sjg
546 1.1 sjg void
547 1.1 sjg meta_job_error(Job *job, GNode *gn, int flags, int status)
548 1.1 sjg {
549 1.1 sjg char cwd[MAXPATHLEN];
550 1.1 sjg BuildMon *pbm;
551 1.1 sjg
552 1.1 sjg if (job != NULL) {
553 1.1 sjg pbm = &job->bm;
554 1.1 sjg } else {
555 1.1 sjg if (!gn)
556 1.1 sjg gn = job->node;
557 1.1 sjg pbm = &Mybm;
558 1.1 sjg }
559 1.1 sjg if (pbm->mfp != NULL) {
560 1.1 sjg fprintf(pbm->mfp, "*** Error code %d%s\n",
561 1.1 sjg status,
562 1.1 sjg (flags & JOB_IGNERR) ?
563 1.1 sjg "(ignored)" : "");
564 1.1 sjg }
565 1.1 sjg if (gn) {
566 1.1 sjg Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
567 1.1 sjg }
568 1.1 sjg getcwd(cwd, sizeof(cwd));
569 1.1 sjg Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
570 1.1 sjg #ifdef USE_FILEMON
571 1.1 sjg if (pbm) {
572 1.1 sjg Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
573 1.1 sjg }
574 1.1 sjg #endif
575 1.1 sjg }
576 1.1 sjg
577 1.1 sjg void
578 1.1 sjg meta_job_output(Job *job, char *cp, const char *nl)
579 1.1 sjg {
580 1.1 sjg BuildMon *pbm;
581 1.1 sjg
582 1.1 sjg if (job != NULL) {
583 1.1 sjg pbm = &job->bm;
584 1.1 sjg } else {
585 1.1 sjg pbm = &Mybm;
586 1.1 sjg }
587 1.1 sjg if (pbm->mfp != NULL) {
588 1.1 sjg if (metaVerbose) {
589 1.1 sjg static char *meta_prefix = NULL;
590 1.1 sjg static int meta_prefix_len;
591 1.1 sjg
592 1.1 sjg if (!meta_prefix) {
593 1.1 sjg char *cp2;
594 1.1 sjg
595 1.1 sjg meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
596 1.1 sjg if ((cp2 = strchr(meta_prefix, '$')))
597 1.1 sjg meta_prefix_len = cp2 - meta_prefix;
598 1.1 sjg else
599 1.1 sjg meta_prefix_len = strlen(meta_prefix);
600 1.1 sjg }
601 1.1 sjg if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
602 1.1 sjg cp = strchr(cp+1, '\n');
603 1.1 sjg if (!cp++)
604 1.1 sjg return;
605 1.1 sjg }
606 1.1 sjg }
607 1.1 sjg fprintf(pbm->mfp, "%s%s", cp, nl);
608 1.1 sjg }
609 1.1 sjg }
610 1.1 sjg
611 1.1 sjg void
612 1.1 sjg meta_cmd_finish(void *pbmp)
613 1.1 sjg {
614 1.1 sjg #ifdef USE_FILEMON
615 1.1 sjg BuildMon *pbm = pbmp;
616 1.1 sjg
617 1.1 sjg if (!pbm)
618 1.1 sjg pbm = &Mybm;
619 1.1 sjg
620 1.1 sjg if (pbm->filemon_fd >= 0) {
621 1.1 sjg close(pbm->filemon_fd);
622 1.1 sjg filemon_read(pbm->mfp, pbm->mon_fd);
623 1.1 sjg pbm->filemon_fd = pbm->mon_fd = -1;
624 1.1 sjg }
625 1.1 sjg #endif
626 1.1 sjg }
627 1.1 sjg
628 1.1 sjg void
629 1.1 sjg meta_job_finish(Job *job)
630 1.1 sjg {
631 1.1 sjg BuildMon *pbm;
632 1.1 sjg
633 1.1 sjg if (job != NULL) {
634 1.1 sjg pbm = &job->bm;
635 1.1 sjg } else {
636 1.1 sjg pbm = &Mybm;
637 1.1 sjg }
638 1.1 sjg if (pbm->mfp != NULL) {
639 1.1 sjg meta_cmd_finish(pbm);
640 1.1 sjg fclose(pbm->mfp);
641 1.1 sjg pbm->mfp = NULL;
642 1.1 sjg }
643 1.1 sjg }
644 1.1 sjg
645 1.1 sjg /*
646 1.1 sjg * Fetch a full line from fp - growing bufp if needed
647 1.1 sjg * Return length in bufp.
648 1.1 sjg */
649 1.1 sjg static int
650 1.1 sjg fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
651 1.1 sjg {
652 1.1 sjg char *buf = *bufp;
653 1.1 sjg size_t bufsz = *szp;
654 1.1 sjg struct stat fs;
655 1.1 sjg int x;
656 1.1 sjg
657 1.1 sjg if (fgets(&buf[o], bufsz - o, fp) != NULL) {
658 1.1 sjg check_newline:
659 1.1 sjg x = o + strlen(&buf[o]);
660 1.1 sjg if (buf[x - 1] == '\n')
661 1.1 sjg return x;
662 1.1 sjg /*
663 1.1 sjg * We need to grow the buffer.
664 1.1 sjg * The meta file can give us a clue.
665 1.1 sjg */
666 1.1 sjg if (fstat(fileno(fp), &fs) == 0) {
667 1.1 sjg size_t newsz;
668 1.1 sjg char *p;
669 1.1 sjg
670 1.1 sjg newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
671 1.1 sjg if (newsz <= bufsz)
672 1.1 sjg newsz = ROUNDUP(fs.st_size, BUFSIZ);
673 1.1 sjg if (DEBUG(META))
674 1.1 sjg fprintf(debug_file, "growing buffer %u -> %u\n",
675 1.1 sjg bufsz, newsz);
676 1.1 sjg p = bmake_realloc(buf, newsz);
677 1.1 sjg if (p) {
678 1.1 sjg *bufp = buf = p;
679 1.1 sjg *szp = bufsz = newsz;
680 1.1 sjg /* fetch the rest */
681 1.1 sjg if (!fgets(&buf[x], bufsz - x, fp))
682 1.1 sjg return x; /* truncated! */
683 1.1 sjg goto check_newline;
684 1.1 sjg }
685 1.1 sjg }
686 1.1 sjg }
687 1.1 sjg return 0;
688 1.1 sjg }
689 1.1 sjg
690 1.1 sjg /*
691 1.1 sjg * When running with 'meta' functionality, a target can be out-of-date
692 1.1 sjg * if any of the references in it's meta data file is more recent.
693 1.1 sjg */
694 1.1 sjg Boolean
695 1.1 sjg meta_oodate(GNode *gn, Boolean oodate)
696 1.1 sjg {
697 1.1 sjg char latestdir[MAXPATHLEN];
698 1.1 sjg char fname[MAXPATHLEN];
699 1.1 sjg char fname1[MAXPATHLEN];
700 1.1 sjg char *p;
701 1.1 sjg char *cp;
702 1.1 sjg FILE *fp;
703 1.1 sjg Boolean ignoreOODATE = FALSE;
704 1.1 sjg
705 1.1 sjg /*
706 1.1 sjg * We need to check if the target is out-of-date. This includes
707 1.1 sjg * checking if the expanded command has changed. This in turn
708 1.1 sjg * requires that all variables are set in the same way that they
709 1.1 sjg * would be if the target needs to be re-built.
710 1.1 sjg */
711 1.1 sjg Make_DoAllVar(gn);
712 1.1 sjg
713 1.1 sjg if (oodate)
714 1.1 sjg return oodate; /* we're done */
715 1.1 sjg
716 1.1 sjg if (getcwd(latestdir, sizeof(latestdir)) == NULL)
717 1.1 sjg err(1, "Could not get current working directory");
718 1.1 sjg
719 1.1 sjg meta_name(gn, fname, sizeof(fname), NULL, NULL);
720 1.1 sjg
721 1.1 sjg if ((fp = fopen(fname, "r")) != NULL) {
722 1.1 sjg static char *buf = NULL;
723 1.1 sjg static size_t bufsz;
724 1.1 sjg int lineno = 0;
725 1.1 sjg int f = 0;
726 1.1 sjg int x;
727 1.1 sjg LstNode ln;
728 1.1 sjg struct stat fs;
729 1.1 sjg
730 1.1 sjg if (!buf) {
731 1.1 sjg bufsz = 8 * BUFSIZ;
732 1.1 sjg buf = bmake_malloc(bufsz);
733 1.1 sjg }
734 1.1 sjg
735 1.1 sjg /* we want to track all the .meta we read */
736 1.1 sjg Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
737 1.1 sjg
738 1.1 sjg ln = Lst_First(gn->commands);
739 1.1 sjg while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
740 1.1 sjg lineno++;
741 1.1 sjg if (buf[x - 1] == '\n')
742 1.1 sjg buf[x - 1] = '\0';
743 1.1 sjg else
744 1.1 sjg warnx("%s: %d: line truncated at %u", fname, lineno, x);
745 1.1 sjg
746 1.1 sjg /* Find the start of the build monitor section. */
747 1.1 sjg if (!f) {
748 1.1 sjg if (strncmp(buf, "-- filemon", 10) == 0) {
749 1.1 sjg f = 1;
750 1.1 sjg continue;
751 1.1 sjg }
752 1.1 sjg if (strncmp(buf, "# buildmon", 10) == 0) {
753 1.1 sjg f = 1;
754 1.1 sjg continue;
755 1.1 sjg }
756 1.1 sjg }
757 1.1 sjg
758 1.1 sjg /* Delimit the record type. */
759 1.1 sjg p = buf;
760 1.1 sjg strsep(&p, " ");
761 1.1 sjg if (f) {
762 1.1 sjg /* Process according to record type. */
763 1.1 sjg switch (buf[0]) {
764 1.1 sjg case 'C':
765 1.1 sjg /* Skip the pid. */
766 1.1 sjg if (strsep(&p, " ") == NULL)
767 1.1 sjg break;
768 1.1 sjg
769 1.1 sjg /* Update the latest directory. */
770 1.1 sjg strlcpy(latestdir, p, sizeof(latestdir));
771 1.1 sjg break;
772 1.1 sjg
773 1.1 sjg case 'R':
774 1.1 sjg case 'E':
775 1.1 sjg /* Skip the pid. */
776 1.1 sjg if (strsep(&p, " ") == NULL)
777 1.1 sjg break;
778 1.1 sjg
779 1.1 sjg /*
780 1.1 sjg * Check for runtime files that can't
781 1.1 sjg * be part of the dependencies because
782 1.1 sjg * they are _expected_ to change.
783 1.1 sjg */
784 1.1 sjg if (strncmp(p, "/var/", 5) == 0)
785 1.1 sjg break;
786 1.1 sjg
787 1.1 sjg /* Ignore device files. */
788 1.1 sjg if (strncmp(p, "/dev/", 5) == 0)
789 1.1 sjg break;
790 1.1 sjg
791 1.1 sjg /* Ignore /etc/ files. */
792 1.1 sjg if (strncmp(p, "/etc/", 5) == 0)
793 1.1 sjg break;
794 1.1 sjg
795 1.1 sjg /*
796 1.1 sjg * The rest of the record is the
797 1.1 sjg * file name.
798 1.1 sjg * Check if it's not an absolute
799 1.1 sjg * path.
800 1.1 sjg */
801 1.1 sjg if (*p != '/') {
802 1.1 sjg /* Use the latest path seen. */
803 1.1 sjg snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
804 1.1 sjg p = fname1;
805 1.1 sjg }
806 1.1 sjg
807 1.1 sjg if (stat(p, &fs) == 0 &&
808 1.1 sjg !S_ISDIR(fs.st_mode) &&
809 1.1 sjg fs.st_mtime > gn->mtime) {
810 1.1 sjg if (DEBUG(META))
811 1.1 sjg fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
812 1.1 sjg oodate = TRUE;
813 1.1 sjg }
814 1.1 sjg break;
815 1.1 sjg default:
816 1.1 sjg break;
817 1.1 sjg }
818 1.1 sjg
819 1.1 sjg /*
820 1.1 sjg * Compare the current command with the one in the
821 1.1 sjg * meta data file.
822 1.1 sjg */
823 1.1 sjg } else if (strcmp(buf, "CMD") == 0) {
824 1.1 sjg if (ln == NULL) {
825 1.1 sjg if (DEBUG(META))
826 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);
827 1.1 sjg oodate = TRUE;
828 1.1 sjg } else {
829 1.1 sjg char *cmd = (char *)Lst_Datum(ln);
830 1.1 sjg
831 1.1 sjg if (!ignoreOODATE) {
832 1.1 sjg if (strstr(cmd, "$?"))
833 1.1 sjg ignoreOODATE = TRUE;
834 1.1 sjg else if ((cp = strstr(cmd, ".OODATE"))) {
835 1.1 sjg /* check for $[{(].OODATE[)}] */
836 1.1 sjg if (cp > cmd + 2 && cp[-2] == '$')
837 1.1 sjg ignoreOODATE = TRUE;
838 1.1 sjg }
839 1.1 sjg if (ignoreOODATE && DEBUG(META))
840 1.1 sjg fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
841 1.1 sjg }
842 1.1 sjg cmd = Var_Subst(NULL, cmd, gn, TRUE);
843 1.1 sjg
844 1.1 sjg if ((cp = strchr(cmd, '\n'))) {
845 1.1 sjg int n;
846 1.1 sjg
847 1.1 sjg /*
848 1.1 sjg * This command contains newlines, we need to
849 1.1 sjg * fetch more from the .meta file before we
850 1.1 sjg * attempt a comparison.
851 1.1 sjg */
852 1.1 sjg /* first put the newline back at buf[x - 1] */
853 1.1 sjg buf[x - 1] = '\n';
854 1.1 sjg do {
855 1.1 sjg /* now fetch the next line */
856 1.1 sjg if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
857 1.1 sjg break;
858 1.1 sjg x = n;
859 1.1 sjg lineno++;
860 1.1 sjg if (buf[x - 1] != '\n') {
861 1.1 sjg warnx("%s: %d: line truncated at %u", fname, lineno, x);
862 1.1 sjg break;
863 1.1 sjg }
864 1.1 sjg cp = strchr(++cp, '\n');
865 1.1 sjg } while (cp);
866 1.1 sjg if (buf[x - 1] == '\n')
867 1.1 sjg buf[x - 1] = '\0';
868 1.1 sjg }
869 1.1 sjg if (!ignoreOODATE &&
870 1.1 sjg !(gn->type & OP_NOMETA_CMP) &&
871 1.1 sjg strcmp(p, cmd) != 0) {
872 1.1 sjg if (DEBUG(META))
873 1.1 sjg fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
874 1.1 sjg if (!metaIgnoreCMDs)
875 1.1 sjg oodate = TRUE;
876 1.1 sjg }
877 1.1 sjg free(cmd);
878 1.1 sjg ln = Lst_Succ(ln);
879 1.1 sjg }
880 1.1 sjg } else if (strcmp(buf, "CWD") == 0) {
881 1.1 sjg char curdir[MAXPATHLEN];
882 1.1 sjg if (strcmp(p, getcwd(curdir, sizeof(curdir))) != 0) {
883 1.1 sjg if (DEBUG(META))
884 1.1 sjg fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
885 1.1 sjg oodate = TRUE;
886 1.1 sjg }
887 1.1 sjg }
888 1.1 sjg }
889 1.1 sjg
890 1.1 sjg /*
891 1.1 sjg * Check if there are extra commands now
892 1.1 sjg * that weren't in the meta data file.
893 1.1 sjg */
894 1.1 sjg if (!oodate && ln != NULL) {
895 1.1 sjg if (DEBUG(META))
896 1.1 sjg fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
897 1.1 sjg oodate = TRUE;
898 1.1 sjg }
899 1.1 sjg
900 1.1 sjg fclose(fp);
901 1.1 sjg }
902 1.1 sjg return oodate;
903 1.1 sjg }
904 1.1 sjg
905 1.1 sjg /* support for compat mode */
906 1.1 sjg
907 1.1 sjg static int childPipe[2];
908 1.1 sjg
909 1.1 sjg void
910 1.1 sjg meta_compat_start(void)
911 1.1 sjg {
912 1.1 sjg #ifdef USE_FILEMON_ONCE
913 1.1 sjg /*
914 1.1 sjg * We need to re-open filemon for each cmd.
915 1.1 sjg */
916 1.1 sjg BuildMon *pbm = &Mybm;
917 1.1 sjg
918 1.1 sjg if (pbm->mfp != NULL && useFilemon) {
919 1.1 sjg filemon_open(pbm);
920 1.1 sjg } else {
921 1.1 sjg pbm->mon_fd = pbm->filemon_fd = -1;
922 1.1 sjg }
923 1.1 sjg #endif
924 1.1 sjg if (pipe(childPipe) < 0)
925 1.1 sjg Punt("Cannot create pipe: %s", strerror(errno));
926 1.1 sjg /* Set close-on-exec flag for both */
927 1.1 sjg (void)fcntl(childPipe[0], F_SETFD, 1);
928 1.1 sjg (void)fcntl(childPipe[1], F_SETFD, 1);
929 1.1 sjg }
930 1.1 sjg
931 1.1 sjg void
932 1.1 sjg meta_compat_child(void)
933 1.1 sjg {
934 1.1 sjg meta_job_child(NULL);
935 1.1 sjg if (dup2(childPipe[1], 1) < 0 ||
936 1.1 sjg dup2(1, 2) < 0) {
937 1.1 sjg execError("dup2", "pipe");
938 1.1 sjg _exit(1);
939 1.1 sjg }
940 1.1 sjg }
941 1.1 sjg
942 1.1 sjg void
943 1.1 sjg meta_compat_parent(void)
944 1.1 sjg {
945 1.1 sjg FILE *fp;
946 1.1 sjg char buf[BUFSIZ];
947 1.1 sjg
948 1.1 sjg close(childPipe[1]); /* child side */
949 1.1 sjg fp = fdopen(childPipe[0], "r");
950 1.1 sjg while (fgets(buf, sizeof(buf), fp)) {
951 1.1 sjg meta_job_output(NULL, buf, "");
952 1.1 sjg printf("%s", buf);
953 1.1 sjg }
954 1.1 sjg fclose(fp);
955 1.1 sjg }
956