look for .so files when we can't find the .a (adjusted based on %Config)
[imager-screenshot.git] / Makefile.PL
1 #!perl -w
2 use strict;
3 use ExtUtils::MakeMaker;
4 use Imager 0.54;
5 use Imager::ExtUtils;
6 use Config;
7 use File::Spec;
8 use Getopt::Long;
9
10 my @incpaths; # places to look for headers
11 my @libpaths; # places to look for libraries
12
13 GetOptions("incpath=s", \@incpaths,
14            "libpath=s" => \@libpaths);
15
16 my @objs = qw/Screenshot.o/;
17 my @cflags;
18 my @lflags;
19 my %seen_incdir;
20 my %seen_libdir;
21 my $X11_lib = $^O eq 'cygwin' ? 'X11.dll' : 'X11';
22 if (find_header("X11/Xlib.h", "X11 header") 
23     and find_lib($X11_lib, "X11 library")) {
24   push @objs, 'scx11.o';
25   push @cflags, '-DSS_X11';
26   push @lflags, '-l'.$X11_lib;
27   print "Found X11\n";
28 }
29 if (find_header('windows.h', "Win32 header")
30     and find_lib('gdi32', "Win32 library")) {
31   push @objs, 'scwin32.o';
32   push @cflags, '-DSS_WIN32';
33   if ($^O eq 'cygwin') {
34     push @lflags, '-L/usr/lib/w32api', '-lgdi32';
35   }
36   print "Found Win32\n";
37 }
38
39 unless (@objs > 1) {
40   die <<DEAD;
41
42 NA: Sorry, I can't find headers or libraries for a supported GUI
43 You need to install development headers and libraries for your GUI
44 For Win32: Platform SDK or a substitute
45 For X11: X11 headers and libraries, eg. the libX11-dev package on Debian
46
47 DEAD
48 }
49
50 my %opts = 
51   (
52    NAME => 'Imager::Screenshot',
53    VERSION_FROM => 'Screenshot.pm',
54    OBJECT => "@objs",
55    PREREQ_PM => {
56                  'Imager'    => 0.54,
57                 },
58    INC => Imager::ExtUtils->includes,
59    TYPEMAPS => [ Imager::ExtUtils->typemap ],
60   );
61
62 $opts{LIBS} = "@lflags" if @lflags;
63 $opts{INC} .= " @cflags" if @cflags;
64
65 # avoid "... isn't numberic in numeric gt ..." warnings for dev versions
66 my $eu_mm_version = eval $ExtUtils::MakeMaker::VERSION;
67 if ($eu_mm_version > 6.06) {
68   $opts{AUTHOR} = 'Tony Cook <tonyc@cpan.org>';
69   $opts{ABSTRACT} = 'Screen/Window capture to Imager images';
70 }
71
72 # LICENSE was introduced in 6.30_01, but Debian etch includes
73 # (as of 2007/01/12) an ExtUtils::MakeMaker versioned 6.30_01 without
74 # LICENSE support
75 # EXTRA_META was also introduced in 6.30_01
76 if ($eu_mm_version > 6.3001) {
77   $opts{LICENSE} = 'perl';
78   $opts{EXTRA_META} = <<META;
79 configure_requires:
80   Imager: 0.54
81 build_requires:
82   Imager: 0.54
83   Test::More: 0.47
84 META
85 }
86
87 WriteMakefile(%opts);
88
89 my @incs;
90 sub header_search_path {
91   @incs and return @incs;
92
93   push @incs, map {; split /\Q$Config{path_sep}/ } @incpaths;
94   push @incs, split /\Q$Config{path_sep}/, $ENV{IM_INCPATH}
95     if defined $ENV{IM_INCPATH};
96   push @incs, '/usr/include', '/usr/X11R6/include'
97     unless $^O eq 'MSWin32' && $Config{cc} =~ /\bcl\b/;
98   push @incs, split /\Q$Config{path_sep}/, $ENV{INCLUDE}
99     if $^O eq 'MSWin32' && $Config{cc} =~ /\bcl\b/ and $ENV{INCLUDE};
100   push @incs, split ' ', $Config{locincpth}
101     if $Config{locincpth};
102   push @incs, split /\Q$Config{path_sep}/, $Config{incpath}
103     if $Config{incpath};
104   push @incs, '/usr/include/w32api', '/usr/X11R6/include'
105     if $^O eq 'cygwin';
106
107   @incs = grep -d, @incs;
108
109   @incs;
110 }
111
112 my @libs;
113 sub library_search_path {
114   @libs and return @libs;
115
116   push @libs, map {; split /\Q$Config{path_sep}/ } @libpaths;
117   push @incs, split /\Q$Config{path_sep}/, $ENV{IM_LIBPATH}
118     if defined $ENV{IM_LIBPATH};
119   push @libs, '/usr/lib', '/usr/X11R6/lib'
120     unless $^O eq 'MSWin32' && $Config{cc} =~ /\bcl\b/;
121   push @libs, split /\Q$Config{path_sep}/, $ENV{LIB}
122     if $^O eq 'MSWin32' && $Config{cc} =~ /\bcl\b/ and $ENV{LIB};
123   push @libs, split ' ', $Config{loclibpth}
124     if $Config{loclibpth};
125   push @libs, split /\Q$Config{path_sep}/, $Config{libpth}
126     if $Config{libpth};
127   push @libs, '/usr/lib/w32api', '/usr/X11R6/lib'
128     if $^O eq 'cygwin';
129
130   @libs = grep -d, @libs;
131
132   @libs;
133 }
134
135 sub _find_file {
136   my ($name, @where) = @_;
137
138   grep -f File::Spec->catfile($_, $name), @where;
139 }
140
141 sub find_header {
142   my ($name, $description) = @_;
143   my @found = _find_file($_[0], header_search_path());
144
145   if (@found) {
146     push @cflags, "-I$_" for grep !$seen_incdir{$_}, @found;
147     @seen_incdir{@found} = (1) x @found;
148   }
149   else {
150     print STDERR "Could not find $name ($description)\n";
151   }
152
153   @found;
154 }
155
156 sub find_lib {
157   my ($name, $description) = shift;
158   my @found;
159   my $libname;
160   if ($^O eq 'MSWin32' && $Config{_a} eq '.lib') {
161     $libname = $name . $Config{_a};
162     @found = _find_file($libname, library_search_path());
163   }
164   else {
165     $libname = "lib" . $name . $Config{_a};
166     @found = _find_file($libname, library_search_path());
167     if (!@found && $Config{so}) {
168       $libname = "lib" . $name . "." . $Config{so};
169       @found = _find_file($libname, library_search_path());
170     }
171   }
172   if (@found) {
173     push @lflags, "-L$_" for grep !$seen_libdir{$_}, @found;
174     @seen_libdir{@found} = (1) x @found;
175   }
176   else {
177     print STDERR "Could not find $libname ($description)\n";
178   }
179
180   @found;
181 }