Skip to content

Commit

Permalink
Fixes #1071
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Sep 11, 2023
1 parent bfad9dd commit 22537db
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 76 deletions.
51 changes: 0 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
24 changes: 20 additions & 4 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/nostd/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>
# include <cstddef>
# include <iterator>
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions api/include/opentelemetry/nostd/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#ifdef HAVE_CPP_STDLIB
#if OPENTELEMETRY_STL_VERSION >= 2011
# include "opentelemetry/std/type_traits.h"
#else
# include <array>
Expand Down Expand Up @@ -154,4 +154,4 @@ struct is_trivially_move_assignable
# endif
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
#endif /* OPENTELEMETRY_STL_VERSION >= 2011 */
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "opentelemetry/version.h"

#ifdef HAVE_CPP_STDLIB
#if OPENTELEMETRY_STL_VERSION >= 2017
# include "opentelemetry/std/variant.h"
#else

Expand Down
15 changes: 0 additions & 15 deletions api/test/nostd/span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ TEST(SpanTest, PointerCountConstruction)
span<int, 3> 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<int, 2>{array.data(), array.size()}), ".*");
#endif
}

TEST(SpanTest, RangeConstruction)
Expand All @@ -78,11 +73,6 @@ TEST(SpanTest, RangeConstruction)
span<int, 3> 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<int, 2>{std::begin(array), std::end(array)}), ".*");
#endif
}

TEST(SpanTest, ArrayConstruction)
Expand Down Expand Up @@ -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<int, 2>{v.data(), 3}), ".*");
#endif

EXPECT_FALSE((std::is_constructible<span<int>, std::vector<double>>::value));
EXPECT_FALSE((std::is_constructible<span<int>, std::list<int>>::value));
}
Expand Down
2 changes: 1 addition & 1 deletion api/test/nostd/string_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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); }, "");
Expand Down

0 comments on commit 22537db

Please sign in to comment.