mkfs: drop support for running on DOS
[minix.git] / usr.bin / make / meta.c
blob37a5b29c294d3341ff14e8aa767db7e4529f8c47
1 /* $NetBSD: meta.c,v 1.24 2011/09/21 14:30:47 christos Exp $ */
3 /*
4 * Implement 'meta' mode.
5 * Adapted from John Birrell's patches to FreeBSD make.
6 * --sjg
7 */
8 /*
9 * Copyright (c) 2009-2010, Juniper Networks, Inc.
10 * Portions Copyright (c) 2009, John Birrell.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
33 #if defined(USE_META)
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <fcntl.h>
41 #include <libgen.h>
42 #include <errno.h>
43 #if !defined(HAVE_CONFIG_H) || defined(HAVE_ERR_H)
44 #include <err.h>
45 #endif
47 #include "make.h"
48 #include "job.h"
50 #ifdef HAVE_FILEMON_H
51 # include <filemon.h>
52 #endif
53 #if !defined(USE_FILEMON) && defined(FILEMON_SET_FD)
54 # define USE_FILEMON
55 #endif
57 static BuildMon Mybm; /* for compat */
58 static Lst metaBailiwick; /* our scope of control */
60 Boolean useMeta = FALSE;
61 static Boolean useFilemon = FALSE;
62 static Boolean writeMeta = FALSE;
63 static Boolean metaEnv = FALSE; /* don't save env unless asked */
64 static Boolean metaVerbose = FALSE;
65 static Boolean metaIgnoreCMDs = FALSE; /* ignore CMDs in .meta files */
66 static Boolean metaCurdirOk = FALSE; /* write .meta in .CURDIR Ok? */
67 static Boolean metaSilent = FALSE; /* if we have a .meta be SILENT */
69 extern Boolean forceJobs;
70 extern Boolean comatMake;
72 #define MAKE_META_PREFIX ".MAKE.META.PREFIX"
74 #ifndef N2U
75 # define N2U(n, u) (((n) + ((u) - 1)) / (u))
76 #endif
77 #ifndef ROUNDUP
78 # define ROUNDUP(n, u) (N2U((n), (u)) * (u))
79 #endif
81 #if !defined(HAVE_STRSEP)
82 # define strsep(s, d) stresep((s), (d), 0)
83 #endif
86 * Filemon is a kernel module which snoops certain syscalls.
88 * C chdir
89 * E exec
90 * F [v]fork
91 * L [sym]link
92 * M rename
93 * R read
94 * W write
95 * S stat
97 * See meta_oodate below - we mainly care about 'E' and 'R'.
99 * We can still use meta mode without filemon, but
100 * the benefits are more limited.
102 #ifdef USE_FILEMON
103 # ifndef _PATH_FILEMON
104 # define _PATH_FILEMON "/dev/filemon"
105 # endif
108 * Open the filemon device.
110 static void
111 filemon_open(BuildMon *pbm)
113 int retry;
115 pbm->mon_fd = pbm->filemon_fd = -1;
116 if (!useFilemon)
117 return;
119 for (retry = 5; retry >= 0; retry--) {
120 if ((pbm->filemon_fd = open(_PATH_FILEMON, O_RDWR)) >= 0)
121 break;
124 if (pbm->filemon_fd < 0) {
125 useFilemon = FALSE;
126 warn("Could not open %s", _PATH_FILEMON);
127 return;
131 * We use a file outside of '.'
132 * to avoid a FreeBSD kernel bug where unlink invalidates
133 * cwd causing getcwd to do a lot more work.
134 * We only care about the descriptor.
136 pbm->mon_fd = mkTempFile("filemon.XXXXXX", NULL);
137 if (ioctl(pbm->filemon_fd, FILEMON_SET_FD, &pbm->mon_fd) < 0) {
138 err(1, "Could not set filemon file descriptor!");
140 /* we don't need these once we exec */
141 (void)fcntl(pbm->mon_fd, F_SETFD, 1);
142 (void)fcntl(pbm->filemon_fd, F_SETFD, 1);
146 * Read the build monitor output file and write records to the target's
147 * metadata file.
149 static void
150 filemon_read(FILE *mfp, int fd)
152 FILE *fp;
153 char buf[BUFSIZ];
155 /* Check if we're not writing to a meta data file.*/
156 if (mfp == NULL) {
157 if (fd >= 0)
158 close(fd); /* not interested */
159 return;
161 /* rewind */
162 (void)lseek(fd, (off_t)0, SEEK_SET);
163 if ((fp = fdopen(fd, "r")) == NULL)
164 err(1, "Could not read build monitor file '%d'", fd);
166 fprintf(mfp, "-- filemon acquired metadata --\n");
168 while (fgets(buf, sizeof(buf), fp)) {
169 fprintf(mfp, "%s", buf);
171 fflush(mfp);
172 clearerr(fp);
173 fclose(fp);
175 #endif
178 * when realpath() fails,
179 * we use this, to clean up ./ and ../
181 static void
182 eat_dots(char *buf, size_t bufsz, int dots)
184 char *cp;
185 char *cp2;
186 const char *eat;
187 size_t eatlen;
189 switch (dots) {
190 case 1:
191 eat = "/./";
192 eatlen = 2;
193 break;
194 case 2:
195 eat = "/../";
196 eatlen = 3;
197 break;
198 default:
199 return;
202 do {
203 cp = strstr(buf, eat);
204 if (cp) {
205 cp2 = cp + eatlen;
206 if (dots == 2 && cp > buf) {
207 do {
208 cp--;
209 } while (cp > buf && *cp != '/');
211 if (*cp == '/') {
212 strlcpy(cp, cp2, bufsz - (cp - buf));
213 } else {
214 return; /* can't happen? */
217 } while (cp);
220 static char *
221 meta_name(struct GNode *gn, char *mname, size_t mnamelen,
222 const char *dname,
223 const char *tname)
225 char buf[MAXPATHLEN];
226 char cwd[MAXPATHLEN];
227 char *rp;
228 char *cp;
229 char *tp;
230 char *p[4]; /* >= number of possible uses */
231 int i;
233 i = 0;
234 if (!dname)
235 dname = Var_Value(".OBJDIR", gn, &p[i++]);
236 if (!tname)
237 tname = Var_Value(TARGET, gn, &p[i++]);
239 if (realpath(dname, cwd))
240 dname = cwd;
243 * Weed out relative paths from the target file name.
244 * We have to be careful though since if target is a
245 * symlink, the result will be unstable.
246 * So we use realpath() just to get the dirname, and leave the
247 * basename as given to us.
249 if ((cp = strrchr(tname, '/'))) {
250 if (realpath(tname, buf)) {
251 if ((rp = strrchr(buf, '/'))) {
252 rp++;
253 cp++;
254 if (strcmp(cp, rp) != 0)
255 strlcpy(rp, cp, sizeof(buf) - (rp - buf));
257 tname = buf;
258 } else {
260 * We likely have a directory which is about to be made.
261 * We pretend realpath() succeeded, to have a chance
262 * of generating the same meta file name that we will
263 * next time through.
265 if (tname[0] == '/') {
266 strlcpy(buf, tname, sizeof(buf));
267 } else {
268 snprintf(buf, sizeof(buf), "%s/%s", cwd, tname);
270 eat_dots(buf, sizeof(buf), 1); /* ./ */
271 eat_dots(buf, sizeof(buf), 2); /* ../ */
272 tname = buf;
275 /* on some systems dirname may modify its arg */
276 tp = bmake_strdup(tname);
277 if (strcmp(dname, dirname(tp)) == 0)
278 snprintf(mname, mnamelen, "%s.meta", tname);
279 else {
280 snprintf(mname, mnamelen, "%s/%s.meta", dname, tname);
283 * Replace path separators in the file name after the
284 * current object directory path.
286 cp = mname + strlen(dname) + 1;
288 while (*cp != '\0') {
289 if (*cp == '/')
290 *cp = '_';
291 cp++;
294 free(tp);
295 for (i--; i >= 0; i--) {
296 if (p[i])
297 free(p[i]);
299 return (mname);
303 * Return true if running ${.MAKE}
304 * Bypassed if target is flagged .MAKE
306 static int
307 is_submake(void *cmdp, void *gnp)
309 static char *p_make = NULL;
310 static int p_len;
311 char *cmd = cmdp;
312 GNode *gn = gnp;
313 char *mp = NULL;
314 char *cp;
315 char *cp2;
316 int rc = 0; /* keep looking */
318 if (!p_make) {
319 p_make = Var_Value(".MAKE", gn, &cp);
320 p_len = strlen(p_make);
322 cp = strchr(cmd, '$');
323 if ((cp)) {
324 mp = Var_Subst(NULL, cmd, gn, FALSE);
325 cmd = mp;
327 cp2 = strstr(cmd, p_make);
328 if ((cp2)) {
329 switch (cp2[p_len]) {
330 case '\0':
331 case ' ':
332 case '\t':
333 case '\n':
334 rc = 1;
335 break;
337 if (cp2 > cmd && rc > 0) {
338 switch (cp2[-1]) {
339 case ' ':
340 case '\t':
341 case '\n':
342 break;
343 default:
344 rc = 0; /* no match */
345 break;
349 if (mp)
350 free(mp);
351 return (rc);
354 typedef struct meta_file_s {
355 FILE *fp;
356 GNode *gn;
357 } meta_file_t;
359 static int
360 printCMD(void *cmdp, void *mfpp)
362 meta_file_t *mfp = mfpp;
363 char *cmd = cmdp;
364 char *cp = NULL;
366 if (strchr(cmd, '$')) {
367 cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
369 fprintf(mfp->fp, "CMD %s\n", cmd);
370 if (cp)
371 free(cp);
372 return 0;
376 * Certain node types never get a .meta file
378 #define SKIP_META_TYPE(_type) do { \
379 if ((gn->type & __CONCAT(OP_, _type))) { \
380 if (DEBUG(META)) { \
381 fprintf(debug_file, "Skipping meta for %s: .%s\n", \
382 gn->name, __STRING(_type)); \
384 return (NULL); \
386 } while (0)
388 static FILE *
389 meta_create(BuildMon *pbm, GNode *gn)
391 extern char **environ;
392 meta_file_t mf;
393 char buf[MAXPATHLEN];
394 char objdir[MAXPATHLEN];
395 char **ptr;
396 const char *dname;
397 const char *tname;
398 char *fname;
399 const char *cp;
400 char *p[4]; /* >= possible uses */
401 int i;
402 struct stat fs;
405 /* This may be a phony node which we don't want meta data for... */
406 /* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
407 /* Or it may be explicitly flagged as .NOMETA */
408 SKIP_META_TYPE(NOMETA);
409 /* Unless it is explicitly flagged as .META */
410 if (!(gn->type & OP_META)) {
411 SKIP_META_TYPE(PHONY);
412 SKIP_META_TYPE(SPECIAL);
413 SKIP_META_TYPE(MAKE);
416 mf.fp = NULL;
418 i = 0;
420 dname = Var_Value(".OBJDIR", gn, &p[i++]);
421 tname = Var_Value(TARGET, gn, &p[i++]);
423 /* The object directory may not exist. Check it.. */
424 if (stat(dname, &fs) != 0) {
425 if (DEBUG(META))
426 fprintf(debug_file, "Skipping meta for %s: no .OBJDIR\n",
427 gn->name);
428 goto out;
430 /* Check if there are no commands to execute. */
431 if (Lst_IsEmpty(gn->commands)) {
432 if (DEBUG(META))
433 fprintf(debug_file, "Skipping meta for %s: no commands\n",
434 gn->name);
435 goto out;
438 /* make sure these are canonical */
439 if (realpath(dname, objdir))
440 dname = objdir;
442 /* If we aren't in the object directory, don't create a meta file. */
443 if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
444 if (DEBUG(META))
445 fprintf(debug_file, "Skipping meta for %s: .OBJDIR == .CURDIR\n",
446 gn->name);
447 goto out;
449 if (!(gn->type & OP_META)) {
450 /* We do not generate .meta files for sub-makes */
451 if (Lst_ForEach(gn->commands, is_submake, gn)) {
452 if (DEBUG(META))
453 fprintf(debug_file, "Skipping meta for %s: .MAKE\n",
454 gn->name);
455 goto out;
459 if (metaVerbose) {
460 char *mp;
462 /* Describe the target we are building */
463 mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
464 if (*mp)
465 fprintf(stdout, "%s\n", mp);
466 free(mp);
468 /* Get the basename of the target */
469 if ((cp = strrchr(tname, '/')) == NULL) {
470 cp = tname;
471 } else {
472 cp++;
475 fflush(stdout);
477 if (strcmp(cp, makeDependfile) == 0)
478 goto out;
480 if (!writeMeta)
481 /* Don't create meta data. */
482 goto out;
484 fname = meta_name(gn, pbm->meta_fname, sizeof(pbm->meta_fname),
485 dname, tname);
487 #ifdef DEBUG_META_MODE
488 if (DEBUG(META))
489 fprintf(debug_file, "meta_create: %s\n", fname);
490 #endif
492 if ((mf.fp = fopen(fname, "w")) == NULL)
493 err(1, "Could not open meta file '%s'", fname);
495 fprintf(mf.fp, "# Meta data file %s\n", fname);
497 mf.gn = gn;
499 Lst_ForEach(gn->commands, printCMD, &mf);
501 fprintf(mf.fp, "CWD %s\n", getcwd(buf, sizeof(buf)));
502 fprintf(mf.fp, "TARGET %s\n", tname);
504 if (metaEnv) {
505 for (ptr = environ; *ptr != NULL; ptr++)
506 fprintf(mf.fp, "ENV %s\n", *ptr);
509 fprintf(mf.fp, "-- command output --\n");
510 fflush(mf.fp);
512 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
513 Var_Append(".MAKE.META.CREATED", fname, VAR_GLOBAL);
515 gn->type |= OP_META; /* in case anyone wants to know */
516 if (metaSilent) {
517 gn->type |= OP_SILENT;
519 out:
520 for (i--; i >= 0; i--) {
521 if (p[i])
522 free(p[i]);
525 return (mf.fp);
528 static Boolean
529 boolValue(char *s)
531 switch(*s) {
532 case '0':
533 case 'N':
534 case 'n':
535 case 'F':
536 case 'f':
537 return FALSE;
539 return TRUE;
542 void
543 meta_init(const char *make_mode)
545 static int once = 0;
546 char *cp;
548 useMeta = TRUE;
549 useFilemon = TRUE;
550 writeMeta = TRUE;
552 if (make_mode) {
553 if (strstr(make_mode, "env"))
554 metaEnv = TRUE;
555 if (strstr(make_mode, "verb"))
556 metaVerbose = TRUE;
557 if (strstr(make_mode, "read"))
558 writeMeta = FALSE;
559 if (strstr(make_mode, "nofilemon"))
560 useFilemon = FALSE;
561 if ((cp = strstr(make_mode, "curdirok="))) {
562 metaCurdirOk = boolValue(&cp[9]);
564 if ((cp = strstr(make_mode, "silent="))) {
565 metaSilent = boolValue(&cp[7]);
567 if (strstr(make_mode, "ignore-cmd"))
568 metaIgnoreCMDs = TRUE;
569 /* for backwards compatability */
570 Var_Set(".MAKE.META_CREATED", "${.MAKE.META.CREATED}", VAR_GLOBAL, 0);
571 Var_Set(".MAKE.META_FILES", "${.MAKE.META.FILES}", VAR_GLOBAL, 0);
573 if (metaVerbose && !Var_Exists(MAKE_META_PREFIX, VAR_GLOBAL)) {
575 * The default value for MAKE_META_PREFIX
576 * prints the absolute path of the target.
577 * This works be cause :H will generate '.' if there is no /
578 * and :tA will resolve that to cwd.
580 Var_Set(MAKE_META_PREFIX, "Building ${.TARGET:H:tA}/${.TARGET:T}", VAR_GLOBAL, 0);
582 if (once)
583 return;
584 once = 1;
585 memset(&Mybm, 0, sizeof(Mybm));
587 * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
589 metaBailiwick = Lst_Init(FALSE);
590 cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
591 if (cp) {
592 str2Lst_Append(metaBailiwick, cp, NULL);
597 * In each case below we allow for job==NULL
599 void
600 meta_job_start(Job *job, GNode *gn)
602 BuildMon *pbm;
604 if (job != NULL) {
605 pbm = &job->bm;
606 } else {
607 pbm = &Mybm;
609 pbm->mfp = meta_create(pbm, gn);
610 #ifdef USE_FILEMON_ONCE
611 /* compat mode we open the filemon dev once per command */
612 if (job == NULL)
613 return;
614 #endif
615 #ifdef USE_FILEMON
616 if (pbm->mfp != NULL && useFilemon) {
617 filemon_open(pbm);
618 } else {
619 pbm->mon_fd = pbm->filemon_fd = -1;
621 #endif
625 * The child calls this before doing anything.
626 * It does not disturb our state.
628 void
629 meta_job_child(Job *job)
631 #ifdef USE_FILEMON
632 BuildMon *pbm;
633 pid_t pid;
635 if (job != NULL) {
636 pbm = &job->bm;
637 } else {
638 pbm = &Mybm;
640 pid = getpid();
641 if (pbm->mfp != NULL && useFilemon) {
642 if (ioctl(pbm->filemon_fd, FILEMON_SET_PID, &pid) < 0) {
643 err(1, "Could not set filemon pid!");
646 #endif
649 void
650 meta_job_error(Job *job, GNode *gn, int flags, int status)
652 char cwd[MAXPATHLEN];
653 BuildMon *pbm;
655 if (job != NULL) {
656 pbm = &job->bm;
657 } else {
658 if (!gn)
659 gn = job->node;
660 pbm = &Mybm;
662 if (pbm->mfp != NULL) {
663 fprintf(pbm->mfp, "*** Error code %d%s\n",
664 status,
665 (flags & JOB_IGNERR) ?
666 "(ignored)" : "");
668 if (gn) {
669 Var_Set(".ERROR_TARGET", gn->path ? gn->path : gn->name, VAR_GLOBAL, 0);
671 getcwd(cwd, sizeof(cwd));
672 Var_Set(".ERROR_CWD", cwd, VAR_GLOBAL, 0);
673 if (pbm && pbm->meta_fname[0]) {
674 Var_Set(".ERROR_META_FILE", pbm->meta_fname, VAR_GLOBAL, 0);
676 meta_job_finish(job);
679 void
680 meta_job_output(Job *job, char *cp, const char *nl)
682 BuildMon *pbm;
684 if (job != NULL) {
685 pbm = &job->bm;
686 } else {
687 pbm = &Mybm;
689 if (pbm->mfp != NULL) {
690 if (metaVerbose) {
691 static char *meta_prefix = NULL;
692 static int meta_prefix_len;
694 if (!meta_prefix) {
695 char *cp2;
697 meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
698 if ((cp2 = strchr(meta_prefix, '$')))
699 meta_prefix_len = cp2 - meta_prefix;
700 else
701 meta_prefix_len = strlen(meta_prefix);
703 if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
704 cp = strchr(cp+1, '\n');
705 if (!cp++)
706 return;
709 fprintf(pbm->mfp, "%s%s", cp, nl);
713 void
714 meta_cmd_finish(void *pbmp)
716 #ifdef USE_FILEMON
717 BuildMon *pbm = pbmp;
719 if (!pbm)
720 pbm = &Mybm;
722 if (pbm->filemon_fd >= 0) {
723 close(pbm->filemon_fd);
724 filemon_read(pbm->mfp, pbm->mon_fd);
725 pbm->filemon_fd = pbm->mon_fd = -1;
727 #endif
730 void
731 meta_job_finish(Job *job)
733 BuildMon *pbm;
735 if (job != NULL) {
736 pbm = &job->bm;
737 } else {
738 pbm = &Mybm;
740 if (pbm->mfp != NULL) {
741 meta_cmd_finish(pbm);
742 fclose(pbm->mfp);
743 pbm->mfp = NULL;
744 pbm->meta_fname[0] = '\0';
749 * Fetch a full line from fp - growing bufp if needed
750 * Return length in bufp.
752 static int
753 fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
755 char *buf = *bufp;
756 size_t bufsz = *szp;
757 struct stat fs;
758 int x;
760 if (fgets(&buf[o], bufsz - o, fp) != NULL) {
761 check_newline:
762 x = o + strlen(&buf[o]);
763 if (buf[x - 1] == '\n')
764 return x;
766 * We need to grow the buffer.
767 * The meta file can give us a clue.
769 if (fstat(fileno(fp), &fs) == 0) {
770 size_t newsz;
771 char *p;
773 newsz = ROUNDUP((fs.st_size / 2), BUFSIZ);
774 if (newsz <= bufsz)
775 newsz = ROUNDUP(fs.st_size, BUFSIZ);
776 if (DEBUG(META))
777 fprintf(debug_file, "growing buffer %zu -> %zu\n",
778 bufsz, newsz);
779 p = bmake_realloc(buf, newsz);
780 if (p) {
781 *bufp = buf = p;
782 *szp = bufsz = newsz;
783 /* fetch the rest */
784 if (!fgets(&buf[x], bufsz - x, fp))
785 return x; /* truncated! */
786 goto check_newline;
790 return 0;
793 static int
794 prefix_match(void *p, void *q)
796 const char *prefix = p;
797 const char *path = q;
798 size_t n = strlen(prefix);
800 return (0 == strncmp(path, prefix, n));
803 static int
804 string_match(const void *p, const void *q)
806 const char *p1 = p;
807 const char *p2 = q;
809 return strcmp(p1, p2);
814 * When running with 'meta' functionality, a target can be out-of-date
815 * if any of the references in it's meta data file is more recent.
816 * We have to track the latestdir on a per-process basis.
818 #define LDIR_VNAME_FMT ".meta.%d.ldir"
821 * It is possible that a .meta file is corrupted,
822 * if we detect this we want to reproduce it.
823 * Setting oodate TRUE will have that effect.
825 #define CHECK_VALID_META(p) if (!(p && *p)) { \
826 warnx("%s: %d: malformed", fname, lineno); \
827 oodate = TRUE; \
828 continue; \
831 Boolean
832 meta_oodate(GNode *gn, Boolean oodate)
834 static char *tmpdir = NULL;
835 static char cwd[MAXPATHLEN];
836 char ldir_vname[64];
837 char latestdir[MAXPATHLEN];
838 char fname[MAXPATHLEN];
839 char fname1[MAXPATHLEN];
840 char fname2[MAXPATHLEN];
841 char *p;
842 char *cp;
843 static size_t cwdlen = 0;
844 static size_t tmplen = 0;
845 FILE *fp;
846 Boolean ignoreOODATE = FALSE;
847 Lst missingFiles;
849 if (oodate)
850 return oodate; /* we're done */
852 missingFiles = Lst_Init(FALSE);
855 * We need to check if the target is out-of-date. This includes
856 * checking if the expanded command has changed. This in turn
857 * requires that all variables are set in the same way that they
858 * would be if the target needs to be re-built.
860 Make_DoAllVar(gn);
862 meta_name(gn, fname, sizeof(fname), NULL, NULL);
864 #ifdef DEBUG_META_MODE
865 if (DEBUG(META))
866 fprintf(debug_file, "meta_oodate: %s\n", fname);
867 #endif
869 if ((fp = fopen(fname, "r")) != NULL) {
870 static char *buf = NULL;
871 static size_t bufsz;
872 int lineno = 0;
873 int lastpid = 0;
874 int pid;
875 int f = 0;
876 int x;
877 LstNode ln;
878 struct stat fs;
880 if (!buf) {
881 bufsz = 8 * BUFSIZ;
882 buf = bmake_malloc(bufsz);
885 if (!cwdlen) {
886 if (getcwd(cwd, sizeof(cwd)) == NULL)
887 err(1, "Could not get current working directory");
888 cwdlen = strlen(cwd);
891 if (!tmpdir) {
892 tmpdir = getTmpdir();
893 tmplen = strlen(tmpdir);
896 /* we want to track all the .meta we read */
897 Var_Append(".MAKE.META.FILES", fname, VAR_GLOBAL);
899 ln = Lst_First(gn->commands);
900 while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
901 lineno++;
902 if (buf[x - 1] == '\n')
903 buf[x - 1] = '\0';
904 else {
905 warnx("%s: %d: line truncated at %u", fname, lineno, x);
906 oodate = TRUE;
907 break;
909 /* Find the start of the build monitor section. */
910 if (!f) {
911 if (strncmp(buf, "-- filemon", 10) == 0) {
912 f = 1;
913 continue;
915 if (strncmp(buf, "# buildmon", 10) == 0) {
916 f = 1;
917 continue;
921 /* Delimit the record type. */
922 p = buf;
923 #ifdef DEBUG_META_MODE
924 if (DEBUG(META))
925 fprintf(debug_file, "%s: %d: %s\n", fname, lineno, buf);
926 #endif
927 strsep(&p, " ");
928 if (f) {
930 * We are in the 'filemon' output section.
931 * Each record from filemon follows the general form:
933 * <key> <pid> <data>
935 * Where:
936 * <key> is a single letter, denoting the syscall.
937 * <pid> is the process that made the syscall.
938 * <data> is the arguments (of interest).
940 switch(buf[0]) {
941 case '#': /* comment */
942 case 'V': /* version */
943 break;
944 default:
946 * We need to track pathnames per-process.
948 * Each process run by make, starts off in the 'CWD'
949 * recorded in the .meta file, if it chdirs ('C')
950 * elsewhere we need to track that - but only for
951 * that process. If it forks ('F'), we initialize
952 * the child to have the same cwd as its parent.
954 * We also need to track the 'latestdir' of
955 * interest. This is usually the same as cwd, but
956 * not if a process is reading directories.
958 * Each time we spot a different process ('pid')
959 * we save the current value of 'latestdir' in a
960 * variable qualified by 'lastpid', and
961 * re-initialize 'latestdir' to any pre-saved
962 * value for the current 'pid' and 'CWD' if none.
964 CHECK_VALID_META(p);
965 pid = atoi(p);
966 if (pid > 0 && pid != lastpid) {
967 char *ldir;
968 char *tp;
970 if (lastpid > 0) {
971 /* We need to remember this. */
972 Var_Set(ldir_vname, latestdir, VAR_GLOBAL, 0);
974 snprintf(ldir_vname, sizeof(ldir_vname), LDIR_VNAME_FMT, pid);
975 lastpid = pid;
976 ldir = Var_Value(ldir_vname, VAR_GLOBAL, &tp);
977 if (ldir) {
978 strlcpy(latestdir, ldir, sizeof(latestdir));
979 if (tp)
980 free(tp);
981 } else
982 strlcpy(latestdir, cwd, sizeof(latestdir));
984 /* Skip past the pid. */
985 if (strsep(&p, " ") == NULL)
986 continue;
987 #ifdef DEBUG_META_MODE
988 if (DEBUG(META))
989 fprintf(debug_file, "%s: %d: cwd=%s ldir=%s\n", fname, lineno, cwd, latestdir);
990 #endif
991 break;
994 CHECK_VALID_META(p);
996 /* Process according to record type. */
997 switch (buf[0]) {
998 case 'X': /* eXit */
999 Var_Delete(ldir_vname, VAR_GLOBAL);
1000 lastpid = 0; /* no need to save ldir_vname */
1001 break;
1003 case 'F': /* [v]Fork */
1005 char cldir[64];
1006 int child;
1008 child = atoi(p);
1009 if (child > 0) {
1010 snprintf(cldir, sizeof(cldir), LDIR_VNAME_FMT, child);
1011 Var_Set(cldir, latestdir, VAR_GLOBAL, 0);
1014 break;
1016 case 'C': /* Chdir */
1017 /* Update the latest directory. */
1018 strlcpy(latestdir, p, sizeof(latestdir));
1019 break;
1021 case 'M': /* renaMe */
1022 if (Lst_IsEmpty(missingFiles))
1023 break;
1024 /* 'L' and 'M' put single quotes around the args */
1025 if (*p == '\'') {
1026 char *ep;
1028 p++;
1029 if ((ep = strchr(p, '\'')))
1030 *ep = '\0';
1032 /* FALLTHROUGH */
1033 case 'D': /* unlink */
1034 if (*p == '/' && !Lst_IsEmpty(missingFiles)) {
1035 /* remove p from the missingFiles list if present */
1036 if ((ln = Lst_Find(missingFiles, p, string_match)) != NULL) {
1037 char *tp = Lst_Datum(ln);
1038 Lst_Remove(missingFiles, ln);
1039 free(tp);
1042 break;
1043 case 'L': /* Link */
1044 /* we want the target */
1045 if (strsep(&p, " ") == NULL)
1046 continue;
1047 CHECK_VALID_META(p);
1048 /* 'L' and 'M' put single quotes around the args */
1049 if (*p == '\'') {
1050 char *ep;
1052 p++;
1053 if ((ep = strchr(p, '\'')))
1054 *ep = '\0';
1056 /* FALLTHROUGH */
1057 case 'W': /* Write */
1059 * If a file we generated within our bailiwick
1060 * but outside of .OBJDIR is missing,
1061 * we need to do it again.
1063 /* ignore non-absolute paths */
1064 if (*p != '/')
1065 break;
1067 if (Lst_IsEmpty(metaBailiwick))
1068 break;
1070 /* ignore cwd - normal dependencies handle those */
1071 if (strncmp(p, cwd, cwdlen) == 0)
1072 break;
1074 if (!Lst_ForEach(metaBailiwick, prefix_match, p))
1075 break;
1077 /* tmpdir might be within */
1078 if (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0)
1079 break;
1081 /* ignore anything containing the string "tmp" */
1082 if ((strstr("tmp", p)))
1083 break;
1085 if (stat(p, &fs) < 0) {
1086 Lst_AtEnd(missingFiles, bmake_strdup(p));
1088 break;
1089 case 'R': /* Read */
1090 case 'E': /* Exec */
1092 * Check for runtime files that can't
1093 * be part of the dependencies because
1094 * they are _expected_ to change.
1096 if (strncmp(p, "/tmp/", 5) == 0 ||
1097 (tmplen > 0 && strncmp(p, tmpdir, tmplen) == 0))
1098 break;
1100 if (strncmp(p, "/var/", 5) == 0)
1101 break;
1103 /* Ignore device files. */
1104 if (strncmp(p, "/dev/", 5) == 0)
1105 break;
1107 /* Ignore /etc/ files. */
1108 if (strncmp(p, "/etc/", 5) == 0)
1109 break;
1111 if ((cp = strrchr(p, '/'))) {
1112 cp++;
1114 * We don't normally expect to see this,
1115 * but we do expect it to change.
1117 if (strcmp(cp, makeDependfile) == 0)
1118 break;
1122 * The rest of the record is the file name.
1123 * Check if it's not an absolute path.
1126 char *sdirs[4];
1127 char **sdp;
1128 int sdx = 0;
1129 int found = 0;
1131 if (*p == '/') {
1132 sdirs[sdx++] = p; /* done */
1133 } else {
1134 if (strcmp(".", p) == 0)
1135 continue; /* no point */
1137 /* Check vs latestdir */
1138 snprintf(fname1, sizeof(fname1), "%s/%s", latestdir, p);
1139 sdirs[sdx++] = fname1;
1141 if (strcmp(latestdir, cwd) != 0) {
1142 /* Check vs cwd */
1143 snprintf(fname2, sizeof(fname2), "%s/%s", cwd, p);
1144 sdirs[sdx++] = fname2;
1147 sdirs[sdx++] = NULL;
1149 for (sdp = sdirs; *sdp && !found; sdp++) {
1150 #ifdef DEBUG_META_MODE
1151 if (DEBUG(META))
1152 fprintf(debug_file, "%s: %d: looking for: %s\n", fname, lineno, *sdp);
1153 #endif
1154 if (stat(*sdp, &fs) == 0) {
1155 found = 1;
1156 p = *sdp;
1159 if (found) {
1160 #ifdef DEBUG_META_MODE
1161 if (DEBUG(META))
1162 fprintf(debug_file, "%s: %d: found: %s\n", fname, lineno, p);
1163 #endif
1164 if (!S_ISDIR(fs.st_mode) &&
1165 fs.st_mtime > gn->mtime) {
1166 if (DEBUG(META))
1167 fprintf(debug_file, "%s: %d: file '%s' is newer than the target...\n", fname, lineno, p);
1168 oodate = TRUE;
1169 } else if (S_ISDIR(fs.st_mode)) {
1170 /* Update the latest directory. */
1171 realpath(p, latestdir);
1173 } else if (errno == ENOENT && *p == '/' &&
1174 strncmp(p, cwd, cwdlen) != 0) {
1176 * A referenced file outside of CWD is missing.
1177 * We cannot catch every eventuality here...
1179 if (DEBUG(META))
1180 fprintf(debug_file, "%s: %d: file '%s' may have moved?...\n", fname, lineno, p);
1181 oodate = TRUE;
1184 break;
1185 default:
1186 break;
1188 } else if (strcmp(buf, "CMD") == 0) {
1190 * Compare the current command with the one in the
1191 * meta data file.
1193 if (ln == NULL) {
1194 if (DEBUG(META))
1195 fprintf(debug_file, "%s: %d: there were more build commands in the meta data file than there are now...\n", fname, lineno);
1196 oodate = TRUE;
1197 } else {
1198 char *cmd = (char *)Lst_Datum(ln);
1200 if (!ignoreOODATE) {
1201 if (strstr(cmd, "$?"))
1202 ignoreOODATE = TRUE;
1203 else if ((cp = strstr(cmd, ".OODATE"))) {
1204 /* check for $[{(].OODATE[)}] */
1205 if (cp > cmd + 2 && cp[-2] == '$')
1206 ignoreOODATE = TRUE;
1208 if (ignoreOODATE && DEBUG(META))
1209 fprintf(debug_file, "%s: %d: cannot compare commands using .OODATE\n", fname, lineno);
1211 cmd = Var_Subst(NULL, cmd, gn, TRUE);
1213 if ((cp = strchr(cmd, '\n'))) {
1214 int n;
1217 * This command contains newlines, we need to
1218 * fetch more from the .meta file before we
1219 * attempt a comparison.
1221 /* first put the newline back at buf[x - 1] */
1222 buf[x - 1] = '\n';
1223 do {
1224 /* now fetch the next line */
1225 if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
1226 break;
1227 x = n;
1228 lineno++;
1229 if (buf[x - 1] != '\n') {
1230 warnx("%s: %d: line truncated at %u", fname, lineno, x);
1231 break;
1233 cp = strchr(++cp, '\n');
1234 } while (cp);
1235 if (buf[x - 1] == '\n')
1236 buf[x - 1] = '\0';
1238 if (!ignoreOODATE &&
1239 !(gn->type & OP_NOMETA_CMP) &&
1240 strcmp(p, cmd) != 0) {
1241 if (DEBUG(META))
1242 fprintf(debug_file, "%s: %d: a build command has changed\n%s\nvs\n%s\n", fname, lineno, p, cmd);
1243 if (!metaIgnoreCMDs)
1244 oodate = TRUE;
1246 free(cmd);
1247 ln = Lst_Succ(ln);
1249 } else if (strcmp(buf, "CWD") == 0) {
1251 * Check if there are extra commands now
1252 * that weren't in the meta data file.
1254 if (!oodate && ln != NULL) {
1255 if (DEBUG(META))
1256 fprintf(debug_file, "%s: %d: there are extra build commands now that weren't in the meta data file\n", fname, lineno);
1257 oodate = TRUE;
1259 if (strcmp(p, cwd) != 0) {
1260 if (DEBUG(META))
1261 fprintf(debug_file, "%s: %d: the current working directory has changed from '%s' to '%s'\n", fname, lineno, p, curdir);
1262 oodate = TRUE;
1267 fclose(fp);
1268 if (!Lst_IsEmpty(missingFiles)) {
1269 if (DEBUG(META))
1270 fprintf(debug_file, "%s: missing files: %s...\n",
1271 fname, (char *)Lst_Datum(Lst_First(missingFiles)));
1272 oodate = TRUE;
1273 Lst_Destroy(missingFiles, (FreeProc *)free);
1275 } else {
1276 if ((gn->type & OP_META)) {
1277 if (DEBUG(META))
1278 fprintf(debug_file, "%s: required but missing\n", fname);
1279 oodate = TRUE;
1282 if (oodate && ignoreOODATE) {
1284 * Target uses .OODATE, so we need to re-compute it.
1285 * We need to clean up what Make_DoAllVar() did.
1287 Var_Delete(ALLSRC, gn);
1288 Var_Delete(OODATE, gn);
1289 gn->flags &= ~DONE_ALLSRC;
1291 return oodate;
1294 /* support for compat mode */
1296 static int childPipe[2];
1298 void
1299 meta_compat_start(void)
1301 #ifdef USE_FILEMON_ONCE
1303 * We need to re-open filemon for each cmd.
1305 BuildMon *pbm = &Mybm;
1307 if (pbm->mfp != NULL && useFilemon) {
1308 filemon_open(pbm);
1309 } else {
1310 pbm->mon_fd = pbm->filemon_fd = -1;
1312 #endif
1313 if (pipe(childPipe) < 0)
1314 Punt("Cannot create pipe: %s", strerror(errno));
1315 /* Set close-on-exec flag for both */
1316 (void)fcntl(childPipe[0], F_SETFD, 1);
1317 (void)fcntl(childPipe[1], F_SETFD, 1);
1320 void
1321 meta_compat_child(void)
1323 meta_job_child(NULL);
1324 if (dup2(childPipe[1], 1) < 0 ||
1325 dup2(1, 2) < 0) {
1326 execError("dup2", "pipe");
1327 _exit(1);
1331 void
1332 meta_compat_parent(void)
1334 FILE *fp;
1335 char buf[BUFSIZ];
1337 close(childPipe[1]); /* child side */
1338 fp = fdopen(childPipe[0], "r");
1339 while (fgets(buf, sizeof(buf), fp)) {
1340 meta_job_output(NULL, buf, "");
1341 printf("%s", buf);
1343 fclose(fp);
1346 #endif /* USE_META */