6 my @alt_transfer = qw/altname incsuffix libbase/;
9 my ($class, $req) = @_;
11 $req->{verbose} ||= $ENV{IM_VERBOSE};
13 my $name = $req->{name};
16 $result = _probe_code($req);
18 if (!$result && $req->{pkg}) {
19 $result = _probe_pkg($req);
21 if (!$result && $req->{inccheck} && ($req->{libcheck} || $req->{libbase})) {
22 $req->{altname} ||= "main";
23 $result = _probe_check($req);
25 if (!$result && $req->{alternatives}) {
28 for my $alt (@{$req->{alternatives}}) {
29 $req->{altname} = $alt->{altname} || "alt $index";
31 and print "$req->{name}: Trying alternative $index\n";
33 for my $key (@alt_transfer) {
34 exists $alt->{$key} and $work{$key} = $alt->{$key};
36 $result = _probe_check(\%work)
42 if (!$result && $req->{testcode}) {
43 $result = _probe_fake($req);
47 if ($req->{testcode}) {
48 $result = _probe_test($req, $result);
59 my $code = $req->{code};
60 my @probes = ref $code eq "ARRAY" ? @$code : $code;
63 for my $probe (@probes) {
64 $result = $probe->($req)
74 my @exe_suffix = $Config{_exe};
75 if ($^O eq 'MSWin32') {
76 push @exe_suffix, qw/.bat .cmd/;
79 for my $dir (File::Spec->path) {
80 for my $suffix (@exe_suffix) {
81 -x File::Spec->catfile($dir, "$name$suffix")
92 # Setup pkg-config's environment variable to search non-standard paths
93 # which may be provided by --libdirs.
94 my @pkgcfg_paths = map { "$_/pkgconfig" } _lib_paths( $req );
95 push @pkgcfg_paths, $ENV{ 'PKG_CONFIG_PATH' } if $ENV{ 'PKG_CONFIG_PATH' };
97 local $ENV{ 'PKG_CONFIG_PATH' } = join $Config{path_sep}, @pkgcfg_paths;
99 is_exe('pkg-config') or return;
100 my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
102 my @pkgs = @{$req->{pkg}};
103 for my $pkg (@pkgs) {
104 if (!system("pkg-config $pkg --exists $redir")) {
105 # if we find it, but the following fail, then pkg-config is too
106 # broken to be useful
107 my $cflags = `pkg-config $pkg --cflags`
110 my $lflags = `pkg-config $pkg --libs`
114 $cflags =~ s/(-D\S+)/$defines .= " $1"; ''/ge;
118 print "$req->{name}: Found via pkg-config $pkg\n";
128 print "$req->{name}: Not found via pkg-config\n";
136 my $libcheck = $req->{libcheck};
137 my $libbase = $req->{libbase};
138 if (!$libcheck && $req->{libbase}) {
139 # synthesize a libcheck
140 my $lext=$Config{'so'}; # Get extensions of libraries
141 my $aext=$Config{'_a'};
143 -e File::Spec->catfile($_[0], "lib$libbase$aext")
144 || -e File::Spec->catfile($_[0], "lib$libbase.$lext")
149 my @lib_search = _lib_paths($req);
150 print "$req->{name}: Searching directories for libraries:\n"
152 for my $path (@lib_search) {
153 print "$req->{name}: $path\n" if $req->{verbose};
154 if ($libcheck->($path)) {
155 print "$req->{name}: Found!\n" if $req->{verbose};
156 $found_libpath = $path;
162 my $inccheck = $req->{inccheck};
163 my @inc_search = _inc_paths($req);
164 print "$req->{name}: Searching directories for headers:\n"
166 for my $path (@inc_search) {
167 print "$req->{name}: $path\n" if $req->{verbose};
168 if ($inccheck->($path)) {
169 print "$req->{name}: Found!\n" if $req->{verbose};
170 $found_incpath = $path;
176 if ($req->{altname}) {
177 $alt = " $req->{altname}:";
179 print "$req->{name}:$alt includes ", $found_incpath ? "" : "not ",
180 "found - libraries ", $found_libpath ? "" : "not ", "found\n";
182 $found_libpath && $found_incpath
185 my @libs = "-L$found_libpath";
186 if ($req->{libopts}) {
187 push @libs, $req->{libopts};
190 push @libs, "-l$libbase";
193 die "$req->{altname}: inccheck but no libbase or libopts";
198 INC => "-I$found_incpath",
207 # the caller provided test code, and the compiler may look in
208 # places we don't, see Imager-Screenshot ticket 56793,
209 # so fake up a result so the test code can
211 if ($req->{libopts}) {
212 $lopts = $req->{libopts};
214 elsif (defined $req->{libbase}) {
215 # might not need extra libraries, eg. Win32 perl already links
217 $lopts = $req->{libbase} ? "-l$req->{libbase}" : "";
219 if (defined $lopts) {
220 print "$req->{name}: Checking if the compiler can find them on its own\n";
229 print "$req->{name}: Can't fake it - no libbase or libopts\n"
236 my ($req, $result) = @_;
238 require Devel::CheckLib;
239 # setup LD_RUN_PATH to match link time
240 my ($extra, $bs_load, $ld_load, $ld_run_path) =
241 ExtUtils::Liblist->ext($result->{LIBS}, $req->{verbose});
242 local $ENV{LD_RUN_PATH};
245 print "Setting LD_RUN_PATH=$ld_run_path for $req->{name} probe\n"
247 $ENV{LD_RUN_PATH} = $ld_run_path;
250 Devel::CheckLib::check_lib
252 debug => $req->{verbose},
253 LIBS => [ $result->{LIBS} ],
254 INC => $result->{INC},
255 header => $req->{testcodeheaders},
256 function => $req->{testcode},
257 prologue => $req->{testcodeprologue},
260 print "$req->{name}: Test code failed: $@";
264 print "$req->{name}: Passed code check\n";
278 @Config{qw/loclibpth libpth libspath/}
280 $^O eq "MSWin32" ? $ENV{LIB} : "",
281 $^O eq "cygwin" ? "/usr/lib/w32api" : "",
294 $^O eq "MSWin32" ? $ENV{INCLUDE} : "",
295 $^O eq "cygwin" ? "/usr/include/w32api" : "",
299 @Config{qw/locincpth incpath/}
302 "/usr/local/include",
305 if ($req->{incsuffix}) {
306 @paths = map File::Spec->catdir($_, $req->{incsuffix}), @paths;
317 # expand any array refs
318 @in = map { ref() ? @$_ : $_ } @in;
322 $path = _tilde_expand($path);
324 push @out, grep -d $_, split /\Q$Config{path_sep}/, $path;
334 if ($path =~ m!^~[/\\]!) {
335 defined $home or $home = $ENV{HOME};
336 if (!defined $home && $^O eq 'MSWin32'
337 && defined $ENV{HOMEDRIVE} && defined $ENV{HOMEPATH}) {
338 $home = $ENV{HOMEDRIVE} . $ENV{HOMEPATH};
340 unless (defined $home) {
341 $home = eval { (getpwuid($<))[7] };
343 defined $home or die "You supplied $path, but I can't find your home directory\n";
345 $path = File::Spec->catdir($home, $path);
357 Imager::Probe - hot needle of inquiry for libraries
361 require Imager::Probe;
365 # short name of what we're looking for (displayed to user)
368 pkg => [ qw/name1 name2 name3/ ],
369 # perl subs that probe for the library
370 code => [ \&foo_probe1, \&foo_probe2 ],
371 # or just: code => \&foo_probe,
372 inccheck => sub { ... },
373 libcheck => sub { ... },
374 # search for this library if libcheck not supplied
376 # library link time options, uses libbase to build options otherwise
378 # C code to check the library is sane
380 # header files needed
381 testcodeheaders => [ "stdio.h", "foo.h" ],
383 my $result = Imager::Probe->probe(\%probe)
384 or print "Foo library not found: ",Imager::Probe->error;
388 Does the probes that were hidden in Imager's F<Makefile.PL>, pulled
389 out so the file format libraries can be externalized.
391 The return value is either nothing if the probe fails, or a hash
398 C<INC> - C<-I> and other C options
402 C<LIBS> - C<-L>, C<-l> and other link-time options
406 C<DEFINE> - C<-D> options, if any.
410 The possible values for the hash supplied to the probe() method are:
416 C<pkg> - an array of F<pkg-config> names to probe for. If the
417 F<pkg-config> checks pass, C<inccheck> and C<libcheck> aren't used.
421 C<inccheck> - a code reference that checks if the supplied include
422 directory contains the required header files.
426 C<libcheck> - a code reference that checks if the supplied library
427 directory contains the required library files. Note: the
428 F<Makefile.PL> version of this was supplied all of the library file
433 C<libbase> - if C<inccheck> is supplied, but C<libcheck> isn't, then a
434 C<libcheck> that checks for C<lib>I<libbase>I<$Config{_a}> and
435 C<lib>I<libbase>.I<$Config{so}> is created. If C<libopts> isn't
436 supplied then that can be synthesized as C<-l>C<<I<libbase>>>.
440 C<libopts> - if the libraries are found via C<inccheck>/C<libcheck>,
441 these are the C<-l> options to supply during the link phase.
445 C<code> - a code reference to perform custom checks. Returns the
446 probe result directly. Can also be an array ref of functions to call.
450 C<testcode> - test C code that is run with Devel::CheckLib. You also
451 need to set C<testcodeheaders>.
455 C<testcodeprologue> - C code to insert between the headers and the
460 C<incpath> - C<$Config{path_sep}> separated list of header file
461 directories to check, or a reference to an array of such.
465 C<libpath> - C<$Config{path_sep}> separated list of library file
466 directories to check, or a reference to an array of such.