1 package Module::Build::Compat;
5 $VERSION = '0.2808_01';
11 use Module::Build::ModuleInfo;
14 my %makefile_to_build =
16 TEST_VERBOSE => 'verbose',
17 VERBINST => 'verbose',
18 INC => sub { map {('--extra_compiler_flags', $_)} Module::Build->split_like_shell(shift) },
19 POLLUTE => sub { ('--extra_compiler_flags', '-DPERL_POLLUTE') },
20 INSTALLDIRS => sub {local $_ = shift; 'installdirs=' . (/^perl$/ ? 'core' : $_) },
21 LIB => sub { ('--install_path', 'lib='.shift()) },
23 # Some names they have in common
24 map {$_, lc($_)} qw(DESTDIR PREFIX INSTALL_BASE UNINST),
29 sub create_makefile_pl {
30 my ($package, $type, $build, %args) = @_;
32 die "Don't know how to build Makefile.PL of type '$type'"
33 unless $type =~ /^(small|passthrough|traditional)$/;
39 $args{file} ||= 'Makefile.PL';
40 $fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
43 print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
45 # Minimum perl version should be specified as "require 5.XXXXXX" in
47 my $requires = $build->requires;
48 if ( my $minimum_perl = $requires->{perl} ) {
49 print {$fh} "require $minimum_perl;\n";
52 # If a *bundled* custom subclass is being used, make sure we add its
54 my $subclass_load = '';
55 if (ref($build) ne "Module::Build") {
56 my $subclass_dir = $package->subclass_dir($build);
58 if (File::Spec->file_name_is_absolute($subclass_dir)) {
59 my $base_dir = $build->base_dir;
61 if ($build->dir_contains($base_dir, $subclass_dir)) {
62 $subclass_dir = File::Spec->abs2rel($subclass_dir, $base_dir);
63 $subclass_load = "use lib '$subclass_dir';";
67 $subclass_load = "use lib '$subclass_dir';";
71 if ($type eq 'small') {
72 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
73 use Module::Build::Compat 0.02;
75 Module::Build::Compat->run_build_pl(args => \@ARGV);
77 Module::Build::Compat->write_makefile(build_class => '%s');
80 } elsif ($type eq 'passthrough') {
81 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
83 unless (eval "use Module::Build::Compat 0.02; 1" ) {
84 print "This module requires Module::Build to install itself.\n";
86 require ExtUtils::MakeMaker;
87 my $yn = ExtUtils::MakeMaker::prompt
88 (' Install Module::Build now from CPAN?', 'y');
90 unless ($yn =~ /^y/i) {
91 die " *** Cannot install without Module::Build. Exiting ...\n";
98 # Save this 'cause CPAN will chdir all over the place.
101 CPAN::Shell->install('Module::Build::Compat');
102 CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate
103 or die "Couldn't install Module::Build, giving up.\n";
105 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
107 eval "use Module::Build::Compat 0.02; 1" or die $@;
109 Module::Build::Compat->run_build_pl(args => \@ARGV);
111 Module::Build::Compat->write_makefile(build_class => '%s');
114 } elsif ($type eq 'traditional') {
116 my (%MM_Args, %prereq);
117 if (eval "use Tie::IxHash; 1") {
118 tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
119 tie %prereq, 'Tie::IxHash'; # Don't care if it fails here
122 my %name = ($build->module_name
123 ? (NAME => $build->module_name)
124 : (DISTNAME => $build->dist_name));
126 my %version = ($build->dist_version_from
127 ? (VERSION_FROM => $build->dist_version_from)
128 : (VERSION => $build->dist_version)
130 %MM_Args = (%name, %version);
132 %prereq = ( %{$build->requires}, %{$build->build_requires} );
133 %prereq = map {$_, $prereq{$_}} sort keys %prereq;
135 delete $prereq{perl};
136 $MM_Args{PREREQ_PM} = \%prereq;
138 $MM_Args{INSTALLDIRS} = $build->installdirs eq 'core' ? 'perl' : $build->installdirs;
140 $MM_Args{EXE_FILES} = [ sort keys %{$build->script_files} ] if $build->script_files;
142 $MM_Args{PL_FILES} = {};
144 local $Data::Dumper::Terse = 1;
145 my $args = Data::Dumper::Dumper(\%MM_Args);
146 $args =~ s/\{(.*)\}/($1)/s;
149 use ExtUtils::MakeMaker;
158 my ($self, $build) = @_;
160 return (Module::Build::ModuleInfo->find_module_dir_by_name(ref $build)
161 || File::Spec->catdir($build->config_dir, 'lib'));
164 sub makefile_to_build_args {
167 foreach my $arg (@_) {
170 my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
171 die "Malformed argument '$arg'");
173 # Do tilde-expansion if it looks like a tilde prefixed path
174 ( $val ) = glob( $val ) if $val =~ /^~/;
176 if (exists $makefile_to_build{$key}) {
177 my $trans = $makefile_to_build{$key};
178 push @out, ref($trans) ? $trans->($val) : ("--$trans", $val);
179 } elsif (exists $Config{lc($key)}) {
180 push @out, '--config', lc($key) . "=$val";
182 # Assume M::B can handle it in lowercase form
183 push @out, "--\L$key", $val;
189 sub makefile_to_build_macros {
191 while (my ($macro, $trans) = each %makefile_to_build) {
192 # On some platforms (e.g. Cygwin with 'make'), the mere presence
193 # of "EXPORT: FOO" in the Makefile will make $ENV{FOO} defined.
194 # Therefore we check length() too.
195 next unless exists $ENV{$macro} && length $ENV{$macro};
196 my $val = $ENV{$macro};
197 push @out, ref($trans) ? $trans->($val) : ($trans => $val);
203 my ($pack, %in) = @_;
204 $in{script} ||= 'Build.PL';
205 my @args = $in{args} ? $pack->makefile_to_build_args(@{$in{args}}) : ();
206 print "# running $in{script} @args\n";
207 Module::Build->run_perl_script($in{script}, [], \@args) or die "Couldn't run $in{script}: $!";
211 my ($self, %args) = @_;
212 unless (exists $args{build_class}) {
213 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
214 $args{build_class} = 'Module::Build';
216 my $class = $args{build_class};
218 my $perl = $class->find_perl_interpreter;
219 my $noop = ($class->is_windowsish ? 'rem>nul' :
220 $class->is_vmsish ? 'Continue' :
222 my $Build = 'Build --makefile_env_macros 1';
224 # Start with a couple special actions
225 my $maketext = <<"EOF";
228 realclean : force_do_it
229 $perl $Build realclean
230 $perl -e unlink -e shift $args{makefile}
236 foreach my $action ($class->known_actions) {
237 next if $action =~ /^(all|realclean|force_do_it)$/; # Don't double-define
238 $maketext .= <<"EOF";
239 $action : force_do_it
244 $maketext .= "\n.EXPORT : " . join(' ', keys %makefile_to_build) . "\n\n";
250 my $file = File::Spec->catfile('_build', 'prereqs');
251 my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
252 my $prereqs = eval do {local $/; <$fh>};
256 foreach my $section (qw/build_requires requires/) {
257 foreach (keys %{$prereqs->{$section}}) {
258 next if $_ eq 'perl';
259 push @prereq, "$_=>q[$prereqs->{$section}{$_}]";
263 return unless @prereq;
264 return "# PREREQ_PM => { " . join(", ", @prereq) . " }\n\n";
269 my ($pack, %in) = @_;
270 $in{makefile} ||= 'Makefile';
271 open MAKE, "> $in{makefile}" or die "Cannot write $in{makefile}: $!";
272 print MAKE $pack->fake_prereqs;
273 print MAKE $pack->fake_makefile(%in);
283 Module::Build::Compat - Compatibility with ExtUtils::MakeMaker
290 my $build = Module::Build->new
291 ( module_name => 'Foo::Bar',
293 create_makefile_pl => 'passthrough' );
299 Because ExtUtils::MakeMaker has been the standard way to distribute
300 modules for a long time, many tools (CPAN.pm, or your system
301 administrator) may expect to find a working Makefile.PL in every
302 distribution they download from CPAN. If you want to throw them a
303 bone, you can use Module::Build::Compat to automatically generate a
304 Makefile.PL for you, in one of several different styles.
306 Module::Build::Compat also provides some code that helps out the
307 Makefile.PL at runtime.
314 =item create_makefile_pl($style, $build)
316 Creates a Makefile.PL in the current directory in one of several
317 styles, based on the supplied Module::Build object C<$build>. This is
318 typically controlled by passing the desired style as the
319 C<create_makefile_pl> parameter to Module::Build's C<new()> method;
320 the Makefile.PL will then be automatically created during the
323 The currently supported styles are:
329 A small Makefile.PL will be created that passes all functionality
330 through to the Build.PL script in the same directory. The user must
331 already have Module::Build installed in order to use this, or else
332 they'll get a module-not-found error.
336 This is just like the C<small> option above, but if Module::Build is
337 not already installed on the user's system, the script will offer to
338 use C<CPAN.pm> to download it and install it before continuing with
343 A Makefile.PL will be created in the "traditional" style, i.e. it will
344 use C<ExtUtils::MakeMaker> and won't rely on C<Module::Build> at all.
345 In order to create the Makefile.PL, we'll include the C<requires> and
346 C<build_requires> dependencies as the C<PREREQ_PM> parameter.
348 You don't want to use this style if during the C<perl Build.PL> stage
349 you ask the user questions, or do some auto-sensing about the user's
350 environment, or if you subclass Module::Build to do some
351 customization, because the vanilla Makefile.PL won't do any of that.
355 =item run_build_pl(args => \@ARGV)
357 This method runs the Build.PL script, passing it any arguments the
358 user may have supplied to the C<perl Makefile.PL> command. Because
359 ExtUtils::MakeMaker and Module::Build accept different arguments, this
360 method also performs some translation between the two.
362 C<run_build_pl()> accepts the following named parameters:
368 The C<args> parameter specifies the parameters that would usually
369 appear on the command line of the C<perl Makefile.PL> command -
370 typically you'll just pass a reference to C<@ARGV>.
374 This is the filename of the script to run - it defaults to C<Build.PL>.
378 =item write_makefile()
380 This method writes a 'dummy' Makefile that will pass all commands
381 through to the corresponding Module::Build actions.
383 C<write_makefile()> accepts the following named parameters:
389 The name of the file to write - defaults to the string C<Makefile>.
398 So, some common scenarios are:
404 Just include a Build.PL script (without a Makefile.PL
405 script), and give installation directions in a README or INSTALL
406 document explaining how to install the module. In particular, explain
407 that the user must install Module::Build before installing your
410 Note that if you do this, you may make things easier for yourself, but
411 harder for people with older versions of CPAN or CPANPLUS on their
412 system, because those tools generally only understand the
413 F<Makefile.PL>/C<ExtUtils::MakeMaker> way of doing things.
417 Include a Build.PL script and a "traditional" Makefile.PL,
418 created either manually or with C<create_makefile_pl()>. Users won't
419 ever have to install Module::Build if they use the Makefile.PL, but
420 they won't get to take advantage of Module::Build's extra features
423 If you go this route, make sure you explicitly set C<PL_FILES> in the
424 call to C<WriteMakefile()> (probably to an empty hash reference), or
425 else MakeMaker will mistakenly run the Build.PL and you'll get an
426 error message about "Too early to run Build script" or something. For
427 good measure, of course, test both the F<Makefile.PL> and the
428 F<Build.PL> before shipping.
432 Include a Build.PL script and a "pass-through" Makefile.PL
433 built using Module::Build::Compat. This will mean that people can
434 continue to use the "old" installation commands, and they may never
435 notice that it's actually doing something else behind the scenes. It
436 will also mean that your installation process is compatible with older
437 versions of tools like CPAN and CPANPLUS.
444 Ken Williams <kwilliams@cpan.org>
449 Copyright (c) 2001-2006 Ken Williams. All rights reserved.
451 This library is free software; you can redistribute it and/or
452 modify it under the same terms as Perl itself.
457 L<Module::Build>(3), L<ExtUtils::MakeMaker>(3)