Makefile::Parser - A Simple Parser for Makefiles |
Makefile::Parser - A Simple Parser for Makefiles
use Makefile::Parser;
# Equivalent to ->new('Makefile'); $parser = Makefile::Parser->new or die Makefile::Parser->error;
# Get last value assigned to the specified variable 'CC': print $parser->var('CC');
# Get all the variable names defined in the Makefile: @vars = $parser->vars; print join(' ', sort @vars);
@roots = $parser->roots; # Get all the "root targets" print $roots[0]->name;
@tars = $parser->targets; # Get all the targets $tar = join("\n", $tars[0]->commands);
# Get the default target, say, the first target defined in Makefile: $tar = $parser->target;
$tar = $parser->target('install'); # Get the name of the target, say, 'install' here: print $tar->name;
# Get the dependencies for the target 'install': @depends = $tar->depends;
# Access the shell command used to build the current target. @cmds = $tar->commands;
# Parse another file using the same Parser object: $parser->parse('Makefile.old') or die Makefile::Parser->error;
# Get the target who is specified by variable EXE_FILE $tar = $parser->target($parser->var('EXE_FILE'));
This is a parser for Makefiles. At this very early stage, the parser only supports a very limited set of features, so it may not do what you expected. Currently its main purpose is to provide basic support for another module named the Makefile::GraphViz manpage, which is aimed to render the building processes specified by a Makefile using the amazing GraphViz library. The the Make manpage module is not satisfactory for this purpose, so I decided to build one of my own.
IMPORTANT! This stuff is highly experimental and is currently at ALPHA stage, so production use is strongly discouraged. Anyway, I have the plan to improve this stuff unfailingly.
This class provide the interface to the Makefile parser.
new()
When an error occurs during the parsing procedure, ->parse will return undef. Otherwise, a reference to Parser object itself is returned. It is recommended to check the return value every time you call this method. The detailed error info can be obtained by calling the ->error method.
var($variable_name)
target($target_name)
When $target_name is omitted, this method will return the default target, say, the first target defined in Makefile, to the user. This can be handy if you try to build a make tool on top of this module.
It is important not to send something like ``$(MY_LIB)'' as the target name. Only raw values are acceptable. If you really want to do something like this, please use the following code:
my $tar = $parser->target($parser->var('MY_LIB'));
but this code will break if you have reassigned values to variable MY_LIB in your Makefile.
@tars = $parser->targets; print $tars[0];
Please use the following syntax instead:
print $parser->target;
The type of the returned list is an array of Makefile::Target objects.
The type of the returned list is an array of Makefile::Target objects.
This class overloads the ``'' operator so its instances can be automatically converted to strings using their names.
This method is usually called internally by the Makefile::Parser class. It doesn't make much sense to me if the user has a need to call it manually.
None by default.
I use the Devel::Cover manpage to test the code coverage of my tests, below is the the Devel::Cover manpage report on this module test suite.
---------------------------- ------ ------ ------ ------ ------ ------ ------ File stmt bran cond sub pod time total ---------------------------- ------ ------ ------ ------ ------ ------ ------ blib/lib/Makefile/Parser.pm 100.0 95.5 87.1 100.0 100.0 100.0 97.3 Total 100.0 95.5 87.1 100.0 100.0 100.0 97.3 ---------------------------- ------ ------ ------ ------ ------ ------ ------
Please feel free to report bugs or send your wish-list to http://rt.cpan.org/NoAuth/Bugs.html.
the Makefile::GraphViz manpage.
Agent Zhang, <agent2002@126.com>
Copyright (c) 2005 Agent Zhang.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Makefile::Parser - A Simple Parser for Makefiles |