Clean up CPAN.pm's environment variable the same way we do for CPANPLUS. Add an
[p5sagit/local-lib.git] / Makefile.PL
1 use strict;
2 use warnings;
3 use File::Spec;
4 use Cwd;
5 use vars qw($bootstrapping $bootstrapping_args);
6
7 my $cwd;
8 BEGIN {
9   $cwd = Cwd::cwd();
10   # watch out for fancy dashes. these can wind up in our @ARGV if the user is
11   # copypasting the bootstrap command from the POD displayed e.g. by perldoc
12   # on a Mac OS X terminal. since no software recognizes and handles these
13   # dashes, it's better to die loudly telling the user exactly what happened
14   # so they don't make the same mistake again rather than being the only
15   # program in the universe that works with them.
16   if(grep { /−/ } @ARGV) {
17       die <<'DEATH';
18 WHOA THERE! It looks like you've got some fancy dashes in your commandline!
19 These are *not* the traditional -- dashes that software recognizes. You
20 probably got these by copy-pasting from the perldoc for this module as
21 rendered by a UTF8-capable formatter. This most typically happens on an OS X
22 terminal, but can happen elsewhere too. Please try again after replacing the
23 dashes with normal minus signs.
24 DEATH
25   }
26   if (my ($x) = grep { /^--bootstrap(?:=.*)?$/ } @ARGV) {
27     @ARGV = grep { !/^--bootstrap(?:=.*)?$/ } @ARGV;
28     $bootstrapping = 1;
29     my ($path) = $x =~ /^--bootstrap(?:=(.*))?$/;
30     my @args = $path ? $path : ();
31
32     {
33       local @INC = @INC;
34       unshift(@INC, 'lib');
35       require local::lib;
36     }
37
38     local::lib->import(@args);
39
40     my @libs = map { "-I$_" } split ':', $ENV{PERL5LIB};
41     $bootstrapping_args = "@libs";
42     push(@ARGV,$ENV{PERL_MM_OPT});
43     push(@ARGV, @libs);
44
45     system($^X, '-MExtUtils::MakeMaker 6.31', '-e1');
46     my $eumm = $? >> 8;
47
48     system($^X, '-MCPAN 1.80', '-e1');
49     my $cpan = $? >> 8;
50     my $cpan_command = '';
51
52     system($^X, '-MCPAN', '-e',
53       'my $done; require ExtUtils::MakeMaker;
54        my $orig = ExtUtils::MakeMaker->can("prompt");
55        *ExtUtils::MakeMaker::prompt = sub {
56          if (!$done && $_[0] =~ /manual configuration/) {
57            $done++;
58            return "no";
59          }
60          return $orig->(@_);
61        };
62        CPAN::Config->load;
63        unless ($done || -w $CPAN::Config->{keep_source_where}) {
64          my $save = $CPAN::Config->{urllist};
65          delete @{$CPAN::Config}{keys %$CPAN::Config};
66          $CPAN::Config->{urllist} = $save;
67          CPAN::Config->init;
68        }'
69     );
70
71     $ENV{PERL_MM_USE_DEFAULT} = 1;
72
73     # XXX - remove the force on EUMM once its test suite survive PERL_MM_OPT
74
75     if ($eumm) { # non-zero exit
76       $cpan_command .= 'force("install","ExtUtils::MakeMaker"); ';
77     }
78     if ($cpan) {
79       $cpan_command .= 'force("install","CPAN"); ';
80     }
81     if(length $cpan_command) {
82       system($^X, '-MCPAN', '-e', $cpan_command);
83     }
84     if ($cpan) {
85       system($^X, '-MCPAN', '-e', 'CPAN::Config->load; CPAN::Config->commit;');
86     }
87
88     chdir($cwd);
89   }
90 }
91
92 use inc::Module::Install;
93
94 name 'local-lib';
95 all_from 'lib/local/lib.pm';
96
97 requires 'ExtUtils::MakeMaker' => '6.31'; # version INSTALL_BASE was added
98 requires 'ExtUtils::CBuilder'; # this and ParseXS are needed for MB C_support
99 requires 'ExtUtils::ParseXS';
100 requires 'Module::Build' => '0.28'; # lib -> lib/perl5 change
101 requires 'CPAN' => '1.80'; # sudo support
102
103 # No, really. See
104 # https://rt.cpan.org/Public/Bug/Display.html?id=23735
105 # for why CPAN now sets the CPANPLUS env var.
106 # trouble is this means we can't auto_install(_now) CPAN itself
107 # without this beautiful hack
108
109 my $no_cpanplus_env = !exists $ENV{PERL5_CPANPLUS_IS_RUNNING};
110 my $no_cpan_env = !exists $ENV{PERL5_CPAN_IS_RUNNING};
111 require CPAN;
112 delete $ENV{PERL5_CPANPLUS_IS_RUNNING} if $no_cpanplus_env;
113 delete $ENV{PERL5_CPAN_IS_RUNNING} if $no_cpan_env;
114
115 # and make sure that the user doesn't have any existing CPAN config that'll
116 # cause us problems for the next few steps.
117 require CPAN::HandleConfig;
118 CPAN::HandleConfig->load;
119 for my $eumm_setting ( qw/makepl_arg make_install_arg/ ) {
120     if ($CPAN::Config->{$eumm_setting} =~ /(?:PREFIX|INSTALL_BASE)/) {
121         die "Possibly invalid config detected in $eumm_setting: " . $CPAN::Config->{$eumm_setting};
122     }
123 }
124
125 for my $mb_setting (qw/mbuild_arg mbuild_install_arg mbuildpl_arg/) {
126     if ($CPAN::Config->{$mb_setting} =~ /(?:--prefix|--install_base)/) {
127         die "Possibly invalid config detected in $mb_setting: " . $CPAN::Config->{$mb_setting};
128     }
129 }
130
131 if ($bootstrapping) {
132   auto_install_now;
133   postamble <<"END";
134 PERL     += $bootstrapping_args
135 FULLPERL += $bootstrapping_args
136 END
137 } else {
138   auto_install;
139 }
140 chdir($cwd);
141 WriteAll;