1 package Module::Build::Compat;
12 use Module::Build::ModuleInfo;
15 my %convert_installdirs = (
21 my %makefile_to_build =
23 TEST_VERBOSE => 'verbose',
24 VERBINST => 'verbose',
25 INC => sub { map {(extra_compiler_flags => $_)} Module::Build->split_like_shell(shift) },
26 POLLUTE => sub { (extra_compiler_flags => '-DPERL_POLLUTE') },
27 INSTALLDIRS => sub { (installdirs => $convert_installdirs{uc shift()}) },
31 installprivlib => $lib,
32 installsitelib => $lib,
33 installarchlib => "$lib/$Config{archname}",
34 installsitearch => "$lib/$Config{archname}"
36 return map { (config => "$_=$config{$_}") } keys %config;
39 # Convert INSTALLVENDORLIB and friends.
44 my @ret = (config => lc($name) . "=" . shift );
45 print STDERR "# Converted to @ret\n";
50 INSTALLARCHLIB INSTALLSITEARCH INSTALLVENDORARCH
51 INSTALLPRIVLIB INSTALLSITELIB INSTALLVENDORLIB
52 INSTALLBIN INSTALLSITEBIN INSTALLVENDORBIN
53 INSTALLSCRIPT INSTALLSITESCRIPT INSTALLVENDORSCRIPT
54 INSTALLMAN1DIR INSTALLSITEMAN1DIR INSTALLVENDORMAN1DIR
55 INSTALLMAN3DIR INSTALLSITEMAN3DIR INSTALLVENDORMAN3DIR
59 # Some names they have in common
60 map {$_, lc($_)} qw(DESTDIR PREFIX INSTALL_BASE UNINST),
63 my %macro_to_build = %makefile_to_build;
64 # "LIB=foo make" is not the same as "perl Makefile.PL LIB=foo"
65 delete $macro_to_build{LIB};
68 return $_[0] =~ /^[0-9_]+\.?[0-9_]*$/; # crudly, a decimal literal
72 my ($req, $breq) = @_;
77 for my $p ( $req, $breq ) {
78 for my $k (keys %$p) {
79 die "Prereq '$p->{$k}' for '$k' is not supported by Module::Build::Compat\n"
80 unless _simple_prereq($p->{$k});
84 my $merge = { %$req };
85 for my $k ( keys %$breq ) {
86 my $v1 = $merge->{$k} || 0;
88 $merge->{$k} = $v1 > $v2 ? $v1 : $v2;
94 sub create_makefile_pl {
95 my ($package, $type, $build, %args) = @_;
97 die "Don't know how to build Makefile.PL of type '$type'"
98 unless $type =~ /^(small|passthrough|traditional)$/;
100 if ($type eq 'passthrough') {
101 $build->log_warn(<<"HERE");
103 IMPORTANT NOTE: The '$type' style of Makefile.PL is deprecated and
104 may be removed in a future version of Module::Build in favor of the
105 'configure_requires' property. See Module::Build::Compat
106 documentation for details.
115 $args{file} ||= 'Makefile.PL';
116 local $build->{properties}{quiet} = 1;
117 $build->delete_filetree($args{file});
118 $fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
121 print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
123 # Minimum perl version should be specified as "require 5.XXXXXX" in
125 my $requires = $build->requires;
126 if ( my $minimum_perl = $requires->{perl} ) {
127 print {$fh} "require $minimum_perl;\n";
130 # If a *bundled* custom subclass is being used, make sure we add its
131 # directory to @INC. Also, lib.pm always needs paths in Unix format.
132 my $subclass_load = '';
133 if (ref($build) ne "Module::Build") {
134 my $subclass_dir = $package->subclass_dir($build);
136 if (File::Spec->file_name_is_absolute($subclass_dir)) {
137 my $base_dir = $build->base_dir;
139 if ($build->dir_contains($base_dir, $subclass_dir)) {
140 $subclass_dir = File::Spec->abs2rel($subclass_dir, $base_dir);
141 $subclass_dir = $package->unixify_dir($subclass_dir);
142 $subclass_load = "use lib '$subclass_dir';";
144 # Otherwise, leave it the empty string
147 $subclass_dir = $package->unixify_dir($subclass_dir);
148 $subclass_load = "use lib '$subclass_dir';";
152 if ($type eq 'small') {
153 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
154 use Module::Build::Compat 0.02;
156 Module::Build::Compat->run_build_pl(args => \@ARGV);
158 Module::Build::Compat->write_makefile(build_class => '%s');
161 } elsif ($type eq 'passthrough') {
162 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
164 unless (eval "use Module::Build::Compat 0.02; 1" ) {
165 print "This module requires Module::Build to install itself.\n";
167 require ExtUtils::MakeMaker;
168 my $yn = ExtUtils::MakeMaker::prompt
169 (' Install Module::Build now from CPAN?', 'y');
171 unless ($yn =~ /^y/i) {
172 die " *** Cannot install without Module::Build. Exiting ...\n";
179 # Save this 'cause CPAN will chdir all over the place.
180 my $cwd = Cwd::cwd();
182 CPAN::Shell->install('Module::Build::Compat');
183 CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate
184 or die "Couldn't install Module::Build, giving up.\n";
186 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
188 eval "use Module::Build::Compat 0.02; 1" or die $@;
190 Module::Build::Compat->run_build_pl(args => \@ARGV);
191 my $build_script = 'Build';
192 $build_script .= '.com' if $^O eq 'VMS';
193 exit(0) unless(-e $build_script); # cpantesters convention
195 Module::Build::Compat->write_makefile(build_class => '%s');
198 } elsif ($type eq 'traditional') {
200 my (%MM_Args, %prereq);
201 if (eval "use Tie::IxHash; 1") {
202 tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
203 tie %prereq, 'Tie::IxHash'; # Don't care if it fails here
206 my %name = ($build->module_name
207 ? (NAME => $build->module_name)
208 : (DISTNAME => $build->dist_name));
210 my %version = ($build->dist_version_from
211 ? (VERSION_FROM => $build->dist_version_from)
212 : (VERSION => $build->dist_version)
214 %MM_Args = (%name, %version);
216 %prereq = _merge_prereq( $build->requires, $build->build_requires );
217 %prereq = map {$_, $prereq{$_}} sort keys %prereq;
219 delete $prereq{perl};
220 $MM_Args{PREREQ_PM} = \%prereq;
222 $MM_Args{INSTALLDIRS} = $build->installdirs eq 'core' ? 'perl' : $build->installdirs;
224 $MM_Args{EXE_FILES} = [ sort keys %{$build->script_files} ] if $build->script_files;
226 $MM_Args{PL_FILES} = $build->PL_files || {};
228 if ($build->recursive_test_files) {
229 $MM_Args{test} = { TESTS => join q{ }, $package->_test_globs($build) };
232 local $Data::Dumper::Terse = 1;
233 my $args = Data::Dumper::Dumper(\%MM_Args);
234 $args =~ s/\{(.*)\}/($1)/s;
237 use ExtUtils::MakeMaker;
245 my ($self, $build) = @_;
247 return map { File::Spec->catfile($_, '*.t') }
248 @{$build->rscan_dir('t', sub { -d $File::Find::name })};
252 my ($self, $build) = @_;
254 return (Module::Build::ModuleInfo->find_module_dir_by_name(ref $build)
255 || File::Spec->catdir($build->config_dir, 'lib'));
259 my ($self, $path) = @_;
260 return join '/', File::Spec->splitdir($path);
263 sub makefile_to_build_args {
266 foreach my $arg (@_) {
269 my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
270 die "Malformed argument '$arg'");
272 # Do tilde-expansion if it looks like a tilde prefixed path
273 ( $val ) = Module::Build->_detildefy( $val ) if $val =~ /^~/;
275 if (exists $makefile_to_build{$key}) {
276 my $trans = $makefile_to_build{$key};
277 push @out, $class->_argvify( ref($trans) ? $trans->($val) : ($trans => $val) );
278 } elsif (exists $Config{lc($key)}) {
279 push @out, $class->_argvify( config => lc($key) . "=$val" );
281 # Assume M::B can handle it in lowercase form
282 push @out, $class->_argvify("\L$key" => $val);
289 my ($self, @pairs) = @_;
292 my ($k, $v) = splice @pairs, 0, 2;
293 push @out, ("--$k", $v);
298 sub makefile_to_build_macros {
300 my %config; # must accumulate and return as a hashref
301 while (my ($macro, $trans) = each %macro_to_build) {
302 # On some platforms (e.g. Cygwin with 'make'), the mere presence
303 # of "EXPORT: FOO" in the Makefile will make $ENV{FOO} defined.
304 # Therefore we check length() too.
305 next unless exists $ENV{$macro} && length $ENV{$macro};
306 my $val = $ENV{$macro};
307 my @args = ref($trans) ? $trans->($val) : ($trans => $val);
309 my ($k, $v) = splice(@args, 0, 2);
310 if ( $k eq 'config' ) {
311 if ( $v =~ /^([^=]+)=(.*)$/ ) {
315 warn "Couldn't parse config '$v'\n";
319 push @out, ($k => $v);
323 push @out, (config => \%config) if %config;
328 my ($pack, %in) = @_;
329 $in{script} ||= 'Build.PL';
330 my @args = $in{args} ? $pack->makefile_to_build_args(@{$in{args}}) : ();
331 print "# running $in{script} @args\n";
332 Module::Build->run_perl_script($in{script}, [], \@args) or die "Couldn't run $in{script}: $!";
336 my ($self, %args) = @_;
337 unless (exists $args{build_class}) {
338 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
339 $args{build_class} = 'Module::Build';
341 my $class = $args{build_class};
343 my $perl = $class->find_perl_interpreter;
345 # VMS MMS/MMK need to use MCR to run the Perl image.
346 $perl = 'MCR ' . $perl if $self->_is_vms_mms;
348 my $noop = ($class->is_windowsish ? 'rem>nul' :
349 $self->_is_vms_mms ? 'Continue' :
352 my $filetype = $class->is_vmsish ? '.COM' : '';
354 my $Build = 'Build' . $filetype . ' --makefile_env_macros 1';
355 my $unlink = $class->oneliner('1 while unlink $ARGV[0]', [], [$args{makefile}]);
356 $unlink =~ s/\$/\$\$/g unless $class->is_vmsish;
358 my $maketext = ($^O eq 'os2' ? "SHELL = sh\n\n" : '');
360 $maketext .= <<"EOF";
363 realclean : force_do_it
364 $perl $Build realclean
366 distclean : force_do_it
367 $perl $Build distclean
375 foreach my $action ($class->known_actions) {
376 next if $action =~ /^(all|distclean|realclean|force_do_it)$/; # Don't double-define
377 $maketext .= <<"EOF";
378 $action : force_do_it
383 if ($self->_is_vms_mms) {
384 # Roll our own .EXPORT as MMS/MMK don't honor that directive.
385 $maketext .= "\n.FIRST\n\t\@ $noop\n";
386 for my $macro (keys %macro_to_build) {
387 $maketext .= ".IFDEF $macro\n\tDEFINE $macro \"\$($macro)\"\n.ENDIF\n";
392 $maketext .= "\n.EXPORT : " . join(' ', keys %macro_to_build) . "\n\n";
399 my $file = File::Spec->catfile('_build', 'prereqs');
400 my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
401 my $prereqs = eval do {local $/; <$fh>};
404 my %merged = _merge_prereq( $prereqs->{requires}, $prereqs->{build_requires} );
406 foreach (sort keys %merged) {
407 next if $_ eq 'perl';
408 push @prereq, "$_=>q[$merged{$_}]";
410 return unless @prereq;
411 return "# PREREQ_PM => { " . join(", ", @prereq) . " }\n\n";
416 my ($pack, %in) = @_;
418 unless (exists $in{build_class}) {
419 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
420 $in{build_class} = 'Module::Build';
422 my $class = $in{build_class};
423 $in{makefile} ||= $pack->_is_vms_mms ? 'Descrip.MMS' : 'Makefile';
425 open MAKE, "> $in{makefile}" or die "Cannot write $in{makefile}: $!";
426 print MAKE $pack->fake_prereqs;
427 print MAKE $pack->fake_makefile(%in);
432 return Module::Build->is_vmsish && ($Config{make} =~ m/MM[SK]/i);
438 =for :stopwords passthrough
442 Module::Build::Compat - Compatibility with ExtUtils::MakeMaker
449 my $build = Module::Build->new
450 ( module_name => 'Foo::Bar',
452 create_makefile_pl => 'traditional' );
458 Because C<ExtUtils::MakeMaker> has been the standard way to distribute
459 modules for a long time, many tools (CPAN.pm, or your system
460 administrator) may expect to find a working F<Makefile.PL> in every
461 distribution they download from CPAN. If you want to throw them a
462 bone, you can use C<Module::Build::Compat> to automatically generate a
463 F<Makefile.PL> for you, in one of several different styles.
465 C<Module::Build::Compat> also provides some code that helps out the
466 F<Makefile.PL> at runtime.
473 =item create_makefile_pl($style, $build)
475 Creates a F<Makefile.PL> in the current directory in one of several
476 styles, based on the supplied C<Module::Build> object C<$build>. This is
477 typically controlled by passing the desired style as the
478 C<create_makefile_pl> parameter to C<Module::Build>'s C<new()> method;
479 the F<Makefile.PL> will then be automatically created during the
482 The currently supported styles are:
488 A F<Makefile.PL> will be created in the "traditional" style, i.e. it will
489 use C<ExtUtils::MakeMaker> and won't rely on C<Module::Build> at all.
490 In order to create the F<Makefile.PL>, we'll include the C<requires> and
491 C<build_requires> dependencies as the C<PREREQ_PM> parameter.
493 You don't want to use this style if during the C<perl Build.PL> stage
494 you ask the user questions, or do some auto-sensing about the user's
495 environment, or if you subclass C<Module::Build> to do some
496 customization, because the vanilla F<Makefile.PL> won't do any of that.
500 A small F<Makefile.PL> will be created that passes all functionality
501 through to the F<Build.PL> script in the same directory. The user must
502 already have C<Module::Build> installed in order to use this, or else
503 they'll get a module-not-found error.
505 =item passthrough (DEPRECATED)
507 This is just like the C<small> option above, but if C<Module::Build> is
508 not already installed on the user's system, the script will offer to
509 use C<CPAN.pm> to download it and install it before continuing with
512 This option has been deprecated and may be removed in a future version
513 of Module::Build. Modern CPAN.pm and CPANPLUS will recognize the
514 C<configure_requires> metadata property and install Module::Build before
515 running Build.PL if Module::Build is listed and Module::Build now
516 adds itself to configure_requires by default.
518 Perl 5.10.1 includes C<configure_requires> support. In the future, when
519 C<configure_requires> support is deemed sufficiently widespread, the
520 C<passthrough> style will be removed.
524 =item run_build_pl(args => \@ARGV)
526 This method runs the F<Build.PL> script, passing it any arguments the
527 user may have supplied to the C<perl Makefile.PL> command. Because
528 C<ExtUtils::MakeMaker> and C<Module::Build> accept different arguments, this
529 method also performs some translation between the two.
531 C<run_build_pl()> accepts the following named parameters:
537 The C<args> parameter specifies the parameters that would usually
538 appear on the command line of the C<perl Makefile.PL> command -
539 typically you'll just pass a reference to C<@ARGV>.
543 This is the filename of the script to run - it defaults to C<Build.PL>.
547 =item write_makefile()
549 This method writes a 'dummy' F<Makefile> that will pass all commands
550 through to the corresponding C<Module::Build> actions.
552 C<write_makefile()> accepts the following named parameters:
558 The name of the file to write - defaults to the string C<Makefile>.
567 So, some common scenarios are:
573 Just include a F<Build.PL> script (without a F<Makefile.PL>
574 script), and give installation directions in a F<README> or F<INSTALL>
575 document explaining how to install the module. In particular, explain
576 that the user must install C<Module::Build> before installing your
579 Note that if you do this, you may make things easier for yourself, but
580 harder for people with older versions of CPAN or CPANPLUS on their
581 system, because those tools generally only understand the
582 F<Makefile.PL>/C<ExtUtils::MakeMaker> way of doing things.
586 Include a F<Build.PL> script and a "traditional" F<Makefile.PL>,
587 created either manually or with C<create_makefile_pl()>. Users won't
588 ever have to install C<Module::Build> if they use the F<Makefile.PL>, but
589 they won't get to take advantage of C<Module::Build>'s extra features
592 For good measure, of course, test both the F<Makefile.PL> and the
593 F<Build.PL> before shipping.
597 Include a F<Build.PL> script and a "pass-through" F<Makefile.PL>
598 built using C<Module::Build::Compat>. This will mean that people can
599 continue to use the "old" installation commands, and they may never
600 notice that it's actually doing something else behind the scenes. It
601 will also mean that your installation process is compatible with older
602 versions of tools like CPAN and CPANPLUS.
609 Ken Williams <kwilliams@cpan.org>
614 Copyright (c) 2001-2006 Ken Williams. All rights reserved.
616 This library is free software; you can redistribute it and/or
617 modify it under the same terms as Perl itself.
622 L<Module::Build>(3), L<ExtUtils::MakeMaker>(3)