| 1 | package Dist::Zilla::Plugin::ModuleBuild::OptionalXS; | ||
| 2 | # ABSTRACT: Build a module that has an XS component that only optionally needs to be built. | ||
| 3 | our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY | ||
| 4 | our $VERSION = '0.01'; # VERSION | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | |||
| 8 | extends 'Dist::Zilla::Plugin::ModuleBuild'; | ||
| 9 | |||
| 10 | my $pp_check = <<'EOF'; | ||
| 11 | my $build_xs; | ||
| 12 | $build_xs = 0 if grep { $_ eq '--pp' } @ARGV; | ||
| 13 | $build_xs = 1 if grep { $_ eq '--xs' } @ARGV; | ||
| 14 | if (!defined $build_xs) { | ||
| 15 | $build_xs = $build->have_c_compiler; | ||
| 16 | } | ||
| 17 | |||
| 18 | unless ($build_xs) { | ||
| 19 | $build->build_elements( | ||
| 20 | [ grep { $_ ne 'xs' } @{ $build->build_elements } ] | ||
| 21 | ); | ||
| 22 | } | ||
| 23 | EOF | ||
| 24 | |||
| 25 | after setup_installer => sub { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | my ($file) = grep { $_->name eq 'Build.PL' } @{ $self->zilla->files }; | ||
| 29 | my $content = $file->content; | ||
| 30 | |||
| 31 | $content =~ s/(\$build->create_build_script;)/$pp_check$1/; | ||
| 32 | |||
| 33 | $file->content($content); | ||
| 34 | }; | ||
| 35 | |||
| 36 | around module_build_args => sub { | ||
| 37 | my $orig = shift; | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | my $args = $self->$orig(@_); | ||
| 41 | |||
| 42 | $args->{c_source} = 'c'; | ||
| 43 | $args->{need_compiler} = 0; | ||
| 44 | |||
| 45 | return $args; | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | __PACKAGE__->meta->make_immutable; | ||
| 50 | |||
| 51 | |||
| 52 | __END__ |