]> git.imager.perl.org - imager.git/blob - FT2/Makefile.PL
convert t/t55trans.t to Test::More
[imager.git] / FT2 / Makefile.PL
1 #!perl -w
2 use strict;
3 use ExtUtils::MakeMaker;
4 use Getopt::Long;
5 use Config;
6
7 my $verbose = $ENV{IM_VERBOSE};
8 my @libpaths;
9 my @incpaths;
10
11 GetOptions("incpath=s", \@incpaths,
12            "libpath=s" => \@libpaths,
13            "verbose|v" => \$verbose);
14
15 our $BUILDING_IMAGER;
16 our %IMAGER_LIBS;
17
18 my $MM_ver = eval $ExtUtils::MakeMaker::VERSION;
19
20 my %opts = 
21   (
22    NAME => 'Imager::Font::FT2',
23    VERSION_FROM => 'FT2.pm',
24    OBJECT => 'FT2.o freetyp2.o',
25    clean => { FILES => 'testout' },
26   );
27
28 my @inc;
29 if ($BUILDING_IMAGER) {
30   push @inc, "-I..";
31   unshift @INC, "../lib";
32 }
33 else {
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
41   my @Imager_req = ( Imager => "0.82" );
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/",
57         repository =>
58         {
59          url => "http://imager.perl.org/svn/trunk/Imager/FT2",
60          web => "http://imager.perl.org/svnweb/public/browse/trunk/Imager/FT2",
61          type => "svn",
62         },
63        },
64       };
65     $opts{PREREQ_PM} =
66       {
67        @Imager_req,
68       };
69   }
70 }
71
72 require Imager::Probe;
73
74 my %probe =
75   (
76    name => "FreeType 2",
77    code => \&freetype2_probe_ftconfig,
78    inccheck =>
79    sub { -e File::Spec->catfile($_[0], "freetype/ftbitmap.h") },
80    libbase => "freetype",
81    testcode => _ft2_test_code(),
82    testcodeheaders => [ "stdio.h", "string.h", "ft2build.h" ],
83    testcodeprologue => _ft2_test_code_prologue(),
84    incpath => \@incpaths,
85    libpath => \@libpaths,
86    alternatives =>
87    [
88     {
89      incsuffix => "freetype2",
90     },
91     {
92      incsuffix => "freetype",
93     },
94    ],
95   );
96
97 my $probe_res = Imager::Probe->probe(\%probe);
98 if ($probe_res) {
99   $IMAGER_LIBS{FT2} = 1;
100
101   push @inc, $probe_res->{INC};
102   $opts{LIBS} = $probe_res->{LIBS};
103   $opts{DEFINE} = $probe_res->{DEFINE};
104   $opts{INC} = "@inc";
105   $opts{LDDLFLAGS} = $probe_res->{LDDLFLAGS}
106     if $probe_res->{LDDLFLAGS};
107
108   if ($MM_ver > 6.06) {
109     $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>';
110     $opts{ABSTRACT} = 'FreeType 2 font driver for Imager';
111   }
112   
113   WriteMakefile(%opts);
114 }
115 else {
116   $IMAGER_LIBS{FT2} = 0;
117
118   if ($BUILDING_IMAGER) {
119     ExtUtils::MakeMaker::WriteEmptyMakefile(%opts);
120   }
121   else {
122     # fail in good way
123     die "OS unsupported: FreeType 2 headers/libraries not found\n";
124   }
125 }
126
127 sub _ft2_test_code {
128   return <<'CODE';
129 FT_Library library = 0;
130 FT_Face face = 0;
131 FT_Error error;
132 error = FT_Init_FreeType(&library);
133 if (error) {
134   fprintf(stderr, "FreeType 2: cannot initialize library: %d\n", error);
135   return 1;
136 }
137 error = FT_New_Face(library, "fontfiles/dodge.ttf", 0, &face);
138 if (error) {
139   fprintf(stderr, "FreeType 2: cannot load font: %d\n", error);
140   return 1;
141 }
142 return 0;
143 CODE
144 }
145
146 sub _ft2_test_code_prologue {
147   return <<'CODE';
148 #include FT_FREETYPE_H
149
150 CODE
151 }
152
153 sub is_exe {
154   my ($name) = @_;
155
156   my @exe_suffix = $Config{_exe};
157   if ($^O eq 'MSWin32') {
158     push @exe_suffix, qw/.bat .cmd/;
159   }
160
161   for my $dir (File::Spec->path) {
162     for my $suffix (@exe_suffix) {
163       -x File::Spec->catfile($dir, "$name$suffix")
164         and return 1;
165     }
166   }
167
168   return;
169 }
170
171 # probes for freetype2 by trying to run freetype-config
172 sub freetype2_probe_ftconfig {
173   my ($req) = @_;
174
175   is_exe('freetype-config') or return;
176
177   my $cflags = `freetype-config --cflags`
178     and !$? or return;
179   chomp $cflags;
180
181   my $lflags = `freetype-config --libs`
182     and !$? or return;
183   chomp $lflags;
184
185   # before 2.1.5 freetype-config --cflags could output
186   # the -I options in the wrong order, causing a conflict with
187   # freetype1.x installed with the same --prefix
188   #
189   # can happen iff:
190   #  - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
191   #    in that order
192   #  - freetype 1.x headers are in prefix/include/freetype
193   my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
194   if (@incdirs == 2 
195       && $incdirs[1] eq "$incdirs[0]/freetype2"
196       && -e "$incdirs[0]/freetype/freetype.h"
197       && -e "$incdirs[0]/freetype/fterrid.h") {
198     print "** freetype-config provided -I options out of order, correcting\n"
199       if $verbose;
200     $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
201                    map "-I$_", reverse @incdirs);
202   }
203
204   print "$req->{name}: configured via freetype-config\n";
205
206   return
207     {
208      INC => $cflags,
209      LIBS => $lflags,
210     };
211 }