more verbose probe output
[imager.git] / lib / Imager / Probe.pm
CommitLineData
1d7e3124
TC
1package Imager::Probe;
2use strict;
3use File::Spec;
4use Config;
d2f87b00 5use Cwd ();
1d7e3124 6
cac1147d
TC
7my @alt_transfer = qw/altname incsuffix libbase/;
8
1d7e3124
TC
9sub probe {
10 my ($class, $req) = @_;
11
12 $req->{verbose} ||= $ENV{IM_VERBOSE};
13
14 my $name = $req->{name};
15 my $result;
16 if ($req->{code}) {
17 $result = _probe_code($req);
18 }
19 if (!$result && $req->{pkg}) {
20 $result = _probe_pkg($req);
21 }
22 if (!$result && $req->{inccheck} && ($req->{libcheck} || $req->{libbase})) {
cac1147d 23 $req->{altname} ||= "main";
1d7e3124
TC
24 $result = _probe_check($req);
25 }
cac1147d
TC
26 if (!$result && $req->{alternatives}) {
27 ALTCHECK:
28 my $index = 1;
29 for my $alt (@{$req->{alternatives}}) {
a9120a5e 30 $req->{altname} = $alt->{altname} || "alt $index";
cac1147d
TC
31 $req->{verbose}
32 and print "$req->{name}: Trying alternative $index\n";
33 my %work = %$req;
34 for my $key (@alt_transfer) {
35 exists $alt->{$key} and $work{$key} = $alt->{$key};
36 }
37 $result = _probe_check(\%work)
38 and last;
39 ++$index;
40 }
41 }
1d7e3124
TC
42
43 if (!$result && $req->{testcode}) {
44 $result = _probe_fake($req);
45 }
46 $result or return;
47
48 if ($req->{testcode}) {
49 $result = _probe_test($req, $result);
50 }
51
52 $result or return;
53
54 return $result;
55}
56
57sub _probe_code {
58 my ($req) = @_;
59
60 my $code = $req->{code};
61 my @probes = ref $code eq "ARRAY" ? @$code : $code;
62
63 my $result;
64 for my $probe (@probes) {
65 $result = $probe->($req)
66 and return $result;
67 }
68
69 return;
70}
71
72sub is_exe {
73 my ($name) = @_;
74
75 my @exe_suffix = $Config{_exe};
76 if ($^O eq 'MSWin32') {
77 push @exe_suffix, qw/.bat .cmd/;
78 }
79
80 for my $dir (File::Spec->path) {
81 for my $suffix (@exe_suffix) {
82 -x File::Spec->catfile($dir, "$name$suffix")
83 and return 1;
84 }
85 }
86
87 return;
88}
89
90sub _probe_pkg {
91 my ($req) = @_;
92
82f14556
TC
93 # Setup pkg-config's environment variable to search non-standard paths
94 # which may be provided by --libdirs.
95 my @pkgcfg_paths = map { "$_/pkgconfig" } _lib_paths( $req );
96 push @pkgcfg_paths, $ENV{ 'PKG_CONFIG_PATH' } if $ENV{ 'PKG_CONFIG_PATH' };
97
e17b7029 98 local $ENV{ 'PKG_CONFIG_PATH' } = join $Config{path_sep}, @pkgcfg_paths;
82f14556 99
1d7e3124
TC
100 is_exe('pkg-config') or return;
101 my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
102
103 my @pkgs = @{$req->{pkg}};
104 for my $pkg (@pkgs) {
105 if (!system("pkg-config $pkg --exists $redir")) {
106 # if we find it, but the following fail, then pkg-config is too
107 # broken to be useful
108 my $cflags = `pkg-config $pkg --cflags`
109 and !$? or return;
110
111 my $lflags = `pkg-config $pkg --libs`
112 and !$? or return;
113
a9120a5e
TC
114 my $defines = '';
115 $cflags =~ s/(-D\S+)/$defines .= " $1"; ''/ge;
116
1d7e3124
TC
117 chomp $cflags;
118 chomp $lflags;
119 print "$req->{name}: Found via pkg-config $pkg\n";
c3f05aa8
TC
120 print <<EOS if $req->{verbose};
121 cflags: $cflags
122 defines: $defines
123 lflags: $lflags
124EOS
1d7e3124
TC
125 return
126 {
127 INC => $cflags,
128 LIBS => $lflags,
5cbee95a 129 DEFINE => $defines,
1d7e3124
TC
130 };
131 }
132 }
133
134 print "$req->{name}: Not found via pkg-config\n";
135
136 return;
137}
138
ae394c13
TC
139sub _is_msvc {
140 return $Config{cc} eq "cl";
141}
142
143sub _lib_basename {
144 my ($base) = @_;
145
146 if (_is_msvc()) {
147 return $base;
148 }
149 else {
150 return "lib$base";
151 }
152}
153
154sub _lib_option {
155 my ($base) = @_;
156
157 if (_is_msvc()) {
158 return $base . $Config{_a};
159 }
160 else {
161 return "-l$base";
162 }
163}
164
165sub _quotearg {
166 my ($opt) = @_;
167
168 return $opt =~ /\s/ ? qq("$opt") : $opt;
169}
170
1d7e3124
TC
171sub _probe_check {
172 my ($req) = @_;
173
174 my $libcheck = $req->{libcheck};
175 my $libbase = $req->{libbase};
176 if (!$libcheck && $req->{libbase}) {
177 # synthesize a libcheck
178 my $lext=$Config{'so'}; # Get extensions of libraries
179 my $aext=$Config{'_a'};
ae394c13 180 my $basename = _lib_basename($libbase);
1d7e3124 181 $libcheck = sub {
ae394c13
TC
182 -e File::Spec->catfile($_[0], "$basename$aext")
183 || -e File::Spec->catfile($_[0], "$basename.$lext")
1d7e3124
TC
184 };
185 }
186
187 my $found_libpath;
188 my @lib_search = _lib_paths($req);
189 print "$req->{name}: Searching directories for libraries:\n"
190 if $req->{verbose};
191 for my $path (@lib_search) {
192 print "$req->{name}: $path\n" if $req->{verbose};
193 if ($libcheck->($path)) {
194 print "$req->{name}: Found!\n" if $req->{verbose};
195 $found_libpath = $path;
196 last;
197 }
198 }
199
200 my $found_incpath;
201 my $inccheck = $req->{inccheck};
202 my @inc_search = _inc_paths($req);
203 print "$req->{name}: Searching directories for headers:\n"
204 if $req->{verbose};
205 for my $path (@inc_search) {
206 print "$req->{name}: $path\n" if $req->{verbose};
207 if ($inccheck->($path)) {
208 print "$req->{name}: Found!\n" if $req->{verbose};
209 $found_incpath = $path;
210 last;
211 }
212 }
213
cac1147d 214 my $alt = "";
a9120a5e 215 if ($req->{altname}) {
cac1147d
TC
216 $alt = " $req->{altname}:";
217 }
218 print "$req->{name}:$alt includes ", $found_incpath ? "" : "not ",
1d7e3124
TC
219 "found - libraries ", $found_libpath ? "" : "not ", "found\n";
220
221 $found_libpath && $found_incpath
222 or return;
223
224 my @libs = "-L$found_libpath";
225 if ($req->{libopts}) {
226 push @libs, $req->{libopts};
227 }
228 elsif ($libbase) {
ae394c13 229 push @libs, _lib_option($libbase);
1d7e3124
TC
230 }
231 else {
a9120a5e 232 die "$req->{altname}: inccheck but no libbase or libopts";
1d7e3124
TC
233 }
234
235 return
236 {
ae394c13
TC
237 INC => _quotearg("-I$found_incpath"),
238 LIBS => join(" ", map _quotearg($_), @libs),
5cbee95a 239 DEFINE => "",
1d7e3124
TC
240 };
241}
242
243sub _probe_fake {
244 my ($req) = @_;
245
246 # the caller provided test code, and the compiler may look in
247 # places we don't, see Imager-Screenshot ticket 56793,
248 # so fake up a result so the test code can
249 my $lopts;
250 if ($req->{libopts}) {
251 $lopts = $req->{libopts};
252 }
253 elsif (defined $req->{libbase}) {
254 # might not need extra libraries, eg. Win32 perl already links
255 # everything
256 $lopts = $req->{libbase} ? "-l$req->{libbase}" : "";
257 }
258 if (defined $lopts) {
fdc9346d 259 print "$req->{name}: Checking if the compiler can find them on its own\n";
1d7e3124
TC
260 return
261 {
262 INC => "",
263 LIBS => $lopts,
5cbee95a 264 DEFINE => "",
1d7e3124
TC
265 };
266 }
267 else {
268 print "$req->{name}: Can't fake it - no libbase or libopts\n"
269 if $req->{verbose};
270 return;
271 }
272}
273
274sub _probe_test {
275 my ($req, $result) = @_;
276
277 require Devel::CheckLib;
278 # setup LD_RUN_PATH to match link time
c3f05aa8 279 print "Asking liblist for LD_RUN_PATH:\n" if $req->{verbose};
1d7e3124 280 my ($extra, $bs_load, $ld_load, $ld_run_path) =
82f14556 281 ExtUtils::Liblist->ext($result->{LIBS}, $req->{verbose});
1d7e3124
TC
282 local $ENV{LD_RUN_PATH};
283
284 if ($ld_run_path) {
82f14556 285 print "Setting LD_RUN_PATH=$ld_run_path for $req->{name} probe\n"
1d7e3124
TC
286 if $req->{verbose};
287 $ENV{LD_RUN_PATH} = $ld_run_path;
ba751dab
TC
288 if ($Config{lddlflags} =~ /([^ ]*-(?:rpath|R)[,=]?)([^ ]+)/
289 && -d $2) {
290 # hackety, hackety
291 # LD_RUN_PATH is ignored when there's already an -rpath option
292 # so provide one
293 my $prefix = $1;
294 $result->{LDDLFLAGS} = $Config{lddlflags} . " " .
295 join " ", map "$prefix$_", split $Config{path_sep}, $ld_run_path;
296 }
1d7e3124
TC
297 }
298 my $good =
299 Devel::CheckLib::check_lib
300 (
301 debug => $req->{verbose},
177f9b37 302 LIBS => [ $result->{LIBS} ],
1d7e3124
TC
303 INC => $result->{INC},
304 header => $req->{testcodeheaders},
305 function => $req->{testcode},
b73378f5 306 prologue => $req->{testcodeprologue},
1d7e3124
TC
307 );
308 unless ($good) {
5fbba567 309 print "$req->{name}: Test code failed: $@";
1d7e3124
TC
310 return;
311 }
312
313 print "$req->{name}: Passed code check\n";
314 return $result;
315}
316
317sub _lib_paths {
318 my ($req) = @_;
319
320 return _paths
321 (
322 $ENV{IM_LIBPATH},
323 $req->{libpath},
324 (
325 map { split ' ' }
326 grep $_,
6725cca4 327 @Config{qw/loclibpth libpth libspath/}
1d7e3124
TC
328 ),
329 $^O eq "MSWin32" ? $ENV{LIB} : "",
330 $^O eq "cygwin" ? "/usr/lib/w32api" : "",
6725cca4
TC
331 "/usr/lib",
332 "/usr/local/lib",
d2f87b00 333 _gcc_lib_paths(),
1d7e3124
TC
334 );
335}
336
d2f87b00
TC
337sub _gcc_lib_paths {
338 $Config{gccversion}
339 or return;
340
341 my ($base_version) = $Config{gccversion} =~ /^([0-9]+)/
342 or return;
343
344 $base_version >= 4
345 or return;
346
347 my ($lib_line) = grep /^libraries:/, `$Config{cc} -print-search-dirs`
348 or return;
349 $lib_line =~ s/^libraries: =//;
350 chomp $lib_line;
351
352 return grep !/gcc/ && -d, split /:/, $lib_line;
353}
354
1d7e3124
TC
355sub _inc_paths {
356 my ($req) = @_;
357
cac1147d 358 my @paths = _paths
1d7e3124
TC
359 (
360 $ENV{IM_INCPATH},
361 $req->{incpath},
362 $^O eq "MSWin32" ? $ENV{INCLUDE} : "",
363 $^O eq "cygwin" ? "/usr/include/w32api" : "",
364 (
365 map { split ' ' }
366 grep $_,
6725cca4 367 @Config{qw/locincpth incpath/}
1d7e3124
TC
368 ),
369 "/usr/include",
370 "/usr/local/include",
371 );
cac1147d
TC
372
373 if ($req->{incsuffix}) {
374 @paths = map File::Spec->catdir($_, $req->{incsuffix}), @paths;
375 }
376
377 return @paths;
1d7e3124
TC
378}
379
380sub _paths {
381 my (@in) = @_;
382
383 my @out;
384
0c3c1180
TC
385 # expand any array refs
386 @in = map { ref() ? @$_ : $_ } @in;
387
1d7e3124
TC
388 for my $path (@in) {
389 $path or next;
390 $path = _tilde_expand($path);
391
392 push @out, grep -d $_, split /\Q$Config{path_sep}/, $path;
393 }
394
d2f87b00
TC
395 @out = map Cwd::realpath($_), @out;
396
397 my %seen;
398 @out = grep !$seen{$_}++, @out;
399
1d7e3124
TC
400 return @out;
401}
402
403my $home;
404sub _tilde_expand {
405 my ($path) = @_;
406
407 if ($path =~ m!^~[/\\]!) {
408 defined $home or $home = $ENV{HOME};
409 if (!defined $home && $^O eq 'MSWin32'
410 && defined $ENV{HOMEDRIVE} && defined $ENV{HOMEPATH}) {
411 $home = $ENV{HOMEDRIVE} . $ENV{HOMEPATH};
412 }
413 unless (defined $home) {
414 $home = eval { (getpwuid($<))[7] };
415 }
416 defined $home or die "You supplied $path, but I can't find your home directory\n";
417 $path =~ s/^~//;
418 $path = File::Spec->catdir($home, $path);
419 }
420
421 return $path;
422}
423
4241;
425
426__END__
427
428=head1 NAME
429
430Imager::Probe - hot needle of inquiry for libraries
431
432=head1 SYNOPSIS
433
434 require Imager::Probe;
435
436 my %probe =
437 (
438 # short name of what we're looking for (displayed to user)
439 name => "FOO",
440 # pkg-config lookup
441 pkg => [ qw/name1 name2 name3/ ],
442 # perl subs that probe for the library
443 code => [ \&foo_probe1, \&foo_probe2 ],
444 # or just: code => \&foo_probe,
445 inccheck => sub { ... },
446 libcheck => sub { ... },
447 # search for this library if libcheck not supplied
448 libbase => "foo",
449 # library link time options, uses libbase to build options otherwise
450 libopts => "-lfoo",
451 # C code to check the library is sane
452 testcode => "...",
453 # header files needed
454 testcodeheaders => [ "stdio.h", "foo.h" ],
455 );
456 my $result = Imager::Probe->probe(\%probe)
457 or print "Foo library not found: ",Imager::Probe->error;
458
459=head1 DESCRIPTION
460
461Does the probes that were hidden in Imager's F<Makefile.PL>, pulled
462out so the file format libraries can be externalized.
463
464The return value is either nothing if the probe fails, or a hash
465containing:
466
467=over
468
469=item *
470
471C<INC> - C<-I> and other C options
472
473=item *
474
475C<LIBS> - C<-L>, C<-l> and other link-time options
476
5dc99e19
TC
477=item *
478
479C<DEFINE> - C<-D> options, if any.
480
1d7e3124
TC
481=back
482
483The possible values for the hash supplied to the probe() method are:
484
485=over
486
487=item *
488
489C<pkg> - an array of F<pkg-config> names to probe for. If the
490F<pkg-config> checks pass, C<inccheck> and C<libcheck> aren't used.
491
492=item *
493
494C<inccheck> - a code reference that checks if the supplied include
495directory contains the required header files.
496
497=item *
498
499C<libcheck> - a code reference that checks if the supplied library
500directory contains the required library files. Note: the
501F<Makefile.PL> version of this was supplied all of the library file
502names instead.
503
504=item *
505
506C<libbase> - if C<inccheck> is supplied, but C<libcheck> isn't, then a
507C<libcheck> that checks for C<lib>I<libbase>I<$Config{_a}> and
508C<lib>I<libbase>.I<$Config{so}> is created. If C<libopts> isn't
509supplied then that can be synthesized as C<-l>C<<I<libbase>>>.
510
511=item *
512
513C<libopts> - if the libraries are found via C<inccheck>/C<libcheck>,
514these are the C<-l> options to supply during the link phase.
515
516=item *
517
518C<code> - a code reference to perform custom checks. Returns the
519probe result directly. Can also be an array ref of functions to call.
520
521=item *
522
523C<testcode> - test C code that is run with Devel::CheckLib. You also
524need to set C<testcodeheaders>.
525
526=item *
527
b73378f5
TC
528C<testcodeprologue> - C code to insert between the headers and the
529main function.
530
531=item *
532
1d7e3124 533C<incpath> - C<$Config{path_sep}> separated list of header file
0c3c1180 534directories to check, or a reference to an array of such.
1d7e3124
TC
535
536=item *
537
538C<libpath> - C<$Config{path_sep}> separated list of library file
0c3c1180 539directories to check, or a reference to an array of such.
1d7e3124
TC
540
541=back
542
543=cut