build: Fallback to libc++ if clang does not support tuples as map keys

Workaround to avoid issue in clang 17
https://discourse.llvm.org/t/pack-expansion-bug/64910
This commit is contained in:
Gabriel Ferreira
2025-06-03 19:52:36 +02:00
parent fd947755e1
commit c464ba6874
4 changed files with 68 additions and 1 deletions

View File

@@ -64,6 +64,19 @@ additional_commands:
PUBLIC_HEADER_FILES: '*'
DEPRECATED_HEADER_FILES: '*'
PRIVATE_HEADER_FILES: '*'
try_compile:
pargs:
nargs: 3
kwargs:
OUTPUT_VARIABLE: '1'
CXX_STANDARD: '1'
CXX_STANDARD_REQUIRED: '1'
CXX_EXTENSIONS: '1'
SOURCES: '*'
CMAKE_FLAGS: '*'
COMPILE_DEFINITIONS: '*'
LINK_OPTIONS: '*'
LINK_LIBRARIES: '*'
format:
tab_size: 2

View File

@@ -6,11 +6,52 @@
include(CheckCXXSourceCompiles)
# Some versions of clang (17) have issues with stdlibc++, so we check if we can
# fallback to libc++ instead
if(CLANG)
try_compile(
CLANG_STDLIBCPP_WORKS
SOURCES ${PROJECT_SOURCE_DIR}/build-support/test-clang17-stdlibcpp-map.cc
COMPILE_DEFINITIONS -stdlib=libstdc++
LINK_OPTIONS -stdlib=libstdc++
CXX_STANDARD 23
CXX_STANDARD_REQUIRED TRUE
CXX_EXTENSIONS FALSE
)
try_compile(
CLANG_LIBCPP_WORKS
SOURCES ${PROJECT_SOURCE_DIR}/build-support/test-clang17-stdlibcpp-map.cc
COMPILE_DEFINITIONS -stdlib=libc++
LINK_OPTIONS -stdlib=libc++
CXX_STANDARD 23
CXX_STANDARD_REQUIRED TRUE
CXX_EXTENSIONS FALSE
)
if((NOT ${CLANG_STDLIBCPP_WORKS}) AND (NOT ${CLANG_LIBCPP_WORKS}))
message(
FATAL_ERROR
"This clang version ${CMAKE_CXX_COMPILER_VERSION} is not compatible with using std::tuple as std::map keys"
)
elseif(${CLANG_STDLIBCPP_WORKS})
message(
STATUS
"This clang version ${CMAKE_CXX_COMPILER_VERSION} is compatible with using std::tuple as std::map keys"
)
else()
message(
${HIGHLIGHTED_STATUS}
"This clang version ${CMAKE_CXX_COMPILER_VERSION} requires libc++ to use std::tuple as std::map keys"
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
endif()
# Some compilers (e.g. GCC <= 15) do not link std::stacktrace by default. If the
# sample program can be linked, it means it is indeed linked by default.
# Otherwise, we link it manually.
# https://en.cppreference.com/w/cpp/header/stacktrace
set(CMAKE_REQUIRED_FLAGS -std=c++23)
set(CMAKE_REQUIRED_FLAGS ${CMAKE_CXX_FLAGS} -std=c++23)
check_cxx_source_compiles(
"
#if __has_include(<stacktrace>)

View File

@@ -0,0 +1,12 @@
#include <iostream>
#include <map>
#include <tuple>
int
main()
{
std::map<std::tuple<int, int>, int> testMap;
testMap[{1, 1}] = 2;
std::cout << testMap.at({1, 1}) << std::endl;
return 0;
}

View File

@@ -18,6 +18,7 @@
- apt upgrade -y
- DEBIAN_FRONTEND=noninteractive apt install -y
clang-$CLANG cmake ninja-build ccache build-essential
lld-$CLANG libc++-$CLANG-dev libc++abi-$CLANG-dev
libboost-all-dev
libeigen3-dev
libgtk-3-0 libgtk-3-dev