]> git.imager.perl.org - imager.git/blob - Makefile.PL
7b0040290809f0f390d47e1c925b3d382193e06d
[imager.git] / Makefile.PL
1 #!perl -w
2 use strict;
3 use ExtUtils::MakeMaker;
4 use Cwd;
5 use Config;
6 use File::Spec;
7 use Getopt::Long;
8 use vars qw(%Recommends);
9 require "metafile.pl";
10 use ExtUtils::Manifest qw(maniread);
11 use vars qw(%formats $VERBOSE $INCPATH $LIBPATH $NOLOG $DEBUG_MALLOC $MANUAL $CFLAGS $LFLAGS $DFLAGS);
12
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 # IM_SUPPRESS_PROMPT  Suppress the prompt asking about gif support
29
30 getenv();     # get environment variables
31
32 my $lext=$Config{'so'};   # Get extensions of libraries
33 my $aext=$Config{'_a'};
34
35 my $help; # display help if set
36 my @enable; # list of drivers to enable
37 my @disable; # or list of drivers to disable
38 my @incpaths; # places to look for headers
39 my @libpaths; # places to look for libraries
40 my $noprobe; # non-zero to disable newer probes
41 my $noexif; # non-zero to disable EXIF parsing of JPEGs
42 GetOptions("help" => \$help,
43            "enable=s" => \@enable,
44            "disable=s" => \@disable,
45            "incpath=s", \@incpaths,
46            "libpath=s" => \@libpaths,
47            "noprobe" => \$noprobe,
48            "verbose|v" => \$VERBOSE,
49            "nolog" => \$NOLOG,
50            "noexif" => \$noexif);
51
52 if ($VERBOSE) { 
53   print "Verbose mode\n"; 
54   require Data::Dumper; 
55   import Data::Dumper qw(Dumper);
56 }
57
58 if ($help) {
59   usage();
60 }
61
62 my @defines;
63
64 if ($NOLOG)   { print "Logging not compiled into module\n"; }
65 else { 
66   push @defines, [ IMAGER_LOG => 1, "Logging system" ];
67 }
68
69 if ($DEBUG_MALLOC) {
70   push @defines, [ IMAGER_DEBUG_MALLOC => 1, "Use Imager's DEBUG malloc()" ];
71   print "Malloc debugging enabled\n";
72 }
73
74 if (@enable && @disable) {
75   print STDERR "Only --enable or --disable can be used, not both, try --help\n";
76   exit 1;
77 }
78
79 my %definc;
80 my %deflib;
81 my @incs; # all the places to look for headers
82 my @libs; # all the places to look for libraries
83
84 init();       # initialize global data
85 pathcheck();  # Check if directories exist
86
87 if (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 }
93 if (@enable) {
94   my %en = map { $_ => 1 } map { split /,/ } @enable;
95   for my $key (keys %formats) {
96     delete $formats{$key} unless $en{$key};
97   }
98 }
99 elsif (@disable) {
100   delete @formats{map { split /,/ } @disable};
101 }
102
103 # Pick what libraries are used
104 if ($MANUAL) {
105   manual();
106 } else {
107   automatic();
108 }
109
110 # Make sure there isn't a clash between the gif libraries.
111 gifcheck();
112
113 my $lib_cflags = '';
114 my $F_LIBS = '';
115 my $F_OBJECT = '';
116 for my $frmkey (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
117   my $frm = $formats{$frmkey};
118   push @defines, [ $frm->{def}, 1, "$frmkey available" ];
119   $F_LIBS   .= ' '  .$frm->{libfiles};
120   $F_OBJECT .= ' '  .$frm->{objfiles};
121   if ($frm->{cflags}) {
122     $lib_cflags   .= ' '  .$frm->{cflags};
123     ++$definc{$_} for map { /^-I(.*)$/ ? ($1) : () }
124       grep /^-I./, split ' ', $frm->{cflags};
125   }
126 }
127
128 unless ($noexif) {
129   print "EXIF support enabled\n";
130   push @defines, [ 'IMEXIF_ENABLE', 1, "Enable experimental EXIF support" ];
131   $F_OBJECT .= ' imexif.o';
132 }
133
134 my $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;
138
139 my $OSLIBS = '';
140 my $OSDEF  = "-DOS_$^O";
141
142 if ($^O eq 'hpux')                { $OSLIBS .= ' -ldld'; }
143 if (defined $Config{'d_dlsymun'}) { $OSDEF  .= ' -DDLSYMUN'; }
144
145 my @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);
152
153 $Recommends{Imager} =
154   { 'Parse::RecDescent' => 0 };
155
156 my %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          );
166
167 if ($ExtUtils::MakeMaker::VERSION > 6.06) {
168   $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
169   $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
170 }
171
172 make_imconfig(\@defines);
173
174 if ($VERBOSE) { print Dumper(\%opts); }
175 mkdir('testout',0777); # since we cannot include it in the archive.
176 WriteMakefile(%opts);
177
178 exit;
179
180
181 sub MY::postamble {
182     my $self = shift;
183     my $perl = $self->{PERLRUN} ? '$(PERLRUN)' : '$(PERL)';
184 '
185 dyntest.$(MYEXTLIB) : dynfilt/Makefile
186         cd dynfilt && $(MAKE) $(PASTHRU)
187
188 lib/Imager/Regops.pm : regmach.h regops.perl
189         $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
190
191 imconfig.h : Makefile.PL
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 <=="
195 '.qq!
196 lib/Imager/APIRef.pm : \$(C_FILES) apidocs.perl
197         $perl apidocs.perl lib/Imager/APIRef.pm
198
199 !;
200
201 }
202
203 # manual configuration of helper libraries
204
205 sub manual {
206   print <<EOF;
207
208       Please answer the following questions about
209       which formats are avaliable on your computer
210
211 press <return> to continue
212 EOF
213
214   <STDIN>; # eat one return
215
216   for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
217   SWX:
218     if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
219     print "Enable $frm support: ";
220     my $gz = <STDIN>;
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
234 sub automatic {
235   print "Automatic probing:\n" if $VERBOSE;
236   for my $frm (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
237     delete $formats{$frm} if !checkformat($frm);        
238   }
239 }
240
241
242 sub gifcheck {
243   if ($formats{'gif'} and $formats{'ungif'}) {
244     print "ungif and gif can not coexist - removing ungif support\n";
245     delete $formats{'ungif'};
246   }
247
248  RETR:
249   if (($formats{'gif'} or $formats{'ungif'}) && !$ENV{IM_SUPPRESS_PROMPT}) {
250     print <<EOFF;
251
252 You have libgif or libungif installed.  They are both known to have
253 bugs.  Imager can crash or display other strange behaviour after
254 reading or writing gif images.  Some of the gif tests can even fail
255 since they stress some parts of the buggy code.
256
257 libungif 4.1.2 and later is safe.  giflib 4.1.3 needs at least one
258 patch to have all the bugs fixed, see README for details.
259
260 Of course it's possible your operating system distributor has patched
261 all of these problems and you have nothing to worry about.
262
263 Do you want to remove gif support? [Y/n]
264 EOFF
265     my $resp = <STDIN>;
266     chomp($resp);
267     if ($resp ne "n") {
268       delete $formats{'gif'};
269       delete $formats{'ungif'};
270       return;
271     }
272   }
273
274   for my $frm (qw(gif ungif)) {
275     checkformat($frm) if ($MANUAL and $formats{$frm});
276   }
277
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
299
300   push @defines, [ IM_GIFMAJOR => $major, "Parsed giflib version" ];
301   push @defines, [ IM_GIFMINOR => $minor ];
302 }
303
304
305 sub 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
320 sub checkformat {
321   my $frm=shift;
322
323   print "  checkformat($frm)\n" if $VERBOSE;
324   
325   my $code = $formats{$frm}{'code'};
326   if ($code && !$noprobe) {
327     print "    Calling probe function\n" if $VERBOSE;
328     return 1 if $code->($formats{$frm}, $frm);
329   }
330
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) {
341     push(@i, $ip) if $incchk->($ip,$frm);
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
351 sub pathcheck {
352   if ($VERBOSE) {
353     print "pathcheck\n";
354     print "  Include paths:\n";
355     for (@incs) { print $_,"\n"; }
356   }
357   @incs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
358
359   if ($VERBOSE) {
360     print "\nLibrary paths:\n";
361     for (@libs) { print $_,"\n"; }
362   }
363   @libs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
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
380 sub init {
381
382   my @definc = qw(/usr/include);
383   @definc{@definc}=(1) x @definc;
384   @incs=(split(/\Q$Config{path_sep}/, $INCPATH),
385         map { split /\Q$Config{path_sep}/} @incpaths );
386   if ($Config{locincpth}) {
387     push @incs, grep -d, split ' ', $Config{locincpth};
388   }
389   if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
390     push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
391   }
392   push @incs, grep -d,
393       qw(/sw/include 
394          /usr/include/freetype2
395          /usr/local/include/freetype2
396          /usr/local/include/freetype1/freetype
397          /usr/include /usr/local/include /usr/include/freetype
398          /usr/local/include/freetype);
399   if ($Config{ccflags}) {
400     my @hidden = map { /^-I(.*)$/ ? ($1) : () } split ' ', $Config{ccflags};
401     push @incs, @hidden;
402     @definc{@hidden} = (1) x @hidden;
403   }
404
405   @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
406     map { split /\Q$Config{path_sep}/} @libpaths );
407   if ($Config{loclibpth}) {
408     push @libs, grep -d, split ' ', $Config{loclibpth};
409   }
410   
411   push @libs, grep -d, qw(/sw/lib),  split(/ /, $Config{'libpth'});
412   push @libs, grep -d, split / /, $Config{libspath} if $Config{libspath};
413   if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
414     push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
415   }
416   if ($^O eq 'cygwin') {
417     push(@libs, '/usr/lib/w32api') if -d '/usr/lib/w32api';
418     push(@incs, '/usr/include/w32api') if -d '/usr/include/w32api';
419   }
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   }
430
431   $formats{'jpeg'}={
432                     order=>'21',
433                     def=>'HAVE_LIBJPEG',
434                     inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
435                     libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
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',
446                     inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
447                     libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
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',
458                    inccheck=>sub { -e catfile($_[0], 'png.h') },
459                    libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
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
465                            is recommended by the w3c, you need libpng to use these formats},
466                    code => \&png_probe,
467                   };
468
469   $formats{'gif'}={
470                    order=>'20',
471                    def=>'HAVE_LIBGIF',
472                    inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
473                    libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
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',
487                      inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
488                      libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
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',
501                         inccheck=>sub { -e catfile($_[0], 't1lib.h') },
502                         libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
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
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=>'',
521      code => \&freetype1_probe,
522      docs=>q{
523 Truetype fonts are scalable fonts. They can include 
524 kerning and hinting information and generally yield good
525 visual quality esp on low resultions. The freetype library is
526 used to rasterize for us. The only drawback is that there
527 are alot of badly designed fonts out there.}
528                        };
529   $formats{'w32'} = {
530                      order=>40,
531                      def=>'HAVE_WIN32',
532                      inccheck=>sub { -e catfile($_[0], 'windows.h') },
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
538 Uses the Win32 GDI for rendering text.
539
540 This currently only works on under normal Win32 and cygwin.
541 DOCS
542                     };
543   $formats{'freetype2'} = {
544                            order=>'29',
545                            def=>'HAVE_FT2',
546                            inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
547                            libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
548                            libfiles=>'-lfreetype',
549                            objfiles=>'freetyp2.o',
550                            docs=><<DOCS,
551 Freetype 2 supports both Truetype and Type 1 fonts, both of which are
552 scalable.  It also supports a variety of other fonts.
553 DOCS
554                            code => \&freetype2_probe,
555                           };
556
557   # Make fix indent
558   for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/  /mg; }
559 }
560
561
562
563 sub gen {
564   my $V = $ENV{$_[0]};
565   defined($V) ? $V : "";
566 }
567
568
569 # Get information from environment variables
570
571 sub 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
591 }
592
593 sub 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
604 EOS
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
615 # use pkg-config to probe for libraries
616 # works around the junk that pkg-config dumps on FreeBSD
617 sub _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
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
636 sub 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
664   printf("%10s: includes %s - libraries %s\n", $frmkey,
665          ($found_inc ? 'found' : 'not found'), 
666          ($found_lib ? 'found' : 'not found'));
667   return unless $found_inc && $found_lib;
668
669   $frm->{cflags} = "-I$found_inc";
670   $frm->{libfiles} = "-lttf";
671
672   return 1;
673 }
674
675 # probes for freetype2 by trying to run freetype-config
676 sub 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;
684
685   my $lflags = `freetype-config --libs`
686     and !$? or return;
687   chomp $lflags;
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;
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
716 sub png_probe {
717   my ($frm, $frmkey) = @_;
718
719   is_exe('pkg-config') or return;
720
721   my $config;
722   for my $check_conf (qw(libpng libpng12 libpng10)) {
723     if (_pkg_probe($check_conf)) {
724       $config = $check_conf;
725       last;
726     }
727   }
728   $config or return;
729
730   my $cflags = `pkg-config $config --cflags`
731     and !$? or return;
732
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
746 sub catfile {
747   return File::Spec->catfile(@_);
748 }
749
750 sub 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 }
760
761 sub usage {
762   print STDERR <<EOS;
763 Usage: $0 [--enable feature1,feature2,...] [other options]
764        $0 [--disable feature1,feature2,...]  [other options]
765        $0 --help
766 Possible feature names are:
767   png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
768 Other 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
779 EOS
780   exit 1;
781
782 }
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
787 sub 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 }