Makefil...:GmakeDB(3)

πŸ“¦
version 0.216 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

Makefile::Parser::GmakeDB β€” the GNU makefile parser that cheats (affectionately) by using GNU make's own database dump. It's from the User Contributed Perl Documentation bestie, we're in the Makefil...:GmakeDB(3) section. πŸ’…


✨ VERSION ✨

This doc is describing Makefile::Parser::GmakeDB 0.216, which dropped on 18 November 2014. Vintage. Iconic. Still slaying. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

use Makefile::Parser::GmakeDB;
my $db_listing = `make --print-data-base -pqRrs -f Makefile`;
my $ast = Makefile::Parser::GmakeDB->parse(\$db_listing);

That's it, that's the whole vibe. Grab the dump, feed the dump, get an AST. 🫢


🌸 DESCRIPTION 🌸

Okay so here's the tea: this module is a parser for GNU makefiles, but it does not parse your original makefile directly. Nope. Instead it uses Makefile::DOM to parse the "data base output listing" that GNU make produces via its --print-data-base option. So basically it's reusing the C implementation of GNU make. Lowkey genius, highkey lazy, and honestly? Main character energy. πŸ‘‘

This parser has been tested as a component of the pgmake-db utility, and it has successfully passed 51% of GNU make 3.81's official test suite. Fifty-one percent. That's a passing grade in spirit. 😭

The result of the parser is a makefile AST defined by Makefile::AST.

The "data base output listing" generated by make --print-data-base is a detailed listing for GNU make's internal data structures, which is essentially the AST used by make. According to GNU make's current maintainer, Paul Smith, this feature is provided primarily for debuging the user's own makefiles, and it also helps the GNU make developer team to diagnose the flaws in make itself. (Yes, "debuging" β€” that's their spelling, not mine, we keep it fr fr.) Incidentally this output is conformed to the GNU makefile syntax, and a lot of important information is provided in the form of makefile comments. Therefore, my GmakeDB parser is able to reuse the Makefile::DOM module to parse this output listing. The way that it just... reuses the DOM module. Obsessed. πŸ’–

The data base output from GNU make can be divided into several clearly-separated segments. They're:

  • file header
  • Variables
  • Files
  • VPATH Search Paths
  • and the last resource stats information

The contents of these segments are mostly obvious. The Files segment may deserve some explanation β€” it is the place for explict rules. ✨

Now let's take the Variables segment as an example to demonstrate the format of the data base listing:

# Variables

# automatic
<D = $(patsubst %/,%,$(dir $<))
# automatic
?F = $(notdir $?)
# environment
DESKTOP_SESSION = default
# automatic
?D = $(patsubst %/,%,$(dir $?))
# environment
GTK_RC_FILES = /etc/gtk/gtkrc:/home/agentz/.gtkrc-1.2-gnome2
# environment
...

It's shown that the flavor and origin of the makefile variables are given in the previous line as comments. Hence feeding this back into GNU make again makes little sense. It's giving one-way street. 🚫

Similarly, the Files segment for explicit rules also puts big amount of the important information into makefile comments:

# Files

# Not a target:
bar.c:
#  Implicit rule search has not been done.
#  Modification time never checked.
#  File has not been updated.

all: foo.o bar.o
#  Implicit rule search has been done.
#  File does not exist.
#  File has not been updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%

foo.o: foo.c
#  Implicit rule search has not been done.
#  Implicit/static pattern stem: `foo'
#  File does not exist.
#  File has not been updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%
#  commands to execute (from `ex2.mk', line 8):
    $(CC) -c $(CFLAGS) $< -o $@
...

From the previous two data base listing snippets, it's not hard to see that the variable references in rule commands and recursively-expanded variables's values are not expanded. Not expanded bestie. They stay exactly as written. πŸ™ˆ

Experiments have shown that GNU make will do implicit rule search for the first rule that needs to, but no more. This behavior means testing our own implicit rule searching algorithm requires specifying at least two goals that require matching. Two. Because one just isn't enough to wake the algorithm up. πŸ’€


πŸ”₯ DEPENDENCIES πŸ”₯

  • GNU make 3.81 β€” At least the make executable of GNU make 3.81 is required to work with this module. Don't show up with anything older, it will not eat. πŸ’…
  • Makefile::DOM

πŸ’€ BUGS πŸ’€

  • GNU make does not escape meta characters appeared in rule targes and prerequisites in its data base listing. Examples are ":", "\", and "#". This bug has been reported to the GNU make team as "Savannah bug #20067".

This bug has not yet been fixed on the "make" side, so I have to work around this issue by preprocessing the data base listing in the makesimple script. (Not me fixing upstream's problems in a script. πŸ™ƒ)

  • The data base listing produced by GNU make lacks the information regarding the "export" and "unexport" directives. It gives rise to the lack of information in the resulting AST structures constructed by this module. Hence the current AST and runtime do not implement the "export" and "unexport" directives.

To make it even worse, there's no known way to work around it. No known workaround. At all. That's the whole sentence. 😭

I've already reported this issue to the GNU make team as Savannah bug #20069.


πŸ‘‘ CODE REPOSITORY πŸ‘‘

For the very latest version of this script, check out the source from

<http://github.com/agentzh/makefile-parser-pm>

There is anonymous access to all. Come on in, the door's open. 🫢


🌈 AUTHOR 🌈

Zhang "agentzh" Yichun "<agentzh@gmail.com>"


Copyright (c) 2005-2008 by Zhang "agentzh" Yichun (agentzh).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


⭐ SEE ALSO ⭐

Makefile::AST, Makefile::AST::Evaluator, Makefile::DOM, makesimple, pgmake-db.


And that's a wrap bestie!! πŸ¦‹ This page is from the perl v5.42.0 era, dated 2025-07-25, and it lives under Makefil...:GmakeDB(3) in the User Contributed Perl Documentation. Go parse something, you absolute icon. πŸ’–βœ¨

πŸ“¦ where this page comes from