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