nzomkxia
nzomkxia

Reputation: 1267

how can I check a particular gcc feature in configure.ac

For example, gcc 4.7 has a new feature -Wnarrowing. In configure.ac, how can I test where a feature is supported by the current gcc or not?
There's a file in gnulibc, but doesn't make much sense to me.

Upvotes: 6

Views: 3386

Answers (3)

Brett Hale
Brett Hale

Reputation: 22308

Both gcc and clang support -W[no-]narrowing and -W[no-]error=narrowing options.

With -std=c++11, gcc emits a warning by default, and clang emits an error by default. Even though you only mention gcc, I think you could extend the functionality check to compilers like clang that attempt to provide the same options and extensions. That might include Intel's icc too.

Let's assume you've selected the C++ compiler with AC_PROG_CXX, and have ensured that it's using the C++11 standard.

ac_save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -Werror -Wno-error=narrowing"
AC_LANG_PUSH([C++])

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
  [[int i {1.0}; (void) i;]])],
  [ac_cxx_warn_narrowing=1], [ac_cxx_warn_narrowing=0])

AS_IF([test $ac_cxx_warn_narrowing -ne 0],
  [AC_MSG_RESULT(['$CXX' supports -Wnarrowing])])

AC_LANG_POP([C++])
CXXFLAGS="$ac_save_CXXFLAGS"

Compilation will only succeed if: 1) the compiler supports -Wnarrowing related options, which implies it supports -Werror, and: 2) recognizes C++11 initialization syntax.

Normally, configure.ac scripts and flags passed to configure should avoid -Werror, as it breaks too many internal tests. In this context, we ensure there are no other warnings besides the narrowing, which is why (void) i; is needed to prevent a warning about unused variables.

Upvotes: 2

Colin Phipps
Colin Phipps

Reputation: 908

See http://code.google.com/p/opendoom/source/browse/trunk/VisualC8/autotools/ac_c_compile_flags.m4 for an example test of this sort - this tries to compile a trivial program with the given compiler flag, and adds it to CFLAGS if it works.

Upvotes: 0

user743382
user743382

Reputation:

The logic behind this should probably be:

Create a correct file that should get a warning with -Wnarrowing. Verify that it gets compiled correctly. This is a sanity check.

Then compile that same file with -Wnarrowing, and verify that it still gets compiled correctly. This makes sure you detect compilers that don't support -Wnarrowing as an option, and don't attempt to pass bogus options to them.

Finally, compile that same file with -Werror=narrowing, and verify that it now does not get compiled correctly. If it now fails, you can be fairly certain that the compiler does indeed support -Wnarrowing. This last check is useful to detect compilers that do accept -Wnarrowing/-Werror=narrowing, but spit out a warning "ignoring unknown option -Wnarrowing". In that case, you shouldn't be passing -Wnarrowing.

Optionally, you may also want to compile a file that shouldn't get a warning with -Wnarrowing with -Werror=narrowing, in case you find a compiler where -Wnarrowing is useless and -Werror=narrowing is a hard error. I cannot think of a compiler where this would be required, though.

Translating this to a configure check should be trivial.

Upvotes: 1

Related Questions