ExtUtils::MakeMaker::FAQ - Frequently Asked Questions About MakeMaker |
ExtUtils::MakeMaker::FAQ - Frequently Asked Questions About MakeMaker
FAQs, tricks and tips for ExtUtils::MakeMaker
.
There are many reasons, but the major one is cross-platform compatibility.
Perl is one of the most ported pieces of software ever. It works on operating systems I've never even heard of (see perlport for details). It needs a build tool that can work on all those platforms and with any wacky C compilers they might have.
No such build tool existed at the time and I only know of one now (Module::Build).
Module::Build is the official heir apparent to MakeMaker and we encourage people to work on M::B rather than spending time improving MakeMaker.
Simplest way to do it automatically is to use your version control system's revision number (you are using version control, right?).
In CVS and RCS you use $Revision$ writing it like so:
$VERSION = sprintf "%d.%03d", q$Revision: 1.9 $ =~ /(\d+)/g;
Every time the file is checked in the $Revision$ will be updated, updating your $VERSION.
In CVS version 1.9 is followed by 1.10. Since CPAN compares version
numbers numerically we use a sprintf()
to convert 1.9 to 1.009 and
1.10 to 1.010 which compare properly.
If branches are involved (ie. $Revision: 1.5.3.4) its a little more complicated.
# must be all on one line or MakeMaker will get confused. $VERSION = do { my @r = (q$Revision: 1.9 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
To shut off its generation, pass the NO_META
flag to WriteMakefile()
.
To avoid this, you can force the Makefile to be rebuilt whenever you
change the module containing the version number by adding this to your
WriteMakefile()
arguments.
depend => { '$(FIRST_MAKEFILE)' => '$(VERSION_FROM)' }
Let's assume that we have a package Cool::Foo
, which includes
Cool::Foo
and Cool::Bar
modules each having a separate XS
file. First we use the following Makefile.PL:
use ExtUtils::MakeMaker;
WriteMakefile( NAME => 'Cool::Foo', VERSION_FROM => 'Foo.pm', OBJECT => q/$(O_FILES)/, # ... other attrs ... );
Notice the OBJECT
attribute. MakeMaker generates the following
variables in Makefile:
# Handy lists of source code files: XS_FILES= Bar.xs \ Foo.xs C_FILES = Bar.c \ Foo.c O_FILES = Bar.o \ Foo.o
Therefore we can use the O_FILES
variable to tell MakeMaker to use
these objects into the shared library.
That's pretty much it. Now write Foo.pm and Foo.xs, Bar.pm and Bar.xs, where Foo.pm bootstraps the shared library and Bar.pm simply loading Foo.pm.
The only issue left is to how to bootstrap Bar.xs. This is done from Foo.xs:
MODULE = Cool::Foo PACKAGE = Cool::Foo
BOOT: # boot the second XS file boot_Cool__Bar(aTHX_ cv);
If you have more than two files, this is the place where you should boot extra XS files from.
The following four files sum up all the details discussed so far.
Foo.pm: ------- package Cool::Foo;
require DynaLoader;
our @ISA = qw(DynaLoader); our $VERSION = '0.01'; bootstrap Cool::Foo $VERSION;
1;
Bar.pm: ------- package Cool::Bar;
use Cool::Foo; # bootstraps Bar.xs
1;
Foo.xs: ------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h"
MODULE = Cool::Foo PACKAGE = Cool::Foo
BOOT: # boot the second XS file boot_Cool__Bar(aTHX_ cv);
MODULE = Cool::Foo PACKAGE = Cool::Foo PREFIX = cool_foo_
void cool_foo_perl_rules()
CODE: fprintf(stderr, "Cool::Foo says: Perl Rules\n");
Bar.xs: ------- #include "EXTERN.h" #include "perl.h" #include "XSUB.h"
MODULE = Cool::Bar PACKAGE = Cool::Bar PREFIX = cool_bar_
void cool_bar_perl_rules()
CODE: fprintf(stderr, "Cool::Bar says: Perl Rules\n");
And of course a very basic test:
test.pl: -------- use Test; BEGIN { plan tests => 1 }; use Cool::Foo; use Cool::Bar; Cool::Foo::perl_rules(); Cool::Bar::perl_rules(); ok 1;
This tip has been brought to you by Nick Ing-Simmons and Stas Bekman.
If you have a question you'd like to see added to the FAQ (whether or not you have the answer) please send it to makemaker@perl.org.
The denizens of makemaker@perl.org.
the ExtUtils::MakeMaker manpage
ExtUtils::MakeMaker::FAQ - Frequently Asked Questions About MakeMaker |