Compress::Zlib becomes zlib agnostic
[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 qw(%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 my $GZIP_OS_CODE = -1 ;
27
28 #$WALL = ' -pedantic ' if $Config{'cc'} =~ /gcc/ ;
29 #$WALL = ' -Wall -Wno-comment ' if $Config{'cc'} =~ /gcc/ ;
30
31 unless($ENV{PERL_CORE}) {
32     $ENV{PERL_CORE} = 1 if grep { $_ eq 'PERL_CORE=1' } @ARGV;
33 }
34
35 # don't ask if MM_USE_DEFAULT is set -- enables perl core building on cygwin
36 if ($^O =~ /cygwin/i and not ($ENV{PERL_MM_USE_DEFAULT} or $ENV{PERL_CORE}))
37 {
38     print <<EOM ;
39
40 I see you are running Cygwin.
41
42 Please note that this module cannot be installed on Cygwin using the
43 CPAN shell. The CPAN Shell uses Compress::Zlib internally and it is not
44 possible to delete an active DLL.
45
46 If you are running the CPAN shell, please exit it and install this module
47 by hand by running 'make install' under the directory
48
49     ~/.cpan/build/Compress-Zlib-VERSION
50
51 EOM
52
53     print "Do you want to continue? [Y/N]: " ;
54     my $answer = <STDIN> ;
55
56     if ($answer =~ /^yes|y/i)
57     {
58         print "continuing...\n" 
59     }
60     else
61     {
62         print "exiting...\n" ;
63         exit 1 ;
64     }
65
66
67 }
68
69 ParseCONFIG() ;
70
71 my @files = ('Zlib.pm', 't/ZlibTestUtils.pm',
72              glob("t/*.t"), 
73              glob("t/*.pl"), 
74              glob("lib/CompressPlugin/*.pm"),
75              glob("lib/UncompressPlugin/*.pm"),
76              glob("lib/IO/Compress/*.pm"),
77              glob("lib/IO/Uncompress/*.pm"),
78              glob("lib/Compress/Zlib/*.pm"),
79              glob("lib/Compress/Gzip/*.pm"),
80              glob("lib/File/*.pm"),
81              glob("bzip2/*.pm"),
82              grep(!/\.bak$/,  glob("examples/*"))) ;
83
84 UpDowngrade(@files) unless $ENV{PERL_CORE};
85
86 WriteMakefile(  
87         NAME            => 'Compress::Zlib',
88         VERSION_FROM    => 'Zlib.pm',
89     #OPTIMIZE   => '-g',
90         INC             => "-I$ZLIB_INCLUDE" ,
91         DEFINE          => "$OLD_ZLIB $WALL -DGZIP_OS_CODE=$GZIP_OS_CODE" ,
92         XS              => { 'Zlib.xs'    => 'Zlib.c'},
93         $ENV{PERL_CORE}
94             ? (MAN3PODS    => {})
95             : (PREREQ_PM   => { 'Scalar::Util'  => 0,
96                     $] >= 5.005 && $] < 5.006   ? ('File::BSDGlob' => 0) : () }
97               ),
98         'depend'        => { 'Makefile'   => 'config.in' },
99         'clean'         => { FILES        => '*.c constants.h constants.xs' },
100         'dist'          => { COMPRESS     => 'gzip', 
101                              SUFFIX       => 'gz',
102                              DIST_DEFAULT => 'MyDoubleCheck downgrade tardist',
103                             },
104         ($BUILD_ZLIB
105           ? zlib_files($ZLIB_LIB)
106           : (LIBS       => [ "-L$ZLIB_LIB -lz " ])
107         ),  
108         $] >= 5.005
109             ? (ABSTRACT_FROM    => 'Zlib.pm',
110                AUTHOR  => 'Paul Marquess <pmqs@cpan.org>')
111             : (),
112         
113     ) ;
114
115 my @names = qw(
116
117         DEF_WBITS
118         MAX_MEM_LEVEL
119         MAX_WBITS
120         OS_CODE
121
122         Z_ASCII
123         Z_BEST_COMPRESSION
124         Z_BEST_SPEED
125         Z_BINARY
126         Z_BLOCK
127         Z_BUF_ERROR
128         Z_DATA_ERROR
129         Z_DEFAULT_COMPRESSION
130         Z_DEFAULT_STRATEGY
131         Z_DEFLATED
132         Z_ERRNO
133         Z_FILTERED
134         Z_FINISH
135         Z_FIXED
136         Z_FULL_FLUSH
137         Z_HUFFMAN_ONLY
138         Z_MEM_ERROR
139         Z_NEED_DICT
140         Z_NO_COMPRESSION
141         Z_NO_FLUSH
142         Z_NULL
143         Z_OK
144         Z_PARTIAL_FLUSH
145         Z_RLE
146         Z_STREAM_END
147         Z_STREAM_ERROR
148         Z_SYNC_FLUSH
149         Z_UNKNOWN
150         Z_VERSION_ERROR
151
152         );
153     #ZLIB_VERNUM
154
155 if (eval {require ExtUtils::Constant; 1}) {
156     # Check the constants above all appear in @EXPORT in Zlib.pm
157     my %names = map { $_, 1} @names, 'ZLIB_VERSION';
158     open F, "<Zlib.pm" or die "Cannot open Zlib.pm: $!\n";
159     while (<F>)
160     {
161         last if /^\s*\@EXPORT\s+=\s+qw\(/ ;
162     }
163
164     while (<F>)
165     {
166         last if /^\s*\)/ ;
167         /(\S+)/ ;
168         delete $names{$1} if defined $1 ;
169     }
170     close F ;
171
172     if ( keys %names )
173     {
174         my $missing = join ("\n\t", sort keys %names) ;
175         die "The following names are missing from \@EXPORT in Zlib.pm\n" .
176             "\t$missing\n" ;
177     }
178     
179     push @names, {name => 'ZLIB_VERSION', type => 'PV' };
180
181     ExtUtils::Constant::WriteConstants(
182                                      NAME => 'Zlib',
183                                      NAMES => \@names,
184                                      C_FILE  => 'constants.h',
185                                      XS_FILE  => 'constants.xs',
186                                                                        
187                                     );
188
189 else {
190     foreach my $name (qw( constants.h constants.xs ))
191     {
192         my $from = catfile('fallback', $name);
193         copy ($from, $name)
194           or die "Can't copy $from to $name: $!";
195     }
196 }
197
198 sub MY::libscan
199 {
200     my $self = shift;
201     my $path = shift;
202
203     return undef
204         if $path =~ /(~|\.bak|_bak)$/ ||
205            $path =~ /\..*\.sw(o|p)$/ ||
206            $path =~ /\B\.svn\b/;
207
208     return $path;
209 }
210
211 sub MY::postamble 
212 {
213     my $postamble = <<'EOM';
214
215 downgrade:
216         @echo Downgrading.
217         perl Makefile.PL -downgrade
218
219 MyDoubleCheck:  
220         @echo Checking config.in is setup for a release
221         @(grep '^LIB *= *./zlib-src' config.in &&                       \
222           grep '^INCLUDE *= *./zlib-src' config.in &&           \
223           grep '^OLD_ZLIB *= *False' config.in &&               \
224           grep '^GZIP_OS_CODE *= *AUTO_DETECT' config.in &&             \
225           grep '^BUILD_ZLIB *= *True' config.in) >/dev/null ||  \
226         (echo config.in needs fixing ; exit 1)
227         @echo config.in is ok
228
229 MyTrebleCheck:
230         @echo Checking for $$^W in files: '. "@files" . '
231         @perl -ne \'                                            \
232             exit 1 if /^\s*local\s*\(\s*\$$\^W\s*\)/;           \
233          \' ' . " @files || " . '                               \
234         (echo found unexpected $$^W ; exit 1)
235         @echo All is ok.
236
237 longtest:
238         @echo Running test suite with Devel::Cover
239         $(MAKE) test COMPRESS_ZLIB_RUN_ALL=1 
240
241 cover:
242         @echo Running test suite with Devel::Cover
243         HARNESS_PERL_SWITCHES=-MDevel::Cover $(MAKE) test
244
245 longcover:
246         @echo Running test suite with Devel::Cover
247         HARNESS_PERL_SWITCHES=-MDevel::Cover $(MAKE) test COMPRESS_ZLIB_RUN_ALL=1 
248
249 test-unicode:
250         @echo Running test suite with unicode support enabled
251         env PERL_UNICODE=63 $(MAKE) test        
252
253 EOM
254
255     $postamble .= <<'EOM' if $^O eq 'linux' ;
256
257 gcov:
258         @echo Running test suite with gcov and Devel::Cover [needs gcc 3.4?]
259         rm -f *.o *.gcov *.da *.bbg *.bb *.gcno 
260         HARNESS_PERL_SWITCHES=-MDevel::Cover $(MAKE) test \
261        OPTIMIZE=-g \
262        CCFLAGS=-O0\ -fprofile-arcs\ -ftest-coverage \
263        OTHERLDFLAGS=-fprofile-arcs\ -ftest-coverage
264         gcov Zlib.xs
265         gcov2perl -db cover_db Zlib.xs.gcov
266 EOM
267
268     return $postamble;
269
270 }
271
272 sub ParseCONFIG
273 {
274     my ($k, $v) ;
275     my @badkey = () ;
276     my %Info = () ;
277     my @Options = qw( INCLUDE LIB BUILD_ZLIB OLD_ZLIB GZIP_OS_CODE ) ;
278     my %ValidOption = map {$_, 1} @Options ;
279     my %Parsed = %ValidOption ;
280     my $CONFIG = 'config.in' ;
281
282     print "Parsing $CONFIG...\n" ;
283
284     open(F, "<$CONFIG") or die "Cannot open file $CONFIG: $!\n" ;
285     while (<F>) {
286         s/^\s*|\s*$//g ;
287         next if /^\s*$/ or /^\s*#/ ;
288         s/\s*#\s*$// ;
289
290         ($k, $v) = split(/\s+=\s+/, $_, 2) ;
291         $k = uc $k ;
292         if ($ValidOption{$k}) {
293             delete $Parsed{$k} ;
294             $Info{$k} = $v ;
295         }
296         else {
297             push(@badkey, $k) ;
298         }
299     }
300     close F ;
301
302     print "Unknown keys in $CONFIG ignored [@badkey]\n"
303         if @badkey ;
304
305     # check parsed values
306     my @missing = () ;
307     die "The following keys are missing from $CONFIG  [@missing]\n" 
308         if @missing = keys %Parsed ;
309
310     $ZLIB_INCLUDE = $ENV{'ZLIB_INCLUDE'} || $Info{'INCLUDE'} ;
311     $ZLIB_LIB = $ENV{'ZLIB_LIB'} || $Info{'LIB'} ;
312
313     if ($^O eq 'VMS') {
314         $ZLIB_INCLUDE = VMS::Filespec::vmspath($ZLIB_INCLUDE);
315         $ZLIB_LIB = VMS::Filespec::vmspath($ZLIB_LIB);
316     }
317
318     my $y = $ENV{'OLD_ZLIB'} || $Info{'OLD_ZLIB'} ;
319     $OLD_ZLIB = '-DOLD_ZLIB' if $y and $y =~ /^yes|on|true|1$/i;
320
321     my $x = $ENV{'BUILD_ZLIB'} || $Info{'BUILD_ZLIB'} ;
322
323     if ($x and $x =~ /^yes|on|true|1$/i ) {
324
325         $BUILD_ZLIB = 1 ;
326
327         # ZLIB_LIB & ZLIB_INCLUDE must point to the same place when 
328         # BUILD_ZLIB is specified.
329         die "INCLUDE & LIB must be the same when BUILD_ZLIB is True\n"
330             if $ZLIB_LIB ne $ZLIB_INCLUDE ;
331
332         # Check the zlib source directory exists
333         die "LIB/INCLUDE directory '$ZLIB_LIB' does not exits\n"
334            unless -d $ZLIB_LIB ;
335
336         # check for a well known file
337         die "LIB/INCLUDE directory, '$ZLIB_LIB', doesn't seem to have the zlib source files\n"
338            unless -e catfile($ZLIB_LIB, 'zlib.h') ;
339
340
341         # write the Makefile
342         print "Building Zlib enabled\n" ;
343     }
344
345     $GZIP_OS_CODE = defined $ENV{'GZIP_OS_CODE'} 
346                           ? $ENV{'GZIP_OS_CODE'} 
347                           : $Info{'GZIP_OS_CODE'} ;
348
349         die "GZIP_OS_CODE not 'AUTO_DETECT' or a number between 0 and 255\n"
350            unless uc $GZIP_OS_CODE eq 'AUTO_DETECT'
351                     || ( $GZIP_OS_CODE =~ /^(\d+)$/ && $1 >= 0 && $1 <= 255) ;
352
353     if (uc $GZIP_OS_CODE eq 'AUTO_DETECT')
354     {
355         print "Auto Detect Gzip OS Code..\n" ;
356         $GZIP_OS_CODE = getOSCode() ;
357     }
358     
359     my $name = getOSname($GZIP_OS_CODE);
360     print "Setting Gzip OS Code to $GZIP_OS_CODE [$name]\n" ;
361
362     print <<EOM if 0 ;
363     INCLUDE         [$ZLIB_INCLUDE]
364     LIB             [$ZLIB_LIB]
365     GZIP_OS_CODE    [$GZIP_OS_CODE]
366     OLD_ZLIB        [$OLD_ZLIB]
367     BUILD_ZLIB      [$BUILD_ZLIB]
368
369 EOM
370
371     print "Looks Good.\n" ;
372
373 }
374
375 sub UpDowngrade
376 {
377     my @files = @_ ;
378
379     # our and use bytes/utf8 is stable from 5.6.0 onward
380     # warnings is stable from 5.6.1 onward
381
382     # Note: this code assumes that each statement it modifies is not
383     #       split across multiple lines.
384
385
386     my $warn_sub = '';
387     my $our_sub = '' ;
388
389     my $opt = shift @ARGV || '' ;
390     my $upgrade = ($opt =~ /^-upgrade/i);
391     my $downgrade = ($opt =~ /^-downgrade/i);
392     push @ARGV, $opt unless $downgrade || $upgrade;
393
394     if ($downgrade) {
395         # From: use|no warnings "blah"
396         # To:   local ($^W) = 1; # use|no warnings "blah"
397         $warn_sub = sub {
398             s/^(\s*)(no\s+warnings)/${1}local (\$^W) = 0; #$2/ ;
399             s/^(\s*)(use\s+warnings)/${1}local (\$^W) = 1; #$2/ ;
400           };
401     }
402     elsif ($] >= 5.006001 || $upgrade) {
403         # From: local ($^W) = 1; # use|no warnings "blah"
404         # To:   use|no warnings "blah"
405         $warn_sub = sub {
406             s/^(\s*)local\s*\(\$\^W\)\s*=\s*\d+\s*;\s*#\s*((no|use)\s+warnings.*)/$1$2/ ;
407           };
408     }
409
410     if ($downgrade) {
411         $our_sub = sub {
412             if ( /^(\s*)our\s+\(\s*([^)]+\s*)\)/ ) {
413                 my $indent = $1;
414                 my $vars = join ' ', split /\s*,\s*/, $2;
415                 $_ = "${indent}use vars qw($vars);\n";
416             }
417             elsif ( /^(\s*)((use|no)\s+(bytes|utf8)\s*;.*)$/)
418             {
419                 $_ = "$1# $2\n";
420             }
421           };
422     }
423     elsif ($] >= 5.006000 || $upgrade) {
424         $our_sub = sub {
425             if ( /^(\s*)use\s+vars\s+qw\((.*?)\)/ ) {
426                 my $indent = $1;
427                 my $vars = join ', ', split ' ', $2;
428                 $_ = "${indent}our ($vars);\n";
429             }
430             elsif ( /^(\s*)#\s*((use|no)\s+(bytes|utf8)\s*;.*)$/)
431             {
432                 $_ = "$1$2\n";
433             }
434           };
435     }
436
437     if (! $our_sub && ! $warn_sub) {
438         warn "Up/Downgrade not needed.\n";
439         if ($upgrade || $downgrade)
440           { exit 0 }
441         else
442           { return }
443     }
444
445     foreach (@files)
446       { doUpDown($our_sub, $warn_sub, $_) }
447
448     warn "Up/Downgrade complete.\n" ;
449     exit 0 if $upgrade || $downgrade;
450
451 }
452
453
454 sub doUpDown
455 {
456     my $our_sub = shift;
457     my $warn_sub = shift;
458
459     return if -d $_[0];
460
461     local ($^I) = ($^O eq 'VMS') ? "_bak" : ".bak";
462     local (@ARGV) = shift;
463
464     while (<>)
465     {
466         print, last if /^__(END|DATA)__/ ;
467
468         &{ $our_sub }() if $our_sub ;
469         &{ $warn_sub }() if $warn_sub ;
470         print ;
471     }
472
473     return if eof ;
474
475     while (<>)
476       { print }
477 }
478
479
480 sub zlib_files
481 {
482     my $dir = shift ;
483
484     my @h_files = ();
485     my @c_files = ();
486     
487     if (-f catfile($dir, "infback.c")) {
488         # zlib 1.2.0 or greater
489         #
490         @h_files = qw(crc32.h    inffast.h inflate.h  trees.h    zconf.in.h 
491                       zutil.h    deflate.h inffixed.h inftrees.h zconf.h  
492                       zlib.h 
493                  );
494         @c_files = qw(adler32  crc32   infback  inflate  uncompr
495                       compress deflate inffast  inftrees  
496                       trees    zutil 
497                  );
498     }
499     else {
500         # zlib 1.1.x
501     
502         @h_files = qw(deflate.h  infcodes.h inftrees.h zconf.h zutil.h
503                       infblock.h inffast.h  infutil.h  zlib.h
504                  );
505         @c_files = qw(adler32  compress crc32    uncompr
506                       deflate  trees    zutil    inflate infblock
507                       inftrees infcodes infutil  inffast
508                  );
509     }
510     
511     @h_files = map { catfile($dir, $_)  } @h_files ;
512     my @o_files = map { "$_\$(OBJ_EXT)" } 'Zlib', @c_files;
513     @c_files = map { "$_.c" } 'Zlib', @c_files ;
514
515     foreach my $file (@c_files)
516       { copy(catfile($dir, $file), '.') }
517     
518     return (
519         #'H'         =>  [ @h_files ],
520         'C'         =>  [ @c_files ] ,
521         #'OBJECT'    => qq[ @o_files ],
522         'OBJECT'    => q[ $(O_FILES) ],
523         
524
525            ) ;
526 }
527
528
529
530 my @GZIP_OS_Names ;
531 my %OSnames ;
532
533 BEGIN
534 {
535   @GZIP_OS_Names = (
536     [ ''        => 0,    'MS-DOS'                       ],
537     [ 'amigaos' => 1,    'Amiga'                        ],
538     [ 'VMS'     => 2,    'VMS'                          ],
539     [ ''        => 3,    'Unix/Default'                 ],
540     [ ''        => 4,    'VM/CMS'                       ],
541     [ ''        => 5,    'Atari TOS'                    ],
542     [ 'os2'     => 6,    'HPFS (OS/2, NT)'              ],
543     [ 'MacOS'   => 7,    'Macintosh'                    ],
544     [ ''        => 8,    'Z-System'                     ],
545     [ ''        => 9,    'CP/M'                         ],
546     [ ''        => 10,   'TOPS-20'                      ],
547     [ ''        => 11,   'NTFS (NT)'                    ],
548     [ ''        => 12,   'SMS QDOS'                     ],
549     [ ''        => 13,   'Acorn RISCOS'                 ],
550     [ 'MSWin32' => 14,   'VFAT file system (Win95, NT)' ],
551     [ ''        => 15,   'MVS'                          ],
552     [ 'beos'    => 16,   'BeOS'                         ],
553     [ ''        => 17,   'Tandem/NSK'                   ],
554     [ ''        => 18,   'THEOS'                        ],
555     [ ''        => 255,  'Unknown OS'                   ],
556   );
557
558   %OSnames = map { $$_[1] => $$_[2] }  
559              @GZIP_OS_Names ;
560 }
561
562 sub getOSCode
563 {
564     my $default = 3 ; # Unix is the default
565
566     my $uname = $^O;
567
568     for my $h (@GZIP_OS_Names)
569     {
570         my ($pattern, $code, $name) = @$h;
571
572         return $code
573             if $pattern && $uname eq $pattern ;
574     }
575
576     return $default ;
577 }
578
579 sub getOSname
580 {
581     my $code = shift ;
582
583     return $OSnames{$code} || 'Unknown OS' ;
584 }
585
586 # end of file Makefile.PL
587