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