Compress::Zlib 1.37
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / Makefile.PL
1 #! perl -w
2
3 use strict ;
4 require 5.004 ;
5
6 use ExtUtils::MakeMaker 5.16 ;
7 use Config ;
8 use File::Copy ;
9
10 BEGIN
11 {
12     eval { require File::Spec::Functions ; File::Spec::Functions->import() } ;
13     if ($@)
14     {
15         *catfile = sub { return "$_[0]/$_[1]" }
16     }
17 }
18
19 require VMS::Filespec if $^O eq 'VMS';
20
21 my $ZLIB_LIB ;
22 my $ZLIB_INCLUDE ;
23 my $BUILD_ZLIB = 0 ;
24 my $OLD_ZLIB = '' ;
25 my $EXTRA_DEFINE = '';
26 my $WALL = '';
27 #$WALL = ' -Wall ';
28
29 unless($ENV{PERL_CORE}) {
30     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
31 }
32
33 # don't ask if MM_USE_DEFAULT is set -- enables perl core building on cygwin
34 if ($^O =~ /cygwin/i and not $ENV{PERL_MM_USE_DEFAULT} and not $ENV{PERL_CORE})
35 {
36     print <<EOM ;
37
38 I see you are running Cygwin.
39
40 Please note that this module cannot be installed on Cygwin using the
41 CPAN shell. The CPAN Shell uses Compress::Zlib internally and it is not
42 possible to delete an active DLL.
43
44 If you are running the CPAN shell, please exit it and install this module
45 by hand by running 'make install' under the directory
46
47     ~/.cpan/build/Compress-Zlib-VERSION
48
49 EOM
50
51     print "Do you want to continue? [Y/N]: " ;
52     my $answer = <STDIN> ;
53
54     if ($answer =~ /^yes|y/i)
55     {
56         print "continuing...\n" 
57     }
58     else
59     {
60         print "exiting...\n" ;
61         exit 1 ;
62     }
63
64
65 }
66
67 ParseCONFIG() ;
68
69 my @files = ('Zlib.pm', glob("t/*.t"), grep(!/\.bak$/,  glob("examples/*"))) ;
70 UpDowngrade(@files) unless $ENV{PERL_CORE} ;
71
72 WriteMakefile(  
73         NAME            => 'Compress::Zlib',
74         VERSION_FROM    => 'Zlib.pm',
75         INC             => "-I$ZLIB_INCLUDE" ,
76         DEFINE          => "$OLD_ZLIB $WALL $EXTRA_DEFINE" ,
77         XS              => { 'Zlib.xs'    => 'Zlib.c' },
78         'depend'        => { 'Makefile'   => 'config.in' },
79         'clean'         => { FILES        => '*.c constants.h constants.xs' },
80         'dist'          => { COMPRESS     => 'gzip', 
81                              SUFFIX       => 'gz',
82                              DIST_DEFAULT => 'MyDoubleCheck Downgrade tardist',
83                             },
84         ($BUILD_ZLIB
85           ? zlib_files($ZLIB_LIB)
86           : (LIBS       => [ "-L$ZLIB_LIB -lz " ])
87         ),  
88         ($] >= 5.005
89             ? (ABSTRACT_FROM    => 'Zlib.pm',
90                AUTHOR  => 'Paul Marquess <pmqs@cpan.org>')
91             : ()
92         ),
93     ) ;
94
95 my @names = qw(
96
97         DEF_WBITS
98         MAX_MEM_LEVEL
99         MAX_WBITS
100         OS_CODE
101
102         Z_ASCII
103         Z_BEST_COMPRESSION
104         Z_BEST_SPEED
105         Z_BINARY
106         Z_BUF_ERROR
107         Z_DATA_ERROR
108         Z_DEFAULT_COMPRESSION
109         Z_DEFAULT_STRATEGY
110         Z_DEFLATED
111         Z_ERRNO
112         Z_FILTERED
113         Z_FINISH
114         Z_FULL_FLUSH
115         Z_HUFFMAN_ONLY
116         Z_MEM_ERROR
117         Z_NEED_DICT
118         Z_NO_COMPRESSION
119         Z_NO_FLUSH
120         Z_NULL
121         Z_OK
122         Z_PARTIAL_FLUSH
123         Z_STREAM_END
124         Z_STREAM_ERROR
125         Z_SYNC_FLUSH
126         Z_UNKNOWN
127         Z_VERSION_ERROR
128         );
129
130 if (eval {require ExtUtils::Constant; 1}) {
131     # Check the constants above all appear in @EXPORT in Zlib.pm
132     my %names = map { $_, 1} @names, 'ZLIB_VERSION';
133     open F, "<Zlib.pm" or die "Cannot open Zlib.pm: $!\n";
134     while (<F>)
135     {
136         last if /^\s*\@EXPORT\s+=\s+qw\(/ ;
137     }
138
139     while (<F>)
140     {
141         last if /^\s*\)/ ;
142         /(\S+)/ ;
143         delete $names{$1} if defined $1 ;
144     }
145     close F ;
146
147     if ( keys %names )
148     {
149         my $missing = join ("\n\t", sort keys %names) ;
150         die "The following names are missing from \@EXPORT in Zlib.pm\n" .
151             "\t$missing\n" ;
152     }
153     
154     push @names, {name => 'ZLIB_VERSION', type => 'PV' };
155
156     ExtUtils::Constant::WriteConstants(
157                                      NAME => 'Zlib',
158                                      NAMES => \@names,
159                                      C_FILE  => 'constants.h',
160                                      XS_FILE  => 'constants.xs',
161                                                                        
162                                     );
163
164 else {
165     copy ('fallback.h', 'constants.h')
166       or die "Can't copy fallback.h to constants.h: $!";
167     copy ('fallback.xs', 'constants.xs')
168       or die "Can't copy fallback.xs to constants.xs: $!";
169 }
170
171 sub MY::postamble 
172 {
173     my $postamble = <<'EOM';
174
175 Downgrade:
176         @echo Downgrading.
177         perl Makefile.PL -downgrade
178
179 MyDoubleCheck:  
180         @echo Checking config.in is setup for a release
181         @(grep '^LIB *= *./zlib' config.in &&                   \
182           grep '^INCLUDE *= *./zlib' config.in &&               \
183           grep '^OLD_ZLIB *= *False' config.in &&               \
184           grep '^BUILD_ZLIB *= *True' config.in) >/dev/null ||  \
185         (echo config.in needs fixing ; exit 1)
186         @echo config.in is ok
187
188 MyTrebleCheck:
189         @echo Checking for $$^W in files: '. "@files" . '
190         @perl -ne \'                                            \
191             exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/;           \
192          \' ' . " @files || " . '                               \
193         (echo found unexpected $$^W ; exit 1)
194         @echo All is ok.
195
196 EOM
197
198     return $postamble;
199
200 }
201
202 sub ParseCONFIG
203 {
204     my ($k, $v) ;
205     my @badkey = () ;
206     my %Info = () ;
207     my @Options = qw( INCLUDE LIB BUILD_ZLIB OLD_ZLIB ) ;
208     my %ValidOption = map {$_, 1} @Options ;
209     my %Parsed = %ValidOption ;
210     my $CONFIG = 'config.in' ;
211
212     print "Parsing $CONFIG...\n" ;
213
214     open(F, "<$CONFIG") or die "Cannot open file $CONFIG: $!\n" ;
215     while (<F>) {
216         s/^\s*|\s*$//g ;
217         next if /^\s*$/ or /^\s*#/ ;
218         s/\s*#\s*$// ;
219
220         ($k, $v) = split(/\s+=\s+/, $_, 2) ;
221         $k = uc $k ;
222         if ($ValidOption{$k}) {
223             delete $Parsed{$k} ;
224             $Info{$k} = $v ;
225         }
226         else {
227             push(@badkey, $k) ;
228         }
229     }
230     close F ;
231
232     print "Unknown keys in $CONFIG ignored [@badkey]\n"
233         if @badkey ;
234
235     # check parsed values
236     my @missing = () ;
237     die "The following keys are missing from $CONFIG  [@missing]\n" 
238         if @missing = keys %Parsed ;
239
240     $ZLIB_INCLUDE = $ENV{'ZLIB_INCLUDE'} || $Info{'INCLUDE'} ;
241     $ZLIB_LIB = $ENV{'ZLIB_LIB'} || $Info{'LIB'} ;
242
243     if ($^O eq 'VMS') {
244         $ZLIB_INCLUDE = VMS::Filespec::vmspath($ZLIB_INCLUDE);
245         $ZLIB_LIB = VMS::Filespec::vmspath($ZLIB_LIB);
246     }
247
248     $EXTRA_DEFINE = $ENV{EXTRA_DEFINE} if defined $ENV{EXTRA_DEFINE};
249
250     my $y = $ENV{'OLD_ZLIB'} || $Info{'OLD_ZLIB'} ;
251     $OLD_ZLIB = '-DOLD_ZLIB' if $y and $y =~ /^yes|on|true|1$/i;
252
253     my $x = $ENV{'BUILD_ZLIB'} || $Info{'BUILD_ZLIB'} ;
254
255     if ($x and $x =~ /^yes|on|true|1$/i ) {
256
257         $BUILD_ZLIB = 1 ;
258
259         # ZLIB_LIB & ZLIB_INCLUDE must point to the same place when 
260         # BUILD_ZLIB is specified.
261         die "INCLUDE & LIB must be the same when BUILD_ZLIB is True\n"
262             if $ZLIB_LIB ne $ZLIB_INCLUDE ;
263
264         # Check the zlib source directory exists
265         die "LIB/INCLUDE directory '$ZLIB_LIB' does not exits\n"
266            unless -d $ZLIB_LIB ;
267
268         # check for a well known file
269         die "LIB/INCLUDE directory, '$ZLIB_LIB', doesn't seem to have the zlib source files\n"
270            unless -e catfile($ZLIB_LIB, 'zlib.h') ;
271
272
273         # check Makefile.zlib has been copied to ZLIB_LIB
274     #copy 'Makefile.zlib', catfile($ZLIB_LIB, 'Makefile.PL') ||
275     #die "Could not copy Makefile.zlib to " . catfile($ZLIB_LIB, 'Makefile.PL') . ": $!\n" ;
276        #print "Created a Makefile.PL for zlib\n" ;
277         
278         # write the Makefile
279         print "Building Zlib enabled\n" ;
280     }
281
282     print <<EOM if 0 ;
283     INCLUDE     [$ZLIB_INCLUDE]
284     LIB         [$ZLIB_LIB]
285
286 EOM
287
288     print "Looks Good.\n" ;
289
290 }
291
292 sub UpDowngrade
293 {
294     my @files = @_ ;
295
296     # our      is stable from 5.6.0 onward
297     # warnings is stable from 5.6.1 onward
298
299     # Note: this code assumes that each statement it modifies is not
300     #       split across multiple lines.
301
302
303     my $warn_sub = '';
304     my $our_sub = '' ;
305
306     my $opt = shift @ARGV || '' ;
307     my $upgrade = ($opt =~ /^-upgrade/i);
308     my $downgrade = ($opt =~ /^-downgrade/i);
309     push @ARGV, $opt unless $downgrade || $upgrade;
310
311     if ($downgrade) {
312         # From: use|no warnings "blah"
313         # To:   local ($^W) = 1; # use|no warnings "blah"
314         $warn_sub = sub {
315             s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ;
316             s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ;
317           };
318     }
319     elsif ($] >= 5.006001 || $upgrade) {
320         # From: local ($^W) = 1; # use|no warnings "blah"
321         # To:   use|no warnings "blah"
322         $warn_sub = sub {
323             s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ;
324           };
325     }
326
327     if ($downgrade) {
328         $our_sub = sub {
329             if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) {
330                 my $indent = $1;
331                 my $vars = join ' ', split /\s*,\s*/, $2;
332                 $_ = "${indent}use vars qw($vars);\n";
333             }
334           };
335     }
336     elsif ($] >= 5.006000 || $upgrade) {
337         $our_sub = sub {
338             if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) {
339                 my $indent = $1;
340                 my $vars = join ', ', split ' ', $2;
341                 $_ = "${indent}our ($vars);\n";
342             }
343           };
344     }
345
346     if (! $our_sub && ! $warn_sub) {
347         warn "Up/Downgrade not needed.\n";
348         if ($upgrade || $downgrade)
349           { exit 0 }
350         else
351           { return }
352     }
353
354     foreach (@files)
355       { doUpDown($our_sub, $warn_sub, $_) }
356
357     warn "Up/Downgrade complete.\n" ;
358     exit 0 if $upgrade || $downgrade;
359
360 }
361
362
363 sub doUpDown
364 {
365     my $our_sub = shift;
366     my $warn_sub = shift;
367
368     local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak";
369     local (@ARGV) = shift;
370
371     while (<>)
372     {
373         print, last if /^__(END|DATA)__/ ;
374
375         &{ $our_sub }() if $our_sub ;
376         &{ $warn_sub }() if $warn_sub ;
377         print ;
378     }
379
380     return if eof ;
381
382     while (<>)
383       { print }
384 }
385
386
387 sub zlib_files
388 {
389     my $dir = shift ;
390
391     my @h_files = ();
392     my @c_files = ();
393     
394     if (-f catfile($dir, "infback.c")) {
395         # zlib 1.2.0 or greater
396         #
397         @h_files = qw(crc32.h    inffast.h inflate.h  trees.h    zconf.in.h 
398                   zutil.h    deflate.h inffixed.h inftrees.h zconf.h  
399                   zlib.h 
400                  );
401         @c_files = qw(adler32  crc32   infback  inflate  uncompr
402                   compress deflate gzio     inffast  inftrees  
403                   trees    zutil 
404                  );
405     }
406     else {
407         # zlib 1.1.x
408     
409         @h_files = qw(deflate.h  infcodes.h inftrees.h zconf.h zutil.h
410                   infblock.h inffast.h  infutil.h  zlib.h
411                  );
412         @c_files = qw(adler32  compress crc32    gzio    uncompr
413                   deflate  trees    zutil    inflate infblock
414                   inftrees infcodes infutil  inffast
415                  );
416     }
417     
418     @h_files = map { catfile($dir, $_)  } @h_files ;
419     my @o_files = map { "$_\$(OBJ_EXT)" } 'Zlib', @c_files;
420     @c_files = map { "$_.c" } 'Zlib', @c_files ;
421
422     foreach my $file (@c_files)
423       { copy(catfile($dir, $file), '.') }
424     
425     return (
426         #'H'         =>  [ @h_files ],
427         'C'         =>  [ @c_files ] ,
428         #'OBJECT'    => qq[ @o_files ],
429         'OBJECT'    => q[ $(O_FILES) ],
430         
431
432            ) ;
433 }
434
435
436 # end of file Makefile.PL
437