1 package Module::Build::Compat;
11 use Module::Build::ModuleInfo;
14 my %convert_installdirs = (
20 my %makefile_to_build =
22 TEST_VERBOSE => 'verbose',
23 VERBINST => 'verbose',
24 INC => sub { map {(extra_compiler_flags => $_)} Module::Build->split_like_shell(shift) },
25 POLLUTE => sub { (extra_compiler_flags => '-DPERL_POLLUTE') },
26 INSTALLDIRS => sub { (installdirs => $convert_installdirs{uc shift()}) },
30 installprivlib => $lib,
31 installsitelib => $lib,
32 installarchlib => "$lib/$Config{archname}",
33 installsitearch => "$lib/$Config{archname}"
35 return map { (config => "$_=$config{$_}") } keys %config;
38 # Convert INSTALLVENDORLIB and friends.
41 my $name = "INSTALL".$_."LIB";
43 my @ret = (config => { lc $name => shift });
44 print STDERR "# Converted to @ret\n";
48 } keys %convert_installdirs
51 # Some names they have in common
52 map {$_, lc($_)} qw(DESTDIR PREFIX INSTALL_BASE UNINST),
55 my %macro_to_build = %makefile_to_build;
56 # "LIB=foo make" is not the same as "perl Makefile.PL LIB=foo"
57 delete $macro_to_build{LIB};
60 sub create_makefile_pl {
61 my ($package, $type, $build, %args) = @_;
63 die "Don't know how to build Makefile.PL of type '$type'"
64 unless $type =~ /^(small|passthrough|traditional)$/;
70 $args{file} ||= 'Makefile.PL';
71 local $build->{properties}{quiet} = 1;
72 $build->delete_filetree($args{file});
73 $fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
76 print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
78 # Minimum perl version should be specified as "require 5.XXXXXX" in
80 my $requires = $build->requires;
81 if ( my $minimum_perl = $requires->{perl} ) {
82 print {$fh} "require $minimum_perl;\n";
85 # If a *bundled* custom subclass is being used, make sure we add its
86 # directory to @INC. Also, lib.pm always needs paths in Unix format.
87 my $subclass_load = '';
88 if (ref($build) ne "Module::Build") {
89 my $subclass_dir = $package->subclass_dir($build);
91 if (File::Spec->file_name_is_absolute($subclass_dir)) {
92 my $base_dir = $build->base_dir;
94 if ($build->dir_contains($base_dir, $subclass_dir)) {
95 $subclass_dir = File::Spec->abs2rel($subclass_dir, $base_dir);
96 $subclass_dir = $package->unixify_dir($subclass_dir);
97 $subclass_load = "use lib '$subclass_dir';";
99 # Otherwise, leave it the empty string
102 $subclass_dir = $package->unixify_dir($subclass_dir);
103 $subclass_load = "use lib '$subclass_dir';";
107 if ($type eq 'small') {
108 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
109 use Module::Build::Compat 0.02;
111 Module::Build::Compat->run_build_pl(args => \@ARGV);
113 Module::Build::Compat->write_makefile(build_class => '%s');
116 } elsif ($type eq 'passthrough') {
117 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
119 unless (eval "use Module::Build::Compat 0.02; 1" ) {
120 print "This module requires Module::Build to install itself.\n";
122 require ExtUtils::MakeMaker;
123 my $yn = ExtUtils::MakeMaker::prompt
124 (' Install Module::Build now from CPAN?', 'y');
126 unless ($yn =~ /^y/i) {
127 die " *** Cannot install without Module::Build. Exiting ...\n";
134 # Save this 'cause CPAN will chdir all over the place.
135 my $cwd = Cwd::cwd();
137 CPAN::Shell->install('Module::Build::Compat');
138 CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate
139 or die "Couldn't install Module::Build, giving up.\n";
141 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
143 eval "use Module::Build::Compat 0.02; 1" or die $@;
145 Module::Build::Compat->run_build_pl(args => \@ARGV);
147 Module::Build::Compat->write_makefile(build_class => '%s');
150 } elsif ($type eq 'traditional') {
152 my (%MM_Args, %prereq);
153 if (eval "use Tie::IxHash; 1") {
154 tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
155 tie %prereq, 'Tie::IxHash'; # Don't care if it fails here
158 my %name = ($build->module_name
159 ? (NAME => $build->module_name)
160 : (DISTNAME => $build->dist_name));
162 my %version = ($build->dist_version_from
163 ? (VERSION_FROM => $build->dist_version_from)
164 : (VERSION => $build->dist_version)
166 %MM_Args = (%name, %version);
168 %prereq = ( %{$build->requires}, %{$build->build_requires} );
169 %prereq = map {$_, $prereq{$_}} sort keys %prereq;
171 delete $prereq{perl};
172 $MM_Args{PREREQ_PM} = \%prereq;
174 $MM_Args{INSTALLDIRS} = $build->installdirs eq 'core' ? 'perl' : $build->installdirs;
176 $MM_Args{EXE_FILES} = [ sort keys %{$build->script_files} ] if $build->script_files;
178 $MM_Args{PL_FILES} = {};
180 local $Data::Dumper::Terse = 1;
181 my $args = Data::Dumper::Dumper(\%MM_Args);
182 $args =~ s/\{(.*)\}/($1)/s;
185 use ExtUtils::MakeMaker;
194 my ($self, $build) = @_;
196 return (Module::Build::ModuleInfo->find_module_dir_by_name(ref $build)
197 || File::Spec->catdir($build->config_dir, 'lib'));
201 my ($self, $path) = @_;
202 return join '/', File::Spec->splitdir($path);
205 sub makefile_to_build_args {
208 foreach my $arg (@_) {
211 my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
212 die "Malformed argument '$arg'");
214 # Do tilde-expansion if it looks like a tilde prefixed path
215 ( $val ) = Module::Build->_detildefy( $val ) if $val =~ /^~/;
217 if (exists $makefile_to_build{$key}) {
218 my $trans = $makefile_to_build{$key};
219 push @out, $class->_argvify( ref($trans) ? $trans->($val) : ($trans => $val) );
220 } elsif (exists $Config{lc($key)}) {
221 push @out, $class->_argvify( config => lc($key) . "=$val" );
223 # Assume M::B can handle it in lowercase form
224 push @out, $class->_argvify("\L$key" => $val);
231 my ($self, @pairs) = @_;
234 my ($k, $v) = splice @pairs, 0, 2;
235 push @out, ("--$k", $v);
240 sub makefile_to_build_macros {
242 while (my ($macro, $trans) = each %macro_to_build) {
243 # On some platforms (e.g. Cygwin with 'make'), the mere presence
244 # of "EXPORT: FOO" in the Makefile will make $ENV{FOO} defined.
245 # Therefore we check length() too.
246 next unless exists $ENV{$macro} && length $ENV{$macro};
247 my $val = $ENV{$macro};
248 push @out, ref($trans) ? $trans->($val) : ($trans => $val);
254 my ($pack, %in) = @_;
255 $in{script} ||= 'Build.PL';
256 my @args = $in{args} ? $pack->makefile_to_build_args(@{$in{args}}) : ();
257 print "# running $in{script} @args\n";
258 Module::Build->run_perl_script($in{script}, [], \@args) or die "Couldn't run $in{script}: $!";
262 my ($self, %args) = @_;
263 unless (exists $args{build_class}) {
264 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
265 $args{build_class} = 'Module::Build';
267 my $class = $args{build_class};
269 my $perl = $class->find_perl_interpreter;
271 # VMS MMS/MMK need to use MCR to run the Perl image.
272 $perl = 'MCR ' . $perl if $self->_is_vms_mms;
274 my $noop = ($class->is_windowsish ? 'rem>nul' :
275 $self->_is_vms_mms ? 'Continue' :
278 my $filetype = $class->is_vmsish ? '.COM' : '';
280 my $Build = 'Build' . $filetype . ' --makefile_env_macros 1';
281 my $unlink = $class->oneliner('1 while unlink $ARGV[0]', [], [$args{makefile}]);
282 $unlink =~ s/\$/\$\$/g;
284 my $maketext = <<"EOF";
287 realclean : force_do_it
288 $perl $Build realclean
295 foreach my $action ($class->known_actions) {
296 next if $action =~ /^(all|realclean|force_do_it)$/; # Don't double-define
297 $maketext .= <<"EOF";
298 $action : force_do_it
303 if ($self->_is_vms_mms) {
304 # Roll our own .EXPORT as MMS/MMK don't honor that directive.
305 $maketext .= "\n.FIRST\n\t\@ $noop\n";
306 for my $macro (keys %macro_to_build) {
307 $maketext .= ".IFDEF $macro\n\tDEFINE $macro \"\$($macro)\"\n.ENDIF\n";
312 $maketext .= "\n.EXPORT : " . join(' ', keys %macro_to_build) . "\n\n";
319 my $file = File::Spec->catfile('_build', 'prereqs');
320 my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
321 my $prereqs = eval do {local $/; <$fh>};
325 foreach my $section (qw/build_requires requires/) {
326 foreach (keys %{$prereqs->{$section}}) {
327 next if $_ eq 'perl';
328 push @prereq, "$_=>q[$prereqs->{$section}{$_}]";
332 return unless @prereq;
333 return "# PREREQ_PM => { " . join(", ", @prereq) . " }\n\n";
338 my ($pack, %in) = @_;
340 unless (exists $in{build_class}) {
341 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
342 $in{build_class} = 'Module::Build';
344 my $class = $in{build_class};
345 $in{makefile} ||= $pack->_is_vms_mms ? 'Descrip.MMS' : 'Makefile';
347 open MAKE, "> $in{makefile}" or die "Cannot write $in{makefile}: $!";
348 print MAKE $pack->fake_prereqs;
349 print MAKE $pack->fake_makefile(%in);
354 return Module::Build->is_vmsish && ($Config{make} =~ m/MM[SK]/i);
363 Module::Build::Compat - Compatibility with ExtUtils::MakeMaker
370 my $build = Module::Build->new
371 ( module_name => 'Foo::Bar',
373 create_makefile_pl => 'passthrough' );
379 Because ExtUtils::MakeMaker has been the standard way to distribute
380 modules for a long time, many tools (CPAN.pm, or your system
381 administrator) may expect to find a working Makefile.PL in every
382 distribution they download from CPAN. If you want to throw them a
383 bone, you can use Module::Build::Compat to automatically generate a
384 Makefile.PL for you, in one of several different styles.
386 Module::Build::Compat also provides some code that helps out the
387 Makefile.PL at runtime.
394 =item create_makefile_pl($style, $build)
396 Creates a Makefile.PL in the current directory in one of several
397 styles, based on the supplied Module::Build object C<$build>. This is
398 typically controlled by passing the desired style as the
399 C<create_makefile_pl> parameter to Module::Build's C<new()> method;
400 the Makefile.PL will then be automatically created during the
403 The currently supported styles are:
409 A small Makefile.PL will be created that passes all functionality
410 through to the Build.PL script in the same directory. The user must
411 already have Module::Build installed in order to use this, or else
412 they'll get a module-not-found error.
416 This is just like the C<small> option above, but if Module::Build is
417 not already installed on the user's system, the script will offer to
418 use C<CPAN.pm> to download it and install it before continuing with
423 A Makefile.PL will be created in the "traditional" style, i.e. it will
424 use C<ExtUtils::MakeMaker> and won't rely on C<Module::Build> at all.
425 In order to create the Makefile.PL, we'll include the C<requires> and
426 C<build_requires> dependencies as the C<PREREQ_PM> parameter.
428 You don't want to use this style if during the C<perl Build.PL> stage
429 you ask the user questions, or do some auto-sensing about the user's
430 environment, or if you subclass Module::Build to do some
431 customization, because the vanilla Makefile.PL won't do any of that.
435 =item run_build_pl(args => \@ARGV)
437 This method runs the Build.PL script, passing it any arguments the
438 user may have supplied to the C<perl Makefile.PL> command. Because
439 ExtUtils::MakeMaker and Module::Build accept different arguments, this
440 method also performs some translation between the two.
442 C<run_build_pl()> accepts the following named parameters:
448 The C<args> parameter specifies the parameters that would usually
449 appear on the command line of the C<perl Makefile.PL> command -
450 typically you'll just pass a reference to C<@ARGV>.
454 This is the filename of the script to run - it defaults to C<Build.PL>.
458 =item write_makefile()
460 This method writes a 'dummy' Makefile that will pass all commands
461 through to the corresponding Module::Build actions.
463 C<write_makefile()> accepts the following named parameters:
469 The name of the file to write - defaults to the string C<Makefile>.
478 So, some common scenarios are:
484 Just include a Build.PL script (without a Makefile.PL
485 script), and give installation directions in a README or INSTALL
486 document explaining how to install the module. In particular, explain
487 that the user must install Module::Build before installing your
490 Note that if you do this, you may make things easier for yourself, but
491 harder for people with older versions of CPAN or CPANPLUS on their
492 system, because those tools generally only understand the
493 F<Makefile.PL>/C<ExtUtils::MakeMaker> way of doing things.
497 Include a Build.PL script and a "traditional" Makefile.PL,
498 created either manually or with C<create_makefile_pl()>. Users won't
499 ever have to install Module::Build if they use the Makefile.PL, but
500 they won't get to take advantage of Module::Build's extra features
503 If you go this route, make sure you explicitly set C<PL_FILES> in the
504 call to C<WriteMakefile()> (probably to an empty hash reference), or
505 else MakeMaker will mistakenly run the Build.PL and you'll get an
506 error message about "Too early to run Build script" or something. For
507 good measure, of course, test both the F<Makefile.PL> and the
508 F<Build.PL> before shipping.
512 Include a Build.PL script and a "pass-through" Makefile.PL
513 built using Module::Build::Compat. This will mean that people can
514 continue to use the "old" installation commands, and they may never
515 notice that it's actually doing something else behind the scenes. It
516 will also mean that your installation process is compatible with older
517 versions of tools like CPAN and CPANPLUS.
524 Ken Williams <kwilliams@cpan.org>
529 Copyright (c) 2001-2006 Ken Williams. All rights reserved.
531 This library is free software; you can redistribute it and/or
532 modify it under the same terms as Perl itself.
537 L<Module::Build>(3), L<ExtUtils::MakeMaker>(3)