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