Imager is moving to github
[imager.git] / FT2 / Makefile.PL
CommitLineData
50c75381
TC
1#!perl -w
2use strict;
d24404be 3use ExtUtils::MakeMaker;
50c75381
TC
4use Getopt::Long;
5use Config;
6
7my $verbose = $ENV{IM_VERBOSE};
8my @libpaths;
9my @incpaths;
10
11GetOptions("incpath=s", \@incpaths,
12 "libpath=s" => \@libpaths,
13 "verbose|v" => \$verbose);
14
15our $BUILDING_IMAGER;
d97c8dbd 16our %IMAGER_LIBS;
50c75381 17
50c75381
TC
18my %opts =
19 (
20 NAME => 'Imager::Font::FT2',
21 VERSION_FROM => 'FT2.pm',
22 OBJECT => 'FT2.o freetyp2.o',
23 clean => { FILES => 'testout' },
24 );
25
572d5255
TC
26if (eval { ExtUtils::MakeMaker->VERSION('6.46'); 1 }) {
27 $opts{LICENSE} = "perl_5";
28 $opts{AUTHOR} = 'Tony Cook <tonyc@cpan.org>';
29 $opts{ABSTRACT} = 'FreeType 2 font driver for Imager';
30 $opts{META_MERGE} =
31 {
32 'meta-spec' =>
33 {
34 version => "2",
35 url => "https://metacpan.org/pod/CPAN::Meta::Spec",
36 },
37 resources =>
38 {
39 homepage => "http://imager.perl.org/",
40 repository =>
41 {
42 type => "git",
08a0751c 43 url => "git://github.com/tonycoz/imager.git",
a40b0805 44 web => "https://github.com/tonycoz/imager.git",
572d5255
TC
45 },
46 bugtracker =>
47 {
48 web => "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Imager",
49 mailto => 'bug-Imager@rt.cpan.org',
50 },
51 },
52 };
53}
54
50c75381
TC
55my @inc;
56if ($BUILDING_IMAGER) {
57 push @inc, "-I..";
58 unshift @INC, "../lib";
59}
60else {
61 unshift @INC, "inc";
62 print "FreeType 2: building independently\n";
63 require Imager::ExtUtils;
64 push @inc, Imager::ExtUtils->includes;
65 $opts{TYPEMAPS} = [ Imager::ExtUtils->typemap ];
66
67 # Imager required configure through use
5eac8456 68 my @Imager_req = ( Imager => "0.95" );
572d5255
TC
69 if (eval { ExtUtils::MakeMaker->VERSION('6.46'); 1 }) {
70 $opts{META_MERGE}{prereqs} =
50c75381 71 {
572d5255
TC
72 configure =>
73 {
74 requires =>
75 {
76 @Imager_req,
77 },
78 },
79 build =>
50c75381 80 {
572d5255
TC
81 requires =>
82 {
83 @Imager_req,
84 'Scalar::Util' => 1.00,
85 "Test::More" => "0.47",
86 }
50c75381 87 },
572d5255 88 runtime =>
50c75381 89 {
572d5255
TC
90 requires =>
91 {
92 @Imager_req,
93 'Scalar::Util' => 1.00,
94 }
50c75381 95 },
572d5255 96 test =>
50c75381 97 {
572d5255
TC
98 requires =>
99 {
100 "Test::More" => "0.47",
101 'Scalar::Util' => 1.00,
102 }
50c75381
TC
103 },
104 };
105 $opts{PREREQ_PM} =
106 {
107 @Imager_req,
98d6ae27 108 'Scalar::Util' => 1.00,
a5919365 109 XSLoader => 0,
50c75381
TC
110 };
111 }
112}
113
114require Imager::Probe;
115
116my %probe =
117 (
118 name => "FreeType 2",
119 code => \&freetype2_probe_ftconfig,
120 inccheck =>
121 sub { -e File::Spec->catfile($_[0], "freetype/ftbitmap.h") },
122 libbase => "freetype",
123 testcode => _ft2_test_code(),
124 testcodeheaders => [ "stdio.h", "string.h", "ft2build.h" ],
b73378f5 125 testcodeprologue => _ft2_test_code_prologue(),
0c3c1180
TC
126 incpath => \@incpaths,
127 libpath => \@libpaths,
781d9251 128 pkg => [ "freetype2" ],
50c75381
TC
129 alternatives =>
130 [
131 {
132 incsuffix => "freetype2",
133 },
134 {
135 incsuffix => "freetype",
136 },
137 ],
0987aae1 138 verbose => $verbose,
50c75381
TC
139 );
140
141my $probe_res = Imager::Probe->probe(\%probe);
142if ($probe_res) {
d97c8dbd
TC
143 $IMAGER_LIBS{FT2} = 1;
144
50c75381
TC
145 push @inc, $probe_res->{INC};
146 $opts{LIBS} = $probe_res->{LIBS};
03e49392 147 $opts{DEFINE} = $probe_res->{DEFINE};
50c75381 148 $opts{INC} = "@inc";
ba751dab
TC
149 $opts{LDDLFLAGS} = $probe_res->{LDDLFLAGS}
150 if $probe_res->{LDDLFLAGS};
50c75381 151
50c75381
TC
152 WriteMakefile(%opts);
153}
154else {
d97c8dbd
TC
155 $IMAGER_LIBS{FT2} = 0;
156
50c75381 157 if ($BUILDING_IMAGER) {
d24404be 158 ExtUtils::MakeMaker::WriteEmptyMakefile(%opts);
50c75381
TC
159 }
160 else {
161 # fail in good way
162 die "OS unsupported: FreeType 2 headers/libraries not found\n";
163 }
164}
165
166sub _ft2_test_code {
167 return <<'CODE';
b73378f5
TC
168FT_Library library = 0;
169FT_Face face = 0;
170FT_Error error;
171error = FT_Init_FreeType(&library);
172if (error) {
173 fprintf(stderr, "FreeType 2: cannot initialize library: %d\n", error);
174 return 1;
175}
176error = FT_New_Face(library, "fontfiles/dodge.ttf", 0, &face);
177if (error) {
178 fprintf(stderr, "FreeType 2: cannot load font: %d\n", error);
179 return 1;
180}
50c75381
TC
181return 0;
182CODE
183}
184
b73378f5
TC
185sub _ft2_test_code_prologue {
186 return <<'CODE';
187#include FT_FREETYPE_H
188
189CODE
190}
191
50c75381
TC
192sub is_exe {
193 my ($name) = @_;
194
195 my @exe_suffix = $Config{_exe};
196 if ($^O eq 'MSWin32') {
197 push @exe_suffix, qw/.bat .cmd/;
198 }
c9eba720
TC
199 elsif ($^O eq 'cygwin') {
200 push @exe_suffix, "";
201 }
50c75381
TC
202
203 for my $dir (File::Spec->path) {
204 for my $suffix (@exe_suffix) {
8eb5a69c 205 -x File::Spec->catfile($dir, "$name$suffix")
50c75381
TC
206 and return 1;
207 }
208 }
209
210 return;
211}
212
213# probes for freetype2 by trying to run freetype-config
214sub freetype2_probe_ftconfig {
215 my ($req) = @_;
216
217 is_exe('freetype-config') or return;
218
219 my $cflags = `freetype-config --cflags`
220 and !$? or return;
221 chomp $cflags;
222
223 my $lflags = `freetype-config --libs`
224 and !$? or return;
225 chomp $lflags;
226
227 # before 2.1.5 freetype-config --cflags could output
228 # the -I options in the wrong order, causing a conflict with
229 # freetype1.x installed with the same --prefix
230 #
231 # can happen iff:
232 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
233 # in that order
234 # - freetype 1.x headers are in prefix/include/freetype
235 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
236 if (@incdirs == 2
237 && $incdirs[1] eq "$incdirs[0]/freetype2"
238 && -e "$incdirs[0]/freetype/freetype.h"
239 && -e "$incdirs[0]/freetype/fterrid.h") {
240 print "** freetype-config provided -I options out of order, correcting\n"
241 if $verbose;
242 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
243 map "-I$_", reverse @incdirs);
244 }
245
246 print "$req->{name}: configured via freetype-config\n";
247
248 return
249 {
250 INC => $cflags,
251 LIBS => $lflags,
252 };
253}