Skip to content

Commit

Permalink
[BUILD] Add cxx feature detections (#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Dec 18, 2024
1 parent 902ee88 commit d9ad23e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 54 deletions.
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,37 @@ if(BUILD_TESTING)
endif()
message(STATUS "GTEST_INCLUDE_DIRS = ${GTEST_INCLUDE_DIRS}")
message(STATUS "GTEST_BOTH_LIBRARIES = ${GTEST_BOTH_LIBRARIES}")

# Try to find gmock
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()
# Reset GMOCK_LIB if it was not found, or some target may link
# GMOCK_LIB-NOTFOUND
if(NOT GMOCK_LIB)
unset(GMOCK_LIB)
unset(GMOCK_LIB CACHE)
endif()
endif()

enable_testing()
if(WITH_BENCHMARK)
# Benchmark respects the CMAKE_PREFIX_PATH
Expand Down
12 changes: 12 additions & 0 deletions api/include/opentelemetry/detail/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@

#define OPENTELEMETRY_CONCAT(A, B) OPENTELEMETRY_CONCAT_(A, B)
#define OPENTELEMETRY_CONCAT_(A, B) A##B

// Import the C++20 feature-test macros
#ifdef __has_include
# if __has_include(<version>)
# include <version>
# endif
#elif defined(_MSC_VER) && ((defined(__cplusplus) && __cplusplus >= 202002L) || \
(defined(_MSVC_LANG) && _MSVC_LANG >= 202002L))
# if _MSC_VER >= 1922
# include <version>
# endif
#endif
9 changes: 8 additions & 1 deletion exporters/elasticsearch/src/es_log_recordable.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <opentelemetry/version.h>

#include <chrono>
#include <ctime>
#include <nlohmann/json.hpp>
#include <string>

#if defined(__cpp_lib_format)
# include <format>
#endif

#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
#include "opentelemetry/logs/severity.h"
#include "opentelemetry/nostd/variant.h"
Expand Down Expand Up @@ -71,7 +77,8 @@ void ElasticSearchRecordable::SetTimestamp(

// If built with with at least cpp 20 then use std::format
// Otherwise use the old style to format the timestamp in UTC
#if __cplusplus >= 202002L
// @see https://en.cppreference.com/w/cpp/feature_test#cpp_lib_format
#if defined(__cpp_lib_format) && __cpp_lib_format >= 201907
const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
#else
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
Expand Down
23 changes: 0 additions & 23 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,29 +313,6 @@ if(BUILD_TESTING)
TEST_PREFIX exporter.otlp.
TEST_LIST otlp_metrics_serialization_test)

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_OTLP_GRPC)
add_executable(otlp_grpc_exporter_test test/otlp_grpc_exporter_test.cc)
target_link_libraries(
Expand Down
11 changes: 0 additions & 11 deletions exporters/zipkin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ if(BUILD_TESTING)
TEST_PREFIX exporter.
TEST_LIST zipkin_recordable_test)

if(MSVC)
if(GMOCK_LIB)
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()

add_executable(zipkin_exporter_test test/zipkin_exporter_test.cc)

target_link_libraries(
Expand Down
19 changes: 0 additions & 19 deletions test_common/src/http/client/nosend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,6 @@ if(${BUILD_TESTING})
set_target_properties(opentelemetry_http_client_nosend
PROPERTIES EXPORT_NAME opentelemetry_http_client_nosend)

if(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(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()

target_link_libraries(
opentelemetry_http_client_nosend opentelemetry_ext
opentelemetry_test_common ${GMOCK_LIB} ${GTEST_BOTH_LIBRARIES})
Expand Down

1 comment on commit d9ad23e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp api Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: d9ad23e Previous: 902ee88 Ratio
BM_NaiveSpinLockThrashing/2/process_time/real_time 0.500281436148945 ms/iter 0.24713729989939723 ms/iter 2.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.