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 21 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
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,42 @@ jobs:
CXX: /usr/bin/g++-4.8
run: ./ci/do_ci.sh cmake.legacy.exporter.otprotocol.test

cmake_test_cxx14_gcc:
name: CMake C++14 test(GCC)
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: setup
env:
CMAKE_VERSION: 3.20.6
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_cmake.sh
- name: run tests (enable stl)
env:
CXX_STANDARD: '14'
run: ./ci/do_ci.sh cmake.c++14.stl.test

cmake_test_cxx17_gcc:
name: CMake C++17 test(GCC)
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: setup
env:
CMAKE_VERSION: 3.20.6
run: |
sudo -E ./ci/setup_ci_environment.sh
sudo -E ./ci/setup_cmake.sh
- name: run tests (enable stl)
env:
CXX_STANDARD: '17'
run: ./ci/do_ci.sh cmake.c++17.stl.test

cmake_test_cxx20_gcc:
name: CMake C++20 test(GCC)
runs-on: ubuntu-20.04
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ Increment the:
* [EXPORTER] Replace colons with underscores when converting to Prometheus label
[#2324](https://github.com/open-telemetry/opentelemetry-cpp/pull/2330)

Breaking changes:

* [BUILD] Need fine-grained HAVE_CPP_STDLIB
[#2304](https://github.com/open-telemetry/opentelemetry-cpp/pull/2304)
* In `CMAKE`, the boolean option `WITH_STL` as changed to an option
that accepts the values `OFF`, `ON`, `CXX11`, `CXX14`, `CXX17`,
`CXX20` and `CXX23`.
* Applications makefiles that did not set WITH_STL need to use
`WITH_STL=OFF` instead (this is the default).
* Applications makefiles that did set WITH_STL need to use
`WITH_STL=ON` instead, or may choose to pick a specific value.
* In the `API` header files, the preprocessor symbol `HAVE_CPP_STDLIB`
is no longer used.
* Applications that did set `HAVE_CPP_STDLIB` before, need to set
`OPENTELEMETRY_STL_VERSION=<version>` instead, to build with a
specific STL version (2011, 2014, 2017, 2020, 2023).
* The opentrelemetry-cpp makefile no longer sets
marcalff marked this conversation as resolved.
Show resolved Hide resolved
CMAKE_CXX_STANDARD by itself.
Instead, the CMAKE_CXX_STANDARD and/or compiler options -stdc++ used
by the caller are honored.
* Applications that set neither CMAKE_CXX_STANDARD nor -stdc++
options may need to provide a C++ standard in their makefiles.

## [1.11.0] 2023-08-21

* [BUILD] Fix more cases for symbol name for 32-bit win32 DLL build
Expand Down
22 changes: 5 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ message(STATUS "OPENTELEMETRY_VERSION=${OPENTELEMETRY_VERSION}")

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 @@ -169,22 +172,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
33 changes: 29 additions & 4 deletions api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,36 @@ 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")
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 "CXX23")
message(STATUS "Building WITH_STL=CXX23")
target_compile_definitions(opentelemetry_api
INTERFACE OPENTELEMETRY_STL_VERSION=2023)
elseif(WITH_STL STREQUAL "ON")
message(STATUS "Building WITH_STL=ON")
# "ON" corresponds to "CXX23" at this time.
target_compile_definitions(opentelemetry_api
INTERFACE OPENTELEMETRY_STL_VERSION=2023)
else()
message("Building with nostd types...")
message(
FATAL_ERROR "WITH_STL must be ON, OFF, CXX11, CXX14, CXX17, CXX20 or CXX23")
endif()

if(WITH_GSL)
Expand Down
14 changes: 10 additions & 4 deletions api/include/opentelemetry/nostd/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/shared_ptr.h"
#else

#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2011
# include "opentelemetry/std/shared_ptr.h"
# define OPENTELEMETRY_HAVE_STD_SHARED_PTR
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_SHARED_PTR)
# include <cstdlib>
# include <memory>
# include <utility>
Expand Down Expand Up @@ -201,4 +207,4 @@ inline bool operator!=(std::nullptr_t, const shared_ptr<T> &rhs) noexcept
}
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
#endif /* OPENTELEMETRY_HAVE_STD_SHARED_PTR */
36 changes: 19 additions & 17 deletions api/include/opentelemetry/nostd/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@
#pragma once

// Try to use either `std::span` or `gsl::span`
#ifdef HAVE_CPP_STDLIB
# include <array>
# include <cstddef>
# include <iterator>
# include <type_traits>
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2020
# include <array>
# include <cstddef>
# include <iterator>
# include <type_traits>

/**
* @brief Clang 14.0.0 with libc++ do not support implicitly construct a span
* for a range. We just use our fallback version.
*
*/
# if !defined(OPENTELEMETRY_OPTION_USE_STD_SPAN) && defined(_LIBCPP_VERSION)
# if _LIBCPP_VERSION <= 14000
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 0
# if !defined(OPENTELEMETRY_OPTION_USE_STD_SPAN) && defined(_LIBCPP_VERSION)
# if _LIBCPP_VERSION <= 14000
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 0
# endif
# endif
# endif
# ifndef OPENTELEMETRY_OPTION_USE_STD_SPAN
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 1
# endif
# if OPENTELEMETRY_OPTION_USE_STD_SPAN
# include "opentelemetry/std/span.h"
# endif
#endif
# ifndef OPENTELEMETRY_OPTION_USE_STD_SPAN
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 1
# endif
# if OPENTELEMETRY_OPTION_USE_STD_SPAN
# include "opentelemetry/std/span.h"
# endif
# endif /* OPENTELEMETRY_STL_VERSION >= 2020 */
#endif /* OPENTELEMETRY_STL_VERSION */

// Fallback to `nostd::span` if necessary
#if !defined(HAVE_SPAN)
#if !defined(OPENTELEMETRY_HAVE_SPAN)
# include <array>
# include <cassert>
# include <cstddef>
Expand Down
13 changes: 9 additions & 4 deletions api/include/opentelemetry/nostd/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

#pragma once

#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/string_view.h"
#else
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2017
# include "opentelemetry/std/string_view.h"
# define OPENTELEMETRY_HAVE_STD_STRING_VIEW
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_STRING_VIEW)
# include <algorithm>
# include <cstddef>
# include <cstring>
Expand Down Expand Up @@ -216,4 +221,4 @@ struct hash<OPENTELEMETRY_NAMESPACE::nostd::string_view>
}
};
} // namespace std
#endif
#endif /* OPENTELEMETRY_HAVE_STD_STRING_VIEW */
13 changes: 9 additions & 4 deletions api/include/opentelemetry/nostd/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

#pragma once

#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/type_traits.h"
#else
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2011
# include "opentelemetry/std/type_traits.h"
# define OPENTELEMETRY_HAVE_STD_TYPE_TRAITS
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_TYPE_TRAITS)
# include <array>
# include <type_traits>

Expand Down Expand Up @@ -154,4 +159,4 @@ struct is_trivially_move_assignable
# endif
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
#endif /* OPENTELEMETRY_HAVE_STD_TYPE_TRAITS */
13 changes: 9 additions & 4 deletions api/include/opentelemetry/nostd/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

#pragma once

#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/unique_ptr.h"
#else
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2011
# include "opentelemetry/std/unique_ptr.h"
# define OPENTELEMETRY_HAVE_STD_UNIQUE_PTR
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_UNIQUE_PTR)
# include <cstddef>
# include <memory>
# include <type_traits>
Expand Down Expand Up @@ -172,4 +177,4 @@ bool operator!=(std::nullptr_t, const unique_ptr<T> &rhs) noexcept
}
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
#endif /* OPENTELEMETRY_HAVE_STD_UNIQUE_PTR */
12 changes: 8 additions & 4 deletions api/include/opentelemetry/nostd/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

#pragma once

#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/utility.h"
#else
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2014
# include "opentelemetry/std/utility.h"
# define OPENTELEMETRY_HAVE_STD_UTILITY
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_UTILITY)
# include <cstddef>
# include <initializer_list>
# include <type_traits>
Expand Down Expand Up @@ -153,4 +157,4 @@ struct in_place_type_t
};
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
#endif /* OPENTELEMETRY_HAVE_STD_UTILITY */
13 changes: 9 additions & 4 deletions api/include/opentelemetry/nostd/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

#include "opentelemetry/version.h"

#ifdef HAVE_CPP_STDLIB
# include "opentelemetry/std/variant.h"
#else
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2017
# include "opentelemetry/std/variant.h"
# define OPENTELEMETRY_HAVE_STD_VARIANT
# endif
#endif

#if !defined(OPENTELEMETRY_HAVE_STD_VARIANT)

# ifndef HAVE_ABSEIL
// We use a LOCAL snapshot of Abseil that is known to compile with Visual Studio 2015.
Expand Down Expand Up @@ -73,4 +78,4 @@ using absl::visit;
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE

#endif
#endif /* OPENTELEMETRY_HAVE_STD_VARIANT */
16 changes: 8 additions & 8 deletions api/include/opentelemetry/std/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
# if __has_include(<version>) // Check for __cpp_{feature}
# include <version>
# if defined(__cpp_lib_span) && __cplusplus > 201703L
# define HAVE_SPAN
# define OPENTELEMETRY_HAVE_SPAN
# endif
# endif
# if !defined(HAVE_SPAN)
# if !defined(OPENTELEMETRY_HAVE_SPAN)
# // Check for Visual Studio span
# if defined(_MSVC_LANG) && _HAS_CXX20
# define HAVE_SPAN
# define OPENTELEMETRY_HAVE_SPAN
# endif
# // Check for other compiler span implementation
# if !defined(_MSVC_LANG) && __has_include(<span>) && __cplusplus > 201703L
// This works as long as compiler standard is set to C++20
# define HAVE_SPAN
# define OPENTELEMETRY_HAVE_SPAN
# endif
# endif
# if !__has_include(<gsl/gsl>)
# undef HAVE_GSL
# endif
#endif

#if !defined(HAVE_SPAN)
#if !defined(OPENTELEMETRY_HAVE_SPAN)
# if defined(HAVE_GSL)
# include <type_traits>
// Guidelines Support Library provides an implementation of std::span
Expand All @@ -44,12 +44,12 @@ template <class ElementType, std::size_t Extent = gsl::dynamic_extent>
using span = gsl::span<ElementType, Extent>;
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
# define HAVE_SPAN
# define OPENTELEMETRY_HAVE_SPAN
# else
// No `gsl::span`, no `std::span`, fallback to `nostd::span`
# endif

#else // HAVE_SPAN
#else // OPENTELEMETRY_HAVE_SPAN
// Using std::span (https://wg21.link/P0122R7) from Standard Library available in C++20 :
// - GCC libstdc++ 10+
// - Clang libc++ 7
Expand All @@ -66,4 +66,4 @@ template <class ElementType, std::size_t Extent = nostd::dynamic_extent>
using span = std::span<ElementType, Extent>;
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif // of HAVE_SPAN
#endif // if OPENTELEMETRY_HAVE_SPAN
Loading