File Coverage

blib/lib/FFI/CheckLib.pm
Criterion Covered Total %
statement 146 174 83.9
branch 67 96 69.7
condition 15 28 53.5
subroutine 18 23 78.2
pod 9 9 100.0
total 255 330 77.2


line stmt bran cond sub pod time code
1             package FFI::CheckLib;
2              
3 6     6   1202974 use strict;
  6         46  
  6         167  
4 6     6   27 use warnings;
  6         12  
  6         131  
5 6     6   24 use File::Spec;
  6         18  
  6         166  
6 6     6   30 use List::Util 1.33 qw( any );
  6         124  
  6         352  
7 6     6   39 use Carp qw( croak carp );
  6         16  
  6         246  
8 6     6   28 use base qw( Exporter );
  6         11  
  6         1255  
9              
10             our @EXPORT = qw(
11             find_lib
12             assert_lib
13             check_lib
14             check_lib_or_exit
15             find_lib_or_exit
16             find_lib_or_die
17             );
18              
19             our @EXPORT_OK = qw(
20             which
21             where
22             has_symbols
23             );
24              
25             # ABSTRACT: Check that a library is available for FFI
26             our $VERSION = '0.30'; # VERSION
27              
28              
29             our $system_path = [];
30             our $os ||= $^O;
31             my $try_ld_on_text = 0;
32              
33             if($os eq 'MSWin32' || $os eq 'msys')
34             {
35             $system_path = eval {
36             require Env;
37             Env->import('@PATH');
38             \our @PATH;
39             };
40             die $@ if $@;
41             }
42             else
43             {
44             $system_path = eval {
45             require DynaLoader;
46 6     6   39 no warnings 'once';
  6         8  
  6         12193  
47             \@DynaLoader::dl_library_path;
48             };
49             die $@ if $@;
50             }
51              
52             our $pattern = [ qr{^lib(.*?)\.so(?:\.([0-9]+(?:\.[0-9]+)*))?$} ];
53             our $version_split = qr/\./;
54              
55             if($os eq 'cygwin')
56             {
57             push @$pattern, qr{^cyg(.*?)(?:-([0-9])+)?\.dll$};
58             }
59             elsif($os eq 'msys')
60             {
61             # doesn't seem as though msys uses psudo libfoo.so files
62             # in the way that cygwin sometimes does. we can revisit
63             # this if we find otherwise.
64             $pattern = [ qr{^msys-(.*?)(?:-([0-9])+)?\.dll$} ];
65             }
66             elsif($os eq 'MSWin32')
67             {
68             # handle cases like libgeos-3-7-0___.dll, libproj_9_1.dll and libgtk-2.0-0.dll
69             $pattern = [ qr{^(?:lib)?(\w+?)(?:[_-]([0-9\-\._]+))?_*\.dll$}i ];
70             $version_split = qr/[_\-]/;
71             }
72             elsif($os eq 'darwin')
73             {
74             push @$pattern, qr{^lib(.*?)(?:\.([0-9]+(?:\.[0-9]+)*))?\.(?:dylib|bundle)$};
75             }
76             elsif($os eq 'linux')
77             {
78             if(-e '/etc/redhat-release' && -x '/usr/bin/ld')
79             {
80             $try_ld_on_text = 1;
81             }
82             }
83              
84             sub _matches
85             {
86 1562     1562   52564 my($filename, $path) = @_;
87              
88 1562         2083 foreach my $regex (@$pattern)
89             {
90             return [
91 2078 100       17697 $1, # 0 capture group 1 library name
    100          
92             File::Spec->catfile($path, $filename), # 1 full path to library
93             defined $2 ? (split $version_split, $2) : (), # 2... capture group 2 library version
94             ] if $filename =~ $regex;
95             }
96 537         1439 return ();
97             }
98              
99             sub _cmp
100             {
101 210     210   388 my($A,$B) = @_;
102              
103 210 100       460 return $A->[0] cmp $B->[0] if $A->[0] ne $B->[0];
104              
105 152         166 my $i=2;
106 152         161 while(1)
107             {
108 256 50 66     369 return 0 if !defined($A->[$i]) && !defined($B->[$i]);
109 256 100       365 return -1 if !defined $A->[$i];
110 246 100       443 return 1 if !defined $B->[$i];
111 118 100       254 return $B->[$i] <=> $A->[$i] if $A->[$i] != $B->[$i];
112 104         108 $i++;
113             }
114             }
115              
116              
117             my $diagnostic;
118              
119             sub _is_binary
120             {
121 0     0   0 -B $_[0]
122             }
123              
124             sub find_lib
125             {
126 80     80   138516 my(%args) = @_;
127              
128 80         121 undef $diagnostic;
129 80 50       185 croak "find_lib requires lib argument" unless defined $args{lib};
130              
131 80   100     330 my $recursive = $args{_r} || $args{recursive} || 0;
132              
133             # make arguments be lists.
134 80         148 foreach my $arg (qw( lib libpath symbol verify alien ))
135             {
136 400 100       647 next if ref $args{$arg} eq 'ARRAY';
137 380 100       527 if(defined $args{$arg})
138             {
139 94         191 $args{$arg} = [ $args{$arg} ];
140             }
141             else
142             {
143 286         497 $args{$arg} = [];
144             }
145             }
146              
147 80 50 33     177 if(defined $args{systempath} && !ref($args{systempath}))
148             {
149 0         0 $args{systempath} = [ $args{systempath} ];
150             }
151              
152 80         90 my @path = @{ $args{libpath} };
  80         125  
153 80 100       141 @path = map { _recurse($_) } @path if $recursive;
  2         5  
154 150         593 push @path, grep { defined } defined $args{systempath}
155 80 50       207 ? @{ $args{systempath} }
  0         0  
156             : @$system_path;
157              
158 80     83   220 my $any = any { $_ eq '*' } @{ $args{lib} };
  83         149  
  80         233  
159 80         180 my %missing = map { $_ => 1 } @{ $args{lib} };
  83         215  
  80         139  
160 80         114 my %symbols = map { $_ => 1 } @{ $args{symbol} };
  27         47  
  80         112  
161 80         96 my @found;
162              
163 80         130 delete $missing{'*'};
164              
165 80         116 alien: foreach my $alien (reverse @{ $args{alien} })
  80         138  
166             {
167 5 100       31 unless($alien =~ /^([A-Za-z_][A-Za-z_0-9]*)(::[A-Za-z_][A-Za-z_0-9]*)*$/)
168             {
169 1         191 croak "Doesn't appear to be a valid Alien name $alien";
170             }
171 4 100       4 unless(eval { $alien->can('dynamic_libs') })
  4         33  
172             {
173             {
174 3         5 my $pm = "$alien.pm";
  3         7  
175 3         9 $pm =~ s/::/\//g;
176 3         6 local $@ = '';
177 3         4 eval { require $pm };
  3         1020  
178 3 100       14 next alien if $@;
179             }
180 2 100       11 unless(eval { $alien->can('dynamic_libs') })
  2         15  
181             {
182 1         91 croak "Alien $alien doesn't provide a dynamic_libs method";
183             }
184             }
185 2         7 unshift @path, [$alien->dynamic_libs];
186             }
187              
188 78         111 foreach my $path (@path)
189             {
190 164 50 66     2804 next if ref $path ne 'ARRAY' && ! -d $path;
191              
192             my @maybe =
193             # make determinist based on names and versions
194 147         197 sort { _cmp($a,$b) }
195             # Filter out the items that do not match the name that we are looking for
196             # Filter out any broken symbolic links
197 957 100 100     4868 grep { ($any || $missing{$_->[0]} ) && (-e $_->[1]) }
198             ref $path eq 'ARRAY'
199             ? do {
200             map {
201 2         4 my($v, $d, $f) = File::Spec->splitpath($_);
  2         24  
202 2         16 _matches($f, File::Spec->catpath($v,$d,''));
203             } @$path;
204             }
205 164 100       484 : do {
206 162         181 my $dh;
207 162         3640 opendir $dh, $path;
208             # get [ name, full_path ] mapping,
209             # each entry is a 2 element list ref
210 162         3156 map { _matches($_,$path) } readdir $dh;
  1492         2479  
211             };
212              
213 164 0 33     686 if($try_ld_on_text && $args{try_linker_script})
214             {
215             # This is tested in t/ci.t only
216             @maybe = map {
217 0 0       0 -B $_->[1] ? $_ : do {
  0         0  
218 0         0 my($name, $so) = @$_;
219 0         0 my $output = `/usr/bin/ld -t $so -o /dev/null -shared`;
220 0 0       0 $output =~ /\((.*?lib.*\.so.*?)\)/
221             ? [$name, $1]
222             : die "unable to parse ld output";
223             }
224             } @maybe;
225             }
226              
227             midloop:
228 164         286 foreach my $lib (@maybe)
229             {
230 178 100 100     2083 next unless $any || $missing{$lib->[0]};
231              
232 111         135 foreach my $verify (@{ $args{verify} })
  111         184  
233             {
234 48 100       92 next midloop unless $verify->(@$lib);
235             }
236              
237 66         1353 delete $missing{$lib->[0]};
238              
239 66 100       104 if(%symbols)
240             {
241 12         67 require DynaLoader;
242 12         42 my $dll = DynaLoader::dl_load_file($lib->[1],0);
243 12         1211 foreach my $symbol (keys %symbols)
244             {
245 27 100       68 if(DynaLoader::dl_find_symbol($dll, $symbol) ? 1 : 0)
    100          
246             {
247 21         158 delete $symbols{$symbol}
248             }
249             }
250 12         47 DynaLoader::dl_unload_file($dll);
251             }
252              
253 66         143 my $found = $lib->[1];
254              
255 66 100       103 unless($any)
256             {
257 63         663 while(-l $found)
258             {
259 0         0 require File::Basename;
260 0         0 my $dir = File::Basename::dirname($found);
261 0         0 $found = File::Spec->rel2abs( readlink($found), $dir );
262             }
263             }
264              
265 66         254 push @found, $found;
266             }
267             }
268              
269 78 100       198 if(%missing)
    100          
270             {
271 17         57 my @missing = sort keys %missing;
272 17 50       44 if(@missing > 1)
273 0         0 { $diagnostic = "libraries not found: @missing" }
274             else
275 17         51 { $diagnostic = "library not found: @missing" }
276             }
277             elsif(%symbols)
278             {
279 6         19 my @missing = sort keys %symbols;
280 6 50       18 if(@missing > 1)
281 0         0 { $diagnostic = "symbols not found: @missing" }
282             else
283 6         22 { $diagnostic = "symbol not found: @missing" }
284             }
285              
286 78 100       167 return if %symbols;
287 72 100       769 return $found[0] unless wantarray;
288 47         248 return @found;
289             }
290              
291             sub _recurse
292             {
293 6     6   15 my($dir) = @_;
294 6 50       72 return unless -d $dir;
295 6         12 my $dh;
296 6         137 opendir $dh, $dir;
297 6         155 my @list = grep { -d $_ } map { File::Spec->catdir($dir, $_) } grep !/^\.\.?$/, readdir $dh;
  10         116  
  10         73  
298 6         55 closedir $dh;
299 6         26 ($dir, map { _recurse($_) } @list);
  4         19  
300             }
301              
302              
303             sub assert_lib
304             {
305 6 100 50 6 1 8244 croak $diagnostic || 'library not found' unless check_lib(@_);
306             }
307              
308              
309             sub check_lib_or_exit
310             {
311 0 0   0 1 0 unless(check_lib(@_))
312             {
313 0   0     0 carp $diagnostic || 'library not found';
314 0         0 exit;
315             }
316             }
317              
318              
319             sub find_lib_or_exit
320             {
321 0     0 1 0 my(@libs) = find_lib(@_);
322 0 0       0 unless(@libs)
323             {
324 0   0     0 carp $diagnostic || 'library not found';
325 0         0 exit;
326             }
327 0 0       0 return unless @libs;
328 0 0       0 wantarray ? @libs : $libs[0];
329             }
330              
331              
332             sub find_lib_or_die
333             {
334 0     0 1 0 my(@libs) = find_lib(@_);
335 0 0       0 unless(@libs)
336             {
337 0   0     0 croak $diagnostic || 'library not found';
338             }
339 0 0       0 return unless @libs;
340 0 0       0 wantarray ? @libs : $libs[0];
341             }
342              
343              
344             sub check_lib
345             {
346 12 100   12 1 7879 find_lib(@_) ? 1 : 0;
347             }
348              
349              
350             sub which
351             {
352 1     1 1 2544 my($name) = @_;
353 1 50       3 croak("cannot which *") if $name eq '*';
354 1         3 scalar find_lib( lib => $name );
355             }
356              
357              
358             sub where
359             {
360 2     2 1 5196 my($name) = @_;
361             $name eq '*'
362             ? find_lib(lib => '*')
363 2 100   0   9 : find_lib(lib => '*', verify => sub { $_[0] eq $name });
  0         0  
364             }
365              
366              
367             sub has_symbols
368             {
369 6     6 1 5996 my($path, @symbols) = @_;
370 6         26 require DynaLoader;
371 6         15 my $dll = DynaLoader::dl_load_file($path, 0);
372              
373 6         1141 my $ok = 1;
374              
375 6         15 foreach my $symbol (@symbols)
376             {
377 11 100       53 unless(DynaLoader::dl_find_symbol($dll, $symbol))
378             {
379 2         19 $ok = 0;
380 2         4 last;
381             }
382             }
383              
384 6         30 DynaLoader::dl_unload_file($dll);
385              
386 6         49 $ok;
387             }
388              
389              
390             sub system_path
391             {
392 1     1 1 2006 $system_path;
393             }
394              
395             1;
396              
397             __END__