CMake incorrectly identifies flag as valid for CLang when compiling C files
I have the following test for flags to be used by GCC and CLANG under Linux (gcc 7.3, clang 5.0.1):
# Test flags for the C compiler and add them to the command line if they are supported
list(APPEND C_COMPILER_FLAGS_TO_USE "-pedantic-errors" "-Wall" "-std=c99" "-std=gnu99" "-ferror-limit=1" "-fmax-errors=1" "-Wfatal-errors" "-Wextra" "-march=native")
foreach (FLAG IN LISTS C_COMPILER_FLAGS_TO_USE)
# Check if the compiler supports the flag. The result of this test
# is printed to STDOUT when calling `cmake`.
string(REGEX REPLACE "[-=+]" "" FLAG_NO_SIGNS ${FLAG})
check_c_compiler_flag(${FLAG} C_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
if(C_COMPILER_SUPPORTS_${FLAG_NO_SIGNS})
string(APPEND CMAKE_C_FLAGS " ${FLAG}")
endif()
endforeach()
I'm testing both "-ferror-limit=1"
and "-fmax-errors=1"
since as I say, I'm compiling on GCC and CLANG simultaneously.
In this case, the CMake test marks both options as valid for CLANG:
-- Performing Test C_COMPILER_SUPPORTS_ferrorlimit1
-- Performing Test C_COMPILER_SUPPORTS_ferrorlimit1 - Success
-- Performing Test C_COMPILER_SUPPORTS_fmaxerrors1
-- Performing Test C_COMPILER_SUPPORTS_fmaxerrors1 - Success
while for GCC, only "-fmax-errors=1"
is marked as valid:
-- Performing Test C_COMPILER_SUPPORTS_ferrorlimit1
-- Performing Test C_COMPILER_SUPPORTS_ferrorlimit1 - Failed
-- Performing Test C_COMPILER_SUPPORTS_fmaxerrors1
-- Performing Test C_COMPILER_SUPPORTS_fmaxerrors1 - Success
(I've removed the -
, =
and +
from the output for the sake of clarity, the test is performed using the full and proper option).
The problem arises later, during compilation, when CLang complains:
[ 12%] Building C object BZraw/src/CMakeFiles/bzraw_objects.dir/gclean.c.o
clang-5.0: warning: argument unused during compilation: '-fmax-errors=1' [-Wunused-command-line-argument]
Surprinsingly, the "-fmax-errors=1"
is NOT marked as supported when testing the C++ options, only the C ones.
Just in case:
CMake 3.13.4
CentOS 7.6
CLang 5.0.1