Skip to content

Commit c464ba6

Browse files
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
1 parent fd94775 commit c464ba6

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

build-support/cmake-format.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ additional_commands:
6464
PUBLIC_HEADER_FILES: '*'
6565
DEPRECATED_HEADER_FILES: '*'
6666
PRIVATE_HEADER_FILES: '*'
67+
try_compile:
68+
pargs:
69+
nargs: 3
70+
kwargs:
71+
OUTPUT_VARIABLE: '1'
72+
CXX_STANDARD: '1'
73+
CXX_STANDARD_REQUIRED: '1'
74+
CXX_EXTENSIONS: '1'
75+
SOURCES: '*'
76+
CMAKE_FLAGS: '*'
77+
COMPILE_DEFINITIONS: '*'
78+
LINK_OPTIONS: '*'
79+
LINK_LIBRARIES: '*'
6780

6881
format:
6982
tab_size: 2

build-support/custom-modules/ns3-compiler-workarounds.cmake

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,52 @@
66

77
include(CheckCXXSourceCompiles)
88

9+
# Some versions of clang (17) have issues with stdlibc++, so we check if we can
10+
# fallback to libc++ instead
11+
if(CLANG)
12+
try_compile(
13+
CLANG_STDLIBCPP_WORKS
14+
SOURCES ${PROJECT_SOURCE_DIR}/build-support/test-clang17-stdlibcpp-map.cc
15+
COMPILE_DEFINITIONS -stdlib=libstdc++
16+
LINK_OPTIONS -stdlib=libstdc++
17+
CXX_STANDARD 23
18+
CXX_STANDARD_REQUIRED TRUE
19+
CXX_EXTENSIONS FALSE
20+
)
21+
22+
try_compile(
23+
CLANG_LIBCPP_WORKS
24+
SOURCES ${PROJECT_SOURCE_DIR}/build-support/test-clang17-stdlibcpp-map.cc
25+
COMPILE_DEFINITIONS -stdlib=libc++
26+
LINK_OPTIONS -stdlib=libc++
27+
CXX_STANDARD 23
28+
CXX_STANDARD_REQUIRED TRUE
29+
CXX_EXTENSIONS FALSE
30+
)
31+
if((NOT ${CLANG_STDLIBCPP_WORKS}) AND (NOT ${CLANG_LIBCPP_WORKS}))
32+
message(
33+
FATAL_ERROR
34+
"This clang version ${CMAKE_CXX_COMPILER_VERSION} is not compatible with using std::tuple as std::map keys"
35+
)
36+
elseif(${CLANG_STDLIBCPP_WORKS})
37+
message(
38+
STATUS
39+
"This clang version ${CMAKE_CXX_COMPILER_VERSION} is compatible with using std::tuple as std::map keys"
40+
)
41+
else()
42+
message(
43+
${HIGHLIGHTED_STATUS}
44+
"This clang version ${CMAKE_CXX_COMPILER_VERSION} requires libc++ to use std::tuple as std::map keys"
45+
)
46+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
47+
endif()
48+
endif()
49+
950
# Some compilers (e.g. GCC <= 15) do not link std::stacktrace by default. If the
1051
# sample program can be linked, it means it is indeed linked by default.
1152
# Otherwise, we link it manually.
1253
# https://en.cppreference.com/w/cpp/header/stacktrace
13-
set(CMAKE_REQUIRED_FLAGS -std=c++23)
54+
set(CMAKE_REQUIRED_FLAGS ${CMAKE_CXX_FLAGS} -std=c++23)
1455
check_cxx_source_compiles(
1556
"
1657
#if __has_include(<stacktrace>)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <tuple>
4+
5+
int
6+
main()
7+
{
8+
std::map<std::tuple<int, int>, int> testMap;
9+
testMap[{1, 1}] = 2;
10+
std::cout << testMap.at({1, 1}) << std::endl;
11+
return 0;
12+
}

utils/tests/gitlab-ci-clang.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- apt upgrade -y
1919
- DEBIAN_FRONTEND=noninteractive apt install -y
2020
clang-$CLANG cmake ninja-build ccache build-essential
21+
lld-$CLANG libc++-$CLANG-dev libc++abi-$CLANG-dev
2122
libboost-all-dev
2223
libeigen3-dev
2324
libgtk-3-0 libgtk-3-dev

0 commit comments

Comments
 (0)