Makefile::Parser
A simple parser for Makefiles
- Provided by: libmakefile-parser-perl (Version: 0.215-2)
- Report a bug
A simple parser for Makefiles
This document describes Makefile::Parser 0.215 released on 18 August 2011.
use Makefile::Parser;
$parser = Makefile::Parser->new;
# equivalent to ->parse('Makefile');
$parser->parse 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 simple parser for Makefiles. At this very early stage, the parser only supports a limited set of features, so it may not recognize most of the advanced features provided by certain make tools like GNU make. Its initial purpose is to provide basic support for another module named Makefile::GraphViz, which is aimed to render the building process specified by a Makefile using the amazing GraphViz library. The Make module is not satisfactory for this purpose, so I decided to build one of my own.
WARNING!!! This stuff is highly experimental and is currently at pre-alpha stage, so production use is strongly discouraged. Right now it's just a toy for parsing trivial makefiles.
IMPORTANT!!! If you're looking for something more serious for parsing GNU makefiles, please see Makefile::Parser::GmakeDB instead. The GmakeDB parser has passed 51% of GNU make's official test suite as of this writing.
If you're looking for something that can parse makefiles losslessly, take a look at the Makefile::DOM module which may fit your needs.
The current parser implementation has been trying to support a common feature set of both MS NMAKE and GNU make. In the future, different formats of Makefiles will be handled by individual subclasses such as Makefile::Parser::Gmake.
MIN_T_FILES = $(PAT_COVER_FILES) t\optest.t t\my_perl.exe.t t\types.cod.t \
t\catln.t t\exe2hex.t t\hex2bin.t t\bin2hex.t t\bin2asm.t t\ndisasmi.t \
t\Idu.t t\pat_tree.t t\state_mac.t t\Idu-Util.t t\cidu.t \
t\opname.t t\error.t t\operand.t t\01disasm.t t\02disasm.t t\03disasm.t \
t\disasm_cover.t t\ndisasm.t
T_FILES = t\main.cod.t t\bin2hex.exe.t t\hex2bin.exe.t $(MIN_T_FILES)
DIRFILESEP = ^\
"Simply expanded" variables' definition syntax in GUN make is also supported:
FOO := blah blah blah
which is considered invalid in Win32 NMake. "Recursively expanded" variables are currently treated as "simply expanded" variables.
Variable redefinition can be handled as well:
CC = cl
%.obj : %.c
$(CC) /nologo /c $<
CC = gcc
%.o : %.c
$(CC) -c $<
Variable expansion syntax
${abc}
is accepted, whereas Win32 NMAKE will complain about it.
Currently, environment variables defined in the command-line are not imported.
I have no idea what default value should be assigned to built-in variables like $(MAKE) and $(CC). Currently they will be left untouched if they're not set explicitly in the Makefile.
Due to the current implementation, expansion of unrecognized built-in variables and variables not previously defined by Makefile will NOT be performed. This behavior is different from any practical make tools, but is reasonable at this early stage of this parser.
$(CIDU_DLL) : C\idu.obj C\idu.def
link /dll /nologo /debug /out:$@ /def:C\idu.def C\idu.obj
$(CIDU_LIB) : $(CIDU_DLL)
C\idu.obj : C\idu.c C\idu.h
cd C
cl /nologo /c /I . idu.c
cd ..
smoke : all pat_cover t\pat_cover.t \
t/pat_cover.ast.ast
perl util\run-smoke.pl . smoke.html
perl txt2html.pl t\*.t t\*.ast
clean:
copy t\pat_cover.ast.ast.html ..\ /Y
$(RM_F) encoding.html encoding.pod state_mac.xml encoding.ast \
pat_tree.ast state_mac.ast \
main.cod pat_cover.pod pat_cover.html types.cod \
hex2bin.exe hex2bin.obj
Special variable $@ will be expanded using its value in the context.
%.obj : %.asm
masm /t $<;
%.exe : %.obj
link /BATCH /NOLOGO $<;
The special variables $< and $* will be expanded according to the context.
.SUFFIXES: .obj .asm .exe
.asm.obj :
masm /t $<
.obj.exe :
link /nologo $<
At this moment, .SUFFIXES is a no-op. So any suffix-like things will be treated as suffixes, excluding the following example:
.c.o: foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
In suffix rules, no prerequisites are allowed according to most make tools.
objects = foo.o bar.o baz.o
sources = $(objects:.o=.c) # foo.c bar.c baz.c
Please consult the GNU make Manual for details and also take a look at the following file for some use cases:
http://github.com/agentzh/makefile-dom-pm/tree/master/t/gmake/sanity/func-refs.t <http://github.com/agentzh/makefile-dom-pm/tree/master/t/gmake/sanity/func-refs.t>
all : ; echo 'hello, world!'
Special variable $@ will be expanded using its value in the context.
For the list of features which will be added very soon, take a look at the "TODO" section.
This class provides the main interface to the Makefile parser.
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.
You can also pass a hash reference to specify initial variables and their values. Note that these variables are treated as "defaults" so assignments in the makefile have higher priority.
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.
%.o : %.c
$(CC) -c $<
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.
For the very latest version of this module, check out the source from http://github.com/agentzh/makefile-parser-pm <http://github.com/agentzh/makefile-parser-pm>. There is anonymous access to all.
The following syntax will be implemented soon:
"origin", "value", "call", "flavor", and "eval".
Please feel free to report bugs or send your wish-list to http://rt.cpan.org/NoAuth/Bugs.html?Dist=Makefile-Parser <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Makefile-Parser>.
plmake, makesimple, Makefile::Parser::GmakeDB, Makefile::GraphViz, Make.
Zhang "agentzh" Yichun, "<agentzh@gmail.com>"
Copyright (c) 2005-2011 by Zhang "agentzh" Yichun (XXX).
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.