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   6379 use 5.006;
  297         1128  
6 297     293   1626 use strict;
  293         570  
  293         5613  
7 293     293   1392 use warnings;
  293         759  
  293         14935  
8              
9             our $VERSION = '0.4234';
10             $VERSION = eval $VERSION;
11              
12 293     293   1797 use Carp;
  293         564  
  293         21315  
13 293     293   1879 use Cwd ();
  293         691  
  293         10097  
14 293     293   157988 use File::Copy ();
  293         724603  
  293         7239  
15 293     293   2064 use File::Find ();
  293         634  
  293         4162  
16 293     293   1364 use File::Path ();
  293         563  
  293         4021  
17 293     293   1328 use File::Basename ();
  293         583  
  293         5377  
18 293     293   1392 use File::Spec 0.82 ();
  293         5673  
  293         5258  
19 293     293   139101 use File::Compare ();
  293         288446  
  293         6669  
20 293     293   138186 use Module::Build::Dumper ();
  293         799  
  293         6066  
21 293     293   142610 use Text::ParseWords ();
  293         394263  
  293         7959  
22              
23 293     293   185346 use Module::Metadata;
  293         2451298  
  293         10621  
24 293     293   152823 use Module::Build::Notes;
  293         900  
  293         10282  
25 293     293   131701 use Module::Build::Config;
  293         930  
  293         9323  
26 293     293   1702 use version;
  293         639  
  293         1558  
27              
28              
29             #################### Constructors ###########################
30             sub new {
31 91     91 0 314333 my $self = shift()->_construct(@_);
32              
33 80   50     1852 $self->{invoked_action} = $self->{action} ||= 'Build_PL';
34 80         1726 $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     1337 if $self->{action} && $self->{action} ne 'Build_PL';
38              
39 80         2334 $self->check_manifest;
40 80         1525 $self->auto_require;
41              
42             # All checks must run regardless if one fails, so no short circuiting!
43 80 100       679 if( grep { !$_ } $self->check_prereq, $self->check_autofeatures ) {
  160         826  
44 10         50 $self->log_warn(<<EOF);
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     125 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         1037 $self->{properties}{_added_to_INC} = [ $self->_added_to_INC ];
62              
63 69         2400 $self->set_bundle_inc;
64              
65 69         1102 $self->dist_name;
66 69         820 $self->dist_version;
67 69         505 $self->release_status;
68 69 100       241 $self->_guess_module_name unless $self->module_name;
69              
70 69         787 $self->_find_nested_builds;
71              
72 69         2441 return $self;
73             }
74              
75             sub resume {
76 469     469 0 45579 my $package = shift;
77 469         22603 my $self = $package->_construct(@_);
78 466         10258 $self->read_config;
79              
80 466 50       1951 my @added_earlier = @{ $self->{properties}{_added_to_INC} || [] };
  466         4730  
81              
82 466         7803 @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       8738 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       6757 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         28199 $self->cull_args(@ARGV);
110              
111 367 50       5672 unless ($self->allow_mb_mismatch) {
112 367         3121 my $mb_version = $Module::Build::VERSION;
113 367 50       7451 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     10693 $self->{invoked_action} = $self->{action} ||= 'build';
123              
124 367         55108 return $self;
125             }
126              
127             sub new_from_context {
128 481     481 0 3385250 my ($package, %args) = @_;
129              
130 481         13892 $package->run_perl_script('Build.PL',[],[$package->unparse_args(\%args)]);
131 427         35857 return $package->resume;
132             }
133              
134             sub current {
135             # hmm, wonder what the right thing to do here is
136 21     21 0 8791 local @ARGV;
137 21         317 return shift()->resume;
138             }
139              
140             sub _construct {
141 560     560   7107 my ($package, %input) = @_;
142              
143 560   50     19026 my $args = delete $input{args} || {};
144 560   100     14632 my $config = delete $input{config} || {};
145              
146 560         30299 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         31961 $self->_set_defaults;
159 560         5654 my ($p, $ph) = ($self->{properties}, $self->{phash});
160              
161 560         4112 foreach (qw(notes config_data features runtime_params cleanup auto_features)) {
162 3360         15734 my $file = File::Spec->catfile($self->config_dir, $_);
163 3360         46253 $ph->{$_} = Module::Build::Notes->new(file => $file);
164 3360 100       88955 $ph->{$_}->restore if -e $file;
165 3360 50       16143 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       11739 $p->{perl} = $self->find_perl_interpreter
179             or $self->log_warn("Warning: Can't locate your perl binary");
180              
181 546     1634   9498 my $blibdir = sub { File::Spec->catdir($p->{blib}, @_) };
  1634         20150  
182 546   100     8440 $p->{bindoc_dirs} ||= [ $blibdir->("script") ];
183 546   100     7613 $p->{libdoc_dirs} ||= [ $blibdir->("lib"), $blibdir->("arch") ];
184              
185 546 100 66     4435 $p->{dist_author} = [ $p->{dist_author} ] if defined $p->{dist_author} and not ref $p->{dist_author};
186              
187             # Synonyms
188 546 50       2746 $p->{requires} = delete $p->{prereq} if defined $p->{prereq};
189 546 100       2320 $p->{script_files} = delete $p->{scripts} if defined $p->{scripts};
190              
191             # Convert to from shell strings to arrays
192 546         2910 for ('extra_compiler_flags', 'extra_linker_flags') {
193 1092 50       17310 $p->{$_} = [ $self->split_like_shell($p->{$_}) ] if exists $p->{$_};
194             }
195              
196             # Convert to arrays
197 546         2980 for ('include_dirs') {
198 546 100 66     9923 $p->{$_} = [ $p->{$_} ] if exists $p->{$_} && !ref $p->{$_}
199             }
200              
201 0         0 $self->add_to_cleanup( @{delete $p->{add_to_cleanup}} )
202 546 50       3271 if $p->{add_to_cleanup};
203              
204 546         11550 return $self;
205             }
206              
207             ################## End constructors #########################
208              
209             sub log_info {
210 168     168 0 733 my $self = shift;
211 168 100 66     2734 print @_ if ref($self) && ( $self->verbose || ! $self->quiet );
      66        
212             }
213             sub log_verbose {
214 4817     4817 0 18012 my $self = shift;
215 4817 100 100     49247 print @_ if ref($self) && $self->verbose;
216             }
217             sub log_debug {
218 790     790 0 2069 my $self = shift;
219 790 50 33     6048 print @_ if ref($self) && $self->debug;
220             }
221              
222             sub log_warn {
223             # Try to make our call stack invisible
224 103     103 0 464 shift;
225 103 50 33     2569 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         26643 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   658 my $self = shift;
238 365         711 my $c = $self->{config};
239 365         710 my $p = {};
240              
241 365 50       1678 my @libstyle = $c->get('installstyle') ?
242             File::Spec->splitdir($c->get('installstyle')) : qw(lib perl5);
243 365         1543 my $arch = $c->get('archname');
244 365         1127 my $version = $c->get('version');
245              
246 365   100     1099 my $bindoc = $c->get('installman1dir') || undef;
247 365   100     1145 my $libdoc = $c->get('installman3dir') || undef;
248              
249 365   100     1093 my $binhtml = $c->get('installhtml1dir') || $c->get('installhtmldir') || undef;
250 365   100     1340 my $libhtml = $c->get('installhtml3dir') || $c->get('installhtmldir') || undef;
251              
252             $p->{install_sets} =
253             {
254 365   33     1277 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     2931 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     1400 $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         2928 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         9216 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         2454 return $p
347             }
348              
349             sub _find_nested_builds {
350 69     69   200 my $self = shift;
351 69 50       654 my $r = $self->recurse_into or return;
352              
353 69         236 my ($file, @r);
354 69 50 33     384 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         313 $self->recurse_into(\@r);
366             }
367              
368             sub cwd {
369 578     578 0 2741763 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   77 my ($self, @args) = @_;
376              
377 7         49 my @quoted;
378              
379 7         35 for (@args) {
380 28 100       203 if ( /^[^\s*?!\$<>;\\|'"\[\]\{\}]+$/ ) {
381             # Looks pretty safe
382 21         56 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         42 s/('+)/'"$1"'/g;
387 7         35 push @quoted, qq('$_');
388             }
389             }
390              
391 7         91 return join " ", @quoted;
392             }
393              
394             sub _backticks {
395 2532     2532   3659313 my ($self, @cmd) = @_;
396 2532 50       25640 if ($self->have_forkpipe) {
397 2532         11543 local *FH;
398 2532         3138863 my $pid = open *FH, "-|";
399 2532 100       144462 if ($pid) {
400 2290 100       6015213293 return wantarray ? <FH> : join '', <FH>;
401             } else {
402 242 50       34767 die "Can't execute @cmd: $!\n" unless defined $pid;
403 242         8406 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 14555 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   5077 my ($self, $perl) = @_;
419              
420 751         3717 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       7677 if ($ENV{PERL_CORE}) {
429 0         0 push @cmd, '-I' . File::Spec->catdir(File::Basename::dirname($perl), 'lib');
430             }
431              
432 751         9574 push @cmd, qw(-MConfig=myconfig -e print -e myconfig);
433 751         10115 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 17510 my $self = shift;
441              
442 4282 100       27911 return $known_perl if defined($known_perl);
443 288         4998 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   1262 my $proto = shift;
459 288 100       2343 my $c = ref($proto) ? $proto->{config} : 'Module::Build::Config';
460              
461 288         2358 my $perl = $^X;
462 288         28594 my $perl_basename = File::Basename::basename($perl);
463              
464 288         1283 my @potential_perls;
465              
466             # Try 1, Check $^X for absolute path
467 288 50       12089 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         6319 my $abs_perl = File::Spec->rel2abs($perl);
472 288         1329 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       2051 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         6694 push( @potential_perls, $c->get('perlpath') );
499              
500 288         24761 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         3181 my $exe = $c->get('exe_ext');
508 288         2427 foreach my $thisperl ( @potential_perls ) {
509              
510 288 50       4200 if (defined $exe) {
511 288 50       5474 $thisperl .= $exe unless $thisperl =~ m/$exe$/i;
512             }
513              
514 288 50 66     12941 if ( -f $thisperl && $proto->_perl_is_same($thisperl) ) {
515 242         17103 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 18 my ($self, $command) = @_;
529              
530 7 50       48 if( File::Spec->file_name_is_absolute($command) ) {
531 7         22 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   14 my($self,$file) = @_;
544 7 50 33     181 return $file if -x $file && ! -d $file;
545 7         27 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     80 ( !$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 = <STDIN>;
564 0 0       0 chomp $answer if defined $answer;
565 0         0 return $answer;
566             }
567              
568             sub prompt {
569 9     9 0 2854 my $self = shift;
570 9 100       51 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       32 @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         51 local $|=1;
582 8         135 print "$mess ", @dispdef;
583              
584 8 100 66     51 if ( $self->_is_unattended && !@def ) {
585 2         23 die <<EOF;
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         51 my $ans = $self->_readline();
592              
593 6 100 66     52 if ( !defined($ans) # Ctrl-D or unattended
594             or !length($ans) ) { # User hit return
595 4         29 print "$dispdef[1]\n";
596 4 100       23 $ans = scalar(@def) ? $def[0] : '';
597             }
598              
599 6         79 return $ans;
600             }
601              
602             sub y_n {
603 5     5 0 3648 my $self = shift;
604 5         18 my ($mess, $def) = @_;
605              
606 5 100       37 die "y_n() called without a prompt message" unless $mess;
607 4 100 100     94 die "Invalid default value: y_n() default must be 'y' or 'n'"
608             if $def && $def !~ /^[yn]/i;
609              
610 3         10 my $answer;
611 3         16 while (1) { # XXX Infinite or a large number followed by an exception ?
612 3         11 $answer = $self->prompt(@_);
613 2 50       15 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 2021 sub invoked_action { shift->{invoked_action} }
622              
623 31     31 0 6147 sub notes { shift()->{phash}{notes}->access(@_) }
624 2     2 0 2030 sub config_data { shift()->{phash}{config_data}->access(@_) }
625 4 50   4 0 132 sub runtime_params { shift->{phash}{runtime_params}->read( @_ ? shift : () ) } # Read-only
626 82     82 0 1212 sub auto_features { shift()->{phash}{auto_features}->access(@_) }
627              
628             sub features {
629 2     2 0 2615 my $self = shift;
630 2         24 my $ph = $self->{phash};
631              
632 2 50       16 if (@_) {
633 2         14 my $key = shift;
634 2 50       33 if ($ph->{features}->exists($key)) {
635 0         0 return $ph->{features}->access($key, @_);
636             }
637              
638 2 100       34 if (my $info = $ph->{auto_features}->access($key)) {
639 1         7 my $disabled;
640 1         9 for my $type ( @{$self->prereq_action_types} ) {
  1         38  
641 5 100 66     46 next if $type eq 'description' || $type eq 'recommends' || ! exists $info->{$type};
      100        
642 1         9 my $prereqs = $info->{$type};
643 1         13 for my $modname ( sort keys %$prereqs ) {
644 1         5 my $spec = $prereqs->{$modname};
645 1         22 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       86 if ( ! eval "require $modname; 1" ) { return 0; }
  0         0  
648             }
649             }
650 1         11 return 1;
651             }
652              
653 1         10 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   1501033 BEGIN { *feature = \&features } # Alias
670              
671             sub _mb_feature {
672 56     56   191 my $self = shift;
673              
674 56 50 50     532 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         9370 require Module::Build::ConfigData;
680 56         1001 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 1396 my ($self, $elem) = @_;
696 2         15 my $elems = $self->build_elements;
697 2 100       18 push @$elems, $elem unless grep { $_ eq $elem } @$elems;
  15         86  
698             }
699              
700             sub ACTION_config_data {
701 61     61 0 334 my $self = shift;
702 61 100       892 return unless $self->has_config_data;
703              
704 1 50       13 my $module_name = $self->module_name
705             or die "The config_data feature requires that 'module_name' be set";
706 1         4 my $notes_name = $module_name . '::ConfigData'; # TODO: Customize name ???
707 1         3 my $notes_pm = File::Spec->catfile($self->blib, 'lib', split /::/, "$notes_name.pm");
708              
709 1 50       6 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         22 $self->log_verbose("Writing config notes to $notes_pm\n");
715 1         160 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         10 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   107189 my $class = ref($_[0]) || $_[0];
735 31127         85153 return ($class, $class->mb_parents);
736             }
737              
738             sub valid_property {
739 27990     27990 0 45988 my ($class, $prop) = @_;
740 27990         48081 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 4472 my %out;
749 560         12962 for my $class (reverse shift->_mb_classes) {
750 1719         207689 @out{ keys %{ $valid_properties{$class} } } = map {
751 49868         241328 $_->()
752 1719         9466 } values %{ $valid_properties{$class} };
  1719         22808  
753             }
754 560         4686 return \%out;
755             }
756              
757             sub array_properties {
758 561 100   561 0 4183 map { exists $additive_properties{$_}->{ARRAY} ? @{$additive_properties{$_}->{ARRAY}} : () } shift->_mb_classes;
  1723         9280  
  562         7000  
759             }
760              
761             sub hash_properties {
762 2016 100   2016 0 16888 map { exists $additive_properties{$_}->{HASH} ? @{$additive_properties{$_}->{HASH}} : () } shift->_mb_classes;
  6181         25353  
  2038         34921  
763             }
764              
765             sub add_property {
766 26102     26102 0 71837 my ($class, $property) = (shift, shift);
767 26102 100       44909 die "Property '$property' already exists"
768             if $class->valid_property($property);
769 26101 100       67636 my %p = @_ == 1 ? ( default => shift ) : @_;
770              
771 26101         46687 my $type = ref $p{default};
772             $valid_properties{$class}{$property} =
773             $type eq 'CODE' ? $p{default} :
774 8406     8406   15855 $type eq 'HASH' ? sub { return { %{ $p{default} } } } :
  8406         57848  
775 3920     3920   8208 $type eq 'ARRAY'? sub { return [ @{ $p{default} } ] } :
  3920         50618  
776 26101 100   37536   122809 sub { return $p{default} } ;
  37536 100       164832  
    100          
777              
778 26101 100       60504 push @{$additive_properties{$class}->{$type}}, $property
  6458         14629  
779             if $type;
780              
781 26101 100       125849 unless ($class->can($property)) {
782             # TODO probably should put these in a util package
783 20241 100       48855 my $sub = $type eq 'HASH'
784             ? _make_hash_accessor($property, \%p)
785             : _make_accessor($property, \%p);
786 293     293   2562 no strict 'refs';
  293         775  
  293         399287  
787 20241         32880 *{"$class\::$property"} = $sub;
  20241         64332  
788             }
789              
790 26101         58954 return $class;
791             }
792              
793             sub property_error {
794 4     4 0 56 my $self = shift;
795 4         52 die 'ERROR: ', @_;
796             }
797              
798             sub _set_defaults {
799 560     560   4878 my $self = shift;
800              
801             # Set the build class.
802 560   66     26720 $self->{properties}{build_class} ||= ref $self;
803              
804             # If there was no orig_dir, set to the same as base_dir
805 560   33     15442 $self->{properties}{orig_dir} ||= $self->{properties}{base_dir};
806              
807 560         14026 my $defaults = $self->valid_properties_defaults;
808              
809 560         11734 foreach my $prop (keys %$defaults) {
810             $self->{properties}{$prop} = $defaults->{$prop}
811 49868 100       142928 unless exists $self->{properties}{$prop};
812             }
813              
814             # Copy defaults for arrays any arrays.
815 560         13269 for my $prop ($self->array_properties) {
816 0         0 $self->{properties}{$prop} = [@{$defaults->{$prop}}]
817 3920 50       12712 unless exists $self->{properties}{$prop};
818             }
819             # Copy defaults for arrays any hashes.
820 560         12334 for my $prop ($self->hash_properties) {
821 0         0 $self->{properties}{$prop} = {%{$defaults->{$prop}}}
822 8406 50       29902 unless exists $self->{properties}{$prop};
823             }
824             }
825              
826             } # end enclosure
827             ########################################################################
828             sub _make_hash_accessor {
829 2935     2935   5284 my ($property, $p) = @_;
830 2935   100 3   17551 my $check = $p->{check} || sub { 1 };
  3         14  
831              
832             return sub {
833 1260     1260   21340 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       9102 unless(ref($self)) {
839 0         0 carp("\n$property not a class method (@_)");
840 0         0 return;
841             }
842              
843 1260         5899 my $x = $self->{properties};
844 1260 100       10494 return $x->{$property} unless @_;
845              
846 8         23 my $prop = $x->{$property};
847 8 100 100     60 if ( defined $_[0] && !ref $_[0] ) {
848 4 100       30 if ( @_ == 1 ) {
    50          
849 1 50       8 return exists $prop->{$_[0]} ? $prop->{$_[0]} : undef;
850             } elsif ( @_ % 2 == 0 ) {
851 3         8 my %new = (%{ $prop }, @_);
  3         20  
852 3         9 local $_ = \%new;
853 3 50       11 $x->{$property} = \%new if $check->($self);
854 3         34 return $x->{$property};
855             } else {
856 0         0 die "Unexpected arguments for property '$property'\n";
857             }
858             } else {
859 4 100 100     43 die "Unexpected arguments for property '$property'\n"
860             if defined $_[0] && ref $_[0] ne 'HASH';
861 3         11 local $_ = $_[0];
862 3 50       10 $x->{$property} = shift if $check->($self);
863             }
864 2935         13236 };
865             }
866             ########################################################################
867             sub _make_accessor {
868 17306     17306   31076 my ($property, $p) = @_;
869 17306   100 179   74508 my $check = $p->{check} || sub { 1 };
  179         1042  
870              
871             return sub {
872 11998     11998   179811 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       44400 unless(ref($self)) {
878 0         0 carp("\n$property not a class method (@_)");
879 0         0 return;
880             }
881              
882 11998         31945 my $x = $self->{properties};
883 11998 100       173299 return $x->{$property} unless @_;
884 187         647 local $_ = $_[0];
885 187 50       1184 $x->{$property} = shift if $check->($self);
886 184         1065 return $x->{$property};
887 17306         75148 };
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 66325 my $self = shift;
1008 7884 100       51704 my $c = ref($self) ? $self->{config} : 'Module::Build::Config';
1009 7884 100       34325 return $c->all_config unless @_;
1010              
1011 7854         57285 my $key = shift;
1012 7854 100       124379 return $c->get($key) unless @_;
1013              
1014 1         4 my $val = shift;
1015 1         7 return $c->set($key => $val);
1016             }
1017              
1018             sub mb_parents {
1019             # Code borrowed from Class::ISA.
1020 31131     31131 0 71939 my @in_stack = (shift);
1021 31131         77084 my %seen = ($in_stack[0] => 1);
1022              
1023 31131         55021 my ($current, @out);
1024 31131         76458 while (@in_stack) {
1025 41615 100 66     253074 next unless defined($current = shift @in_stack)
1026             && $current->isa('Module::Build::Base');
1027 41614         92388 push @out, $current;
1028 41614 100       122992 next if $current eq 'Module::Build::Base';
1029 293     293   2532 no strict 'refs';
  293         1178  
  293         1588563  
1030             unshift @in_stack,
1031             map {
1032 10485         35732 my $c = $_; # copy, to avoid being destructive
1033 10485 50       38279 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       84600 $seen{$c}++ ? () : $c;
1037 10483         23705 } @{"$current\::ISA"};
  10483         75375  
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         47438 shift @out;
1044 31131         130831 return @out;
1045             }
1046              
1047 19     19 0 262 sub extra_linker_flags { shift->_list_accessor('extra_linker_flags', @_) }
1048 23     23 0 5987 sub extra_compiler_flags { shift->_list_accessor('extra_compiler_flags', @_) }
1049              
1050             sub _list_accessor {
1051 42     42   389 (my $self, local $_) = (shift, shift);
1052 42         247 my $p = $self->{properties};
1053 42 50       250 $p->{$_} = [@_] if @_;
1054 42 50       251 $p->{$_} = [] unless exists $p->{$_};
1055 42 50       895 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 134314 my ($pack, %opts) = @_;
1062              
1063 22         301 my $build_dir = '_build'; # XXX The _build directory is ostensibly settable by the user. Shouldn't hard-code here.
1064 22 100       934 $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     525 unless $opts{code} or $opts{class};
1068              
1069 22   100     494 $opts{code} ||= '';
1070 22   100     631 $opts{class} ||= 'MyModuleBuilder';
1071              
1072 22         592 my $filename = File::Spec->catfile($build_dir, 'lib', split '::', $opts{class}) . '.pm';
1073 22         2444 my $filedir = File::Basename::dirname($filename);
1074 22         787 $pack->log_verbose("Creating custom builder $filename in $filedir\n");
1075              
1076 22         7393 File::Path::mkpath($filedir);
1077 22 50       467 die "Can't create directory $filedir: $!" unless -d $filedir;
1078              
1079 22 50       2120 open(my $fh, '>', $filename) or die "Can't create $filename: $!";
1080 22         584 print $fh <<EOF;
1081             package $opts{class};
1082             use $pack;
1083             our \@ISA = qw($pack);
1084             $opts{code}
1085             1;
1086             EOF
1087 22         977 close $fh;
1088              
1089 22         2421 unshift @INC, File::Spec->catdir(File::Spec->rel2abs($build_dir), 'lib');
1090 22         4234 eval "use $opts{class}";
1091 22 50       178 die $@ if $@;
1092              
1093 22         470 return $opts{class};
1094             }
1095              
1096             sub _guess_module_name {
1097 5     5   115 my $self = shift;
1098 5         35 my $p = $self->{properties};
1099 5 50       80 return if $p->{module_name};
1100 5 50 33     55 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         60 my $mod_path = my $mod_name = $p->{dist_name};
1106 5         65 $mod_name =~ s{-}{::}g;
1107 5         20 $mod_path =~ s{-}{/}g;
1108 5         35 $mod_path .= ".pm";
1109 5 50 33     265 if ( -e $mod_path || -e "lib/$mod_path" ) {
1110 5         70 $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 10103 my $self = shift;
1125 225         838 my $p = $self->{properties};
1126 225         1191 my $me = 'dist_name';
1127 225 100       2494 return $p->{$me} if defined $p->{$me};
1128              
1129 63 50       759 die "Can't determine distribution name, must supply either 'dist_name' or 'module_name' parameter"
1130             unless $self->module_name;
1131              
1132 63         292 ($p->{$me} = $self->module_name) =~ s/::/-/g;
1133              
1134 63         488 return $p->{$me};
1135             }
1136              
1137             sub release_status {
1138 141     141 0 62874 my ($self) = @_;
1139 141         1027 my $me = 'release_status';
1140 141         732 my $p = $self->{properties};
1141              
1142 141 100       824 if ( ! defined $p->{$me} ) {
1143 69 50       804 $p->{$me} = $self->_is_dev_version ? 'testing' : 'stable';
1144             }
1145              
1146 141 50       3654 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     2054 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         1489 return $p->{$me};
1155             }
1156              
1157             sub dist_suffix {
1158 40     40 0 15388 my ($self) = @_;
1159 40         153 my $p = $self->{properties};
1160 40         179 my $me = 'dist_suffix';
1161              
1162 40 100       267 return $p->{$me} if defined $p->{$me};
1163              
1164 22 100       179 if ( $self->release_status eq 'stable' ) {
1165 13         57 $p->{$me} = "";
1166             }
1167             else {
1168             # non-stable release but non-dev version number needs '-TRIAL' appended
1169 9 50       121 $p->{$me} = $self->_is_dev_version ? "" : "TRIAL" ;
1170             }
1171              
1172 22         315 return $p->{$me};
1173             }
1174              
1175             sub dist_version_from {
1176 170     170 0 868 my ($self) = @_;
1177 170         512 my $p = $self->{properties};
1178 170         907 my $me = 'dist_version_from';
1179              
1180 170 50       1289 if ($self->module_name) {
1181 170   66     1424 $p->{$me} ||=
1182             join( '/', 'lib', split(/::/, $self->module_name) ) . '.pm';
1183             }
1184 170   50     1450 return $p->{$me} || undef;
1185             }
1186              
1187             sub dist_version {
1188 374     374 0 1180 my ($self) = @_;
1189 374         1105 my $p = $self->{properties};
1190 374         1365 my $me = 'dist_version';
1191              
1192 374 100       2790 return $p->{$me} if defined $p->{$me};
1193              
1194 63 50       710 if ( my $dist_version_from = $self->dist_version_from ) {
1195 63         2456 my $version_from = File::Spec->catfile( split( qr{/}, $dist_version_from ) );
1196 63 50       1742 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         103628 $p->{$me} = $self->normalize_version( $pm_info->version() );
1200 63 50       791 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       853 unless defined $p->{$me};
1208              
1209 63         212 return $p->{$me};
1210             }
1211              
1212             sub _is_dev_version {
1213 199     199   697 my ($self) = @_;
1214 199         943 my $dist_version = $self->dist_version;
1215 199         575 my $version_obj = eval { version->new( $dist_version ) };
  199         2283  
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       2195 return $@ ? 0 : $version_obj->is_alpha;
1220             }
1221              
1222 67     67 0 16736 sub dist_author { shift->_pod_parse('author') }
1223 57     57 0 262 sub dist_abstract { shift->_pod_parse('abstract') }
1224              
1225             sub _pod_parse {
1226 124     124   470 my ($self, $part) = @_;
1227 124         371 my $p = $self->{properties};
1228 124         343 my $member = "dist_$part";
1229 124 100       1752 return $p->{$member} if defined $p->{$member};
1230              
1231 57 50       812 my $docfile = $self->_main_docfile
1232             or return;
1233 57 50       2983 open(my $fh, '<', $docfile)
1234             or return;
1235              
1236 57         15418 require Module::Build::PodParser;
1237 57         732 my $parser = Module::Build::PodParser->new(fh => $fh);
1238 57         282 my $method = "get_$part";
1239 57         376 return $p->{$member} = $parser->$method();
1240             }
1241              
1242             sub version_from_file { # Method provided for backwards compatibility
1243 1     1 0 2723 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   294341 for my $f ( map glob($_), @{ $unlink_list_for_pid{$$} || [] } ) {
  69         4643  
1264 18 0       480 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 6161 my $self = shift;
1272 191         1026 my %files = map {$self->localize_file_path($_), 1} @_;
  193         2573  
1273 191         3741 $self->{phash}{cleanup}->write(\%files);
1274             }
1275              
1276             sub cleanup {
1277 51     51 0 2160 my $self = shift;
1278 51         790 my $all = $self->{phash}{cleanup}->read;
1279 51 100       4238 return wantarray ? sort keys %$all : keys %$all;
1280             }
1281              
1282             sub config_file {
1283 492     492 0 1826 my $self = shift;
1284 492 50       2519 return unless -d $self->config_dir;
1285 492         3991 return File::Spec->catfile($self->config_dir, @_);
1286             }
1287              
1288             sub read_config {
1289 466     466 0 3304 my ($self) = @_;
1290              
1291 466 50       5064 my $file = $self->config_file('build_params')
1292             or die "Can't find 'build_params' in " . $self->config_dir;
1293 466 50       23207 open(my $fh, '<', $file) or die "Can't read '$file': $!";
1294 466         1962 my $ref = eval do {local $/; <$fh>};
  466         2753  
  466         223387  
1295 466 50       4284 die if $@;
1296 466         7253 close $fh;
1297 466         2317 my $c;
1298 466         9516 ($self->{args}, $c, $self->{properties}) = @$ref;
1299 466         9995 $self->{config} = Module::Build::Config->new(values => $c);
1300             }
1301              
1302             sub has_config_data {
1303 61     61 0 284 my $self = shift;
1304 61         888 return scalar grep $self->{phash}{$_}->has_data(), qw(config_data features auto_features);
1305             }
1306              
1307             sub _write_data {
1308 18     18   103 my ($self, $filename, $data) = @_;
1309              
1310 18         122 my $file = $self->config_file($filename);
1311 18 50       1977 open(my $fh, '>', $file) or die "Can't create '$file': $!";
1312 18 100       185 unless (ref($data)) { # e.g. magicnum
1313 6         138 print $fh $data;
1314 6         563 return;
1315             }
1316              
1317 12         65 print {$fh} Module::Build::Dumper->_data_dump($data);
  12         292  
1318 12         9530 close $fh;
1319             }
1320              
1321             sub write_config {
1322 6     6 0 47 my ($self) = @_;
1323              
1324 6         532 File::Path::mkpath($self->{properties}{config_dir});
1325 6 50       156 -d $self->{properties}{config_dir} or die "Can't mkdir $self->{properties}{config_dir}: $!";
1326              
1327 6         27 my @items = @{ $self->prereq_action_types };
  6         177  
1328 6         75 $self->_write_data('prereqs', { map { $_, $self->$_() } @items });
  30         323  
1329 6         194 $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         307 $self->_write_data('magicnum', $self->magic_number(int rand 1_000_000));
1333              
1334 6         111 $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 309 my $self = shift;
1364              
1365 69         339 my $bundle_inc = $self->{properties}{bundle_inc};
1366 69         281 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       2072 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 294 my ($self) = @_;
1410 80         867 my $features = $self->auto_features;
1411              
1412 80 50       446 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 76 my ($self) = @_;
1481 11         68 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     127 if ( $self->dist_name ne 'Module-Build'
      100        
1486             && $self->auto_configure_requires
1487             && ! exists $p->{configure_requires}{'Module::Build'}
1488             ) {
1489 7         216 (my $ver = $VERSION) =~ s/^(\d+\.\d\d).*$/$1/; # last major release only
1490 7         151 $self->log_warn(<<EOM);
1491             Module::Build was not found in configure_requires! Adding it now
1492             automatically as: configure_requires => { 'Module::Build' => $ver }
1493             EOM
1494 7         144 $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       455 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         54 return;
1509             }
1510              
1511             # Automatically detect and add prerequisites based on configuration
1512             sub auto_require {
1513 80     80 0 407 my ($self) = @_;
1514 80         305 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         1551 my $xs_files = $self->find_xs_files;
1519 80 50       433 if ( ! defined $p->{needs_compiler} ) {
1520 80 50 33     12177 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     1701 ( ref($self->c_source) ne 'ARRAY' || @{$self->c_source} )
1526             )
1527             );
1528             }
1529             }
1530 80 50       357 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       960 if ( $self->share_dir ) {
1543 0         0 $self->_add_prereq( 'requires', 'File::ShareDir', '1.00' );
1544             }
1545              
1546 80         237 return;
1547             }
1548              
1549             sub _add_prereq {
1550 7     7   102 my ($self, $type, $module, $version) = @_;
1551 7         48 my $p = $self->{properties};
1552 7 50       46 $version = 0 unless defined $version;
1553 7 50       55 if ( exists $p->{$type}{$module} ) {
1554 0 0       0 return if $self->compare_versions( $version, '<=', $p->{$type}{$module} );
1555             }
1556 7         101 $self->log_verbose("Adding to $type\: $module => $version\n");
1557 7         32 $p->{$type}{$module} = $version;
1558 7         33 return 1;
1559             }
1560              
1561             sub prereq_failures {
1562 107     107 0 12572 my ($self, $info) = @_;
1563              
1564 107         233 my @types = @{ $self->prereq_action_types };
  107         314  
1565 107   100     500 $info ||= {map {$_, $self->$_()} @types};
  125         295  
1566              
1567 107         255 my $out;
1568              
1569 107         306 foreach my $type (@types) {
1570 535         853 my $prereqs = $info->{$type};
1571 535         1381 for my $modname ( keys %$prereqs ) {
1572 45         169 my $spec = $prereqs->{$modname};
1573 45         430 my $status = $self->check_installed_status($modname, $spec);
1574              
1575 45 100       286 if ($type =~ /^(?:\w+_)?conflicts$/) {
    100          
1576 1 50       19 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       65 next if $status->{ok};
1582 5 50 33     135 $status->{message} = (!ref($status->{have}) && $status->{have} eq '<none>'
1583             ? "$modname is not installed"
1584             : "$modname ($status->{have}) is installed, but we prefer to have $spec");
1585             } else {
1586 39 100       131 next if $status->{ok};
1587             }
1588              
1589 34         154 $out->{$type}{$modname} = $status;
1590             }
1591             }
1592              
1593 107         991 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   210 my $self = shift;
1599 82         198 my %prereqs;
1600 82         192 foreach my $type ( @{ $self->prereq_action_types } ) {
  82         615  
1601 410 50       2788 if ( $self->can( $type ) ) {
1602 410   50     1378 my $prereq = $self->$type() || {};
1603 410 100       1187 $prereqs{$type} = $prereq if %$prereq;
1604             }
1605             }
1606 82         283 return \%prereqs;
1607             }
1608              
1609             sub check_prereq {
1610 80     80 0 212 my $self = shift;
1611              
1612             # Check to see if there are any prereqs to check
1613 80         594 my $info = $self->_enum_prereqs;
1614 80 50       259 return 1 unless $info;
1615              
1616 80         473 my $log_text = "Checking prerequisites...\n";
1617              
1618 80         819 my $failures = $self->prereq_failures($info);
1619              
1620 80 100       372 if ( $failures ) {
1621 10         130 $self->log_warn($log_text);
1622 10         285 for my $type ( @{ $self->prereq_action_types } ) {
  10         60  
1623 50         155 my $prereqs = $failures->{$type};
1624 50 100       185 $self->log_warn(" ${type}:\n") if keys %$prereqs;
1625 50         200 for my $module ( sort keys %$prereqs ) {
1626 10         20 my $status = $prereqs->{$module};
1627 10 100       80 my $prefix = ($type =~ /^(?:\w+_)?recommends$/) ? "* " : "! ";
1628 10         70 $self->log_warn(" $prefix $status->{message}\n");
1629             }
1630             }
1631 10         195 return 0;
1632             } else {
1633 70         524 $self->log_verbose($log_text . "Looks good\n\n");
1634 70         874 return 1;
1635             }
1636             }
1637              
1638             sub perl_version {
1639 7     7 0 374 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       356 return $^V ? $self->perl_version_to_float(sprintf "%vd", $^V) : $];
1644             }
1645              
1646             sub perl_version_to_float {
1647 7     7 0 34 my ($self, $version) = @_;
1648 7 50       146 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   578 my ($self, $spec) = @_;
1656              
1657 148 50       691 return ">= 0" if not defined $spec;
1658 148 100       2204 if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
1659 143         1052 return (">= $spec");
1660             } else {
1661 5         65 return split /\s*,\s*/, $spec;
1662             }
1663             }
1664              
1665             sub try_require {
1666 21     21 0 266 my ($self, $modname, $spec) = @_;
1667 21 50       458 my $status = $self->check_installed_status($modname, defined($spec) ? $spec : 0);
1668 21 50       104 return unless $status->{ok};
1669 21         78 my $path = $modname;
1670 21         222 $path =~ s{::}{/}g;
1671 21         78 $path .= ".pm";
1672 21 100       126 if ( defined $INC{$path} ) {
    50          
1673 12         121 return 1;
1674             }
1675             elsif ( exists $INC{$path} ) { # failed before, don't try again
1676 0         0 return;
1677             }
1678             else {
1679 9         566 return eval "require $modname";
1680             }
1681             }
1682              
1683             sub check_installed_status {
1684 154     154 0 12818 my ($self, $modname, $spec) = @_;
1685 154         920 my %status = (need => $spec);
1686              
1687 154 50       884 if ($modname eq 'perl') {
    100          
1688 0         0 $status{have} = $self->perl_version;
1689              
1690 293     293   3454 } elsif (eval { no strict; $status{have} = ${"${modname}::VERSION"} }) {
  293         813  
  293         1460171  
  154         312  
  154         3496  
1691             # Don't try to load if it's already loaded
1692              
1693             } else {
1694 86         1190 my $pm_info = Module::Metadata->new_from_module( $modname );
1695 86 100       869977 unless (defined( $pm_info )) {
1696 6         140 @status{ qw(have message) } = ('<none>', "$modname is not installed");
1697 6         87 return \%status;
1698             }
1699              
1700 80         217 $status{have} = eval { $pm_info->version() };
  80         432  
1701 80 50 66     2051 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         1557 my @conditions = $self->_parse_conditions($spec);
1708              
1709 148         670 foreach (@conditions) {
1710 148 50       1739 my ($op, $version) = /^\s* (<=?|>=?|==|!=) \s* ([\w.]+) \s*$/x
1711             or die "Invalid prerequisite condition '$_' for $modname";
1712              
1713 148 50       652 $version = $self->perl_version_to_float($version)
1714             if $modname eq 'perl';
1715              
1716 148 100 100     1220 next if $op eq '>=' and !$version; # Module doesn't have to actually define a $VERSION
1717              
1718 60 100       721 unless ($self->compare_versions( $status{have}, $op, $version )) {
1719 29         222 $status{message} = "$modname ($status{have}) is installed, but we need version $op $version";
1720 29         125 return \%status;
1721             }
1722             }
1723              
1724 119         617 $status{ok} = 1;
1725 119         707 return \%status;
1726             }
1727              
1728             sub compare_versions {
1729 61     61 0 2393 my $self = shift;
1730 61         194 my ($v1, $op, $v2) = @_;
1731             $v1 = version->new($v1)
1732 61 100       146 unless eval { $v1->isa('version') };
  61         912  
1733              
1734 61         275 my $eval_str = "\$v1 $op \$v2";
1735 61         4164 my $result = eval $eval_str;
1736 61 50       458 $self->log_warn("error comparing versions: '$eval_str' $@") if $@;
1737              
1738 61         317 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 '<none>';
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 59 my $self = shift;
1762 15         70 foreach (@_) {
1763 15         273 my $current_mode = (stat $_)[2];
1764 15         410 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   19550 my $self = shift;
1781              
1782 4286         10721 my %seen;
1783 4286         23225 $seen{$_}++ foreach $self->_default_INC;
1784 4240         294213 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   12791 my $self = shift;
1792 4749 100       104543 return @default_inc if @default_inc;
1793              
1794 242         8827 local $ENV{PERL5LIB}; # this is not considered part of the default.
1795              
1796 242 100       8038 my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
1797              
1798 242         2803 my @inc = $self->_backticks($perl, '-le', 'print for @INC');
1799 196         9364 chomp @inc;
1800              
1801 196         30217 return @default_inc = @inc;
1802             }
1803             }
1804              
1805             sub print_build_script {
1806 6     6 0 26 my ($self, $fh) = @_;
1807              
1808 6         75 my $build_package = $self->build_class;
1809              
1810 6         36 my $closedata="";
1811              
1812 6         28 my $config_requires;
1813 6 50       54 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     62 $config_requires ||= 0;
1818              
1819 6         25 my %q = map {$_, $self->$_()} qw(config_dir base_dir);
  12         127  
1820              
1821 6 50       56 $q{base_dir} = Win32::GetShortPathName($q{base_dir}) if $self->is_windowsish;
1822              
1823 6         195 $q{magic_numfile} = $self->config_file('magicnum');
1824              
1825 6         67 my @myINC = $self->_added_to_INC;
1826 6         33 for (@myINC, values %q) {
1827 69 50       162 $_ = File::Spec->canonpath( $_ ) unless $self->is_vmsish;
1828 69         878 s/([\\\'])/\\$1/g;
1829             }
1830              
1831 6         88 my $quoted_INC = join ",\n", map " '$_'", @myINC;
1832 6         93 my $shebang = $self->_startperl;
1833 6         43 my $magic_number = $self->magic_number;
1834              
1835 6 50       38 my $dot_in_inc_code = $INC[-1] eq '.' ? <<'END' : '';
1836             if ($INC[-1] ne '.') {
1837             push @INC, '.';
1838             }
1839             END
1840 6         192 print $fh <<EOF;
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 51 my ($self) = @_;
1905              
1906 6         61 my ($meta_obj, $mymeta);
1907 6         113 my @metafiles = ( $self->metafile2, $self->metafile, );
1908 6         67 my @mymetafiles = ( $self->mymetafile2, $self->mymetafile, );
1909              
1910             # cleanup old MYMETA
1911 6         67 for my $f ( @mymetafiles ) {
1912 12 50       103 if ( $self->delete_filetree($f) ) {
1913 12         131 $self->log_verbose("Removed previous '$f'\n");
1914             }
1915             }
1916              
1917             # Try loading META.json or META.yml
1918 6 50       211 if ( $self->try_require("CPAN::Meta", "2.142060") ) {
1919 6         181342 for my $file ( @metafiles ) {
1920 12 50       340 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       72 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         136 $mymeta_obj = $self->_get_meta_object(quiet => 0, dynamic => 0, fatal => 1, auto => 0);
1941             }
1942              
1943 6         126 my @created = $self->_write_meta_files( $mymeta_obj, 'MYMETA' );
1944              
1945 6 50       27 $self->log_warn("Could not create MYMETA files\n")
1946             unless @created;
1947              
1948 6         88 return 1;
1949             }
1950              
1951             sub create_build_script {
1952 6     6 0 4233 my ($self) = @_;
1953              
1954 6         161 $self->write_config;
1955 6         218 $self->create_mymeta;
1956              
1957             # Create Build
1958 6         86 my ($build_script, $dist_name, $dist_version)
1959             = map $self->$_(), qw(build_script dist_name dist_version);
1960              
1961 6 50       61 if ( $self->delete_filetree($build_script) ) {
1962 6         54 $self->log_verbose("Removed previous script '$build_script'\n");
1963             }
1964              
1965 6         73 $self->log_info("Creating new '$build_script' script for ",
1966             "'$dist_name' version '$dist_version'\n");
1967 6 50       439 open(my $fh, '>', $build_script) or die "Can't create '$build_script': $!";
1968 6         201 $self->print_build_script($fh);
1969 6         217 close $fh;
1970              
1971 6         95 $self->make_executable($build_script);
1972              
1973 6         46 return 1;
1974             }
1975              
1976             sub check_manifest {
1977 80     80 0 341 my $self = shift;
1978 80 100       3272 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         36101 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
1984 78         209627 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
1985              
1986 78         1079 $self->log_verbose("Checking whether your kit is complete...\n");
1987 78 50       711 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         103936 $self->log_verbose("Looks good\n\n");
1993             }
1994             }
1995              
1996             sub dispatch {
1997 130     130 0 200384 my $self = shift;
1998 130         1595 local $self->{_completed_actions} = {};
1999              
2000 130 50       1302 if (@_) {
2001 130         1132 my ($action, %p) = @_;
2002 130 100       1206 my $args = $p{args} ? delete($p{args}) : {};
2003              
2004 130         935 local $self->{invoked_action} = $action;
2005 130         512 local $self->{args} = {%{$self->{args}}, %$args};
  130         1327  
2006 130         579 local $self->{properties} = {%{$self->{properties}}, %p};
  130         22470  
2007 130         2398 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   2691 my ($self, $action) = @_;
2017              
2018 482 100       4762 return if $self->{_completed_actions}{$action}++;
2019              
2020 398         2033 local $self->{action} = $action;
2021 398         2308 my $method = $self->can_action( $action );
2022 398 50       1856 die "No action '$action' defined, try running the 'help' action.\n" unless $method;
2023 398         3725 $self->log_debug("Starting ACTION_$action\n");
2024 398         4688 my $rc = $self->$method();
2025 392         4100 $self->log_debug("Finished ACTION_$action\n");
2026 392         25617 return $rc;
2027             }
2028              
2029             sub can_action {
2030 398     398 0 1338 my ($self, $action) = @_;
2031 398         7475 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 3618 my $self = shift;
2037 561         3615 my (@argv) = @_;
2038              
2039             # XXX is it even valid to call this as a class method?
2040 561 100       8418 return({}, @argv) unless(ref($self)); # no object
2041              
2042 447         11287 my $specs = $self->get_options;
2043 447 100 66     15215 return({}, @argv) unless($specs and %$specs); # no user options
2044              
2045 10         9373 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         57912 my @specs;
2051 10         162 my $args = {};
2052             # Construct the specifications for GetOptions.
2053 10         434 foreach my $k (sort keys %$specs) {
2054 40         243 my $v = $specs->{$k};
2055             # Throw an error if specs conflict with our own.
2056 40 50       488 die "Option specification '$k' conflicts with a " . ref $self
2057             . " option of the same name"
2058             if $self->valid_property($k);
2059 40 100       428 push @specs, $k . (defined $v->{type} ? $v->{type} : '');
2060 40 50       234 push @specs, $v->{store} if exists $v->{store};
2061 40 100       256 $args->{$k} = $v->{default} if exists $v->{default};
2062             }
2063              
2064 10         151 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       133 if ( @specs ) {
2069 10         237 Getopt::Long::Configure('pass_through');
2070 10         981 Getopt::Long::GetOptions($args, @specs);
2071             }
2072              
2073 10         6353 return $args, @ARGV;
2074             }
2075              
2076             sub unparse_args {
2077 538     538 0 48040 my ($self, $args) = @_;
2078 538         2617 my @out;
2079 538         6949 foreach my $k (sort keys %$args) {
2080 599         2565 my $v = $args->{$k};
2081 277         1655 push @out, (ref $v eq 'HASH' ? map {+"--$k", "$_=$v->{$_}"} sort keys %$v :
2082 599 100       7582 ref $v eq 'ARRAY' ? map {+"--$k", $_} @$v :
  0 100       0  
2083             ("--$k", $v));
2084             }
2085 538         13752 return @out;
2086             }
2087              
2088             sub args {
2089 56     56 0 4058 my $self = shift;
2090 56 100       303 return wantarray ? %{ $self->{args} } : $self->{args} unless @_;
  4 100       84  
2091 48         113 my $key = shift;
2092 48 50       114 $self->{args}{$key} = shift if @_;
2093 48         389 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   1020 my $self = shift;
2100 693         925 my $opt = shift;
2101              
2102 693         1414 (my $tr_opt = $opt) =~ tr/-/_/;
2103              
2104 693 50       93751 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         2052 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   1106 my ($self, $args, $key, $val) = @_;
2130              
2131 404         1224 $key = $self->_translate_option($key);
2132              
2133 404 100 66     1979 if ( exists $args->{$key} and not $singular_argument{$key} ) {
2134 58 50       292 $args->{$key} = [ $args->{$key} ] unless ref $args->{$key};
2135 58         120 push @{$args->{$key}}, $val;
  58         355  
2136             } else {
2137 346         1518 $args->{$key} = $val;
2138             }
2139             }
2140              
2141             # decide whether or not an option requires/has an operand
2142             sub _optional_arg {
2143 289     289   594 my $self = shift;
2144 289         726 my $opt = shift;
2145 289         485 my $argv = shift;
2146              
2147 289         736 $opt = $self->_translate_option($opt);
2148              
2149 289         972 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       31106 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       1992 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         8 my $arg = 1;
2180 1 50 33     27 $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 67944 my $self = shift;
2187              
2188 561         13198 (my $args, @_) = $self->cull_options(@_);
2189 561         4440 my %args = %$args;
2190              
2191 561         13380 my $opt_re = qr/[\w\-]+/;
2192              
2193 561         3200 my ($action, @argv);
2194 561         6008 while (@_) {
2195 404         949 local $_ = shift;
2196 404 100 0     10584 if ( /^(?:--)?($opt_re)=(.*)$/ ) {
    50          
    0          
2197 115         897 $self->_read_arg(\%args, $1, $2);
2198             } elsif ( /^--($opt_re)$/ ) {
2199 289         2810 my($opt, $arg) = $self->_optional_arg($1, \@_);
2200 289         846 $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         7231 $args{ARGV} = \@argv;
2208              
2209 561         5648 for ('extra_compiler_flags', 'extra_linker_flags') {
2210 1122 50       10994 $args{$_} = [ $self->split_like_shell($args{$_}) ] if exists $args{$_};
2211             }
2212              
2213             # Convert to arrays
2214 561         3198 for ('include_dirs') {
2215 561 50 33     4621 $args{$_} = [ $args{$_} ] if exists $args{$_} && !ref $args{$_}
2216             }
2217              
2218             # Hashify these parameters
2219 561         9579 for ($self->hash_properties, 'config') {
2220 8981 100       23979 next unless exists $args{$_};
2221 58         580 my %hash;
2222 58   50     298 $args{$_} ||= [];
2223 58 50       916 $args{$_} = [ $args{$_} ] unless ref $args{$_};
2224 58         231 foreach my $arg ( @{$args{$_}} ) {
  58         179  
2225 116 50       5816 $arg =~ /($opt_re)=(.*)/
2226             or die "Malformed '$_' argument: '$arg' should be something like 'foo=bar'";
2227 116         648 $hash{$1} = $2;
2228             }
2229 58         238 $args{$_} = \%hash;
2230             }
2231              
2232             # De-tilde-ify any path parameters
2233 561         6844 for my $key (qw(prefix install_base destdir)) {
2234 1683 50       7301 next if !defined $args{$key};
2235 0         0 $args{$key} = $self->_detildefy($args{$key});
2236             }
2237              
2238 561         3134 for my $key (qw(install_path)) {
2239 561 50       3976 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       3200 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         7349 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   3679 my( $self, $opts1, $opts2 ) = @_;
2270              
2271 812   100     3091 $opts1 ||= {};
2272 812   50     2703 $opts2 ||= {};
2273 812         4053 my %new_opts = %$opts1;
2274 812         5114 while (my ($key, $val) = each %$opts2) {
2275 1665 50       3060 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         4890 $new_opts{$key} = $val
2283             }
2284             }
2285              
2286 812         5438 return %new_opts;
2287             }
2288              
2289             # Look for a home directory on various systems.
2290             sub _home_dir {
2291 443     443   1707 my @home_dirs;
2292 443 50       7712 push( @home_dirs, $ENV{HOME} ) if $ENV{HOME};
2293              
2294             push( @home_dirs, File::Spec->catpath($ENV{HOMEDRIVE}, $ENV{HOMEPATH}, '') )
2295 443 0 33     4104 if $ENV{HOMEDRIVE} && $ENV{HOMEPATH};
2296              
2297 443         5852 my @other_home_envs = qw( USERPROFILE APPDATA WINDIR SYS$LOGIN );
2298 443         4550 push( @home_dirs, map $ENV{$_}, grep $ENV{$_}, @other_home_envs );
2299              
2300 443         18141 my @real_home_dirs = grep -d, @home_dirs;
2301              
2302 443 50       6913 return wantarray ? @real_home_dirs : shift( @real_home_dirs );
2303             }
2304              
2305             sub _find_user_config {
2306 443     443   1787 my $self = shift;
2307 443         3570 my $file = shift;
2308 443         6351 foreach my $dir ( $self->_home_dir ) {
2309 436         18556 my $path = File::Spec->catfile( $dir, $file );
2310 436 50       9453 return $path if -e $path;
2311             }
2312 443         2521 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 5422 my( $self, $action ) = @_;
2319              
2320 447 100       10260 return () unless $self->use_rcfile;
2321              
2322 443         1736 my $modulebuildrc;
2323 443 50 33     6658 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         5299 $modulebuildrc = $self->_find_user_config( '.modulebuildrc' );
2334 443 50       4470 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 4855 my( $self, $action, %cmdline_opts ) = @_;
2381 447   100     17008 my %rc_opts = $self->read_modulebuildrc( $action || $self->{action} || 'build' );
2382 447         5881 my %new_opts = $self->_merge_arglist( \%cmdline_opts, \%rc_opts );
2383 447         3731 $self->merge_args( $action, %new_opts );
2384             }
2385              
2386             sub merge_args {
2387 894     894 0 5638 my ($self, $action, %args) = @_;
2388 894 50       4301 $self->{action} = $action if defined $action;
2389              
2390 894         4128 my %additive = map { $_ => 1 } $self->hash_properties;
  13420         59749  
2391              
2392             # Extract our 'properties' from $cmd_args, the rest are put in 'args'.
2393 894         11258 while (my ($key, $val) = each %args) {
2394 922 100       8212 $self->{phash}{runtime_params}->access( $key => $val )
2395             if $self->valid_property($key);
2396              
2397 922 50       7458 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       6076 $self->{args} ;
    100          
2403              
2404 922 100       4285 if ($additive{$key}) {
2405 2         19 $add_to->{$_} = $val->{$_} foreach keys %$val;
2406             } else {
2407 920         9834 $add_to->{$key} = $val;
2408             }
2409             }
2410             }
2411             }
2412              
2413             sub cull_args {
2414 447     447 0 4743 my $self = shift;
2415 447         4607 my @arg_list = @_;
2416             unshift @arg_list, $self->split_like_shell($ENV{PERL_MB_OPT})
2417 447 100       8976 if $ENV{PERL_MB_OPT};
2418 447         12686 my ($args, $action) = $self->read_args(@arg_list);
2419 447         10130 $self->merge_args($action, %$args);
2420 447         9789 $self->merge_modulebuildrc( $action, %$args );
2421             }
2422              
2423             sub super_classes {
2424 181     181 0 367 my ($self, $class, $seen) = @_;
2425 181   66     703 $class ||= ref($self) || $self;
      66        
2426 181   100     618 $seen ||= {};
2427              
2428 293     293   2765 no strict 'refs';
  293         936  
  293         38884  
2429 181         300 my @super = grep {not $seen->{$_}++} $class, @{ $class . '::ISA' };
  322         1015  
  181         870  
2430 181         611 return @super, map {$self->super_classes($_,$seen)} @super;
  141         378  
2431             }
2432              
2433             sub known_actions {
2434 27     27 0 4034 my ($self) = @_;
2435              
2436 27         86 my %actions;
2437 293     293   2640 no strict 'refs';
  293         1068  
  293         3623871  
2438              
2439 27         241 foreach my $class ($self->super_classes) {
2440 92         168 foreach ( keys %{ $class . '::' } ) {
  92         14451  
2441 12635 100       26627 $actions{$1}++ if /^ACTION_(\w+)/;
2442             }
2443             }
2444              
2445 27 100       444 return wantarray ? sort keys %actions : \%actions;
2446             }
2447              
2448             sub get_action_docs {
2449 15     15 0 18148 my ($self, $action) = @_;
2450 15         95 my $actions = $self->known_actions;
2451 15 100       98 die "No known action '$action'" unless $actions->{$action};
2452              
2453 13         44 my ($files_found, @docs) = (0);
2454 13         48 foreach my $class ($self->super_classes) {
2455 49         260 (my $file = $class) =~ s{::}{/}g;
2456             # NOTE: silently skipping relative paths if any chdir() happened
2457 49 50       203 $file = $INC{$file . '.pm'} or next;
2458 49 50       1924 open(my $fh, '<', $file) or next;
2459 49         138 $files_found++;
2460              
2461             # Code below modified from /usr/bin/perldoc
2462              
2463             # Skip to ACTIONS section
2464 49         82 local $_;
2465 49         962 while (<$fh>) {
2466 77491 100       161767 last if /^=head1 ACTIONS\s/;
2467             }
2468              
2469             # Look for our action and determine the style
2470 49         110 my $style;
2471 49         315 while (<$fh>) {
2472 5671 100       10444 last if /^=head1 /;
2473              
2474             # only item and head2 are allowed (3&4 are not in 5.005)
2475 5661 100       19488 if(/^=(item|head2)\s+\Q$action\E\b/) {
2476 11         38 $style = $1;
2477 11         40 push @docs, $_;
2478 11         37 last;
2479             }
2480             }
2481 49 100       593 $style or next; # not here
2482              
2483             # and the content
2484 11 100       37 if($style eq 'item') {
2485 8         21 my ($found, $inlist) = (0, 0);
2486 8         50 while (<$fh>) {
2487 64 100       145 if (/^=(item|back)/) {
2488 8 50       207 last unless $inlist;
2489             }
2490 56         97 push @docs, $_;
2491 56 50       103 ++$inlist if /^=over/;
2492 56 50       136 --$inlist if /^=back/;
2493             }
2494             }
2495             else { # head2 style
2496             # stop at anything equal or greater than the found level
2497 3         25 while (<$fh>) {
2498 18 100       99 last if(/^=(?:head[12]|cut)/);
2499 15         32 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       60 unless ($files_found) {
2507 0         0 $@ = "Couldn't find any documentation to search";
2508 0         0 return;
2509             }
2510 13 100       32 unless (@docs) {
2511 3         23 $@ = "Couldn't find any docs for action '$action'";
2512 3         48 return;
2513             }
2514              
2515 10         154 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 <<EOF;
2597              
2598             Usage: $0 <action> --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 <action>` 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 28 my ($self) = @_;
2640              
2641 2         13 my @types;
2642 2         52 for my $action (grep { $_ ne 'all' } $self->get_test_types) {
  4         35  
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         12 push(@types, $action);
2649             #$self->_call_action( "test$action" );
2650             }
2651 2         29 $self->generic_test(types => ['default', @types]);
2652             }
2653              
2654             sub get_test_types {
2655 2     2 0 9 my ($self) = @_;
2656              
2657 2         8 my $t = $self->{properties}->{test_types};
2658 2 50       47 return ( defined $t ? ( wantarray ? sort keys %$t : keys %$t ) : () );
    50          
2659             }
2660              
2661              
2662             sub ACTION_test {
2663 23     23 0 295 my ($self) = @_;
2664 23         514 $self->generic_test(type => 'default');
2665             }
2666              
2667             sub generic_test {
2668 32     32 0 344 my $self = shift;
2669 32 50       320 (@_ % 2) and croak('Odd number of elements in argument hash');
2670 32         468 my %args = @_;
2671              
2672 32         163 my $p = $self->{properties};
2673              
2674             my @types = (
2675             (exists($args{type}) ? $args{type} : ()),
2676 32 100       562 (exists($args{types}) ? @{$args{types}} : ()),
  2 100       19  
2677             );
2678 32 50       258 @types or croak "need some types of tests to check";
2679              
2680             my %test_types = (
2681             default => $p->{test_file_exts},
2682 32 100       444 (defined($p->{test_types}) ? %{$p->{test_types}} : ()),
  9         103  
2683             );
2684              
2685 32         373 for my $type (@types) {
2686             croak "$type not defined in test_types!"
2687 36 50       337 unless defined $test_types{ $type };
2688             }
2689              
2690             # we use local here because it ends up two method calls deep
2691 32 100       191 local $p->{test_file_exts} = [ map { ref $_ ? @$_ : $_ } @test_types{@types} ];
  36         381  
2692 32         749 $self->depends_on('code');
2693              
2694             # Protect others against our @INC changes
2695 32         1350 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         245 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       285 @INC = grep {ref() || -d} @INC if @INC > 100;
  0 50       0  
2704              
2705 32         401 $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 182 my $self = shift;
2712              
2713 32         379 my $tests = $self->find_test_files;
2714              
2715 32         558 local $ENV{PERL_DL_NONLAZY} = 1;
2716              
2717 32 50       374 if(@$tests) {
2718 32         430 my $args = $self->tap_harness_args;
2719 32 100 33     287 if($self->use_tap_harness or ($args and %$args)) {
      66        
2720 5         24 my $aggregate = $self->run_tap_harness($tests);
2721 5 50       59 if ( $aggregate->has_errors ) {
2722 0         0 die "Errors in testing. Cannot continue.\n";
2723             }
2724             }
2725             else {
2726 27         367 $self->run_test_harness($tests);
2727             }
2728             }
2729             else {
2730 0         0 $self->log_info("No tests defined.\n");
2731             }
2732              
2733 32         16804618 $self->run_visual_script;
2734             }
2735              
2736             sub run_tap_harness {
2737 5     5 0 18 my ($self, $tests) = @_;
2738              
2739 5         3335 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         4749 %{ $self->tap_harness_args },
  5         18  
2748             })->runtests(@$tests);
2749              
2750 5         2764452 return $aggregate;
2751             }
2752              
2753             sub run_test_harness {
2754 29     29 0 742 my ($self, $tests) = @_;
2755 29         14238 require Test::Harness;
2756              
2757 29   100     506420 local $Test::Harness::verbose = $self->verbose || 0;
2758 29         399 local $Test::Harness::switches = join ' ', $self->harness_switches;
2759              
2760 29         226 Test::Harness::runtests(@$tests);
2761             }
2762              
2763             sub run_visual_script {
2764 32     32 0 404 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       6310 $self->run_perl_script('visual.pl', '-Mblib='.$self->blib)
2768             if -e 'visual.pl';
2769             }
2770              
2771             sub harness_switches {
2772 32     32 0 155 my $self = shift;
2773 32         104 my @res;
2774 32 50       173 push @res, qw(-w -d) if $self->{properties}{debugger};
2775 32 50       178 push @res, '-MDevel::Cover' if $self->{properties}{cover};
2776 32         175 return @res;
2777             }
2778              
2779             sub test_files {
2780 4     4 0 3432 my $self = shift;
2781 4         6 my $p = $self->{properties};
2782 4 100       14 if (@_) {
2783 2 100       20 return $p->{test_files} = (@_ == 1 ? shift : [@_]);
2784             }
2785 2         24 return $self->find_test_files;
2786             }
2787              
2788             sub expand_test_dir {
2789 33     33 0 326 my ($self, $dir) = @_;
2790 33         254 my $exts = $self->{properties}{test_file_exts};
2791              
2792 33 100       360 return sort map { @{$self->rscan_dir($dir, qr{^[^.].*\Q$_\E$})} } @$exts
  5         27  
  5         182  
2793             if $self->recursive_test_files;
2794              
2795 30         165 return sort map { glob File::Spec->catfile($dir, "*$_") } @$exts;
  32         4452  
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 445 my ($self) = @_;
2834              
2835             # All installable stuff gets created in blib/ .
2836             # Create blib/arch to keep blib.pm happy
2837 64         1435 my $blib = $self->blib;
2838 64         866 $self->add_to_cleanup($blib);
2839 64         20266 File::Path::mkpath( File::Spec->catdir($blib, 'arch') );
2840              
2841 64 50       1915 if (my $split = $self->autosplit) {
2842 0 0       0 $self->autosplit_file($_, $blib) for ref($split) ? @$split : ($split);
2843             }
2844              
2845 64         241 foreach my $element (@{$self->build_elements}) {
  64         902  
2846 440         2487 my $method = "process_${element}_files";
2847 440 100       6548 $method = "process_files_by_extension" unless $self->can($method);
2848 440         3579 $self->$method($element);
2849             }
2850              
2851 61         666 $self->depends_on('config_data');
2852             }
2853              
2854             sub ACTION_build {
2855 30     30 0 146 my $self = shift;
2856 30         346 $self->log_info("Building " . $self->dist_name . "\n");
2857 30         597 $self->depends_on('code');
2858 27         206 $self->depends_on('docs');
2859             }
2860              
2861             sub process_files_by_extension {
2862 126     126 0 803 my ($self, $ext) = @_;
2863              
2864 126         704 my $method = "find_${ext}_files";
2865 126 100       1664 my $files = $self->can($method) ? $self->$method() : $self->_find_file_by_type($ext, 'lib');
2866              
2867 126         1312 foreach my $file (sort keys %$files) {
2868 74         556 $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 386 my $self = shift;
2874 64         296 my $p = $self->{properties};
2875 64 50       388 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 469 my $self = shift;
2896 61         982 my $files = $self->_find_share_dir_files;
2897 61 100       541 return unless $files;
2898              
2899             # root for all File::ShareDir paths
2900 2         10 my $share_prefix = File::Spec->catdir($self->blib, qw/lib auto share/);
2901              
2902             # copy all share files to blib
2903 2         17 foreach my $file (sort keys %$files) {
2904             $self->copy_if_modified(
2905 8         91 from => $file, to => File::Spec->catfile( $share_prefix, $files->{$file} )
2906             );
2907             }
2908             }
2909              
2910             sub _find_share_dir_files {
2911 62     62   4921 my $self = shift;
2912 62         1201 my $share_dir = $self->share_dir;
2913 62 100       428 return unless $share_dir;
2914              
2915 3         9 my @file_map;
2916 3 50       34 if ( $share_dir->{dist} ) {
2917 3         96 my $prefix = "dist/".$self->dist_name;
2918 3         64 push @file_map, $self->_share_dir_map( $prefix, $share_dir->{dist} );
2919             }
2920              
2921 3 50       26 if ( $share_dir->{module} ) {
2922 3         9 for my $mod ( sort keys %{ $share_dir->{module} } ) {
  3         13  
2923 3         25 (my $altmod = $mod) =~ s{::}{-}g;
2924 3         8 my $prefix = "module/$altmod";
2925 3         13 push @file_map, $self->_share_dir_map($prefix, $share_dir->{module}{$mod});
2926             }
2927             }
2928              
2929 3         29 return { @file_map };
2930             }
2931              
2932             sub _share_dir_map {
2933 6     6   21 my ($self, $prefix, $list) = @_;
2934 6         9 my %files;
2935 6         34 for my $dir ( @$list ) {
2936 6     27   20 for my $f ( @{ $self->rscan_dir( $dir, sub {-f} )} ) {
  6         104  
  27         1345  
2937 12         198 $f =~ s{\A.*?\Q$dir\E/}{};
2938 12         74 $files{"$dir/$f"} = "$prefix/$f";
2939             }
2940             }
2941 6         35 return %files;
2942             }
2943              
2944             sub process_PL_files {
2945 64     64 0 396 my ($self) = @_;
2946 64         904 my $files = $self->find_PL_files;
2947              
2948 64         992 foreach my $file (sort keys %$files) {
2949 6         31 my $to = $files->{$file};
2950 6 50       145 unless ($self->up_to_date( $file, $to )) {
2951 6 50       95 $self->run_perl_script($file, [], [@$to]) or die "$file failed";
2952 6         310 $self->add_to_cleanup(@$to);
2953             }
2954             }
2955             }
2956              
2957             sub process_xs_files {
2958 64     64 0 256 my $self = shift;
2959 64 100 100     1094 return if $self->pureperl_only && $self->allow_pureperl;
2960 62         540 my $files = $self->find_xs_files;
2961 62 100 100     796 croak 'Can\'t build xs files under --pureperl-only' if %$files && $self->pureperl_only;
2962 60         737 foreach my $from (sort keys %$files) {
2963 19         70 my $to = $files->{$from};
2964 19 100       119 unless ($from eq $to) {
2965 2         33 $self->add_to_cleanup($to);
2966 2         19 $self->copy_if_modified( from => $from, to => $to );
2967             }
2968 19         357 $self->process_xs($to);
2969             }
2970             }
2971              
2972 61     61 0 619 sub process_pod_files { shift()->process_files_by_extension(shift()) }
2973 64     64 0 858 sub process_pm_files { shift()->process_files_by_extension(shift()) }
2974              
2975             sub process_script_files {
2976 61     61 0 263 my $self = shift;
2977 61         563 my $files = $self->find_script_files;
2978 61 100       613 return unless keys %$files;
2979              
2980 14         111 my $script_dir = File::Spec->catdir($self->blib, 'script');
2981 14         2181 File::Path::mkpath( $script_dir );
2982              
2983 14         170 foreach my $file (sort keys %$files) {
2984 17 100       137 my $result = $self->copy_if_modified($file, $script_dir, 'flatten') or next;
2985 9 50       345 $self->fix_shebang_line($result) unless $self->is_vmsish;
2986 9         227 $self->make_executable($result);
2987             }
2988             }
2989              
2990             sub find_PL_files {
2991 64     64 0 266 my $self = shift;
2992 64 100       424 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       74 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         9 my %out;
3002 3         38 while (my ($file, $to) = each %$files) {
3003 5 100       48 $out{ $self->localize_file_path($file) } = [ map $self->localize_file_path($_),
3004             ref $to ? @$to : ($to) ];
3005             }
3006 3         22 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       1173 return unless -d 'lib';
3014             return {
3015 1         12 map {$_, [/^(.*)\.PL$/i ]}
3016 60         268 @{ $self->rscan_dir('lib', $self->file_qr('\.PL$')) }
  60         1113  
3017             };
3018             }
3019              
3020 83     83 0 768 sub find_pm_files { shift->_find_file_by_type('pm', 'lib') }
3021 61     61 0 648 sub find_pod_files { shift->_find_file_by_type('pod', 'lib') }
3022 147     147 0 1688 sub find_xs_files { shift->_find_file_by_type('xs', 'lib') }
3023              
3024             sub find_script_files {
3025 61     61 0 243 my $self = shift;
3026 61 50       756 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         546 return { map {$self->localize_file_path($_), $files->{$_}} keys %$files };
  17         101  
3030             }
3031              
3032             # No default location for script files
3033 0         0 return {};
3034             }
3035              
3036             sub find_test_files {
3037 34     34 0 113 my $self = shift;
3038 34         135 my $p = $self->{properties};
3039              
3040 34 100       158 if (my $files = $p->{test_files}) {
3041 2 50       8 $files = [sort keys %$files] if ref $files eq 'HASH';
3042 2 100       17 $files = [map { -d $_ ? $self->expand_test_dir($_) : $_ }
  4         363  
3043             map glob,
3044             $self->split_like_shell($files)];
3045              
3046             # Always given as a Unix file spec.
3047 2         27 return [ map $self->localize_file_path($_), @$files ];
3048              
3049             } else {
3050             # Find all possible tests in t/ or test.pl
3051 32         122 my @tests;
3052 32 50       718 push @tests, 'test.pl' if -e 'test.pl';
3053 32 50 33     1255 push @tests, $self->expand_test_dir('t') if -e 't' and -d _;
3054 32         254 return \@tests;
3055             }
3056             }
3057              
3058             sub _find_file_by_type {
3059 295     295   6181 my ($self, $type, $dir) = @_;
3060              
3061 295 100       2369 if (my $files = $self->{properties}{"${type}_files"}) {
3062             # Always given as a Unix file spec
3063 5         78 return { map $self->localize_file_path($_), %$files };
3064             }
3065              
3066 290 100       4841 return {} unless -d $dir;
3067 114         1361 return { map {$_, $_}
3068             map $self->localize_file_path($_),
3069             grep !/\.\#/,
3070 286         1193 @{ $self->rscan_dir($dir, $self->file_qr("\\.$type\$")) } };
  286         3752  
3071             }
3072              
3073             sub localize_file_path {
3074 683     683 0 8908 my ($self, $path) = @_;
3075 683         10871 return File::Spec->catfile( split m{/}, $path );
3076             }
3077              
3078             sub localize_dir_path {
3079 88     88 0 218 my ($self, $path) = @_;
3080 88         749 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 572 my ($self, @files) = @_;
3085 9 50       79 my $c = ref($self) ? $self->{config} : 'Module::Build::Config';
3086              
3087 9         133 my ($does_shbang) = $c->get('sharpbang') =~ /^\s*\#\!/;
3088 9         119 for my $file (@files) {
3089 9 50       497 open(my $FIXIN, '<', $file) or die "Can't process '$file': $!";
3090 9         153 local $/ = "\n";
3091 9         317 chomp(my $line = <$FIXIN>);
3092 9 100       177 next unless $line =~ s/^\s*\#!\s*//; # Not a shebang file.
3093              
3094 7         59 my ($cmd, $arg) = (split(' ', $line, 2), '');
3095 7 50       90 next unless $cmd =~ /perl/i;
3096 7         33 my $interpreter = $self->{properties}{perl};
3097              
3098 7         62 $self->log_verbose("Changing sharpbang in $file to $interpreter\n");
3099 7         50 my $shb = '';
3100 7 50       64 $shb .= $c->get('sharpbang')."$interpreter $arg\n" if $does_shbang;
3101              
3102 7 50       641 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         50 local $\;
3107 7         69 undef $/; # Was localized above
3108 7         254 print $FIXOUT $shb, <$FIXIN>;
3109 7         97 close $FIXIN;
3110 7         244 close $FIXOUT;
3111              
3112 7 50       367 rename($file, "$file.bak")
3113             or die "Can't rename $file to $file.bak: $!";
3114              
3115 7 50       276 rename("$file.new", $file)
3116             or die "Can't rename $file.new to $file: $!";
3117              
3118 7 50       69 $self->delete_filetree("$file.bak")
3119             or $self->log_warn("Couldn't clean up $file.bak, leaving it there");
3120              
3121 7 50       46 $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 155 my $self = shift;
3172              
3173 28         148 $self->depends_on('code');
3174 28         193 $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   874 my $self = shift;
3183 112         279 my $type = shift;
3184             return ( $self->install_destination($type) &&
3185             ( $self->install_path($type) ||
3186 112 100 100     839 $self->install_sets($self->installdirs)->{$type} )
3187             ) ? 1 : 0;
3188             }
3189              
3190             sub _is_ActivePerl {
3191             # return 0;
3192 29     29   133 my $self = shift;
3193 29 100       214 unless (exists($self->{_is_ActivePerl})) {
3194 8   50     42 $self->{_is_ActivePerl} = (eval { require ActivePerl::DocTools; } || 0);
3195             }
3196 29         224 return $self->{_is_ActivePerl};
3197             }
3198              
3199             sub _is_ActivePPM {
3200             # return 0;
3201 9     9   34 my $self = shift;
3202 9 100       174 unless (exists($self->{_is_ActivePPM})) {
3203 5   50     36 $self->{_is_ActivePPM} = (eval { require ActivePerl::PPM; } || 0);
3204             }
3205 9         106 return $self->{_is_ActivePPM};
3206             }
3207              
3208             sub ACTION_manpages {
3209 28     28 0 121 my $self = shift;
3210              
3211 28 50       441 return unless $self->_mb_feature('manpage_support');
3212              
3213 28         273 $self->depends_on('code');
3214              
3215 28 100       321 my %extra_manify_args = $self->{properties}{'extra_manify_args'} ? %{ $self->{properties}{'extra_manify_args'} } : ();
  1         6  
3216              
3217 28         200 foreach my $type ( qw(bin lib) ) {
3218 56 100 66     975 next unless ( $self->invoked_action eq 'manpages' || $self->_is_default_installable("${type}doc"));
3219 24         172 my $files = $self->_find_pods( $self->{properties}{"${type}doc_dirs"},
3220             exclude => [ $self->file_qr('\.bat$') ] );
3221 24 100       271 next unless %$files;
3222              
3223 18         266 my $sub = $self->can("manify_${type}_pods");
3224 18 50       203 $self->$sub( %extra_manify_args ) if defined( $sub );
3225             }
3226             }
3227              
3228             sub manify_bin_pods {
3229 6     6 0 23 my $self = shift;
3230 6         60 my $section = $self->config('man1ext');
3231 6         79 my %podman_args = (section => $section, @_);
3232              
3233             my $files = $self->_find_pods( $self->{properties}{bindoc_dirs},
3234 6         84 exclude => [ $self->file_qr('\.bat$') ] );
3235 6 50       63 return unless keys %$files;
3236              
3237 6         37 my $mandir = File::Spec->catdir( $self->blib, 'bindoc' );
3238 6         680 File::Path::mkpath( $mandir, 0, oct(777) );
3239              
3240 6         53 require Pod::Man;
3241 6         51 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         225 my $parser = Pod::Man->new( %podman_args );
3245 18         6197 my $manpage = $self->man1page_name( $file ) . '.' .
3246             $self->config( 'man1ext' );
3247 18         199 my $outfile = File::Spec->catfile($mandir, $manpage);
3248 18 100       65 next if $self->up_to_date( $file, $outfile );
3249 8         73 $self->log_verbose("Manifying $file -> $outfile\n");
3250 8 50       24 eval { $parser->parse_from_file( $file, $outfile ); 1 }
  8         103  
  8         25504  
3251             or $self->log_warn("Error creating '$outfile': $@\n");
3252 8         287 $files->{$file} = $outfile;
3253             }
3254             }
3255              
3256             sub manify_lib_pods {
3257 12     12 0 42 my $self = shift;
3258 12         109 my $section = $self->config('man3ext');
3259 12         93 my %podman_args = (section => $section, @_);
3260              
3261 12         59 my $files = $self->_find_pods($self->{properties}{libdoc_dirs});
3262 12 50       93 return unless keys %$files;
3263              
3264 12         56 my $mandir = File::Spec->catdir( $self->blib, 'libdoc' );
3265 12         1222 File::Path::mkpath( $mandir, 0, oct(777) );
3266              
3267 12         122 require Pod::Man;
3268 12         113 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         201 my $parser = Pod::Man->new( %podman_args );
3272 17         5116 my $manpage = $self->man3page_name( $files->{$file} ) . '.' .
3273             $self->config( 'man3ext' );
3274 17         180 my $outfile = File::Spec->catfile( $mandir, $manpage);
3275 17 100       177 next if $self->up_to_date( $file, $outfile );
3276 9         148 $self->log_verbose("Manifying $file -> $outfile\n");
3277 9 50       33 eval { $parser->parse_from_file( $file, $outfile ); 1 }
  9         57  
  9         51000  
3278             or $self->log_warn("Error creating '$outfile': $@\n");
3279 9         321 $files->{$file} = $outfile;
3280             }
3281             }
3282              
3283             sub _find_pods {
3284 52     52   294 my ($self, $dirs, %args) = @_;
3285 52         142 my %files;
3286 52         186 foreach my $spec (@$dirs) {
3287 87         336 my $dir = $self->localize_dir_path($spec);
3288 87 100       1399 next unless -e $dir;
3289              
3290 86         232 FILE: foreach my $file ( @{ $self->rscan_dir( $dir ) } ) {
  86         334  
3291 237         597 foreach my $regexp ( @{ $args{exclude} } ) {
  237         649  
3292 176 50       1121 next FILE if $file =~ $regexp;
3293             }
3294 237         789 $file = $self->localize_file_path($file);
3295 237 100       804 $files{$file} = File::Spec->abs2rel($file, $dir) if $self->contains_pod( $file )
3296             }
3297             }
3298 52         299 return \%files;
3299             }
3300              
3301             sub contains_pod {
3302 246     246 0 4368 my ($self, $file) = @_;
3303 246 100       11161 return '' unless -T $file; # Only look at text files
3304              
3305 117 50       3833 open(my $fh, '<', $file ) or die "Can't open $file: $!";
3306 117         1717 while (my $line = <$fh>) {
3307 757 100       9708 return 1 if $line =~ /^\=(?:head|pod|item)/;
3308             }
3309              
3310 30         507 return '';
3311             }
3312              
3313             sub ACTION_html {
3314 28     28 0 114 my $self = shift;
3315              
3316 28 50       141 return unless $self->_mb_feature('HTML_support');
3317              
3318 28         328 $self->depends_on('code');
3319              
3320 28         288 foreach my $type ( qw(bin lib) ) {
3321 56 100 66     325 next unless ( $self->invoked_action eq 'html' || $self->_is_default_installable("${type}html"));
3322 4         44 $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 39 my $self = shift;
3331 10         31 my $type = shift;
3332 10   66     113 my $htmldir = shift || File::Spec->catdir($self->blib, "${type}html");
3333              
3334 10         103 $self->add_to_cleanup('pod2htm*');
3335              
3336 10         114 my $pods = $self->_find_pods( $self->{properties}{"${type}doc_dirs"},
3337             exclude => [ $self->file_qr('\.(?:bat|com|html)$') ] );
3338 10 50       47 return unless %$pods; # nothing to do
3339              
3340 10 100       215 unless ( -d $htmldir ) {
3341 7 50       1192 File::Path::mkpath($htmldir, 0, oct(755))
3342             or die "Couldn't mkdir $htmldir: $!";
3343             }
3344              
3345 10 50       86 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       93 : $self->original_prefix('core');
3350              
3351 10         92 my $htmlroot = $self->install_sets('core')->{libhtml};
3352 10         32 my $podpath;
3353 10 50 33     99 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         401 my $blibdir = join('/', File::Spec->splitdir(
3366             (File::Spec->splitpath(File::Spec->rel2abs($htmldir),1))[1]),''
3367             );
3368              
3369 10         34 my ($with_ActiveState, $htmltool);
3370              
3371 10 50 33     81 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         87 require Module::Build::PodParser;
3380 10         40 require Pod::Html;
3381 10         248 $htmltool = "Pod::Html " . Pod::Html->VERSION;
3382             }
3383 10         102 $self->log_verbose("Converting Pod to HTML with $htmltool\n");
3384              
3385 10         25 my $errors = 0;
3386              
3387             POD:
3388 10         66 foreach my $pod ( sort keys %$pods ) {
3389              
3390 10         44 my ($name, $path) = File::Basename::fileparse($pods->{$pod},
3391             $self->file_qr('\.(?:pm|plx?|pod)$')
3392             );
3393 10         102 my @dirs = File::Spec->splitdir( File::Spec->canonpath( $path ) );
3394 10 50 33     153 pop( @dirs ) if scalar(@dirs) && $dirs[-1] eq File::Spec->curdir;
3395              
3396 10         77 my $fulldir = File::Spec->catdir($htmldir, @rootdirs, @dirs);
3397 10         100 my $tmpfile = File::Spec->catfile($fulldir, "${name}.tmp");
3398 10         75 my $outfile = File::Spec->catfile($fulldir, "${name}.html");
3399 10         803 my $infile = File::Spec->abs2rel($pod);
3400              
3401 10 50       115 next if $self->up_to_date($infile, $outfile);
3402              
3403 10 50       123 unless ( -d $fulldir ){
3404 10 50       1762 File::Path::mkpath($fulldir, 0, oct(755))
3405             or die "Couldn't mkdir $fulldir: $!";
3406             }
3407              
3408 10         196 $self->log_verbose("HTMLifying $infile -> $outfile\n");
3409 10 50       41 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         129 my $path2root = File::Spec->catdir((File::Spec->updir) x @dirs);
3425 10 50       409 open(my $fh, '<', $infile) or die "Can't read $infile: $!";
3426 10         256 my $abstract = Module::Build::PodParser->new(fh => $fh)->get_abstract();
3427              
3428 10         51 my $title = join( '::', (@dirs, $name) );
3429 10 100       40 $title .= " - $abstract" if $abstract;
3430              
3431 10 50       114 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       28 unless ( eval{Pod::Html->VERSION(1.12)} ) {
  10         236  
3441 0         0 push( @opts, ('--flush') ); # caching removed in 1.12
3442             }
3443              
3444 10 50       25 if ( eval{Pod::Html->VERSION(1.12)} ) {
  10 0       102  
3445 10         29 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         201 $self->log_verbose("P::H::pod2html @opts\n");
3451             {
3452 10         20 my $orig = Cwd::getcwd();
  10         105  
3453 10         116 eval { Pod::Html::pod2html(@opts); 1 }
  10         84084  
3454             or $self->log_warn("[$htmltool] pod2html( " .
3455 10 50       30 join(", ", map { "q{$_}" } @opts) . ") failed: $@");
  0         0  
3456 10         335 chdir($orig);
3457             }
3458             }
3459             # We now have to cleanup the resulting html file
3460 10 50       206 if ( ! -r $tmpfile ) {
3461 0         0 $errors++;
3462 0         0 next POD;
3463             }
3464 10 50       411 open(my $fh, '<', $tmpfile) or die "Can't read $tmpfile: $!";
3465 10         522 my $html = join('',<$fh>);
3466 10         141 close $fh;
3467 10 50       96 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         174 $html =~ s#^<!DOCTYPE .*?>#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">#im;
3472 10         111 $html =~ s#<html xmlns="http://www.w3.org/1999/xhtml">#<html>#i;
3473              
3474             # IE6+ will not display local HTML files with strict
3475             # security without this comment
3476 10         84 $html =~ s#<head>#<head>\n<!-- saved from url=(0017)http://localhost/ -->#i;
3477             }
3478             # Fixup links that point to our temp blib
3479 10         150 $html =~ s/\Q$blibdir\E//g;
3480              
3481 10 50       689 open($fh, '>', $outfile) or die "Can't write $outfile: $!";
3482 10         77 print $fh $html;
3483 10         383 close $fh;
3484 10         612 unlink($tmpfile);
3485             }
3486              
3487 10         165 return ! $errors;
3488              
3489             }
3490              
3491             # Adapted from ExtUtils::MM_Unix
3492             sub man1page_name {
3493 18     18 0 42 my $self = shift;
3494 18         566 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 68 my $self = shift;
3503 17         302 my ($vol, $dirs, $file) = File::Spec->splitpath( shift );
3504 17         127 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         121 return join( $self->manpage_separator, @dirs, $file );
3510             }
3511              
3512             sub manpage_separator {
3513 19     19 0 2810 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 56 my ($self) = @_;
3569 9         6965 require ExtUtils::Install;
3570 9         75466 $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   668 $self->install_map, $self->verbose, 0, $self->{args}{uninst}||0
3577             );
3578 9         218 });
3579 9 0 33     656 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       117 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         25 my $info = $self->_enum_prereqs;
3636 2 50       8 if (! $info ) {
3637 0         0 $self->log_info( "No prerequisites detected\n" );
3638 0         0 return;
3639             }
3640              
3641 2         27 my $failures = $self->prereq_failures($info);
3642 2 50       9 if ( ! $failures ) {
3643 0         0 $self->log_info( "All prerequisites satisfied\n" );
3644 0         0 return;
3645             }
3646              
3647 2         5 my @install;
3648 2         22 foreach my $type (sort keys %$failures) {
3649 4         10 my $prereqs = $failures->{$type};
3650 4 50       34 if($type =~ m/^(?:\w+_)?requires$/) {
3651 4         17 push(@install, sort keys %$prereqs);
3652 4         10 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       7 return unless @install;
3661              
3662 2         15 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       369 if ( ! File::Spec->file_name_is_absolute( $command ) ) {
3667             # prefer site to vendor to core
3668 1         15 my @loc = ( 'site', 'vendor', '' );
3669 1         33 my @bindirs = File::Basename::dirname($self->perl);
3670             push @bindirs,
3671             map {
3672 1         9 ($self->config->{"install${_}bin"}, $self->config->{"install${_}script"})
  3         25  
3673             } @loc;
3674 1         6 for my $d ( @bindirs ) {
3675 7         88 my $abs_cmd = $self->find_command(File::Spec->catfile( $d, $command ));
3676 7 50       32 if ( defined $abs_cmd ) {
3677 0         0 $command = $abs_cmd;
3678 0         0 last;
3679             }
3680             }
3681             }
3682              
3683 2         21 $self->do_system($command, @opts, @install);
3684             }
3685              
3686             sub ACTION_clean {
3687 46     46 0 293 my ($self) = @_;
3688 46         652 $self->log_info("Cleaning up build files\n");
3689 46         1088 foreach my $item (map glob($_), $self->cleanup) {
3690 125         819 $self->delete_filetree($item);
3691             }
3692             }
3693              
3694             sub ACTION_realclean {
3695 35     35 0 1842 my ($self) = @_;
3696 35         472 $self->depends_on('clean');
3697 35         245 $self->log_info("Cleaning up configuration files\n");
3698 35         700 $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 33 my ($self) = @_;
3705              
3706 5         4259 require Module::Build::PPMMaker;
3707 5         105 my $ppd = Module::Build::PPMMaker->new();
3708 5         30 my $file = $ppd->make_ppd(%{$self->{args}}, build => $self);
  5         95  
3709 5         101 $self->add_to_cleanup($file);
3710             }
3711              
3712             sub ACTION_ppmdist {
3713 3     3 0 22 my ($self) = @_;
3714              
3715 3         63 $self->depends_on( 'build' );
3716              
3717 3         94 my $ppm = $self->ppm_name;
3718 3         25 $self->delete_filetree( $ppm );
3719 3         23 $self->log_info( "Creating $ppm\n" );
3720 3         41 $self->add_to_cleanup( $ppm, "$ppm.tar.gz" );
3721              
3722 3         71 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         33 foreach my $type ($self->install_types) {
3734 24 100 66     148 next if exists( $types{$type} ) && !defined( $types{$type} );
3735              
3736 18         62 my $dir = File::Spec->catdir( $self->blib, $type );
3737 18 100       322 next unless -e $dir;
3738              
3739 15         64 my $files = $self->rscan_dir( $dir );
3740 15         49 foreach my $file ( @$files ) {
3741 39 100       501 next unless -f $file;
3742 18         1658 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       173 exists( $types{$type} ) ? $types{$type} : $type,
3748             $rel_file );
3749 18         95 $self->copy_if_modified( from => $file, to => $to_file );
3750             }
3751             }
3752              
3753 3         18 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         74 my $target = File::Spec->catfile( File::Spec->updir, $ppm );
3760 3     3   142 $self->_do_in_dir( $ppm, sub { $self->make_tarball( 'blib', $target ) } );
  3         167  
3761              
3762 3         88 $self->depends_on( 'ppd' );
3763              
3764 3         19 $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 6 my ($self) = @_;
3789              
3790             # MUST dispatch() and not depends_ok() so we generate a clean distdir
3791 1         12 $self->dispatch('distdir');
3792              
3793 1         23 my $dist_dir = $self->dist_dir;
3794              
3795 1         24 $self->make_tarball($dist_dir);
3796 1         11651 $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   232 my ($self, $manifest, $lines) = @_;
3842 30 50       251 $lines = [$lines] unless ref $lines;
3843              
3844 30         230 my $existing_files = $self->_read_manifest($manifest);
3845 30 100       4114 return unless defined( $existing_files );
3846              
3847 22 50       88 @$lines = grep {!exists $existing_files->{$_}} @$lines
  22         188  
3848             or return;
3849              
3850 22         347 my $mode = (stat $manifest)[2];
3851 22 50       556 chmod($mode | oct(222), $manifest) or die "Can't make $manifest writable: $!";
3852              
3853 22 50       893 open(my $fh, '<', $manifest) or die "Can't read $manifest: $!";
3854 22   50     705 my $last_line = (<$fh>)[-1] || "\n";
3855 22         302 my $has_newline = $last_line =~ /\n$/;
3856 22         246 close $fh;
3857              
3858 22 50       729 open($fh, '>>', $manifest) or die "Can't write to $manifest: $!";
3859 22 50       178 print $fh "\n" unless $has_newline;
3860 22         237 print $fh map "$_\n", @$lines;
3861 22         571 close $fh;
3862 22         350 chmod($mode, $manifest);
3863              
3864 22         351 $self->log_verbose(map "Added to $manifest: $_\n", @$lines);
3865             }
3866              
3867             sub _sign_dir {
3868 5     5   17 my ($self, $dir) = @_;
3869              
3870 5 50       12 unless (eval { require Module::Signature; 1 }) {
  5         30  
  5         21  
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         9 my $manifest = File::Spec->catfile($dir, 'MANIFEST');
  5         56  
3878 5 50       78 die "Signing a distribution requires a MANIFEST file" unless -e $manifest;
3879 5         51 $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   123 $self->_do_in_dir($dir, sub {local $Module::Signature::Quiet = 1; Module::Signature::sign()});
  5         57  
  5         123  
3885             }
3886              
3887             sub _do_in_dir {
3888 18     18   87 my ($self, $dir, $do) = @_;
3889              
3890 18         177 my $start_dir = File::Spec->rel2abs($self->cwd);
3891 18 50       794 chdir $dir or die "Can't chdir() to $dir: $!";
3892 18         235 eval {$do->()};
  18         309  
3893 18 50       446069 my @err = $@ ? ($@) : ();
3894 18 50       895 chdir $start_dir or push @err, "Can't chdir() back to $start_dir: $!";
3895 18 50       1093 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 1094 my $self = shift;
3924 1         30 require Module::Build::Compat;
3925 1         31 $self->log_info("Creating Makefile.PL\n");
3926 1         7 eval { Module::Build::Compat->create_makefile_pl($self->create_makefile_pl, $self, @_) };
  1         43  
3927 1 50       25 if ( $@ ) {
3928 0         0 1 while unlink 'Makefile.PL';
3929 0         0 die "$@\n";
3930             }
3931 1         21 $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 307 my $self = shift;
3964 6         280 $self->delete_filetree('README');
3965              
3966 6         169 my $docfile = $self->_main_docfile;
3967 6 50       81 unless ( $docfile ) {
3968 0         0 $self->log_warn(<<EOF);
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       56 if ( eval {require Pod::Readme; Pod::Readme->can('new') } ) {
  6 50       2518  
  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         6031 } elsif ( eval {require Pod::Text; 1} ) {
  6         171231  
3984 6         73 $self->log_info("Creating README using Pod::Text\n");
3985              
3986 6 50       665 if ( open(my $fh, '>', 'README') ) {
3987 6         103 local $^W = 0;
3988 293     293   2822 no strict "refs";
  293         2870  
  293         757551  
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         26 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     38 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         17780 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         199 $self->_add_to_manifest('MANIFEST', 'README');
4020             }
4021              
4022             sub _main_docfile {
4023 63     63   228 my $self = shift;
4024 63 50       427 if ( my $pm_file = $self->dist_version_from ) {
4025 63         822 (my $pod_file = $pm_file) =~ s/.pm$/.pod/;
4026 63 100       1388 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 46 my ($self) = @_;
4043              
4044 10 50 33     23 if ( @{$self->bundle_inc} && ! $self->_mb_feature('inc_bundling_support') ) {
  10         134  
4045 0         0 $self->_warn_mb_feature_deps('inc_bundling_support');
4046 0         0 die "Aborting.\n";
4047             }
4048              
4049 10         152 $self->depends_on('distmeta');
4050              
4051 10 50       131 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         2374 delete $dist_files->{SIGNATURE}; # Don't copy, create a fresh one
4054 10 50 33     205 die "No files found in MANIFEST - try running 'manifest' action?\n"
4055             unless ($dist_files and keys %$dist_files);
4056 10         110 my $metafile = $self->metafile;
4057             $self->log_warn("*** Did you forget to add $metafile to the MANIFEST?\n")
4058 10 50       57 unless exists $dist_files->{$metafile};
4059              
4060 10         99 my $dist_dir = $self->dist_dir;
4061 10         83 $self->delete_filetree($dist_dir);
4062 10         131 $self->log_info("Creating $dist_dir\n");
4063 10         146 $self->add_to_cleanup($dist_dir);
4064              
4065 10         128 foreach my $file (sort keys %$dist_files) {
4066 69 50       282 next if $file =~ m{^MYMETA\.}; # Double check that we skip MYMETA.*
4067 69         303 my $new = $self->copy_if_modified(from => $file, to_dir => $dist_dir, verbose => 0);
4068             }
4069              
4070 10 50       31 $self->do_create_bundle_inc if @{$self->bundle_inc};
  10         60  
4071              
4072 10 100       93 $self->_sign_dir($dist_dir) if $self->{properties}{sign};
4073             }
4074              
4075             sub ACTION_disttest {
4076 1     1 0 14 my ($self) = @_;
4077              
4078 1         29 $self->depends_on('distdir');
4079              
4080             $self->_do_in_dir
4081             ( $self->dist_dir,
4082             sub {
4083 1     1   28 local $ENV{AUTHOR_TESTING} = 1;
4084 1         48 local $ENV{RELEASE_TESTING} = 1;
4085              
4086             # XXX could be different names for scripts
4087              
4088 1 50       34 $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       134 $self->run_perl_script($self->build_script)
4091             or die "Error executing $self->build_script in dist directory: $!";
4092 1 50       95 $self->run_perl_script($self->build_script, [], ['test'])
4093             or die "Error executing 'Build test' in dist directory";
4094 1         6 });
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   1384 my $self = shift;
4128              
4129 2         1680 require ExtUtils::Manifest;
4130 2         8043 return eval { ExtUtils::Manifest->VERSION(1.50); 1 };
  2         44  
  2         48  
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   118 my $self = shift;
4189 35 100       209 if ( ref $self ) {
4190             $self->{_case_tolerant} = File::Spec->case_tolerant
4191 34 100       446 unless defined($self->{_case_tolerant});
4192 34         597 return $self->{_case_tolerant};
4193             }
4194             else {
4195 1         42 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   53 my $self = shift;
4213 1   50     5 my $file = shift || 'MANIFEST.SKIP';
4214 1 50       144 open(my $fh, '>', $file)
4215             or die "Can't open $file: $!";
4216              
4217 1 50       29 my $content = $self->_eumanifest_has_include ? "#!include_default\n"
4218             : $self->_slurp( $self->_default_maniskip );
4219              
4220 1         6 $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         8 $content .= '\b'.$self->dist_name.'-[\d\.\_]+'."\n";
4238              
4239 1         12 print $fh $content;
4240            
4241 1         64 close $fh;
4242              
4243 1         10 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 13715 return shift->{_case_tolerant} ? qr($_[0])i : qr($_[0]);
4289             }
4290              
4291             sub dist_dir {
4292 29     29 0 110765 my ($self) = @_;
4293 29         460 my $dir = join "-", $self->dist_name, $self->dist_version;
4294 29 100       261 $dir .= "-" . $self->dist_suffix if $self->dist_suffix;
4295 29         251 return $dir;
4296             }
4297              
4298             sub ppm_name {
4299 8     8 0 3174 my $self = shift;
4300 8         89 return 'PPM-' . $self->dist_dir;
4301             }
4302              
4303             sub _files_in {
4304 93     93   861 my ($self, $dir) = @_;
4305 93 100       1786 return unless -d $dir;
4306              
4307 2         18 local *DH;
4308 2 50       52 opendir DH, $dir or die "Can't read directory $dir: $!";
4309              
4310 2         18 my @files;
4311 2         52 while (defined (my $file = readdir DH)) {
4312 8         78 my $full_path = File::Spec->catfile($dir, $file);
4313 8 100       116 next if -d $full_path;
4314 4         34 push @files, $full_path;
4315             }
4316 2         44 return @files;
4317             }
4318              
4319             sub share_dir {
4320 178     178 0 31756 my $self = shift;
4321 178         766 my $p = $self->{properties};
4322              
4323 178 50       878 $p->{share_dir} = shift if @_;
4324              
4325             # Always coerce to proper hash form
4326 178 100       1571 if ( ! defined $p->{share_dir} ) {
    50          
    50          
    50          
4327 154         820 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         207 my $share_dir = $p->{share_dir};
4340             # check dist key
4341 24 50       181 if ( defined $share_dir->{dist} ) {
4342 24 50       540 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       293 if ( defined $share_dir->{module} ) {
4352 6         42 my $mod_hash = $share_dir->{module};
4353 6 50       52 if ( ref $mod_hash eq 'HASH' ) {
4354 6         70 for my $k ( sort keys %$mod_hash ) {
4355 6 50       92 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         765 return $p->{share_dir};
4373             }
4374              
4375             sub script_files {
4376 106     106 0 1859 my $self = shift;
4377              
4378 106         593 for ($self->{properties}{script_files}) {
4379 106 50       569 $_ = shift if @_;
4380 106 100       509 next unless $_;
4381              
4382             # Always coerce into a hash
4383 13 50       83 return $_ if ref $_ eq 'HASH';
4384 13 50       101 return $_ = { map {$_,1} @$_ } if ref $_ eq 'ARRAY';
  16         185  
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         311 File::Spec->canonpath( $_ ) => 1
4394 93 100       326 } keys %{ $self->PL_files || {} };
  93         1011  
4395              
4396 93         1263 my @bin_files = $self->_files_in('bin');
4397              
4398             my %bin_map = map {
4399 93         590 $_ => File::Spec->canonpath( $_ )
  4         52  
4400             } @bin_files;
4401              
4402 93         1557 return $_ = { map {$_ => 1} grep !$pl_files{$bin_map{$_}}, @bin_files };
  2         20  
4403             }
4404 293     293   2617448 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 654 return \%licenses;
4452             }
4453             sub _license_url {
4454 22     22   208 return $license_urls{$_[1]};
4455             }
4456             }
4457              
4458             sub _software_license_class {
4459 23     23   353 my ($self, $license) = @_;
4460 23 50 66     80 if ($self->valid_licenses->{$license} && eval { require Software::LicenseUtils; Software::LicenseUtils->VERSION(0.103009) }) {
  20         6015  
  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         398 LICENSE: for my $l ( $self->valid_licenses->{ $license }, $license ) {
4468 46 100       215 next unless defined $l;
4469 43         162 my $trial = "Software::License::" . $l;
4470 43 100       4558 if ( eval "require Software::License; Software::License->VERSION(0.014); require $trial; 1" ) {
4471 1         10 return $trial;
4472             }
4473             }
4474 22         357 return;
4475             }
4476              
4477             # use mapping or license name directly
4478             sub _software_license_object {
4479 23     23   119 my ($self) = @_;
4480 23 50       113 return unless defined( my $license = $self->license );
4481              
4482 23 100       343 my $class = $self->_software_license_class($license) or return;
4483              
4484             # Software::License requires a 'holder' argument
4485 1   50     10 my $author = join( " & ", @{ $self->dist_author }) || 'unknown';
4486 1         4 my $sl = eval { $class->new({holder=>$author}) };
  1         25  
4487 1 50       17 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 99 my ($self) = @_;
4507 13 50       247 $self->do_create_makefile_pl if $self->create_makefile_pl;
4508 13 50       138 $self->do_create_readme if $self->create_readme;
4509 13 50       139 $self->do_create_license if $self->create_license;
4510 13         175 $self->do_create_metafile;
4511             }
4512              
4513             sub do_create_metafile {
4514 13     13 0 62 my $self = shift;
4515 13 100       79 return if $self->{wrote_metadata};
4516              
4517 9         38 my $p = $self->{properties};
4518              
4519 9 50       110 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         177 my @metafiles = ( $self->metafile, $self->metafile2 );
4525             # If we're in the distdir, the metafile may exist and be non-writable.
4526 9         244 $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         277 local @INC = @INC;
4531 9 50 50     187 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         200 my $meta_obj = $self->_get_meta_object(
4537             quiet => 1, fatal => 1, auto => 1
4538             );
4539 9         155 my @created = $self->_write_meta_files( $meta_obj, 'META' );
4540 9 50       47 if ( @created ) {
4541 9         50 $self->{wrote_metadata} = 1;
4542 9         242 $self->_add_to_manifest('MANIFEST', $_) for @created;
4543             }
4544 9         234 return 1;
4545             }
4546              
4547             sub _write_meta_files {
4548 15     15   66 my $self = shift;
4549 15         87 my ($meta, $file) = @_;
4550 15         69 $file =~ s{\.(?:yml|json)$}{};
4551              
4552 15         48 my @created;
4553 15 50 33     279 push @created, "$file\.yml"
4554             if $meta && $meta->save( "$file\.yml", {version => "1.4"} );
4555 15 50 33     135830 push @created, "$file\.json"
4556             if $meta && $meta->save( "$file\.json" );
4557              
4558 15 50       250280 if ( @created ) {
4559 15         320 $self->log_info("Created " . join(" and ", @created) . "\n");
4560             }
4561 15         108 return @created;
4562             }
4563              
4564             sub _get_meta_object {
4565 15     15   71 my $self = shift;
4566 15         160 my %args = @_;
4567 15 50       279 return unless $self->try_require("CPAN::Meta", "2.142060");
4568              
4569 15         94705 my $meta;
4570 15         48 eval {
4571             my $data = $self->get_metadata(
4572             fatal => $args{fatal},
4573             auto => $args{auto},
4574 15         269 );
4575 15 100       111 $data->{dynamic_config} = $args{dynamic} if defined $args{dynamic};
4576 15         221 $meta = CPAN::Meta->create($data);
4577             };
4578 15 50 33     10786 if ($@ && ! $args{quiet}) {
4579 0         0 $self->log_warn(
4580             "Could not get valid metadata. Error is: $@\n"
4581             );
4582             }
4583              
4584 15         62 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 2295 my ($self, $version) = @_;
4598 125 100 100     2584 $version = 0 unless defined $version and length $version;
4599              
4600 125 50       1985 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       799 $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         814 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   4618 my ($self) = @_;
4628 26         86 my $p = $self->{properties};
4629              
4630             # copy prereq data structures so we can modify them before writing to META
4631 26         63 my %prereq_types;
4632 26         82 for my $type ( 'configure_requires', @{$self->prereq_action_types} ) {
  26         277  
4633 156 100 66     567 if (exists $p->{$type} and keys %{ $p->{$type} }) {
  156         649  
4634 21         45 my ($phase, $relation) = @{ $prereq_map{$type} };
  21         164  
4635 21         63 for my $mod ( keys %{ $p->{$type} } ) {
  21         101  
4636 21         156 $prereq_types{$phase}{$relation}{$mod} = $self->normalize_version($p->{$type}{$mod});
4637             }
4638             }
4639             }
4640 26         208 return \%prereq_types;
4641             }
4642              
4643             sub _get_license {
4644 23     23   117 my $self = shift;
4645              
4646 23         94 my $license = $self->license;
4647 23         80 my ($meta_license, $meta_license_url);
4648              
4649 23         434 my $valid_licenses = $self->valid_licenses();
4650 23 100       402 if ( my $sl = $self->_software_license_object ) {
    50          
4651 1         10 $meta_license = $sl->meta2_name;
4652 1         13 $meta_license_url = $sl->url;
4653             }
4654             elsif ( exists $valid_licenses->{$license} ) {
4655 22 100       155 $meta_license = $valid_licenses->{$license} ? lc $valid_licenses->{$license} : $license;
4656 22         283 $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         136 return ($meta_license, $meta_license_url);
4664             }
4665              
4666             sub get_metadata {
4667 23     23 0 1332 my ($self, %args) = @_;
4668              
4669 23   100     232 my $fatal = $args{fatal} || 0;
4670 23         112 my $p = $self->{properties};
4671              
4672 23 100       404 $self->auto_config_requires if $args{auto};
4673              
4674             # validate required fields
4675 23         213 foreach my $f (qw(dist_name dist_version dist_author dist_abstract license)) {
4676 115         1467 my $field = $self->$f();
4677 115 50 33     908 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       144 dynamic_config => exists $p->{dynamic_config} ? $p->{dynamic_config} : 1,
4699             release_status => $self->release_status,
4700             );
4701              
4702 23         410 my ($meta_license, $meta_license_url) = $self->_get_license;
4703 23         121 $metadata{license} = [ $meta_license ];
4704 23 100       152 $metadata{resources}{license} = [ $meta_license_url ] if defined $meta_license_url;
4705              
4706 23         227 $metadata{prereqs} = $self->_normalize_prereqs;
4707              
4708 23 50       153 if (exists $p->{no_index}) {
    100          
4709 0         0 $metadata{no_index} = $p->{no_index};
4710 23         318 } elsif (my $pkgs = eval { $self->find_dist_packages }) {
4711 19 100       104 $metadata{provides} = $pkgs if %$pkgs;
4712             } else {
4713 4         72 $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       324 if (my $add = $self->meta_add) {
4718 23 50 33     142 if (not exists $add->{'meta-spec'} or $add->{'meta-spec'}{version} != 2) {
4719 23         174 require CPAN::Meta::Converter;
4720 23 50       622 if (CPAN::Meta::Converter->VERSION('2.141170')) {
4721 23         305 $add = CPAN::Meta::Converter->new($add)->upgrade_fragment;
4722 23         42654 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         112 while (my($k, $v) = each %{$add}) {
  53         261  
4730 30         100 $metadata{$k} = $v;
4731             }
4732             }
4733              
4734 23 50       290 if (my $merge = $self->meta_merge) {
4735 23 50       64 if (eval { require CPAN::Meta::Merge }) {
  23         10913  
4736 23         39100 %metadata = %{ CPAN::Meta::Merge->new(default_version => '1.4')->merge(\%metadata, $merge) };
  23         162  
4737             }
4738             else {
4739 0         0 $self->log_warn("Can't merge without CPAN::Meta::Merge");
4740             }
4741             }
4742              
4743 23         50649 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   293 my ($self, $file) = @_;
4764 63 100       1047 return undef unless -e $file;
4765              
4766 51         10955 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
4767 51         92575 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
4768 51         446 return scalar ExtUtils::Manifest::maniread($file);
4769             }
4770              
4771             sub find_dist_packages {
4772 23     23 0 81 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       346 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         3675 my %dist_files = map { $self->localize_file_path($_) => $_ }
  88         554  
4783             keys %$manifest;
4784              
4785 18         123 my @pm_files = sort grep { $_ !~ m{^t} } # skip things in t/
4786 18         124 grep {exists $dist_files{$_}}
4787 19         97 keys %{ $self->find_pm_files };
  19         351  
4788              
4789 19         406 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 116 my ($self, $file_list, $filename_map) = @_;
4796              
4797             # First, we enumerate all packages & versions,
4798             # separating into primary & alternative candidates
4799 19         76 my( %prime, %alt );
4800 19         55 foreach my $file (@{$file_list}) {
  19         124  
4801 18         98 my $mapped_filename = $filename_map->{$file};
4802 18         118 my @path = split( /\//, $mapped_filename );
4803 18         283 (my $prime_package = join( '::', @path[1..$#path] )) =~ s/\.pm$//;
4804              
4805 18         320 my $pm_info = Module::Metadata->new_from_file( $file );
4806              
4807 18         22434 foreach my $package ( $pm_info->packages_inside ) {
4808 18 50       211 next if $package eq 'main'; # main can appear numerous times, ignore
4809 18 50       70 next if $package eq 'DB'; # special debugging package, ignore
4810 18 50       126 next if grep /^_/, split( /::/, $package ); # private package, ignore
4811              
4812 18         109 my $version = $pm_info->version( $package );
4813              
4814 18 50       272 if ( $package eq $prime_package ) {
4815 18 50       75 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         80 $prime{$package}{file} = $mapped_filename;
4820 18 50       263 $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         120 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         116 for my $provides ( values %prime ) {
4897 18 50       393 if ( $provides->{version} ) {
4898             $provides->{version} = $self->normalize_version( $provides->{version} )
4899 18         133 }
4900             else {
4901 0         0 delete $provides->{version};
4902             }
4903             }
4904              
4905 19         222 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 66 my ($self, $dir, $file) = @_;
4949 4   66     74 $file ||= $dir;
4950              
4951 4         90 $self->log_info("Creating $file.tar.gz\n");
4952              
4953 4 50       49 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       43 eval { require Archive::Tar && Archive::Tar->VERSION(1.09); 1 }
  4 50       424  
  4         71  
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         110 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       27 (grep { length($_) >= 100 } @$files) ? 0 : 1;
  73         143  
4980              
4981 4         95 my $tar = Archive::Tar->new;
4982 4         167 $tar->add_files(@$files);
4983 4         28226 for my $f ($tar->get_files) {
4984 73         1374 $f->mode($f->mode & ~022); # chmod go-w
4985             }
4986 4         127 $tar->write("$file.tar.gz", 1);
4987             }
4988             }
4989              
4990             sub install_path {
4991 334     334 0 6474 my $self = shift;
4992 334         1108 my( $type, $value ) = ( @_, '<empty>' );
4993              
4994 334 50       1013 Carp::croak( 'Type argument missing' )
4995             unless defined( $type );
4996              
4997 334         915 my $map = $self->{properties}{install_path};
4998 334 100       1043 return $map unless @_;
4999              
5000             # delete existing value if $value is literal undef()
5001 319 100       1014 unless ( defined( $value ) ) {
5002 1         5 delete( $map->{$type} );
5003 1         6 return undef;
5004             }
5005              
5006             # return existing value if no new $value is given
5007 318 100       968 if ( $value eq '<empty>' ) {
5008 317 100       1518 return undef unless exists $map->{$type};
5009 51         878 return $map->{$type};
5010             }
5011              
5012             # set value if $value is a valid relative path
5013 1         24 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 1506 my ($self, $dirs, $key, $value) = @_;
5020 217 100       613 $dirs = $self->installdirs unless defined $dirs;
5021             # update property before merging with defaults
5022 217 0 33     813 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         1129 )};
5030 217 50 33     3235 if ( defined $dirs && defined $key ) {
    50          
5031 0         0 return $map->{$dirs}{$key};
5032             }
5033             elsif ( defined $dirs ) {
5034 217         2838 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 100 my ($self, $key, $value) = @_;
5045             # update property before merging with defaults
5046 34 50 33     108 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         126 )};
5054 34 50       412 return $map unless defined $key;
5055 34         123 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 4380 my $self = shift;
5062 82 100       235 if ( @_ > 1 ) { # change values before merge
5063 3         35 $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         342 )};
5069 81 100       1043 return $map unless @_;
5070 73         170 my $relpath = $map->{$_[0]};
5071 73 100       794 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 4402 my $self = shift;
5079 34 50 66     115 my $installdirs = shift || $self->installdirs
5080             or croak "Can't determine installdirs for prefix_relpaths()";
5081 34 100       79 if ( @_ > 1 ) { # change values before merge
5082 3   100     23 $self->{properties}{prefix_relpaths}{$installdirs} ||= {};
5083 3         10 $self->_set_relpaths($self->{properties}{prefix_relpaths}{$installdirs}, @_);
5084             }
5085             my $map = {$self->_merge_arglist(
5086             $self->{properties}{prefix_relpaths}{$installdirs},
5087 33         108 $self->_default_install_paths->{prefix_relpaths}{$installdirs}
5088             )};
5089 33 100       410 return $map unless @_;
5090 27         63 my $relpath = $map->{$_[0]};
5091 27 100       317 return defined $relpath ? File::Spec->catdir( @$relpath ) : undef;
5092             }
5093              
5094             sub _set_relpaths {
5095 6     6   24 my $self = shift;
5096 6         22 my( $map, $type, $value ) = @_;
5097              
5098 6 50       16 Carp::croak( 'Type argument missing' )
5099             unless defined( $type );
5100              
5101             # set undef if $value is literal undef()
5102 6 100       20 if ( ! defined( $value ) ) {
5103 2         4 $map->{$type} = undef;
5104 2         13 return;
5105             }
5106             # set value if $value is a valid relative path
5107             else {
5108 4 100       578 Carp::croak( "Value must be a relative path" )
5109             if File::Spec::Unix->file_name_is_absolute($value);
5110              
5111 2         9 my @value = split( /\//, $value );
5112 2         13 $map->{$type} = \@value;
5113             }
5114             }
5115              
5116             # Translated from ExtUtils::MM_Any::init_INSTALL_from_PREFIX
5117             sub prefix_relative {
5118 24     24 0 39 my ($self, $type) = @_;
5119 24         50 my $installdirs = $self->installdirs;
5120              
5121 24         62 my $relpath = $self->install_sets($installdirs)->{$type};
5122              
5123 24         108 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         77 my $rprefix = $self->prefix;
5134 24 50       89 $rprefix .= '/' if $sprefix =~ m|/$|;
5135              
5136 24 50 33     184 $self->log_verbose(" prefixify $path from $sprefix to $rprefix\n")
5137             if defined( $path ) && length( $path );
5138              
5139 24 50 33     352 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         74 $self->log_verbose(" cannot prefixify, falling back to default.\n");
5146 24         71 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   38 my $self = shift;
5156 24         34 my $type = shift;
5157 24         33 my $rprefix = shift;
5158              
5159 24         51 my $default = $self->prefix_relpaths($self->installdirs, $type);
5160 24 50       66 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         82 return $default;
5165             }
5166             }
5167              
5168             sub install_destination {
5169 241     241 0 34676 my ($self, $type) = @_;
5170              
5171 241 100       1368 return $self->install_path($type) if $self->install_path($type);
5172              
5173 216 100       1080 if ( $self->install_base ) {
5174 70         278 my $relpath = $self->install_base_relpaths($type);
5175 70 50       267 return $relpath ? File::Spec->catdir($self->install_base, $relpath) : undef;
5176             }
5177              
5178 146 100       731 if ( $self->prefix ) {
5179 24         76 my $relpath = $self->prefix_relative($type);
5180 24 50       75 return $relpath ? File::Spec->catdir($self->prefix, $relpath) : undef;
5181             }
5182              
5183 122         666 return $self->install_sets($self->installdirs)->{$type};
5184             }
5185              
5186             sub install_types {
5187 12     12 0 59 my $self = shift;
5188              
5189 12         74 my %types;
5190 12 100       206 if ( $self->install_base ) {
    50          
5191 5         72 %types = %{$self->install_base_relpaths};
  5         134  
5192             } elsif ( $self->prefix ) {
5193 0         0 %types = %{$self->prefix_relpaths};
  0         0  
5194             } else {
5195 7         21 %types = %{$self->install_sets($self->installdirs)};
  7         328  
5196             }
5197              
5198 12         96 %types = (%types, %{$self->install_path});
  12         107  
5199              
5200 12         201 return sort keys %types;
5201             }
5202              
5203             sub install_map {
5204 9     9 0 91 my ($self, $blib) = @_;
5205 9   33     331 $blib ||= $self->blib;
5206              
5207 9         45 my( %map, @skipping );
5208 9         146 foreach my $type ($self->install_types) {
5209 72         520 my $localdir = File::Spec->catdir( $blib, $type );
5210 72 100       1193 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       672 if (my $dest = $self->install_destination($type)) {
5220 33         300 $map{$localdir} = $dest;
5221             } else {
5222 0         0 push( @skipping, $type );
5223             }
5224             }
5225              
5226             $self->log_warn(
5227 9 50       78 "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     211 if ($self->create_packlist and my $module_name = $self->module_name) {
5233 9         60 my $archdir = $self->install_destination('arch');
5234 9         91 my @ext = split /::/, $module_name;
5235 9         204 $map{write} = File::Spec->catfile($archdir, 'auto', @ext, '.packlist');
5236             }
5237              
5238             # Handle destdir
5239 9 100 100     213 if (length(my $destdir = $self->destdir || '')) {
5240 5         52 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         451 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         128 my @dirs = File::Spec->splitdir($path);
5249              
5250             # First merge the directories
5251 25         152 $path = File::Spec->catdir($destdir, @dirs);
5252              
5253             # Then put the file back on if there is one.
5254 25 50       65 if ($file ne '') {
5255 25         185 $map{$_} = File::Spec->catfile($path, $file)
5256             } else {
5257 0         0 $map{$_} = $path;
5258             }
5259             }
5260             }
5261              
5262 9         90 $map{read} = ''; # To keep ExtUtils::Install quiet
5263              
5264 9         156 return \%map;
5265             }
5266              
5267             sub depends_on {
5268 324     324 0 1274 my $self = shift;
5269 324         1403 foreach my $action (@_) {
5270 352         2564 $self->_call_action($action);
5271             }
5272             }
5273              
5274             sub rscan_dir {
5275 466     466 0 2771 my ($self, $dir, $pattern) = @_;
5276 466         1126 my @result;
5277 466         1237 local $_; # find() can overwrite $_, so protect ourselves
5278 349     349   10738 my $subr = !$pattern ? sub {push @result, $File::Find::name} :
5279 1077 100   1077   42803 !ref($pattern) || (ref $pattern eq 'Regexp') ? sub {push @result, $File::Find::name if /$pattern/} :
5280 59 100   59   430 ref($pattern) eq 'CODE' ? sub {push @result, $File::Find::name if $pattern->()} :
5281 466 50 66     8248 die "Unknown pattern type";
    100          
    100          
5282              
5283 466     694   50843 File::Find::find({wanted => $subr, no_chdir => 1, preprocess => sub { sort @_ } }, $dir);
  694         25882  
5284 466         7089 return \@result;
5285             }
5286              
5287             sub delete_filetree {
5288 475     475 0 1502 my $self = shift;
5289 475         1222 my $deleted = 0;
5290 475         1575 foreach (@_) {
5291 580 100       8663 next unless -e $_;
5292 200         2583 $self->log_verbose("Deleting $_\n");
5293 200         138131 File::Path::rmtree($_, 0, 0);
5294 200 50       5284 die "Couldn't remove '$_': $!\n" if -e $_;
5295 200         906 $deleted++;
5296             }
5297 475         2166 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 19580 my $self = shift;
5311 54         293 my $s = $self->{stash};
5312 54 100       1239 return $s->{_cbuilder} if $s->{_cbuilder};
5313              
5314 17         10379 require ExtUtils::CBuilder;
5315 17 100       446004 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 4082 my ($self) = @_;
5323              
5324 27         214 my $p = $self->{properties};
5325 27 100       288 return $p->{_have_c_compiler} if defined $p->{_have_c_compiler};
5326              
5327 8         243 $self->log_verbose("Checking if compiler tools configured... ");
5328 8         211 my $b = $self->cbuilder;
5329 8   33     61473 my $have = $b && eval { $b->have_compiler };
5330 8 50       553269 $self->log_verbose($have ? "ok.\n" : "failed.\n");
5331 8         558 return $p->{_have_c_compiler} = $have;
5332             }
5333              
5334             sub compile_c {
5335 19     19 0 248 my ($self, $file, %args) = @_;
5336              
5337 19 100       246 if ( ! $self->have_c_compiler ) {
5338 1         371 die "Error: no compiler detected to compile '$file'. Aborting\n";
5339             }
5340              
5341 18         204 my $b = $self->cbuilder;
5342 18         67729 my $obj_file = $b->object_file($file);
5343 18         935 $self->add_to_cleanup($obj_file);
5344 18 100       257 return $obj_file if $self->up_to_date($file, $obj_file);
5345              
5346             $b->compile(source => $file,
5347             defines => $args{defines},
5348 12         447 object_file => $obj_file,
5349             include_dirs => $self->include_dirs,
5350             extra_compiler_flags => $self->extra_compiler_flags,
5351             );
5352              
5353 12         2934296 return $obj_file;
5354             }
5355              
5356             sub link_c {
5357 18     18 0 201 my ($self, $spec) = @_;
5358 18         110 my $p = $self->{properties}; # For convenience
5359              
5360 18         232 $self->add_to_cleanup($spec->{lib_file});
5361              
5362 18   50     495 my $objects = $p->{objects} || [];
5363              
5364             return $spec->{lib_file}
5365             if $self->up_to_date([$spec->{obj_file}, @$objects],
5366 18 100       380 $spec->{lib_file});
5367              
5368 12   33     166 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         247 extra_linker_flags => $self->extra_linker_flags );
5375              
5376 12         510854 return $spec->{lib_file};
5377             }
5378              
5379             sub compile_xs {
5380 13     13 0 113 my ($self, $file, %args) = @_;
5381              
5382 13         122 $self->log_verbose("$file -> $args{outfile}\n");
5383              
5384 13 50       86 if (eval {require ExtUtils::ParseXS; 1}) {
  13         7683  
  13         147444  
5385              
5386             ExtUtils::ParseXS::process_file(
5387             filename => $file,
5388             prototypes => 0,
5389             output => $args{outfile},
5390 13         155 );
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 432956 my ($self, $string) = @_;
5422              
5423 1804 100       11936 return () unless defined($string);
5424 538 100       3695 return @$string if ref $string eq 'ARRAY';
5425 423         5053 $string =~ s/^\s+|\s+$//g;
5426 423 100       2471 return () unless length($string);
5427              
5428 360         3243 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 56 my($self, $cmd, $switches, $args) = @_;
5440 7 50       56 $switches = [] unless defined $switches;
5441 7 50       28 $args = [] unless defined $args;
5442              
5443             # Strip leading and trailing newlines
5444 7         56 $cmd =~ s{^\n+}{};
5445 7         77 $cmd =~ s{\n+$}{};
5446              
5447 7 50       105 my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
5448 7         196 return $self->_quote_args($perl, @$switches, '-e', $cmd, @$args);
5449             }
5450              
5451             sub run_perl_script {
5452 3754     3754 0 7443049 my ($self, $script, $preargs, $postargs) = @_;
5453 3754         34128 foreach ($preargs, $postargs) {
5454 7508 100       50065 $_ = [ $self->split_like_shell($_) ] unless ref();
5455             }
5456 3754         45561 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 36068 my ($self, $args) = @_;
5463 3766 50       12593 $args = [ $self->split_like_shell($args) ] unless ref($args);
5464 3766 100       61476 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         69170 local $ENV{PERL5LIB} = join $self->config('path_sep'), $self->_added_to_INC;
5468              
5469 3702         47622 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         42 my $file = shift;
5479              
5480 19         66 my $cf = $self->{config};
5481              
5482 19         50 my %spec;
5483              
5484 19         441 my( $v, $d, $f ) = File::Spec->splitpath( $file );
5485 19         193 my @d = File::Spec->splitdir( $d );
5486 19         352 (my $file_base = $f) =~ s/\.[^.]+$//i;
5487              
5488 19         205 $spec{base_name} = $file_base;
5489              
5490 19         363 $spec{src_dir} = File::Spec->catpath( $v, $d, '' );
5491              
5492             # the module name
5493 19   100     688 shift( @d ) while @d && ($d[0] eq 'lib' || $d[0] eq '');
      100        
5494 19   100     362 pop( @d ) while @d && $d[-1] eq '';
5495 19         158 $spec{module_name} = join( '::', (@d, $file_base) );
5496              
5497 19         323 $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         429 "${file_base}.c" );
5502              
5503             $spec{obj_file} = File::Spec->catfile( $spec{src_dir},
5504 19         286 "${file_base}".$cf->get('obj_ext') );
5505              
5506 19         366 require DynaLoader;
5507 19 50       120 my $modfname = defined &DynaLoader::mod2fname ? DynaLoader::mod2fname([@d, $file_base]) : $file_base;
5508              
5509 19         289 $spec{bs_file} = File::Spec->catfile($spec{archdir}, "$modfname.bs");
5510              
5511 19         158 $spec{lib_file} = File::Spec->catfile($spec{archdir}, "$modfname.".$cf->get('dlext'));
5512              
5513 19         151 return \%spec;
5514             }
5515              
5516             sub process_xs {
5517 19     19 0 104 my ($self, $file) = @_;
5518              
5519 19         158 my $spec = $self->_infer_xs_spec($file);
5520              
5521             # File name, minus the suffix
5522 19         224 (my $file_base = $file) =~ s/\.[^.]+$//;
5523              
5524             # .xs -> .c
5525 19         111 $self->add_to_cleanup($spec->{c_file});
5526              
5527 19 100       165 unless ($self->up_to_date($file, $spec->{c_file})) {
5528 13         361 $self->compile_xs($file, outfile => $spec->{c_file});
5529             }
5530              
5531             # .c -> .o
5532 19         779315 my $v = $self->dist_version;
5533             $self->compile_c($spec->{c_file},
5534 19         783 defines => {VERSION => qq{"$v"}, XS_VERSION => qq{"$v"}});
5535              
5536             # archdir
5537 18 100       15015 File::Path::mkpath($spec->{archdir}, 0, oct(777)) unless -d $spec->{archdir};
5538              
5539             # .xs -> .bs
5540 18         602 $self->add_to_cleanup($spec->{bs_file});
5541 18 100       373 unless ($self->up_to_date($file, $spec->{bs_file})) {
5542 12         9821 require ExtUtils::Mkbootstrap;
5543 12         9336 $self->log_info("ExtUtils::Mkbootstrap::Mkbootstrap('$spec->{bs_file}')\n");
5544 12         368 ExtUtils::Mkbootstrap::Mkbootstrap($spec->{bs_file}); # Original had $BSLOADLIBS - what's that?
5545 12         4803 open(my $fh, '>>', $spec->{bs_file}); # create
5546 12         1001 utime((time)x2, $spec->{bs_file}); # touch
5547             }
5548              
5549             # .o -> .(a|bundle)
5550 18         709 $self->link_c($spec);
5551             }
5552              
5553             sub do_system {
5554 4046     4046 0 1628378 my ($self, @cmd) = @_;
5555 4046         68626 $self->log_verbose("@cmd\n");
5556              
5557             # Some systems proliferate huge PERL5LIBs, try to ameliorate:
5558 4046         9849 my %seen;
5559 4046         25021 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       49076 );
    50          
5566              
5567 4046         328656234 my $status = system(@cmd);
5568 4046 50 66     104602 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         745391 return !$status;
5574             }
5575              
5576             sub copy_if_modified {
5577 189     189 0 4594 my $self = shift;
5578 189 100       2255 my %args = (@_ > 3
5579             ? ( @_ )
5580             : ( from => shift, to_dir => shift, flatten => shift )
5581             );
5582             $args{verbose} = !$self->quiet
5583 189 100       1519 unless exists $args{verbose};
5584              
5585 189         662 my $file = $args{from};
5586 189 50 33     1619 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       2392 $args{flatten} = 1 if File::Spec->file_name_is_absolute( $file );
5592              
5593 189         583 my $to_path;
5594 189 100 66     1813 if (defined $args{to} and length $args{to}) {
    50 33        
5595 102         443 $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       1945 ? 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       1509 return if $self->up_to_date($file, $to_path); # Already fresh
5605              
5606             {
5607 144         521 local $self->{properties}{quiet} = 1;
  144         559  
5608 144         818 $self->delete_filetree($to_path); # delete destination if exists
5609             }
5610              
5611             # Create parent directories
5612 144         20697 File::Path::mkpath(File::Basename::dirname($to_path), 0, oct(777));
5613              
5614 144         1613 $self->log_verbose("Copying $file -> $to_path\n");
5615              
5616 144 50       1110 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       1393 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       55197 my $mode = oct(444) | ( $self->is_executable($file) ? oct(111) : 0 );
5625 144         2393 chmod( $mode, $to_path );
5626              
5627 144         1261 return $to_path;
5628             }
5629              
5630             sub up_to_date {
5631 314     314 0 1280 my ($self, $source, $derived) = @_;
5632 314 100       1504 $source = [$source] unless ref $source;
5633 314 100       1322 $derived = [$derived] unless ref $derived;
5634              
5635             # empty $derived means $source should always run
5636 314 100 66     3096 return 0 if @$source && !@$derived || grep {not -e} @$derived;
  313   100     9237  
5637              
5638 88         445 my $most_recent_source = time / (24*60*60);
5639 88         430 foreach my $file (@$source) {
5640 88 50       1301 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       674 $most_recent_source = -M _ if -M _ < $most_recent_source;
5645             }
5646              
5647 88         357 foreach my $derived (@$derived) {
5648 88 100       1222 return 0 if -M $derived > $most_recent_source;
5649             }
5650 87         1165 return 1;
5651             }
5652              
5653             sub dir_contains {
5654 35     35 0 983 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         502 ($first, $second) = map File::Spec->canonpath($_), ($first, $second);
5659 35         321 my @first_dirs = File::Spec->splitdir($first);
5660 35         274 my @second_dirs = File::Spec->splitdir($second);
5661              
5662 35 50       212 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   439 : sub {shift() eq shift()} );
  91         418  
5667              
5668 35         210 while (@first_dirs) {
5669 91 50       1286 return 0 unless $is_same->(shift @first_dirs, shift @second_dirs);
5670             }
5671              
5672 35         401 return 1;
5673             }
5674              
5675             1;
5676             __END__
5677              
5678              
5679             =head1 NAME
5680              
5681             Module::Build::Base - Default methods for Module::Build
5682              
5683             =head1 SYNOPSIS
5684              
5685             Please see the Module::Build documentation.
5686              
5687             =head1 DESCRIPTION
5688              
5689             The C<Module::Build::Base> module defines the core functionality of
5690             C<Module::Build>. Its methods may be overridden by any of the
5691             platform-dependent modules in the C<Module::Build::Platform::>
5692             namespace, but the intention here is to make this base module as
5693             platform-neutral as possible. Nicely enough, Perl has several core
5694             tools available in the C<File::> namespace for doing this, so the task
5695             isn't very difficult.
5696              
5697             Please see the C<Module::Build> documentation for more details.
5698              
5699             =head1 AUTHOR
5700              
5701             Ken Williams <kwilliams@cpan.org>
5702              
5703             =head1 COPYRIGHT
5704              
5705             Copyright (c) 2001-2006 Ken Williams. All rights reserved.
5706              
5707             This library is free software; you can redistribute it and/or
5708             modify it under the same terms as Perl itself.
5709              
5710             =head1 SEE ALSO
5711              
5712             perl(1), Module::Build(3)
5713              
5714             =cut