home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Compat.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-08  |  10.1 KB  |  366 lines

  1. package Module::Build::Compat;
  2.  
  3. use strict;
  4. use vars qw($VERSION);
  5. $VERSION = '0.03';
  6.  
  7. use File::Spec;
  8. use IO::File;
  9. use Config;
  10. use Module::Build;
  11. use Data::Dumper;
  12.  
  13. my %makefile_to_build = 
  14.   (
  15.    TEST_VERBOSE => 'verbose',
  16.    VERBINST     => 'verbose',
  17.    INC     => sub { map "extra_compiler_flags=-I$_", Module::Build->split_like_shell(shift) },
  18.    POLLUTE => sub { 'extra_compiler_flags=-DPERL_POLLUTE' },
  19.    INSTALLDIRS => sub {local $_ = shift; 'installdirs=' . (/^perl$/ ? 'core' : $_) },
  20.    PREFIX => sub {die "Sorry, PREFIX is not supported.  See the Module::Build\n".
  21.               "documentation for 'destdir' or 'install_base' instead.\n"},
  22.   );
  23.  
  24. sub create_makefile_pl {
  25.   my ($package, $type, $build, %args) = @_;
  26.   
  27.   die "Don't know how to build Makefile.PL of type '$type'"
  28.     unless $type =~ /^(small|passthrough|traditional)$/;
  29.  
  30.   my $fh;
  31.   if ($args{fh}) {
  32.     $fh = $args{fh};
  33.   } else {
  34.     $args{file} ||= 'Makefile.PL';
  35.     $fh = IO::File->new("> $args{file}") or die "Can't write $args{file}: $!";
  36.   }
  37.  
  38.   print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";
  39.  
  40.   if ($type eq 'small') {
  41.     printf {$fh} <<'EOF', ref($build);
  42.     use Module::Build::Compat 0.02;
  43.     Module::Build::Compat->run_build_pl(args => \@ARGV);
  44.     Module::Build::Compat->write_makefile(build_class => '%s');
  45. EOF
  46.  
  47.   } elsif ($type eq 'passthrough') {
  48.     printf {$fh} <<'EOF', ref($build);
  49.     
  50.     unless (eval "use Module::Build::Compat 0.02; 1" ) {
  51.       print "This module requires Module::Build to install itself.\n";
  52.       
  53.       require ExtUtils::MakeMaker;
  54.       my $yn = ExtUtils::MakeMaker::prompt
  55.     ('  Install Module::Build now from CPAN?', 'y');
  56.       
  57.       unless ($yn =~ /^y/i) {
  58.     die " *** Cannot install without Module::Build.  Exiting ...\n";
  59.       }
  60.       
  61.       require Cwd;
  62.       require File::Spec;
  63.       require CPAN;
  64.       
  65.       # Save this 'cause CPAN will chdir all over the place.
  66.       my $cwd = Cwd::cwd();
  67.       my $makefile = File::Spec->rel2abs($0);
  68.       
  69.       CPAN::Shell->install('Module::Build::Compat')
  70.     or die " *** Cannot install without Module::Build.  Exiting ...\n";
  71.       
  72.       chdir $cwd or die "Cannot chdir() back to $cwd: $!";
  73.       exec $^X, $makefile, @ARGV;  # Redo now that we have Module::Build
  74.     }
  75.     Module::Build::Compat->run_build_pl(args => \@ARGV);
  76.     Module::Build::Compat->write_makefile(build_class => '%s');
  77. EOF
  78.     
  79.   } elsif ($type eq 'traditional') {
  80.  
  81.     my %MM_Args;
  82.     if (eval "use Tie::IxHash; 1") {
  83.       tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
  84.     }
  85.     
  86.     my %name = ($build->module_name
  87.         ? (NAME => $build->module_name)
  88.         : (DISTNAME => $build->dist_name));
  89.     
  90.     my %version = ($build->dist_version_from
  91.            ? (VERSION_FROM => $build->dist_version_from)
  92.            : (VERSION      => $build->dist_version)
  93.           );
  94.     %MM_Args = (%name, %version);
  95.     
  96.     my %prereq = ( %{$build->requires}, %{$build->build_requires} );
  97.     delete $prereq{perl};
  98.     $MM_Args{PREREQ_PM} = \%prereq;
  99.     
  100.     $MM_Args{INSTALLDIRS} = $build->installdirs eq 'core' ? 'perl' : $build->installdirs;
  101.     
  102.     $MM_Args{EXE_FILES} = [ sort keys %{$build->script_files} ] if $build->script_files;
  103.     
  104.     $MM_Args{PL_FILES} = {};
  105.     
  106.     local $Data::Dumper::Terse = 1;
  107.     my $args = Data::Dumper::Dumper(\%MM_Args);
  108.     $args =~ s/\{(.*)\}/($1)/s;
  109.     
  110.     print $fh <<"EOF";
  111. use ExtUtils::MakeMaker;
  112. WriteMakefile
  113. $args;
  114. EOF
  115.   }
  116. }
  117.  
  118.  
  119. sub makefile_to_build_args {
  120.   shift;
  121.   my @out;
  122.   foreach my $arg (@_) {
  123.     my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
  124.                $arg =~ /^(verbose)$/ ? ($1, 1)  :
  125.                die "Malformed argument '$arg'");
  126.     if (exists $Config{lc($key)}) {
  127.       push @out, 'config=' . lc($key) . "=$val";
  128.     } elsif (exists $makefile_to_build{$key}) {
  129.       my $trans = $makefile_to_build{$key};
  130.       push @out, ref($trans) ? $trans->($val) : "$trans=$val";
  131.     } else {
  132.       # Assume M::B can handle it in lowercase form
  133.       push @out, "\L$key\E=$val";
  134.     }
  135.   }
  136.   return @out;
  137. }
  138.  
  139. sub run_build_pl {
  140.   my ($pack, %in) = @_;
  141.   $in{script} ||= 'Build.PL';
  142.   my @args = $in{args} ? $pack->makefile_to_build_args(@{$in{args}}) : ();
  143.   print "$^X $in{script} @args\n";
  144.   system($^X, $in{script}, @args) == 0 or die "Couldn't run $in{script}: $!";
  145. }
  146.  
  147. sub fake_makefile {
  148.   my ($self, %args) = @_;
  149.   
  150.   my $perl = $args{build_class}->find_perl_interpreter;
  151.   my $os_type = $args{build_class}->os_type;
  152.   my $noop = ($os_type eq 'Windows' ? 'rem' :
  153.           $os_type eq 'VMS'     ? 'Continue' :
  154.           'true');
  155.  
  156.   # Start with a couple special actions
  157.   my $maketext = <<"EOF";
  158. all : force_do_it
  159.     $perl Build
  160. realclean : force_do_it
  161.     $perl Build realclean
  162.     $perl -e unlink -e shift $args{makefile}
  163.  
  164. force_do_it :
  165.     @ $noop
  166. EOF
  167.  
  168.   foreach my $action ($args{build_class}->known_actions) {
  169.     next if $action =~ /^(all|realclean|force_do_it)$/;  # Don't double-define
  170.     $maketext .= <<"EOF";
  171. $action : force_do_it
  172.     $perl Build $action
  173. EOF
  174.   }
  175.   
  176.   return $maketext;
  177. }
  178.  
  179. sub fake_prereqs {
  180.   my $file = File::Spec->catfile('_build', 'prereqs');
  181.   my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
  182.   my $prereqs = eval do {local $/; <$fh>};
  183.   close $fh;
  184.   
  185.   my @prereq;
  186.   foreach my $section (qw/build_requires requires/) {
  187.     foreach (keys %{$prereqs->{$section}}) {
  188.       next if $_ eq 'perl';
  189.       push @prereq, "$_=>q[$prereqs->{$section}{$_}]";
  190.     }
  191.   }
  192.  
  193.   return unless @prereq;
  194.   return "#     PREREQ_PM => { " . join(", ", @prereq) . " }\n\n";
  195. }
  196.  
  197.  
  198. sub write_makefile {
  199.   my ($pack, %in) = @_;
  200.   $in{makefile} ||= 'Makefile';
  201.   open  MAKE, "> $in{makefile}" or die "Cannot write $in{makefile}: $!";
  202.   print MAKE $pack->fake_prereqs;
  203.   print MAKE $pack->fake_makefile(%in);
  204.   close MAKE;
  205. }
  206.  
  207. 1;
  208. __END__
  209.  
  210.  
  211. =head1 NAME
  212.  
  213. Module::Build::Compat - Compatibility with ExtUtils::MakeMaker
  214.  
  215. =head1 SYNOPSIS
  216.  
  217.  # In a Build.PL :
  218.  use Module::Build;
  219.  my $build = Module::Build->new
  220.    ( module_name => 'Foo::Bar',
  221.      license => 'perl',
  222.      create_makefile_pl => 'passthrough' );
  223.  ...
  224.  
  225. =head1 DESCRIPTION
  226.  
  227. Because ExtUtils::MakeMaker has been the standard way to distribute
  228. modules for a long time, many tools (CPAN.pm, or your system
  229. administrator) may expect to find a working Makefile.PL in every
  230. distribution they download from CPAN.  If you want to throw them a
  231. bone, you can use Module::Build::Compat to automatically generate a
  232. Makefile.PL for you, in one of several different styles.
  233.  
  234. Module::Build::Compat also provides some code that helps out the
  235. Makefile.PL at runtime.
  236.  
  237. =head1 METHODS
  238.  
  239. =over 4
  240.  
  241. =item create_makefile_pl( $style, $build )
  242.  
  243. Creates a Makefile.PL in the current directory in one of several
  244. styles, based on the supplied Module::Build object C<$build>.  This is
  245. typically controlled by passing the desired style as the
  246. C<create_makefile_pl> parameter to Module::Build's C<new()> method;
  247. the Makefile.PL will then be automatically created during the
  248. C<distdir> action.
  249.  
  250. The currently supported styles are:
  251.  
  252. =over 4
  253.  
  254. =item small
  255.  
  256. A small Makefile.PL will be created that passes all functionality
  257. through to the Build.PL script in the same directory.  The user must
  258. already have Module::Build installed in order to use this, or else
  259. they'll get a module-not-found error.
  260.  
  261. =item passthrough
  262.  
  263. This is just like the C<small> option above, but if Module::Build is
  264. not already installed on the user's system, the script will offer to
  265. use C<CPAN.pm> to download it and install it before continuing with
  266. the build.
  267.  
  268. =item traditional
  269.  
  270. A Makefile.PL will be created in the "traditional" style, i.e. it will
  271. use C<ExtUtils::MakeMaker> and won't rely on C<Module::Build> at all.
  272. In order to create the Makefile.PL, we'll include the C<requires> and
  273. C<build_requires> dependencies as the C<PREREQ_PM> parameter.
  274.  
  275. You don't want to use this style if during the C<perl Build.PL> stage
  276. you ask the user questions, or do some auto-sensing about the user's
  277. environment, or if you subclass Module::Build to do some
  278. customization, because the vanilla Makefile.PL won't do any of that.
  279.  
  280. =back
  281.  
  282. =item run_build_pl( args => \@ARGV )
  283.  
  284. This method runs the Build.PL script, passing it any arguments the
  285. user may have supplied to the C<perl Makefile.PL> command.  Because
  286. ExtUtils::MakeMaker and Module::Build accept different arguments, this
  287. method also performs some translation between the two.
  288.  
  289. C<run_build_pl()> accepts the following named parameters:
  290.  
  291. =over 4
  292.  
  293. =item args
  294.  
  295. The C<args> parameter specifies the parameters that would usually
  296. appear on the command line of the C<perl Makefile.PL> command -
  297. typically you'll just pass a reference to C<@ARGV>.
  298.  
  299. =item script
  300.  
  301. This is the filename of the script to run - it defaults to C<Build.PL>.
  302.  
  303. =back
  304.  
  305.  
  306. =item write_makefile()
  307.  
  308. This method writes a 'dummy' Makefile that will pass all commands
  309. through to the corresponding Module::Build actions.
  310.  
  311. C<write_makefile()> accepts the following named parameters:
  312.  
  313. =over 4
  314.  
  315. =item makefile
  316.  
  317. The name of the file to write - defaults to the string C<Makefile>.
  318.  
  319. =back
  320.  
  321. =back
  322.  
  323. =head1 SCENARIOS
  324.  
  325. So, some common scenarios are:
  326.  
  327. =over 4
  328.  
  329. =item 1.
  330.  
  331. Just include a Build.PL script (without a Makefile.PL
  332. script), and give installation directions in a README or INSTALL
  333. document explaining how to install the module.  In particular, explain
  334. that the user must install Module::Build before installing your
  335. module.  I prefer this method, mainly because I believe that the woes
  336. and hardships of doing this are far less significant than most people
  337. would have you believe.  It's also the simplest method, which is nice.
  338. But anyone with an older version of CPAN or CPANPLUS on their system
  339. will probably have problems installing your module with it.
  340.  
  341. =item 2.
  342.  
  343. Include a Build.PL script and a "traditional" Makefile.PL,
  344. created either manually or with create_makefile_pl().  Users won't
  345. ever have to install Module::Build, but in truth it's difficult to
  346. create a proper Makefile.PL
  347.  
  348. =item 3.
  349.  
  350. Include a Build.PL script and a "pass-through" Makefile.PL
  351. built using Module::Build::Compat.  This will mean that people can
  352. continue to use the "old" installation commands, and they may never
  353. notice that it's actually doing something else behind the scenes.
  354.  
  355. =back
  356.  
  357. =head1 AUTHOR
  358.  
  359. Ken Williams, ken@mathforum.org
  360.  
  361. =head1 SEE ALSO
  362.  
  363. Module::Build(3), ExtUtils::MakeMaker(3)
  364.  
  365. =cut
  366.