| 1 |
# $Id: CheckLib.pm,v 1.25 2008/10/27 12:16:23 drhyde Exp $ |
| 2 |
|
| 3 |
package # |
| 4 |
Devel::CheckLib; |
| 5 |
|
| 6 |
use 5.00405; #postfix foreach |
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
use vars qw($VERSION @ISA @EXPORT); |
| 11 |
$VERSION = '1.01'; |
| 12 |
use Config qw(%Config); |
| 13 |
use Text::ParseWords 'quotewords'; |
| 14 |
|
| 15 |
use File::Spec; |
| 16 |
use File::Temp; |
| 17 |
|
| 18 |
require Exporter; |
| 19 |
@ISA = qw(Exporter); |
| 20 |
@EXPORT = qw(assert_lib check_lib_or_exit check_lib); |
| 21 |
|
| 22 |
# localising prevents the warningness leaking out of this module |
| 23 |
local $^W = 1; # use warnings is a 5.6-ism |
| 24 |
|
| 25 |
_findcc(); # bomb out early if there's no compiler |
| 26 |
|
| 27 |
=head1 NAME |
| 28 |
|
| 29 |
Devel::CheckLib - check that a library is available |
| 30 |
|
| 31 |
=head1 DESCRIPTION |
| 32 |
|
| 33 |
Devel::CheckLib is a perl module that checks whether a particular C |
| 34 |
library and its headers are available. |
| 35 |
|
| 36 |
=head1 SYNOPSIS |
| 37 |
|
| 38 |
use Devel::CheckLib; |
| 39 |
|
| 40 |
check_lib_or_exit( lib => 'jpeg', header => 'jpeglib.h' ); |
| 41 |
check_lib_or_exit( lib => [ 'iconv', 'jpeg' ] ); |
| 42 |
|
| 43 |
# or prompt for path to library and then do this: |
| 44 |
check_lib_or_exit( lib => 'jpeg', libpath => $additional_path ); |
| 45 |
|
| 46 |
=head1 USING IT IN Makefile.PL or Build.PL |
| 47 |
|
| 48 |
If you want to use this from Makefile.PL or Build.PL, do |
| 49 |
not simply copy the module into your distribution as this may cause |
| 50 |
problems when PAUSE and search.cpan.org index the distro. Instead, use |
| 51 |
the use-devel-checklib script. |
| 52 |
|
| 53 |
=head1 HOW IT WORKS |
| 54 |
|
| 55 |
You pass named parameters to a function, describing to it how to build |
| 56 |
and link to the libraries. |
| 57 |
|
| 58 |
It works by trying to compile some code - which defaults to this: |
| 59 |
|
| 60 |
int main(void) { return 0; } |
| 61 |
|
| 62 |
and linking it to the specified libraries. If something pops out the end |
| 63 |
which looks executable, it gets executed, and if main() returns 0 we know |
| 64 |
that it worked. That tiny program is |
| 65 |
built once for each library that you specify, and (without linking) once |
| 66 |
for each header file. |
| 67 |
|
| 68 |
If you want to check for the presence of particular functions in a |
| 69 |
library, or even that those functions return particular results, then |
| 70 |
you can pass your own function body for main() thus: |
| 71 |
|
| 72 |
check_lib_or_exit( |
| 73 |
function => 'foo();if(libversion() > 5) return 0; else return 1;' |
| 74 |
incpath => ... |
| 75 |
libpath => ... |
| 76 |
lib => ... |
| 77 |
header => ... |
| 78 |
); |
| 79 |
|
| 80 |
In that case, it will fail to build if either foo() or libversion() don't |
| 81 |
exist, and main() will return the wrong value if libversion()'s return |
| 82 |
value isn't what you want. |
| 83 |
|
| 84 |
=head1 FUNCTIONS |
| 85 |
|
| 86 |
All of these take the same named parameters and are exported by default. |
| 87 |
To avoid exporting them, C<use Devel::CheckLib ()>. |
| 88 |
|
| 89 |
=head2 assert_lib |
| 90 |
|
| 91 |
This takes several named parameters, all of which are optional, and dies |
| 92 |
with an error message if any of the libraries listed can |
| 93 |
not be found. B<Note>: dying in a Makefile.PL or Build.PL may provoke |
| 94 |
a 'FAIL' report from CPAN Testers' automated smoke testers. Use |
| 95 |
C<check_lib_or_exit> instead. |
| 96 |
|
| 97 |
The named parameters are: |
| 98 |
|
| 99 |
=over |
| 100 |
|
| 101 |
=item lib |
| 102 |
|
| 103 |
Must be either a string with the name of a single |
| 104 |
library or a reference to an array of strings of library names. Depending |
| 105 |
on the compiler found, library names will be fed to the compiler either as |
| 106 |
C<-l> arguments or as C<.lib> file names. (E.g. C<-ljpeg> or C<jpeg.lib>) |
| 107 |
|
| 108 |
=item libpath |
| 109 |
|
| 110 |
a string or an array of strings |
| 111 |
representing additional paths to search for libraries. |
| 112 |
|
| 113 |
=item LIBS |
| 114 |
|
| 115 |
a C<ExtUtils::MakeMaker>-style space-seperated list of |
| 116 |
libraries (each preceded by '-l') and directories (preceded by '-L'). |
| 117 |
|
| 118 |
This can also be supplied on the command-line. |
| 119 |
|
| 120 |
=item debug |
| 121 |
|
| 122 |
If true - emit information during processing that can be used for |
| 123 |
debugging. |
| 124 |
|
| 125 |
=back |
| 126 |
|
| 127 |
And libraries are no use without header files, so ... |
| 128 |
|
| 129 |
=over |
| 130 |
|
| 131 |
=item header |
| 132 |
|
| 133 |
Must be either a string with the name of a single |
| 134 |
header file or a reference to an array of strings of header file names. |
| 135 |
|
| 136 |
=item incpath |
| 137 |
|
| 138 |
a string or an array of strings |
| 139 |
representing additional paths to search for headers. |
| 140 |
|
| 141 |
=item INC |
| 142 |
|
| 143 |
a C<ExtUtils::MakeMaker>-style space-seperated list of |
| 144 |
incpaths, each preceded by '-I'. |
| 145 |
|
| 146 |
This can also be supplied on the command-line. |
| 147 |
|
| 148 |
=back |
| 149 |
|
| 150 |
=head2 check_lib_or_exit |
| 151 |
|
| 152 |
This behaves exactly the same as C<assert_lib()> except that instead of |
| 153 |
dieing, it warns (with exactly the same error message) and exits. |
| 154 |
This is intended for use in Makefile.PL / Build.PL |
| 155 |
when you might want to prompt the user for various paths and |
| 156 |
things before checking that what they've told you is sane. |
| 157 |
|
| 158 |
If any library or header is missing, it exits with an exit value of 0 to avoid |
| 159 |
causing a CPAN Testers 'FAIL' report. CPAN Testers should ignore this |
| 160 |
result -- which is what you want if an external library dependency is not |
| 161 |
available. |
| 162 |
|
| 163 |
=head2 check_lib |
| 164 |
|
| 165 |
This behaves exactly the same as C<assert_lib()> except that it is silent, |
| 166 |
returning false instead of dieing, or true otherwise. |
| 167 |
|
| 168 |
=cut |
| 169 |
|
| 170 |
sub check_lib_or_exit { |
| 171 |
eval 'assert_lib(@_)'; |
| 172 |
if($@) { |
| 173 |
warn $@; |
| 174 |
exit; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
sub check_lib { |
| 179 |
eval 'assert_lib(@_)'; |
| 180 |
return $@ ? 0 : 1; |
| 181 |
} |
| 182 |
|
| 183 |
sub assert_lib { |
| 184 |
my %args = @_; |
| 185 |
my (@libs, @libpaths, @headers, @incpaths); |
| 186 |
|
| 187 |
# FIXME: these four just SCREAM "refactor" at me |
| 188 |
@libs = (ref($args{lib}) ? @{$args{lib}} : $args{lib}) |
| 189 |
if $args{lib}; |
| 190 |
@libpaths = (ref($args{libpath}) ? @{$args{libpath}} : $args{libpath}) |
| 191 |
if $args{libpath}; |
| 192 |
@headers = (ref($args{header}) ? @{$args{header}} : $args{header}) |
| 193 |
if $args{header}; |
| 194 |
@incpaths = (ref($args{incpath}) ? @{$args{incpath}} : $args{incpath}) |
| 195 |
if $args{incpath}; |
| 196 |
|
| 197 |
# work-a-like for Makefile.PL's LIBS and INC arguments |
| 198 |
# if given as command-line argument, append to %args |
| 199 |
for my $arg (@ARGV) { |
| 200 |
for my $mm_attr_key (qw(LIBS INC)) { |
| 201 |
if (my ($mm_attr_value) = $arg =~ /\A $mm_attr_key = (.*)/x) { |
| 202 |
# it is tempting to put some \s* into the expression, but the |
| 203 |
# MM command-line parser only accepts LIBS etc. followed by =, |
| 204 |
# so we should not be any more lenient with whitespace than that |
| 205 |
$args{$mm_attr_key} .= " $mm_attr_value"; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
# using special form of split to trim whitespace |
| 211 |
if(defined($args{LIBS})) { |
| 212 |
foreach my $arg (split(' ', $args{LIBS})) { |
| 213 |
die("LIBS argument badly-formed: $arg\n") unless($arg =~ /^-[lLR]/); |
| 214 |
push @{$arg =~ /^-l/ ? \@libs : \@libpaths}, substr($arg, 2); |
| 215 |
} |
| 216 |
} |
| 217 |
if(defined($args{INC})) { |
| 218 |
foreach my $arg (split(' ', $args{INC})) { |
| 219 |
die("INC argument badly-formed: $arg\n") unless($arg =~ /^-I/); |
| 220 |
push @incpaths, substr($arg, 2); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
my ($cc, $ld) = _findcc(); |
| 225 |
my @missing; |
| 226 |
my @wrongresult; |
| 227 |
my @use_headers; |
| 228 |
|
| 229 |
# first figure out which headers we can't find ... |
| 230 |
for my $header (@headers) { |
| 231 |
push @use_headers, $header; |
| 232 |
my($ch, $cfile) = File::Temp::tempfile( |
| 233 |
'assertlibXXXXXXXX', SUFFIX => '.c' |
| 234 |
); |
| 235 |
my $ofile = $cfile; |
| 236 |
$ofile =~ s/\.c$/$Config{_o}/; |
| 237 |
print $ch qq{#include <$_>\n} for @use_headers; |
| 238 |
print $ch qq{int main(void) { return 0; }\n}; |
| 239 |
close($ch); |
| 240 |
my $exefile = File::Temp::mktemp( 'assertlibXXXXXXXX' ) . $Config{_exe}; |
| 241 |
my @sys_cmd; |
| 242 |
# FIXME: re-factor - almost identical code later when linking |
| 243 |
if ( $Config{cc} eq 'cl' ) { # Microsoft compiler |
| 244 |
require Win32; |
| 245 |
@sys_cmd = ( |
| 246 |
@$cc, |
| 247 |
$cfile, |
| 248 |
"/Fe$exefile", |
| 249 |
(map { '/I'.Win32::GetShortPathName($_) } @incpaths), |
| 250 |
"/link", |
| 251 |
@$ld |
| 252 |
); |
| 253 |
} elsif($Config{cc} =~ /bcc32(\.exe)?/) { # Borland |
| 254 |
@sys_cmd = ( |
| 255 |
@$cc, |
| 256 |
@$ld, |
| 257 |
(map { "-I$_" } @incpaths), |
| 258 |
"-o$exefile", |
| 259 |
$cfile |
| 260 |
); |
| 261 |
} else { # Unix-ish: gcc, Sun, AIX (gcc, cc), ... |
| 262 |
@sys_cmd = ( |
| 263 |
@$cc, |
| 264 |
@$ld, |
| 265 |
$cfile, |
| 266 |
(map { "-I$_" } @incpaths), |
| 267 |
"-o", "$exefile" |
| 268 |
); |
| 269 |
} |
| 270 |
warn "# @sys_cmd\n" if $args{debug}; |
| 271 |
my $rv = $args{debug} ? system(@sys_cmd) : _quiet_system(@sys_cmd); |
| 272 |
push @missing, $header if $rv != 0 || ! -x $exefile; |
| 273 |
_cleanup_exe($exefile); |
| 274 |
unlink $ofile if -e $ofile; |
| 275 |
unlink $cfile; |
| 276 |
} |
| 277 |
|
| 278 |
# now do each library in turn with headers |
| 279 |
my($ch, $cfile) = File::Temp::tempfile( |
| 280 |
'assertlibXXXXXXXX', SUFFIX => '.c' |
| 281 |
); |
| 282 |
my $ofile = $cfile; |
| 283 |
$ofile =~ s/\.c$/$Config{_o}/; |
| 284 |
print $ch qq{#include <$_>\n} foreach (@headers); |
| 285 |
print $ch "int main(void) { ".($args{function} || 'return 0;')." }\n"; |
| 286 |
close($ch); |
| 287 |
for my $lib ( @libs ) { |
| 288 |
my $exefile = File::Temp::mktemp( 'assertlibXXXXXXXX' ) . $Config{_exe}; |
| 289 |
my @sys_cmd; |
| 290 |
if ( $Config{cc} eq 'cl' ) { # Microsoft compiler |
| 291 |
require Win32; |
| 292 |
my @libpath = map { |
| 293 |
q{/libpath:} . Win32::GetShortPathName($_) |
| 294 |
} @libpaths; |
| 295 |
# this is horribly sensitive to the order of arguments |
| 296 |
@sys_cmd = ( |
| 297 |
@$cc, |
| 298 |
$cfile, |
| 299 |
"${lib}.lib", |
| 300 |
"/Fe$exefile", |
| 301 |
(map { '/I'.Win32::GetShortPathName($_) } @incpaths), |
| 302 |
"/link", |
| 303 |
@$ld, |
| 304 |
(map {'/libpath:'.Win32::GetShortPathName($_)} @libpaths), |
| 305 |
); |
| 306 |
} elsif($Config{cc} eq 'CC/DECC') { # VMS |
| 307 |
} elsif($Config{cc} =~ /bcc32(\.exe)?/) { # Borland |
| 308 |
@sys_cmd = ( |
| 309 |
@$cc, |
| 310 |
@$ld, |
| 311 |
"-o$exefile", |
| 312 |
(map { "-I$_" } @incpaths), |
| 313 |
(map { "-L$_" } @libpaths), |
| 314 |
"-l$lib", |
| 315 |
$cfile); |
| 316 |
} else { # Unix-ish |
| 317 |
# gcc, Sun, AIX (gcc, cc) |
| 318 |
@sys_cmd = ( |
| 319 |
@$cc, |
| 320 |
@$ld, |
| 321 |
$cfile, |
| 322 |
"-o", "$exefile", |
| 323 |
(map { "-I$_" } @incpaths), |
| 324 |
(map { "-L$_" } @libpaths), |
| 325 |
"-l$lib", |
| 326 |
); |
| 327 |
} |
| 328 |
warn "# @sys_cmd\n" if $args{debug}; |
| 329 |
my $rv = $args{debug} ? system(@sys_cmd) : _quiet_system(@sys_cmd); |
| 330 |
push @missing, $lib if $rv != 0 || ! -x $exefile; |
| 331 |
my $absexefile = File::Spec->rel2abs($exefile); |
| 332 |
$absexefile = '"'.$absexefile.'"' if $absexefile =~ m/\s/; |
| 333 |
push @wrongresult, $lib if $rv == 0 && -x $exefile && system($absexefile) != 0; |
| 334 |
unlink $ofile if -e $ofile; |
| 335 |
_cleanup_exe($exefile); |
| 336 |
} |
| 337 |
unlink $cfile; |
| 338 |
|
| 339 |
my $miss_string = join( q{, }, map { qq{'$_'} } @missing ); |
| 340 |
die("Can't link/include C library $miss_string, aborting.\n") if @missing; |
| 341 |
my $wrong_string = join( q{, }, map { qq{'$_'} } @wrongresult); |
| 342 |
die("wrong result: $wrong_string\n") if @wrongresult; |
| 343 |
} |
| 344 |
|
| 345 |
sub _cleanup_exe { |
| 346 |
my ($exefile) = @_; |
| 347 |
my $ofile = $exefile; |
| 348 |
$ofile =~ s/$Config{_exe}$/$Config{_o}/; |
| 349 |
unlink $exefile if -f $exefile; |
| 350 |
unlink $ofile if -f $ofile; |
| 351 |
unlink "$exefile\.manifest" if -f "$exefile\.manifest"; |
| 352 |
if ( $Config{cc} eq 'cl' ) { |
| 353 |
# MSVC also creates foo.ilk and foo.pdb |
| 354 |
my $ilkfile = $exefile; |
| 355 |
$ilkfile =~ s/$Config{_exe}$/.ilk/; |
| 356 |
my $pdbfile = $exefile; |
| 357 |
$pdbfile =~ s/$Config{_exe}$/.pdb/; |
| 358 |
unlink $ilkfile if -f $ilkfile; |
| 359 |
unlink $pdbfile if -f $pdbfile; |
| 360 |
} |
| 361 |
return |
| 362 |
} |
| 363 |
|
| 364 |
# return ($cc, $ld) |
| 365 |
# where $cc is an array ref of compiler name, compiler flags |
| 366 |
# where $ld is an array ref of linker flags |
| 367 |
sub _findcc { |
| 368 |
# Need to use $keep=1 to work with MSWin32 backslashes and quotes |
| 369 |
my $Config_ccflags = $Config{ccflags}; # use copy so ASPerl will compile |
| 370 |
my @Config_ldflags = (); |
| 371 |
for my $config_val ( @Config{qw(ldflags perllibs)} ){ |
| 372 |
push @Config_ldflags, $config_val if ( $config_val =~ /\S/ ); |
| 373 |
} |
| 374 |
my @ccflags = grep { length } quotewords('\s+', 1, $Config_ccflags||''); |
| 375 |
my @ldflags = grep { length } quotewords('\s+', 1, @Config_ldflags); |
| 376 |
my @paths = split(/$Config{path_sep}/, $ENV{PATH}); |
| 377 |
my @cc = split(/\s+/, $Config{cc}); |
| 378 |
return ( [ @cc, @ccflags ], \@ldflags ) if -x $cc[0]; |
| 379 |
foreach my $path (@paths) { |
| 380 |
my $compiler = File::Spec->catfile($path, $cc[0]) . $Config{_exe}; |
| 381 |
return ([ $compiler, @cc[1 .. $#cc], @ccflags ], \@ldflags) |
| 382 |
if -x $compiler; |
| 383 |
} |
| 384 |
die("Couldn't find your C compiler\n"); |
| 385 |
} |
| 386 |
|
| 387 |
# code substantially borrowed from IPC::Run3 |
| 388 |
sub _quiet_system { |
| 389 |
my (@cmd) = @_; |
| 390 |
|
| 391 |
# save handles |
| 392 |
local *STDOUT_SAVE; |
| 393 |
local *STDERR_SAVE; |
| 394 |
open STDOUT_SAVE, ">&STDOUT" or die "CheckLib: $! saving STDOUT"; |
| 395 |
open STDERR_SAVE, ">&STDERR" or die "CheckLib: $! saving STDERR"; |
| 396 |
|
| 397 |
# redirect to nowhere |
| 398 |
local *DEV_NULL; |
| 399 |
open DEV_NULL, ">" . File::Spec->devnull |
| 400 |
or die "CheckLib: $! opening handle to null device"; |
| 401 |
open STDOUT, ">&" . fileno DEV_NULL |
| 402 |
or die "CheckLib: $! redirecting STDOUT to null handle"; |
| 403 |
open STDERR, ">&" . fileno DEV_NULL |
| 404 |
or die "CheckLib: $! redirecting STDERR to null handle"; |
| 405 |
|
| 406 |
# run system command |
| 407 |
my $rv = system(@cmd); |
| 408 |
|
| 409 |
# restore handles |
| 410 |
open STDOUT, ">&" . fileno STDOUT_SAVE |
| 411 |
or die "CheckLib: $! restoring STDOUT handle"; |
| 412 |
open STDERR, ">&" . fileno STDERR_SAVE |
| 413 |
or die "CheckLib: $! restoring STDERR handle"; |
| 414 |
|
| 415 |
return $rv; |
| 416 |
} |
| 417 |
|
| 418 |
=head1 PLATFORMS SUPPORTED |
| 419 |
|
| 420 |
You must have a C compiler installed. We check for C<$Config{cc}>, |
| 421 |
both literally as it is in Config.pm and also in the $PATH. |
| 422 |
|
| 423 |
It has been tested with varying degrees on rigourousness on: |
| 424 |
|
| 425 |
=over |
| 426 |
|
| 427 |
=item gcc (on Linux, *BSD, Mac OS X, Solaris, Cygwin) |
| 428 |
|
| 429 |
=item Sun's compiler tools on Solaris |
| 430 |
|
| 431 |
=item IBM's tools on AIX |
| 432 |
|
| 433 |
=item SGI's tools on Irix 6.5 |
| 434 |
|
| 435 |
=item Microsoft's tools on Windows |
| 436 |
|
| 437 |
=item MinGW on Windows (with Strawberry Perl) |
| 438 |
|
| 439 |
=item Borland's tools on Windows |
| 440 |
|
| 441 |
=item QNX |
| 442 |
|
| 443 |
=back |
| 444 |
|
| 445 |
=head1 WARNINGS, BUGS and FEEDBACK |
| 446 |
|
| 447 |
This is a very early release intended primarily for feedback from |
| 448 |
people who have discussed it. The interface may change and it has |
| 449 |
not been adequately tested. |
| 450 |
|
| 451 |
Feedback is most welcome, including constructive criticism. |
| 452 |
Bug reports should be made using L<http://rt.cpan.org/> or by email. |
| 453 |
|
| 454 |
When submitting a bug report, please include the output from running: |
| 455 |
|
| 456 |
perl -V |
| 457 |
perl -MDevel::CheckLib -e0 |
| 458 |
|
| 459 |
=head1 SEE ALSO |
| 460 |
|
| 461 |
L<Devel::CheckOS> |
| 462 |
|
| 463 |
L<Probe::Perl> |
| 464 |
|
| 465 |
=head1 AUTHORS |
| 466 |
|
| 467 |
David Cantrell E<lt>[email protected]<gt> |
| 468 |
|
| 469 |
David Golden E<lt>[email protected]<gt> |
| 470 |
|
| 471 |
Yasuhiro Matsumoto E<lt>[email protected]<gt> |
| 472 |
|
| 473 |
Thanks to the cpan-testers-discuss mailing list for prompting us to write it |
| 474 |
in the first place; |
| 475 |
|
| 476 |
to Chris Williams for help with Borland support; |
| 477 |
|
| 478 |
to Tony Cook for help with Microsoft compiler command-line options |
| 479 |
|
| 480 |
=head1 COPYRIGHT and LICENCE |
| 481 |
|
| 482 |
Copyright 2007 David Cantrell. Portions copyright 2007 David Golden. |
| 483 |
|
| 484 |
This module is free-as-in-speech software, and may be used, distributed, |
| 485 |
and modified under the same conditions as perl itself. |
| 486 |
|
| 487 |
=head1 CONSPIRACY |
| 488 |
|
| 489 |
This module is also free-as-in-mason software. |
| 490 |
|
| 491 |
=cut |
| 492 |
|
| 493 |
1; |