<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Module::Build::Compat;

use strict;
use vars qw($VERSION);
$VERSION = '0.39_01';

use File::Basename ();
use File::Spec;
use IO::File;
use Config;
use Module::Build;
use Module::Build::ModuleInfo;
use Module::Build::Version;
use Data::Dumper;

my %convert_installdirs = (
    PERL        =&gt; 'core',
    SITE        =&gt; 'site',
    VENDOR      =&gt; 'vendor',
);

my %makefile_to_build =
  (
   TEST_VERBOSE =&gt; 'verbose',
   VERBINST     =&gt; 'verbose',
   INC          =&gt; sub { map {(extra_compiler_flags =&gt; $_)} Module::Build-&gt;split_like_shell(shift) },
   POLLUTE      =&gt; sub { (extra_compiler_flags =&gt; '-DPERL_POLLUTE') },
   INSTALLDIRS  =&gt; sub { (installdirs =&gt; $convert_installdirs{uc shift()}) },
   LIB          =&gt; sub {
       my $lib = shift;
       my %config = (
           installprivlib  =&gt; $lib,
           installsitelib  =&gt; $lib,
           installarchlib  =&gt; "$lib/$Config{archname}",
           installsitearch =&gt; "$lib/$Config{archname}"
       );
       return map { (config =&gt; "$_=$config{$_}") } keys %config;
   },

   # Convert INSTALLVENDORLIB and friends.
   (
       map {
           my $name = $_;
           $name =&gt; sub {
                 my @ret = (config =&gt; lc($name) . "=" . shift );
                 print STDERR "# Converted to @ret\n";

                 return @ret;
           }
       } qw(
         INSTALLARCHLIB  INSTALLSITEARCH     INSTALLVENDORARCH
         INSTALLPRIVLIB  INSTALLSITELIB      INSTALLVENDORLIB
         INSTALLBIN      INSTALLSITEBIN      INSTALLVENDORBIN
         INSTALLSCRIPT   INSTALLSITESCRIPT   INSTALLVENDORSCRIPT
         INSTALLMAN1DIR  INSTALLSITEMAN1DIR  INSTALLVENDORMAN1DIR
         INSTALLMAN3DIR  INSTALLSITEMAN3DIR  INSTALLVENDORMAN3DIR
       )
   ),

   # Some names they have in common
   map {$_, lc($_)} qw(DESTDIR PREFIX INSTALL_BASE UNINST),
  );

my %macro_to_build = %makefile_to_build;
# "LIB=foo make" is not the same as "perl Makefile.PL LIB=foo"
delete $macro_to_build{LIB};

sub _merge_prereq {
  my ($req, $breq) = @_;
  $req ||= {};
  $breq ||= {};

  # validate formats
  for my $p ( $req, $breq ) {
    for my $k (keys %$p) {
      next if $k eq 'perl';

      my $v_obj = eval { Module::Build::Version-&gt;new($p-&gt;{$k}) };
      if ( ! defined $v_obj ) {
          die "A prereq of the form '$p-&gt;{$k}' for '$k' is not supported by Module::Build::Compat ( use a simpler version like '0.05' or 'v1.4.25' )\n";
      }

      # It seems like a lot of people trip over "0.1.2" stuff, so we help them here...
      if ( $v_obj-&gt;is_qv ) {
        my $proper_ver = $v_obj-&gt;numify;
        warn "Dotted-decimal prereq '$p-&gt;{$k}' for '$k' is not portable - converting it to '$proper_ver'\n";
        $p-&gt;{$k} = $proper_ver;
      }
    }
  }
  # merge
  my $merge = { %$req };
  for my $k ( keys %$breq ) {
    my $v1 = $merge-&gt;{$k} || 0;
    my $v2 = $breq-&gt;{$k};
    $merge-&gt;{$k} = $v1 &gt; $v2 ? $v1 : $v2;
  }
  return %$merge;
}


sub create_makefile_pl {
  my ($package, $type, $build, %args) = @_;

  die "Don't know how to build Makefile.PL of type '$type'"
    unless $type =~ /^(small|passthrough|traditional)$/;

  if ($type eq 'passthrough') {
    $build-&gt;log_warn(&lt;&lt;"HERE");

IMPORTANT NOTE: The '$type' style of Makefile.PL is deprecated and
may be removed in a future version of Module::Build in favor of the
'configure_requires' property.  See Module::Build::Compat
documentation for details.

HERE
  }

  my $fh;
  if ($args{fh}) {
    $fh = $args{fh};
  } else {
    $args{file} ||= 'Makefile.PL';
    local $build-&gt;{properties}{quiet} = 1;
    $build-&gt;delete_filetree($args{file});
    $fh = IO::File-&gt;new("&gt; $args{file}") or die "Can't write $args{file}: $!";
  }

  print {$fh} "# Note: this file was auto-generated by ", __PACKAGE__, " version $VERSION\n";

  # Minimum perl version should be specified as "require 5.XXXXXX" in
  # Makefile.PL
  my $requires = $build-&gt;requires;
  if ( my $minimum_perl = $requires-&gt;{perl} ) {
    my $min_ver = Module::Build::Version-&gt;new($minimum_perl)-&gt;numify;
    print {$fh} "require $min_ver;\n";
  }

  # If a *bundled* custom subclass is being used, make sure we add its
  # directory to @INC.  Also, lib.pm always needs paths in Unix format.
  my $subclass_load = '';
  if (ref($build) ne "Module::Build") {
    my $subclass_dir = $package-&gt;subclass_dir($build);

    if (File::Spec-&gt;file_name_is_absolute($subclass_dir)) {
      my $base_dir = $build-&gt;base_dir;

      if ($build-&gt;dir_contains($base_dir, $subclass_dir)) {
	$subclass_dir = File::Spec-&gt;abs2rel($subclass_dir, $base_dir);
	$subclass_dir = $package-&gt;unixify_dir($subclass_dir);
        $subclass_load = "use lib '$subclass_dir';";
      }
      # Otherwise, leave it the empty string

    } else {
      $subclass_dir = $package-&gt;unixify_dir($subclass_dir);
      $subclass_load = "use lib '$subclass_dir';";
    }
  }

  if ($type eq 'small') {
    printf {$fh} &lt;&lt;'EOF', $subclass_load, ref($build), ref($build);
    use Module::Build::Compat 0.02;
    %s
    Module::Build::Compat-&gt;run_build_pl(args =&gt; \@ARGV);
    require %s;
    Module::Build::Compat-&gt;write_makefile(build_class =&gt; '%s');
EOF

  } elsif ($type eq 'passthrough') {
    printf {$fh} &lt;&lt;'EOF', $subclass_load, ref($build), ref($build);

    unless (eval "use Module::Build::Compat 0.02; 1" ) {
      print "This module requires Module::Build to install itself.\n";

      require ExtUtils::MakeMaker;
      my $yn = ExtUtils::MakeMaker::prompt
	('  Install Module::Build now from CPAN?', 'y');

      unless ($yn =~ /^y/i) {
	die " *** Cannot install without Module::Build.  Exiting ...\n";
      }

      require Cwd;
      require File::Spec;
      require CPAN;

      # Save this 'cause CPAN will chdir all over the place.
      my $cwd = Cwd::cwd();

      CPAN::Shell-&gt;install('Module::Build::Compat');
      CPAN::Shell-&gt;expand("Module", "Module::Build::Compat")-&gt;uptodate
	or die "Couldn't install Module::Build, giving up.\n";

      chdir $cwd or die "Cannot chdir() back to $cwd: $!";
    }
    eval "use Module::Build::Compat 0.02; 1" or die $@;
    %s
    Module::Build::Compat-&gt;run_build_pl(args =&gt; \@ARGV);
    my $build_script = 'Build';
    $build_script .= '.com' if $^O eq 'VMS';
    exit(0) unless(-e $build_script); # cpantesters convention
    require %s;
    Module::Build::Compat-&gt;write_makefile(build_class =&gt; '%s');
EOF

  } elsif ($type eq 'traditional') {

    my (%MM_Args, %prereq);
    if (eval "use Tie::IxHash 1.2; 1") {
      tie %MM_Args, 'Tie::IxHash'; # Don't care if it fails here
      tie %prereq,  'Tie::IxHash'; # Don't care if it fails here
    }

    my %name = ($build-&gt;module_name
		? (NAME =&gt; $build-&gt;module_name)
		: (DISTNAME =&gt; $build-&gt;dist_name));

    my %version = ($build-&gt;dist_version_from
		   ? (VERSION_FROM =&gt; $build-&gt;dist_version_from)
		   : (VERSION      =&gt; $build-&gt;dist_version)
		  );
    %MM_Args = (%name, %version);

    %prereq = _merge_prereq( $build-&gt;requires, $build-&gt;build_requires );
    %prereq = map {$_, $prereq{$_}} sort keys %prereq;

     delete $prereq{perl};
    $MM_Args{PREREQ_PM} = \%prereq;

    $MM_Args{INSTALLDIRS} = $build-&gt;installdirs eq 'core' ? 'perl' : $build-&gt;installdirs;

    $MM_Args{EXE_FILES} = [ sort keys %{$build-&gt;script_files} ] if $build-&gt;script_files;

    $MM_Args{PL_FILES} = $build-&gt;PL_files || {};

    if ($build-&gt;recursive_test_files) {
        $MM_Args{test} = { TESTS =&gt; join q{ }, $package-&gt;_test_globs($build) };
    }

    local $Data::Dumper::Terse = 1;
    my $args = Data::Dumper::Dumper(\%MM_Args);
    $args =~ s/\{(.*)\}/($1)/s;

    print $fh &lt;&lt;"EOF";
use ExtUtils::MakeMaker;
WriteMakefile
$args;
EOF
  }
}

sub _test_globs {
  my ($self, $build) = @_;

  return map { File::Spec-&gt;catfile($_, '*.t') }
         @{$build-&gt;rscan_dir('t', sub { -d $File::Find::name })};
}

sub subclass_dir {
  my ($self, $build) = @_;

  return (Module::Build::ModuleInfo-&gt;find_module_dir_by_name(ref $build)
	  || File::Spec-&gt;catdir($build-&gt;config_dir, 'lib'));
}

sub unixify_dir {
  my ($self, $path) = @_;
  return join '/', File::Spec-&gt;splitdir($path);
}

sub makefile_to_build_args {
  my $class = shift;
  my @out;
  foreach my $arg (@_) {
    next if $arg eq '';

    my ($key, $val) = ($arg =~ /^(\w+)=(.+)/ ? ($1, $2) :
		       die "Malformed argument '$arg'");

    # Do tilde-expansion if it looks like a tilde prefixed path
    ( $val ) = Module::Build-&gt;_detildefy( $val ) if $val =~ /^~/;

    if (exists $makefile_to_build{$key}) {
      my $trans = $makefile_to_build{$key};
      push @out, $class-&gt;_argvify( ref($trans) ? $trans-&gt;($val) : ($trans =&gt; $val) );
    } elsif (exists $Config{lc($key)}) {
      push @out, $class-&gt;_argvify( config =&gt; lc($key) . "=$val" );
    } else {
      # Assume M::B can handle it in lowercase form
      push @out, $class-&gt;_argvify("\L$key" =&gt; $val);
    }
  }
  return @out;
}

sub _argvify {
  my ($self, @pairs) = @_;
  my @out;
  while (@pairs) {
    my ($k, $v) = splice @pairs, 0, 2;
    push @out, ("--$k", $v);
  }
  return @out;
}

sub makefile_to_build_macros {
  my @out;
  my %config; # must accumulate and return as a hashref
  while (my ($macro, $trans) = each %macro_to_build) {
    # On some platforms (e.g. Cygwin with 'make'), the mere presence
    # of "EXPORT: FOO" in the Makefile will make $ENV{FOO} defined.
    # Therefore we check length() too.
    next unless exists $ENV{$macro} &amp;&amp; length $ENV{$macro};
    my $val = $ENV{$macro};
    my @args = ref($trans) ? $trans-&gt;($val) : ($trans =&gt; $val);
    while (@args) {
      my ($k, $v) = splice(@args, 0, 2);
      if ( $k eq 'config' ) {
        if ( $v =~ /^([^=]+)=(.*)$/ ) {
          $config{$1} = $2;
        }
        else {
          warn "Couldn't parse config '$v'\n";
        }
      }
      else {
        push @out, ($k =&gt; $v);
      }
    }
  }
  push @out, (config =&gt; \%config) if %config;
  return @out;
}

sub run_build_pl {
  my ($pack, %in) = @_;
  $in{script} ||= 'Build.PL';
  my @args = $in{args} ? $pack-&gt;makefile_to_build_args(@{$in{args}}) : ();
  print "# running $in{script} @args\n";
  Module::Build-&gt;run_perl_script($in{script}, [], \@args) or die "Couldn't run $in{script}: $!";
}

sub fake_makefile {
  my ($self, %args) = @_;
  unless (exists $args{build_class}) {
    warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
    $args{build_class} = 'Module::Build';
  }
  my $class = $args{build_class};

  my $perl = $class-&gt;find_perl_interpreter;

  # VMS MMS/MMK need to use MCR to run the Perl image.
  $perl = 'MCR ' . $perl if $self-&gt;_is_vms_mms;

  my $noop = ($class-&gt;is_windowsish ? 'rem&gt;nul'  :
	      $self-&gt;_is_vms_mms    ? 'Continue' :
	      'true');

  my $filetype = $class-&gt;is_vmsish ? '.COM' : '';

  my $Build = 'Build' . $filetype . ' --makefile_env_macros 1';
  my $unlink = $class-&gt;oneliner('1 while unlink $ARGV[0]', [], [$args{makefile}]);
  $unlink =~ s/\$/\$\$/g unless $class-&gt;is_vmsish;

  my $maketext = ($^O eq 'os2' ? "SHELL = sh\n\n" : '');

  $maketext .= &lt;&lt;"EOF";
all : force_do_it
	$perl $Build
realclean : force_do_it
	$perl $Build realclean
	$unlink
distclean : force_do_it
	$perl $Build distclean
	$unlink


force_do_it :
	@ $noop
EOF

  foreach my $action ($class-&gt;known_actions) {
    next if $action =~ /^(all|distclean|realclean|force_do_it)$/;  # Don't double-define
    $maketext .= &lt;&lt;"EOF";
$action : force_do_it
	$perl $Build $action
EOF
  }

  if ($self-&gt;_is_vms_mms) {
    # Roll our own .EXPORT as MMS/MMK don't honor that directive.
    $maketext .= "\n.FIRST\n\t\@ $noop\n";
    for my $macro (keys %macro_to_build) {
      $maketext .= ".IFDEF $macro\n\tDEFINE $macro \"\$($macro)\"\n.ENDIF\n";
    }
    $maketext .= "\n";
  }
  else {
    $maketext .= "\n.EXPORT : " . join(' ', keys %macro_to_build) . "\n\n";
  }

  return $maketext;
}

sub fake_prereqs {
  my $file = File::Spec-&gt;catfile('_build', 'prereqs');
  my $fh = IO::File-&gt;new("&lt; $file") or die "Can't read $file: $!";
  my $prereqs = eval do {local $/; &lt;$fh&gt;};
  close $fh;

  my %merged = _merge_prereq( $prereqs-&gt;{requires}, $prereqs-&gt;{build_requires} );
  my @prereq;
  foreach (sort keys %merged) {
    next if $_ eq 'perl';
    push @prereq, "$_=&gt;q[$merged{$_}]";
  }
  return unless @prereq;
  return "#     PREREQ_PM =&gt; { " . join(", ", @prereq) . " }\n\n";
}


sub write_makefile {
  my ($pack, %in) = @_;

  unless (exists $in{build_class}) {
    warn "Unknown 'build_class', defaulting to 'Module::Build'\n";
    $in{build_class} = 'Module::Build';
  }
  my $class = $in{build_class};
  $in{makefile} ||= $pack-&gt;_is_vms_mms ? 'Descrip.MMS' : 'Makefile';

  open  MAKE, "&gt; $in{makefile}" or die "Cannot write $in{makefile}: $!";
  print MAKE $pack-&gt;fake_prereqs;
  print MAKE $pack-&gt;fake_makefile(%in);
  close MAKE;
}

sub _is_vms_mms {
  return Module::Build-&gt;is_vmsish &amp;&amp; ($Config{make} =~ m/MM[SK]/i);
}

1;
__END__

=for :stopwords passthrough

=head1 NAME

Module::Build::Compat - Compatibility with ExtUtils::MakeMaker

=head1 SYNOPSIS

  # In a Build.PL :
  use Module::Build;
  my $build = Module::Build-&gt;new
    ( module_name =&gt; 'Foo::Bar',
      license     =&gt; 'perl',
      create_makefile_pl =&gt; 'traditional' );
  ...


=head1 DESCRIPTION

Because C&lt;ExtUtils::MakeMaker&gt; has been the standard way to distribute
modules for a long time, many tools (CPAN.pm, or your system
administrator) may expect to find a working F&lt;Makefile.PL&gt; in every
distribution they download from CPAN.  If you want to throw them a
bone, you can use C&lt;Module::Build::Compat&gt; to automatically generate a
F&lt;Makefile.PL&gt; for you, in one of several different styles.

C&lt;Module::Build::Compat&gt; also provides some code that helps out the
F&lt;Makefile.PL&gt; at runtime.


=head1 METHODS

=over 4

=item create_makefile_pl($style, $build)

Creates a F&lt;Makefile.PL&gt; in the current directory in one of several
styles, based on the supplied C&lt;Module::Build&gt; object C&lt;$build&gt;.  This is
typically controlled by passing the desired style as the
C&lt;create_makefile_pl&gt; parameter to C&lt;Module::Build&gt;'s C&lt;new()&gt; method;
the F&lt;Makefile.PL&gt; will then be automatically created during the
C&lt;distdir&gt; action.

The currently supported styles are:

=over 4

=item traditional

A F&lt;Makefile.PL&gt; will be created in the "traditional" style, i.e. it will
use C&lt;ExtUtils::MakeMaker&gt; and won't rely on C&lt;Module::Build&gt; at all.
In order to create the F&lt;Makefile.PL&gt;, we'll include the C&lt;requires&gt; and
C&lt;build_requires&gt; dependencies as the C&lt;PREREQ_PM&gt; parameter.

You don't want to use this style if during the C&lt;perl Build.PL&gt; stage
you ask the user questions, or do some auto-sensing about the user's
environment, or if you subclass C&lt;Module::Build&gt; to do some
customization, because the vanilla F&lt;Makefile.PL&gt; won't do any of that.

=item small

A small F&lt;Makefile.PL&gt; will be created that passes all functionality
through to the F&lt;Build.PL&gt; script in the same directory.  The user must
already have C&lt;Module::Build&gt; installed in order to use this, or else
they'll get a module-not-found error.

=item passthrough (DEPRECATED)

This is just like the C&lt;small&gt; option above, but if C&lt;Module::Build&gt; is
not already installed on the user's system, the script will offer to
use C&lt;CPAN.pm&gt; to download it and install it before continuing with
the build.

This option has been deprecated and may be removed in a future version
of Module::Build.  Modern CPAN.pm and CPANPLUS will recognize the
C&lt;configure_requires&gt; metadata property and install Module::Build before
running Build.PL if Module::Build is listed and Module::Build now
adds itself to configure_requires by default.

Perl 5.10.1 includes C&lt;configure_requires&gt; support.  In the future, when
C&lt;configure_requires&gt; support is deemed sufficiently widespread, the
C&lt;passthrough&gt; style will be removed.

=back

=item run_build_pl(args =&gt; \@ARGV)

This method runs the F&lt;Build.PL&gt; script, passing it any arguments the
user may have supplied to the C&lt;perl Makefile.PL&gt; command.  Because
C&lt;ExtUtils::MakeMaker&gt; and C&lt;Module::Build&gt; accept different arguments, this
method also performs some translation between the two.

C&lt;run_build_pl()&gt; accepts the following named parameters:

=over 4

=item args

The C&lt;args&gt; parameter specifies the parameters that would usually
appear on the command line of the C&lt;perl Makefile.PL&gt; command -
typically you'll just pass a reference to C&lt;@ARGV&gt;.

=item script

This is the filename of the script to run - it defaults to C&lt;Build.PL&gt;.

=back

=item write_makefile()

This method writes a 'dummy' F&lt;Makefile&gt; that will pass all commands
through to the corresponding C&lt;Module::Build&gt; actions.

C&lt;write_makefile()&gt; accepts the following named parameters:

=over 4

=item makefile

The name of the file to write - defaults to the string C&lt;Makefile&gt;.

=back

=back


=head1 SCENARIOS

So, some common scenarios are:

=over 4

=item 1.

Just include a F&lt;Build.PL&gt; script (without a F&lt;Makefile.PL&gt;
script), and give installation directions in a F&lt;README&gt; or F&lt;INSTALL&gt;
document explaining how to install the module.  In particular, explain
that the user must install C&lt;Module::Build&gt; before installing your
module.

Note that if you do this, you may make things easier for yourself, but
harder for people with older versions of CPAN or CPANPLUS on their
system, because those tools generally only understand the
F&lt;Makefile.PL&gt;/C&lt;ExtUtils::MakeMaker&gt; way of doing things.

=item 2.

Include a F&lt;Build.PL&gt; script and a "traditional" F&lt;Makefile.PL&gt;,
created either manually or with C&lt;create_makefile_pl()&gt;.  Users won't
ever have to install C&lt;Module::Build&gt; if they use the F&lt;Makefile.PL&gt;, but
they won't get to take advantage of C&lt;Module::Build&gt;'s extra features
either.

For good measure, of course, test both the F&lt;Makefile.PL&gt; and the
F&lt;Build.PL&gt; before shipping.

=item 3.

Include a F&lt;Build.PL&gt; script and a "pass-through" F&lt;Makefile.PL&gt;
built using C&lt;Module::Build::Compat&gt;.  This will mean that people can
continue to use the "old" installation commands, and they may never
notice that it's actually doing something else behind the scenes.  It
will also mean that your installation process is compatible with older
versions of tools like CPAN and CPANPLUS.

=back


=head1 AUTHOR

Ken Williams &lt;kwilliams@cpan.org&gt;


=head1 COPYRIGHT

Copyright (c) 2001-2006 Ken Williams.  All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.


=head1 SEE ALSO

L&lt;Module::Build&gt;(3), L&lt;ExtUtils::MakeMaker&gt;(3)


=cut
</pre></body></html>