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