diff --git a/build-support/cmake-format.yaml b/build-support/cmake-format.yaml index 76f9e004b..c9d7813df 100644 --- a/build-support/cmake-format.yaml +++ b/build-support/cmake-format.yaml @@ -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 diff --git a/build-support/custom-modules/ns3-compiler-workarounds.cmake b/build-support/custom-modules/ns3-compiler-workarounds.cmake index f91fc389a..8bbe15035 100644 --- a/build-support/custom-modules/ns3-compiler-workarounds.cmake +++ b/build-support/custom-modules/ns3-compiler-workarounds.cmake @@ -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() diff --git a/build-support/test-clang17-stdlibcpp-map.cc b/build-support/test-clang17-stdlibcpp-map.cc new file mode 100644 index 000000000..e8d1f1ccb --- /dev/null +++ b/build-support/test-clang17-stdlibcpp-map.cc @@ -0,0 +1,12 @@ +#include +#include +#include + +int +main() +{ + std::map, int> testMap; + testMap[{1, 1}] = 2; + std::cout << testMap.at({1, 1}) << std::endl; + return 0; +} diff --git a/utils/tests/gitlab-ci-clang.yml b/utils/tests/gitlab-ci-clang.yml index 45eda1065..c86493123 100644 --- a/utils/tests/gitlab-ci-clang.yml +++ b/utils/tests/gitlab-ci-clang.yml @@ -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