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