AC_SUBST does not expand variable
In an Autotools project I am trying to generate parts of my .conf file. The program should read from $(pkgdatadir)
, but I know this variable is only set to Makefile.in
, so instead of datadir
and PACKAGE
should be replaced.
configure.ac:
AC_PREREQ([2.69])
AC_INIT([foo], [1.0.0], [me@foo.com])
AC_CONFIG_SRCDIR([foo.c])
AM_INIT_AUTOMAKE
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIRS([m4])
AC_SUBST(datadir)
AC_SUBST(PACKAGE)
AC_CONFIG_FILES(foo.conf)
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_MKDIR_P
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
foo.conf.in:
Dir = @datadir@/@PACKAGE@
As a result, foo.conf:
Dir = ${prefix}/share/foo
I would like autoconf to evaluate ${prefix}
on substitution, and I have no idea how.
source to share
Unfortunately, you cannot replace Makefile variables like datadir
configure-time as they are not fully expanded. (See documentation here .)
An unfortunate solution if you want to do both the wait time and the build time is to do a two-step substitution, from foo.conf.in.in
to foo.conf.in
at setup time and foo.conf.in
to foo.conf
at build time:
in configure.ac file:
AC_SUBST([PACKAGE])
AC_PROG_SED
AC_CONFIG_FILES([foo.conf.in])
in Makefile.am:
edit_script = $(SED) \
-e 's,%datadir%,$(datadir),'g \
-e ...other build-time substitutions you might want to do... \
$(NULL)
foo.conf: foo.conf.in Makefile
$(AM_V_GEN)rm -f $@ $@.tmp && \
$(edit_script) $< >$@.tmp && \
chmod a-w $@.tmp && \
mv $@.tmp $@
CLEANFILES += foo.conf
in the foo.conf.in.in file:
Dir = %datadir%/@PACKAGE@
I use marks %
to replace build times, so I don't confuse them with time-to-times marked replacements @
. The makefile rule above also makes the generated foo.conf
read-only, so you don't make it by mistake and overwrite your changes.
source to share
The second parameter can be added to AC_CONFIG_FILES
, which is the shell code to run in the context of variables inside the configure script.
So you can add some kind of substitution there. For example change
AC_CONFIG_FILES(foo.conf)
in
AC_CONFIG_FILES([foo.conf], [sed -i -e"s/\${prefix}/$prefix/" foo.conf])
What do I do if this code gets long and cumbersome to put it all in a separate file and run it, or perhaps its source.
Disclaimer: When I tried to replicate your results I was getting messages about --dataroot
that were ignored and the $ prefix was not showing up in foo.conf
the 2nd disclaimer. I'm not a fan of autoconf.
source to share