| 1 |
package Imager::Probe; |
| 2 |
use 5.006; |
| 3 |
use strict; |
| 4 |
use File::Spec; |
| 5 |
use Config; |
| 6 |
use Cwd (); |
| 7 |
use File::Temp (); |
| 8 |
use Carp (); |
| 9 |
|
| 10 |
our $VERSION = "1.011"; |
| 11 |
|
| 12 |
my @alt_transfer = qw/altname incsuffix libbase/; |
| 13 |
|
| 14 |
sub probe { |
| 15 |
my ($class, $req) = @_; |
| 16 |
|
| 17 |
_populate_defaults($req); |
| 18 |
|
| 19 |
my $name = $req->{name}; |
| 20 |
my $result; |
| 21 |
if ($req->{code}) { |
| 22 |
$result = _probe_code($req); |
| 23 |
} |
| 24 |
if (!$result && $req->{pkg}) { |
| 25 |
$result = _probe_pkg($req); |
| 26 |
} |
| 27 |
if (!$result && $req->{inccheck} && ($req->{libcheck} || $req->{libbase})) { |
| 28 |
$req->{altname} ||= "main"; |
| 29 |
$result = _probe_check($req); |
| 30 |
} |
| 31 |
|
| 32 |
if ($result && $req->{testcode}) { |
| 33 |
$result = _probe_test($req, $result); |
| 34 |
} |
| 35 |
|
| 36 |
if (!$result && $req->{alternatives}) { |
| 37 |
ALTCHECK: |
| 38 |
my $index = 1; |
| 39 |
for my $alt (@{$req->{alternatives}}) { |
| 40 |
$req->{altname} = $alt->{altname} || "alt $index"; |
| 41 |
$req->{verbose} |
| 42 |
and print "$req->{name}: Trying alternative $index\n"; |
| 43 |
my %work = %$req; |
| 44 |
for my $key (@alt_transfer) { |
| 45 |
exists $alt->{$key} and $work{$key} = $alt->{$key}; |
| 46 |
} |
| 47 |
$result = _probe_check(\%work); |
| 48 |
|
| 49 |
if ($result && $req->{testcode}) { |
| 50 |
$result = _probe_test(\%work, $result); |
| 51 |
} |
| 52 |
|
| 53 |
$result |
| 54 |
and last; |
| 55 |
|
| 56 |
++$index; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if (!$result && $req->{testcode}) { |
| 61 |
$result = _probe_fake($req); |
| 62 |
|
| 63 |
$result or return; |
| 64 |
|
| 65 |
$result = _probe_test($req, $result); |
| 66 |
} |
| 67 |
|
| 68 |
$result or return; |
| 69 |
|
| 70 |
return $result; |
| 71 |
} |
| 72 |
|
| 73 |
my @default_argv_names = |
| 74 |
( |
| 75 |
qw(ccflags optimize cc ldflags) |
| 76 |
); |
| 77 |
|
| 78 |
sub _populate_defaults { |
| 79 |
my ($req) = @_; |
| 80 |
$req->{verbose} ||= $ENV{IM_VERBOSE}; |
| 81 |
my %names = map {; $_ => 1 } @default_argv_names; |
| 82 |
@$req{@default_argv_names} = @Config{@default_argv_names}; |
| 83 |
for my $arg (@ARGV) { |
| 84 |
if ($arg =~ /^\U\Q$arg\E=(.*)$/) { |
| 85 |
$req->{$arg} = $1; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
sub _probe_code { |
| 91 |
my ($req) = @_; |
| 92 |
|
| 93 |
my $code = $req->{code}; |
| 94 |
my @probes = ref $code eq "ARRAY" ? @$code : $code; |
| 95 |
|
| 96 |
my $result; |
| 97 |
for my $probe (@probes) { |
| 98 |
$result = $probe->($req) |
| 99 |
and return $result; |
| 100 |
} |
| 101 |
|
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
sub is_exe { |
| 106 |
my ($name) = @_; |
| 107 |
|
| 108 |
my @exe_suffix = $Config{_exe}; |
| 109 |
if ($^O eq 'MSWin32') { |
| 110 |
push @exe_suffix, qw/.bat .cmd/; |
| 111 |
} |
| 112 |
elsif ($^O eq 'cygwin') { |
| 113 |
push @exe_suffix, ""; |
| 114 |
} |
| 115 |
|
| 116 |
for my $dir (File::Spec->path) { |
| 117 |
for my $suffix (@exe_suffix) { |
| 118 |
-x File::Spec->catfile($dir, "$name$suffix") |
| 119 |
and return 1; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
sub _probe_pkg { |
| 127 |
my ($req) = @_; |
| 128 |
|
| 129 |
# Setup pkg-config's environment variable to search non-standard paths |
| 130 |
# which may be provided by --libdirs. |
| 131 |
my @pkgcfg_paths = map { "$_/pkgconfig" } _lib_paths( $req ); |
| 132 |
unshift @pkgcfg_paths, $ENV{ 'PKG_CONFIG_PATH' } if $ENV{ 'PKG_CONFIG_PATH' }; |
| 133 |
|
| 134 |
local $ENV{ 'PKG_CONFIG_PATH' } = join $Config{path_sep}, @pkgcfg_paths; |
| 135 |
print "PKG_CONFIG_PATH=$ENV{PKG_CONFIG_PATH}\n" if $req->{verbose}; |
| 136 |
|
| 137 |
is_exe('pkg-config') or return; |
| 138 |
my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null'; |
| 139 |
|
| 140 |
my @pkgs = @{$req->{pkg}}; |
| 141 |
for my $pkg (@pkgs) { |
| 142 |
if (!system("pkg-config $pkg --exists $redir")) { |
| 143 |
# if we find it, but the following fail, then pkg-config is too |
| 144 |
# broken to be useful |
| 145 |
my $cflags = `pkg-config $pkg --cflags` |
| 146 |
and !$? or return; |
| 147 |
|
| 148 |
my $lflags = `pkg-config $pkg --libs` |
| 149 |
and !$? or return; |
| 150 |
|
| 151 |
my $defines = ''; |
| 152 |
$cflags =~ s/(-D\S+)/$defines .= " $1"; ''/ge; |
| 153 |
|
| 154 |
chomp $cflags; |
| 155 |
chomp $lflags; |
| 156 |
print "$req->{name}: Found via pkg-config $pkg\n"; |
| 157 |
print <<EOS if $req->{verbose}; |
| 158 |
cflags: $cflags |
| 159 |
defines: $defines |
| 160 |
lflags: $lflags |
| 161 |
EOS |
| 162 |
# rt 75869 |
| 163 |
# if Win32 doesn't provide this information, too bad |
| 164 |
if (!grep(/^-L/, split " ", $lflags) |
| 165 |
&& $^O ne 'MSWin32') { |
| 166 |
# pkg-config told us about the library, make sure it's |
| 167 |
# somewhere EU::MM can find it |
| 168 |
print "Checking if EU::MM can find $lflags\n" if $req->{verbose}; |
| 169 |
my ($extra, $bs_load, $ld_load, $ld_run_path) = |
| 170 |
ExtUtils::Liblist->ext($lflags, $req->{verbose}); |
| 171 |
unless ($ld_run_path) { |
| 172 |
# search our standard places |
| 173 |
$lflags = _resolve_libs($req, $lflags); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return |
| 178 |
{ |
| 179 |
INC => $cflags, |
| 180 |
LIBS => $lflags, |
| 181 |
DEFINE => $defines, |
| 182 |
}; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
print "$req->{name}: Not found via pkg-config\n"; |
| 187 |
|
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
sub _is_msvc { |
| 192 |
return $Config{cc} eq "cl"; |
| 193 |
} |
| 194 |
|
| 195 |
sub _lib_basename { |
| 196 |
my ($base) = @_; |
| 197 |
|
| 198 |
if (_is_msvc()) { |
| 199 |
return $base; |
| 200 |
} |
| 201 |
else { |
| 202 |
return "lib$base"; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
sub _lib_option { |
| 207 |
my ($base) = @_; |
| 208 |
|
| 209 |
if (_is_msvc()) { |
| 210 |
return $base . $Config{_a}; |
| 211 |
} |
| 212 |
else { |
| 213 |
return "-l$base"; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
sub _quotearg { |
| 218 |
my ($opt) = @_; |
| 219 |
|
| 220 |
return $opt =~ /\s/ ? qq("$opt") : $opt; |
| 221 |
} |
| 222 |
|
| 223 |
sub _probe_check { |
| 224 |
my ($req) = @_; |
| 225 |
|
| 226 |
my @libcheck; |
| 227 |
my @libbase; |
| 228 |
if ($req->{libcheck}) { |
| 229 |
if (ref $req->{libcheck} eq "ARRAY") { |
| 230 |
push @libcheck, @{$req->{libcheck}}; |
| 231 |
} |
| 232 |
else { |
| 233 |
push @libcheck, $req->{libcheck}; |
| 234 |
} |
| 235 |
} |
| 236 |
elsif ($req->{libbase}) { |
| 237 |
@libbase = ref $req->{libbase} ? @{$req->{libbase}} : $req->{libbase}; |
| 238 |
|
| 239 |
my $lext=$Config{'so'}; # Get extensions of libraries |
| 240 |
my $aext=$Config{'_a'}; |
| 241 |
|
| 242 |
for my $libbase (@libbase) { |
| 243 |
my $basename = _lib_basename($libbase); |
| 244 |
push @libcheck, sub { |
| 245 |
-e File::Spec->catfile($_[0], "$basename$aext") |
| 246 |
|| -e File::Spec->catfile($_[0], "$basename.$lext") |
| 247 |
}; |
| 248 |
} |
| 249 |
} |
| 250 |
else { |
| 251 |
print "$req->{name}: No libcheck or libbase, nothing to search for\n" |
| 252 |
if $req->{verbose}; |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
my @found_libpath; |
| 257 |
my @lib_search = _lib_paths($req); |
| 258 |
print "$req->{name}: Searching directories for libraries:\n" |
| 259 |
if $req->{verbose}; |
| 260 |
for my $libcheck (@libcheck) { |
| 261 |
for my $path (@lib_search) { |
| 262 |
print "$req->{name}: $path\n" if $req->{verbose}; |
| 263 |
if ($libcheck->($path)) { |
| 264 |
print "$req->{name}: Found!\n" if $req->{verbose}; |
| 265 |
push @found_libpath, $path; |
| 266 |
last; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
my $found_incpath; |
| 272 |
my $inccheck = $req->{inccheck}; |
| 273 |
my @inc_search = _inc_paths($req); |
| 274 |
print "$req->{name}: Searching directories for headers:\n" |
| 275 |
if $req->{verbose}; |
| 276 |
for my $path (@inc_search) { |
| 277 |
print "$req->{name}: $path\n" if $req->{verbose}; |
| 278 |
if ($inccheck->($path)) { |
| 279 |
print "$req->{name}: Found!\n" if $req->{verbose}; |
| 280 |
$found_incpath = $path; |
| 281 |
last; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
my $alt = ""; |
| 286 |
if ($req->{altname}) { |
| 287 |
$alt = " $req->{altname}:"; |
| 288 |
} |
| 289 |
print "$req->{name}:$alt includes ", $found_incpath ? "" : "not ", |
| 290 |
"found - libraries ", @found_libpath == @libcheck ? "" : "not ", "found\n"; |
| 291 |
|
| 292 |
@found_libpath == @libcheck && $found_incpath |
| 293 |
or return; |
| 294 |
|
| 295 |
my @libs = map "-L$_", @found_libpath; |
| 296 |
if ($req->{libopts}) { |
| 297 |
push @libs, $req->{libopts}; |
| 298 |
} |
| 299 |
elsif (@libbase) { |
| 300 |
push @libs, map _lib_option($_), @libbase; |
| 301 |
} |
| 302 |
else { |
| 303 |
die "$req->{altname}: inccheck but no libbase or libopts"; |
| 304 |
} |
| 305 |
|
| 306 |
return |
| 307 |
{ |
| 308 |
INC => _quotearg("-I$found_incpath"), |
| 309 |
LIBS => join(" ", map _quotearg($_), @libs), |
| 310 |
DEFINE => "", |
| 311 |
}; |
| 312 |
} |
| 313 |
|
| 314 |
sub _probe_fake { |
| 315 |
my ($req) = @_; |
| 316 |
|
| 317 |
# the caller provided test code, and the compiler may look in |
| 318 |
# places we don't, see Imager-Screenshot ticket 56793, |
| 319 |
# so fake up a result so the test code can |
| 320 |
my $lopts; |
| 321 |
if ($req->{libopts}) { |
| 322 |
$lopts = $req->{libopts}; |
| 323 |
} |
| 324 |
elsif (defined $req->{libbase}) { |
| 325 |
# might not need extra libraries, eg. Win32 perl already links |
| 326 |
# everything |
| 327 |
my @libs = $req->{libbase} |
| 328 |
? ( ref $req->{libbase} ? @{$req->{libbase}} : $req->{libbase} ) |
| 329 |
: (); |
| 330 |
$lopts = join " ", map _lib_option($_), @libs; |
| 331 |
} |
| 332 |
if (defined $lopts) { |
| 333 |
print "$req->{name}: Checking if the compiler can find them on its own\n"; |
| 334 |
return |
| 335 |
{ |
| 336 |
INC => "", |
| 337 |
LIBS => $lopts, |
| 338 |
DEFINE => "", |
| 339 |
}; |
| 340 |
} |
| 341 |
else { |
| 342 |
print "$req->{name}: Can't fake it - no libbase or libopts\n" |
| 343 |
if $req->{verbose}; |
| 344 |
return; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
sub _probe_test { |
| 349 |
my ($req, $result) = @_; |
| 350 |
|
| 351 |
# setup LD_RUN_PATH to match link time |
| 352 |
print "Asking liblist for LD_RUN_PATH:\n" if $req->{verbose}; |
| 353 |
my ($extra, $bs_load, $ld_load, $ld_run_path) = |
| 354 |
ExtUtils::Liblist->ext($result->{LIBS}, $req->{verbose}); |
| 355 |
local $ENV{LD_RUN_PATH}; |
| 356 |
|
| 357 |
if ($ld_run_path) { |
| 358 |
print "Setting LD_RUN_PATH=$ld_run_path for $req->{name} probe\n" |
| 359 |
if $req->{verbose}; |
| 360 |
$ENV{LD_RUN_PATH} = $ld_run_path; |
| 361 |
if ($Config{lddlflags} =~ /([^ ]*-(?:rpath|R)[,=]?)([^ ]+)/ |
| 362 |
&& -d $2) { |
| 363 |
# hackety, hackety |
| 364 |
# LD_RUN_PATH is ignored when there's already an -rpath option |
| 365 |
# so provide one |
| 366 |
my $prefix = $1; |
| 367 |
$result->{LDDLFLAGS} = $Config{lddlflags} . " " . |
| 368 |
join " ", map "$prefix$_", split $Config{path_sep}, $ld_run_path; |
| 369 |
} |
| 370 |
} |
| 371 |
my $good = _check_lib( |
| 372 |
$req, |
| 373 |
LIBS => $result->{LIBS}, |
| 374 |
INC => $result->{INC}, |
| 375 |
header => $req->{testcodeheaders}, |
| 376 |
function => $req->{testcode}, |
| 377 |
prologue => $req->{testcodeprologue}, |
| 378 |
); |
| 379 |
unless ($good) { |
| 380 |
print "$req->{name}: Test code failed: $@"; |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
print "$req->{name}: Passed code check\n"; |
| 385 |
return $result; |
| 386 |
} |
| 387 |
|
| 388 |
sub _check_lib { |
| 389 |
my ($req, %args) = @_; |
| 390 |
|
| 391 |
my $code = ""; |
| 392 |
if (my $hdrs = $args{header}) { |
| 393 |
ref $hdrs or $hdrs = [ $hdrs ]; |
| 394 |
$code .= "#include <$_>\n" for @$hdrs; |
| 395 |
} |
| 396 |
$code .= $args{prologue} if $args{prologue}; |
| 397 |
$args{function} or Carp::confess "Missing function"; |
| 398 |
$code .= "int main(int argc, char **argv) {\n$args{function}\n}\n"; |
| 399 |
|
| 400 |
return _try_compile_and_link |
| 401 |
( |
| 402 |
$code, $req, |
| 403 |
name => $req->{name}, |
| 404 |
ccflags => $args{INC}, |
| 405 |
run => 1, |
| 406 |
libs => $args{LIBS}, |
| 407 |
); |
| 408 |
} |
| 409 |
|
| 410 |
sub _resolve_libs { |
| 411 |
my ($req, $lflags) = @_; |
| 412 |
|
| 413 |
my @libs = grep /^-l/, split ' ', $lflags; |
| 414 |
my %paths; |
| 415 |
my @paths = _lib_paths($req); |
| 416 |
my $so = $Config{so}; |
| 417 |
my $libext = $Config{_a}; |
| 418 |
for my $lib (@libs) { |
| 419 |
$lib =~ s/^-l/lib/; |
| 420 |
|
| 421 |
for my $path (@paths) { |
| 422 |
if (-e "$path/$lib.$so" || -e "$path/$lib$libext") { |
| 423 |
$paths{$path} = 1; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
return join(" ", ( map "-L$_", keys %paths ), $lflags ); |
| 429 |
} |
| 430 |
|
| 431 |
sub _lib_paths { |
| 432 |
my ($req) = @_; |
| 433 |
|
| 434 |
print "$req->{name} IM_LIBPATH: $ENV{IM_LIBPATH}\n" |
| 435 |
if $req->{verbose} && defined $ENV{IM_LIBPATH}; |
| 436 |
print "$req->{name} LIB: $ENV{IM_LIBPATH}\n" |
| 437 |
if $req->{verbose} && defined $ENV{LIB} && $^O eq "MSWin32"; |
| 438 |
my $lp = $req->{libpath}; |
| 439 |
print "$req->{name} libpath: ", ref $lp ? join($Config{path_sep}, @$lp) : $lp, "\n" |
| 440 |
if $req->{verbose} && defined $lp; |
| 441 |
|
| 442 |
return _paths |
| 443 |
( |
| 444 |
$ENV{IM_LIBPATH}, |
| 445 |
$req->{libpath}, |
| 446 |
( |
| 447 |
map { split ' ' } |
| 448 |
grep $_, |
| 449 |
@Config{qw/loclibpth libpth libspath/} |
| 450 |
), |
| 451 |
$^O eq "MSWin32" ? $ENV{LIB} : "", |
| 452 |
$^O eq "cygwin" ? "/usr/lib/w32api" : "", |
| 453 |
"/usr/lib", |
| 454 |
"/usr/local/lib", |
| 455 |
_gcc_lib_paths(), |
| 456 |
_dyn_lib_paths(), |
| 457 |
); |
| 458 |
} |
| 459 |
|
| 460 |
sub _gcc_lib_paths { |
| 461 |
$Config{gccversion} |
| 462 |
or return; |
| 463 |
|
| 464 |
my ($base_version) = $Config{gccversion} =~ /^([0-9]+)/ |
| 465 |
or return; |
| 466 |
|
| 467 |
$base_version >= 4 |
| 468 |
or return; |
| 469 |
|
| 470 |
local $ENV{LANG} = "C"; |
| 471 |
local $ENV{LC_ALL} = "C"; |
| 472 |
my ($lib_line) = grep /^libraries:/, `$Config{cc} -print-search-dirs` |
| 473 |
or return; |
| 474 |
$lib_line =~ s/^libraries: =//; |
| 475 |
chomp $lib_line; |
| 476 |
|
| 477 |
return grep !/gcc/ && -d, split /:/, $lib_line; |
| 478 |
} |
| 479 |
|
| 480 |
sub _dyn_lib_paths { |
| 481 |
return map { defined() ? split /\Q$Config{path_sep}/ : () } |
| 482 |
map $ENV{$_}, |
| 483 |
qw(LD_RUN_PATH LD_LIBRARY_PATH DYLD_LIBRARY_PATH LIBRARY_PATH); |
| 484 |
} |
| 485 |
|
| 486 |
sub _inc_paths { |
| 487 |
my ($req) = @_; |
| 488 |
|
| 489 |
print "$req->{name} IM_INCPATH: $ENV{IM_INCPATH}\n" |
| 490 |
if $req->{verbose} && defined $ENV{IM_INCPATH}; |
| 491 |
print "$req->{name} INCLUDE: $ENV{INCLUDE}\n" |
| 492 |
if $req->{verbose} && defined $ENV{INCLUDE} && $^O eq "MSWin32"; |
| 493 |
my $ip = $req->{incpath}; |
| 494 |
print "$req->{name} incpath: ", ref $ip ? join($Config{path_sep}, @$ip) : $ip, "\n" |
| 495 |
if $req->{verbose} && defined $req->{incpath}; |
| 496 |
|
| 497 |
my @paths = _paths |
| 498 |
( |
| 499 |
$ENV{IM_INCPATH}, |
| 500 |
$req->{incpath}, |
| 501 |
$^O eq "MSWin32" ? $ENV{INCLUDE} : "", |
| 502 |
$^O eq "cygwin" ? "/usr/include/w32api" : "", |
| 503 |
( |
| 504 |
map { split ' ' } |
| 505 |
grep $_, |
| 506 |
@Config{qw/locincpth incpath/} |
| 507 |
), |
| 508 |
"/usr/include", |
| 509 |
"/usr/local/include", |
| 510 |
_gcc_inc_paths(), |
| 511 |
_dyn_inc_paths(), |
| 512 |
); |
| 513 |
|
| 514 |
if ($req->{incsuffix}) { |
| 515 |
@paths = map File::Spec->catdir($_, $req->{incsuffix}), @paths; |
| 516 |
} |
| 517 |
|
| 518 |
return @paths; |
| 519 |
} |
| 520 |
|
| 521 |
sub _gcc_inc_paths { |
| 522 |
$Config{gccversion} |
| 523 |
or return; |
| 524 |
|
| 525 |
my ($base_version) = $Config{gccversion} =~ /^([0-9]+)/ |
| 526 |
or return; |
| 527 |
|
| 528 |
$base_version >= 4 |
| 529 |
or return; |
| 530 |
|
| 531 |
local $ENV{LANG} = "C"; |
| 532 |
local $ENV{LC_ALL} = "C"; |
| 533 |
my $devnull = File::Spec->devnull; |
| 534 |
my @spam = `$Config{cc} -E -v - <$devnull 2>&1`; |
| 535 |
# output includes lines like: |
| 536 |
# ... |
| 537 |
# ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../x86_64-linux-gnu/include" |
| 538 |
# #include "..." search starts here: |
| 539 |
# #include <...> search starts here: |
| 540 |
# /usr/lib/gcc/x86_64-linux-gnu/4.9/include |
| 541 |
# /usr/local/include |
| 542 |
# /usr/lib/gcc/x86_64-linux-gnu/4.9/include-fixed |
| 543 |
# /usr/include/x86_64-linux-gnu |
| 544 |
# /usr/include |
| 545 |
# End of search list. |
| 546 |
# # 1 "<stdin>" |
| 547 |
# # 1 "<built-in>" |
| 548 |
# ... |
| 549 |
|
| 550 |
while (@spam && $spam[0] !~ /^#include /) { |
| 551 |
shift @spam; |
| 552 |
} |
| 553 |
my @inc; |
| 554 |
while (@spam && $spam[0] !~ /^End of search/) { |
| 555 |
my $line = shift @spam; |
| 556 |
chomp $line; |
| 557 |
next if $line =~ /^#include /; |
| 558 |
next unless $line =~ s/^\s+//; |
| 559 |
push @inc, $line; |
| 560 |
} |
| 561 |
return @inc; |
| 562 |
} |
| 563 |
|
| 564 |
sub _dyn_inc_paths { |
| 565 |
return map { |
| 566 |
my $tmp = $_; |
| 567 |
$tmp =~ s/\blib$/include/ ? $tmp : () |
| 568 |
} _dyn_lib_paths(); |
| 569 |
} |
| 570 |
|
| 571 |
sub _paths { |
| 572 |
my (@in) = @_; |
| 573 |
|
| 574 |
my @out; |
| 575 |
|
| 576 |
# expand any array refs |
| 577 |
@in = map { ref() ? @$_ : $_ } @in; |
| 578 |
|
| 579 |
for my $path (@in) { |
| 580 |
$path or next; |
| 581 |
$path = _tilde_expand($path); |
| 582 |
|
| 583 |
push @out, grep -d $_, split /\Q$Config{path_sep}/, $path; |
| 584 |
} |
| 585 |
|
| 586 |
@out = map Cwd::realpath($_), @out; |
| 587 |
|
| 588 |
my %seen; |
| 589 |
@out = grep !$seen{$_}++, @out; |
| 590 |
|
| 591 |
return @out; |
| 592 |
} |
| 593 |
|
| 594 |
my $home; |
| 595 |
sub _tilde_expand { |
| 596 |
my ($path) = @_; |
| 597 |
|
| 598 |
if ($path =~ m!^~[/\\]!) { |
| 599 |
defined $home or $home = $ENV{HOME}; |
| 600 |
if (!defined $home && $^O eq 'MSWin32' |
| 601 |
&& defined $ENV{HOMEDRIVE} && defined $ENV{HOMEPATH}) { |
| 602 |
$home = $ENV{HOMEDRIVE} . $ENV{HOMEPATH}; |
| 603 |
} |
| 604 |
unless (defined $home) { |
| 605 |
$home = eval { (getpwuid($<))[7] }; |
| 606 |
} |
| 607 |
defined $home or die "You supplied $path, but I can't find your home directory\n"; |
| 608 |
$path =~ s/^~//; |
| 609 |
$path = File::Spec->catdir($home, $path); |
| 610 |
} |
| 611 |
|
| 612 |
return $path; |
| 613 |
} |
| 614 |
|
| 615 |
sub _tcal_state { |
| 616 |
my ($req) = @_; |
| 617 |
my %state; |
| 618 |
|
| 619 |
$state{start_dir} = Cwd::getcwd() |
| 620 |
or die "Cannot save current directory: $!\n"; |
| 621 |
$state{keep} = !!$ENV{IMAGER_PROBE_KEEP_FILES}; |
| 622 |
$state{tmpdir} = File::Temp->newdir(CLEANUP => !$state{keep}) |
| 623 |
or Carp::confess("panic: cannot create temp directory"); |
| 624 |
$state{num} = 1; |
| 625 |
|
| 626 |
\%state; |
| 627 |
} |
| 628 |
|
| 629 |
sub _sort_std { |
| 630 |
my @stds = grep defined, @_; |
| 631 |
# we don't support -std=iso9899:1999 etc |
| 632 |
my $std_map = sub { my $std = shift; return $std > 80 ? $std-100 : $std }; |
| 633 |
return sort { $std_map->($b) <=> $std_map->($a) } grep defined, @stds; |
| 634 |
} |
| 635 |
|
| 636 |
# _try_compile_and_link |
| 637 |
# adapted from Time::HiRes and then adapted some more. |
| 638 |
# This is not part of the Imager::Probe API |
| 639 |
# |
| 640 |
# $code - code to test |
| 641 |
# |
| 642 |
# %args - options to control this test |
| 643 |
# name - required description of what we're testing, used for verbose |
| 644 |
# cc - optional C compiler (overrides $req->{cc} |
| 645 |
# ccflags - optional extra compiler flags |
| 646 |
# ccflags_only - only use ccflags and don't use $req->{ccflags} |
| 647 |
# libs - extra libraries to link to |
| 648 |
# std - C standard, needs stdflag set in $req per by probe_std() |
| 649 |
# nolink - only test compilation success |
| 650 |
# |
| 651 |
# $req contains probing state, used here: |
| 652 |
# verbose - print messages if true |
| 653 |
# stdflag - compiler option used to select a C standard, by probe_std |
| 654 |
# minstd - minimum standard (default: 99) |
| 655 |
# ccflags - base ccflags |
| 656 |
# cc - base cc |
| 657 |
# |
| 658 |
|
| 659 |
sub _try_compile_and_link { |
| 660 |
my ($code, $req, %args) = @_; |
| 661 |
|
| 662 |
my $verbose = $req->{verbose} || 0; |
| 663 |
my $indent = $req->{indent} || ""; |
| 664 |
my $name = $args{name} |
| 665 |
or Carp::confess("panic: no name supplied to _try_compile_and_link"); |
| 666 |
my $state = $req->{tcal_state} ||= _tcal_state($req); |
| 667 |
chdir $state->{tmpdir} |
| 668 |
or die "Cannot chdir to $state->{tmpdir}: $!"; |
| 669 |
my $ok = 0; |
| 670 |
my $base = "test$state->{num}"; |
| 671 |
++$state->{num}; |
| 672 |
|
| 673 |
my $obj_ext = $Config{obj_ext} || ".o"; |
| 674 |
unlink("$base.c", "$base$obj_ext"); |
| 675 |
|
| 676 |
if (open(my $tmpc, '>', "$base.c")) { |
| 677 |
print $tmpc $code; |
| 678 |
close($tmpc); |
| 679 |
|
| 680 |
# we don't need this for our probes |
| 681 |
#my $COREincdir = File::Spec->catdir($Config{'archlibexp'}, 'CORE'); |
| 682 |
|
| 683 |
my $cc = $req->{cc}; |
| 684 |
my $ccflags = ""; |
| 685 |
$ccflags = $req->{ccflags} unless $args{ccflags_only}; |
| 686 |
$ccflags .= " $args{ccflags}" if $args{ccflags}; |
| 687 |
$ccflags .= " $req->{'optimize'}"; |
| 688 |
# we want the maximum std |
| 689 |
my ($std) = _sort_std($args{std}, $req->{minstd}); |
| 690 |
my $stdflag = $args{stdflag} || $req->{stdflag}; |
| 691 |
if ($std && $stdflag) { |
| 692 |
$ccflags .= " $stdflag$std"; |
| 693 |
} |
| 694 |
# we don't need this (yet) |
| 695 |
#. ' ' . "-I$COREincdir" . ' -DPERL_NO_INLINE_FUNCTIONS'; |
| 696 |
|
| 697 |
my $errornull = $verbose ? '' : "2>" . File::Spec->devnull; |
| 698 |
|
| 699 |
my $cccmd; |
| 700 |
my $base_out; |
| 701 |
if ($args{nolink}) { |
| 702 |
$base_out = "$base$obj_ext"; |
| 703 |
$cccmd = "$cc -c -o $base_out $ccflags $base.c $errornull"; |
| 704 |
} |
| 705 |
else { |
| 706 |
my $libs = $args{libs} || ''; |
| 707 |
$base_out = "$base$Config{_exe}"; |
| 708 |
$cccmd = "$cc -o $base_out $ccflags $base.c $libs $errornull"; |
| 709 |
} |
| 710 |
|
| 711 |
printf "${indent}cccmd = $cccmd\n" if $verbose; |
| 712 |
my $res = system($cccmd); |
| 713 |
$ok = defined($res) && $res == 0 && -s $base_out; |
| 714 |
|
| 715 |
if ($ok && !$args{nolink} && !-x $base_out) { |
| 716 |
$ok = 0; |
| 717 |
} |
| 718 |
|
| 719 |
if ( $ok && $args{run} ) { |
| 720 |
# some Imager module probes use files shipped with the module |
| 721 |
# so run from the original directory so they're visible |
| 722 |
chdir $state->{start_dir} |
| 723 |
or die "Cannot return to original current directory $state->{start_dir}: $!\n"; |
| 724 |
|
| 725 |
my $tmp_exe = |
| 726 |
File::Spec->catfile($state->{tmpdir}, $base_out); |
| 727 |
my @run = $tmp_exe; |
| 728 |
unshift @run, $Config{run} if $Config{run} && -e $Config{run}; |
| 729 |
printf "${indent}Running $tmp_exe..." if $verbose; |
| 730 |
if (system(@run) == 0) { |
| 731 |
$ok = 1; |
| 732 |
print "${indent} success\n" if $verbose; |
| 733 |
} else { |
| 734 |
$ok = 0; |
| 735 |
my $errno = $? >> 8; |
| 736 |
local $! = $errno; |
| 737 |
printf <<EOF if $verbose; |
| 738 |
|
| 739 |
${indent}*** The test run of '$tmp_exe' failed: status $? |
| 740 |
${indent}*** (the status means: errno = $errno or '$!') |
| 741 |
EOF |
| 742 |
} |
| 743 |
} |
| 744 |
# now cleaned up with the temp dir |
| 745 |
# unlink("$tmp.c", $tmp_out); |
| 746 |
} |
| 747 |
|
| 748 |
# redundant but harmless if we ran the test executable |
| 749 |
chdir $state->{start_dir} |
| 750 |
or die "Cannot return to original current directory $state->{start_dir}: $!\n"; |
| 751 |
|
| 752 |
|
| 753 |
return $ok; |
| 754 |
} |
| 755 |
|
| 756 |
1; |
| 757 |
|
| 758 |
__END__ |
| 759 |
|
| 760 |
=head1 NAME |
| 761 |
|
| 762 |
Imager::Probe - hot needle of inquiry for libraries |
| 763 |
|
| 764 |
=head1 SYNOPSIS |
| 765 |
|
| 766 |
require Imager::Probe; |
| 767 |
|
| 768 |
my %probe = |
| 769 |
( |
| 770 |
# short name of what we're looking for (displayed to user) |
| 771 |
name => "FOO", |
| 772 |
# pkg-config lookup |
| 773 |
pkg => [ qw/name1 name2 name3/ ], |
| 774 |
# perl subs that probe for the library |
| 775 |
code => [ \&foo_probe1, \&foo_probe2 ], |
| 776 |
# or just: code => \&foo_probe, |
| 777 |
inccheck => sub { ... }, |
| 778 |
libcheck => sub { ... }, |
| 779 |
# search for this library if libcheck not supplied |
| 780 |
libbase => "foo", |
| 781 |
# library link time options, uses libbase to build options otherwise |
| 782 |
libopts => "-lfoo", |
| 783 |
# C code to check the library is sane |
| 784 |
testcode => "...", |
| 785 |
# header files needed |
| 786 |
testcodeheaders => [ "stdio.h", "foo.h" ], |
| 787 |
); |
| 788 |
my $result = Imager::Probe->probe(\%probe) |
| 789 |
or print "Foo library not found: ",Imager::Probe->error; |
| 790 |
|
| 791 |
=head1 DESCRIPTION |
| 792 |
|
| 793 |
Does the probes that were hidden in Imager's F<Makefile.PL>, pulled |
| 794 |
out so the file format libraries can be externalized. |
| 795 |
|
| 796 |
The return value is either nothing if the probe fails, or a hash |
| 797 |
containing: |
| 798 |
|
| 799 |
=over |
| 800 |
|
| 801 |
=item * |
| 802 |
|
| 803 |
C<INC> - C<-I> and other C options |
| 804 |
|
| 805 |
=item * |
| 806 |
|
| 807 |
C<LIBS> - C<-L>, C<-l> and other link-time options |
| 808 |
|
| 809 |
=item * |
| 810 |
|
| 811 |
C<DEFINE> - C<-D> options, if any. |
| 812 |
|
| 813 |
=back |
| 814 |
|
| 815 |
The possible values for the hash supplied to the probe() method are: |
| 816 |
|
| 817 |
=over |
| 818 |
|
| 819 |
=item * |
| 820 |
|
| 821 |
C<pkg> - an array of F<pkg-config> names to probe for. If the |
| 822 |
F<pkg-config> checks pass, C<inccheck> and C<libcheck> aren't used. |
| 823 |
|
| 824 |
=item * |
| 825 |
|
| 826 |
C<inccheck> - a code reference that checks if the supplied include |
| 827 |
directory contains the required header files. |
| 828 |
|
| 829 |
=item * |
| 830 |
|
| 831 |
C<libcheck> - a code reference that checks if the supplied library |
| 832 |
directory contains the required library files. Note: the |
| 833 |
F<Makefile.PL> version of this was supplied all of the library file |
| 834 |
names instead. C<libcheck> can also be an arrayref of library check |
| 835 |
code references, all of which must find a match for the library to be |
| 836 |
considered "found". |
| 837 |
|
| 838 |
=item * |
| 839 |
|
| 840 |
C<libbase> - if C<inccheck> is supplied, but C<libcheck> isn't, then a |
| 841 |
C<libcheck> that checks for C<lib>I<libbase>I<$Config{_a}> and |
| 842 |
C<lib>I<libbase>.I<$Config{so}> is created. If C<libopts> isn't |
| 843 |
supplied then that can be synthesized as C<< -lI<libbase> |
| 844 |
>>. C<libbase> can also be an arrayref of library base names to search |
| 845 |
for, in which case all of the libraries mentioned must be found for |
| 846 |
the probe to succeed. |
| 847 |
|
| 848 |
=item * |
| 849 |
|
| 850 |
C<libopts> - if the libraries are found via C<inccheck>/C<libcheck>, |
| 851 |
these are the C<-l> options to supply during the link phase. |
| 852 |
|
| 853 |
=item * |
| 854 |
|
| 855 |
C<code> - a code reference to perform custom checks. Returns the |
| 856 |
probe result directly. Can also be an array ref of functions to call. |
| 857 |
|
| 858 |
=item * |
| 859 |
|
| 860 |
C<testcode> - test C code that is run with Devel::CheckLib. You also |
| 861 |
need to set C<testcodeheaders>. |
| 862 |
|
| 863 |
=item * |
| 864 |
|
| 865 |
C<testcodeprologue> - C code to insert between the headers and the |
| 866 |
main function. |
| 867 |
|
| 868 |
=item * |
| 869 |
|
| 870 |
C<incpath> - C<$Config{path_sep}> separated list of header file |
| 871 |
directories to check, or a reference to an array of such. |
| 872 |
|
| 873 |
=item * |
| 874 |
|
| 875 |
C<libpath> - C<$Config{path_sep}> separated list of library file |
| 876 |
directories to check, or a reference to an array of such. |
| 877 |
|
| 878 |
=item * |
| 879 |
|
| 880 |
C<alternatives> - an optional array reference of alternate |
| 881 |
configurations (as hash references) to test if the primary |
| 882 |
configuration isn't successful. Each alternative should include an |
| 883 |
C<altname> key describing the alternative. Any key not mentioned in |
| 884 |
an alternative defaults to the value from the main configuration. |
| 885 |
|
| 886 |
=back |
| 887 |
|
| 888 |
=head1 AUTHOR |
| 889 |
|
| 890 |
Tony Cook <[email protected]>, Arnar M. Hrafnkelsson |
| 891 |
|
| 892 |
=cut |