Noeud:Using Gperf with the GNU Build System, Noeud « Next »:Exercises on Gperf, Noeud « Previous »:Advanced Use of Gperf, Noeud « Up »:Scanning with Gperf
Currently neither Autoconf nor Automake provide direct Gperf support, but interfacing Gperf with them is straightforward.
All configure.ac
needs to do is to look for gperf
and
to provide its definition for Makefiles via AC_SUBST
Autoconf
(FIXME: ref
Autoconf.). In fact, using Automake's macro to put gperf
under
the control of missing
is enough:
AM_MISSING_PROG([GPERF], [gperf])
Then your Makefile.am
should include:
# Handling the Gperf code GPERFFLAGS = --compare-strncmp --switch=1 --language=ANSI-C BUILT_SOURCES = atoms.c atoms.c: atoms.gperf if $(GPERF) $(GPERFFLAGS) --key-positions=1,3,4 --struct-type \ atoms.gperf >$@t; then \ mv $@t $@; \ elif $(GPERF) --version >/dev/null 2>&1; then \ rm $@t; \ exit 1; \ else \ rm $@t; \ touch $@; \ fi
I personally avoid using short options in scripts and Makefiles, because
short options are likely to change and because long options are easier
to understand when you don't know the program. Do not forget to help
Automake understand that atoms.c
is to be built early (before its
uses) using BUILT_SOURCES
.
The program gperf
is a maintainer requirement: someone
changing the package needs it, but its result is shipped so that a
regular user does not need it. Hence, we go through some hoops in order
to ensure that a failed run of gperf
doesn't erase the
maintainer's pre-built copy of atoms.c
. Had gperf
provided an --output
option, missing
would have
handled these details gracefully.
There are three cases to handle:
gperf
succeeded
@t
, as the actual
output, @
;
gperf
failed
$(GPERF)
invocation failed, but $(GPERF) --version
succeeded, then this is certainly an actual error in the input file. In
this case, do not hide the failure and exit with failure.
gperf
is missing
$(GPERF)
does not answer to --version
, it is
certainly missing, and missing
already suggested to install
Gperf. Then remove the temporary output file, and let the compilation
proceed by updating the timestamp of the output file. That's a best
effort, essentially helping users who get the project with broken
timestamps.