1#! perl -w
2
3use strict ;
4require 5.006 ;
5
6use lib '.';
7use private::MakeUtil;
8use ExtUtils::MakeMaker 5.16 ;
9use ExtUtils::Install (); # only needed to check for version
10use Config;
11
12my $ZLIB_LIB ;
13my $ZLIB_INCLUDE ;
14my $BUILD_ZLIB = 0 ;
15my $OLD_ZLIB = '' ;
16my $WALL = '' ;
17my $GZIP_OS_CODE = -1 ;
18my $USE_PPPORT_H = ($ENV{PERL_CORE}) ? '' : '-DUSE_PPPORT_H';
19my $OPTIMIZE = $Config{'optimize'};
20if ($Config{'gccversion'} and $OPTIMIZE =~ /-g (gdb)? 3/x) {
21  $OPTIMIZE =~ s/-g(gdb)?3/-g/g; # [88936] out of memory with -g3 since 2.062
22}
23
24# Windows uses a different name for the zlib library
25my $ZLIB_LIBRARY_NAME = $^O eq 'MSWin32' ? 'zlib' : 'z' ;
26
27#$WALL = ' -pedantic ' if $Config{'cc'} =~ /gcc/ ;
28#$WALL = ' -Wall -Wno-comment ' if $Config{'cc'} =~ /gcc/ ;
29
30# Ticket #18986 says that ExtUtils::Install 1.39 fixes the in-use issue
31# on win32/cygwin, so make the code below conditional on the version of
32# ExtUtils::Install.
33
34# Don't ask if MM_USE_DEFAULT is set -- enables perl core building on cygwin
35if ($^O =~ /cygwin/i and $ExtUtils::Install::VERSION < 1.39
36        and not ($ENV{PERL_MM_USE_DEFAULT} or $ENV{PERL_CORE}))
37{
38    print <<EOM ;
39
40I see you are running Cygwin.
41
42Please note that this module cannot be installed on Cygwin using the CPAN
43shell. The CPAN Shell uses Compress::Raw::Zlib internally and it is not
44possible to delete an active DLL.
45
46If you are running the CPAN shell, please exit it and install this module
47by hand by running 'make install' under the directory
48
49    ~/.cpan/build/Compress-Raw-Zlib-VERSION
50
51EOM
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
69ParseCONFIG() ;
70
71UpDowngrade(getPerlFiles('MANIFEST'))
72    unless $ENV{PERL_CORE};
73
74WriteMakefile(
75    NAME         => 'Compress::Raw::Zlib',
76    VERSION_FROM => 'lib/Compress/Raw/Zlib.pm',
77    INC          => "-I$ZLIB_INCLUDE" ,
78    DEFINE       => "-DNO_VIZ -DZ_SOLO $OLD_ZLIB $WALL -DGZIP_OS_CODE=$GZIP_OS_CODE $USE_PPPORT_H" ,
79    XS           => { 'Zlib.xs' => 'Zlib.c'},
80    'depend'     => { 'Makefile'   => 'config.in' },
81    'clean'      => { FILES        => '*.c constants.h constants.xs' },
82    'dist'       => { COMPRESS     => 'gzip',
83                      TARFLAGS     => '-chvf',
84                      SUFFIX       => 'gz',
85                      DIST_DEFAULT => 'MyTrebleCheck tardist',
86                    },
87
88    (
89      $BUILD_ZLIB
90        ? zlib_files($ZLIB_LIB)
91        : (LIBS => [ "-L$ZLIB_LIB -l$ZLIB_LIBRARY_NAME " ])
92    ),
93    OPTIMIZE => $OPTIMIZE,
94
95    INSTALLDIRS => ($] >= 5.009 && $] < 5.011 ? 'perl' : 'site'),
96
97    META_MERGE => {
98        no_index => {
99            directory => [ 't', 'private' ],
100        },
101    },
102
103    ((ExtUtils::MakeMaker->VERSION() gt '6.30') ?
104        ('LICENSE'  => 'perl')         : ()),
105
106) ;
107
108sub version_Macro
109{
110    my $ver = shift ;
111
112    return [ "#if ZLIB_VERNUM >= 0x$ver\n", "#endif\n" ];
113}
114
115my @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
155my %verSpecificNames = (
156    Z_TREES => '1240',
157);
158
159if (eval {require ExtUtils::Constant; 1}) {
160    # Check the constants above all appear in @EXPORT in Zlib.pm
161    my %names = %verSpecificNames, map { $_, 1} @names, 'ZLIB_VERSION';
162    open F, "<lib/Compress/Raw/Zlib.pm" or die "Cannot open Zlib.pm: $!\n";
163    while (<F>)
164    {
165        last if /^\s*\@EXPORT\s+=\s+qw\(/ ;
166    }
167
168    while (<F>)
169    {
170        last if /^\s*\)/ ;
171        /(\S+)/ ;
172        delete $names{$1} if defined $1 ;
173    }
174    close F ;
175
176    if ( keys %names )
177    {
178        my $missing = join ("\n\t", sort keys %names) ;
179        die "The following names are missing from \@EXPORT in Zlib.pm\n" .
180            "\t$missing\n" ;
181    }
182
183    push @names, { name => 'ZLIB_VERSION', type => 'PV' };
184
185    push @names, map { { name => $_,
186                         macro => version_Macro $verSpecificNames{$_}
187                       }
188                     }
189                 keys %verSpecificNames ;
190
191    ExtUtils::Constant::WriteConstants(
192                                     NAME => 'Zlib',
193                                     NAMES => \@names,
194                                     C_FILE  => 'constants.h',
195                                     XS_FILE  => 'constants.xs',
196
197                                    );
198
199}
200else {
201    foreach my $name (qw( constants.h constants.xs ))
202    {
203        my $from = catfile('fallback', $name);
204        copy ($from, $name)
205          or die "Can't copy $from to $name: $!";
206    }
207}
208
209sub ParseCONFIG
210{
211    my ($k, $v) ;
212    my @badkey = () ;
213    my %Info = () ;
214    my @Options = qw( INCLUDE LIB BUILD_ZLIB OLD_ZLIB GZIP_OS_CODE ) ;
215    my %ValidOption = map {$_, 1} @Options ;
216    my %Parsed = %ValidOption ;
217    my $CONFIG = 'config.in' ;
218
219    print "Parsing $CONFIG...\n" ;
220
221    open(F, "<$CONFIG") or die "Cannot open file $CONFIG: $!\n" ;
222    while (<F>) {
223	s/^\s*|\s*$//g ;
224	next if /^\s*$/ or /^\s*#/ ;
225	s/\s*#\s*$// ;
226
227	($k, $v) = split(/\s+=\s+/, $_, 2) ;
228	$k = uc $k ;
229	if ($ValidOption{$k}) {
230	    delete $Parsed{$k} ;
231	    $Info{$k} = $v ;
232	}
233	else {
234	    push(@badkey, $k) ;
235	}
236    }
237    close F ;
238
239    print "Unknown keys in $CONFIG ignored [@badkey]\n"
240	if @badkey ;
241
242    # check parsed values
243    my @missing = () ;
244    die "The following keys are missing from $CONFIG  [@missing]\n"
245        if @missing = keys %Parsed ;
246
247    $ZLIB_INCLUDE = defined $ENV{'ZLIB_INCLUDE'}
248                        ? $ENV{'ZLIB_INCLUDE'}
249                        : $Info{'INCLUDE'} ;
250    $ZLIB_LIB = defined $ENV{'ZLIB_LIB'}
251                    ?$ENV{'ZLIB_LIB'}
252                    : $Info{'LIB'} ;
253
254    if ($^O eq 'VMS') {
255        $ZLIB_INCLUDE = VMS::Filespec::vmspath($ZLIB_INCLUDE);
256        $ZLIB_LIB = VMS::Filespec::vmspath($ZLIB_LIB);
257    }
258
259    my $y = defined $ENV{'OLD_ZLIB'}
260                ? $ENV{'OLD_ZLIB'}
261                : $Info{'OLD_ZLIB'} ;
262    $OLD_ZLIB = '-DOLD_ZLIB' if $y and $y =~ /^yes|on|true|1$/i;
263
264    my $x = defined $ENV{'BUILD_ZLIB'}
265                ? $ENV{'BUILD_ZLIB'}
266                : $Info{'BUILD_ZLIB'} ;
267
268    if ($x and $x =~ /^yes|on|true|1$/i ) {
269
270        $BUILD_ZLIB = 1 ;
271
272	# ZLIB_LIB & ZLIB_INCLUDE must point to the same place when
273	# BUILD_ZLIB is specified.
274	die "INCLUDE & LIB must be the same when BUILD_ZLIB is True\n"
275	    if $ZLIB_LIB ne $ZLIB_INCLUDE ;
276
277	# Check the zlib source directory exists
278	die "LIB/INCLUDE directory '$ZLIB_LIB' does not exits\n"
279	   unless -d $ZLIB_LIB ;
280
281	# check for a well known file
282	die "LIB/INCLUDE directory, '$ZLIB_LIB', doesn't seem to have the zlib source files\n"
283	   unless -e catfile($ZLIB_LIB, 'zlib.h') ;
284
285
286	# write the Makefile
287	print "Building Zlib enabled\n" ;
288    }
289
290    $GZIP_OS_CODE = defined $ENV{'GZIP_OS_CODE'}
291                          ? $ENV{'GZIP_OS_CODE'}
292                          : $Info{'GZIP_OS_CODE'} ;
293
294	die "GZIP_OS_CODE not 'AUTO_DETECT' or a number between 0 and 255\n"
295	   unless uc $GZIP_OS_CODE eq 'AUTO_DETECT'
296                    || ( $GZIP_OS_CODE =~ /^(\d+)$/ && $1 >= 0 && $1 <= 255) ;
297
298    if (uc $GZIP_OS_CODE eq 'AUTO_DETECT')
299    {
300        print "Auto Detect Gzip OS Code..\n" ;
301        $GZIP_OS_CODE = getOSCode() ;
302    }
303
304    my $name = getOSname($GZIP_OS_CODE);
305    print "Setting Gzip OS Code to $GZIP_OS_CODE [$name]\n" ;
306
307    print <<EOM if 0 ;
308    INCLUDE         [$ZLIB_INCLUDE]
309    LIB             [$ZLIB_LIB]
310    GZIP_OS_CODE    [$GZIP_OS_CODE]
311    OLD_ZLIB        [$OLD_ZLIB]
312    BUILD_ZLIB      [$BUILD_ZLIB]
313
314EOM
315
316    print "Looks Good.\n" ;
317
318}
319
320
321
322sub zlib_files
323{
324    my $dir = shift ;
325
326    my @h_files = ();
327    my @c_files = ();
328
329    if (-f catfile($dir, "infback.c")) {
330        # zlib 1.2.0 or greater
331        #
332        @h_files = qw(crc32.h    inffast.h inflate.h  trees.h    zconf.in.h
333    	              zutil.h    deflate.h inffixed.h inftrees.h zconf.h
334    		      zlib.h
335    		 );
336        @c_files = qw(adler32  crc32   infback  inflate  uncompr
337    		      compress deflate inffast  inftrees
338    		      trees    zutil
339    		 );
340    }
341    else {
342        # zlib 1.1.x
343
344        @h_files = qw(deflate.h  infcodes.h inftrees.h zconf.h zutil.h
345    		      infblock.h inffast.h  infutil.h  zlib.h
346    		 );
347        @c_files = qw(adler32  compress crc32    uncompr
348    		      deflate  trees    zutil    inflate infblock
349    		      inftrees infcodes infutil  inffast
350    		 );
351    }
352
353    @h_files = map { catfile($dir, $_)  } @h_files ;
354    my @o_files = map { "$_\$(OBJ_EXT)" } 'Zlib', @c_files;
355    @c_files = map { "$_.c" } 'Zlib', @c_files ;
356
357    foreach my $file (@c_files)
358      { copy(catfile($dir, $file), '.') }
359
360    return (
361        #'H'         =>  [ @h_files ],
362    	'C'         =>  [ @c_files ] ,
363        #'OBJECT'    => qq[ @o_files ],
364        'OBJECT'    => q[ $(O_FILES) ],
365
366
367           ) ;
368}
369
370
371
372use vars qw ( @GZIP_OS_Names  %OSnames) ;
373
374BEGIN
375{
376  @GZIP_OS_Names = (
377    [ ''        => 0,    'MS-DOS'                       ],
378    [ 'amigaos' => 1,    'Amiga'                        ],
379    [ 'VMS'     => 2,    'VMS'                          ],
380    [ ''        => 3,    'Unix/Default'                 ],
381    [ ''        => 4,    'VM/CMS'                       ],
382    [ ''        => 5,    'Atari TOS'                    ],
383    [ 'os2'     => 6,    'HPFS (OS/2, NT)'              ],
384    [ 'MacOS'   => 7,    'Macintosh'                    ],
385    [ ''        => 8,    'Z-System'                     ],
386    [ ''        => 9,    'CP/M'                         ],
387    [ ''        => 10,   'TOPS-20'                      ],
388    [ ''        => 11,   'NTFS (NT)'                    ],
389    [ ''        => 12,   'SMS QDOS'                     ],
390    [ ''        => 13,   'Acorn RISCOS'                 ],
391    [ 'MSWin32' => 14,   'VFAT file system (Win95, NT)' ],
392    [ ''        => 15,   'MVS'                          ],
393    [ 'beos'    => 16,   'BeOS'                         ],
394    [ ''        => 17,   'Tandem/NSK'                   ],
395    [ ''        => 18,   'THEOS'                        ],
396    [ ''        => 255,  'Unknown OS'                   ],
397  );
398
399  %OSnames = map { $$_[1] => $$_[2] }
400             @GZIP_OS_Names ;
401}
402
403sub getOSCode
404{
405    my $default = 3 ; # Unix is the default
406
407    my $uname = $^O;
408
409    for my $h (@GZIP_OS_Names)
410    {
411        my ($pattern, $code, $name) = @$h;
412
413        return $code
414            if $pattern && $uname eq $pattern ;
415    }
416
417    return $default ;
418}
419
420sub getOSname
421{
422    my $code = shift ;
423
424    return $OSnames{$code} || 'Unknown OS' ;
425}
426
427# end of file Makefile.PL
428
429