Zaki's Hacktivity

I'll think of a subtitle later...

Using Alien::Base, Dist::Zilla, and EU::MM

Since I use Dist::Zilla to help manage my Perl distributions, I wanted to use it with the XS package that I am working on. This post is just a small note how how to do that if you are using Alien::Base to build your native library.

Dist::Zilla usually writes it’s own Makefile.PL so that ExtUtils::MakeMaker will know how to build, test, and install the code. However, since I’m using Alien::Base, I need to pass the compiler and linker flags to ExtUtils::MakeMaker as well. To do that, I grabbed the Dist::Zilla::Plugin::MakeMaker::Awesome plugin. Setting that up in your dist.ini is relatively straightforward:

(dist.ini) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
; authordep Dist::Zilla::Plugin::MakeMaker::Awesome

[Prereqs / Develop ]
-phase = develop
Dist::Zilla::Plugin::MakeMaker::Awesome = 0

[@Filter]
-bundle = @Basic
-remove = MakeMaker

[=inc::MyLibMakeMaker]

[Prereqs]
Alien::MyLib = 0

The line

1
[=inc::MyLibMakeMaker]

specifies that the code that will be used to generate the Makefile.PL will be in a module called inc/MyLibMakeMaker.pm. Now, in that file, I’ll need to specify the compilation flags by calling the cflags and libs methods on my Alien::Base subclass (Alien::MyLib). But this needs to happen when Makefile.PL is run by the user, not when Dist::Zilla writes out the file. The following code does that by appending our own options to the string we write out to in Makefile.PL.

(MyLibMakeMaker.pm) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package inc::MyLibMakeMaker;
use Moose;

extends 'Dist::Zilla::Plugin::MakeMaker::Awesome';

override _build_WriteMakefile_dump => sub {
  my $str = super();
  $str .= <<'END';
$WriteMakefileArgs{CONFIGURE} = sub {
  require Alien::MyLib;
  my $alien_mylib = Alien::MyLib->new;
  +{ CCFLAGS => $alien_mylib->cflags, LIBS => $alien_mylib->libs };
};
END
  $str;
};

__PACKAGE__->meta->make_immutable;

We use the CONFIGURE option to set CCFLAGS and LIBS instead of setting CCFLAGS and LIBS directly because these need to be set after the Alien::MyLib prerequisite has been met.

Comments