Imported Upstream version 0.16
[libwww-opensearch-perl.git] / inc / Module / Install / Makefile.pm
1 #line 1
2 package Module::Install::Makefile;
3
4 use strict 'vars';
5 use ExtUtils::MakeMaker   ();
6 use Module::Install::Base ();
7
8 use vars qw{$VERSION @ISA $ISCORE};
9 BEGIN {
10         $VERSION = '0.97';
11         @ISA     = 'Module::Install::Base';
12         $ISCORE  = 1;
13 }
14
15 sub Makefile { $_[0] }
16
17 my %seen = ();
18
19 sub prompt {
20         shift;
21
22         # Infinite loop protection
23         my @c = caller();
24         if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
25                 die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
26         }
27
28         # In automated testing or non-interactive session, always use defaults
29         if ( ($ENV{AUTOMATED_TESTING} or -! -t STDIN) and ! $ENV{PERL_MM_USE_DEFAULT} ) {
30                 local $ENV{PERL_MM_USE_DEFAULT} = 1;
31                 goto &ExtUtils::MakeMaker::prompt;
32         } else {
33                 goto &ExtUtils::MakeMaker::prompt;
34         }
35 }
36
37 # Store a cleaned up version of the MakeMaker version,
38 # since we need to behave differently in a variety of
39 # ways based on the MM version.
40 my $makemaker = eval $ExtUtils::MakeMaker::VERSION;
41
42 # If we are passed a param, do a "newer than" comparison.
43 # Otherwise, just return the MakeMaker version.
44 sub makemaker {
45         ( @_ < 2 or $makemaker >= eval($_[1]) ) ? $makemaker : 0
46 }
47
48 # Ripped from ExtUtils::MakeMaker 6.56, and slightly modified
49 # as we only need to know here whether the attribute is an array
50 # or a hash or something else (which may or may not be appendable).
51 my %makemaker_argtype = (
52  C                  => 'ARRAY',
53  CONFIG             => 'ARRAY',
54 # CONFIGURE          => 'CODE', # ignore
55  DIR                => 'ARRAY',
56  DL_FUNCS           => 'HASH',
57  DL_VARS            => 'ARRAY',
58  EXCLUDE_EXT        => 'ARRAY',
59  EXE_FILES          => 'ARRAY',
60  FUNCLIST           => 'ARRAY',
61  H                  => 'ARRAY',
62  IMPORTS            => 'HASH',
63  INCLUDE_EXT        => 'ARRAY',
64  LIBS               => 'ARRAY', # ignore ''
65  MAN1PODS           => 'HASH',
66  MAN3PODS           => 'HASH',
67  META_ADD           => 'HASH',
68  META_MERGE         => 'HASH',
69  PL_FILES           => 'HASH',
70  PM                 => 'HASH',
71  PMLIBDIRS          => 'ARRAY',
72  PMLIBPARENTDIRS    => 'ARRAY',
73  PREREQ_PM          => 'HASH',
74  CONFIGURE_REQUIRES => 'HASH',
75  SKIP               => 'ARRAY',
76  TYPEMAPS           => 'ARRAY',
77  XS                 => 'HASH',
78 # VERSION            => ['version',''],  # ignore
79 # _KEEP_AFTER_FLUSH  => '',
80
81  clean      => 'HASH',
82  depend     => 'HASH',
83  dist       => 'HASH',
84  dynamic_lib=> 'HASH',
85  linkext    => 'HASH',
86  macro      => 'HASH',
87  postamble  => 'HASH',
88  realclean  => 'HASH',
89  test       => 'HASH',
90  tool_autosplit => 'HASH',
91
92  # special cases where you can use makemaker_append
93  CCFLAGS   => 'APPENDABLE',
94  DEFINE    => 'APPENDABLE',
95  INC       => 'APPENDABLE',
96  LDDLFLAGS => 'APPENDABLE',
97  LDFROM    => 'APPENDABLE',
98 );
99
100 sub makemaker_args {
101         my ($self, %new_args) = @_;
102         my $args = ( $self->{makemaker_args} ||= {} );
103         foreach my $key (keys %new_args) {
104                 if ($makemaker_argtype{$key}) {
105                         if ($makemaker_argtype{$key} eq 'ARRAY') {
106                                 $args->{$key} = [] unless defined $args->{$key};
107                                 unless (ref $args->{$key} eq 'ARRAY') {
108                                         $args->{$key} = [$args->{$key}]
109                                 }
110                                 push @{$args->{$key}},
111                                         ref $new_args{$key} eq 'ARRAY'
112                                                 ? @{$new_args{$key}}
113                                                 : $new_args{$key};
114                         }
115                         elsif ($makemaker_argtype{$key} eq 'HASH') {
116                                 $args->{$key} = {} unless defined $args->{$key};
117                                 foreach my $skey (keys %{ $new_args{$key} }) {
118                                         $args->{$key}{$skey} = $new_args{$key}{$skey};
119                                 }
120                         }
121                         elsif ($makemaker_argtype{$key} eq 'APPENDABLE') {
122                                 $self->makemaker_append($key => $new_args{$key});
123                         }
124                 }
125                 else {
126                         if (defined $args->{$key}) {
127                                 warn qq{MakeMaker attribute "$key" is overriden; use "makemaker_append" to append values\n};
128                         }
129                         $args->{$key} = $new_args{$key};
130                 }
131         }
132         return $args;
133 }
134
135 # For mm args that take multiple space-seperated args,
136 # append an argument to the current list.
137 sub makemaker_append {
138         my $self = shift;
139         my $name = shift;
140         my $args = $self->makemaker_args;
141         $args->{$name} = defined $args->{$name}
142                 ? join( ' ', $args->{$name}, @_ )
143                 : join( ' ', @_ );
144 }
145
146 sub build_subdirs {
147         my $self    = shift;
148         my $subdirs = $self->makemaker_args->{DIR} ||= [];
149         for my $subdir (@_) {
150                 push @$subdirs, $subdir;
151         }
152 }
153
154 sub clean_files {
155         my $self  = shift;
156         my $clean = $self->makemaker_args->{clean} ||= {};
157           %$clean = (
158                 %$clean,
159                 FILES => join ' ', grep { length $_ } ($clean->{FILES} || (), @_),
160         );
161 }
162
163 sub realclean_files {
164         my $self      = shift;
165         my $realclean = $self->makemaker_args->{realclean} ||= {};
166           %$realclean = (
167                 %$realclean,
168                 FILES => join ' ', grep { length $_ } ($realclean->{FILES} || (), @_),
169         );
170 }
171
172 sub libs {
173         my $self = shift;
174         my $libs = ref $_[0] ? shift : [ shift ];
175         $self->makemaker_args( LIBS => $libs );
176 }
177
178 sub inc {
179         my $self = shift;
180         $self->makemaker_args( INC => shift );
181 }
182
183 sub _wanted_t {
184 }
185
186 sub tests_recursive {
187         my $self = shift;
188         my $dir = shift || 't';
189         unless ( -d $dir ) {
190                 die "tests_recursive dir '$dir' does not exist";
191         }
192         my %tests = map { $_ => 1 } split / /, ($self->tests || '');
193         require File::Find;
194         File::Find::find(
195         sub { /\.t$/ and -f $_ and $tests{"$File::Find::dir/*.t"} = 1 },
196         $dir
197     );
198         $self->tests( join ' ', sort keys %tests );
199 }
200
201 sub write {
202         my $self = shift;
203         die "&Makefile->write() takes no arguments\n" if @_;
204
205         # Check the current Perl version
206         my $perl_version = $self->perl_version;
207         if ( $perl_version ) {
208                 eval "use $perl_version; 1"
209                         or die "ERROR: perl: Version $] is installed, "
210                         . "but we need version >= $perl_version";
211         }
212
213         # Make sure we have a new enough MakeMaker
214         require ExtUtils::MakeMaker;
215
216         if ( $perl_version and $self->_cmp($perl_version, '5.006') >= 0 ) {
217                 # MakeMaker can complain about module versions that include
218                 # an underscore, even though its own version may contain one!
219                 # Hence the funny regexp to get rid of it.  See RT #35800
220                 # for details.
221                 my $v = $ExtUtils::MakeMaker::VERSION =~ /^(\d+\.\d+)/;
222                 $self->build_requires(     'ExtUtils::MakeMaker' => $v );
223                 $self->configure_requires( 'ExtUtils::MakeMaker' => $v );
224         } else {
225                 # Allow legacy-compatibility with 5.005 by depending on the
226                 # most recent EU:MM that supported 5.005.
227                 $self->build_requires(     'ExtUtils::MakeMaker' => 6.42 );
228                 $self->configure_requires( 'ExtUtils::MakeMaker' => 6.42 );
229         }
230
231         # Generate the MakeMaker params
232         my $args = $self->makemaker_args;
233         $args->{DISTNAME} = $self->name;
234         $args->{NAME}     = $self->module_name || $self->name;
235         $args->{NAME}     =~ s/-/::/g;
236         $args->{VERSION}  = $self->version or die <<'EOT';
237 ERROR: Can't determine distribution version. Please specify it
238 explicitly via 'version' in Makefile.PL, or set a valid $VERSION
239 in a module, and provide its file path via 'version_from' (or
240 'all_from' if you prefer) in Makefile.PL.
241 EOT
242
243         $DB::single = 1;
244         if ( $self->tests ) {
245                 my @tests = split ' ', $self->tests;
246                 my %seen;
247                 $args->{test} = {
248                         TESTS => (join ' ', grep {!$seen{$_}++} @tests),
249                 };
250     } elsif ( $Module::Install::ExtraTests::use_extratests ) {
251         # Module::Install::ExtraTests doesn't set $self->tests and does its own tests via harness.
252         # So, just ignore our xt tests here.
253         } elsif ( -d 'xt' and ($Module::Install::AUTHOR or $ENV{RELEASE_TESTING}) ) {
254                 $args->{test} = {
255                         TESTS => join( ' ', map { "$_/*.t" } grep { -d $_ } qw{ t xt } ),
256                 };
257         }
258         if ( $] >= 5.005 ) {
259                 $args->{ABSTRACT} = $self->abstract;
260                 $args->{AUTHOR}   = join ', ', @{$self->author || []};
261         }
262         if ( $self->makemaker(6.10) ) {
263                 $args->{NO_META}   = 1;
264                 #$args->{NO_MYMETA} = 1;
265         }
266         if ( $self->makemaker(6.17) and $self->sign ) {
267                 $args->{SIGN} = 1;
268         }
269         unless ( $self->is_admin ) {
270                 delete $args->{SIGN};
271         }
272         if ( $self->makemaker(6.31) and $self->license ) {
273                 $args->{LICENSE} = $self->license;
274         }
275
276         my $prereq = ($args->{PREREQ_PM} ||= {});
277         %$prereq = ( %$prereq,
278                 map { @$_ } # flatten [module => version]
279                 map { @$_ }
280                 grep $_,
281                 ($self->requires)
282         );
283
284         # Remove any reference to perl, PREREQ_PM doesn't support it
285         delete $args->{PREREQ_PM}->{perl};
286
287         # Merge both kinds of requires into BUILD_REQUIRES
288         my $build_prereq = ($args->{BUILD_REQUIRES} ||= {});
289         %$build_prereq = ( %$build_prereq,
290                 map { @$_ } # flatten [module => version]
291                 map { @$_ }
292                 grep $_,
293                 ($self->configure_requires, $self->build_requires)
294         );
295
296         # Remove any reference to perl, BUILD_REQUIRES doesn't support it
297         delete $args->{BUILD_REQUIRES}->{perl};
298
299         # Delete bundled dists from prereq_pm, add it to Makefile DIR
300         my $subdirs = ($args->{DIR} || []);
301         if ($self->bundles) {
302                 my %processed;
303                 foreach my $bundle (@{ $self->bundles }) {
304                         my ($mod_name, $dist_dir) = @$bundle;
305                         delete $prereq->{$mod_name};
306                         $dist_dir = File::Basename::basename($dist_dir); # dir for building this module
307                         if (not exists $processed{$dist_dir}) {
308                                 if (-d $dist_dir) {
309                                         # List as sub-directory to be processed by make
310                                         push @$subdirs, $dist_dir;
311                                 }
312                                 # Else do nothing: the module is already present on the system
313                                 $processed{$dist_dir} = undef;
314                         }
315                 }
316         }
317
318         unless ( $self->makemaker('6.55_03') ) {
319                 %$prereq = (%$prereq,%$build_prereq);
320                 delete $args->{BUILD_REQUIRES};
321         }
322
323         if ( my $perl_version = $self->perl_version ) {
324                 eval "use $perl_version; 1"
325                         or die "ERROR: perl: Version $] is installed, "
326                         . "but we need version >= $perl_version";
327
328                 if ( $self->makemaker(6.48) ) {
329                         $args->{MIN_PERL_VERSION} = $perl_version;
330                 }
331         }
332
333         if ($self->installdirs) {
334                 warn qq{old INSTALLDIRS (probably set by makemaker_args) is overriden by installdirs\n} if $args->{INSTALLDIRS};
335                 $args->{INSTALLDIRS} = $self->installdirs;
336         }
337
338         my %args = map {
339                 ( $_ => $args->{$_} ) } grep {defined($args->{$_} )
340         } keys %$args;
341
342         my $user_preop = delete $args{dist}->{PREOP};
343         if ( my $preop = $self->admin->preop($user_preop) ) {
344                 foreach my $key ( keys %$preop ) {
345                         $args{dist}->{$key} = $preop->{$key};
346                 }
347         }
348
349         my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
350         $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
351 }
352
353 sub fix_up_makefile {
354         my $self          = shift;
355         my $makefile_name = shift;
356         my $top_class     = ref($self->_top) || '';
357         my $top_version   = $self->_top->VERSION || '';
358
359         my $preamble = $self->preamble
360                 ? "# Preamble by $top_class $top_version\n"
361                         . $self->preamble
362                 : '';
363         my $postamble = "# Postamble by $top_class $top_version\n"
364                 . ($self->postamble || '');
365
366         local *MAKEFILE;
367         open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
368         my $makefile = do { local $/; <MAKEFILE> };
369         close MAKEFILE or die $!;
370
371         $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
372         $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
373         $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
374         $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
375         $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
376
377         # Module::Install will never be used to build the Core Perl
378         # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
379         # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
380         $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
381         #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
382
383         # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
384         $makefile =~ s/(\"?)-I\$\(PERL_LIB\)\1//g;
385
386         # XXX - This is currently unused; not sure if it breaks other MM-users
387         # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
388
389         open  MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
390         print MAKEFILE  "$preamble$makefile$postamble" or die $!;
391         close MAKEFILE  or die $!;
392
393         1;
394 }
395
396 sub preamble {
397         my ($self, $text) = @_;
398         $self->{preamble} = $text . $self->{preamble} if defined $text;
399         $self->{preamble};
400 }
401
402 sub postamble {
403         my ($self, $text) = @_;
404         $self->{postamble} ||= $self->admin->postamble;
405         $self->{postamble} .= $text if defined $text;
406         $self->{postamble}
407 }
408
409 1;
410
411 __END__
412
413 #line 539