depend on ::XS if a compiler is available
[gitmo/Package-Stash.git] / inc / MMPackageStash.pm
CommitLineData
354ce5a6 1package inc::MMPackageStash;
2use Moose;
3
4extends 'Dist::Zilla::Plugin::MakeMaker::Awesome';
5
6# XXX: this is pretty gross, it should be possible to clean this up later
7around _build_MakeFile_PL_template => sub {
8 my $orig = shift;
9 my $self = shift;
354ce5a6 10
079d40f5 11 # copied from M::I
12 my $can_cc = <<'CAN_CC';
13use Config ();
14use File::Spec ();
15
16# check if we can run some command
17sub can_run {
18 my ($cmd) = @_;
19
20 my $_cmd = $cmd;
21 return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd));
22
23 for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
24 next if $dir eq '';
25 my $abs = File::Spec->catfile($dir, $_[1]);
26 return $abs if (-x $abs or $abs = MM->maybe_command($abs));
27 }
28
29 return;
30}
31
32# can we locate a (the) C compiler
33sub can_cc {
34 my @chunks = split(/ /, $Config::Config{cc}) or return;
354ce5a6 35
079d40f5 36 # $Config{cc} may contain args; try to find out the program part
37 while (@chunks) {
38 return can_run("@chunks") || (pop(@chunks), next);
39 }
40
41 return;
42}
43CAN_CC
44
45 # copied out of moose
46 my $check_conflicts = <<'CHECK_CONFLICTS';
354ce5a6 47sub check_conflicts {
48 my %conflicts = (
dc378b60 49 'Class::MOP' => '1.08',
50 'MooseX::Role::WithOverloading' => '0.08',
b6f2c7d8 51 'namespace::clean' => '0.18',
354ce5a6 52 );
53 my $found = 0;
54 for my $mod ( sort keys %conflicts ) {
55 eval "require $mod";
56 next if $@;
57
58 my $installed = $mod->VERSION();
59 if ( $installed le $conflicts{$mod} ) {
60
61 print <<"EOF";
62
63***
64 This version of Package::Stash conflicts with the version of
65 $mod ($installed) you have installed.
66
67 You will need to upgrade $mod after installing
68 this version of Package::Stash.
69***
70
71EOF
72
73 $found = 1;
74 }
75 }
76
77 return unless $found;
78
79 # More or less copied from Module::Build
80 return if $ENV{PERL_MM_USE_DEFAULT};
81 return unless -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT));
82
83 sleep 4;
84}
85CHECK_CONFLICTS
86
079d40f5 87 my $template = $self->$orig(@_);
88
89 $template =~ s/(use ExtUtils::MakeMaker.*)/$1\n\ncheck_conflicts();/;
90 $template =~ s/(WriteMakefile\()/delete \$WriteMakefileArgs{PREREQ_PM}{'Package::Stash::XS'}\n unless can_cc();\n\n$1/;
91
92 return $template . $can_cc . $check_conflicts;
354ce5a6 93};
94
95__PACKAGE__->meta->make_immutable;
96no Moose;
97
981;