-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperl6-precompile-all
executable file
·106 lines (89 loc) · 2.08 KB
/
perl6-precompile-all
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010000;
use autodie;
use File::Find;
use Getopt::Long;
use File::Spec;
use File::Temp;
my $perl6 = 'perl6-m';
GetOptions(
'perl6=s' => \$perl6
);
my $prefix = prefix();
my $sitelib = "$prefix/share/perl6/site/lib/";
&compile_all;
sub debugf {
my ($fmt, @p) = @_;
print STDERR sprintf($fmt, @p) . "\n" if $ENV{DEBUG};
}
sub compile_all {
find({
wanted => sub {
return 1 unless -f $_;
return 1 unless /\.pm6?\z/;
precompile(File::Spec->rel2abs($_));
return 1;
},
no_chdir => 1,
}, $sitelib);
}
sub prefix {
my $V = qx!$perl6 -V!;
$V =~ /moar::prefix=(.*)/ or die "invalid output: $V";
$1;
}
sub precompile_module {
my $pkg = shift;
state %seen;
return if $seen{$pkg}++;
$pkg =~ s!::!/!g;
for my $suffix (qw(.pm .pm6)) {
my $path = "$sitelib/$pkg$suffix";
if (-f $path) {
debugf("precompile target found: $pkg");
precompile($path);
last;
}
}
}
sub precompile {
my $file = shift @_;
$file = File::Spec->canonpath($file);
state %seen;
return if $seen{$file};
scan_use($file);
my $comptarget='mbc';
my $compext='moarvm';
my $output = "$file.${compext}";
my $file_mtime = (stat $file)[9];
my $output_mtime = -e $output ? (stat $output)[9] : 0;
if ($file_mtime <= $output_mtime) {
print "up to date $output. skip\n";
return 1;
}
my @cmd = ($perl6, "--target=$comptarget", "--output=${output}", $file);
print "@cmd\n";
my $out = `@cmd 2>&1`;
my $retval = $?;
if ($out =~ /Please pre-compile (.*)$/m) {
print "$1 is required\n";
precompile($1); # recompile deps
precompile($file); # try it again
} else {
$seen{$file}++;
print $out;
}
}
sub scan_use {
my $file = shift;
open my $fh, '<', $file;
while (<$fh>) {
if (/use\s+([^;\n]+)/) {
next if $1 eq 'v6';
precompile_module($1);
}
}
}