COMPILER_HAS_HIDDEN*VISIBILITY checks fail in presence of unsupported -W option (or link-time only option)
CMake tests for C++ compiler support of the hidden visibility options by invoking the compiler and checking its diagnostics messages. This would work fine and reliably if the compiler were invoked with ONLY the test option, OR if only feedback about the option being tested were taken into account. Neither is the case. As a result the test gives false negatives when the user or build system introduces compiler options that causes compiler warning(s).
I ran into this issue because KDE's ECM add -Wdate-time to the compiler arguments, but this should be an easy test example: use clang and a warning option like -Wblabla
. The terminal output shows
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Failed
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Failed
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
and CMakeError.log has
Performing C++ SOURCE FILE Test COMPILER_HAS_HIDDEN_VISIBILITY failed with the following output:
Change Dir: /path/to/kf5-kdecoration/work/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_4559c/fast"
/usr/bin/make -f CMakeFiles/cmTC_4559c.dir/build.make CMakeFiles/cmTC_4559c.dir/build
make[1]: Entering directory `/path/to/kf5-kdecoration/work/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_4559c.dir/src.cxx.o
/opt/local/bin/clang++-mp-4.0 -Wblabla -flto -DNDEBUG -std=c++11 -arch x86_64 -std=c++0x -fno-operator-names -fno-exceptions -DQT_NO_EXCEPTIONS -Wno-gnu-zero-variadic-macro-arguments -Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef -Wnon-virtual-dtor -Woverloaded-virtual -Werror=return-type -Wvla -Wdate-time -pedantic -DCOMPILER_HAS_HIDDEN_VISIBILITY -fvisibility=hidden -o CMakeFiles/cmTC_4559c.dir/src.cxx.o -c /path/to/kf5-kdecoration/work/build/CMakeFiles/CMakeTmp/src.cxx
clang: warning: argument unused during compilation: '-arch x86_64' [-Wunused-command-line-argument]
warning: unknown warning option '-Wblabla' [-Wunknown-warning-option]
1 warning generated.
Linking CXX executable cmTC_4559c
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_4559c.dir/link.txt --verbose=1
/opt/local/bin/clang++-mp-4.0 -Wblabla -flto -DNDEBUG -std=c++11 -arch x86_64 -std=c++0x -fno-operator-names -fno-exceptions -DQT_NO_EXCEPTIONS -Wno-gnu-zero-variadic-macro-arguments -Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef -Wnon-virtual-dtor -Woverloaded-virtual -Werror=return-type -Wvla -Wdate-time -pedantic -DCOMPILER_HAS_HIDDEN_VISIBILITY -Wl,-R,/opt/local/lib -Wblabla -flto -arch x86_64 -rdynamic CMakeFiles/cmTC_4559c.dir/src.cxx.o -o cmTC_4559c
clang: warning: argument unused during compilation: '-arch x86_64' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-arch x86_64' [-Wunused-command-line-argument]
make[1]: Leaving directory `/path/to/kf5-kdecoration/work/build/CMakeFiles/CMakeTmp'
Source file was:
int main() { return 0; }
Curiously the warning about the -arch x86_64
command doesn't seem to interfere with the test. Still, I think that there's no guarantee that compiler warnings about options that are intended for the linker will never interfere with the testing being done.
I can see how testing, say, "hidden visibility" support as a function of the other compiler options could be useful, but ultimately that's no longer a test whether the compiler supports the option per se. I think that COMPILER_HAS_???
tests should test using only the compiler option to be tested, plus where (and only when) needed any options that are known to influence that support.