Initial revision
[imager.git] / Makefile.PL
1
2 use ExtUtils::MakeMaker;
3 use Cwd;
4 use Config;
5
6 $lext=$Config{'so'};   # Get extensions of libraries
7
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
23
24
25 getenv();     # get environment variables
26 init();       # initialize global data
27 pathcheck();  # Check if directories exist
28
29 # Pick what libraries are used
30 if ($MANUAL) {
31   manual();
32 } else {
33   automatic();
34   if (exists $ENV{IM_ENABLE}) {
35     my %en = map { $_, 1 } split ' ', $ENV{IM_ENABLE};
36     for my $key (keys %formats) {
37       delete $formats{$key} unless $en{$key};
38     }
39   }
40 }
41
42 # Make sure there isn't a clash between the gif libraries.
43 gifcheck();
44
45 for $frm(values %formats) {
46   $F_DEFINE .= ' -D'.$frm->{def};
47   $F_LIBS   .= ' '  .$frm->{libfiles};
48   $F_OBJECT .= ' '  .$frm->{objfiles};
49 }
50
51 $F_INC  = join(" ",map { (exists $definc{$_})?'':'-I'.$_ } @incs);
52 $F_LIBS = join(" ",map { '-L'.$_ } @libs).' '.$F_LIBS;
53
54 $OSLIBS = '';
55 $OSDEF  = "-DOS_$^O";
56
57 if ($^O eq 'hpux')                { $OSLIBS .= ' -ldld'; }
58 if (defined $Config{'d_dlsymun'}) { $OSDEF  .= ' -DDLSYMUN'; }
59
60 @objs = qw(Imager.o draw.o image.o io.o iolayer.o log.o
61            gaussian.o conv.o pnm.o raw.o feat.o font.o
62            filters.o dynaload.o stackmach.o datatypes.o
63            regmach.o trans2.o quant.o);
64
65 %opts=(
66        'NAME'         => 'Imager',
67        'VERSION_FROM' => 'Imager.pm',
68        'LIBS'         => "$LFLAGS -lm $OSLIBS $F_LIBS",
69        'DEFINE'       => "$F_DEFINE $EXTDEF $OSDEF $CFLAGS",
70        'INC'          => "$DFLAGS $F_INC",
71        'OBJECT'       => join(' ', @objs, $F_OBJECT)
72       );
73
74 if ($VERBOSE) { print Dumper(\%opts); }
75 mkdir('testout',0777); # since we cannot include it in the archive.
76 WriteMakefile(%opts);
77 exit;
78
79
80 sub MY::postamble {
81 '
82 dyntest.(MYEXTLIB) : dynfilt/Makefile
83         cd dynfilt && $(MAKE) $(PASTHRU)
84
85 lib/Imager/Regops.pm : regmach.h regops.perl
86         $(PERL) regops.perl regmach.h lib/Imager/Regops.pm
87 ';
88 }
89
90 # manual configuration of helper libraries
91
92 sub manual {
93   print <<EOF;
94
95       Please answer the following questions about
96       which formats are avaliable on your computer
97
98 press <return> to continue
99 EOF
100
101   <STDIN>; # eat one return
102
103   for $frm(sort { $formats{$b}{order} <=> $formats{$a}{order} } keys %formats) {
104   SWX:
105     if ($formats{$frm}{docs}) { print "\n",$formats{$frm}{docs},"\n\n"; }
106     print "Enable $frm support: ";
107     $gz=<STDIN>;
108     chomp($gz);
109     if ($gz =~ m/^(y|yes|n|no)/i) {
110       $gz=substr(lc($gz),0,1);
111       if ($gz eq 'n') {
112         delete $formats{$frm};
113       }
114     } else { goto SWX; }
115   }
116 }
117
118
119 # automatic configuration of helper libraries
120
121 sub automatic {
122   for $frm(keys %formats) {
123     delete $formats{$frm} if !checkformat($frm);        
124   }
125 }
126
127
128 sub gifcheck {
129   if ($formats{'gif'} and $formats{'ungif'}) { 
130     print "ungif and gif can not coexist - removing ungif support\n";
131     delete $formats{'ungif'};
132   }
133   my @dirs;
134   for my $frm (grep $formats{$_}, qw(gif ungif)) {
135     push(@dirs, @{$formats{$frm}{incdir}}) if $formats{$frm}{incdir};
136   }
137   my $minor = 0;
138   my $major = 0;
139   FILES: for my $dir (@dirs) {
140     my $h = "$dir/gif_lib.h";
141     open H, "< $h" or next;
142     while (<H>) {
143       if (/GIF_LIB_VERSION\s+"\s*version\s*(\d+)\.(\d+)/i) {
144         $major = $1;
145         $minor = $2;
146         close H;
147         last FILES;
148       }
149     }
150     close H;
151   }
152
153   # we need the version in a #ifdefable form
154   
155   $F_DEFINE .= "-DIM_GIFMAJOR=$major -DIM_GIFMINOR=$minor";
156 }
157
158
159 sub gd {
160   my($path,$chk)=@_;
161
162 #    print "checking path $path\n";
163   if ( !opendir(DH,$path) ) {
164     warn "Cannot open dir $path: $!\n";
165     return;
166   }
167   my @l=grep { $chk->($_) } readdir(DH);
168   #    print @l;
169   close(DH);
170   return map $path, @l;
171 }
172
173
174 sub checkformat {
175   my $frm=shift;
176   my $libchk=$formats{$frm}{'libcheck'};
177   my $incchk=$formats{$frm}{'inccheck'};
178
179   my @l;
180   for my $lp (@libs) {
181     push(@l, gd($lp,$libchk));
182   }
183
184   my @i;
185   for my $ip (@incs) {
186     push(@i, gd($ip,$incchk));
187   }
188
189   printf("%10s: includes %s - libraries %s\n",$frm,(@i?'found':'not found'),(@l?'found':'not found'));
190   $formats{$frm}{incdir} = \@i;
191   $formats{$frm}{libdir} = \@l;
192   return scalar(@i && @l);
193 }
194
195
196
197
198 sub pathcheck {
199   if ($VERBOSE) {
200     print "pathcheck\n";
201     print "  Include paths:\n";
202     for (@incs) { print $_,"\n"; }
203   }
204   @incs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed."),0) } @incs;
205
206   if ($VERBOSE) {
207     print "\nLibrary paths:\n";
208     for (@incs) { print $_,"\n"; }
209   }
210   @libs=grep { -d $_ && -r _ && -x _ or ( print("  $_ doesnt exist or is unaccessible - removed."),0) } @libs;
211   print "\ndone.\n";
212 }
213
214
215 # Format data initialization
216
217 # format definition is:
218 # defines needed
219 # default include path
220 # files needed for include (boolean perl code)
221 # default lib path
222 # libs needed
223 # files needed for link (boolean perl code)
224 # object files needed for the format
225
226
227 sub init {
228
229   @definc{'/usr/include'}=();
230   @incs=(qw(/usr/include /usr/local/include /usr/include/freetype /usr/local/include/freetype), split /:/, $INCPATH );
231   @libs=(split(/ /, $Config{'libpth'}), split(/:/, $LIBPATH) );
232
233   $formats{'jpeg'}={
234                     order=>'21',
235                     def=>'HAVE_LIBJPEG',
236                     inccheck=>sub { $_[0] eq 'jpeglib.h' },
237                     libcheck=>sub { $_[0] eq 'libjpeg.a' or $_ eq "libjpeg.$lext" },
238                     libfiles=>'-ljpeg',
239                     objfiles=>'jpeg.o',
240                     docs=>q{
241                             In order to use jpeg with this module you need to have libjpeg
242                             installed on your computer}
243                    };
244
245   $formats{'tiff'}={
246                     order=>'23',
247                     def=>'HAVE_LIBTIFF',
248                     inccheck=>sub { $_[0] eq 'tiffio.h' },
249                     libcheck=>sub { $_[0] eq 'libtiff.a' or $_ eq "libtiff.$lext" },
250                     libfiles=>'-ltiff',
251                     objfiles=>'tiff.o',
252                     docs=>q{
253                             In order to use tiff with this module you need to have libtiff
254                             installed on your computer}
255                    };
256
257   $formats{'png'}={
258                    order=>'22',
259                    def=>'HAVE_LIBPNG',
260                    inccheck=>sub { $_[0] eq 'png.h' },
261                    libcheck=>sub { $_[0] eq 'libpng.a' or $_[0] eq "libpng.$lext" },
262                    libfiles=>'-lpng -lz',
263                    objfiles=>'png.o',
264                    docs=>q{
265                            Png stands for Portable Network Graphics and is intended as
266                            a replacement for gif on the web. It is patent free and
267                            is recommended by the w3c, you need libpng to use these formats}
268                   };
269
270   $formats{'gif'}={
271                    order=>'20',
272                    def=>'HAVE_LIBGIF',
273                    inccheck=>sub { $_[0] eq 'gif_lib.h' },
274                    libcheck=>sub { $_[0] eq 'libgif.a' or $_[0] eq "libgif.$lext" },
275                    libfiles=>'-lgif',
276                    objfiles=>'gif.o',
277                    docs=>q{
278                            gif is the de facto standard for webgraphics at the moment,
279                            it does have some patent problems. If you have giflib and
280                            are not in violation with the unisys patent you should use
281                            this instead of the 'ungif' option.  Note that they cannot
282                            be in use at the same time}
283                   };
284
285   $formats{'ungif'}={
286                      order=>'21',
287                      def=>'HAVE_LIBGIF',
288                      inccheck=>sub { $_[0] eq 'gif_lib.h' },
289                      libcheck=>sub { $_[0] eq 'libungif.a' or $_[0] eq "libungif.$lext" },
290                      libfiles=>'-lungif',
291                      objfiles=>'gif.o',
292                      docs=>q{
293                              gif is the de facto standard for webgraphics at the moment,
294                              it does have some patent problems. If you have libungif and
295                              want to create images free from LZW patented compression you
296                              should use this option instead of the 'gif' option}
297                     };
298
299   $formats{'T1-fonts'}={
300                         order=>'30',
301                         def=>'HAVE_LIBT1',
302                         inccheck=>sub { $_[0] eq 't1lib.h' },
303                         libcheck=>sub { $_[0] eq 'libt1.a' or $_[0] eq "libt1.$lext" },
304                         libfiles=>'-lt1',
305                         objfiles=>'',
306                         docs=>q{
307                                 postscript t1 fonts are scalable fonts. They can include 
308                                 ligatures and kerning information and generally yield good
309                                 visual quality. We depend on libt1 to rasterize the fonts
310                                 for use in images.}
311                        };
312
313   $formats{'TT-fonts'}={
314                         order=>'31',
315                         def=>'HAVE_LIBTT',
316                         inccheck=>sub { $_[0] eq 'freetype.h' },
317                         libcheck=>sub { $_[0] eq 'libttf.a' or $_[0] eq "libttf.$lext" },
318                         libfiles=>'-lttf',
319                         objfiles=>'',
320                         docs=>q{
321                                 Truetype fonts are scalable fonts. They can include 
322                                 kerning and hinting information and generally yield good
323                                 visual quality esp on low resultions. The freetype library is
324                                 used to rasterize for us. The only drawback is that there
325                                 are alot of badly designed fonts out there.}
326                        };
327   # Make fix indent
328   for (keys %formats) { $formats{$_}->{docs} =~ s/^\s+/  /mg; }
329 }
330
331
332
333 sub gen {
334   my $V = $ENV{$_[0]};
335   defined($V) ? $V : "";
336 }
337
338
339 # Get information from environment variables
340
341 sub getenv {
342
343   ($VERBOSE,
344    $INCPATH,
345    $LIBPATH,
346    $NOLOG,
347    $DEBUG_MALLOC,
348    $MANUAL,
349    $CFLAGS,
350    $LFLAGS,
351    $DFLAGS) = map { gen $_ } qw(IM_VERBOSE
352                                 IM_INCPATH
353                                 IM_LIBPATH
354                                 IM_NOLOG
355                                 IM_DEBUG_MALLOC
356                                 IM_MANUAL
357                                 IM_CFLAGS
358                                 IM_LFLAGS
359                                 IM_DFLAGS);
360
361   if ($VERBOSE) { print "Verbose mode\n"; require Data::Dumper; import Data::Dumper qw(Dumper);}
362
363   if ($NOLOG)   { print "Logging not compiled into module\n"; }
364   else { $EXTDEF.=' -DIMAGER_LOG'; }
365
366   if ($DEBUG_MALLOC) {
367     $EXTDEF.=' -DIMAGER_DEBUG_MALLOC';
368     print "Malloc debugging enabled\n";
369   }
370
371 }