- added version/level to the API function table structure
[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
TC
8use vars qw(%Recommends);
9require "metafile.pl";
10use ExtUtils::Manifest qw(maniread);
714cf158 11use vars qw(%formats $VERBOSE $INCPATH $LIBPATH $NOLOG $DEBUG_MALLOC $MANUAL $CFLAGS $LFLAGS $DFLAGS);
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
f4dbac50 28# IM_SUPPRESS_PROMPT Suppress the prompt asking about gif support
02d1d628 29
855c5808
TC
30getenv(); # get environment variables
31
714cf158
TC
32my $lext=$Config{'so'}; # Get extensions of libraries
33my $aext=$Config{'_a'};
34
35my $help; # display help if set
36my @enable; # list of drivers to enable
37my @disable; # or list of drivers to disable
38my @incpaths; # places to look for headers
39my @libpaths; # places to look for libraries
40my $noprobe; # non-zero to disable newer probes
41my $noexif; # non-zero to disable EXIF parsing of JPEGs
37959076
TC
42GetOptions("help" => \$help,
43 "enable=s" => \@enable,
44 "disable=s" => \@disable,
45 "incpath=s", \@incpaths,
46 "libpath=s" => \@libpaths,
855c5808 47 "noprobe" => \$noprobe,
274cd32b 48 "verbose|v" => \$VERBOSE,
f7450478
TC
49 "nolog" => \$NOLOG,
50 "noexif" => \$noexif);
855c5808
TC
51
52if ($VERBOSE) {
53 print "Verbose mode\n";
54 require Data::Dumper;
55 import Data::Dumper qw(Dumper);
56}
37959076
TC
57
58if ($help) {
59 usage();
60}
61
f7450478
TC
62my @defines;
63
274cd32b
TC
64if ($NOLOG) { print "Logging not compiled into module\n"; }
65else {
66 push @defines, [ IMAGER_LOG => 1, "Logging system" ];
67}
68
69if ($DEBUG_MALLOC) {
70 push @defines, [ IMAGER_DEBUG_MALLOC => 1, "Use Imager's DEBUG malloc()" ];
71 print "Malloc debugging enabled\n";
72}
73
37959076
TC
74if (@enable && @disable) {
75 print STDERR "Only --enable or --disable can be used, not both, try --help\n";
76 exit 1;
77}
02d1d628 78
714cf158
TC
79my %definc;
80my %deflib;
81my @incs; # all the places to look for headers
82my @libs; # all the places to look for libraries
83
02d1d628
AMH
84init(); # initialize global data
85pathcheck(); # Check if directories exist
86
37959076
TC
87if (exists $ENV{IM_ENABLE}) {
88 my %en = map { $_, 1 } split ' ', $ENV{IM_ENABLE};
89 for my $key (keys %formats) {
90 delete $formats{$key} unless $en{$key};
91 }
92}
93if (@enable) {
94 my %en = map { $_ => 1 } map { split /,/ } @enable;
95 for my $key (keys %formats) {
96 delete $formats{$key} unless $en{$key};
97 }
98}
99elsif (@disable) {
100 delete @formats{map { split /,/ } @disable};
101}
102
02d1d628
AMH
103# Pick what libraries are used
104if ($MANUAL) {
105 manual();
106} else {
107 automatic();
02d1d628
AMH
108}
109
110# Make sure there isn't a clash between the gif libraries.
111gifcheck();
112
07ea6c21 113my $lib_cflags = '';
80c15fc7
TC
114my $F_LIBS = '';
115my $F_OBJECT = '';
714cf158 116for my $frmkey (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
e11d297f
TC
117 my $frm = $formats{$frmkey};
118 push @defines, [ $frm->{def}, 1, "$frmkey available" ];
02d1d628
AMH
119 $F_LIBS .= ' ' .$frm->{libfiles};
120 $F_OBJECT .= ' ' .$frm->{objfiles};
714cf158
TC
121 if ($frm->{cflags}) {
122 $lib_cflags .= ' ' .$frm->{cflags};
123 ++$definc{$_} for map { /^-I(.*)$/ ? ($1) : () }
124 grep /^-I./, split ' ', $frm->{cflags};
125 }
02d1d628 126}
714cf158 127
f7450478
TC
128unless ($noexif) {
129 print "EXIF support enabled\n";
130 push @defines, [ 'IMEXIF_ENABLE', 1, "Enable experimental EXIF support" ];
131 $F_OBJECT .= ' imexif.o';
132}
02d1d628 133
714cf158
TC
134my $F_INC = join ' ', map "-I$_", map / / ? qq{"$_"} : $_,
135 grep !$definc{$_}, @incs;
136$F_LIBS = join(' ',map "-L$_", map / / ? qq{"$_"} : $_,
137 grep !$deflib{$_}++, @libs) . $F_LIBS;
02d1d628 138
714cf158
TC
139my $OSLIBS = '';
140my $OSDEF = "-DOS_$^O";
02d1d628
AMH
141
142if ($^O eq 'hpux') { $OSLIBS .= ' -ldld'; }
143if (defined $Config{'d_dlsymun'}) { $OSDEF .= ' -DDLSYMUN'; }
144
714cf158
TC
145my @objs = qw(Imager.o draw.o polygon.o image.o io.o iolayer.o
146 log.o gaussian.o conv.o pnm.o raw.o feat.o font.o
147 filters.o dynaload.o stackmach.o datatypes.o
148 regmach.o trans2.o quant.o error.o convert.o
149 map.o tags.o palimg.o maskimg.o img16.o rotate.o
150 bmp.o tga.o rgb.o color.o fills.o imgdouble.o limits.o hlines.o
151 imext.o);
92bda632
TC
152
153$Recommends{Imager} =
154 { 'Parse::RecDescent' => 0 };
02d1d628 155
714cf158
TC
156my %opts=(
157 'NAME' => 'Imager',
158 'VERSION_FROM' => 'Imager.pm',
159 'LIBS' => "$LFLAGS -lm $OSLIBS $F_LIBS",
160 'DEFINE' => "$OSDEF $CFLAGS",
161 'INC' => "$lib_cflags $DFLAGS $F_INC",
162 'OBJECT' => join(' ', @objs, $F_OBJECT),
163 clean => { FILES=>'testout meta.tmp' },
164 PM => gen_PM(),
165 );
02d1d628 166
29316bdb
TC
167if ($ExtUtils::MakeMaker::VERSION > 6.06) {
168 $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
ca508100
TC
169 $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
170}
171
e11d297f
TC
172make_imconfig(\@defines);
173
02d1d628
AMH
174if ($VERBOSE) { print Dumper(\%opts); }
175mkdir('testout',0777); # since we cannot include it in the archive.
176WriteMakefile(%opts);
4dce694d 177
02d1d628
AMH
178exit;
179
180
181sub MY::postamble {
5a7e62b6
TC
182 my $self = shift;
183 my $perl = $self->{PERLRUN} ? '$(PERLRUN)' : '$(PERL)';
02d1d628 184'
faa9b3e7 185dyntest.$(MYEXTLIB) : dynfilt/Makefile
02d1d628
AMH
186 cd dynfilt && $(MAKE) $(PASTHRU)
187
188lib/Imager/Regops.pm : regmach.h regops.perl
189 $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
e11d297f 190
92bda632 191imconfig.h : Makefile.PL
e11d297f
TC
192 $(ECHO) "imconfig.h out-of-date with respect to $?"
193 $(PERLRUN) Makefile.PL
194 $(ECHO) "==> Your Makefile has been rebuilt - re-run your make command <=="
5a7e62b6
TC
195'.qq!
196lib/Imager/APIRef.pm : \$(C_FILES) apidocs.perl
197 $perl apidocs.perl lib/Imager/APIRef.pm
92bda632 198
5a7e62b6 199!;
02d1d628 200
55932d2a
TC
201}
202
02d1d628
AMH
203# manual configuration of helper libraries
204
205sub manual {
206 print <<EOF;
207
208 Please answer the following questions about
209 which formats are avaliable on your computer
210
211press <return> to continue
212EOF
213
214 <STDIN>; # eat one return
215
2646b26c 216 for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
02d1d628
AMH
217 SWX:
218 if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
219 print "Enable $frm support: ";
714cf158 220 my $gz = <STDIN>;
02d1d628
AMH
221 chomp($gz);
222 if ($gz =~ m/^(y|yes|n|no)/i) {
223 $gz=substr(lc($gz),0,1);
224 if ($gz eq 'n') {
225 delete $formats{$frm};
226 }
227 } else { goto SWX; }
228 }
229}
230
231
232# automatic configuration of helper libraries
233
234sub automatic {
714cf158
TC
235 print "Automatic probing:\n" if $VERBOSE;
236 for my $frm (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
02d1d628
AMH
237 delete $formats{$frm} if !checkformat($frm);
238 }
239}
240
241
242sub gifcheck {
5f5fe73e 243 if ($formats{'gif'} and $formats{'ungif'}) {
02d1d628
AMH
244 print "ungif and gif can not coexist - removing ungif support\n";
245 delete $formats{'ungif'};
246 }
5f5fe73e
AMH
247
248 RETR:
f4dbac50 249 if (($formats{'gif'} or $formats{'ungif'}) && !$ENV{IM_SUPPRESS_PROMPT}) {
5f5fe73e
AMH
250 print <<EOFF;
251
252You have libgif or libungif installed. They are both known to have
253bugs. Imager can crash or display other strange behaviour after
254reading or writing gif images. Some of the gif tests can even fail
255since they stress some parts of the buggy code.
256
f1967c11
TC
257libungif 4.1.2 and later is safe. giflib 4.1.3 needs at least one
258patch to have all the bugs fixed, see README for details.
259
260Of course it's possible your operating system distributor has patched
261all of these problems and you have nothing to worry about.
0b836ff8 262
5f5fe73e
AMH
263Do you want to remove gif support? [Y/n]
264EOFF
265 my $resp = <STDIN>;
266 chomp($resp);
267 if ($resp ne "n") {
268 delete $formats{'gif'};
269 delete $formats{'ungif'};
09fd3468 270 return;
5f5fe73e
AMH
271 }
272 }
09fd3468
AMH
273
274 for my $frm (qw(gif ungif)) {
275 checkformat($frm) if ($MANUAL and $formats{$frm});
276 }
277
02d1d628
AMH
278 my @dirs;
279 for my $frm (grep $formats{$_}, qw(gif ungif)) {
280 push(@dirs, @{$formats{$frm}{incdir}}) if $formats{$frm}{incdir};
281 }
282 my $minor = 0;
283 my $major = 0;
284 FILES: for my $dir (@dirs) {
285 my $h = "$dir/gif_lib.h";
286 open H, "< $h" or next;
287 while (<H>) {
288 if (/GIF_LIB_VERSION\s+"\s*version\s*(\d+)\.(\d+)/i) {
289 $major = $1;
290 $minor = $2;
291 close H;
292 last FILES;
293 }
294 }
295 close H;
296 }
297
298 # we need the version in a #ifdefable form
4dce694d 299
714cf158
TC
300 push @defines, [ IM_GIFMAJOR => $major, "Parsed giflib version" ];
301 push @defines, [ IM_GIFMINOR => $minor ];
02d1d628
AMH
302}
303
304
305sub gd {
306 my($path,$chk)=@_;
307
308# print "checking path $path\n";
309 if ( !opendir(DH,$path) ) {
310 warn "Cannot open dir $path: $!\n";
311 return;
312 }
313 my @l=grep { $chk->($_) } readdir(DH);
314 # print @l;
315 close(DH);
316 return map $path, @l;
317}
318
319
320sub checkformat {
321 my $frm=shift;
714cf158
TC
322
323 print " checkformat($frm)\n" if $VERBOSE;
37959076 324
07ea6c21 325 my $code = $formats{$frm}{'code'};
37959076 326 if ($code && !$noprobe) {
714cf158 327 print " Calling probe function\n" if $VERBOSE;
07ea6c21
TC
328 return 1 if $code->($formats{$frm}, $frm);
329 }
330
02d1d628
AMH
331 my $libchk=$formats{$frm}{'libcheck'};
332 my $incchk=$formats{$frm}{'inccheck'};
333
334 my @l;
335 for my $lp (@libs) {
336 push(@l, gd($lp,$libchk));
337 }
338
339 my @i;
340 for my $ip (@incs) {
f8e9bc07 341 push(@i, $ip) if $incchk->($ip,$frm);
02d1d628
AMH
342 }
343
344 printf("%10s: includes %s - libraries %s\n",$frm,(@i?'found':'not found'),(@l?'found':'not found'));
345 $formats{$frm}{incdir} = \@i;
346 $formats{$frm}{libdir} = \@l;
347 return scalar(@i && @l);
348}
349
350
02d1d628
AMH
351sub pathcheck {
352 if ($VERBOSE) {
353 print "pathcheck\n";
354 print " Include paths:\n";
355 for (@incs) { print $_,"\n"; }
356 }
3a6bb91b 357 @incs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
02d1d628
AMH
358
359 if ($VERBOSE) {
360 print "\nLibrary paths:\n";
855c5808 361 for (@libs) { print $_,"\n"; }
02d1d628 362 }
3a6bb91b 363 @libs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
02d1d628
AMH
364 print "\ndone.\n";
365}
366
367
368# Format data initialization
369
370# format definition is:
371# defines needed
372# default include path
373# files needed for include (boolean perl code)
374# default lib path
375# libs needed
376# files needed for link (boolean perl code)
377# object files needed for the format
378
379
380sub init {
381
714cf158
TC
382 my @definc = qw(/usr/include);
383 @definc{@definc}=(1) x @definc;
37959076
TC
384 @incs=(split(/\Q$Config{path_sep}/, $INCPATH),
385 map { split /\Q$Config{path_sep}/} @incpaths );
2646b26c 386 if ($Config{locincpth}) {
6552acfe 387 push @incs, grep -d, split ' ', $Config{locincpth};
2646b26c 388 }
88a763e2
TC
389 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
390 push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
2646b26c 391 }
6552acfe 392 push @incs, grep -d,
2646b26c
TC
393 qw(/sw/include
394 /usr/include/freetype2
395 /usr/local/include/freetype2
396 /usr/local/include/freetype1/freetype
37959076 397 /usr/include /usr/local/include /usr/include/freetype
2646b26c 398 /usr/local/include/freetype);
714cf158
TC
399 if ($Config{ccflags}) {
400 my @hidden = map { /^-I(.*)$/ ? ($1) : () } split ' ', $Config{ccflags};
401 push @incs, @hidden;
402 @definc{@hidden} = (1) x @hidden;
403 }
2646b26c 404
37959076
TC
405 @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
406 map { split /\Q$Config{path_sep}/} @libpaths );
2646b26c 407 if ($Config{loclibpth}) {
6552acfe 408 push @libs, grep -d, split ' ', $Config{loclibpth};
2646b26c 409 }
714cf158 410
6552acfe 411 push @libs, grep -d, qw(/sw/lib), split(/ /, $Config{'libpth'});
714cf158 412 push @libs, grep -d, split / /, $Config{libspath} if $Config{libspath};
2646b26c 413 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
88a763e2
TC
414 push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
415 }
faa9b3e7
TC
416 if ($^O eq 'cygwin') {
417 push(@libs, '/usr/lib/w32api') if -d '/usr/lib/w32api';
274cd32b 418 push(@incs, '/usr/include/w32api') if -d '/usr/include/w32api';
faa9b3e7 419 }
714cf158
TC
420 if ($Config{ldflags}) {
421 # some builds of perl put -Ldir into ldflags without putting it in
422 # loclibpth, let's extract them
423 my @hidden = grep -d, map { /^-L(.*)$/ ? ($1) : () }
424 split ' ', $Config{ldflags};
425 push @libs, @hidden;
426 # don't mark them as seen - EU::MM will remove any libraries
427 # it can't find and it doesn't look for -L in ldflags
428 #@deflib{@hidden} = @hidden;
429 }
2646b26c 430
02d1d628
AMH
431 $formats{'jpeg'}={
432 order=>'21',
433 def=>'HAVE_LIBJPEG',
f8e9bc07 434 inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
50ee6f9c 435 libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
02d1d628
AMH
436 libfiles=>'-ljpeg',
437 objfiles=>'jpeg.o',
438 docs=>q{
439 In order to use jpeg with this module you need to have libjpeg
440 installed on your computer}
441 };
442
443 $formats{'tiff'}={
444 order=>'23',
445 def=>'HAVE_LIBTIFF',
f8e9bc07 446 inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
76c8a0a4 447 libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
02d1d628
AMH
448 libfiles=>'-ltiff',
449 objfiles=>'tiff.o',
450 docs=>q{
451 In order to use tiff with this module you need to have libtiff
452 installed on your computer}
453 };
454
455 $formats{'png'}={
456 order=>'22',
457 def=>'HAVE_LIBPNG',
f8e9bc07 458 inccheck=>sub { -e catfile($_[0], 'png.h') },
88a763e2 459 libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
02d1d628
AMH
460 libfiles=>'-lpng -lz',
461 objfiles=>'png.o',
462 docs=>q{
463 Png stands for Portable Network Graphics and is intended as
464 a replacement for gif on the web. It is patent free and
07ea6c21
TC
465 is recommended by the w3c, you need libpng to use these formats},
466 code => \&png_probe,
02d1d628
AMH
467 };
468
469 $formats{'gif'}={
470 order=>'20',
471 def=>'HAVE_LIBGIF',
f8e9bc07 472 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 473 libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
02d1d628
AMH
474 libfiles=>'-lgif',
475 objfiles=>'gif.o',
476 docs=>q{
477 gif is the de facto standard for webgraphics at the moment,
478 it does have some patent problems. If you have giflib and
479 are not in violation with the unisys patent you should use
480 this instead of the 'ungif' option. Note that they cannot
481 be in use at the same time}
482 };
483
484 $formats{'ungif'}={
485 order=>'21',
486 def=>'HAVE_LIBGIF',
f8e9bc07 487 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 488 libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
02d1d628
AMH
489 libfiles=>'-lungif',
490 objfiles=>'gif.o',
491 docs=>q{
492 gif is the de facto standard for webgraphics at the moment,
493 it does have some patent problems. If you have libungif and
494 want to create images free from LZW patented compression you
495 should use this option instead of the 'gif' option}
496 };
497
498 $formats{'T1-fonts'}={
499 order=>'30',
500 def=>'HAVE_LIBT1',
f8e9bc07 501 inccheck=>sub { -e catfile($_[0], 't1lib.h') },
faa9b3e7 502 libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
02d1d628
AMH
503 libfiles=>'-lt1',
504 objfiles=>'',
505 docs=>q{
506 postscript t1 fonts are scalable fonts. They can include
507 ligatures and kerning information and generally yield good
508 visual quality. We depend on libt1 to rasterize the fonts
509 for use in images.}
510 };
511
f8e9bc07
TC
512 $formats{'TT-fonts'}=
513 {
514 order=>'31',
515 def=>'HAVE_LIBTT',
516 inccheck=>sub { -e catfile($_[0], 'freetype.h')
517 && !-e catfile($_[0], 'fterrors.h') },
518 libcheck=>sub { $_[0] eq "libttf$aext" or $_[0] eq "libttf.$lext" },
519 libfiles=>'-lttf',
520 objfiles=>'',
714cf158 521 code => \&freetype1_probe,
f8e9bc07
TC
522 docs=>q{
523Truetype fonts are scalable fonts. They can include
524kerning and hinting information and generally yield good
525visual quality esp on low resultions. The freetype library is
526used to rasterize for us. The only drawback is that there
527are alot of badly designed fonts out there.}
02d1d628 528 };
faa9b3e7
TC
529 $formats{'w32'} = {
530 order=>40,
531 def=>'HAVE_WIN32',
f8e9bc07 532 inccheck=>sub { -e catfile($_[0], 'windows.h') },
faa9b3e7
TC
533 libcheck=>sub { lc $_[0] eq 'gdi32.lib'
534 || lc $_[0] eq 'libgdi32.a' },
535 libfiles=>$^O eq 'cygwin' ? '-lgdi32' : '',
536 objfiles=>'win32.o',
537 docs => <<DOCS
538Uses the Win32 GDI for rendering text.
539
540This currently only works on under normal Win32 and cygwin.
541DOCS
542 };
543 $formats{'freetype2'} = {
544 order=>'29',
545 def=>'HAVE_FT2',
f8e9bc07 546 inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
faa9b3e7
TC
547 libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
548 libfiles=>'-lfreetype',
549 objfiles=>'freetyp2.o',
07ea6c21 550 docs=><<DOCS,
faa9b3e7 551Freetype 2 supports both Truetype and Type 1 fonts, both of which are
f8e9bc07 552scalable. It also supports a variety of other fonts.
faa9b3e7 553DOCS
07ea6c21 554 code => \&freetype2_probe,
faa9b3e7 555 };
f7450478 556
02d1d628
AMH
557 # Make fix indent
558 for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/ /mg; }
559}
560
561
562
563sub gen {
564 my $V = $ENV{$_[0]};
565 defined($V) ? $V : "";
566}
567
568
569# Get information from environment variables
570
571sub getenv {
572
573 ($VERBOSE,
574 $INCPATH,
575 $LIBPATH,
576 $NOLOG,
577 $DEBUG_MALLOC,
578 $MANUAL,
579 $CFLAGS,
580 $LFLAGS,
581 $DFLAGS) = map { gen $_ } qw(IM_VERBOSE
582 IM_INCPATH
583 IM_LIBPATH
584 IM_NOLOG
585 IM_DEBUG_MALLOC
586 IM_MANUAL
587 IM_CFLAGS
588 IM_LFLAGS
589 IM_DFLAGS);
590
02d1d628 591}
07ea6c21 592
e11d297f
TC
593sub make_imconfig {
594 my ($defines) = @_;
595
596 open CONFIG, "> imconfig.h"
597 or die "Cannot create imconfig.h: $!\n";
598 print CONFIG <<EOS;
599/* This file is automatically generated by Makefile.PL.
600 Don't edit this file, since any changes will be lost */
601
602#ifndef IMAGER_IMCONFIG_H
603#define IMAGER_IMCONFIG_H
604EOS
605 for my $define (@$defines) {
606 if ($define->[2]) {
607 print CONFIG "\n/*\n $define->[2]\n*/\n\n";
608 }
609 print CONFIG "#define $define->[0] $define->[1]\n";
610 }
611 print CONFIG "\n#endif\n";
612 close CONFIG;
613}
614
dbc33d8a
TC
615# use pkg-config to probe for libraries
616# works around the junk that pkg-config dumps on FreeBSD
617sub _pkg_probe {
618 my ($pkg) = @_;
619
620 is_exe('pkg-config') or return;
621
622 my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
623
624 !system("pkg-config $pkg --exists $redir");
625}
626
714cf158
TC
627# probes for freetype1 by scanning @incs for the includes and
628# @libs for the libs. This is done separately because freetype's headers
629# are stored in a freetype or freetype1 directory under PREFIX/include.
630#
631# we could find the files with the existing mechanism, but it won't set
632# -I flags correctly.
633#
634# This could be extended to freetype2 too, but freetype-config catches
635# that
636sub freetype1_probe {
637 my ($frm, $frmkey) = @_;
638
639 my $found_inc;
640 INCS:
641 for my $inc (@incs) {
642 for my $subdir (qw/freetype freetype1/) {
643 my $path = File::Spec->catfile($inc, $subdir, 'freetype.h');
644 -e $path or next;
645 $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
646 -e $path and next;
647
648 $found_inc = File::Spec->catdir($inc, $subdir);
649 last INCS;
650 }
651 }
652
653 my $found_lib;
654 LIBS:
655 for my $lib (@libs) {
656 my $a_path = File::Spec->catfile($lib, "libttf$aext");
657 my $l_path = File::Spec->catfile($lib, "libttf.$lext");
658 if (-e $a_path || -e $l_path) {
659 $found_lib = $lib;
660 last LIBS;
661 }
662 }
663
89d37182 664 return unless $found_inc && $found_lib;
714cf158
TC
665 printf("%10s: includes %s - libraries %s\n", $frmkey,
666 ($found_inc ? 'found' : 'not found'),
667 ($found_lib ? 'found' : 'not found'));
714cf158
TC
668
669 $frm->{cflags} = "-I$found_inc";
670 $frm->{libfiles} = "-lttf";
671
672 return 1;
673}
674
07ea6c21
TC
675# probes for freetype2 by trying to run freetype-config
676sub freetype2_probe {
677 my ($frm, $frmkey) = @_;
678
679 is_exe('freetype-config') or return;
680
681 my $cflags = `freetype-config --cflags`
682 and !$? or return;
683 chomp $cflags;
88e16587 684
07ea6c21
TC
685 my $lflags = `freetype-config --libs`
686 and !$? or return;
687 chomp $lflags;
88e16587
TC
688
689 # before 2.1.5 freetype-config --cflags could output
690 # the -I options in the wrong order, causing a conflict with
691 # freetype1.x installed with the same --prefix
692 #
693 # can happen iff:
694 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
695 # in that order
696 # - freetype 1.x headers are in prefix/include/freetype
697 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
698 if (@incdirs == 2
699 && $incdirs[1] eq "$incdirs[0]/freetype2"
700 && -e "$incdirs[0]/freetype/freetype.h"
701 && -e "$incdirs[0]/freetype/fterrid.h") {
702 print "** freetype-config provided -I options out of order, correcting\n"
703 if $VERBOSE;
704 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
705 map "-I$_", reverse @incdirs);
706 }
707 $frm->{cflags} = $cflags;
07ea6c21
TC
708 $frm->{libfiles} = $lflags;
709
710 printf "%10s: configured via freetype-config\n", $frmkey;
711
712 return 1;
713}
714
715# probes for libpng via pkg-config
716sub png_probe {
717 my ($frm, $frmkey) = @_;
718
719 is_exe('pkg-config') or return;
720
07ea6c21
TC
721 my $config;
722 for my $check_conf (qw(libpng libpng12 libpng10)) {
dbc33d8a 723 if (_pkg_probe($check_conf)) {
07ea6c21
TC
724 $config = $check_conf;
725 last;
726 }
727 }
728 $config or return;
729
dbc33d8a
TC
730 my $cflags = `pkg-config $config --cflags`
731 and !$? or return;
732
07ea6c21
TC
733 my $lflags = `pkg-config $config --libs`
734 and !$? or return;
735
736 chomp $cflags;
737 chomp $lflags;
738 $frm->{cflags} = $cflags;
739 $frm->{libfiles} = $lflags;
740
741 printf "%10s: configured via `pkg-config $config ...`\n", $frmkey;
742
743 return 1;
744}
745
746sub catfile {
747 return File::Spec->catfile(@_);
748}
749
750sub is_exe {
751 my ($name) = @_;
752
753 for my $dir (File::Spec->path) {
754 -x catfile($dir, "$name$Config{_exe}")
755 and return 1;
756 }
757
758 return;
759}
37959076
TC
760
761sub usage {
762 print STDERR <<EOS;
274cd32b
TC
763Usage: $0 [--enable feature1,feature2,...] [other options]
764 $0 [--disable feature1,feature2,...] [other options]
37959076
TC
765 $0 --help
766Possible feature names are:
767 png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
274cd32b
TC
768Other options:
769 --verbose | -v
770 Verbose library probing (or set IM_VERBOSE in the environment)
771 --nolog
772 Disable logging (or set IM_NOLOG in the environment)
773 --incpath dir
774 Add to the include search path
775 --libpath dir
776 Add to the library search path
777 --noprobe
778 Don't use pkg-config or freetype2-config to probe for freetype2 and libpng
37959076
TC
779EOS
780 exit 1;
781
782}
92bda632
TC
783
784# generate the PM MM argument
785# I'd prefer to modify the public version, but there doesn't seem to be
786# a public API to do that
787sub gen_PM {
788 my %pm;
789 my $instbase = '$(INST_LIBDIR)';
790
791 # first the basics, .pm and .pod files
792 $pm{"Imager.pm"} = "$instbase/Imager.pm";
793
794 my $mani = maniread();
795
796 for my $filename (keys %$mani) {
797 if ($filename =~ m!^lib/! && $filename =~ /\.(pm|pod)$/) {
798 (my $work = $filename) =~ s/^lib//;
799 $pm{$filename} = $instbase . $work;
800 }
801 }
802
803 # need the typemap
804 $pm{typemap} = $instbase . '/Imager/typemap';
805
806 # and the core headers
807 for my $filename (keys %$mani) {
808 if ($filename =~ /^\w+\.h$/) {
809 $pm{$filename} = $instbase . '/Imager/include/' . $filename;
810 }
811 }
812
813 # and the generated header
814 $pm{"imconfig.h"} = $instbase . '/Imager/include/imconfig.h';
815
816 \%pm;
817}