]> git.imager.perl.org - imager.git/blob - Makefile.PL
- reading an image with an idstring of 128 or more bytes would result
[imager.git] / Makefile.PL
1 #!perl -w
2 use ExtUtils::MakeMaker;
3 use Cwd;
4 use Config;
5 use File::Spec;
6 use Getopt::Long;
7
8 #
9 # IM_INCPATH      colon seperated list of paths to extra include paths
10 # IM_LIBPATH      colon seperated list of paths to extra library paths
11 #
12 # IM_VERBOSE      turns on verbose mode for the library finding and such
13 # IM_MANUAL       to manually select which libraries are used and which not
14 # IM_ENABLE       to programmatically select which libraries are used
15 #                 and which are not
16 # IM_NOLOG        if true logging will not be compiled into the module
17 # IM_DEBUG_MALLOC if true malloc debbuging will be compiled into the module
18 #                 do not use IM_DEBUG_MALLOC in production - this slows
19 #                 everything down by alot
20 # IM_CFLAGS       Extra flags to pass to the compiler
21 # IM_LFLAGS       Extra flags to pass to the linker
22 # IM_DFLAGS       Extra flags to pass to the preprocessor
23 # IM_SUPPRESS_PROMPT  Suppress the prompt asking about gif support
24
25 my $help;
26 my @enable;
27 my @disable;
28 my @incpaths;
29 my @libpaths;
30 my $noprobe;
31 GetOptions("help" => \$help,
32            "enable=s" => \@enable,
33            "disable=s" => \@disable,
34            "incpath=s", \@incpaths,
35            "libpath=s" => \@libpaths,
36            "noprobe" => \$noprobe);
37
38 if ($help) {
39   usage();
40 }
41
42 if (@enable && @disable) {
43   print STDERR "Only --enable or --disable can be used, not both, try --help\n";
44   exit 1;
45 }
46
47 getenv();     # get environment variables
48 init();       # initialize global data
49 pathcheck();  # Check if directories exist
50
51 if (exists $ENV{IM_ENABLE}) {
52   my %en = map { $_, 1 } split ' ', $ENV{IM_ENABLE};
53   for my $key (keys %formats) {
54     delete $formats{$key} unless $en{$key};
55   }
56 }
57 if (@enable) {
58   my %en = map { $_ => 1 } map { split /,/ } @enable;
59   for my $key (keys %formats) {
60     delete $formats{$key} unless $en{$key};
61   }
62 }
63 elsif (@disable) {
64   delete @formats{map { split /,/ } @disable};
65 }
66
67 my @defines;
68
69 # Pick what libraries are used
70 if ($MANUAL) {
71   manual();
72 } else {
73   automatic();
74 }
75
76 # Make sure there isn't a clash between the gif libraries.
77 gifcheck();
78
79 my $lib_cflags = '';
80 for my $frmkey (keys %formats) {
81   my $frm = $formats{$frmkey};
82   push @defines, [ $frm->{def}, 1, "$frmkey available" ];
83   $F_LIBS   .= ' '  .$frm->{libfiles};
84   $F_OBJECT .= ' '  .$frm->{objfiles};
85   $lib_cflags   .= ' '  .$frm->{cflags} if $frm->{cflags};
86 }
87
88 $F_INC  = join ' ', map "-I$_", map / / ? qq{"$_"} : $_, 
89   grep !exists $definc{$_}, @incs;
90 $F_LIBS = join(' ',map "-L$_", map / / ? qq{"$_"} : $_, @libs) . $F_LIBS;
91
92 $OSLIBS = '';
93 $OSDEF  = "-DOS_$^O";
94
95 if ($^O eq 'hpux')                { $OSLIBS .= ' -ldld'; }
96 if (defined $Config{'d_dlsymun'}) { $OSDEF  .= ' -DDLSYMUN'; }
97
98 @objs = qw(Imager.o draw.o polygon.o image.o io.o iolayer.o
99            log.o gaussian.o conv.o pnm.o raw.o feat.o font.o
100            filters.o dynaload.o stackmach.o datatypes.o
101            regmach.o trans2.o quant.o error.o convert.o
102            map.o tags.o palimg.o maskimg.o img16.o rotate.o
103            bmp.o tga.o rgb.o color.o fills.o imgdouble.o);
104
105 %opts=(
106        'NAME'         => 'Imager',
107        'VERSION_FROM' => 'Imager.pm',
108        'LIBS'         => "$LFLAGS -lm $OSLIBS $F_LIBS",
109        'DEFINE'       => "$OSDEF $CFLAGS",
110        'INC'          => "$lib_cflags $DFLAGS $F_INC",
111        'OBJECT'       => join(' ', @objs, $F_OBJECT),
112        clean          => { FILES=>'testout' },
113       );
114
115 if ($ExtUtils::MakeMaker::VERSION > 6.06) {
116   $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
117   $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
118 }
119 if ($ExtUtils::MakeMaker::VERSION > 6.10) {
120   $opts{NO_META} = 1;
121 }
122
123 make_imconfig(\@defines);
124
125 if ($VERBOSE) { print Dumper(\%opts); }
126 mkdir('testout',0777); # since we cannot include it in the archive.
127 WriteMakefile(%opts);
128
129 exit;
130
131
132 sub MY::postamble {
133 '
134 dyntest.$(MYEXTLIB) : dynfilt/Makefile
135         cd dynfilt && $(MAKE) $(PASTHRU)
136
137 lib/Imager/Regops.pm : regmach.h regops.perl
138         $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
139
140 imconfig.h: Makefile.PL
141         $(ECHO) "imconfig.h out-of-date with respect to $?"
142         $(PERLRUN) Makefile.PL
143         $(ECHO) "==> Your Makefile has been rebuilt - re-run your make command <=="
144 ';
145 }
146
147 # manual configuration of helper libraries
148
149 sub manual {
150   print <<EOF;
151
152       Please answer the following questions about
153       which formats are avaliable on your computer
154
155 press <return> to continue
156 EOF
157
158   <STDIN>; # eat one return
159
160   for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
161   SWX:
162     if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
163     print "Enable $frm support: ";
164     $gz = <STDIN>;
165     chomp($gz);
166     if ($gz =~ m/^(y|yes|n|no)/i) {
167       $gz=substr(lc($gz),0,1);
168       if ($gz eq 'n') {
169         delete $formats{$frm};
170       }
171     } else { goto SWX; }
172   }
173 }
174
175
176 # automatic configuration of helper libraries
177
178 sub automatic {
179   for $frm(keys %formats) {
180     delete $formats{$frm} if !checkformat($frm);        
181   }
182 }
183
184
185 sub gifcheck {
186   if ($formats{'gif'} and $formats{'ungif'}) {
187     print "ungif and gif can not coexist - removing ungif support\n";
188     delete $formats{'ungif'};
189   }
190
191  RETR:
192   if (($formats{'gif'} or $formats{'ungif'}) && !$ENV{IM_SUPPRESS_PROMPT}) {
193     print <<EOFF;
194
195 You have libgif or libungif installed.  They are both known to have
196 bugs.  Imager can crash or display other strange behaviour after
197 reading or writing gif images.  Some of the gif tests can even fail
198 since they stress some parts of the buggy code.
199
200 libungif 4.1.2 and later is safe.  giflib 4.1.3 needs at least one
201 patch to have all the bugs fixed, see README for details.
202
203 Of course it's possible your operating system distributor has patched
204 all of these problems and you have nothing to worry about.
205
206 Do you want to remove gif support? [Y/n]
207 EOFF
208     my $resp = <STDIN>;
209     chomp($resp);
210     if ($resp ne "n") {
211       delete $formats{'gif'};
212       delete $formats{'ungif'};
213       return;
214     }
215   }
216
217   for my $frm (qw(gif ungif)) {
218     checkformat($frm) if ($MANUAL and $formats{$frm});
219   }
220
221   my @dirs;
222   for my $frm (grep $formats{$_}, qw(gif ungif)) {
223     push(@dirs, @{$formats{$frm}{incdir}}) if $formats{$frm}{incdir};
224   }
225   my $minor = 0;
226   my $major = 0;
227   FILES: for my $dir (@dirs) {
228     my $h = "$dir/gif_lib.h";
229     open H, "< $h" or next;
230     while (<H>) {
231       if (/GIF_LIB_VERSION\s+"\s*version\s*(\d+)\.(\d+)/i) {
232         $major = $1;
233         $minor = $2;
234         close H;
235         last FILES;
236       }
237     }
238     close H;
239   }
240
241   # we need the version in a #ifdefable form
242
243   push @defines, [ IM_GIFMAJOR, $major, "Parsed giflib version" ];
244   push @defines, [ IM_GIFMINOR, $minor ];
245 }
246
247
248 sub gd {
249   my($path,$chk)=@_;
250
251 #    print "checking path $path\n";
252   if ( !opendir(DH,$path) ) {
253     warn "Cannot open dir $path: $!\n";
254     return;
255   }
256   my @l=grep { $chk->($_) } readdir(DH);
257   #    print @l;
258   close(DH);
259   return map $path, @l;
260 }
261
262
263 sub checkformat {
264   my $frm=shift;
265   
266   my $code = $formats{$frm}{'code'};
267   if ($code && !$noprobe) {
268     return 1 if $code->($formats{$frm}, $frm);
269   }
270
271   my $libchk=$formats{$frm}{'libcheck'};
272   my $incchk=$formats{$frm}{'inccheck'};
273
274   my @l;
275   for my $lp (@libs) {
276     push(@l, gd($lp,$libchk));
277   }
278
279   my @i;
280   for my $ip (@incs) {
281     push(@i, $ip) if $incchk->($ip,$frm);
282   }
283
284   printf("%10s: includes %s - libraries %s\n",$frm,(@i?'found':'not found'),(@l?'found':'not found'));
285   $formats{$frm}{incdir} = \@i;
286   $formats{$frm}{libdir} = \@l;
287   return scalar(@i && @l);
288 }
289
290
291 sub pathcheck {
292   if ($VERBOSE) {
293     print "pathcheck\n";
294     print "  Include paths:\n";
295     for (@incs) { print $_,"\n"; }
296   }
297   @incs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
298
299   if ($VERBOSE) {
300     print "\nLibrary paths:\n";
301     for (@incs) { print $_,"\n"; }
302   }
303   @libs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
304   print "\ndone.\n";
305 }
306
307
308 # Format data initialization
309
310 # format definition is:
311 # defines needed
312 # default include path
313 # files needed for include (boolean perl code)
314 # default lib path
315 # libs needed
316 # files needed for link (boolean perl code)
317 # object files needed for the format
318
319
320 sub init {
321
322   @definc{'/usr/include'}=();
323   @incs=(split(/\Q$Config{path_sep}/, $INCPATH),
324         map { split /\Q$Config{path_sep}/} @incpaths );
325   if ($Config{locincpth}) {
326     push @incs, grep -d, split ' ', $Config{locincpth};
327   }
328   if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
329     push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
330   }
331   push @incs, grep -d,
332       qw(/sw/include 
333          /usr/include/freetype2
334          /usr/local/include/freetype2
335          /usr/local/include/freetype1/freetype
336          /usr/include /usr/local/include /usr/include/freetype
337          /usr/local/include/freetype);
338
339   @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
340     map { split /\Q$Config{path_sep}/} @libpaths );
341   if ($Config{loclibpth}) {
342     push @libs, grep -d, split ' ', $Config{loclibpth};
343   }
344   push @libs, grep -d, qw(/sw/lib),  split(/ /, $Config{'libpth'});
345   if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
346     push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
347   }
348   if ($^O eq 'cygwin') {
349     push(@libs, '/usr/lib/w32api') if -d '/usr/lib/w32api';
350     push(@incs, '/usr/include/w32api') if -d '/usr/lib/w32api';
351   }
352
353   my $lext=$Config{'so'};   # Get extensions of libraries
354   my $aext=$Config{'_a'};
355
356   $formats{'jpeg'}={
357                     order=>'21',
358                     def=>'HAVE_LIBJPEG',
359                     inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
360                     libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
361                     libfiles=>'-ljpeg',
362                     objfiles=>'jpeg.o',
363                     docs=>q{
364                             In order to use jpeg with this module you need to have libjpeg
365                             installed on your computer}
366                    };
367
368   $formats{'tiff'}={
369                     order=>'23',
370                     def=>'HAVE_LIBTIFF',
371                     inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
372                     libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
373                     libfiles=>'-ltiff',
374                     objfiles=>'tiff.o',
375                     docs=>q{
376                             In order to use tiff with this module you need to have libtiff
377                             installed on your computer}
378                    };
379
380   $formats{'png'}={
381                    order=>'22',
382                    def=>'HAVE_LIBPNG',
383                    inccheck=>sub { -e catfile($_[0], 'png.h') },
384                    libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
385                    libfiles=>'-lpng -lz',
386                    objfiles=>'png.o',
387                    docs=>q{
388                            Png stands for Portable Network Graphics and is intended as
389                            a replacement for gif on the web. It is patent free and
390                            is recommended by the w3c, you need libpng to use these formats},
391                    code => \&png_probe,
392                   };
393
394   $formats{'gif'}={
395                    order=>'20',
396                    def=>'HAVE_LIBGIF',
397                    inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
398                    libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
399                    libfiles=>'-lgif',
400                    objfiles=>'gif.o',
401                    docs=>q{
402                            gif is the de facto standard for webgraphics at the moment,
403                            it does have some patent problems. If you have giflib and
404                            are not in violation with the unisys patent you should use
405                            this instead of the 'ungif' option.  Note that they cannot
406                            be in use at the same time}
407                   };
408
409   $formats{'ungif'}={
410                      order=>'21',
411                      def=>'HAVE_LIBGIF',
412                      inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
413                      libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
414                      libfiles=>'-lungif',
415                      objfiles=>'gif.o',
416                      docs=>q{
417                              gif is the de facto standard for webgraphics at the moment,
418                              it does have some patent problems. If you have libungif and
419                              want to create images free from LZW patented compression you
420                              should use this option instead of the 'gif' option}
421                     };
422
423   $formats{'T1-fonts'}={
424                         order=>'30',
425                         def=>'HAVE_LIBT1',
426                         inccheck=>sub { -e catfile($_[0], 't1lib.h') },
427                         libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
428                         libfiles=>'-lt1',
429                         objfiles=>'',
430                         docs=>q{
431                                 postscript t1 fonts are scalable fonts. They can include 
432                                 ligatures and kerning information and generally yield good
433                                 visual quality. We depend on libt1 to rasterize the fonts
434                                 for use in images.}
435                        };
436
437   $formats{'TT-fonts'}=
438     {
439      order=>'31',
440      def=>'HAVE_LIBTT',
441      inccheck=>sub { -e catfile($_[0], 'freetype.h')
442                        && !-e catfile($_[0], 'fterrors.h') },
443      libcheck=>sub { $_[0] eq "libttf$aext" or $_[0] eq "libttf.$lext" },
444      libfiles=>'-lttf',
445      objfiles=>'',
446      docs=>q{
447 Truetype fonts are scalable fonts. They can include 
448 kerning and hinting information and generally yield good
449 visual quality esp on low resultions. The freetype library is
450 used to rasterize for us. The only drawback is that there
451 are alot of badly designed fonts out there.}
452                        };
453   $formats{'w32'} = {
454                      order=>40,
455                      def=>'HAVE_WIN32',
456                      inccheck=>sub { -e catfile($_[0], 'windows.h') },
457                      libcheck=>sub { lc $_[0] eq 'gdi32.lib' 
458                                        || lc $_[0] eq 'libgdi32.a' },
459                      libfiles=>$^O eq 'cygwin' ? '-lgdi32' : '',
460                      objfiles=>'win32.o',
461                      docs => <<DOCS
462 Uses the Win32 GDI for rendering text.
463
464 This currently only works on under normal Win32 and cygwin.
465 DOCS
466                     };
467   $formats{'freetype2'} = {
468                            order=>'29',
469                            def=>'HAVE_FT2',
470                            inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
471                            libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
472                            libfiles=>'-lfreetype',
473                            objfiles=>'freetyp2.o',
474                            docs=><<DOCS,
475 Freetype 2 supports both Truetype and Type 1 fonts, both of which are
476 scalable.  It also supports a variety of other fonts.
477 DOCS
478                            code => \&freetype2_probe,
479                           };
480   # Make fix indent
481   for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/  /mg; }
482 }
483
484
485
486 sub gen {
487   my $V = $ENV{$_[0]};
488   defined($V) ? $V : "";
489 }
490
491
492 # Get information from environment variables
493
494 sub getenv {
495
496   ($VERBOSE,
497    $INCPATH,
498    $LIBPATH,
499    $NOLOG,
500    $DEBUG_MALLOC,
501    $MANUAL,
502    $CFLAGS,
503    $LFLAGS,
504    $DFLAGS) = map { gen $_ } qw(IM_VERBOSE
505                                 IM_INCPATH
506                                 IM_LIBPATH
507                                 IM_NOLOG
508                                 IM_DEBUG_MALLOC
509                                 IM_MANUAL
510                                 IM_CFLAGS
511                                 IM_LFLAGS
512                                 IM_DFLAGS);
513
514   if ($VERBOSE) { print "Verbose mode\n"; require Data::Dumper; import Data::Dumper qw(Dumper);}
515
516   if ($NOLOG)   { print "Logging not compiled into module\n"; }
517   else { 
518     push @defines, [ IMAGER_LOG => 1, "Logging system" ];
519   }
520
521   if ($DEBUG_MALLOC) {
522     push @defines, [ IMAGER_DEBUG_MALLOC => 1, "Use Imager's DEBUG malloc()" ];
523     print "Malloc debugging enabled\n";
524   }
525
526 }
527
528 sub make_imconfig {
529   my ($defines) = @_;
530
531   open CONFIG, "> imconfig.h"
532     or die "Cannot create imconfig.h: $!\n";
533   print CONFIG <<EOS;
534 /* This file is automatically generated by Makefile.PL.
535    Don't edit this file, since any changes will be lost */
536
537 #ifndef IMAGER_IMCONFIG_H
538 #define IMAGER_IMCONFIG_H
539 EOS
540   for my $define (@$defines) {
541     if ($define->[2]) {
542       print CONFIG "\n/*\n  $define->[2]\n*/\n\n";
543     }
544     print CONFIG "#define $define->[0] $define->[1]\n";
545   }
546   print CONFIG "\n#endif\n";
547   close CONFIG;
548 }
549
550 # probes for freetype2 by trying to run freetype-config
551 sub freetype2_probe {
552   my ($frm, $frmkey) = @_;
553
554   is_exe('freetype-config') or return;
555
556   my $cflags = `freetype-config --cflags`
557     and !$? or return;
558   chomp $cflags;
559   
560   $frm->{cflags} = $cflags;
561   my $lflags = `freetype-config --libs`
562     and !$? or return;
563   chomp $lflags;
564   $frm->{libfiles} = $lflags;
565
566   printf "%10s: configured via freetype-config\n", $frmkey;
567
568   return 1;
569 }
570
571 # probes for libpng via pkg-config
572 sub png_probe {
573   my ($frm, $frmkey) = @_;
574
575   is_exe('pkg-config') or return;
576
577   my $cflags;
578   my $config;
579   for my $check_conf (qw(libpng libpng12 libpng10)) {
580     $cflags = `pkg-config $check_conf --cflags`;
581     if ($cflags && !$?) {
582       $config = $check_conf;
583       last;
584     }
585   }
586   $config or return;
587
588   my $lflags = `pkg-config $config --libs`
589     and !$? or return;
590
591   chomp $cflags;
592   chomp $lflags;
593   $frm->{cflags} = $cflags;
594   $frm->{libfiles} = $lflags;
595
596   printf "%10s: configured via `pkg-config $config ...`\n", $frmkey;
597
598   return 1;
599 }
600
601 sub catfile {
602   return File::Spec->catfile(@_);
603 }
604
605 sub is_exe {
606   my ($name) = @_;
607
608   for my $dir (File::Spec->path) {
609     -x catfile($dir, "$name$Config{_exe}")
610       and return 1;
611   }
612
613   return;
614 }
615
616 sub usage {
617   print STDERR <<EOS;
618 Usage: $0 [--enable feature1,feature2,...] [--incpath ...] [--libpath ...]
619        $0 [--disable feature1,feature2,...] [--incpath ...] [--libpath ...]
620        $0 --help
621 Possible feature names are:
622   png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
623 EOS
624   exit 1;
625
626 }