Skip to content

Commit 8e8b1b4

Browse files
committed
Detect used C++ standard library (#4793)
Signed-off-by: Niels Lohmann <[email protected]>
1 parent 39c59b8 commit 8e8b1b4

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

cmake/detect_libcpp_version.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Detect used C++ Standard Library
3+
*
4+
* This file is compiled and run via try_run in download_test_data.cmake.
5+
*/
6+
7+
#include <cstdio>
8+
9+
// see https://en.cppreference.com/w/cpp/header/ciso646
10+
#if __cplusplus >= 202002L
11+
#include <version>
12+
#else
13+
#include <ciso646>
14+
#endif
15+
16+
int main()
17+
{
18+
#if defined(_LIBCPP_VERSION)
19+
std::printf("LLVM C++ Standard Library (libc++), _LIBCPP_VERSION=%d", _LIBCPP_VERSION);
20+
#elif defined(__GLIBCXX__)
21+
std::printf("GNU C++ Standard Library (libstdc++), __GLIBCXX__=%d", __GLIBCXX__);
22+
#elif defined(_MSVC_STL_VERSION)
23+
std::printf("Microsoft C++ Standard Library (MSVC STL), _MSVC_STL_VERSION=%d", _MSVC_STL_VERSION);
24+
#elif defined(_LIBCUDACXX_VERSION)
25+
std::printf("NVIDIA C++ Standard Library (libcudacxx), _LIBCUDACXX_VERSION=%d", _LIBCUDACXX_VERSION);
26+
#elif defined(EASTL_VERSION)
27+
std::printf("Electronic Arts Standard Template Library (EASTL), EASTL_VERSION=%d", EASTL_VERSION);
28+
#else
29+
std::printf("unknown");
30+
#endif
31+
}

cmake/download_test_data.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,18 @@ else()
5454
endif()
5555
string(REGEX REPLACE "[ ]*\n" "; " CXX_VERSION_RESULT "${CXX_VERSION_RESULT}")
5656
message(STATUS "Compiler: ${CXX_VERSION_RESULT}")
57+
58+
# determine used C++ standard library (for debug and support purposes)
59+
if(NOT DEFINED LIBCPP_VERSION_OUTPUT_CACHED)
60+
try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR
61+
"${CMAKE_BINARY_DIR}" SOURCES "${CMAKE_SOURCE_DIR}/cmake/detect_libcpp_version.cpp"
62+
RUN_OUTPUT_VARIABLE LIBCPP_VERSION_OUTPUT COMPILE_OUTPUT_VARIABLE LIBCPP_VERSION_COMPILE_OUTPUT
63+
)
64+
if(NOT LIBCPP_VERSION_OUTPUT)
65+
set(LIBCPP_VERSION_OUTPUT "Unknown")
66+
message(AUTHOR_WARNING "Failed to compile cmake/detect_libcpp_version to detect the used C++ standard library. This does not affect the library or the test cases. Please still create an issue at https://github.com/nlohmann/json to investigate this.\n${LIBCPP_VERSION_COMPILE_OUTPUT}")
67+
endif()
68+
set(LIBCPP_VERSION_OUTPUT_CACHED "${LIBCPP_VERSION_OUTPUT}" CACHE STRING "Detected C++ standard library version")
69+
endif()
70+
71+
message(STATUS "C++ standard library: ${LIBCPP_VERSION_OUTPUT_CACHED}")

tests/src/unit-msgpack.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,10 +1914,12 @@ TEST_CASE("MessagePack with std::byte")
19141914
SECTION("empty vector")
19151915
{
19161916
const std::vector<std::byte> empty_data;
1917-
CHECK_THROWS_WITH_AS([&]() {
1917+
CHECK_THROWS_WITH_AS([&]()
1918+
{
19181919
[[maybe_unused]] auto result = json::from_msgpack(empty_data);
19191920
return true;
1920-
}(),
1921+
}
1922+
(),
19211923
"[json.exception.parse_error.110] parse error at byte 1: syntax error while parsing MessagePack value: unexpected end of input",
19221924
json::parse_error&);
19231925
}

0 commit comments

Comments
 (0)