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