-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.c
1106 lines (968 loc) · 25.9 KB
/
meta.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: meta.c,v 1.210 2024/06/02 15:31:26 rillig Exp $ */
/*
* Implement 'meta' mode.
* Adapted from John Birrell's patches to FreeBSD make.
* --sjg
*/
/*
* Copyright (c) 2009-2016, Juniper Networks, Inc.
* Portions Copyright (c) 2009, John Birrell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(USE_META)
#include <direct.h>
#include <sys/stat.h>
#include "make.h"
#include "dir.h"
#include "job.h"
static BuildMon Mybm; /* for compat */
static StringList metaBailiwick = LST_INIT; /* our scope of control */
static char *metaBailiwickStr; /* string storage for the list */
static StringList metaIgnorePaths = LST_INIT; /* paths we deliberately ignore */
static char *metaIgnorePathsStr; /* string storage for the list */
#ifndef MAKE_META_IGNORE_PATHS
#define MAKE_META_IGNORE_PATHS ".MAKE.META.IGNORE_PATHS"
#endif
#ifndef MAKE_META_IGNORE_PATTERNS
#define MAKE_META_IGNORE_PATTERNS ".MAKE.META.IGNORE_PATTERNS"
#endif
#ifndef MAKE_META_IGNORE_FILTER
#define MAKE_META_IGNORE_FILTER ".MAKE.META.IGNORE_FILTER"
#endif
#ifndef MAKE_META_CMP_FILTER
#define MAKE_META_CMP_FILTER ".MAKE.META.CMP_FILTER"
#endif
char *dirname(char *);
bool useMeta = false;
static bool writeMeta = false;
static bool metaMissing = false; /* oodate if missing */
static bool metaEnv = false; /* don't save env unless asked */
static bool metaVerbose = false;
static bool metaIgnoreCMDs = false; /* ignore CMDs in .meta files */
static bool metaIgnorePatterns = false; /* do we need to do pattern matches */
static bool metaIgnoreFilter = false; /* do we have more complex filtering? */
static bool metaCmpFilter = false; /* do we have CMP_FILTER ? */
static bool metaCurdirOk = false; /* write .meta in .CURDIR Ok? */
static bool metaSilent = false; /* if we have a .meta be SILENT */
#define MAKE_META_PREFIX ".MAKE.META.PREFIX"
#define N2U(n, u) (((n) + ((u) - 1)) / (u))
#define ROUNDUP(n, u) (N2U((n), (u)) * (u))
#define strsep(s, d) stresep((s), (d), '\0')
char *stresep(char **, const char *, int);
/*
* when realpath() fails,
* we use this, to clean up ./ and ../
*/
static void
eat_dots(char *buf)
{
char *p;
/* Make life easier by replacing all backslashes. */
replaceSlash(buf);
while ((p = strstr(buf, "/./")) != NULL)
memmove(p, p + 2, strlen(p + 2) + 1);
while ((p = strstr(buf, "/../")) != NULL) {
char *p2 = p + 3;
if (p > buf) {
do {
p--;
} while (p > buf && *p != '/');
}
if (*p == '/')
memmove(p, p2, strlen(p2) + 1);
else
return; /* can't happen? */
}
}
static char *
meta_name(char *mname, size_t mnamelen,
const char *dname,
const char *tname,
const char *cwd)
{
char buf[MAXPATHLEN];
char *rp, *cp;
const char *tname_base;
char *tp;
char *dtp;
size_t ldname;
/*
* Weed out relative paths from the target file name.
* We have to be careful though since if target is a
* symlink, the result will be unstable.
* So we use realpath() just to get the dirname, and leave the
* basename as given to us.
*/
if ((tname_base = lastSlash(tname)) != NULL) {
if (cached_realpath(tname, buf) != NULL) {
if ((rp = lastSlash(buf)) != NULL) {
rp++;
tname_base++;
if (strcmp(tname_base, rp) != 0)
strlcpy(rp, tname_base, sizeof buf - (size_t)(rp - buf));
}
tname = buf;
} else {
/*
* We likely have a directory which is about to be made.
* We pretend realpath() succeeded, to have a chance
* of generating the same meta file name that we will
* next time through.
*/
if (isAbs(tname)) {
strlcpy(buf, tname, sizeof buf);
} else {
snprintf(buf, sizeof buf, "%s\\%s", cwd, tname);
}
eat_dots(buf);
tname = buf;
}
}
/* on some systems dirname may modify its arg */
tp = bmake_strdup(tname);
dtp = dirname(tp);
if (strcmp(dname, dtp) == 0) {
if (snprintf(mname, mnamelen, "%s.meta", tname) >= (int)mnamelen)
mname[mnamelen - 1] = '\0';
} else {
int x;
ldname = strlen(dname);
if (strncmp(dname, dtp, ldname) == 0 && (dtp[ldname] == '/' || dtp[ldname] == '\\'))
x = snprintf(mname, mnamelen, "%s\\%s.meta", dname, &tname[ldname + 1]);
else
x = snprintf(mname, mnamelen, "%s\\%s.meta", dname, tname);
if (x >= (int)mnamelen)
mname[mnamelen - 1] = '\0';
/*
* Replace path separators in the file name after the
* current object directory path.
*/
cp = mname + strlen(dname) + 1;
while (*cp != '\0') {
if (*cp == '/' ||
*cp == '\\')
*cp = '_';
cp++;
}
}
free(tp);
return mname;
}
/*
* Return true if running ${.MAKE}
* Bypassed if target is flagged .MAKE
*/
static bool
is_submake(const char *cmd, GNode *gn)
{
static const char *p_make = NULL;
static size_t p_len;
char *mp = NULL;
const char *cp2;
bool rc = false;
if (p_make == NULL) {
p_make = Var_Value(gn, ".MAKE").str;
p_len = strlen(p_make);
}
if (strchr(cmd, '$') != NULL) {
mp = Var_Subst(cmd, gn, VARE_EVAL);
/* TODO: handle errors */
cmd = mp;
}
cp2 = strstr(cmd, p_make);
if (cp2 != NULL) {
switch (cp2[p_len]) {
case '\0':
case ' ':
case '\t':
case '\n':
rc = true;
break;
}
if (cp2 > cmd && rc) {
switch (cp2[-1]) {
case ' ':
case '\t':
case '\n':
break;
default:
rc = false; /* no match */
break;
}
}
}
free(mp);
return rc;
}
static bool
any_is_submake(GNode *gn)
{
StringListNode *ln;
for (ln = gn->commands.first; ln != NULL; ln = ln->next)
if (is_submake(ln->datum, gn))
return true;
return false;
}
static void
printCMD(const char *ucmd, FILE *fp, GNode *gn)
{
FStr xcmd = FStr_InitRefer(ucmd);
Var_Expand(&xcmd, gn, VARE_EVAL);
fprintf(fp, "CMD %s\n", xcmd.str);
FStr_Done(&xcmd);
}
static void
printCMDs(GNode *gn, FILE *fp)
{
StringListNode *ln;
for (ln = gn->commands.first; ln != NULL; ln = ln->next)
printCMD(ln->datum, fp, gn);
}
/*
* Certain node types never get a .meta file
*/
#define SKIP_META_TYPE(flag, str) do { \
if ((gn->type & (flag))) { \
if (verbose) \
debug_printf("Skipping meta for %s: .%s\n", gn->name, str); \
return false; \
} \
} while (false)
/*
* Do we need/want a .meta file ?
*/
static bool
meta_needed(GNode *gn, const char *dname,
char *objdir_realpath, bool verbose)
{
struct cached_stat cst;
if (verbose)
verbose = DEBUG(META);
/* This may be a phony node which we don't want meta data for... */
/* Skip .meta for .BEGIN, .END, .ERROR etc as well. */
/* Or it may be explicitly flagged as .NOMETA */
SKIP_META_TYPE(OP_NOMETA, "NOMETA");
/* Unless it is explicitly flagged as .META */
if (!(gn->type & OP_META)) {
SKIP_META_TYPE(OP_PHONY, "PHONY");
SKIP_META_TYPE(OP_SPECIAL, "SPECIAL");
SKIP_META_TYPE(OP_MAKE, "MAKE");
}
/* Check if there are no commands to execute. */
if (Lst_IsEmpty(&gn->commands)) {
if (verbose)
debug_printf("Skipping meta for %s: no commands\n", gn->name);
return false;
}
if ((gn->type & (OP_META | OP_SUBMAKE)) == OP_SUBMAKE) {
/* OP_SUBMAKE is a bit too aggressive */
if (any_is_submake(gn)) {
DEBUG1(META, "Skipping meta for %s: .SUBMAKE\n", gn->name);
return false;
}
}
/* The object directory may not exist. Check it.. */
if (cached_stat(dname, &cst) != 0) {
if (verbose)
debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
return false;
}
/* make sure these are canonical */
if (cached_realpath(dname, objdir_realpath) != NULL)
dname = objdir_realpath;
/* If we aren't in the object directory, don't create a meta file. */
if (!metaCurdirOk && strcmp(curdir, dname) == 0) {
if (verbose)
debug_printf("Skipping meta for %s: .OBJDIR == .CURDIR\n",
gn->name);
return false;
}
return true;
}
static FILE *
meta_create(BuildMon *pbm, GNode *gn)
{
FILE *fp;
char buf[MAXPATHLEN];
char objdir_realpath[MAXPATHLEN];
char **ptr;
FStr dname;
const char *tname;
char *fname;
const char *cp;
fp = NULL;
dname = Var_Value(gn, ".OBJDIR");
tname = GNode_VarTarget(gn);
/* if this succeeds objdir_realpath is realpath of dname */
if (!meta_needed(gn, dname.str, objdir_realpath, true))
goto out;
dname.str = objdir_realpath;
if (metaVerbose) {
/* Describe the target we are building */
char *mp = Var_Subst("${" MAKE_META_PREFIX "}", gn, VARE_EVAL);
/* TODO: handle errors */
if (mp[0] != '\0')
fprintf(stdout, "%s\n", mp);
free(mp);
}
/* Get the basename of the target */
cp = str_basename(tname);
fflush(stdout);
if (!writeMeta)
/* Don't create meta data. */
goto out;
fname = meta_name(pbm->meta_fname, sizeof pbm->meta_fname,
dname.str, tname, objdir_realpath);
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_create: %s\n", fname);
#endif
if ((fp = fopen(fname, "w")) == NULL)
Punt("could not open meta file '%s': %s", fname, strerror(errno));
fprintf(fp, "# Meta data file %s\n", fname);
printCMDs(gn, fp);
fprintf(fp, "CWD %s\n", _getcwd(buf, sizeof buf));
fprintf(fp, "TARGET %s\n", tname);
cp = GNode_VarOodate(gn);
if (cp != NULL && *cp != '\0') {
fprintf(fp, "OODATE %s\n", cp);
}
if (metaEnv) {
for (ptr = environ; *ptr != NULL; ptr++)
fprintf(fp, "ENV %s\n", *ptr);
}
fprintf(fp, "-- command output --\n");
if (fflush(fp) != 0)
Punt("cannot write expanded command to meta file: %s",
strerror(errno));
Global_Append(".MAKE.META.FILES", fname);
Global_Append(".MAKE.META.CREATED", fname);
gn->type |= OP_META; /* in case anyone wants to know */
if (metaSilent) {
gn->type |= OP_SILENT;
}
out:
FStr_Done(&dname);
return fp;
}
static bool
boolValue(const char *s)
{
switch (*s) {
case '0':
case 'N':
case 'n':
case 'F':
case 'f':
return false;
}
return true;
}
#define get_mode_bf(bf, token) \
if ((cp = strstr(make_mode, token)) != NULL) \
bf = boolValue(cp + sizeof (token) - 1)
/*
* Initialization we need after reading makefiles.
*/
void
meta_mode_init(const char *make_mode)
{
static bool once = false;
const char *cp;
useMeta = true;
writeMeta = true;
if (make_mode != NULL) {
if (strstr(make_mode, "env") != NULL)
metaEnv = true;
if (strstr(make_mode, "verb") != NULL)
metaVerbose = true;
if (strstr(make_mode, "read") != NULL)
writeMeta = false;
if (strstr(make_mode, "ignore-cmd") != NULL)
metaIgnoreCMDs = true;
get_mode_bf(metaCurdirOk, "curdirok=");
get_mode_bf(metaMissing, "missing-meta=");
get_mode_bf(metaSilent, "silent=");
}
if (metaVerbose && !Var_Exists(SCOPE_GLOBAL, MAKE_META_PREFIX)) {
/*
* The default value for MAKE_META_PREFIX
* prints the absolute path of the target.
* This works be cause :H will generate '.' if there is no /
* and :tA will resolve that to cwd.
*/
Global_Set(MAKE_META_PREFIX,
"Building ${.TARGET:H:tA}\\${.TARGET:T}");
}
if (once)
return;
once = true;
memset(&Mybm, 0, sizeof Mybm);
/*
* We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
*/
metaBailiwickStr = Var_Subst("${.MAKE.META.BAILIWICK:O:u:tA}",
SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
AppendWords(&metaBailiwick, metaBailiwickStr);
/*
* We ignore any paths that start with ${.MAKE.META.IGNORE_PATHS}
*/
metaIgnorePathsStr = Var_Subst("${" MAKE_META_IGNORE_PATHS ":O:u:tA}",
SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
AppendWords(&metaIgnorePaths, metaIgnorePathsStr);
/*
* We ignore any paths that match ${.MAKE.META.IGNORE_PATTERNS}
*/
metaIgnorePatterns = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_PATTERNS);
metaIgnoreFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_IGNORE_FILTER);
metaCmpFilter = Var_Exists(SCOPE_GLOBAL, MAKE_META_CMP_FILTER);
}
MAKE_INLINE BuildMon *
BM(Job *job)
{
return ((job != NULL) ? &job->bm : &Mybm);
}
/*
* In each case below we allow for job==NULL
*/
void
meta_job_start(Job *job, GNode *gn)
{
BuildMon *pbm;
pbm = BM(job);
pbm->mfp = meta_create(pbm, gn);
}
void
meta_job_error(Job *job, GNode *gn, bool ignerr, int status)
{
char cwd[MAXPATHLEN];
BuildMon *pbm;
pbm = BM(job);
if (job != NULL && gn == NULL)
gn = job->node;
if (pbm->mfp != NULL) {
fprintf(pbm->mfp, "\n*** Error code %d%s\n",
status, ignerr ? "(ignored)" : "");
}
if (gn != NULL)
Global_Set(".ERROR_TARGET", GNode_Path(gn));
if (getcwd(cwd, sizeof cwd) == NULL)
Punt("cannot get cwd: %s", strerror(errno));
Global_Set(".ERROR_CWD", cwd);
if (pbm->meta_fname[0] != '\0') {
Global_Set(".ERROR_META_FILE", pbm->meta_fname);
}
meta_job_finish(job);
}
void
meta_job_output(Job *job, char *cp, const char *nl)
{
BuildMon *pbm;
pbm = BM(job);
if (pbm->mfp != NULL) {
if (metaVerbose) {
static char *meta_prefix = NULL;
static size_t meta_prefix_len;
if (meta_prefix == NULL) {
char *cp2;
meta_prefix = Var_Subst("${" MAKE_META_PREFIX "}",
SCOPE_GLOBAL, VARE_EVAL);
/* TODO: handle errors */
if ((cp2 = strchr(meta_prefix, '$')) != NULL)
meta_prefix_len = (size_t)(cp2 - meta_prefix);
else
meta_prefix_len = strlen(meta_prefix);
}
if (strncmp(cp, meta_prefix, meta_prefix_len) == 0) {
cp = strchr(cp + 1, '\n');
if (cp == NULL)
return;
cp++;
}
}
fprintf(pbm->mfp, "%s%s", cp, nl);
}
}
int
meta_cmd_finish(void *pbmp)
{
int error = 0;
BuildMon *pbm = pbmp;
if (pbm == NULL)
pbm = &Mybm;
fprintf(pbm->mfp, "\n"); /* ensure end with newline */
return error;
}
int
meta_job_finish(Job *job)
{
BuildMon *pbm;
int error = 0;
int x;
pbm = BM(job);
if (pbm->mfp != NULL) {
error = meta_cmd_finish(pbm);
x = fclose(pbm->mfp);
if (error == 0 && x != 0)
error = errno;
pbm->mfp = NULL;
pbm->meta_fname[0] = '\0';
}
return error;
}
void
meta_finish(void)
{
Lst_Done(&metaBailiwick);
free(metaBailiwickStr);
Lst_Done(&metaIgnorePaths);
free(metaIgnorePathsStr);
}
/*
* Fetch a full line from fp - growing bufp if needed
* Return length in bufp.
*/
static int
fgetLine(char **bufp, size_t *szp, int o, FILE *fp)
{
char *buf = *bufp;
size_t bufsz = *szp;
struct stat fs;
int x;
if (fgets(&buf[o], (int)bufsz - o, fp) != NULL) {
check_newline:
x = o + (int)strlen(&buf[o]);
if (buf[x - 1] == '\n')
return x;
/*
* We need to grow the buffer.
* The meta file can give us a clue.
*/
if (fstat(fileno(fp), &fs) == 0) {
size_t newsz;
char *p;
newsz = ROUNDUP(((size_t)fs.st_size / 2), BUFSIZ);
if (newsz <= bufsz)
newsz = ROUNDUP((size_t)fs.st_size, BUFSIZ);
if (newsz <= bufsz)
return x; /* truncated */
DEBUG2(META, "growing buffer %u -> %u\n",
(unsigned)bufsz, (unsigned)newsz);
p = bmake_realloc(buf, newsz);
*bufp = buf = p;
*szp = bufsz = newsz;
/* fetch the rest */
if (fgets(&buf[x], (int)bufsz - x, fp) == NULL)
return x; /* truncated! */
goto check_newline;
}
}
return 0;
}
static bool
prefix_match(const char *prefix, const char *path)
{
size_t n = strlen(prefix);
return strncmp(path, prefix, n) == 0;
}
static bool
has_any_prefix(const char *path, StringList *prefixes)
{
StringListNode *ln;
for (ln = prefixes->first; ln != NULL; ln = ln->next)
if (prefix_match(ln->datum, path))
return true;
return false;
}
/* See if the path equals prefix or starts with "prefix/". */
static bool
path_starts_with(const char *path, const char *prefix)
{
size_t n = strlen(prefix);
if (strncmp(path, prefix, n) != 0)
return false;
return path[n] == '\0' || path[n] == '/' ||
path[n] == '\\';
}
static bool
meta_ignore(GNode *gn, const char *p)
{
char fname[MAXPATHLEN];
if (p == NULL)
return true;
if (isAbs(p)) {
/* first try the raw path "as is" */
if (has_any_prefix(p, &metaIgnorePaths)) {
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
#endif
return true;
}
cached_realpath(p, fname); /* clean it up */
if (has_any_prefix(fname, &metaIgnorePaths)) {
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_oodate: ignoring path: %s\n", p);
#endif
return true;
}
}
if (metaIgnorePatterns) {
const char *expr;
char *pm;
/*
* XXX: This variable is set on a target GNode but is not one of
* the usual local variables. It should be deleted afterwards.
* Ideally it would not be created in the first place, just like
* in a .for loop.
*/
Var_Set(gn, ".p.", p);
expr = "${" MAKE_META_IGNORE_PATTERNS ":@m@${.p.:M$m}@}";
pm = Var_Subst(expr, gn, VARE_EVAL);
/* TODO: handle errors */
if (pm[0] != '\0') {
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_oodate: ignoring pattern: %s\n", p);
#endif
free(pm);
return true;
}
free(pm);
}
if (metaIgnoreFilter) {
char *fm;
/* skip if filter result is empty */
snprintf(fname, sizeof fname,
"${%s:L:${%s:ts:}}",
p, MAKE_META_IGNORE_FILTER);
fm = Var_Subst(fname, gn, VARE_EVAL);
/* TODO: handle errors */
if (*fm == '\0') {
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_oodate: ignoring filtered: %s\n", p);
#endif
free(fm);
return true;
}
free(fm);
}
return false;
}
/*
* When running with 'meta' functionality, a target can be out-of-date
* if any of the references in its meta data file is more recent.
* We have to track the latestdir on a per-process basis.
*/
#define LCWD_VNAME_FMT ".meta.%d.lcwd"
#define LDIR_VNAME_FMT ".meta.%d.ldir"
/*
* It is possible that a .meta file is corrupted,
* if we detect this we want to reproduce it.
* Setting oodate true will have that effect.
*/
#define CHECK_VALID_META(p) if (!(p != NULL && *p != '\0')) { \
warnx("%s: %u: malformed", fname, lineno); \
oodate = true; \
continue; \
}
#define DEQUOTE(p) if (*p == '\'') { \
char *ep; \
p++; \
if ((ep = strchr(p, '\'')) != NULL) \
*ep = '\0'; \
}
static void
append_if_new(StringList *list, const char *str)
{
StringListNode *ln;
for (ln = list->first; ln != NULL; ln = ln->next)
if (strcmp(ln->datum, str) == 0)
return;
Lst_Append(list, bmake_strdup(str));
}
/* A "reserved" variable to store the command to be filtered */
#define META_CMD_FILTER_VAR ".MAKE.cmd_filtered"
static char *
meta_filter_cmd(GNode *gn, char *s)
{
Var_Set(gn, META_CMD_FILTER_VAR, s);
s = Var_Subst(
"${" META_CMD_FILTER_VAR ":${" MAKE_META_CMP_FILTER ":ts:}}",
gn, VARE_EVAL);
return s;
}
static int
meta_cmd_cmp(GNode *gn, char *a, char *b, bool filter)
{
int rc;
rc = strcmp(a, b);
if (rc == 0 || !filter)
return rc;
a = meta_filter_cmd(gn, a);
b = meta_filter_cmd(gn, b);
rc = strcmp(a, b);
free(a);
free(b);
Var_Delete(gn, META_CMD_FILTER_VAR);
return rc;
}
bool
meta_oodate(GNode *gn, bool oodate)
{
static char *tmpdir = NULL;
static char cwd[MAXPATHLEN];
char lcwd[MAXPATHLEN];
char latestdir[MAXPATHLEN];
char fname[MAXPATHLEN];
char fname3[MAXPATHLEN];
FStr dname;
const char *tname;
char *p;
char *link_src;
char *move_target;
static size_t cwdlen = 0;
static size_t tmplen = 0;
FILE *fp;
bool needOODATE = false;
StringList missingFiles;
bool cmp_filter;
if (oodate)
return oodate; /* we're done */
dname = Var_Value(gn, ".OBJDIR");
tname = GNode_VarTarget(gn);
/* if this succeeds fname3 is realpath of dname */
if (!meta_needed(gn, dname.str, fname3, false))
goto oodate_out;
dname.str = fname3;
Lst_Init(&missingFiles);
/*
* We need to check if the target is out-of-date. This includes
* checking if the expanded command has changed. This in turn
* requires that all variables are set in the same way that they
* would be if the target needs to be re-built.
*/
GNode_SetLocalVars(gn);
meta_name(fname, sizeof fname, dname.str, tname, dname.str);
#ifdef DEBUG_META_MODE
DEBUG1(META, "meta_oodate: %s\n", fname);
#endif
if ((fp = fopen(fname, "r")) != NULL) {
static char *buf = NULL;
static size_t bufsz;
unsigned lineno = 0;
int x;
StringListNode *cmdNode;
if (buf == NULL) {
bufsz = 8 * BUFSIZ;
buf = bmake_malloc(bufsz);
}
if (cwdlen == 0) {
if (getcwd(cwd, sizeof cwd) == NULL)
err(1, "Could not get current working directory");
cwdlen = strlen(cwd);
}
strlcpy(lcwd, cwd, sizeof lcwd);
strlcpy(latestdir, cwd, sizeof latestdir);
if (tmpdir == NULL) {
tmpdir = getTmpdir();
tmplen = strlen(tmpdir);
}
/* we want to track all the .meta we read */
Global_Append(".MAKE.META.FILES", fname);
cmp_filter = metaCmpFilter || Var_Exists(gn, MAKE_META_CMP_FILTER);
cmdNode = gn->commands.first;
while (!oodate && (x = fgetLine(&buf, &bufsz, 0, fp)) > 0) {
lineno++;
if (buf[x - 1] == '\n')
buf[x - 1] = '\0';
else {
warnx("%s: %u: line truncated at %u", fname, lineno, x);
oodate = true;
break;
}
link_src = NULL;
move_target = NULL;
/* Delimit the record type. */
p = buf;
#ifdef DEBUG_META_MODE
DEBUG3(META, "%s: %u: %s\n", fname, lineno, buf);
#endif
strsep(&p, " ");
if (strcmp(buf, "CMD") == 0) {
/*
* Compare the current command with the one in the
* meta data file.
*/
if (cmdNode == NULL) {
DEBUG2(META, "%s: %u: there were more build commands in the meta data file than there are now...\n",
fname, lineno);
oodate = true;
} else {
const char *cp;
char *cmd = cmdNode->datum;
bool hasOODATE = false;
if (strstr(cmd, "$?") != NULL)
hasOODATE = true;
else if ((cp = strstr(cmd, ".OODATE")) != NULL) {
/* check for $[{(].OODATE[:)}] */
if (cp > cmd + 2 && cp[-2] == '$')
hasOODATE = true;
}
if (hasOODATE) {
needOODATE = true;
DEBUG2(META, "%s: %u: cannot compare command using .OODATE\n",
fname, lineno);
}
cmd = Var_Subst(cmd, gn, VARE_EVAL_DEFINED);
/* TODO: handle errors */
if ((cp = strchr(cmd, '\n')) != NULL) {
int n;
/*
* This command contains newlines, we need to
* fetch more from the .meta file before we
* attempt a comparison.
*/
/* first put the newline back at buf[x - 1] */
buf[x - 1] = '\n';
do {
/* now fetch the next line */
if ((n = fgetLine(&buf, &bufsz, x, fp)) <= 0)
break;
x = n;
lineno++;
if (buf[x - 1] != '\n') {
warnx("%s: %u: line truncated at %u", fname, lineno, x);
break;
}
cp = strchr(cp + 1, '\n');
} while (cp != NULL);
if (buf[x - 1] == '\n')
buf[x - 1] = '\0';
}
if (p != NULL &&
!hasOODATE &&
!(gn->type & OP_NOMETA_CMP) &&
(meta_cmd_cmp(gn, p, cmd, cmp_filter) != 0)) {
DEBUG4(META, "%s: %u: a build command has changed\n%s\nvs\n%s\n",
fname, lineno, p, cmd);
if (!metaIgnoreCMDs)
oodate = true;
}
free(cmd);
cmdNode = cmdNode->next;
}
} else if (strcmp(buf, "CWD") == 0) {
/*
* Check if there are extra commands now
* that weren't in the meta data file.
*/