/
PrecompilationRepository.pm
156 lines (139 loc) Β· 5.47 KB
/
PrecompilationRepository.pm
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
{
my $i;
role CompUnit::PrecompilationRepository {
has $!i = $i++;
method load(CompUnit::PrecompilationId $id) returns CompUnit {
CompUnit
}
method may-precomp() {
$i < 3 # number of next repo after None and the first Default
}
}
}
BEGIN CompUnit::PrecompilationRepository::<None> := CompUnit::PrecompilationRepository.new;
class CompUnit { ... }
class CompUnit::PrecompilationRepository::Default does CompUnit::PrecompilationRepository {
has CompUnit::PrecompilationStore $.store;
has %!loaded;
my $lle;
my $profile;
method !load-handle-for-path(IO::Path $path) {
my $preserve_global := nqp::ifnull(nqp::gethllsym('perl6', 'GLOBAL'), Mu);
if $*RAKUDO_MODULE_DEBUG -> $RMD { $RMD("Loading precompiled\n$path") }
my $handle := CompUnit::Loader.load-precompilation-file($path);
nqp::bindhllsym('perl6', 'GLOBAL', $preserve_global);
CATCH {
default {
nqp::bindhllsym('perl6', 'GLOBAL', $preserve_global);
.throw;
}
}
LEAVE {
self.store.unlock;
}
$handle
}
method !load-dependencies(IO::Path $path, Instant $since) {
my $compiler-id = $*PERL.compiler.id;
my $RMD = $*RAKUDO_MODULE_DEBUG;
for ($path ~ '.deps').IO.lines -> $dependency {
Rakudo::Internals.KEY_SPACE_VALUE($dependency,my $id,my $src);
my $file = self.store.path($compiler-id, $id);
my $modified = $file.modified;
$RMD("$file\nmtime: $modified\nsince: $since\n src: {$src.IO.modified}")
if $RMD;
return False if $modified > $since;
my $srcIO = $src.IO;
return False if not $srcIO.e or $modified <= $srcIO.modified;
%!loaded{$id} //= self!load-handle-for-path(self.store.load($compiler-id, $id));
# report back id and source location of dependency to dependant
say "$id $src" if $*W and $*W.is_precompilation_mode;
}
True
}
method load(CompUnit::PrecompilationId $id, Instant :$since) returns CompUnit::Handle {
my $compiler-id = $*PERL.compiler.id;
my $path = self.store.load($compiler-id, $id);
if $path {
my $modified = $path.modified;
if (not $since or $modified > $since) and self!load-dependencies($path, $modified) {
%!loaded{$id} //= self!load-handle-for-path($path)
}
else {
if $*RAKUDO_MODULE_DEBUG -> $RMD {
$RMD("Removing precompiled $path\nmtime: $modified\nsince: $since")
}
# remove outdated file so we precompile again
self.store.delete($compiler-id, $id);
self.store.unlock;
CompUnit::Handle
}
}
else {
self.store.unlock;
CompUnit::Handle
}
}
method precompile(IO::Path:D $path, CompUnit::PrecompilationId $id, Bool :$force = False) {
my $compiler-id = $*PERL.compiler.id;
my $io = self.store.destination($compiler-id, $id);
my $RMD = $*RAKUDO_MODULE_DEBUG;
if not $force and $io.e and $io.s {
$RMD("$path\nalready precompiled into\n$io") if $RMD;
self.store.unlock;
return True;
}
my $rev-deps = ($io ~ '.rev-deps').IO;
if $rev-deps.e {
for $rev-deps.lines {
$RMD("removing outdated rev-dep $_") if $RMD;
self.store.delete($compiler-id, $_);
}
}
$lle //= Rakudo::Internals.LL-EXCEPTION;
$profile //= Rakudo::Internals.PROFILE;
my %ENV := %*ENV;
%ENV<RAKUDO_PRECOMP_WITH> = $*REPO.repo-chain.map(*.path-spec).join(',');
%ENV<RAKUDO_PRECOMP_LOADING> = to-json @*MODULES // [];
my $current_dist = %ENV<RAKUDO_PRECOMP_DIST>;
%ENV<RAKUDO_PRECOMP_DIST> = $*RESOURCES ?? $*RESOURCES.Str !! '{}';
$RMD("Precompiling $path into $io") if $RMD;
my $perl6 = $*EXECUTABLE.subst('perl6-debug', 'perl6'); # debugger would try to precompile it's UI
my $proc = run(
$perl6,
$lle,
$profile,
"--target=" ~ Rakudo::Internals.PRECOMP-TARGET,
"--output=$io",
$path,
:out,
);
%ENV.DELETE-KEY(<RAKUDO_PRECOMP_WITH>);
%ENV.DELETE-KEY(<RAKUDO_PRECOMP_LOADING>);
%ENV<RAKUDO_PRECOMP_DIST> = $current_dist;
my @result = $proc.out.lines.unique;
if not $proc.out.close or $proc.status { # something wrong
self.store.unlock;
push @result, "Return status { $proc.status }\n";
$RMD("Precomping $path failed: {@result}") if $RMD;
fail @result if @result;
}
else {
$RMD("Precompiled $path into $io") if $RMD;
my str $dependencies = '';
for @result -> $dependency {
Rakudo::Internals.KEY_SPACE_VALUE(
$dependency,my $dependency-id,my $dependency-src);
my $path = self.store.path($compiler-id, $dependency-id);
if $path.e {
$dependencies ~= "$dependency\n";
spurt($path ~ '.rev-deps', "$id\n", :append);
}
}
spurt($io ~ '.deps', $dependencies);
self.store.unlock;
True
}
}
}
# vim: ft=perl6 expandtab sw=4