From 3aff75847783dbaa7113ad7adf3cdb0fd3b15bbf Mon Sep 17 00:00:00 2001 From: Boris Kuschel Date: Thu, 21 Nov 2024 18:38:32 -0800 Subject: [PATCH] Fix vs2022 build --- CMakeLists.txt | 5 +++- ext/test/http/CMakeLists.txt | 23 +++++++++++++++++++ .../src/http/client/nosend/CMakeLists.txt | 18 +++++++++------ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9e017eed2..e0587f3999 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,10 @@ else() endif() endif() message(STATUS "Building for architecture ARCH=${ARCH}") - +# Force C++17 +if(MSVC) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17") +endif(MSVC) # Autodetect vcpkg toolchain if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) set(CMAKE_TOOLCHAIN_FILE diff --git a/ext/test/http/CMakeLists.txt b/ext/test/http/CMakeLists.txt index 9f5514d07b..41a79baa4c 100644 --- a/ext/test/http/CMakeLists.txt +++ b/ext/test/http/CMakeLists.txt @@ -1,6 +1,29 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 +if(NOT GMOCK_LIB AND TARGET GTest::gmock) + set(GMOCK_LIB GTest::gmock) +elseif(MSVC) + # Explicitly specify that we consume GTest from shared library. The rest of + # code logic below determines whether we link Release or Debug flavor of the + # library. These flavors have different prefix on Windows, gmock and gmockd + # respectively. + add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1) + if(GMOCK_LIB) + # unset GMOCK_LIB to force find_library to redo the lookup, as the cached + # entry could cause linking to incorrect flavor of gmock and leading to + # runtime error. + unset(GMOCK_LIB CACHE) + endif() +endif() +if(NOT GMOCK_LIB) + if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") + find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) + else() + find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) + endif() +endif() + if(WITH_HTTP_CLIENT_CURL) set(FILENAME curl_http_test) add_compile_definitions(WITH_CURL) diff --git a/test_common/src/http/client/nosend/CMakeLists.txt b/test_common/src/http/client/nosend/CMakeLists.txt index 7394053a22..2b68399f28 100644 --- a/test_common/src/http/client/nosend/CMakeLists.txt +++ b/test_common/src/http/client/nosend/CMakeLists.txt @@ -7,8 +7,9 @@ if(${BUILD_TESTING}) set_target_properties(opentelemetry_http_client_nosend PROPERTIES EXPORT_NAME opentelemetry_http_client_nosend) - - if(MSVC) + if(NOT GMOCK_LIB AND TARGET GTest::gmock) + set(GMOCK_LIB GTest::gmock) + elseif(MSVC) # Explicitly specify that we consume GTest from shared library. The rest of # code logic below determines whether we link Release or Debug flavor of the # library. These flavors have different prefix on Windows, gmock and gmockd @@ -21,12 +22,15 @@ if(${BUILD_TESTING}) unset(GMOCK_LIB CACHE) endif() endif() - if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") - find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) - else() - find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) - endif() + if(NOT GMOCK_LIB) + if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") + find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) + else() + find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) + endif() + endif() + target_link_libraries( opentelemetry_http_client_nosend opentelemetry_ext opentelemetry_test_common ${GMOCK_LIB} ${GTEST_BOTH_LIBRARIES})