98779db0da5c829fa4cce730bed9b77fddb5b838
[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.91';
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, always use defaults
29         if ( $ENV{AUTOMATED_TESTING} 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 sub makemaker_args {
38         my $self = shift;
39         my $args = ( $self->{makemaker_args} ||= {} );
40         %$args = ( %$args, @_ );
41         return $args;
42 }
43
44 # For mm args that take multiple space-seperated args,
45 # append an argument to the current list.
46 sub makemaker_append {
47         my $self = sShift;
48         my $name = shift;
49         my $args = $self->makemaker_args;
50         $args->{name} = defined $args->{$name}
51                 ? join( ' ', $args->{name}, @_ )
52                 : join( ' ', @_ );
53 }
54
55 sub build_subdirs {
56         my $self    = shift;
57         my $subdirs = $self->makemaker_args->{DIR} ||= [];
58         for my $subdir (@_) {
59                 push @$subdirs, $subdir;
60         }
61 }
62
63 sub clean_files {
64         my $self  = shift;
65         my $clean = $self->makemaker_args->{clean} ||= {};
66           %$clean = (
67                 %$clean,
68                 FILES => join ' ', grep { length $_ } ($clean->{FILES} || (), @_),
69         );
70 }
71
72 sub realclean_files {
73         my $self      = shift;
74         my $realclean = $self->makemaker_args->{realclean} ||= {};
75           %$realclean = (
76                 %$realclean,
77                 FILES => join ' ', grep { length $_ } ($realclean->{FILES} || (), @_),
78         );
79 }
80
81 sub libs {
82         my $self = shift;
83         my $libs = ref $_[0] ? shift : [ shift ];
84         $self->makemaker_args( LIBS => $libs );
85 }
86
87 sub inc {
88         my $self = shift;
89         $self->makemaker_args( INC => shift );
90 }
91
92 my %test_dir = ();
93
94 sub _wanted_t {
95         /\.t$/ and -f $_ and $test_dir{$File::Find::dir} = 1;
96 }
97
98 sub tests_recursive {
99         my $self = shift;
100         if ( $self->tests ) {
101                 die "tests_recursive will not work if tests are already defined";
102         }
103         my $dir = shift || 't';
104         unless ( -d $dir ) {
105                 die "tests_recursive dir '$dir' does not exist";
106         }
107         %test_dir = ();
108         require File::Find;
109         File::Find::find( \&_wanted_t, $dir );
110         $self->tests( join ' ', map { "$_/*.t" } sort keys %test_dir );
111 }
112
113 sub write {
114         my $self = shift;
115         die "&Makefile->write() takes no arguments\n" if @_;
116
117         # Check the current Perl version
118         my $perl_version = $self->perl_version;
119         if ( $perl_version ) {
120                 eval "use $perl_version; 1"
121                         or die "ERROR: perl: Version $] is installed, "
122                         . "but we need version >= $perl_version";
123         }
124
125         # Make sure we have a new enough MakeMaker
126         require ExtUtils::MakeMaker;
127
128         if ( $perl_version and $self->_cmp($perl_version, '5.006') >= 0 ) {
129                 # MakeMaker can complain about module versions that include
130                 # an underscore, even though its own version may contain one!
131                 # Hence the funny regexp to get rid of it.  See RT #35800
132                 # for details.
133                 $self->build_requires( 'ExtUtils::MakeMaker' => $ExtUtils::MakeMaker::VERSION =~ /^(\d+\.\d+)/ );
134                 $self->configure_requires( 'ExtUtils::MakeMaker' => $ExtUtils::MakeMaker::VERSION =~ /^(\d+\.\d+)/ );
135         } else {
136                 # Allow legacy-compatibility with 5.005 by depending on the
137                 # most recent EU:MM that supported 5.005.
138                 $self->build_requires( 'ExtUtils::MakeMaker' => 6.42 );
139                 $self->configure_requires( 'ExtUtils::MakeMaker' => 6.42 );
140         }
141
142         # Generate the MakeMaker params
143         my $args = $self->makemaker_args;
144         $args->{DISTNAME} = $self->name;
145         $args->{NAME}     = $self->module_name || $self->name;
146         $args->{VERSION}  = $self->version;
147         $args->{NAME}     =~ s/-/::/g;
148         if ( $self->tests ) {
149                 $args->{test} = { TESTS => $self->tests };
150         }
151         if ( $] >= 5.005 ) {
152                 $args->{ABSTRACT} = $self->abstract;
153                 $args->{AUTHOR}   = $self->author;
154         }
155         if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) {
156                 $args->{NO_META} = 1;
157         }
158         if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) {
159                 $args->{SIGN} = 1;
160         }
161         unless ( $self->is_admin ) {
162                 delete $args->{SIGN};
163         }
164
165         # Merge both kinds of requires into prereq_pm
166         my $prereq = ($args->{PREREQ_PM} ||= {});
167         %$prereq = ( %$prereq,
168                 map { @$_ }
169                 map { @$_ }
170                 grep $_,
171                 ($self->configure_requires, $self->build_requires, $self->requires)
172         );
173
174         # Remove any reference to perl, PREREQ_PM doesn't support it
175         delete $args->{PREREQ_PM}->{perl};
176
177         # merge both kinds of requires into prereq_pm
178         my $subdirs = ($args->{DIR} ||= []);
179         if ($self->bundles) {
180                 foreach my $bundle (@{ $self->bundles }) {
181                         my ($file, $dir) = @$bundle;
182                         push @$subdirs, $dir if -d $dir;
183                         delete $prereq->{$file};
184                 }
185         }
186
187         if ( my $perl_version = $self->perl_version ) {
188                 eval "use $perl_version; 1"
189                         or die "ERROR: perl: Version $] is installed, "
190                         . "but we need version >= $perl_version";
191         }
192
193         $args->{INSTALLDIRS} = $self->installdirs;
194
195         my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args;
196
197         my $user_preop = delete $args{dist}->{PREOP};
198         if (my $preop = $self->admin->preop($user_preop)) {
199                 foreach my $key ( keys %$preop ) {
200                         $args{dist}->{$key} = $preop->{$key};
201                 }
202         }
203
204         my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
205         $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
206 }
207
208 sub fix_up_makefile {
209         my $self          = shift;
210         my $makefile_name = shift;
211         my $top_class     = ref($self->_top) || '';
212         my $top_version   = $self->_top->VERSION || '';
213
214         my $preamble = $self->preamble
215                 ? "# Preamble by $top_class $top_version\n"
216                         . $self->preamble
217                 : '';
218         my $postamble = "# Postamble by $top_class $top_version\n"
219                 . ($self->postamble || '');
220
221         local *MAKEFILE;
222         open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
223         my $makefile = do { local $/; <MAKEFILE> };
224         close MAKEFILE or die $!;
225
226         $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
227         $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
228         $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
229         $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
230         $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
231
232         # Module::Install will never be used to build the Core Perl
233         # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
234         # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
235         $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
236         #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
237
238         # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
239         $makefile =~ s/(\"?)-I\$\(PERL_LIB\)\1//g;
240
241         # XXX - This is currently unused; not sure if it breaks other MM-users
242         # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
243
244         open  MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
245         print MAKEFILE  "$preamble$makefile$postamble" or die $!;
246         close MAKEFILE  or die $!;
247
248         1;
249 }
250
251 sub preamble {
252         my ($self, $text) = @_;
253         $self->{preamble} = $text . $self->{preamble} if defined $text;
254         $self->{preamble};
255 }
256
257 sub postamble {
258         my ($self, $text) = @_;
259         $self->{postamble} ||= $self->admin->postamble;
260         $self->{postamble} .= $text if defined $text;
261         $self->{postamble}
262 }
263
264 1;
265
266 __END__
267
268 #line 394