Warning/Error for incorrect `check_cxx_compiler_flag` usage
I spent quite a while debugging why some check_cxx_compiler_flag in LLVM
were not working as expected. It turns out the configure checks were using:
check_cxx_compiler_flag(-Werror -Wempty-body COMPILER_RT_HAS_EMPTY_BODY_FLAG)
The check_cxx_compiler_flag macro only takes two arguments and passing
three to it ends up calling
cmake_check_compiler_flag(CXX "${_FLAG}" ${_RESULT})
with ${_FLAG}
equal to -Werror
and the result variable being the actually tested
compiler flag (-Wempty-body).
--debug-trycompile
shows the following surprising line in the generated
CMakeLists.txt:
add_definitions([==[-D-Wempty-body]==] [==[-Werror]==])
which then
results in the following error while running the check:
FAILED: CMakeFiles/cmTC_72736.dir/src.cxx.o
tmp/upstream-llvm-readonly/bin/clang++ -nodefaultlibs -std=c++17 -fcolor-diagnostics -D-Wempty-body -Werror -MD -MT CMakeFiles/cmTC_72736.dir/src.cxx.o -MF CMakeFiles/cmTC_72736.dir/src.cxx.o.d -o CMakeFiles/cmTC_72736.dir/src.cxx.o -c .../cmake-build-all-sanitizers/CMakeFiles/CMakeScratch/TryCompile-nyh3QR/src.cxx
In file included from <built-in>:450:
<command line>:1:9: error: macro name must be an identifier
1 | #define -Wempty-body 1
| ^
1 error generated.
I tried running the CMake debugger to see why this add_defitions line is being added, but I can't figure it out easily.
I think it would be great if the callers of cmake_check_compiler_flag() could warn/error for such incorrect usage that can be quite difficult to track down.