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