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