updates, rearranged to get the stuff still needed at the top
[imager.git] / Makefile.PL
CommitLineData
ea9e6c3f 1#!perl -w
02d1d628
AMH
2use ExtUtils::MakeMaker;
3use Cwd;
4use Config;
07ea6c21 5use File::Spec;
37959076 6use Getopt::Long;
02d1d628 7
02d1d628
AMH
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
f4dbac50 23# IM_SUPPRESS_PROMPT Suppress the prompt asking about gif support
02d1d628 24
37959076
TC
25my $help;
26my @enable;
27my @disable;
28my @incpaths;
29my @libpaths;
30my $noprobe;
31GetOptions("help" => \$help,
32 "enable=s" => \@enable,
33 "disable=s" => \@disable,
34 "incpath=s", \@incpaths,
35 "libpath=s" => \@libpaths,
36 "noprobe" => \$noprobe);
37
38if ($help) {
39 usage();
40}
41
42if (@enable && @disable) {
43 print STDERR "Only --enable or --disable can be used, not both, try --help\n";
44 exit 1;
45}
02d1d628
AMH
46
47getenv(); # get environment variables
48init(); # initialize global data
49pathcheck(); # Check if directories exist
50
37959076
TC
51if (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}
57if (@enable) {
58 my %en = map { $_ => 1 } map { split /,/ } @enable;
59 for my $key (keys %formats) {
60 delete $formats{$key} unless $en{$key};
61 }
62}
63elsif (@disable) {
64 delete @formats{map { split /,/ } @disable};
65}
66
e11d297f
TC
67my @defines;
68
02d1d628
AMH
69# Pick what libraries are used
70if ($MANUAL) {
71 manual();
72} else {
73 automatic();
02d1d628
AMH
74}
75
76# Make sure there isn't a clash between the gif libraries.
77gifcheck();
78
07ea6c21 79my $lib_cflags = '';
e11d297f
TC
80for my $frmkey (keys %formats) {
81 my $frm = $formats{$frmkey};
82 push @defines, [ $frm->{def}, 1, "$frmkey available" ];
02d1d628
AMH
83 $F_LIBS .= ' ' .$frm->{libfiles};
84 $F_OBJECT .= ' ' .$frm->{objfiles};
07ea6c21 85 $lib_cflags .= ' ' .$frm->{cflags} if $frm->{cflags};
02d1d628
AMH
86}
87
ea9e6c3f
TC
88$F_INC = join ' ', map "-I$_", map / / ? qq{"$_"} : $_,
89 grep !exists $definc{$_}, @incs;
90$F_LIBS = join(' ',map "-L$_", map / / ? qq{"$_"} : $_, @libs) . $F_LIBS;
02d1d628
AMH
91
92$OSLIBS = '';
93$OSDEF = "-DOS_$^O";
94
95if ($^O eq 'hpux') { $OSLIBS .= ' -ldld'; }
96if (defined $Config{'d_dlsymun'}) { $OSDEF .= ' -DDLSYMUN'; }
97
9982a307
AMH
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
02d1d628 100 filters.o dynaload.o stackmach.o datatypes.o
2df3535a 101 regmach.o trans2.o quant.o error.o convert.o
261f91c5 102 map.o tags.o palimg.o maskimg.o img16.o rotate.o
737a830c 103 bmp.o tga.o rgb.o color.o fills.o imgdouble.o);
02d1d628
AMH
104
105%opts=(
106 'NAME' => 'Imager',
107 'VERSION_FROM' => 'Imager.pm',
108 'LIBS' => "$LFLAGS -lm $OSLIBS $F_LIBS",
e11d297f 109 'DEFINE' => "$OSDEF $CFLAGS",
07ea6c21 110 'INC' => "$lib_cflags $DFLAGS $F_INC",
de2b8bae
TC
111 'OBJECT' => join(' ', @objs, $F_OBJECT),
112 clean => { FILES=>'testout' },
02d1d628
AMH
113 );
114
29316bdb
TC
115if ($ExtUtils::MakeMaker::VERSION > 6.06) {
116 $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
ca508100
TC
117 $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
118}
29316bdb
TC
119if ($ExtUtils::MakeMaker::VERSION > 6.10) {
120 $opts{NO_META} = 1;
121}
ca508100 122
e11d297f
TC
123make_imconfig(\@defines);
124
02d1d628
AMH
125if ($VERBOSE) { print Dumper(\%opts); }
126mkdir('testout',0777); # since we cannot include it in the archive.
127WriteMakefile(%opts);
4dce694d 128
02d1d628
AMH
129exit;
130
131
132sub MY::postamble {
133'
faa9b3e7 134dyntest.$(MYEXTLIB) : dynfilt/Makefile
02d1d628
AMH
135 cd dynfilt && $(MAKE) $(PASTHRU)
136
137lib/Imager/Regops.pm : regmach.h regops.perl
138 $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
e11d297f
TC
139
140imconfig.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 <=="
02d1d628
AMH
144';
145}
146
147# manual configuration of helper libraries
148
149sub manual {
150 print <<EOF;
151
152 Please answer the following questions about
153 which formats are avaliable on your computer
154
155press <return> to continue
156EOF
157
158 <STDIN>; # eat one return
159
2646b26c 160 for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
02d1d628
AMH
161 SWX:
162 if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
163 print "Enable $frm support: ";
09fd3468 164 $gz = <STDIN>;
02d1d628
AMH
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
178sub automatic {
179 for $frm(keys %formats) {
180 delete $formats{$frm} if !checkformat($frm);
181 }
182}
183
184
185sub gifcheck {
5f5fe73e 186 if ($formats{'gif'} and $formats{'ungif'}) {
02d1d628
AMH
187 print "ungif and gif can not coexist - removing ungif support\n";
188 delete $formats{'ungif'};
189 }
5f5fe73e
AMH
190
191 RETR:
f4dbac50 192 if (($formats{'gif'} or $formats{'ungif'}) && !$ENV{IM_SUPPRESS_PROMPT}) {
5f5fe73e
AMH
193 print <<EOFF;
194
195You have libgif or libungif installed. They are both known to have
196bugs. Imager can crash or display other strange behaviour after
197reading or writing gif images. Some of the gif tests can even fail
198since they stress some parts of the buggy code.
199
f1967c11
TC
200libungif 4.1.2 and later is safe. giflib 4.1.3 needs at least one
201patch to have all the bugs fixed, see README for details.
202
203Of course it's possible your operating system distributor has patched
204all of these problems and you have nothing to worry about.
0b836ff8 205
5f5fe73e
AMH
206Do you want to remove gif support? [Y/n]
207EOFF
208 my $resp = <STDIN>;
209 chomp($resp);
210 if ($resp ne "n") {
211 delete $formats{'gif'};
212 delete $formats{'ungif'};
09fd3468 213 return;
5f5fe73e
AMH
214 }
215 }
09fd3468
AMH
216
217 for my $frm (qw(gif ungif)) {
218 checkformat($frm) if ($MANUAL and $formats{$frm});
219 }
220
02d1d628
AMH
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
4dce694d 242
e11d297f
TC
243 push @defines, [ IM_GIFMAJOR, $major, "Parsed giflib version" ];
244 push @defines, [ IM_GIFMINOR, $minor ];
02d1d628
AMH
245}
246
247
248sub 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
263sub checkformat {
264 my $frm=shift;
37959076 265
07ea6c21 266 my $code = $formats{$frm}{'code'};
37959076 267 if ($code && !$noprobe) {
07ea6c21
TC
268 return 1 if $code->($formats{$frm}, $frm);
269 }
270
02d1d628
AMH
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) {
f8e9bc07 281 push(@i, $ip) if $incchk->($ip,$frm);
02d1d628
AMH
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
02d1d628
AMH
291sub pathcheck {
292 if ($VERBOSE) {
293 print "pathcheck\n";
294 print " Include paths:\n";
295 for (@incs) { print $_,"\n"; }
296 }
3a6bb91b 297 @incs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
02d1d628
AMH
298
299 if ($VERBOSE) {
300 print "\nLibrary paths:\n";
301 for (@incs) { print $_,"\n"; }
302 }
3a6bb91b 303 @libs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
02d1d628
AMH
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
320sub init {
321
322 @definc{'/usr/include'}=();
37959076
TC
323 @incs=(split(/\Q$Config{path_sep}/, $INCPATH),
324 map { split /\Q$Config{path_sep}/} @incpaths );
2646b26c 325 if ($Config{locincpth}) {
6552acfe 326 push @incs, grep -d, split ' ', $Config{locincpth};
2646b26c 327 }
88a763e2
TC
328 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
329 push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
2646b26c 330 }
6552acfe 331 push @incs, grep -d,
2646b26c
TC
332 qw(/sw/include
333 /usr/include/freetype2
334 /usr/local/include/freetype2
335 /usr/local/include/freetype1/freetype
37959076 336 /usr/include /usr/local/include /usr/include/freetype
2646b26c
TC
337 /usr/local/include/freetype);
338
37959076
TC
339 @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
340 map { split /\Q$Config{path_sep}/} @libpaths );
2646b26c 341 if ($Config{loclibpth}) {
6552acfe 342 push @libs, grep -d, split ' ', $Config{loclibpth};
2646b26c 343 }
6552acfe 344 push @libs, grep -d, qw(/sw/lib), split(/ /, $Config{'libpth'});
2646b26c 345 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
88a763e2
TC
346 push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
347 }
faa9b3e7
TC
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 }
02d1d628 352
2646b26c
TC
353 my $lext=$Config{'so'}; # Get extensions of libraries
354 my $aext=$Config{'_a'};
355
02d1d628
AMH
356 $formats{'jpeg'}={
357 order=>'21',
358 def=>'HAVE_LIBJPEG',
f8e9bc07 359 inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
50ee6f9c 360 libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
02d1d628
AMH
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',
f8e9bc07 371 inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
76c8a0a4 372 libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
02d1d628
AMH
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',
f8e9bc07 383 inccheck=>sub { -e catfile($_[0], 'png.h') },
88a763e2 384 libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
02d1d628
AMH
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
07ea6c21
TC
390 is recommended by the w3c, you need libpng to use these formats},
391 code => \&png_probe,
02d1d628
AMH
392 };
393
394 $formats{'gif'}={
395 order=>'20',
396 def=>'HAVE_LIBGIF',
f8e9bc07 397 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 398 libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
02d1d628
AMH
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',
f8e9bc07 412 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 413 libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
02d1d628
AMH
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',
f8e9bc07 426 inccheck=>sub { -e catfile($_[0], 't1lib.h') },
faa9b3e7 427 libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
02d1d628
AMH
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
f8e9bc07
TC
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{
447Truetype fonts are scalable fonts. They can include
448kerning and hinting information and generally yield good
449visual quality esp on low resultions. The freetype library is
450used to rasterize for us. The only drawback is that there
451are alot of badly designed fonts out there.}
02d1d628 452 };
faa9b3e7
TC
453 $formats{'w32'} = {
454 order=>40,
455 def=>'HAVE_WIN32',
f8e9bc07 456 inccheck=>sub { -e catfile($_[0], 'windows.h') },
faa9b3e7
TC
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
462Uses the Win32 GDI for rendering text.
463
464This currently only works on under normal Win32 and cygwin.
465DOCS
466 };
467 $formats{'freetype2'} = {
468 order=>'29',
469 def=>'HAVE_FT2',
f8e9bc07 470 inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
faa9b3e7
TC
471 libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
472 libfiles=>'-lfreetype',
473 objfiles=>'freetyp2.o',
07ea6c21 474 docs=><<DOCS,
faa9b3e7 475Freetype 2 supports both Truetype and Type 1 fonts, both of which are
f8e9bc07 476scalable. It also supports a variety of other fonts.
faa9b3e7 477DOCS
07ea6c21 478 code => \&freetype2_probe,
faa9b3e7 479 };
02d1d628
AMH
480 # Make fix indent
481 for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/ /mg; }
482}
483
484
485
486sub gen {
487 my $V = $ENV{$_[0]};
488 defined($V) ? $V : "";
489}
490
491
492# Get information from environment variables
493
494sub 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"; }
e11d297f
TC
517 else {
518 push @defines, [ IMAGER_LOG => 1, "Logging system" ];
519 }
02d1d628
AMH
520
521 if ($DEBUG_MALLOC) {
e11d297f 522 push @defines, [ IMAGER_DEBUG_MALLOC => 1, "Use Imager's DEBUG malloc()" ];
02d1d628
AMH
523 print "Malloc debugging enabled\n";
524 }
525
526}
07ea6c21 527
e11d297f
TC
528sub 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
539EOS
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
07ea6c21
TC
550# probes for freetype2 by trying to run freetype-config
551sub 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
572sub 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
601sub catfile {
602 return File::Spec->catfile(@_);
603}
604
605sub 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}
37959076
TC
615
616sub usage {
617 print STDERR <<EOS;
618Usage: $0 [--enable feature1,feature2,...] [--incpath ...] [--libpath ...]
619 $0 [--disable feature1,feature2,...] [--incpath ...] [--libpath ...]
620 $0 --help
621Possible feature names are:
622 png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
623EOS
624 exit 1;
625
626}