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