blib/lib/Devel/CheckLib.pm

filter: Statementstmt 45.1% 172 / 381 Branchbran 25.3% 65 / 256 Conditioncond 30.2% 29 / 96 MC/DCmcdc 23.2% 13 / 56 Subroutinesub 63.3% 19 / 30 Podpod 75.0% 6 / 8 totaltotal 36.7% 304 / 827 CC 189 SCAR 52.0
CC
189
Cov
36%
assert_lib
381.7
assert_lib
381.7
_parse_line
166.2
CRAP 180.9
↑ Summary
1 # $Id: CheckLib.pm,v 1.25 2008/10/27 12:16:23 drhyde Exp $
2  
3 package Devel::CheckLib;
4  
5 12 ▶use 5.00405; #postfix foreach
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 $VERSION = '1.16';
9 12 ▶use Config qw(%Config);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Text::ParseWords qw(quotewords shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 require Exporter;
16 11 @ISA = qw(Exporter);
17 11 @EXPORT = qw(assert_lib check_lib_or_exit check_lib);
18  
19 # localising prevents the warningness leaking out of this module
20 11 local $^W = 1; # use warnings is a 5.6-ism
21  
22 11 _findcc(); # bomb out early if there's no compiler
23  
24 =head1 NAME
25  
26 Devel::CheckLib - check that a library is available
27  
28 =head1 DESCRIPTION
29  
30 Devel::CheckLib is a perl module that checks whether a particular C
31 library and its headers are available.
32  
33 =head1 SYNOPSIS
34  
35 use Devel::CheckLib;
36  
37 check_lib_or_exit( lib => 'jpeg', header => 'jpeglib.h' );
38 check_lib_or_exit( lib => [ 'iconv', 'jpeg' ] );
39
40 # or prompt for path to library and then do this:
41 check_lib_or_exit( lib => 'jpeg', libpath => $additional_path );
42  
43 =head1 USING IT IN Makefile.PL or Build.PL
44  
45 If you want to use this from Makefile.PL or Build.PL, do
46 not simply copy the module into your distribution as this may cause
47 problems when PAUSE and search.cpan.org index the distro. Instead, use
48 the use-devel-checklib script.
49  
50 =head1 HOW IT WORKS
51  
52 You pass named parameters to a function, describing to it how to build
53 and link to the libraries.
54  
55 It works by trying to compile some code - which defaults to this:
56  
57 int main(int argc, char *argv[]) { return 0; }
58  
59 and linking it to the specified libraries. If something pops out the end
60 which looks executable, it gets executed, and if main() returns 0 we know
61 that it worked. That tiny program is
62 built once for each library that you specify, and (without linking) once
63 for each header file.
64  
65 If you want to check for the presence of particular functions in a
66 library, or even that those functions return particular results, then
67 you can pass your own function body for main() thus:
68  
69 check_lib_or_exit(
70 function => 'foo();if(libversion() > 5) return 0; else return 1;'
71 incpath => ...
72 libpath => ...
73 lib => ...
74 header => ...
75 );
76  
77 In that case, it will fail to build if either foo() or libversion() don't
78 exist, and main() will return the wrong value if libversion()'s return
79 value isn't what you want.
80  
81 =head1 FUNCTIONS
82  
83 All of these take the same named parameters and are exported by default.
84 To avoid exporting them, C<use Devel::CheckLib ()>.
85  
86 =head2 assert_lib
87  
88 This takes several named parameters, all of which are optional, and dies
89 with an error message if any of the libraries listed can
90 not be found. B<Note>: dying in a Makefile.PL or Build.PL may provoke
91 a 'FAIL' report from CPAN Testers' automated smoke testers. Use
92 C<check_lib_or_exit> instead.
93  
94 The named parameters are:
95  
96 =over
97  
98 =item lib
99  
100 Must be either a string with the name of a single
101 library or a reference to an array of strings of library names. Depending
102 on the compiler found, library names will be fed to the compiler either as
103 C<-l> arguments or as C<.lib> file names. (E.g. C<-ljpeg> or C<jpeg.lib>)
104  
105 =item libpath
106  
107 a string or an array of strings
108 representing additional paths to search for libraries.
109  
110 =item LIBS
111  
112 a C<ExtUtils::MakeMaker>-style space-separated list of
113 libraries (each preceded by '-l') and directories (preceded by '-L').
114  
115 This can also be supplied on the command-line.
116  
117 =item debug
118  
119 If true - emit information during processing that can be used for
120 debugging.
121  
122 =back
123  
124 And libraries are no use without header files, so ...
125  
126 =over
127  
128 =item header
129  
130 Must be either a string with the name of a single
131 header file or a reference to an array of strings of header file names.
132  
133 =item incpath
134  
135 a string or an array of strings
136 representing additional paths to search for headers.
137  
138 =item INC
139  
140 a C<ExtUtils::MakeMaker>-style space-separated list of
141 incpaths, each preceded by '-I'.
142  
143 This can also be supplied on the command-line.
144  
145 =item ccflags
146  
147 Extra flags to pass to the compiler.
148  
149 =item ldflags
150  
151 Extra flags to pass to the linker.
152  
153 =item analyze_binary
154  
155 a callback function that will be invoked in order to perform custom
156 analysis of the generated binary. The callback arguments are the
157 library name and the path to the binary just compiled.
158  
159 It is possible to use this callback, for instance, to inspect the
160 binary for further dependencies.
161  
162 =item not_execute
163  
164 Do not try to execute generated binary. Only check that compilation has not failed.
165  
166 =back
167  
168 =head2 check_lib_or_exit
169  
170 This behaves exactly the same as C<assert_lib()> except that instead of
171 dieing, it warns (with exactly the same error message) and exits.
172 This is intended for use in Makefile.PL / Build.PL
173 when you might want to prompt the user for various paths and
174 things before checking that what they've told you is sane.
175  
176 If any library or header is missing, it exits with an exit value of 0 to avoid
177 causing a CPAN Testers 'FAIL' report. CPAN Testers should ignore this
178 result -- which is what you want if an external library dependency is not
179 available.
180  
181 =head2 check_lib
182  
183 This behaves exactly the same as C<assert_lib()> except that it is silent,
184 returning false instead of dieing, or true otherwise.
185  
186 =cut
187  
188 1 ▶sub check_lib_or_exit {
Subroutine check_lib_or_exit: called CC=2 SCAR=10.3
Pod documented
189 0 ▶ eval 'assert_lib(@_)';
Subroutine check_lib_or_exit: not called CC=2 SCAR=10.3
Pod documented
190 0 ▶ if($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 1 0 1
if ($@) 0 0 0
191 0 warn $@;
192 0 exit;
193 }
194 }
195  
196 8 ▶sub check_lib {
Subroutine check_lib: called CC=2 SCAR=9.2
Pod documented
197 0 ▶ eval 'assert_lib(@_)';
Subroutine check_lib: not called CC=2 SCAR=9.2
Pod documented
198 0 ▶ return $@ ? 0 : 1;
Branch: 2 branches
BranchTrueFalseTotal
$@ ? : 4 4 8
$@ ? : 0 0 0
199 }
200  
201 # borrowed from Text::ParseWords
202 32 ▶sub _parse_line {
Subroutine _parse_line: called CC=14 SCAR=51.1
203 0 ▶ my($delimiter, $keep, $line) = @_;
Subroutine _parse_line: not called CC=14 SCAR=51.1
204 0 my($word, @pieces);
205  
206 12 ▶ no warnings 'uninitialized'; # we will be testing undef strings
Subroutine BEGIN: called CC=1 SCAR=0.0
207  
208 0 ▶ while (length($line)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $line) 0 32 32
if (length $line) 0 0 0
209 # This pattern is optimised to be stack conservative on older perls.
210 # Do not refactor without being careful and testing it on very long strings.
211 # See Perl bug #42980 for an example of a stack busting input.
212 0 ▶ $line =~ s/^
Branch: 2 branches
BranchTrueFalseTotal
$line =~ s/^ (?: # double quoted string (") # $quote ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted | # --OR-- # singe quoted string (') # $quote ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted | # --OR-- # unquoted string ( # $unquoted (?:\\.|[^\\"'])*? ) # followed by ( # $delim \Z(?!\n) # EOL | # --OR-- (?-x:$delimiter) # delimiter | # --OR-- (?!^)(?=["']) # a quote ) )//sx or return 0 0 0
$line =~ s/^ (?: # double quoted string (") # $quote ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted | # --OR-- # singe quoted string (') # $quote ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted | # --OR-- # unquoted string ( # $unquoted (?:\\.|[^\\"'])*? ) # followed by ( # $delim \Z(?!\n) # EOL | # --OR-- (?-x:$delimiter) # delimiter | # --OR-- (?!^)(?=["']) # a quote ) )//sx or return 0 0 0
213 (?:
214 # double quoted string
215 (") # $quote
216 ((?>[^\\"]*(?:\\.[^\\"]*)*))" # $quoted
217 | # --OR--
218 # singe quoted string
219 (') # $quote
220 ((?>[^\\']*(?:\\.[^\\']*)*))' # $quoted
221 | # --OR--
222 # unquoted string
223 ( # $unquoted
224 (?:\\.|[^\\"'])*?
225 )
226 # followed by
227 ( # $delim
228 \Z(?!\n) # EOL
229 | # --OR--
230 (?-x:$delimiter) # delimiter
231 | # --OR--
232 (?!^)(?=["']) # a quote
233 )
234 )//xs or return; # extended layout
235 0 ▶ my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6);
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$1 ? : 0 0 0
236  
237 0 ▶ return() unless( defined($quote) || length($unquoted) || length($delim));
Branch: 2 branches
BranchTrueFalseTotal
unless defined $quote or length $unquoted or length $delim 0 0 0
unless defined $quote or length $unquoted or length $delim 0 0 0
Condition0%0/12
defined $quote or length $unquoted or length $delim
l!l&&r!l&&!r
000
defined $quote or length $unquoted
l!l&&r!l&&!r
000
defined $quote or length $unquoted or length $delim
l!l&&r!l&&!r
000
defined $quote or length $unquoted
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $quote or length $unquoted or length $delim
defined $quote length $unquoted length $delim
MC/DC0%0/3
defined $quote or length $unquoted or length $delim
defined $quote length $unquoted length $delim
Truth table0%0/4
A or B or C
A= defined $quoteB= length $unquotedC= length $delim
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/4
A or B or C
A= defined $quoteB= length $unquotedC= length $delim
ABCresult
1XX1
01X1
0011
0000
238  
239 0 ▶ if ($keep) {
Branch: 2 branches
BranchTrueFalseTotal
if ($keep) { } 0 0 0
if ($keep) { } 0 0 0
240 0 $quoted = "$quote$quoted$quote";
241 }
242 else {
243 0 $unquoted =~ s/\\(.)/$1/sg;
244 0 ▶ if (defined $quote) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $quote) 0 0 0
if (defined $quote) 0 0 0
245 0 ▶ $quoted =~ s/\\(.)/$1/sg if ($quote eq '"');
Branch: 2 branches
BranchTrueFalseTotal
if $quote eq "\"" 0 0 0
if $quote eq "\"" 0 0 0
246 }
247 }
248 0 $word .= substr($line, 0, 0); # leave results tainted
249 0 ▶ $word .= defined $quote ? $quoted : $unquoted;
Branch: 2 branches
BranchTrueFalseTotal
defined $quote ? : 0 0 0
defined $quote ? : 0 0 0
250  
251 0 ▶ if (length($delim)) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $delim) 0 0 0
if (length $delim) 0 0 0
252 0 push(@pieces, $word);
253 0 ▶ push(@pieces, $delim) if ($keep eq 'delimiters');
Branch: 2 branches
BranchTrueFalseTotal
if $keep eq "delimiters" 0 0 0
if $keep eq "delimiters" 0 0 0
254 0 undef $word;
255 }
256 0 ▶ if (!length($line)) {
Branch: 2 branches
BranchTrueFalseTotal
unless (length $line) 0 0 0
unless (length $line) 0 0 0
257 0 push(@pieces, $word);
258 }
259 }
260 0 return(@pieces);
261 }
262  
263 132 ▶sub _parsewords {
Subroutine _parsewords: called CC=3 SCAR=22.6
264 0 ▶ return shellwords @_ if $^O ne 'MSWin32';
Subroutine _parsewords: not called CC=3 SCAR=22.6
Branch: 2 branches
BranchTrueFalseTotal
if $^O ne "MSWin32" 132 0 132
if $^O ne "MSWin32" 0 0 0
265 # for Win32, take off "" but leave \
266 0 ▶ map { my $s=$_; $s =~ s/^"(.*)"$/$1/; $s } grep defined && length, quotewords '\s+', 1, @_;
Condition0%0/6
defined $_ && length $_
!ll&&!rl&&r
000
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
267 }
268  
269 49 ▶sub _compile_cmd {
Subroutine _compile_cmd: called CC=8 SCAR=36.3
270 0 ▶ my ($Config_cc, $cc, $cfile, $exefile, $incpaths, $ld, $Config_libs, $lib, $libpaths) = @_;
Subroutine _compile_cmd: not called CC=8 SCAR=36.3
271 0 my @sys_cmd = @$cc;
272 0 ▶ if ( $Config_cc eq 'cl' ) { # Microsoft compiler
Branch: 4 branches
BranchTrueFalseTotal
if ($Config_cc eq 'cl') { } 0 49 49
elsif ($Config_cc =~ /bcc32(\.exe)?/) { } 0 49 49
if ($Config_cc eq 'cl') { } 0 0 0
elsif ($Config_cc =~ /bcc32(\.exe)?/) { } 0 0 0
273 # this is horribly sensitive to the order of arguments
274 0 ▶ push @sys_cmd,
Branch: 4 branches
BranchTrueFalseTotal
defined $lib ? : 0 0 0
defined $lib ? : 0 0 0
defined $lib ? : 0 0 0
defined $lib ? : 0 0 0
275 $cfile,
276 (defined $lib ? "${lib}.lib" : ()),
277 "/Fe$exefile",
278 (map '/I'.$_, @$incpaths),
279 "/link",
280 @$ld,
281 _parsewords($Config_libs),
282 (defined $lib ? map '/libpath:'.$_, @$libpaths : ()),
283 ;
284 } elsif($Config_cc =~ /bcc32(\.exe)?/) { # Borland
285 0 ▶ push @sys_cmd,
Branch: 2 branches
BranchTrueFalseTotal
defined $lib ? : 0 0 0
defined $lib ? : 0 0 0
286 @$ld,
287 (map "-I$_", @$incpaths),
288 "-o$exefile",
289 (defined $lib ? ((map "-L$_", @$libpaths), "-l$lib") : ()),
290 $cfile,
291 ;
292 } else { # Unix-ish: gcc, Sun, AIX (gcc, cc), ...
293 push @sys_cmd,
294 (map "-I$_", @$incpaths),
295 $cfile,
296 (!defined $lib ? () : (
297 (map "-L$_", @$libpaths),
298 0 ▶ ($^O eq 'darwin' ? (map { "-Wl,-rpath,$_" } @$libpaths) : ()),
Branch: 4 branches
BranchTrueFalseTotal
!defined($lib) ? : 13 36 49
$^O eq 'darwin' ? : 0 36 36
!defined($lib) ? : 0 0 0
$^O eq 'darwin' ? : 0 0 0
299 "-l$lib",
300 )),
301 @$ld,
302 "-o", $exefile,
303 ;
304 }
305 0 @sys_cmd;
306 }
307  
308 44 ▶sub _make_cfile {
Subroutine _make_cfile: called CC=4 SCAR=19.9
309 0 ▶ my ($use_headers, $function, $debug) = @_;
Subroutine _make_cfile: not called CC=4 SCAR=19.9
310 0 my $code = '';
311 0 $code .= qq{#include <$_>\n} for @$use_headers;
312 0 ▶ $code .= "int main(int argc, char *argv[]) { ".($function || 'return 0;')." }\n";
Condition50%2/4
$function || 'return 0;'
l!l
638
$function || 'return 0;'
l!l
00
MC/DC100%1/1
$function || 'return 0;'
$function
MC/DC0%0/1
$function || 'return 0;'
$function
Truth table100%2/2
A
A= $function
Aresult
11
00
Truth table0%0/2
A
A= $function
Aresult
11
00
313 0 ▶ if ($debug) {
Branch: 2 branches
BranchTrueFalseTotal
if ($debug) 0 44 44
if ($debug) 0 0 0
314 0 (my $c = $code) =~ s:^:# :gm;
315 0 warn "# Code:\n$c\n";
316 }
317 0 my ($ch, $cfile) = File::Temp::tempfile(
318 'assertlibXXXXXXXX', SUFFIX => '.c'
319 );
320 0 print $ch $code;
321 0 close $ch;
322 0 (my $ofile = $cfile) =~ s/\.c$/$Config{_o}/;
323 0 ($cfile, $ofile);
324 }
325  
326 32 ▶sub assert_lib {
Subroutine assert_lib: called CC=42 SCAR=59.4
Pod documented
327 0 ▶ my %args = @_;
Subroutine assert_lib: not called CC=42 SCAR=59.4
Pod documented
328 $args{$_} = [$args{$_}]
329 0 ▶ for grep $args{$_} && !ref($args{$_}), qw(lib libpath header incpath);
Condition50%3/6
$args{$_} && !ref($args{$_})
!ll&&!rl&&r
65954
$args{$_} && !ref($args{$_})
!ll&&!rl&&r
000
MC/DC100%2/2
$args{$_} && !ref($args{$_})
$args{$_} !ref($args{$_})
MC/DC0%0/2
$args{$_} && !ref($args{$_})
$args{$_} !ref($args{$_})
Truth table100%3/3
A && not(B)
A= $args{$_}B= !ref($args{$_})
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= $args{$_}B= !ref($args{$_})
ABresult
0X0
100
111
330 0 ▶ my @libs = @{$args{lib} || []};
Condition50%2/4
$args{'lib'} || []
l!l
266
$args{'lib'} || []
l!l
00
MC/DC100%1/1
$args{'lib'} || []
$args{'lib'}
MC/DC0%0/1
$args{'lib'} || []
$args{'lib'}
Truth table100%2/2
A
A= $args{'lib'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'lib'}
Aresult
11
00
331 0 ▶ my @libpaths = @{$args{libpath} || []};
Condition50%2/4
$args{'libpath'} || []
l!l
1121
$args{'libpath'} || []
l!l
00
MC/DC100%1/1
$args{'libpath'} || []
$args{'libpath'}
MC/DC0%0/1
$args{'libpath'} || []
$args{'libpath'}
Truth table100%2/2
A
A= $args{'libpath'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'libpath'}
Aresult
11
00
332 0 ▶ my @headers = @{$args{header} || []};
Condition50%2/4
$args{'header'} || []
l!l
1418
$args{'header'} || []
l!l
00
MC/DC100%1/1
$args{'header'} || []
$args{'header'}
MC/DC0%0/1
$args{'header'} || []
$args{'header'}
Truth table100%2/2
A
A= $args{'header'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'header'}
Aresult
11
00
333 0 ▶ my @incpaths = @{$args{incpath} || []};
Condition50%2/4
$args{'incpath'} || []
l!l
1220
$args{'incpath'} || []
l!l
00
MC/DC100%1/1
$args{'incpath'} || []
$args{'incpath'}
MC/DC0%0/1
$args{'incpath'} || []
$args{'incpath'}
Truth table100%2/2
A
A= $args{'incpath'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'incpath'}
Aresult
11
00
334 0 my $analyze_binary = $args{analyze_binary};
335 0 my $execute = !$args{not_execute};
336  
337 0 my @argv = @ARGV;
338 0 ▶ push @argv, _parse_line('\s+', 0, $ENV{PERL_MM_OPT}||'');
Condition25%1/4
$ENV{'PERL_MM_OPT'} || ''
l!l
032
$ENV{'PERL_MM_OPT'} || ''
l!l
00
MC/DC0%0/1
$ENV{'PERL_MM_OPT'} || ''
$ENV{'PERL_MM_OPT'}
MC/DC0%0/1
$ENV{'PERL_MM_OPT'} || ''
$ENV{'PERL_MM_OPT'}
Truth table50%1/2
A
A= $ENV{'PERL_MM_OPT'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'PERL_MM_OPT'}
Aresult
11
00
339  
340 # work-a-like for Makefile.PL's LIBS and INC arguments
341 # if given as command-line argument, append to %args
342 0 for my $arg (@argv) {
343 0 for my $mm_attr_key (qw(LIBS INC)) {
344 0 ▶ if (my ($mm_attr_value) = $arg =~ /\A $mm_attr_key = (.*)/x) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($mm_attr_value) = $arg =~ /\A $mm_attr_key = (.*)/x) 0 0 0
if (my($mm_attr_value) = $arg =~ /\A $mm_attr_key = (.*)/x) 0 0 0
345 # it is tempting to put some \s* into the expression, but the
346 # MM command-line parser only accepts LIBS etc. followed by =,
347 # so we should not be any more lenient with whitespace than that
348 0 $args{$mm_attr_key} .= " $mm_attr_value";
349 }
350 }
351 }
352  
353 0 ▶ if(defined($args{LIBS})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{"LIBS"}) 1 31 32
if (defined $args{"LIBS"}) 0 0 0
354 0 foreach my $arg (_parsewords($args{LIBS})) {
355 0 ▶ die("LIBS argument badly-formed: $arg\n") unless($arg =~ /^-[lLR]/);
Branch: 2 branches
BranchTrueFalseTotal
unless $arg =~ /^-[lLR]/ 0 3 3
unless $arg =~ /^-[lLR]/ 0 0 0
356 0 ▶ push @{$arg =~ /^-l/ ? \@libs : \@libpaths}, substr($arg, 2);
Branch: 2 branches
BranchTrueFalseTotal
$arg =~ /^-l/ ? : 2 1 3
$arg =~ /^-l/ ? : 0 0 0
357 }
358 }
359 0 ▶ if(defined($args{INC})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{"INC"}) 2 30 32
if (defined $args{"INC"}) 0 0 0
360 0 foreach my $arg (_parsewords($args{INC})) {
361 0 ▶ die("INC argument badly-formed: $arg\n") unless($arg =~ /^-I/);
Branch: 2 branches
BranchTrueFalseTotal
unless $arg =~ /^-I/ 1 2 3
unless $arg =~ /^-I/ 0 0 0
362 0 push @incpaths, substr($arg, 2);
363 }
364 }
365  
366 0 my ($cc, $ld) = _findcc($args{debug}, $args{ccflags}, $args{ldflags});
367 0 my @missing;
368 my @wrongresult;
369 0 my @wronganalysis;
370 0 my @use_headers;
371  
372 # first figure out which headers we can't find ...
373 0 for my $header (@headers) {
374 0 push @use_headers, $header;
375 0 my ($cfile, $ofile) = _make_cfile(\@use_headers, '', $args{debug});
376 0 my $exefile = File::Temp::mktemp( 'assertlibXXXXXXXX' ) . $Config{_exe};
377 0 my @sys_cmd = _compile_cmd($Config{cc}, $cc, $cfile, $exefile, \@incpaths, $ld, $Config{libs});
378 0 ▶ warn "# @sys_cmd\n" if $args{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"debug"} 0 13 13
if $args{"debug"} 0 0 0
379 0 ▶ my $rv = $args{debug} ? system(@sys_cmd) : _quiet_system(@sys_cmd);
Branch: 2 branches
BranchTrueFalseTotal
$args{'debug'} ? : 0 13 13
$args{'debug'} ? : 0 0 0
380 0 ▶ push @missing, $header if $rv != 0 || ! -f $exefile;
Branch: 2 branches
BranchTrueFalseTotal
if $rv != 0 or not -f $exefile 1 12 13
if $rv != 0 or not -f $exefile 0 0 0
Condition33%2/6
$rv != 0 or not -f $exefile
l!l&&r!l&&!r
1012
$rv != 0 or not -f $exefile
l!l&&r!l&&!r
000
MC/DC50%1/2
$rv != 0 or not -f $exefile
$rv != 0 not -f $exefile
MC/DC0%0/2
$rv != 0 or not -f $exefile
$rv != 0 not -f $exefile
Truth table66%2/3
A or not(B)
A= $rv != 0B= not -f $exefile
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $rv != 0B= not -f $exefile
ABresult
1X1
011
000
381 0 _cleanup_exe($exefile);
382 0 unlink $cfile;
383 }
384  
385 # now do each library in turn with headers
386 0 my ($cfile, $ofile) = _make_cfile(\@use_headers, @args{qw(function debug)});
387 0 for my $lib ( @libs ) {
388 0 ▶ last if $Config{cc} eq 'CC/DECC'; # VMS
Branch: 2 branches
BranchTrueFalseTotal
if $Devel::CheckLib::Config{"cc"} eq "CC/DECC" 0 36 36
if $Devel::CheckLib::Config{"cc"} eq "CC/DECC" 0 0 0
389 0 my $exefile = File::Temp::mktemp( 'assertlibXXXXXXXX' ) . $Config{_exe};
390 0 my @sys_cmd = _compile_cmd($Config{cc}, $cc, $cfile, $exefile, \@incpaths, $ld, $Config{libs}, $lib, \@libpaths);
391 0 ▶ warn "# @sys_cmd\n" if $args{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"debug"} 0 36 36
if $args{"debug"} 0 0 0
392 0 ▶ local $ENV{LD_RUN_PATH} = join(":", grep $_, @libpaths, $ENV{LD_RUN_PATH}) unless $^O eq 'MSWin32' or $^O eq 'darwin';
Branch: 2 branches
BranchTrueFalseTotal
unless $^O eq "MSWin32" or $^O eq "darwin" 36 0 36
unless $^O eq "MSWin32" or $^O eq "darwin" 0 0 0
Condition16%1/6
$^O eq "MSWin32" or $^O eq "darwin"
l!l&&r!l&&!r
0036
$^O eq "MSWin32" or $^O eq "darwin"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "darwin"
$^O eq "MSWin32" $^O eq "darwin"
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "darwin"
$^O eq "MSWin32" $^O eq "darwin"
Truth table33%1/3
A or B
A= $^O eq "MSWin32"B= $^O eq "darwin"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "darwin"
ABresult
1X1
011
000
393 0 ▶ local $ENV{PATH} = join(";", @libpaths).";".$ENV{PATH} if $^O eq 'MSWin32';
Branch: 2 branches
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 36 36
if $^O eq "MSWin32" 0 0 0
394 0 ▶ my $rv = $args{debug} ? system(@sys_cmd) : _quiet_system(@sys_cmd);
Branch: 2 branches
BranchTrueFalseTotal
$args{'debug'} ? : 0 36 36
$args{'debug'} ? : 0 0 0
395 0 ▶ if ($rv != 0 || ! -f $exefile) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv != 0 or not -f $exefile) { } 12 24 36
if ($rv != 0 or not -f $exefile) { } 0 0 0
Condition33%2/6
$rv != 0 or not -f $exefile
l!l&&r!l&&!r
12024
$rv != 0 or not -f $exefile
l!l&&r!l&&!r
000
MC/DC50%1/2
$rv != 0 or not -f $exefile
$rv != 0 not -f $exefile
MC/DC0%0/2
$rv != 0 or not -f $exefile
$rv != 0 not -f $exefile
Truth table66%2/3
A or not(B)
A= $rv != 0B= not -f $exefile
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $rv != 0B= not -f $exefile
ABresult
1X1
011
000
396 0 push @missing, $lib;
397 }
398 else {
399 0 chmod 0755, $exefile;
400 0 my $absexefile = File::Spec->rel2abs($exefile);
401 0 ▶ $absexefile = '"'.$absexefile.'"' if $absexefile =~ m/\s/;
Branch: 2 branches
BranchTrueFalseTotal
if $absexefile =~ /\s/ 0 24 24
if $absexefile =~ /\s/ 0 0 0
402 0 ▶ warn "# Execute($execute): $absexefile\n" if $args{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"debug"} 0 24 24
if $args{"debug"} 0 0 0
403 0 ▶ if ($execute) {
Branch: 2 branches
BranchTrueFalseTotal
if ($execute) 24 0 24
if ($execute) 0 0 0
404 0 my $retval = system($absexefile);
405 0 ▶ warn "# return value: $retval\n" if $args{debug};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"debug"} 0 24 24
if $args{"debug"} 0 0 0
406 0 ▶ push @wrongresult, $lib if $retval != 0;
Branch: 2 branches
BranchTrueFalseTotal
if $retval != 0 2 22 24
if $retval != 0 0 0 0
407 }
408 0 ▶ push @wronganalysis, $lib
Branch: 2 branches
BranchTrueFalseTotal
if $analyze_binary and not &$analyze_binary($lib, $exefile) 1 23 24
if $analyze_binary and not &$analyze_binary($lib, $exefile) 0 0 0
Condition50%3/6
$analyze_binary and not &$analyze_binary($lib, $exefile)
!ll&&!rl&&r
2211
$analyze_binary and not &$analyze_binary($lib, $exefile)
!ll&&!rl&&r
000
MC/DC100%2/2
$analyze_binary and not &$analyze_binary($lib, $exefile)
$analyze_binary not &$analyze_binary($lib, $exefile)
MC/DC0%0/2
$analyze_binary and not &$analyze_binary($lib, $exefile)
$analyze_binary not &$analyze_binary($lib, $exefile)
Truth table100%3/3
A and not(B)
A= $analyze_binaryB= not &$analyze_binary($lib, $exefile)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $analyze_binaryB= not &$analyze_binary($lib, $exefile)
ABresult
0X0
100
111
409 if $analyze_binary and !$analyze_binary->($lib, $exefile);
410 }
411 0 _cleanup_exe($exefile);
412 }
413 0 unlink $cfile;
414  
415 0 my $miss_string = join( q{, }, map qq{'$_'}, @missing );
416 0 ▶ die("Can't link/include C library $miss_string, aborting.\n") if @missing;
Branch: 2 branches
BranchTrueFalseTotal
if @missing 11 20 31
if @missing 0 0 0
417 0 my $wrong_string = join( q{, }, map qq{'$_'}, @wrongresult);
418 0 ▶ die("wrong result: $wrong_string\n") if @wrongresult;
Branch: 2 branches
BranchTrueFalseTotal
if @wrongresult 2 18 20
if @wrongresult 0 0 0
419 0 my $analysis_string = join(q{, }, map qq{'$_'}, @wronganalysis );
420 0 ▶ die("wrong analysis: $analysis_string") if @wronganalysis;
Branch: 2 branches
BranchTrueFalseTotal
if @wronganalysis 1 17 18
if @wronganalysis 0 0 0
421 }
422  
423 49 ▶sub _cleanup_exe {
Subroutine _cleanup_exe: called CC=4 SCAR=22.4
424 0 ▶ my ($exefile) = @_;
Subroutine _cleanup_exe: not called CC=4 SCAR=22.4
425 0 my $ofile = $exefile;
426 0 $ofile =~ s/$Config{_exe}$/$Config{_o}/;
427 # List of files to remove
428 0 my @rmfiles;
429 0 push @rmfiles, $exefile, $ofile, "$exefile\.manifest";
430 0 ▶ if ( $Config{cc} eq 'cl' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Devel::CheckLib::Config{"cc"} eq "cl") 0 49 49
if ($Devel::CheckLib::Config{"cc"} eq "cl") 0 0 0
431 # MSVC also creates foo.ilk and foo.pdb
432 0 my $ilkfile = $exefile;
433 0 $ilkfile =~ s/$Config{_exe}$/.ilk/;
434 0 my $pdbfile = $exefile;
435 0 $pdbfile =~ s/$Config{_exe}$/.pdb/;
436 0 push @rmfiles, $ilkfile, $pdbfile;
437 }
438 0 foreach (grep -f, @rmfiles) {
439 0 ▶ unlink $_ or warn "Could not remove $_: $!";
Branch: 2 branches
BranchTrueFalseTotal
unlink $_ or warn "Could not remove ${_}: $!" 36 0 36
unlink $_ or warn "Could not remove ${_}: $!" 0 0 0
440 }
441 return
442 0 }
443  
444 # return ($cc, $ld)
445 # where $cc is an array ref of compiler name, compiler flags
446 # where $ld is an array ref of linker flags
447 43 ▶sub _findcc {
Subroutine _findcc: called CC=13 SCAR=40.4
448 0 ▶ my ($debug, $user_ccflags, $user_ldflags) = @_;
Subroutine _findcc: not called CC=13 SCAR=40.4
449 # Need to use $keep=1 to work with MSWin32 backslashes and quotes
450 0 my $Config_ccflags = $Config{ccflags}; # use copy so ASPerl will compile
451 0 $Config_ccflags =~ s:-O\S*::; # stop GCC optimising away test code
452 0 my @Config_ldflags = ();
453 0 for my $config_val ( @Config{qw(ldflags)} ){
454 0 ▶ push @Config_ldflags, $config_val if ( $config_val =~ /\S/ );
Branch: 2 branches
BranchTrueFalseTotal
if $config_val =~ /\S/ 43 0 43
if $config_val =~ /\S/ 0 0 0
455 }
456 0 ▶ my @ccflags = grep { length } _parsewords($Config_ccflags||'', $user_ccflags||'');
Condition37%3/8
$Config_ccflags || ''
l!l
430
$user_ccflags || ''
l!l
142
$Config_ccflags || ''
l!l
00
$user_ccflags || ''
l!l
00
MC/DC0%0/1
$Config_ccflags || ''
$Config_ccflags
MC/DC100%1/1
$user_ccflags || ''
$user_ccflags
MC/DC0%0/1
$Config_ccflags || ''
$Config_ccflags
MC/DC0%0/1
$user_ccflags || ''
$user_ccflags
Truth table50%1/2
A
A= $Config_ccflags
Aresult
11
00
Truth table100%2/2
A
A= $user_ccflags
Aresult
11
00
Truth table0%0/2
A
A= $Config_ccflags
Aresult
11
00
Truth table0%0/2
A
A= $user_ccflags
Aresult
11
00
457 0 ▶ my @ldflags = grep { length && $_ !~ m/^-Wl/ } _parsewords(@Config_ldflags, $user_ldflags||'');
Condition20%2/10
length $_ && !($_ =~ /^-Wl/)
!ll&&!rl&&r
0086
$user_ldflags || ''
l!l
043
length $_ && !($_ =~ /^-Wl/)
!ll&&!rl&&r
000
$user_ldflags || ''
l!l
00
MC/DC0%0/2
length $_ && !($_ =~ /^-Wl/)
length $_ !($_ =~ /^-Wl/)
MC/DC0%0/1
$user_ldflags || ''
$user_ldflags
MC/DC0%0/2
length $_ && !($_ =~ /^-Wl/)
length $_ !($_ =~ /^-Wl/)
MC/DC0%0/1
$user_ldflags || ''
$user_ldflags
Truth table33%1/3
A && not(B)
A= length $_B= !($_ =~ /^-Wl/)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $user_ldflags
Aresult
11
00
Truth table0%0/3
A && not(B)
A= length $_B= !($_ =~ /^-Wl/)
ABresult
0X0
100
111
Truth table0%0/2
A
A= $user_ldflags
Aresult
11
00
458 0 my @paths = split(/$Config{path_sep}/, $ENV{PATH});
459 0 my @cc = _parsewords($Config{cc});
460 0 ▶ if (check_compiler ($cc[0], $debug)) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_compiler($cc[0], $debug)) 1 42 43
if (check_compiler($cc[0], $debug)) 0 0 0
461 0 return ( [ @cc, @ccflags ], \@ldflags );
462 }
463 # Find the extension for executables.
464 0 my $exe = $Config{_exe};
465 0 ▶ if ($^O eq 'cygwin') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq "cygwin") 0 42 42
if ($^O eq "cygwin") 0 0 0
466 0 $exe = '';
467 }
468 0 foreach my $path (@paths) {
469 # Look for "$path/$cc[0].exe"
470 0 my $compiler = File::Spec->catfile($path, $cc[0]) . $exe;
471 0 ▶ if (check_compiler ($compiler, $debug)) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_compiler($compiler, $debug)) 41 297 338
if (check_compiler($compiler, $debug)) 0 0 0
472 0 return ([ $compiler, @cc[1 .. $#cc], @ccflags ], \@ldflags)
473 }
474 0 ▶ next if ! $exe;
Branch: 2 branches
BranchTrueFalseTotal
unless $exe 297 0 297
unless $exe 0 0 0
475 # Look for "$path/$cc[0]" without the .exe, if necessary.
476 0 $compiler = File::Spec->catfile($path, $cc[0]);
477 0 ▶ if (check_compiler ($compiler, $debug)) {
Branch: 2 branches
BranchTrueFalseTotal
if (check_compiler($compiler, $debug)) 0 0 0
if (check_compiler($compiler, $debug)) 0 0 0
478 0 return ([ $compiler, @cc[1 .. $#cc], @ccflags ], \@ldflags)
479 }
480 }
481 0 die("Couldn't find your C compiler.\n");
482 }
483  
484 sub check_compiler
485 381 ▶{
Subroutine check_compiler: called CC=5 SCAR=23.4
Pod undocumented
486 0 ▶ my ($compiler, $debug) = @_;
Subroutine check_compiler: not called CC=5 SCAR=23.4
Pod undocumented
487 0 ▶ if (-f $compiler && -x $compiler) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $compiler and -x $compiler) 42 339 381
if (-f $compiler and -x $compiler) 0 0 0
Condition33%2/6
-f $compiler and -x $compiler
!ll&&!rl&&r
339042
-f $compiler and -x $compiler
!ll&&!rl&&r
000
MC/DC50%1/2
-f $compiler and -x $compiler
-f $compiler -x $compiler
MC/DC0%0/2
-f $compiler and -x $compiler
-f $compiler -x $compiler
Truth table66%2/3
A and B
A= -f $compilerB= -x $compiler
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= -f $compilerB= -x $compiler
ABresult
0X0
100
111
488 0 ▶ warn "# Compiler seems to be $compiler\n" if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 42 42
if $debug 0 0 0
489 0 return 1;
490 }
491 0 ▶ warn "# Compiler was not $compiler\n" if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 339 339
if $debug 0 0 0
492 0 return '';
493 }
494  
495  
496 # code substantially borrowed from IPC::Run3
497 67 ▶sub _quiet_system {
Subroutine _quiet_system: called CC=8 SCAR=30.0
498 0 ▶ my (@cmd) = @_;
Subroutine _quiet_system: not called CC=8 SCAR=30.0
499  
500 # save handles
501 0 local *STDOUT_SAVE;
502 0 local *STDERR_SAVE;
503 0 ▶ open STDOUT_SAVE, ">&STDOUT" or die "CheckLib: $! saving STDOUT";
Branch: 2 branches
BranchTrueFalseTotal
open STDOUT_SAVE, ">&STDOUT" or die "CheckLib: $! saving STDOUT" 67 0 67
open STDOUT_SAVE, ">&STDOUT" or die "CheckLib: $! saving STDOUT" 0 0 0
504 0 ▶ open STDERR_SAVE, ">&STDERR" or die "CheckLib: $! saving STDERR";
Branch: 2 branches
BranchTrueFalseTotal
open STDERR_SAVE, ">&STDERR" or die "CheckLib: $! saving STDERR" 67 0 67
open STDERR_SAVE, ">&STDERR" or die "CheckLib: $! saving STDERR" 0 0 0
505
506 # redirect to nowhere
507 0 local *DEV_NULL;
508 67 ▶ open DEV_NULL, ">" . File::Spec->devnull
Branch: 2 branches
BranchTrueFalseTotal
open DEV_NULL, '>' . "File::Spec"->devnull or die "CheckLib: $! opening handle to null device" 67 0 67
open DEV_NULL, '>' . "File::Spec"->devnull or die "CheckLib: $! opening handle to null device" 0 0 0
509 or die "CheckLib: $! opening handle to null device";
510 67 ▶ open STDOUT, ">&" . fileno DEV_NULL
Branch: 2 branches
BranchTrueFalseTotal
open STDOUT, '>&' . fileno(DEV_NULL) or die "CheckLib: $! redirecting STDOUT to null handle" 67 0 67
open STDOUT, '>&' . fileno(DEV_NULL) or die "CheckLib: $! redirecting STDOUT to null handle" 0 0 0
511 or die "CheckLib: $! redirecting STDOUT to null handle";
512 67 ▶ open STDERR, ">&" . fileno DEV_NULL
Branch: 2 branches
BranchTrueFalseTotal
open STDERR, '>&' . fileno(DEV_NULL) or die "CheckLib: $! redirecting STDERR to null handle" 67 0 67
open STDERR, '>&' . fileno(DEV_NULL) or die "CheckLib: $! redirecting STDERR to null handle" 0 0 0
513 or die "CheckLib: $! redirecting STDERR to null handle";
514  
515 # run system command
516 67 my $rv = system(@cmd);
517  
518 # restore handles
519 67 ▶ open STDOUT, ">&" . fileno STDOUT_SAVE
Branch: 2 branches
BranchTrueFalseTotal
open STDOUT, '>&' . fileno(STDOUT_SAVE) or die "CheckLib: $! restoring STDOUT handle" 67 0 67
open STDOUT, '>&' . fileno(STDOUT_SAVE) or die "CheckLib: $! restoring STDOUT handle" 0 0 0
520 or die "CheckLib: $! restoring STDOUT handle";
521 67 ▶ open STDERR, ">&" . fileno STDERR_SAVE
Branch: 2 branches
BranchTrueFalseTotal
open STDERR, '>&' . fileno(STDERR_SAVE) or die "CheckLib: $! restoring STDERR handle" 67 0 67
open STDERR, '>&' . fileno(STDERR_SAVE) or die "CheckLib: $! restoring STDERR handle" 0 0 0
522 or die "CheckLib: $! restoring STDERR handle";
523  
524 67 return $rv;
525 }
526  
527 =head1 PLATFORMS SUPPORTED
528  
529 You must have a C compiler installed. We check for C<$Config{cc}>,
530 both literally as it is in Config.pm and also in the $PATH.
531  
532 It has been tested with varying degrees of rigorousness on:
533  
534 =over
535  
536 =item gcc (on Linux, *BSD, Mac OS X, Solaris, Cygwin)
537  
538 =item Sun's compiler tools on Solaris
539  
540 =item IBM's tools on AIX
541  
542 =item SGI's tools on Irix 6.5
543  
544 =item Microsoft's tools on Windows
545  
546 =item MinGW on Windows (with Strawberry Perl)
547  
548 =item Borland's tools on Windows
549  
550 =item QNX
551  
552 =back
553  
554 =head1 WARNINGS, BUGS and FEEDBACK
555  
556 This is a very early release intended primarily for feedback from
557 people who have discussed it. The interface may change and it has
558 not been adequately tested.
559  
560 Feedback is most welcome, including constructive criticism.
561 Bug reports should be made using L<http://rt.cpan.org/> or by email.
562  
563 When submitting a bug report, please include the output from running:
564  
565 perl -V
566 perl -MDevel::CheckLib -e0
567  
568 =head1 SEE ALSO
569  
570 L<Devel::CheckOS>
571  
572 L<Probe::Perl>
573  
574 =head1 AUTHORS
575  
576 David Cantrell E<lt>david@cantrell.org.ukE<gt>
577  
578 David Golden E<lt>dagolden@cpan.orgE<gt>
579  
580 Yasuhiro Matsumoto E<lt>mattn@cpan.orgE<gt>
581  
582 Thanks to the cpan-testers-discuss mailing list for prompting us to write it
583 in the first place;
584  
585 to Chris Williams for help with Borland support;
586  
587 to Tony Cook for help with Microsoft compiler command-line options
588  
589 =head1 COPYRIGHT and LICENCE
590  
591 Copyright 2007 David Cantrell. Portions copyright 2007 David Golden.
592  
593 This module is free-as-in-speech software, and may be used, distributed,
594 and modified under the same conditions as perl itself.
595  
596 =head1 CONSPIRACY
597  
598 This module is also free-as-in-mason software.
599  
600 =cut
601  
602 11 1;
↑ Summary