File Coverage

blib/lib/Module/Build/Base.pm
Criterion Covered Total %
statement 1953 2685 72.7
branch 798 1420 56.2
condition 233 472 49.3
subroutine 268 323 82.9
pod 0 198 0.0
total 3252 5098 63.7


line stmt bran cond sub pod time code
1             # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2             # vim:ts=8:sw=2:et:sta:sts=2
3             package Module::Build::Base;
4              
5 297     297   6818 use 5.006;
  297         1022  
6 297     293   1718 use strict;
  293         581  
  293         5740  
7 293     293   1566 use warnings;
  293         527  
  293         13561  
8              
9             our $VERSION = '0.42_33';
10             $VERSION = eval $VERSION;
11              
12 293     293   1745 use Carp;
  293         802  
  293         18519  
13 293     293   2117 use Cwd ();
  293         577  
  293         9422  
14 293     293   150998 use File::Copy ();
  293         699824  
  293         6814  
15 293     293   1970 use File::Find ();
  293         591  
  293         3743  
16 293     293   1401 use File::Path ();
  293         571  
  293         3747  
17 293     293   1329 use File::Basename ();
  293         677  
  293         5005  
18 293     293   1397 use File::Spec 0.82 ();
  293         5235  
  293         4968  
19 293     293   138601 use File::Compare ();
  293         284808  
  293         6384  
20 293     293   131138 use Module::Build::Dumper ();
  293         728  
  293         5849  
21 293     293   139154 use Text::ParseWords ();
  293         387279  
  293         7780  
22              
23 293     293   181167 use Module::Metadata;
  293         2358931  
  293         11033  
24 293     293   151290 use Module::Build::Notes;
  293         967  
  293         9615  
25 293     293   130708 use Module::Build::Config;
  293         738  
  293         8619  
26 293     293   1743 use version;
  293         580  
  293         1496  
27              
28              
29             #################### Constructors ###########################
30             sub new {
31 91     91 0 281793 my $self = shift()->_construct(@_);
32              
33 80   50     1319 $self->{invoked_action} = $self->{action} ||= 'Build_PL';
34 80         1947 $self->cull_args(@ARGV);
35              
36             die "Too early to specify a build action '$self->{action}'. Do 'Build $self->{action}' instead.\n"
37 80 50 33     1026 if $self->{action} && $self->{action} ne 'Build_PL';
38              
39 80         1372 $self->check_manifest;
40 80         1252 $self->auto_require;
41              
42             # All checks must run regardless if one fails, so no short circuiting!
43 80 100       697 if( grep { !$_ } $self->check_prereq, $self->check_autofeatures ) {
  160         816  
44 10         70 $self->log_warn(<
45              
46             ERRORS/WARNINGS FOUND IN PREREQUISITES. You may wish to install the versions
47             of the modules indicated above before proceeding with this installation
48              
49             EOF
50 10 50 33     155 unless (
      33        
51             $self->dist_name eq 'Module-Build' ||
52             $ENV{PERL5_CPANPLUS_IS_RUNNING} || $ENV{PERL5_CPAN_IS_RUNNING}
53             ) {
54 10         40 $self->log_warn(
55             "Run 'Build installdeps' to install missing prerequisites.\n\n"
56             );
57             }
58             }
59              
60             # record for later use in resume;
61 80         1039 $self->{properties}{_added_to_INC} = [ $self->_added_to_INC ];
62              
63 69         1970 $self->set_bundle_inc;
64              
65 69         850 $self->dist_name;
66 69         801 $self->dist_version;
67 69         390 $self->release_status;
68 69 100       227 $self->_guess_module_name unless $self->module_name;
69              
70 69         863 $self->_find_nested_builds;
71              
72 69         2400 return $self;
73             }
74              
75             sub resume {
76 469     469 0 33254 my $package = shift;
77 469         22479 my $self = $package->_construct(@_);
78 466         9009 $self->read_config;
79              
80 466 50       2187 my @added_earlier = @{ $self->{properties}{_added_to_INC} || [] };
  466         5652  
81              
82 466         8129 @INC = ($self->_added_to_INC, @added_earlier, $self->_default_INC);
83              
84             # If someone called Module::Build->current() or
85             # Module::Build->new_from_context() and the correct class to use is
86             # actually a *subclass* of Module::Build, we may need to load that
87             # subclass here and re-delegate the resume() method to it.
88 463 50       7980 unless ( $package->isa($self->build_class) ) {
89 0         0 my $build_class = $self->build_class;
90 0   0     0 my $config_dir = $self->config_dir || '_build';
91 0         0 my $build_lib = File::Spec->catdir( $config_dir, 'lib' );
92 0         0 unshift( @INC, $build_lib );
93 0 0       0 unless ( $build_class->can('new') ) {
94 0 0       0 eval "require $build_class; 1" or die "Failed to re-load '$build_class': $@";
95             }
96 0         0 return $build_class->resume(@_);
97             }
98              
99 463 50       6038 unless ($self->_perl_is_same($self->{properties}{perl})) {
100 0         0 my $perl = $self->find_perl_interpreter;
101 0         0 die(<<"DIEFATAL");
102             * FATAL ERROR: Perl interpreter mismatch. Configuration was initially
103             created with '$self->{properties}{perl}'
104             but we are now using '$perl'. You must
105             run 'Build realclean' or 'make realclean' and re-configure.
106             DIEFATAL
107             }
108              
109 367         24552 $self->cull_args(@ARGV);
110              
111 367 50       6877 unless ($self->allow_mb_mismatch) {
112 367         3005 my $mb_version = $Module::Build::VERSION;
113 367 50       6974 if ( $mb_version ne $self->{properties}{mb_version} ) {
114 0         0 $self->log_warn(<<"MISMATCH");
115             * WARNING: Configuration was initially created with Module::Build
116             version '$self->{properties}{mb_version}' but we are now using version '$mb_version'.
117             If errors occur, you must re-run the Build.PL or Makefile.PL script.
118             MISMATCH
119             }
120             }
121              
122 367   50     10247 $self->{invoked_action} = $self->{action} ||= 'build';
123              
124 367         48283 return $self;
125             }
126              
127             sub new_from_context {
128 481     481 0 3060540 my ($package, %args) = @_;
129              
130 481         12043 $package->run_perl_script('Build.PL',[],[$package->unparse_args(\%args)]);
131 427         32367 return $package->resume;
132             }
133              
134             sub current {
135             # hmm, wonder what the right thing to do here is
136 21     21 0 5396 local @ARGV;
137 21         260 return shift()->resume;
138             }
139              
140             sub _construct {
141 560     560   6035 my ($package, %input) = @_;
142              
143 560   50     17659 my $args = delete $input{args} || {};
144 560   100     12659 my $config = delete $input{config} || {};
145              
146 560         24825 my $self = bless {
147             args => {%$args},
148             config => Module::Build::Config->new(values => $config),
149             properties => {
150             base_dir => $package->cwd,
151             mb_version => $Module::Build::VERSION,
152             %input,
153             },
154             phash => {},
155             stash => {}, # temporary caching, not stored in _build
156             }, $package;
157              
158 560         29385 $self->_set_defaults;
159 560         4663 my ($p, $ph) = ($self->{properties}, $self->{phash});
160              
161 560         3252 foreach (qw(notes config_data features runtime_params cleanup auto_features)) {
162 3360         14731 my $file = File::Spec->catfile($self->config_dir, $_);
163 3360         44509 $ph->{$_} = Module::Build::Notes->new(file => $file);
164 3360 100       86143 $ph->{$_}->restore if -e $file;
165 3360 50       16465 if (exists $p->{$_}) {
166 0         0 my $vals = delete $p->{$_};
167 0         0 foreach my $k (sort keys %$vals) {
168 0         0 $self->$_($k, $vals->{$k});
169             }
170             }
171             }
172              
173             # The following warning could be unnecessary if the user is running
174             # an embedded perl, but there aren't too many of those around, and
175             # embedded perls aren't usually used to install modules, and the
176             # installation process sometimes needs to run external scripts
177             # (e.g. to run tests).
178 560 50       8118 $p->{perl} = $self->find_perl_interpreter
179             or $self->log_warn("Warning: Can't locate your perl binary");
180              
181 546     1634   9895 my $blibdir = sub { File::Spec->catdir($p->{blib}, @_) };
  1634         21320  
182 546   100     8869 $p->{bindoc_dirs} ||= [ $blibdir->("script") ];
183 546   100     7933 $p->{libdoc_dirs} ||= [ $blibdir->("lib"), $blibdir->("arch") ];
184              
185 546 100 66     4339 $p->{dist_author} = [ $p->{dist_author} ] if defined $p->{dist_author} and not ref $p->{dist_author};
186              
187             # Synonyms
188 546 50       2938 $p->{requires} = delete $p->{prereq} if defined $p->{prereq};
189 546 100       2395 $p->{script_files} = delete $p->{scripts} if defined $p->{scripts};
190              
191             # Convert to from shell strings to arrays
192 546         2543 for ('extra_compiler_flags', 'extra_linker_flags') {
193 1092 50       15568 $p->{$_} = [ $self->split_like_shell($p->{$_}) ] if exists $p->{$_};
194             }
195              
196             # Convert to arrays
197 546         2589 for ('include_dirs') {
198 546 100 66     10660 $p->{$_} = [ $p->{$_} ] if exists $p->{$_} && !ref $p->{$_}
199             }
200              
201 0         0 $self->add_to_cleanup( @{delete $p->{add_to_cleanup}} )
202 546 50       3254 if $p->{add_to_cleanup};
203              
204 546         11555 return $self;
205             }
206              
207             ################## End constructors #########################
208              
209             sub log_info {
210 168     168 0 533 my $self = shift;
211 168 100 66     2308 print @_ if ref($self) && ( $self->verbose || ! $self->quiet );
      66        
212             }
213             sub log_verbose {
214 4817     4817 0 17509 my $self = shift;
215 4817 100 100     41192 print @_ if ref($self) && $self->verbose;
216             }
217             sub log_debug {
218 790     790 0 1787 my $self = shift;
219 790 50 33     5363 print @_ if ref($self) && $self->debug;
220             }
221              
222             sub log_warn {
223             # Try to make our call stack invisible
224 103     103 0 626 shift;
225 103 50 33     2408 if (@_ and $_[-1] !~ /\n$/) {
226 0         0 my (undef, $file, $line) = caller();
227 0         0 warn @_, " at $file line $line.\n";
228             } else {
229 103         27195 warn @_;
230             }
231             }
232              
233              
234             # install paths must be generated when requested to be sure all changes
235             # to config (from various sources) are included
236             sub _default_install_paths {
237 365     365   696 my $self = shift;
238 365         749 my $c = $self->{config};
239 365         684 my $p = {};
240              
241 365 50       1633 my @libstyle = $c->get('installstyle') ?
242             File::Spec->splitdir($c->get('installstyle')) : qw(lib perl5);
243 365         1247 my $arch = $c->get('archname');
244 365         1218 my $version = $c->get('version');
245              
246 365   100     1032 my $bindoc = $c->get('installman1dir') || undef;
247 365   100     1028 my $libdoc = $c->get('installman3dir') || undef;
248              
249 365   100     1014 my $binhtml = $c->get('installhtml1dir') || $c->get('installhtmldir') || undef;
250 365   100     1241 my $libhtml = $c->get('installhtml3dir') || $c->get('installhtmldir') || undef;
251              
252             $p->{install_sets} =
253             {
254 365   33     1234 core => {
      33        
      66        
      66        
      66        
      66        
      33        
      33        
      66        
      66        
      66        
      66        
255             lib => $c->get('installprivlib'),
256             arch => $c->get('installarchlib'),
257             bin => $c->get('installbin'),
258             script => $c->get('installscript'),
259             bindoc => $bindoc,
260             libdoc => $libdoc,
261             binhtml => $binhtml,
262             libhtml => $libhtml,
263             },
264             site => {
265             lib => $c->get('installsitelib'),
266             arch => $c->get('installsitearch'),
267             bin => $c->get('installsitebin') || $c->get('installbin'),
268             script => $c->get('installsitescript') ||
269             $c->get('installsitebin') || $c->get('installscript'),
270             bindoc => $c->get('installsiteman1dir') || $bindoc,
271             libdoc => $c->get('installsiteman3dir') || $libdoc,
272             binhtml => $c->get('installsitehtml1dir') || $binhtml,
273             libhtml => $c->get('installsitehtml3dir') || $libhtml,
274             },
275             vendor => {
276             lib => $c->get('installvendorlib'),
277             arch => $c->get('installvendorarch'),
278             bin => $c->get('installvendorbin') || $c->get('installbin'),
279             script => $c->get('installvendorscript') ||
280             $c->get('installvendorbin') || $c->get('installscript'),
281             bindoc => $c->get('installvendorman1dir') || $bindoc,
282             libdoc => $c->get('installvendorman3dir') || $libdoc,
283             binhtml => $c->get('installvendorhtml1dir') || $binhtml,
284             libhtml => $c->get('installvendorhtml3dir') || $libhtml,
285             },
286             };
287              
288             $p->{original_prefix} =
289             {
290 365 50 0     2865 core => $c->get('installprefixexp') || $c->get('installprefix') ||
291             $c->get('prefixexp') || $c->get('prefix') || '',
292             site => $c->get('siteprefixexp'),
293             vendor => $c->get('usevendorprefix') ? $c->get('vendorprefixexp') : '',
294             };
295 365   33     1285 $p->{original_prefix}{site} ||= $p->{original_prefix}{core};
296              
297             # Note: you might be tempted to use $Config{installstyle} here
298             # instead of hard-coding lib/perl5, but that's been considered and
299             # (at least for now) rejected. `perldoc Config` has some wisdom
300             # about it.
301             $p->{install_base_relpaths} =
302             {
303 365         2701 lib => ['lib', 'perl5'],
304             arch => ['lib', 'perl5', $arch],
305             bin => ['bin'],
306             script => ['bin'],
307             bindoc => ['man', 'man1'],
308             libdoc => ['man', 'man3'],
309             binhtml => ['html'],
310             libhtml => ['html'],
311             };
312              
313             $p->{prefix_relpaths} =
314             {
315 365         8123 core => {
316             lib => [@libstyle],
317             arch => [@libstyle, $version, $arch],
318             bin => ['bin'],
319             script => ['bin'],
320             bindoc => ['man', 'man1'],
321             libdoc => ['man', 'man3'],
322             binhtml => ['html'],
323             libhtml => ['html'],
324             },
325             vendor => {
326             lib => [@libstyle],
327             arch => [@libstyle, $version, $arch],
328             bin => ['bin'],
329             script => ['bin'],
330             bindoc => ['man', 'man1'],
331             libdoc => ['man', 'man3'],
332             binhtml => ['html'],
333             libhtml => ['html'],
334             },
335             site => {
336             lib => [@libstyle, 'site_perl'],
337             arch => [@libstyle, 'site_perl', $version, $arch],
338             bin => ['bin'],
339             script => ['bin'],
340             bindoc => ['man', 'man1'],
341             libdoc => ['man', 'man3'],
342             binhtml => ['html'],
343             libhtml => ['html'],
344             },
345             };
346 365         2265 return $p
347             }
348              
349             sub _find_nested_builds {
350 69     69   217 my $self = shift;
351 69 50       661 my $r = $self->recurse_into or return;
352              
353 69         233 my ($file, @r);
354 69 50 33     322 if (!ref($r) && $r eq 'auto') {
355 0         0 local *DH;
356 0 0       0 opendir DH, $self->base_dir
357             or die "Can't scan directory " . $self->base_dir . " for nested builds: $!";
358 0         0 while (defined($file = readdir DH)) {
359 0         0 my $subdir = File::Spec->catdir( $self->base_dir, $file );
360 0 0       0 next unless -d $subdir;
361 0 0       0 push @r, $subdir if -e File::Spec->catfile( $subdir, 'Build.PL' );
362             }
363             }
364              
365 69         286 $self->recurse_into(\@r);
366             }
367              
368             sub cwd {
369 578     578 0 2418766 return Cwd::cwd();
370             }
371              
372             sub _quote_args {
373             # Returns a string that can become [part of] a command line with
374             # proper quoting so that the subprocess sees this same list of args.
375 7     7   112 my ($self, @args) = @_;
376              
377 7         21 my @quoted;
378              
379 7         63 for (@args) {
380 28 100       301 if ( /^[^\s*?!\$<>;\\|'"\[\]\{\}]+$/ ) {
381             # Looks pretty safe
382 21         161 push @quoted, $_;
383             } else {
384             # XXX this will obviously have to improve - is there already a
385             # core module lying around that does proper quoting?
386 7         70 s/('+)/'"$1"'/g;
387 7         49 push @quoted, qq('$_');
388             }
389             }
390              
391 7         63 return join " ", @quoted;
392             }
393              
394             sub _backticks {
395 2532     2532   3523290 my ($self, @cmd) = @_;
396 2532 50       17785 if ($self->have_forkpipe) {
397 2532         10570 local *FH;
398 2532         2722044 my $pid = open *FH, "-|";
399 2532 100       139575 if ($pid) {
400 2290 100       5794426499 return wantarray ? : join '', ;
401             } else {
402 242 50       31847 die "Can't execute @cmd: $!\n" unless defined $pid;
403 242         7737 exec { $cmd[0] } @cmd;
  242         0  
404             }
405             } else {
406 0         0 my $cmd = $self->_quote_args(@cmd);
407 0         0 return `$cmd`;
408             }
409             }
410              
411             # Tells us whether the construct open($fh, '-|', @command) is
412             # supported. It would probably be better to dynamically sense this.
413 2532     2532 0 11174 sub have_forkpipe { 1 }
414              
415             # Determine whether a given binary is the same as the perl
416             # (configuration) that started this process.
417             sub _perl_is_same {
418 751     751   4729 my ($self, $perl) = @_;
419              
420 751         4098 my @cmd = ($perl);
421              
422             # When run from the perl core, @INC will include the directories
423             # where perl is yet to be installed. We need to reference the
424             # absolute path within the source distribution where it can find
425             # it's Config.pm This also prevents us from picking up a Config.pm
426             # from a different configuration that happens to be already
427             # installed in @INC.
428 751 50       11688 if ($ENV{PERL_CORE}) {
429 0         0 push @cmd, '-I' . File::Spec->catdir(File::Basename::dirname($perl), 'lib');
430             }
431              
432 751         9913 push @cmd, qw(-MConfig=myconfig -e print -e myconfig);
433 751         9427 return $self->_backticks(@cmd) eq Config->myconfig;
434             }
435              
436             # cache _discover_perl_interpreter() results
437             {
438             my $known_perl;
439             sub find_perl_interpreter {
440 4282     4282 0 15479 my $self = shift;
441              
442 4282 100       29044 return $known_perl if defined($known_perl);
443 288         4369 return $known_perl = $self->_discover_perl_interpreter;
444             }
445             }
446              
447             # Returns the absolute path of the perl interpreter used to invoke
448             # this process. The path is derived from $^X or $Config{perlpath}. On
449             # some platforms $^X contains the complete absolute path of the
450             # interpreter, on other it may contain a relative path, or simply
451             # 'perl'. This can also vary depending on whether a path was supplied
452             # when perl was invoked. Additionally, the value in $^X may omit the
453             # executable extension on platforms that use one. It's a fatal error
454             # if the interpreter can't be found because it can result in undefined
455             # behavior by routines that depend on it (generating errors or
456             # invoking the wrong perl.)
457             sub _discover_perl_interpreter {
458 288     288   1126 my $proto = shift;
459 288 100       2597 my $c = ref($proto) ? $proto->{config} : 'Module::Build::Config';
460              
461 288         2416 my $perl = $^X;
462 288         26076 my $perl_basename = File::Basename::basename($perl);
463              
464 288         1240 my @potential_perls;
465              
466             # Try 1, Check $^X for absolute path
467 288 50       11084 push( @potential_perls, $perl )
468             if File::Spec->file_name_is_absolute($perl);
469              
470             # Try 2, Check $^X for a valid relative path
471 288         5760 my $abs_perl = File::Spec->rel2abs($perl);
472 288         1339 push( @potential_perls, $abs_perl );
473              
474             # Try 3, Last ditch effort: These two option use hackery to try to locate
475             # a suitable perl. The hack varies depending on whether we are running
476             # from an installed perl or an uninstalled perl in the perl source dist.
477 288 50       1809 if ($ENV{PERL_CORE}) {
478              
479             # Try 3.A, If we are in a perl source tree, running an uninstalled
480             # perl, we can keep moving up the directory tree until we find our
481             # binary. We wouldn't do this under any other circumstances.
482              
483             # CBuilder is also in the core, so it should be available here
484 0         0 require ExtUtils::CBuilder;
485 0         0 my $perl_src = Cwd::realpath( ExtUtils::CBuilder->perl_src );
486 0 0 0     0 if ( defined($perl_src) && length($perl_src) ) {
487 0         0 my $uninstperl =
488             File::Spec->rel2abs(File::Spec->catfile( $perl_src, $perl_basename ));
489 0         0 push( @potential_perls, $uninstperl );
490             }
491              
492             } else {
493              
494             # Try 3.B, First look in $Config{perlpath}, then search the user's
495             # PATH. We do not want to do either if we are running from an
496             # uninstalled perl in a perl source tree.
497              
498 288         6019 push( @potential_perls, $c->get('perlpath') );
499              
500 288         23102 push( @potential_perls,
501             map File::Spec->catfile($_, $perl_basename), File::Spec->path() );
502             }
503              
504             # Now that we've enumerated the potential perls, it's time to test
505             # them to see if any of them match our configuration, returning the
506             # absolute path of the first successful match.
507 288         2517 my $exe = $c->get('exe_ext');
508 288         2341 foreach my $thisperl ( @potential_perls ) {
509              
510 288 50       2090 if (defined $exe) {
511 288 50       5438 $thisperl .= $exe unless $thisperl =~ m/$exe$/i;
512             }
513              
514 288 50 66     12517 if ( -f $thisperl && $proto->_perl_is_same($thisperl) ) {
515 242         12745 return $thisperl;
516             }
517             }
518              
519             # We've tried all alternatives, and didn't find a perl that matches
520             # our configuration. Throw an exception, and list alternatives we tried.
521 0         0 my @paths = map File::Basename::dirname($_), @potential_perls;
522 0         0 die "Can't locate the perl binary used to run this script " .
523             "in (@paths)\n";
524             }
525              
526             # Adapted from IPC::Cmd::can_run()
527             sub find_command {
528 7     7 0 22 my ($self, $command) = @_;
529              
530 7 50       43 if( File::Spec->file_name_is_absolute($command) ) {
531 7         24 return $self->_maybe_command($command);
532              
533             } else {
534 0         0 for my $dir ( File::Spec->path ) {
535 0         0 my $abs = File::Spec->catfile($dir, $command);
536 0 0       0 return $abs if $abs = $self->_maybe_command($abs);
537             }
538             }
539             }
540              
541             # Copied from ExtUtils::MM_Unix::maybe_command
542             sub _maybe_command {
543 7     7   20 my($self,$file) = @_;
544 7 50 33     205 return $file if -x $file && ! -d $file;
545 7         30 return;
546             }
547              
548             sub _is_interactive {
549 0   0 0   0 return -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; # Pipe?
550             }
551              
552             # NOTE this is a blocking operation if(-t STDIN)
553             sub _is_unattended {
554 8     8   15 my $self = shift;
555             return $ENV{PERL_MM_USE_DEFAULT} ||
556 8   66     60 ( !$self->_is_interactive && eof STDIN );
557             }
558              
559             sub _readline {
560 0     0   0 my $self = shift;
561 0 0       0 return undef if $self->_is_unattended;
562              
563 0         0 my $answer = ;
564 0 0       0 chomp $answer if defined $answer;
565 0         0 return $answer;
566             }
567              
568             sub prompt {
569 9     9 0 2514 my $self = shift;
570 9 100       49 my $mess = shift
571             or die "prompt() called without a prompt message";
572              
573             # use a list to distinguish a default of undef() from no default
574 8         14 my @def;
575 8 100       27 @def = (shift) if @_;
576             # use dispdef for output
577 8 100       56 my @dispdef = scalar(@def) ?
    100          
578             ('[', (defined($def[0]) ? $def[0] . ' ' : ''), ']') :
579             (' ', '');
580              
581 8         53 local $|=1;
582 8         90 print "$mess ", @dispdef;
583              
584 8 100 66     43 if ( $self->_is_unattended && !@def ) {
585 2         20 die <
586             ERROR: This build seems to be unattended, but there is no default value
587             for this question. Aborting.
588             EOF
589             }
590              
591 6         85 my $ans = $self->_readline();
592              
593 6 100 66     47 if ( !defined($ans) # Ctrl-D or unattended
594             or !length($ans) ) { # User hit return
595 4         35 print "$dispdef[1]\n";
596 4 100       21 $ans = scalar(@def) ? $def[0] : '';
597             }
598              
599 6         30 return $ans;
600             }
601              
602             sub y_n {
603 5     5 0 3676 my $self = shift;
604 5         16 my ($mess, $def) = @_;
605              
606 5 100       35 die "y_n() called without a prompt message" unless $mess;
607 4 100 100     104 die "Invalid default value: y_n() default must be 'y' or 'n'"
608             if $def && $def !~ /^[yn]/i;
609              
610 3         14 my $answer;
611 3         9 while (1) { # XXX Infinite or a large number followed by an exception ?
612 3         10 $answer = $self->prompt(@_);
613 2 50       25 return 1 if $answer =~ /^y/i;
614 0 0       0 return 0 if $answer =~ /^n/i;
615 0         0 local $|=1;
616 0         0 print "Please answer 'y' or 'n'.\n";
617             }
618             }
619              
620 0     0 0 0 sub current_action { shift->{action} }
621 112     112 0 1231 sub invoked_action { shift->{invoked_action} }
622              
623 31     31 0 6546 sub notes { shift()->{phash}{notes}->access(@_) }
624 2     2 0 1852 sub config_data { shift()->{phash}{config_data}->access(@_) }
625 4 50   4 0 116 sub runtime_params { shift->{phash}{runtime_params}->read( @_ ? shift : () ) } # Read-only
626 82     82 0 881 sub auto_features { shift()->{phash}{auto_features}->access(@_) }
627              
628             sub features {
629 2     2 0 2448 my $self = shift;
630 2         17 my $ph = $self->{phash};
631              
632 2 50       19 if (@_) {
633 2         14 my $key = shift;
634 2 50       29 if ($ph->{features}->exists($key)) {
635 0         0 return $ph->{features}->access($key, @_);
636             }
637              
638 2 100       27 if (my $info = $ph->{auto_features}->access($key)) {
639 1         15 my $disabled;
640 1         7 for my $type ( @{$self->prereq_action_types} ) {
  1         31  
641 5 100 66     44 next if $type eq 'description' || $type eq 'recommends' || ! exists $info->{$type};
      100        
642 1         13 my $prereqs = $info->{$type};
643 1         11 for my $modname ( sort keys %$prereqs ) {
644 1         8 my $spec = $prereqs->{$modname};
645 1         25 my $status = $self->check_installed_status($modname, $spec);
646 1 50 25     15 if ((!$status->{ok}) xor ($type =~ /conflicts$/)) { return 0; }
  0         0  
647 1 50       73 if ( ! eval "require $modname; 1" ) { return 0; }
  0         0  
648             }
649             }
650 1         9 return 1;
651             }
652              
653 1         14 return $ph->{features}->access($key, @_);
654             }
655              
656             # No args - get the auto_features & overlay the regular features
657 0         0 my %features;
658 0         0 my %auto_features = $ph->{auto_features}->access();
659 0         0 while (my ($name, $info) = each %auto_features) {
660 0         0 my $failures = $self->prereq_failures($info);
661 0 0       0 my $disabled = grep( /^(?:\w+_)?(?:requires|conflicts)$/,
662             keys %$failures ) ? 1 : 0;
663 0 0       0 $features{$name} = $disabled ? 0 : 1;
664             }
665 0         0 %features = (%features, $ph->{features}->access());
666              
667 0 0       0 return wantarray ? %features : \%features;
668             }
669 293     293   1454537 BEGIN { *feature = \&features } # Alias
670              
671             sub _mb_feature {
672 56     56   161 my $self = shift;
673              
674 56 50 50     460 if (($self->module_name || '') eq 'Module::Build') {
675             # We're building Module::Build itself, so ...::ConfigData isn't
676             # valid, but $self->features() should be.
677 0         0 return $self->feature(@_);
678             } else {
679 56         7569 require Module::Build::ConfigData;
680 56         849 return Module::Build::ConfigData->feature(@_);
681             }
682             }
683              
684             sub _warn_mb_feature_deps {
685 0     0   0 my $self = shift;
686 0         0 my $name = shift;
687 0         0 $self->log_warn(
688             "The '$name' feature is not available. Please install missing\n" .
689             "feature dependencies and try again.\n".
690             $self->_feature_deps_msg($name) . "\n"
691             );
692             }
693              
694             sub add_build_element {
695 2     2 0 768 my ($self, $elem) = @_;
696 2         19 my $elems = $self->build_elements;
697 2 100       5 push @$elems, $elem unless grep { $_ eq $elem } @$elems;
  15         47  
698             }
699              
700             sub ACTION_config_data {
701 61     61 0 257 my $self = shift;
702 61 100       690 return unless $self->has_config_data;
703              
704 1 50       15 my $module_name = $self->module_name
705             or die "The config_data feature requires that 'module_name' be set";
706 1         12 my $notes_name = $module_name . '::ConfigData'; # TODO: Customize name ???
707 1         6 my $notes_pm = File::Spec->catfile($self->blib, 'lib', split /::/, "$notes_name.pm");
708              
709 1 50       7 return if $self->up_to_date(['Build.PL',
710             $self->config_file('config_data'),
711             $self->config_file('features')
712             ], $notes_pm);
713              
714 1         24 $self->log_verbose("Writing config notes to $notes_pm\n");
715 1         142 File::Path::mkpath(File::Basename::dirname($notes_pm));
716              
717             Module::Build::Notes->write_config_data
718             (
719             file => $notes_pm,
720             module => $module_name,
721             config_module => $notes_name,
722             config_data => scalar $self->config_data,
723 1         7 feature => scalar $self->{phash}{features}->access(),
724             auto_features => scalar $self->auto_features,
725             );
726             }
727              
728             ########################################################################
729             { # enclosing these lexicals -- TODO
730             my %valid_properties = ( __PACKAGE__, {} );
731             my %additive_properties;
732              
733             sub _mb_classes {
734 31127   66 31127   102151 my $class = ref($_[0]) || $_[0];
735 31127         84564 return ($class, $class->mb_parents);
736             }
737              
738             sub valid_property {
739 27990     27990 0 46881 my ($class, $prop) = @_;
740 27990         45345 return grep exists( $valid_properties{$_}{$prop} ), $class->_mb_classes;
741             }
742              
743             sub valid_properties {
744 0     0 0 0 return keys %{ shift->valid_properties_defaults() };
  0         0  
745             }
746              
747             sub valid_properties_defaults {
748 560     560 0 3697 my %out;
749 560         11084 for my $class (reverse shift->_mb_classes) {
750 1719         202555 @out{ keys %{ $valid_properties{$class} } } = map {
751 49868         215047 $_->()
752 1719         8491 } values %{ $valid_properties{$class} };
  1719         20734  
753             }
754 560         4774 return \%out;
755             }
756              
757             sub array_properties {
758 561 100   561 0 4343 map { exists $additive_properties{$_}->{ARRAY} ? @{$additive_properties{$_}->{ARRAY}} : () } shift->_mb_classes;
  1723         9194  
  562         6327  
759             }
760              
761             sub hash_properties {
762 2016 100   2016 0 14775 map { exists $additive_properties{$_}->{HASH} ? @{$additive_properties{$_}->{HASH}} : () } shift->_mb_classes;
  6181         23884  
  2038         32546  
763             }
764              
765             sub add_property {
766 26102     26102 0 70780 my ($class, $property) = (shift, shift);
767 26102 100       43746 die "Property '$property' already exists"
768             if $class->valid_property($property);
769 26101 100       65631 my %p = @_ == 1 ? ( default => shift ) : @_;
770              
771 26101         43733 my $type = ref $p{default};
772             $valid_properties{$class}{$property} =
773             $type eq 'CODE' ? $p{default} :
774 8406     8406   15299 $type eq 'HASH' ? sub { return { %{ $p{default} } } } :
  8406         55496  
775 3920     3920   8162 $type eq 'ARRAY'? sub { return [ @{ $p{default} } ] } :
  3920         44008  
776 26101 100   37536   128969 sub { return $p{default} } ;
  37536 100       153642  
    100          
777              
778 26101 100       60202 push @{$additive_properties{$class}->{$type}}, $property
  6458         14735  
779             if $type;
780              
781 26101 100       119365 unless ($class->can($property)) {
782             # TODO probably should put these in a util package
783 20241 100       48563 my $sub = $type eq 'HASH'
784             ? _make_hash_accessor($property, \%p)
785             : _make_accessor($property, \%p);
786 293     293   2460 no strict 'refs';
  293         707  
  293         386827  
787 20241         31296 *{"$class\::$property"} = $sub;
  20241         61382  
788             }
789              
790 26101         58283 return $class;
791             }
792              
793             sub property_error {
794 4     4 0 67 my $self = shift;
795 4         42 die 'ERROR: ', @_;
796             }
797              
798             sub _set_defaults {
799 560     560   4231 my $self = shift;
800              
801             # Set the build class.
802 560   66     24494 $self->{properties}{build_class} ||= ref $self;
803              
804             # If there was no orig_dir, set to the same as base_dir
805 560   33     12136 $self->{properties}{orig_dir} ||= $self->{properties}{base_dir};
806              
807 560         12464 my $defaults = $self->valid_properties_defaults;
808              
809 560         10865 foreach my $prop (keys %$defaults) {
810             $self->{properties}{$prop} = $defaults->{$prop}
811 49868 100       146342 unless exists $self->{properties}{$prop};
812             }
813              
814             # Copy defaults for arrays any arrays.
815 560         11077 for my $prop ($self->array_properties) {
816 0         0 $self->{properties}{$prop} = [@{$defaults->{$prop}}]
817 3920 50       12211 unless exists $self->{properties}{$prop};
818             }
819             # Copy defaults for arrays any hashes.
820 560         12153 for my $prop ($self->hash_properties) {
821 0         0 $self->{properties}{$prop} = {%{$defaults->{$prop}}}
822 8406 50       28539 unless exists $self->{properties}{$prop};
823             }
824             }
825              
826             } # end enclosure
827             ########################################################################
828             sub _make_hash_accessor {
829 2935     2935   5315 my ($property, $p) = @_;
830 2935   100 3   19365 my $check = $p->{check} || sub { 1 };
  3         14  
831              
832             return sub {
833 1260     1260   17487 my $self = shift;
834              
835             # This is only here to deprecate the historic accident of calling
836             # properties as class methods - I suspect it only happens in our
837             # test suite.
838 1260 50       8510 unless(ref($self)) {
839 0         0 carp("\n$property not a class method (@_)");
840 0         0 return;
841             }
842              
843 1260         5757 my $x = $self->{properties};
844 1260 100       9279 return $x->{$property} unless @_;
845              
846 8         23 my $prop = $x->{$property};
847 8 100 100     106 if ( defined $_[0] && !ref $_[0] ) {
848 4 100       31 if ( @_ == 1 ) {
    50          
849 1 50       9 return exists $prop->{$_[0]} ? $prop->{$_[0]} : undef;
850             } elsif ( @_ % 2 == 0 ) {
851 3         7 my %new = (%{ $prop }, @_);
  3         17  
852 3         13 local $_ = \%new;
853 3 50       13 $x->{$property} = \%new if $check->($self);
854 3         38 return $x->{$property};
855             } else {
856 0         0 die "Unexpected arguments for property '$property'\n";
857             }
858             } else {
859 4 100 100     28 die "Unexpected arguments for property '$property'\n"
860             if defined $_[0] && ref $_[0] ne 'HASH';
861 3         7 local $_ = $_[0];
862 3 50       8 $x->{$property} = shift if $check->($self);
863             }
864 2935         12702 };
865             }
866             ########################################################################
867             sub _make_accessor {
868 17306     17306   29808 my ($property, $p) = @_;
869 17306   100 179   74486 my $check = $p->{check} || sub { 1 };
  179         10346  
870              
871             return sub {
872 11998     11998   164204 my $self = shift;
873              
874             # This is only here to deprecate the historic accident of calling
875             # properties as class methods - I suspect it only happens in our
876             # test suite.
877 11998 50       41216 unless(ref($self)) {
878 0         0 carp("\n$property not a class method (@_)");
879 0         0 return;
880             }
881              
882 11998         32046 my $x = $self->{properties};
883 11998 100       167753 return $x->{$property} unless @_;
884 187         608 local $_ = $_[0];
885 187 50       825 $x->{$property} = shift if $check->($self);
886 184         1043 return $x->{$property};
887 17306         72323 };
888             }
889             ########################################################################
890              
891             # Add the default properties.
892             __PACKAGE__->add_property(auto_configure_requires => 1);
893             __PACKAGE__->add_property(blib => 'blib');
894             __PACKAGE__->add_property(build_class => 'Module::Build');
895             __PACKAGE__->add_property(build_elements => [qw(PL support pm xs share_dir pod script)]);
896             __PACKAGE__->add_property(build_script => 'Build');
897             __PACKAGE__->add_property(build_bat => 0);
898             __PACKAGE__->add_property(bundle_inc => []);
899             __PACKAGE__->add_property(bundle_inc_preload => []);
900             __PACKAGE__->add_property(config_dir => '_build');
901             __PACKAGE__->add_property(dynamic_config => 1);
902             __PACKAGE__->add_property(include_dirs => []);
903             __PACKAGE__->add_property(license => 'unknown');
904             __PACKAGE__->add_property(metafile => 'META.yml');
905             __PACKAGE__->add_property(mymetafile => 'MYMETA.yml');
906             __PACKAGE__->add_property(metafile2 => 'META.json');
907             __PACKAGE__->add_property(mymetafile2 => 'MYMETA.json');
908             __PACKAGE__->add_property(recurse_into => []);
909             __PACKAGE__->add_property(use_rcfile => 1);
910             __PACKAGE__->add_property(create_packlist => 1);
911             __PACKAGE__->add_property(allow_mb_mismatch => 0);
912             __PACKAGE__->add_property(config => undef);
913             __PACKAGE__->add_property(test_file_exts => ['.t']);
914             __PACKAGE__->add_property(use_tap_harness => 0);
915             __PACKAGE__->add_property(cpan_client => 'cpan');
916             __PACKAGE__->add_property(tap_harness_args => {});
917             __PACKAGE__->add_property(pureperl_only => 0);
918             __PACKAGE__->add_property(allow_pureperl => 0);
919             __PACKAGE__->add_property(
920             'installdirs',
921             default => 'site',
922             check => sub {
923             return 1 if /^(core|site|vendor)$/;
924             return shift->property_error(
925             $_ eq 'perl'
926             ? 'Perhaps you meant installdirs to be "core" rather than "perl"?'
927             : 'installdirs must be one of "core", "site", or "vendor"'
928             );
929             return shift->property_error("Perhaps you meant 'core'?") if $_ eq 'perl';
930             return 0;
931             },
932             );
933              
934             {
935             __PACKAGE__->add_property(html_css => '');
936             }
937              
938             {
939             my @prereq_action_types = qw(requires build_requires test_requires conflicts recommends);
940             foreach my $type (@prereq_action_types) {
941             __PACKAGE__->add_property($type => {});
942             }
943             __PACKAGE__->add_property(prereq_action_types => \@prereq_action_types);
944             }
945              
946             __PACKAGE__->add_property($_ => {}) for qw(
947             get_options
948             install_base_relpaths
949             install_path
950             install_sets
951             meta_add
952             meta_merge
953             original_prefix
954             prefix_relpaths
955             configure_requires
956             );
957              
958             __PACKAGE__->add_property($_) for qw(
959             PL_files
960             autosplit
961             base_dir
962             bindoc_dirs
963             c_source
964             cover
965             create_license
966             create_makefile_pl
967             create_readme
968             debugger
969             destdir
970             dist_abstract
971             dist_author
972             dist_name
973             dist_suffix
974             dist_version
975             dist_version_from
976             extra_compiler_flags
977             extra_linker_flags
978             has_config_data
979             install_base
980             libdoc_dirs
981             magic_number
982             mb_version
983             module_name
984             needs_compiler
985             orig_dir
986             perl
987             pm_files
988             pod_files
989             pollute
990             prefix
991             program_name
992             quiet
993             recursive_test_files
994             release_status
995             script_files
996             scripts
997             share_dir
998             sign
999             test_files
1000             verbose
1001             debug
1002             xs_files
1003             extra_manify_args
1004             );
1005              
1006             sub config {
1007 7884     7884 0 59979 my $self = shift;
1008 7884 100       40931 my $c = ref($self) ? $self->{config} : 'Module::Build::Config';
1009 7884 100       29535 return $c->all_config unless @_;
1010              
1011 7854         54374 my $key = shift;
1012 7854 100       118012 return $c->get($key) unless @_;
1013              
1014 1         3 my $val = shift;
1015 1         6 return $c->set($key => $val);
1016             }
1017              
1018             sub mb_parents {
1019             # Code borrowed from Class::ISA.
1020 31131     31131 0 69795 my @in_stack = (shift);
1021 31131         69630 my %seen = ($in_stack[0] => 1);
1022              
1023 31131         53189 my ($current, @out);
1024 31131         72574 while (@in_stack) {
1025 41615 100 66     244773 next unless defined($current = shift @in_stack)
1026             && $current->isa('Module::Build::Base');
1027 41614         86897 push @out, $current;
1028 41614 100       115394 next if $current eq 'Module::Build::Base';
1029 293     293   2594 no strict 'refs';
  293         1560  
  293         1629476  
1030             unshift @in_stack,
1031             map {
1032 10485         34121 my $c = $_; # copy, to avoid being destructive
1033 10485 50       37741 substr($c,0,2) = "main::" if substr($c,0,2) eq '::';
1034             # Canonize the :: -> main::, ::foo -> main::foo thing.
1035             # Should I ever canonize the Foo'Bar = Foo::Bar thing?
1036 10485 100       80209 $seen{$c}++ ? () : $c;
1037 10483         21857 } @{"$current\::ISA"};
  10483         72411  
1038              
1039             # I.e., if this class has any parents (at least, ones I've never seen
1040             # before), push them, in order, onto the stack of classes I need to
1041             # explore.
1042             }
1043 31131         46532 shift @out;
1044 31131         127078 return @out;
1045             }
1046              
1047 19     19 0 224 sub extra_linker_flags { shift->_list_accessor('extra_linker_flags', @_) }
1048 23     23 0 8083 sub extra_compiler_flags { shift->_list_accessor('extra_compiler_flags', @_) }
1049              
1050             sub _list_accessor {
1051 42     42   352 (my $self, local $_) = (shift, shift);
1052 42         231 my $p = $self->{properties};
1053 42 50       208 $p->{$_} = [@_] if @_;
1054 42 50       222 $p->{$_} = [] unless exists $p->{$_};
1055 42 50       940 return ref($p->{$_}) ? $p->{$_} : [$p->{$_}];
1056             }
1057              
1058             # XXX Problem - if Module::Build is loaded from a different directory,
1059             # it'll look for (and perhaps destroy/create) a _build directory.
1060             sub subclass {
1061 22     22 0 149871 my ($pack, %opts) = @_;
1062              
1063 22         343 my $build_dir = '_build'; # XXX The _build directory is ostensibly settable by the user. Shouldn't hard-code here.
1064 22 100       1466 $pack->delete_filetree($build_dir) if -e $build_dir;
1065              
1066             die "Must provide 'code' or 'class' option to subclass()\n"
1067 22 50 66     614 unless $opts{code} or $opts{class};
1068              
1069 22   100     552 $opts{code} ||= '';
1070 22   100     757 $opts{class} ||= 'MyModuleBuilder';
1071              
1072 22         705 my $filename = File::Spec->catfile($build_dir, 'lib', split '::', $opts{class}) . '.pm';
1073 22         2729 my $filedir = File::Basename::dirname($filename);
1074 22         674 $pack->log_verbose("Creating custom builder $filename in $filedir\n");
1075              
1076 22         7888 File::Path::mkpath($filedir);
1077 22 50       581 die "Can't create directory $filedir: $!" unless -d $filedir;
1078              
1079 22 50       2311 open(my $fh, '>', $filename) or die "Can't create $filename: $!";
1080 22         470 print $fh <
1081             package $opts{class};
1082             use $pack;
1083             our \@ISA = qw($pack);
1084             $opts{code}
1085             1;
1086             EOF
1087 22         967 close $fh;
1088              
1089 22         2360 unshift @INC, File::Spec->catdir(File::Spec->rel2abs($build_dir), 'lib');
1090 22         4448 eval "use $opts{class}";
1091 22 50       141 die $@ if $@;
1092              
1093 22         434 return $opts{class};
1094             }
1095              
1096             sub _guess_module_name {
1097 5     5   65 my $self = shift;
1098 5         145 my $p = $self->{properties};
1099 5 50       50 return if $p->{module_name};
1100 5 50 33     105 if ( $p->{dist_version_from} && -e $p->{dist_version_from} ) {
1101 0         0 my $mi = Module::Metadata->new_from_file($self->dist_version_from);
1102 0         0 $p->{module_name} = $mi->name;
1103             }
1104             else {
1105 5         50 my $mod_path = my $mod_name = $p->{dist_name};
1106 5         50 $mod_name =~ s{-}{::}g;
1107 5         15 $mod_path =~ s{-}{/}g;
1108 5         35 $mod_path .= ".pm";
1109 5 50 33     255 if ( -e $mod_path || -e "lib/$mod_path" ) {
1110 5         35 $p->{module_name} = $mod_name;
1111             }
1112             else {
1113 0         0 $self->log_warn( << 'END_WARN' );
1114             No 'module_name' was provided and it could not be inferred
1115             from other properties. This will prevent a packlist from
1116             being written for this file. Please set either 'module_name'
1117             or 'dist_version_from' in Build.PL.
1118             END_WARN
1119             }
1120             }
1121             }
1122              
1123             sub dist_name {
1124 225     225 0 7651 my $self = shift;
1125 225         643 my $p = $self->{properties};
1126 225         936 my $me = 'dist_name';
1127 225 100       2359 return $p->{$me} if defined $p->{$me};
1128              
1129 63 50       622 die "Can't determine distribution name, must supply either 'dist_name' or 'module_name' parameter"
1130             unless $self->module_name;
1131              
1132 63         323 ($p->{$me} = $self->module_name) =~ s/::/-/g;
1133              
1134 63         490 return $p->{$me};
1135             }
1136              
1137             sub release_status {
1138 141     141 0 57395 my ($self) = @_;
1139 141         937 my $me = 'release_status';
1140 141         549 my $p = $self->{properties};
1141              
1142 141 100       646 if ( ! defined $p->{$me} ) {
1143 69 50       619 $p->{$me} = $self->_is_dev_version ? 'testing' : 'stable';
1144             }
1145              
1146 141 50       3489 unless ( $p->{$me} =~ qr/\A(?:stable|testing|unstable)\z/ ) {
1147 0         0 die "Illegal value '$p->{$me}' for $me\n";
1148             }
1149              
1150 141 50 66     2212 if ( $p->{$me} eq 'stable' && $self->_is_dev_version ) {
1151 0         0 my $version = $self->dist_version;
1152 0         0 die "Illegal value '$p->{$me}' with version '$version'\n";
1153             }
1154 141         1643 return $p->{$me};
1155             }
1156              
1157             sub dist_suffix {
1158 40     40 0 20969 my ($self) = @_;
1159 40         132 my $p = $self->{properties};
1160 40         220 my $me = 'dist_suffix';
1161              
1162 40 100       304 return $p->{$me} if defined $p->{$me};
1163              
1164 22 100       197 if ( $self->release_status eq 'stable' ) {
1165 13         77 $p->{$me} = "";
1166             }
1167             else {
1168             # non-stable release but non-dev version number needs '-TRIAL' appended
1169 9 50       190 $p->{$me} = $self->_is_dev_version ? "" : "TRIAL" ;
1170             }
1171              
1172 22         350 return $p->{$me};
1173             }
1174              
1175             sub dist_version_from {
1176 170     170 0 541 my ($self) = @_;
1177 170         435 my $p = $self->{properties};
1178 170         777 my $me = 'dist_version_from';
1179              
1180 170 50       1061 if ($self->module_name) {
1181 170   66     1514 $p->{$me} ||=
1182             join( '/', 'lib', split(/::/, $self->module_name) ) . '.pm';
1183             }
1184 170   50     1576 return $p->{$me} || undef;
1185             }
1186              
1187             sub dist_version {
1188 374     374 0 1061 my ($self) = @_;
1189 374         964 my $p = $self->{properties};
1190 374         1476 my $me = 'dist_version';
1191              
1192 374 100       2785 return $p->{$me} if defined $p->{$me};
1193              
1194 63 50       687 if ( my $dist_version_from = $self->dist_version_from ) {
1195 63         2169 my $version_from = File::Spec->catfile( split( qr{/}, $dist_version_from ) );
1196 63 50       1795 my $pm_info = Module::Metadata->new_from_file( $version_from )
1197             or die "Can't find file $version_from to determine version";
1198             #$p->{$me} is undef here
1199 63         101593 $p->{$me} = $self->normalize_version( $pm_info->version() );
1200 63 50       742 unless (defined $p->{$me}) {
1201 0         0 die "Can't determine distribution version from $version_from";
1202             }
1203             }
1204              
1205             die ("Can't determine distribution version, must supply either 'dist_version',\n".
1206             "'dist_version_from', or 'module_name' parameter")
1207 63 50       633 unless defined $p->{$me};
1208              
1209 63         200 return $p->{$me};
1210             }
1211              
1212             sub _is_dev_version {
1213 199     199   643 my ($self) = @_;
1214 199         649 my $dist_version = $self->dist_version;
1215 199         552 my $version_obj = eval { version->new( $dist_version ) };
  199         2416  
1216             # assume it's normal if the version string is fatal -- in this case
1217             # the author might be doing something weird so should play along and
1218             # assume they'll specify all necessary behavior
1219 199 100       2245 return $@ ? 0 : $version_obj->is_alpha;
1220             }
1221              
1222 67     67 0 12821 sub dist_author { shift->_pod_parse('author') }
1223 57     57 0 237 sub dist_abstract { shift->_pod_parse('abstract') }
1224              
1225             sub _pod_parse {
1226 124     124   536 my ($self, $part) = @_;
1227 124         361 my $p = $self->{properties};
1228 124         375 my $member = "dist_$part";
1229 124 100       1295 return $p->{$member} if defined $p->{$member};
1230              
1231 57 50       588 my $docfile = $self->_main_docfile
1232             or return;
1233 57 50       2685 open(my $fh, '<', $docfile)
1234             or return;
1235              
1236 57         15837 require Module::Build::PodParser;
1237 57         631 my $parser = Module::Build::PodParser->new(fh => $fh);
1238 57         209 my $method = "get_$part";
1239 57         322 return $p->{$member} = $parser->$method();
1240             }
1241              
1242             sub version_from_file { # Method provided for backwards compatibility
1243 1     1 0 1949 return Module::Metadata->new_from_file($_[1])->version();
1244             }
1245              
1246             sub find_module_by_name { # Method provided for backwards compatibility
1247 0     0 0 0 return Module::Metadata->find_module_by_name(@_[1,2]);
1248             }
1249              
1250             {
1251             # $unlink_list_for_pid{$$} = [ ... ]
1252             my %unlink_list_for_pid;
1253              
1254             sub _unlink_on_exit {
1255 0     0   0 my $self = shift;
1256 0         0 for my $f ( @_ ) {
1257 0 0       0 push @{$unlink_list_for_pid{$$}}, $f if -f $f;
  0         0  
1258             }
1259 0         0 return 1;
1260             }
1261              
1262             END {
1263 69 50   69   274559 for my $f ( map glob($_), @{ $unlink_list_for_pid{$$} || [] } ) {
  69         4487  
1264 18 0       396 next unless -e $f;
1265 0           File::Path::rmtree($f, 0, 0);
1266             }
1267             }
1268             }
1269              
1270             sub add_to_cleanup {
1271 191     191 0 4901 my $self = shift;
1272 191         725 my %files = map {$self->localize_file_path($_), 1} @_;
  193         1817  
1273 191         2618 $self->{phash}{cleanup}->write(\%files);
1274             }
1275              
1276             sub cleanup {
1277 51     51 0 1705 my $self = shift;
1278 51         701 my $all = $self->{phash}{cleanup}->read;
1279 51 100       3977 return wantarray ? sort keys %$all : keys %$all;
1280             }
1281              
1282             sub config_file {
1283 492     492 0 1547 my $self = shift;
1284 492 50       2159 return unless -d $self->config_dir;
1285 492         3882 return File::Spec->catfile($self->config_dir, @_);
1286             }
1287              
1288             sub read_config {
1289 466     466 0 3011 my ($self) = @_;
1290              
1291 466 50       4446 my $file = $self->config_file('build_params')
1292             or die "Can't find 'build_params' in " . $self->config_dir;
1293 466 50       22937 open(my $fh, '<', $file) or die "Can't read '$file': $!";
1294 466         2286 my $ref = eval do {local $/; <$fh>};
  466         3195  
  466         228110  
1295 466 50       4186 die if $@;
1296 466         8007 close $fh;
1297 466         1910 my $c;
1298 466         9493 ($self->{args}, $c, $self->{properties}) = @$ref;
1299 466         9546 $self->{config} = Module::Build::Config->new(values => $c);
1300             }
1301              
1302             sub has_config_data {
1303 61     61 0 193 my $self = shift;
1304 61         766 return scalar grep $self->{phash}{$_}->has_data(), qw(config_data features auto_features);
1305             }
1306              
1307             sub _write_data {
1308 18     18   66 my ($self, $filename, $data) = @_;
1309              
1310 18         77 my $file = $self->config_file($filename);
1311 18 50       1196 open(my $fh, '>', $file) or die "Can't create '$file': $!";
1312 18 100       104 unless (ref($data)) { # e.g. magicnum
1313 6         155 print $fh $data;
1314 6         513 return;
1315             }
1316              
1317 12         36 print {$fh} Module::Build::Dumper->_data_dump($data);
  12         165  
1318 12         6046 close $fh;
1319             }
1320              
1321             sub write_config {
1322 6     6 0 29 my ($self) = @_;
1323              
1324 6         401 File::Path::mkpath($self->{properties}{config_dir});
1325 6 50       87 -d $self->{properties}{config_dir} or die "Can't mkdir $self->{properties}{config_dir}: $!";
1326              
1327 6         19 my @items = @{ $self->prereq_action_types };
  6         71  
1328 6         33 $self->_write_data('prereqs', { map { $_, $self->$_() } @items });
  30         229  
1329 6         140 $self->_write_data('build_params', [$self->{args}, $self->{config}->values_set, $self->{properties}]);
1330              
1331             # Set a new magic number and write it to a file
1332 6         219 $self->_write_data('magicnum', $self->magic_number(int rand 1_000_000));
1333              
1334 6         162 $self->{phash}{$_}->write() foreach qw(notes cleanup features auto_features config_data runtime_params);
1335             }
1336              
1337             {
1338             # packfile map -- keys are guts of regular expressions; If they match,
1339             # values are module names corresponding to the packlist
1340             my %packlist_map = (
1341             '^File::Spec' => 'Cwd',
1342             '^Devel::AssertOS' => 'Devel::CheckOS',
1343             );
1344              
1345             sub _find_packlist {
1346 0     0   0 my ($self, $inst, $mod) = @_;
1347 0         0 my $lookup = $mod;
1348 0         0 my $packlist = eval { $inst->packlist($lookup) };
  0         0  
1349 0 0       0 if ( ! $packlist ) {
1350             # try from packlist_map
1351 0         0 while ( my ($re, $new_mod) = each %packlist_map ) {
1352 0 0       0 if ( $mod =~ qr/$re/ ) {
1353 0         0 $lookup = $new_mod;
1354 0         0 $packlist = eval { $inst->packlist($lookup) };
  0         0  
1355 0         0 last;
1356             }
1357             }
1358             }
1359 0 0       0 return $packlist ? $lookup : undef;
1360             }
1361              
1362             sub set_bundle_inc {
1363 69     69 0 262 my $self = shift;
1364              
1365 69         404 my $bundle_inc = $self->{properties}{bundle_inc};
1366 69         523 my $bundle_inc_preload = $self->{properties}{bundle_inc_preload};
1367             # We're in author mode if inc::latest is loaded, but not from cwd
1368 69 50       1938 return unless inc::latest->can('loaded_modules');
1369 0         0 require ExtUtils::Installed;
1370             # ExtUtils::Installed is buggy about finding additions to default @INC
1371 0         0 my $inst = eval { ExtUtils::Installed->new(extra_libs => [@INC]) };
  0         0  
1372 0 0       0 if ($@) {
1373 0         0 $self->log_warn( << "EUI_ERROR" );
1374             Bundling in inc/ is disabled because ExtUtils::Installed could not
1375             create a list of your installed modules. Here is the error:
1376             $@
1377             EUI_ERROR
1378 0         0 return;
1379             }
1380 0         0 my @bundle_list = map { [ $_, 0 ] } inc::latest->loaded_modules;
  0         0  
1381              
1382             # XXX TODO: Need to get ordering of prerequisites correct so they are
1383             # are loaded in the right order. Use an actual tree?!
1384              
1385 0         0 while( @bundle_list ) {
1386 0         0 my ($mod, $prereq) = @{ shift @bundle_list };
  0         0  
1387              
1388             # XXX TODO: Append prereqs to list
1389             # skip if core or already in bundle or preload lists
1390             # push @bundle_list, [$_, 1] for prereqs()
1391              
1392             # Locate packlist for bundling
1393 0         0 my $lookup = $self->_find_packlist($inst,$mod);
1394 0 0       0 if ( ! $lookup ) {
1395             # XXX Really needs a more helpful error message here
1396 0         0 die << "NO_PACKLIST";
1397             Could not find a packlist for '$mod'. If it's a core module, try
1398             force installing it from CPAN.
1399             NO_PACKLIST
1400             }
1401             else {
1402 0 0       0 push @{ $prereq ? $bundle_inc_preload : $bundle_inc }, $lookup;
  0         0  
1403             }
1404             }
1405             } # sub check_bundling
1406             }
1407              
1408             sub check_autofeatures {
1409 80     80 0 310 my ($self) = @_;
1410 80         896 my $features = $self->auto_features;
1411              
1412 80 50       391 return 1 unless %$features;
1413              
1414             # TODO refactor into ::Util
1415             my $longest = sub {
1416 0 0   0   0 my @str = @_ or croak("no strings given");
1417              
1418 0         0 my @len = map({length($_)} @str);
  0         0  
1419 0         0 my $max = 0;
1420 0         0 my $longest;
1421 0         0 for my $i (0..$#len) {
1422 0 0       0 ($max, $longest) = ($len[$i], $str[$i]) if($len[$i] > $max);
1423             }
1424 0         0 return($longest);
1425 0         0 };
1426 0         0 my $max_name_len = length($longest->(keys %$features));
1427              
1428 0         0 my ($num_disabled, $log_text) = (0, "\nChecking optional features...\n");
1429 0         0 for my $name ( sort keys %$features ) {
1430 0         0 $log_text .= $self->_feature_deps_msg($name, $max_name_len);
1431             }
1432              
1433 0         0 $num_disabled = () = $log_text =~ /disabled/g;
1434              
1435             # warn user if features disabled
1436 0 0       0 if ( $num_disabled ) {
1437 0         0 $self->log_warn( $log_text );
1438 0         0 return 0;
1439             }
1440             else {
1441 0         0 $self->log_verbose( $log_text );
1442 0         0 return 1;
1443             }
1444             }
1445              
1446             sub _feature_deps_msg {
1447 0     0   0 my ($self, $name, $max_name_len) = @_;
1448 0   0     0 $max_name_len ||= length $name;
1449 0         0 my $features = $self->auto_features;
1450 0         0 my $info = $features->{$name};
1451 0         0 my $feature_text = "$name" . '.' x ($max_name_len - length($name) + 4);
1452              
1453 0         0 my ($log_text, $disabled) = ('','');
1454 0 0       0 if ( my $failures = $self->prereq_failures($info) ) {
1455 0 0       0 $disabled = grep( /^(?:\w+_)?(?:requires|conflicts)$/,
1456             keys %$failures ) ? 1 : 0;
1457 0 0       0 $feature_text .= $disabled ? "disabled\n" : "enabled\n";
1458              
1459 0         0 for my $type ( @{ $self->prereq_action_types } ) {
  0         0  
1460 0 0       0 next unless exists $failures->{$type};
1461 0         0 $feature_text .= " $type:\n";
1462 0         0 my $prereqs = $failures->{$type};
1463 0         0 for my $module ( sort keys %$prereqs ) {
1464 0         0 my $status = $prereqs->{$module};
1465 0 0       0 my $required =
1466             ($type =~ /^(?:\w+_)?(?:requires|conflicts)$/) ? 1 : 0;
1467 0 0       0 my $prefix = ($required) ? '!' : '*';
1468 0         0 $feature_text .= " $prefix $status->{message}\n";
1469             }
1470             }
1471             } else {
1472 0         0 $feature_text .= "enabled\n";
1473             }
1474 0 0 0     0 $log_text .= $feature_text if $disabled || $self->verbose;
1475 0         0 return $log_text;
1476             }
1477              
1478             # Automatically detect configure_requires prereqs
1479             sub auto_config_requires {
1480 11     11 0 82 my ($self) = @_;
1481 11         39 my $p = $self->{properties};
1482              
1483             # add current Module::Build to configure_requires if there
1484             # isn't one already specified (but not ourself, so we're not circular)
1485 11 100 66     123 if ( $self->dist_name ne 'Module-Build'
      100        
1486             && $self->auto_configure_requires
1487             && ! exists $p->{configure_requires}{'Module::Build'}
1488             ) {
1489 7         289 (my $ver = $VERSION) =~ s/^(\d+\.\d\d).*$/$1/; # last major release only
1490 7         275 $self->log_warn(<
1491             Module::Build was not found in configure_requires! Adding it now
1492             automatically as: configure_requires => { 'Module::Build' => $ver }
1493             EOM
1494 7         205 $self->_add_prereq('configure_requires', 'Module::Build', $ver);
1495             }
1496              
1497             # if we're in author mode, add inc::latest modules to
1498             # configure_requires if not already set. If we're not in author mode
1499             # then configure_requires will have been satisfied, or we'll just
1500             # live with what we've bundled
1501 11 50       479 if ( inc::latest->can('loaded_module') ) {
1502 0         0 for my $mod ( inc::latest->loaded_modules ) {
1503 0 0       0 next if exists $p->{configure_requires}{$mod};
1504 0         0 $self->_add_prereq('configure_requires', $mod, $mod->VERSION);
1505             }
1506             }
1507              
1508 11         56 return;
1509             }
1510              
1511             # Automatically detect and add prerequisites based on configuration
1512             sub auto_require {
1513 80     80 0 300 my ($self) = @_;
1514 80         234 my $p = $self->{properties};
1515              
1516             # If needs_compiler is not explicitly set, automatically set it
1517             # If set, we need ExtUtils::CBuilder (and a compiler)
1518 80         1480 my $xs_files = $self->find_xs_files;
1519 80 50       388 if ( ! defined $p->{needs_compiler} ) {
1520 80 50 33     865 if ( $self->pureperl_only && $self->allow_pureperl ) {
1521 0         0 $self->needs_compiler( 0 );
1522             } else {
1523             $self->needs_compiler( keys %$xs_files ||
1524             ( defined $self->c_source &&
1525 80   33     1663 ( ref($self->c_source) ne 'ARRAY' || @{$self->c_source} )
1526             )
1527             );
1528             }
1529             }
1530 80 50       330 if ($self->needs_compiler) {
1531 0         0 $self->_add_prereq('build_requires', 'ExtUtils::CBuilder', 0);
1532 0 0       0 if ( ! $self->have_c_compiler ) {
1533 0         0 $self->log_warn(<<'EOM');
1534             Warning: ExtUtils::CBuilder not installed or no compiler detected
1535             Proceeding with configuration, but compilation may fail during Build
1536              
1537             EOM
1538             }
1539             }
1540              
1541             # If using share_dir, require File::ShareDir
1542 80 50       927 if ( $self->share_dir ) {
1543 0         0 $self->_add_prereq( 'requires', 'File::ShareDir', '1.00' );
1544             }
1545              
1546 80         251 return;
1547             }
1548              
1549             sub _add_prereq {
1550 7     7   90 my ($self, $type, $module, $version) = @_;
1551 7         42 my $p = $self->{properties};
1552 7 50       80 $version = 0 unless defined $version;
1553 7 50       52 if ( exists $p->{$type}{$module} ) {
1554 0 0       0 return if $self->compare_versions( $version, '<=', $p->{$type}{$module} );
1555             }
1556 7         157 $self->log_verbose("Adding to $type\: $module => $version\n");
1557 7         35 $p->{$type}{$module} = $version;
1558 7         79 return 1;
1559             }
1560              
1561             sub prereq_failures {
1562 107     107 0 12130 my ($self, $info) = @_;
1563              
1564 107         213 my @types = @{ $self->prereq_action_types };
  107         283  
1565 107   100     438 $info ||= {map {$_, $self->$_()} @types};
  125         305  
1566              
1567 107         218 my $out;
1568              
1569 107         314 foreach my $type (@types) {
1570 535         954 my $prereqs = $info->{$type};
1571 535         1447 for my $modname ( keys %$prereqs ) {
1572 45         243 my $spec = $prereqs->{$modname};
1573 45         388 my $status = $self->check_installed_status($modname, $spec);
1574              
1575 45 100       263 if ($type =~ /^(?:\w+_)?conflicts$/) {
    100          
1576 1 50       23 next if !$status->{ok};
1577 0         0 $status->{conflicts} = delete $status->{need};
1578 0         0 $status->{message} = "$modname ($status->{have}) conflicts with this distribution";
1579              
1580             } elsif ($type =~ /^(?:\w+_)?recommends$/) {
1581 5 50       75 next if $status->{ok};
1582 5 50 33     200 $status->{message} = (!ref($status->{have}) && $status->{have} eq ''
1583             ? "$modname is not installed"
1584             : "$modname ($status->{have}) is installed, but we prefer to have $spec");
1585             } else {
1586 39 100       125 next if $status->{ok};
1587             }
1588              
1589 34         173 $out->{$type}{$modname} = $status;
1590             }
1591             }
1592              
1593 107         970 return $out;
1594             }
1595              
1596             # returns a hash of defined prerequisites; i.e. only prereq types with values
1597             sub _enum_prereqs {
1598 82     82   191 my $self = shift;
1599 82         164 my %prereqs;
1600 82         177 foreach my $type ( @{ $self->prereq_action_types } ) {
  82         557  
1601 410 50       2305 if ( $self->can( $type ) ) {
1602 410   50     1580 my $prereq = $self->$type() || {};
1603 410 100       1155 $prereqs{$type} = $prereq if %$prereq;
1604             }
1605             }
1606 82         286 return \%prereqs;
1607             }
1608              
1609             sub check_prereq {
1610 80     80 0 226 my $self = shift;
1611              
1612             # Check to see if there are any prereqs to check
1613 80         539 my $info = $self->_enum_prereqs;
1614 80 50       242 return 1 unless $info;
1615              
1616 80         427 my $log_text = "Checking prerequisites...\n";
1617              
1618 80         607 my $failures = $self->prereq_failures($info);
1619              
1620 80 100       326 if ( $failures ) {
1621 10         65 $self->log_warn($log_text);
1622 10         350 for my $type ( @{ $self->prereq_action_types } ) {
  10         45  
1623 50         175 my $prereqs = $failures->{$type};
1624 50 100       190 $self->log_warn(" ${type}:\n") if keys %$prereqs;
1625 50         170 for my $module ( sort keys %$prereqs ) {
1626 10         20 my $status = $prereqs->{$module};
1627 10 100       100 my $prefix = ($type =~ /^(?:\w+_)?recommends$/) ? "* " : "! ";
1628 10         75 $self->log_warn(" $prefix $status->{message}\n");
1629             }
1630             }
1631 10         215 return 0;
1632             } else {
1633 70         578 $self->log_verbose($log_text . "Looks good\n\n");
1634 70         1010 return 1;
1635             }
1636             }
1637              
1638             sub perl_version {
1639 7     7 0 339 my ($self) = @_;
1640             # Check the current perl interpreter
1641             # It's much more convenient to use $] here than $^V, but 'man
1642             # perlvar' says I'm not supposed to. Bloody tyrant.
1643 7 50       291 return $^V ? $self->perl_version_to_float(sprintf "%vd", $^V) : $];
1644             }
1645              
1646             sub perl_version_to_float {
1647 7     7 0 26 my ($self, $version) = @_;
1648 7 50       141 return $version if grep( /\./, $version ) < 2;
1649 0         0 $version =~ s/\./../;
1650 0         0 $version =~ s/\.(\d+)/sprintf '%03d', $1/eg;
  0         0  
1651 0         0 return $version;
1652             }
1653              
1654             sub _parse_conditions {
1655 148     148   508 my ($self, $spec) = @_;
1656              
1657 148 50       510 return ">= 0" if not defined $spec;
1658 148 100       1699 if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
1659 143         878 return (">= $spec");
1660             } else {
1661 5         25 return split /\s*,\s*/, $spec;
1662             }
1663             }
1664              
1665             sub try_require {
1666 21     21 0 281 my ($self, $modname, $spec) = @_;
1667 21 50       368 my $status = $self->check_installed_status($modname, defined($spec) ? $spec : 0);
1668 21 50       96 return unless $status->{ok};
1669 21         51 my $path = $modname;
1670 21         238 $path =~ s{::}{/}g;
1671 21         61 $path .= ".pm";
1672 21 100       120 if ( defined $INC{$path} ) {
    50          
1673 12         85 return 1;
1674             }
1675             elsif ( exists $INC{$path} ) { # failed before, don't try again
1676 0         0 return;
1677             }
1678             else {
1679 9         526 return eval "require $modname";
1680             }
1681             }
1682              
1683             sub check_installed_status {
1684 154     154 0 14039 my ($self, $modname, $spec) = @_;
1685 154         738 my %status = (need => $spec);
1686              
1687 154 50       799 if ($modname eq 'perl') {
    100          
1688 0         0 $status{have} = $self->perl_version;
1689              
1690 293     293   3023 } elsif (eval { no strict; $status{have} = ${"${modname}::VERSION"} }) {
  293         882  
  293         1425916  
  154         304  
  154         2323  
1691             # Don't try to load if it's already loaded
1692              
1693             } else {
1694 86         1211 my $pm_info = Module::Metadata->new_from_module( $modname );
1695 86 100       880578 unless (defined( $pm_info )) {
1696 6         184 @status{ qw(have message) } = ('', "$modname is not installed");
1697 6         92 return \%status;
1698             }
1699              
1700 80         236 $status{have} = eval { $pm_info->version() };
  80         488  
1701 80 50 66     2046 if ($spec and !defined($status{have})) {
1702 0         0 @status{ qw(have message) } = (undef, "Couldn't find a \$VERSION in prerequisite $modname");
1703 0         0 return \%status;
1704             }
1705             }
1706              
1707 148         1500 my @conditions = $self->_parse_conditions($spec);
1708              
1709 148         566 foreach (@conditions) {
1710 148 50       1738 my ($op, $version) = /^\s* (<=?|>=?|==|!=) \s* ([\w.]+) \s*$/x
1711             or die "Invalid prerequisite condition '$_' for $modname";
1712              
1713 148 50       693 $version = $self->perl_version_to_float($version)
1714             if $modname eq 'perl';
1715              
1716 148 100 100     1237 next if $op eq '>=' and !$version; # Module doesn't have to actually define a $VERSION
1717              
1718 60 100       877 unless ($self->compare_versions( $status{have}, $op, $version )) {
1719 29         201 $status{message} = "$modname ($status{have}) is installed, but we need version $op $version";
1720 29         150 return \%status;
1721             }
1722             }
1723              
1724 119         516 $status{ok} = 1;
1725 119         540 return \%status;
1726             }
1727              
1728             sub compare_versions {
1729 61     61 0 1666 my $self = shift;
1730 61         177 my ($v1, $op, $v2) = @_;
1731             $v1 = version->new($v1)
1732 61 100       128 unless eval { $v1->isa('version') };
  61         1092  
1733              
1734 61         243 my $eval_str = "\$v1 $op \$v2";
1735 61         4571 my $result = eval $eval_str;
1736 61 50       464 $self->log_warn("error comparing versions: '$eval_str' $@") if $@;
1737              
1738 61         293 return $result;
1739             }
1740              
1741             # I wish I could set $! to a string, but I can't, so I use $@
1742             sub check_installed_version {
1743 0     0 0 0 my ($self, $modname, $spec) = @_;
1744              
1745 0         0 my $status = $self->check_installed_status($modname, $spec);
1746              
1747 0 0       0 if ($status->{ok}) {
1748 0 0 0     0 return $status->{have} if $status->{have} and "$status->{have}" ne '';
1749 0         0 return '0 but true';
1750             }
1751              
1752 0         0 $@ = $status->{message};
1753 0         0 return 0;
1754             }
1755              
1756             sub make_executable {
1757             # Perl's chmod() is mapped to useful things on various non-Unix
1758             # platforms, so we use it in the base class even though it looks
1759             # Unixish.
1760              
1761 15     15 0 88 my $self = shift;
1762 15         64 foreach (@_) {
1763 15         216 my $current_mode = (stat $_)[2];
1764 15         313 chmod $current_mode | oct(111), $_;
1765             }
1766             }
1767              
1768             sub is_executable {
1769             # We assume this does the right thing on generic platforms, though
1770             # we do some other more specific stuff on Unixish platforms.
1771 0     0 0 0 my ($self, $file) = @_;
1772 0         0 return -x $file;
1773             }
1774              
1775 0     0   0 sub _startperl { shift()->config('startperl') }
1776              
1777             # Return any directories in @INC which are not in the default @INC for
1778             # this perl. For example, stuff passed in with -I or loaded with "use lib".
1779             sub _added_to_INC {
1780 4286     4286   19281 my $self = shift;
1781              
1782 4286         8917 my %seen;
1783 4286         19266 $seen{$_}++ foreach $self->_default_INC;
1784 4240         297539 return grep !$seen{$_}++, @INC;
1785             }
1786              
1787             # Determine the default @INC for this Perl
1788             {
1789             my @default_inc; # Memoize
1790             sub _default_INC {
1791 4749     4749   12051 my $self = shift;
1792 4749 100       117988 return @default_inc if @default_inc;
1793              
1794 242         6081 local $ENV{PERL5LIB}; # this is not considered part of the default.
1795              
1796 242 100       4339 my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
1797              
1798 242         2172 my @inc = $self->_backticks($perl, '-le', 'print for @INC');
1799 196         8856 chomp @inc;
1800              
1801 196         22943 return @default_inc = @inc;
1802             }
1803             }
1804              
1805             sub print_build_script {
1806 6     6 0 28 my ($self, $fh) = @_;
1807              
1808 6         55 my $build_package = $self->build_class;
1809              
1810 6         40 my $closedata="";
1811              
1812 6         20 my $config_requires;
1813 6 50       44 if ( -f $self->metafile ) {
1814 0         0 my $meta = eval { $self->read_metafile( $self->metafile ) };
  0         0  
1815 0   0     0 $config_requires = $meta && $meta->{prereqs}{configure}{requires}{'Module::Build'};
1816             }
1817 6   50     118 $config_requires ||= 0;
1818              
1819 6         27 my %q = map {$_, $self->$_()} qw(config_dir base_dir);
  12         84  
1820              
1821 6 50       61 $q{base_dir} = Win32::GetShortPathName($q{base_dir}) if $self->is_windowsish;
1822              
1823 6         214 $q{magic_numfile} = $self->config_file('magicnum');
1824              
1825 6         47 my @myINC = $self->_added_to_INC;
1826 6         29 for (@myINC, values %q) {
1827 69 50       143 $_ = File::Spec->canonpath( $_ ) unless $self->is_vmsish;
1828 69         817 s/([\\\'])/\\$1/g;
1829             }
1830              
1831 6         68 my $quoted_INC = join ",\n", map " '$_'", @myINC;
1832 6         83 my $shebang = $self->_startperl;
1833 6         36 my $magic_number = $self->magic_number;
1834              
1835 6 50       91 my $dot_in_inc_code = $INC[-1] eq '.' ? <<'END' : '';
1836             if ($INC[-1] ne '.') {
1837             push @INC, '.';
1838             }
1839             END
1840 6         168 print $fh <
1841             $shebang
1842              
1843             use strict;
1844             use Cwd;
1845             use File::Basename;
1846             use File::Spec;
1847              
1848             sub magic_number_matches {
1849             return 0 unless -e '$q{magic_numfile}';
1850             my \$FH;
1851             open \$FH, '<','$q{magic_numfile}' or return 0;
1852             my \$filenum = <\$FH>;
1853             close \$FH;
1854             return \$filenum == $magic_number;
1855             }
1856              
1857             my \$progname;
1858             my \$orig_dir;
1859             BEGIN {
1860             \$^W = 1; # Use warnings
1861             \$progname = basename(\$0);
1862             \$orig_dir = Cwd::cwd();
1863             my \$base_dir = '$q{base_dir}';
1864             if (!magic_number_matches()) {
1865             unless (chdir(\$base_dir)) {
1866             die ("Couldn't chdir(\$base_dir), aborting\\n");
1867             }
1868             unless (magic_number_matches()) {
1869             die ("Configuration seems to be out of date, please re-run 'perl Build.PL' again.\\n");
1870             }
1871             }
1872             unshift \@INC,
1873             (
1874             $quoted_INC
1875             );
1876             $dot_in_inc_code
1877             }
1878              
1879             close(*DATA) unless eof(*DATA); # ensure no open handles to this script
1880              
1881             use $build_package;
1882             Module::Build->VERSION(q{$config_requires});
1883              
1884             # Some platforms have problems setting \$^X in shebang contexts, fix it up here
1885             \$^X = Module::Build->find_perl_interpreter;
1886              
1887             if (-e 'Build.PL' and not $build_package->up_to_date('Build.PL', \$progname)) {
1888             warn "Warning: Build.PL has been altered. You may need to run 'perl Build.PL' again.\\n";
1889             }
1890              
1891             # This should have just enough arguments to be able to bootstrap the rest.
1892             my \$build = $build_package->resume (
1893             properties => {
1894             config_dir => '$q{config_dir}',
1895             orig_dir => \$orig_dir,
1896             },
1897             );
1898              
1899             \$build->dispatch;
1900             EOF
1901             }
1902              
1903             sub create_mymeta {
1904 6     6 0 76 my ($self) = @_;
1905              
1906 6         20 my ($meta_obj, $mymeta);
1907 6         99 my @metafiles = ( $self->metafile2, $self->metafile, );
1908 6         79 my @mymetafiles = ( $self->mymetafile2, $self->mymetafile, );
1909              
1910             # cleanup old MYMETA
1911 6         41 for my $f ( @mymetafiles ) {
1912 12 50       90 if ( $self->delete_filetree($f) ) {
1913 12         69 $self->log_verbose("Removed previous '$f'\n");
1914             }
1915             }
1916              
1917             # Try loading META.json or META.yml
1918 6 50       149 if ( $self->try_require("CPAN::Meta", "2.142060") ) {
1919 6         164827 for my $file ( @metafiles ) {
1920 12 50       355 next unless -f $file;
1921 0         0 $meta_obj = eval { CPAN::Meta->load_file($file, { lazy_validation => 0 }) };
  0         0  
1922 0 0       0 last if $meta_obj;
1923             }
1924             }
1925              
1926             # maybe get a copy in spec v2 format (regardless of original source)
1927              
1928 6         21 my $mymeta_obj;
1929 6 50       45 if ($meta_obj) {
1930             # if we have metadata, just update it
1931             my %updated = (
1932 0         0 %{ $meta_obj->as_struct({ version => 2.0 }) },
  0         0  
1933             prereqs => $self->_normalize_prereqs,
1934             dynamic_config => 0,
1935             generated_by => "Module::Build version $Module::Build::VERSION",
1936             );
1937 0         0 $mymeta_obj = CPAN::Meta->new( \%updated, { lazy_validation => 0 } );
1938             }
1939             else {
1940 6         151 $mymeta_obj = $self->_get_meta_object(quiet => 0, dynamic => 0, fatal => 1, auto => 0);
1941             }
1942              
1943 6         81 my @created = $self->_write_meta_files( $mymeta_obj, 'MYMETA' );
1944              
1945 6 50       19 $self->log_warn("Could not create MYMETA files\n")
1946             unless @created;
1947              
1948 6         85 return 1;
1949             }
1950              
1951             sub create_build_script {
1952 6     6 0 1886 my ($self) = @_;
1953              
1954 6         143 $self->write_config;
1955 6         131 $self->create_mymeta;
1956              
1957             # Create Build
1958 6         69 my ($build_script, $dist_name, $dist_version)
1959             = map $self->$_(), qw(build_script dist_name dist_version);
1960              
1961 6 50       53 if ( $self->delete_filetree($build_script) ) {
1962 6         42 $self->log_verbose("Removed previous script '$build_script'\n");
1963             }
1964              
1965 6         61 $self->log_info("Creating new '$build_script' script for ",
1966             "'$dist_name' version '$dist_version'\n");
1967 6 50       376 open(my $fh, '>', $build_script) or die "Can't create '$build_script': $!";
1968 6         194 $self->print_build_script($fh);
1969 6         208 close $fh;
1970              
1971 6         74 $self->make_executable($build_script);
1972              
1973 6         39 return 1;
1974             }
1975              
1976             sub check_manifest {
1977 80     80 0 297 my $self = shift;
1978 80 100       1513 return unless -e 'MANIFEST';
1979              
1980             # Stolen nearly verbatim from MakeMaker. But ExtUtils::Manifest
1981             # could easily be re-written into a modern Perl dialect.
1982              
1983 78         37170 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
1984 78         191932 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
1985              
1986 78         1082 $self->log_verbose("Checking whether your kit is complete...\n");
1987 78 50       876 if (my @missed = ExtUtils::Manifest::manicheck()) {
1988 0         0 $self->log_warn("WARNING: the following files are missing in your kit:\n",
1989             "\t", join("\n\t", @missed), "\n",
1990             "Please inform the author.\n\n");
1991             } else {
1992 78         107800 $self->log_verbose("Looks good\n\n");
1993             }
1994             }
1995              
1996             sub dispatch {
1997 130     130 0 176575 my $self = shift;
1998 130         1210 local $self->{_completed_actions} = {};
1999              
2000 130 50       935 if (@_) {
2001 130         972 my ($action, %p) = @_;
2002 130 100       800 my $args = $p{args} ? delete($p{args}) : {};
2003              
2004 130         587 local $self->{invoked_action} = $action;
2005 130         563 local $self->{args} = {%{$self->{args}}, %$args};
  130         1167  
2006 130         445 local $self->{properties} = {%{$self->{properties}}, %p};
  130         16383  
2007 130         1786 return $self->_call_action($action);
2008             }
2009              
2010 0 0       0 die "No build action specified" unless $self->{action};
2011 0         0 local $self->{invoked_action} = $self->{action};
2012 0         0 $self->_call_action($self->{action});
2013             }
2014              
2015             sub _call_action {
2016 482     482   2376 my ($self, $action) = @_;
2017              
2018 482 100       3604 return if $self->{_completed_actions}{$action}++;
2019              
2020 398         1492 local $self->{action} = $action;
2021 398         2044 my $method = $self->can_action( $action );
2022 398 50       1665 die "No action '$action' defined, try running the 'help' action.\n" unless $method;
2023 398         2974 $self->log_debug("Starting ACTION_$action\n");
2024 398         3248 my $rc = $self->$method();
2025 392         3553 $self->log_debug("Finished ACTION_$action\n");
2026 392         23112 return $rc;
2027             }
2028              
2029             sub can_action {
2030 398     398 0 1171 my ($self, $action) = @_;
2031 398         6138 return $self->can( "ACTION_$action" );
2032             }
2033              
2034             # cuts the user-specified options out of the command-line args
2035             sub cull_options {
2036 561     561 0 3149 my $self = shift;
2037 561         3550 my (@argv) = @_;
2038              
2039             # XXX is it even valid to call this as a class method?
2040 561 100       7651 return({}, @argv) unless(ref($self)); # no object
2041              
2042 447         10413 my $specs = $self->get_options;
2043 447 100 66     13819 return({}, @argv) unless($specs and %$specs); # no user options
2044              
2045 10         11300 require Getopt::Long;
2046             # XXX Should we let Getopt::Long handle M::B's options? That would
2047             # be easy-ish to add to @specs right here, but wouldn't handle options
2048             # passed without "--" as M::B currently allows. We might be able to
2049             # get around this by setting the "prefix_pattern" Configure option.
2050 10         61723 my @specs;
2051 10         143 my $args = {};
2052             # Construct the specifications for GetOptions.
2053 10         384 foreach my $k (sort keys %$specs) {
2054 40         153 my $v = $specs->{$k};
2055             # Throw an error if specs conflict with our own.
2056 40 50       291 die "Option specification '$k' conflicts with a " . ref $self
2057             . " option of the same name"
2058             if $self->valid_property($k);
2059 40 100       373 push @specs, $k . (defined $v->{type} ? $v->{type} : '');
2060 40 50       192 push @specs, $v->{store} if exists $v->{store};
2061 40 100       283 $args->{$k} = $v->{default} if exists $v->{default};
2062             }
2063              
2064 10         140 local @ARGV = @argv; # No other way to dupe Getopt::Long
2065              
2066             # Get the options values and return them.
2067             # XXX Add option to allow users to set options?
2068 10 50       209 if ( @specs ) {
2069 10         200 Getopt::Long::Configure('pass_through');
2070 10         1131 Getopt::Long::GetOptions($args, @specs);
2071             }
2072              
2073 10         6269 return $args, @ARGV;
2074             }
2075              
2076             sub unparse_args {
2077 538     538 0 61058 my ($self, $args) = @_;
2078 538         2449 my @out;
2079 538         6883 foreach my $k (sort keys %$args) {
2080 599         2894 my $v = $args->{$k};
2081 277         1648 push @out, (ref $v eq 'HASH' ? map {+"--$k", "$_=$v->{$_}"} sort keys %$v :
2082 599 100       7410 ref $v eq 'ARRAY' ? map {+"--$k", $_} @$v :
  0 100       0  
2083             ("--$k", $v));
2084             }
2085 538         11568 return @out;
2086             }
2087              
2088             sub args {
2089 56     56 0 4094 my $self = shift;
2090 56 100       230 return wantarray ? %{ $self->{args} } : $self->{args} unless @_;
  4 100       84  
2091 48         122 my $key = shift;
2092 48 50       110 $self->{args}{$key} = shift if @_;
2093 48         314 return $self->{args}{$key};
2094             }
2095              
2096             # allows select parameters (with underscores) to be spoken with dashes
2097             # when used as command-line options
2098             sub _translate_option {
2099 693     693   1069 my $self = shift;
2100 693         1004 my $opt = shift;
2101              
2102 693         1317 (my $tr_opt = $opt) =~ tr/-/_/;
2103              
2104 693 50       87879 return $tr_opt if grep $tr_opt =~ /^(?:no_?)?$_$/, qw(
2105             create_license
2106             create_makefile_pl
2107             create_readme
2108             extra_compiler_flags
2109             extra_linker_flags
2110             install_base
2111             install_path
2112             meta_add
2113             meta_merge
2114             test_files
2115             use_rcfile
2116             use_tap_harness
2117             tap_harness_args
2118             cpan_client
2119             pureperl_only
2120             allow_pureperl
2121             ); # normalize only selected option names
2122              
2123 693         2115 return $opt;
2124             }
2125              
2126             my %singular_argument = map { ($_ => 1) } qw/install_base prefix destdir installdirs verbose quiet uninst debug sign/;
2127              
2128             sub _read_arg {
2129 404     404   1197 my ($self, $args, $key, $val) = @_;
2130              
2131 404         1134 $key = $self->_translate_option($key);
2132              
2133 404 100 66     1823 if ( exists $args->{$key} and not $singular_argument{$key} ) {
2134 58 50       297 $args->{$key} = [ $args->{$key} ] unless ref $args->{$key};
2135 58         174 push @{$args->{$key}}, $val;
  58         292  
2136             } else {
2137 346         1733 $args->{$key} = $val;
2138             }
2139             }
2140              
2141             # decide whether or not an option requires/has an operand
2142             sub _optional_arg {
2143 289     289   481 my $self = shift;
2144 289         783 my $opt = shift;
2145 289         487 my $argv = shift;
2146              
2147 289         795 $opt = $self->_translate_option($opt);
2148              
2149 289         976 my @bool_opts = qw(
2150             build_bat
2151             create_license
2152             create_readme
2153             pollute
2154             quiet
2155             uninst
2156             use_rcfile
2157             verbose
2158             debug
2159             sign
2160             use_tap_harness
2161             pureperl_only
2162             allow_pureperl
2163             );
2164              
2165             # inverted boolean options; eg --noverbose or --no-verbose
2166             # converted to proper name & returned with false value (verbose, 0)
2167 289 50       28833 if ( grep $opt =~ /^no[-_]?$_$/, @bool_opts ) {
2168 0         0 $opt =~ s/^no-?//;
2169 0         0 return ($opt, 0);
2170             }
2171              
2172             # non-boolean option; return option unchanged along with its argument
2173 289 100       2056 return ($opt, shift(@$argv)) unless grep $_ eq $opt, @bool_opts;
2174              
2175             # we're punting a bit here, if an option appears followed by a digit
2176             # we take the digit as the argument for the option. If there is
2177             # nothing that looks like a digit, we pretend the option is a flag
2178             # that is being set and has no argument.
2179 1         11 my $arg = 1;
2180 1 50 33     26 $arg = shift(@$argv) if @$argv && $argv->[0] =~ /^\d+$/;
2181              
2182 1         10 return ($opt, $arg);
2183             }
2184              
2185             sub read_args {
2186 561     561 0 73428 my $self = shift;
2187              
2188 561         10429 (my $args, @_) = $self->cull_options(@_);
2189 561         3987 my %args = %$args;
2190              
2191 561         12840 my $opt_re = qr/[\w\-]+/;
2192              
2193 561         2717 my ($action, @argv);
2194 561         5109 while (@_) {
2195 404         870 local $_ = shift;
2196 404 100 0     11899 if ( /^(?:--)?($opt_re)=(.*)$/ ) {
    50          
    0          
2197 115         752 $self->_read_arg(\%args, $1, $2);
2198             } elsif ( /^--($opt_re)$/ ) {
2199 289         1281 my($opt, $arg) = $self->_optional_arg($1, \@_);
2200 289         1089 $self->_read_arg(\%args, $opt, $arg);
2201             } elsif ( /^($opt_re)$/ and !defined($action)) {
2202 0         0 $action = $1;
2203             } else {
2204 0         0 push @argv, $_;
2205             }
2206             }
2207 561         6733 $args{ARGV} = \@argv;
2208              
2209 561         5266 for ('extra_compiler_flags', 'extra_linker_flags') {
2210 1122 50       5901 $args{$_} = [ $self->split_like_shell($args{$_}) ] if exists $args{$_};
2211             }
2212              
2213             # Convert to arrays
2214 561         2323 for ('include_dirs') {
2215 561 50 33     4346 $args{$_} = [ $args{$_} ] if exists $args{$_} && !ref $args{$_}
2216             }
2217              
2218             # Hashify these parameters
2219 561         8360 for ($self->hash_properties, 'config') {
2220 8981 100       23562 next unless exists $args{$_};
2221 58         181 my %hash;
2222 58   50     233 $args{$_} ||= [];
2223 58 50       299 $args{$_} = [ $args{$_} ] unless ref $args{$_};
2224 58         118 foreach my $arg ( @{$args{$_}} ) {
  58         179  
2225 116 50       4729 $arg =~ /($opt_re)=(.*)/
2226             or die "Malformed '$_' argument: '$arg' should be something like 'foo=bar'";
2227 116         536 $hash{$1} = $2;
2228             }
2229 58         238 $args{$_} = \%hash;
2230             }
2231              
2232             # De-tilde-ify any path parameters
2233 561         5710 for my $key (qw(prefix install_base destdir)) {
2234 1683 50       6707 next if !defined $args{$key};
2235 0         0 $args{$key} = $self->_detildefy($args{$key});
2236             }
2237              
2238 561         2964 for my $key (qw(install_path)) {
2239 561 50       3083 next if !defined $args{$key};
2240              
2241 0         0 for my $subkey (keys %{$args{$key}}) {
  0         0  
2242 0 0       0 next if !defined $args{$key}{$subkey};
2243 0         0 my $subkey_ext = $self->_detildefy($args{$key}{$subkey});
2244 0 0       0 if ( $subkey eq 'html' ) { # translate for compatibility
2245 0         0 $args{$key}{binhtml} = $subkey_ext;
2246 0         0 $args{$key}{libhtml} = $subkey_ext;
2247             } else {
2248 0         0 $args{$key}{$subkey} = $subkey_ext;
2249             }
2250             }
2251             }
2252              
2253 561 50       2742 if ($args{makefile_env_macros}) {
2254 0         0 require Module::Build::Compat;
2255 0         0 %args = (%args, Module::Build::Compat->makefile_to_build_macros);
2256             }
2257              
2258 561         6286 return \%args, $action;
2259             }
2260              
2261             # Default: do nothing. Overridden for Unix & Windows.
2262       0     sub _detildefy {}
2263              
2264              
2265             # merge Module::Build argument lists that have already been parsed
2266             # by read_args(). Takes two references to option hashes and merges
2267             # the contents, giving priority to the first.
2268             sub _merge_arglist {
2269 812     812   3088 my( $self, $opts1, $opts2 ) = @_;
2270              
2271 812   100     3080 $opts1 ||= {};
2272 812   50     2647 $opts2 ||= {};
2273 812         4208 my %new_opts = %$opts1;
2274 812         5516 while (my ($key, $val) = each %$opts2) {
2275 1665 50       2946 if ( exists( $opts1->{$key} ) ) {
2276 0 0       0 if ( ref( $val ) eq 'HASH' ) {
2277 0         0 while (my ($k, $v) = each %$val) {
2278 0 0       0 $new_opts{$key}{$k} = $v unless exists( $opts1->{$key}{$k} );
2279             }
2280             }
2281             } else {
2282 1665         4879 $new_opts{$key} = $val
2283             }
2284             }
2285              
2286 812         4505 return %new_opts;
2287             }
2288              
2289             # Look for a home directory on various systems.
2290             sub _home_dir {
2291 443     443   1585 my @home_dirs;
2292 443 50       6660 push( @home_dirs, $ENV{HOME} ) if $ENV{HOME};
2293              
2294             push( @home_dirs, File::Spec->catpath($ENV{HOMEDRIVE}, $ENV{HOMEPATH}, '') )
2295 443 0 33     3098 if $ENV{HOMEDRIVE} && $ENV{HOMEPATH};
2296              
2297 443         5908 my @other_home_envs = qw( USERPROFILE APPDATA WINDIR SYS$LOGIN );
2298 443         4212 push( @home_dirs, map $ENV{$_}, grep $ENV{$_}, @other_home_envs );
2299              
2300 443         17401 my @real_home_dirs = grep -d, @home_dirs;
2301              
2302 443 50       6180 return wantarray ? @real_home_dirs : shift( @real_home_dirs );
2303             }
2304              
2305             sub _find_user_config {
2306 443     443   1957 my $self = shift;
2307 443         3415 my $file = shift;
2308 443         5161 foreach my $dir ( $self->_home_dir ) {
2309 436         18220 my $path = File::Spec->catfile( $dir, $file );
2310 436 50       9191 return $path if -e $path;
2311             }
2312 443         2959 return undef;
2313             }
2314              
2315             # read ~/.modulebuildrc returning global options '*' and
2316             # options specific to the currently executing $action.
2317             sub read_modulebuildrc {
2318 447     447 0 4620 my( $self, $action ) = @_;
2319              
2320 447 100       9777 return () unless $self->use_rcfile;
2321              
2322 443         1837 my $modulebuildrc;
2323 443 50 33     5689 if ( exists($ENV{MODULEBUILDRC}) && $ENV{MODULEBUILDRC} eq 'NONE' ) {
    50 33        
    50          
2324 0         0 return ();
2325             } elsif ( exists($ENV{MODULEBUILDRC}) && -e $ENV{MODULEBUILDRC} ) {
2326 0         0 $modulebuildrc = $ENV{MODULEBUILDRC};
2327             } elsif ( exists($ENV{MODULEBUILDRC}) ) {
2328 0         0 $self->log_warn("WARNING: Can't find resource file " .
2329             "'$ENV{MODULEBUILDRC}' defined in environment.\n" .
2330             "No options loaded\n");
2331 0         0 return ();
2332             } else {
2333 443         5123 $modulebuildrc = $self->_find_user_config( '.modulebuildrc' );
2334 443 50       3468 return () unless $modulebuildrc;
2335             }
2336              
2337 0 0       0 open(my $fh, '<', $modulebuildrc )
2338             or die "Can't open $modulebuildrc: $!";
2339              
2340 0         0 my %options; my $buffer = '';
  0         0  
2341 0         0 while (defined( my $line = <$fh> )) {
2342 0         0 chomp( $line );
2343 0         0 $line =~ s/#.*$//;
2344 0 0       0 next unless length( $line );
2345              
2346 0 0       0 if ( $line =~ /^\S/ ) {
2347 0 0       0 if ( $buffer ) {
2348 0         0 my( $action, $options ) = split( /\s+/, $buffer, 2 );
2349 0         0 $options{$action} .= $options . ' ';
2350 0         0 $buffer = '';
2351             }
2352 0         0 $buffer = $line;
2353             } else {
2354 0         0 $buffer .= $line;
2355             }
2356             }
2357              
2358 0 0       0 if ( $buffer ) { # anything left in $buffer ?
2359 0         0 my( $action, $options ) = split( /\s+/, $buffer, 2 );
2360 0         0 $options{$action} .= $options . ' '; # merge if more than one line
2361             }
2362              
2363             my ($global_opts) =
2364 0   0     0 $self->read_args( $self->split_like_shell( $options{'*'} || '' ) );
2365              
2366             # let fakeinstall act like install if not provided
2367 0 0 0     0 if ( $action eq 'fakeinstall' && ! exists $options{fakeinstall} ) {
2368 0         0 $action = 'install';
2369             }
2370             my ($action_opts) =
2371 0   0     0 $self->read_args( $self->split_like_shell( $options{$action} || '' ) );
2372              
2373             # specific $action options take priority over global options '*'
2374 0         0 return $self->_merge_arglist( $action_opts, $global_opts );
2375             }
2376              
2377             # merge the relevant options in ~/.modulebuildrc into Module::Build's
2378             # option list where they do not conflict with commandline options.
2379             sub merge_modulebuildrc {
2380 447     447 0 3387 my( $self, $action, %cmdline_opts ) = @_;
2381 447   100     14368 my %rc_opts = $self->read_modulebuildrc( $action || $self->{action} || 'build' );
2382 447         6269 my %new_opts = $self->_merge_arglist( \%cmdline_opts, \%rc_opts );
2383 447         2554 $self->merge_args( $action, %new_opts );
2384             }
2385              
2386             sub merge_args {
2387 894     894 0 5622 my ($self, $action, %args) = @_;
2388 894 50       4027 $self->{action} = $action if defined $action;
2389              
2390 894         3761 my %additive = map { $_ => 1 } $self->hash_properties;
  13420         53747  
2391              
2392             # Extract our 'properties' from $cmd_args, the rest are put in 'args'.
2393 894         9723 while (my ($key, $val) = each %args) {
2394 922 100       6644 $self->{phash}{runtime_params}->access( $key => $val )
2395             if $self->valid_property($key);
2396              
2397 922 50       6157 if ($key eq 'config') {
2398 0         0 $self->config($_ => $val->{$_}) foreach keys %$val;
2399             } else {
2400             my $add_to = $additive{$key} ? $self->{properties}{$key} :
2401             $self->valid_property($key) ? $self->{properties} :
2402 922 100       5244 $self->{args} ;
    100          
2403              
2404 922 100       3887 if ($additive{$key}) {
2405 2         20 $add_to->{$_} = $val->{$_} foreach keys %$val;
2406             } else {
2407 920         9269 $add_to->{$key} = $val;
2408             }
2409             }
2410             }
2411             }
2412              
2413             sub cull_args {
2414 447     447 0 4273 my $self = shift;
2415 447         3470 my @arg_list = @_;
2416             unshift @arg_list, $self->split_like_shell($ENV{PERL_MB_OPT})
2417 447 100       8712 if $ENV{PERL_MB_OPT};
2418 447         11715 my ($args, $action) = $self->read_args(@arg_list);
2419 447         8489 $self->merge_args($action, %$args);
2420 447         7515 $self->merge_modulebuildrc( $action, %$args );
2421             }
2422              
2423             sub super_classes {
2424 181     181 0 418 my ($self, $class, $seen) = @_;
2425 181   66     703 $class ||= ref($self) || $self;
      66        
2426 181   100     631 $seen ||= {};
2427              
2428 293     293   2725 no strict 'refs';
  293         864  
  293         37725  
2429 181         293 my @super = grep {not $seen->{$_}++} $class, @{ $class . '::ISA' };
  322         1012  
  181         923  
2430 181         660 return @super, map {$self->super_classes($_,$seen)} @super;
  141         448  
2431             }
2432              
2433             sub known_actions {
2434 27     27 0 3965 my ($self) = @_;
2435              
2436 27         92 my %actions;
2437 293     293   2455 no strict 'refs';
  293         743  
  293         3508245  
2438              
2439 27         279 foreach my $class ($self->super_classes) {
2440 92         182 foreach ( keys %{ $class . '::' } ) {
  92         14942  
2441 12635 100       26903 $actions{$1}++ if /^ACTION_(\w+)/;
2442             }
2443             }
2444              
2445 27 100       482 return wantarray ? sort keys %actions : \%actions;
2446             }
2447              
2448             sub get_action_docs {
2449 15     15 0 18374 my ($self, $action) = @_;
2450 15         102 my $actions = $self->known_actions;
2451 15 100       100 die "No known action '$action'" unless $actions->{$action};
2452              
2453 13         54 my ($files_found, @docs) = (0);
2454 13         86 foreach my $class ($self->super_classes) {
2455 49         276 (my $file = $class) =~ s{::}{/}g;
2456             # NOTE: silently skipping relative paths if any chdir() happened
2457 49 50       240 $file = $INC{$file . '.pm'} or next;
2458 49 50       2342 open(my $fh, '<', $file) or next;
2459 49         129 $files_found++;
2460              
2461             # Code below modified from /usr/bin/perldoc
2462              
2463             # Skip to ACTIONS section
2464 49         99 local $_;
2465 49         1021 while (<$fh>) {
2466 77491 100       164590 last if /^=head1 ACTIONS\s/;
2467             }
2468              
2469             # Look for our action and determine the style
2470 49         99 my $style;
2471 49         321 while (<$fh>) {
2472 5671 100       10117 last if /^=head1 /;
2473              
2474             # only item and head2 are allowed (3&4 are not in 5.005)
2475 5661 100       18700 if(/^=(item|head2)\s+\Q$action\E\b/) {
2476 11         57 $style = $1;
2477 11         37 push @docs, $_;
2478 11         29 last;
2479             }
2480             }
2481 49 100       645 $style or next; # not here
2482              
2483             # and the content
2484 11 100       47 if($style eq 'item') {
2485 8         30 my ($found, $inlist) = (0, 0);
2486 8         44 while (<$fh>) {
2487 64 100       137 if (/^=(item|back)/) {
2488 8 50       188 last unless $inlist;
2489             }
2490 56         104 push @docs, $_;
2491 56 50       109 ++$inlist if /^=over/;
2492 56 50       133 --$inlist if /^=back/;
2493             }
2494             }
2495             else { # head2 style
2496             # stop at anything equal or greater than the found level
2497 3         24 while (<$fh>) {
2498 18 100       103 last if(/^=(?:head[12]|cut)/);
2499 15         40 push @docs, $_;
2500             }
2501             }
2502             # TODO maybe disallow overriding just pod for an action
2503             # TODO and possibly: @docs and last;
2504             }
2505              
2506 13 50       53 unless ($files_found) {
2507 0         0 $@ = "Couldn't find any documentation to search";
2508 0         0 return;
2509             }
2510 13 100       38 unless (@docs) {
2511 3         33 $@ = "Couldn't find any docs for action '$action'";
2512 3         46 return;
2513             }
2514              
2515 10         146 return join '', @docs;
2516             }
2517              
2518             sub ACTION_prereq_report {
2519 0     0 0 0 my $self = shift;
2520 0         0 $self->log_info( $self->prereq_report );
2521             }
2522              
2523             sub ACTION_prereq_data {
2524 0     0 0 0 my $self = shift;
2525 0         0 $self->log_info( Module::Build::Dumper->_data_dump( $self->prereq_data ) );
2526             }
2527              
2528             sub prereq_data {
2529 0     0 0 0 my $self = shift;
2530 0         0 my @types = ('configure_requires', @{ $self->prereq_action_types } );
  0         0  
2531 0         0 my $info = { map { $_ => $self->$_() } grep { %{$self->$_()} } @types };
  0         0  
  0         0  
  0         0  
2532 0         0 return $info;
2533             }
2534              
2535             sub prereq_report {
2536 0     0 0 0 my $self = shift;
2537 0         0 my $info = $self->prereq_data;
2538              
2539 0         0 my $output = '';
2540 0         0 foreach my $type (sort keys %$info) {
2541 0         0 my $prereqs = $info->{$type};
2542 0         0 $output .= "\n$type:\n";
2543 0         0 my $mod_len = 2;
2544 0         0 my $ver_len = 4;
2545 0         0 my %mods;
2546 0         0 foreach my $modname (sort keys %$prereqs) {
2547 0         0 my $spec = $prereqs->{$modname};
2548 0         0 my $len = length $modname;
2549 0 0       0 $mod_len = $len if $len > $mod_len;
2550 0   0     0 $spec ||= '0';
2551 0         0 $len = length $spec;
2552 0 0       0 $ver_len = $len if $len > $ver_len;
2553              
2554 0         0 my $mod = $self->check_installed_status($modname, $spec);
2555 0         0 $mod->{name} = $modname;
2556 0   0     0 $mod->{ok} ||= 0;
2557 0 0       0 $mod->{ok} = ! $mod->{ok} if $type =~ /^(\w+_)?conflicts$/;
2558              
2559 0         0 $mods{lc $modname} = $mod;
2560             }
2561              
2562 0         0 my $space = q{ } x ($mod_len - 3);
2563 0         0 my $vspace = q{ } x ($ver_len - 3);
2564 0         0 my $sline = q{-} x ($mod_len - 3);
2565 0         0 my $vline = q{-} x ($ver_len - 3);
2566 0 0       0 my $disposition = ($type =~ /^(\w+_)?conflicts$/) ?
2567             'Clash' : 'Need';
2568 0         0 $output .=
2569             " Module $space $disposition $vspace Have\n".
2570             " ------$sline+------$vline-+----------\n";
2571              
2572              
2573 0         0 for my $k (sort keys %mods) {
2574 0         0 my $mod = $mods{$k};
2575 0         0 my $space = q{ } x ($mod_len - length $k);
2576 0         0 my $vspace = q{ } x ($ver_len - length $mod->{need});
2577 0 0       0 my $f = $mod->{ok} ? ' ' : '!';
2578             $output .=
2579             " $f $mod->{name} $space $mod->{need} $vspace ".
2580 0 0       0 (defined($mod->{have}) ? $mod->{have} : "")."\n";
2581             }
2582             }
2583 0         0 return $output;
2584             }
2585              
2586             sub ACTION_help {
2587 0     0 0 0 my ($self) = @_;
2588 0         0 my $actions = $self->known_actions;
2589              
2590 0 0       0 if (@{$self->{args}{ARGV}}) {
  0         0  
2591 0         0 my $msg = eval {$self->get_action_docs($self->{args}{ARGV}[0], $actions)};
  0         0  
2592 0 0       0 print $@ ? "$@\n" : $msg;
2593 0         0 return;
2594             }
2595              
2596 0         0 print <
2597              
2598             Usage: $0 --arg1=value --arg2=value ...
2599             Example: $0 test --verbose=1
2600              
2601             Actions defined:
2602             EOF
2603              
2604 0         0 print $self->_action_listing($actions);
2605              
2606 0         0 print "\nRun `Build help ` for details on an individual action.\n";
2607 0         0 print "See `perldoc Module::Build` for complete documentation.\n";
2608             }
2609              
2610             sub _action_listing {
2611 0     0   0 my ($self, $actions) = @_;
2612              
2613             # Flow down columns, not across rows
2614 0         0 my @actions = sort keys %$actions;
2615 0         0 @actions = map $actions[($_ + ($_ % 2) * @actions) / 2], 0..$#actions;
2616              
2617 0         0 my $out = '';
2618 0         0 while (my ($one, $two) = splice @actions, 0, 2) {
2619 0   0     0 $out .= sprintf(" %-12s %-12s\n", $one, $two||'');
2620             }
2621 0         0 $out =~ s{\s*$}{}mg; # remove trailing spaces
2622 0         0 return $out;
2623             }
2624              
2625             sub ACTION_retest {
2626 0     0 0 0 my ($self) = @_;
2627              
2628             # Protect others against our @INC changes
2629 0         0 local @INC = @INC;
2630              
2631             # Filter out nonsensical @INC entries - some versions of
2632             # Test::Harness will really explode the number of entries here
2633 0 0       0 @INC = grep {ref() || -d} @INC if @INC > 100;
  0 0       0  
2634              
2635 0         0 $self->do_tests;
2636             }
2637              
2638             sub ACTION_testall {
2639 2     2 0 19 my ($self) = @_;
2640              
2641 2         11 my @types;
2642 2         39 for my $action (grep { $_ ne 'all' } $self->get_test_types) {
  4         33  
2643             # XXX We can't just dispatch because we get multiple summaries but
2644             # we'll need to dispatch to support custom setup/teardown in the
2645             # action. To support that, we'll need to call something besides
2646             # Harness::runtests() because we'll need to collect the results in
2647             # parts, then run the summary.
2648 4         13 push(@types, $action);
2649             #$self->_call_action( "test$action" );
2650             }
2651 2         28 $self->generic_test(types => ['default', @types]);
2652             }
2653              
2654             sub get_test_types {
2655 2     2 0 10 my ($self) = @_;
2656              
2657 2         12 my $t = $self->{properties}->{test_types};
2658 2 50       54 return ( defined $t ? ( wantarray ? sort keys %$t : keys %$t ) : () );
    50          
2659             }
2660              
2661              
2662             sub ACTION_test {
2663 23     23 0 131 my ($self) = @_;
2664 23         321 $self->generic_test(type => 'default');
2665             }
2666              
2667             sub generic_test {
2668 32     32 0 125 my $self = shift;
2669 32 50       183 (@_ % 2) and croak('Odd number of elements in argument hash');
2670 32         279 my %args = @_;
2671              
2672 32         130 my $p = $self->{properties};
2673              
2674             my @types = (
2675             (exists($args{type}) ? $args{type} : ()),
2676 32 100       263 (exists($args{types}) ? @{$args{types}} : ()),
  2 100       17  
2677             );
2678 32 50       191 @types or croak "need some types of tests to check";
2679              
2680             my %test_types = (
2681             default => $p->{test_file_exts},
2682 32 100       319 (defined($p->{test_types}) ? %{$p->{test_types}} : ()),
  9         108  
2683             );
2684              
2685 32         345 for my $type (@types) {
2686             croak "$type not defined in test_types!"
2687 36 50       252 unless defined $test_types{ $type };
2688             }
2689              
2690             # we use local here because it ends up two method calls deep
2691 32 100       147 local $p->{test_file_exts} = [ map { ref $_ ? @$_ : $_ } @test_types{@types} ];
  36         409  
2692 32         519 $self->depends_on('code');
2693              
2694             # Protect others against our @INC changes
2695 32         545 local @INC = @INC;
2696              
2697             # Make sure we test the module in blib/
2698             unshift @INC, (File::Spec->catdir($p->{base_dir}, $self->blib, 'lib'),
2699 32         222 File::Spec->catdir($p->{base_dir}, $self->blib, 'arch'));
2700              
2701             # Filter out nonsensical @INC entries - some versions of
2702             # Test::Harness will really explode the number of entries here
2703 32 0       232 @INC = grep {ref() || -d} @INC if @INC > 100;
  0 50       0  
2704              
2705 32         325 $self->do_tests;
2706             }
2707              
2708             # Test::Harness dies on failure but TAP::Harness does not, so we must
2709             # die if running under TAP::Harness
2710             sub do_tests {
2711 32     32 0 163 my $self = shift;
2712              
2713 32         217 my $tests = $self->find_test_files;
2714              
2715 32         480 local $ENV{PERL_DL_NONLAZY} = 1;
2716              
2717 32 50       193 if(@$tests) {
2718 32         370 my $args = $self->tap_harness_args;
2719 32 100 33     294 if($self->use_tap_harness or ($args and %$args)) {
      66        
2720 5         23 my $aggregate = $self->run_tap_harness($tests);
2721 5 50       58 if ( $aggregate->has_errors ) {
2722 0         0 die "Errors in testing. Cannot continue.\n";
2723             }
2724             }
2725             else {
2726 27         229 $self->run_test_harness($tests);
2727             }
2728             }
2729             else {
2730 0         0 $self->log_info("No tests defined.\n");
2731             }
2732              
2733 32         16528855 $self->run_visual_script;
2734             }
2735              
2736             sub run_tap_harness {
2737 5     5 0 10 my ($self, $tests) = @_;
2738              
2739 5         2262 require TAP::Harness::Env;
2740              
2741             # TODO allow the test @INC to be set via our API?
2742              
2743             my $aggregate = TAP::Harness::Env->create({
2744             lib => [@INC],
2745             verbosity => $self->{properties}{verbose},
2746             switches => [ $self->harness_switches ],
2747 5         3325 %{ $self->tap_harness_args },
  5         14  
2748             })->runtests(@$tests);
2749              
2750 5         2064077 return $aggregate;
2751             }
2752              
2753             sub run_test_harness {
2754 29     29 0 237 my ($self, $tests) = @_;
2755 29         10990 require Test::Harness;
2756              
2757 29   100     473453 local $Test::Harness::verbose = $self->verbose || 0;
2758 29         330 local $Test::Harness::switches = join ' ', $self->harness_switches;
2759              
2760 29         210 Test::Harness::runtests(@$tests);
2761             }
2762              
2763             sub run_visual_script {
2764 32     32 0 249 my $self = shift;
2765             # This will get run and the user will see the output. It doesn't
2766             # emit Test::Harness-style output.
2767 32 50       5190 $self->run_perl_script('visual.pl', '-Mblib='.$self->blib)
2768             if -e 'visual.pl';
2769             }
2770              
2771             sub harness_switches {
2772 32     32 0 90 my $self = shift;
2773 32         78 my @res;
2774 32 50       214 push @res, qw(-w -d) if $self->{properties}{debugger};
2775 32 50       225 push @res, '-MDevel::Cover' if $self->{properties}{cover};
2776 32         202 return @res;
2777             }
2778              
2779             sub test_files {
2780 4     4 0 3737 my $self = shift;
2781 4         7 my $p = $self->{properties};
2782 4 100       14 if (@_) {
2783 2 100       23 return $p->{test_files} = (@_ == 1 ? shift : [@_]);
2784             }
2785 2         23 return $self->find_test_files;
2786             }
2787              
2788             sub expand_test_dir {
2789 33     33 0 329 my ($self, $dir) = @_;
2790 33         173 my $exts = $self->{properties}{test_file_exts};
2791              
2792 33 100       238 return sort map { @{$self->rscan_dir($dir, qr{^[^.].*\Q$_\E$})} } @$exts
  5         20  
  5         206  
2793             if $self->recursive_test_files;
2794              
2795 30         134 return sort map { glob File::Spec->catfile($dir, "*$_") } @$exts;
  32         3630  
2796             }
2797              
2798             sub ACTION_testdb {
2799 0     0 0 0 my ($self) = @_;
2800 0         0 local $self->{properties}{debugger} = 1;
2801 0         0 $self->depends_on('test');
2802             }
2803              
2804             sub ACTION_testcover {
2805 0     0 0 0 my ($self) = @_;
2806              
2807 0 0       0 unless (Module::Metadata->find_module_by_name('Devel::Cover')) {
2808 0         0 warn("Cannot run testcover action unless Devel::Cover is installed.\n");
2809 0         0 return;
2810             }
2811              
2812 0         0 $self->add_to_cleanup('coverage', 'cover_db');
2813 0         0 $self->depends_on('code');
2814              
2815             # See whether any of the *.pm files have changed since last time
2816             # testcover was run. If so, start over.
2817 0 0       0 if (-e 'cover_db') {
2818 0         0 my $pm_files = $self->rscan_dir
2819             (File::Spec->catdir($self->blib, 'lib'), $self->file_qr('\.pm$') );
2820 0 0   0   0 my $cover_files = $self->rscan_dir('cover_db', sub {-f $_ and not /\.html$/});
  0         0  
2821              
2822 0 0 0     0 $self->do_system(qw(cover -delete))
2823             unless $self->up_to_date($pm_files, $cover_files)
2824             && $self->up_to_date($self->test_files, $cover_files);
2825             }
2826              
2827 0         0 local $self->{properties}{cover} = 1;
2828 0         0 $self->depends_on('test');
2829 0         0 $self->do_system('cover');
2830             }
2831              
2832             sub ACTION_code {
2833 64     64 0 268 my ($self) = @_;
2834              
2835             # All installable stuff gets created in blib/ .
2836             # Create blib/arch to keep blib.pm happy
2837 64         939 my $blib = $self->blib;
2838 64         746 $self->add_to_cleanup($blib);
2839 64         15168 File::Path::mkpath( File::Spec->catdir($blib, 'arch') );
2840              
2841 64 50       1613 if (my $split = $self->autosplit) {
2842 0 0       0 $self->autosplit_file($_, $blib) for ref($split) ? @$split : ($split);
2843             }
2844              
2845 64         186 foreach my $element (@{$self->build_elements}) {
  64         629  
2846 440         2046 my $method = "process_${element}_files";
2847 440 100       5769 $method = "process_files_by_extension" unless $self->can($method);
2848 440         2848 $self->$method($element);
2849             }
2850              
2851 61         608 $self->depends_on('config_data');
2852             }
2853              
2854             sub ACTION_build {
2855 30     30 0 135 my $self = shift;
2856 30         300 $self->log_info("Building " . $self->dist_name . "\n");
2857 30         449 $self->depends_on('code');
2858 27         196 $self->depends_on('docs');
2859             }
2860              
2861             sub process_files_by_extension {
2862 126     126 0 597 my ($self, $ext) = @_;
2863              
2864 126         480 my $method = "find_${ext}_files";
2865 126 100       1453 my $files = $self->can($method) ? $self->$method() : $self->_find_file_by_type($ext, 'lib');
2866              
2867 126         1246 foreach my $file (sort keys %$files) {
2868 74         518 $self->copy_if_modified(from => $file, to => File::Spec->catfile($self->blib, $files->{$file}) );
2869             }
2870             }
2871              
2872             sub process_support_files {
2873 64     64 0 255 my $self = shift;
2874 64         254 my $p = $self->{properties};
2875 64 50       430 return unless $p->{c_source};
2876 0 0 0     0 return if $self->pureperl_only && $self->allow_pureperl;
2877              
2878 0         0 my $files;
2879 0 0       0 if (ref($p->{c_source}) eq "ARRAY") {
2880 0         0 push @{$p->{include_dirs}}, @{$p->{c_source}};
  0         0  
  0         0  
2881 0         0 for my $path (@{$p->{c_source}}) {
  0         0  
2882 0         0 push @$files, @{ $self->rscan_dir($path, $self->file_qr('\.c(c|p|pp|xx|\+\+)?$')) };
  0         0  
2883             }
2884             } else {
2885 0         0 push @{$p->{include_dirs}}, $p->{c_source};
  0         0  
2886 0         0 $files = $self->rscan_dir($p->{c_source}, $self->file_qr('\.c(c|p|pp|xx|\+\+)?$'));
2887             }
2888              
2889 0         0 foreach my $file (@$files) {
2890 0         0 push @{$p->{objects}}, $self->compile_c($file);
  0         0  
2891             }
2892             }
2893              
2894             sub process_share_dir_files {
2895 61     61 0 357 my $self = shift;
2896 61         887 my $files = $self->_find_share_dir_files;
2897 61 100       349 return unless $files;
2898              
2899             # root for all File::ShareDir paths
2900 2         13 my $share_prefix = File::Spec->catdir($self->blib, qw/lib auto share/);
2901              
2902             # copy all share files to blib
2903 2         14 foreach my $file (sort keys %$files) {
2904             $self->copy_if_modified(
2905 8         89 from => $file, to => File::Spec->catfile( $share_prefix, $files->{$file} )
2906             );
2907             }
2908             }
2909              
2910             sub _find_share_dir_files {
2911 62     62   3153 my $self = shift;
2912 62         921 my $share_dir = $self->share_dir;
2913 62 100       385 return unless $share_dir;
2914              
2915 3         13 my @file_map;
2916 3 50       18 if ( $share_dir->{dist} ) {
2917 3         72 my $prefix = "dist/".$self->dist_name;
2918 3         26 push @file_map, $self->_share_dir_map( $prefix, $share_dir->{dist} );
2919             }
2920              
2921 3 50       17 if ( $share_dir->{module} ) {
2922 3         8 for my $mod ( sort keys %{ $share_dir->{module} } ) {
  3         12  
2923 3         22 (my $altmod = $mod) =~ s{::}{-}g;
2924 3         10 my $prefix = "module/$altmod";
2925 3         19 push @file_map, $self->_share_dir_map($prefix, $share_dir->{module}{$mod});
2926             }
2927             }
2928              
2929 3         28 return { @file_map };
2930             }
2931              
2932             sub _share_dir_map {
2933 6     6   21 my ($self, $prefix, $list) = @_;
2934 6         15 my %files;
2935 6         18 for my $dir ( @$list ) {
2936 6     27   9 for my $f ( @{ $self->rscan_dir( $dir, sub {-f} )} ) {
  6         61  
  27         1277  
2937 12         165 $f =~ s{\A.*?\Q$dir\E/}{};
2938 12         82 $files{"$dir/$f"} = "$prefix/$f";
2939             }
2940             }
2941 6         34 return %files;
2942             }
2943              
2944             sub process_PL_files {
2945 64     64 0 282 my ($self) = @_;
2946 64         625 my $files = $self->find_PL_files;
2947              
2948 64         736 foreach my $file (sort keys %$files) {
2949 6         21 my $to = $files->{$file};
2950 6 50       175 unless ($self->up_to_date( $file, $to )) {
2951 6 50       92 $self->run_perl_script($file, [], [@$to]) or die "$file failed";
2952 6         238 $self->add_to_cleanup(@$to);
2953             }
2954             }
2955             }
2956              
2957             sub process_xs_files {
2958 64     64 0 216 my $self = shift;
2959 64 100 100     1042 return if $self->pureperl_only && $self->allow_pureperl;
2960 62         440 my $files = $self->find_xs_files;
2961 62 100 100     565 croak 'Can\'t build xs files under --pureperl-only' if %$files && $self->pureperl_only;
2962 60         501 foreach my $from (sort keys %$files) {
2963 19         51 my $to = $files->{$from};
2964 19 100       69 unless ($from eq $to) {
2965 2         22 $self->add_to_cleanup($to);
2966 2         28 $self->copy_if_modified( from => $from, to => $to );
2967             }
2968 19         242 $self->process_xs($to);
2969             }
2970             }
2971              
2972 61     61 0 400 sub process_pod_files { shift()->process_files_by_extension(shift()) }
2973 64     64 0 593 sub process_pm_files { shift()->process_files_by_extension(shift()) }
2974              
2975             sub process_script_files {
2976 61     61 0 339 my $self = shift;
2977 61         513 my $files = $self->find_script_files;
2978 61 100       450 return unless keys %$files;
2979              
2980 14         149 my $script_dir = File::Spec->catdir($self->blib, 'script');
2981 14         2481 File::Path::mkpath( $script_dir );
2982              
2983 14         142 foreach my $file (sort keys %$files) {
2984 17 100       163 my $result = $self->copy_if_modified($file, $script_dir, 'flatten') or next;
2985 9 50       279 $self->fix_shebang_line($result) unless $self->is_vmsish;
2986 9         257 $self->make_executable($result);
2987             }
2988             }
2989              
2990             sub find_PL_files {
2991 64     64 0 196 my $self = shift;
2992 64 100       399 if (my $files = $self->{properties}{PL_files}) {
2993             # 'PL_files' is given as a Unix file spec, so we localize_file_path().
2994              
2995 3 50       57 if (ref $files eq 'ARRAY') {
    50          
2996 0         0 return { map {$_, [/^(.*)\.PL$/]}
  0         0  
2997             map $self->localize_file_path($_),
2998             @$files };
2999              
3000             } elsif (ref $files eq 'HASH') {
3001 3         8 my %out;
3002 3         43 while (my ($file, $to) = each %$files) {
3003 5 100       45 $out{ $self->localize_file_path($file) } = [ map $self->localize_file_path($_),
3004             ref $to ? @$to : ($to) ];
3005             }
3006 3         12 return \%out;
3007              
3008             } else {
3009 0         0 die "'PL_files' must be a hash reference or array reference";
3010             }
3011             }
3012              
3013 61 100       901 return unless -d 'lib';
3014             return {
3015 1         12 map {$_, [/^(.*)\.PL$/i ]}
3016 60         282 @{ $self->rscan_dir('lib', $self->file_qr('\.PL$')) }
  60         905  
3017             };
3018             }
3019              
3020 83     83 0 766 sub find_pm_files { shift->_find_file_by_type('pm', 'lib') }
3021 61     61 0 486 sub find_pod_files { shift->_find_file_by_type('pod', 'lib') }
3022 147     147 0 1345 sub find_xs_files { shift->_find_file_by_type('xs', 'lib') }
3023              
3024             sub find_script_files {
3025 61     61 0 220 my $self = shift;
3026 61 50       803 if (my $files = $self->script_files) {
3027             # Always given as a Unix file spec. Values in the hash are
3028             # meaningless, but we preserve if present.
3029 61         431 return { map {$self->localize_file_path($_), $files->{$_}} keys %$files };
  17         73  
3030             }
3031              
3032             # No default location for script files
3033 0         0 return {};
3034             }
3035              
3036             sub find_test_files {
3037 34     34 0 100 my $self = shift;
3038 34         147 my $p = $self->{properties};
3039              
3040 34 100       132 if (my $files = $p->{test_files}) {
3041 2 50       8 $files = [sort keys %$files] if ref $files eq 'HASH';
3042 2 100       20 $files = [map { -d $_ ? $self->expand_test_dir($_) : $_ }
  4         372  
3043             map glob,
3044             $self->split_like_shell($files)];
3045              
3046             # Always given as a Unix file spec.
3047 2         30 return [ map $self->localize_file_path($_), @$files ];
3048              
3049             } else {
3050             # Find all possible tests in t/ or test.pl
3051 32         108 my @tests;
3052 32 50       625 push @tests, 'test.pl' if -e 'test.pl';
3053 32 50 33     1218 push @tests, $self->expand_test_dir('t') if -e 't' and -d _;
3054 32         194 return \@tests;
3055             }
3056             }
3057              
3058             sub _find_file_by_type {
3059 295     295   5743 my ($self, $type, $dir) = @_;
3060              
3061 295 100       2040 if (my $files = $self->{properties}{"${type}_files"}) {
3062             # Always given as a Unix file spec
3063 5         71 return { map $self->localize_file_path($_), %$files };
3064             }
3065              
3066 290 100       4326 return {} unless -d $dir;
3067 114         1112 return { map {$_, $_}
3068             map $self->localize_file_path($_),
3069             grep !/\.\#/,
3070 286         1120 @{ $self->rscan_dir($dir, $self->file_qr("\\.$type\$")) } };
  286         3438  
3071             }
3072              
3073             sub localize_file_path {
3074 683     683 0 5543 my ($self, $path) = @_;
3075 683         9354 return File::Spec->catfile( split m{/}, $path );
3076             }
3077              
3078             sub localize_dir_path {
3079 88     88 0 216 my ($self, $path) = @_;
3080 88         735 return File::Spec->catdir( split m{/}, $path );
3081             }
3082              
3083             sub fix_shebang_line { # Adapted from fixin() in ExtUtils::MM_Unix 1.35
3084 9     9 0 513 my ($self, @files) = @_;
3085 9 50       90 my $c = ref($self) ? $self->{config} : 'Module::Build::Config';
3086              
3087 9         142 my ($does_shbang) = $c->get('sharpbang') =~ /^\s*\#\!/;
3088 9         188 for my $file (@files) {
3089 9 50       474 open(my $FIXIN, '<', $file) or die "Can't process '$file': $!";
3090 9         130 local $/ = "\n";
3091 9         331 chomp(my $line = <$FIXIN>);
3092 9 100       140 next unless $line =~ s/^\s*\#!\s*//; # Not a shebang file.
3093              
3094 7         89 my ($cmd, $arg) = (split(' ', $line, 2), '');
3095 7 50       99 next unless $cmd =~ /perl/i;
3096 7         39 my $interpreter = $self->{properties}{perl};
3097              
3098 7         84 $self->log_verbose("Changing sharpbang in $file to $interpreter\n");
3099 7         43 my $shb = '';
3100 7 50       106 $shb .= $c->get('sharpbang')."$interpreter $arg\n" if $does_shbang;
3101              
3102 7 50       592 open(my $FIXOUT, '>', "$file.new")
3103             or die "Can't create new $file: $!\n";
3104              
3105             # Print out the new #! line (or equivalent).
3106 7         42 local $\;
3107 7         46 undef $/; # Was localized above
3108 7         264 print $FIXOUT $shb, <$FIXIN>;
3109 7         94 close $FIXIN;
3110 7         246 close $FIXOUT;
3111              
3112 7 50       416 rename($file, "$file.bak")
3113             or die "Can't rename $file to $file.bak: $!";
3114              
3115 7 50       259 rename("$file.new", $file)
3116             or die "Can't rename $file.new to $file: $!";
3117              
3118 7 50       86 $self->delete_filetree("$file.bak")
3119             or $self->log_warn("Couldn't clean up $file.bak, leaving it there");
3120              
3121 7 50       63 $self->do_system($c->get('eunicefix'), $file) if $c->get('eunicefix') ne ':';
3122             }
3123             }
3124              
3125              
3126             sub ACTION_testpod {
3127 0     0 0 0 my $self = shift;
3128 0         0 $self->depends_on('docs');
3129              
3130 0 0       0 eval q{use Test::Pod 0.95; 1}
3131             or die "The 'testpod' action requires Test::Pod version 0.95";
3132              
3133 0         0 my @files = sort keys %{$self->_find_pods($self->libdoc_dirs)},
3134 0 0       0 keys %{$self->_find_pods
  0         0  
3135             ($self->bindoc_dirs,
3136             exclude => [ $self->file_qr('\.bat$') ])}
3137             or die "Couldn't find any POD files to test\n";
3138              
3139 0         0 { package # hide from PAUSE
3140             Module::Build::PodTester; # Don't want to pollute the main namespace
3141 0         0 Test::Pod->import( tests => scalar @files );
3142 0         0 pod_file_ok($_) foreach @files;
3143             }
3144             }
3145              
3146             sub ACTION_testpodcoverage {
3147 0     0 0 0 my $self = shift;
3148              
3149 0         0 $self->depends_on('docs');
3150              
3151 0 0       0 eval q{use Test::Pod::Coverage 1.00; 1}
3152             or die "The 'testpodcoverage' action requires ",
3153             "Test::Pod::Coverage version 1.00";
3154              
3155             # TODO this needs test coverage!
3156              
3157             # XXX work-around a bug in Test::Pod::Coverage previous to v1.09
3158             # Make sure we test the module in blib/
3159 0         0 local @INC = @INC;
3160 0         0 my $p = $self->{properties};
3161             unshift(@INC,
3162             # XXX any reason to include arch?
3163 0         0 File::Spec->catdir($p->{base_dir}, $self->blib, 'lib'),
3164             #File::Spec->catdir($p->{base_dir}, $self->blib, 'arch')
3165             );
3166              
3167 0         0 all_pod_coverage_ok();
3168             }
3169              
3170             sub ACTION_docs {
3171 28     28 0 122 my $self = shift;
3172              
3173 28         121 $self->depends_on('code');
3174 28         167 $self->depends_on('manpages', 'html');
3175             }
3176              
3177             # Given a file type, will return true if the file type would normally
3178             # be installed when neither install-base nor prefix has been set.
3179             # I.e. it will be true only if the path is set from Config.pm or
3180             # set explicitly by the user via install-path.
3181             sub _is_default_installable {
3182 112     112   676 my $self = shift;
3183 112         207 my $type = shift;
3184             return ( $self->install_destination($type) &&
3185             ( $self->install_path($type) ||
3186 112 100 100     576 $self->install_sets($self->installdirs)->{$type} )
3187             ) ? 1 : 0;
3188             }
3189              
3190             sub _is_ActivePerl {
3191             # return 0;
3192 29     29   113 my $self = shift;
3193 29 100       147 unless (exists($self->{_is_ActivePerl})) {
3194 8   50     32 $self->{_is_ActivePerl} = (eval { require ActivePerl::DocTools; } || 0);
3195             }
3196 29         195 return $self->{_is_ActivePerl};
3197             }
3198              
3199             sub _is_ActivePPM {
3200             # return 0;
3201 9     9   63 my $self = shift;
3202 9 100       168 unless (exists($self->{_is_ActivePPM})) {
3203 5   50     18 $self->{_is_ActivePPM} = (eval { require ActivePerl::PPM; } || 0);
3204             }
3205 9         127 return $self->{_is_ActivePPM};
3206             }
3207              
3208             sub ACTION_manpages {
3209 28     28 0 84 my $self = shift;
3210              
3211 28 50       311 return unless $self->_mb_feature('manpage_support');
3212              
3213 28         177 $self->depends_on('code');
3214              
3215 28 100       235 my %extra_manify_args = $self->{properties}{'extra_manify_args'} ? %{ $self->{properties}{'extra_manify_args'} } : ();
  1         8  
3216              
3217 28         228 foreach my $type ( qw(bin lib) ) {
3218 56 100 66     619 next unless ( $self->invoked_action eq 'manpages' || $self->_is_default_installable("${type}doc"));
3219 24         150 my $files = $self->_find_pods( $self->{properties}{"${type}doc_dirs"},
3220             exclude => [ $self->file_qr('\.bat$') ] );
3221 24 100       235 next unless %$files;
3222              
3223 18         263 my $sub = $self->can("manify_${type}_pods");
3224 18 50       228 $self->$sub( %extra_manify_args ) if defined( $sub );
3225             }
3226             }
3227              
3228             sub manify_bin_pods {
3229 6     6 0 20 my $self = shift;
3230 6         41 my $section = $self->config('man1ext');
3231 6         65 my %podman_args = (section => $section, @_);
3232              
3233             my $files = $self->_find_pods( $self->{properties}{bindoc_dirs},
3234 6         37 exclude => [ $self->file_qr('\.bat$') ] );
3235 6 50       46 return unless keys %$files;
3236              
3237 6         30 my $mandir = File::Spec->catdir( $self->blib, 'bindoc' );
3238 6         608 File::Path::mkpath( $mandir, 0, oct(777) );
3239              
3240 6         56 require Pod::Man;
3241 6         42 foreach my $file (sort keys %$files) {
3242             # Pod::Simple based parsers only support one document per instance.
3243             # This is expected to change in a future version (Pod::Simple > 3.03).
3244 18         241 my $parser = Pod::Man->new( %podman_args );
3245 18         5693 my $manpage = $self->man1page_name( $file ) . '.' .
3246             $self->config( 'man1ext' );
3247 18         206 my $outfile = File::Spec->catfile($mandir, $manpage);
3248 18 100       67 next if $self->up_to_date( $file, $outfile );
3249 8         76 $self->log_verbose("Manifying $file -> $outfile\n");
3250 8 50       19 eval { $parser->parse_from_file( $file, $outfile ); 1 }
  8         96  
  8         24700  
3251             or $self->log_warn("Error creating '$outfile': $@\n");
3252 8         255 $files->{$file} = $outfile;
3253             }
3254             }
3255              
3256             sub manify_lib_pods {
3257 12     12 0 41 my $self = shift;
3258 12         72 my $section = $self->config('man3ext');
3259 12         114 my %podman_args = (section => $section, @_);
3260              
3261 12         56 my $files = $self->_find_pods($self->{properties}{libdoc_dirs});
3262 12 50       106 return unless keys %$files;
3263              
3264 12         58 my $mandir = File::Spec->catdir( $self->blib, 'libdoc' );
3265 12         1078 File::Path::mkpath( $mandir, 0, oct(777) );
3266              
3267 12         124 require Pod::Man;
3268 12         127 foreach my $file (sort keys %$files) {
3269             # Pod::Simple based parsers only support one document per instance.
3270             # This is expected to change in a future version (Pod::Simple > 3.03).
3271 17         209 my $parser = Pod::Man->new( %podman_args );
3272 17         5154 my $manpage = $self->man3page_name( $files->{$file} ) . '.' .
3273             $self->config( 'man3ext' );
3274 17         229 my $outfile = File::Spec->catfile( $mandir, $manpage);
3275 17 100       160 next if $self->up_to_date( $file, $outfile );
3276 9         138 $self->log_verbose("Manifying $file -> $outfile\n");
3277 9 50       52 eval { $parser->parse_from_file( $file, $outfile ); 1 }
  9         51  
  9         51127  
3278             or $self->log_warn("Error creating '$outfile': $@\n");
3279 9         292 $files->{$file} = $outfile;
3280             }
3281             }
3282              
3283             sub _find_pods {
3284 52     52   265 my ($self, $dirs, %args) = @_;
3285 52         101 my %files;
3286 52         178 foreach my $spec (@$dirs) {
3287 87         339 my $dir = $self->localize_dir_path($spec);
3288 87 100       1359 next unless -e $dir;
3289              
3290 86         209 FILE: foreach my $file ( @{ $self->rscan_dir( $dir ) } ) {
  86         284  
3291 237         536 foreach my $regexp ( @{ $args{exclude} } ) {
  237         546  
3292 176 50       1032 next FILE if $file =~ $regexp;
3293             }
3294 237         687 $file = $self->localize_file_path($file);
3295 237 100       689 $files{$file} = File::Spec->abs2rel($file, $dir) if $self->contains_pod( $file )
3296             }
3297             }
3298 52         271 return \%files;
3299             }
3300              
3301             sub contains_pod {
3302 246     246 0 4595 my ($self, $file) = @_;
3303 246 100       11371 return '' unless -T $file; # Only look at text files
3304              
3305 117 50       4042 open(my $fh, '<', $file ) or die "Can't open $file: $!";
3306 117         1668 while (my $line = <$fh>) {
3307 757 100       9532 return 1 if $line =~ /^\=(?:head|pod|item)/;
3308             }
3309              
3310 30         504 return '';
3311             }
3312              
3313             sub ACTION_html {
3314 28     28 0 90 my $self = shift;
3315              
3316 28 50       123 return unless $self->_mb_feature('HTML_support');
3317              
3318 28         274 $self->depends_on('code');
3319              
3320 28         201 foreach my $type ( qw(bin lib) ) {
3321 56 100 66     277 next unless ( $self->invoked_action eq 'html' || $self->_is_default_installable("${type}html"));
3322 4         48 $self->htmlify_pods( $type );
3323             }
3324             }
3325              
3326             # 1) If it's an ActiveState perl install, we need to run
3327             # ActivePerl::DocTools->UpdateTOC;
3328             # 2) Links to other modules are not being generated
3329             sub htmlify_pods {
3330 10     10 0 123 my $self = shift;
3331 10         47 my $type = shift;
3332 10   66     89 my $htmldir = shift || File::Spec->catdir($self->blib, "${type}html");
3333              
3334 10         89 $self->add_to_cleanup('pod2htm*');
3335              
3336 10         128 my $pods = $self->_find_pods( $self->{properties}{"${type}doc_dirs"},
3337             exclude => [ $self->file_qr('\.(?:bat|com|html)$') ] );
3338 10 50       53 return unless %$pods; # nothing to do
3339              
3340 10 100       193 unless ( -d $htmldir ) {
3341 7 50       1212 File::Path::mkpath($htmldir, 0, oct(755))
3342             or die "Couldn't mkdir $htmldir: $!";
3343             }
3344              
3345 10 50       105 my @rootdirs = ($type eq 'bin') ? qw(bin) :
    100          
3346             $self->installdirs eq 'core' ? qw(lib) : qw(site lib);
3347             my $podroot = $ENV{PERL_CORE}
3348             ? File::Basename::dirname($ENV{PERL_CORE})
3349 10 50       80 : $self->original_prefix('core');
3350              
3351 10         27 my $htmlroot = $self->install_sets('core')->{libhtml};
3352 10         26 my $podpath;
3353 10 50 33     69 unless (defined $self->args('html_links') and !$self->args('html_links')) {
3354 0         0 my @podpath = ( (map { File::Spec->abs2rel($_ ,$podroot) } grep { -d }
  0         0  
  0         0  
3355             ( $self->install_sets('core', 'lib'), # lib
3356             $self->install_sets('core', 'bin'), # bin
3357             $self->install_sets('site', 'lib'), # site/lib
3358             ) ), File::Spec->rel2abs($self->blib) );
3359              
3360             $podpath = $ENV{PERL_CORE}
3361             ? File::Spec->catdir($podroot, 'lib')
3362 0 0       0 : join(":", map { tr,:\\,|/,; $_ } @podpath);
  0         0  
  0         0  
3363             }
3364              
3365 10         338 my $blibdir = join('/', File::Spec->splitdir(
3366             (File::Spec->splitpath(File::Spec->rel2abs($htmldir),1))[1]),''
3367             );
3368              
3369 10         36 my ($with_ActiveState, $htmltool);
3370              
3371 10 50 33     48 if ( $with_ActiveState = $self->_is_ActivePerl
3372             && eval { require ActivePerl::DocTools::Pod; 1 }
3373             ) {
3374 0         0 my $tool_v = ActiveState::DocTools::Pod->VERSION;
3375 0         0 $htmltool = "ActiveState::DocTools::Pod";
3376 0 0 0     0 $htmltool .= " $tool_v" if $tool_v && length $tool_v;
3377             }
3378             else {
3379 10         72 require Module::Build::PodParser;
3380 10         41 require Pod::Html;
3381 10         234 $htmltool = "Pod::Html " . Pod::Html->VERSION;
3382             }
3383 10         78 $self->log_verbose("Converting Pod to HTML with $htmltool\n");
3384              
3385 10         27 my $errors = 0;
3386              
3387             POD:
3388 10         78 foreach my $pod ( sort keys %$pods ) {
3389              
3390 10         49 my ($name, $path) = File::Basename::fileparse($pods->{$pod},
3391             $self->file_qr('\.(?:pm|plx?|pod)$')
3392             );
3393 10         87 my @dirs = File::Spec->splitdir( File::Spec->canonpath( $path ) );
3394 10 50 33     122 pop( @dirs ) if scalar(@dirs) && $dirs[-1] eq File::Spec->curdir;
3395              
3396 10         73 my $fulldir = File::Spec->catdir($htmldir, @rootdirs, @dirs);
3397 10         106 my $tmpfile = File::Spec->catfile($fulldir, "${name}.tmp");
3398 10         67 my $outfile = File::Spec->catfile($fulldir, "${name}.html");
3399 10         731 my $infile = File::Spec->abs2rel($pod);
3400              
3401 10 50       79 next if $self->up_to_date($infile, $outfile);
3402              
3403 10 50       102 unless ( -d $fulldir ){
3404 10 50       1798 File::Path::mkpath($fulldir, 0, oct(755))
3405             or die "Couldn't mkdir $fulldir: $!";
3406             }
3407              
3408 10         93 $self->log_verbose("HTMLifying $infile -> $outfile\n");
3409 10 50       28 if ( $with_ActiveState ) {
3410 0         0 my $depth = @rootdirs + @dirs;
3411 0 0       0 my %opts = ( infile => $infile,
3412             outfile => $tmpfile,
3413             ( defined($podpath) ? (podpath => $podpath) : ()),
3414             podroot => $podroot,
3415             index => 1,
3416             depth => $depth,
3417             );
3418             eval {
3419 0         0 ActivePerl::DocTools::Pod::pod2html(map { ($_, $opts{$_}) } sort keys %opts);
  0         0  
3420 0         0 1;
3421             } or $self->log_warn("[$htmltool] pod2html (" .
3422 0 0       0 join(", ", map { "q{$_} => q{$opts{$_}}" } (sort keys %opts)) . ") failed: $@");
  0         0  
3423             } else {
3424 10         123 my $path2root = File::Spec->catdir((File::Spec->updir) x @dirs);
3425 10 50       398 open(my $fh, '<', $infile) or die "Can't read $infile: $!";
3426 10         184 my $abstract = Module::Build::PodParser->new(fh => $fh)->get_abstract();
3427              
3428 10         103 my $title = join( '::', (@dirs, $name) );
3429 10 100       40 $title .= " - $abstract" if $abstract;
3430              
3431 10 50       127 my @opts = (
    50          
3432             "--title=$title",
3433             ( defined($podpath) ? "--podpath=$podpath" : ()),
3434             "--infile=$infile",
3435             "--outfile=$tmpfile",
3436             "--podroot=$podroot",
3437             ($path2root ? "--htmlroot=$path2root" : ()),
3438             );
3439              
3440 10 50       26 unless ( eval{Pod::Html->VERSION(1.12)} ) {
  10         206  
3441 0         0 push( @opts, ('--flush') ); # caching removed in 1.12
3442             }
3443              
3444 10 50       24 if ( eval{Pod::Html->VERSION(1.12)} ) {
  10 0       97  
3445 10         40 push( @opts, ('--header', '--backlink') );
3446 0         0 } elsif ( eval{Pod::Html->VERSION(1.03)} ) {
3447 0         0 push( @opts, ('--header', '--backlink=Back to Top') );
3448             }
3449              
3450 10         79 $self->log_verbose("P::H::pod2html @opts\n");
3451             {
3452 10         20 my $orig = Cwd::getcwd();
  10         102  
3453 10         101 eval { Pod::Html::pod2html(@opts); 1 }
  10         79829  
3454             or $self->log_warn("[$htmltool] pod2html( " .
3455 10 50       28 join(", ", map { "q{$_}" } @opts) . ") failed: $@");
  0         0  
3456 10         315 chdir($orig);
3457             }
3458             }
3459             # We now have to cleanup the resulting html file
3460 10 50       191 if ( ! -r $tmpfile ) {
3461 0         0 $errors++;
3462 0         0 next POD;
3463             }
3464 10 50       390 open(my $fh, '<', $tmpfile) or die "Can't read $tmpfile: $!";
3465 10         473 my $html = join('',<$fh>);
3466 10         140 close $fh;
3467 10 50       140 if (!$self->_is_ActivePerl) {
3468             # These fixups are already done by AP::DT:P:pod2html
3469             # The output from pod2html is NOT XHTML!
3470             # IE6+ will display content that is not valid for DOCTYPE
3471 10         179 $html =~ s#^##im;
3472 10         90 $html =~ s###i;
3473              
3474             # IE6+ will not display local HTML files with strict
3475             # security without this comment
3476 10         85 $html =~ s##\n#i;
3477             }
3478             # Fixup links that point to our temp blib
3479 10         140 $html =~ s/\Q$blibdir\E//g;
3480              
3481 10 50       694 open($fh, '>', $outfile) or die "Can't write $outfile: $!";
3482 10         133 print $fh $html;
3483 10         332 close $fh;
3484 10         588 unlink($tmpfile);
3485             }
3486              
3487 10         211 return ! $errors;
3488              
3489             }
3490              
3491             # Adapted from ExtUtils::MM_Unix
3492             sub man1page_name {
3493 18     18 0 39 my $self = shift;
3494 18         485 return File::Basename::basename( shift );
3495             }
3496              
3497             # Adapted from ExtUtils::MM_Unix and Pod::Man
3498             # Depending on M::B's dependency policy, it might make more sense to refactor
3499             # Pod::Man::begin_pod() to extract a name() methods, and use them...
3500             # -spurkis
3501             sub man3page_name {
3502 17     17 0 44 my $self = shift;
3503 17         248 my ($vol, $dirs, $file) = File::Spec->splitpath( shift );
3504 17         139 my @dirs = File::Spec->splitdir( File::Spec->canonpath($dirs) );
3505              
3506             # Remove known exts from the base name
3507 17         201 $file =~ s/\.p(?:od|m|l)\z//i;
3508              
3509 17         110 return join( $self->manpage_separator, @dirs, $file );
3510             }
3511              
3512             sub manpage_separator {
3513 19     19 0 3287 return '::';
3514             }
3515              
3516             # For systems that don't have 'diff' executable, should use Algorithm::Diff
3517             sub ACTION_diff {
3518 0     0 0 0 my $self = shift;
3519 0         0 $self->depends_on('build');
3520 0         0 my $local_lib = File::Spec->rel2abs('lib');
3521 0         0 my @myINC = grep {$_ ne $local_lib} @INC;
  0         0  
3522              
3523             # The actual install destination might not be in @INC, so check there too.
3524 0         0 push @myINC, map $self->install_destination($_), qw(lib arch);
3525              
3526 0         0 my @flags = @{$self->{args}{ARGV}};
  0         0  
3527 0 0 0     0 @flags = $self->split_like_shell($self->{args}{flags} || '') unless @flags;
3528              
3529 0         0 my $installmap = $self->install_map;
3530 0         0 delete $installmap->{read};
3531 0         0 delete $installmap->{write};
3532              
3533 0         0 my $text_suffix = $self->file_qr('\.(pm|pod)$');
3534              
3535 0         0 foreach my $localdir (sort keys %$installmap) {
3536 0         0 my @localparts = File::Spec->splitdir($localdir);
3537 0     0   0 my $files = $self->rscan_dir($localdir, sub {-f});
  0         0  
3538              
3539 0         0 foreach my $file (@$files) {
3540 0         0 my @parts = File::Spec->splitdir($file);
3541 0         0 @parts = @parts[@localparts .. $#parts]; # Get rid of blib/lib or similar
3542              
3543 0         0 my $installed = Module::Metadata->find_module_by_name(
3544             join('::', @parts), \@myINC );
3545 0 0       0 if (not $installed) {
3546 0         0 print "Only in lib: $file\n";
3547 0         0 next;
3548             }
3549              
3550 0         0 my $status = File::Compare::compare($installed, $file);
3551 0 0       0 next if $status == 0; # Files are the same
3552 0 0       0 die "Can't compare $installed and $file: $!" if $status == -1;
3553              
3554 0 0       0 if ($file =~ $text_suffix) {
3555 0         0 $self->do_system('diff', @flags, $installed, $file);
3556             } else {
3557 0         0 print "Binary files $file and $installed differ\n";
3558             }
3559             }
3560             }
3561             }
3562              
3563             sub ACTION_pure_install {
3564 0     0 0 0 shift()->depends_on('install');
3565             }
3566              
3567             sub ACTION_install {
3568 9     9 0 35 my ($self) = @_;
3569 9         6899 require ExtUtils::Install;
3570 9         79401 $self->depends_on('build');
3571             # RT#63003 suggest that odd circumstances that we might wind up
3572             # in a different directory than we started, so wrap with _do_in_dir to
3573             # ensure we get back to where we started; hope this fixes it!
3574             $self->_do_in_dir( ".", sub {
3575             ExtUtils::Install::install(
3576 9   50 9   655 $self->install_map, $self->verbose, 0, $self->{args}{uninst}||0
3577             );
3578 9         208 });
3579 9 0 33     636 if ($self->_is_ActivePerl && $self->{_completed_actions}{html}) {
3580 0         0 $self->log_info("Building ActivePerl Table of Contents\n");
3581 0 0       0 eval { ActivePerl::DocTools::WriteTOC(verbose => $self->verbose ? 1 : 0); 1; }
  0 0       0  
  0         0  
3582             or $self->log_warn("AP::DT:: WriteTOC() failed: $@");
3583             }
3584 9 50       139 if ($self->_is_ActivePPM) {
3585             # We touch 'lib/perllocal.pod'. There is an existing logic in subroutine _init_db()
3586             # of 'ActivePerl/PPM/InstallArea.pm' that says that if 'lib/perllocal.pod' has a 'date-last-touched'
3587             # greater than that of the PPM SQLite databases ('etc/ppm-perl-area.db' and/or
3588             # 'site/etc/ppm-site-area.db') then the PPM SQLite databases are rebuilt from scratch.
3589              
3590             # in the following line, 'perllocal.pod' this is *always* 'lib/perllocal.pod', never 'site/lib/perllocal.pod'
3591 0         0 my $F_perllocal = File::Spec->catfile($self->install_sets('core', 'lib'), 'perllocal.pod');
3592 0         0 my $dt_stamp = time;
3593              
3594 0         0 $self->log_info("For ActivePerl's PPM: touch '$F_perllocal'\n");
3595              
3596 0         0 open my $perllocal, ">>", $F_perllocal;
3597 0         0 close $perllocal;
3598 0         0 utime($dt_stamp, $dt_stamp, $F_perllocal);
3599             }
3600             }
3601              
3602             sub ACTION_fakeinstall {
3603 0     0 0 0 my ($self) = @_;
3604 0         0 require ExtUtils::Install;
3605 0         0 my $eui_version = ExtUtils::Install->VERSION;
3606 0 0       0 if ( $eui_version < 1.32 ) {
3607 0         0 $self->log_warn(
3608             "The 'fakeinstall' action requires Extutils::Install 1.32 or later.\n"
3609             . "(You only have version $eui_version)."
3610             );
3611 0         0 return;
3612             }
3613 0         0 $self->depends_on('build');
3614 0   0     0 ExtUtils::Install::install($self->install_map, !$self->quiet, 1, $self->{args}{uninst}||0);
3615             }
3616              
3617             sub ACTION_versioninstall {
3618 0     0 0 0 my ($self) = @_;
3619              
3620             die "You must have only.pm 0.25 or greater installed for this operation: $@\n"
3621 0 0       0 unless eval { require only; 'only'->VERSION(0.25); 1 };
  0         0  
  0         0  
  0         0  
3622              
3623 0         0 $self->depends_on('build');
3624              
3625 0 0       0 my %onlyargs = map {exists($self->{args}{$_}) ? ($_ => $self->{args}{$_}) : ()}
  0         0  
3626             qw(version versionlib);
3627 0         0 only::install::install(%onlyargs);
3628             }
3629              
3630             sub ACTION_installdeps {
3631 2     2 0 7 my ($self) = @_;
3632              
3633             # XXX include feature prerequisites as optional prereqs?
3634              
3635 2         37 my $info = $self->_enum_prereqs;
3636 2 50       10 if (! $info ) {
3637 0         0 $self->log_info( "No prerequisites detected\n" );
3638 0         0 return;
3639             }
3640              
3641 2         23 my $failures = $self->prereq_failures($info);
3642 2 50       8 if ( ! $failures ) {
3643 0         0 $self->log_info( "All prerequisites satisfied\n" );
3644 0         0 return;
3645             }
3646              
3647 2         4 my @install;
3648 2         23 foreach my $type (sort keys %$failures) {
3649 4         9 my $prereqs = $failures->{$type};
3650 4 50       40 if($type =~ m/^(?:\w+_)?requires$/) {
3651 4         15 push(@install, sort keys %$prereqs);
3652 4         11 next;
3653             }
3654 0         0 $self->log_info("Checking optional dependencies:\n");
3655 0         0 foreach my $module (sort keys %$prereqs) {
3656 0 0       0 push(@install, $module) if($self->y_n("Install $module?", 'y'));
3657             }
3658             }
3659              
3660 2 50       9 return unless @install;
3661              
3662 2         23 my ($command, @opts) = $self->split_like_shell($self->cpan_client);
3663              
3664             # relative command should be relative to our active Perl
3665             # so we need to locate that command
3666 2 100       430 if ( ! File::Spec->file_name_is_absolute( $command ) ) {
3667             # prefer site to vendor to core
3668 1         11 my @loc = ( 'site', 'vendor', '' );
3669 1         51 my @bindirs = File::Basename::dirname($self->perl);
3670             push @bindirs,
3671             map {
3672 1         13 ($self->config->{"install${_}bin"}, $self->config->{"install${_}script"})
  3         26  
3673             } @loc;
3674 1         8 for my $d ( @bindirs ) {
3675 7         97 my $abs_cmd = $self->find_command(File::Spec->catfile( $d, $command ));
3676 7 50       38 if ( defined $abs_cmd ) {
3677 0         0 $command = $abs_cmd;
3678 0         0 last;
3679             }
3680             }
3681             }
3682              
3683 2         31 $self->do_system($command, @opts, @install);
3684             }
3685              
3686             sub ACTION_clean {
3687 46     46 0 207 my ($self) = @_;
3688 46         408 $self->log_info("Cleaning up build files\n");
3689 46         801 foreach my $item (map glob($_), $self->cleanup) {
3690 125         717 $self->delete_filetree($item);
3691             }
3692             }
3693              
3694             sub ACTION_realclean {
3695 35     35 0 1908 my ($self) = @_;
3696 35         403 $self->depends_on('clean');
3697 35         203 $self->log_info("Cleaning up configuration files\n");
3698 35         585 $self->delete_filetree(
3699             $self->config_dir, $self->mymetafile, $self->mymetafile2, $self->build_script
3700             );
3701             }
3702              
3703             sub ACTION_ppd {
3704 5     5 0 22 my ($self) = @_;
3705              
3706 5         2651 require Module::Build::PPMMaker;
3707 5         107 my $ppd = Module::Build::PPMMaker->new();
3708 5         18 my $file = $ppd->make_ppd(%{$self->{args}}, build => $self);
  5         79  
3709 5         82 $self->add_to_cleanup($file);
3710             }
3711              
3712             sub ACTION_ppmdist {
3713 3     3 0 14 my ($self) = @_;
3714              
3715 3         51 $self->depends_on( 'build' );
3716              
3717 3         102 my $ppm = $self->ppm_name;
3718 3         24 $self->delete_filetree( $ppm );
3719 3         54 $self->log_info( "Creating $ppm\n" );
3720 3         36 $self->add_to_cleanup( $ppm, "$ppm.tar.gz" );
3721              
3722 3         49 my %types = ( # translate types/dirs to those expected by ppm
3723             lib => 'lib',
3724             arch => 'arch',
3725             bin => 'bin',
3726             script => 'script',
3727             bindoc => 'man1',
3728             libdoc => 'man3',
3729             binhtml => undef,
3730             libhtml => undef,
3731             );
3732              
3733 3         57 foreach my $type ($self->install_types) {
3734 24 100 66     155 next if exists( $types{$type} ) && !defined( $types{$type} );
3735              
3736 18         58 my $dir = File::Spec->catdir( $self->blib, $type );
3737 18 100       296 next unless -e $dir;
3738              
3739 15         59 my $files = $self->rscan_dir( $dir );
3740 15         37 foreach my $file ( @$files ) {
3741 39 100       465 next unless -f $file;
3742 18         1631 my $rel_file =
3743             File::Spec->abs2rel( File::Spec->rel2abs( $file ),
3744             File::Spec->rel2abs( $dir ) );
3745             my $to_file =
3746             File::Spec->catfile( $ppm, 'blib',
3747 18 50       182 exists( $types{$type} ) ? $types{$type} : $type,
3748             $rel_file );
3749 18         87 $self->copy_if_modified( from => $file, to => $to_file );
3750             }
3751             }
3752              
3753 3         29 foreach my $type ( qw(bin lib) ) {
3754 6         97 $self->htmlify_pods( $type, File::Spec->catdir($ppm, 'blib', 'html') );
3755             }
3756              
3757             # create a tarball;
3758             # the directory tar'ed must be blib so we need to do a chdir first
3759 3         63 my $target = File::Spec->catfile( File::Spec->updir, $ppm );
3760 3     3   121 $self->_do_in_dir( $ppm, sub { $self->make_tarball( 'blib', $target ) } );
  3         150  
3761              
3762 3         82 $self->depends_on( 'ppd' );
3763              
3764 3         33 $self->delete_filetree( $ppm );
3765             }
3766              
3767             sub ACTION_pardist {
3768 0     0 0 0 my ($self) = @_;
3769              
3770             # Need PAR::Dist
3771 0 0       0 if ( not eval { require PAR::Dist; PAR::Dist->VERSION(0.17) } ) {
  0         0  
  0         0  
3772 0         0 $self->log_warn(
3773             "In order to create .par distributions, you need to\n"
3774             . "install PAR::Dist first."
3775             );
3776 0         0 return();
3777             }
3778              
3779 0         0 $self->depends_on( 'build' );
3780              
3781 0         0 return PAR::Dist::blib_to_par(
3782             name => $self->dist_name,
3783             version => $self->dist_version,
3784             );
3785             }
3786              
3787             sub ACTION_dist {
3788 1     1 0 10 my ($self) = @_;
3789              
3790             # MUST dispatch() and not depends_ok() so we generate a clean distdir
3791 1         14 $self->dispatch('distdir');
3792              
3793 1         19 my $dist_dir = $self->dist_dir;
3794              
3795 1         29 $self->make_tarball($dist_dir);
3796 1         10699 $self->delete_filetree($dist_dir);
3797             }
3798              
3799             sub ACTION_distcheck {
3800 0     0 0 0 my ($self) = @_;
3801              
3802 0 0       0 $self->_check_manifest_skip unless $self->invoked_action eq 'distclean';
3803              
3804 0         0 require ExtUtils::Manifest;
3805 0         0 local $^W; # ExtUtils::Manifest is not warnings clean.
3806 0         0 my ($missing, $extra) = ExtUtils::Manifest::fullcheck();
3807              
3808 0 0 0     0 return unless @$missing || @$extra;
3809              
3810 0         0 my $msg = "MANIFEST appears to be out of sync with the distribution\n";
3811 0 0       0 if ( $self->invoked_action eq 'distcheck' ) {
3812 0         0 die $msg;
3813             } else {
3814 0         0 warn $msg;
3815             }
3816             }
3817              
3818             sub _check_mymeta_skip {
3819 0     0   0 my $self = shift;
3820 0   0     0 my $maniskip = shift || 'MANIFEST.SKIP';
3821              
3822 0         0 require ExtUtils::Manifest;
3823 0         0 local $^W; # ExtUtils::Manifest is not warnings clean.
3824              
3825             # older ExtUtils::Manifest had a private _maniskip
3826 0   0     0 my $skip_factory = ExtUtils::Manifest->can('maniskip')
3827             || ExtUtils::Manifest->can('_maniskip');
3828              
3829 0         0 my $mymetafile = $self->mymetafile;
3830             # we can't check it, just add it anyway to be safe
3831 0         0 for my $file ( $self->mymetafile, $self->mymetafile2 ) {
3832 0 0 0     0 unless ( $skip_factory && $skip_factory->($maniskip)->($file) ) {
3833 0         0 $self->log_warn("File '$maniskip' does not include '$file'. Adding it now.\n");
3834 0         0 my $safe = quotemeta($file);
3835 0         0 $self->_append_maniskip("^$safe\$", $maniskip);
3836             }
3837             }
3838             }
3839              
3840             sub _add_to_manifest {
3841 30     30   174 my ($self, $manifest, $lines) = @_;
3842 30 50       207 $lines = [$lines] unless ref $lines;
3843              
3844 30         219 my $existing_files = $self->_read_manifest($manifest);
3845 30 100       3832 return unless defined( $existing_files );
3846              
3847 22 50       83 @$lines = grep {!exists $existing_files->{$_}} @$lines
  22         143  
3848             or return;
3849              
3850 22         347 my $mode = (stat $manifest)[2];
3851 22 50       500 chmod($mode | oct(222), $manifest) or die "Can't make $manifest writable: $!";
3852              
3853 22 50       771 open(my $fh, '<', $manifest) or die "Can't read $manifest: $!";
3854 22   50     583 my $last_line = (<$fh>)[-1] || "\n";
3855 22         252 my $has_newline = $last_line =~ /\n$/;
3856 22         227 close $fh;
3857              
3858 22 50       803 open($fh, '>>', $manifest) or die "Can't write to $manifest: $!";
3859 22 50       127 print $fh "\n" unless $has_newline;
3860 22         213 print $fh map "$_\n", @$lines;
3861 22         566 close $fh;
3862 22         308 chmod($mode, $manifest);
3863              
3864 22         250 $self->log_verbose(map "Added to $manifest: $_\n", @$lines);
3865             }
3866              
3867             sub _sign_dir {
3868 5     5   15 my ($self, $dir) = @_;
3869              
3870 5 50       13 unless (eval { require Module::Signature; 1 }) {
  5         29  
  5         15  
3871 0         0 $self->log_warn("Couldn't load Module::Signature for 'distsign' action:\n $@\n");
3872 0         0 return;
3873             }
3874              
3875             # Add SIGNATURE to the MANIFEST
3876             {
3877 5         8 my $manifest = File::Spec->catfile($dir, 'MANIFEST');
  5         54  
3878 5 50       68 die "Signing a distribution requires a MANIFEST file" unless -e $manifest;
3879 5         52 $self->_add_to_manifest($manifest, "SIGNATURE Added here by Module::Build");
3880             }
3881              
3882             # Would be nice if Module::Signature took a directory argument.
3883              
3884 5     5   110 $self->_do_in_dir($dir, sub {local $Module::Signature::Quiet = 1; Module::Signature::sign()});
  5         43  
  5         106  
3885             }
3886              
3887             sub _do_in_dir {
3888 18     18   79 my ($self, $dir, $do) = @_;
3889              
3890 18         115 my $start_dir = File::Spec->rel2abs($self->cwd);
3891 18 50       805 chdir $dir or die "Can't chdir() to $dir: $!";
3892 18         214 eval {$do->()};
  18         409  
3893 18 50       378789 my @err = $@ ? ($@) : ();
3894 18 50       802 chdir $start_dir or push @err, "Can't chdir() back to $start_dir: $!";
3895 18 50       909 die join "\n", @err if @err;
3896             }
3897              
3898             sub ACTION_distsign {
3899 0     0 0 0 my ($self) = @_;
3900             {
3901 0         0 local $self->{properties}{sign} = 0; # We'll sign it ourselves
  0         0  
3902 0 0       0 $self->depends_on('distdir') unless -d $self->dist_dir;
3903             }
3904 0         0 $self->_sign_dir($self->dist_dir);
3905             }
3906              
3907             sub ACTION_skipcheck {
3908 0     0 0 0 my ($self) = @_;
3909              
3910 0         0 require ExtUtils::Manifest;
3911 0         0 local $^W; # ExtUtils::Manifest is not warnings clean.
3912 0         0 ExtUtils::Manifest::skipcheck();
3913             }
3914              
3915             sub ACTION_distclean {
3916 0     0 0 0 my ($self) = @_;
3917              
3918 0         0 $self->depends_on('realclean');
3919 0         0 $self->depends_on('distcheck');
3920             }
3921              
3922             sub do_create_makefile_pl {
3923 1     1 0 1036 my $self = shift;
3924 1         30 require Module::Build::Compat;
3925 1         32 $self->log_info("Creating Makefile.PL\n");
3926 1         11 eval { Module::Build::Compat->create_makefile_pl($self->create_makefile_pl, $self, @_) };
  1         33  
3927 1 50       11 if ( $@ ) {
3928 0         0 1 while unlink 'Makefile.PL';
3929 0         0 die "$@\n";
3930             }
3931 1         27 $self->_add_to_manifest('MANIFEST', 'Makefile.PL');
3932             }
3933              
3934             sub do_create_license {
3935 0     0 0 0 my $self = shift;
3936 0         0 $self->log_info("Creating LICENSE file\n");
3937              
3938 0 0       0 if ( ! $self->_mb_feature('license_creation') ) {
3939 0         0 $self->_warn_mb_feature_deps('license_creation');
3940 0         0 die "Aborting.\n";
3941             }
3942              
3943 0 0       0 my $l = $self->license
3944             or die "Can't create LICENSE file: No license specified\n";
3945              
3946 0 0       0 my $license = $self->_software_license_object
3947             or die << "HERE";
3948             Can't create LICENSE file: '$l' is not a valid license key
3949             or Software::License subclass;
3950             HERE
3951              
3952 0         0 $self->delete_filetree('LICENSE');
3953              
3954 0 0       0 open(my $fh, '>', 'LICENSE')
3955             or die "Can't write LICENSE file: $!";
3956 0         0 print $fh $license->fulltext;
3957 0         0 close $fh;
3958              
3959 0         0 $self->_add_to_manifest('MANIFEST', 'LICENSE');
3960             }
3961              
3962             sub do_create_readme {
3963 6     6 0 393 my $self = shift;
3964 6         235 $self->delete_filetree('README');
3965              
3966 6         123 my $docfile = $self->_main_docfile;
3967 6 50       139 unless ( $docfile ) {
3968 0         0 $self->log_warn(<
3969             Cannot create README: can't determine which file contains documentation;
3970             Must supply either 'dist_version_from', or 'module_name' parameter.
3971             EOF
3972 0         0 return;
3973             }
3974              
3975             # work around some odd Pod::Readme->new() failures in test reports by
3976             # confirming that new() is available
3977 6 50       54 if ( eval {require Pod::Readme; Pod::Readme->can('new') } ) {
  6 50       2776  
  0         0  
3978 0         0 $self->log_info("Creating README using Pod::Readme\n");
3979              
3980 0         0 my $parser = Pod::Readme->new;
3981 0         0 $parser->parse_from_file($docfile, 'README', @_);
3982              
3983 6         6994 } elsif ( eval {require Pod::Text; 1} ) {
  6         160224  
3984 6         75 $self->log_info("Creating README using Pod::Text\n");
3985              
3986 6 50       817 if ( open(my $fh, '>', 'README') ) {
3987 6         129 local $^W = 0;
3988 293     293   2942 no strict "refs";
  293         841  
  293         743085  
3989              
3990             # work around bug in Pod::Text 3.01, which expects
3991             # Pod::Simple::parse_file to take input and output filehandles
3992             # when it actually only takes an input filehandle
3993              
3994 6         15 my $old_parse_file;
3995 0         0 $old_parse_file = \&{"Pod::Simple::parse_file"}
3996             and
3997 0         0 local *{"Pod::Simple::parse_file"} = sub {
3998 0     0   0 my $self = shift;
3999 0 0       0 $self->output_fh($_[1]) if $_[1];
4000 0         0 $self->$old_parse_file($_[0]);
4001             }
4002 6 50 0     34 if $Pod::Text::VERSION
4003             == 3.01; # Split line to avoid evil version-finder
4004              
4005 6         88 Pod::Text::pod2text( $docfile, $fh );
4006              
4007 6         16988 close $fh;
4008             } else {
4009 0         0 $self->log_warn(
4010             "Cannot create 'README' file: Can't open file for writing\n" );
4011 0         0 return;
4012             }
4013              
4014             } else {
4015 0         0 $self->log_warn("Can't load Pod::Readme or Pod::Text to create README\n");
4016 0         0 return;
4017             }
4018              
4019 6         125 $self->_add_to_manifest('MANIFEST', 'README');
4020             }
4021              
4022             sub _main_docfile {
4023 63     63   235 my $self = shift;
4024 63 50       455 if ( my $pm_file = $self->dist_version_from ) {
4025 63         909 (my $pod_file = $pm_file) =~ s/.pm$/.pod/;
4026 63 100       1199 return (-e $pod_file ? $pod_file : $pm_file);
4027             } else {
4028 0         0 return undef;
4029             }
4030             }
4031              
4032             sub do_create_bundle_inc {
4033 0     0 0 0 my $self = shift;
4034 0         0 my $dist_inc = File::Spec->catdir( $self->dist_dir, 'inc' );
4035 0         0 require inc::latest;
4036 0         0 inc::latest->write($dist_inc, @{$self->bundle_inc_preload});
  0         0  
4037 0         0 inc::latest->bundle_module($_, $dist_inc) for @{$self->bundle_inc};
  0         0  
4038 0         0 return 1;
4039             }
4040              
4041             sub ACTION_distdir {
4042 10     10 0 38 my ($self) = @_;
4043              
4044 10 50 33     28 if ( @{$self->bundle_inc} && ! $self->_mb_feature('inc_bundling_support') ) {
  10         121  
4045 0         0 $self->_warn_mb_feature_deps('inc_bundling_support');
4046 0         0 die "Aborting.\n";
4047             }
4048              
4049 10         157 $self->depends_on('distmeta');
4050              
4051 10 50       75 my $dist_files = $self->_read_manifest('MANIFEST')
4052             or die "Can't create distdir without a MANIFEST file - run 'manifest' action first.\n";
4053 10         2418 delete $dist_files->{SIGNATURE}; # Don't copy, create a fresh one
4054 10 50 33     137 die "No files found in MANIFEST - try running 'manifest' action?\n"
4055             unless ($dist_files and keys %$dist_files);
4056 10         108 my $metafile = $self->metafile;
4057             $self->log_warn("*** Did you forget to add $metafile to the MANIFEST?\n")
4058 10 50       42 unless exists $dist_files->{$metafile};
4059              
4060 10         133 my $dist_dir = $self->dist_dir;
4061 10         82 $self->delete_filetree($dist_dir);
4062 10         140 $self->log_info("Creating $dist_dir\n");
4063 10         115 $self->add_to_cleanup($dist_dir);
4064              
4065 10         112 foreach my $file (sort keys %$dist_files) {
4066 69 50       273 next if $file =~ m{^MYMETA\.}; # Double check that we skip MYMETA.*
4067 69         297 my $new = $self->copy_if_modified(from => $file, to_dir => $dist_dir, verbose => 0);
4068             }
4069              
4070 10 50       33 $self->do_create_bundle_inc if @{$self->bundle_inc};
  10         63  
4071              
4072 10 100       101 $self->_sign_dir($dist_dir) if $self->{properties}{sign};
4073             }
4074              
4075             sub ACTION_disttest {
4076 1     1 0 6 my ($self) = @_;
4077              
4078 1         14 $self->depends_on('distdir');
4079              
4080             $self->_do_in_dir
4081             ( $self->dist_dir,
4082             sub {
4083 1     1   23 local $ENV{AUTHOR_TESTING} = 1;
4084 1         25 local $ENV{RELEASE_TESTING} = 1;
4085              
4086             # XXX could be different names for scripts
4087              
4088 1 50       25 $self->run_perl_script('Build.PL') # XXX Should this be run w/ --nouse-rcfile
4089             or die "Error executing 'Build.PL' in dist directory: $!";
4090 1 50       42 $self->run_perl_script($self->build_script)
4091             or die "Error executing $self->build_script in dist directory: $!";
4092 1 50       121 $self->run_perl_script($self->build_script, [], ['test'])
4093             or die "Error executing 'Build test' in dist directory";
4094 1         5 });
4095             }
4096              
4097             sub ACTION_distinstall {
4098 0     0 0 0 my ($self, @args) = @_;
4099              
4100 0         0 $self->depends_on('distdir');
4101              
4102             $self->_do_in_dir ( $self->dist_dir,
4103             sub {
4104 0 0   0   0 $self->run_perl_script('Build.PL')
4105             or die "Error executing 'Build.PL' in dist directory: $!";
4106 0 0       0 $self->run_perl_script($self->build_script)
4107             or die "Error executing $self->build_script in dist directory: $!";
4108 0 0       0 $self->run_perl_script($self->build_script, [], ['install'])
4109             or die "Error executing 'Build install' in dist directory";
4110             }
4111 0         0 );
4112             }
4113              
4114             =begin private
4115              
4116             my $has_include = $build->_eumanifest_has_include;
4117              
4118             Returns true if the installed version of ExtUtils::Manifest supports
4119             #include and #include_default directives. False otherwise.
4120              
4121             =end private
4122              
4123             =cut
4124              
4125             # #!include and #!include_default were added in 1.50
4126             sub _eumanifest_has_include {
4127 2     2   1306 my $self = shift;
4128              
4129 2         841 require ExtUtils::Manifest;
4130 2         5527 return eval { ExtUtils::Manifest->VERSION(1.50); 1 };
  2         27  
  2         21  
4131             }
4132              
4133              
4134             =begin private
4135              
4136             my $maniskip_file = $build->_default_maniskip;
4137              
4138             Returns the location of the installed MANIFEST.SKIP file used by
4139             default.
4140              
4141             =end private
4142              
4143             =cut
4144              
4145             sub _default_maniskip {
4146 0     0   0 my $self = shift;
4147              
4148 0         0 my $default_maniskip;
4149 0         0 for my $dir (@INC) {
4150 0         0 $default_maniskip = File::Spec->catfile($dir, "ExtUtils", "MANIFEST.SKIP");
4151 0 0       0 last if -r $default_maniskip;
4152             }
4153              
4154 0         0 return $default_maniskip;
4155             }
4156              
4157              
4158             =begin private
4159              
4160             my $content = $build->_slurp($file);
4161              
4162             Reads $file and returns the $content.
4163              
4164             =end private
4165              
4166             =cut
4167              
4168             sub _slurp {
4169 0     0   0 my $self = shift;
4170 0         0 my $file = shift;
4171 0   0     0 my $mode = shift || "";
4172 0 0       0 open my $fh, "<$mode", $file or croak "Can't open $file for reading: $!";
4173 0         0 local $/;
4174 0         0 return <$fh>;
4175             }
4176              
4177             sub _spew {
4178 0     0   0 my $self = shift;
4179 0         0 my $file = shift;
4180 0   0     0 my $content = shift || "";
4181 0   0     0 my $mode = shift || "";
4182 0 0       0 open my $fh, ">$mode", $file or croak "Can't open $file for writing: $!";
4183 0         0 print {$fh} $content;
  0         0  
4184 0         0 close $fh;
4185             }
4186              
4187             sub _case_tolerant {
4188 35     35   106 my $self = shift;
4189 35 100       143 if ( ref $self ) {
4190             $self->{_case_tolerant} = File::Spec->case_tolerant
4191 34 100       304 unless defined($self->{_case_tolerant});
4192 34         402 return $self->{_case_tolerant};
4193             }
4194             else {
4195 1         40 return File::Spec->case_tolerant;
4196             }
4197             }
4198              
4199             sub _append_maniskip {
4200 0     0   0 my $self = shift;
4201 0         0 my $skip = shift;
4202 0   0     0 my $file = shift || 'MANIFEST.SKIP';
4203 0 0 0     0 return unless defined $skip && length $skip;
4204 0 0       0 open(my $fh, '>>', $file)
4205             or die "Can't open $file: $!";
4206              
4207 0         0 print $fh "$skip\n";
4208 0         0 close $fh;
4209             }
4210              
4211             sub _write_default_maniskip {
4212 1     1   30 my $self = shift;
4213 1   50     3 my $file = shift || 'MANIFEST.SKIP';
4214 1 50       82 open(my $fh, '>', $file)
4215             or die "Can't open $file: $!";
4216              
4217 1 50       16 my $content = $self->_eumanifest_has_include ? "#!include_default\n"
4218             : $self->_slurp( $self->_default_maniskip );
4219              
4220 1         5 $content .= <<'EOF';
4221             # Avoid configuration metadata file
4222             ^MYMETA\.
4223              
4224             # Avoid Module::Build generated and utility files.
4225             \bBuild$
4226             \bBuild.bat$
4227             \b_build
4228             \bBuild.COM$
4229             \bBUILD.COM$
4230             \bbuild.com$
4231             ^MANIFEST\.SKIP
4232              
4233             # Avoid archives of this distribution
4234             EOF
4235              
4236             # Skip, for example, 'Module-Build-0.27.tar.gz'
4237 1         4 $content .= '\b'.$self->dist_name.'-[\d\.\_]+'."\n";
4238              
4239 1         9 print $fh $content;
4240            
4241 1         45 close $fh;
4242              
4243 1         6 return;
4244             }
4245              
4246             sub _check_manifest_skip {
4247 0     0   0 my ($self) = @_;
4248              
4249 0         0 my $maniskip = 'MANIFEST.SKIP';
4250              
4251 0 0       0 if ( ! -e $maniskip ) {
4252 0         0 $self->log_warn("File '$maniskip' does not exist: Creating a temporary '$maniskip'\n");
4253 0         0 $self->_write_default_maniskip($maniskip);
4254 0         0 $self->_unlink_on_exit($maniskip);
4255             }
4256             else {
4257             # MYMETA must not be added to MANIFEST, so always confirm the skip
4258 0         0 $self->_check_mymeta_skip( $maniskip );
4259             }
4260              
4261 0         0 return;
4262             }
4263              
4264             sub ACTION_manifest {
4265 0     0 0 0 my ($self) = @_;
4266              
4267 0         0 $self->_check_manifest_skip;
4268              
4269 0         0 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
4270 0         0 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
4271 0         0 ExtUtils::Manifest::mkmanifest();
4272             }
4273              
4274             sub ACTION_manifest_skip {
4275 0     0 0 0 my ($self) = @_;
4276              
4277 0 0       0 if ( -e 'MANIFEST.SKIP' ) {
4278 0         0 $self->log_warn("MANIFEST.SKIP already exists.\n");
4279 0         0 return 0;
4280             }
4281 0         0 $self->log_info("Creating a new MANIFEST.SKIP file\n");
4282 0         0 return $self->_write_default_maniskip;
4283 0         0 return -e 'MANIFEST.SKIP'
4284             }
4285              
4286             # Case insensitive regex for files
4287             sub file_qr {
4288 396 50   396 0 12274 return shift->{_case_tolerant} ? qr($_[0])i : qr($_[0]);
4289             }
4290              
4291             sub dist_dir {
4292 29     29 0 103677 my ($self) = @_;
4293 29         447 my $dir = join "-", $self->dist_name, $self->dist_version;
4294 29 100       227 $dir .= "-" . $self->dist_suffix if $self->dist_suffix;
4295 29         220 return $dir;
4296             }
4297              
4298             sub ppm_name {
4299 8     8 0 2930 my $self = shift;
4300 8         71 return 'PPM-' . $self->dist_dir;
4301             }
4302              
4303             sub _files_in {
4304 93     93   764 my ($self, $dir) = @_;
4305 93 100       1573 return unless -d $dir;
4306              
4307 2         18 local *DH;
4308 2 50       48 opendir DH, $dir or die "Can't read directory $dir: $!";
4309              
4310 2         6 my @files;
4311 2         50 while (defined (my $file = readdir DH)) {
4312 8         74 my $full_path = File::Spec->catfile($dir, $file);
4313 8 100       110 next if -d $full_path;
4314 4         44 push @files, $full_path;
4315             }
4316 2         34 return @files;
4317             }
4318              
4319             sub share_dir {
4320 178     178 0 29908 my $self = shift;
4321 178         887 my $p = $self->{properties};
4322              
4323 178 50       832 $p->{share_dir} = shift if @_;
4324              
4325             # Always coerce to proper hash form
4326 178 100       1473 if ( ! defined $p->{share_dir} ) {
    50          
    50          
    50          
4327 154         832 return;
4328             }
4329             elsif ( ! ref $p->{share_dir} ) {
4330             # scalar -- treat as a single 'dist' directory
4331 0         0 $p->{share_dir} = { dist => [ $p->{share_dir} ] };
4332             }
4333             elsif ( ref $p->{share_dir} eq 'ARRAY' ) {
4334             # array -- treat as a list of 'dist' directories
4335 0         0 $p->{share_dir} = { dist => $p->{share_dir} };
4336             }
4337             elsif ( ref $p->{share_dir} eq 'HASH' ) {
4338             # hash -- check structure
4339 24         217 my $share_dir = $p->{share_dir};
4340             # check dist key
4341 24 50       217 if ( defined $share_dir->{dist} ) {
4342 24 50       345 if ( ! ref $share_dir->{dist} ) {
    50          
4343             # scalar, so upgrade to arrayref
4344 0         0 $share_dir->{dist} = [ $share_dir->{dist} ];
4345             }
4346             elsif ( ref $share_dir->{dist} ne 'ARRAY' ) {
4347 0         0 die "'dist' key in 'share_dir' must be scalar or arrayref";
4348             }
4349             }
4350             # check module key
4351 24 100       228 if ( defined $share_dir->{module} ) {
4352 6         44 my $mod_hash = $share_dir->{module};
4353 6 50       36 if ( ref $mod_hash eq 'HASH' ) {
4354 6         63 for my $k ( sort keys %$mod_hash ) {
4355 6 50       69 if ( ! ref $mod_hash->{$k} ) {
    50          
4356 0         0 $mod_hash->{$k} = [ $mod_hash->{$k} ];
4357             }
4358             elsif( ref $mod_hash->{$k} ne 'ARRAY' ) {
4359 0         0 die "modules in 'module' key of 'share_dir' must be scalar or arrayref";
4360             }
4361             }
4362             }
4363             else {
4364 0         0 die "'module' key in 'share_dir' must be hashref";
4365             }
4366             }
4367             }
4368             else {
4369 0         0 die "'share_dir' must be hashref, arrayref or string";
4370             }
4371              
4372 24         981 return $p->{share_dir};
4373             }
4374              
4375             sub script_files {
4376 106     106 0 1237 my $self = shift;
4377              
4378 106         492 for ($self->{properties}{script_files}) {
4379 106 50       591 $_ = shift if @_;
4380 106 100       454 next unless $_;
4381              
4382             # Always coerce into a hash
4383 13 50       135 return $_ if ref $_ eq 'HASH';
4384 13 50       151 return $_ = { map {$_,1} @$_ } if ref $_ eq 'ARRAY';
  16         172  
4385              
4386 0 0       0 die "'script_files' must be a hashref, arrayref, or string" if ref();
4387              
4388 0 0       0 return $_ = { map {$_,1} $self->_files_in( $_ ) } if -d $_;
  0         0  
4389 0         0 return $_ = {$_ => 1};
4390             }
4391              
4392             my %pl_files = map {
4393 21         251 File::Spec->canonpath( $_ ) => 1
4394 93 100       227 } keys %{ $self->PL_files || {} };
  93         984  
4395              
4396 93         953 my @bin_files = $self->_files_in('bin');
4397              
4398             my %bin_map = map {
4399 93         384 $_ => File::Spec->canonpath( $_ )
  4         34  
4400             } @bin_files;
4401              
4402 93         1025 return $_ = { map {$_ => 1} grep !$pl_files{$bin_map{$_}}, @bin_files };
  2         20  
4403             }
4404 293     293   2554562 BEGIN { *scripts = \&script_files; }
4405              
4406             {
4407             my %licenses = (
4408             perl => 'Perl_5',
4409             apache => 'Apache_2_0',
4410             apache_1_1 => 'Apache_1_1',
4411             artistic => 'Artistic_1',
4412             artistic_2 => 'Artistic_2',
4413             lgpl => 'LGPL_2_1',
4414             lgpl2 => 'LGPL_2_1',
4415             lgpl3 => 'LGPL_3_0',
4416             bsd => 'BSD',
4417             gpl => 'GPL_1',
4418             gpl2 => 'GPL_2',
4419             gpl3 => 'GPL_3',
4420             mit => 'MIT',
4421             mozilla => 'Mozilla_1_1',
4422             restrictive => 'Restricted',
4423             open_source => undef,
4424             unrestricted => undef,
4425             unknown => undef,
4426             );
4427              
4428             # TODO - would be nice to not have these here, since they're more
4429             # properly stored only in Software::License
4430             my %license_urls = (
4431             perl => 'http://dev.perl.org/licenses/',
4432             apache => 'http://apache.org/licenses/LICENSE-2.0',
4433             apache_1_1 => 'http://apache.org/licenses/LICENSE-1.1',
4434             artistic => 'http://opensource.org/licenses/artistic-license.php',
4435             artistic_2 => 'http://opensource.org/licenses/artistic-license-2.0.php',
4436             lgpl => 'http://opensource.org/licenses/lgpl-license.php',
4437             lgpl2 => 'http://opensource.org/licenses/lgpl-2.1.php',
4438             lgpl3 => 'http://opensource.org/licenses/lgpl-3.0.html',
4439             bsd => 'http://opensource.org/licenses/bsd-license.php',
4440             gpl => 'http://opensource.org/licenses/gpl-license.php',
4441             gpl2 => 'http://opensource.org/licenses/gpl-2.0.php',
4442             gpl3 => 'http://opensource.org/licenses/gpl-3.0.html',
4443             mit => 'http://opensource.org/licenses/mit-license.php',
4444             mozilla => 'http://opensource.org/licenses/mozilla1.1.php',
4445             restrictive => undef,
4446             open_source => undef,
4447             unrestricted => undef,
4448             unknown => undef,
4449             );
4450             sub valid_licenses {
4451 69     69 0 581 return \%licenses;
4452             }
4453             sub _license_url {
4454 22     22   225 return $license_urls{$_[1]};
4455             }
4456             }
4457              
4458             sub _software_license_class {
4459 23     23   353 my ($self, $license) = @_;
4460 23 50 66     138 if ($self->valid_licenses->{$license} && eval { require Software::LicenseUtils; Software::LicenseUtils->VERSION(0.103009) }) {
  20         5910  
  0         0  
4461 0         0 my @classes = Software::LicenseUtils->guess_license_from_meta_key($license, 1);
4462 0 0       0 if (@classes == 1) {
4463 0         0 eval "require $classes[0]";
4464 0         0 return $classes[0];
4465             }
4466             }
4467 23         330 LICENSE: for my $l ( $self->valid_licenses->{ $license }, $license ) {
4468 46 100       216 next unless defined $l;
4469 43         152 my $trial = "Software::License::" . $l;
4470 43 100       4070 if ( eval "require Software::License; Software::License->VERSION(0.014); require $trial; 1" ) {
4471 1         22 return $trial;
4472             }
4473             }
4474 22         460 return;
4475             }
4476              
4477             # use mapping or license name directly
4478             sub _software_license_object {
4479 23     23   97 my ($self) = @_;
4480 23 50       102 return unless defined( my $license = $self->license );
4481              
4482 23 100       335 my $class = $self->_software_license_class($license) or return;
4483              
4484             # Software::License requires a 'holder' argument
4485 1   50     6 my $author = join( " & ", @{ $self->dist_author }) || 'unknown';
4486 1         6 my $sl = eval { $class->new({holder=>$author}) };
  1         19  
4487 1 50       18 if ( $@ ) {
4488 0         0 $self->log_warn( "Error getting '$class' object: $@" );
4489             }
4490              
4491 1         9 return $sl;
4492             }
4493              
4494             sub _hash_merge {
4495 0     0   0 my ($self, $h, $k, $v) = @_;
4496 0 0       0 if (ref $h->{$k} eq 'ARRAY') {
    0          
4497 0 0       0 push @{$h->{$k}}, ref $v ? @$v : $v;
  0         0  
4498             } elsif (ref $h->{$k} eq 'HASH') {
4499 0         0 $h->{$k}{$_} = $v->{$_} foreach keys %$v;
4500             } else {
4501 0         0 $h->{$k} = $v;
4502             }
4503             }
4504              
4505             sub ACTION_distmeta {
4506 13     13 0 141 my ($self) = @_;
4507 13 50       318 $self->do_create_makefile_pl if $self->create_makefile_pl;
4508 13 50       150 $self->do_create_readme if $self->create_readme;
4509 13 50       136 $self->do_create_license if $self->create_license;
4510 13         165 $self->do_create_metafile;
4511             }
4512              
4513             sub do_create_metafile {
4514 13     13 0 68 my $self = shift;
4515 13 100       91 return if $self->{wrote_metadata};
4516              
4517 9         124 my $p = $self->{properties};
4518              
4519 9 50       84 unless ($p->{license}) {
4520 0         0 $self->log_warn("No license specified, setting license = 'unknown'\n");
4521 0         0 $p->{license} = 'unknown';
4522             }
4523              
4524 9         145 my @metafiles = ( $self->metafile, $self->metafile2 );
4525             # If we're in the distdir, the metafile may exist and be non-writable.
4526 9         302 $self->delete_filetree($_) for @metafiles;
4527              
4528             # Since we're building ourself, we have to do some special stuff
4529             # here: the ConfigData module is found in blib/lib.
4530 9         377 local @INC = @INC;
4531 9 50 50     158 if (($self->module_name || '') eq 'Module::Build') {
4532 0         0 $self->depends_on('config_data');
4533 0         0 push @INC, File::Spec->catdir($self->blib, 'lib');
4534             }
4535              
4536 9         203 my $meta_obj = $self->_get_meta_object(
4537             quiet => 1, fatal => 1, auto => 1
4538             );
4539 9         115 my @created = $self->_write_meta_files( $meta_obj, 'META' );
4540 9 50       38 if ( @created ) {
4541 9         66 $self->{wrote_metadata} = 1;
4542 9         227 $self->_add_to_manifest('MANIFEST', $_) for @created;
4543             }
4544 9         236 return 1;
4545             }
4546              
4547             sub _write_meta_files {
4548 15     15   54 my $self = shift;
4549 15         93 my ($meta, $file) = @_;
4550 15         131 $file =~ s{\.(?:yml|json)$}{};
4551              
4552 15         39 my @created;
4553 15 50 33     341 push @created, "$file\.yml"
4554             if $meta && $meta->save( "$file\.yml", {version => "1.4"} );
4555 15 50 33     131254 push @created, "$file\.json"
4556             if $meta && $meta->save( "$file\.json" );
4557              
4558 15 50       237068 if ( @created ) {
4559 15         310 $self->log_info("Created " . join(" and ", @created) . "\n");
4560             }
4561 15         106 return @created;
4562             }
4563              
4564             sub _get_meta_object {
4565 15     15   74 my $self = shift;
4566 15         231 my %args = @_;
4567 15 50       242 return unless $self->try_require("CPAN::Meta", "2.142060");
4568              
4569 15         90916 my $meta;
4570 15         54 eval {
4571             my $data = $self->get_metadata(
4572             fatal => $args{fatal},
4573             auto => $args{auto},
4574 15         379 );
4575 15 100       85 $data->{dynamic_config} = $args{dynamic} if defined $args{dynamic};
4576 15         187 $meta = CPAN::Meta->create($data);
4577             };
4578 15 50 33     10686 if ($@ && ! $args{quiet}) {
4579 0         0 $self->log_warn(
4580             "Could not get valid metadata. Error is: $@\n"
4581             );
4582             }
4583              
4584 15         59 return $meta;
4585             }
4586              
4587             sub read_metafile {
4588 0     0 0 0 my $self = shift;
4589 0         0 my ($metafile) = @_;
4590              
4591 0 0       0 return unless $self->try_require("CPAN::Meta", "2.110420");
4592 0         0 my $meta = CPAN::Meta->load_file($metafile);
4593 0         0 return $meta->as_struct( {version => "2.0"} );
4594             }
4595              
4596             sub normalize_version {
4597 125     125 0 2219 my ($self, $version) = @_;
4598 125 100 100     2548 $version = 0 unless defined $version and length $version;
4599              
4600 125 50       1814 if ( $version =~ /[=<>!,]/ ) { # logic, not just version
    100          
    50          
4601             # take as is without modification
4602             }
4603             elsif ( ref $version eq 'version') { # version objects
4604 81 50       743 $version = $version->is_qv ? $version->normal : $version->stringify;
4605             }
4606             elsif ( $version =~ /^[^v][^.]*\.[^.]+\./ ) { # no leading v, multiple dots
4607             # normalize string tuples without "v": "1.2.3" -> "v1.2.3"
4608 0         0 $version = "v$version";
4609             }
4610             else {
4611             # leave alone
4612             }
4613 125         777 return $version;
4614             }
4615              
4616             my %prereq_map = (
4617             requires => [ qw/runtime requires/],
4618             configure_requires => [qw/configure requires/],
4619             build_requires => [ qw/build requires/ ],
4620             test_requires => [ qw/test requires/ ],
4621             test_recommends => [ qw/test recommends/ ],
4622             recommends => [ qw/runtime recommends/ ],
4623             conflicts => [ qw/runtime conflicts/ ],
4624             );
4625              
4626             sub _normalize_prereqs {
4627 26     26   7649 my ($self) = @_;
4628 26         76 my $p = $self->{properties};
4629              
4630             # copy prereq data structures so we can modify them before writing to META
4631 26         59 my %prereq_types;
4632 26         77 for my $type ( 'configure_requires', @{$self->prereq_action_types} ) {
  26         235  
4633 156 100 66     535 if (exists $p->{$type} and keys %{ $p->{$type} }) {
  156         631  
4634 21         54 my ($phase, $relation) = @{ $prereq_map{$type} };
  21         158  
4635 21         63 for my $mod ( keys %{ $p->{$type} } ) {
  21         114  
4636 21         132 $prereq_types{$phase}{$relation}{$mod} = $self->normalize_version($p->{$type}{$mod});
4637             }
4638             }
4639             }
4640 26         231 return \%prereq_types;
4641             }
4642              
4643             sub _get_license {
4644 23     23   163 my $self = shift;
4645              
4646 23         105 my $license = $self->license;
4647 23         91 my ($meta_license, $meta_license_url);
4648              
4649 23         362 my $valid_licenses = $self->valid_licenses();
4650 23 100       369 if ( my $sl = $self->_software_license_object ) {
    50          
4651 1         9 $meta_license = $sl->meta2_name;
4652 1         12 $meta_license_url = $sl->url;
4653             }
4654             elsif ( exists $valid_licenses->{$license} ) {
4655 22 100       143 $meta_license = $valid_licenses->{$license} ? lc $valid_licenses->{$license} : $license;
4656 22         237 $meta_license_url = $self->_license_url( $license );
4657             }
4658             else {
4659 0         0 $self->log_warn( "Can not determine license type for '" . $self->license
4660             . "'\nSetting META license field to 'unknown'.\n");
4661 0         0 $meta_license = 'unknown';
4662             }
4663 23         150 return ($meta_license, $meta_license_url);
4664             }
4665              
4666             sub get_metadata {
4667 23     23 0 1082 my ($self, %args) = @_;
4668              
4669 23   100     251 my $fatal = $args{fatal} || 0;
4670 23         106 my $p = $self->{properties};
4671              
4672 23 100       331 $self->auto_config_requires if $args{auto};
4673              
4674             # validate required fields
4675 23         149 foreach my $f (qw(dist_name dist_version dist_author dist_abstract license)) {
4676 115         1295 my $field = $self->$f();
4677 115 50 33     894 unless ( defined $field and length $field ) {
4678 0         0 my $err = "ERROR: Missing required field '$f' for metafile\n";
4679 0 0       0 if ( $fatal ) {
4680 0         0 die $err;
4681             }
4682             else {
4683 0         0 $self->log_warn($err);
4684             }
4685             }
4686             }
4687              
4688             my %metadata = (
4689             name => $self->dist_name,
4690             version => $self->normalize_version($self->dist_version),
4691             author => $self->dist_author,
4692             abstract => $self->dist_abstract,
4693             generated_by => "Module::Build version $Module::Build::VERSION",
4694             'meta-spec' => {
4695             version => '2',
4696             url => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
4697             },
4698 23 50       180 dynamic_config => exists $p->{dynamic_config} ? $p->{dynamic_config} : 1,
4699             release_status => $self->release_status,
4700             );
4701              
4702 23         407 my ($meta_license, $meta_license_url) = $self->_get_license;
4703 23         102 $metadata{license} = [ $meta_license ];
4704 23 100       158 $metadata{resources}{license} = [ $meta_license_url ] if defined $meta_license_url;
4705              
4706 23         233 $metadata{prereqs} = $self->_normalize_prereqs;
4707              
4708 23 50       119 if (exists $p->{no_index}) {
    100          
4709 0         0 $metadata{no_index} = $p->{no_index};
4710 23         285 } elsif (my $pkgs = eval { $self->find_dist_packages }) {
4711 19 100       98 $metadata{provides} = $pkgs if %$pkgs;
4712             } else {
4713 4         48 $self->log_warn("$@\nWARNING: Possible missing or corrupt 'MANIFEST' file.\n" .
4714             "Nothing to enter for 'provides' field in metafile.\n");
4715             }
4716              
4717 23 50       337 if (my $add = $self->meta_add) {
4718 23 50 33     126 if (not exists $add->{'meta-spec'} or $add->{'meta-spec'}{version} != 2) {
4719 23         152 require CPAN::Meta::Converter;
4720 23 50       552 if (CPAN::Meta::Converter->VERSION('2.141170')) {
4721 23         339 $add = CPAN::Meta::Converter->new($add)->upgrade_fragment;
4722 23         39874 delete $add->{prereqs}; # XXX this would now overwrite all prereqs
4723             }
4724             else {
4725 0         0 $self->log_warn("Can't meta_add without CPAN::Meta 2.141170");
4726             }
4727             }
4728              
4729 23         137 while (my($k, $v) = each %{$add}) {
  53         257  
4730 30         99 $metadata{$k} = $v;
4731             }
4732             }
4733              
4734 23 50       190 if (my $merge = $self->meta_merge) {
4735 23 50       53 if (eval { require CPAN::Meta::Merge }) {
  23         11112  
4736 23         39133 %metadata = %{ CPAN::Meta::Merge->new(default_version => '1.4')->merge(\%metadata, $merge) };
  23         178  
4737             }
4738             else {
4739 0         0 $self->log_warn("Can't merge without CPAN::Meta::Merge");
4740             }
4741             }
4742              
4743 23         49285 return \%metadata;
4744             }
4745              
4746             # To preserve compatibility with old API, $node *must* be a hashref
4747             # passed in to prepare_metadata. $keys is an arrayref holding a
4748             # list of keys -- it's use is optional and generally no longer needed
4749             # but kept for back compatibility. $args is an optional parameter to
4750             # support the new 'fatal' toggle
4751              
4752             sub prepare_metadata {
4753 0     0 0 0 my ($self, $node, $keys, $args) = @_;
4754 0 0       0 unless ( ref $node eq 'HASH' ) {
4755 0         0 croak "prepare_metadata() requires a hashref argument to hold output\n";
4756             }
4757 0 0       0 croak 'Keys argument to prepare_metadata is no longer supported' if $keys;
4758 0         0 %{$node} = %{ $self->get_meta(%{$args}) };
  0         0  
  0         0  
  0         0  
4759 0         0 return $node;
4760             }
4761              
4762             sub _read_manifest {
4763 63     63   316 my ($self, $file) = @_;
4764 63 100       993 return undef unless -e $file;
4765              
4766 51         10099 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
4767 51         86700 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
4768 51         418 return scalar ExtUtils::Manifest::maniread($file);
4769             }
4770              
4771             sub find_dist_packages {
4772 23     23 0 78 my $self = shift;
4773              
4774             # Only packages in .pm files are candidates for inclusion here.
4775             # Only include things in the MANIFEST, not things in developer's
4776             # private stock.
4777              
4778 23 100       285 my $manifest = $self->_read_manifest('MANIFEST')
4779             or die "Can't find dist packages without a MANIFEST file\nRun 'Build manifest' to generate one\n";
4780              
4781             # Localize
4782 19         3614 my %dist_files = map { $self->localize_file_path($_) => $_ }
  88         555  
4783             keys %$manifest;
4784              
4785 18         116 my @pm_files = sort grep { $_ !~ m{^t} } # skip things in t/
4786 18         78 grep {exists $dist_files{$_}}
4787 19         84 keys %{ $self->find_pm_files };
  19         294  
4788              
4789 19         358 return $self->find_packages_in_files(\@pm_files, \%dist_files);
4790             }
4791              
4792             # XXX Do not document this function; mst wrote it and now says the API is
4793             # stupid and needs to be fixed and it shouldn't become a public API until then
4794             sub find_packages_in_files {
4795 19     19 0 89 my ($self, $file_list, $filename_map) = @_;
4796              
4797             # First, we enumerate all packages & versions,
4798             # separating into primary & alternative candidates
4799 19         49 my( %prime, %alt );
4800 19         40 foreach my $file (@{$file_list}) {
  19         127  
4801 18         69 my $mapped_filename = $filename_map->{$file};
4802 18         91 my @path = split( /\//, $mapped_filename );
4803 18         228 (my $prime_package = join( '::', @path[1..$#path] )) =~ s/\.pm$//;
4804              
4805 18         299 my $pm_info = Module::Metadata->new_from_file( $file );
4806              
4807 18         21163 foreach my $package ( $pm_info->packages_inside ) {
4808 18 50       179 next if $package eq 'main'; # main can appear numerous times, ignore
4809 18 50       96 next if $package eq 'DB'; # special debugging package, ignore
4810 18 50       147 next if grep /^_/, split( /::/, $package ); # private package, ignore
4811              
4812 18         96 my $version = $pm_info->version( $package );
4813              
4814 18 50       267 if ( $package eq $prime_package ) {
4815 18 50       62 if ( exists( $prime{$package} ) ) {
4816             # Module::Metadata will handle this conflict
4817 0         0 die "Unexpected conflict in '$package'; multiple versions found.\n";
4818             } else {
4819 18         88 $prime{$package}{file} = $mapped_filename;
4820 18 50       191 $prime{$package}{version} = $version if defined( $version );
4821             }
4822             } else {
4823 0         0 push( @{$alt{$package}}, {
  0         0  
4824             file => $mapped_filename,
4825             version => $version,
4826             } );
4827             }
4828             }
4829             }
4830              
4831             # Then we iterate over all the packages found above, identifying conflicts
4832             # and selecting the "best" candidate for recording the file & version
4833             # for each package.
4834 19         108 foreach my $package ( sort keys( %alt ) ) {
4835 0         0 my $result = $self->_resolve_module_versions( $alt{$package} );
4836              
4837 0 0       0 if ( exists( $prime{$package} ) ) { # primary package selected
4838              
4839 0 0       0 if ( $result->{err} ) {
    0          
4840             # Use the selected primary package, but there are conflicting
4841             # errors among multiple alternative packages that need to be
4842             # reported
4843             $self->log_warn(
4844             "Found conflicting versions for package '$package'\n" .
4845             " $prime{$package}{file} ($prime{$package}{version})\n" .
4846             $result->{err}
4847 0         0 );
4848              
4849             } elsif ( defined( $result->{version} ) ) {
4850             # There is a primary package selected, and exactly one
4851             # alternative package
4852              
4853 0 0 0     0 if ( exists( $prime{$package}{version} ) &&
4854             defined( $prime{$package}{version} ) ) {
4855             # Unless the version of the primary package agrees with the
4856             # version of the alternative package, report a conflict
4857 0 0       0 if ( $self->compare_versions( $prime{$package}{version}, '!=',
4858             $result->{version} ) ) {
4859 0         0 $self->log_warn(
4860             "Found conflicting versions for package '$package'\n" .
4861             " $prime{$package}{file} ($prime{$package}{version})\n" .
4862             " $result->{file} ($result->{version})\n"
4863             );
4864             }
4865              
4866             } else {
4867             # The prime package selected has no version so, we choose to
4868             # use any alternative package that does have a version
4869 0         0 $prime{$package}{file} = $result->{file};
4870 0         0 $prime{$package}{version} = $result->{version};
4871             }
4872              
4873             } else {
4874             # no alt package found with a version, but we have a prime
4875             # package so we use it whether it has a version or not
4876             }
4877              
4878             } else { # No primary package was selected, use the best alternative
4879              
4880 0 0       0 if ( $result->{err} ) {
4881             $self->log_warn(
4882             "Found conflicting versions for package '$package'\n" .
4883             $result->{err}
4884 0         0 );
4885             }
4886              
4887             # Despite possible conflicting versions, we choose to record
4888             # something rather than nothing
4889 0         0 $prime{$package}{file} = $result->{file};
4890             $prime{$package}{version} = $result->{version}
4891 0 0       0 if defined( $result->{version} );
4892             }
4893             }
4894              
4895             # Normalize versions or delete them if undef/0
4896 19         148 for my $provides ( values %prime ) {
4897 18 50       366 if ( $provides->{version} ) {
4898             $provides->{version} = $self->normalize_version( $provides->{version} )
4899 18         117 }
4900             else {
4901 0         0 delete $provides->{version};
4902             }
4903             }
4904              
4905 19         183 return \%prime;
4906             }
4907              
4908             # separate out some of the conflict resolution logic from
4909             # $self->find_dist_packages(), above, into a helper function.
4910             #
4911             sub _resolve_module_versions {
4912 0     0   0 my $self = shift;
4913              
4914 0         0 my $packages = shift;
4915              
4916 0         0 my( $file, $version );
4917 0         0 my $err = '';
4918 0         0 foreach my $p ( @$packages ) {
4919 0 0       0 if ( defined( $p->{version} ) ) {
4920 0 0       0 if ( defined( $version ) ) {
4921 0 0       0 if ( $self->compare_versions( $version, '!=', $p->{version} ) ) {
4922 0         0 $err .= " $p->{file} ($p->{version})\n";
4923             } else {
4924             # same version declared multiple times, ignore
4925             }
4926             } else {
4927 0         0 $file = $p->{file};
4928 0         0 $version = $p->{version};
4929             }
4930             }
4931 0 0 0     0 $file ||= $p->{file} if defined( $p->{file} );
4932             }
4933              
4934 0 0       0 if ( $err ) {
4935 0         0 $err = " $file ($version)\n" . $err;
4936             }
4937              
4938 0         0 my %result = (
4939             file => $file,
4940             version => $version,
4941             err => $err
4942             );
4943              
4944 0         0 return \%result;
4945             }
4946              
4947             sub make_tarball {
4948 4     4 0 56 my ($self, $dir, $file) = @_;
4949 4   66     74 $file ||= $dir;
4950              
4951 4         96 $self->log_info("Creating $file.tar.gz\n");
4952              
4953 4 50       51 if ($self->{args}{tar}) {
4954 0 0       0 my $tar_flags = $self->verbose ? 'cvf' : 'cf';
4955              
4956             # See ExtUtils::MM_Darwin
4957             # 10.4 wants COPY_EXTENDED_ATTRIBUTES_DISABLE.
4958             # 10.5 wants COPYFILE_DISABLE.
4959             # So just set both.
4960 0 0       0 local $ENV{COPY_EXTENDED_ATTRIBUTES_DISABLE} = 1 if $^O eq 'darwin';
4961 0 0       0 local $ENV{COPYFILE_DISABLE} = 1 if $^O eq 'darwin';
4962              
4963 0         0 $self->do_system($self->split_like_shell($self->{args}{tar}), $tar_flags, "$file.tar", $dir);
4964 0 0       0 $self->do_system($self->split_like_shell($self->{args}{gzip}), "$file.tar") if $self->{args}{gzip};
4965             } else {
4966 4 50       27 eval { require Archive::Tar && Archive::Tar->VERSION(1.09); 1 }
  4 50       414  
  4         70  
4967             or die "You must install Archive::Tar 1.09+ to make a distribution tarball\n".
4968             "or specify a binary tar program with the '--tar' option.\n".
4969             "See the documentation for the 'dist' action.\n";
4970              
4971 4         57 my $files = $self->rscan_dir($dir);
4972              
4973             # Archive::Tar versions >= 1.09 use the following to enable a compatibility
4974             # hack so that the resulting archive is compatible with older clients.
4975             # If no file path is 100 chars or longer, we disable the prefix field
4976             # for maximum compatibility. If there are any long file paths then we
4977             # need the prefix field after all.
4978             $Archive::Tar::DO_NOT_USE_PREFIX =
4979 4 50       45 (grep { length($_) >= 100 } @$files) ? 0 : 1;
  73         151  
4980              
4981 4         110 my $tar = Archive::Tar->new;
4982 4         173 $tar->add_files(@$files);
4983 4         26716 for my $f ($tar->get_files) {
4984 73         1130 $f->mode($f->mode & ~022); # chmod go-w
4985             }
4986 4         119 $tar->write("$file.tar.gz", 1);
4987             }
4988             }
4989              
4990             sub install_path {
4991 334     334 0 5428 my $self = shift;
4992 334         1068 my( $type, $value ) = ( @_, '' );
4993              
4994 334 50       1097 Carp::croak( 'Type argument missing' )
4995             unless defined( $type );
4996              
4997 334         780 my $map = $self->{properties}{install_path};
4998 334 100       1013 return $map unless @_;
4999              
5000             # delete existing value if $value is literal undef()
5001 319 100       823 unless ( defined( $value ) ) {
5002 1         7 delete( $map->{$type} );
5003 1         4 return undef;
5004             }
5005              
5006             # return existing value if no new $value is given
5007 318 100       1095 if ( $value eq '' ) {
5008 317 100       1382 return undef unless exists $map->{$type};
5009 51         786 return $map->{$type};
5010             }
5011              
5012             # set value if $value is a valid relative path
5013 1         16 return $map->{$type} = $value;
5014             }
5015              
5016             sub install_sets {
5017             # Usage: install_sets('site'), install_sets('site', 'lib'),
5018             # or install_sets('site', 'lib' => $value);
5019 217     217 0 1379 my ($self, $dirs, $key, $value) = @_;
5020 217 100       538 $dirs = $self->installdirs unless defined $dirs;
5021             # update property before merging with defaults
5022 217 0 33     821 if ( @_ == 4 && defined $dirs && defined $key) {
      33        
5023             # $value can be undef; will mask default
5024 0         0 $self->{properties}{install_sets}{$dirs}{$key} = $value;
5025             }
5026             my $map = { $self->_merge_arglist(
5027             $self->{properties}{install_sets},
5028             $self->_default_install_paths->{install_sets}
5029 217         924 )};
5030 217 50 33     3012 if ( defined $dirs && defined $key ) {
    50          
5031 0         0 return $map->{$dirs}{$key};
5032             }
5033             elsif ( defined $dirs ) {
5034 217         2651 return $map->{$dirs};
5035             }
5036             else {
5037 0         0 croak "Can't determine installdirs for install_sets()";
5038             }
5039             }
5040              
5041             sub original_prefix {
5042             # Usage: original_prefix(), original_prefix('lib'),
5043             # or original_prefix('lib' => $value);
5044 34     34 0 96 my ($self, $key, $value) = @_;
5045             # update property before merging with defaults
5046 34 50 33     102 if ( @_ == 3 && defined $key) {
5047             # $value can be undef; will mask default
5048 0         0 $self->{properties}{original_prefix}{$key} = $value;
5049             }
5050             my $map = { $self->_merge_arglist(
5051             $self->{properties}{original_prefix},
5052             $self->_default_install_paths->{original_prefix}
5053 34         101 )};
5054 34 50       382 return $map unless defined $key;
5055 34         131 return $map->{$key}
5056             }
5057              
5058             sub install_base_relpaths {
5059             # Usage: install_base_relpaths(), install_base_relpaths('lib'),
5060             # or install_base_relpaths('lib' => $value);
5061 82     82 0 5223 my $self = shift;
5062 82 100       225 if ( @_ > 1 ) { # change values before merge
5063 3         19 $self->_set_relpaths($self->{properties}{install_base_relpaths}, @_);
5064             }
5065             my $map = { $self->_merge_arglist(
5066             $self->{properties}{install_base_relpaths},
5067             $self->_default_install_paths->{install_base_relpaths}
5068 81         367 )};
5069 81 100       1024 return $map unless @_;
5070 73         171 my $relpath = $map->{$_[0]};
5071 73 100       818 return defined $relpath ? File::Spec->catdir( @$relpath ) : undef;
5072             }
5073              
5074             # Defaults to use in case the config install paths cannot be prefixified.
5075             sub prefix_relpaths {
5076             # Usage: prefix_relpaths('site'), prefix_relpaths('site', 'lib'),
5077             # or prefix_relpaths('site', 'lib' => $value);
5078 34     34 0 5287 my $self = shift;
5079 34 50 66     114 my $installdirs = shift || $self->installdirs
5080             or croak "Can't determine installdirs for prefix_relpaths()";
5081 34 100       82 if ( @_ > 1 ) { # change values before merge
5082 3   100     24 $self->{properties}{prefix_relpaths}{$installdirs} ||= {};
5083 3         11 $self->_set_relpaths($self->{properties}{prefix_relpaths}{$installdirs}, @_);
5084             }
5085             my $map = {$self->_merge_arglist(
5086             $self->{properties}{prefix_relpaths}{$installdirs},
5087 33         86 $self->_default_install_paths->{prefix_relpaths}{$installdirs}
5088             )};
5089 33 100       366 return $map unless @_;
5090 27         64 my $relpath = $map->{$_[0]};
5091 27 100       289 return defined $relpath ? File::Spec->catdir( @$relpath ) : undef;
5092             }
5093              
5094             sub _set_relpaths {
5095 6     6   9 my $self = shift;
5096 6         25 my( $map, $type, $value ) = @_;
5097              
5098 6 50       17 Carp::croak( 'Type argument missing' )
5099             unless defined( $type );
5100              
5101             # set undef if $value is literal undef()
5102 6 100       17 if ( ! defined( $value ) ) {
5103 2         5 $map->{$type} = undef;
5104 2         5 return;
5105             }
5106             # set value if $value is a valid relative path
5107             else {
5108 4 100       516 Carp::croak( "Value must be a relative path" )
5109             if File::Spec::Unix->file_name_is_absolute($value);
5110              
5111 2         11 my @value = split( /\//, $value );
5112 2         20 $map->{$type} = \@value;
5113             }
5114             }
5115              
5116             # Translated from ExtUtils::MM_Any::init_INSTALL_from_PREFIX
5117             sub prefix_relative {
5118 24     24 0 49 my ($self, $type) = @_;
5119 24         48 my $installdirs = $self->installdirs;
5120              
5121 24         50 my $relpath = $self->install_sets($installdirs)->{$type};
5122              
5123 24         82 return $self->_prefixify($relpath,
5124             $self->original_prefix($installdirs),
5125             $type,
5126             );
5127             }
5128              
5129             # Translated from ExtUtils::MM_Unix::prefixify()
5130             sub _prefixify {
5131 24     24   53 my($self, $path, $sprefix, $type) = @_;
5132              
5133 24         51 my $rprefix = $self->prefix;
5134 24 50       84 $rprefix .= '/' if $sprefix =~ m|/$|;
5135              
5136 24 50 33     174 $self->log_verbose(" prefixify $path from $sprefix to $rprefix\n")
5137             if defined( $path ) && length( $path );
5138              
5139 24 50 33     328 if( !defined( $path ) || ( length( $path ) == 0 ) ) {
    50          
    50          
5140 0         0 $self->log_verbose(" no path to prefixify, falling back to default.\n");
5141 0         0 return $self->_prefixify_default( $type, $rprefix );
5142             } elsif( !File::Spec->file_name_is_absolute($path) ) {
5143 0         0 $self->log_verbose(" path is relative, not prefixifying.\n");
5144             } elsif( $path !~ s{^\Q$sprefix\E\b}{}s ) {
5145 24         71 $self->log_verbose(" cannot prefixify, falling back to default.\n");
5146 24         60 return $self->_prefixify_default( $type, $rprefix );
5147             }
5148              
5149 0         0 $self->log_verbose(" now $path in $rprefix\n");
5150              
5151 0         0 return $path;
5152             }
5153              
5154             sub _prefixify_default {
5155 24     24   39 my $self = shift;
5156 24         33 my $type = shift;
5157 24         35 my $rprefix = shift;
5158              
5159 24         50 my $default = $self->prefix_relpaths($self->installdirs, $type);
5160 24 50       64 if( !$default ) {
5161 0         0 $self->log_verbose(" no default install location for type '$type', using prefix '$rprefix'.\n");
5162 0         0 return $rprefix;
5163             } else {
5164 24         84 return $default;
5165             }
5166             }
5167              
5168             sub install_destination {
5169 241     241 0 39969 my ($self, $type) = @_;
5170              
5171 241 100       1291 return $self->install_path($type) if $self->install_path($type);
5172              
5173 216 100       991 if ( $self->install_base ) {
5174 70         301 my $relpath = $self->install_base_relpaths($type);
5175 70 50       287 return $relpath ? File::Spec->catdir($self->install_base, $relpath) : undef;
5176             }
5177              
5178 146 100       699 if ( $self->prefix ) {
5179 24         61 my $relpath = $self->prefix_relative($type);
5180 24 50       71 return $relpath ? File::Spec->catdir($self->prefix, $relpath) : undef;
5181             }
5182              
5183 122         570 return $self->install_sets($self->installdirs)->{$type};
5184             }
5185              
5186             sub install_types {
5187 12     12 0 57 my $self = shift;
5188              
5189 12         62 my %types;
5190 12 100       214 if ( $self->install_base ) {
    50          
5191 5         51 %types = %{$self->install_base_relpaths};
  5         85  
5192             } elsif ( $self->prefix ) {
5193 0         0 %types = %{$self->prefix_relpaths};
  0         0  
5194             } else {
5195 7         19 %types = %{$self->install_sets($self->installdirs)};
  7         382  
5196             }
5197              
5198 12         107 %types = (%types, %{$self->install_path});
  12         99  
5199              
5200 12         240 return sort keys %types;
5201             }
5202              
5203             sub install_map {
5204 9     9 0 102 my ($self, $blib) = @_;
5205 9   33     366 $blib ||= $self->blib;
5206              
5207 9         47 my( %map, @skipping );
5208 9         158 foreach my $type ($self->install_types) {
5209 72         625 my $localdir = File::Spec->catdir( $blib, $type );
5210 72 100       1304 next unless -e $localdir;
5211              
5212             # the line "...next if (($type eq 'bindoc'..." was one of many changes introduced for
5213             # improving HTML generation on ActivePerl, see https://rt.cpan.org/Public/Bug/Display.html?id=53478
5214             # Most changes were ok, but this particular line caused test failures in t/manifypods.t on windows,
5215             # therefore it is commented out.
5216              
5217             # ********* next if (($type eq 'bindoc' || $type eq 'libdoc') && not $self->is_unixish);
5218              
5219 33 50       625 if (my $dest = $self->install_destination($type)) {
5220 33         313 $map{$localdir} = $dest;
5221             } else {
5222 0         0 push( @skipping, $type );
5223             }
5224             }
5225              
5226             $self->log_warn(
5227 9 50       87 "WARNING: Can't figure out install path for types: @skipping\n" .
5228             "Files will not be installed.\n"
5229             ) if @skipping;
5230              
5231             # Write the packlist into the same place as ExtUtils::MakeMaker.
5232 9 50 33     181 if ($self->create_packlist and my $module_name = $self->module_name) {
5233 9         63 my $archdir = $self->install_destination('arch');
5234 9         59 my @ext = split /::/, $module_name;
5235 9         219 $map{write} = File::Spec->catfile($archdir, 'auto', @ext, '.packlist');
5236             }
5237              
5238             # Handle destdir
5239 9 100 100     205 if (length(my $destdir = $self->destdir || '')) {
5240 5         57 foreach (keys %map) {
5241             # Need to remove volume from $map{$_} using splitpath, or else
5242             # we'll create something crazy like C:\Foo\Bar\E:\Baz\Quux
5243             # VMS will always have the file separate than the path.
5244 25         574 my ($volume, $path, $file) = File::Spec->splitpath( $map{$_}, 0 );
5245              
5246             # catdir needs a list of directories, or it will create something
5247             # crazy like volume:[Foo.Bar.volume.Baz.Quux]
5248 25         153 my @dirs = File::Spec->splitdir($path);
5249              
5250             # First merge the directories
5251 25         192 $path = File::Spec->catdir($destdir, @dirs);
5252              
5253             # Then put the file back on if there is one.
5254 25 50       77 if ($file ne '') {
5255 25         245 $map{$_} = File::Spec->catfile($path, $file)
5256             } else {
5257 0         0 $map{$_} = $path;
5258             }
5259             }
5260             }
5261              
5262 9         94 $map{read} = ''; # To keep ExtUtils::Install quiet
5263              
5264 9         183 return \%map;
5265             }
5266              
5267             sub depends_on {
5268 324     324 0 843 my $self = shift;
5269 324         1131 foreach my $action (@_) {
5270 352         2079 $self->_call_action($action);
5271             }
5272             }
5273              
5274             sub rscan_dir {
5275 466     466 0 2568 my ($self, $dir, $pattern) = @_;
5276 466         1127 my @result;
5277 466         1018 local $_; # find() can overwrite $_, so protect ourselves
5278 349     349   10841 my $subr = !$pattern ? sub {push @result, $File::Find::name} :
5279 1077 100   1077   39708 !ref($pattern) || (ref $pattern eq 'Regexp') ? sub {push @result, $File::Find::name if /$pattern/} :
5280 59 100   59   406 ref($pattern) eq 'CODE' ? sub {push @result, $File::Find::name if $pattern->()} :
5281 466 50 66     7634 die "Unknown pattern type";
    100          
    100          
5282              
5283 466     694   45016 File::Find::find({wanted => $subr, no_chdir => 1, preprocess => sub { sort @_ } }, $dir);
  694         23827  
5284 466         6042 return \@result;
5285             }
5286              
5287             sub delete_filetree {
5288 475     475 0 1722 my $self = shift;
5289 475         1013 my $deleted = 0;
5290 475         1458 foreach (@_) {
5291 580 100       8561 next unless -e $_;
5292 200         2461 $self->log_verbose("Deleting $_\n");
5293 200         127968 File::Path::rmtree($_, 0, 0);
5294 200 50       4798 die "Couldn't remove '$_': $!\n" if -e $_;
5295 200         868 $deleted++;
5296             }
5297 475         2055 return $deleted;
5298             }
5299              
5300             sub autosplit_file {
5301 0     0 0 0 my ($self, $file, $to) = @_;
5302 0         0 require AutoSplit;
5303 0         0 my $dir = File::Spec->catdir($to, 'lib', 'auto');
5304 0         0 AutoSplit::autosplit($file, $dir);
5305             }
5306              
5307             sub cbuilder {
5308             # Returns a CBuilder object
5309              
5310 54     54 0 11314 my $self = shift;
5311 54         198 my $s = $self->{stash};
5312 54 100       886 return $s->{_cbuilder} if $s->{_cbuilder};
5313              
5314 17         7832 require ExtUtils::CBuilder;
5315 17 100       443038 return $s->{_cbuilder} = ExtUtils::CBuilder->new(
5316             config => $self->config,
5317             ($self->quiet ? (quiet => 1 ) : ()),
5318             );
5319             }
5320              
5321             sub have_c_compiler {
5322 27     27 0 3006 my ($self) = @_;
5323              
5324 27         142 my $p = $self->{properties};
5325 27 100       244 return $p->{_have_c_compiler} if defined $p->{_have_c_compiler};
5326              
5327 8         128 $self->log_verbose("Checking if compiler tools configured... ");
5328 8         121 my $b = $self->cbuilder;
5329 8   33     61099 my $have = $b && eval { $b->have_compiler };
5330 8 50       476419 $self->log_verbose($have ? "ok.\n" : "failed.\n");
5331 8         297 return $p->{_have_c_compiler} = $have;
5332             }
5333              
5334             sub compile_c {
5335 19     19 0 218 my ($self, $file, %args) = @_;
5336              
5337 19 100       225 if ( ! $self->have_c_compiler ) {
5338 1         445 die "Error: no compiler detected to compile '$file'. Aborting\n";
5339             }
5340              
5341 18         146 my $b = $self->cbuilder;
5342 18         65979 my $obj_file = $b->object_file($file);
5343 18         731 $self->add_to_cleanup($obj_file);
5344 18 100       211 return $obj_file if $self->up_to_date($file, $obj_file);
5345              
5346             $b->compile(source => $file,
5347             defines => $args{defines},
5348 12         304 object_file => $obj_file,
5349             include_dirs => $self->include_dirs,
5350             extra_compiler_flags => $self->extra_compiler_flags,
5351             );
5352              
5353 12         2537774 return $obj_file;
5354             }
5355              
5356             sub link_c {
5357 18     18 0 131 my ($self, $spec) = @_;
5358 18         86 my $p = $self->{properties}; # For convenience
5359              
5360 18         152 $self->add_to_cleanup($spec->{lib_file});
5361              
5362 18   50     332 my $objects = $p->{objects} || [];
5363              
5364             return $spec->{lib_file}
5365             if $self->up_to_date([$spec->{obj_file}, @$objects],
5366 18 100       160 $spec->{lib_file});
5367              
5368 12   33     106 my $module_name = $spec->{module_name} || $self->module_name;
5369              
5370             $self->cbuilder->link(
5371             module_name => $module_name,
5372             objects => [$spec->{obj_file}, @$objects],
5373             lib_file => $spec->{lib_file},
5374 12         221 extra_linker_flags => $self->extra_linker_flags );
5375              
5376 12         424765 return $spec->{lib_file};
5377             }
5378              
5379             sub compile_xs {
5380 13     13 0 107 my ($self, $file, %args) = @_;
5381              
5382 13         102 $self->log_verbose("$file -> $args{outfile}\n");
5383              
5384 13 50       87 if (eval {require ExtUtils::ParseXS; 1}) {
  13         5517  
  13         127613  
5385              
5386             ExtUtils::ParseXS::process_file(
5387             filename => $file,
5388             prototypes => 0,
5389             output => $args{outfile},
5390 13         157 );
5391             } else {
5392             # Ok, I give up. Just use backticks.
5393              
5394 0 0       0 my $xsubpp = Module::Metadata->find_module_by_name('ExtUtils::xsubpp')
5395             or die "Can't find ExtUtils::xsubpp in INC (@INC)";
5396              
5397 0         0 my @typemaps;
5398 0         0 push @typemaps, Module::Metadata->find_module_by_name(
5399             'ExtUtils::typemap', \@INC
5400             );
5401 0         0 my $lib_typemap = Module::Metadata->find_module_by_name(
5402             'typemap', [File::Basename::dirname($file), File::Spec->rel2abs('.')]
5403             );
5404 0 0       0 push @typemaps, $lib_typemap if $lib_typemap;
5405 0         0 @typemaps = map {+'-typemap', $_} @typemaps;
  0         0  
5406              
5407 0         0 my $cf = $self->{config};
5408 0         0 my $perl = $self->{properties}{perl};
5409              
5410 0         0 my @command = ($perl, "-I".$cf->get('installarchlib'), "-I".$cf->get('installprivlib'), $xsubpp, '-noprototypes',
5411             @typemaps, $file);
5412              
5413 0         0 $self->log_info("@command\n");
5414 0 0       0 open(my $fh, '>', $args{outfile}) or die "Couldn't write $args{outfile}: $!";
5415 0         0 print {$fh} $self->_backticks(@command);
  0         0  
5416 0         0 close $fh;
5417             }
5418             }
5419              
5420             sub split_like_shell {
5421 1804     1804 0 522027 my ($self, $string) = @_;
5422              
5423 1804 100       11453 return () unless defined($string);
5424 538 100       3129 return @$string if ref $string eq 'ARRAY';
5425 423         4137 $string =~ s/^\s+|\s+$//g;
5426 423 100       2798 return () unless length($string);
5427              
5428 360         3062 return Text::ParseWords::shellwords($string);
5429             }
5430              
5431             sub oneliner {
5432             # Returns a string that the shell can evaluate as a perl command.
5433             # This should be avoided whenever possible, since "the shell" really
5434             # means zillions of shells on zillions of platforms and it's really
5435             # hard to get it right all the time.
5436              
5437             # Some of this code is stolen with permission from ExtUtils::MakeMaker.
5438              
5439 7     7 0 70 my($self, $cmd, $switches, $args) = @_;
5440 7 50       91 $switches = [] unless defined $switches;
5441 7 50       98 $args = [] unless defined $args;
5442              
5443             # Strip leading and trailing newlines
5444 7         63 $cmd =~ s{^\n+}{};
5445 7         35 $cmd =~ s{\n+$}{};
5446              
5447 7 50       70 my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
5448 7         161 return $self->_quote_args($perl, @$switches, '-e', $cmd, @$args);
5449             }
5450              
5451             sub run_perl_script {
5452 3754     3754 0 8248934 my ($self, $script, $preargs, $postargs) = @_;
5453 3754         35702 foreach ($preargs, $postargs) {
5454 7508 100       48713 $_ = [ $self->split_like_shell($_) ] unless ref();
5455             }
5456 3754         40218 return $self->run_perl_command([@$preargs, $script, @$postargs]);
5457             }
5458              
5459             sub run_perl_command {
5460             # XXX Maybe we should accept @args instead of $args? Must resolve
5461             # this before documenting.
5462 3766     3766 0 27428 my ($self, $args) = @_;
5463 3766 50       13097 $args = [ $self->split_like_shell($args) ] unless ref($args);
5464 3766 100       65773 my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
5465              
5466             # Make sure our local additions to @INC are propagated to the subprocess
5467 3734         67888 local $ENV{PERL5LIB} = join $self->config('path_sep'), $self->_added_to_INC;
5468              
5469 3702         48140 return $self->do_system($perl, @$args);
5470             }
5471              
5472             # Infer various data from the path of the input filename
5473             # that is needed to create output files.
5474             # The input filename is expected to be of the form:
5475             # lib/Module/Name.ext or Module/Name.ext
5476             sub _infer_xs_spec {
5477 19     19   60 my $self = shift;
5478 19         37 my $file = shift;
5479              
5480 19         54 my $cf = $self->{config};
5481              
5482 19         58 my %spec;
5483              
5484 19         338 my( $v, $d, $f ) = File::Spec->splitpath( $file );
5485 19         167 my @d = File::Spec->splitdir( $d );
5486 19         281 (my $file_base = $f) =~ s/\.[^.]+$//i;
5487              
5488 19         143 $spec{base_name} = $file_base;
5489              
5490 19         196 $spec{src_dir} = File::Spec->catpath( $v, $d, '' );
5491              
5492             # the module name
5493 19   100     470 shift( @d ) while @d && ($d[0] eq 'lib' || $d[0] eq '');
      100        
5494 19   100     321 pop( @d ) while @d && $d[-1] eq '';
5495 19         107 $spec{module_name} = join( '::', (@d, $file_base) );
5496              
5497 19         283 $spec{archdir} = File::Spec->catdir($self->blib, 'arch', 'auto',
5498             @d, $file_base);
5499              
5500             $spec{c_file} = File::Spec->catfile( $spec{src_dir},
5501 19         257 "${file_base}.c" );
5502              
5503             $spec{obj_file} = File::Spec->catfile( $spec{src_dir},
5504 19         230 "${file_base}".$cf->get('obj_ext') );
5505              
5506 19         287 require DynaLoader;
5507 19 50       124 my $modfname = defined &DynaLoader::mod2fname ? DynaLoader::mod2fname([@d, $file_base]) : $file_base;
5508              
5509 19         200 $spec{bs_file} = File::Spec->catfile($spec{archdir}, "$modfname.bs");
5510              
5511 19         124 $spec{lib_file} = File::Spec->catfile($spec{archdir}, "$modfname.".$cf->get('dlext'));
5512              
5513 19         165 return \%spec;
5514             }
5515              
5516             sub process_xs {
5517 19     19 0 84 my ($self, $file) = @_;
5518              
5519 19         167 my $spec = $self->_infer_xs_spec($file);
5520              
5521             # File name, minus the suffix
5522 19         262 (my $file_base = $file) =~ s/\.[^.]+$//;
5523              
5524             # .xs -> .c
5525 19         91 $self->add_to_cleanup($spec->{c_file});
5526              
5527 19 100       119 unless ($self->up_to_date($file, $spec->{c_file})) {
5528 13         263 $self->compile_xs($file, outfile => $spec->{c_file});
5529             }
5530              
5531             # .c -> .o
5532 19         629731 my $v = $self->dist_version;
5533             $self->compile_c($spec->{c_file},
5534 19         678 defines => {VERSION => qq{"$v"}, XS_VERSION => qq{"$v"}});
5535              
5536             # archdir
5537 18 100       9571 File::Path::mkpath($spec->{archdir}, 0, oct(777)) unless -d $spec->{archdir};
5538              
5539             # .xs -> .bs
5540 18         389 $self->add_to_cleanup($spec->{bs_file});
5541 18 100       300 unless ($self->up_to_date($file, $spec->{bs_file})) {
5542 12         5202 require ExtUtils::Mkbootstrap;
5543 12         5868 $self->log_info("ExtUtils::Mkbootstrap::Mkbootstrap('$spec->{bs_file}')\n");
5544 12         212 ExtUtils::Mkbootstrap::Mkbootstrap($spec->{bs_file}); # Original had $BSLOADLIBS - what's that?
5545 12         2772 open(my $fh, '>>', $spec->{bs_file}); # create
5546 12         584 utime((time)x2, $spec->{bs_file}); # touch
5547             }
5548              
5549             # .o -> .(a|bundle)
5550 18         339 $self->link_c($spec);
5551             }
5552              
5553             sub do_system {
5554 4046     4046 0 1469941 my ($self, @cmd) = @_;
5555 4046         65553 $self->log_verbose("@cmd\n");
5556              
5557             # Some systems proliferate huge PERL5LIBs, try to ameliorate:
5558 4046         10290 my %seen;
5559 4046         22743 my $sep = $self->config('path_sep');
5560             local $ENV{PERL5LIB} =
5561             ( !exists($ENV{PERL5LIB}) ? '' :
5562             length($ENV{PERL5LIB}) < 500
5563             ? $ENV{PERL5LIB}
5564 0 0       0 : join $sep, grep { ! $seen{$_}++ and -d $_ } split($sep, $ENV{PERL5LIB})
5565 4046 50       51423 );
    50          
5566              
5567 4046         320790638 my $status = system(@cmd);
5568 4046 50 66     101806 if ($status and $! =~ /Argument list too long/i) {
5569 0         0 my $env_entries = '';
5570 0         0 foreach (sort keys %ENV) { $env_entries .= "$_=>".length($ENV{$_})."; " }
  0         0  
5571 0         0 warn "'Argument list' was 'too long', env lengths are $env_entries";
5572             }
5573 4046         727971 return !$status;
5574             }
5575              
5576             sub copy_if_modified {
5577 189     189 0 3509 my $self = shift;
5578 189 100       1809 my %args = (@_ > 3
5579             ? ( @_ )
5580             : ( from => shift, to_dir => shift, flatten => shift )
5581             );
5582             $args{verbose} = !$self->quiet
5583 189 100       1334 unless exists $args{verbose};
5584              
5585 189         498 my $file = $args{from};
5586 189 50 33     1499 unless (defined $file and length $file) {
5587 0         0 die "No 'from' parameter given to copy_if_modified";
5588             }
5589              
5590             # makes no sense to replicate an absolute path, so assume flatten
5591 189 100       1994 $args{flatten} = 1 if File::Spec->file_name_is_absolute( $file );
5592              
5593 189         505 my $to_path;
5594 189 100 66     1632 if (defined $args{to} and length $args{to}) {
    50 33        
5595 102         352 $to_path = $args{to};
5596             } elsif (defined $args{to_dir} and length $args{to_dir}) {
5597             $to_path = File::Spec->catfile( $args{to_dir}, $args{flatten}
5598 87 100       1865 ? File::Basename::basename($file)
5599             : $file );
5600             } else {
5601 0         0 die "No 'to' or 'to_dir' parameter given to copy_if_modified";
5602             }
5603              
5604 189 100       1245 return if $self->up_to_date($file, $to_path); # Already fresh
5605              
5606             {
5607 144         417 local $self->{properties}{quiet} = 1;
  144         506  
5608 144         896 $self->delete_filetree($to_path); # delete destination if exists
5609             }
5610              
5611             # Create parent directories
5612 144         19329 File::Path::mkpath(File::Basename::dirname($to_path), 0, oct(777));
5613              
5614 144         1483 $self->log_verbose("Copying $file -> $to_path\n");
5615              
5616 144 50       866 if ($^O eq 'os2') {# copy will not overwrite; 0x1 = overwrite
5617 0         0 chmod 0666, $to_path;
5618 0 0       0 File::Copy::syscopy($file, $to_path, 0x1) or die "Can't copy('$file', '$to_path'): $!";
5619             } else {
5620 144 50       1256 File::Copy::copy($file, $to_path) or die "Can't copy('$file', '$to_path'): $!";
5621             }
5622              
5623             # mode is read-only + (executable if source is executable)
5624 144 100       51169 my $mode = oct(444) | ( $self->is_executable($file) ? oct(111) : 0 );
5625 144         2460 chmod( $mode, $to_path );
5626              
5627 144         1165 return $to_path;
5628             }
5629              
5630             sub up_to_date {
5631 314     314 0 1030 my ($self, $source, $derived) = @_;
5632 314 100       1394 $source = [$source] unless ref $source;
5633 314 100       1261 $derived = [$derived] unless ref $derived;
5634              
5635             # empty $derived means $source should always run
5636 314 100 66     2528 return 0 if @$source && !@$derived || grep {not -e} @$derived;
  313   100     8214  
5637              
5638 88         518 my $most_recent_source = time / (24*60*60);
5639 88         417 foreach my $file (@$source) {
5640 88 50       1187 unless (-e $file) {
5641 0         0 $self->log_warn("Can't find source file $file for up-to-date check");
5642 0         0 next;
5643             }
5644 88 50       538 $most_recent_source = -M _ if -M _ < $most_recent_source;
5645             }
5646              
5647 88         333 foreach my $derived (@$derived) {
5648 88 100       1384 return 0 if -M $derived > $most_recent_source;
5649             }
5650 87         1112 return 1;
5651             }
5652              
5653             sub dir_contains {
5654 35     35 0 1363 my ($self, $first, $second) = @_;
5655             # File::Spec doesn't have an easy way to check whether one directory
5656             # is inside another, unfortunately.
5657              
5658 35         335 ($first, $second) = map File::Spec->canonpath($_), ($first, $second);
5659 35         223 my @first_dirs = File::Spec->splitdir($first);
5660 35         217 my @second_dirs = File::Spec->splitdir($second);
5661              
5662 35 50       216 return 0 if @second_dirs < @first_dirs;
5663              
5664             my $is_same = ( $self->_case_tolerant
5665 0     0   0 ? sub {lc(shift()) eq lc(shift())}
5666 35 50   91   332 : sub {shift() eq shift()} );
  91         394  
5667              
5668 35         224 while (@first_dirs) {
5669 91 50       805 return 0 unless $is_same->(shift @first_dirs, shift @second_dirs);
5670             }
5671              
5672 35         322 return 1;
5673             }
5674              
5675             1;
5676             __END__