Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Module / Install / Makefile.pm
1 package Module::Install::Makefile;
2
3 use strict 'vars';
4 use ExtUtils::MakeMaker   ();
5 use Module::Install::Base ();
6
7 use vars qw{$VERSION @ISA $ISCORE};
8 BEGIN {
9         $VERSION = '0.91';
10         @ISA     = 'Module::Install::Base';
11         $ISCORE  = 1;
12 }
13
14 sub Makefile { $_[0] }
15
16 my %seen = ();
17
18 sub prompt {
19         shift;
20
21         # Infinite loop protection
22         my @c = caller();
23         if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
24                 die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
25         }
26
27         # In automated testing, always use defaults
28         if ( $ENV{AUTOMATED_TESTING} and ! $ENV{PERL_MM_USE_DEFAULT} ) {
29                 local $ENV{PERL_MM_USE_DEFAULT} = 1;
30                 goto &ExtUtils::MakeMaker::prompt;
31         } else {
32                 goto &ExtUtils::MakeMaker::prompt;
33         }
34 }
35
36 sub makemaker_args {
37         my $self = shift;
38         my $args = ( $self->{makemaker_args} ||= {} );
39         %$args = ( %$args, @_ );
40         return $args;
41 }
42
43 # For mm args that take multiple space-seperated args,
44 # append an argument to the current list.
45 sub makemaker_append {
46         my $self = sShift;
47         my $name = shift;
48         my $args = $self->makemaker_args;
49         $args->{name} = defined $args->{$name}
50                 ? join( ' ', $args->{name}, @_ )
51                 : join( ' ', @_ );
52 }
53
54 sub build_subdirs {
55         my $self    = shift;
56         my $subdirs = $self->makemaker_args->{DIR} ||= [];
57         for my $subdir (@_) {
58                 push @$subdirs, $subdir;
59         }
60 }
61
62 sub clean_files {
63         my $self  = shift;
64         my $clean = $self->makemaker_args->{clean} ||= {};
65           %$clean = (
66                 %$clean,
67                 FILES => join ' ', grep { length $_ } ($clean->{FILES} || (), @_),
68         );
69 }
70
71 sub realclean_files {
72         my $self      = shift;
73         my $realclean = $self->makemaker_args->{realclean} ||= {};
74           %$realclean = (
75                 %$realclean,
76                 FILES => join ' ', grep { length $_ } ($realclean->{FILES} || (), @_),
77         );
78 }
79
80 sub libs {
81         my $self = shift;
82         my $libs = ref $_[0] ? shift : [ shift ];
83         $self->makemaker_args( LIBS => $libs );
84 }
85
86 sub inc {
87         my $self = shift;
88         $self->makemaker_args( INC => shift );
89 }
90
91 my %test_dir = ();
92
93 sub _wanted_t {
94         /\.t$/ and -f $_ and $test_dir{$File::Find::dir} = 1;
95 }
96
97 sub tests_recursive {
98         my $self = shift;
99         if ( $self->tests ) {
100                 die "tests_recursive will not work if tests are already defined";
101         }
102         my $dir = shift || 't';
103         unless ( -d $dir ) {
104                 die "tests_recursive dir '$dir' does not exist";
105         }
106         %test_dir = ();
107         require File::Find;
108         File::Find::find( \&_wanted_t, $dir );
109         $self->tests( join ' ', map { "$_/*.t" } sort keys %test_dir );
110 }
111
112 sub write {
113         my $self = shift;
114         die "&Makefile->write() takes no arguments\n" if @_;
115
116         # Check the current Perl version
117         my $perl_version = $self->perl_version;
118         if ( $perl_version ) {
119                 eval "use $perl_version; 1"
120                         or die "ERROR: perl: Version $] is installed, "
121                         . "but we need version >= $perl_version";
122         }
123
124         # Make sure we have a new enough MakeMaker
125         require ExtUtils::MakeMaker;
126
127         if ( $perl_version and $self->_cmp($perl_version, '5.006') >= 0 ) {
128                 # MakeMaker can complain about module versions that include
129                 # an underscore, even though its own version may contain one!
130                 # Hence the funny regexp to get rid of it.  See RT #35800
131                 # for details.
132                 $self->build_requires( 'ExtUtils::MakeMaker' => $ExtUtils::MakeMaker::VERSION =~ /^(\d+\.\d+)/ );
133                 $self->configure_requires( 'ExtUtils::MakeMaker' => $ExtUtils::MakeMaker::VERSION =~ /^(\d+\.\d+)/ );
134         } else {
135                 # Allow legacy-compatibility with 5.005 by depending on the
136                 # most recent EU:MM that supported 5.005.
137                 $self->build_requires( 'ExtUtils::MakeMaker' => 6.42 );
138                 $self->configure_requires( 'ExtUtils::MakeMaker' => 6.42 );
139         }
140
141         # Generate the MakeMaker params
142         my $args = $self->makemaker_args;
143         $args->{DISTNAME} = $self->name;
144         $args->{NAME}     = $self->module_name || $self->name;
145         $args->{VERSION}  = $self->version;
146         $args->{NAME}     =~ s/-/::/g;
147         if ( $self->tests ) {
148                 $args->{test} = { TESTS => $self->tests };
149         }
150         if ( $] >= 5.005 ) {
151                 $args->{ABSTRACT} = $self->abstract;
152                 $args->{AUTHOR}   = $self->author;
153         }
154         if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) {
155                 $args->{NO_META} = 1;
156         }
157         if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) {
158                 $args->{SIGN} = 1;
159         }
160         unless ( $self->is_admin ) {
161                 delete $args->{SIGN};
162         }
163
164         # Merge both kinds of requires into prereq_pm
165         my $prereq = ($args->{PREREQ_PM} ||= {});
166         %$prereq = ( %$prereq,
167                 map { @$_ }
168                 map { @$_ }
169                 grep $_,
170                 ($self->configure_requires, $self->build_requires, $self->requires)
171         );
172
173         # Remove any reference to perl, PREREQ_PM doesn't support it
174         delete $args->{PREREQ_PM}->{perl};
175
176         # merge both kinds of requires into prereq_pm
177         my $subdirs = ($args->{DIR} ||= []);
178         if ($self->bundles) {
179                 foreach my $bundle (@{ $self->bundles }) {
180                         my ($file, $dir) = @$bundle;
181                         push @$subdirs, $dir if -d $dir;
182                         delete $prereq->{$file};
183                 }
184         }
185
186         if ( my $perl_version = $self->perl_version ) {
187                 eval "use $perl_version; 1"
188                         or die "ERROR: perl: Version $] is installed, "
189                         . "but we need version >= $perl_version";
190         }
191
192         $args->{INSTALLDIRS} = $self->installdirs;
193
194         my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args;
195
196         my $user_preop = delete $args{dist}->{PREOP};
197         if (my $preop = $self->admin->preop($user_preop)) {
198                 foreach my $key ( keys %$preop ) {
199                         $args{dist}->{$key} = $preop->{$key};
200                 }
201         }
202
203         my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
204         $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
205 }
206
207 sub fix_up_makefile {
208         my $self          = shift;
209         my $makefile_name = shift;
210         my $top_class     = ref($self->_top) || '';
211         my $top_version   = $self->_top->VERSION || '';
212
213         my $preamble = $self->preamble
214                 ? "# Preamble by $top_class $top_version\n"
215                         . $self->preamble
216                 : '';
217         my $postamble = "# Postamble by $top_class $top_version\n"
218                 . ($self->postamble || '');
219
220         local *MAKEFILE;
221         open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
222         my $makefile = do { local $/; <MAKEFILE> };
223         close MAKEFILE or die $!;
224
225         $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
226         $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
227         $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
228         $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
229         $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
230
231         # Module::Install will never be used to build the Core Perl
232         # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
233         # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
234         $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
235         #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
236
237         # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
238         $makefile =~ s/(\"?)-I\$\(PERL_LIB\)\1//g;
239
240         # XXX - This is currently unused; not sure if it breaks other MM-users
241         # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
242
243         open  MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
244         print MAKEFILE  "$preamble$makefile$postamble" or die $!;
245         close MAKEFILE  or die $!;
246
247         1;
248 }
249
250 sub preamble {
251         my ($self, $text) = @_;
252         $self->{preamble} = $text . $self->{preamble} if defined $text;
253         $self->{preamble};
254 }
255
256 sub postamble {
257         my ($self, $text) = @_;
258         $self->{postamble} ||= $self->admin->postamble;
259         $self->{postamble} .= $text if defined $text;
260         $self->{postamble}
261 }
262
263 1;
264
265 __END__
266
267 =pod
268
269 =head1 NAME
270
271 Module::Install::MakeMaker - Extension Rules for ExtUtils::MakeMaker
272
273 =head1 SYNOPSIS
274
275 In your F<Makefile.PL>:
276
277     use inc::Module::Install;
278     WriteMakefile();
279
280 =head1 DESCRIPTION
281
282 This module is a wrapper around B<ExtUtils::MakeMaker>.  It exports
283 two functions: C<prompt> (an alias for C<ExtUtils::MakeMaker::prompt>)
284 and C<WriteMakefile>.
285
286 The C<WriteMakefile> function will pass on keyword/value pair functions
287 to C<ExtUtils::MakeMaker::WriteMakefile>. The required parameters
288 C<NAME> and C<VERSION> (or C<VERSION_FROM>) are not necessary if
289 it can find them unambiguously in your code.
290
291 =head1 CONFIGURATION OPTIONS
292
293 This module also adds some Configuration parameters of its own:
294
295 =head2 NAME
296
297 The NAME parameter is required by B<ExtUtils::MakeMaker>. If you have a
298 single module in your distribution, or if the module name indicated by
299 the current directory exists under F<lib/>, this module will use the
300 guessed package name as the default.
301
302 If this module can't find a default for C<NAME> it will ask you to specify
303 it manually.
304
305 =head2 VERSION
306
307 B<ExtUtils::MakeMaker> requires either the C<VERSION> or C<VERSION_FROM>
308 parameter.  If this module can guess the package's C<NAME>, it will attempt
309 to parse the C<VERSION> from it.
310
311 If this module can't find a default for C<VERSION> it will ask you to
312 specify it manually.
313
314 =head1 MAKE TARGETS
315
316 B<ExtUtils::MakeMaker> provides you with many useful C<make> targets. A
317 C<make> B<target> is the word you specify after C<make>, like C<test>
318 for C<make test>. Some of the more useful targets are:
319
320 =over 4
321
322 =item * all
323
324 This is the default target. When you type C<make> it is the same as
325 entering C<make all>. This target builds all of your code and stages it
326 in the C<blib> directory.
327
328 =item * test
329
330 Run your distribution's test suite.
331
332 =item * install
333
334 Copy the contents of the C<blib> directory into the appropriate
335 directories in your Perl installation.
336
337 =item * dist
338
339 Create a distribution tarball, ready for uploading to CPAN or sharing
340 with a friend.
341
342 =item * clean distclean purge
343
344 Remove the files created by C<perl Makefile.PL> and C<make>.
345
346 =item * help
347
348 Same as typing C<perldoc ExtUtils::MakeMaker>.
349
350 =back
351
352 This module modifies the behaviour of some of these targets, depending
353 on your requirements, and also adds the following targets to your Makefile:
354
355 =over 4
356
357 =item * cpurge
358
359 Just like purge, except that it also deletes the files originally added
360 by this module itself.
361
362 =item * chelp
363
364 Short cut for typing C<perldoc Module::Install>.
365
366 =item * distsign
367
368 Short cut for typing C<cpansign -s>, for B<Module::Signature> users to
369 sign the distribution before release.
370
371 =back
372
373 =head1 SEE ALSO
374
375 L<Module::Install>, L<CPAN::MakeMaker>, L<CPAN::MakeMaker::Philosophy>
376
377 =head1 AUTHORS
378
379 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
380
381 Audrey Tang E<lt>autrijus@autrijus.orgE<gt>
382
383 Brian Ingerson E<lt>INGY@cpan.orgE<gt>
384
385 =head1 COPYRIGHT
386
387 Some parts copyright 2008 - 2009 Adam Kennedy.
388
389 Copyright 2002, 2003, 2004 Audrey Tang and Brian Ingerson.
390
391 This program is free software; you can redistribute it and/or modify it
392 under the same terms as Perl itself.
393
394 =cut