-
Notifications
You must be signed in to change notification settings - Fork 21
/
Makefile.PL
330 lines (273 loc) · 10 KB
/
Makefile.PL
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
BEGIN {
unshift @INC, '.' unless -d ".git";
}
use inc::Module::Install 1.17;
use Cwd;
use Config;
use File::Glob qw(bsd_glob);
use strict;
use warnings;
my $win32 = ($^O eq 'MSWin32');
my $darwin = ($^O eq 'darwin');
my $solaris = ($^O eq 'solaris');
my $linux = ($^O eq 'linux');
my $cc_gcc = $Config{cc} =~ /gcc/;
my $cc_wincl = $win32 && $Config{cc} =~ /cl/;
my $path_sep = $Config{path_sep};
print <<'EOF';
The libssh2 library is required by this module. If you don't have it, you can
download it from http://www.libssh2.org; you may also need OpenSSL, which can
be obtained from http://www.openssl.org , or libgcrypt, which can be obtained
from http://www.gnupg.org .
Debian: sudo aptitude install libssh2-1-dev
OpenSUSE: sudo zypper in libssh2-1 libssh2-devel
You can pass your libssh2 lib and include dirs (and extra link args) on the
command line. E.g.:
perl Makefile.PL lib=$HOME/libssh2/lib inc=$HOME/libssh2/include \
ldargs="-lz"
These can also be set through the LIBSSH2_LIB/LIBSSH2_INCLUDE/LIBSSH2_LDARGS
environment variables.
To build with libgcrypt instead of OpenSSL, use the crypto_backend
option when calling Makefile.PL, e.g.:
perl Makefile.PL crypto_backend=gcrypt
EOF
my $crypto_backend;
my $do_not_check_libs;
sub makemaker_append_once;
sub capture;
sub findlib;
name 'Net-SSH2';
all_from 'lib/Net/SSH2.pm';
perl_version '5.006000';
auto_provides;
makemaker_append_once CCFLAGS => $Config{ccflags};
makemaker_append_once LDDLFLAGS => $Config{lddlflags};
# On MSYS2 there are issues finding system DLL import libs.
makemaker_append_once LIBS => ':nosearch' if $win32;
my %arg2mmappend = ( inc => 'INC',
lib => 'LIBS',
ldargs => 'LDDLFLAGS' );
my %arg2prefix = (inc => '-I',
lib => '-L');
my $arg_keys = join('|', map quotemeta, keys %arg2mmappend);
my $do_not_check_libs_keys = 'lib|inc';
for (keys %ENV) {
if (/^LIBSSH2_($arg_keys)$/) {
my $value = $ENV{$_};
my $prefix = $arg2prefix{lc $1} || '';
$value = File::Spec->rel2abs($value) if $prefix;
makemaker_append_once $arg2mmappend{lc $1} => $prefix . $value;
$do_not_check_libs ||= /^LIBSSH2_($do_not_check_libs_keys)$/i;
}
}
my @pass_through_args;
for (@ARGV) {
if (/^($arg_keys)=(.*)\z/) {
my $prefix = $arg2prefix{$1} || '';
my $value = $2;
$value = File::Spec->rel2abs($value) if $prefix;
makemaker_append_once $arg2mmappend{$1} => $prefix.$value;
$do_not_check_libs ||= /^($do_not_check_libs_keys)=/;
}
elsif (/^crypto_backend=(.*)$/) {
$crypto_backend = $1;
}
else {
push @pass_through_args, $_;
}
}
@ARGV = @pass_through_args;
my @search_paths;
for my $env ('LD_RUN_PATH', ($darwin ? 'DYLD_LIBRARY_PATH' : 'LD_LIBRARY_PATH')) {
my $v = $ENV{$env};
push @search_paths, split(/\Q$path_sep\E/, $v) if defined $v;
}
push @search_paths,
split(' ', $Config{libspath}),
$Config{siteprefixexp}, $Config{prefixexp},
'/usr', '/usr/local', '/opt', '/opt/local',
'/usr/local/libssh2', '/opt/libssh2',
'/usr/local/libssh2/*', '/usr/local/ssl';
push @search_paths, $ENV{HOME}, "$ENV{HOME}/libssh2" if defined $ENV{HOME};
# mac homebrew support
if ($^O eq 'darwin' && (qx`command -v brew`)[0]) {
if(system("brew info libssh2 | grep '^Not installed' 2>&1 >/dev/null") >> 8 == 0) {
system("brew -v install libssh2");
}
if(system("brew info openssl | grep '^Not installed' 2>&1 >/dev/null") >> 8 == 0) {
system("brew -v install openssl");
}
for(qw/openssl libssh2/) {
push @search_paths, map { chomp; $_ } (qx`brew --prefix $_`)[0];
}
}
@search_paths = map { /\*/
? (sort { (stat $b)[9] <=> (stat $a)[9] } grep -d, bsd_glob($_))
: $_ } @search_paths;
my %sp_seen;
@search_paths = grep !$sp_seen{$_}++, @search_paths;
sub findlib {
local $Module::Install::AUTHOR;
my %args = @_;
my @search_paths = @{delete $args{search_paths} || []};
for my $sp (undef, @search_paths) {
for my $libpath (defined $sp ? (map [$_], grep -d, "$sp/lib", "$sp/lib64", $sp) : []) {
my $incpath = (defined $sp ? [grep -d, "$sp/include"] : []);
my @ldflags = '';
push @ldflags, "-Wl,-rpath=".$libpath->[0] if $cc_gcc and defined $sp;
for my $ldflags (@ldflags) {
if (eval {
assertlibs( %args,
libpath => $libpath,
incpath => $incpath,
ldflags => $ldflags);
1; }) {
warn "Library $args{lib} found in ".(defined $sp ? $sp : "standard place")."\n";
makemaker_append_once INC => "-I$_" for @$incpath;
makemaker_append_once LIBS => "-L$_" for @$libpath;
makemaker_append_once LIBS => "-l$args{lib}";
makemaker_append_once LDDLFLAGS => $ldflags if $ldflags;
return 1;
}
else {
warn $@ if $@ and $ENV{AUTOMATED_TESTING};
}
}
}
}
die "Unable to find a working version of library $args{lib} in the following directories:\n ".
(join "\n ", @search_paths)."\n";
}
if ($do_not_check_libs) {
$crypto_backend ||= 'openssl';
}
else {
warn "Looking for libraries...\n";
my $detected_crypto_backend;
# This is a workaround for Module::Install::CheckLib behaving
# differently in user and author mode. Specifically, in author
# mode, we call M::I::CheckLib normally so that it can copy
# its files under "inc/", and then, we hide we are in author
# mode and call it again so that it actually runs the
# checklibs stuff..
if ($Module::Install::AUTHOR) {
eval {
assertlibs( lib => 'ssh2',
header => 'libssh2.h',
libpath => [],
incpath => []);
};
}
findlib(lib => 'ssh2',
header => 'libssh2.h',
search_paths => \@search_paths,
function => <<EOF,
if (libssh2_init(0))
return 1;
/* check that library and headers are on par */
return (libssh2_version(LIBSSH2_VERSION_NUM) ? 0 : 1);
EOF
analyze_binary => sub {
warn "Analyzing file $_[1]\n";
local $@;
eval {
my $out = capture ldd => $_[1];
# warn "out:\n$out";
if ($out =~ /gcrypt/i) {
$detected_crypto_backend = 'gcrypt';
}
elsif ($out =~ /lib(ssl|crypto)/i) {
$detected_crypto_backend = 'openssl';
}
else {
warn "Unable to detect crypto backend used by libssh2!\n"
}
};
warn $@ if $@ and $ENV{AUTOMATED_TESTING};
1;
},
run_checks_as_author => 1, );
if (defined $detected_crypto_backend) {
if (defined $crypto_backend) {
$crypto_backend eq $detected_crypto_backend or
warn "Detected crypto backend ($detected_crypto_backend) does not match ".
"the one requested on the command line ($crypto_backend)";
}
else {
warn "Detected crypto backend: $detected_crypto_backend\n";
$crypto_backend = $detected_crypto_backend;
}
}
$crypto_backend ||= 'openssl';
warn "crypto backend set to $crypto_backend\n";
findlib(lib => 'z',
header => 'zlib.h',
search_paths => \@search_paths,
run_checks_as_author => 1);
if ($crypto_backend eq 'gcrypt') {
findlib(lib => 'gcrypt',
header => 'gcrypt.h',
search_paths => \@search_paths,
run_checks_as_author => 1);
}
else {
findlib(lib => 'ssl',
header => 'openssl/ssl.h',
search_paths => \@search_paths,
run_checks_as_author => 1);
findlib(lib => 'crypto',
header => 'openssl/crypto.h',
search_paths => \@search_paths,
run_checks_as_author => 1);
}
}
makemaker_append_once LIBS => '-lssh2', '-lz';
if ($crypto_backend eq 'gcrypt') {
makemaker_append_once(LDDLFLAGS => '-lgcrypt');
}
else {
makemaker_append_once(LIBS => "-l$_") for qw(ssl crypto);
}
makemaker_append_once(DEFINE => '-DLIBSSH2_WIN32') if $win32;
makemaker_append_once(DEFINE => '-DUSE_GCRYPT') if $crypto_backend eq 'gcrypt';
makemaker_append_once(CCFLAGS => '-Wno-deprecated-declarations') if $^O eq 'darwin';
makemaker_append_once(CCFLAGS => '-DPERL_GCC_PEDANTIC -std=c11 -pedantic-errors -Wno-long-long')
if $Module::Install::AUTHOR and $linux and $cc_gcc;
resources repository => 'git://github.com/rkitover/net-ssh2.git';
resources bugtracker => 'https://github.com/rkitover/net-ssh2/issues';
my $gen = "util/gen_constants.pl";
if (-f $gen) {
system $^X, $gen
and warn "$gen failed: $?\n";
}
WriteAll;
# Generate README.pod
if ($Module::Install::AUTHOR and -d '.git') {
require Pod::Simple::JustPod;
my $parser = Pod::Simple::JustPod->new;
$parser->output_string(\my $out_str);
$parser->parse_file('lib/Net/SSH2.pm');
open my $out_fh, '>README.pod' or die "Can't open README.pod for writing: $!";
print $out_fh $out_str;
close $out_fh;
}
my %appended;
sub makemaker_append_once {
my $key = shift;
for (@_) {
makemaker_append $key, $_
unless $appended{$key}{$_}++;
}
}
sub capture {
# Ignore not found errors.
open(my $olderr, '>&STDERR') or die "can't dup STDERR: $!";
close STDERR;
my $out = '';
if (open my $fh, '-|', @_) {
$out = do { local $/, <$fh> };
close $fh;
}
open(STDERR, '>&', $olderr) or die "can't dup STDERR: $!";
return $out;
}