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