forked from zhaozhixu/LightNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure
executable file
·254 lines (228 loc) · 7.98 KB
/
configure
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
#! /usr/bin/env perl
use warnings;
use strict;
use Sort::strverscmp;
use Getopt::Long;
# version information and dependencies
my $major = 0;
my $minor = 1;
my $micro = 0;
# my ($major, $minor, $micro) = &get_version("src/lightnet.h");
my $requires = "tensorlight >= 0.1.0";
my $usage = <<EOF;
Usage: $0 [OPTION[=VALUE]...]
Generate configuration makefile for building LightNet.
options:
-h, --help print help information
--target=NAME target name [lightnet]
--abbr=ABBR abbreviation name [ln]
--build-dir=DIR building directory [build]
--install-dir=DIR installation directory [/usr/local]
--prefix=DIR same as --install-dir; who comes later counts
--pkgconfig-dir=DIR pkgconfig directory [INSTALL_DIR/lib/pkgconfig]
--with-cuda=BOOL set to yes if build with CUDA [no]
--cuda-install-dir=DIR cuda installation directory [/usr/local/cuda]
--with-cudnn=BOOL set to yes if build with cudnn library [no]
--cudnn-install-dir=DIR cudn installation directory [/usr/local/cuda]
--with-tensorrt=BOOL set to yes if build with TensorRT [no]
--tensorrt-install-dir=DIR tensorrt installation directory [/usr]
--with-dpu=BOOL set to yes if build with DPU [no]
--with-python=BOOL set to yes if build with Python [no]
--python-prefix=DIR python package installation directory;
set to your virtualenv directory if using
virtualenv [INSTALL_DIR]
--python-version=VERSION python version [3]
--python-cmd=CMD python command [python3]
--python-user=BOOL install python packages to user site-package
'\$HOME/.local/lib/python<version>/site-packages'
--debug=BOOL set to yes when debugging [no]
--doc=BOOL set to yes if build the documents too [yes]
EOF
# default options, go in config.mk
my %opts = (
"TARGET" => "lightnet",
"ABBR" => "ln",
"BUILD_DIR" => "build",
"INSTALL_DIR" => "/usr/local",
"PKGCONFIG_DIR" => "/usr/local/lib/pkgconfig",
"WITH_CUDA" => "no",
"CUDA_INSTALL_DIR" => "/usr/local/cuda",
"WITH_CUDNN" => "no",
"CUDNN_INSTALL_DIR" => "/usr/local/cuda",
"WITH_TENSORRT" => "no",
"TENSORRT_INSTALL_DIR" => "/usr",
"WITH_DPU" => "no",
"WITH_PYTHON" => "no",
"PYTHON_PREFIX" => "/usr/local",
"PYTHON_VERSION" => "3",
"PYTHON_CMD" => "python3",
"PYTHON_USER" => "no",
"DEBUG" => "no",
"DOC" => "yes",
);
# parse arguments
GetOptions(
'help' => sub {&exit_msg(0, $usage)},
'target=s' => \$opts{TARGET},
'abbr=s' => \$opts{ABBR},
'build-dir=s' => \$opts{BUILD_DIR},
'install-dir=s' => \&set_prefix,
'prefix=s' => \&set_prefix,
'pkgconfig-dir=s' => \$opts{PKGCONFIG_DIR},
'with-cuda=s' => \$opts{WITH_CUDA},
'cuda-install-dir=s' => \$opts{CUDA_INSTALL_DIR},
'with-cudnn=s' => \$opts{WITH_CUDNN},
'cudnn-install-dir=s' => \$opts{CUDNN_INSTALL_DIR},
'with-tensorrt=s' => \$opts{WITH_TENSORRT},
'tensorrt-install-dir=s' => \$opts{TENSORRT_INSTALL_DIR},
'with-dpu=s' => \$opts{WITH_DPU},
'with-python=s' => \$opts{WITH_PYTHON},
'python-prefix=s' => \$opts{PYTHON_PREFIX},
'python-version=s' => \$opts{PYTHON_VERSION},
'python-cmd=s' => \$opts{PYTHON_CMD},
'python-user=s' => \$opts{PYTHON_USER},
'debug=s' => \$opts{DEBUG},
'doc=s' => \$opts{DOC},
) or &exit_msg(1, $usage);
# check configuration
# TODO: check versions
my $output;
$output = `gcc --version`;
if (!defined $output or $output eq "") {
&err_exit("gcc is not installed");
}
$output = `make --version`;;
if (!defined $output or $output eq "") {
&err_exit("make is not installed");
}
$output = `pkg-config --version`;;
if (!defined $output or $output eq "") {
&err_exit("pkg-config is not installed");
}
$output = `pkg-config --modversion check`;
if (!defined $output or $output eq "") {
&err_exit("check is not installed");
}
$output = `pkg-config --modversion tensorlight`;
if (!defined $output or $output eq "") {
&err_exit("tensorlight is not installed");
}
if ($opts{WITH_CUDNN} eq "yes") {
if (not $opts{WITH_CUDA} eq "yes") {
$opts{WITH_CUDA} = "yes";
print ("automatically set --with-cuda=yes\n");
}
}
if ($opts{WITH_TENSORRT} eq "yes") {
if (not $opts{WITH_CUDA} eq "yes") {
$opts{WITH_CUDA} = "yes";
print ("automatically set --with-cuda=yes\n");
}
}
if ($opts{WITH_CUDA} eq "yes") {
$output = `nvcc --version`;;
if (!defined $output or $output eq "") {
&err_exit("nvcc is not installed");
}
}
if ($opts{WITH_PYTHON} eq "yes") {
$output = `$opts{PYTHON_CMD} --version 2>&1`;
if (not defined $output or $output eq "" or
not &version_roughly_match($output, $opts{PYTHON_VERSION})) {
&warn_msg("PYTHON_CMD $opts{PYTHON_CMD} is not installed or doesn't match PYTHON_VERSION $opts{PYTHON_VERSION}, now set to python$opts{PYTHON_VERSION}");
$opts{PYTHON_CMD} = "python$opts{PYTHON_VERSION}";
$output = `$opts{PYTHON_CMD} --version 2>&1`;
if (!defined $output or $output eq "") {
&err_exit("$opts{PYTHON_CMD} is not installed");
}
}
}
if ($opts{DOC} eq "yes") {
$output = `mkdocs --version`;;
if (!defined $output or $output eq "") {
&warn_msg("mkdocs is is not installed; documents are not going to be built");
$opts{DOC} = "no";
} elsif (strverscmp(&mkdocs_version($output), "1.0.0") < 0) {
my $version = &mkdocs_version($output);
&warn_msg("mkdocs requires mininal version 1.0.0, but get $version; documents are not going to be built");
$opts{DOC} = "no";
}
}
my $config_str = <<EOF;
.SUFFIXES:
MAJOR = $major
MINOR = $minor
MICRO = $micro
REQUIRES = $requires
EOF
foreach my $key (sort keys %opts) {
$config_str .= "$key ?= $opts{$key}\n";
print "$key = $opts{$key}\n";
}
my $conf_file = "config.mk";
open my $conf_fh, '>', $conf_file or die "Cannot open $conf_file: $!";
print $conf_fh $config_str;
close $conf_fh;
sub mkdocs_version {
my $str = shift;
my $version = "";
$version = $1 if $str =~ /version (\d\.\d\.\d)/;
$version;
}
sub get_version {
my $version_h = shift;
open my $version_h_fh, '<', $version_h or die "Cannot open $version_h: $!";
my ($major, $minor, $micro);
while (<$version_h_fh>) {
$major = $1 if /MAJOR_VERSION \((\d)\)/;
$minor = $1 if /MINOR_VERSION \((\d)\)/;
$micro = $1 if /MICRO_VERSION \((\d)\)/;
}
close $version_h_fh;
if (not defined $major or not defined $minor or not defined $micro) {
&err_exit("cannot find version macros in $version_h, maybe a bug?");
}
($major, $minor, $micro);
}
sub set_prefix {
my $prefix = $_[1];
$opts{INSTALL_DIR} = $prefix;
$opts{PKGCONFIG_DIR} = "$prefix/lib/pkgconfig";
$opts{PYTHON_PREFIX} = $prefix;
}
sub get_python_lib_dir {
my $python_cmd = shift;
my $dir = `echo 'import distutils.sysconfig as conf
print(conf.get_python_lib())' | $python_cmd`;
chomp $dir;
$dir;
}
sub match_version {
my $input = shift;
my $version;
$version = $1 if $input =~ /([0-9a-z.]+)$/;
}
sub version_roughly_match {
my $ver1 = &match_version(shift);
my $ver2 = &match_version(shift);
if ($ver1 =~ /^$ver2/ or $ver2 =~ /^$ver1/) {
return 1;
} else {
return 0;
}
}
sub err_exit {
my $msg = shift;
print STDERR "\e[31mError:\e[0m $msg\n";
exit 1;
}
sub exit_msg {
my $code = shift;
my $msg = shift;
print $msg;
exit $code;
}
sub warn_msg {
my $msg = shift;
print STDERR "\e[35mWarning:\e[0m $msg\n";
}