2 package ExtUtils::AutoInstall;
3 $ExtUtils::AutoInstall::VERSION = '0.63';
7 use ExtUtils::MakeMaker ();
11 # special map on pre-defined feature sets
13 '' => 'Core Features', # XXX: deprecated
14 '-core' => 'Core Features',
17 # various lexical flags
18 my (@Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS);
19 my ($Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly);
20 my ($PostambleActions, $PostambleUsed);
22 _accept_default(!-t STDIN); # see if it's a non-interactive session
26 $AcceptDefault = shift;
35 [ UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}],
40 # initialize various flags, and/or perform install
42 foreach my $arg (@ARGV, split(/[\s\t]+/, $ENV{PERL_EXTUTILS_AUTOINSTALL} || '')) {
43 if ($arg =~ /^--config=(.*)$/) {
44 $Config = [ split(',', $1) ];
46 elsif ($arg =~ /^--installdeps=(.*)$/) {
47 __PACKAGE__->install($Config, @Missing = split(/,/, $1));
50 elsif ($arg =~ /^--default(?:deps)?$/) {
53 elsif ($arg =~ /^--check(?:deps)?$/) {
56 elsif ($arg =~ /^--skip(?:deps)?$/) {
59 elsif ($arg =~ /^--test(?:only)?$/) {
65 # overrides MakeMaker's prompt() to automatically accept the default choice
67 goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
69 my ($prompt, $default) = @_;
70 my $y = ($default =~ /^[Yy]/);
72 print $prompt, ' [', ($y ? 'Y' : 'y'), '/', ($y ? 'n' : 'N'), '] ';
80 my @args = @_ or return;
83 print "*** $class version ".$class->VERSION."\n";
84 print "*** Checking for dependencies...\n";
90 my $maxlen = length((sort { length($b) <=> length($a) }
92 map { ref($_) ? keys %{ref($_) eq 'HASH' ? $_ : +{@{$_}}} : '' }
93 map { +{@args}->{$_} }
94 grep { /^[^\-]/ or /^-core$/i } keys %{+{@args}})[0]);
96 while (my ($feature, $modules) = splice(@args, 0, 2)) {
97 my (@required, @tests, @skiptests);
101 if ($feature =~ m/^-(\w+)$/) {
104 # check for a newer version of myself
105 _update_to($modules, @_) and return if $option eq 'version';
107 # sets CPAN configuration options
108 $Config = $modules if $option eq 'config';
110 # promote every features to core status
111 $core_all = ($modules =~ /^all$/i) and next
112 if $option eq 'core';
114 next unless $option eq 'core';
117 print "[".($FeatureMap{lc($feature)} || $feature)."]\n";
119 $modules = [ %{$modules} ] if UNIVERSAL::isa($modules, 'HASH');
121 unshift @$modules, -default => &{shift(@$modules)}
122 if (ref($modules->[0]) eq 'CODE'); # XXX: bugward combatability
124 while (my ($mod, $arg) = splice(@$modules, 0, 2)) {
125 if ($mod =~ m/^-(\w+)$/) {
128 $default = $arg if ($option eq 'default');
129 $conflict = $arg if ($option eq 'conflict');
130 @tests = @{$arg} if ($option eq 'tests');
131 @skiptests = @{$arg} if ($option eq 'skiptests');
136 printf("- %-${maxlen}s ...", $mod);
138 # XXX: check for conflicts and uninstalls(!) them.
139 if (defined(my $cur = _version_check(_load($mod), $arg ||= 0))) {
140 print "loaded. ($cur".($arg ? " >= $arg" : '').")\n";
141 push @Existing, $mod => $arg;
142 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
145 print "missing." . ($arg ? " (would need $arg)" : '') . "\n";
146 push @required, $mod => $arg;
150 next unless @required;
152 my $mandatory = ($feature eq '-core' or $core_all);
154 if (!$SkipInstall and ($CheckOnly or _prompt(
155 qq{==> Auto-install the }. (@required / 2).
156 ($mandatory ? ' mandatory' : ' optional').
157 qq{ module(s) from CPAN?}, $default ? 'y' : 'n',
159 push (@Missing, @required);
160 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
163 elsif (!$SkipInstall and $default and $mandatory and _prompt(
164 qq{==> The module(s) are mandatory! Really skip?}, 'n',
166 push (@Missing, @required);
167 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
171 $DisabledTests{$_} = 1 for map { glob($_) } @tests;
175 _check_lock(); # check for $UnderCPAN
177 if (@Missing and not ($CheckOnly or $UnderCPAN)) {
179 print "*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n";
180 # make an educated guess of whether we'll need root permission.
181 print " (You may need to do that as the 'root' user.)\n" if eval '$>';
183 print "*** $class configuration finished.\n";
189 *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
192 # CPAN.pm is non-reentrant, so check if we're under it and have no CPANPLUS
194 return unless @Missing;
195 return if _has_cpanplus();
197 require CPAN; CPAN::Config->load;
198 my $lock = MM->catfile($CPAN::Config->{cpan_home}, ".lock");
200 if (-f $lock and open(LOCK, $lock)
201 and ($^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid())
202 and ($CPAN::Config->{prerequisites_policy} || '') ne 'ignore'
206 *** Since we're running under CPAN, I'll just let it take care
207 of the dependency's installation later.
218 my $i; # used below to strip leading '-' from config keys
219 my @config = (map { s/^-// if ++$i; $_ } @{+shift});
221 my (@modules, @installed);
222 while (my ($pkg, $ver) = splice(@_, 0, 2)) {
223 # grep out those already installed
224 if (defined(_version_check(_load($pkg), $ver))) {
225 push @installed, $pkg;
228 push @modules, $pkg, $ver;
232 return @installed unless @modules; # nothing to do
234 print "*** Installing dependencies...\n";
236 return unless _connected_to('cpan.org');
241 if ($args{do_once} and open(FAILED, '.#autoinstall.failed')) {
242 while (<FAILED>) { chomp; $failed{$_}++ }
246 while (my ($k, $v) = splice(@modules, 0, 2)) {
247 push @newmod, ($k => $v) unless $failed{$k};
252 if (_has_cpanplus()) {
253 _install_cpanplus(\@modules, \@config);
256 _install_cpan(\@modules, \@config);
259 print "*** $class installation finished.\n";
261 # see if we have successfully installed them
262 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
263 if (defined(_version_check(_load($pkg), $ver))) {
264 push @installed, $pkg;
266 elsif ($args{do_once} and open(FAILED, '>> .#autoinstall.failed')) {
267 print FAILED "$pkg\n";
271 close FAILED if $args{do_once};
276 sub _install_cpanplus {
277 my @modules = @{+shift};
278 my @config = @{+shift};
281 require CPANPLUS::Backend;
282 my $cp = CPANPLUS::Backend->new;
283 my $conf = $cp->configure_object;
285 return unless _can_write(
287 ? $conf->get_conf('base') # 0.05x+
288 : $conf->_get_build('base') # 0.04x
291 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
292 my $makeflags = $conf->get_conf('makeflags') || '';
293 if (UNIVERSAL::isa($makeflags, 'HASH')) {
294 # 0.03+ uses a hashref here
295 $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
298 # 0.02 and below uses a scalar
299 $makeflags = join(' ', split(' ', $makeflags), 'UNINST=1')
300 if ($makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' });
302 $conf->set_conf(makeflags => $makeflags);
303 $conf->set_conf(prereqs => 1);
305 while (my ($key, $val) = splice(@config, 0, 2)) {
306 eval { $conf->set_conf($key, $val) };
309 my $modtree = $cp->module_tree;
310 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
311 print "*** Installing $pkg...\n";
313 MY::preinstall($pkg, $ver) or next if defined &MY::preinstall;
316 my $obj = $modtree->{$pkg};
318 if ($obj and defined(_version_check($obj->{version}, $ver))) {
319 my $pathname = $pkg; $pathname =~ s/::/\\W/;
321 foreach my $inc (grep { m/$pathname.pm/i } keys(%INC)) {
325 my $rv = $cp->install( modules => [ $obj->{module} ]);
327 if ($rv and ($rv->{$obj->{module}} or $rv->{ok})) {
328 print "*** $pkg successfully installed.\n";
332 print "*** $pkg installation cancelled.\n";
336 $installed += $success;
340 *** Could not find a version $ver or above for $pkg; skipping.
344 MY::postinstall($pkg, $ver, $success) if defined &MY::postinstall;
351 my @modules = @{+shift};
352 my @config = @{+shift};
356 require CPAN; CPAN::Config->load;
359 return unless _can_write(MM->catfile($CPAN::Config->{cpan_home}, 'sources'))
360 and _can_write($Config::Config{sitelib});
362 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
363 my $makeflags = $CPAN::Config->{make_install_arg} || '';
364 $CPAN::Config->{make_install_arg} = join(' ', split(' ', $makeflags), 'UNINST=1')
365 if ($makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' });
367 # don't show start-up info
368 $CPAN::Config->{inhibit_startup_message} = 1;
370 # set additional options
371 while (my ($opt, $arg) = splice(@config, 0, 2)) {
372 ($args{$opt} = $arg, next)
373 if $opt =~ /^force$/; # pseudo-option
374 $CPAN::Config->{$opt} = $arg;
377 local $CPAN::Config->{prerequisites_policy} = 'follow';
379 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
380 MY::preinstall($pkg, $ver) or next if defined &MY::preinstall;
382 print "*** Installing $pkg...\n";
384 my $obj = CPAN::Shell->expand(Module => $pkg);
387 if ($obj and defined(_version_check($obj->cpan_version, $ver))) {
388 my $pathname = $pkg; $pathname =~ s/::/\\W/;
390 foreach my $inc (grep { m/$pathname.pm/i } keys(%INC)) {
394 $obj->force('install') if $args{force};
396 my $rv = $obj->install || eval {
397 $CPAN::META->instance(
398 'CPAN::Distribution',
400 )->{install} if $CPAN::META
404 print "*** $pkg successfully installed.\n";
408 print "*** $pkg installation failed.\n";
412 $installed += $success;
416 *** Could not find a version $ver or above for $pkg; skipping.
420 MY::postinstall($pkg, $ver, $success) if defined &MY::postinstall;
429 $INC{'CPANPLUS/Config.pm'} or
430 _load('CPANPLUS::Shell::Default')
435 # make guesses on whether we're under the CPAN installation directory
440 my $cwd = File::Spec->canonpath(Cwd::cwd());
441 my $cpan = File::Spec->canonpath($CPAN::Config->{cpan_home});
443 return (index($cwd, $cpan) > -1);
447 my $class = __PACKAGE__;
450 return if defined(_version_check(_load($class), $ver)); # no need to upgrade
453 "==> A newer version of $class ($ver) is required. Install?", 'y'
455 die "*** Please install $class $ver manually.\n";
459 *** Trying to fetch it from CPAN...
463 _load($class) and return $class->import(@_)
464 if $class->install([], $class, $ver);
466 print << '.'; exit 1;
468 *** Cannot bootstrap myself. :-( Installation terminated.
472 # check if we're connected to some host, using inet_aton
477 ( _load('Socket') and Socket::inet_aton($site) ) or _prompt(qq(
478 *** Your host cannot resolve the domain name '$site', which
479 probably means the Internet connections are unavailable.
480 ==> Should we try to install the required module(s) anyway?), 'n'
485 # check if a directory is writable; may create it on demand
488 mkdir ($path, 0755) unless -e $path;
490 return 1 if -w $path;
493 *** You are not allowed to write to the directory '$path';
494 the installation may fail due to insufficient permissions.
497 if (eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(qq(
498 ==> Should we try to re-execute the autoinstall process with 'sudo'?), 'y'
500 # try to bootstrap ourselves from sudo
502 *** Trying to re-execute the autoinstall process with 'sudo'...
504 my $missing = join(',', @Missing);
505 my $config = join(',',
506 UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}
509 return unless system('sudo', $^X, $0, "--config=$config", "--installdeps=$missing");
512 *** The 'sudo' command exited with error! Resuming...
517 ==> Should we try to install the required module(s) anyway?), 'n'
521 # load a module and return the version it reports
523 my $mod = pop; # class/instance doesn't matter
530 return eval { require $file; $mod->VERSION } || ($@ ? undef : 0);
533 # compare two versions, either use Sort::Versions or plain comparison
535 my ($cur, $min) = @_;
536 return unless defined $cur;
540 # check for version numbers that are not in decimal format
541 if (ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./) {
542 if ($version::VERSION or defined(_load('version'))) {
543 # use version.pm if it is installed.
544 return ((version->new($cur) >= version->new($min)) ? $cur : undef);
546 elsif ($Sort::Versions::VERSION or defined(_load('Sort::Versions'))) {
547 # use Sort::Versions as the sorting algorithm for a.b.c versions
548 return ((Sort::Versions::versioncmp($cur, $min) != -1) ? $cur : undef);
551 warn "Cannot reliably compare non-decimal formatted versions.\n".
552 "Please install version.pm or Sort::Versions.\n";
556 local $^W = 0; # shuts off 'not numeric' bugs
557 return ($cur >= $min ? $cur : undef);
560 # nothing; this usage is deprecated.
561 sub main::PREREQ_PM { return {}; }
566 $args{PREREQ_PM} = { %{$args{PREREQ_PM} || {} }, @Existing, @Missing }
567 if $UnderCPAN or $TestOnly;
569 if ($args{EXE_FILES}) {
570 require ExtUtils::Manifest;
571 my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
574 grep { exists $manifest->{$_} } @{$args{EXE_FILES}}
578 $args{test}{TESTS} ||= 't/*.t';
579 $args{test}{TESTS} = join(' ', grep {
580 !exists($DisabledTests{$_})
581 } map { glob($_) } split(/\s+/, $args{test}{TESTS}));
583 my $missing = join(',', @Missing);
584 my $config = join(',',
585 UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}
588 $PostambleActions = (
589 $missing ? "\$(PERL) $0 --config=$config --installdeps=$missing"
596 # a wrapper to ExtUtils::MakeMaker::WriteMakefile
599 Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
603 *** Makefile not written in check-only mode.
608 my %args = _make_args(@_);
613 local *MY::postamble = \&postamble unless defined &MY::postamble;
614 ExtUtils::MakeMaker::WriteMakefile(%args);
616 print << "." unless $PostambleUsed;
617 *** WARNING: Makefile written with customized MY::postamble() without
618 including contents from ExtUtils::AutoInstall::postamble() --
619 auto installation features disabled. Please contact the author.
630 config :: installdeps
634 \t\$(PERL) $0 --checkdeps