perlmoduleperl-xsmakemaker

What tools can help build an XS project?


I've recently started learning XS using perlxstut and the tutorial suggests that I create my module using the old h2xs tool to create an ExtUtils::MakeMaker-based project. However for pure Perl projects, h2xs/EUMM has long been disfavoured in favour of Module::Install, Module::Build or Dist::Zilla.

Is there a more modern way of creating XS projects? Can Module::Starter create XS projects? Can Module::Build or Dist::Zilla build XS projects? Their pod pages are silent on the matter.

On the flip side, does the criticism that was levelled at h2xs/EUMM apply to XS projects? If you need a C compiler anyway, is it reasonable to demand a make tool as well?

EDIT: I see this question answers my question about creating a project. I'd still like to know about building: is EUMM the only option, or are Module::Build and Dist::Zilla also capable of building XS?


Solution

  • It turns out that Module::Build is perfectly capable of compiling XS. Here is a complete Build.PL I managed to scrape together:

    use strict;
    use Module::Build;
    
    my $build = Module::Build->new(
        module_name  => 'Chocolate::Belgian',
        dynamic_config => 1,
        license      => 'perl',
        requires     => {
            'Module::Build' => '0.19', # xs
            'Test::More' => 0,
        },
        extra_compiler_flags => '-Iinclude',
        extra_linker_flags   => '',
        c_source     => 'src',
        needs_compiler => 1,
        xs_files     => {
            './Belgian.xs' => 'lib/Chocolate/Belgian.xs',
        },
    
       );
    
    $build->create_build_script;
    

    This will build a distribution with .h include files (such as ppport.h) in the include/ directory, .c source files in the src/ directory, and an .xs file corresponding to package Chocolate::Belgian in the project base directory.

    extra_compiler_flags corresponds to make CCFLAGS, while extra_linker_flags corresponds to LIBS (so you might want -lm there to link the C math library).