1#! perl -w
2
3use strict;
4use 5.006;
5
6use ExtUtils::MakeMaker;
7use Config ;
8use File::Copy ;
9
10BEGIN {
11  eval { require File::Spec::Functions ; File::Spec::Functions->import( catfile rel2abs ) } ;
12  *catfile = sub { return join( '/', @_ ) } if $@;
13}
14
15require VMS::Filespec if $^O eq 'VMS';
16
17my $BZLIB_BIN ;
18my $BZLIB_LIB ;
19my $BZLIB_INCLUDE ;
20my $BUILD_BZLIB = 0; # override with env BUILD_BZLIB=1
21
22ParseCONFIG() ;
23
24my %Bzip2 = ( pm => catfile( qw( lib Compress Bzip2.pm ) ),
25	      lib => catfile( 'bzlib-src', 'libbz2'.$Config{_a} ) );
26
27my $dlflags = $Config{ccdlflags}; # RT 105096
28if ($Config{ld} ne $Config{cc} and $Config{ld} =~ m{^(.*/)?\w*ld\w*$}) {
29  my @flags;
30  for (split m/\s+/ => $dlflags) {
31    s/^-Wl,(-+[-\w]+),/$1 / or s/^-Wl,//;
32    push @flags, $_;
33  }
34  $dlflags = join " " => @flags;
35}
36
37# See lib/ExtUtils/MakeMaker.pm for details of how to influence
38# the contents of the Makefile that is written.
39WriteMakefile(
40  NAME              => 'Compress::Bzip2',
41  VERSION_FROM      => $Bzip2{pm},
42  PREREQ_PM         => {
43    'constant'   => 1.04, # for constant => {}
44    'Test::More' => 0,
45    'File::Spec' => 0,
46    'File::Copy' => 0,
47    'Config'     => 0,
48    'Carp'       => 0,
49    'Getopt::Std'=> 0,
50    'Fcntl'      => 0,
51  },
52  XS	 	=> { 'Bzip2.xs'    => 'Bzip2.c' },
53  ($] >= 5.005 ?
54   (ABSTRACT_FROM  => $Bzip2{pm},
55    AUTHOR         => 'Rob Janes <arjay@cpan.org>') : ()),
56  LIBS             => $BUILD_BZLIB ? [] : [ $BZLIB_LIB ? "-L$BZLIB_LIB -lbz2" : '-lbz2' ],
57  # ccdlflags needed for -R [cpan #68572, #105096]
58  LDDLFLAGS         => $Config{lddlflags} . " " . $dlflags,
59  INC               => $BUILD_BZLIB ? '-Ibzlib-src' : $BZLIB_INCLUDE ? "-I$BZLIB_INCLUDE" : '',
60  clean             => {
61    FILES=>
62      join(' ',
63	   map { catfile( split( /\//, $_ ) ) }
64	   qw( const-c.inc const-xs.inc junk* bztest show_bzversion t/???-tmp-* ))
65  },
66  depend            => { 'Makefile'	  => 'config.in' },
67  NORECURS          => $BUILD_BZLIB ? 0 : 1,
68  ($BUILD_BZLIB ? ( MYEXTLIB       => $Bzip2{lib} ) : ()),
69  ($ExtUtils::MakeMaker::VERSION gt '6.46' ?
70   ('LICENSE'     => 'perl',
71    'META_MERGE'  =>
72    {"recommends" =>
73     {
74       'Compress::Zlib'       => '1.19',
75       'IO::Compress::Bzip2'  => '2.060',
76       'Compress::Raw::Bzip2' => '2.060',
77     },
78     resources =>
79     {
80       license     => 'http://dev.perl.org/licenses/',
81       repository  => 'https://github.com/rurban/Compress-Bzip2',
82     },
83    }
84   ) : ()),
85  SIGN  => 1,
86);
87
88if  (eval {require ExtUtils::Constant; 1}) {
89  # If you edit these definitions to change the constants used by this module,
90  # you will need to use the generated const-c.inc and const-xs.inc
91  # files to replace their "fallback" counterparts before distributing your
92  # changes.
93  my @names = (qw(BZ_CONFIG_ERROR BZ_DATA_ERROR BZ_DATA_ERROR_MAGIC
94		 BZ_FINISH BZ_FINISH_OK BZ_FLUSH BZ_FLUSH_OK BZ_IO_ERROR
95		 BZ_MAX_UNUSED BZ_MEM_ERROR BZ_OK BZ_OUTBUFF_FULL
96		 BZ_PARAM_ERROR BZ_RUN BZ_RUN_OK BZ_SEQUENCE_ERROR
97		 BZ_STREAM_END BZ_UNEXPECTED_EOF));
98  ExtUtils::Constant::WriteConstants(
99                                     NAME         => 'Compress::Bzip2',
100                                     NAMES        => \@names,
101                                     DEFAULT_TYPE => 'IV',
102                                     C_FILE       => 'const-c.inc',
103                                     XS_FILE      => 'const-xs.inc',
104                                  );
105
106}
107else {
108  use File::Copy;
109  use File::Spec;
110  foreach my $file ('const-c.inc', 'const-xs.inc') {
111    my $fallback = catfile('fallback', $file);
112    copy ($fallback, $file) or die "Can't copy $fallback to $file: $!";
113  }
114}
115
116sub MY::postamble {
117  !$BUILD_BZLIB ? '' :
118'
119$(MYEXTLIB): '.catfile( qw( bzlib-src Makefile ) ).'
120	cd bzlib-src && $(MAKE) $(PASSTHRU)
121';
122}
123
124sub ParseCONFIG {
125  my ($k, $v) ;
126  my @badkey = () ;
127  my %Info = () ;
128  my @Options = qw( BZLIB_INCLUDE BZLIB_LIB BUILD_BZLIB BZLIB_BIN ) ;
129  my %ValidOption = map {$_, 1} @Options ;
130  my %Parsed = %ValidOption ;
131  my $CONFIG = 'config.in' ;
132
133  print "Parsing $CONFIG...\n" ;
134
135  if (!open(F, "<$CONFIG")) {
136    warn "warning: failed to open $CONFIG: $!\n";
137  }
138  else {
139    while (<F>) {
140      chomp;
141      s/#.*$//;
142      next if !/\S/;
143
144      ($k, $v) = split(/\s*=\s*/, $_, 2) ;
145      $k = uc $k ;
146
147      if ($ValidOption{$k}) {
148	delete $Parsed{$k} ;
149	$Info{$k} = $v ;
150      }
151      else {
152	push(@badkey, $k) ;
153      }
154    }
155    close F ;
156  }
157
158  print "Unknown keys in $CONFIG ignored [@badkey]\n"
159      if @badkey ;
160
161  $BZLIB_INCLUDE = $ENV{'BZLIB_INCLUDE'} || $Info{'BZLIB_INCLUDE'} ;
162  $BZLIB_LIB = $ENV{'BZLIB_LIB'} || $Info{'BZLIB_LIB'} ;
163  $BZLIB_BIN = $ENV{'BZLIB_BIN'} || $Info{'BZLIB_BIN'} ;
164
165  if ($^O eq 'VMS') {
166    $BZLIB_INCLUDE = VMS::Filespec::vmspath($BZLIB_INCLUDE);
167    $BZLIB_LIB = VMS::Filespec::vmspath($BZLIB_LIB);
168    $BZLIB_BIN = VMS::Filespec::vmspath($BZLIB_BIN);
169  }
170
171  my $x = defined($ENV{BUILD_BZLIB}) ? $ENV{BUILD_BZLIB} : $Info{BUILD_BZLIB};
172  $x = 'Test' if !defined($x);
173
174  if ( $x =~ /^yes|on|true|1$/i ) {
175    $BUILD_BZLIB = 1;
176
177    print "Building internal libbz2 enabled\n" ;
178  }
179  elsif ( $x =~ /^test$/i ) {
180    undef $BUILD_BZLIB;
181
182    ## prefix libpth locincpth
183    my $command = $Config{cc} .
184	' '. $Config{ccflags} .
185	( $BZLIB_INCLUDE ? " -I$BZLIB_INCLUDE" : '' ) .
186	' '. $Config{ccdlflags} .
187	' '. $Config{ldflags} .
188	' -o show_bzversion show_bzversion.c' .
189	( $BZLIB_LIB ? " -L$BZLIB_LIB" : '' ) .
190	' -lbz2';
191
192    #print "command $command\n";
193    if ( !system( $command ) ) {
194      if ( -x 'show_bzversion' && -s 'show_bzversion' ) {
195	my $version = `./show_bzversion`;
196	if ( $version ) {
197	  chomp $version;
198	  $BUILD_BZLIB = 0;
199	  print "found bzip2 $version ".($BZLIB_LIB ? "in $BZLIB_LIB" : 'installed')."\n";
200	}
201	else {
202	  $BUILD_BZLIB = 1;
203	  print "compile command '$command' failed\n";
204	  print "system bzip2 not useable, building internal libbz2\n";
205	}
206      }
207      else {
208	$BUILD_BZLIB = 1;
209	print "compile command '$command' failed\n";
210	print "system bzip2 not useable, building internal libbz2\n";
211      }
212    }
213    else {
214      $BUILD_BZLIB = 1;
215      print "compile command '$command' failed\n";
216      print "system bzip2 not found, building internal libbz2\n";
217    }
218  }
219
220  print <<EOM if 0 ;
221INCLUDE	[$BZLIB_INCLUDE]
222LIB	[$BZLIB_LIB]
223BIN	[$BZLIB_BIN]
224
225EOM
226;
227
228  print "Looks Good.\n" ;
229
230}
231