Skip to content

Commit 036e168

Browse files
authored
[Cxx] Make C++ flags configurable for Linux SDK (#59764)
Allow Linux distributions to provide their own C++ flags to compile the C++ overlay correctly. The default is kept the same for Ubuntu and CentOS, but other distributions can provide other flags to use their own distro GCC stdlibc++ or even libc++ if they choose. This should not change the current compilation, but opens the door for other maintainers to provide a different value that work on their systems.
1 parent 021c6a4 commit 036e168

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,21 @@ macro(configure_sdk_unix name architectures)
282282
endif()
283283
set(SWIFT_SDK_${prefix}_USE_ISYSROOT FALSE)
284284

285+
# GCC on Linux is usually located under `/usr`.
286+
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which
287+
# does not include libstdc++. Swift build scripts pass `--sysroot=/` to
288+
# Clang. By default, Clang tries to find GCC installation under sysroot, and
289+
# if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it
290+
# attempts to use the GCC without the C++ stdlib, which causes a build
291+
# failure. To fix that, we tell Clang explicitly to use GCC from `/usr`.
292+
# FIXME: This is a compromise. The value might depend on the architecture
293+
# but add_swift_target_library does not allow passing different values
294+
# depending on the architecture, so having a single value is the only
295+
# possibility right now.
296+
set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS
297+
-Xcc --gcc-toolchain=/usr
298+
CACHE STRING "Extra flags for compiling the C++ overlay")
299+
285300
foreach(arch ${architectures})
286301
if("${prefix}" STREQUAL "ANDROID")
287302
swift_android_sysroot(android_sysroot)

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,7 @@ add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
143143
-Xfrontend -module-interface-preserve-types-as-written
144144

145145
SWIFT_COMPILE_FLAGS_LINUX
146-
# GCC on Linux is usually located under `/usr`.
147-
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++.
148-
# Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot,
149-
# and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the
150-
# C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`.
151-
-Xcc --gcc-toolchain=/usr
146+
${SWIFT_SDK_LINUX_CXX_OVERLAY_SWIFT_COMPILE_FLAGS}
152147

153148
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
154149
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX

0 commit comments

Comments
 (0)