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