- use scanline oriented operations to flip images instead of pixel
[imager.git] / Makefile.PL
CommitLineData
ea9e6c3f 1#!perl -w
714cf158 2use strict;
02d1d628
AMH
3use ExtUtils::MakeMaker;
4use Cwd;
5use Config;
07ea6c21 6use File::Spec;
37959076 7use Getopt::Long;
92bda632 8use ExtUtils::Manifest qw(maniread);
714cf158 9use vars qw(%formats $VERBOSE $INCPATH $LIBPATH $NOLOG $DEBUG_MALLOC $MANUAL $CFLAGS $LFLAGS $DFLAGS);
812ae05c
TC
10use lib 'inc';
11use Devel::CheckLib;
02d1d628 12
02d1d628
AMH
13#
14# IM_INCPATH colon seperated list of paths to extra include paths
15# IM_LIBPATH colon seperated list of paths to extra library paths
16#
17# IM_VERBOSE turns on verbose mode for the library finding and such
18# IM_MANUAL to manually select which libraries are used and which not
19# IM_ENABLE to programmatically select which libraries are used
20# and which are not
21# IM_NOLOG if true logging will not be compiled into the module
22# IM_DEBUG_MALLOC if true malloc debbuging will be compiled into the module
23# do not use IM_DEBUG_MALLOC in production - this slows
24# everything down by alot
25# IM_CFLAGS Extra flags to pass to the compiler
26# IM_LFLAGS Extra flags to pass to the linker
27# IM_DFLAGS Extra flags to pass to the preprocessor
28
812ae05c
TC
29my $KEEP_FILES = $ENV{IMAGER_KEEP_FILES};
30
855c5808
TC
31getenv(); # get environment variables
32
714cf158
TC
33my $lext=$Config{'so'}; # Get extensions of libraries
34my $aext=$Config{'_a'};
35
36my $help; # display help if set
37my @enable; # list of drivers to enable
38my @disable; # or list of drivers to disable
39my @incpaths; # places to look for headers
40my @libpaths; # places to look for libraries
714cf158 41my $noexif; # non-zero to disable EXIF parsing of JPEGs
1660561c 42my $no_gif_set_version; # disable calling EGifSetGifVersion
1ef586b1 43my $coverage; # build for coverage testing
b3afeed5 44my $assert; # build with assertions
37959076
TC
45GetOptions("help" => \$help,
46 "enable=s" => \@enable,
47 "disable=s" => \@disable,
48 "incpath=s", \@incpaths,
49 "libpath=s" => \@libpaths,
274cd32b 50 "verbose|v" => \$VERBOSE,
f7450478 51 "nolog" => \$NOLOG,
1660561c 52 "noexif" => \$noexif,
1ef586b1 53 "nogifsetversion" => \$no_gif_set_version,
b3afeed5
TC
54 'coverage' => \$coverage,
55 "assert|a" => \$assert);
56
57if ($ENV{AUTOMATED_TESTING}) {
58 $assert = 1;
59}
855c5808
TC
60
61if ($VERBOSE) {
62 print "Verbose mode\n";
63 require Data::Dumper;
64 import Data::Dumper qw(Dumper);
65}
37959076
TC
66
67if ($help) {
68 usage();
69}
70
f7450478
TC
71my @defines;
72
274cd32b
TC
73if ($NOLOG) { print "Logging not compiled into module\n"; }
74else {
75 push @defines, [ IMAGER_LOG => 1, "Logging system" ];
76}
77
b3afeed5
TC
78if ($assert) {
79 push @defines, [ IM_ASSERT => 1, "im_assert() are effective" ];
80}
81
274cd32b
TC
82if ($DEBUG_MALLOC) {
83 push @defines, [ IMAGER_DEBUG_MALLOC => 1, "Use Imager's DEBUG malloc()" ];
84 print "Malloc debugging enabled\n";
85}
86
37959076
TC
87if (@enable && @disable) {
88 print STDERR "Only --enable or --disable can be used, not both, try --help\n";
89 exit 1;
90}
02d1d628 91
714cf158
TC
92my %definc;
93my %deflib;
94my @incs; # all the places to look for headers
95my @libs; # all the places to look for libraries
96
02d1d628
AMH
97init(); # initialize global data
98pathcheck(); # Check if directories exist
99
37959076
TC
100if (exists $ENV{IM_ENABLE}) {
101 my %en = map { $_, 1 } split ' ', $ENV{IM_ENABLE};
102 for my $key (keys %formats) {
103 delete $formats{$key} unless $en{$key};
104 }
105}
106if (@enable) {
107 my %en = map { $_ => 1 } map { split /,/ } @enable;
108 for my $key (keys %formats) {
109 delete $formats{$key} unless $en{$key};
110 }
111}
112elsif (@disable) {
113 delete @formats{map { split /,/ } @disable};
114}
115
02d1d628
AMH
116# Pick what libraries are used
117if ($MANUAL) {
118 manual();
119} else {
120 automatic();
02d1d628
AMH
121}
122
123# Make sure there isn't a clash between the gif libraries.
124gifcheck();
125
07ea6c21 126my $lib_cflags = '';
a8395b42 127my $lib_lflags = '';
80c15fc7
TC
128my $F_LIBS = '';
129my $F_OBJECT = '';
714cf158 130for my $frmkey (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
e11d297f
TC
131 my $frm = $formats{$frmkey};
132 push @defines, [ $frm->{def}, 1, "$frmkey available" ];
02d1d628 133 $F_OBJECT .= ' ' .$frm->{objfiles};
714cf158
TC
134 if ($frm->{cflags}) {
135 $lib_cflags .= ' ' .$frm->{cflags};
136 ++$definc{$_} for map { /^-I(.*)$/ ? ($1) : () }
137 grep /^-I./, split ' ', $frm->{cflags};
138 }
a8395b42
TC
139 if ($frm->{lflags}) {
140 $lib_lflags .= ' ' . $frm->{lflags};
141 }
142 else {
143 $F_LIBS .= ' ' .$frm->{libfiles};
144 }
145
02d1d628 146}
714cf158 147
f7450478
TC
148unless ($noexif) {
149 print "EXIF support enabled\n";
150 push @defines, [ 'IMEXIF_ENABLE', 1, "Enable experimental EXIF support" ];
151 $F_OBJECT .= ' imexif.o';
152}
02d1d628 153
714cf158
TC
154my $F_INC = join ' ', map "-I$_", map / / ? qq{"$_"} : $_,
155 grep !$definc{$_}, @incs;
156$F_LIBS = join(' ',map "-L$_", map / / ? qq{"$_"} : $_,
157 grep !$deflib{$_}++, @libs) . $F_LIBS;
02d1d628 158
714cf158
TC
159my $OSLIBS = '';
160my $OSDEF = "-DOS_$^O";
02d1d628
AMH
161
162if ($^O eq 'hpux') { $OSLIBS .= ' -ldld'; }
163if (defined $Config{'d_dlsymun'}) { $OSDEF .= ' -DDLSYMUN'; }
164
714cf158
TC
165my @objs = qw(Imager.o draw.o polygon.o image.o io.o iolayer.o
166 log.o gaussian.o conv.o pnm.o raw.o feat.o font.o
167 filters.o dynaload.o stackmach.o datatypes.o
168 regmach.o trans2.o quant.o error.o convert.o
169 map.o tags.o palimg.o maskimg.o img16.o rotate.o
d5477d3d 170 bmp.o tga.o color.o fills.o imgdouble.o limits.o hlines.o
e41cfe8f 171 imext.o scale.o rubthru.o render.o paste.o compose.o flip.o);
92bda632 172
714cf158
TC
173my %opts=(
174 'NAME' => 'Imager',
175 'VERSION_FROM' => 'Imager.pm',
a8395b42 176 'LIBS' => "$LFLAGS -lm $lib_lflags $OSLIBS $F_LIBS",
714cf158
TC
177 'DEFINE' => "$OSDEF $CFLAGS",
178 'INC' => "$lib_cflags $DFLAGS $F_INC",
179 'OBJECT' => join(' ', @objs, $F_OBJECT),
f45b774f 180 clean => { FILES=>'testout rubthru.c scale.c conv.c filters.c gaussian.c render.c rubthru.c' },
714cf158 181 PM => gen_PM(),
867acf5b 182 PREREQ_PM => { 'Test::More' => 0.47 },
714cf158 183 );
02d1d628 184
1ef586b1
TC
185if ($coverage) {
186 if ($Config{gccversion}) {
187 push @ARGV, 'OPTIMIZE=-ftest-coverage -fprofile-arcs';
188 #$opts{dynamic_lib} = { OTHERLDFLAGS => '-ftest-coverage -fprofile-arcs' };
189 }
190 else {
191 die "Don't know the coverage C flags for your compiler\n";
192 }
193}
194
c52cbef2
TC
195# eval to prevent warnings about versions with _ in them
196my $MM_ver = eval $ExtUtils::MakeMaker::VERSION;
197if ($MM_ver > 6.06) {
29316bdb 198 $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
ca508100
TC
199 $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
200}
f45b774f
TC
201
202if ($MM_ver >= 6.46) {
203 $opts{META_MERGE} =
204 {
205 recommends =>
206 {
207 "Parse::RecDescent" => 0
208 },
209 license => "perl",
210 dynamic_config => 1,
211 };
135d30e3 212}
ca508100 213
e11d297f
TC
214make_imconfig(\@defines);
215
02d1d628
AMH
216if ($VERBOSE) { print Dumper(\%opts); }
217mkdir('testout',0777); # since we cannot include it in the archive.
135d30e3 218
812ae05c
TC
219-d "probe" and rmdir "probe";
220
02d1d628 221WriteMakefile(%opts);
4dce694d 222
02d1d628
AMH
223exit;
224
225
226sub MY::postamble {
5a7e62b6
TC
227 my $self = shift;
228 my $perl = $self->{PERLRUN} ? '$(PERLRUN)' : '$(PERL)';
fe415ad2
TC
229 my $mani = maniread;
230
231 my @ims = grep /\.im$/, keys %$mani;
02d1d628 232'
faa9b3e7 233dyntest.$(MYEXTLIB) : dynfilt/Makefile
02d1d628
AMH
234 cd dynfilt && $(MAKE) $(PASTHRU)
235
236lib/Imager/Regops.pm : regmach.h regops.perl
237 $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
e11d297f 238
92bda632 239imconfig.h : Makefile.PL
e11d297f
TC
240 $(ECHO) "imconfig.h out-of-date with respect to $?"
241 $(PERLRUN) Makefile.PL
242 $(ECHO) "==> Your Makefile has been rebuilt - re-run your make command <=="
5a7e62b6 243'.qq!
6cfee9d1 244lib/Imager/APIRef.pod : \$(C_FILES) \$(H_FILES) apidocs.perl
2a69ed21 245 $perl apidocs.perl lib/Imager/APIRef.pod
92bda632 246
fe415ad2
TC
247!.join('', map _im_rule($perl, $_), @ims)
248
249}
250
251sub _im_rule {
252 my ($perl, $im) = @_;
253
254 (my $c = $im) =~ s/\.im$/.c/;
255 return <<MAKE;
256
9b1ec2b8
TC
257$c: $im lib/Imager/Preprocess.pm
258 $perl -Ilib -MImager::Preprocess -epreprocess $im $c
fe415ad2
TC
259
260MAKE
02d1d628 261
55932d2a
TC
262}
263
02d1d628
AMH
264# manual configuration of helper libraries
265
266sub manual {
267 print <<EOF;
268
269 Please answer the following questions about
270 which formats are avaliable on your computer
271
272press <return> to continue
273EOF
274
275 <STDIN>; # eat one return
276
2646b26c 277 for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
02d1d628
AMH
278 SWX:
279 if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
280 print "Enable $frm support: ";
714cf158 281 my $gz = <STDIN>;
02d1d628
AMH
282 chomp($gz);
283 if ($gz =~ m/^(y|yes|n|no)/i) {
284 $gz=substr(lc($gz),0,1);
285 if ($gz eq 'n') {
286 delete $formats{$frm};
287 }
288 } else { goto SWX; }
289 }
290}
291
292
293# automatic configuration of helper libraries
294
295sub automatic {
714cf158
TC
296 print "Automatic probing:\n" if $VERBOSE;
297 for my $frm (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
02d1d628
AMH
298 delete $formats{$frm} if !checkformat($frm);
299 }
300}
301
302
303sub gifcheck {
5f5fe73e 304 if ($formats{'gif'} and $formats{'ungif'}) {
02d1d628
AMH
305 print "ungif and gif can not coexist - removing ungif support\n";
306 delete $formats{'ungif'};
307 }
5f5fe73e 308
09fd3468
AMH
309 for my $frm (qw(gif ungif)) {
310 checkformat($frm) if ($MANUAL and $formats{$frm});
311 }
312
02d1d628
AMH
313 my @dirs;
314 for my $frm (grep $formats{$_}, qw(gif ungif)) {
315 push(@dirs, @{$formats{$frm}{incdir}}) if $formats{$frm}{incdir};
316 }
317 my $minor = 0;
318 my $major = 0;
319 FILES: for my $dir (@dirs) {
320 my $h = "$dir/gif_lib.h";
321 open H, "< $h" or next;
322 while (<H>) {
323 if (/GIF_LIB_VERSION\s+"\s*version\s*(\d+)\.(\d+)/i) {
324 $major = $1;
325 $minor = $2;
326 close H;
327 last FILES;
328 }
329 }
330 close H;
331 }
332
333 # we need the version in a #ifdefable form
4dce694d 334
714cf158
TC
335 push @defines, [ IM_GIFMAJOR => $major, "Parsed giflib version" ];
336 push @defines, [ IM_GIFMINOR => $minor ];
1660561c
TC
337 push @defines, [ IM_NO_SET_GIF_VERSION => 1, "Disable EGifSetGifVersion" ]
338 if $no_gif_set_version;
02d1d628
AMH
339}
340
341
c6e870ae
TC
342sub grep_directory {
343 my($path, $chk)=@_;
02d1d628
AMH
344
345# print "checking path $path\n";
346 if ( !opendir(DH,$path) ) {
347 warn "Cannot open dir $path: $!\n";
348 return;
349 }
350 my @l=grep { $chk->($_) } readdir(DH);
351 # print @l;
352 close(DH);
353 return map $path, @l;
354}
355
812ae05c
TC
356sub _probe_default {
357 my ($format, $frm) = @_;
07ea6c21 358
c6e870ae
TC
359 my $lib_check=$formats{$frm}{'libcheck'};
360 my $inc_check=$formats{$frm}{'inccheck'};
02d1d628 361
c6e870ae
TC
362 if ($lib_check) {
363 my @l;
364 for my $lp (@libs) {
365 push(@l, grep_directory($lp,$lib_check));
366 }
367
368 my @i;
369 for my $ip (@incs) {
370 push(@i, $ip) if $inc_check->($ip,$frm);
371 }
372
373 printf("%10s: includes %s - libraries %s\n",$frm,(@i?'found':'not found'),(@l?'found':'not found'));
374 $formats{$frm}{incdir} = \@i;
375 $formats{$frm}{libdir} = \@l;
376 return 1 if scalar(@i && @l);
02d1d628 377 }
c6e870ae
TC
378 else {
379 printf("%10s: not available\n", $frm);
02d1d628
AMH
380 }
381
c6e870ae 382 return 0;
02d1d628
AMH
383}
384
812ae05c
TC
385sub checkformat {
386 my $frm=shift;
387
388 print " checkformat($frm)\n" if $VERBOSE;
389
390 my $format = $formats{$frm};
391
392 my @probes;
393 if (my $code = $format->{'code'}) {
394 if (ref $code eq 'ARRAY') {
395 push @probes, @$code;
396 }
397 else {
398 push @probes, $code;
399 }
400 }
401 push @probes, \&_probe_default;
402
403 print " Calling probe function\n" if $VERBOSE;
404 my $found;
405 for my $func (@probes) {
406 if ($func->($format, $frm)) {
407 ++$found;
408 last;
409 }
410 }
411
412 $found or return;
413
414 if ($format->{postcheck}) {
415 print " Calling postcheck function\n" if $VERBOSE;
416 $format->{postcheck}->($format, $frm)
417 or return;
418 }
419
420 return 1;
421}
422
02d1d628 423
02d1d628
AMH
424sub pathcheck {
425 if ($VERBOSE) {
426 print "pathcheck\n";
427 print " Include paths:\n";
428 for (@incs) { print $_,"\n"; }
429 }
3a6bb91b 430 @incs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
02d1d628
AMH
431
432 if ($VERBOSE) {
433 print "\nLibrary paths:\n";
855c5808 434 for (@libs) { print $_,"\n"; }
02d1d628 435 }
3a6bb91b 436 @libs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
02d1d628
AMH
437 print "\ndone.\n";
438}
439
440
441# Format data initialization
442
443# format definition is:
444# defines needed
445# default include path
446# files needed for include (boolean perl code)
447# default lib path
448# libs needed
449# files needed for link (boolean perl code)
450# object files needed for the format
451
452
453sub init {
454
714cf158
TC
455 my @definc = qw(/usr/include);
456 @definc{@definc}=(1) x @definc;
d8e0c3ba
TC
457 @incs=
458 (
459 split(/\Q$Config{path_sep}/, $INCPATH),
460 map _tilde_expand($_), map { split /\Q$Config{path_sep}/ } @incpaths
461 );
2646b26c 462 if ($Config{locincpth}) {
6552acfe 463 push @incs, grep -d, split ' ', $Config{locincpth};
2646b26c 464 }
88a763e2
TC
465 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
466 push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
2646b26c 467 }
16cf4610
TC
468 if ($Config{incpath}) {
469 push @incs, grep -d, split /\Q$Config{path_sep}/, $Config{incpath};
470 }
6552acfe 471 push @incs, grep -d,
2646b26c
TC
472 qw(/sw/include
473 /usr/include/freetype2
474 /usr/local/include/freetype2
475 /usr/local/include/freetype1/freetype
37959076 476 /usr/include /usr/local/include /usr/include/freetype
2646b26c 477 /usr/local/include/freetype);
714cf158
TC
478 if ($Config{ccflags}) {
479 my @hidden = map { /^-I(.*)$/ ? ($1) : () } split ' ', $Config{ccflags};
480 push @incs, @hidden;
481 @definc{@hidden} = (1) x @hidden;
482 }
2646b26c 483
37959076 484 @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
d8e0c3ba 485 map _tilde_expand($_), map { split /\Q$Config{path_sep}/} @libpaths );
2646b26c 486 if ($Config{loclibpth}) {
6552acfe 487 push @libs, grep -d, split ' ', $Config{loclibpth};
2646b26c 488 }
714cf158 489
6552acfe 490 push @libs, grep -d, qw(/sw/lib), split(/ /, $Config{'libpth'});
714cf158 491 push @libs, grep -d, split / /, $Config{libspath} if $Config{libspath};
2646b26c 492 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
88a763e2
TC
493 push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
494 }
faa9b3e7
TC
495 if ($^O eq 'cygwin') {
496 push(@libs, '/usr/lib/w32api') if -d '/usr/lib/w32api';
274cd32b 497 push(@incs, '/usr/include/w32api') if -d '/usr/include/w32api';
faa9b3e7 498 }
714cf158
TC
499 if ($Config{ldflags}) {
500 # some builds of perl put -Ldir into ldflags without putting it in
501 # loclibpth, let's extract them
502 my @hidden = grep -d, map { /^-L(.*)$/ ? ($1) : () }
503 split ' ', $Config{ldflags};
504 push @libs, @hidden;
505 # don't mark them as seen - EU::MM will remove any libraries
506 # it can't find and it doesn't look for -L in ldflags
507 #@deflib{@hidden} = @hidden;
508 }
ed28c9cc 509 push @libs, grep -d, qw(/usr/local/lib);
2646b26c 510
02d1d628
AMH
511 $formats{'jpeg'}={
512 order=>'21',
513 def=>'HAVE_LIBJPEG',
f8e9bc07 514 inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
50ee6f9c 515 libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
02d1d628
AMH
516 libfiles=>'-ljpeg',
517 objfiles=>'jpeg.o',
518 docs=>q{
519 In order to use jpeg with this module you need to have libjpeg
520 installed on your computer}
521 };
522
523 $formats{'tiff'}={
524 order=>'23',
525 def=>'HAVE_LIBTIFF',
f8e9bc07 526 inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
76c8a0a4 527 libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
02d1d628
AMH
528 libfiles=>'-ltiff',
529 objfiles=>'tiff.o',
530 docs=>q{
531 In order to use tiff with this module you need to have libtiff
812ae05c
TC
532 installed on your computer},
533 postcheck => \&postcheck_tiff,
02d1d628
AMH
534 };
535
536 $formats{'png'}={
537 order=>'22',
538 def=>'HAVE_LIBPNG',
f8e9bc07 539 inccheck=>sub { -e catfile($_[0], 'png.h') },
88a763e2 540 libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
64ddd21f 541 libfiles=>$^O eq 'MSWin32' ? '-lpng -lzlib' : '-lpng -lz',
02d1d628
AMH
542 objfiles=>'png.o',
543 docs=>q{
544 Png stands for Portable Network Graphics and is intended as
545 a replacement for gif on the web. It is patent free and
07ea6c21
TC
546 is recommended by the w3c, you need libpng to use these formats},
547 code => \&png_probe,
02d1d628
AMH
548 };
549
550 $formats{'gif'}={
551 order=>'20',
552 def=>'HAVE_LIBGIF',
f8e9bc07 553 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 554 libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
02d1d628
AMH
555 libfiles=>'-lgif',
556 objfiles=>'gif.o',
557 docs=>q{
558 gif is the de facto standard for webgraphics at the moment,
559 it does have some patent problems. If you have giflib and
560 are not in violation with the unisys patent you should use
561 this instead of the 'ungif' option. Note that they cannot
562 be in use at the same time}
563 };
564
565 $formats{'ungif'}={
566 order=>'21',
567 def=>'HAVE_LIBGIF',
f8e9bc07 568 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 569 libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
02d1d628
AMH
570 libfiles=>'-lungif',
571 objfiles=>'gif.o',
572 docs=>q{
573 gif is the de facto standard for webgraphics at the moment,
574 it does have some patent problems. If you have libungif and
575 want to create images free from LZW patented compression you
576 should use this option instead of the 'gif' option}
577 };
578
579 $formats{'T1-fonts'}={
580 order=>'30',
581 def=>'HAVE_LIBT1',
f8e9bc07 582 inccheck=>sub { -e catfile($_[0], 't1lib.h') },
faa9b3e7 583 libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
02d1d628
AMH
584 libfiles=>'-lt1',
585 objfiles=>'',
586 docs=>q{
587 postscript t1 fonts are scalable fonts. They can include
588 ligatures and kerning information and generally yield good
589 visual quality. We depend on libt1 to rasterize the fonts
590 for use in images.}
591 };
592
f8e9bc07
TC
593 $formats{'TT-fonts'}=
594 {
595 order=>'31',
596 def=>'HAVE_LIBTT',
597 inccheck=>sub { -e catfile($_[0], 'freetype.h')
598 && !-e catfile($_[0], 'fterrors.h') },
599 libcheck=>sub { $_[0] eq "libttf$aext" or $_[0] eq "libttf.$lext" },
600 libfiles=>'-lttf',
601 objfiles=>'',
714cf158 602 code => \&freetype1_probe,
f8e9bc07
TC
603 docs=>q{
604Truetype fonts are scalable fonts. They can include
605kerning and hinting information and generally yield good
606visual quality esp on low resultions. The freetype library is
607used to rasterize for us. The only drawback is that there
608are alot of badly designed fonts out there.}
02d1d628 609 };
faa9b3e7
TC
610 $formats{'w32'} = {
611 order=>40,
612 def=>'HAVE_WIN32',
f8e9bc07 613 inccheck=>sub { -e catfile($_[0], 'windows.h') },
faa9b3e7
TC
614 libcheck=>sub { lc $_[0] eq 'gdi32.lib'
615 || lc $_[0] eq 'libgdi32.a' },
616 libfiles=>$^O eq 'cygwin' ? '-lgdi32' : '',
617 objfiles=>'win32.o',
618 docs => <<DOCS
619Uses the Win32 GDI for rendering text.
620
621This currently only works on under normal Win32 and cygwin.
622DOCS
623 };
c6e870ae
TC
624 $formats{'freetype2'} =
625 {
626 order=>'29',
627 def=>'HAVE_FT2',
628 # we always use a probe function
629 #inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
630 #libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
631 libfiles=>'-lfreetype',
632 objfiles=>'freetyp2.o',
633 docs=><<DOCS,
faa9b3e7 634Freetype 2 supports both Truetype and Type 1 fonts, both of which are
f8e9bc07 635scalable. It also supports a variety of other fonts.
faa9b3e7 636DOCS
c6e870ae
TC
637 code =>
638 [
639 \&freetype2_probe_ftconfig,
640 \&freetype2_probe_scan
641 ],
642 };
f7450478 643
02d1d628
AMH
644 # Make fix indent
645 for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/ /mg; }
646}
647
648
649
650sub gen {
651 my $V = $ENV{$_[0]};
652 defined($V) ? $V : "";
653}
654
655
656# Get information from environment variables
657
658sub getenv {
659
660 ($VERBOSE,
661 $INCPATH,
662 $LIBPATH,
663 $NOLOG,
664 $DEBUG_MALLOC,
665 $MANUAL,
666 $CFLAGS,
667 $LFLAGS,
668 $DFLAGS) = map { gen $_ } qw(IM_VERBOSE
669 IM_INCPATH
670 IM_LIBPATH
671 IM_NOLOG
672 IM_DEBUG_MALLOC
673 IM_MANUAL
674 IM_CFLAGS
675 IM_LFLAGS
676 IM_DFLAGS);
677
02d1d628 678}
07ea6c21 679
e11d297f
TC
680sub make_imconfig {
681 my ($defines) = @_;
682
683 open CONFIG, "> imconfig.h"
684 or die "Cannot create imconfig.h: $!\n";
685 print CONFIG <<EOS;
686/* This file is automatically generated by Makefile.PL.
687 Don't edit this file, since any changes will be lost */
688
689#ifndef IMAGER_IMCONFIG_H
690#define IMAGER_IMCONFIG_H
691EOS
692 for my $define (@$defines) {
693 if ($define->[2]) {
694 print CONFIG "\n/*\n $define->[2]\n*/\n\n";
695 }
696 print CONFIG "#define $define->[0] $define->[1]\n";
697 }
698 print CONFIG "\n#endif\n";
699 close CONFIG;
700}
701
dbc33d8a
TC
702# use pkg-config to probe for libraries
703# works around the junk that pkg-config dumps on FreeBSD
704sub _pkg_probe {
705 my ($pkg) = @_;
706
707 is_exe('pkg-config') or return;
708
709 my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
710
711 !system("pkg-config $pkg --exists $redir");
712}
713
714cf158
TC
714# probes for freetype1 by scanning @incs for the includes and
715# @libs for the libs. This is done separately because freetype's headers
716# are stored in a freetype or freetype1 directory under PREFIX/include.
717#
718# we could find the files with the existing mechanism, but it won't set
719# -I flags correctly.
720#
721# This could be extended to freetype2 too, but freetype-config catches
722# that
723sub freetype1_probe {
724 my ($frm, $frmkey) = @_;
725
726 my $found_inc;
727 INCS:
728 for my $inc (@incs) {
729 for my $subdir (qw/freetype freetype1/) {
730 my $path = File::Spec->catfile($inc, $subdir, 'freetype.h');
731 -e $path or next;
732 $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
733 -e $path and next;
734
735 $found_inc = File::Spec->catdir($inc, $subdir);
736 last INCS;
737 }
738 }
739
740 my $found_lib;
741 LIBS:
742 for my $lib (@libs) {
743 my $a_path = File::Spec->catfile($lib, "libttf$aext");
744 my $l_path = File::Spec->catfile($lib, "libttf.$lext");
745 if (-e $a_path || -e $l_path) {
746 $found_lib = $lib;
747 last LIBS;
748 }
749 }
750
89d37182 751 return unless $found_inc && $found_lib;
714cf158
TC
752 printf("%10s: includes %s - libraries %s\n", $frmkey,
753 ($found_inc ? 'found' : 'not found'),
754 ($found_lib ? 'found' : 'not found'));
714cf158
TC
755
756 $frm->{cflags} = "-I$found_inc";
757 $frm->{libfiles} = "-lttf";
758
759 return 1;
760}
761
07ea6c21 762# probes for freetype2 by trying to run freetype-config
c6e870ae 763sub freetype2_probe_ftconfig {
07ea6c21
TC
764 my ($frm, $frmkey) = @_;
765
766 is_exe('freetype-config') or return;
767
768 my $cflags = `freetype-config --cflags`
769 and !$? or return;
770 chomp $cflags;
88e16587 771
07ea6c21
TC
772 my $lflags = `freetype-config --libs`
773 and !$? or return;
774 chomp $lflags;
88e16587
TC
775
776 # before 2.1.5 freetype-config --cflags could output
777 # the -I options in the wrong order, causing a conflict with
778 # freetype1.x installed with the same --prefix
779 #
780 # can happen iff:
781 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
782 # in that order
783 # - freetype 1.x headers are in prefix/include/freetype
784 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
785 if (@incdirs == 2
786 && $incdirs[1] eq "$incdirs[0]/freetype2"
787 && -e "$incdirs[0]/freetype/freetype.h"
788 && -e "$incdirs[0]/freetype/fterrid.h") {
789 print "** freetype-config provided -I options out of order, correcting\n"
790 if $VERBOSE;
791 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
792 map "-I$_", reverse @incdirs);
793 }
794 $frm->{cflags} = $cflags;
a8395b42 795 $frm->{lflags} = $lflags;
07ea6c21
TC
796
797 printf "%10s: configured via freetype-config\n", $frmkey;
798
799 return 1;
800}
801
c6e870ae
TC
802# attempt to probe for freetype2 by scanning directories
803# we can't use the normal scan since we need to find the directory
804# containing most of the includes
805sub freetype2_probe_scan {
806 my ($frm, $frmkey) = @_;
807
808 my $found_inc;
809 my $found_inc2;
810 INCS:
811 for my $inc (@incs) {
812 my $path = File::Spec->catfile($inc, 'ft2build.h');
813 -e $path or next;
814
c6e870ae
TC
815 # try to find what it's including
816 my $ftheader;
817 open FT2BUILD, "< $path"
818 or next;
c6e870ae
TC
819 while (<FT2BUILD>) {
820 if (m!^\s*\#\s*include\s+<([\w/.]+)>!
821 || m!^\s*\#\s*include\s+"([\w/.]+)"!) {
c6e870ae
TC
822 $ftheader = $1;
823 last;
824 }
825 }
826 close FT2BUILD;
827 $ftheader
828 or next;
c6e870ae
TC
829 # non-Unix installs put this directly under the same directory in
830 # theory
831 if (-e File::Spec->catfile($inc, $ftheader)) {
832 $found_inc = $inc;
833 last INCS;
834 }
835 for my $subdir (qw/freetype2 freetype/) {
836 $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
837 -e $path and next;
838
839 $found_inc = $inc;
840 $found_inc2 = File::Spec->catdir($inc, $subdir);
841 last INCS;
842 }
843 }
844
845 my $found_lib;
846 LIBS:
847 for my $lib (@libs) {
848 my $a_path = File::Spec->catfile($lib, "libfreetype$aext");
849 my $l_path = File::Spec->catfile($lib, "libfreetype.$lext");
850 if (-e $a_path || -e $l_path) {
851 $found_lib = $lib;
852 last LIBS;
853 }
854 }
855
856 printf("%10s: includes %s - libraries %s\n", $frmkey,
857 ($found_inc ? 'found' : 'not found'),
858 ($found_lib ? 'found' : 'not found'));
859
860 return unless $found_inc && $found_lib;
861
6c2b7cf1
TC
862 $frm->{cflags} = _make_I($found_inc);
863 $frm->{cflags} .= " " . _make_I($found_inc2) if $found_inc2;
c6e870ae
TC
864 $frm->{libfiles} = "-lfreetype";
865
866 return 1;
867}
868
6c2b7cf1
TC
869sub _make_I {
870 my ($inc_dir) = @_;
871
872 $definc{$inc_dir}
e1fe2094 873 and return '';
6c2b7cf1
TC
874
875 $inc_dir =~ / / ? qq!-I"$inc_dir"! : "-I$inc_dir";
876}
877
07ea6c21
TC
878# probes for libpng via pkg-config
879sub png_probe {
880 my ($frm, $frmkey) = @_;
881
882 is_exe('pkg-config') or return;
883
07ea6c21
TC
884 my $config;
885 for my $check_conf (qw(libpng libpng12 libpng10)) {
dbc33d8a 886 if (_pkg_probe($check_conf)) {
07ea6c21
TC
887 $config = $check_conf;
888 last;
889 }
890 }
891 $config or return;
892
dbc33d8a
TC
893 my $cflags = `pkg-config $config --cflags`
894 and !$? or return;
895
07ea6c21
TC
896 my $lflags = `pkg-config $config --libs`
897 and !$? or return;
898
899 chomp $cflags;
900 chomp $lflags;
901 $frm->{cflags} = $cflags;
a8395b42 902 $frm->{lflags} = $lflags;
07ea6c21
TC
903
904 printf "%10s: configured via `pkg-config $config ...`\n", $frmkey;
905
906 return 1;
907}
908
909sub catfile {
910 return File::Spec->catfile(@_);
911}
912
913sub is_exe {
914 my ($name) = @_;
915
3e6d8b86
TC
916 my @exe_suffix = $Config{_exe};
917 if ($^O eq 'MSWin32') {
918 push @exe_suffix, qw/.bat .cmd/;
919 }
920
07ea6c21 921 for my $dir (File::Spec->path) {
3e6d8b86
TC
922 for my $suffix (@exe_suffix) {
923 -x catfile($dir, "$name$suffix")
924 and return 1;
925 }
07ea6c21
TC
926 }
927
928 return;
929}
37959076
TC
930
931sub usage {
932 print STDERR <<EOS;
274cd32b
TC
933Usage: $0 [--enable feature1,feature2,...] [other options]
934 $0 [--disable feature1,feature2,...] [other options]
37959076
TC
935 $0 --help
936Possible feature names are:
937 png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
274cd32b
TC
938Other options:
939 --verbose | -v
940 Verbose library probing (or set IM_VERBOSE in the environment)
941 --nolog
942 Disable logging (or set IM_NOLOG in the environment)
943 --incpath dir
944 Add to the include search path
945 --libpath dir
946 Add to the library search path
35a15603
TC
947 --coverage
948 Build for coverage testing.
949 --assert
950 Build with assertions active.
951 --noexif
952 Disable EXIF parsing.
37959076
TC
953EOS
954 exit 1;
955
956}
92bda632
TC
957
958# generate the PM MM argument
959# I'd prefer to modify the public version, but there doesn't seem to be
960# a public API to do that
961sub gen_PM {
962 my %pm;
963 my $instbase = '$(INST_LIBDIR)';
964
965 # first the basics, .pm and .pod files
966 $pm{"Imager.pm"} = "$instbase/Imager.pm";
967
968 my $mani = maniread();
969
970 for my $filename (keys %$mani) {
971 if ($filename =~ m!^lib/! && $filename =~ /\.(pm|pod)$/) {
972 (my $work = $filename) =~ s/^lib//;
973 $pm{$filename} = $instbase . $work;
974 }
975 }
976
977 # need the typemap
978 $pm{typemap} = $instbase . '/Imager/typemap';
979
980 # and the core headers
981 for my $filename (keys %$mani) {
982 if ($filename =~ /^\w+\.h$/) {
983 $pm{$filename} = $instbase . '/Imager/include/' . $filename;
984 }
985 }
986
987 # and the generated header
988 $pm{"imconfig.h"} = $instbase . '/Imager/include/imconfig.h';
989
990 \%pm;
991}
135d30e3 992
d8e0c3ba
TC
993my $home;
994sub _tilde_expand {
995 my ($path) = @_;
996
997 if ($path =~ m!^~[/\\]!) {
998 defined $home or $home = $ENV{HOME};
999 if (!defined $home && $^O eq 'MSWin32'
1000 && defined $ENV{HOMEDRIVE} && defined $ENV{HOMEPATH}) {
1001 $home = $ENV{HOMEDRIVE} . $ENV{HOMEPATH};
1002 }
1003 unless (defined $home) {
1004 $home = eval { (getpwuid($<))[7] };
1005 }
1006 defined $home or die "You supplied $path, but I can't find your home directory\n";
1007 $path =~ s/^~//;
1008 $path = File::Spec->catdir($home, $path);
1009 }
1010
1011 $path;
1012}
1013
812ae05c
TC
1014sub postcheck_tiff {
1015 my ($format, $frm) = @_;
1016
1017 -d "probe" or mkdir "probe";
1018
1019 my $tiffver_name = "probe/tiffver.txt";
1020
b8ea81ac
TC
1021 my $lib;
1022 if ($Config{cc} =~ /\b(cl|bcc)\b/) {
1023 $lib = "libtiff";
1024 }
1025 else {
1026 $lib = "tiff";
1027 }
1028
812ae05c
TC
1029 my $good =
1030 eval {
1031 assert_lib
1032 (
1033 debug => $VERBOSE,
1034 incpath => $format->{incdir},
1035 libpath => $format->{libdir},
b8ea81ac 1036 lib => $lib,
812ae05c
TC
1037 header => [ qw(stdio.h tiffio.h) ],
1038 function => <<FUNCTION,
1039 {
1040 const char *vers = TIFFGetVersion();
1041 FILE *f = fopen("$tiffver_name", "wb");
1042 if (!f)
1043 return 1;
1044 fputs(vers, f);
1045 if (fclose(f))
1046 return 1;
1047 return 0;
1048 }
1049FUNCTION
1050 );
1051 1;
1052 };
1053
1054 unless ($good && -s $tiffver_name
1055 && open(VERS, "< probe/tiffver.txt")) {
1056 unlink $tiffver_name unless $KEEP_FILES;
1057 print <<EOS;
1058 **tiff: cannot determine libtiff version number
1059 tiff: DISABLED
1060EOS
1061 return;
1062 }
1063
1064 # version file seems to be there, load it up
1065 my $ver_str = do { local $/; <VERS> };
1066 close VERS;
1067 unlink $tiffver_name unless $KEEP_FILES;
1068
1069 my ($version) = $ver_str =~ /(\d+\.\d+\.\d+)/;
1070
1071 if ($version eq '3.9.0') {
1072 print <<EOS;
1073 **tiff: libtiff 3.9.0 introduced a serious bug, please install 3.9.1
1074 tiff: DISABLED
1075EOS
1076 return;
1077 }
1078
1079 return 1;
1080}
1081
678a9a65
TC
1082# This isn't a module, but some broken tools, like
1083# Module::Depends::Instrusive insist on treating it like one.
1084#
1085# http://rt.cpan.org/Public/Bug/Display.html?id=21229
1086
10871;