Skip to content

Commit d9ad23e

Browse files
authored
[BUILD] Add cxx feature detections (open-telemetry#3203)
1 parent 902ee88 commit d9ad23e

File tree

6 files changed

+51
-54
lines changed

6 files changed

+51
-54
lines changed

CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,37 @@ if(BUILD_TESTING)
645645
endif()
646646
message(STATUS "GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
647647
message(STATUS "GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")
648+
649+
# Try to find gmock
650+
if(NOT GMOCK_LIB AND TARGET GTest::gmock)
651+
set(GMOCK_LIB GTest::gmock)
652+
elseif(MSVC)
653+
# Explicitly specify that we consume GTest from shared library. The rest of
654+
# code logic below determines whether we link Release or Debug flavor of the
655+
# library. These flavors have different prefix on Windows, gmock and gmockd
656+
# respectively.
657+
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
658+
if(GMOCK_LIB)
659+
# unset GMOCK_LIB to force find_library to redo the lookup, as the cached
660+
# entry could cause linking to incorrect flavor of gmock and leading to
661+
# runtime error.
662+
unset(GMOCK_LIB CACHE)
663+
endif()
664+
endif()
665+
if(NOT GMOCK_LIB)
666+
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
667+
find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib)
668+
else()
669+
find_library(GMOCK_LIB gmock PATH_SUFFIXES lib)
670+
endif()
671+
# Reset GMOCK_LIB if it was not found, or some target may link
672+
# GMOCK_LIB-NOTFOUND
673+
if(NOT GMOCK_LIB)
674+
unset(GMOCK_LIB)
675+
unset(GMOCK_LIB CACHE)
676+
endif()
677+
endif()
678+
648679
enable_testing()
649680
if(WITH_BENCHMARK)
650681
# Benchmark respects the CMAKE_PREFIX_PATH

api/include/opentelemetry/detail/preprocessor.h

+12
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@
1111

1212
#define OPENTELEMETRY_CONCAT(A, B) OPENTELEMETRY_CONCAT_(A, B)
1313
#define OPENTELEMETRY_CONCAT_(A, B) A##B
14+
15+
// Import the C++20 feature-test macros
16+
#ifdef __has_include
17+
# if __has_include(<version>)
18+
# include <version>
19+
# endif
20+
#elif defined(_MSC_VER) && ((defined(__cplusplus) && __cplusplus >= 202002L) || \
21+
(defined(_MSVC_LANG) && _MSVC_LANG >= 202002L))
22+
# if _MSC_VER >= 1922
23+
# include <version>
24+
# endif
25+
#endif

exporters/elasticsearch/src/es_log_recordable.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <opentelemetry/version.h>
5+
46
#include <chrono>
57
#include <ctime>
68
#include <nlohmann/json.hpp>
79
#include <string>
810

11+
#if defined(__cpp_lib_format)
12+
# include <format>
13+
#endif
14+
915
#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
1016
#include "opentelemetry/logs/severity.h"
1117
#include "opentelemetry/nostd/variant.h"
@@ -71,7 +77,8 @@ void ElasticSearchRecordable::SetTimestamp(
7177

7278
// If built with with at least cpp 20 then use std::format
7379
// Otherwise use the old style to format the timestamp in UTC
74-
#if __cplusplus >= 202002L
80+
// @see https://en.cppreference.com/w/cpp/feature_test#cpp_lib_format
81+
#if defined(__cpp_lib_format) && __cpp_lib_format >= 201907
7582
const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
7683
#else
7784
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);

exporters/otlp/CMakeLists.txt

-23
Original file line numberDiff line numberDiff line change
@@ -313,29 +313,6 @@ if(BUILD_TESTING)
313313
TEST_PREFIX exporter.otlp.
314314
TEST_LIST otlp_metrics_serialization_test)
315315

316-
if(NOT GMOCK_LIB AND TARGET GTest::gmock)
317-
set(GMOCK_LIB GTest::gmock)
318-
elseif(MSVC)
319-
# Explicitly specify that we consume GTest from shared library. The rest of
320-
# code logic below determines whether we link Release or Debug flavor of the
321-
# library. These flavors have different prefix on Windows, gmock and gmockd
322-
# respectively.
323-
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
324-
if(GMOCK_LIB)
325-
# unset GMOCK_LIB to force find_library to redo the lookup, as the cached
326-
# entry could cause linking to incorrect flavor of gmock and leading to
327-
# runtime error.
328-
unset(GMOCK_LIB CACHE)
329-
endif()
330-
endif()
331-
if(NOT GMOCK_LIB)
332-
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
333-
find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib)
334-
else()
335-
find_library(GMOCK_LIB gmock PATH_SUFFIXES lib)
336-
endif()
337-
endif()
338-
339316
if(WITH_OTLP_GRPC)
340317
add_executable(otlp_grpc_exporter_test test/otlp_grpc_exporter_test.cc)
341318
target_link_libraries(

exporters/zipkin/CMakeLists.txt

-11
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@ if(BUILD_TESTING)
4949
TEST_PREFIX exporter.
5050
TEST_LIST zipkin_recordable_test)
5151

52-
if(MSVC)
53-
if(GMOCK_LIB)
54-
unset(GMOCK_LIB CACHE)
55-
endif()
56-
endif()
57-
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
58-
find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib)
59-
else()
60-
find_library(GMOCK_LIB gmock PATH_SUFFIXES lib)
61-
endif()
62-
6352
add_executable(zipkin_exporter_test test/zipkin_exporter_test.cc)
6453

6554
target_link_libraries(

test_common/src/http/client/nosend/CMakeLists.txt

-19
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ if(${BUILD_TESTING})
88
set_target_properties(opentelemetry_http_client_nosend
99
PROPERTIES EXPORT_NAME opentelemetry_http_client_nosend)
1010

11-
if(MSVC)
12-
# Explicitly specify that we consume GTest from shared library. The rest of
13-
# code logic below determines whether we link Release or Debug flavor of the
14-
# library. These flavors have different prefix on Windows, gmock and gmockd
15-
# respectively.
16-
add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1)
17-
if(GMOCK_LIB)
18-
# unset GMOCK_LIB to force find_library to redo the lookup, as the cached
19-
# entry could cause linking to incorrect flavor of gmock and leading to
20-
# runtime error.
21-
unset(GMOCK_LIB CACHE)
22-
endif()
23-
endif()
24-
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
25-
find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib)
26-
else()
27-
find_library(GMOCK_LIB gmock PATH_SUFFIXES lib)
28-
endif()
29-
3011
target_link_libraries(
3112
opentelemetry_http_client_nosend opentelemetry_ext
3213
opentelemetry_test_common ${GMOCK_LIB} ${GTEST_BOTH_LIBRARIES})

0 commit comments

Comments
 (0)