forked from skaji/relocatable-perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relocatable-perl-build
executable file
·209 lines (175 loc) · 5.65 KB
/
relocatable-perl-build
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
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use utf8;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case bundling);
use 5.010;
use Config;
use Cwd 'getcwd';
use File::Spec;
use File::Temp qw(tempfile tempdir);
use File::Path 'rmtree';
use File::Basename 'dirname';
use Pod::Usage 'pod2usage';
sub run { !system @_ or die "failed @_\n" }
my @LINUX_LIBPTH = qw(
/usr/local/lib64
/usr/local/lib
/lib/x86_64-linux-gnu
/lib64
/lib
/usr/lib/x86_64-linux-gnu
/usr/lib64
/usr/lib
);
=head1 NAME
relocatable-perl-build - building perl with relocatable settings
=head1 SYNOPSIS
> relocatable-perl-build OPTIONS
Options:
--prefix install prefix, this is just for installation.
After installation, you can move perl wherever you want!
--perl_version install perl version
--tarball use local tar.gz
--help, -h show this help message
Examples:
> relocatable-perl-build --prefix /usr/local --perl_version 5.20.0
> relocatable-perl-build --prefix ~/perl --tarball /tmp/perl-5.18.2.tar.gz
=head1 AUTHOR
Shoichi Kaji
=cut
GetOptions
"prefix=s" => \(my $prefix),
"perl_version=s" => \(my $perl_version),
"tarball=s" => \(my $tarball),
"help|h" => sub { pod2usage(0) },
or pod2usage(1);
$prefix or do { warn "prefix option is required.\n"; pod2usage(1) };
if ($tarball && $tarball =~ /5\.(\d+)\.(\d+)/) {
$perl_version = "5.$1.$2";
}
$perl_version or do { warn "perl_verion option is required.\n"; pod2usage(1) };
if (!-d $prefix) {
mkdir $prefix or die "mkdir $prefix: $!\n";
} elsif (!-w $prefix) {
die "don't have write permission to $prefix\n";
}
perl_build($prefix, $perl_version, $tarball);
my $config_heavy = `$prefix/bin/perldoc -lm Config_heavy.pl`;
die "failed to exec $prefix/bin/perldoc -lm Config_heavy.pl\n" if $? != 0;
chomp $config_heavy;
patch_config_heavy($config_heavy);
my $config_pm = `$prefix/bin/perldoc -lm Config`;
die "failed to exec $prefix/bin/perldoc -lm Config\n" if $? != 0;
chomp $config_pm;
patch_config_pm($config_pm);
say "---> successfully build perl $perl_version to $prefix";
debug_information("$prefix/bin/perl");
exit;
sub perl_build {
my ($perefix, $perl_version, $tarball) = @_;
my $current_dir = getcwd;
my $now = time;
my $tempdir = "/tmp/perl-build-$now.$$";
my $log = "/tmp/perl-build-$now.$$.log";
mkdir $tempdir or die "mkdir $tempdir: $!\n";
if ($tarball) {
say "---> use $tarball";
run "tar", "xzf", $tarball, "-C", $tempdir;
} else {
say "---> downloding perl-$perl_version.tar.gz";
chdir $tempdir;
run http_get(), "http://www.cpan.org/src/5.0/perl-$perl_version.tar.gz";
run "tar", "xzf", "perl-$perl_version.tar.gz";
}
my @Configure = (
"./Configure",
"-Dprefix=$prefix",
"-Duserelocatableinc",
"-Dman1dir=none",
"-Dman3dir=none",
($Config{archname} =~ /linux/i ? "-Dlibpth='@LINUX_LIBPTH'" : ()),
"-des",
);
chdir "$tempdir/perl-$perl_version" or die;
say "---> building perl $perl_version, see $log for progress";
run "@Configure >>$log 2>&1";
run "make -j4 >>$log 2>&1";
run "make install >>$log 2>&1";
chdir $current_dir;
rmtree $tempdir;
}
sub http_get {
if (grep { -x "$_/wget" } File::Spec->path) {
return ("wget", "-q");
} elsif (grep { -x "$_/curl" } File::Spec->path) {
return ("curl", "-sO");
} else {
die "Cannot find wget nor curl\n";
}
}
sub patch_config_heavy {
my $config_heavy = shift;
my @relocatable = do {
open my $fh, "<", $config_heavy or die "open $config_heavy: $!\n";
my @relocatable;
while (<$fh>) {
if (/^([^=]+)=['"].*\.\.\./) {
push @relocatable, $1;
}
}
@relocatable;
};
open my $in, "<", $config_heavy or die "open $config_heavy: $!\n";
my ($out, $tmpname) = tempfile UNLINK => 0, DIR => dirname($config_heavy);
my $mode = (stat $config_heavy)[2];
chmod $mode, $tmpname;
push @relocatable, "perlpath", "startperl";
@relocatable = uniq(@relocatable);
my $fix_line1 = 'foreach my $what';
my $fix_line2 = 's/^($what=)';
while (<$in>) {
if (/^perlpath=/) {
say {$out} "perlpath='.../perl'";
} elsif (/^startperl=/) {
say {$out} "startperl='#!.../perl'";
} elsif (/^\Q$fix_line1\E/) {
say {$out} 'foreach my $what (qw(' . "@relocatable" . ')) {';
} elsif (/^(\s+)\Q$fix_line2\E/) {
say {$out} $1, q{s/^($what=)(['"])(#!)?(.*?)\2/$1 . $2 . ($3 || "") . relocate_inc($4) . $2/me;};
} else {
print {$out} $_;
}
}
close $_ for $in, $out;
rename $tmpname, $config_heavy or die "rename $tmpname $config_heavy: $!\n";
}
sub patch_config_pm {
my $config_pm = shift;
open my $in, "<", $config_pm or die "open $config_pm: $!\n";
my ($out, $tmpname) = tempfile UNLINK => 0, DIR => dirname($config_pm);
my $mode = (stat $config_pm)[2];
chmod $mode, $tmpname;
while (<$in>) {
if (/^(\s+)scriptdir\s*=>/) {
say {$out} "$1scriptdir => relocate_inc('.../'),";
} else {
print {$out} $_;
}
}
close $_ for $in, $out;
rename $tmpname, $config_pm or die "rename $tmpname $config_pm: $!\n";
}
sub debug_information {
my $perl = shift;
system $perl, "-MConfig", "-E", q(
for my $key (qw(config_args libpth libs)) {
say "---> with $key: $Config{$key}";
}
);
}
sub uniq {
my @item = @_;
my %seen;
grep { !$seen{$_}++ } @item;
}