-
Notifications
You must be signed in to change notification settings - Fork 9
/
Builder.pm
282 lines (248 loc) · 8.9 KB
/
Builder.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
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
package My::Builder;
use strict;
use warnings;
use base 'Module::Build';
use lib "inc";
use My::Utility qw(find_SDL_dir find_file sed_inplace);
use File::Spec::Functions qw(catdir catfile splitpath catpath rel2abs abs2rel);
use File::Path qw(make_path remove_tree);
use File::Copy qw(cp);
use File::Fetch;
use File::Find;
use Archive::Extract;
use Digest::SHA1 qw(sha1_hex);
use Config;
sub ACTION_build {
my $self = shift;
# as we want to wipe 'sharedir' during 'Build clean' we has
# to recreate 'sharedir' at this point if it does not exist
mkdir 'sharedir' unless(-d 'sharedir');
$self->add_to_cleanup('sharedir');
$self->SUPER::ACTION_build;
}
sub ACTION_code {
my $self = shift;
my $bp = $self->notes('build_params');
die "###ERROR### Cannot continue build_params not defined" unless defined($bp);
# check marker
if (! $self->check_build_done_marker) {
# important directories
my $download = 'download';
my $patches = 'patches';
# we are deriving the subdir name from $bp->{title} as we want to
# prevent troubles when user reinstalls the same version of
# Alien::SDL with different build options
my $share_subdir = $self->{properties}->{dist_version} . '_' . substr(sha1_hex($bp->{title}), 0, 8);
my $build_out = catfile('sharedir', $share_subdir);
my $build_src = 'build_src';
$self->add_to_cleanup($build_src, $build_out);
# save some data into future Alien::SDL::ConfigData
$self->config_data('build_prefix', $build_out);
$self->config_data('build_params', $bp);
$self->config_data('build_cc', $Config{cc});
$self->config_data('build_arch', $Config{archname});
$self->config_data('build_os', $^O);
if($bp->{buildtype} eq 'use_config_script') {
$self->config_data('script', $bp->{script});
}
elsif($bp->{buildtype} eq 'use_prebuilt_binaries') {
# all the following functions die on error, no need to test ret values
$self->fetch_binaries($download);
$self->clean_dir($build_out);
$self->extract_binaries($download, $build_out);
$self->set_config_data($build_out);
}
elsif($bp->{buildtype} eq 'build_from_sources' ) {
# all the following functions die on error, no need to test ret values
$self->fetch_sources($download);
$self->extract_sources($download, $patches, $build_src);
$self->clean_dir($build_out);
$self->build_binaries($build_out, $build_src);
$self->set_config_data($build_out);
}
# mark sucessfully finished build
$self->touch_build_done_marker;
}
$self->SUPER::ACTION_code;
}
sub fetch_file {
my ($self, $url, $sha1sum, $download) = @_;
die "###ERROR### _fetch_file undefined url\n" unless $url;
die "###ERROR### _fetch_file undefined sha1sum\n" unless $sha1sum;
my $ff = File::Fetch->new(uri => $url);
my $fn = catfile($download, $ff->file);
if (-e $fn) {
print "Checking checksum for already existing '$fn'...\n";
return 1 if $self->check_sha1sum($fn, $sha1sum);
unlink $fn; #exists but wrong checksum
}
print "Fetching '$url'...\n";
my $fullpath = $ff->fetch(to => $download);
die "###ERROR### Unable to fetch '$url'" unless $fullpath;
if (-e $fn) {
print "Checking checksum for '$fn'...\n";
return 1 if $self->check_sha1sum($fn, $sha1sum);
die "###ERROR### Checksum failed '$fn'";
}
die "###ERROR### _fetch_file failed '$fn'";
}
sub fetch_binaries {
my ($self, $download) = @_;
my $bp = $self->notes('build_params');
$self->fetch_file($bp->{url}, $bp->{sha1sum}, $download);
}
sub fetch_sources {
my ($self, $download) = @_;
my $bp = $self->notes('build_params');
$self->fetch_file($_->{url}, $_->{sha1sum}, $download) foreach (@{$bp->{members}});
}
sub extract_binaries {
my ($self, $download, $build_out) = @_;
# do extract binaries
my $bp = $self->notes('build_params');
my $archive = catfile($download, File::Fetch->new(uri => $bp->{url})->file);
print "Extracting $archive...\n";
my $ae = Archive::Extract->new( archive => $archive );
die "###ERROR###: Cannot extract $archive ", $ae->error unless $ae->extract(to => $build_out);
# fix hardcoded prefix path in bin/sdl-config
my ($version, $prefix, $incdir, $libdir) = find_SDL_dir(rel2abs($build_out));
sed_inplace("$prefix/bin/sdl-config", 's/^prefix=.*/prefix=\''.quotemeta($prefix).'\'/');
}
sub extract_sources {
my ($self, $download, $patches, $build_src) = @_;
my $bp = $self->notes('build_params');
foreach my $pack (@{$bp->{members}}) {
my $srcdir = catfile($build_src, $pack->{dirname});
my $unpack = 'y';
$unpack = $self->prompt("Dir '$srcdir' exists, wanna replace with clean sources?", "n") if (-d $srcdir);
if (lc($unpack) eq 'y') {
my $archive = catfile($download, File::Fetch->new(uri => $pack->{url})->file);
print "Extracting $pack->{pack}...\n";
my $ae = Archive::Extract->new( archive => $archive );
die "###ERROR###: cannot extract $pack ", $ae->error unless $ae->extract(to => $build_src);
foreach my $i (@{$pack->{patches}}) {
chdir catfile($build_src, $pack->{dirname});
print "Applying patch '$i'\n";
my $cmd = $self->patch_command($srcdir, catfile($patches, $i));
if ($cmd) {
print "(cmd: $cmd)\n";
$self->do_system($cmd) or die "###ERROR### [$?] during patch ... ";
}
chdir $self->base_dir();
}
}
}
return 1;
}
sub set_config_data {
my( $self, $build_out ) = @_;
# try to find SDL root dir
my ($version, $prefix, $incdir, $libdir) = find_SDL_dir(rel2abs($build_out));
die "###ERROR### Cannot find SDL directory in 'sharedir'" unless $version;
$self->config_data('share_subdir', abs2rel($prefix, rel2abs('sharedir')));
# set defaults
my $cfg = {
# defaults
version => $version,
prefix => '@PrEfIx@',
libs => '"-L@PrEfIx@/lib" -lSDLmain -lSDL',
cflags => '"-I@PrEfIx@/include/SDL" -D_GNU_SOURCE=1 -Dmain=SDL_main "-I@PrEfIx@/include" "-I@PrEfIx@/include/smpeg"',
shared_libs => [ ],
};
# overwrite values available via sdl-config
my $bp = $self->config_data('build_prefix') || $prefix;
my $devnull = File::Spec->devnull();
my $script = rel2abs("$prefix/bin/sdl-config");
foreach my $p (qw(version prefix libs cflags)) {
my $o=`$script --$p 2>$devnull`;
if ($o) {
$o =~ s/[\r\n]*$//;
$o =~ s/\Q$prefix\E/\@PrEfIx\@/g;
# prefix-hack required to support also nonSDL libs in 'sharedir'
$o .= ' "-I@PrEfIx@/include" "-I@PrEfIx@/include/smpeg"' if ($p eq 'cflags');
$cfg->{$p} = $o;
}
}
# find and set ld_shared_libs
my @shlibs = find_file($build_out, qr/\.\Q$Config{dlext}\E$/);
my $p = rel2abs($prefix);
$_ =~ s/^\Q$prefix\E/\@PrEfIx\@/ foreach (@shlibs);
$cfg->{ld_shared_libs} = [ @shlibs ];
# set ld_paths and ld_shlib_map
my %tmp = ();
my %shlib_map = ();
foreach my $full (@shlibs) {
my ($v, $d, $f) = splitpath($full);
$tmp{ catpath($v, $d, '') } = 1;
if ($f =~ /smpeg/) {
$shlib_map{smpeg} = $full;
}
elsif ($f =~ /^(lib)?(SDL_[a-zA-Z]{2,8})[^a-zA-Z]/) {
# sort of dark magic how to detect SDL_<something> related shlib
$shlib_map{$2} = $full;
}
elsif ($f =~ /^(lib)?(SDL)/) {
# '*SDL*' that did not pass previous test is probably core 'SDL'
$shlib_map{SDL} = $full;
}
};
$cfg->{ld_paths} = [ keys %tmp ];
$cfg->{ld_shlib_map} = \%shlib_map;
# write config
$self->config_data('config', $cfg);
}
sub can_build_binaries_from_sources {
# this needs to be overriden in My::Builder::<platform>
return 0; # no
}
sub build_binaries {
# this needs to be overriden in My::Builder::<platform>
my( $self, $build_out, $build_src ) = @_;
die "###ERROR### My::Builder cannot build SDL from sources, use rather My::Builder::<platform>";
}
sub clean_dir {
my( $self, $dir ) = @_;
if (-d $dir) {
remove_tree($dir);
make_path($dir);
}
}
sub check_build_done_marker {
my $self = shift;
return (-e 'build_done');
}
sub touch_build_done_marker {
my $self = shift;
require ExtUtils::Command;
local @ARGV = ('build_done');
ExtUtils::Command::touch();
$self->add_to_cleanup('build_done');
}
sub clean_build_done_marker {
my $self = shift;
unlink 'build_done' if (-e 'build_done');
}
sub check_sha1sum {
my( $self, $file, $sha1sum ) = @_;
my $sha1 = Digest::SHA1->new;
my $fh;
open($fh, $file) or die "###ERROR## Cannot check checksum for '$file'\n";
binmode($fh);
$sha1->addfile($fh);
close($fh);
return ($sha1->hexdigest eq $sha1sum) ? 1 : 0
}
sub patch_command {
my( $self, $base_dir, $patch_file ) = @_;
my $devnull = File::Spec->devnull();
my $test = `patch --help 2> $devnull`;
if ($test) {
$patch_file = File::Spec->abs2rel( $patch_file, $base_dir );
# the patches are expected with UNIX newlines
# the following command works on both UNIX+Windows
return "$^X -pe '' -- $patch_file | patch -N -p1 -u";
}
warn "###WARN### patch not available";
return '';
}
1;