Skip to content

Commit 27e9d56

Browse files
committed
cmake: disable deprecated warning when building googletest
In commit 986f691, we updated the googletest submodule to silence a CMake warning. However, this change broke the build due to a Clang issue where it warns about deprecated declarations even from system headers (see llvm/llvm-project#76515). Since we use `-Werror` in our compile options, these warnings become errors and fail the build. This change detects if we're affected by the specific combination of compiler, standard library, and compile options that triggers this issue. It then conditionally disables the `-Wdeprecated-declarations` warning when building googletest to resolve build failures caused by deprecated functions like `get_temporary_buffer<>` in the standard library. The build failure looks like: ``` FAILED: src/googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o ... In file included from /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest-all.cc:38: In file included from /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/include/gtest/gtest.h:55: In file included from /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/memory:66: /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/stl_tempbuf.h:263:8: error: 'get_temporary_buffer<testing::TestInfo *>' is deprecated [-Werror,-Wdeprecated-declarations] 263 | std::get_temporary_buffer<value_type>(_M_original_len)); | ^ /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/stl_algo.h:4996:15: note: in instantiation of member function 'std::_Temporary_buffer<__gnu_cxx::__normal_iterator<testing::TestInfo **, std::vector<testing::TestInfo *>>, testing::TestInfo *>::_Temporary_buffer' requested here 4996 | _TmpBuf __buf(__first, (__last - __first + 1) / 2); | ^ /usr/lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/bits/stl_algo.h:5070:23: note: in instantiation of function template specialization 'std::__stable_sort<__gnu_cxx::__normal_iterator<testing::TestInfo **, std::vector<testing::TestInfo *>>, __gnu_cxx::__ops::_Iter_comp_iter<(lambda at /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3024:20)>>' requested here 5070 | _GLIBCXX_STD_A::__stable_sort(__first, __last, | ^ /home/jenkins-build/build/workspace/ceph-pull-requests/src/googletest/googletest/src/gtest.cc:3023:8: note: in instantiation of function template specializatio ``` Signed-off-by: Kefu Chai <[email protected]>
1 parent 7bc3ddb commit 27e9d56

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

cmake/modules/CephChecks.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ else(NOT CMAKE_CROSSCOMPILING)
150150
message(STATUS "Assuming unaligned access is supported")
151151
endif(NOT CMAKE_CROSSCOMPILING)
152152

153+
# Clang warns on deprecated specialization used in system
154+
# headers. but libstdc++-12 uses deprecated get_temporary_buffer<>
155+
# to implement templated stable_sort(), which is turn used by
156+
# googletest. see https://github.com/llvm/llvm-project/issues/76515
157+
# Let's detect it, so we can disable -Wdeprecated-declarations when
158+
# building googletest.
159+
cmake_push_check_state(RESET)
160+
set(CMAKE_REQUIRED_FLAGS "-Werror=deprecated-declarations")
161+
check_cxx_source_compiles("
162+
#include <algorithm>
163+
int main() { std::stable_sort((int *)0, (int*)0); }
164+
" COMPILER_IGNORES_DEPRECATED_DECL_IN_SYSTEM_HEADERS)
165+
cmake_pop_check_state()
166+
153167
set(version_script_source "v1 { }; v2 { } v1;")
154168
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/version_script.txt "${version_script_source}")
155169
cmake_push_check_state(RESET)

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,12 @@ if(WITH_TESTS)
767767
find_package(GTest 1.13.0 REQUIRED)
768768
find_package(GMock REQUIRED)
769769
else()
770+
if(NOT COMPILER_IGNORES_DEPRECATED_DECL_IN_SYSTEM_HEADERS)
771+
# See https://github.com/llvm/llvm-project/issues/76515
772+
set_property(DIRECTORY googletest
773+
APPEND "-Wno-deprecated-declarations"
774+
PROPERTY COMPILE_OPTIONS)
775+
endif()
770776
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
771777
add_subdirectory(googletest)
772778
add_library(GMock::GMock ALIAS gmock)

0 commit comments

Comments
 (0)