diff --git a/CMakeLists.txt b/CMakeLists.txt index d9a4c5f668..664cc229ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,42 +122,6 @@ option(WITH_GSL option(WITH_ABSEIL "Whether to use Abseil for C++latest features" OFF) -if(WITH_STL STREQUAL "OFF") - add_definitions(-DOPENTELEMETRY_STL_VERSION=0) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 11) - endif() - # STL not used. -elseif(WITH_STL STREQUAL "CXX11") - add_definitions(-DOPENTELEMETRY_STL_VERSION=2011) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 11) - endif() -elseif(WITH_STL STREQUAL "CXX14") - add_definitions(-DOPENTELEMETRY_STL_VERSION=2014) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 14) - endif() -elseif(WITH_STL STREQUAL "CXX17") - add_definitions(-DOPENTELEMETRY_STL_VERSION=2017) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 17) - endif() -elseif(WITH_STL STREQUAL "CXX20") - add_definitions(-DOPENTELEMETRY_STL_VERSION=2020) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 20) - endif() -elseif(WITH_STL STREQUAL "ON") - # "ON" corresponds to "CXX20" at this time. - add_definitions(-DOPENTELEMETRY_STL_VERSION=2020) - if(NOT DEFINED CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 20) - endif() -else() - message(FATAL_ERROR "WITH_STL must be ON, OFF, CXX11, CXX14, CXX17 or CXX20") -endif() - set(OPENTELEMETRY_INSTALL_default ON) if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) set(OPENTELEMETRY_INSTALL_default OFF) @@ -167,21 +131,6 @@ option(OPENTELEMETRY_INSTALL "Whether to install opentelemetry targets" include("${PROJECT_SOURCE_DIR}/cmake/tools.cmake") -if(NOT DEFINED CMAKE_CXX_STANDARD) - if(NOT WITH_STL STREQUAL "OFF") - # Require at least C++17. C++20 is needed to avoid gsl::span - if(CMAKE_VERSION VERSION_GREATER 3.11.999) - # Ask for 20, may get anything below - set(CMAKE_CXX_STANDARD 20) - else() - # Ask for 17, may get anything below - set(CMAKE_CXX_STANDARD 17) - endif() - else() - set(CMAKE_CXX_STANDARD 11) - endif() -endif() - if(NOT WITH_STL STREQUAL "OFF") # These definitions are needed for test projects that do not link against # opentelemetry-api library directly. We ensure that variant implementation diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index 41b6997980..de5fd09110 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -51,11 +51,27 @@ if(WITH_ABSEIL) "absl_bits" "absl_city") endif() -if(WITH_STL) - message("Building with standard library types...") - target_compile_definitions(opentelemetry_api INTERFACE HAVE_CPP_STDLIB) +if(WITH_STL STREQUAL "OFF") + message(STATUS "Building WITH_STL=OFF") + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=0) +elseif(WITH_STL STREQUAL "CXX11") + message(STATUS "Building WITH_STL=CXX1") + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=2011) +elseif(WITH_STL STREQUAL "CXX14") + message(STATUS "Building WITH_STL=CXX14") + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=2014) +elseif(WITH_STL STREQUAL "CXX17") + message(STATUS "Building WITH_STL=CXX17") + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=2017) +elseif(WITH_STL STREQUAL "CXX20") + message(STATUS "Building WITH_STL=CXX20") + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=2020) +elseif(WITH_STL STREQUAL "ON") + message(STATUS "Building WITH_STL=ON") + # "ON" corresponds to "CXX20" at this time. + target_compile_definitions(opentelemetry_api INTERFACE OPENTELEMETRY_STL_VERSION=2020) else() - message("Building with nostd types...") + message(FATAL_ERROR "WITH_STL must be ON, OFF, CXX11, CXX14, CXX17 or CXX20") endif() if(WITH_GSL) diff --git a/api/include/opentelemetry/nostd/span.h b/api/include/opentelemetry/nostd/span.h index 3022a913d9..389e26b178 100644 --- a/api/include/opentelemetry/nostd/span.h +++ b/api/include/opentelemetry/nostd/span.h @@ -4,7 +4,7 @@ #pragma once // Try to use either `std::span` or `gsl::span` -#ifdef HAVE_CPP_STDLIB +#if OPENTELEMETRY_STL_VERSION >= 2020 # include # include # include @@ -26,7 +26,7 @@ # if OPENTELEMETRY_OPTION_USE_STD_SPAN # include "opentelemetry/std/span.h" # endif -#endif +#endif /* OPENTELEMETRY_STL_VERSION >= 2020 */ // Fallback to `nostd::span` if necessary #if !defined(HAVE_SPAN) diff --git a/api/include/opentelemetry/nostd/type_traits.h b/api/include/opentelemetry/nostd/type_traits.h index 3d212ea3e2..709e2afb89 100644 --- a/api/include/opentelemetry/nostd/type_traits.h +++ b/api/include/opentelemetry/nostd/type_traits.h @@ -3,7 +3,7 @@ #pragma once -#ifdef HAVE_CPP_STDLIB +#if OPENTELEMETRY_STL_VERSION >= 2011 # include "opentelemetry/std/type_traits.h" #else # include @@ -154,4 +154,4 @@ struct is_trivially_move_assignable # endif } // namespace nostd OPENTELEMETRY_END_NAMESPACE -#endif +#endif /* OPENTELEMETRY_STL_VERSION >= 2011 */ diff --git a/api/include/opentelemetry/nostd/variant.h b/api/include/opentelemetry/nostd/variant.h index b6b2326998..536786bd05 100644 --- a/api/include/opentelemetry/nostd/variant.h +++ b/api/include/opentelemetry/nostd/variant.h @@ -5,7 +5,7 @@ #include "opentelemetry/version.h" -#ifdef HAVE_CPP_STDLIB +#if OPENTELEMETRY_STL_VERSION >= 2017 # include "opentelemetry/std/variant.h" #else diff --git a/api/test/nostd/span_test.cc b/api/test/nostd/span_test.cc index 98e825142d..5c13ee590e 100644 --- a/api/test/nostd/span_test.cc +++ b/api/test/nostd/span_test.cc @@ -60,11 +60,6 @@ TEST(SpanTest, PointerCountConstruction) span s2{array.data(), array.size()}; EXPECT_EQ(s2.data(), array.data()); EXPECT_EQ(s2.size(), array.size()); - -#ifndef HAVE_CPP_STDLIB - /* This test is not supposed to fail with STL. Why is this invalid construct? */ - EXPECT_DEATH((span{array.data(), array.size()}), ".*"); -#endif } TEST(SpanTest, RangeConstruction) @@ -78,11 +73,6 @@ TEST(SpanTest, RangeConstruction) span s2{std::begin(array), std::end(array)}; EXPECT_EQ(s2.data(), array); EXPECT_EQ(s2.size(), 3); - -#ifndef HAVE_CPP_STDLIB - /* This test is not supposed to fail with STL. Why is this invalid construct? */ - EXPECT_DEATH((span{std::begin(array), std::end(array)}), ".*"); -#endif } TEST(SpanTest, ArrayConstruction) @@ -122,11 +112,6 @@ TEST(SpanTest, ContainerConstruction) EXPECT_EQ(s2.data(), v.data()); EXPECT_EQ(s2.size(), v.size()); -#ifndef HAVE_CPP_STDLIB - /* This test is not supposed to fail with STL. Why is this invalid construct? */ - EXPECT_DEATH((span{v.data(), 3}), ".*"); -#endif - EXPECT_FALSE((std::is_constructible, std::vector>::value)); EXPECT_FALSE((std::is_constructible, std::list>::value)); } diff --git a/api/test/nostd/string_view_test.cc b/api/test/nostd/string_view_test.cc index fc580debc1..3db28096a7 100644 --- a/api/test/nostd/string_view_test.cc +++ b/api/test/nostd/string_view_test.cc @@ -71,7 +71,7 @@ TEST(StringViewTest, SubstrPortion) TEST(StringViewTest, SubstrOutOfRange) { string_view s = "abc123"; -#if __EXCEPTIONS || defined(HAVE_CPP_STDLIB) +#if __EXCEPTIONS || (OPENTELEMETRY_STL_VERSION >= 2020) EXPECT_THROW(s.substr(10), std::out_of_range); #else EXPECT_DEATH({ s.substr(10); }, "");