From a53b2787ac1f6ec0280ad8abda77ad4ce9e14fea Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Mon, 6 Oct 2025 22:12:33 -0700 Subject: [PATCH 1/3] CMake: Check platform availability on Apple Platform availability is an Apple concept because Swift is shipped as part of the OS. It doesn't make sense to check or to warn on other platforms. Only checking and setting it for Apple platforms. --- .../Core/cmake/modules/PlatformInfo.cmake | 74 ++++++++++--------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/Runtimes/Core/cmake/modules/PlatformInfo.cmake b/Runtimes/Core/cmake/modules/PlatformInfo.cmake index 49ccc06aa4759..3938ecdebb5d2 100644 --- a/Runtimes/Core/cmake/modules/PlatformInfo.cmake +++ b/Runtimes/Core/cmake/modules/PlatformInfo.cmake @@ -43,47 +43,49 @@ if(NOT SwiftCore_ARCH_SUBDIR) message(CONFIGURE_LOG "Swift Arch: ${arch}") endif() -# Note: *moduleTriple* doesn't have an "x" on the end of "macos"; just to be -# safe, we support both cases here. -set(availability_platform_macos "macOS") -set(availaiblity_platform_macosx "macOS") -set(availability_platform_ios "iOS") -set(availability_platform_watchos "watchOS") -set(availability_platform_tvos "tvOS") -set(availability_platform_xros "visionOS") -set(availability_platform_bridgeos "bridgeOS") +if(APPLE) + # Note: *moduleTriple* doesn't have an "x" on the end of "macos"; just to be + # safe, we support both cases here. + set(availability_platform_macos "macOS") + set(availaiblity_platform_macosx "macOS") + set(availability_platform_ios "iOS") + set(availability_platform_watchos "watchOS") + set(availability_platform_tvos "tvOS") + set(availability_platform_xros "visionOS") + set(availability_platform_bridgeos "bridgeOS") -if(NOT SwiftCore_SWIFT_AVAILABILITY_PLATFORM) - if(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)-simulator$") - set(platform "${CMAKE_MATCH_1}") - elseif(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)-msvc$") - set(platform "${CMAKE_MATCH_1}") - elseif(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)$") - set(platform "${CMAKE_MATCH_1}") - else() - message(WARNING "Unable to extract platform name from triple ${SwiftCore_MODULE_TRIPLE}") - endif() + if(NOT SwiftCore_SWIFT_AVAILABILITY_PLATFORM) + if(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)-simulator$") + set(platform "${CMAKE_MATCH_1}") + elseif(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)-msvc$") + set(platform "${CMAKE_MATCH_1}") + elseif(SwiftCore_MODULE_TRIPLE MATCHES ".*-([^-]+)$") + set(platform "${CMAKE_MATCH_1}") + else() + message(WARNING "Unable to extract platform name from triple ${SwiftCore_MODULE_TRIPLE}") + endif() - if(availability_platform_${platform}) - set(SwiftCore_SWIFT_AVAILABILITY_PLATFORM "${availability_platform_${platform}}") - else() - set(SwiftCore_SWIFT_AVAILABILITY_PLATFORM "unknown") - message(WARNING "Unknown platform ${platform} for availability") + if(availability_platform_${platform}) + set(SwiftCore_SWIFT_AVAILABILITY_PLATFORM "${availability_platform_${platform}}") + else() + set(SwiftCore_SWIFT_AVAILABILITY_PLATFORM "unknown") + message(WARNING "Unknown platform ${platform} for availability") + endif() endif() -endif() -set(SwiftCore_VARIANT_AVAILABILITY_PLATFORM "none") -if(SwiftCore_VARIANT_MODULE_TRIPLE) - if(SwiftCore_VARIANT_MODULE_TRIPLE MATCHES ".*-([^-]+)$") - set(platform "${CMAKE_MATCH_1}") - else() - message(FATAL_ERROR "Unable to extract platform name from triple ${SwiftCore_VARIANT_MODULE_TRIPLE}") - endif() + set(SwiftCore_VARIANT_AVAILABILITY_PLATFORM "none") + if(SwiftCore_VARIANT_MODULE_TRIPLE) + if(SwiftCore_VARIANT_MODULE_TRIPLE MATCHES ".*-([^-]+)$") + set(platform "${CMAKE_MATCH_1}") + else() + message(FATAL_ERROR "Unable to extract platform name from triple ${SwiftCore_VARIANT_MODULE_TRIPLE}") + endif() - if(availability_platform_${platform}) - set(SwiftCore_VARIANT_AVAILABILITY_PLATFORM "${availability_platform_${platform}}") - else() - message(WARNING "Unknown platform ${platform} for variant availability") + if(availability_platform_${platform}) + set(SwiftCore_VARIANT_AVAILABILITY_PLATFORM "${availability_platform_${platform}}") + else() + message(WARNING "Unknown platform ${platform} for variant availability") + endif() endif() endif() From 28a63171c3ef815382cc05447af86c8f35a62b4a Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 7 Oct 2025 10:52:49 -0700 Subject: [PATCH 2/3] SDK Overlays: Linux: disable explicit module builds Explicit module builds crash the compiler when building the glibc overlay library. The crash occurs in the Swift driver dependency scanner. It appears to be related to vfs overlays, modulemaps, injected header files, and explicit module builds. This is odd though because the Android builds also have this combination, but work. --- Runtimes/Overlay/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Runtimes/Overlay/CMakeLists.txt b/Runtimes/Overlay/CMakeLists.txt index 5f60cff85ed9e..b1ad05f0df693 100644 --- a/Runtimes/Overlay/CMakeLists.txt +++ b/Runtimes/Overlay/CMakeLists.txt @@ -64,7 +64,6 @@ add_compile_definitions( $<$:SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT>) add_compile_options( - $<$:-explicit-module-build> $<$:-nostdlibimport> $<$:-strict-memory-safety> "$<$:SHELL:-Xfrontend -enable-lexical-lifetimes=false>" @@ -91,6 +90,12 @@ add_link_options($<$:LINKER:-z,defs>) include(ExperimentalFeatures) +if(NOT LINUX) + # explicit module builds crash the Swift compiler when building the Glibc + # platform overlay https://github.com/swiftlang/swift/issues/84740 + add_compile_options($<$:-explicit-module-build>) +endif() + add_subdirectory(clang) if(ANDROID) add_subdirectory(Android) From 44830d788cd8b609a40c864be3642252492bd537 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 7 Oct 2025 10:59:13 -0700 Subject: [PATCH 3/3] Overlays: Add Glibc Linux SDK overlay build Adding a new glibc overlay for Linux builds. We will eventually need one for Musl, and if someone really wants to, LLVM and cosmopolitan libcs are also out there. --- Runtimes/Overlay/CMakeLists.txt | 6 ++-- Runtimes/Overlay/Linux/glibc/CMakeLists.txt | 35 ++++++++++++++++++ .../Overlay/Linux/glibc/clang/CMakeLists.txt | 36 +++++++++++++++++++ .../cmake/modules/DefaultSettings.cmake | 7 +++- Runtimes/Resync.cmake | 16 +++++++++ 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 Runtimes/Overlay/Linux/glibc/CMakeLists.txt create mode 100644 Runtimes/Overlay/Linux/glibc/clang/CMakeLists.txt diff --git a/Runtimes/Overlay/CMakeLists.txt b/Runtimes/Overlay/CMakeLists.txt index b1ad05f0df693..096bc3ca2d656 100644 --- a/Runtimes/Overlay/CMakeLists.txt +++ b/Runtimes/Overlay/CMakeLists.txt @@ -99,9 +99,11 @@ endif() add_subdirectory(clang) if(ANDROID) add_subdirectory(Android) -endif() -if(WIN32) +elseif(WIN32) add_subdirectory(Windows) +elseif(LINUX) + defaulted_set(SwiftOverlay_LINUX_LIBC STRING "Select Linux libc flavor. Options are: glibc") + add_subdirectory("Linux/${SwiftOverlay_LINUX_LIBC}") endif() if(SwiftOverlay_ENABLE_CXX_INTEROP) diff --git a/Runtimes/Overlay/Linux/glibc/CMakeLists.txt b/Runtimes/Overlay/Linux/glibc/CMakeLists.txt new file mode 100644 index 0000000000000..19cb1af053161 --- /dev/null +++ b/Runtimes/Overlay/Linux/glibc/CMakeLists.txt @@ -0,0 +1,35 @@ +add_subdirectory(clang) + +gyb_expand(Glibc.swift.gyb Glibc.swift) +gyb_expand(tgmath.swift.gyb tgmath.swift) + +add_library(swiftGlibc + Glibc.swift + POSIXError.swift + Platform.swift + TiocConstants.swift + tgmath.swift) + +target_link_libraries(swiftGlibc + PRIVATE + swiftCore + SwiftGlibcClangOverlay) + +set_target_properties(swiftGlibc PROPERTIES + Swift_MODULE_NAME Glibc) + +target_compile_options(swiftGlibc PRIVATE + "SHELL:-Xcc -fno-omit-frame-pointer" + "SHELL:-Xfrontend -empty-abi-descriptor" + "SHELL:-Xfrontend -prespecialize-generic-metadata" + "SHELL:-Xfrontend -require-explicit-availability=ignore" + "SHELL:-library-level api") + +install(TARGETS swiftGlibc + EXPORT SwiftOverlayTargets + ARCHIVE DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${SwiftOverlay_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") +emit_swift_interface(swiftGlibc) +install_swift_interface(swiftGlibc) +embed_manifest(swiftGlibc) diff --git a/Runtimes/Overlay/Linux/glibc/clang/CMakeLists.txt b/Runtimes/Overlay/Linux/glibc/clang/CMakeLists.txt new file mode 100644 index 0000000000000..597cc2ee6feff --- /dev/null +++ b/Runtimes/Overlay/Linux/glibc/clang/CMakeLists.txt @@ -0,0 +1,36 @@ +gyb_expand(glibc.modulemap.gyb glibc.modulemap + FLAGS -DCMAKE_SDK="LINUX") +gyb_expand(SwiftGlibc.h.gyb SwiftGlibc.h) + +file(CONFIGURE + OUTPUT glibc-overlay.yml + CONTENT [==[ +--- +version: 0 +case-sensitive: false +use-external-names: false +roots: + - name: "/usr/include" + type: directory + contents: + - name: module.modulemap + type: file + external-contents: "@CMAKE_CURRENT_BINARY_DIR@/glibc.modulemap" + - name: SwiftGlibc.h + type: file + external-contents: "@CMAKE_CURRENT_BINARY_DIR@/SwiftGlibc.h" +]==] + ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF) + +add_library(SwiftGlibcClangOverlay INTERFACE + SwiftGlibc.h + glibc.modulemap) +target_compile_options(SwiftGlibcClangOverlay INTERFACE + "$:SHELL:-vfsoverlay ${CMAKE_CURRENT_BINARY_DIR}/glibc-overlay.yml>>") + +install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/glibc.modulemap" + "${CMAKE_CURRENT_BINARY_DIR}/SwiftGlibc.h" + DESTINATION + "${CMAKE_INSTALL_LIBDIR}/swift/${SwiftOverlay_PLATFORM_SUBDIR}/${SwiftOverlay_ARCH_SUBDIR}") diff --git a/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake b/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake index 259f24aa20a18..b634bf7829154 100644 --- a/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake +++ b/Runtimes/Overlay/cmake/modules/DefaultSettings.cmake @@ -30,9 +30,14 @@ if(APPLE) elseif(CMAKE_SYSTEM_NAME STREQUAL "WASM") set(SwiftOverlay_ENABLE_REFLECTION_default OFF) set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF) -elseif(LINUX OR ANDROID OR BSD) +elseif(ANDROID OR BSD) set(SwiftOverlay_ENABLE_REFLECTION_default OFF) set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF) +elseif(LINUX) + # TODO: Enable once we have them building + set(SwiftOverlay_ENABLE_REFLECTION_default OFF) + set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF) + set(SwiftOverlay_LINUX_LIBC_default "glibc") elseif(WIN32) set(SwiftOverlay_ENABLE_REFLECTION_default ON) set(SwiftOverlay_ENABLE_CXX_INTEROP_default OFF) diff --git a/Runtimes/Resync.cmake b/Runtimes/Resync.cmake index 9b93b807e85a2..39a4685b70cc6 100644 --- a/Runtimes/Resync.cmake +++ b/Runtimes/Resync.cmake @@ -150,6 +150,22 @@ copy_files(public/Platform Overlay/Android/Math FILES Math.swift) +# Linux Glibc Overlay +message(STATUS "Glibc modulemaps[${StdlibSources}/Platform] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/Linux/glibc/clang") +copy_files(public/Platform Overlay/Linux/glibc/clang + FILES + glibc.modulemap.gyb + SwiftGlibc.h.gyb) + +message(STATUS "Glibc [${StdlibSources}/Platform] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/Linux/glibc") +copy_files(public/Platform Overlay/Linux/glibc + FILES + Glibc.swift.gyb + POSIXError.swift + Platform.swift + TiocConstants.swift + tgmath.swift.gyb) + # Windows Overlay message(STATUS "WinSDK[${StdlibSources}/public/Windows] -> ${CMAKE_CURRENT_LIST_DIR}/Overlay/Windows/WinSDK") copy_files(public/Windows Overlay/Windows/WinSDK FILES WinSDK.swift)