fix channel output for FT2
[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
06115805 41 my @Imager_req = ( Imager => "0.94" );
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,
98d6ae27 64 'Scalar::Util' => 1.00,
a5919365 65 XSLoader => 0,
50c75381
TC
66 };
67 }
68}
69
70require Imager::Probe;
71
72my %probe =
73 (
74 name => "FreeType 2",
75 code => \&freetype2_probe_ftconfig,
76 inccheck =>
77 sub { -e File::Spec->catfile($_[0], "freetype/ftbitmap.h") },
78 libbase => "freetype",
79 testcode => _ft2_test_code(),
80 testcodeheaders => [ "stdio.h", "string.h", "ft2build.h" ],
b73378f5 81 testcodeprologue => _ft2_test_code_prologue(),
0c3c1180
TC
82 incpath => \@incpaths,
83 libpath => \@libpaths,
50c75381
TC
84 alternatives =>
85 [
86 {
87 incsuffix => "freetype2",
88 },
89 {
90 incsuffix => "freetype",
91 },
92 ],
0987aae1 93 verbose => $verbose,
50c75381
TC
94 );
95
96my $probe_res = Imager::Probe->probe(\%probe);
97if ($probe_res) {
d97c8dbd
TC
98 $IMAGER_LIBS{FT2} = 1;
99
50c75381
TC
100 push @inc, $probe_res->{INC};
101 $opts{LIBS} = $probe_res->{LIBS};
03e49392 102 $opts{DEFINE} = $probe_res->{DEFINE};
50c75381 103 $opts{INC} = "@inc";
ba751dab
TC
104 $opts{LDDLFLAGS} = $probe_res->{LDDLFLAGS}
105 if $probe_res->{LDDLFLAGS};
50c75381
TC
106
107 if ($MM_ver > 6.06) {
5b480b14 108 $opts{AUTHOR} = 'Tony Cook <tonyc@cpan.org>';
50c75381
TC
109 $opts{ABSTRACT} = 'FreeType 2 font driver for Imager';
110 }
111
112 WriteMakefile(%opts);
113}
114else {
d97c8dbd
TC
115 $IMAGER_LIBS{FT2} = 0;
116
50c75381 117 if ($BUILDING_IMAGER) {
d24404be 118 ExtUtils::MakeMaker::WriteEmptyMakefile(%opts);
50c75381
TC
119 }
120 else {
121 # fail in good way
122 die "OS unsupported: FreeType 2 headers/libraries not found\n";
123 }
124}
125
126sub _ft2_test_code {
127 return <<'CODE';
b73378f5
TC
128FT_Library library = 0;
129FT_Face face = 0;
130FT_Error error;
131error = FT_Init_FreeType(&library);
132if (error) {
133 fprintf(stderr, "FreeType 2: cannot initialize library: %d\n", error);
134 return 1;
135}
136error = FT_New_Face(library, "fontfiles/dodge.ttf", 0, &face);
137if (error) {
138 fprintf(stderr, "FreeType 2: cannot load font: %d\n", error);
139 return 1;
140}
50c75381
TC
141return 0;
142CODE
143}
144
b73378f5
TC
145sub _ft2_test_code_prologue {
146 return <<'CODE';
147#include FT_FREETYPE_H
148
149CODE
150}
151
50c75381
TC
152sub is_exe {
153 my ($name) = @_;
154
155 my @exe_suffix = $Config{_exe};
156 if ($^O eq 'MSWin32') {
157 push @exe_suffix, qw/.bat .cmd/;
158 }
c9eba720
TC
159 elsif ($^O eq 'cygwin') {
160 push @exe_suffix, "";
161 }
50c75381
TC
162
163 for my $dir (File::Spec->path) {
164 for my $suffix (@exe_suffix) {
8eb5a69c 165 -x File::Spec->catfile($dir, "$name$suffix")
50c75381
TC
166 and return 1;
167 }
168 }
169
170 return;
171}
172
173# probes for freetype2 by trying to run freetype-config
174sub freetype2_probe_ftconfig {
175 my ($req) = @_;
176
177 is_exe('freetype-config') or return;
178
179 my $cflags = `freetype-config --cflags`
180 and !$? or return;
181 chomp $cflags;
182
183 my $lflags = `freetype-config --libs`
184 and !$? or return;
185 chomp $lflags;
186
187 # before 2.1.5 freetype-config --cflags could output
188 # the -I options in the wrong order, causing a conflict with
189 # freetype1.x installed with the same --prefix
190 #
191 # can happen iff:
192 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
193 # in that order
194 # - freetype 1.x headers are in prefix/include/freetype
195 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
196 if (@incdirs == 2
197 && $incdirs[1] eq "$incdirs[0]/freetype2"
198 && -e "$incdirs[0]/freetype/freetype.h"
199 && -e "$incdirs[0]/freetype/fterrid.h") {
200 print "** freetype-config provided -I options out of order, correcting\n"
201 if $verbose;
202 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
203 map "-I$_", reverse @incdirs);
204 }
205
206 print "$req->{name}: configured via freetype-config\n";
207
208 return
209 {
210 INC => $cflags,
211 LIBS => $lflags,
212 };
213}