0.84_01 development release
[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 $MM_ver = eval $ExtUtils::MakeMaker::VERSION;
19
20my %opts =
21 (
22 NAME => 'Imager::Font::FT2',
23 VERSION_FROM => 'FT2.pm',
24 OBJECT => 'FT2.o freetyp2.o',
25 clean => { FILES => 'testout' },
26 );
27
28my @inc;
29if ($BUILDING_IMAGER) {
30 push @inc, "-I..";
31 unshift @INC, "../lib";
32}
33else {
34 unshift @INC, "inc";
35 print "FreeType 2: building independently\n";
36 require Imager::ExtUtils;
37 push @inc, Imager::ExtUtils->includes;
38 $opts{TYPEMAPS} = [ Imager::ExtUtils->typemap ];
39
40 # Imager required configure through use
fa61d195 41 my @Imager_req = ( Imager => "0.84_01" );
50c75381
TC
42 if ($MM_ver >= 6.46) {
43 $opts{META_MERGE} =
44 {
45 configure_requires =>
46 {
47 @Imager_req,
48 },
49 build_requires =>
50 {
51 @Imager_req,
52 "Test::More" => "0.47",
53 },
54 resources =>
55 {
56 homepage => "http://imager.perl.org/",
04b6f77c
TC
57 repository => "git://git.imager.perl.org/imager.git",
58 bugtracker => "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Imager",
50c75381
TC
59 },
60 };
61 $opts{PREREQ_PM} =
62 {
63 @Imager_req,
64 };
65 }
66}
67
68require Imager::Probe;
69
70my %probe =
71 (
72 name => "FreeType 2",
73 code => \&freetype2_probe_ftconfig,
74 inccheck =>
75 sub { -e File::Spec->catfile($_[0], "freetype/ftbitmap.h") },
76 libbase => "freetype",
77 testcode => _ft2_test_code(),
78 testcodeheaders => [ "stdio.h", "string.h", "ft2build.h" ],
b73378f5 79 testcodeprologue => _ft2_test_code_prologue(),
0c3c1180
TC
80 incpath => \@incpaths,
81 libpath => \@libpaths,
50c75381
TC
82 alternatives =>
83 [
84 {
85 incsuffix => "freetype2",
86 },
87 {
88 incsuffix => "freetype",
89 },
90 ],
91 );
92
93my $probe_res = Imager::Probe->probe(\%probe);
94if ($probe_res) {
d97c8dbd
TC
95 $IMAGER_LIBS{FT2} = 1;
96
50c75381
TC
97 push @inc, $probe_res->{INC};
98 $opts{LIBS} = $probe_res->{LIBS};
03e49392 99 $opts{DEFINE} = $probe_res->{DEFINE};
50c75381 100 $opts{INC} = "@inc";
ba751dab
TC
101 $opts{LDDLFLAGS} = $probe_res->{LDDLFLAGS}
102 if $probe_res->{LDDLFLAGS};
50c75381
TC
103
104 if ($MM_ver > 6.06) {
5b480b14 105 $opts{AUTHOR} = 'Tony Cook <tonyc@cpan.org>';
50c75381
TC
106 $opts{ABSTRACT} = 'FreeType 2 font driver for Imager';
107 }
108
109 WriteMakefile(%opts);
110}
111else {
d97c8dbd
TC
112 $IMAGER_LIBS{FT2} = 0;
113
50c75381 114 if ($BUILDING_IMAGER) {
d24404be 115 ExtUtils::MakeMaker::WriteEmptyMakefile(%opts);
50c75381
TC
116 }
117 else {
118 # fail in good way
119 die "OS unsupported: FreeType 2 headers/libraries not found\n";
120 }
121}
122
123sub _ft2_test_code {
124 return <<'CODE';
b73378f5
TC
125FT_Library library = 0;
126FT_Face face = 0;
127FT_Error error;
128error = FT_Init_FreeType(&library);
129if (error) {
130 fprintf(stderr, "FreeType 2: cannot initialize library: %d\n", error);
131 return 1;
132}
133error = FT_New_Face(library, "fontfiles/dodge.ttf", 0, &face);
134if (error) {
135 fprintf(stderr, "FreeType 2: cannot load font: %d\n", error);
136 return 1;
137}
50c75381
TC
138return 0;
139CODE
140}
141
b73378f5
TC
142sub _ft2_test_code_prologue {
143 return <<'CODE';
144#include FT_FREETYPE_H
145
146CODE
147}
148
50c75381
TC
149sub is_exe {
150 my ($name) = @_;
151
152 my @exe_suffix = $Config{_exe};
153 if ($^O eq 'MSWin32') {
154 push @exe_suffix, qw/.bat .cmd/;
155 }
156
157 for my $dir (File::Spec->path) {
158 for my $suffix (@exe_suffix) {
8eb5a69c 159 -x File::Spec->catfile($dir, "$name$suffix")
50c75381
TC
160 and return 1;
161 }
162 }
163
164 return;
165}
166
167# probes for freetype2 by trying to run freetype-config
168sub freetype2_probe_ftconfig {
169 my ($req) = @_;
170
171 is_exe('freetype-config') or return;
172
173 my $cflags = `freetype-config --cflags`
174 and !$? or return;
175 chomp $cflags;
176
177 my $lflags = `freetype-config --libs`
178 and !$? or return;
179 chomp $lflags;
180
181 # before 2.1.5 freetype-config --cflags could output
182 # the -I options in the wrong order, causing a conflict with
183 # freetype1.x installed with the same --prefix
184 #
185 # can happen iff:
186 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
187 # in that order
188 # - freetype 1.x headers are in prefix/include/freetype
189 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
190 if (@incdirs == 2
191 && $incdirs[1] eq "$incdirs[0]/freetype2"
192 && -e "$incdirs[0]/freetype/freetype.h"
193 && -e "$incdirs[0]/freetype/fterrid.h") {
194 print "** freetype-config provided -I options out of order, correcting\n"
195 if $verbose;
196 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
197 map "-I$_", reverse @incdirs);
198 }
199
200 print "$req->{name}: configured via freetype-config\n";
201
202 return
203 {
204 INC => $cflags,
205 LIBS => $lflags,
206 };
207}