]> git.imager.perl.org - imager.git/blobdiff - lib/Imager/Probe.pm
move t1lib font support to a separate module
[imager.git] / lib / Imager / Probe.pm
index 8e0c3274f53d9ab9ec663928dd80ecc076d2738d..dd8e8f65e09adf286532f964d3da62d53c12c569 100644 (file)
@@ -3,6 +3,8 @@ use strict;
 use File::Spec;
 use Config;
 
+my @alt_transfer = qw/altname incsuffix libbase/;
+
 sub probe {
   my ($class, $req) = @_;
 
@@ -17,8 +19,25 @@ sub probe {
     $result = _probe_pkg($req);
   }
   if (!$result && $req->{inccheck} && ($req->{libcheck} || $req->{libbase})) {
+    $req->{altname} ||= "main";
     $result = _probe_check($req);
   }
+  if (!$result && $req->{alternatives}) {
+  ALTCHECK:
+    my $index = 1;
+    for my $alt (@{$req->{alternatives}}) {
+      $req->{altname} = $alt->{altname} || "alt $index";
+      $req->{verbose}
+       and print "$req->{name}: Trying alternative $index\n";
+      my %work = %$req;
+      for my $key (@alt_transfer) {
+       exists $alt->{$key} and $work{$key} = $alt->{$key};
+      }
+      $result = _probe_check(\%work)
+       and last;
+      ++$index;
+    }
+  }
 
   if (!$result && $req->{testcode}) {
     $result = _probe_fake($req);
@@ -91,6 +110,9 @@ sub _probe_pkg {
       my $lflags = `pkg-config $pkg --libs`
        and !$? or return;
 
+      my $defines = '';
+      $cflags =~ s/(-D\S+)/$defines .= " $1"; ''/ge;
+
       chomp $cflags;
       chomp $lflags;
       print "$req->{name}: Found via pkg-config $pkg\n";
@@ -98,6 +120,7 @@ sub _probe_pkg {
        {
         INC => $cflags,
         LIBS => $lflags,
+        DEFINE => $defines,
        };
     }
   }
@@ -149,7 +172,11 @@ sub _probe_check {
     }
   }
 
-  print "$req->{name}: includes ", $found_incpath ? "" : "not ",
+  my $alt = "";
+  if ($req->{altname}) {
+    $alt = " $req->{altname}:";
+  }
+  print "$req->{name}:$alt includes ", $found_incpath ? "" : "not ",
     "found - libraries ", $found_libpath ? "" : "not ", "found\n";
 
   $found_libpath && $found_incpath
@@ -163,13 +190,14 @@ sub _probe_check {
     push @libs, "-l$libbase";
   }
   else {
-    die "$req->{name}: inccheck but no libbase or libopts";
+    die "$req->{altname}: inccheck but no libbase or libopts";
   }
 
   return
     {
      INC => "-I$found_incpath",
      LIBS => "@libs",
+     DEFINE => "",
     };
 }
 
@@ -189,11 +217,12 @@ sub _probe_fake {
     $lopts = $req->{libbase} ? "-l$req->{libbase}" : "";
   }
   if (defined $lopts) {
-    print "$req->{name}: Checking if the compiler can find them on it's own\n";
+    print "$req->{name}: Checking if the compiler can find them on its own\n";
     return
       {
        INC => "",
        LIBS => $lopts,
+       DEFINE => "",
       };
   }
   else {
@@ -225,6 +254,7 @@ sub _probe_test {
         INC => $result->{INC},
         header => $req->{testcodeheaders},
         function => $req->{testcode},
+        prologue => $req->{testcodeprologue},
        );
   unless ($good) {
     print "$req->{name}: Test code failed: $@";
@@ -245,17 +275,19 @@ sub _lib_paths {
      (
       map { split ' ' }
       grep $_,
-      @Config{qw/loclibpath libpth libspath/}
+      @Config{qw/loclibpth libpth libspath/}
      ),
      $^O eq "MSWin32" ? $ENV{LIB} : "",
      $^O eq "cygwin" ? "/usr/lib/w32api" : "",
+     "/usr/lib",
+     "/usr/local/lib",
     );
 }
 
 sub _inc_paths {
   my ($req) = @_;
 
-  return _paths
+  my @paths = _paths
     (
      $ENV{IM_INCPATH},
      $req->{incpath},
@@ -264,11 +296,17 @@ sub _inc_paths {
      (
       map { split ' ' }
       grep $_,
-      @Config{qw/locincpath incpath/}
+      @Config{qw/locincpth incpath/}
      ),
      "/usr/include",
      "/usr/local/include",
     );
+
+  if ($req->{incsuffix}) {
+    @paths = map File::Spec->catdir($_, $req->{incsuffix}), @paths;
+  }
+
+  return @paths;
 }
 
 sub _paths {
@@ -276,6 +314,9 @@ sub _paths {
 
   my @out;
 
+  # expand any array refs
+  @in = map { ref() ? @$_ : $_ } @in;
+
   for my $path (@in) {
     $path or next;
     $path = _tilde_expand($path);
@@ -360,6 +401,10 @@ C<INC> - C<-I> and other C options
 
 C<LIBS> - C<-L>, C<-l> and other link-time options
 
+=item *
+
+C<DEFINE> - C<-D> options, if any.
+
 =back
 
 The possible values for the hash supplied to the probe() method are:
@@ -407,13 +452,18 @@ need to set C<testcodeheaders>.
 
 =item *
 
+C<testcodeprologue> - C code to insert between the headers and the
+main function.
+
+=item *
+
 C<incpath> - C<$Config{path_sep}> separated list of header file
-directories to check.
+directories to check, or a reference to an array of such.
 
 =item *
 
 C<libpath> - C<$Config{path_sep}> separated list of library file
-directories to check.
+directories to check, or a reference to an array of such.
 
 =back