5

We use autotools as our build infrastructure, and we use clang and gcc as compilers. Recently we hit a gcc warning that needed

--param max-vartrack-size=100000000

to silence (without completey disabling gcc's vartracking). Clang doesn't accept that option and produces

argument unused during compilation: '--param max-vartrack-size=100000000'

To silence that, clang needs

-Qunused-arguments

and that produces an error under gcc:

unrecognized command line option ‘-Qunused-arguments’
  1. What is the best way to define compiler-specific flags in configure.ac, after a compiler has been selected with, e.g. AC_PROG_CXX? We AC_SUBST(AM_CXXFLAGS) so I guess I'd be expanding a compiler specific variable within AM_CXXFLAGS.

  2. What is the right way to enable a per-target option in a Makefile.am for just one compiler? I'm thinking of:

    if HAVE_GCC
        SPECIFIC_CXXFLAGS = --param...
    endif
    if HAVE_CLANG
        SPECIFIC_CXXFLAGS = -Q...
    endif
    libfoo_la_CXXFLAGS = $(AM_CXXFLAGS) $(SPECIFIC_CXXFLAGS)
    

but I'd need the HAVE_* vars subst'ed from configure.ac. Do AC_PROG_CXX/CC perhaps already define something like this?

Irfy
  • 9,323
  • 1
  • 45
  • 67

2 Answers2

3

AM_CXXFLAGS isn't something you should AC_SUBST. It is reserved for use by automake. If you look at the Makefile generated for a C++ target, you will find definitions for CXXCOMPILE and LTCXXCOMPILE, which always include the AM_CXXFLAGS variable.

You want to add the (conditional) compiler flag to AM_CXXFLAGS or to libfoo_la_CXXFLAGS. The former will affect all C++ compilations, and the latter just the per-library compilations. So it's just a matter of getting SPECIFIC_CXXFLAGS right in configure.ac.

AC_PROG_CXX
...
FOO_SPECIFIC_CXXFLAGS=;
if `$CXX -v 2>&1 | grep 'gcc version' >/dev/null 2>&1` ; then
  FOO_SPECIFIC_CXXFLAGS="--param max-vartrack-size=100000000"
fi

AC_SUBST(FOO_SPECIFIC_CXXFLAGS, $FOO_SPECIFIC_CXXFLAGS)

The GXX test is insufficient as autoconf just tests for the __GNUC__ macro (AFAIK), so clang++ will set: GXX=yes

The problem is, there isn't a portable way of detecting command line options that are unknown. Intel's icc -v will even reproduce the gcc version string. So you might have to add another filter, like: | grep -v 'icc'

You could optionally check that the flag works as advertised before AC_SUBST, but this doesn't really help if the compiler only generates a warning:

saved_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS $FOO_SPECIFIC_CXXFLAGS"

AC_LANG_PUSH([C++])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],,[FOO_SPECIFIC_CXXFLAGS=;])
AC_LANG_POP([C++])
CXXFLAGS="$saved_CXXFLAGS"

Then in Makefile.am:

AM_CXXFLAGS = $(FOO_SPECIFIC_CXXFLAGS)

or:

libfoo_la_CXXFLAGS = $(FOO_SPECIFIC_CXXFLAGS)
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • I'm puzzled by your statement that one should not `AC_SUBST` `AM_CXXFLAGS`. How is that different from setting `AM_CXXFLAGS` in all of the 15ish `Makefile.am`s by hand? `AC_SUBST`ing `AM_CXXFLAGS` is the perfect solution for things like enabling warnings for the whole project by default, or choosing the older ABI with gcc 5.1 (which needs to be the same for all objects). How else would I achieve this, short of setting `AM_CXXFLAGS` in each of the `Makefile.am`s? – Irfy Jun 11 '15 at 09:41
  • I opted to add two `AM_CONDITIONAL`s to my configure.ac, `HAVE_GCC` and `HAVE_CLANG` with the conditional test equivalent to your `grep` check. Thanks! – Irfy Jun 11 '15 at 10:03
  • @Irfy - that's what `CXXFLAGS` is for. You can pass compiler specific flags to `configure` via `CXXFLAGS`, or add / modify `CXXFLAGS` flags in `configure.ac`. This variable is also included in the C++ build rules. – Brett Hale Jun 17 '15 at 15:05
1

If some flag produces an error it is easy to write an m4 macro to detect if the compiler supports this flag.

Check ax_check_compile_flag from the autoconf archive.

arved
  • 4,401
  • 4
  • 30
  • 53
  • I'm sure this is the *right* way to do it, but since it's an internal project, not distributed, I opted for the simplest thing possible, which turned out to be two `AM_CONDITIONAL`s. – Irfy Jun 11 '15 at 10:04