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 ? 'perl' : 'site'),
86
87    ((ExtUtils::MakeMaker->VERSION() gt '6.30') ?
88        ('LICENSE'  => 'perl')         : ()),
89
90) ;
91
92sub version_Macro
93{
94    my $ver = shift ;
95
96    return [ "#if ZLIB_VERNUM >= 0x$ver\n", "#endif\n" ];
97}
98
99
100my @names = qw(
101
102    DEF_WBITS
103    MAX_MEM_LEVEL
104    MAX_WBITS
105    OS_CODE
106
107    Z_ASCII
108    Z_BEST_COMPRESSION
109    Z_BEST_SPEED
110    Z_BINARY
111    Z_BLOCK
112    Z_BUF_ERROR
113    Z_DATA_ERROR
114    Z_DEFAULT_COMPRESSION
115    Z_DEFAULT_STRATEGY
116    Z_DEFLATED
117    Z_ERRNO
118    Z_FILTERED
119    Z_FINISH
120    Z_FIXED
121    Z_FULL_FLUSH
122    Z_HUFFMAN_ONLY
123    Z_MEM_ERROR
124    Z_NEED_DICT
125    Z_NO_COMPRESSION
126    Z_NO_FLUSH
127    Z_NULL
128    Z_OK
129    Z_PARTIAL_FLUSH
130    Z_RLE
131    Z_STREAM_END
132    Z_STREAM_ERROR
133    Z_SYNC_FLUSH
134    Z_UNKNOWN
135    Z_VERSION_ERROR
136
137);
138    #ZLIB_VERNUM
139
140my %verSpecificNames = (
141    Z_TREES => '1240',
142);
143
144if (eval {require ExtUtils::Constant; 1}) {
145    # Check the constants above all appear in @EXPORT in Zlib.pm
146    my %names = %verSpecificNames, map { $_, 1} @names, 'ZLIB_VERSION';
147    open F, "<lib/Compress/Raw/Zlib.pm" or die "Cannot open Zlib.pm: $!\n";
148    while (<F>)
149    {
150        last if /^\s*\@EXPORT\s+=\s+qw\(/ ;
151    }
152
153    while (<F>)
154    {
155        last if /^\s*\)/ ;
156        /(\S+)/ ;
157        delete $names{$1} if defined $1 ;
158    }
159    close F ;
160
161    if ( keys %names )
162    {
163        my $missing = join ("\n\t", sort keys %names) ;
164        die "The following names are missing from \@EXPORT in Zlib.pm\n" .
165            "\t$missing\n" ;
166    }
167
168    push @names, { name => 'ZLIB_VERSION', type => 'PV' };
169
170    push @names, map { { name => $_,
171                         macro => version_Macro $verSpecificNames{$_}
172                       }
173                     }
174                 keys %verSpecificNames ;
175
176    ExtUtils::Constant::WriteConstants(
177                                     NAME => 'Zlib',
178                                     NAMES => \@names,
179                                     C_FILE  => 'constants.h',
180                                     XS_FILE  => 'constants.xs',
181
182                                    );
183
184}
185else {
186    foreach my $name (qw( constants.h constants.xs ))
187    {
188        my $from = catfile('fallback', $name);
189        copy ($from, $name)
190          or die "Can't copy $from to $name: $!";
191    }
192}
193
194sub ParseCONFIG
195{
196    my ($k, $v) ;
197    my @badkey = () ;
198    my %Info = () ;
199    my @Options = qw( INCLUDE LIB BUILD_ZLIB OLD_ZLIB GZIP_OS_CODE ) ;
200    my %ValidOption = map {$_, 1} @Options ;
201    my %Parsed = %ValidOption ;
202    my $CONFIG = 'config.in' ;
203
204    print "Parsing $CONFIG...\n" ;
205
206    open(F, "<$CONFIG") or die "Cannot open file $CONFIG: $!\n" ;
207    while (<F>) {
208	s/^\s*|\s*$//g ;
209	next if /^\s*$/ or /^\s*#/ ;
210	s/\s*#\s*$// ;
211
212	($k, $v) = split(/\s+=\s+/, $_, 2) ;
213	$k = uc $k ;
214	if ($ValidOption{$k}) {
215	    delete $Parsed{$k} ;
216	    $Info{$k} = $v ;
217	}
218	else {
219	    push(@badkey, $k) ;
220	}
221    }
222    close F ;
223
224    print "Unknown keys in $CONFIG ignored [@badkey]\n"
225	if @badkey ;
226
227    # check parsed values
228    my @missing = () ;
229    die "The following keys are missing from $CONFIG  [@missing]\n"
230        if @missing = keys %Parsed ;
231
232    $ZLIB_INCLUDE = $ENV{'ZLIB_INCLUDE'} || $Info{'INCLUDE'} ;
233    $ZLIB_LIB = $ENV{'ZLIB_LIB'} || $Info{'LIB'} ;
234
235    if ($^O eq 'VMS') {
236        $ZLIB_INCLUDE = VMS::Filespec::vmspath($ZLIB_INCLUDE);
237        $ZLIB_LIB = VMS::Filespec::vmspath($ZLIB_LIB);
238    }
239
240    my $y = $ENV{'OLD_ZLIB'} || $Info{'OLD_ZLIB'} ;
241    $OLD_ZLIB = '-DOLD_ZLIB' if $y and $y =~ /^yes|on|true|1$/i;
242
243    my $x = $ENV{'BUILD_ZLIB'} || $Info{'BUILD_ZLIB'} ;
244
245    if ($x and $x =~ /^yes|on|true|1$/i ) {
246
247        $BUILD_ZLIB = 1 ;
248
249	# ZLIB_LIB & ZLIB_INCLUDE must point to the same place when
250	# BUILD_ZLIB is specified.
251	die "INCLUDE & LIB must be the same when BUILD_ZLIB is True\n"
252	    if $ZLIB_LIB ne $ZLIB_INCLUDE ;
253
254	# Check the zlib source directory exists
255	die "LIB/INCLUDE directory '$ZLIB_LIB' does not exits\n"
256	   unless -d $ZLIB_LIB ;
257
258	# check for a well known file
259	die "LIB/INCLUDE directory, '$ZLIB_LIB', doesn't seem to have the zlib source files\n"
260	   unless -e catfile($ZLIB_LIB, 'zlib.h') ;
261
262
263	# write the Makefile
264	print "Building Zlib enabled\n" ;
265    }
266
267    $GZIP_OS_CODE = defined $ENV{'GZIP_OS_CODE'}
268                          ? $ENV{'GZIP_OS_CODE'}
269                          : $Info{'GZIP_OS_CODE'} ;
270
271	die "GZIP_OS_CODE not 'AUTO_DETECT' or a number between 0 and 255\n"
272	   unless uc $GZIP_OS_CODE eq 'AUTO_DETECT'
273                    || ( $GZIP_OS_CODE =~ /^(\d+)$/ && $1 >= 0 && $1 <= 255) ;
274
275    if (uc $GZIP_OS_CODE eq 'AUTO_DETECT')
276    {
277        print "Auto Detect Gzip OS Code..\n" ;
278        $GZIP_OS_CODE = getOSCode() ;
279    }
280
281    my $name = getOSname($GZIP_OS_CODE);
282    print "Setting Gzip OS Code to $GZIP_OS_CODE [$name]\n" ;
283
284    print <<EOM if 0 ;
285    INCLUDE         [$ZLIB_INCLUDE]
286    LIB             [$ZLIB_LIB]
287    GZIP_OS_CODE    [$GZIP_OS_CODE]
288    OLD_ZLIB        [$OLD_ZLIB]
289    BUILD_ZLIB      [$BUILD_ZLIB]
290
291EOM
292
293    print "Looks Good.\n" ;
294
295}
296
297
298
299sub zlib_files
300{
301    my $dir = shift ;
302
303    my @h_files = ();
304    my @c_files = ();
305
306    if (-f catfile($dir, "infback.c")) {
307        # zlib 1.2.0 or greater
308        #
309        @h_files = qw(crc32.h    inffast.h inflate.h  trees.h    zconf.in.h
310    	              zutil.h    deflate.h inffixed.h inftrees.h zconf.h
311    		      zlib.h
312    		 );
313        @c_files = qw(adler32  crc32   infback  inflate  uncompr
314    		      compress deflate inffast  inftrees
315    		      trees    zutil
316    		 );
317    }
318    else {
319        # zlib 1.1.x
320
321        @h_files = qw(deflate.h  infcodes.h inftrees.h zconf.h zutil.h
322    		      infblock.h inffast.h  infutil.h  zlib.h
323    		 );
324        @c_files = qw(adler32  compress crc32    uncompr
325    		      deflate  trees    zutil    inflate infblock
326    		      inftrees infcodes infutil  inffast
327    		 );
328    }
329
330    @h_files = map { catfile($dir, $_)  } @h_files ;
331    my @o_files = map { "$_\$(OBJ_EXT)" } 'Zlib', @c_files;
332    @c_files = map { "$_.c" } 'Zlib', @c_files ;
333
334    foreach my $file (@c_files)
335      { copy(catfile($dir, $file), '.') }
336
337    return (
338        #'H'         =>  [ @h_files ],
339    	'C'         =>  [ @c_files ] ,
340        #'OBJECT'    => qq[ @o_files ],
341        'OBJECT'    => q[ $(O_FILES) ],
342
343
344           ) ;
345}
346
347
348
349use vars qw ( @GZIP_OS_Names  %OSnames) ;
350
351BEGIN
352{
353  @GZIP_OS_Names = (
354    [ ''        => 0,    'MS-DOS'                       ],
355    [ 'amigaos' => 1,    'Amiga'                        ],
356    [ 'VMS'     => 2,    'VMS'                          ],
357    [ ''        => 3,    'Unix/Default'                 ],
358    [ ''        => 4,    'VM/CMS'                       ],
359    [ ''        => 5,    'Atari TOS'                    ],
360    [ 'os2'     => 6,    'HPFS (OS/2, NT)'              ],
361    [ 'MacOS'   => 7,    'Macintosh'                    ],
362    [ ''        => 8,    'Z-System'                     ],
363    [ ''        => 9,    'CP/M'                         ],
364    [ ''        => 10,   'TOPS-20'                      ],
365    [ ''        => 11,   'NTFS (NT)'                    ],
366    [ ''        => 12,   'SMS QDOS'                     ],
367    [ ''        => 13,   'Acorn RISCOS'                 ],
368    [ 'MSWin32' => 14,   'VFAT file system (Win95, NT)' ],
369    [ ''        => 15,   'MVS'                          ],
370    [ 'beos'    => 16,   'BeOS'                         ],
371    [ ''        => 17,   'Tandem/NSK'                   ],
372    [ ''        => 18,   'THEOS'                        ],
373    [ ''        => 255,  'Unknown OS'                   ],
374  );
375
376  %OSnames = map { $$_[1] => $$_[2] }
377             @GZIP_OS_Names ;
378}
379
380sub getOSCode
381{
382    my $default = 3 ; # Unix is the default
383
384    my $uname = $^O;
385
386    for my $h (@GZIP_OS_Names)
387    {
388        my ($pattern, $code, $name) = @$h;
389
390        return $code
391            if $pattern && $uname eq $pattern ;
392    }
393
394    return $default ;
395}
396
397sub getOSname
398{
399    my $code = shift ;
400
401    return $OSnames{$code} || 'Unknown OS' ;
402}
403
404# end of file Makefile.PL
405
406