- fix/simplify META.yml generation - we now generate META.yml at
[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
85
37959076
TC
86if (exists $ENV{IM_ENABLE}) {
87 my %en = map { $_, 1 } split ' ', $ENV{IM_ENABLE};
88 for my $key (keys %formats) {
89 delete $formats{$key} unless $en{$key};
90 }
91}
92if (@enable) {
93 my %en = map { $_ => 1 } map { split /,/ } @enable;
94 for my $key (keys %formats) {
95 delete $formats{$key} unless $en{$key};
96 }
97}
98elsif (@disable) {
99 delete @formats{map { split /,/ } @disable};
100}
101
02d1d628
AMH
102# Pick what libraries are used
103if ($MANUAL) {
104 manual();
105} else {
106 automatic();
02d1d628
AMH
107}
108
109# Make sure there isn't a clash between the gif libraries.
110gifcheck();
111
07ea6c21 112my $lib_cflags = '';
80c15fc7
TC
113my $F_LIBS = '';
114my $F_OBJECT = '';
714cf158 115for my $frmkey (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
e11d297f
TC
116 my $frm = $formats{$frmkey};
117 push @defines, [ $frm->{def}, 1, "$frmkey available" ];
02d1d628
AMH
118 $F_LIBS .= ' ' .$frm->{libfiles};
119 $F_OBJECT .= ' ' .$frm->{objfiles};
714cf158
TC
120 if ($frm->{cflags}) {
121 $lib_cflags .= ' ' .$frm->{cflags};
122 ++$definc{$_} for map { /^-I(.*)$/ ? ($1) : () }
123 grep /^-I./, split ' ', $frm->{cflags};
124 }
02d1d628 125}
714cf158 126
f7450478
TC
127unless ($noexif) {
128 print "EXIF support enabled\n";
129 push @defines, [ 'IMEXIF_ENABLE', 1, "Enable experimental EXIF support" ];
130 $F_OBJECT .= ' imexif.o';
131}
02d1d628 132
714cf158
TC
133my $F_INC = join ' ', map "-I$_", map / / ? qq{"$_"} : $_,
134 grep !$definc{$_}, @incs;
135$F_LIBS = join(' ',map "-L$_", map / / ? qq{"$_"} : $_,
136 grep !$deflib{$_}++, @libs) . $F_LIBS;
02d1d628 137
714cf158
TC
138my $OSLIBS = '';
139my $OSDEF = "-DOS_$^O";
02d1d628
AMH
140
141if ($^O eq 'hpux') { $OSLIBS .= ' -ldld'; }
142if (defined $Config{'d_dlsymun'}) { $OSDEF .= ' -DDLSYMUN'; }
143
714cf158
TC
144my @objs = qw(Imager.o draw.o polygon.o image.o io.o iolayer.o
145 log.o gaussian.o conv.o pnm.o raw.o feat.o font.o
146 filters.o dynaload.o stackmach.o datatypes.o
147 regmach.o trans2.o quant.o error.o convert.o
148 map.o tags.o palimg.o maskimg.o img16.o rotate.o
149 bmp.o tga.o rgb.o color.o fills.o imgdouble.o limits.o hlines.o
150 imext.o);
92bda632
TC
151
152$Recommends{Imager} =
153 { 'Parse::RecDescent' => 0 };
02d1d628 154
714cf158
TC
155my %opts=(
156 'NAME' => 'Imager',
157 'VERSION_FROM' => 'Imager.pm',
158 'LIBS' => "$LFLAGS -lm $OSLIBS $F_LIBS",
159 'DEFINE' => "$OSDEF $CFLAGS",
160 'INC' => "$lib_cflags $DFLAGS $F_INC",
161 'OBJECT' => join(' ', @objs, $F_OBJECT),
162 clean => { FILES=>'testout meta.tmp' },
163 PM => gen_PM(),
164 );
02d1d628 165
29316bdb
TC
166if ($ExtUtils::MakeMaker::VERSION > 6.06) {
167 $opts{AUTHOR} = 'Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson';
ca508100
TC
168 $opts{ABSTRACT} = 'Perl extension for Generating 24 bit Images';
169}
135d30e3
TC
170if ($ExtUtils::MakeMaker::VERSION > 6.10) {
171 $opts{NO_META} = 1;
172}
ca508100 173
e11d297f
TC
174make_imconfig(\@defines);
175
02d1d628
AMH
176if ($VERBOSE) { print Dumper(\%opts); }
177mkdir('testout',0777); # since we cannot include it in the archive.
135d30e3
TC
178
179make_metafile(\%opts);
180
02d1d628 181WriteMakefile(%opts);
4dce694d 182
02d1d628
AMH
183exit;
184
185
186sub MY::postamble {
5a7e62b6
TC
187 my $self = shift;
188 my $perl = $self->{PERLRUN} ? '$(PERLRUN)' : '$(PERL)';
02d1d628 189'
faa9b3e7 190dyntest.$(MYEXTLIB) : dynfilt/Makefile
02d1d628
AMH
191 cd dynfilt && $(MAKE) $(PASTHRU)
192
193lib/Imager/Regops.pm : regmach.h regops.perl
194 $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
e11d297f 195
92bda632 196imconfig.h : Makefile.PL
e11d297f
TC
197 $(ECHO) "imconfig.h out-of-date with respect to $?"
198 $(PERLRUN) Makefile.PL
199 $(ECHO) "==> Your Makefile has been rebuilt - re-run your make command <=="
5a7e62b6
TC
200'.qq!
201lib/Imager/APIRef.pm : \$(C_FILES) apidocs.perl
202 $perl apidocs.perl lib/Imager/APIRef.pm
92bda632 203
5a7e62b6 204!;
02d1d628 205
55932d2a
TC
206}
207
02d1d628
AMH
208# manual configuration of helper libraries
209
210sub manual {
211 print <<EOF;
212
213 Please answer the following questions about
214 which formats are avaliable on your computer
215
216press <return> to continue
217EOF
218
219 <STDIN>; # eat one return
220
2646b26c 221 for my $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
02d1d628
AMH
222 SWX:
223 if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
224 print "Enable $frm support: ";
714cf158 225 my $gz = <STDIN>;
02d1d628
AMH
226 chomp($gz);
227 if ($gz =~ m/^(y|yes|n|no)/i) {
228 $gz=substr(lc($gz),0,1);
229 if ($gz eq 'n') {
230 delete $formats{$frm};
231 }
232 } else { goto SWX; }
233 }
234}
235
236
237# automatic configuration of helper libraries
238
239sub automatic {
714cf158
TC
240 print "Automatic probing:\n" if $VERBOSE;
241 for my $frm (sort { $formats{$a}{order} <=> $formats{$b}{order} } keys %formats) {
02d1d628
AMH
242 delete $formats{$frm} if !checkformat($frm);
243 }
244}
245
246
247sub gifcheck {
5f5fe73e 248 if ($formats{'gif'} and $formats{'ungif'}) {
02d1d628
AMH
249 print "ungif and gif can not coexist - removing ungif support\n";
250 delete $formats{'ungif'};
251 }
5f5fe73e
AMH
252
253 RETR:
f4dbac50 254 if (($formats{'gif'} or $formats{'ungif'}) && !$ENV{IM_SUPPRESS_PROMPT}) {
5f5fe73e
AMH
255 print <<EOFF;
256
257You have libgif or libungif installed. They are both known to have
258bugs. Imager can crash or display other strange behaviour after
259reading or writing gif images. Some of the gif tests can even fail
260since they stress some parts of the buggy code.
261
f1967c11
TC
262libungif 4.1.2 and later is safe. giflib 4.1.3 needs at least one
263patch to have all the bugs fixed, see README for details.
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
270 my $resp = <STDIN>;
271 chomp($resp);
272 if ($resp ne "n") {
273 delete $formats{'gif'};
274 delete $formats{'ungif'};
09fd3468 275 return;
5f5fe73e
AMH
276 }
277 }
09fd3468
AMH
278
279 for my $frm (qw(gif ungif)) {
280 checkformat($frm) if ($MANUAL and $formats{$frm});
281 }
282
02d1d628
AMH
283 my @dirs;
284 for my $frm (grep $formats{$_}, qw(gif ungif)) {
285 push(@dirs, @{$formats{$frm}{incdir}}) if $formats{$frm}{incdir};
286 }
287 my $minor = 0;
288 my $major = 0;
289 FILES: for my $dir (@dirs) {
290 my $h = "$dir/gif_lib.h";
291 open H, "< $h" or next;
292 while (<H>) {
293 if (/GIF_LIB_VERSION\s+"\s*version\s*(\d+)\.(\d+)/i) {
294 $major = $1;
295 $minor = $2;
296 close H;
297 last FILES;
298 }
299 }
300 close H;
301 }
302
303 # we need the version in a #ifdefable form
4dce694d 304
714cf158
TC
305 push @defines, [ IM_GIFMAJOR => $major, "Parsed giflib version" ];
306 push @defines, [ IM_GIFMINOR => $minor ];
02d1d628
AMH
307}
308
309
310sub gd {
311 my($path,$chk)=@_;
312
313# print "checking path $path\n";
314 if ( !opendir(DH,$path) ) {
315 warn "Cannot open dir $path: $!\n";
316 return;
317 }
318 my @l=grep { $chk->($_) } readdir(DH);
319 # print @l;
320 close(DH);
321 return map $path, @l;
322}
323
324
325sub checkformat {
326 my $frm=shift;
714cf158
TC
327
328 print " checkformat($frm)\n" if $VERBOSE;
37959076 329
07ea6c21 330 my $code = $formats{$frm}{'code'};
37959076 331 if ($code && !$noprobe) {
714cf158 332 print " Calling probe function\n" if $VERBOSE;
07ea6c21
TC
333 return 1 if $code->($formats{$frm}, $frm);
334 }
335
02d1d628
AMH
336 my $libchk=$formats{$frm}{'libcheck'};
337 my $incchk=$formats{$frm}{'inccheck'};
338
339 my @l;
340 for my $lp (@libs) {
341 push(@l, gd($lp,$libchk));
342 }
343
344 my @i;
345 for my $ip (@incs) {
f8e9bc07 346 push(@i, $ip) if $incchk->($ip,$frm);
02d1d628
AMH
347 }
348
349 printf("%10s: includes %s - libraries %s\n",$frm,(@i?'found':'not found'),(@l?'found':'not found'));
350 $formats{$frm}{incdir} = \@i;
351 $formats{$frm}{libdir} = \@l;
352 return scalar(@i && @l);
353}
354
355
02d1d628
AMH
356sub pathcheck {
357 if ($VERBOSE) {
358 print "pathcheck\n";
359 print " Include paths:\n";
360 for (@incs) { print $_,"\n"; }
361 }
3a6bb91b 362 @incs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @incs;
02d1d628
AMH
363
364 if ($VERBOSE) {
365 print "\nLibrary paths:\n";
855c5808 366 for (@libs) { print $_,"\n"; }
02d1d628 367 }
3a6bb91b 368 @libs=grep { -d $_ && -r _ && -x _ or ( print(" $_ doesnt exist or is unaccessible - removed.\n"),0) } @libs;
02d1d628
AMH
369 print "\ndone.\n";
370}
371
372
373# Format data initialization
374
375# format definition is:
376# defines needed
377# default include path
378# files needed for include (boolean perl code)
379# default lib path
380# libs needed
381# files needed for link (boolean perl code)
382# object files needed for the format
383
384
385sub init {
386
714cf158
TC
387 my @definc = qw(/usr/include);
388 @definc{@definc}=(1) x @definc;
37959076
TC
389 @incs=(split(/\Q$Config{path_sep}/, $INCPATH),
390 map { split /\Q$Config{path_sep}/} @incpaths );
2646b26c 391 if ($Config{locincpth}) {
6552acfe 392 push @incs, grep -d, split ' ', $Config{locincpth};
2646b26c 393 }
88a763e2
TC
394 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
395 push(@incs, split /;/, $ENV{INCLUDE}) if exists $ENV{INCLUDE};
2646b26c 396 }
6552acfe 397 push @incs, grep -d,
2646b26c
TC
398 qw(/sw/include
399 /usr/include/freetype2
400 /usr/local/include/freetype2
401 /usr/local/include/freetype1/freetype
37959076 402 /usr/include /usr/local/include /usr/include/freetype
2646b26c 403 /usr/local/include/freetype);
714cf158
TC
404 if ($Config{ccflags}) {
405 my @hidden = map { /^-I(.*)$/ ? ($1) : () } split ' ', $Config{ccflags};
406 push @incs, @hidden;
407 @definc{@hidden} = (1) x @hidden;
408 }
2646b26c 409
37959076
TC
410 @libs= ( split(/\Q$Config{path_sep}/,$LIBPATH),
411 map { split /\Q$Config{path_sep}/} @libpaths );
2646b26c 412 if ($Config{loclibpth}) {
6552acfe 413 push @libs, grep -d, split ' ', $Config{loclibpth};
2646b26c 414 }
714cf158 415
6552acfe 416 push @libs, grep -d, qw(/sw/lib), split(/ /, $Config{'libpth'});
714cf158 417 push @libs, grep -d, split / /, $Config{libspath} if $Config{libspath};
2646b26c 418 if ($^O =~ /win32/i && $Config{cc} =~ /\bcl\b/i) {
88a763e2
TC
419 push(@libs, split /;/, $ENV{LIB}) if exists $ENV{LIB};
420 }
faa9b3e7
TC
421 if ($^O eq 'cygwin') {
422 push(@libs, '/usr/lib/w32api') if -d '/usr/lib/w32api';
274cd32b 423 push(@incs, '/usr/include/w32api') if -d '/usr/include/w32api';
faa9b3e7 424 }
714cf158
TC
425 if ($Config{ldflags}) {
426 # some builds of perl put -Ldir into ldflags without putting it in
427 # loclibpth, let's extract them
428 my @hidden = grep -d, map { /^-L(.*)$/ ? ($1) : () }
429 split ' ', $Config{ldflags};
430 push @libs, @hidden;
431 # don't mark them as seen - EU::MM will remove any libraries
432 # it can't find and it doesn't look for -L in ldflags
433 #@deflib{@hidden} = @hidden;
434 }
2646b26c 435
02d1d628
AMH
436 $formats{'jpeg'}={
437 order=>'21',
438 def=>'HAVE_LIBJPEG',
f8e9bc07 439 inccheck=>sub { -e catfile($_[0], 'jpeglib.h') },
50ee6f9c 440 libcheck=>sub { $_[0] eq "libjpeg$aext" or $_ eq "libjpeg.$lext" },
02d1d628
AMH
441 libfiles=>'-ljpeg',
442 objfiles=>'jpeg.o',
443 docs=>q{
444 In order to use jpeg with this module you need to have libjpeg
445 installed on your computer}
446 };
447
448 $formats{'tiff'}={
449 order=>'23',
450 def=>'HAVE_LIBTIFF',
f8e9bc07 451 inccheck=>sub { -e catfile($_[0], 'tiffio.h') },
76c8a0a4 452 libcheck=>sub { $_[0] eq "libtiff$aext" or $_ eq "libtiff.$lext" },
02d1d628
AMH
453 libfiles=>'-ltiff',
454 objfiles=>'tiff.o',
455 docs=>q{
456 In order to use tiff with this module you need to have libtiff
457 installed on your computer}
458 };
459
460 $formats{'png'}={
461 order=>'22',
462 def=>'HAVE_LIBPNG',
f8e9bc07 463 inccheck=>sub { -e catfile($_[0], 'png.h') },
88a763e2 464 libcheck=>sub { $_[0] eq "libpng$aext" or $_[0] eq "libpng.$lext" },
02d1d628
AMH
465 libfiles=>'-lpng -lz',
466 objfiles=>'png.o',
467 docs=>q{
468 Png stands for Portable Network Graphics and is intended as
469 a replacement for gif on the web. It is patent free and
07ea6c21
TC
470 is recommended by the w3c, you need libpng to use these formats},
471 code => \&png_probe,
02d1d628
AMH
472 };
473
474 $formats{'gif'}={
475 order=>'20',
476 def=>'HAVE_LIBGIF',
f8e9bc07 477 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 478 libcheck=>sub { $_[0] eq "libgif$aext" or $_[0] eq "libgif.$lext" },
02d1d628
AMH
479 libfiles=>'-lgif',
480 objfiles=>'gif.o',
481 docs=>q{
482 gif is the de facto standard for webgraphics at the moment,
483 it does have some patent problems. If you have giflib and
484 are not in violation with the unisys patent you should use
485 this instead of the 'ungif' option. Note that they cannot
486 be in use at the same time}
487 };
488
489 $formats{'ungif'}={
490 order=>'21',
491 def=>'HAVE_LIBGIF',
f8e9bc07 492 inccheck=>sub { -e catfile($_[0], 'gif_lib.h') },
e02451e0 493 libcheck=>sub { $_[0] eq "libungif$aext" or $_[0] eq "libungif.$lext" },
02d1d628
AMH
494 libfiles=>'-lungif',
495 objfiles=>'gif.o',
496 docs=>q{
497 gif is the de facto standard for webgraphics at the moment,
498 it does have some patent problems. If you have libungif and
499 want to create images free from LZW patented compression you
500 should use this option instead of the 'gif' option}
501 };
502
503 $formats{'T1-fonts'}={
504 order=>'30',
505 def=>'HAVE_LIBT1',
f8e9bc07 506 inccheck=>sub { -e catfile($_[0], 't1lib.h') },
faa9b3e7 507 libcheck=>sub { $_[0] eq "libt1$aext" or $_[0] eq "libt1.$lext" },
02d1d628
AMH
508 libfiles=>'-lt1',
509 objfiles=>'',
510 docs=>q{
511 postscript t1 fonts are scalable fonts. They can include
512 ligatures and kerning information and generally yield good
513 visual quality. We depend on libt1 to rasterize the fonts
514 for use in images.}
515 };
516
f8e9bc07
TC
517 $formats{'TT-fonts'}=
518 {
519 order=>'31',
520 def=>'HAVE_LIBTT',
521 inccheck=>sub { -e catfile($_[0], 'freetype.h')
522 && !-e catfile($_[0], 'fterrors.h') },
523 libcheck=>sub { $_[0] eq "libttf$aext" or $_[0] eq "libttf.$lext" },
524 libfiles=>'-lttf',
525 objfiles=>'',
714cf158 526 code => \&freetype1_probe,
f8e9bc07
TC
527 docs=>q{
528Truetype fonts are scalable fonts. They can include
529kerning and hinting information and generally yield good
530visual quality esp on low resultions. The freetype library is
531used to rasterize for us. The only drawback is that there
532are alot of badly designed fonts out there.}
02d1d628 533 };
faa9b3e7
TC
534 $formats{'w32'} = {
535 order=>40,
536 def=>'HAVE_WIN32',
f8e9bc07 537 inccheck=>sub { -e catfile($_[0], 'windows.h') },
faa9b3e7
TC
538 libcheck=>sub { lc $_[0] eq 'gdi32.lib'
539 || lc $_[0] eq 'libgdi32.a' },
540 libfiles=>$^O eq 'cygwin' ? '-lgdi32' : '',
541 objfiles=>'win32.o',
542 docs => <<DOCS
543Uses the Win32 GDI for rendering text.
544
545This currently only works on under normal Win32 and cygwin.
546DOCS
547 };
548 $formats{'freetype2'} = {
549 order=>'29',
550 def=>'HAVE_FT2',
f8e9bc07 551 inccheck=>sub { -e catfile($_[0], 'ft2build.h') },
faa9b3e7
TC
552 libcheck=>sub { $_[0] eq "libfreetype$aext" or $_[0] eq "libfreetype.$lext" },
553 libfiles=>'-lfreetype',
554 objfiles=>'freetyp2.o',
07ea6c21 555 docs=><<DOCS,
faa9b3e7 556Freetype 2 supports both Truetype and Type 1 fonts, both of which are
f8e9bc07 557scalable. It also supports a variety of other fonts.
faa9b3e7 558DOCS
07ea6c21 559 code => \&freetype2_probe,
faa9b3e7 560 };
f7450478 561
02d1d628
AMH
562 # Make fix indent
563 for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/ /mg; }
564}
565
566
567
568sub gen {
569 my $V = $ENV{$_[0]};
570 defined($V) ? $V : "";
571}
572
573
574# Get information from environment variables
575
576sub getenv {
577
578 ($VERBOSE,
579 $INCPATH,
580 $LIBPATH,
581 $NOLOG,
582 $DEBUG_MALLOC,
583 $MANUAL,
584 $CFLAGS,
585 $LFLAGS,
586 $DFLAGS) = map { gen $_ } qw(IM_VERBOSE
587 IM_INCPATH
588 IM_LIBPATH
589 IM_NOLOG
590 IM_DEBUG_MALLOC
591 IM_MANUAL
592 IM_CFLAGS
593 IM_LFLAGS
594 IM_DFLAGS);
595
02d1d628 596}
07ea6c21 597
e11d297f
TC
598sub make_imconfig {
599 my ($defines) = @_;
600
601 open CONFIG, "> imconfig.h"
602 or die "Cannot create imconfig.h: $!\n";
603 print CONFIG <<EOS;
604/* This file is automatically generated by Makefile.PL.
605 Don't edit this file, since any changes will be lost */
606
607#ifndef IMAGER_IMCONFIG_H
608#define IMAGER_IMCONFIG_H
609EOS
610 for my $define (@$defines) {
611 if ($define->[2]) {
612 print CONFIG "\n/*\n $define->[2]\n*/\n\n";
613 }
614 print CONFIG "#define $define->[0] $define->[1]\n";
615 }
616 print CONFIG "\n#endif\n";
617 close CONFIG;
618}
619
dbc33d8a
TC
620# use pkg-config to probe for libraries
621# works around the junk that pkg-config dumps on FreeBSD
622sub _pkg_probe {
623 my ($pkg) = @_;
624
625 is_exe('pkg-config') or return;
626
627 my $redir = $^O eq 'MSWin32' ? '' : '2>/dev/null';
628
629 !system("pkg-config $pkg --exists $redir");
630}
631
714cf158
TC
632# probes for freetype1 by scanning @incs for the includes and
633# @libs for the libs. This is done separately because freetype's headers
634# are stored in a freetype or freetype1 directory under PREFIX/include.
635#
636# we could find the files with the existing mechanism, but it won't set
637# -I flags correctly.
638#
639# This could be extended to freetype2 too, but freetype-config catches
640# that
641sub freetype1_probe {
642 my ($frm, $frmkey) = @_;
643
644 my $found_inc;
645 INCS:
646 for my $inc (@incs) {
647 for my $subdir (qw/freetype freetype1/) {
648 my $path = File::Spec->catfile($inc, $subdir, 'freetype.h');
649 -e $path or next;
650 $path = File::Spec->catfile($inc, $subdir, 'fterrors.h');
651 -e $path and next;
652
653 $found_inc = File::Spec->catdir($inc, $subdir);
654 last INCS;
655 }
656 }
657
658 my $found_lib;
659 LIBS:
660 for my $lib (@libs) {
661 my $a_path = File::Spec->catfile($lib, "libttf$aext");
662 my $l_path = File::Spec->catfile($lib, "libttf.$lext");
663 if (-e $a_path || -e $l_path) {
664 $found_lib = $lib;
665 last LIBS;
666 }
667 }
668
89d37182 669 return unless $found_inc && $found_lib;
714cf158
TC
670 printf("%10s: includes %s - libraries %s\n", $frmkey,
671 ($found_inc ? 'found' : 'not found'),
672 ($found_lib ? 'found' : 'not found'));
714cf158
TC
673
674 $frm->{cflags} = "-I$found_inc";
675 $frm->{libfiles} = "-lttf";
676
677 return 1;
678}
679
07ea6c21
TC
680# probes for freetype2 by trying to run freetype-config
681sub freetype2_probe {
682 my ($frm, $frmkey) = @_;
683
684 is_exe('freetype-config') or return;
685
686 my $cflags = `freetype-config --cflags`
687 and !$? or return;
688 chomp $cflags;
88e16587 689
07ea6c21
TC
690 my $lflags = `freetype-config --libs`
691 and !$? or return;
692 chomp $lflags;
88e16587
TC
693
694 # before 2.1.5 freetype-config --cflags could output
695 # the -I options in the wrong order, causing a conflict with
696 # freetype1.x installed with the same --prefix
697 #
698 # can happen iff:
699 # - both -Iprefix/include and -Iprefix/include/freetype2 are in cflags
700 # in that order
701 # - freetype 1.x headers are in prefix/include/freetype
702 my @incdirs = map substr($_, 2), grep /^-I/, split ' ', $cflags;
703 if (@incdirs == 2
704 && $incdirs[1] eq "$incdirs[0]/freetype2"
705 && -e "$incdirs[0]/freetype/freetype.h"
706 && -e "$incdirs[0]/freetype/fterrid.h") {
707 print "** freetype-config provided -I options out of order, correcting\n"
708 if $VERBOSE;
709 $cflags = join(' ', grep(!/-I/, split ' ', $cflags),
710 map "-I$_", reverse @incdirs);
711 }
712 $frm->{cflags} = $cflags;
07ea6c21
TC
713 $frm->{libfiles} = $lflags;
714
715 printf "%10s: configured via freetype-config\n", $frmkey;
716
717 return 1;
718}
719
720# probes for libpng via pkg-config
721sub png_probe {
722 my ($frm, $frmkey) = @_;
723
724 is_exe('pkg-config') or return;
725
07ea6c21
TC
726 my $config;
727 for my $check_conf (qw(libpng libpng12 libpng10)) {
dbc33d8a 728 if (_pkg_probe($check_conf)) {
07ea6c21
TC
729 $config = $check_conf;
730 last;
731 }
732 }
733 $config or return;
734
dbc33d8a
TC
735 my $cflags = `pkg-config $config --cflags`
736 and !$? or return;
737
07ea6c21
TC
738 my $lflags = `pkg-config $config --libs`
739 and !$? or return;
740
741 chomp $cflags;
742 chomp $lflags;
743 $frm->{cflags} = $cflags;
744 $frm->{libfiles} = $lflags;
745
746 printf "%10s: configured via `pkg-config $config ...`\n", $frmkey;
747
748 return 1;
749}
750
751sub catfile {
752 return File::Spec->catfile(@_);
753}
754
755sub is_exe {
756 my ($name) = @_;
757
758 for my $dir (File::Spec->path) {
759 -x catfile($dir, "$name$Config{_exe}")
760 and return 1;
761 }
762
763 return;
764}
37959076
TC
765
766sub usage {
767 print STDERR <<EOS;
274cd32b
TC
768Usage: $0 [--enable feature1,feature2,...] [other options]
769 $0 [--disable feature1,feature2,...] [other options]
37959076
TC
770 $0 --help
771Possible feature names are:
772 png gif ungif jpeg tiff T1-fonts TT-fonts freetype2
274cd32b
TC
773Other options:
774 --verbose | -v
775 Verbose library probing (or set IM_VERBOSE in the environment)
776 --nolog
777 Disable logging (or set IM_NOLOG in the environment)
778 --incpath dir
779 Add to the include search path
780 --libpath dir
781 Add to the library search path
782 --noprobe
783 Don't use pkg-config or freetype2-config to probe for freetype2 and libpng
37959076
TC
784EOS
785 exit 1;
786
787}
92bda632
TC
788
789# generate the PM MM argument
790# I'd prefer to modify the public version, but there doesn't seem to be
791# a public API to do that
792sub gen_PM {
793 my %pm;
794 my $instbase = '$(INST_LIBDIR)';
795
796 # first the basics, .pm and .pod files
797 $pm{"Imager.pm"} = "$instbase/Imager.pm";
798
799 my $mani = maniread();
800
801 for my $filename (keys %$mani) {
802 if ($filename =~ m!^lib/! && $filename =~ /\.(pm|pod)$/) {
803 (my $work = $filename) =~ s/^lib//;
804 $pm{$filename} = $instbase . $work;
805 }
806 }
807
808 # need the typemap
809 $pm{typemap} = $instbase . '/Imager/typemap';
810
811 # and the core headers
812 for my $filename (keys %$mani) {
813 if ($filename =~ /^\w+\.h$/) {
814 $pm{$filename} = $instbase . '/Imager/include/' . $filename;
815 }
816 }
817
818 # and the generated header
819 $pm{"imconfig.h"} = $instbase . '/Imager/include/imconfig.h';
820
821 \%pm;
822}
135d30e3
TC
823
824sub make_metafile {
825 my ($opts) = @_;
826
827 # extract the version
828 my $version = MM->parse_version($opts->{VERSION_FROM})
829 or die "Could not extract version number from $opts->{VERSION_FROM}\n";
830
831 my $meta = <<YAML;
832--- #YAML:1.0
833name: $opts->{NAME}
834version: $version
835version_from: $opts->{VERSION_FROM}
836author: $opts->{AUTHOR}
837abstract: $opts->{ABSTRACT}
838installdirs: site
839YAML
840 if (keys %{$Recommends{$opts->{NAME}}}) {
841 $meta .= "recommends:\n";
842 while (my ($module, $version) = each %{$Recommends{$opts->{NAME}}}) {
843 $meta .= " $module: $version\n";
844 }
845 }
846 $meta .= <<YAML;
847license: perl
848dynamic_config: 1
849distribution_type: module
850generated_by: $opts->{NAME} version $version
851YAML
852 my $save_meta;
853 if (open META, "< META.yml") {
854 my $old_meta = do { local $/; <META> };
855 close META;
856
857 $save_meta = $old_meta ne $meta;
858 }
859 else {
860 ++$save_meta;
861 }
862 if ($save_meta) {
863 print "Updating META.yml\n";
864 open META, "> META.yml" or die "Cannot create META.yml: $!";
865 print META $meta;
866 close META;
867 }
868}