| 1 | package ExtUtils::Builder::ParseXS; | ||
| 2 | $ExtUtils::Builder::ParseXS::VERSION = '0.038'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent 'ExtUtils::Builder::Planner::Extension'; | ||
| 7 | |||
| 8 | use File::Basename qw/basename dirname/; | ||
| 9 | use File::Spec::Functions qw/abs2rel curdir catfile catdir splitdir splitpath/; | ||
| 10 | |||
| 11 | use ExtUtils::Builder::Util 'function'; | ||
| 12 | |||
| 13 | sub add_methods { | ||
| 14 | my ($self, $planner, %options) = @_; | ||
| 15 | |||
| 16 | my $config = $options{config} // ($planner->can('config') ? $planner->config : ExtUtils::Config->new); | ||
| 17 | |||
| 18 | $planner->add_delegate('parse_xs', sub { | ||
| 19 | my (undef, $source, $destination, %options) = @_; | ||
| 20 | |||
| 21 | my @actions; | ||
| 22 | my $dirname = dirname($destination); | ||
| 23 | if ($options{mkdir}) { | ||
| 24 | push @actions, function( | ||
| 25 | module => 'File::Path', | ||
| 26 | function => 'make_path', | ||
| 27 | arguments => [ $dirname ], | ||
| 28 | exports => 'explicit', | ||
| 29 | message => "mkdir $dirname", | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | my %args = ( | ||
| 33 | filename => $source, | ||
| 34 | output => $destination, | ||
| 35 | prototypes => 0, | ||
| 36 | die_on_error => 1, | ||
| 37 | ); | ||
| 38 | $args{$_} = $options{$_} for grep { defined $options{$_} } qw/typemap hiertype versioncheck linenumbers optimize prototypes/; | ||
| 39 | $args{typemap} = [ $args{typemap} ] if defined $args{typemap} and not ref $args{typemap}; | ||
| 40 | |||
| 41 | push @actions, function( | ||
| 42 | module => 'ExtUtils::ParseXS', | ||
| 43 | function => 'process_file', | ||
| 44 | arguments => [ %args ], | ||
| 45 | message => "parse-xs $source", | ||
| 46 | ); | ||
| 47 | |||
| 48 | my @dependencies = @{ $options{dependencies} // [] }; | ||
| 49 | for my $dir ($dirname, curdir) { | ||
| 50 | my $filename = catfile($dir, 'typemap'); | ||
| 51 | push @dependencies, $filename if -f $filename; | ||
| 52 | } | ||
| 53 | push @dependencies, @{ $args{typemap} } if $args{typemap}; | ||
| 54 | |||
| 55 | $planner->create_node( | ||
| 56 | target => $destination, | ||
| 57 | dependencies => [ $source, @dependencies ], | ||
| 58 | actions => \@actions, | ||
| 59 | ); | ||
| 60 | }); | ||
| 61 | |||
| 62 | $planner->add_delegate('c_file_for_xs', sub { | ||
| 63 | my (undef, $source, $outdir) = @_; | ||
| 64 | $outdir //= dirname($source); | ||
| 65 | my $file_base = basename($source, '.xs'); | ||
| 66 | return catfile($outdir, "$file_base.c"); | ||
| 67 | }); | ||
| 68 | |||
| 69 | $planner->add_delegate('module_for_xs', sub { | ||
| 70 | my (undef, $source, $relative) = @_; | ||
| 71 | my (undef, $dirs, $file) = splitpath(abs2rel($source, $relative)); | ||
| 72 | return join '::', splitdir($dirs), basename($file, '.xs', '.c'); | ||
| 73 | }); | ||
| 74 | |||
| 75 | require DynaLoader; | ||
| 76 | my $mod2fname = defined &DynaLoader::mod2fname ? \&DynaLoader::mod2fname : sub { return $_[0][-1] }; | ||
| 77 | |||
| 78 | $planner->add_delegate('extension_filename', sub { | ||
| 79 | my (undef, $module) = @_; | ||
| 80 | my @parts = split '::', $module; | ||
| 81 | my $archdir = catdir(qw/blib arch auto/, @parts); | ||
| 82 | |||
| 83 | my $basename = $mod2fname->(\@parts); | ||
| 84 | my $filename = "$basename." . $config->get('dlext'); | ||
| 85 | return catfile($archdir, $filename); | ||
| 86 | }); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||
| 91 | # ABSTRACT: Essential functions for implementing XS in a Plan | ||
| 92 | |||
| 93 | __END__ |