forked from hdp/module-build
/
Base.pm
2784 lines (2215 loc) · 77.5 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Module::Build::Base;
use strict;
BEGIN { require 5.00503 }
use Config;
use File::Copy ();
use File::Find ();
use File::Path ();
use File::Basename ();
use File::Spec ();
use File::Compare ();
use Data::Dumper ();
use IO::File ();
use Text::ParseWords ();
#################### Constructors ###########################
sub new {
my $self = shift()->_construct(@_);
$self->cull_args(@ARGV);
die "Too early to specify a build action '$self->{action}'. Do 'Build $self->{action}' instead.\n"
if $self->{action};
$self->_set_install_paths;
$self->dist_name;
$self->check_manifest;
$self->check_prereq;
$self->set_autofeatures;
$self->dist_version;
return $self;
}
sub resume {
my $self = shift()->_construct(@_);
$self->read_config;
# If someone called Module::Build->current() or
# Module::Build->new_from_context() and the correct class to use is
# actually a *subclass* of Module::Build, we may need to load that
# subclass here and re-delegate the resume() method to it.
unless ( UNIVERSAL::isa($self, $self->build_class) ) {
my $build_class = $self->build_class;
my $config_dir = $self->config_dir || '_build';
my $build_lib = File::Spec->catdir( $config_dir, 'lib' );
unshift( @INC, $build_lib );
unless ( $build_class->can('new') ) {
eval "require $build_class; 1" or die "Failed to re-load '$build_class': $@";
}
return $build_class->resume(@_);
}
unless ($self->_perl_is_same($self->{properties}{perl})) {
my $perl = $self->find_perl_interpreter;
warn(" * WARNING: Configuration was initially created with '$self->{properties}{perl}',\n".
" but we are now using '$perl'.\n");
}
my $mb_version = $Module::Build::VERSION;
die(" * ERROR: Configuration was initially created with Module::Build version '$self->{properties}{mb_version}',\n".
" but we are now using version '$mb_version'. Please re-run the Build.PL or Makefile.PL script.\n")
unless $mb_version eq $self->{properties}{mb_version};
$self->cull_args(@ARGV);
$self->{action} ||= 'build';
return $self;
}
sub new_from_context {
my ($package, %args) = @_;
# XXX Read the META.yml and see whether we need to run the Build.PL
# Run the Build.PL
$package->run_perl_script('Build.PL', [], [$package->unparse_args(\%args)]);
return $package->resume;
}
sub current {
# hmm, wonder what the right thing to do here is
local @ARGV;
return shift()->resume;
}
sub _construct {
my ($package, %input) = @_;
my $args = delete $input{args} || {};
my $config = delete $input{config} || {};
my $self = bless {
args => {%$args},
config => {%Config, %$config},
properties => {
module_name => '',
build_script => 'Build',
base_dir => $package->cwd,
config_dir => '_build',
blib => 'blib',
requires => {},
recommends => {},
build_requires => {},
conflicts => {},
mb_version => $Module::Build::VERSION,
build_elements => [qw( PL support pm xs pod script )],
installdirs => 'site',
install_path => {},
include_dirs => [],
create_packlist => 1,
%input,
},
}, $package;
my ($p, $c) = ($self->{properties}, $self->{config});
$p->{build_class} ||= ref $self;
# The following warning could be unnecessary if the user is running
# an embedded perl, but there aren't too many of those around, and
# embedded perls aren't usually used to install modules, and the
# installation process sometimes needs to run external scripts
# (e.g. to run tests).
$p->{perl} = $self->find_perl_interpreter
or warn "Warning: Can't locate your perl binary";
$p->{bindoc_dirs} ||= [ "$p->{blib}/script" ];
$p->{libdoc_dirs} ||= [ "$p->{blib}/lib", "$p->{blib}/arch" ];
$p->{dist_author} = [ $p->{dist_author} ] if exists $p->{dist_author} and not ref $p->{dist_author};
# Synonyms
$p->{requires} = delete $p->{prereq} if exists $p->{prereq};
$p->{script_files} = delete $p->{scripts} if exists $p->{scripts};
$self->add_to_cleanup( @{delete $p->{add_to_cleanup}} )
if $p->{add_to_cleanup};
return $self;
}
################## End constructors #########################
sub _set_install_paths {
my $self = shift;
my $c = $self->{config};
my @html = $c->{installhtmldir} ? (html => $c->{installhtmldir}) : ();
$self->{properties}{install_sets} =
{
core => {
lib => $c->{installprivlib},
arch => $c->{installarchlib},
bin => $c->{installbin},
script => $c->{installscript},
bindoc => $c->{installman1dir},
libdoc => $c->{installman3dir},
@html,
},
site => {
lib => $c->{installsitelib},
arch => $c->{installsitearch},
bin => $c->{installsitebin} || $c->{installbin},
script => $c->{installsitescript} || $c->{installsitebin} || $c->{installscript},
bindoc => $c->{installsiteman1dir} || $c->{installman1dir},
libdoc => $c->{installsiteman3dir} || $c->{installman3dir},
@html,
},
vendor => {
lib => $c->{installvendorlib},
arch => $c->{installvendorarch},
bin => $c->{installvendorbin} || $c->{installbin},
script => $c->{installvendorscript} || $c->{installvendorbin} || $c->{installscript},
bindoc => $c->{installvendorman1dir} || $c->{installman1dir},
libdoc => $c->{installvendorman3dir} || $c->{installman3dir},
@html,
},
};
}
sub cwd {
require Cwd;
return Cwd::cwd();
}
sub _perl_is_same {
my ($self, $perl) = @_;
return `$perl -MConfig=myconfig -e print -e myconfig` eq Config->myconfig;
}
sub find_perl_interpreter {
return $^X if File::Spec->file_name_is_absolute($^X);
my $proto = shift;
my $c = ref($proto) ? $proto->{config} : \%Config::Config;
my $exe = $c->{exe_ext};
my $thisperl = $^X;
if ($proto->os_type eq 'VMS') {
# VMS might have a file version at the end
$thisperl .= $exe unless $thisperl =~ m/$exe(;\d+)?$/i;
} elsif (defined $exe) {
$thisperl .= $exe unless $thisperl =~ m/$exe$/i;
}
foreach my $perl ( $c->{perlpath},
map File::Spec->catfile($_, $thisperl), File::Spec->path()
) {
return $perl if -f $perl and $proto->_perl_is_same($perl);
}
return;
}
sub base_dir { shift()->{properties}{base_dir} }
sub installdirs { shift()->{properties}{installdirs} }
sub _is_interactive {
return -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; # Pipe?
}
sub prompt {
my $self = shift;
my ($mess, $def) = @_;
die "prompt() called without a prompt message" unless @_;
($def, my $dispdef) = defined $def ? ($def, "[$def] ") : ('', ' ');
{
local $|=1;
print "$mess $dispdef";
}
my $ans;
if ($self->_is_interactive) {
$ans = <STDIN>;
if ( defined $ans ) {
chomp $ans;
} else { # user hit ctrl-D
print "\n";
}
}
unless (defined($ans) and length($ans)) {
print "$def\n";
$ans = $def;
}
return $ans;
}
sub y_n {
my $self = shift;
die "y_n() called without a prompt message" unless @_;
my $interactive = $self->_is_interactive;
my $answer;
while (1) {
$answer = $self->prompt(@_);
return 1 if $answer =~ /^y/i;
return 0 if $answer =~ /^n/i;
die "No y/n answer given, no default supplied, and no user to ask again" unless $interactive;
print "Please answer 'y' or 'n'.\n";
}
}
sub _general_notes {
my $self = shift;
my $type = shift;
return $self->_persistent_hash_read($type) unless @_;
my $key = shift;
return $self->_persistent_hash_read($type, $key) unless @_;
my $value = shift;
$self->has_config_data(1) if $type =~ /^(config_data|features)$/;
return $self->_persistent_hash_write($type, { $key => $value });
}
sub notes { shift()->_general_notes('notes', @_) }
sub config_data { shift()->_general_notes('config_data', @_) }
sub feature { shift()->_general_notes('features', @_) }
sub add_build_element {
my $self = shift;
push @{$self->build_elements}, shift;
}
sub ACTION_config_data {
my $self = shift;
return unless $self->has_config_data;
my $module_name = $self->module_name
or die "The config_data feature requires that 'module_name' be set";
my $notes_name = $module_name . '::ConfigData';
my $notes_pm = File::Spec->catfile($self->blib, 'lib', split /::/, "$notes_name.pm");
return if $self->up_to_date([$self->config_file('config_data'), $self->config_file('features')], $notes_pm);
print "Writing config notes to $notes_pm\n";
File::Path::mkpath(File::Basename::dirname($notes_pm));
my $fh = IO::File->new("> $notes_pm") or die "Can't create '$notes_pm': $!";
printf $fh <<'EOF', $notes_name;
package %s;
use strict;
my $arrayref = eval do {local $/; <DATA>}
or die "Couldn't load ConfigData data: $@";
close DATA;
my ($config, $features) = @$arrayref;
sub config { $config->{$_[1]} }
sub feature { $features->{$_[1]} }
sub set_config { $config->{$_[1]} = $_[2] }
sub set_feature { $features->{$_[1]} = 0+!!$_[2] }
sub feature_names { keys %%$features }
sub config_names { keys %%$config }
sub write {
my $me = __FILE__;
require IO::File;
require Data::Dumper;
my $mode_orig = (stat $me)[2] & 07777;
chmod($mode_orig | 0222, $me); # Make it writeable
my $fh = IO::File->new($me, 'r+') or die "Can't rewrite $me: $!";
seek($fh, 0, 0);
while (<$fh>) {
last if /^__DATA__$/;
}
die "Couldn't find __DATA__ token in $me" if eof($fh);
local $Data::Dumper::Terse = 1;
seek($fh, tell($fh), 0);
$fh->print( Data::Dumper::Dumper([$config, $features]) );
truncate($fh, tell($fh));
$fh->close;
chmod($mode_orig, $me)
or warn "Couldn't restore permissions on $me: $!";
}
EOF
printf $fh <<"EOF", $notes_name, $module_name;
=head1 NAME
$notes_name - Configuration for $module_name
=head1 SYNOPSIS
use $notes_name;
\$value = $notes_name->config('foo');
\$value = $notes_name->feature('bar');
\@names = $notes_name->config_names;
\@names = $notes_name->feature_names;
$notes_name->set_config(foo => \$new_value);
$notes_name->set_feature(bar => \$new_value);
$notes_name->write; # Save changes
=head1 DESCRIPTION
This module holds the configuration data for the C<$module_name>
module. It also provides a programmatic interface for getting or
setting that configuration data. Note that in order to actually make
changes, you'll have to have write access to the C<$notes_name>
module, and you should attempt to understand the repercussions of your
actions.
=head1 METHODS
=over 4
=item config(\$name)
Given a string argument, returns the value of the configuration item
by that name, or C<undef> if no such item exists.
=item feature(\$name)
Given a string argument, returns the value of the feature by that
name, or C<undef> if no such feature exists.
=item set_config(\$name, \$value)
Sets the configuration item with the given name to the given value.
The value may be any Perl scalar that will serialize correctly using
C<Data::Dumper>. This includes references, objects (usually), and
complex data structures. It probably does not include transient
things like filehandles or sockets.
=item set_feature(\$name, \$value)
Sets the feature with the given name to the given boolean value. The
value will be converted to 0 or 1 automatically.
=item config_names()
Returns a list of all the names of config items currently defined in
C<$notes_name>, or in scalar context the number of items.
=item feature_names()
Returns a list of all the names of features currently defined in
C<$notes_name>, or in scalar context the number of features.
=item write()
Commits any changes from C<set_config()> and C<set_feature()> to disk.
Requires write access to the C<$notes_name> module.
=back
=head1 AUTHOR
C<$notes_name> was automatically created using C<Module::Build>.
C<Module::Build> was written by Ken Williams, but he holds no
authorship claim or copyright claim to the contents of C<$notes_name>.
=cut
__DATA__
EOF
local $Data::Dumper::Terse = 1;
print $fh Data::Dumper::Dumper([scalar $self->config_data, scalar $self->feature]);
}
{
# XXX huge hack alert - will revisit this later
my %valid_properties = map {$_ => 1}
qw(
build_class
module_name
dist_name
dist_version
dist_version_from
dist_author
dist_abstract
requires
recommends
license
pm_files
xs_files
pod_files
PL_files
scripts
script_files
test_files
recursive_test_files
perl
config_dir
blib
has_config_data
build_script
build_elements
install_sets
install_path
install_base
installdirs
destdir
debugger
verbose
c_source
autosplit
create_makefile_pl
create_readme
pollute
extra_compiler_flags
include_dirs
bindoc_dirs
libdoc_dirs
get_options
create_packlist
);
sub valid_property { exists $valid_properties{$_[1]} }
sub valid_properties { keys %valid_properties }
# Create an accessor for each property that doesn't already have one
foreach my $property (keys %valid_properties) {
next if __PACKAGE__->can($property);
no strict 'refs';
*{$property} = sub {
my $self = shift;
$self->{properties}{$property} = shift if @_;
return $self->{properties}{$property};
};
}
}
sub extra_compiler_flags {
my $self = shift;
my $p = $self->{properties};
$p->{extra_compiler_flags} = [@_] if @_;
return ref($p->{extra_compiler_flags}) ? $p->{extra_compiler_flags} : [$p->{extra_compiler_flags}];
}
# XXX Problem - if Module::Build is loaded from a different directory,
# it'll look for (and perhaps destroy/create) a _build directory.
sub subclass {
my ($pack, %opts) = @_;
my $build_dir = '_build'; # XXX The _build directory is ostensibly settable by the user. Shouldn't hard-code here.
$pack->delete_filetree($build_dir) if -e $build_dir;
die "Must provide 'code' or 'class' option to subclass()\n"
unless $opts{code} or $opts{class};
$opts{code} ||= '';
$opts{class} ||= 'MyModuleBuilder';
my $filename = File::Spec->catfile($build_dir, 'lib', split '::', $opts{class}) . '.pm';
my $filedir = File::Basename::dirname($filename);
print "Creating custom builder $filename in $filedir\n";
File::Path::mkpath($filedir);
die "Can't create directory $filedir: $!" unless -d $filedir;
my $fh = IO::File->new("> $filename") or die "Can't create $filename: $!";
print $fh <<EOF;
package $opts{class};
use Module::Build;
\@ISA = qw(Module::Build);
$opts{code}
1;
EOF
close $fh;
push @INC, File::Spec->catdir(File::Spec->rel2abs($build_dir), 'lib');
eval "use $opts{class}";
die $@ if $@;
return $opts{class};
}
sub dist_name {
my $self = shift;
my $p = $self->{properties};
return $p->{dist_name} if exists $p->{dist_name};
die "Can't determine distribution name, must supply either 'dist_name' or 'module_name' parameter"
unless $p->{module_name};
($p->{dist_name} = $p->{module_name}) =~ s/::/-/g;
return $p->{dist_name};
}
sub dist_version {
my ($self) = @_;
my $p = $self->{properties};
return $p->{dist_version} if exists $p->{dist_version};
if ($self->module_name) {
$p->{dist_version_from} ||= join( '/', 'lib', split '::', $self->module_name ) . '.pm';
}
die ("Can't determine distribution version, must supply either 'dist_version',\n".
"'dist_version_from', or 'module_name' parameter")
unless $p->{dist_version_from};
my $version_from = File::Spec->catfile( split '/', $p->{dist_version_from} );
return $p->{dist_version} = $self->version_from_file($version_from);
}
sub dist_author { shift->_pod_parse('author') }
sub dist_abstract { shift->_pod_parse('abstract') }
sub _pod_parse {
my ($self, $part) = @_;
my $p = $self->{properties};
my $member = "dist_$part";
return $p->{$member} if exists $p->{$member};
return unless $p->{dist_version_from};
my $fh = IO::File->new($p->{dist_version_from}) or return;
require Module::Build::PodParser;
my $parser = Module::Build::PodParser->new(fh => $fh);
my $method = "get_$part";
return $p->{$member} = $parser->$method();
}
sub find_module_by_name {
my ($self, $mod, $dirs) = @_;
my $file = File::Spec->catfile(split '::', $mod);
foreach (@$dirs) {
my $testfile = File::Spec->catfile($_, $file);
return $testfile if -e $testfile and !-d _; # For stuff like ExtUtils::xsubpp
return "$testfile.pm" if -e "$testfile.pm";
}
return;
}
sub _next_code_line {
my ($self, $fh, $pat) = @_;
my $inpod = 0;
local $_;
while (<$fh>) {
$inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
next if $inpod || /^\s*#/;
return wantarray ? ($_, /$pat/) : $_
if $_ =~ $pat;
}
return;
}
sub version_from_file {
my ($self, $file) = @_;
# Some of this code came from the ExtUtils:: hierarchy.
my $fh = IO::File->new($file) or die "Can't open '$file' for version: $!";
my $match = qr/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/;
my ($v_line, $sigil, $var) = $self->_next_code_line($fh, $match) or return undef;
my $eval = qq{q# Hide from _packages_inside()
#; package Module::Build::Base::_version;
no strict;
local $sigil$var;
\$$var=undef; do {
$v_line
}; \$$var
};
local $^W;
# version.pm will change the ->VERSION method, so we mitigate the
# potential effects here. Unfortunately local(*UNIVERSAL::VERSION)
# will crash perl < 5.8.1.
my $old_version = \&UNIVERSAL::VERSION;
eval {require version};
my $result = eval $eval;
*UNIVERSAL::VERSION = $old_version;
warn "Error evaling version line '$eval' in $file: $@\n" if $@;
# Unbless it if it's a version.pm object
$result = "$result" if UNIVERSAL::isa( $result, 'version' );
return $result;
}
sub _persistent_hash_write {
my ($self, $name, $href) = @_;
$href ||= {};
my $ph = $self->{phash}{$name} ||= {disk => {}, new => {}};
@{$ph->{new}}{ keys %$href } = values %$href; # Merge
# Do some optimization to avoid unnecessary writes
foreach my $key (keys %{ $ph->{new} }) {
next if ref $ph->{new}{$key};
next if ref $ph->{disk}{$key} or !exists $ph->{disk}{$key};
delete $ph->{new}{$key} if $ph->{new}{$key} eq $ph->{disk}{$key};
}
if (my $file = $self->config_file($name)) {
return if -e $file and !keys %{ $ph->{new} }; # Nothing to do
@{$ph->{disk}}{ keys %{$ph->{new}} } = values %{$ph->{new}}; # Merge
$self->_write_dumper($name, $ph->{disk});
$ph->{new} = {};
}
return $self->_persistent_hash_read($name);
}
sub _persistent_hash_read {
my $self = shift;
my $name = shift;
my $ph = $self->{phash}{$name} ||= {disk => {}, new => {}};
if (@_) {
# Return 1 key as a scalar
my $key = shift;
return $ph->{new}{$key} if exists $ph->{new}{$key};
return $ph->{disk}{$key};
} else {
# Return all data
my $out = (keys %{$ph->{new}}
? {%{$ph->{disk}}, %{$ph->{new}}}
: $ph->{disk});
return wantarray ? %$out : $out;
}
}
sub _persistent_hash_restore {
my ($self, $name) = @_;
my $ph = $self->{phash}{$name} ||= {disk => {}, new => {}};
my $file = $self->config_file($name) or die "No config file '$name'";
my $fh = IO::File->new("< $file") or die "Can't read $file: $!";
$ph->{disk} = eval do {local $/; <$fh>};
die $@ if $@;
}
sub add_to_cleanup {
my $self = shift;
my %files = map {$self->localize_file_path($_), 1} @_;
$self->_persistent_hash_write('cleanup', \%files);
}
sub cleanup {
my $self = shift;
my $all = $self->_persistent_hash_read('cleanup');
return keys %$all;
}
sub config_file {
my $self = shift;
return unless -d $self->config_dir;
return File::Spec->catfile($self->config_dir, @_);
}
sub read_config {
my ($self) = @_;
my $file = $self->config_file('build_params');
my $fh = IO::File->new($file) or die "Can't read '$file': $!";
my $ref = eval do {local $/; <$fh>};
die if $@;
($self->{args}, $self->{config}, $self->{properties}) = @$ref;
close $fh;
for ('cleanup', 'notes', 'features', 'config_data') {
next unless -e $self->config_file($_);
$self->_persistent_hash_restore($_);
}
$self->has_config_data(1) if keys(%{$self->config_data}) || keys(%{$self->feature});
}
sub _write_dumper {
my ($self, $filename, $data) = @_;
my $file = $self->config_file($filename);
my $fh = IO::File->new("> $file") or die "Can't create '$file': $!";
local $Data::Dumper::Terse = 1;
print $fh Data::Dumper::Dumper($data);
}
sub write_config {
my ($self) = @_;
File::Path::mkpath($self->{properties}{config_dir});
-d $self->{properties}{config_dir} or die "Can't mkdir $self->{properties}{config_dir}: $!";
my @items = qw(requires build_requires conflicts recommends);
$self->_write_dumper('prereqs', { map { $_, $self->$_() } @items });
$self->_write_dumper('build_params', [$self->{args}, $self->{config}, $self->{properties}]);
$self->_persistent_hash_write($_) foreach qw(notes cleanup features config_data);
}
sub config { shift()->{config} }
sub requires { shift()->{properties}{requires} }
sub recommends { shift()->{properties}{recommends} }
sub build_requires { shift()->{properties}{build_requires} }
sub conflicts { shift()->{properties}{conflicts} }
sub set_autofeatures {
my ($self) = @_;
my $features = delete $self->{properties}{auto_features}
or return;
while (my ($name, $info) = each %$features) {
my $failures = $self->prereq_failures($info);
if ($failures) {
warn "Feature '$name' disabled because of the following prerequisite failures:\n";
foreach my $type (qw(requires build_requires conflicts recommends)) {
next unless $failures->{$type};
while (my ($module, $status) = each %{$failures->{$type}}) {
warn " * $status->{message}\n";
}
warn "\n";
}
$self->feature($name => 0);
} else {
warn "Feature '$name' enabled.\n\n";
$self->feature($name => 1);
}
}
}
sub prereq_failures {
my ($self, $info) = @_;
my @types = qw(requires recommends build_requires conflicts);
$info ||= {map {$_, $self->$_()} @types};
my $out;
foreach my $type (@types) {
my $prereqs = $info->{$type};
while ( my ($modname, $spec) = each %$prereqs ) {
my $status = $self->check_installed_status($modname, $spec);
if ($type eq 'conflicts') {
next if !$status->{ok};
$status->{conflicts} = delete $status->{need};
$status->{message} = "Installed version '$status->{have}' of $modname conflicts with this distribution";
} elsif ($type eq 'recommends') {
next if $status->{ok};
$status->{message} = ($status->{have} eq '<none>'
? "Optional prerequisite $modname isn't installed"
: "Version $status->{have} of $modname is installed, but we prefer to have $spec");
} else {
next if $status->{ok};
}
$out->{$type}{$modname} = $status;
}
}
return $out;
}
sub check_prereq {
my $self = shift;
my $failures = $self->prereq_failures;
return 1 unless $failures;
foreach my $type (qw(requires build_requires conflicts recommends)) {
next unless $failures->{$type};
my $prefix = $type eq 'recommends' ? '' : 'ERROR: ';
while (my ($module, $status) = each %{$failures->{$type}}) {
warn " * $prefix$status->{message}\n";
}
}
warn "ERRORS/WARNINGS FOUND IN PREREQUISITES. You may wish to install the versions\n".
" of the modules indicated above before proceeding with this installation.\n\n";
return 0;
}
sub perl_version {
my ($self) = @_;
# Check the current perl interpreter
# It's much more convenient to use $] here than $^V, but 'man
# perlvar' says I'm not supposed to. Bloody tyrant.
return $^V ? $self->perl_version_to_float(sprintf "%vd", $^V) : $];
}
sub perl_version_to_float {
my ($self, $version) = @_;
$version =~ s/\./../;
$version =~ s/\.(\d+)/sprintf '%03d', $1/eg;
return $version;
}
sub _parse_conditions {
my ($self, $spec) = @_;
if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
return (">= $spec");
} else {
return split /\s*,\s*/, $spec;
}
}
sub check_installed_status {
my ($self, $modname, $spec) = @_;
my %status = (need => $spec);
if ($modname eq 'perl') {
$status{have} = $self->perl_version;
} elsif (eval { no strict; $status{have} = ${"${modname}::VERSION"} }) {
# Don't try to load if it's already loaded
} else {
my $file = $self->find_module_by_name($modname, \@INC);
unless ($file) {
@status{ qw(have message) } = ('<none>', "Prerequisite $modname isn't installed");
return \%status;
}
$status{have} = $self->version_from_file($file);
if ($spec and !$status{have}) {
@status{ qw(have message) } = (undef, "Couldn't find a \$VERSION in prerequisite $modname");
return \%status;
}
}
my @conditions = $self->_parse_conditions($spec);
foreach (@conditions) {
my ($op, $version) = /^\s* (<=?|>=?|==|!=) \s* ([\w.]+) \s*$/x
or die "Invalid prerequisite condition '$_' for $modname";
$version = $self->perl_version_to_float($version)
if $modname eq 'perl';
next if $op eq '>=' and !$version; # Module doesn't have to actually define a $VERSION
unless ($self->compare_versions( $status{have}, $op, $version )) {
$status{message} = "Version $status{have} of $modname is installed, but we need version $op $version";
return \%status;
}
}
$status{ok} = 1;
return \%status;
}
sub compare_versions {
my $self = shift;
my ($v1, $op, $v2) = @_;
# for alpha versions - this doesn't cover all cases, but should work for most:
$v1 =~ s/_(\d+)\z/$1/;
$v2 =~ s/_(\d+)\z/$1/;
my $eval_str = "\$v1 $op \$v2";
my $result = eval $eval_str;
warn "error comparing versions: '$eval_str' $@" if $@;
return $result;
}
# I wish I could set $! to a string, but I can't, so I use $@
sub check_installed_version {
my ($self, $modname, $spec) = @_;
my $status = $self->check_installed_status($modname, $spec);
if ($status->{ok}) {
return $status->{have} if $status->{have} and $status->{have} ne '<none>';
return '0 but true';
}
$@ = $status->{message};
return 0;
}
sub make_executable {
# Perl's chmod() is mapped to useful things on various non-Unix
# platforms, so we use it in the base class even though it looks
# Unixish.
my $self = shift;
foreach (@_) {
my $current_mode = (stat $_)[2];
chmod $current_mode | 0111, $_;
}
}
sub _startperl { shift()->{config}{startperl} }
# Return any directories in @INC which are not in the default @INC for
# this perl. For example, stuff passed in with -I or loaded with "use lib".
sub _added_to_INC {
my $self = shift;
my %seen;
$seen{$_}++ foreach $self->_default_INC;
return grep !$seen{$_}++, @INC;
}
# Determine the default @INC for this Perl
sub _default_INC {
my $self = shift;
my $perl = ref($self) ? $self->perl : $self->find_perl_interpreter;
my @inc = `$perl -le "print for \@INC"`;
chomp @inc;
return @inc;
}
sub print_build_script {
my ($self, $fh) = @_;
my $build_package = ref($self);
my %q = map {$_, $self->$_()} qw(config_dir base_dir);
$q{base_dir} = Win32::GetShortPathName($q{base_dir}) if $^O eq 'MSWin32';
my @myINC = $self->_added_to_INC;
for (@myINC, values %q) {
$_ = File::Spec->canonpath( File::Spec->rel2abs($_) );
s/([\\\'])/\\$1/g;
}