Put splitting PERL_LOCAL_LIB_ROOT into its own method
[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 $no_manpages);
6 use Config;
7
8 # Solaris (and possibly other Unices) have a tar in /usr/bin that, among
9 # other things, does not understand @LongLink. This can cause 
10 # extraction to look like it succeeded, but it actually failed (because
11 # the error message for the @LongLink failure scrolled offscreen).
12 # Therefore, given the fact that GNU tar is the most widespread tar available,
13 # and it actually supports the feature we want (I'd bet it originated in GNU
14 # tar, but I digress), we'll look for GNU tar. If we don't find it, and the
15 # user hasn't pointed us to a suitable tar, we'll bomb and tell them what to
16 # do.
17
18 my $have_gtar = 0;
19 if($^O eq 'solaris') {
20   $have_gtar = 0;
21   for my $env_path (split /:/, $ENV{PATH}) {
22     $have_gtar = 1 if -x File::Spec->catfile($env_path, 'gtar');
23   }
24 } else {
25   $have_gtar = 1;
26 }
27 if(exists $ENV{PERL_LL_TAR}) {
28   $have_gtar ||= -x $ENV{PERL_LL_TAR};
29 }
30
31 die <<'DEATH' unless $have_gtar;
32 You are using Solaris (or another traditional Unix) that does not provide a sane
33 tar, capable of dealing with the output of GNU tar. Please either set the
34 PERL_LL_TAR environment variable to the location of a version of tar that
35 understands the @LongLink convention or put a binary named gtar somewhere on
36 your PATH.
37 DEATH
38
39 my $cwd;
40 BEGIN {
41   $cwd = Cwd::cwd();
42   # watch out for fancy dashes. these can wind up in our @ARGV if the user is
43   # copypasting the bootstrap command from the POD displayed e.g. by perldoc
44   # on a Mac OS X terminal. since no software recognizes and handles these
45   # dashes, it's better to die loudly telling the user exactly what happened
46   # so they don't make the same mistake again rather than being the only
47   # program in the universe that works with them.
48   if(grep { /−/ } @ARGV) {
49       die <<'DEATH';
50 WHOA THERE! It looks like you've got some fancy dashes in your commandline!
51 These are *not* the traditional -- dashes that software recognizes. You
52 probably got these by copy-pasting from the perldoc for this module as
53 rendered by a UTF8-capable formatter. This most typically happens on an OS X
54 terminal, but can happen elsewhere too. Please try again after replacing the
55 dashes with normal minus signs.
56 DEATH
57   }
58   if (my ($x) = grep { /^--bootstrap(?:=.*)?$/ } @ARGV) {
59     @ARGV = grep { !/^--bootstrap(?:=.*)?$/ } @ARGV;
60     $bootstrapping = 1;
61     if(my ($x) = grep { /^--no-manpages/ } @ARGV) {
62       $no_manpages = 1;
63       @ARGV = grep { !/^--no-manpages/ } @ARGV;
64     }
65     my ($path) = $x =~ /^--bootstrap(?:=(.*))?$/;
66     my @args = $path ? $path : ();
67
68     {
69       local @INC = @INC;
70       unshift(@INC, 'lib');
71       require local::lib;
72     }
73
74     local::lib->import(@args);
75
76     my @libs = map { "-I$_" } split $Config{path_sep}, $ENV{PERL5LIB};
77     $bootstrapping_args = "@libs";
78     push(@ARGV,$ENV{PERL_MM_OPT});
79     push(@ARGV, @libs);
80
81     # <mst> GODDAMN makepl_arg INSTALLDIRS=site
82     # <mst> we must set PERL_AUTOINSTALL_PREFER_CPAN too
83     $ENV{PERL_AUTOINSTALL_PREFER_CPAN} = 1;
84
85     system($^X, '-MExtUtils::MakeMaker 6.31', '-e1');
86     my $eumm = $? >> 8;
87
88     system($^X, '-MExtUtils::Install 1.43', '-e1');
89     my $eui = $? >> 8;
90
91     system($^X, '-MModule::Build 0.36', '-e1');
92     my $mb = $? >> 8;
93
94     system($^X, '-MCPAN 1.82', '-e1');
95     my $cpan = $? >> 8;
96     my $cpan_command = '';
97
98     my $did_cpan_config = 0;
99     my $cpan_config_command =
100       'my $done; require ExtUtils::MakeMaker;
101        my $orig = ExtUtils::MakeMaker->can("prompt");
102        *ExtUtils::MakeMaker::prompt = sub ($;$) {
103          if (!$done && $_[0] =~ /manual configuration/) {
104            $done++;
105            return "no";
106          }
107          return $orig->(@_);
108        };
109        # not yet -- apeiron, 2010-03-10 
110        #$CPAN::Config->{urllist} = ["http://cpan.shadowcatprojects.net"];
111        # <mst> all bootstrapped fine on one DH account
112        # <mst> on another, it tries to install man stuff into /usr/local
113        # <mst> cannot for the life of me figure out why
114        # <mst> (same fucking server as well)
115        # <mst> GOT THE BASTARD
116        # <mst> ExtUtils::ParseXS uses Module::Build
117        # <mst> but Module::Build depends on it
118        # <mst> so you need to set prefer_installer MM
119        # <mst> so cpan uses EU::ParseXS Makefile.PL
120        # <mst> since we already got EUMM, *that* works
121        $CPAN::Config->{prefer_installer} = "EUMM";
122        CPAN::Config->load;
123        unless ($done || -w $CPAN::Config->{keep_source_where}) {
124          my $save = $CPAN::Config->{urllist};
125          delete @{$CPAN::Config}{keys %$CPAN::Config};
126          $CPAN::Config->{urllist} = $save;
127          CPAN::Config->init;
128        }';
129
130     $ENV{PERL_MM_USE_DEFAULT} = 1;
131
132     # XXX - remove the force on EUMM once its test suite survive PERL_MM_OPT
133
134     if ($eumm) { # non-zero exit
135       $cpan_command .= 'force("install","ExtUtils::MakeMaker"); ';
136     }
137     if ($eui) {
138       $cpan_command .= 'install("ExtUtils::Install"); ';
139     }
140     if ($mb) {
141       $cpan_command .= 'install("Module::Build"); ';
142     }
143     if ($cpan) {
144       $cpan_command .= 'force("install","CPAN"); ';
145     }
146     if(length $cpan_command) {
147       system($^X, '-MCPAN', '-e', $cpan_config_command);
148       $did_cpan_config++;
149       system($^X, '-MCPAN', '-e', $cpan_command);
150     }
151     if ($cpan) {
152       system($^X, '-MCPAN', '-e', 'CPAN::Config->load; CPAN::Config->commit;');
153     }
154     if($no_manpages) {
155       # if we call this code directly, the changes get written to
156       # $BOOTSTRAP/lib/perl5/CPAN/Config.pm, not where the user expects them to
157       # be in their ~/.cpan/CPAN/MyConfig.pm.
158       system($^X, '-MCPAN', '-e', $cpan_config_command)
159         unless $did_cpan_config;
160       system($^X, '-MCPAN',
161         '-e', 
162         q[CPAN::HandleConfig->load;],
163         '-e', 
164         q[$CPAN::Config->{makepl_arg}  = ] . 
165           q['INSTALLMAN1DIR=none INSTALLMAN3DIR=none';],
166         '-e',
167         q[$CPAN::Config->{buildpl_arg} = ] .
168           q['--install_path libdoc="" --install_path bindoc=""';],
169         '-e',
170         q[CPAN::Config->commit;],
171       );
172     }
173
174     chdir($cwd);
175   }
176 }
177
178 use inc::Module::Install;
179
180 name 'local-lib';
181 all_from 'lib/local/lib.pm';
182
183 requires 'ExtUtils::MakeMaker' => '6.31'; # version INSTALL_BASE was added
184 requires 'ExtUtils::Install' => '1.43'; # ditto
185 requires 'Module::Build' => '0.36'; # PERL_MB_OPT
186 my $required_CPAN = '1.82';
187 requires 'CPAN' => $required_CPAN; # sudo support + CPAN::HandleConfig
188
189 # No, really. See
190 # https://rt.cpan.org/Public/Bug/Display.html?id=23735
191 # for why CPAN now sets the CPANPLUS env var.
192 # trouble is this means we can't auto_install(_now) CPAN itself
193 # without this beautiful hack
194
195 my $no_cpanplus_env = !exists $ENV{PERL5_CPANPLUS_IS_RUNNING};
196 my $no_cpan_env = !exists $ENV{PERL5_CPAN_IS_RUNNING};
197 require CPAN;
198 delete $ENV{PERL5_CPANPLUS_IS_RUNNING} if $no_cpanplus_env;
199 delete $ENV{PERL5_CPAN_IS_RUNNING} if $no_cpan_env;
200
201 # and make sure that the user doesn't have any existing CPAN config that'll
202 # cause us problems for the next few steps.
203 {
204     local $@;
205     eval { require CPAN::HandleConfig; };
206     # Need newish CPAN.pm for this, ergo skip it if that version of CPAN isn't
207     # installed yet.
208     # It will already be installed by the time we reach here if bootstrapping,
209     # otherwise, if we're running from CPAN then it will be installed soon
210     # enough, and we'll come back here..
211     if (!$@ ) {
212         CPAN::HandleConfig->require_myconfig_or_config;
213         if ( $CPAN::Config ) {
214             for my $eumm_setting ( qw/makepl_arg make_install_arg/ ) {
215                 if ($CPAN::Config->{$eumm_setting} =~ /(?:PREFIX|INSTALL_BASE)/) {
216                     die <<"DEATH";
217 WHOA THERE! It looks like you've got $CPAN::Config->{$eumm_setting} set. This is
218 known to cause problems with local::lib. Please either remove this setting or
219 clear out your .cpan directory.
220 DEATH
221                 }
222             }
223
224             for my $mb_setting (qw/mbuild_arg mbuild_install_arg mbuildpl_arg/) {
225                 if ($CPAN::Config->{$mb_setting} =~ /(?:--prefix|--install_base)/) {
226                     die <<"DEATH";
227 WHOA THERE! It looks like you've got $CPAN::Config->{$mb_setting} set. This is
228 known to cause problems with local::lib. Please either remove this setting or
229 clear out your .cpan directory.
230 DEATH
231                 }
232             }
233         }
234     }
235     else {
236         my $error = $@;
237         require CPAN;
238         # Explode if it looks like requiring CPAN::HandleConfig should
239         # have worked, but didn't.
240         die($error) if $CPAN::VERSION >= $required_CPAN;
241     }
242 }
243
244 if ($bootstrapping) {
245   auto_install_now;
246   postamble <<"END";
247 PERL     += $bootstrapping_args
248 FULLPERL += $bootstrapping_args
249 END
250 } else {
251   auto_install;
252 }
253 chdir($cwd);
254 resources(
255   repository => 'git://git.shadowcat.co.uk/p5sagit/local-lib.git',
256   homepage => 'http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit/local-lib.git',
257   bugtracker => 'http://rt.cpan.org/Public/Dist/Display.html?Name=local-lib',
258 );
259 WriteAll;