2 package Module::Install::Makefile;
5 use ExtUtils::MakeMaker ();
6 use Module::Install::Base ();
7 use Fcntl qw/:flock :seek/;
9 use vars qw{$VERSION @ISA $ISCORE};
12 @ISA = 'Module::Install::Base';
16 sub Makefile { $_[0] }
23 # Infinite loop protection
25 if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) {
26 die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])";
29 # In automated testing or non-interactive session, always use defaults
30 if ( ($ENV{AUTOMATED_TESTING} or -! -t STDIN) and ! $ENV{PERL_MM_USE_DEFAULT} ) {
31 local $ENV{PERL_MM_USE_DEFAULT} = 1;
32 goto &ExtUtils::MakeMaker::prompt;
34 goto &ExtUtils::MakeMaker::prompt;
38 # Store a cleaned up version of the MakeMaker version,
39 # since we need to behave differently in a variety of
40 # ways based on the MM version.
41 my $makemaker = eval $ExtUtils::MakeMaker::VERSION;
43 # If we are passed a param, do a "newer than" comparison.
44 # Otherwise, just return the MakeMaker version.
46 ( @_ < 2 or $makemaker >= eval($_[1]) ) ? $makemaker : 0
49 # Ripped from ExtUtils::MakeMaker 6.56, and slightly modified
50 # as we only need to know here whether the attribute is an array
51 # or a hash or something else (which may or may not be appendable).
52 my %makemaker_argtype = (
55 # CONFIGURE => 'CODE', # ignore
59 EXCLUDE_EXT => 'ARRAY',
64 INCLUDE_EXT => 'ARRAY',
65 LIBS => 'ARRAY', # ignore ''
73 PMLIBPARENTDIRS => 'ARRAY',
75 CONFIGURE_REQUIRES => 'HASH',
79 # VERSION => ['version',''], # ignore
80 # _KEEP_AFTER_FLUSH => '',
91 tool_autosplit => 'HASH',
93 # special cases where you can use makemaker_append
94 CCFLAGS => 'APPENDABLE',
95 DEFINE => 'APPENDABLE',
97 LDDLFLAGS => 'APPENDABLE',
98 LDFROM => 'APPENDABLE',
102 my ($self, %new_args) = @_;
103 my $args = ( $self->{makemaker_args} ||= {} );
104 foreach my $key (keys %new_args) {
105 if ($makemaker_argtype{$key}) {
106 if ($makemaker_argtype{$key} eq 'ARRAY') {
107 $args->{$key} = [] unless defined $args->{$key};
108 unless (ref $args->{$key} eq 'ARRAY') {
109 $args->{$key} = [$args->{$key}]
111 push @{$args->{$key}},
112 ref $new_args{$key} eq 'ARRAY'
116 elsif ($makemaker_argtype{$key} eq 'HASH') {
117 $args->{$key} = {} unless defined $args->{$key};
118 foreach my $skey (keys %{ $new_args{$key} }) {
119 $args->{$key}{$skey} = $new_args{$key}{$skey};
122 elsif ($makemaker_argtype{$key} eq 'APPENDABLE') {
123 $self->makemaker_append($key => $new_args{$key});
127 if (defined $args->{$key}) {
128 warn qq{MakeMaker attribute "$key" is overriden; use "makemaker_append" to append values\n};
130 $args->{$key} = $new_args{$key};
136 # For mm args that take multiple space-separated args,
137 # append an argument to the current list.
138 sub makemaker_append {
141 my $args = $self->makemaker_args;
142 $args->{$name} = defined $args->{$name}
143 ? join( ' ', $args->{$name}, @_ )
149 my $subdirs = $self->makemaker_args->{DIR} ||= [];
150 for my $subdir (@_) {
151 push @$subdirs, $subdir;
157 my $clean = $self->makemaker_args->{clean} ||= {};
160 FILES => join ' ', grep { length $_ } ($clean->{FILES} || (), @_),
164 sub realclean_files {
166 my $realclean = $self->makemaker_args->{realclean} ||= {};
169 FILES => join ' ', grep { length $_ } ($realclean->{FILES} || (), @_),
175 my $libs = ref $_[0] ? shift : [ shift ];
176 $self->makemaker_args( LIBS => $libs );
181 $self->makemaker_args( INC => shift );
187 sub tests_recursive {
189 my $dir = shift || 't';
191 die "tests_recursive dir '$dir' does not exist";
193 my %tests = map { $_ => 1 } split / /, ($self->tests || '');
196 sub { /\.t$/ and -f $_ and $tests{"$File::Find::dir/*.t"} = 1 },
199 $self->tests( join ' ', sort keys %tests );
204 die "&Makefile->write() takes no arguments\n" if @_;
206 # Check the current Perl version
207 my $perl_version = $self->perl_version;
208 if ( $perl_version ) {
209 eval "use $perl_version; 1"
210 or die "ERROR: perl: Version $] is installed, "
211 . "but we need version >= $perl_version";
214 # Make sure we have a new enough MakeMaker
215 require ExtUtils::MakeMaker;
217 if ( $perl_version and $self->_cmp($perl_version, '5.006') >= 0 ) {
218 # This previous attempted to inherit the version of
219 # ExtUtils::MakeMaker in use by the module author, but this
220 # was found to be untenable as some authors build releases
221 # using future dev versions of EU:MM that nobody else has.
222 # Instead, #toolchain suggests we use 6.59 which is the most
223 # stable version on CPAN at time of writing and is, to quote
224 # ribasushi, "not terminally fucked, > and tested enough".
225 # TODO: We will now need to maintain this over time to push
226 # the version up as new versions are released.
227 $self->build_requires( 'ExtUtils::MakeMaker' => 6.59 );
228 $self->configure_requires( 'ExtUtils::MakeMaker' => 6.59 );
230 # Allow legacy-compatibility with 5.005 by depending on the
231 # most recent EU:MM that supported 5.005.
232 $self->build_requires( 'ExtUtils::MakeMaker' => 6.36 );
233 $self->configure_requires( 'ExtUtils::MakeMaker' => 6.36 );
236 # Generate the MakeMaker params
237 my $args = $self->makemaker_args;
238 $args->{DISTNAME} = $self->name;
239 $args->{NAME} = $self->module_name || $self->name;
240 $args->{NAME} =~ s/-/::/g;
241 $args->{VERSION} = $self->version or die <<'EOT';
242 ERROR: Can't determine distribution version. Please specify it
243 explicitly via 'version' in Makefile.PL, or set a valid $VERSION
244 in a module, and provide its file path via 'version_from' (or
245 'all_from' if you prefer) in Makefile.PL.
248 if ( $self->tests ) {
249 my @tests = split ' ', $self->tests;
252 TESTS => (join ' ', grep {!$seen{$_}++} @tests),
254 } elsif ( $Module::Install::ExtraTests::use_extratests ) {
255 # Module::Install::ExtraTests doesn't set $self->tests and does its own tests via harness.
256 # So, just ignore our xt tests here.
257 } elsif ( -d 'xt' and ($Module::Install::AUTHOR or $ENV{RELEASE_TESTING}) ) {
259 TESTS => join( ' ', map { "$_/*.t" } grep { -d $_ } qw{ t xt } ),
263 $args->{ABSTRACT} = $self->abstract;
264 $args->{AUTHOR} = join ', ', @{$self->author || []};
266 if ( $self->makemaker(6.10) ) {
267 $args->{NO_META} = 1;
268 #$args->{NO_MYMETA} = 1;
270 if ( $self->makemaker(6.17) and $self->sign ) {
273 unless ( $self->is_admin ) {
274 delete $args->{SIGN};
276 if ( $self->makemaker(6.31) and $self->license ) {
277 $args->{LICENSE} = $self->license;
280 my $prereq = ($args->{PREREQ_PM} ||= {});
281 %$prereq = ( %$prereq,
282 map { @$_ } # flatten [module => version]
288 # Remove any reference to perl, PREREQ_PM doesn't support it
289 delete $args->{PREREQ_PM}->{perl};
291 # Merge both kinds of requires into BUILD_REQUIRES
292 my $build_prereq = ($args->{BUILD_REQUIRES} ||= {});
293 %$build_prereq = ( %$build_prereq,
294 map { @$_ } # flatten [module => version]
297 ($self->configure_requires, $self->build_requires)
300 # Remove any reference to perl, BUILD_REQUIRES doesn't support it
301 delete $args->{BUILD_REQUIRES}->{perl};
303 # Delete bundled dists from prereq_pm, add it to Makefile DIR
304 my $subdirs = ($args->{DIR} || []);
305 if ($self->bundles) {
307 foreach my $bundle (@{ $self->bundles }) {
308 my ($mod_name, $dist_dir) = @$bundle;
309 delete $prereq->{$mod_name};
310 $dist_dir = File::Basename::basename($dist_dir); # dir for building this module
311 if (not exists $processed{$dist_dir}) {
313 # List as sub-directory to be processed by make
314 push @$subdirs, $dist_dir;
316 # Else do nothing: the module is already present on the system
317 $processed{$dist_dir} = undef;
322 unless ( $self->makemaker('6.55_03') ) {
323 %$prereq = (%$prereq,%$build_prereq);
324 delete $args->{BUILD_REQUIRES};
327 if ( my $perl_version = $self->perl_version ) {
328 eval "use $perl_version; 1"
329 or die "ERROR: perl: Version $] is installed, "
330 . "but we need version >= $perl_version";
332 if ( $self->makemaker(6.48) ) {
333 $args->{MIN_PERL_VERSION} = $perl_version;
337 if ($self->installdirs) {
338 warn qq{old INSTALLDIRS (probably set by makemaker_args) is overriden by installdirs\n} if $args->{INSTALLDIRS};
339 $args->{INSTALLDIRS} = $self->installdirs;
343 ( $_ => $args->{$_} ) } grep {defined($args->{$_} )
346 my $user_preop = delete $args{dist}->{PREOP};
347 if ( my $preop = $self->admin->preop($user_preop) ) {
348 foreach my $key ( keys %$preop ) {
349 $args{dist}->{$key} = $preop->{$key};
353 my $mm = ExtUtils::MakeMaker::WriteMakefile(%args);
354 $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile');
357 sub fix_up_makefile {
359 my $makefile_name = shift;
360 my $top_class = ref($self->_top) || '';
361 my $top_version = $self->_top->VERSION || '';
363 my $preamble = $self->preamble
364 ? "# Preamble by $top_class $top_version\n"
367 my $postamble = "# Postamble by $top_class $top_version\n"
368 . ($self->postamble || '');
371 open MAKEFILE, "+< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!";
372 eval { flock MAKEFILE, LOCK_EX };
373 my $makefile = do { local $/; <MAKEFILE> };
375 $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /;
376 $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g;
377 $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g;
378 $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m;
379 $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m;
381 # Module::Install will never be used to build the Core Perl
382 # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks
383 # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist
384 $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m;
385 #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m;
387 # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well.
388 $makefile =~ s/(\"?)-I\$\(PERL_LIB\)\1//g;
390 # XXX - This is currently unused; not sure if it breaks other MM-users
391 # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg;
393 seek MAKEFILE, 0, SEEK_SET;
394 truncate MAKEFILE, 0;
395 print MAKEFILE "$preamble$makefile$postamble" or die $!;
396 close MAKEFILE or die $!;
402 my ($self, $text) = @_;
403 $self->{preamble} = $text . $self->{preamble} if defined $text;
408 my ($self, $text) = @_;
409 $self->{postamble} ||= $self->admin->postamble;
410 $self->{postamble} .= $text if defined $text;