-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalienfile
330 lines (287 loc) · 8.58 KB
/
alienfile
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use alienfile;
use strict;
use warnings;
use Config;
use File::Spec;
use File::Find;
use File::Copy;
use Path::Tiny;
use URI;
# Settings:
#
# - ALIEN_LIBTENSORFLOW_FROM_BINARY_VERSION
#
# Filter of version to download (e.g., "2.9.3" to download
# libtensorflow v2.9.3).
#
# - ALIEN_LIBTENSORFLOW_FROM_BINARY_ALLOW_RELEASE_CANDIDATES
#
# Boolean that allows release candidate binary releases when true (default: 0).
#
# - ALIEN_LIBTENSORFLOW_FROM_SOURCE
#
# Boolean to control if libtensorflow is built from source (default: 0).
#
# When set to 0, will attempt to download binary if available for platform
# and builds from source otherwise.
#
# When set to 1, always builds from source.
#
# - ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE
#
# Which type of build to create.
#
# Values:
# - "release": release build (default)
#
# This has no debugging symbols.
#
# - "debug": debug build
#
# - ALIEN_LIBTENSORFLOW_DEVICE_TYPE
#
# Which device type to use for binary release.
#
# Values:
# - "auto": automatically detect type (default)
# - "cpu": CPU
# - "gpu": GPU (Currently only Nvidia)
use Env qw(
ALIEN_LIBTENSORFLOW_FROM_BINARY_VERSION
ALIEN_LIBTENSORFLOW_FROM_BINARY_ALLOW_RELEASE_CANDIDATES
ALIEN_LIBTENSORFLOW_FROM_SOURCE
ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE
ALIEN_LIBTENSORFLOW_DEVICE_TYPE
@PATH
);
my %os_arch_data = (
'linux:x86_64' => {
device_type => {
cpu => {
bucket_prefix => 'libtensorflow/libtensorflow-cpu-linux-x86_64',
bucket_format => 'tar.gz',
},
gpu => {
bucket_prefix => 'libtensorflow/libtensorflow-gpu-linux-x86_64',
bucket_format => 'tar.gz',
},
}
},
'darwin:x86_64' => {
device_type => {
cpu => {
bucket_prefix => 'libtensorflow/libtensorflow-cpu-darwin-x86_64',
bucket_format => 'tar.gz',
}
},
},
'MSWin32:x86_64' => {
device_type => {
cpu => {
bucket_prefix => 'libtensorflow/libtensorflow-cpu-windows-x86_64',
bucket_format => 'zip',
},
gpu => {
bucket_prefix => 'libtensorflow/libtensorflow-gpu-windows-x86_64',
bucket_format => 'zip',
},
}
},
);
my %os_dynamic_lib = (
'linux' => 'libtensorflow.so.2',
'darwin' => 'libtensorflow.2.dylib',
'MSWin32' => 'tensorflow.dll',
);
my %other_os_dynamic_lib = (
'linux' => 'libtensorflow_framework.so.2',
'darwin' => 'libtensorflow_framework.2.dylib',
);
probe sub {
# linux: ./lib/libtensorflow.so.2
# darwin: ./lib/libtensorflow.2.dylib
# win32: ./lib/tensorflow.dll
my @prefix = ( "/usr/local" );
for my $prefix (@prefix) {
my $dylib_path = File::Spec->catfile(
$prefix, qw(lib),
$os_dynamic_lib{ $^O }
);
return 'system' if -f $dylib_path;
}
return 'share';
};
sub detect_gpu {
# detect Nvidia
if( $^O eq 'linux' ) {
return !! File::Which::which('nvidia-smi');
} elsif( $^O eq 'MSWin32' ) {
local $ENV{PATH} = $ENV{PATH};
push @PATH, File::Spec->catfile($ENV{ProgramFiles}, 'NVIDIA Corporation','NVSMI' );
return !! File::Which::which('nvidia-smi');
}
return 0;
}
sub device_type {
return 'gpu' if detect_gpu;
return 'cpu';
}
share {
requires 'HTTP::Tiny' => 0;
requires 'Net::SSLeay' => 0;
requires 'IO::Socket::SSL' => 0;
requires 'URI' => 0;
requires 'File::Which';
requires 'Alien::Build::Plugin::Download::GitHub' => 0.10;
$ENV{ALIEN_LIBTENSORFLOW_FROM_BINARY_ALLOW_RELEASE_CANDIDATES} ||= 0;
# 0|1 (default: 0)
$ENV{ALIEN_LIBTENSORFLOW_FROM_SOURCE} ||= 0;
# release|debug (default: release)
$ENV{ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE} ||= 'release';
die "Unknown value for ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE = $ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE"
unless $ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE =~ /^(release|debug)$/;
# auto|cpu|gpu (default: auto)
$ENV{ALIEN_LIBTENSORFLOW_DEVICE_TYPE} ||= 'auto';
die "Unknown value for ALIEN_LIBTENSORFLOW_DEVICE_TYPE = $ALIEN_LIBTENSORFLOW_DEVICE_TYPE"
unless $ALIEN_LIBTENSORFLOW_DEVICE_TYPE =~ /^(auto|cpu|gpu)$/;
my $os_arch = join ":", ( $^O, meta->prop->{platform}{cpu}{arch}{name} );
if(exists $os_arch_data{$os_arch} && !$ENV{ALIEN_LIBTENSORFLOW_FROM_SOURCE}) {
my $device_type = $ALIEN_LIBTENSORFLOW_DEVICE_TYPE eq 'auto'
? device_type()
: $ALIEN_LIBTENSORFLOW_DEVICE_TYPE;
die "Binary release for $os_arch + $device_type does not exist"
unless exists $os_arch_data{$os_arch}{device_type}{$device_type};
my $data = $os_arch_data{$os_arch}{device_type}{$device_type};
my $version_filter = exists $ENV{ALIEN_LIBTENSORFLOW_FROM_BINARY_VERSION}
? $ALIEN_LIBTENSORFLOW_FROM_BINARY_VERSION
: '2.';
(my $bucket_prefix_no_dir = $data->{bucket_prefix}) =~ s,^libtensorflow/,,;
my $re = qr{
^
\Q@{[ $bucket_prefix_no_dir ]}\E
-
(?<version> .* )
\.
\Q@{[ $data->{bucket_format} ]}\E
$
}x;
my $bucket_url = 'https://storage.googleapis.com/tensorflow/';
my $start_url = URI->new($bucket_url);
$start_url->query_form( prefix => join('',
$data->{bucket_prefix},
'-',
$version_filter,
));
start_url $start_url;
plugin 'Decode::Mojo';
meta->around_hook( fetch => sub {
my $orig = shift;
my $build = shift;
my $fetched = $orig->($build, @_);
if( $fetched->{filename} eq 'tensorflow' ) {
# bucket data
$fetched->{content} = path($fetched->{path})->slurp_raw if ! exists $fetched->{content} && exists $fetched->{path};
my $xml = $build->meta_prop->{plugin_decode_mojo_class}->new( $fetched->{content} );
my $list = $xml->find('Key')->map(sub{
my $content = $_[0]->content;
(my $filename = $content) =~ s,^libtensorflow/,,;
my $bucket_item = {
filename => $filename,
version => do { $filename =~ $re && $1 },
url => do { join '', $bucket_url, $content },
protocol => 'https',
};
if( ! $ALIEN_LIBTENSORFLOW_FROM_BINARY_ALLOW_RELEASE_CANDIDATES && $bucket_item->{version} =~ /-rc/ ) {
return ();
}
if( $content !~ /\.\Q@{[ $data->{bucket_format} ]}\E$/ ) {
return ();
}
return $bucket_item;
});
return {
type => 'list',
list => $list,
};
}
return $fetched;
});
plugin Download => (
version => $re,
prefer => 1,
);
if( $data->{bucket_format} eq 'tar.gz' ) {
# Extract::CommandLine more reliable than
# Extract::ArchiveTar for large .tar.gz.
#
# Extract::ArchiveTar gives an "Out of memory!" error.
plugin 'Extract::CommandLine' => $data->{bucket_format};
} else {
plugin 'Extract' => $data->{bucket_format};
}
patch [
sub {
my ($build) = @_;
my $lib_dir = 'lib';
# This is because ExtUtils::Install uses File::Copy::copy()
# which does not handle symlinks (it copies the
# contents of what the symlinks point to).
$build->log("Only keep one copy of library, no symlinks");
for my $lib ( map { exists $_->{$^O} ? $_->{$^O} : () } \%os_dynamic_lib, \%other_os_dynamic_lib ) {
my $lib_symlink = File::Spec->catfile($lib_dir, $lib );
next unless -l $lib_symlink;
$build->log( "Processing $lib" );
my $lib_file = $lib_symlink;
$lib_file = File::Spec->rel2abs(readlink $lib_file, $lib_dir) while -l $lib_file;
unlink $lib_symlink;
File::Copy::move($lib_file , $lib_symlink);
}
my @symlinks;
find(sub { push @symlinks, $File::Find::name if -l }, $lib_dir);
unlink @symlinks;
},
];
plugin 'Build::Copy';
meta->after_hook( build => sub {
my($build) = @_;
$build->runtime_prop->{'style'} = 'binary';
$build->runtime_prop->{'device_type'} = $device_type;
});
gather sub {
my($build) = @_;
my $prefix = $build->runtime_prop->{prefix};
my $include_path = File::Spec->catfile($prefix, qw(include));
my $lib_path = File::Spec->catfile($prefix, qw(lib));
my $cflags = "-I$include_path";
my @ldlibs = "-ltensorflow";
my $libs = join " ", "-L$lib_path", @ldlibs;
$build->runtime_prop->{cflags} = $cflags;
$build->runtime_prop->{libs} = $libs;
};
} else {
requires 'Alien::Bazel';
plugin 'Download::GitHub' => (
github_user => 'tensorflow',
github_repo => 'tensorflow',
);
build [
sub {
my ($build) = @_;
$build->install_prop->{libtensorflow_dynlib} = $os_dynamic_lib{$^O};
},
[
qw(bazel build),
'--verbose_failures',
( $ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE eq 'debug' ? q(--config=dbg) : () ),
'//tensorflow:%{.install.libtensorflow_dynlib}'
],
'%{make_path} %{.install.stage}/lib',
"%{cp} bazel-bin/tensorflow/%{.install.libtensorflow_dynlib} %{.install.stage}/lib",
];
meta->after_hook( build => sub {
my($build) = @_;
$build->runtime_prop->{'style'} = 'source';
$build->runtime_prop->{'build_type'} = $ALIEN_LIBTENSORFLOW_FROM_SOURCE_BUILD_TYPE;
});
}
};