| 1 |
root |
1.1 |
use strict; |
| 2 |
|
|
use Config; |
| 3 |
|
|
use ExtUtils::MakeMaker; |
| 4 |
|
|
|
| 5 |
|
|
my $inventory = require "./binaries.pl"; |
| 6 |
|
|
my ($major) = split /\./, $inventory->{version}; |
| 7 |
|
|
my $name = "alien-tclsh$major"; |
| 8 |
|
|
my $exe = "$name$Config{_exe}"; |
| 9 |
|
|
|
| 10 |
|
|
sub platform { |
| 11 |
|
|
my $arch = $Config{archname}; |
| 12 |
|
|
|
| 13 |
|
|
if ($^O eq "darwin") { |
| 14 |
|
|
return "darwin-universal" if $Config{ptrsize} == 8 && $arch !~ /powerpc|ppc/i; |
| 15 |
|
|
} elsif ($^O eq "MSWin32") { |
| 16 |
|
|
return "mswin32-x86_64" if $arch =~ /(?:x64|x86_64|amd64)/i; |
| 17 |
|
|
return "mswin32-x86" if $arch =~ /(?:x86|i[3-6]86)/i; |
| 18 |
|
|
} elsif ($^O eq "linux" && $arch !~ /musl/) { |
| 19 |
|
|
return "linux-x86_64" if $arch =~ /^x86_64-/ && $Config{ptrsize} == 8; |
| 20 |
|
|
return "linux-aarch64" if $arch =~ /^(?:aarch64|arm64)-/ && $Config{ptrsize} == 8; |
| 21 |
|
|
return "linux-riscv64" if $arch =~ /^riscv64-/ && $Config{ptrsize} == 8; |
| 22 |
|
|
} |
| 23 |
|
|
|
| 24 |
|
|
die "Alien::Tclsh: unsupported platform $^O/$arch\n"; |
| 25 |
|
|
} |
| 26 |
|
|
|
| 27 |
|
|
sub load_binary { |
| 28 |
|
|
my $file = shift; |
| 29 |
|
|
|
| 30 |
|
|
open my $fh, "<:raw", $file or die "$file: $!\n"; |
| 31 |
|
|
local $/; |
| 32 |
|
|
<$fh> |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
sub save_binary { |
| 36 |
|
|
my ($file, $data) = @_; |
| 37 |
|
|
|
| 38 |
|
|
require File::Temp; |
| 39 |
|
|
|
| 40 |
|
|
my ($dir) = $file =~ m{^(.*)/[^/]+$}; |
| 41 |
|
|
my ($fh, $tmp) = File::Temp::tempfile (DIR => $dir, UNLINK => 1); |
| 42 |
|
|
binmode $fh; |
| 43 |
|
|
print $fh $data or die "$tmp: $!\n"; |
| 44 |
|
|
close $fh or die "$tmp: $!\n"; |
| 45 |
|
|
rename $tmp, $file or die "rename $tmp to $file: $!\n"; |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
|
sub unpack_binary { |
| 49 |
|
|
my $gz = shift; |
| 50 |
|
|
|
| 51 |
|
|
require IO::Uncompress::Gunzip; |
| 52 |
|
|
|
| 53 |
|
|
IO::Uncompress::Gunzip::gunzip (\$gz => \my $data, Transparent => 0, Strict => 1) |
| 54 |
|
|
or die "Invalid gzip archive: $IO::Uncompress::Gunzip::GunzipError\n"; |
| 55 |
|
|
|
| 56 |
|
|
$data |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
sub verify_binary { |
| 60 |
|
|
my ($data, $digest) = @_; |
| 61 |
|
|
|
| 62 |
|
|
require Digest::SHA; |
| 63 |
|
|
|
| 64 |
|
|
Digest::SHA::sha384_hex ($data) eq $digest |
| 65 |
|
|
or die "Alien::Tclsh: SHA-384 mismatch (expected $digest)\n"; |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
sub filename { |
| 69 |
|
|
my $platform = shift; |
| 70 |
|
|
my $digest = $inventory->{binaries}{$platform} |
| 71 |
|
|
or die "Alien::Tclsh: no binary for $platform\n"; |
| 72 |
|
|
|
| 73 |
|
|
"$name-$platform-$digest" |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
|
sub stage_binary { |
| 77 |
|
|
my $dir = shift; |
| 78 |
|
|
|
| 79 |
|
|
my $platform = platform; |
| 80 |
|
|
my $digest = $inventory->{binaries}{$platform}; |
| 81 |
|
|
my $file = "prebuilt/" . filename $platform; |
| 82 |
|
|
my $data; |
| 83 |
|
|
|
| 84 |
|
|
if (-f $file) { |
| 85 |
|
|
$data = load_binary $file; |
| 86 |
|
|
} elsif (-f "$file.gz") { |
| 87 |
|
|
$data = unpack_binary load_binary "$file.gz"; |
| 88 |
|
|
} else { |
| 89 |
|
|
$ENV{ALIEN_TCLSH_NO_DOWNLOAD} |
| 90 |
|
|
and die "Alien::Tclsh: missing $file (downloads disabled)\n"; |
| 91 |
|
|
|
| 92 |
|
|
require HTTP::Tiny; |
| 93 |
|
|
|
| 94 |
|
|
my $url = "https://alien-tclsh.schmorp.de/alien-tclsh/" . filename ($platform) . ".gz"; |
| 95 |
|
|
print "Downloading $url\n"; |
| 96 |
|
|
|
| 97 |
|
|
my $res = HTTP::Tiny->new (verify_SSL => 1, max_size => 64 * 1024 * 1024)->get ($url); |
| 98 |
|
|
$res->{success} |
| 99 |
|
|
or die "$url: $res->{status} $res->{reason}\n"; |
| 100 |
|
|
|
| 101 |
|
|
$data = unpack_binary $res->{content}; |
| 102 |
|
|
verify_binary $data, $digest; |
| 103 |
|
|
|
| 104 |
|
|
mkdir "prebuilt"; |
| 105 |
|
|
save_binary $file, $data; |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
verify_binary $data, $digest; |
| 109 |
|
|
require File::Path; |
| 110 |
|
|
File::Path::make_path ($dir); |
| 111 |
|
|
save_binary "$dir/$exe", $data; |
| 112 |
|
|
chmod 0755, "$dir/$exe" or die "chmod $dir/$exe: $!\n"; |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
sub dist_prebuilt { |
| 116 |
|
|
my $dir = shift; |
| 117 |
|
|
|
| 118 |
|
|
open my $fh, "<", "$dir/MANIFEST" or die "$dir/MANIFEST: $!\n"; |
| 119 |
|
|
my @manifest = <$fh>; |
| 120 |
|
|
close $fh; |
| 121 |
|
|
|
| 122 |
|
|
for (@manifest) { |
| 123 |
|
|
if (m{^(prebuilt/\Q$name\E-([a-z0-9_-]+)-([0-9a-f]{96}))(?=\s|$)}) { |
| 124 |
|
|
my ($file, $platform, $digest) = ($1, $2, $3); |
| 125 |
|
|
$inventory->{binaries}{$platform} eq $digest or die "$file: not in binary inventory\n"; |
| 126 |
|
|
verify_binary +(load_binary "$dir/$file"), $digest; |
| 127 |
|
|
|
| 128 |
|
|
system "pigz", "-11", "-n", "-k", "--", "$dir/$file" |
| 129 |
|
|
and die "pigz $dir/$file failed\n"; |
| 130 |
|
|
unlink "$dir/$file" or die "unlink $dir/$file: $!\n"; |
| 131 |
|
|
s/^\Q$file\E/$file.gz/; |
| 132 |
|
|
} |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
open $fh, ">", "$dir/MANIFEST" or die "$dir/MANIFEST: $!\n"; |
| 136 |
|
|
print $fh @manifest or die "$dir/MANIFEST: $!\n"; |
| 137 |
|
|
close $fh or die "$dir/MANIFEST: $!\n"; |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
if (@ARGV && $ARGV[0] eq "--stage") { |
| 141 |
|
|
stage_binary $ARGV[1]; |
| 142 |
|
|
exit; |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
if (@ARGV && $ARGV[0] eq "--dist-prebuilt") { |
| 146 |
|
|
dist_prebuilt $ARGV[1]; |
| 147 |
|
|
exit; |
| 148 |
|
|
} |
| 149 |
|
|
|
| 150 |
|
|
my $file = "prebuilt/" . filename (platform); |
| 151 |
|
|
my %build_requires = ( |
| 152 |
|
|
"Digest::SHA" => 0, |
| 153 |
|
|
"IO::Uncompress::Gunzip" => 0, |
| 154 |
|
|
); |
| 155 |
|
|
|
| 156 |
|
|
unless (-f $file || -f "$file.gz") { |
| 157 |
|
|
%build_requires = ( |
| 158 |
|
|
%build_requires, |
| 159 |
|
|
"HTTP::Tiny" => 0, |
| 160 |
|
|
"IO::Socket::SSL" => 0, |
| 161 |
|
|
"Net::SSLeay" => 0, |
| 162 |
|
|
); |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
WriteMakefile ( |
| 166 |
|
|
NAME => "Alien::Tclsh", |
| 167 |
|
|
VERSION_FROM => "Tclsh.pm", |
| 168 |
|
|
ABSTRACT_FROM => "Tclsh.pm", |
| 169 |
|
|
AUTHOR => "Marc Lehmann", |
| 170 |
|
|
LICENSE => "perl_5", |
| 171 |
|
|
PM => { "Tclsh.pm" => '$(INST_LIBDIR)/Tclsh.pm' }, |
| 172 |
|
|
BUILD_REQUIRES => \%build_requires, |
| 173 |
|
|
TEST_REQUIRES => { "Test::More" => 0 }, |
| 174 |
|
|
dist => { |
| 175 |
|
|
COMPRESS => "pigz -11", |
| 176 |
|
|
SUFFIX => ".gz", |
| 177 |
|
|
DIST_CP => "cp", |
| 178 |
|
|
}, |
| 179 |
|
|
); |
| 180 |
|
|
|
| 181 |
|
|
sub MY::postamble { |
| 182 |
|
|
my $source = -f $file ? $file : -f "$file.gz" ? "$file.gz" : ""; |
| 183 |
|
|
|
| 184 |
|
|
<<"MAKE"; |
| 185 |
|
|
pure_all :: \$(INST_SCRIPT)/$exe |
| 186 |
|
|
|
| 187 |
|
|
\$(INST_SCRIPT)/$exe : \$(FIRST_MAKEFILE) binaries.pl $source |
| 188 |
|
|
\$(PERLRUN) Makefile.PL --stage "\$(INST_SCRIPT)" |
| 189 |
|
|
|
| 190 |
|
|
MAKE |
| 191 |
|
|
} |
| 192 |
|
|
|
| 193 |
|
|
package MY; |
| 194 |
|
|
|
| 195 |
|
|
sub distdir { |
| 196 |
|
|
my $self = shift; |
| 197 |
|
|
my $make = $self->SUPER::distdir (@_); |
| 198 |
|
|
|
| 199 |
|
|
$make =~ s{(\ndistdir\s*:)}{\t\$(PERLRUN) Makefile.PL --dist-prebuilt "\$(DISTVNAME)"\n$1} |
| 200 |
|
|
or die "Cannot locate MakeMaker distdir target\n"; |
| 201 |
|
|
|
| 202 |
|
|
$make |
| 203 |
|
|
} |