William Pursell
William Pursell

Reputation: 212404

Suppress unnecessary output to config.h.in

When AC_CHECK_HEADERS is invoked inside m4_foreach_w, autoheader(2.65) seems to generate a bogus template. For example:

m4_foreach_w([hdr],[[foo.h] [bar.h]],
    [AC_DEFINE([HAVE_]m4_translit(m4_toupper(hdr),[/.],[__]),[1],
        [Define to 1 if ]hdr[ is available])]
    [AC_CHECK_HEADERS([hdr])]
)

Causes config.h.in (assuming AC_CONFIG_HEADERS([config.h])) to have a template:

/* Define to 1 if you have the <hdr> header file.*/
#undef HAVE_HDR

Is there any way to avoid that bogus template? Note that the m4_foreach_w is being invoked inside a macro and the actual text is m4_foreach_w([hdr],[$1], and I'm wondering if I have an m4 quoting issue, but I suspect the problem is more fundamental. Which is to say, I always assume I've misquoted the m4--but that does not seem to be the problem here. Rather, autoheader is scanning the text without honoring the m4_foreach_w. I don't think the template causes any problems, but it looks really weird in the final config.h.

Upvotes: 2

Views: 214

Answers (1)

adl
adl

Reputation: 16054

Note that autoheader does not "scan" configure.ac. Rather, it runs configure.ac through m4 in trace mode, in order to catch low-level calls to AC_DEFINE_TRACE_LITERAL (which is called indirectly by AC_DEFINE) and to AH_OUTPUT (called byAC_CHECK_HEADERS). So because everything goes through m4, autoheader does not ignore m4_foreach. However it sees the arguments the way they are actually received by the macros.

You can see the problem in your code by tracing the calls to AC_DEFINE and AC_CHECK_HEADERS in your configure.ac:

% cat configure.ac
AC_INIT(somelib.so, 1.0, [email protected])
m4_foreach_w([hdr],[[foo.h] [bar.h]], [
     AC_DEFINE([HAVE_]m4_translit(m4_toupper(hdr),[/.],[__]),[1],
        [Define to 1 if ]hdr[ is available])
     AC_CHECK_HEADERS([hdr])
])
AC_CONFIG_HEADERS([config.h])
AC_OUTPUT
% autoconf -t AC_DEFINE -t AC_CHECK_HEADERS
configure.ac:3:AC_DEFINE:HAVE_FOO_H:1:Define to 1 if foo.h is available
configure.ac:3:AC_CHECK_HEADERS:hdr
configure.ac:3:AC_DEFINE:STDC_HEADERS:1:Define to 1 if you have the ANSI C header files.
configure.ac:3:AC_CHECK_HEADERS:sys/types.h sys/stat.h stdlib.h string.h memory.h strings.h inttypes.h stdint.h unistd.h:::$ac_includes_default
configure.ac:3:AC_DEFINE:HAVE_BAR_H:1:Define to 1 if bar.h is available
configure.ac:3:AC_CHECK_HEADERS:hdr

So we see that AC_CHECK_HEADERS is called twice with hdr as argument. While in the call to AC_DEFINE, hdr has been correctly expanded. That's because hdr is quoted one more time in your call to AC_CHECK_HEADERS, preventing its expansion.

I'd fix it this way:

m4_foreach_w([hdr],[[foo.h] [bar.h]], [
     AC_DEFINE([HAVE_]m4_translit(m4_toupper(hdr),[/.],[__]),[1],
        [Define to 1 if ]hdr[ is available])
     AC_CHECK_HEADERS(hdr)
])

(But that is still assuming that the names foo.h and bar.h will never trigger any macro.)

Upvotes: 3

Related Questions