initial OS X screenshot support
[imager-screenshot.git] / Makefile.PL
CommitLineData
0ddb7051
TC
1#!perl -w
2use strict;
3use ExtUtils::MakeMaker;
37819a40 4use Imager 0.54;
0ddb7051
TC
5use Imager::ExtUtils;
6use Config;
7use File::Spec;
574c4fe0 8use Getopt::Long;
47f35d89
TC
9use lib "inc";
10use Devel::CheckLib;
574c4fe0
TC
11
12my @incpaths; # places to look for headers
13my @libpaths; # places to look for libraries
14
15GetOptions("incpath=s", \@incpaths,
16 "libpath=s" => \@libpaths);
0ddb7051
TC
17
18my @objs = qw/Screenshot.o/;
19my @cflags;
20my @lflags;
bc99c241 21my @lddlflags;
8a7d6890
TC
22my %seen_incdir;
23my %seen_libdir;
0ddb7051 24my $X11_lib = $^O eq 'cygwin' ? 'X11.dll' : 'X11';
574c4fe0
TC
25if (find_header("X11/Xlib.h", "X11 header")
26 and find_lib($X11_lib, "X11 library")) {
0ddb7051
TC
27 push @objs, 'scx11.o';
28 push @cflags, '-DSS_X11';
29 push @lflags, '-l'.$X11_lib;
30 print "Found X11\n";
31}
574c4fe0 32if (find_header('windows.h', "Win32 header")
47f35d89
TC
33 && find_lib('gdi32', "Win32 library")
34 || check_lib(header => "windows.h",
35 lib => "gdi32",
36 title => "Win32")) {
0ddb7051
TC
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
bc99c241
TC
45if ($^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
0ddb7051 52unless (@objs > 1) {
574c4fe0 53 die <<DEAD;
7f198d6e 54OS unsupported: Headers or libraries not found for a supported GUI
574c4fe0 55
7f198d6e 56Sorry, I can't find headers or libraries for a supported GUI
574c4fe0
TC
57You need to install development headers and libraries for your GUI
58For Win32: Platform SDK or a substitute
59For X11: X11 headers and libraries, eg. the libX11-dev package on Debian
bc99c241 60For OS X: Install Xcode
574c4fe0
TC
61
62DEAD
0ddb7051
TC
63}
64
65my %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
bc99c241
TC
80if (@lddlflags) {
81 $opts{LDDLFLAGS} = $Config{lddlflags} . " @lddlflags";
82}
83
37819a40
TC
84# avoid "... isn't numberic in numeric gt ..." warnings for dev versions
85my $eu_mm_version = eval $ExtUtils::MakeMaker::VERSION;
86if ($eu_mm_version > 6.06) {
0ddb7051
TC
87 $opts{AUTHOR} = 'Tony Cook <tonyc@cpan.org>';
88 $opts{ABSTRACT} = 'Screen/Window capture to Imager images';
89}
90
37819a40
TC
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
54f11a66 94# EXTRA_META was also introduced in 6.30_01
37819a40 95if ($eu_mm_version > 6.3001) {
770c534e 96 $opts{LICENSE} = 'perl';
8cb7800a
TC
97}
98if ($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 };
770c534e
TC
111}
112
0ddb7051
TC
113WriteMakefile(%opts);
114
115my @incs;
116sub header_search_path {
117 @incs and return @incs;
118
574c4fe0
TC
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};
8a7d6890 122 push @incs, '/usr/include', '/usr/X11R6/include'
0ddb7051
TC
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
138my @libs;
139sub library_search_path {
140 @libs and return @libs;
141
574c4fe0
TC
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};
8aca6763 145 push @libs, '/usr/lib', '/usr/X11R6/lib'
0ddb7051
TC
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
0ddb7051
TC
161sub _find_file {
162 my ($name, @where) = @_;
163
164 grep -f File::Spec->catfile($_, $name), @where;
165}
166
167sub find_header {
574c4fe0 168 my ($name, $description) = @_;
8a7d6890
TC
169 my @found = _find_file($_[0], header_search_path());
170
171 if (@found) {
574c4fe0
TC
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";
8a7d6890
TC
177 }
178
179 @found;
0ddb7051
TC
180}
181
182sub find_lib {
574c4fe0 183 my ($name, $description) = shift;
0ddb7051 184 my @found;
574c4fe0 185 my $libname;
0ddb7051 186 if ($^O eq 'MSWin32' && $Config{_a} eq '.lib') {
574c4fe0 187 $libname = $name . $Config{_a};
91eba490 188 @found = _find_file($libname, library_search_path());
0ddb7051
TC
189 }
190 else {
574c4fe0 191 $libname = "lib" . $name . $Config{_a};
91eba490
TC
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 }
0ddb7051
TC
197 }
198 if (@found) {
8a7d6890
TC
199 push @lflags, "-L$_" for grep !$seen_libdir{$_}, @found;
200 @seen_libdir{@found} = (1) x @found;
0ddb7051 201 }
574c4fe0
TC
202 else {
203 print STDERR "Could not find $libname ($description)\n";
204 }
205
0ddb7051
TC
206 @found;
207}
47f35d89
TC
208
209# wrapper around assert lib that doesn't exit and doesn't die
210sub 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}