This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate version.pm-0.77 into bleadperl
[perl5.git] / lib / Module / Build / Compat.pm
CommitLineData
bb4e9162
YST
1package Module::Build::Compat;
2
3use strict;
4use vars qw($VERSION);
94410036 5$VERSION = '0.33_02';
bb4e9162
YST
6
7use File::Spec;
8use IO::File;
9use Config;
10use Module::Build;
11use Module::Build::ModuleInfo;
12use Data::Dumper;
13
738349a8
SH
14my %convert_installdirs = (
15 PERL => 'core',
16 SITE => 'site',
17 VENDOR => 'vendor',
18);
19
bb4e9162
YST
20my %makefile_to_build =
21 (
22 TEST_VERBOSE => 'verbose',
23 VERBINST => 'verbose',
738349a8
SH
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()}) },
27 LIB => sub {
28 my $lib = shift;
29 my %config = (
30 installprivlib => $lib,
31 installsitelib => $lib,
32 installarchlib => "$lib/$Config{archname}",
33 installsitearch => "$lib/$Config{archname}"
34 );
35 return map { (config => "$_=$config{$_}") } keys %config;
36 },
37
38 # Convert INSTALLVENDORLIB and friends.
39 (
40 map {
41 my $name = "INSTALL".$_."LIB";
42 $name => sub {
43 my @ret = (config => { lc $name => shift });
44 print STDERR "# Converted to @ret\n";
45
46 return @ret;
47 }
48 } keys %convert_installdirs
49 ),
bb4e9162
YST
50
51 # Some names they have in common
52 map {$_, lc($_)} qw(DESTDIR PREFIX INSTALL_BASE UNINST),
53 );
54
738349a8
SH
55my %macro_to_build = %makefile_to_build;
56# "LIB=foo make" is not the same as "perl Makefile.PL LIB=foo"
57delete $macro_to_build{LIB};
bb4e9162
YST
58
59
60sub create_makefile_pl {
61 my ($package, $type, $build, %args) = @_;
62
63 die "Don't know how to build Makefile.PL of type '$type'"
64 unless $type =~ /^(small|passthrough|traditional)$/;
65
66 my $fh;
67 if ($args{fh}) {
68 $fh = $args{fh};
69 } else {
70 $args{file} ||= 'Makefile.PL';
738349a8
SH
71 local $build->{properties}{quiet} = 1;
72 $build->delete_filetree($args{file});
bb4e9162
YST
73 $fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
74 }
75
76 print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
77
7a827510
RGS
78 # Minimum perl version should be specified as "require 5.XXXXXX" in
79 # Makefile.PL
80 my $requires = $build->requires;
81 if ( my $minimum_perl = $requires->{perl} ) {
82 print {$fh} "require $minimum_perl;\n";
83 }
84
dc8021d3 85 # If a *bundled* custom subclass is being used, make sure we add its
738349a8 86 # directory to @INC. Also, lib.pm always needs paths in Unix format.
bb4e9162
YST
87 my $subclass_load = '';
88 if (ref($build) ne "Module::Build") {
89 my $subclass_dir = $package->subclass_dir($build);
dc8021d3 90
bb4e9162
YST
91 if (File::Spec->file_name_is_absolute($subclass_dir)) {
92 my $base_dir = $build->base_dir;
93
94 if ($build->dir_contains($base_dir, $subclass_dir)) {
95 $subclass_dir = File::Spec->abs2rel($subclass_dir, $base_dir);
738349a8 96 $subclass_dir = $package->unixify_dir($subclass_dir);
dc8021d3 97 $subclass_load = "use lib '$subclass_dir';";
bb4e9162 98 }
738349a8 99 # Otherwise, leave it the empty string
dc8021d3
SP
100
101 } else {
738349a8 102 $subclass_dir = $package->unixify_dir($subclass_dir);
dc8021d3 103 $subclass_load = "use lib '$subclass_dir';";
bb4e9162 104 }
bb4e9162 105 }
dc8021d3 106
bb4e9162
YST
107 if ($type eq 'small') {
108 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
109 use Module::Build::Compat 0.02;
110 %s
111 Module::Build::Compat->run_build_pl(args => \@ARGV);
112 require %s;
113 Module::Build::Compat->write_makefile(build_class => '%s');
114EOF
115
116 } elsif ($type eq 'passthrough') {
117 printf {$fh} <<'EOF', $subclass_load, ref($build), ref($build);
118
119 unless (eval "use Module::Build::Compat 0.02; 1" ) {
120 print "This module requires Module::Build to install itself.\n";
121
122 require ExtUtils::MakeMaker;
123 my $yn = ExtUtils::MakeMaker::prompt
124 (' Install Module::Build now from CPAN?', 'y');
125
126 unless ($yn =~ /^y/i) {
127 die " *** Cannot install without Module::Build. Exiting ...\n";
128 }
129
130 require Cwd;
131 require File::Spec;
132 require CPAN;
133
134 # Save this 'cause CPAN will chdir all over the place.
135 my $cwd = Cwd::cwd();
136
bb4e9162 137 CPAN::Shell->install('Module::Build::Compat');
47f13fd5
SP
138 CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate
139 or die "Couldn't install Module::Build, giving up.\n";
bb4e9162
YST
140
141 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
142 }
143 eval "use Module::Build::Compat 0.02; 1" or die $@;
144 %s
145 Module::Build::Compat->run_build_pl(args => \@ARGV);
94410036
DG
146 my $build_script = 'Build';
147 $build_script .= '.com' if $^O eq 'VMS';
148 exit(0) unless(-e $build_script); # cpantesters convention
bb4e9162
YST
149 require %s;
150 Module::Build::Compat->write_makefile(build_class => '%s');
151EOF
152
153 } elsif ($type eq 'traditional') {
154
155 my (%MM_Args, %prereq);
156 if (eval "use Tie::IxHash; 1") {
157 tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
158 tie %prereq, 'Tie::IxHash'; # Don't care if it fails here
159 }
160
161 my %name = ($build->module_name
162 ? (NAME => $build->module_name)
163 : (DISTNAME => $build->dist_name));
164
165 my %version = ($build->dist_version_from
166 ? (VERSION_FROM => $build->dist_version_from)
167 : (VERSION => $build->dist_version)
168 );
169 %MM_Args = (%name, %version);
170
171 %prereq = ( %{$build->requires}, %{$build->build_requires} );
172 %prereq = map {$_, $prereq{$_}} sort keys %prereq;
173
174 delete $prereq{perl};
175 $MM_Args{PREREQ_PM} = \%prereq;
176
177 $MM_Args{INSTALLDIRS} = $build->installdirs eq 'core' ? 'perl' : $build->installdirs;
178
179 $MM_Args{EXE_FILES} = [ sort keys %{$build->script_files} ] if $build->script_files;
180
94410036 181 $MM_Args{PL_FILES} = $build->PL_files || {};
bb4e9162
YST
182
183 local $Data::Dumper::Terse = 1;
184 my $args = Data::Dumper::Dumper(\%MM_Args);
185 $args =~ s/\{(.*)\}/($1)/s;
186
187 print $fh <<"EOF";
188use ExtUtils::MakeMaker;
189WriteMakefile
190$args;
191EOF
192 }
193}
194
195
196sub subclass_dir {
197 my ($self, $build) = @_;
198
199 return (Module::Build::ModuleInfo->find_module_dir_by_name(ref $build)
200 || File::Spec->catdir($build->config_dir, 'lib'));
201}
202
738349a8
SH
203sub unixify_dir {
204 my ($self, $path) = @_;
205 return join '/', File::Spec->splitdir($path);
206}
207
bb4e9162 208sub makefile_to_build_args {
738349a8 209 my $class = shift;
bb4e9162
YST
210 my @out;
211 foreach my $arg (@_) {
212 next if $arg eq '';
213
214 my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
215 die "Malformed argument '$arg'");
216
217 # Do tilde-expansion if it looks like a tilde prefixed path
738349a8 218 ( $val ) = Module::Build->_detildefy( $val ) if $val =~ /^~/;
bb4e9162
YST
219
220 if (exists $makefile_to_build{$key}) {
221 my $trans = $makefile_to_build{$key};
738349a8 222 push @out, $class->_argvify( ref($trans) ? $trans->($val) : ($trans => $val) );
bb4e9162 223 } elsif (exists $Config{lc($key)}) {
738349a8 224 push @out, $class->_argvify( config => lc($key) . "=$val" );
bb4e9162
YST
225 } else {
226 # Assume M::B can handle it in lowercase form
738349a8 227 push @out, $class->_argvify("\L$key" => $val);
bb4e9162
YST
228 }
229 }
230 return @out;
231}
232
738349a8
SH
233sub _argvify {
234 my ($self, @pairs) = @_;
235 my @out;
236 while (@pairs) {
237 my ($k, $v) = splice @pairs, 0, 2;
238 push @out, ("--$k", $v);
239 }
240 return @out;
241}
242
bb4e9162
YST
243sub makefile_to_build_macros {
244 my @out;
738349a8 245 while (my ($macro, $trans) = each %macro_to_build) {
bb4e9162
YST
246 # On some platforms (e.g. Cygwin with 'make'), the mere presence
247 # of "EXPORT: FOO" in the Makefile will make $ENV{FOO} defined.
248 # Therefore we check length() too.
249 next unless exists $ENV{$macro} && length $ENV{$macro};
250 my $val = $ENV{$macro};
251 push @out, ref($trans) ? $trans->($val) : ($trans => $val);
252 }
253 return @out;
254}
255
256sub run_build_pl {
257 my ($pack, %in) = @_;
258 $in{script} ||= 'Build.PL';
259 my @args = $in{args} ? $pack->makefile_to_build_args(@{$in{args}}) : ();
260 print "# running $in{script} @args\n";
261 Module::Build->run_perl_script($in{script}, [], \@args) or die "Couldn't run $in{script}: $!";
262}
263
264sub fake_makefile {
265 my ($self, %args) = @_;
266 unless (exists $args{build_class}) {
267 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
268 $args{build_class} = 'Module::Build';
269 }
c1d8f74e 270 my $class = $args{build_class};
bb4e9162 271
c1d8f74e 272 my $perl = $class->find_perl_interpreter;
738349a8
SH
273
274 # VMS MMS/MMK need to use MCR to run the Perl image.
275 $perl = 'MCR ' . $perl if $self->_is_vms_mms;
276
c1d8f74e 277 my $noop = ($class->is_windowsish ? 'rem>nul' :
738349a8 278 $self->_is_vms_mms ? 'Continue' :
bb4e9162 279 'true');
bb4e9162 280
738349a8
SH
281 my $filetype = $class->is_vmsish ? '.COM' : '';
282
283 my $Build = 'Build' . $filetype . ' --makefile_env_macros 1';
284 my $unlink = $class->oneliner('1 while unlink $ARGV[0]', [], [$args{makefile}]);
86bddcbf 285 $unlink =~ s/\$/\$\$/g unless $class->is_vmsish;
738349a8 286
bb4e9162
YST
287 my $maketext = <<"EOF";
288all : force_do_it
289 $perl $Build
290realclean : force_do_it
291 $perl $Build realclean
738349a8 292 $unlink
94410036
DG
293distclean : force_do_it
294 $perl $Build distclean
295 $unlink
296
bb4e9162
YST
297
298force_do_it :
299 @ $noop
300EOF
301
c1d8f74e 302 foreach my $action ($class->known_actions) {
94410036 303 next if $action =~ /^(all|distclean|realclean|force_do_it)$/; # Don't double-define
bb4e9162
YST
304 $maketext .= <<"EOF";
305$action : force_do_it
306 $perl $Build $action
307EOF
308 }
309
738349a8
SH
310 if ($self->_is_vms_mms) {
311 # Roll our own .EXPORT as MMS/MMK don't honor that directive.
312 $maketext .= "\n.FIRST\n\t\@ $noop\n";
313 for my $macro (keys %macro_to_build) {
314 $maketext .= ".IFDEF $macro\n\tDEFINE $macro \"\$($macro)\"\n.ENDIF\n";
315 }
316 $maketext .= "\n";
317 }
318 else {
319 $maketext .= "\n.EXPORT : " . join(' ', keys %macro_to_build) . "\n\n";
320 }
bb4e9162
YST
321
322 return $maketext;
323}
324
325sub fake_prereqs {
326 my $file = File::Spec->catfile('_build', 'prereqs');
327 my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
328 my $prereqs = eval do {local $/; <$fh>};
329 close $fh;
330
331 my @prereq;
332 foreach my $section (qw/build_requires requires/) {
333 foreach (keys %{$prereqs->{$section}}) {
334 next if $_ eq 'perl';
335 push @prereq, "$_=>q[$prereqs->{$section}{$_}]";
336 }
337 }
338
339 return unless @prereq;
340 return "# PREREQ_PM => { " . join(", ", @prereq) . " }\n\n";
341}
342
343
344sub write_makefile {
345 my ($pack, %in) = @_;
738349a8
SH
346
347 unless (exists $in{build_class}) {
348 warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
349 $in{build_class} = 'Module::Build';
350 }
351 my $class = $in{build_class};
352 $in{makefile} ||= $pack->_is_vms_mms ? 'Descrip.MMS' : 'Makefile';
353
bb4e9162
YST
354 open MAKE, "> $in{makefile}" or die "Cannot write $in{makefile}: $!";
355 print MAKE $pack->fake_prereqs;
356 print MAKE $pack->fake_makefile(%in);
357 close MAKE;
358}
359
738349a8
SH
360sub _is_vms_mms {
361 return Module::Build->is_vmsish && ($Config{make} =~ m/MM[SK]/i);
362}
363
bb4e9162
YST
3641;
365__END__
366
367
368=head1 NAME
369
370Module::Build::Compat - Compatibility with ExtUtils::MakeMaker
371
372
373=head1 SYNOPSIS
374
375 # In a Build.PL :
376 use Module::Build;
377 my $build = Module::Build->new
378 ( module_name => 'Foo::Bar',
379 license => 'perl',
380 create_makefile_pl => 'passthrough' );
381 ...
382
383
384=head1 DESCRIPTION
385
386Because ExtUtils::MakeMaker has been the standard way to distribute
387modules for a long time, many tools (CPAN.pm, or your system
388administrator) may expect to find a working Makefile.PL in every
389distribution they download from CPAN. If you want to throw them a
390bone, you can use Module::Build::Compat to automatically generate a
391Makefile.PL for you, in one of several different styles.
392
393Module::Build::Compat also provides some code that helps out the
394Makefile.PL at runtime.
395
396
397=head1 METHODS
398
399=over 4
400
401=item create_makefile_pl($style, $build)
402
403Creates a Makefile.PL in the current directory in one of several
404styles, based on the supplied Module::Build object C<$build>. This is
405typically controlled by passing the desired style as the
406C<create_makefile_pl> parameter to Module::Build's C<new()> method;
407the Makefile.PL will then be automatically created during the
408C<distdir> action.
409
410The currently supported styles are:
411
412=over 4
413
414=item small
415
416A small Makefile.PL will be created that passes all functionality
417through to the Build.PL script in the same directory. The user must
418already have Module::Build installed in order to use this, or else
419they'll get a module-not-found error.
420
421=item passthrough
422
423This is just like the C<small> option above, but if Module::Build is
424not already installed on the user's system, the script will offer to
425use C<CPAN.pm> to download it and install it before continuing with
426the build.
427
428=item traditional
429
430A Makefile.PL will be created in the "traditional" style, i.e. it will
431use C<ExtUtils::MakeMaker> and won't rely on C<Module::Build> at all.
432In order to create the Makefile.PL, we'll include the C<requires> and
433C<build_requires> dependencies as the C<PREREQ_PM> parameter.
434
435You don't want to use this style if during the C<perl Build.PL> stage
436you ask the user questions, or do some auto-sensing about the user's
437environment, or if you subclass Module::Build to do some
438customization, because the vanilla Makefile.PL won't do any of that.
439
440=back
441
442=item run_build_pl(args => \@ARGV)
443
444This method runs the Build.PL script, passing it any arguments the
445user may have supplied to the C<perl Makefile.PL> command. Because
446ExtUtils::MakeMaker and Module::Build accept different arguments, this
447method also performs some translation between the two.
448
449C<run_build_pl()> accepts the following named parameters:
450
451=over 4
452
453=item args
454
455The C<args> parameter specifies the parameters that would usually
456appear on the command line of the C<perl Makefile.PL> command -
457typically you'll just pass a reference to C<@ARGV>.
458
459=item script
460
461This is the filename of the script to run - it defaults to C<Build.PL>.
462
463=back
464
465=item write_makefile()
466
467This method writes a 'dummy' Makefile that will pass all commands
468through to the corresponding Module::Build actions.
469
470C<write_makefile()> accepts the following named parameters:
471
472=over 4
473
474=item makefile
475
476The name of the file to write - defaults to the string C<Makefile>.
477
478=back
479
480=back
481
482
483=head1 SCENARIOS
484
485So, some common scenarios are:
486
487=over 4
488
489=item 1.
490
491Just include a Build.PL script (without a Makefile.PL
492script), and give installation directions in a README or INSTALL
493document explaining how to install the module. In particular, explain
494that the user must install Module::Build before installing your
495module.
496
497Note that if you do this, you may make things easier for yourself, but
498harder for people with older versions of CPAN or CPANPLUS on their
499system, because those tools generally only understand the
500F<Makefile.PL>/C<ExtUtils::MakeMaker> way of doing things.
501
502=item 2.
503
504Include a Build.PL script and a "traditional" Makefile.PL,
505created either manually or with C<create_makefile_pl()>. Users won't
506ever have to install Module::Build if they use the Makefile.PL, but
507they won't get to take advantage of Module::Build's extra features
508either.
509
94410036 510For good measure, of course, test both the F<Makefile.PL> and the
bb4e9162
YST
511F<Build.PL> before shipping.
512
513=item 3.
514
515Include a Build.PL script and a "pass-through" Makefile.PL
516built using Module::Build::Compat. This will mean that people can
517continue to use the "old" installation commands, and they may never
518notice that it's actually doing something else behind the scenes. It
519will also mean that your installation process is compatible with older
520versions of tools like CPAN and CPANPLUS.
521
522=back
523
524
525=head1 AUTHOR
526
77e96e88 527Ken Williams <kwilliams@cpan.org>
bb4e9162
YST
528
529
530=head1 COPYRIGHT
531
77e96e88 532Copyright (c) 2001-2006 Ken Williams. All rights reserved.
bb4e9162
YST
533
534This library is free software; you can redistribute it and/or
535modify it under the same terms as Perl itself.
536
537
538=head1 SEE ALSO
539
dc8021d3 540L<Module::Build>(3), L<ExtUtils::MakeMaker>(3)
bb4e9162
YST
541
542
543=cut