man Module::Build () - Build and install Perl modules
NAME
Module::Build - Build and install Perl modules
SYNOPSIS
Standard process for building & installing modules:
perl Build.PL ./Build ./Build test ./Build install
Or, if you're on a platform (like DOS or Windows) that doesn't like the ./ notation, you can do this:
perl Build.PL perl Build perl Build test perl Build install
DESCRIPTION
CWModule::Build is a system for building, testing, and installing Perl modules. It is meant to be an alternative to CWExtUtils::MakeMaker. Developers may alter the behavior of the module through subclassing in a much more straightforward way than with CWMakeMaker. It also does not require a CWmake on your system - most of the CWModule::Build code is pure-perl and written in a very cross-platform way. In fact, you don't even need a shell, so even platforms like MacOS (traditional) can use it fairly easily. Its only prerequisites are modules that are included with perl 5.6.0, and it works fine on perl 5.005 if you can install a few additional modules.
See MOTIVATIONS for more comparisons between CWExtUtils::MakeMaker and CWModule::Build.
To install CWModule::Build, and any other module that uses CWModule::Build for its installation process, do the following:
perl Build.PL # 'Build.PL' script creates the 'Build' script ./Build # Need ./ to ensure we're using this "Build" script ./Build test # and not another one that happens to be in the PATH ./Build install
This illustrates initial configuration and the running of three 'actions'. In this case the actions run are 'build' (the default action), 'test', and 'install'. Other actions defined so far include:
build fakeinstall config_data help clean html code install diff manifest dist ppd distcheck ppmdist distclean realclean distdir skipcheck distmeta test distsign testcover disttest testdb docs versioninstall
You can run the 'help' action for a complete list of actions.
When creating a CWBuild.PL script for a module, something like the following code will typically be used:
use Module::Build; my $build = Module::Build->new ( module_name => 'Foo::Bar', license => 'perl', requires => { 'perl' => '5.6.1', 'Some::Module' => '1.23', 'Other::Module' => '>= 1.2, != 1.5, < 2.0', }, ); $build->create_build_script;
A simple module could get away with something as short as this for its CWBuild.PL script:
use Module::Build; Module::Build->new( module_name => 'Foo::Bar', license => 'perl', )->create_build_script;
The model used by CWModule::Build is a lot like the CWMakeMaker metaphor, with the following correspondences:
In Module::Build In ExtUtils::MakeMaker --------------------------- ------------------------ Build.PL (initial script) Makefile.PL (initial script) Build (a short perl script) Makefile (a long Makefile) _build/ (saved state info) various config text in the Makefile
Any customization can be done simply by subclassing CWModule::Build and adding a method called (for example) CWACTION_test, overriding the default 'test' action. You could also add a method called CWACTION_whatever, and then you could perform the action CWBuild whatever.
For information on providing compatibility with CWExtUtils::MakeMaker, see Module::Build::Compat and <http://www.makemaker.org/wiki/index.cgi?ModuleBuildConversionGuide>.
METHODS
I list here some of the most important methods in CWModule::Build. Normally you won't need to deal with these methods unless you want to subclass CWModule::Build. But since one of the reasons I created this module in the first place was so that subclassing is possible (and easy), I will certainly write more docs as the interface stabilizes.
- new()
- Creates a new Module::Build object. Arguments to the new() method are listed below. Most arguments are optional, but you must provide either the CWmodule_name argument, or CWdist_name and one of CWdist_version or CWdist_version_from. In other words, you must provide enough information to determine both a distribution name and version.
- module_name
- The CWmodule_name is a shortcut for setting default values of CWdist_name and CWdist_version_from, reflecting the fact that the majority of CPAN distributions are centered around one main module. For instance, if you set CWmodule_name to CWFoo::Bar, then CWdist_name will default to CWFoo-Bar and CWdist_version_from will default to CWlib/Foo/Bar.pm. CWdist_version_from will in turn be used to set CWdist_version. Setting CWmodule_name won't override a CWdist_* parameter you specify explicitly.
- dist_name
- Specifies the name for this distribution. Most authors won't need to set this directly, they can use CWmodule_name to set CWdist_name to a reasonable default. However, some agglomerative distributions like CWlibwww-perl or CWbioperl have names that don't correspond directly to a module name, so CWdist_name can be set independently.
- dist_version
- Specifies a version number for the distribution. See CWmodule_name or CWdist_version_from for ways to have this set automatically from a CW$VERSION variable in a module. One way or another, a version number needs to be set.
- dist_version_from
-
Specifies a file to look for the distribution version in. Most
authors won't need to set this directly, they can use CWmodule_name
to set it to a reasonable default.
The version is extracted from the specified file according to the same
rules as CWExtUtils::MakeMaker and CWCPAN.pm. It involves finding
the first line that matches the regular expression
/([\$*])(([\w\:\']*)\bVERSION)\b.*\=/
, eval()-ing that line, then checking the value of the CW$VERSION variable. Quite ugly, really, but all the modules on CPAN depend on this process, so there's no real opportunity to change to something better. - license
- Specifies the licensing terms of your distribution. Valid options include:
- perl
- The distribution may be copied and redistributed under the same terms as perl itself (this is by far the most common licensing option for modules on CPAN). This is a dual license, in which the user may choose between either the GPL or the Artistic license.
- gpl
- The distribution is distributed under the terms of the Gnu General Public License (http://www.opensource.org/licenses/gpl-license.php).
- lgpl
- The distribution is distributed under the terms of the Gnu Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php).
- artistic
- The distribution is licensed under the Artistic License, as specified by the Artistic file in the standard perl distribution.
- bsd
- The distribution is licensed under the BSD License (http://www.opensource.org/licenses/bsd-license.php).
- open_source
- The distribution is licensed under some other Open Source Initiative-approved license listed at http://www.opensource.org/licenses/ .
- unrestricted
- The distribution is licensed under a license that is not approved by www.opensource.org but that allows distribution without restrictions.
- restrictive
- The distribution may not be redistributed without special permission from the author and/or copyright holder. Note that you must still include the terms of your license in your documentation - this field only lets automated tools figure out your licensing restrictions. Humans still need something to read. It is a fatal error to use a license other than the ones mentioned above. This is not because I wish to impose licensing terms on you - please let me know if you would like another license option to be added to the list. You may also use a license type of CWunknown if you don't wish to specify your terms (but this is usually not a good idea for you to do!). I just started out with a small set of licenses to keep things simple, figuring I'd let people with actual working knowledge in this area tell me what to do. So if that's you, drop me a line.
- requires
-
An optional CWrequires argument specifies any module prerequisites that
the current module depends on. The prerequisites are given in a hash
reference, where the keys are the module names and the values are
version specifiers:
requires => {Foo::Module => '2.4', Bar::Module => 0, Ken::Module => '>= 1.2, != 1.5, < 2.0', perl => '5.6.0'},
These four version specifiers have different effects. The value CW'2.4' means that at least version 2.4 of CWFoo::Module must be installed. The value CW0 means that any version of CWBar::Module is acceptable, even if CWBar::Module doesn't define a version. The more verbose value CW'>= 1.2, != 1.5, < 2.0' means that CWKen::Module's version must be at least 1.2, less than 2.0, and not equal to 1.5. The list of criteria is separated by commas, and all criteria must be satisfied. A special CWperl entry lets you specify the versions of the Perl interpreter that are supported by your module. The same version dependency-checking semantics are available, except that we also understand perl's new double-dotted version numbers. One note: currently CWModule::Build doesn't actually require the user to have dependencies installed, it just strongly urges. In the future we may require it. There's now a CWrecommends section for things that aren't absolutely required. Automated tools like CPAN.pm should refuse to install a module if one of its dependencies isn't satisfied, unless a force command is given by the user. If the tools are helpful, they should also offer to install the dependencies. A sysnonym for CWrequires is CWprereq, to help succour people transitioning from CWExtUtils::MakeMaker. The CWrequires term is preferred, but the CWprereq term will remain valid in future distributions. - recommends
- This is just like the CWrequires argument, except that modules listed in this section aren't essential, just a good idea. We'll just print a friendly warning if one of these modules aren't found, but we'll continue running. If a module is recommended but not required, all tests should still pass if the module isn't installed. This may mean that some tests will be skipped if recommended dependencies aren't present. Automated tools like CPAN.pm should inform the user when recommended modules aren't installed, and it should offer to install them if it wants to be helpful.
- build_requires
- Modules listed in this section are necessary to build and install the given module, but are not necessary for regular usage of it. This is actually an important distinction - it allows for tighter control over the body of installed modules, and facilitates correct dependency checking on binary/packaged distributions of the module.
- conflicts
- Modules listed in this section conflict in some serious way with the given module. CWModule::Build (or some higher-level tool) will refuse to install the given module if the given module/version is also installed.
- create_makefile_pl
- This parameter lets you use Module::Build::Compat during the CWdistdir (or CWdist) action to automatically create a Makefile.PL for compatibility with ExtUtils::MakeMaker. The parameter's value should be one of the styles named in the Module::Build::Compat documentation.
- create_readme
- This parameter tells Module::Build to automatically create a README file at the top level of your distribution. Currently it will simply use CWPod::Text on the file indicated by CWdist_version_from and put the result in the README file. This is by no means the only recommended style for writing a README, but it seems to be one common one used on the CPAN.
- c_source
- An optional CWc_source argument specifies a directory which contains C source files that the rest of the build may depend on. Any CW.c files in the directory will be compiled to object files. The directory will be added to the search path during the compilation and linking phases of any C or XS files.
- pm_files
-
An optional parameter specifying the set of CW.pm files in this
distribution, specified as a hash reference whose keys are the files'
locations in the distributions, and whose values are their logical
locations based on their package name, i.e. where they would be found
in a normal Module::Build-style distribution. This parameter is
mainly intended to support alternative layouts of files.
For instance, if you have an old-style MakeMaker distribution for a
module called CWFoo::Bar and a Bar.pm file at the top level of the
distribution, you could specify your layout in your CWBuild.PL like
this:
my $build = Module::Build->new ( module_name => 'Foo::Bar', ... pm_files => { 'Bar.pm' => 'lib/Foo/Bar.pm' }, );
Note that the values should include CWlib/, because this is where they would be found in a normal Module::Build-style distribution. Note also that the path specifications are always given in Unix-like format, not in the style of the local system. - pod_files
- Just like CWpm_files, but used for specifying the set of CW.pod files in your distribution.
- xs_files
- Just like CWpm_files, but used for specifying the set of CW.xs files in your distribution.
- PL_files
-
An optional parameter specifying a set of CW.PL files in your
distribution. These will be run as Perl scripts prior to processing
the rest of the files in your distribution. They are usually used as
templates for creating other files dynamically, so that a file like
CWlib/Foo/Bar.pm.PL might create the file CWlib/Foo/Bar.pm.
The files are specified with the CW.PL files as hash keys, and the
file(s) they generate as hash values, like so:
my $build = Module::Build->new ( module_name => 'Foo::Bar', ... PL_files => { 'lib/Bar.pm.PL' => 'lib/Bar.pm', 'lib/Foo.PL' => [ 'lib/Foo1.pm', 'lib/Foo2.pm' ], }, );
Note that the path specifications are always given in Unix-like format, not in the style of the local system. - script_files
- An optional parameter specifying a set of files that should be installed as executable perl scripts when the module is installed. May be given as an array reference of the files, or as a hash reference whose keys are the files (and whose values will currently be ignored). The default is to install no script files - in other words, there is no default location where Module::Build will look for script files to install. For backward compatibility, you may use the parameter CWscripts instead of CWscript_files. Please consider this usage deprecated, though it will continue to exist for several version releases.
- test_files
- An optional parameter specifying a set of files that should be used as CWTest::Harness-style regression tests to be run during the CWtest action. May be given as an array reference of the files, or as a hash reference whose keys are the files (and whose values will currently be ignored). If the argument is given as a single string (not in an array reference), that string will be treated as a CWglob() pattern specifying the files to use. The default is to look for a test.pl script in the top-level directory of the distribution, and any files matching the glob pattern CW*.t in the t/ subdirectory. If the CWrecursive_test_files property is true, then the CWt/ directory will be scanned recursively for CW*.t files.
- autosplit
- An optional CWautosplit argument specifies a file which should be run through the CWAutosplit::autosplit() function. If multiple files should be split, the argument may be given as an array of the files to split. In general I don't consider autosplitting a great idea, because it's not always clear that autosplitting achieves its intended performance benefits. It may even harm performance in environments like mod_perl, where as much as possible of a module's code should be loaded during startup.
- dynamic_config
- A boolean flag indicating whether the Build.PL file must be executed, or whether this module can be built, tested and installed solely from consulting its metadata file. The default value is 0, reflecting the fact that most of the modules on CPAN just need to be copied from one place to another. The main reason to set this to a true value is that your module performs some dynamic configuration as part of its build/install process. Currently CWModule::Build doesn't actually do anything with this flag - it's probably going to be up to tools like CWCPAN.pm to do something useful with it. It can potentially bring lots of security, packaging, and convenience improvements.
- add_to_cleanup
- An array reference of files to be cleaned up when the CWclean action is performed. See also the add_to_cleanup() method.
- sign
- If a true value is specified for this parameter, CWModule::Signature will be used (via the 'distsign' action) to create a SIGNATURE file for your distribution during the 'distdir' action, and to add the SIGNATURE file to the MANIFEST (therefore, don't add it yourself). The default value is false. In the future, the default may change to true if you have CWModule::Signature installed on your system.
- extra_compiler_flags
- extra_linker_flags
-
These parameters can contain array references (or strings, in which
case they will be split into arrays) to pass through to the compiler
and linker phases when compiling/linking C code. For example, to tell
the compiler that your code is , you might do:
my build = Module::Build->new( module_name => 'Spangly', extra_compiler_flags => ['-x', 'c++'], );
To link your XS code against glib you might write something like:my build = Module::Build->new( module_name => 'Spangly', dynamic_config => 1, extra_compiler_flags => scalar `glib-config --cflags`, extra_linker_flags => scalar `glib-config --libs`, );
- include_dirs
- Specifies any additional directories in which to search for C header files. May be given as a string indicating a single directory, or as a list reference indicating multiple directories.
- dist_author
- This should be something like John Doe <jdoe@example.com>, or if there are multiple authors, an anonymous array of strings may be specified. This is used when generating metadata for META.yml and PPD files. If this is not specified, then CWModule::Build looks at the module from which it gets the distribution's version. If it finds a POD section marked =head1 AUTHOR, then it uses the contents of this section.
- dist_abstract
- This should be a short description of the distribution. This is used when generating metadata for META.yml and PPD files. If it is not given then CWModule::Build looks in the POD of the module from which it gets the distribution's version. It looks for the first line matching CW$package\s-\s(.+), and uses the captured text as the abstract.
- auto_features
-
This parameter supports the setting of features (see
feature($name)) automatically based on a set of prerequisites. For
instance, for a module that could optionally use either MySQL or
PostgreSQL databases, you might use CWauto_features like this:
my $b = Module::Build->new ( ... other stuff here... auto_features => { pg_support => { description => "Interface with Postgres databases", requires => q{ DBD::Pg >= 23.3 && DateTime::Format::Pg }, }, mysql_support => { description => "Interface with MySQL databases", requires => q{ DBD::mysql >= 17.9 && DateTime::Format::Pg }, }, );
For each feature named, the prerequisite options will be checked, and if there are no failures, the feature will be enabled (set to CW1). Otherwise the failures will be displayed to the user and the feature will be disabled (set to CW0). - get_options
- You can pass arbitrary command-line options to Build.PL or Build, and they will be stored in the Module::Build object and can be accessed via the CWargs() method. However, sometimes you want more flexibility out of your argument processing than this allows. In such cases, use the CWget_options parameter to pass in a hash reference of argument specifications, and the list of arguments to Build.PL or Build will be processed according to those specifications before they're passed on to CWModule::Build's own argument processing. The supported option specification hash keys are:
- type
- The type of option. The types are those supported by Getopt::Long; consult its documentation for a complete list. Typical types are CW=s for strings, CW+ for additive options, and CW! for negatable options. If the type is not specified, it will be considered a boolean, i.e. no argument is taken and a value of 1 will be assigned when the option is encountered.
- store
- A reference to a scalar in which to store the value passed to the option. If not specified, the value will be stored under the option name in the hash returned by the CWargs() method.
- default
-
A default value for the option. If no default value is specified and no option
is passed, then the option key will not exist in the hash returned by
CWargs().
You can combine references to your own variables or subroutines with
unreferenced specifications, for which the result will also be stored in the
has returned by CWargs(). For example:
my $loud = 0; my $build = Module::Build->new( module_name => 'Spangly', get_options => { loud => { store => \$loud }, dbd => { type => '=s' }, quantity => { type => '+' }, } );
print STDERR "HEY, ARE YOU LISTENING??\n" if $loud; print "We'll use the ", $build->args('dbd'), " DBI driver\n"; print "Are you sure you want that many?\n" if $build->args('quantity') > 2;
The arguments for such a specification can be called like so:% perl Build.PL --loud --dbd=DBD::pg --quantity --quantity --quantity
WARNING: Any option specifications that conflict with Module::Build's own options (defined by its properties) will throw an exception. Consult the Getopt::Long documentation for details on its usage. - args()
-
my $args_href = $build->args; my %args = $build->args; my $arg_value = $build->args($key); $build->args($key, $value);
This method is the preferred interface for retreiving the arguments passed via command-line options to Build.PL or Build, minus the Module-Build specific options. When called in in a scalar context with no arguments, this method returns a reference to the hash storing all of the arguments; in an array context, it returns the hash itself. When passed a single argument, it returns the value stored in the args hash for that option key. When called with two arguments, the second argument is assigned to the args hash under the key passed as the first argument. - subclass()
- This creates a new CWModule::Build subclass on the fly, as described in the SUBCLASSING section. The caller must provide either a CWclass or CWcode parameter, or both. The CWclass parameter indicates the name to use for the new subclass, and defaults to CWMyModuleBuilder. The CWcode parameter specifies Perl code to use as the body of the subclass.
- create_build_script()
- Creates an executable script called CWBuild in the current directory that will be used to execute further user actions. This script is roughly analogous (in function, not in form) to the Makefile created by CWExtUtils::MakeMaker. This method also creates some temporary data in a directory called CW_build/. Both of these will be removed when the CWrealclean action is performed.
- add_to_cleanup(@files)
- You may call CW$self->add_to_cleanup(@patterns) to tell CWModule::Build that certain files should be removed when the user performs the CWBuild clean action. The arguments to the method are patterns suitable for passing to Perl's CWglob() function, specified in either Unix format or the current machine's native format. It's usually convenient to use Unix format when you hard-code the filenames (e.g. in Build.PL) and the native format when the names are programmatically generated (e.g. in a testing script). I decided to provide a dynamic method of the CW$build object, rather than just use a static list of files named in the Build.PL, because these static lists can get difficult to manage. I usually prefer to keep the responsibility for registering temporary files close to the code that creates them.
- resume()
- You'll probably never call this method directly, it's only called from the auto-generated CWBuild script. The CWnew() method is only called once, when the user runs CWperl Build.PL. Thereafter, when the user runs CWBuild test or another action, the CWModule::Build object is created using the CWresume() method to reinstantiate with the settings given earlier to CWnew().
- current()
-
This method returns a reasonable faxsimile of the currently-executing
CWModule::Build object representing the current build. You can use
this object to query its CWnotes() method, inquire about installed
modules, and so on. This is a great way to share information between
different parts of your build process. For instance, you can ask
the user a question during CWperl Build.PL, then use their answer
during a regression test:
# In Build.PL: my $color = $build->prompt("What is your favorite color?"); $build->notes(color => $color);
# In t/colortest.t: use Module::Build; my $build = Module::Build->current; my $color = $build->notes('color'); ...
The way the CWcurrent() method is currently implemented, there may be slight differences between the CW$build object in Build.PL and the one in CWt/colortest.t. It is our goal to minimize these differences in future releases of Module::Build, so please report any anomalies you find. One important caveat: in its current implementation, CWcurrent() will NOT work correctly if you have changed out of the directory that CWModule::Build was invoked from. - notes()
- notes($key)
- The CWnotes() value allows you to store your own persistent information about the build, and to share that information among different entities involved in the build. See the example in the CWcurrent() method. The CWnotes() method is essentally a glorified hash access. With no arguments, CWnotes() returns a reference to the entire hash of notes. With one argument, CWnotes($key) returns the value associated with the given key. With two arguments, CWnotes($key, $value) sets the value associated with the given key to CW$value. The lifetime of the CWnotes data is for a build - that is, the CWnotes hash is created when CWperl Build.PL is run (or when the CWnew() method is run, if the Module::Build Perl API is being used instead of called from a shell), and lasts until CWperl Build.PL is run again or the CWclean action is run.
- config()
- Returns a hash reference containing the CWConfig.pm hash, including any changes the author or user has specified. This is a reference to the actual internal hash we use, so you probably shouldn't modify stuff there. This method is also called from the auto-generated CWBuild script. It parses the command-line arguments into an action and an argument list, then calls the appropriate routine to handle the action. Currently (though this may change), an action CWfoo will invoke the CWACTION_foo method. All arguments (including everything mentioned in ACTIONS below) are contained in the CW$self->{args} hash reference.
- os_type()
- If you're subclassing Module::Build and some code needs to alter its behavior based on the current platform, you may only need to know whether you're running on Windows, Unix, MacOS, VMS, etc. and not the fine-grained value of Perl's CW$^O variable. The CWos_type() method will return a string like CWWindows, CWUnix, CWMacOS, CWVMS, or whatever is appropriate. If you're running on an unknown platform, it will return CWundef - there shouldn't be many unknown platforms though.
- prereq_failures()
-
Returns a data structure containing information about any failed
prerequisites (of any of the types described above), or CWundef if
all prerequisites are met.
The data structure returned is a hash reference. The top level keys
are the type of prerequisite failed, one of requires,
build_requires, conflicts, or recommends. The associated values
are hash references whose keys are the names of required (or
conflicting) modules. The associated values of those are hash
references indicating some information about the failure. For example:
{ have => '0.42', need => '0.59', message => 'Version 0.42 is installed, but we need version 0.59', }
or{ have => '<none>', need => '0.59', message => 'Prerequisite Foo isn't installed', }
This hash has the same structure as the hash returned by the CWcheck_installed_status() method, except that in the case of conflicts dependencies we change the need key to conflicts and construct a proper message. Examples:# Check a required dependency on Foo::Bar if ( $m->prereq_failures->{requires}{Foo::Bar} ) { ...
# Check whether there were any failures if ( $m->prereq_failures ) { ...
# Show messages for all failures my $failures = $m->prereq_failures; while (my ($type, $list) = each %$failures) { while (my ($name, $hash) = each %$list) { print "Failure for $name: $hash->{message}\n"; } }
- requires()
- build_requires()
- recommends()
- conflicts()
-
Each of these methods returns a hash reference indicating the
prerequisites that were passed to the CWnew() method.
This method returns a hash reference indicating whether a version
dependency on a certain module is satisfied. The CW$module argument
is given as a string like CW"Data::Dumper" or CW"perl", and the
CW$version argument can take any of the forms described in requires
above. This allows very fine-grained version checking.
The returned hash reference has the following structure:
{ ok => $whether_the_dependency_is_satisfied, have => $version_already_installed, need => $version_requested, # Same as incoming $version argument message => $informative_error_message, }
If no version of CW$module is currently installed, the CWhave value will be the string CW"<none>". Otherwise the CWhave value will simply be the version of the installed module. Note that this means that if CW$module is installed but doesn't define a version number, the CWhave value will be CWundef - this is why we don't use CWundef for the case when CW$module isn't installed at all. This method may be called either as an object method (CW$build->check_installed_status($module, $version)) or as a class method (CWModule::Build->check_installed_status($module, $version)). Like CWcheck_installed_status(), but simply returns true or false depending on whether module CW$module statisfies the dependency CW$version. If the check succeeds, the return value is the actual version of CW$module installed on the system. This allows you to do the following:my $installed = $m->check_installed_version('DBI', '1.15'); if ($installed) { print "Congratulations, version $installed of DBI is installed.\n"; } else { die "Sorry, you must install DBI.\n"; }
If the check fails, we return false and set CW$@ to an informative error message. If CW$version is any nontrue value (notably zero) and any version of CW$module is installed, we return true. In this case, if CW$module doesn't define a version, or if its version is zero, we return the special value 0 but true, which is numerically zero, but logically true. In general you might prefer to use CWcheck_installed_status if you need detailed information, or this method if you just need a yes/no answer. Asks the user a question and returns their response as a string. The first argument specifies the message to display to the user (for example, CW"Where do you keep your money?"). The second argument, which is optional, specifies a default answer (for example, CW"wallet"). The user will be asked the question once. If the current session doesn't seem to be interactive (i.e. if CWSTDIN and CWSTDOUT look like they're attached to files or something, not terminals), we'll just use the default without letting the user provide an answer. This method may be called as a class or object method. Asks the user a yes/no question using CWprompt() and returns true or false accordingly. The user will be asked the question repeatedly until they give an answer that looks like yes or no. The first argument specifies the message to display to the user (for example, CW"Shall I invest your money for you?"), and the second argument specifies the default answer (for example, CW"y"). Note that the default is specified as a string like CW"y" or CW"n", and the return value is a Perl boolean value like 1 or 0. I thought about this for a while and this seemed like the most useful way to do it. This method may be called as a class or object method. - script_files()
- Returns a hash reference whose keys are the perl script files to be installed, if any. This corresponds to the CWscript_files parameter to the CWnew() method. With an optional argument, this parameter may be set dynamically. For backward compatibility, the CWscripts() method does exactly the same thing as CWscript_files(). CWscripts() is deprecated, but it will stay around for several versions to give people time to transition.
- add_build_element($type)
- Adds a new type of entry to the build process. Accepts a single string specifying its type-name. There must also be a method defined to process things of that type, e.g. if you add a build element called CW'foo', then you must also define a method called CWprocess_foo_files(). See also Adding new elements to the build process in Module::Build::Compat.
- copy_if_modified(%parameters)
- Takes the file in the CWfrom parameter and copies it to the file in the CWto parameter, or the directory in the CWto_dir parameter, if the file has changed since it was last copied (or if it doesn't exist in the new location). By default the entire directory structure of CWfrom will be copied into CWto_dir; an optional CWflatten parameter will copy into CWto_dir without doing so. Returns the path to the destination file, or CWundef if nothing needed to be copied. Any directories that need to be created in order to perform the copying will be automatically created. This is a fairly simple wrapper around Perl's CWsystem() built-in command. Given a command and an array of optional arguments, this method will print the command to CWSTDOUT, and then execute it using Perl's CWsystem(). It returns true or false to indicate success or failure (the opposite of how CWsystem() works, but more intuitive). Note that if you supply a single argument to CWdo_system(), it will/may be processed by the systems's shell, and any special characters will do their special things. If you supply multiple arguments, no shell will get involved and the command will be executed directly.
- have_c_compiler()
- Returns true if the current system seems to have a working C compiler. We currently determine this by attempting to compile a simple C source file and reporting whether the attempt was successful.
- base_dir()
- Returns a string containing the root-level directory of this build, i.e. where the CWBuild.PL script and the CWlib directory can be found. This is usually the same as the current working directory, because the CWBuild script will CWchdir() into this directory as soon as it begins execution.
- dist_name()
- Returns the name of the current distribution, as passed to the CWnew() method in a CWdist_name or modified CWmodule_name parameter.
- dist_version()
- Returns the version of the current distribution, as determined by the CWnew() method from a CWdist_version, CWdist_version_from, or CWmodule_name parameter.
- up_to_date(\@source_files, \@derived_files)
- This method can be used to compare a set of source files to a set of derived files. If any of the source files are newer than any of the derived files, it returns false. Additionally, if any of the derived files do not exist, it returns false. Otherwise it returns true. The arguments may be either a scalar or an array reference of file names.
- contains_pod($file)
- Returns true if the given file appears to contain POD documentation. Currently this checks whether the file has a line beginning with '=pod', '=head', or '=item', but the exact semantics may change in the future.
- feature($name)
- With a single argument, returns true if the given feature is set. With two arguments, sets the given feature to the given boolean value. In this context, a feature is any optional functionality of an installed module. For instance, if you write a module that could optionally support a MySQL or PostgreSQL backend, you might create features called CWmysql_support and CWpostgres_support, and set them to true/false depending on whether the user has the proper databases installed and configured. Features set in this way using the Module::Build object will be available for querying during the build/test process and after installation via the generated CW...::ConfigData module, as CW...::ConfigData->feature($name). The CWfeature() and CWconfig_data() methods represent Module::Build's main support for configuration of installed modules. See also SAVING CONFIGURATION INFORMATION.
- config_data($name)
- With a single argument, returns the value of the configuration variable CW$name. With two arguments, sets the given configuration variable to the given value. The value may be any perl scalar that's serializable with CWData::Dumper. For instance, if you write a module that can use a MySQL or PostgreSQL backend, you might create configuration variables called CWmysql_connect and CWpostgres_connect, and set each to an array of connection parameters for CWDBI->connect(). Configuration values set in this way using the Module::Build object will be available for querying during the build/test process and after installation via the generated CW...::ConfigData module, as CW...::ConfigData->config($name). The CWfeature() and CWconfig_data() methods represent Module::Build's main support for configuration of installed modules. See also SAVING CONFIGURATION INFORMATION.
ACTIONS
There are some general principles at work here. First, each task when building a module is called an action. These actions are listed above; they correspond to the building, testing, installing, packaging, etc. tasks.
Second, arguments are processed in a very systematic way. Arguments are always key=value pairs. They may be specified at CWperl Build.PL time (i.e. CWperl Build.PL destdir=/my/secret/place), in which case their values last for the lifetime of the CWBuild script. They may also be specified when executing a particular action (i.e. CWBuild test verbose=1), in which case their values last only for the lifetime of that command. Per-action command-line parameters take precedence over parameters specified at CWperl Build.PL time.
The build process also relies heavily on the CWConfig.pm module, and all the key=value pairs in CWConfig.pm are available in
CW$self->{config}. If the user wishes to override any of the values in CWConfig.pm, she may specify them like so:
perl Build.PL --config cc=gcc --config ld=gcc
The following build actions are provided by default.
- help
- This action will simply print out a message that is meant to help you use the build process. It will show you a list of available build actions too. With an optional argument specifying an action name (e.g. CWBuild help test), the 'help' action will show you any POD documentation it can find for that action.
- build
- If you run the CWBuild script without any arguments, it runs the CWbuild action, which in turn runs the CWcode and CWdocs actions. This is analogous to the MakeMaker 'make all' target.
- code
-
This action builds your codebase.
By default it just creates a CWblib/ directory and copies any CW.pm
and CW.pod files from your CWlib/ directory into the CWblib/
directory. It also compiles any CW.xs files from CWlib/ and places
them in CWblib/. Of course, you need a working C compiler (probably
the same one that built perl itself) for the compilation to work
properly.
The CWbuild action also runs any CW.PL files in your lib/
directory. Typically these create other files, named the same but
without the CW.PL ending. For example, a file lib/Foo/Bar.pm.PL
could create the file lib/Foo/Bar.pm. The CW.PL files are
processed first, so any CW.pm files (or other kinds that we deal
with) will get copied correctly.
If your CW.PL scripts don't create any files, or if they create files
with unexpected names, or even if they create multiple files, you
should tell us that so that we can clean up properly after these
created files. Use the CWPL_files parameter to CWnew():
PL_files => { 'lib/Foo/Bar_pm.PL' => 'lib/Foo/Bar.pm', 'lib/something.PL' => ['/lib/something', '/lib/else'], 'lib/funny.PL' => [] }
Note that in contrast to MakeMaker, the CWbuild action only (currently) handles CW.pm, CW.pod, CW.PL, and CW.xs files. They must all be in the CWlib/ directory, in the directory structure that they should have when installed. We also handle CW.c files that can be in the place of your choosing - see the CWc_source argument to CWnew(). The CW.xs support is currently in alpha. Please let me know whether it works for you. - docs
- This will generate documentation (ie: Unix man pages) for any binary and library files under blib/ that contain POD. If there are no CWbindoc or CWlibdoc installation targets defined (as will be the case on systems that don't support Unix manpages) this action does nothing.
- test
-
This will use CWTest::Harness to run any regression tests and report
their results. Tests can be defined in the standard places: a file
called CWtest.pl in the top-level directory, or several files ending
with CW.t in a CWt/ directory.
If you want tests to be 'verbose', i.e. show details of test execution
rather than just summary information, pass the argument CWverbose=1.
If you want to run tests under the perl debugger, pass the argument
CWdebugger=1.
In addition, if a file called CWvisual.pl exists in the top-level
directory, this file will be executed as a Perl script and its output
will be shown to the user. This is a good place to put speed tests or
other tests that don't use the CWTest::Harness format for output.
To override the choice of tests to run, you may pass a CWtest_files
argument whose value is a whitespace-separated list of test scripts to
run. This is especially useful in development, when you only want to
run a single test to see whether you've squashed a certain bug yet:
./Build test --test_files t/something_failing.t
You may also pass several CWtest_files arguments separately:./Build test --test_files t/one.t --test_files t/two.t
or use a CWglob()-style pattern:./Build test --test_files 't/01-*.t'
- testcover
- Runs the CWtest action using CWDevel::Cover, generating a code-coverage report showing which parts of the code were actually exercised during the tests.
- testdb
- This is a synonym for the 'test' action with the CWdebugger=1 argument.
- testpod
- This checks all the files described in the CWdocs action and produces CWTest::Harness-style output. If you are a module author, this is useful to run before creating a new release.
- clean
- This action will clean up any files that the build process may have created, including the CWblib/ directory (but not including the CW_build/ directory and the CWBuild script itself).
- realclean
- This action is just like the CWclean action, but also removes the CW_build directory and the CWBuild script. If you run the CWrealclean action, you are essentially starting over, so you will have to re-create the CWBuild script again.
- diff
-
This action will compare the files about to be installed with their
installed counterparts. For .pm and .pod files, a diff will be shown
(this currently requires a 'diff' program to be in your PATH). For
other files like compiled binary files, we simply report whether they
differ.
A CWflags parameter may be passed to the action, which will be passed
to the 'diff' program. Consult your 'diff' documentation for the
parameters it will accept - a good one is CW-u:
./Build diff flags=-u
- install
-
This action will use CWExtUtils::Install to install the files from
CWblib/ into the system. See How Installation Paths are Determined for details
about how Module::Build determines where to install things, and how to
influence this process.
If you want the installation process to look around in CW@INC for
other versions of the stuff you're installing and try to delete it,
you can use the CWuninst parameter, which tells CWExtUtils::Install to
do so:
Build install uninst=1
This can be a good idea, as it helps prevent multiple versions of a module from being present on your system, which can be a confusing situation indeed. - fakeinstall
- This is just like the CWinstall action, but it won't actually do anything, it will just report what it would have done if you had actually run the CWinstall action.
- versioninstall
-
** Note: since CWonly.pm is so new, and since we just recently added
support for it here too, this feature is to be considered
experimental. **
If you have the CWonly.pm module installed on your system, you can
use this action to install a module into the version-specific library
trees. This means that you can have several versions of the same
module installed and CWuse a specific one like this:
use only MyModule => 0.55;
To override the default installation libraries in CWonly::config, specify the CWversionlib parameter when you run the CWBuild.PL script:perl Build.PL versionlib=/my/version/place/
To override which version the module is installed as, specify the CWversionlib parameter when you run the CWBuild.PL script:perl Build.PL version=0.50
See the CWonly.pm documentation for more information on version-specific installs. - manifest
-
This is an action intended for use by module authors, not people
installing modules. It will bring the MANIFEST up to date with the
files currently present in the distribution. You may use a
MANIFEST.SKIP file to exclude certain files or directories from
inclusion in the MANIFEST. MANIFEST.SKIP should contain a bunch
of regular expressions, one per line. If a file in the distribution
directory matches any of the regular expressions, it won't be included
in the MANIFEST.
The following is a reasonable MANIFEST.SKIP starting point, you can
add your own stuff to it:
^_build ^Build$ ^blib ~$ \.bak$ ^MANIFEST\.SKIP$ CVS
See the distcheck and skipcheck actions if you want to find out what the CWmanifest action would do, without actually doing anything. - dist
-
This action is helpful for module authors who want to package up their
module for source distribution through a medium like CPAN. It will create a
tarball of the files listed in MANIFEST and compress the tarball using
GZIP compression.
By default, this action will use the external CWtar and CWgzip
executables on Unix-like platforms, and the CWArchive::Tar module
elsewhere. However, you can force it to use whatever executable you
want by supplying an explicit CWtar (and optional CWgzip) parameter:
perl Build dist --tar C:\path\to\tar.exe --gzip C:\path\to\zip.exe
- ppmdist
- Generates a PPM binary distribution and a PPD description file. This action also invokes the 'ppd' action, so it can accept the same CWcodebase argument described under that action. This uses the same mechanism as the CWdist action to tar & zip its output, so you can supply CWtar and/or CWgzip parameters to affect the result.
- distsign
- Uses CWModule::Signature to create a SIGNATURE file for your distribution, and adds the SIGNATURE file to the distribution's MANIFEST.
- distmeta
- Creates the META.yml file for your distribution.
- distcheck
- Reports which files are in the build directory but not in the MANIFEST file, and vice versa. (See manifest for details)
- skipcheck
- Reports which files are skipped due to the entries in the MANIFEST.SKIP file (See manifest for details)
- distclean
- Performs the 'realclean' action and then the 'distcheck' action.
- distdir
- Creates a directory called CW$(DISTNAME)-$(VERSION) (if that directory already exists, it will be removed first). Then copies all the files listed in the MANIFEST file to that directory. This directory is what people will see when they download your distribution and unpack it. While performing the 'distdir' action, a file containing various bits of metadata will be created. The metadata includes the module's name, version, dependencies, license, and the CWdynamic_config flag. This file is created as META.yml in YAML format, so you must have the CWYAML module installed in order to create it. You should also ensure that the META.yml file is listed in your MANIFEST - if it's not, a warning will be issued.
- disttest
- Performs the 'distdir' action, then switches into that directory and runs a CWperl Build.PL, followed by the 'build' and 'test' actions in that directory.
- ppd
-
Build a PPD file for your distribution.
This action takes an optional argument CWcodebase which is used in
the generated ppd file to specify the (usually relative) URL of the
distribution. By default, this value is the distribution name without
any path information.
Example:
perl Build ppd codebase="MSWin32-x86-multi-thread/Module-Build-0.21.tar.gz"
How Installation Paths are Determined
When you invoke Module::Build's CWbuild action, it needs to figure out where to install things. The nutshell version of how this works is that default installation locations are determined from Config.pm, and they may be overridden by using the CWinstall_path parameter. An CWinstall_base parameter lets you specify an alternative installation root like /home/foo, and a CWdestdir lets you specify a temporary installation directory like /tmp/install in case you want to create bundled-up installable packages.
Natively, Module::Build provides default installation locations for the following types of installable items:
- lib
- Usually pure-Perl module files ending in .pm.
- arch
- Architecture-dependent module files, usually produced by compiling XS, Inline, or similar code.
- script
- Programs written in pure Perl. In order to improve reuse, try to make these as small as possible - put the code into modules whenever possible.
- bin
- Architecture-dependent executable programs, i.e. compiled C code or something. Pretty rare to see this in a perl distribution, but it happens.
- libdoc
- Documentation for the stuff in CWlib and CWarch. This is usually generated from the POD in .pm files. Under Unix, these are manual pages belonging to the 'man3' category.
- bindoc
- Documentation for the stuff in CWscript and CWbin. Usually generated from the POD in those files. Under Unix, these are manual pages belonging to the 'man1' category.
Four other parameters let you control various aspects of how installation paths are determined:
- installdirs
-
The default destinations for these installable things come from
entries in your system's CWConfig.pm. You can select from three
different sets of default locations by setting the CWinstalldirs
parameter as follows:
'installdirs' set to: core site vendor
uses the following defaults from Config.pm:
lib => installprivlib installsitelib installvendorlib arch => installarchlib installsitearch installvendorarch script => installscript installsitebin installvendorbin bin => installbin installsitebin installvendorbin libdoc => installman3dir installsiteman3dir installvendorman3dir bindoc => installman1dir installsiteman1dir installvendorman1dir
The default value of CWinstalldirs is site. If you're creating vendor distributions of module packages, you may want to do something like this:perl Build.PL installdirs=vendor
orBuild install installdirs=vendor
If you're installing an updated version of a module that was included with perl itself (i.e. a core module), then you may set CWinstalldirs to core to overwrite the module in its present location. (Note that the 'script' line is different from MakeMaker - unfortunately there's no such thing as installsitescript or installvendorscript entry in CWConfig.pm, so we use the installsitebin and installvendorbin entries to at least get the general location right. In the future, if CWConfig.pm adds some more appropriate entries, we'll start using those.) - install_path
-
Once the defaults have been set, you can override them. You can set
individual entries by using the CWinstall_path parameter:
my $m = Module::Build->new (...other options..., install_path => {lib => '/foo/lib', arch => '/foo/lib/arch'});
On the command line, that would look like this:perl Build.PL --install_path lib=/foo/lib --install_path arch=/foo/lib/arch
or this:Build install --install_path lib=/foo/lib --install_path arch=/foo/lib/arch
- install_base
-
You can also set the whole bunch of installation paths by supplying the
CWinstall_base parameter to point to a directory on your system. For
instance, if you set CWinstall_base to /home/ken on a Linux
system, you'll install as follows:
lib => /home/ken/lib arch => /home/ken/lib/i386-linux script => /home/ken/scripts bin => /home/ken/bin bindoc => /home/ken/man/man1 libdoc => /home/ken/man/man3
Note that this is different from how MakeMaker's CWPREFIX parameter works. CWPREFIX tries to create a mini-replica of a CWsite-style installation under the directory you specify, which is not always possible (and the results are not always pretty in this case). CWinstall_base just gives you a default layout under the directory you specify, which may have little to do with the CWinstalldirs=site layout. The exact layout under the directory you specify may vary by system - we try to do the sensible thing on each platform. - destdir
-
If you want to install everything into a temporary directory first
(for instance, if you want to create a directory tree that a package
manager like CWrpm or CWdpkg could create a package from), you can
use the CWdestdir parameter:
perl Build.PL destdir=/tmp/foo
orBuild install destdir=/tmp/foo
This will effectively install to /tmp/foo/$sitelib, /tmp/foo/$sitearch, and the like, except that it will use CWFile::Spec to make the pathnames work correctly on whatever platform you're installing on.
SAVING CONFIGURATION INFORMATION
Module::Build provides a very convenient way to save configuration information that your installed modules (or your regression tests) can access. If your Build process calls the CWfeature() or CWconfig_data() methods, then a CWFoo::Bar::ConfigData module will automatically be created for you, where CWFoo::Bar is the CWmodule_name parameter as passed to CWnew(). This module provides access to the data saved by these methods, and a way to update the values. There is also a utility script called CWconfig_data distributed with Module::Build that provides a command-line interface to this same functionality. See also the generated CWFoo::Bar::ConfigData documentation, and the CWconfig_data script's documentation, for more information.
AUTOMATION
One advantage of Module::Build is that since it's implemented as Perl methods, you can invoke these methods directly if you want to install a module non-interactively. For instance, the following Perl script will invoke the entire build/install procedure:
my $m = Module::Build->new(module_name => 'MyModule'); $m->dispatch('build'); $m->dispatch('test'); $m->dispatch('install');
If any of these steps encounters an error, it will throw a fatal exception.
You can also pass arguments as part of the build process:
my $m = Module::Build->new(module_name => 'MyModule'); $m->dispatch('build'); $m->dispatch('test', verbose => 1); $m->dispatch('install', sitelib => '/my/secret/place/');
Building and installing modules in this way skips creating the CWBuild script.
STRUCTURE
Module::Build creates a class hierarchy conducive to customization. Here is the parent-child class hierarchy in classy ASCII art:
/--------------------\ | Your::Parent | (If you subclass Module::Build) \--------------------/ | | /--------------------\ (Doesn't define any functionality | Module::Build | of its own - just figures out what \--------------------/ other modules to load.) | | /-----------------------------------\ (Some values of $^O may | Module::Build::Platform::$^O | define specialized functionality. \-----------------------------------/ Otherwise it's ...::Default, a | pass-through class.) | /--------------------------\ | Module::Build::Base | (Most of the functionality of \--------------------------/ Module::Build is defined here.)
SUBCLASSING
Right now, there are two ways to subclass Module::Build. The first way is to create a regular module (in a CW.pm file) that inherits from Module::Build, and use that module's class instead of using Module::Build directly:
------ in Build.PL: ---------- #!/usr/bin/perl
use lib qw(/nonstandard/library/path); use My::Builder; # Or whatever you want to call it
my $m = My::Builder->new (module_name=> 'Next::Big::Thing', # All the regular args... license=> 'perl', dist_author=> 'A N Other <me@here.net.au>', requires=> {Carp => 0}); $m->create_build_script;
This is relatively straightforward, and is the best way to do things if your My::Builder class contains lots of code. The CWcreate_build_script() method will ensure that the current value of CW@INC (including the CW/nonstandard/library/path) is propogated to the Build script, so that My::Builder can be found when running build actions.
For very small additions, Module::Build provides a CWsubclass() method that lets you subclass Module::Build more conveniently, without creating a separate file for your module:
------ in Build.PL: ---------- #!/usr/bin/perl
use Module::Build; my $class = Module::Build->subclass ( class => 'My::Builder', code => q{ sub ACTION_foo { print "I'm fooing to death!\n"; } }, );
my $m = $class->new (module_name=> 'Next::Big::Thing', # All the regular args... license=> 'perl', dist_author=> 'A N Other <me@here.net.au>', requires=> {Carp => 0}); $m->create_build_script;
Behind the scenes, this actually does create a CW.pm file, since the code you provide must persist after Build.PL is run if it is to be very useful.
See also the documentation for the CWsubclass() method.
MOTIVATIONS
There are several reasons I wanted to start over, and not just fix what I didn't like about MakeMaker:
- •
- I don't like the core idea of MakeMaker, namely that CWmake should be involved in the build process. Here are my reasons:
- +
- When a person is installing a Perl module, what can you assume about their environment? Can you assume they have CWmake? No, but you can assume they have some version of Perl.
- +
- When a person is writing a Perl module for intended distribution, can you assume that they know how to build a Makefile, so they can customize their build process? No, but you can assume they know Perl, and could customize that way. For years, these things have been a barrier to people getting the build/install process to do what they want.
- •
- There are several architectural decisions in MakeMaker that make it very difficult to customize its behavior. For instance, when using MakeMaker you do CWuse ExtUtils::MakeMaker, but the object created in CWWriteMakefile() is actually blessed into a package name that's created on the fly, so you can't simply subclass CWExtUtils::MakeMaker. There is a workaround CWMY package that lets you override certain MakeMaker methods, but only certain explicitly preselected (by MakeMaker) methods can be overridden. Also, the method of customization is very crude: you have to modify a string containing the Makefile text for the particular target. Since these strings aren't documented, and can't be documented (they take on different values depending on the platform, version of perl, version of MakeMaker, etc.), you have no guarantee that your modifications will work on someone else's machine or after an upgrade of MakeMaker or perl.
- •
- It is risky to make major changes to MakeMaker, since it does so many things, is so important, and generally works. CWModule::Build is an entirely separate package so that I can work on it all I want, without worrying about backward compatibility.
- •
- Finally, Perl is said to be a language for system administration. Could it really be the case that Perl isn't up to the task of building and installing software? Even if that software is a bunch of stupid little CW.pm files that just need to be copied from one place to another? My sense was that we could design a system to accomplish this in a flexible, extensible, and friendly manner. Or die trying.
MIGRATION
Note that if you want to provide both a Makefile.PL and a Build.PL for your distribution, you probably want to add the following to CWWriteMakefile in your Makefile.PL so that MakeMaker doesn't try to run your Build.PL as a normal .PL file:
PL_FILES => {},
You may also be interested in looking at the CWModule::Build::Compat module, which can automatically create various kinds of Makefile.PL compatibility layers.
TO DO
The current method of relying on time stamps to determine whether a derived file is out of date isn't likely to scale well, since it requires tracing all dependencies backward, it runs into problems on NFS, and it's just generally flimsy. It would be better to use an MD5 signature or the like, if available. See CWcons for an example.
- append to perllocal.pod - write .packlist in appropriate location (needed for un-install) - add a 'plugin' functionality
AUTHOR
Ken Williams, kwilliams@cpan.org
Development questions, bug reports, and patches should be sent to the Module-Build mailing list at module-build-general@lists.sourceforge.net .
Bug reports are also welcome at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Build .
An anonymous CVS repository containing the latest development version is available; see http://sourceforge.net/cvs/?group_id=45731 for the details of how to access it.
SEE ALSO
perl(1), Module::Build::Cookbook(3), ExtUtils::MakeMaker(3), YAML(3)
http://www.dsmit.com/cons/