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