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