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