From 50cf4b1f6e93403ff1d5f0b080797f4edb42a5a5 Mon Sep 17 00:00:00 2001 From: Artur Wojcik Date: Tue, 31 Oct 2023 18:17:41 +0100 Subject: [PATCH] [Windows] cmake: fix cmake/googletest.cmake on Windows (#2350) * cmake: fix cmake/googletest.cmake on Windows * incorporate review feedback --------- Co-authored-by: Artur Wojcik --- cmake/googletest.cmake | 44 --------------------- cmake/gtest.cmake | 82 +++++++++++++++++++++++++++++++++++++++ test/gtest/CMakeLists.txt | 11 ++---- 3 files changed, 86 insertions(+), 51 deletions(-) delete mode 100644 cmake/googletest.cmake create mode 100644 cmake/gtest.cmake diff --git a/cmake/googletest.cmake b/cmake/googletest.cmake deleted file mode 100644 index 5df2f3963f..0000000000 --- a/cmake/googletest.cmake +++ /dev/null @@ -1,44 +0,0 @@ -include(FetchContent) - -set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) - -set(GOOGLETEST_DIR "" CACHE STRING "Location of local GoogleTest repo to build against") - -if(GOOGLETEST_DIR) - set(FETCHCONTENT_SOURCE_DIR_GOOGLETEST ${GOOGLETEST_DIR} CACHE STRING "GoogleTest source directory override") -endif() - -message(STATUS "Fetching GoogleTest") - -list(APPEND GTEST_CMAKE_CXX_FLAGS - -Wno-undef - -Wno-reserved-identifier - -Wno-global-constructors - -Wno-missing-noreturn - -Wno-disabled-macro-expansion - -Wno-used-but-marked-unused - -Wno-switch-enum - -Wno-zero-as-null-pointer-constant - -Wno-unused-member-function - -Wno-comma - -Wno-old-style-cast - -Wno-deprecated - -Wno-unsafe-buffer-usage - -Wno-float-equal -) -message(STATUS "Suppressing googltest warnings with flags: ${GTEST_CMAKE_CXX_FLAGS}") - -FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG f8d7d77c06936315286eb55f8de22cd23c188571 -) - -# Will be necessary for windows build -# set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(googletest) - -target_compile_options(gtest PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) -target_compile_options(gtest_main PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) -target_compile_options(gmock PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) -target_compile_options(gmock_main PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) diff --git a/cmake/gtest.cmake b/cmake/gtest.cmake new file mode 100644 index 0000000000..249033cd81 --- /dev/null +++ b/cmake/gtest.cmake @@ -0,0 +1,82 @@ +################################################################################ +# +# MIT License +# +# Copyright (c) 2023 Advanced Micro Devices, Inc. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +################################################################################ + +include(FetchContent) + +set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + +set(GOOGLETEST_DIR "" CACHE STRING "Location of local GoogleTest repo to build against") + +if(GOOGLETEST_DIR) + set(FETCHCONTENT_SOURCE_DIR_GOOGLETEST ${GOOGLETEST_DIR} CACHE STRING "GoogleTest source directory override") +endif() + +set(BUILD_GMOCK OFF CACHE INTERNAL "") + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG f8d7d77c06936315286eb55f8de22cd23c188571 +) + +if(WIN32) + set(gtest_force_shared_crt ON CACHE INTERNAL "") +endif() + +# Store the current value of BUILD_SHARED_LIBS +set(__build_shared_libs ${BUILD_SHARED_LIBS}) +set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "") + +FetchContent_MakeAvailable(googletest) + +# Restore the old value of BUILD_SHARED_LIBS +set(BUILD_SHARED_LIBS ${__build_shared_libs} CACHE BOOL "Type of libraries to build" FORCE) + +set(GTEST_CMAKE_CXX_FLAGS + -Wno-undef + -Wno-reserved-identifier + -Wno-global-constructors + -Wno-missing-noreturn + -Wno-disabled-macro-expansion + -Wno-used-but-marked-unused + -Wno-switch-enum + -Wno-zero-as-null-pointer-constant + -Wno-unused-member-function + -Wno-comma + -Wno-old-style-cast + -Wno-deprecated + -Wno-unsafe-buffer-usage + -Wno-float-equal + -Wno-ignored-attributes + -Wno-incompatible-pointer-types + -Wno-enum-constexpr-conversion + -Wno-deprecated-builtins) + +target_compile_options(gtest PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) +target_compile_options(gtest_main PRIVATE ${GTEST_CMAKE_CXX_FLAGS}) + +include(GoogleTest) +unset(GTEST_CMAKE_CXX_FLAGS) diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index a752cb2cc0..2740730a90 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -1,11 +1,9 @@ -include(googletest) #include googletest.cmake +include(gtest) #include gtest.cmake enable_testing() -include(GoogleTest) find_package(rocblas) -set(SOURCES) -list(APPEND SOURCES +set(SOURCES log.cpp platform.cpp ) @@ -26,10 +24,9 @@ function(add_gtest TEST_NAME) target_include_directories(test_${TEST_NAME} SYSTEM PRIVATE $) target_include_directories(test_${TEST_NAME} SYSTEM PRIVATE $) endif() + target_link_libraries(test_${TEST_NAME} gtest gtest_main MIOpen ${Boost_LIBRARIES} hip::host $) if(NOT MIOPEN_EMBED_DB STREQUAL "") - target_link_libraries(test_${TEST_NAME} gtest_main MIOpen ${Boost_LIBRARIES} hip::host $ $) - else() - target_link_libraries(test_${TEST_NAME} gtest_main MIOpen ${Boost_LIBRARIES} hip::host $) + target_link_libraries(test_${TEST_NAME} $) endif() # Enable CMake to discover the test binary gtest_discover_tests(test_${TEST_NAME} PROPERTIES ENVIRONMENT "MIOPEN_USER_DB_PATH=${CMAKE_CURRENT_BINARY_DIR};MIOPEN_TEST_FLOAT_ARG=${MIOPEN_TEST_FLOAT_ARG};MIOPEN_TEST_ALL=${MIOPEN_TEST_ALL};MIOPEN_TEST_MLIR=${MIOPEN_TEST_MLIR};MIOPEN_TEST_COMPOSABLEKERNEL=${MIOPEN_TEST_COMPOSABLEKERNEL}")