File Coverage

blib/lib/Module/Build/Base.pm
Criterion Covered Total %
statement 1896 2624 72.2
branch 763 1370 55.6
condition 195 406 48.0
subroutine 264 320 82.5
pod 0 195 0.0
total 3118 4915 63.4


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