Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD] Need fine-grained HAVE_CPP_STDLIB #2304

Merged
merged 24 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ endif()

option(WITH_NO_DEPRECATED_CODE "Do not include deprecated code" OFF)

option(WITH_STL "Whether to use Standard Library for C++ latest features" OFF)
set(WITH_STL
"OFF"
CACHE STRING "Which version of the Standard Library for C++ to use")

option(WITH_GSL
"Whether to use Guidelines Support Library for C++ latest features" OFF)

Expand All @@ -130,22 +133,7 @@ option(OPENTELEMETRY_INSTALL "Whether to install opentelemetry targets"

include("${PROJECT_SOURCE_DIR}/cmake/tools.cmake")

if(NOT DEFINED CMAKE_CXX_STANDARD)
if(WITH_STL)
# 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(WITH_STL)
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
# (absl::variant or std::variant) in variant unit test code is consistent with
Expand Down
30 changes: 26 additions & 4 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,33 @@ 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)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
elseif(WITH_STL STREQUAL "CXX11")
message(STATUS "Building WITH_STL=CXX1")
marcalff marked this conversation as resolved.
Show resolved Hide resolved
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
marcalff marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 2 additions & 1 deletion api/include/opentelemetry/nostd/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef HAVE_CPP_STDLIB

#if OPENTELEMETRY_STL_VERSION >= 2011
# include "opentelemetry/std/shared_ptr.h"
#else
# include <cstdlib>
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
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/string_view.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 >= 2017
# include "opentelemetry/std/string_view.h"
#else
# include <algorithm>
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/unique_ptr.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/unique_ptr.h"
#else
# include <cstddef>
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/nostd/utility.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 >= 2014
# include "opentelemetry/std/utility.h"
#else

Expand Down
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
2 changes: 1 addition & 1 deletion ext/test/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ endif()
set(URL_PARSER_FILENAME url_parser_test)
add_executable(${URL_PARSER_FILENAME} ${URL_PARSER_FILENAME}.cc)
target_link_libraries(${URL_PARSER_FILENAME} ${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
marcalff marked this conversation as resolved.
Show resolved Hide resolved
gtest_add_tests(
TARGET ${URL_PARSER_FILENAME}
TEST_PREFIX ext.http.urlparser.
Expand Down