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