From ffe74289b3f93219f12e5c3567640eb6f0403cd9 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Fri, 1 Mar 2024 08:01:28 -0500 Subject: CMakeDetermineCompilerId: extract C++ standard library impl This is needed for C++ modules because `import std` support may differ based on the standard library implementation. Extract the information as necessary. --- Modules/CMakeDetermineCompilerId.cmake | 24 ++++++++++++++++++++++++ Modules/CXX-DetectStdlib.h | 10 ++++++++++ 2 files changed, 34 insertions(+) create mode 100644 Modules/CXX-DetectStdlib.h diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake index e75018b..7fcfbdc 100644 --- a/Modules/CMakeDetermineCompilerId.cmake +++ b/Modules/CMakeDetermineCompilerId.cmake @@ -312,6 +312,29 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) endif () endif () + set(CMAKE_${lang}_STANDARD_LIBRARY "") + if ("x${lang}" STREQUAL "xCXX" AND + EXISTS "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${lang}-DetectStdlib.h" AND + "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xClang" AND + "x${CMAKE_${lang}_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU") + # See #20851 for a proper abstraction for this. + execute_process( + COMMAND "${CMAKE_${lang}_COMPILER}" + ${CMAKE_${lang}_COMPILER_ID_ARG1} + ${CMAKE_CXX_COMPILER_ID_FLAGS_LIST} + -E + -x c++-header + "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${lang}-DetectStdlib.h" + -o - # Write to stdout. + OUTPUT_VARIABLE _lang_stdlib_out + ERROR_VARIABLE _lang_stdlib_err + RESULT_VARIABLE _lang_stdlib_res + ERROR_STRIP_TRAILING_WHITESPACE) + if (_lang_stdlib_res EQUAL 0) + string(REGEX REPLACE ".*CMAKE-STDLIB-DETECT: (.+)\n.*" "\\1" "CMAKE_${lang}_STANDARD_LIBRARY" "${_lang_stdlib_out}") + endif () + endif () + # Display the final identification result. if(CMAKE_${lang}_COMPILER_ID) if(CMAKE_${lang}_COMPILER_VERSION) @@ -355,6 +378,7 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) set(CMAKE_${lang}_COMPILER_PRODUCED_OUTPUT "${COMPILER_${lang}_PRODUCED_OUTPUT}" PARENT_SCOPE) set(CMAKE_${lang}_COMPILER_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE) set(CMAKE_${lang}_COMPILER_CLANG_RESOURCE_DIR "${CMAKE_${lang}_COMPILER_CLANG_RESOURCE_DIR}" PARENT_SCOPE) + set(CMAKE_${lang}_STANDARD_LIBRARY "${CMAKE_${lang}_STANDARD_LIBRARY}" PARENT_SCOPE) endfunction() include(CMakeCompilerIdDetection) diff --git a/Modules/CXX-DetectStdlib.h b/Modules/CXX-DetectStdlib.h new file mode 100644 index 0000000..0214dee --- /dev/null +++ b/Modules/CXX-DetectStdlib.h @@ -0,0 +1,10 @@ +#include +// clang-format off +#if defined(_LIBCPP_VERSION) +CMAKE-STDLIB-DETECT: libc++ +#elif defined(__GLIBCXX__) +CMAKE-STDLIB-DETECT: libstdc++ +#else +CMAKE-STDLIB-DETECT: UNKNOWN +#endif + // clang-format on -- cgit v0.12