From 0f8fc4541f3c95a3ca05aae935ea873a10c53c81 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 14:33:25 +0000 Subject: [PATCH 1/9] Back-port foundation fixes to 6.0 --- ...t-for-WebAssembly-target-architectur.patch | 29 + .../swift-corelibs-foundation/5061.patch | 357 +++++ .../swift-corelibs-foundation/5063.patch | 30 + .../swift-corelibs-foundation/5064.patch | 63 + .../release-6.0/swift-foundation/781.patch | 105 ++ .../release-6.0/swift-foundation/825.patch | 1150 +++++++++++++++++ .../release-6.0/swift-foundation/835.patch | 34 + .../release-6.0/swift-foundation/836.patch | 271 ++++ .../release-6.0/swift-foundation/843.patch | 104 ++ schemes/release-6.0/swift/75821.patch | 130 ++ 10 files changed, 2273 insertions(+) create mode 100644 schemes/release-6.0/swift-collections/0001-CMake-Add-support-for-WebAssembly-target-architectur.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/5061.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/5063.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/5064.patch create mode 100644 schemes/release-6.0/swift-foundation/781.patch create mode 100644 schemes/release-6.0/swift-foundation/825.patch create mode 100644 schemes/release-6.0/swift-foundation/835.patch create mode 100644 schemes/release-6.0/swift-foundation/836.patch create mode 100644 schemes/release-6.0/swift-foundation/843.patch create mode 100644 schemes/release-6.0/swift/75821.patch diff --git a/schemes/release-6.0/swift-collections/0001-CMake-Add-support-for-WebAssembly-target-architectur.patch b/schemes/release-6.0/swift-collections/0001-CMake-Add-support-for-WebAssembly-target-architectur.patch new file mode 100644 index 00000000..13434d01 --- /dev/null +++ b/schemes/release-6.0/swift-collections/0001-CMake-Add-support-for-WebAssembly-target-architectur.patch @@ -0,0 +1,29 @@ +From effb33569a926c12dc8e7bae979ac2075c219189 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 26 Jul 2024 03:59:24 +0000 +Subject: [PATCH] [CMake] Add support for WebAssembly target architectures + +To repair the swift-corelibs-foundation build on WebAssembly, we need to +add support for Wasm targets to swift-collections' CMake build system. +--- + cmake/modules/SwiftSupport.cmake | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake +index 0ce99fb8..21b9d693 100644 +--- a/cmake/modules/SwiftSupport.cmake ++++ b/cmake/modules/SwiftSupport.cmake +@@ -45,6 +45,10 @@ function(get_swift_host_arch result_var_name) + set("${result_var_name}" "i686" PARENT_SCOPE) + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686") + set("${result_var_name}" "i686" PARENT_SCOPE) ++ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32") ++ set("${result_var_name}" "wasm32" PARENT_SCOPE) ++ elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm64") ++ set("${result_var_name}" "wasm64" PARENT_SCOPE) + else() + message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}") + endif() +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/5061.patch b/schemes/release-6.0/swift-corelibs-foundation/5061.patch new file mode 100644 index 00000000..5304ae63 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/5061.patch @@ -0,0 +1,357 @@ +From 6a78203fc24702ed81a2b2bb2a9949168e694161 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Thu, 8 Aug 2024 23:33:15 +0000 +Subject: [PATCH 1/3] [XMLParser] Use `TaskLocal` for storing the current + parser + +Instead of thread-local storage, use `TaskLocal` to store the current +parser. This solves three issues: + +1. If someone calls `XMLParser.parse()` with a new parser instance in + a delegate method call, it overwrote the current parser and wrote + it back after the call as `nil`, not the previous current parser. + This reentrancy issue can be a problem especially when someone uses + external entity resolving since the feature depends on the current + parser tracking. Using `TaskLocal` solves this issue since it tracks + values as a stack and restores the previous value at the end of the + `withValue` call. +2. Since jobs of different tasks can be scheduled on the same thread, + different tasks can refer to the same thread-local storage. This + wouldn't be a problem for now since the `parse()` method doesn't + have any suspention points and different tasks can't run on the same + thread during the parsing. However, it's better to use `TaskLocal` + to leverage the concurrency model of Swift. +3. The global variable `_currentParser` existed in the WASI platform + path but it's unsafe in the Swift concurrency model. It wouldn't be a + problem on WASI since it's always single-threaded, we should avoid + platform-specific assumption as much as possible. +--- + Sources/FoundationXML/XMLParser.swift | 85 ++++++++++----------------- + Tests/Foundation/TestXMLParser.swift | 43 +++++++++++++- + 2 files changed, 74 insertions(+), 54 deletions(-) + +diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift +index d89d0ee1f4..e3d718a86f 100644 +--- a/Sources/FoundationXML/XMLParser.swift ++++ b/Sources/FoundationXML/XMLParser.swift +@@ -398,9 +398,7 @@ extension XMLParser : @unchecked Sendable { } + + open class XMLParser : NSObject { + private var _handler: _CFXMLInterfaceSAXHandler +-#if !os(WASI) + internal var _stream: InputStream? +-#endif + internal var _data: Data? + + internal var _chunkSize = Int(4096 * 32) // a suitably large number for a decent chunk size +@@ -469,33 +467,35 @@ open class XMLParser : NSObject { + open var externalEntityResolvingPolicy: ExternalEntityResolvingPolicy = .never + + open var allowedExternalEntityURLs: Set? +- +-#if os(WASI) +- private static var _currentParser: XMLParser? +-#endif + +- internal static func currentParser() -> XMLParser? { +-#if os(WASI) +- return _currentParser +-#else +- if let current = Thread.current.threadDictionary["__CurrentNSXMLParser"] { +- return current as? XMLParser +- } else { +- return nil ++ /// The current parser is stored in a task local variable to allow for ++ /// concurrent parsing in different tasks with different parsers. ++ /// ++ /// Rationale for `@unchecked Sendable`: ++ /// While the ``XMLParser`` class itself is not `Sendable`, `TaskLocal` ++ /// requires the value type to be `Sendable`. The sendability requirement ++ /// of `TaskLocal` is only for the "default" value and values set with ++ /// `withValue` will not be shared between tasks. ++ /// So as long as 1. the default value is safe to be shared between tasks ++ /// and 2. the `Sendable` conformance of `_CurrentParser` is not used ++ /// outside of `TaskLocal`, it is safe to mark it as `@unchecked Sendable`. ++ private struct _CurrentParser: @unchecked Sendable { ++ let parser: XMLParser? ++ ++ static var `default`: _CurrentParser { ++ return _CurrentParser(parser: nil) + } +-#endif ++ } ++ ++ @TaskLocal ++ private static var _currentParser: _CurrentParser = .default ++ ++ internal static func currentParser() -> XMLParser? { ++ return _currentParser.parser + } + +- internal static func setCurrentParser(_ parser: XMLParser?) { +-#if os(WASI) +- _currentParser = parser +-#else +- if let p = parser { +- Thread.current.threadDictionary["__CurrentNSXMLParser"] = p +- } else { +- Thread.current.threadDictionary.removeObject(forKey: "__CurrentNSXMLParser") +- } +-#endif ++ internal static func withCurrentParser(_ parser: XMLParser, _ body: () -> R) -> R { ++ return self.$_currentParser.withValue(_CurrentParser(parser: parser), operation: body) + } + + internal func _handleParseResult(_ parseResult: Int32) -> Bool { +@@ -569,7 +569,6 @@ open class XMLParser : NSObject { + return result + } + +-#if !os(WASI) + internal func parseFrom(_ stream : InputStream) -> Bool { + var result = true + +@@ -598,37 +597,17 @@ open class XMLParser : NSObject { + + return result + } +-#else +- internal func parse(from data: Data) -> Bool { +- var result = true +- var chunkStart = 0 +- var chunkEnd = min(_chunkSize, data.count) +- while result && chunkStart < chunkEnd { +- let chunk = data[chunkStart.. Bool { +-#if os(WASI) +- return _data.map { parse(from: $0) } ?? false +-#else +- XMLParser.setCurrentParser(self) +- defer { XMLParser.setCurrentParser(nil) } +- +- if _stream != nil { +- return parseFrom(_stream!) +- } else if _data != nil { +- return parseData(_data!, lastChunkOfData: true) ++ return Self.withCurrentParser(self) { ++ if _stream != nil { ++ return parseFrom(_stream!) ++ } else if _data != nil { ++ return parseData(_data!, lastChunkOfData: true) ++ } ++ return false + } +- +- return false +-#endif + } + + // called by the delegate to stop the parse. The delegate will get an error message sent to it. +diff --git a/Tests/Foundation/TestXMLParser.swift b/Tests/Foundation/TestXMLParser.swift +index c98741eb38..df3685a82e 100644 +--- a/Tests/Foundation/TestXMLParser.swift ++++ b/Tests/Foundation/TestXMLParser.swift +@@ -198,5 +198,46 @@ class TestXMLParser : XCTestCase { + ElementNameChecker("noPrefix").check() + ElementNameChecker("myPrefix:myLocalName").check() + } +- ++ ++ func testExternalEntity() throws { ++ class Delegate: XMLParserDelegateEventStream { ++ override func parserDidStartDocument(_ parser: XMLParser) { ++ // Start a child parser, updating `currentParser` to the child parser ++ // to ensure that `currentParser` won't be reset to `nil`, which would ++ // ignore any external entity related configuration. ++ let childParser = XMLParser(data: "".data(using: .utf8)!) ++ XCTAssertTrue(childParser.parse()) ++ super.parserDidStartDocument(parser) ++ } ++ } ++ try withTemporaryDirectory { dir, _ in ++ let greetingPath = dir.appendingPathComponent("greeting.xml") ++ try Data("".utf8).write(to: greetingPath) ++ let xml = """ ++ ++ ++ ]> ++ &greeting; ++ """ ++ ++ let parser = XMLParser(data: xml.data(using: .utf8)!) ++ // Explicitly disable external entity resolving ++ parser.externalEntityResolvingPolicy = .never ++ let delegate = Delegate() ++ parser.delegate = delegate ++ // The parse result changes depending on the libxml2 version ++ // because of the following libxml2 commit (shipped in libxml2 2.9.10): ++ // https://gitlab.gnome.org/GNOME/libxml2/-/commit/eddfbc38fa7e84ccd480eab3738e40d1b2c83979 ++ // So we don't check the parse result here. ++ _ = parser.parse() ++ XCTAssertEqual(delegate.events, [ ++ .startDocument, ++ .didStartElement("doc", nil, nil, [:]), ++ // Should not have parsed the external entity ++ .didEndElement("doc", nil, nil), ++ .endDocument, ++ ]) ++ } ++ } + } + +From 7a6125f16e07bb9cdd15e8bee5e7e2c283da4205 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 9 Aug 2024 01:51:50 +0000 +Subject: [PATCH 2/3] Remove unnecessary `#if os(WASI)` condition in + XMLParser.swift + +--- + Sources/FoundationXML/XMLParser.swift | 6 ------ + 1 file changed, 6 deletions(-) + +diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift +index e3d718a86f..39eea6c3d8 100644 +--- a/Sources/FoundationXML/XMLParser.swift ++++ b/Sources/FoundationXML/XMLParser.swift +@@ -412,9 +412,6 @@ open class XMLParser : NSObject { + + // initializes the parser with the specified URL. + public convenience init?(contentsOf url: URL) { +-#if os(WASI) +- return nil +-#else + setupXMLParsing() + if url.isFileURL { + if let stream = InputStream(url: url) { +@@ -432,7 +429,6 @@ open class XMLParser : NSObject { + return nil + } + } +-#endif + } + + // create the parser from data +@@ -448,7 +444,6 @@ open class XMLParser : NSObject { + _CFXMLInterfaceDestroyContext(_parserContext) + } + +-#if !os(WASI) + //create a parser that incrementally pulls data from the specified stream and parses it. + public init(stream: InputStream) { + setupXMLParsing() +@@ -456,7 +451,6 @@ open class XMLParser : NSObject { + _handler = _CFXMLInterfaceCreateSAXHandler() + _parserContext = nil + } +-#endif + + open weak var delegate: XMLParserDelegate? + + +From a4a80e1c2f6721b7e92e137d10dbf37dacb65be9 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 23 Aug 2024 06:20:59 +0000 +Subject: [PATCH 3/3] Keep the current parser in TLS instead of TaskLocal + +TaskLocal storage is inherited by non-detached child tasks, which can +lead to the parser being shared between tasks. This is not our intention +and can lead to inconsistent state. Instead, we should keep the current +parser in thread-local storage. This should be safe as long as we don't +have any structured suspension points in `withCurrentParser` block. +--- + Sources/FoundationXML/XMLParser.swift | 65 ++++++++++++++++++--------- + 1 file changed, 44 insertions(+), 21 deletions(-) + +diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift +index 39eea6c3d8..952c25cd58 100644 +--- a/Sources/FoundationXML/XMLParser.swift ++++ b/Sources/FoundationXML/XMLParser.swift +@@ -462,34 +462,57 @@ open class XMLParser : NSObject { + + open var allowedExternalEntityURLs: Set? + +- /// The current parser is stored in a task local variable to allow for +- /// concurrent parsing in different tasks with different parsers. +- /// +- /// Rationale for `@unchecked Sendable`: +- /// While the ``XMLParser`` class itself is not `Sendable`, `TaskLocal` +- /// requires the value type to be `Sendable`. The sendability requirement +- /// of `TaskLocal` is only for the "default" value and values set with +- /// `withValue` will not be shared between tasks. +- /// So as long as 1. the default value is safe to be shared between tasks +- /// and 2. the `Sendable` conformance of `_CurrentParser` is not used +- /// outside of `TaskLocal`, it is safe to mark it as `@unchecked Sendable`. +- private struct _CurrentParser: @unchecked Sendable { +- let parser: XMLParser? +- +- static var `default`: _CurrentParser { +- return _CurrentParser(parser: nil) ++ /// The current parser context for the current thread. ++ private class _CurrentParserContext { ++ var _stack: [XMLParser] = [] ++ var _current: XMLParser? { ++ return _stack.last + } + } + +- @TaskLocal +- private static var _currentParser: _CurrentParser = .default ++ #if os(WASI) ++ /// The current parser associated with the current thread. (assuming no multi-threading) ++ /// FIXME: Unify the implementation with the other platforms once we unlock `threadDictionary` ++ /// or migrate to `FoundationEssentials._ThreadLocal`. ++ private static nonisolated(unsafe) var _currentParserContext: _CurrentParserContext? ++ #else ++ /// The current parser associated with the current thread. ++ private static var _currentParserContext: _CurrentParserContext? { ++ get { ++ return Thread.current.threadDictionary["__CurrentNSXMLParser"] as? _CurrentParserContext ++ } ++ set { ++ Thread.current.threadDictionary["__CurrentNSXMLParser"] = newValue ++ } ++ } ++ #endif + ++ /// The current parser associated with the current thread. + internal static func currentParser() -> XMLParser? { +- return _currentParser.parser ++ if let ctx = _currentParserContext { ++ return ctx._current ++ } ++ return nil + } +- ++ ++ /// Execute the given closure with the current parser set to the given parser. + internal static func withCurrentParser(_ parser: XMLParser, _ body: () -> R) -> R { +- return self.$_currentParser.withValue(_CurrentParser(parser: parser), operation: body) ++ var ctx: _CurrentParserContext ++ if let current = _currentParserContext { ++ // Use the existing context if it exists ++ ctx = current ++ } else { ++ // Create a new context in TLS ++ ctx = _CurrentParserContext() ++ _currentParserContext = ctx ++ } ++ // Push the parser onto the stack ++ ctx._stack.append(parser) ++ defer { ++ // Pop the parser off the stack ++ ctx._stack.removeLast() ++ } ++ return body() + } + + internal func _handleParseResult(_ parseResult: Int32) -> Bool { diff --git a/schemes/release-6.0/swift-corelibs-foundation/5063.patch b/schemes/release-6.0/swift-corelibs-foundation/5063.patch new file mode 100644 index 00000000..ae23a73e --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/5063.patch @@ -0,0 +1,30 @@ +From be0e853408a6278fa62f43864d90bf2f53170734 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 12:26:08 +0000 +Subject: [PATCH] Remove the workaround for WASI errno conflict + +As we have fixed the conflict in the WASILibc overlay module while +porting swift-foundation, we can remove the workaround from +corelibs-foundation too. +--- + Sources/Foundation/NSPathUtilities.swift | 7 ------- + 1 file changed, 7 deletions(-) + +diff --git a/Sources/Foundation/NSPathUtilities.swift b/Sources/Foundation/NSPathUtilities.swift +index f6cda6abf9..408b18bebf 100644 +--- a/Sources/Foundation/NSPathUtilities.swift ++++ b/Sources/Foundation/NSPathUtilities.swift +@@ -12,13 +12,6 @@ + import WinSDK + #elseif os(WASI) + import WASILibc +-// CoreFoundation brings but it conflicts with WASILibc.errno +-// definition, so we need to explicitly select the one from WASILibc. +-// This is defined as "internal" since this workaround also used in other files. +-internal var errno: Int32 { +- get { WASILibc.errno } +- set { WASILibc.errno = newValue } +-} + #endif + + #if os(Windows) diff --git a/schemes/release-6.0/swift-corelibs-foundation/5064.patch b/schemes/release-6.0/swift-corelibs-foundation/5064.patch new file mode 100644 index 00000000..c7f736b0 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/5064.patch @@ -0,0 +1,63 @@ +From 4127e856e0eda0c1508e60f1c03b4d5c1cbe78db Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 18:35:56 +0000 +Subject: [PATCH] Make curl an optional dependency when not building + FoundationNetworking + +When building for WASI, FoundationNetworking is not supported, so we +should not require curl to be present. This change makes curl an optional +dependency when FoundationNetworking is not being built. +--- + CMakeLists.txt | 12 +++++++++++- + Sources/CMakeLists.txt | 4 ++-- + 2 files changed, 13 insertions(+), 3 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 4b545a381e..1d9c684d94 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -63,6 +63,14 @@ if(BUILD_SHARED_LIBS) + option(FOUNDATION_BUILD_TOOLS "build tools" ON) + endif() + ++set(FOUNDATION_BUILD_NETWORKING_default ON) ++if(CMAKE_SYSTEM_NAME STREQUAL WASI) ++ # Networking is not supported on WASI ++ set(FOUNDATION_BUILD_NETWORKING_default OFF) ++endif() ++option(FOUNDATION_BUILD_NETWORKING "build FoundationNetworking" ++ ${FOUNDATION_BUILD_NETWORKING_default}) ++ + set(CMAKE_POSITION_INDEPENDENT_CODE YES) + + # Fetchable dependcies +@@ -121,7 +129,9 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) + endif() + endif() + find_package(LibXml2 REQUIRED) +-find_package(CURL REQUIRED) ++if(FOUNDATION_BUILD_NETWORKING) ++ find_package(CURL REQUIRED) ++endif() + + # Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation) + list(APPEND _Foundation_common_build_flags +diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt +index 29b9244065..f239bdf0ff 100644 +--- a/Sources/CMakeLists.txt ++++ b/Sources/CMakeLists.txt +@@ -14,12 +14,12 @@ + + add_subdirectory(CoreFoundation) + add_subdirectory(_CFXMLInterface) +-if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") ++if(FOUNDATION_BUILD_NETWORKING) + add_subdirectory(_CFURLSessionInterface) + endif() + add_subdirectory(Foundation) + add_subdirectory(FoundationXML) +-if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") ++if(FOUNDATION_BUILD_NETWORKING) + add_subdirectory(FoundationNetworking) + endif() + if(FOUNDATION_BUILD_TOOLS) diff --git a/schemes/release-6.0/swift-foundation/781.patch b/schemes/release-6.0/swift-foundation/781.patch new file mode 100644 index 00000000..0624a977 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/781.patch @@ -0,0 +1,105 @@ +From 4e06bcd6ff381c271fab535449dcd0d1291c83e7 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 26 Jul 2024 07:03:21 +0000 +Subject: [PATCH] Use platform shims for clock ids to support wasi-libc + +This change adds platform shims for clock ids so that we can use +them in Swift code because the clock id macro definitions in wasi-libc +can't be imported through ClangImporter. + +Also wasi-libc's `timespec.tv_sec` and `timespec.tv_nsec` are not +imported as `Int` but as `Int64` and `Int32` respectively, so we need to +cast them to `UInt64` before doing arithmetic operations on them. +--- + Sources/FoundationEssentials/CMakeLists.txt | 1 + + .../ProcessInfo/ProcessInfo.swift | 2 +- + .../WASILibc+Extensions.swift | 32 +++++++++++++++++++ + .../include/platform_shims.h | 13 ++++++++ + 4 files changed, 47 insertions(+), 1 deletion(-) + create mode 100644 Sources/FoundationEssentials/WASILibc+Extensions.swift + +diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt +index d9ebc2ebb..836d849ae 100644 +--- a/Sources/FoundationEssentials/CMakeLists.txt ++++ b/Sources/FoundationEssentials/CMakeLists.txt +@@ -30,6 +30,7 @@ add_library(FoundationEssentials + SortComparator.swift + UUID_Wrappers.swift + UUID.swift ++ WASILibc+Extensions.swift + WinSDK+Extensions.swift) + + add_subdirectory(AttributedString) +diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +index 302d3c62d..78b86c266 100644 +--- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift ++++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +@@ -129,7 +129,7 @@ final class _ProcessInfo: Sendable { + #else + var ts: timespec = timespec() + clock_gettime(CLOCK_MONOTONIC_RAW, &ts) +- let time: UInt64 = UInt64(ts.tv_sec * 1000000000 + ts.tv_nsec) ++ let time: UInt64 = UInt64(ts.tv_sec) * 1000000000 + UInt64(ts.tv_nsec) + #endif + let timeString = String(time, radix: 16, uppercase: true) + let padding = String(repeating: "0", count: 16 - timeString.count) +diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift +new file mode 100644 +index 000000000..1c05f9958 +--- /dev/null ++++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift +@@ -0,0 +1,32 @@ ++//===----------------------------------------------------------------------===// ++// ++// This source file is part of the Swift.org open source project ++// ++// Copyright (c) 2024 Apple Inc. and the Swift project authors ++// Licensed under Apache License v2.0 with Runtime Library Exception ++// ++// See https://swift.org/LICENSE.txt for license information ++// ++//===----------------------------------------------------------------------===// ++ ++#if os(WASI) ++ ++import WASILibc ++internal import _FoundationCShims ++ ++// MARK: - Clock ++ ++internal var CLOCK_REALTIME: clockid_t { ++ return _platform_shims_clock_realtime() ++} ++ ++internal var CLOCK_MONOTONIC: clockid_t { ++ return _platform_shims_clock_monotonic() ++} ++ ++internal var CLOCK_MONOTONIC_RAW: clockid_t { ++ // WASI does not have a raw monotonic clock, so we use the monotonic clock instead. ++ return CLOCK_MONOTONIC ++} ++ ++#endif // os(WASI) +diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h +index 911fc9e7a..9c7e95925 100644 +--- a/Sources/_FoundationCShims/include/platform_shims.h ++++ b/Sources/_FoundationCShims/include/platform_shims.h +@@ -68,4 +68,17 @@ typedef enum { + INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); + #endif + ++#if TARGET_OS_WASI ++// Define clock id getter shims so that we can use them in Swift ++// even if clock id macros can't be imported through ClangImporter. ++ ++#include ++static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) { ++ return CLOCK_MONOTONIC; ++} ++static inline _Nonnull clockid_t _platform_shims_clock_realtime(void) { ++ return CLOCK_REALTIME; ++} ++#endif ++ + #endif /* CSHIMS_PLATFORM_SHIMS */ diff --git a/schemes/release-6.0/swift-foundation/825.patch b/schemes/release-6.0/swift-foundation/825.patch new file mode 100644 index 00000000..1bae05b9 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/825.patch @@ -0,0 +1,1150 @@ +From cfb5a0220a2c859e8f2f58d52de98fc2171e3cfb Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 00:50:27 +0900 +Subject: [PATCH 01/12] Add explicit include of `wasi/libc-environ.h` (#786) + +This is necessary to get the `__wasilibc_get_environ` function +declaration. + +(cherry picked from commit 243066f12d0b6f1ab8ad9fefd8526b2383641892) +--- + Sources/_FoundationCShims/platform_shims.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c +index 5a400a468..6f792241c 100644 +--- a/Sources/_FoundationCShims/platform_shims.c ++++ b/Sources/_FoundationCShims/platform_shims.c +@@ -21,6 +21,10 @@ + extern char **environ; + #endif + ++#if __wasi__ ++#include // for __wasilibc_get_environ ++#endif ++ + #if __has_include() + #import + void _platform_shims_lock_environ() { + +From 189cfd856357620038849645f5ca9ba077837266 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:02:11 +0900 +Subject: [PATCH 02/12] Add explicit void type parameter to C functions without + parameters (#775) + +C functions with `()` as parameter list can take any number of +parameters. But WebAssembly requires static signature information for +every function call, so we need to explicitly specify `(void)` to +indicate that the function takes no parameters. + +(cherry picked from commit 8f34f38f30858f7b9dfa9a40b07c18fd7a7b93ae) +--- + .../_FoundationCShims/include/platform_shims.h | 12 ++++++------ + Sources/_FoundationCShims/platform_shims.c | 16 ++++++++-------- + 2 files changed, 14 insertions(+), 14 deletions(-) + +diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h +index 911fc9e7a..f8048e678 100644 +--- a/Sources/_FoundationCShims/include/platform_shims.h ++++ b/Sources/_FoundationCShims/include/platform_shims.h +@@ -31,19 +31,19 @@ + #include + #endif + +-INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(); ++INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(void); + +-INTERNAL void _platform_shims_lock_environ(); +-INTERNAL void _platform_shims_unlock_environ(); ++INTERNAL void _platform_shims_lock_environ(void); ++INTERNAL void _platform_shims_unlock_environ(void); + + #if __has_include() + #include +-INTERNAL vm_size_t _platform_shims_vm_size(); ++INTERNAL vm_size_t _platform_shims_vm_size(void); + #endif + + #if __has_include() + #include +-INTERNAL mach_port_t _platform_mach_task_self(); ++INTERNAL mach_port_t _platform_mach_task_self(void); + #endif + + #if __has_include() +@@ -65,7 +65,7 @@ typedef enum { + } _platform_shims_OSThermalPressureLevel; + + +-INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); ++INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(void); + #endif + + #endif /* CSHIMS_PLATFORM_SHIMS */ +diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c +index 6f792241c..556bc9416 100644 +--- a/Sources/_FoundationCShims/platform_shims.c ++++ b/Sources/_FoundationCShims/platform_shims.c +@@ -27,19 +27,19 @@ extern char **environ; + + #if __has_include() + #import +-void _platform_shims_lock_environ() { ++void _platform_shims_lock_environ(void) { + environ_lock_np(); + } + +-void _platform_shims_unlock_environ() { ++void _platform_shims_unlock_environ(void) { + environ_unlock_np(); + } + #else +-void _platform_shims_lock_environ() { /* noop */ } +-void _platform_shims_unlock_environ() { /* noop */ } ++void _platform_shims_lock_environ(void) { /* noop */ } ++void _platform_shims_unlock_environ(void) { /* noop */ } + #endif + +-char ** _platform_shims_get_environ() { ++char ** _platform_shims_get_environ(void) { + #if __has_include() + return *_NSGetEnviron(); + #elif defined(_WIN32) +@@ -52,20 +52,20 @@ char ** _platform_shims_get_environ() { + } + + #if __has_include() +-const char * _platform_shims_kOSThermalNotificationPressureLevelName() { ++const char * _platform_shims_kOSThermalNotificationPressureLevelName(void) { + return kOSThermalNotificationPressureLevelName; + } + #endif + + #if __has_include() +-vm_size_t _platform_shims_vm_size() { ++vm_size_t _platform_shims_vm_size(void) { + // This shim exists because vm_page_size is not marked const, and therefore looks like global mutable state to Swift. + return vm_page_size; + } + #endif + + #if __has_include() +-mach_port_t _platform_mach_task_self() { ++mach_port_t _platform_mach_task_self(void) { + // This shim exists because mach_task_self_ is not marked const, and therefore looks like global mutable state to Swift. + return mach_task_self(); + } + +From 9f3ab600d3062e8de6c79d306449faad8a194449 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:06:57 +0900 +Subject: [PATCH 03/12] Exclude EREMOTE definition for WASI platform (#778) + +WASI does not define the EREMOTE error code. + +(cherry picked from commit 6bb5ff7b29ed65a722119057d406ffd4bdcdf1b9) +--- + Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +index a7e01952c..d6977ba47 100644 +--- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift ++++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +@@ -465,11 +465,13 @@ extension POSIXError { + return .ESTALE + } + ++ #if !os(WASI) + /// Too many levels of remote in path. + public static var EREMOTE: POSIXErrorCode { + return .EREMOTE + } + #endif ++ #endif + + #if canImport(Darwin) + /// RPC struct is bad. + +From 9c6d53cb0b49bb7b79eda03b7dcd4aacee064118 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:12:39 +0900 +Subject: [PATCH 04/12] Throw `.featureUnsupported` when attempting to create + temp files on WASI (#779) + +WASI does not have temp directory concept, and does not provide mktemp +family of functions, so attempting to create a temporary file should be +considered a feature unsupported. + +(cherry picked from commit fb11420dc6f546e67fce249c15bb17e208c40aff) +--- + Sources/FoundationEssentials/Data/Data+Writing.swift | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index 4f92cb1a6..44cf53d64 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -127,6 +127,10 @@ private func cleanupTemporaryDirectory(at inPath: String?) { + + /// Caller is responsible for calling `close` on the `Int32` file descriptor. + private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, prefix: String, options: Data.WritingOptions) throws -> (Int32, String) { ++#if os(WASI) ++ // WASI does not have temp directories ++ throw CocoaError(.featureUnsupported) ++#else + var directoryPath = destinationPath + if !directoryPath.isEmpty && directoryPath.last! != "/" { + directoryPath.append("/") +@@ -181,6 +185,7 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, + } + } + } while true ++#endif // os(WASI) + } + + /// Returns `(file descriptor, temporary file path, temporary directory path)` + +From 1b8c399cd90a8d6e960a18c5edfda427ec2ba461 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:15:23 +0900 +Subject: [PATCH 05/12] Fix `operatingSystemVersion` on WASI (#782) + +The `operatingSystemVersion` property type is a tuple but the it was +returning an `OperatingSystemVersion` instance on unknown platforms. + +(cherry picked from commit a8f12255dbf98e0899af84d0e674ab71be30bd85) +--- + Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +index 302d3c62d..a3e6b6c60 100644 +--- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift ++++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +@@ -389,7 +389,7 @@ extension _ProcessInfo { + patch: Int(osVersionInfo.dwBuildNumber) + ) + #else +- return OperatingSystemVersion(majorVersion: -1, minorVersion: 0, patchVersion: 0) ++ return (major: -1, minor: 0, patch: 0) + #endif + } + + +From 0f1c377bc3935e2060848f234a20ac89ef085040 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:20:05 +0900 +Subject: [PATCH 06/12] Guard out extended or fs attributes related code on + WASI (#784) + +This commit guards out the extended attributes and file system +attributes related code on WASI as WASI does not support these +features. Just return nothing or ignore the set request. + +(cherry picked from commit fab7195ea2503d296ce80ff6bc5cdb6da8c71b9c) +--- + .../FileManager/FileManager+Files.swift | 13 +++++++++++-- + .../FileManager/FileManager+Utilities.swift | 2 +- + 2 files changed, 12 insertions(+), 3 deletions(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index 72e05f937..cf5cb79a4 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -484,7 +484,7 @@ extension _FileManagerImpl { + #endif + } + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + private func _extendedAttribute(_ key: UnsafePointer, at path: UnsafePointer, followSymlinks: Bool) throws -> Data? { + #if canImport(Darwin) + var size = getxattr(path, key, nil, 0, 0, followSymlinks ? 0 : XATTR_NOFOLLOW) +@@ -638,10 +638,11 @@ extension _FileManagerImpl { + + var attributes = statAtPath.fileAttributes + try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes) +- ++ #if !os(WASI) // WASI does not support extended attributes + if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) { + attributes[._extendedAttributes] = extendedAttrs + } ++ #endif + + #if !targetEnvironment(simulator) && FOUNDATION_FRAMEWORK + if statAtPath.isRegular || statAtPath.isDirectory { +@@ -703,6 +704,9 @@ extension _FileManagerImpl { + ] + } + } ++#elseif os(WASI) ++ // WASI does not support file system attributes ++ return [:] + #else + try fileManager.withFileSystemRepresentation(for: path) { rep in + guard let rep else { +@@ -930,7 +934,12 @@ extension _FileManagerImpl { + try Self._setCatInfoAttributes(attributes, path: path) + + if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] { ++ #if os(WASI) ++ // WASI does not support extended attributes ++ throw CocoaError.errorWithFilePath(.featureUnsupported, path) ++ #else + try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false) ++ #endif + } + + if let date = attributes[.modificationDate] as? Date { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index bba8ed5c9..eb430baaf 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -173,7 +173,7 @@ extension _FileManagerImpl { + #endif + } + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + static func _setAttribute(_ key: UnsafePointer, value: Data, at path: UnsafePointer, followSymLinks: Bool) throws { + try value.withUnsafeBytes { buffer in + #if canImport(Darwin) + +From 0e3174f872f9d1a48daf6aa9e6f80adda77e16f3 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:23:13 +0900 +Subject: [PATCH 07/12] Guard out user/group related code on WASI (#783) + +* Guard out user/group related code on WASI + +This change guards out the user/group related code on WASI, as WASI does +not have the concept of users or groups. + +* Throw explicit unsupported error if trying to set user or group on WASI + +Instead of implicitly ignoring user-given values, we should throw +exception to make it clear that those values cannot be set on WASI. + +(cherry picked from commit 0b3974d35103fd1e3e5213f2cdcefc1fd7fa84f4) +--- + Sources/FoundationEssentials/Data/Data+Writing.swift | 2 ++ + .../FoundationEssentials/FileManager/FileManager+Files.swift | 5 +++++ + .../FileManager/FileManager+Utilities.swift | 2 +- + .../FoundationEssentials/FileManager/FileOperations.swift | 2 ++ + Sources/FoundationEssentials/Platform.swift | 4 ++-- + Sources/FoundationEssentials/String/String+Path.swift | 2 ++ + 6 files changed, 14 insertions(+), 3 deletions(-) + +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index 44cf53d64..c86f27bc3 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -519,6 +519,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint + + cleanupTemporaryDirectory(at: temporaryDirectoryPath) + ++#if !os(WASI) // WASI does not support fchmod for now + if let mode { + // Try to change the mode if the path has not changed. Do our best, but don't report an error. + #if FOUNDATION_FRAMEWORK +@@ -542,6 +543,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint + fchmod(fd, mode) + #endif + } ++#endif // os(WASI) + } + } + } +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index cf5cb79a4..acf7d3c51 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -922,6 +922,10 @@ extension _FileManagerImpl { + let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self) + + if user != nil || userID != nil || group != nil || groupID != nil { ++ #if os(WASI) ++ // WASI does not have the concept of users or groups ++ throw CocoaError.errorWithFilePath(.featureUnsupported, path) ++ #else + // Bias toward userID & groupID - try to prevent round trips to getpwnam if possible. + var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) } + let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Self._userAccountNameToNumber) ?? leaveUnchanged +@@ -929,6 +933,7 @@ extension _FileManagerImpl { + if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 { + throw CocoaError.errorWithFilePath(path, errno: errno, reading: false) + } ++ #endif + } + + try Self._setCatInfoAttributes(attributes, path: path) +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index eb430baaf..45f498937 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -271,7 +271,7 @@ extension _FileManagerImpl { + } + #endif + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + static func _userAccountNameToNumber(_ name: String) -> uid_t? { + name.withCString { ptr in + getpwnam(ptr)?.pointee.pw_uid +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 8e67a771f..130a07ba4 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -855,12 +855,14 @@ enum _FileOperations { + } + defer { close(dstfd) } + ++ #if !os(WASI) // WASI doesn't have fchmod for now + // Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues + let permissions = fileInfo.st_mode & ~S_IFMT + guard fchmod(dstfd, permissions) == 0 else { + try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) + return + } ++ #endif + + if fileInfo.st_size == 0 { + // no copying required +diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift +index 20dc561af..bf2e652d3 100644 +--- a/Sources/FoundationEssentials/Platform.swift ++++ b/Sources/FoundationEssentials/Platform.swift +@@ -113,7 +113,7 @@ private let _cachedUGIDs: (uid_t, gid_t) = { + }() + #endif + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + extension Platform { + private static var ROOT_USER: UInt32 { 0 } + static func getUGIDs(allowEffectiveRootUID: Bool = true) -> (uid: UInt32, gid: UInt32) { +@@ -174,7 +174,7 @@ extension Platform { + // FIXME: bionic implements this as `return 0;` and does not expose the + // function via headers. We should be able to shim this and use the call + // if it is available. +-#if !os(Android) ++#if !os(Android) && !os(WASI) + guard issetugid() == 0 else { return nil } + #endif + if let value = getenv(name) { +diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift +index 79bcbfa76..5e42aef4c 100644 +--- a/Sources/FoundationEssentials/String/String+Path.swift ++++ b/Sources/FoundationEssentials/String/String+Path.swift +@@ -450,6 +450,7 @@ extension String { + return envVar.standardizingPath + } + ++ #if !os(WASI) // WASI does not have user concept + // Next, attempt to find the home directory via getpwnam/getpwuid + var pass: UnsafeMutablePointer? + if let user { +@@ -463,6 +464,7 @@ extension String { + if let dir = pass?.pointee.pw_dir { + return String(cString: dir).standardizingPath + } ++ #endif + + // Fallback to HOME for the current user if possible + if user == nil, let home = getenv("HOME") { + +From 05fc5ebc4c0b92438498ce8a4e38270761b0545f Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:30:17 +0900 +Subject: [PATCH 08/12] Skip sticky-bit check in `isDeletableFile` on WASI + (#785) + +WASI does not surface the sticky bit and getuid, so we cannot check +whether the file is actually deletable before attempting to delete it. + +(cherry picked from commit e90b6c3f90e52f840fc087b05468f430eae1d05a) +--- + .../FoundationEssentials/FileManager/FileManager+Files.swift | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index acf7d3c51..99f1b32c6 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -461,7 +461,7 @@ extension _FileManagerImpl { + parent = fileManager.currentDirectoryPath + } + +-#if os(Windows) ++#if os(Windows) || os(WASI) + return fileManager.isWritableFile(atPath: parent) && fileManager.isWritableFile(atPath: path) + #else + guard fileManager.isWritableFile(atPath: parent), + +From 779e11a4c35584a7a5e43fbc4ca88bfd8a570aca Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 02:50:04 +0900 +Subject: [PATCH 09/12] Implement `_copyRegularFile` for WASI without + `sendfile` (#787) + +WASI doesn't have `sendfile`, so we need to implement the copy in user +space with `read` and `write`. It's not as efficient as `sendfile`, but +it's the best we can do. + +(cherry picked from commit 2a6afeb50aecc5fdfaa7b739399afff1eca024d1) +--- + .../FileManager/FileOperations.swift | 19 +++++++++++++++++++ + 1 file changed, 19 insertions(+) + +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 130a07ba4..51f4f7ea4 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -873,12 +873,31 @@ enum _FileOperations { + let chunkSize: Int = Int(fileInfo.st_blksize) + var current: off_t = 0 + ++ #if os(WASI) ++ // WASI doesn't have sendfile, so we need to do it in user space with read/write ++ try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: chunkSize) { buffer in ++ while current < total { ++ let readSize = Swift.min(total - Int(current), chunkSize) ++ let bytesRead = read(srcfd, buffer.baseAddress, readSize) ++ guard bytesRead >= 0 else { ++ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) ++ return ++ } ++ guard write(dstfd, buffer.baseAddress, bytesRead) == bytesRead else { ++ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) ++ return ++ } ++ current += off_t(bytesRead) ++ } ++ } ++ #else + while current < total { + guard sendfile(dstfd, srcfd, ¤t, Swift.min(total - Int(current), chunkSize)) != -1 else { + try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) + return + } + } ++ #endif + } + #endif + + +From 03c088435bf1300b0b5fe914a7d100d756c19234 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 05:57:20 +0900 +Subject: [PATCH 10/12] Port `LockedState` and `_ThreadLocal` to WASI without + any locking (#780) + +(cherry picked from commit aa68eebebcb8dd16b9636c54e580e0ad32bb57e3) +--- + Sources/FoundationEssentials/LockedState.swift | 9 +++++++++ + Sources/FoundationEssentials/_ThreadLocal.swift | 8 ++++++++ + 2 files changed, 17 insertions(+) + +diff --git a/Sources/FoundationEssentials/LockedState.swift b/Sources/FoundationEssentials/LockedState.swift +index ad432c704..59fbd8b8e 100644 +--- a/Sources/FoundationEssentials/LockedState.swift ++++ b/Sources/FoundationEssentials/LockedState.swift +@@ -33,6 +33,9 @@ package struct LockedState { + typealias Primitive = pthread_mutex_t + #elseif canImport(WinSDK) + typealias Primitive = SRWLOCK ++#elseif os(WASI) ++ // WASI is single-threaded, so we don't need a lock. ++ typealias Primitive = Void + #endif + + typealias PlatformLock = UnsafeMutablePointer +@@ -45,6 +48,8 @@ package struct LockedState { + pthread_mutex_init(platformLock, nil) + #elseif canImport(WinSDK) + InitializeSRWLock(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + +@@ -62,6 +67,8 @@ package struct LockedState { + pthread_mutex_lock(platformLock) + #elseif canImport(WinSDK) + AcquireSRWLockExclusive(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + +@@ -72,6 +79,8 @@ package struct LockedState { + pthread_mutex_unlock(platformLock) + #elseif canImport(WinSDK) + ReleaseSRWLockExclusive(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + } +diff --git a/Sources/FoundationEssentials/_ThreadLocal.swift b/Sources/FoundationEssentials/_ThreadLocal.swift +index 67f5dfaae..df5bad8d7 100644 +--- a/Sources/FoundationEssentials/_ThreadLocal.swift ++++ b/Sources/FoundationEssentials/_ThreadLocal.swift +@@ -30,6 +30,8 @@ struct _ThreadLocal { + fileprivate typealias PlatformKey = tss_t + #elseif canImport(WinSDK) + fileprivate typealias PlatformKey = DWORD ++#elseif os(WASI) ++ fileprivate typealias PlatformKey = UnsafeMutablePointer + #endif + + struct Key { +@@ -46,6 +48,8 @@ struct _ThreadLocal { + self.key = key + #elseif canImport(WinSDK) + key = FlsAlloc(nil) ++#elseif os(WASI) ++ key = UnsafeMutablePointer.allocate(capacity: 1) + #endif + } + } +@@ -58,6 +62,8 @@ struct _ThreadLocal { + tss_get(key) + #elseif canImport(WinSDK) + FlsGetValue(key) ++#elseif os(WASI) ++ key.pointee + #endif + } + +@@ -68,6 +74,8 @@ struct _ThreadLocal { + tss_set(key, newValue) + #elseif canImport(WinSDK) + FlsSetValue(key, newValue) ++#elseif os(WASI) ++ key.pointee = newValue + #endif + } + } + +From 2ca6906f97be3110a101a23c452d3389432a98ff Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 3 Aug 2024 01:55:56 +0900 +Subject: [PATCH 11/12] Add WASI platform conditions for libc imports and word + size (#776) + +* Add `import WASILibc` statements to libc import chains + +* Declare wasm32 arch as 32-bit environment + +* Switch to _pointerBitWidth for architecture checks + +This change switches the architecture checks in Data.swift to use the +_pointerBitWidth instead of the arch() checks for consistency with newer +platforms. + +(cherry picked from commit c82d1673eb112ac62e4f770947c8a238a7659163) +--- + .../FoundationEssentials/Calendar/Calendar.swift | 2 ++ + .../Calendar/Calendar_Gregorian.swift | 2 ++ + .../FoundationEssentials/Data/Data+Reading.swift | 2 ++ + .../FoundationEssentials/Data/Data+Writing.swift | 2 ++ + Sources/FoundationEssentials/Data/Data.swift | 14 ++++++++------ + Sources/FoundationEssentials/Date.swift | 2 ++ + .../Decimal/Decimal+Math.swift | 2 ++ + .../Error/CocoaError+FilePath.swift | 2 ++ + .../Error/ErrorCodes+POSIX.swift | 2 ++ + .../FileManager/FileManager+Basics.swift | 2 ++ + .../FileManager/FileManager+Directories.swift | 2 ++ + .../FileManager/FileManager+Files.swift | 3 +++ + .../FileManager/FileManager+SymbolicLinks.swift | 2 ++ + .../FileManager/FileManager+Utilities.swift | 2 ++ + .../FileManager/FileOperations.swift | 2 ++ + ...BinaryInteger+NumericStringRepresentation.swift | 2 ++ + .../ProcessInfo/ProcessInfo.swift | 2 ++ + .../PropertyList/OpenStepPlist.swift | 2 ++ + .../FoundationEssentials/String/String+Path.swift | 2 ++ + .../Calendar/Calendar_ICU.swift | 2 ++ + .../Formatting/Duration+Formatting.swift | 2 ++ + 21 files changed, 49 insertions(+), 6 deletions(-) + +diff --git a/Sources/FoundationEssentials/Calendar/Calendar.swift b/Sources/FoundationEssentials/Calendar/Calendar.swift +index 99d72e161..94a67d60a 100644 +--- a/Sources/FoundationEssentials/Calendar/Calendar.swift ++++ b/Sources/FoundationEssentials/Calendar/Calendar.swift +@@ -18,6 +18,8 @@ import Android + import Glibc + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift +index fd863cef8..a7cc7386f 100644 +--- a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift ++++ b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift +@@ -18,6 +18,8 @@ import Android + import Glibc + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + +diff --git a/Sources/FoundationEssentials/Data/Data+Reading.swift b/Sources/FoundationEssentials/Data/Data+Reading.swift +index eb66e3f7c..4c893f6cb 100644 +--- a/Sources/FoundationEssentials/Data/Data+Reading.swift ++++ b/Sources/FoundationEssentials/Data/Data+Reading.swift +@@ -25,6 +25,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + func _fgetxattr(_ fd: Int32, _ name: UnsafePointer!, _ value: UnsafeMutableRawPointer!, _ size: Int, _ position: UInt32, _ options: Int32) -> Int { +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index c86f27bc3..7150157aa 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -27,6 +27,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !NO_FILESYSTEM +diff --git a/Sources/FoundationEssentials/Data/Data.swift b/Sources/FoundationEssentials/Data/Data.swift +index a88478261..40fee4a2c 100644 +--- a/Sources/FoundationEssentials/Data/Data.swift ++++ b/Sources/FoundationEssentials/Data/Data.swift +@@ -76,6 +76,8 @@ import Glibc + import Musl + #elseif canImport(ucrt) + import ucrt ++#elseif canImport(WASILibc) ++import WASILibc + #endif + + #if os(Windows) +@@ -580,11 +582,11 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + @usableFromInline + @frozen + internal struct InlineData : Sendable { +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, + UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum + @usableFromInline var bytes: Buffer +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, + UInt8, UInt8) //len //enum + @usableFromInline var bytes: Buffer +@@ -615,9 +617,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + @inlinable // This is @inlinable as a trivial initializer. + init(count: Int = 0) { + assert(count <= MemoryLayout.size) +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) + #else + #error ("Unsupported architecture: initialization for Buffer is required for this architecture") +@@ -802,9 +804,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + } + } + +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + @usableFromInline internal typealias HalfInt = Int32 +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + @usableFromInline internal typealias HalfInt = Int16 + #else + #error ("Unsupported architecture: a definition of half of the pointer sized Int needs to be defined for this architecture") +diff --git a/Sources/FoundationEssentials/Date.swift b/Sources/FoundationEssentials/Date.swift +index 8811aa433..66e778c1c 100644 +--- a/Sources/FoundationEssentials/Date.swift ++++ b/Sources/FoundationEssentials/Date.swift +@@ -18,6 +18,8 @@ import Bionic + import Glibc + #elseif canImport(WinSDK) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift +index 172454463..38ef0cf5e 100644 +--- a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift ++++ b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift +@@ -18,6 +18,8 @@ import Android + import Glibc + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + private let powerOfTen: [Decimal.VariableLengthInteger] = [ +diff --git a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift +index fbce0f6f8..3b60afe22 100644 +--- a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift ++++ b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift +@@ -22,6 +22,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + extension CocoaError.Code { +diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +index d6977ba47..9fae00120 100644 +--- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift ++++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +@@ -19,6 +19,8 @@ + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift +index 03ca025a2..d7a4de859 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift +@@ -19,6 +19,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if os(Windows) +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +index 89e3f1f97..a29b31195 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +@@ -26,6 +26,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationCShims +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index 99f1b32c6..5d7c4a2c0 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -26,6 +26,9 @@ internal import _FoundationCShims + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++internal import _FoundationCShims ++import WASILibc + #endif + + extension Date { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift +index fc9b8f70e..92eb3accb 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift +@@ -21,6 +21,8 @@ import Glibc + import CRT + import WinSDK + internal import _FoundationCShims ++#elseif os(WASI) ++import WASILibc + #endif + + extension _FileManagerImpl { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index 45f498937..870e453ff 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -31,6 +31,8 @@ internal import _FoundationCShims + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if os(Windows) +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 51f4f7ea4..d3fc7b2d5 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -19,6 +19,8 @@ import Glibc + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift +index db02789e2..b9a60576a 100644 +--- a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift ++++ b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift +@@ -18,6 +18,8 @@ import Android + import Glibc + #elseif os(Windows) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + // MARK: - BinaryInteger + Numeric string representation +diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +index a3e6b6c60..bd3f042fe 100644 +--- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift ++++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +@@ -21,6 +21,8 @@ import unistd + import Glibc + #elseif os(Windows) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !NO_PROCESS +diff --git a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift +index 174a6edda..42982deeb 100644 +--- a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift ++++ b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift +@@ -16,6 +16,8 @@ import Darwin + import Bionic + #elseif canImport(Glibc) + import Glibc ++#elseif os(WASI) ++import WASILibc + #endif + + #if canImport(CRT) +diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift +index 5e42aef4c..5d4207fb4 100644 +--- a/Sources/FoundationEssentials/String/String+Path.swift ++++ b/Sources/FoundationEssentials/String/String+Path.swift +@@ -18,6 +18,8 @@ import Android + import Glibc + #elseif os(Windows) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationCShims +diff --git a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift +index 0f0b97386..86d1d8d6d 100644 +--- a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift ++++ b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift +@@ -22,6 +22,8 @@ import Glibc + import CRT + #elseif canImport(Darwin) + import Darwin ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationICU +diff --git a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift +index ccfea7873..41d14b388 100644 +--- a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift ++++ b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift +@@ -22,6 +22,8 @@ import Android + import Glibc + #elseif os(Windows) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) + +From fafdf905c4b70652d3a1573ed0e537135c52d891 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Tue, 6 Aug 2024 02:20:54 +0900 +Subject: [PATCH 12/12] Enable wasi-libc emulation features (#777) + +* Enable wasi-libc emulation features + +Those features require explicit macro definitions to be enabled, so add +them to the package definition. Only affects WASI builds. + +* Prefer `TARGET_OS_WASI` over `__wasi__` + +And explain why we need definition checks for `signal.h` and `sys/mman.h` + +(cherry picked from commit c86692f7e7b6d7cb1625d66ee6ff2618011f22f1) +--- + CMakeLists.txt | 8 +++++ + Package.swift | 35 ++++++++++++++----- + Sources/FoundationEssentials/CMakeLists.txt | 1 + + .../CMakeLists.txt | 1 + + Sources/_FoundationCShims/include/_CStdlib.h | 16 ++++++++- + 5 files changed, 51 insertions(+), 10 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index b99fcfa8f..2afb0787e 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -104,6 +104,14 @@ foreach(version ${_SwiftFoundation_versions}) + endforeach() + endforeach() + ++# wasi-libc emulation feature flags ++set(_SwiftFoundation_wasi_libc_flags) ++if(CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ list(APPEND _SwiftFoundation_wasi_libc_flags ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") ++endif() ++ + include(GNUInstallDirs) + include(SwiftFoundationSwiftSupport) + +diff --git a/Package.swift b/Package.swift +index 70f77537b..6477b7ffb 100644 +--- a/Package.swift ++++ b/Package.swift +@@ -70,6 +70,11 @@ var dependencies: [Package.Dependency] { + } + } + ++let wasiLibcCSettings: [CSetting] = [ ++ .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), ++ .define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])), ++] ++ + let package = Package( + name: "FoundationPreview", + platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")], +@@ -91,15 +96,23 @@ let package = Package( + path: "Sources/Foundation"), + + // _FoundationCShims (Internal) +- .target(name: "_FoundationCShims", +- cSettings: [.define("_CRT_SECURE_NO_WARNINGS", +- .when(platforms: [.windows]))]), ++ .target( ++ name: "_FoundationCShims", ++ cSettings: [ ++ .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])) ++ ] + wasiLibcCSettings ++ ), + + // TestSupport (Internal) +- .target(name: "TestSupport", dependencies: [ +- "FoundationEssentials", +- "FoundationInternationalization", +- ], swiftSettings: availabilityMacros + concurrencyChecking), ++ .target( ++ name: "TestSupport", ++ dependencies: [ ++ "FoundationEssentials", ++ "FoundationInternationalization", ++ ], ++ cSettings: wasiLibcCSettings, ++ swiftSettings: availabilityMacros + concurrencyChecking ++ ), + + // FoundationEssentials + .target( +@@ -130,11 +143,14 @@ let package = Package( + ], + cSettings: [ + .define("_GNU_SOURCE", .when(platforms: [.linux])) +- ], ++ ] + wasiLibcCSettings, + swiftSettings: [ + .enableExperimentalFeature("VariadicGenerics"), + .enableExperimentalFeature("AccessLevelOnImport") +- ] + availabilityMacros + concurrencyChecking ++ ] + availabilityMacros + concurrencyChecking, ++ linkerSettings: [ ++ .linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])), ++ ] + ), + .testTarget( + name: "FoundationEssentialsTests", +@@ -166,6 +182,7 @@ let package = Package( + "CMakeLists.txt", + "Predicate/CMakeLists.txt" + ], ++ cSettings: wasiLibcCSettings, + swiftSettings: [ + .enableExperimentalFeature("AccessLevelOnImport") + ] + availabilityMacros + concurrencyChecking +diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt +index d9ebc2ebb..7cddff682 100644 +--- a/Sources/FoundationEssentials/CMakeLists.txt ++++ b/Sources/FoundationEssentials/CMakeLists.txt +@@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE + "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" + "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") + target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros}) ++target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc_flags}) + target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation") + + target_link_libraries(FoundationEssentials PUBLIC +diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt +index 5a89ceb88..857db9cac 100644 +--- a/Sources/FoundationInternationalization/CMakeLists.txt ++++ b/Sources/FoundationInternationalization/CMakeLists.txt +@@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE + "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" + "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") + target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros}) ++target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags}) + target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation") + + target_link_libraries(FoundationInternationalization PUBLIC +diff --git a/Sources/_FoundationCShims/include/_CStdlib.h b/Sources/_FoundationCShims/include/_CStdlib.h +index 8967eb7f4..03373934b 100644 +--- a/Sources/_FoundationCShims/include/_CStdlib.h ++++ b/Sources/_FoundationCShims/include/_CStdlib.h +@@ -60,7 +60,21 @@ + #endif + + #if __has_include() +-#include ++/// Guard against including `signal.h` on WASI. The `signal.h` header file ++/// itself is available in wasi-libc, but it's just a stub that doesn't actually ++/// do anything. And also including it requires a special macro definition ++/// (`_WASI_EMULATED_SIGNAL`) and it causes compilation errors without the macro. ++# if !TARGET_OS_WASI || defined(_WASI_EMULATED_SIGNAL) ++# include ++# endif ++#endif ++ ++#if __has_include() ++/// Similar to `signal.h`, guard against including `sys/mman.h` on WASI unless ++/// `_WASI_EMULATED_MMAN` is enabled. ++# if !TARGET_OS_WASI || defined(_WASI_EMULATED_MMAN) ++# include ++# endif + #endif + + #if __has_include() diff --git a/schemes/release-6.0/swift-foundation/835.patch b/schemes/release-6.0/swift-foundation/835.patch new file mode 100644 index 00000000..23101fd5 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/835.patch @@ -0,0 +1,34 @@ +From abd2b40e2553a3ce05a1ab4db4ee96bd29d26ecc Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Thu, 8 Aug 2024 18:31:56 +0000 +Subject: [PATCH] [wasm] Fall back to a default chunk size when `st_blksize` is + not available + +The `st_blksize` field in `stat` struct is not provided by WASI, so we +fall back to a default chunk size 4KB, which is a common page size. +--- + .../FileManager/FileOperations.swift | 11 ++++++++++- + 1 file changed, 10 insertions(+), 1 deletion(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index d3fc7b2d5..d93468554 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -872,7 +872,16 @@ enum _FileOperations { + } + + let total: Int = Int(fileInfo.st_size) +- let chunkSize: Int = Int(fileInfo.st_blksize) ++ // Respect the optimal block size for the file system if available ++ // Some platforms including WASI don't provide this information, so we ++ // fall back to the default chunk size 4KB, which is a common page size. ++ let defaultChunkSize = 1024 * 4 // 4KB ++ let chunkSize: Int ++ if fileInfo.st_blksize > 0 { ++ chunkSize = Int(fileInfo.st_blksize) ++ } else { ++ chunkSize = defaultChunkSize ++ } + var current: off_t = 0 + + #if os(WASI) diff --git a/schemes/release-6.0/swift-foundation/836.patch b/schemes/release-6.0/swift-foundation/836.patch new file mode 100644 index 00000000..67ea838a --- /dev/null +++ b/schemes/release-6.0/swift-foundation/836.patch @@ -0,0 +1,271 @@ +From 08af69e0991f1ae8b3a68c8144fab987987d8e63 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 26 Jul 2024 07:41:19 +0000 +Subject: [PATCH 1/2] Port directory enumeration related code to WASI + +For now wasi-libc does not include fts(3) implementation, so mark +features depending on it as unsupported on WASI. Once wasi-libc includes +fts or we decide to implement and maintain our own fts-like API, we can +remove these `#if os(WASI)` guards. + +wasi-libc issue tracking fts support: +https://github.com/WebAssembly/wasi-libc/issues/520 + +Also, wasi-libc defines some constants in a way that ClangImporter can't +understand, so we need to grab them manually through _FoundationCShims in +function call form. +--- + .../FileManager/FileManager+Directories.swift | 3 ++ + .../FileOperations+Enumeration.swift | 20 +++++++- + .../FileManager/FileOperations.swift | 49 ++++++++++++++++++- + .../WASILibc+Extensions.swift | 21 ++++++++ + .../include/platform_shims.h | 23 +++++++++ + 5 files changed, 113 insertions(+), 3 deletions(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +index a29b31195..bcbb5e998 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +@@ -193,6 +193,9 @@ extension _FileManagerImpl { + } + } + return results ++#elseif os(WASI) ++ // wasi-libc does not support FTS for now ++ throw CocoaError.errorWithFilePath(.featureUnsupported, path) + #else + return try path.withFileSystemRepresentation { fileSystemRep in + guard let fileSystemRep else { +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift b/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift +index 22111f52c..904f5fb75 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift +@@ -115,10 +115,16 @@ import posix_filesystem.dirent + #elseif canImport(Glibc) + import Glibc + internal import _FoundationCShims ++#elseif os(WASI) ++import WASILibc ++internal import _FoundationCShims + #endif + + // MARK: Directory Iteration + ++// No FTS support in wasi-libc for now (https://github.com/WebAssembly/wasi-libc/issues/520) ++#if !os(WASI) ++ + struct _FTSSequence: Sequence { + enum Element { + struct SwiftFTSENT { +@@ -315,10 +321,12 @@ extension Sequence<_FTSSequence.Element> { + } + } + ++#endif // !os(WASI) ++ + struct _POSIXDirectoryContentsSequence: Sequence { + #if canImport(Darwin) + typealias DirectoryEntryPtr = UnsafeMutablePointer +- #elseif os(Android) || canImport(Glibc) ++ #elseif os(Android) || canImport(Glibc) || os(WASI) + typealias DirectoryEntryPtr = OpaquePointer + #endif + +@@ -343,10 +351,18 @@ struct _POSIXDirectoryContentsSequence: Sequence { + continue + } + // Use name +- let fileName = withUnsafeBytes(of: &dent.pointee.d_name) { buf in ++ let fileName: String ++ #if os(WASI) ++ // Use shim on WASI because wasi-libc defines `d_name` as ++ // "flexible array member" which is not supported by ++ // ClangImporter yet. ++ fileName = String(cString: _platform_shims_dirent_d_name(dent)) ++ #else ++ fileName = withUnsafeBytes(of: &dent.pointee.d_name) { buf in + let ptr = buf.baseAddress!.assumingMemoryBound(to: CChar.self) + return String(cString: ptr) + } ++ #endif + + if fileName == "." || fileName == ".." || fileName == "._" { + continue +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index d3fc7b2d5..dc51ba55b 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -512,7 +512,14 @@ enum _FileOperations { + // We failed for a reason other than the directory not being empty, so throw + throw CocoaError.removeFileError(errno, resolve(path: pathStr)) + } +- ++ ++ #if os(WASI) ++ ++ // wasi-libc does not support FTS, so we don't support removing non-empty directories on WASI for now. ++ throw CocoaError.errorWithFilePath(.featureUnsupported, pathStr) ++ ++ #else ++ + let seq = _FTSSequence(path, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT) + let iterator = seq.makeIterator() + var isFirst = true +@@ -561,6 +568,7 @@ enum _FileOperations { + } + } + } ++ #endif + + } + #endif +@@ -903,6 +911,44 @@ enum _FileOperations { + } + #endif + ++ #if os(WASI) ++ private static func _linkOrCopyFile(_ srcPtr: UnsafePointer, _ dstPtr: UnsafePointer, with fileManager: FileManager, delegate: some LinkOrCopyDelegate) throws { ++ let src = String(cString: srcPtr) ++ let dst = String(cString: dstPtr) ++ guard delegate.shouldPerformOnItemAtPath(src, to: dst) else { return } ++ ++ var stat = stat() ++ guard lstat(srcPtr, &stat) == 0, !stat.isDirectory else { ++ // wasi-libc does not support FTS for now, so we don't support copying/linking ++ // directories on WASI for now. ++ throw CocoaError.errorWithFilePath(.featureUnsupported, String(cString: srcPtr)) ++ } ++ ++ // For now, we support only copying regular files and symlinks. ++ // After we get FTS support (https://github.com/WebAssembly/wasi-libc/pull/522), ++ // we can remove this method and use the below FTS-based implementation. ++ ++ if stat.isSymbolicLink { ++ try withUnsafeTemporaryAllocation(of: CChar.self, capacity: FileManager.MAX_PATH_SIZE) { tempBuff in ++ tempBuff.initialize(repeating: 0) ++ defer { tempBuff.deinitialize() } ++ let len = readlink(srcPtr, tempBuff.baseAddress!, FileManager.MAX_PATH_SIZE - 1) ++ if len >= 0, symlink(tempBuff.baseAddress!, dstPtr) != -1 { ++ return ++ } ++ try delegate.throwIfNecessary(errno, src, dst) ++ } ++ } else { ++ if delegate.copyData { ++ try _copyRegularFile(srcPtr, dstPtr, delegate: delegate) ++ } else { ++ if link(srcPtr, dstPtr) != 0 { ++ try delegate.throwIfNecessary(errno, src, dst) ++ } ++ } ++ } ++ } ++ #else + private static func _linkOrCopyFile(_ srcPtr: UnsafePointer, _ dstPtr: UnsafePointer, with fileManager: FileManager, delegate: some LinkOrCopyDelegate) throws { + try withUnsafeTemporaryAllocation(of: CChar.self, capacity: FileManager.MAX_PATH_SIZE) { buffer in + let dstLen = Platform.copyCString(dst: buffer.baseAddress!, src: dstPtr, size: FileManager.MAX_PATH_SIZE) +@@ -1015,6 +1061,7 @@ enum _FileOperations { + } + } + } ++ #endif + + private static func linkOrCopyFile(_ src: String, dst: String, with fileManager: FileManager, delegate: some LinkOrCopyDelegate) throws { + try src.withFileSystemRepresentation { srcPtr in +diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift +index 1c05f9958..351fe19f2 100644 +--- a/Sources/FoundationEssentials/WASILibc+Extensions.swift ++++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift +@@ -29,4 +29,25 @@ internal var CLOCK_MONOTONIC_RAW: clockid_t { + return CLOCK_MONOTONIC + } + ++// MARK: - File Operations ++ ++internal var DT_DIR: UInt8 { ++ return _platform_shims_DT_DIR() ++} ++internal var DT_UNKNOWN: UInt8 { ++ return _platform_shims_DT_UNKNOWN() ++} ++internal var O_CREAT: Int32 { ++ return _platform_shims_O_CREAT() ++} ++internal var O_EXCL: Int32 { ++ return _platform_shims_O_EXCL() ++} ++internal var O_TRUNC: Int32 { ++ return _platform_shims_O_TRUNC() ++} ++internal var O_WRONLY: Int32 { ++ return _platform_shims_O_WRONLY() ++} ++ + #endif // os(WASI) +diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h +index f45f5fd55..6bc0a0e15 100644 +--- a/Sources/_FoundationCShims/include/platform_shims.h ++++ b/Sources/_FoundationCShims/include/platform_shims.h +@@ -79,6 +79,29 @@ static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) { + static inline _Nonnull clockid_t _platform_shims_clock_realtime(void) { + return CLOCK_REALTIME; + } ++ ++// Define dirent shims so that we can use them in Swift because wasi-libc defines ++// `d_name` as "flexible array member" which is not supported by ClangImporter yet. ++ ++#include ++ ++static inline char * _Nonnull _platform_shims_dirent_d_name(struct dirent * _Nonnull entry) { ++ return entry->d_name; ++} ++ ++// Define getter shims for constants because wasi-libc defines them as function-like macros ++// which are not supported by ClangImporter yet. ++ ++#include ++#include ++#include ++ ++static inline uint8_t _platform_shims_DT_DIR(void) { return DT_DIR; } ++static inline uint8_t _platform_shims_DT_UNKNOWN(void) { return DT_UNKNOWN; } ++static inline int32_t _platform_shims_O_CREAT(void) { return O_CREAT; } ++static inline int32_t _platform_shims_O_EXCL(void) { return O_EXCL; } ++static inline int32_t _platform_shims_O_TRUNC(void) { return O_TRUNC; } ++static inline int32_t _platform_shims_O_WRONLY(void) { return O_WRONLY; } + #endif + + #endif /* CSHIMS_PLATFORM_SHIMS */ + +From 36fed41f7b5b91e4733b9120c144309aba121c2d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Mon, 12 Aug 2024 17:32:17 +0000 +Subject: [PATCH 2/2] Delegate error throwing decisions to the delegate + +--- + .../FileManager/FileOperations.swift | 10 ++++++++-- + 1 file changed, 8 insertions(+), 2 deletions(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index dc51ba55b..3da9c5022 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -918,10 +918,16 @@ enum _FileOperations { + guard delegate.shouldPerformOnItemAtPath(src, to: dst) else { return } + + var stat = stat() +- guard lstat(srcPtr, &stat) == 0, !stat.isDirectory else { ++ guard lstat(srcPtr, &stat) == 0 else { ++ try delegate.throwIfNecessary(errno, src, dst) ++ return ++ } ++ guard !stat.isDirectory else { + // wasi-libc does not support FTS for now, so we don't support copying/linking + // directories on WASI for now. +- throw CocoaError.errorWithFilePath(.featureUnsupported, String(cString: srcPtr)) ++ let error = CocoaError.fileOperationError(.featureUnsupported, src, dst) ++ try delegate.throwIfNecessary(error, src, dst) ++ return + } + + // For now, we support only copying regular files and symlinks. diff --git a/schemes/release-6.0/swift-foundation/843.patch b/schemes/release-6.0/swift-foundation/843.patch new file mode 100644 index 00000000..28e2e6f5 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/843.patch @@ -0,0 +1,104 @@ +From ac455270257a1b27bfb7a8d85c4b296e93ba03bf Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 16:13:28 +0000 +Subject: [PATCH 1/2] Autolink wasi-libc emulation libraries + +`_WASI_EMULATED_XXX` macros require linking against the corresponding +emulation libraries. This patch adds `-Xfrontend -public-autolink-library` +flags to the Swift compiler invocation to automatically link against the +emulation libraries. Also enable getpid emulation explicitly, as it is +available by default but emiting a warning. +--- + CMakeLists.txt | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 108b6572..a2024859 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -118,8 +118,12 @@ endforeach() + set(_SwiftFoundation_wasi_libc_flags) + if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _SwiftFoundation_wasi_libc_flags ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" + "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" +- "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") + endif() + + include(GNUInstallDirs) + +From 7f4abde248649fab83966ddc36df5e65827e663d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 16 Aug 2024 09:52:40 +0000 +Subject: [PATCH 2/2] Link emulation libraries directly if building shared + libraries + +--- + CMakeLists.txt | 19 ++++++++++++++----- + Sources/FoundationEssentials/CMakeLists.txt | 1 + + .../CMakeLists.txt | 1 + + 3 files changed, 16 insertions(+), 5 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index a2024859..79ad98d2 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -114,16 +114,25 @@ foreach(version ${_SwiftFoundation_versions}) + endforeach() + endforeach() + +-# wasi-libc emulation feature flags ++# wasi-libc emulation feature flags passed to the Swift compiler + set(_SwiftFoundation_wasi_libc_flags) ++# wasi-libc emulation libraries to link against when building a shared library ++set(_SwiftFoundation_wasi_libc_libraries) + if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _SwiftFoundation_wasi_libc_flags + "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" +- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" + "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" +- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" +- "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" +- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") ++ if(BUILD_SHARED_LIBS) ++ # Link emulation libraries to the shared library directly when building a shared library ++ list(APPEND _SwiftFoundation_wasi_libc_libraries wasi-emulated-getpid wasi-emulated-signal wasi-emulated-mman) ++ else() ++ # Emit autolink entries to let clients to link against the emulation libraries ++ list(APPEND _SwiftFoundation_wasi_libc_flags ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") ++ endif() + endif() + + include(GNUInstallDirs) +diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt +index e8417d19..006b6caf 100644 +--- a/Sources/FoundationEssentials/CMakeLists.txt ++++ b/Sources/FoundationEssentials/CMakeLists.txt +@@ -73,6 +73,7 @@ target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundati + target_link_libraries(FoundationEssentials PUBLIC + _FoundationCShims + _FoundationCollections) ++target_link_libraries(FoundationEssentials PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) + + if(NOT BUILD_SHARED_LIBS) + target_compile_options(FoundationEssentials PRIVATE +diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt +index 857db9ca..baaf7451 100644 +--- a/Sources/FoundationInternationalization/CMakeLists.txt ++++ b/Sources/FoundationInternationalization/CMakeLists.txt +@@ -40,6 +40,7 @@ target_link_libraries(FoundationInternationalization PUBLIC + FoundationEssentials + _FoundationCShims + _FoundationICU) ++target_link_libraries(FoundationInternationalization PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) + + if(NOT BUILD_SHARED_LIBS) + target_compile_options(FoundationInternationalization PRIVATE diff --git a/schemes/release-6.0/swift/75821.patch b/schemes/release-6.0/swift/75821.patch new file mode 100644 index 00000000..59dac7aa --- /dev/null +++ b/schemes/release-6.0/swift/75821.patch @@ -0,0 +1,130 @@ +From 90799cfdf73e69147baab6f92f934c3a74235c5e Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 13:29:48 +0000 +Subject: [PATCH 1/2] Revert "[wasm] Fix ambiguous `errno` error when importing + WASILibc module" + +This reverts commit 164ec0adaa80e7a29d2a10fb31ee72baddeda6b2. +--- + stdlib/public/Platform/Platform.swift | 6 ------ + 1 file changed, 6 deletions(-) + +diff --git a/stdlib/public/Platform/Platform.swift b/stdlib/public/Platform/Platform.swift +index ca7417c53574b..0c2d63a0cffdb 100644 +--- a/stdlib/public/Platform/Platform.swift ++++ b/stdlib/public/Platform/Platform.swift +@@ -84,11 +84,6 @@ func _convertDarwinBooleanToBool(_ x: DarwinBoolean) -> Bool { + + #endif + +-// wasi-libc defines `errno` in a way ClangImporter can understand, so we don't +-// need to define shims for it. On the contrary, if we define the shim, we will +-// get an ambiguity error when importing WASILibc module and SwiftWASILibc Clang +-// module (or a Clang module that re-exports SwiftWASILibc). +-#if !os(WASI) + //===----------------------------------------------------------------------===// + // sys/errno.h + //===----------------------------------------------------------------------===// +@@ -101,7 +96,6 @@ public var errno : Int32 { + return _swift_stdlib_setErrno(val) + } + } +-#endif + + + //===----------------------------------------------------------------------===// + +From be08ebb12a99636832f055e94956cb500af1a86d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 13:50:57 +0000 +Subject: [PATCH 2/2] [wasm] Annotate errno as SwiftPrivate by apinotes + +This patch adds an apinotes file for SwiftWASILibc clang module to mark +`errno` macro hidden from Swift code. This resolves ambiguity between +the C macro definition and the Swift wrapper in WASILibc overlay module. + +This change installs the apinotes file to the resource directories for +both lib/swift/apinotes and lib/swift_static/apinotes. +--- + stdlib/public/Platform/CMakeLists.txt | 28 +++++++++++++++++++ + stdlib/public/Platform/SwiftWASILibc.apinotes | 5 ++++ + test/stdlib/WASILibcAPI.swift | 19 +++++++++++++ + 3 files changed, 52 insertions(+) + create mode 100644 stdlib/public/Platform/SwiftWASILibc.apinotes + create mode 100644 test/stdlib/WASILibcAPI.swift + +diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt +index 928b5d9cb6302..7a7cab3da6d59 100644 +--- a/stdlib/public/Platform/CMakeLists.txt ++++ b/stdlib/public/Platform/CMakeLists.txt +@@ -527,6 +527,34 @@ if("WASI" IN_LIST SWIFT_SDKS) + DESTINATION "lib/swift_static/${arch_subdir}" + COMPONENT sdk-overlay) + endif() ++ ++ set(wasilibc_apinotes_source "SwiftWASILibc.apinotes") ++ add_custom_command_target( ++ copy_wasilibc_apinotes_resource ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "make_directory" ${SWIFTLIB_DIR}/apinotes ${SWIFTSTATICLIB_DIR}/apinotes ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "copy_if_different" ++ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTLIB_DIR}/apinotes ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "copy_if_different" ++ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTSTATICLIB_DIR}/apinotes ++ OUTPUT ++ ${SWIFTLIB_DIR}/apinotes/${wasilibc_apinotes_source} ++ ${SWIFTSTATICLIB_DIR}/apinotes/${wasilibc_apinotes_source} ++ COMMENT "Copying WASILibc API notes to resource directories") ++ ++ list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_apinotes_resource}) ++ add_dependencies(sdk-overlay ${copy_wasilibc_apinotes_resource}) ++ swift_install_in_component(FILES "${wasilibc_apinotes_source}" ++ DESTINATION "lib/swift/apinotes" ++ COMPONENT sdk-overlay) ++ if(SWIFT_BUILD_STATIC_STDLIB) ++ swift_install_in_component(FILES "${wasilibc_apinotes_source}" ++ DESTINATION "lib/swift_static/apinotes" ++ COMPONENT sdk-overlay) ++ endif() ++ + endforeach() + endif() + add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list}) +diff --git a/stdlib/public/Platform/SwiftWASILibc.apinotes b/stdlib/public/Platform/SwiftWASILibc.apinotes +new file mode 100644 +index 0000000000000..001acc7ebb596 +--- /dev/null ++++ b/stdlib/public/Platform/SwiftWASILibc.apinotes +@@ -0,0 +1,5 @@ ++Name: SwiftWASILibc ++Globals: ++ # errno macro is importable but we provide explicit Swift wrapper ++ - Name: errno ++ SwiftPrivate: true +diff --git a/test/stdlib/WASILibcAPI.swift b/test/stdlib/WASILibcAPI.swift +new file mode 100644 +index 0000000000000..fe599bb9f3878 +--- /dev/null ++++ b/test/stdlib/WASILibcAPI.swift +@@ -0,0 +1,19 @@ ++// RUN: %target-swift-frontend -typecheck -swift-version 6 %s -verify ++// REQUIRES: executable_test ++// REQUIRES: OS=wasi ++ ++import WASILibc ++ ++// errno is a global thread-local variable, so it should be accessible ++// from any context. ++ ++enum TestErrno { ++ static func testSyncContext() { ++ _ = errno ++ errno = 0 ++ } ++ static func testAsyncContext() async { ++ _ = errno ++ errno = 0 ++ } ++} From 3ab0a59abadd3b68b6919511c729f2a983f0430d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:25:24 +0000 Subject: [PATCH 2/9] Update base tag for release-6.0 to swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-22-a --- schemes/release-6.0/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemes/release-6.0/manifest.json b/schemes/release-6.0/manifest.json index b7a8a730..30a002a2 100644 --- a/schemes/release-6.0/manifest.json +++ b/schemes/release-6.0/manifest.json @@ -1,6 +1,6 @@ { "update-checkout-scheme": "release/6.0", - "base-tag": "swift-6.0-DEVELOPMENT-SNAPSHOT-2024-07-19-a", + "base-tag": "swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-22-a", "build-compiler": false, "icu4c": ["https://github.com/swiftwasm/icu4c-wasi/releases/download/0.8.0/icu4c-wasi.tar.xz"], "libxml2": ["https://github.com/swiftwasm/libxml2-wasm/releases/download/2.0.0/libxml2-wasm32-unknown-wasi.tar.gz"], From 0264b755a8bd62ebe00042074417c3a4fca68ce2 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 15:26:12 +0000 Subject: [PATCH 3/9] Fix conflict --- ...ld-Repair-the-build-on-WASI-platform.patch | 96 ++ ...swift-WASI-changes-in-CMakeLists.txt.patch | 66 + ...platformsWithThreads-in-Package.swif.patch | 25 + ...2_INCLUDE_DIR-instead-of-hardcoding-.patch | 30 + ...bdispatch-threads-enable-some-emulat.patch | 84 ++ ...undationNetworking-and-_CFURLSession.patch | 36 + ...references.h-CFRunLoop.h-and-CFStrea.patch | 51 + ...-CFString.c-compilation-error-on-WAS.patch | 26 + ...endored-version-of-BlocksRuntime-on-.patch | 93 ++ ...d-strlcat-are-available-in-wasi-libc.patch | 30 + ...Build-Repair-WASI-build-with-SwiftPM.patch | 77 ++ ...turn_address-instead-of-__builtin_fr.patch | 29 + ...even-try-to-find-libdispatch-on-WASI.patch | 52 + ...uild-BlocksRuntime-as-object-library.patch | 55 + ...ITION_INDEPENDENT_CODE-FALSE-from-Bl.patch | 28 + ...ocal-for-storing-the-current-parser.patch} | 157 +-- ...ry-if-os-WASI-condition-in-XMLParser.patch | 51 + ...t-parser-in-TLS-instead-of-TaskLocal.patch | 100 ++ ...-workaround-for-WASI-errno-conflict.patch} | 7 +- ...onal-dependency-when-not-building-F.patch} | 11 +- ...s-for-clock-ids-to-support-wasi-lib.patch} | 15 +- ...t-include-of-wasi-libc-environ.h-786.patch | 31 + ...d-type-parameter-to-C-functions-with.patch | 109 ++ ...-a-default-chunk-size-when-st_blksi.patch} | 11 +- ...ry-enumeration-related-code-to-WASI.patch} | 66 +- ...ble-wasi-libc-emulation-features-777.patch | 166 +++ ...tolink-wasi-libc-emulation-libraries.patch | 35 + .../release-6.0/swift-foundation/825.patch | 1150 ----------------- .../release-6.0/swift-foundation/843.patch | 104 -- 29 files changed, 1322 insertions(+), 1469 deletions(-) create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0001-build-Repair-the-build-on-WASI-platform.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0002-Reflect-Package.swift-WASI-changes-in-CMakeLists.txt.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0003-Add-.windows-to-platformsWithThreads-in-Package.swif.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0004-CMake-Use-LIBXML2_INCLUDE_DIR-instead-of-hardcoding-.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0005-CMake-Disable-libdispatch-threads-enable-some-emulat.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0006-CMake-Exclude-FoundationNetworking-and-_CFURLSession.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0007-wasm-Include-CFPreferences.h-CFRunLoop.h-and-CFStrea.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0008-wasm-Fix-the-new-CFString.c-compilation-error-on-WAS.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0009-wasm-Build-the-vendored-version-of-BlocksRuntime-on-.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0010-wasm-strlcpy-and-strlcat-are-available-in-wasi-libc.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0011-Build-Repair-WASI-build-with-SwiftPM.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0012-Use-__builtin_return_address-instead-of-__builtin_fr.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0013-CMake-Not-even-try-to-find-libdispatch-on-WASI.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0014-CMake-Build-BlocksRuntime-as-object-library.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0015-CMake-Remove-POSITION_INDEPENDENT_CODE-FALSE-from-Bl.patch rename schemes/release-6.0/swift-corelibs-foundation/{5061.patch => 0016-Use-TaskLocal-for-storing-the-current-parser.patch} (58%) create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0017-Remove-unnecessary-if-os-WASI-condition-in-XMLParser.patch create mode 100644 schemes/release-6.0/swift-corelibs-foundation/0018-Keep-the-current-parser-in-TLS-instead-of-TaskLocal.patch rename schemes/release-6.0/swift-corelibs-foundation/{5063.patch => 0019-Remove-the-workaround-for-WASI-errno-conflict.patch} (90%) rename schemes/release-6.0/swift-corelibs-foundation/{5064.patch => 0020-Make-curl-an-optional-dependency-when-not-building-F.patch} (90%) rename schemes/release-6.0/swift-foundation/{781.patch => 0001-Use-platform-shims-for-clock-ids-to-support-wasi-lib.patch} (94%) create mode 100644 schemes/release-6.0/swift-foundation/0002-Add-explicit-include-of-wasi-libc-environ.h-786.patch create mode 100644 schemes/release-6.0/swift-foundation/0003-Add-explicit-void-type-parameter-to-C-functions-with.patch rename schemes/release-6.0/swift-foundation/{835.patch => 0004-wasm-Fall-back-to-a-default-chunk-size-when-st_blksi.patch} (88%) rename schemes/release-6.0/swift-foundation/{836.patch => 0005-Port-directory-enumeration-related-code-to-WASI.patch} (80%) create mode 100644 schemes/release-6.0/swift-foundation/0006-Enable-wasi-libc-emulation-features-777.patch create mode 100644 schemes/release-6.0/swift-foundation/0007-Autolink-wasi-libc-emulation-libraries.patch delete mode 100644 schemes/release-6.0/swift-foundation/825.patch delete mode 100644 schemes/release-6.0/swift-foundation/843.patch diff --git a/schemes/release-6.0/swift-corelibs-foundation/0001-build-Repair-the-build-on-WASI-platform.patch b/schemes/release-6.0/swift-corelibs-foundation/0001-build-Repair-the-build-on-WASI-platform.patch new file mode 100644 index 00000000..6f48c2eb --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0001-build-Repair-the-build-on-WASI-platform.patch @@ -0,0 +1,96 @@ +From 4db45e0b9c75e3a6ac31cf45a06018592af381f5 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 30 Mar 2024 10:28:40 +0000 +Subject: [PATCH] build: Repair the build on WASI platform + +Cherry picked from https://github.com/apple/swift-corelibs-foundation/pull/4934 + +(cherry picked from commit 51ee6906be0556eb63cd35a16ad4167f69f16a63) +--- + Package.swift | 20 ++++++++++++++++++-- + Sources/CoreFoundation/CFBundle.c | 8 +++++++- + Sources/CoreFoundation/CFString.c | 2 +- + 3 files changed, 26 insertions(+), 4 deletions(-) + +diff --git a/Package.swift b/Package.swift +index 6d660388..4949fe4e 100644 +--- a/Package.swift ++++ b/Package.swift +@@ -3,6 +3,16 @@ + + import PackageDescription + ++let platformsWithThreads: [Platform] = [ ++ .iOS, ++ .macOS, ++ .tvOS, ++ .watchOS, ++ .macCatalyst, ++ .driverKit, ++ .android, ++ .linux, ++] + var dispatchIncludeFlags: [CSetting] + if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] { + dispatchIncludeFlags = [.unsafeFlags([ +@@ -31,8 +41,11 @@ let coreFoundationBuildSettings: [CSetting] = [ + .define("DEPLOYMENT_ENABLE_LIBDISPATCH"), + .define("DEPLOYMENT_RUNTIME_SWIFT"), + .define("HAVE_STRUCT_TIMESPEC"), +- .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"), ++ .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), + .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), ++ .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), ++ .define("HAVE_STRLCPY", .when(platforms: [.wasi])), ++ .define("HAVE_STRLCAT", .when(platforms: [.wasi])), + .unsafeFlags([ + "-Wno-shorten-64-to-32", + "-Wno-deprecated-declarations", +@@ -61,8 +74,11 @@ let interfaceBuildSettings: [CSetting] = [ + .define("CF_BUILDING_CF"), + .define("DEPLOYMENT_ENABLE_LIBDISPATCH"), + .define("HAVE_STRUCT_TIMESPEC"), +- .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"), ++ .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), + .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), ++ .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), ++ .define("HAVE_STRLCPY", .when(platforms: [.wasi])), ++ .define("HAVE_STRLCAT", .when(platforms: [.wasi])), + .unsafeFlags([ + "-Wno-shorten-64-to-32", + "-Wno-deprecated-declarations", +diff --git a/Sources/CoreFoundation/CFBundle.c b/Sources/CoreFoundation/CFBundle.c +index 8026a262..05afe988 100644 +--- a/Sources/CoreFoundation/CFBundle.c ++++ b/Sources/CoreFoundation/CFBundle.c +@@ -596,7 +596,13 @@ static CFBundleRef _CFBundleGetBundleWithIdentifier(CFStringRef bundleID, void * + + CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) { + // Use the frame that called this as a hint +- return _CFBundleGetBundleWithIdentifier(bundleID, __builtin_return_address(0)); ++ void *hint; ++#if TARGET_OS_WASI ++ hint = NULL; ++#else ++ hint = __builtin_frame_address(0); ++#endif ++ return _CFBundleGetBundleWithIdentifier(bundleID, hint); + } + + CFBundleRef _CFBundleGetBundleWithIdentifierWithHint(CFStringRef bundleID, void *pointer) { +diff --git a/Sources/CoreFoundation/CFString.c b/Sources/CoreFoundation/CFString.c +index 1de46dac..94a6c86d 100644 +--- a/Sources/CoreFoundation/CFString.c ++++ b/Sources/CoreFoundation/CFString.c +@@ -28,7 +28,7 @@ + #include "CFRuntime_Internal.h" + #include + #include <_foundation_unicode/uchar.h> +-#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD ++#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI + #include "CFConstantKeys.h" + #include "CFStringLocalizedFormattingInternal.h" + #endif +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0002-Reflect-Package.swift-WASI-changes-in-CMakeLists.txt.patch b/schemes/release-6.0/swift-corelibs-foundation/0002-Reflect-Package.swift-WASI-changes-in-CMakeLists.txt.patch new file mode 100644 index 00000000..03d9392b --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0002-Reflect-Package.swift-WASI-changes-in-CMakeLists.txt.patch @@ -0,0 +1,66 @@ +From f8c20b56988da0f649c1645dd4e0ea18af739b95 Mon Sep 17 00:00:00 2001 +From: Max Desiatov +Date: Mon, 5 Aug 2024 20:28:46 +0100 +Subject: [PATCH] Reflect `Package.swift` WASI changes in `CMakeLists.txt` + +--- + CMakeLists.txt | 26 ++++++++++++++++++++++++-- + 1 file changed, 24 insertions(+), 2 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 7f290d16..1fbdee6a 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -146,7 +146,6 @@ list(APPEND _Foundation_common_build_flags + "-DCF_BUILDING_CF" + "-DDEPLOYMENT_ENABLE_LIBDISPATCH" + "-DHAVE_STRUCT_TIMESPEC" +- "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" + "-Wno-shorten-64-to-32" + "-Wno-deprecated-declarations" + "-Wno-unreachable-code" +@@ -158,6 +157,18 @@ list(APPEND _Foundation_common_build_flags + "-Wno-switch" + "-fblocks") + ++if(CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ list(APPEND _Foundation_common_build_flags ++ "-D_WASI_EMULATED_SIGNAL" ++ "-DHAVE_STRLCPY" ++ "-DHAVE_STRLCAT" ++ ) ++else() ++ list(APPEND _Foundation_common_build_flags ++ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" ++ ) ++endif() ++ + if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC") + list(APPEND _Foundation_common_build_flags + "-fconstant-cfstrings" +@@ -185,10 +196,21 @@ set(_Foundation_swift_build_flags) + list(APPEND _Foundation_swift_build_flags + "-swift-version 6" + "-DDEPLOYMENT_RUNTIME_SWIFT" +- "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" + "-Xfrontend" + "-require-explicit-sendable") + ++if(CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ list(APPEND _Foundation_swift_build_flags ++ "-D_WASI_EMULATED_SIGNAL" ++ "-DHAVE_STRLCPY" ++ "-DHAVE_STRLCAT" ++ ) ++else() ++ list(APPEND _Foundation_swift_build_flags ++ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" ++ ) ++endif() ++ + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") + list(APPEND _Foundation_common_build_flags + "-D_GNU_SOURCE") +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0003-Add-.windows-to-platformsWithThreads-in-Package.swif.patch b/schemes/release-6.0/swift-corelibs-foundation/0003-Add-.windows-to-platformsWithThreads-in-Package.swif.patch new file mode 100644 index 00000000..6f6b87e7 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0003-Add-.windows-to-platformsWithThreads-in-Package.swif.patch @@ -0,0 +1,25 @@ +From 0c954d9de286e15204240722d717e1928e165cbb Mon Sep 17 00:00:00 2001 +From: Max Desiatov +Date: Mon, 5 Aug 2024 20:16:22 +0100 +Subject: [PATCH] Add `.windows` to `platformsWithThreads` in `Package.swift` + +(cherry picked from commit bc0dd484adbabdb978e68cf6905e3b44ca5117bd) +--- + Package.swift | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/Package.swift b/Package.swift +index 4949fe4e..aca50e80 100644 +--- a/Package.swift ++++ b/Package.swift +@@ -12,6 +12,7 @@ let platformsWithThreads: [Platform] = [ + .driverKit, + .android, + .linux, ++ .windows, + ] + var dispatchIncludeFlags: [CSetting] + if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] { +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0004-CMake-Use-LIBXML2_INCLUDE_DIR-instead-of-hardcoding-.patch b/schemes/release-6.0/swift-corelibs-foundation/0004-CMake-Use-LIBXML2_INCLUDE_DIR-instead-of-hardcoding-.patch new file mode 100644 index 00000000..f0d02e34 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0004-CMake-Use-LIBXML2_INCLUDE_DIR-instead-of-hardcoding-.patch @@ -0,0 +1,30 @@ +From 278d6ffde7788d16455f5dbcef9400b75f849adf Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:10:42 +0000 +Subject: [PATCH] [CMake] Use LIBXML2_INCLUDE_DIR instead of hardcoding + /usr/include/libxml2 + +`find_package(LibXml2 REQUIRED)` sets `LIBXML2_INCLUDE_DIR` to the correct +include directory for the libxml2 headers. Use this variable instead of +hardcoding `/usr/include/libxml2`. This allows the build to work with +custom libxml2 builds on WASI. +--- + Sources/_CFXMLInterface/CMakeLists.txt | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/_CFXMLInterface/CMakeLists.txt b/Sources/_CFXMLInterface/CMakeLists.txt +index d6e63a3f..d550a520 100644 +--- a/Sources/_CFXMLInterface/CMakeLists.txt ++++ b/Sources/_CFXMLInterface/CMakeLists.txt +@@ -20,7 +20,7 @@ target_include_directories(_CFXMLInterface + ../CoreFoundation/include + PRIVATE + ../CoreFoundation/internalInclude +- /usr/include/libxml2/) ++ ${LIBXML2_INCLUDE_DIR}) + + target_compile_options(_CFXMLInterface INTERFACE + "$<$:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/../CoreFoundation/include/module.modulemap>" +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0005-CMake-Disable-libdispatch-threads-enable-some-emulat.patch b/schemes/release-6.0/swift-corelibs-foundation/0005-CMake-Disable-libdispatch-threads-enable-some-emulat.patch new file mode 100644 index 00000000..6b92d7ec --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0005-CMake-Disable-libdispatch-threads-enable-some-emulat.patch @@ -0,0 +1,84 @@ +From 7e4cdc1b0163c1a3afdd35da87d4f25b50aa22be Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:13:38 +0000 +Subject: [PATCH] [CMake] Disable libdispatch & threads, enable some emulations + on WASI + +This commit disables libdispatch and threads on WASI, and enables +wasi-libc emulation features. +--- + CMakeLists.txt | 36 +++++++++++++++--------------------- + 1 file changed, 15 insertions(+), 21 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 1fbdee6a..e7d0ebcf 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -125,7 +125,7 @@ endif() + # System dependencies + find_package(LibRT) + find_package(dispatch CONFIG) +-if(NOT dispatch_FOUND) ++if(NOT dispatch_FOUND AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") + set(DEFAULT_DISPATCH_INCLUDE_PATH "/usr/lib/swift") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") +@@ -144,7 +144,6 @@ find_package(CURL REQUIRED) + list(APPEND _Foundation_common_build_flags + "-DDEPLOYMENT_RUNTIME_SWIFT" + "-DCF_BUILDING_CF" +- "-DDEPLOYMENT_ENABLE_LIBDISPATCH" + "-DHAVE_STRUCT_TIMESPEC" + "-Wno-shorten-64-to-32" + "-Wno-deprecated-declarations" +@@ -157,16 +156,10 @@ list(APPEND _Foundation_common_build_flags + "-Wno-switch" + "-fblocks") + +-if(CMAKE_SYSTEM_NAME STREQUAL "WASI") +- list(APPEND _Foundation_common_build_flags +- "-D_WASI_EMULATED_SIGNAL" +- "-DHAVE_STRLCPY" +- "-DHAVE_STRLCAT" +- ) +-else() +- list(APPEND _Foundation_common_build_flags +- "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" +- ) ++if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) ++ list(APPEND _Foundation_common_build_flags ++ "-DDEPLOYMENT_ENABLE_LIBDISPATCH" ++ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS") + endif() + + if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC") +@@ -199,16 +192,17 @@ list(APPEND _Foundation_swift_build_flags + "-Xfrontend" + "-require-explicit-sendable") + +-if(CMAKE_SYSTEM_NAME STREQUAL "WASI") +- list(APPEND _Foundation_swift_build_flags +- "-D_WASI_EMULATED_SIGNAL" +- "-DHAVE_STRLCPY" +- "-DHAVE_STRLCAT" +- ) ++if(CMAKE_SYSTEM_NAME STREQUAL WASI) ++ # Enable wasi-libc emulation features ++ set(WASI_EMULATION_DEFS _WASI_EMULATED_MMAN _WASI_EMULATED_SIGNAL _WASI_EMULATED_PROCESS_CLOCKS) ++ foreach(def ${WASI_EMULATION_DEFS}) ++ list(APPEND _Foundation_swift_build_flags "SHELL:-Xcc -D${def}") ++ list(APPEND _Foundation_common_build_flags "-D${def}") ++ endforeach() + else() +- list(APPEND _Foundation_swift_build_flags +- "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" +- ) ++ # Assume we have threads on other platforms ++ list(APPEND _Foundation_swift_build_flags ++ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS") + endif() + + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0006-CMake-Exclude-FoundationNetworking-and-_CFURLSession.patch b/schemes/release-6.0/swift-corelibs-foundation/0006-CMake-Exclude-FoundationNetworking-and-_CFURLSession.patch new file mode 100644 index 00000000..0da29de5 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0006-CMake-Exclude-FoundationNetworking-and-_CFURLSession.patch @@ -0,0 +1,36 @@ +From e9657f073a0fde4435840e6d4ef93d181d192b8b Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:15:39 +0000 +Subject: [PATCH] [CMake] Exclude FoundationNetworking and + _CFURLSessionInterface on WASI + +Because networking is not a part of WASI Preview 1. We can add it back +when it is available. +--- + Sources/CMakeLists.txt | 8 ++++++-- + 1 file changed, 6 insertions(+), 2 deletions(-) + +diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt +index 0ee266a4..29b92440 100644 +--- a/Sources/CMakeLists.txt ++++ b/Sources/CMakeLists.txt +@@ -14,10 +14,14 @@ + + add_subdirectory(CoreFoundation) + add_subdirectory(_CFXMLInterface) +-add_subdirectory(_CFURLSessionInterface) ++if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ add_subdirectory(_CFURLSessionInterface) ++endif() + add_subdirectory(Foundation) + add_subdirectory(FoundationXML) +-add_subdirectory(FoundationNetworking) ++if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ add_subdirectory(FoundationNetworking) ++endif() + if(FOUNDATION_BUILD_TOOLS) + add_subdirectory(plutil) + endif() +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0007-wasm-Include-CFPreferences.h-CFRunLoop.h-and-CFStrea.patch b/schemes/release-6.0/swift-corelibs-foundation/0007-wasm-Include-CFPreferences.h-CFRunLoop.h-and-CFStrea.patch new file mode 100644 index 00000000..47011859 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0007-wasm-Include-CFPreferences.h-CFRunLoop.h-and-CFStrea.patch @@ -0,0 +1,51 @@ +From d7c300fa5bb26c061aaf41553e06c814cf8e5d8f Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:34:51 +0000 +Subject: [PATCH] [wasm] Include CFPreferences.h, CFRunLoop.h, and CFStream.h + in WASI builds + +Those headers has been imported by Swift side through +`CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h` since +44031b5a0e96ad91eada2261db2d3890818fe1d0 but we switched to +use CoreFoundation.h directly after the recore, and the header was not +updated in 44031b5a0e96ad91eada2261db2d3890818fe1d0 +--- + Sources/CoreFoundation/include/CoreFoundation.h | 8 +++++--- + 1 file changed, 5 insertions(+), 3 deletions(-) + +diff --git a/Sources/CoreFoundation/include/CoreFoundation.h b/Sources/CoreFoundation/include/CoreFoundation.h +index a66e7e61..64313f0b 100644 +--- a/Sources/CoreFoundation/include/CoreFoundation.h ++++ b/Sources/CoreFoundation/include/CoreFoundation.h +@@ -58,9 +58,7 @@ + #include "CFLocale.h" + #include "CFNumber.h" + #include "CFNumberFormatter.h" +-#if !TARGET_OS_WASI + #include "CFPreferences.h" +-#endif + #include "CFPropertyList.h" + #include "CFSet.h" + #include "CFString.h" +@@ -76,13 +74,17 @@ + + #include "ForSwiftFoundationOnly.h" + +-#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX ++#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI ++# if !TARGET_OS_WASI + #include "CFMessagePort.h" + #include "CFPlugIn.h" ++# endif + #include "CFRunLoop.h" + #include "CFStream.h" ++# if !TARGET_OS_WASI + #include "CFSocket.h" + #include "CFMachPort.h" ++# endif + + #include "CFAttributedString.h" + #include "CFNotificationCenter.h" +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0008-wasm-Fix-the-new-CFString.c-compilation-error-on-WAS.patch b/schemes/release-6.0/swift-corelibs-foundation/0008-wasm-Fix-the-new-CFString.c-compilation-error-on-WAS.patch new file mode 100644 index 00000000..8b5ba010 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0008-wasm-Fix-the-new-CFString.c-compilation-error-on-WAS.patch @@ -0,0 +1,26 @@ +From 275c2bd2f3505ba280575baa49d089b5cd8b5f82 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:39:34 +0000 +Subject: [PATCH] [wasm] Fix the new CFString.c compilation error on WASI + +Treat WASI as an usual Unix-like system +--- + Sources/CoreFoundation/CFString.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/CoreFoundation/CFString.c b/Sources/CoreFoundation/CFString.c +index 94a6c86d..f8899e15 100644 +--- a/Sources/CoreFoundation/CFString.c ++++ b/Sources/CoreFoundation/CFString.c +@@ -35,7 +35,7 @@ + #include + #include + #include +-#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD ++#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI + #include + #endif + #if TARGET_OS_WASI +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0009-wasm-Build-the-vendored-version-of-BlocksRuntime-on-.patch b/schemes/release-6.0/swift-corelibs-foundation/0009-wasm-Build-the-vendored-version-of-BlocksRuntime-on-.patch new file mode 100644 index 00000000..514e23b5 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0009-wasm-Build-the-vendored-version-of-BlocksRuntime-on-.patch @@ -0,0 +1,93 @@ +From 0b94918d483207aa8d04ffe5e4cadf856557b75d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:40:24 +0000 +Subject: [PATCH] [wasm] Build the vendored version of BlocksRuntime on WASI + +We had been using the vendored BlocksRuntime on WASI, but the build +configuration was removed during the recore. This change restores the +vendored BlocksRuntime build configuration on WASI. +--- + .../BlockRuntime/CMakeLists.txt | 37 +++++++++++++++++++ + Sources/CoreFoundation/CMakeLists.txt | 6 +++ + Sources/_CFXMLInterface/CMakeLists.txt | 4 ++ + 3 files changed, 47 insertions(+) + create mode 100644 Sources/CoreFoundation/BlockRuntime/CMakeLists.txt + +diff --git a/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +new file mode 100644 +index 00000000..afcd826a +--- /dev/null ++++ b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +@@ -0,0 +1,37 @@ ++##===----------------------------------------------------------------------===## ++## ++## This source file is part of the Swift open source project ++## ++## Copyright (c) 2024 Apple Inc. and the Swift project authors ++## Licensed under Apache License v2.0 ++## ++## See LICENSE.txt for license information ++## See CONTRIBUTORS.md for the list of Swift project authors ++## ++## SPDX-License-Identifier: Apache-2.0 ++## ++##===----------------------------------------------------------------------===## ++ ++# Build the vendored version of the BlocksRuntime library, which is used by ++# platforms that don't support libdispatch. ++ ++add_library(BlocksRuntime ++ data.c ++ runtime.c) ++ ++target_include_directories(BlocksRuntime PUBLIC ++ ${CMAKE_CURRENT_SOURCE_DIR}/include ++ # For CFTargetConditionals.h ++ ${CMAKE_CURRENT_SOURCE_DIR}/../include) ++ ++set_target_properties(BlocksRuntime PROPERTIES ++ POSITION_INDEPENDENT_CODE FALSE) ++ ++add_library(BlocksRuntime::BlocksRuntime ALIAS BlocksRuntime) ++ ++if(NOT BUILD_SHARED_LIBS) ++ set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS BlocksRuntime) ++ install(TARGETS BlocksRuntime ++ ARCHIVE DESTINATION lib/swift$<$>:_static>/${SWIFT_SYSTEM_NAME} ++ LIBRARY DESTINATION lib/swift$<$>:_static>/${SWIFT_SYSTEM_NAME}) ++endif() +diff --git a/Sources/CoreFoundation/CMakeLists.txt b/Sources/CoreFoundation/CMakeLists.txt +index 312992bc..9d556ae1 100644 +--- a/Sources/CoreFoundation/CMakeLists.txt ++++ b/Sources/CoreFoundation/CMakeLists.txt +@@ -119,6 +119,12 @@ target_link_libraries(CoreFoundation + _FoundationICU + dispatch) + ++if(CMAKE_SYSTEM_NAME STREQUAL WASI) ++ # On WASI, we use vendored BlocksRuntime instead of the one from libdispatch ++ add_subdirectory(BlockRuntime) ++ target_link_libraries(CoreFoundation PRIVATE BlocksRuntime) ++endif() ++ + set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS CoreFoundation) + + # Copy Headers to known directory for direct client (XCTest) test builds +diff --git a/Sources/_CFXMLInterface/CMakeLists.txt b/Sources/_CFXMLInterface/CMakeLists.txt +index d550a520..9ca0c279 100644 +--- a/Sources/_CFXMLInterface/CMakeLists.txt ++++ b/Sources/_CFXMLInterface/CMakeLists.txt +@@ -33,6 +33,10 @@ target_link_libraries(_CFXMLInterface PRIVATE + dispatch + LibXml2::LibXml2) + ++if(CMAKE_SYSTEM_NAME STREQUAL WASI) ++ target_link_libraries(_CFXMLInterface PRIVATE BlocksRuntime) ++endif() ++ + if(NOT BUILD_SHARED_LIBS) + set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFXMLInterface) + install(TARGETS _CFXMLInterface +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0010-wasm-strlcpy-and-strlcat-are-available-in-wasi-libc.patch b/schemes/release-6.0/swift-corelibs-foundation/0010-wasm-strlcpy-and-strlcat-are-available-in-wasi-libc.patch new file mode 100644 index 00000000..6048ac3e --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0010-wasm-strlcpy-and-strlcat-are-available-in-wasi-libc.patch @@ -0,0 +1,30 @@ +From 113978e7339cbb51dc1fcb2908543249e8e6856e Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 05:41:37 +0000 +Subject: [PATCH] [wasm] `strlcpy` and `strlcat` are available in wasi-libc + +Mark them available on WASI. Otherwise, the `static inline` implementations +are activated and the build fails with multiple definitions. +--- + .../CoreFoundation/internalInclude/CoreFoundation_Prefix.h | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h b/Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h +index 9ef8f64a..dea3b575 100644 +--- a/Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h ++++ b/Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h +@@ -109,6 +109,11 @@ typedef char * Class; + #include + #endif + ++#if TARGET_OS_WASI ++#define HAVE_STRLCPY 1 ++#define HAVE_STRLCAT 1 ++#endif ++ + #if TARGET_OS_WIN32 + #define BOOL WINDOWS_BOOL + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0011-Build-Repair-WASI-build-with-SwiftPM.patch b/schemes/release-6.0/swift-corelibs-foundation/0011-Build-Repair-WASI-build-with-SwiftPM.patch new file mode 100644 index 00000000..9a7477f8 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0011-Build-Repair-WASI-build-with-SwiftPM.patch @@ -0,0 +1,77 @@ +From 839dfc85f3c1e3cb82cf5a71386ebf477014d9e8 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 06:06:22 +0000 +Subject: [PATCH] [Build] Repair WASI build with SwiftPM + +--- + Package.swift | 21 +++++++++++++++------ + 1 file changed, 15 insertions(+), 6 deletions(-) + +diff --git a/Package.swift b/Package.swift +index aca50e80..86a1a8d0 100644 +--- a/Package.swift ++++ b/Package.swift +@@ -39,14 +39,12 @@ let coreFoundationBuildSettings: [CSetting] = [ + .headerSearchPath("internalInclude"), + .define("DEBUG", .when(configuration: .debug)), + .define("CF_BUILDING_CF"), +- .define("DEPLOYMENT_ENABLE_LIBDISPATCH"), ++ .define("DEPLOYMENT_ENABLE_LIBDISPATCH", .when(platforms: platformsWithThreads)), + .define("DEPLOYMENT_RUNTIME_SWIFT"), + .define("HAVE_STRUCT_TIMESPEC"), + .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), + .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), + .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), +- .define("HAVE_STRLCPY", .when(platforms: [.wasi])), +- .define("HAVE_STRLCAT", .when(platforms: [.wasi])), + .unsafeFlags([ + "-Wno-shorten-64-to-32", + "-Wno-deprecated-declarations", +@@ -78,8 +76,6 @@ let interfaceBuildSettings: [CSetting] = [ + .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), + .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), + .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), +- .define("HAVE_STRLCPY", .when(platforms: [.wasi])), +- .define("HAVE_STRLCAT", .when(platforms: [.wasi])), + .unsafeFlags([ + "-Wno-shorten-64-to-32", + "-Wno-deprecated-declarations", +@@ -161,7 +157,8 @@ let package = Package( + .product(name: "FoundationEssentials", package: "swift-foundation"), + "Foundation", + "CoreFoundation", +- "_CFXMLInterface" ++ "_CFXMLInterface", ++ .target(name: "BlocksRuntime", condition: .when(platforms: [.wasi])), + ], + path: "Sources/FoundationXML", + exclude: [ +@@ -187,6 +184,7 @@ let package = Package( + name: "CoreFoundation", + dependencies: [ + .product(name: "_FoundationICU", package: "swift-foundation-icu"), ++ .target(name: "BlocksRuntime", condition: .when(platforms: [.wasi])), + ], + path: "Sources/CoreFoundation", + exclude: [ +@@ -195,6 +193,17 @@ let package = Package( + ], + cSettings: coreFoundationBuildSettings + ), ++ .target( ++ name: "BlocksRuntime", ++ path: "Sources/CoreFoundation/BlockRuntime", ++ exclude: [ ++ "CMakeLists.txt" ++ ], ++ cSettings: [ ++ // For CFTargetConditionals.h ++ .headerSearchPath("../include"), ++ ] ++ ), + .target( + name: "_CFXMLInterface", + dependencies: [ +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0012-Use-__builtin_return_address-instead-of-__builtin_fr.patch b/schemes/release-6.0/swift-corelibs-foundation/0012-Use-__builtin_return_address-instead-of-__builtin_fr.patch new file mode 100644 index 00000000..d15b29ee --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0012-Use-__builtin_return_address-instead-of-__builtin_fr.patch @@ -0,0 +1,29 @@ +From 5dd43901d12cd263200ee52d4344bcfb5cc45856 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 06:12:47 +0000 +Subject: [PATCH] Use `__builtin_return_address` instead of + `__builtin_frame_address` + +We accidentally changed it to `__builtin_frame_address` in +7f382649f9052da61b6c1054a5e06fa6f345ddc8 but we should keep it as +`__builtin_return_address` as it was before. +--- + Sources/CoreFoundation/CFBundle.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/CoreFoundation/CFBundle.c b/Sources/CoreFoundation/CFBundle.c +index 05afe988..607c7bd9 100644 +--- a/Sources/CoreFoundation/CFBundle.c ++++ b/Sources/CoreFoundation/CFBundle.c +@@ -600,7 +600,7 @@ CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) { + #if TARGET_OS_WASI + hint = NULL; + #else +- hint = __builtin_frame_address(0); ++ hint = __builtin_return_address(0); + #endif + return _CFBundleGetBundleWithIdentifier(bundleID, hint); + } +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0013-CMake-Not-even-try-to-find-libdispatch-on-WASI.patch b/schemes/release-6.0/swift-corelibs-foundation/0013-CMake-Not-even-try-to-find-libdispatch-on-WASI.patch new file mode 100644 index 00000000..463459af --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0013-CMake-Not-even-try-to-find-libdispatch-on-WASI.patch @@ -0,0 +1,52 @@ +From b7ad7208bbd913ef35f9b9816569d59add1c655d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 15:48:14 +0000 +Subject: [PATCH] [CMake] Not even try to find libdispatch on WASI + +--- + CMakeLists.txt | 28 ++++++++++++++++------------ + 1 file changed, 16 insertions(+), 12 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index e7d0ebcf..db674738 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -123,19 +123,23 @@ if(NOT SwiftFoundation_MODULE_TRIPLE) + endif() + + # System dependencies +-find_package(LibRT) +-find_package(dispatch CONFIG) +-if(NOT dispatch_FOUND AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI) +- if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") +- set(DEFAULT_DISPATCH_INCLUDE_PATH "/usr/lib/swift") +- elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") +- set(DEFAULT_DISPATCH_INCLUDE_PATH "$ENV{SDKROOT}usr/include") ++ ++# We know libdispatch is always unavailable on WASI ++if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) ++ find_package(LibRT) ++ find_package(dispatch CONFIG) ++ if(NOT dispatch_FOUND) ++ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") ++ set(DEFAULT_DISPATCH_INCLUDE_PATH "/usr/lib/swift") ++ elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") ++ set(DEFAULT_DISPATCH_INCLUDE_PATH "$ENV{SDKROOT}usr/include") ++ endif() ++ set(DISPATCH_INCLUDE_PATH "${DEFAULT_DISPATCH_INCLUDE_PATH}" CACHE STRING "A path to where you can find libdispatch headers") ++ message("-- dispatch_DIR not found, using dispatch from SDK at ${DISPATCH_INCLUDE_PATH}") ++ list(APPEND _Foundation_common_build_flags ++ "-I${DISPATCH_INCLUDE_PATH}" ++ "-I${DISPATCH_INCLUDE_PATH}/Block") + endif() +- set(DISPATCH_INCLUDE_PATH "${DEFAULT_DISPATCH_INCLUDE_PATH}" CACHE STRING "A path to where you can find libdispatch headers") +- message("-- dispatch_DIR not found, using dispatch from SDK at ${DISPATCH_INCLUDE_PATH}") +- list(APPEND _Foundation_common_build_flags +- "-I${DISPATCH_INCLUDE_PATH}" +- "-I${DISPATCH_INCLUDE_PATH}/Block") + endif() + find_package(LibXml2 REQUIRED) + find_package(CURL REQUIRED) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0014-CMake-Build-BlocksRuntime-as-object-library.patch b/schemes/release-6.0/swift-corelibs-foundation/0014-CMake-Build-BlocksRuntime-as-object-library.patch new file mode 100644 index 00000000..16357c8c --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0014-CMake-Build-BlocksRuntime-as-object-library.patch @@ -0,0 +1,55 @@ +From c2e5c54b1f484c7bef471e86d348ae5e54d3758d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 7 Aug 2024 16:40:22 +0000 +Subject: [PATCH] [CMake] Build BlocksRuntime as object library + +To avoid shippig BlocksRuntime as a separate library, build it as an +object library and include it in CoreFoundation static archive. +--- + Sources/CoreFoundation/BlockRuntime/CMakeLists.txt | 13 ++++--------- + Sources/CoreFoundation/CMakeLists.txt | 1 + + 2 files changed, 5 insertions(+), 9 deletions(-) + +diff --git a/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +index afcd826a..fe5e13bb 100644 +--- a/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt ++++ b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +@@ -15,7 +15,9 @@ + # Build the vendored version of the BlocksRuntime library, which is used by + # platforms that don't support libdispatch. + +-add_library(BlocksRuntime ++# Build the BlocksRuntime as an object library, shipped as a part ++# of libCoreFoundation. ++add_library(BlocksRuntime OBJECT + data.c + runtime.c) + +@@ -27,11 +29,4 @@ target_include_directories(BlocksRuntime PUBLIC + set_target_properties(BlocksRuntime PROPERTIES + POSITION_INDEPENDENT_CODE FALSE) + +-add_library(BlocksRuntime::BlocksRuntime ALIAS BlocksRuntime) +- +-if(NOT BUILD_SHARED_LIBS) +- set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS BlocksRuntime) +- install(TARGETS BlocksRuntime +- ARCHIVE DESTINATION lib/swift$<$>:_static>/${SWIFT_SYSTEM_NAME} +- LIBRARY DESTINATION lib/swift$<$>:_static>/${SWIFT_SYSTEM_NAME}) +-endif() ++set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS BlocksRuntime) +diff --git a/Sources/CoreFoundation/CMakeLists.txt b/Sources/CoreFoundation/CMakeLists.txt +index 9d556ae1..7ae617b4 100644 +--- a/Sources/CoreFoundation/CMakeLists.txt ++++ b/Sources/CoreFoundation/CMakeLists.txt +@@ -122,6 +122,7 @@ target_link_libraries(CoreFoundation + if(CMAKE_SYSTEM_NAME STREQUAL WASI) + # On WASI, we use vendored BlocksRuntime instead of the one from libdispatch + add_subdirectory(BlockRuntime) ++ # Add BlocksRuntime object library to CoreFoundation static archive + target_link_libraries(CoreFoundation PRIVATE BlocksRuntime) + endif() + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0015-CMake-Remove-POSITION_INDEPENDENT_CODE-FALSE-from-Bl.patch b/schemes/release-6.0/swift-corelibs-foundation/0015-CMake-Remove-POSITION_INDEPENDENT_CODE-FALSE-from-Bl.patch new file mode 100644 index 00000000..397ebe89 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0015-CMake-Remove-POSITION_INDEPENDENT_CODE-FALSE-from-Bl.patch @@ -0,0 +1,28 @@ +From 15180fdc00c60d1a5ad7be879ee7108eeb992a85 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Thu, 8 Aug 2024 17:09:49 +0000 +Subject: [PATCH] [CMake] Remove POSITION_INDEPENDENT_CODE=FALSE from + BlocksRuntime + +We ported BlocksRuntime CMakeLists.txt from the state of +02b7d8f0c141b9accdade1922a080898d2d0e0a2 but we don't find any +reason to set POSITION_INDEPENDENT_CODE=FALSE for BlocksRuntime. +--- + Sources/CoreFoundation/BlockRuntime/CMakeLists.txt | 3 --- + 1 file changed, 3 deletions(-) + +diff --git a/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +index fe5e13bb..47779784 100644 +--- a/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt ++++ b/Sources/CoreFoundation/BlockRuntime/CMakeLists.txt +@@ -26,7 +26,4 @@ target_include_directories(BlocksRuntime PUBLIC + # For CFTargetConditionals.h + ${CMAKE_CURRENT_SOURCE_DIR}/../include) + +-set_target_properties(BlocksRuntime PROPERTIES +- POSITION_INDEPENDENT_CODE FALSE) +- + set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS BlocksRuntime) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/5061.patch b/schemes/release-6.0/swift-corelibs-foundation/0016-Use-TaskLocal-for-storing-the-current-parser.patch similarity index 58% rename from schemes/release-6.0/swift-corelibs-foundation/5061.patch rename to schemes/release-6.0/swift-corelibs-foundation/0016-Use-TaskLocal-for-storing-the-current-parser.patch index 5304ae63..c018e63e 100644 --- a/schemes/release-6.0/swift-corelibs-foundation/5061.patch +++ b/schemes/release-6.0/swift-corelibs-foundation/0016-Use-TaskLocal-for-storing-the-current-parser.patch @@ -1,8 +1,7 @@ -From 6a78203fc24702ed81a2b2bb2a9949168e694161 Mon Sep 17 00:00:00 2001 +From a70b45dd79873b9842b87d75b98094cb8e76e201 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 8 Aug 2024 23:33:15 +0000 -Subject: [PATCH 1/3] [XMLParser] Use `TaskLocal` for storing the current - parser +Subject: [PATCH] Use `TaskLocal` for storing the current parser Instead of thread-local storage, use `TaskLocal` to store the current parser. This solves three issues: @@ -31,7 +30,7 @@ parser. This solves three issues: 2 files changed, 74 insertions(+), 54 deletions(-) diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift -index d89d0ee1f4..e3d718a86f 100644 +index d89d0ee1..e3d718a8 100644 --- a/Sources/FoundationXML/XMLParser.swift +++ b/Sources/FoundationXML/XMLParser.swift @@ -398,9 +398,7 @@ extension XMLParser : @unchecked Sendable { } @@ -157,7 +156,7 @@ index d89d0ee1f4..e3d718a86f 100644 // called by the delegate to stop the parse. The delegate will get an error message sent to it. diff --git a/Tests/Foundation/TestXMLParser.swift b/Tests/Foundation/TestXMLParser.swift -index c98741eb38..df3685a82e 100644 +index c98741eb..df3685a8 100644 --- a/Tests/Foundation/TestXMLParser.swift +++ b/Tests/Foundation/TestXMLParser.swift @@ -198,5 +198,46 @@ class TestXMLParser : XCTestCase { @@ -208,150 +207,6 @@ index c98741eb38..df3685a82e 100644 + } + } } +-- +2.43.2 -From 7a6125f16e07bb9cdd15e8bee5e7e2c283da4205 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 9 Aug 2024 01:51:50 +0000 -Subject: [PATCH 2/3] Remove unnecessary `#if os(WASI)` condition in - XMLParser.swift - ---- - Sources/FoundationXML/XMLParser.swift | 6 ------ - 1 file changed, 6 deletions(-) - -diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift -index e3d718a86f..39eea6c3d8 100644 ---- a/Sources/FoundationXML/XMLParser.swift -+++ b/Sources/FoundationXML/XMLParser.swift -@@ -412,9 +412,6 @@ open class XMLParser : NSObject { - - // initializes the parser with the specified URL. - public convenience init?(contentsOf url: URL) { --#if os(WASI) -- return nil --#else - setupXMLParsing() - if url.isFileURL { - if let stream = InputStream(url: url) { -@@ -432,7 +429,6 @@ open class XMLParser : NSObject { - return nil - } - } --#endif - } - - // create the parser from data -@@ -448,7 +444,6 @@ open class XMLParser : NSObject { - _CFXMLInterfaceDestroyContext(_parserContext) - } - --#if !os(WASI) - //create a parser that incrementally pulls data from the specified stream and parses it. - public init(stream: InputStream) { - setupXMLParsing() -@@ -456,7 +451,6 @@ open class XMLParser : NSObject { - _handler = _CFXMLInterfaceCreateSAXHandler() - _parserContext = nil - } --#endif - - open weak var delegate: XMLParserDelegate? - - -From a4a80e1c2f6721b7e92e137d10dbf37dacb65be9 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 23 Aug 2024 06:20:59 +0000 -Subject: [PATCH 3/3] Keep the current parser in TLS instead of TaskLocal - -TaskLocal storage is inherited by non-detached child tasks, which can -lead to the parser being shared between tasks. This is not our intention -and can lead to inconsistent state. Instead, we should keep the current -parser in thread-local storage. This should be safe as long as we don't -have any structured suspension points in `withCurrentParser` block. ---- - Sources/FoundationXML/XMLParser.swift | 65 ++++++++++++++++++--------- - 1 file changed, 44 insertions(+), 21 deletions(-) - -diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift -index 39eea6c3d8..952c25cd58 100644 ---- a/Sources/FoundationXML/XMLParser.swift -+++ b/Sources/FoundationXML/XMLParser.swift -@@ -462,34 +462,57 @@ open class XMLParser : NSObject { - - open var allowedExternalEntityURLs: Set? - -- /// The current parser is stored in a task local variable to allow for -- /// concurrent parsing in different tasks with different parsers. -- /// -- /// Rationale for `@unchecked Sendable`: -- /// While the ``XMLParser`` class itself is not `Sendable`, `TaskLocal` -- /// requires the value type to be `Sendable`. The sendability requirement -- /// of `TaskLocal` is only for the "default" value and values set with -- /// `withValue` will not be shared between tasks. -- /// So as long as 1. the default value is safe to be shared between tasks -- /// and 2. the `Sendable` conformance of `_CurrentParser` is not used -- /// outside of `TaskLocal`, it is safe to mark it as `@unchecked Sendable`. -- private struct _CurrentParser: @unchecked Sendable { -- let parser: XMLParser? -- -- static var `default`: _CurrentParser { -- return _CurrentParser(parser: nil) -+ /// The current parser context for the current thread. -+ private class _CurrentParserContext { -+ var _stack: [XMLParser] = [] -+ var _current: XMLParser? { -+ return _stack.last - } - } - -- @TaskLocal -- private static var _currentParser: _CurrentParser = .default -+ #if os(WASI) -+ /// The current parser associated with the current thread. (assuming no multi-threading) -+ /// FIXME: Unify the implementation with the other platforms once we unlock `threadDictionary` -+ /// or migrate to `FoundationEssentials._ThreadLocal`. -+ private static nonisolated(unsafe) var _currentParserContext: _CurrentParserContext? -+ #else -+ /// The current parser associated with the current thread. -+ private static var _currentParserContext: _CurrentParserContext? { -+ get { -+ return Thread.current.threadDictionary["__CurrentNSXMLParser"] as? _CurrentParserContext -+ } -+ set { -+ Thread.current.threadDictionary["__CurrentNSXMLParser"] = newValue -+ } -+ } -+ #endif - -+ /// The current parser associated with the current thread. - internal static func currentParser() -> XMLParser? { -- return _currentParser.parser -+ if let ctx = _currentParserContext { -+ return ctx._current -+ } -+ return nil - } -- -+ -+ /// Execute the given closure with the current parser set to the given parser. - internal static func withCurrentParser(_ parser: XMLParser, _ body: () -> R) -> R { -- return self.$_currentParser.withValue(_CurrentParser(parser: parser), operation: body) -+ var ctx: _CurrentParserContext -+ if let current = _currentParserContext { -+ // Use the existing context if it exists -+ ctx = current -+ } else { -+ // Create a new context in TLS -+ ctx = _CurrentParserContext() -+ _currentParserContext = ctx -+ } -+ // Push the parser onto the stack -+ ctx._stack.append(parser) -+ defer { -+ // Pop the parser off the stack -+ ctx._stack.removeLast() -+ } -+ return body() - } - - internal func _handleParseResult(_ parseResult: Int32) -> Bool { diff --git a/schemes/release-6.0/swift-corelibs-foundation/0017-Remove-unnecessary-if-os-WASI-condition-in-XMLParser.patch b/schemes/release-6.0/swift-corelibs-foundation/0017-Remove-unnecessary-if-os-WASI-condition-in-XMLParser.patch new file mode 100644 index 00000000..50b21491 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0017-Remove-unnecessary-if-os-WASI-condition-in-XMLParser.patch @@ -0,0 +1,51 @@ +From bf7e4b3172dee8813dfe92b62b7512c70a97d890 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 9 Aug 2024 01:51:50 +0000 +Subject: [PATCH] Remove unnecessary `#if os(WASI)` condition in + XMLParser.swift + +--- + Sources/FoundationXML/XMLParser.swift | 6 ------ + 1 file changed, 6 deletions(-) + +diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift +index e3d718a8..39eea6c3 100644 +--- a/Sources/FoundationXML/XMLParser.swift ++++ b/Sources/FoundationXML/XMLParser.swift +@@ -412,9 +412,6 @@ open class XMLParser : NSObject { + + // initializes the parser with the specified URL. + public convenience init?(contentsOf url: URL) { +-#if os(WASI) +- return nil +-#else + setupXMLParsing() + if url.isFileURL { + if let stream = InputStream(url: url) { +@@ -432,7 +429,6 @@ open class XMLParser : NSObject { + return nil + } + } +-#endif + } + + // create the parser from data +@@ -448,7 +444,6 @@ open class XMLParser : NSObject { + _CFXMLInterfaceDestroyContext(_parserContext) + } + +-#if !os(WASI) + //create a parser that incrementally pulls data from the specified stream and parses it. + public init(stream: InputStream) { + setupXMLParsing() +@@ -456,7 +451,6 @@ open class XMLParser : NSObject { + _handler = _CFXMLInterfaceCreateSAXHandler() + _parserContext = nil + } +-#endif + + open weak var delegate: XMLParserDelegate? + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/0018-Keep-the-current-parser-in-TLS-instead-of-TaskLocal.patch b/schemes/release-6.0/swift-corelibs-foundation/0018-Keep-the-current-parser-in-TLS-instead-of-TaskLocal.patch new file mode 100644 index 00000000..761eeb16 --- /dev/null +++ b/schemes/release-6.0/swift-corelibs-foundation/0018-Keep-the-current-parser-in-TLS-instead-of-TaskLocal.patch @@ -0,0 +1,100 @@ +From 6f652bc5310608a0e5ce3cfbb863b0a9da04a751 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 23 Aug 2024 06:20:59 +0000 +Subject: [PATCH] Keep the current parser in TLS instead of TaskLocal + +TaskLocal storage is inherited by non-detached child tasks, which can +lead to the parser being shared between tasks. This is not our intention +and can lead to inconsistent state. Instead, we should keep the current +parser in thread-local storage. This should be safe as long as we don't +have any structured suspension points in `withCurrentParser` block. +--- + Sources/FoundationXML/XMLParser.swift | 65 ++++++++++++++++++--------- + 1 file changed, 44 insertions(+), 21 deletions(-) + +diff --git a/Sources/FoundationXML/XMLParser.swift b/Sources/FoundationXML/XMLParser.swift +index 39eea6c3..952c25cd 100644 +--- a/Sources/FoundationXML/XMLParser.swift ++++ b/Sources/FoundationXML/XMLParser.swift +@@ -462,34 +462,57 @@ open class XMLParser : NSObject { + + open var allowedExternalEntityURLs: Set? + +- /// The current parser is stored in a task local variable to allow for +- /// concurrent parsing in different tasks with different parsers. +- /// +- /// Rationale for `@unchecked Sendable`: +- /// While the ``XMLParser`` class itself is not `Sendable`, `TaskLocal` +- /// requires the value type to be `Sendable`. The sendability requirement +- /// of `TaskLocal` is only for the "default" value and values set with +- /// `withValue` will not be shared between tasks. +- /// So as long as 1. the default value is safe to be shared between tasks +- /// and 2. the `Sendable` conformance of `_CurrentParser` is not used +- /// outside of `TaskLocal`, it is safe to mark it as `@unchecked Sendable`. +- private struct _CurrentParser: @unchecked Sendable { +- let parser: XMLParser? +- +- static var `default`: _CurrentParser { +- return _CurrentParser(parser: nil) ++ /// The current parser context for the current thread. ++ private class _CurrentParserContext { ++ var _stack: [XMLParser] = [] ++ var _current: XMLParser? { ++ return _stack.last + } + } + +- @TaskLocal +- private static var _currentParser: _CurrentParser = .default ++ #if os(WASI) ++ /// The current parser associated with the current thread. (assuming no multi-threading) ++ /// FIXME: Unify the implementation with the other platforms once we unlock `threadDictionary` ++ /// or migrate to `FoundationEssentials._ThreadLocal`. ++ private static nonisolated(unsafe) var _currentParserContext: _CurrentParserContext? ++ #else ++ /// The current parser associated with the current thread. ++ private static var _currentParserContext: _CurrentParserContext? { ++ get { ++ return Thread.current.threadDictionary["__CurrentNSXMLParser"] as? _CurrentParserContext ++ } ++ set { ++ Thread.current.threadDictionary["__CurrentNSXMLParser"] = newValue ++ } ++ } ++ #endif + ++ /// The current parser associated with the current thread. + internal static func currentParser() -> XMLParser? { +- return _currentParser.parser ++ if let ctx = _currentParserContext { ++ return ctx._current ++ } ++ return nil + } +- ++ ++ /// Execute the given closure with the current parser set to the given parser. + internal static func withCurrentParser(_ parser: XMLParser, _ body: () -> R) -> R { +- return self.$_currentParser.withValue(_CurrentParser(parser: parser), operation: body) ++ var ctx: _CurrentParserContext ++ if let current = _currentParserContext { ++ // Use the existing context if it exists ++ ctx = current ++ } else { ++ // Create a new context in TLS ++ ctx = _CurrentParserContext() ++ _currentParserContext = ctx ++ } ++ // Push the parser onto the stack ++ ctx._stack.append(parser) ++ defer { ++ // Pop the parser off the stack ++ ctx._stack.removeLast() ++ } ++ return body() + } + + internal func _handleParseResult(_ parseResult: Int32) -> Bool { +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/5063.patch b/schemes/release-6.0/swift-corelibs-foundation/0019-Remove-the-workaround-for-WASI-errno-conflict.patch similarity index 90% rename from schemes/release-6.0/swift-corelibs-foundation/5063.patch rename to schemes/release-6.0/swift-corelibs-foundation/0019-Remove-the-workaround-for-WASI-errno-conflict.patch index ae23a73e..a96c2a96 100644 --- a/schemes/release-6.0/swift-corelibs-foundation/5063.patch +++ b/schemes/release-6.0/swift-corelibs-foundation/0019-Remove-the-workaround-for-WASI-errno-conflict.patch @@ -1,4 +1,4 @@ -From be0e853408a6278fa62f43864d90bf2f53170734 Mon Sep 17 00:00:00 2001 +From 73d12a30cdd15e0a3eebd4be36eed1a0dab7ad2d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 12:26:08 +0000 Subject: [PATCH] Remove the workaround for WASI errno conflict @@ -11,7 +11,7 @@ corelibs-foundation too. 1 file changed, 7 deletions(-) diff --git a/Sources/Foundation/NSPathUtilities.swift b/Sources/Foundation/NSPathUtilities.swift -index f6cda6abf9..408b18bebf 100644 +index f6cda6ab..408b18be 100644 --- a/Sources/Foundation/NSPathUtilities.swift +++ b/Sources/Foundation/NSPathUtilities.swift @@ -12,13 +12,6 @@ @@ -28,3 +28,6 @@ index f6cda6abf9..408b18bebf 100644 #endif #if os(Windows) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-corelibs-foundation/5064.patch b/schemes/release-6.0/swift-corelibs-foundation/0020-Make-curl-an-optional-dependency-when-not-building-F.patch similarity index 90% rename from schemes/release-6.0/swift-corelibs-foundation/5064.patch rename to schemes/release-6.0/swift-corelibs-foundation/0020-Make-curl-an-optional-dependency-when-not-building-F.patch index c7f736b0..e897a163 100644 --- a/schemes/release-6.0/swift-corelibs-foundation/5064.patch +++ b/schemes/release-6.0/swift-corelibs-foundation/0020-Make-curl-an-optional-dependency-when-not-building-F.patch @@ -1,4 +1,4 @@ -From 4127e856e0eda0c1508e60f1c03b4d5c1cbe78db Mon Sep 17 00:00:00 2001 +From f26ad277e5065500641f3572f5af9599acb14bd1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 10 Aug 2024 18:35:56 +0000 Subject: [PATCH] Make curl an optional dependency when not building @@ -13,7 +13,7 @@ dependency when FoundationNetworking is not being built. 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt -index 4b545a381e..1d9c684d94 100644 +index db674738..abecd29b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,14 @@ if(BUILD_SHARED_LIBS) @@ -31,7 +31,7 @@ index 4b545a381e..1d9c684d94 100644 set(CMAKE_POSITION_INDEPENDENT_CODE YES) # Fetchable dependcies -@@ -121,7 +129,9 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) +@@ -142,7 +150,9 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) endif() endif() find_package(LibXml2 REQUIRED) @@ -43,7 +43,7 @@ index 4b545a381e..1d9c684d94 100644 # Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation) list(APPEND _Foundation_common_build_flags diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt -index 29b9244065..f239bdf0ff 100644 +index 29b92440..f239bdf0 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -14,12 +14,12 @@ @@ -61,3 +61,6 @@ index 29b9244065..f239bdf0ff 100644 add_subdirectory(FoundationNetworking) endif() if(FOUNDATION_BUILD_TOOLS) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/781.patch b/schemes/release-6.0/swift-foundation/0001-Use-platform-shims-for-clock-ids-to-support-wasi-lib.patch similarity index 94% rename from schemes/release-6.0/swift-foundation/781.patch rename to schemes/release-6.0/swift-foundation/0001-Use-platform-shims-for-clock-ids-to-support-wasi-lib.patch index 0624a977..dea99c47 100644 --- a/schemes/release-6.0/swift-foundation/781.patch +++ b/schemes/release-6.0/swift-foundation/0001-Use-platform-shims-for-clock-ids-to-support-wasi-lib.patch @@ -1,4 +1,4 @@ -From 4e06bcd6ff381c271fab535449dcd0d1291c83e7 Mon Sep 17 00:00:00 2001 +From 6295398c69d07cf03a49a8505b57f331f64368c0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 26 Jul 2024 07:03:21 +0000 Subject: [PATCH] Use platform shims for clock ids to support wasi-libc @@ -19,7 +19,7 @@ cast them to `UInt64` before doing arithmetic operations on them. create mode 100644 Sources/FoundationEssentials/WASILibc+Extensions.swift diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt -index d9ebc2ebb..836d849ae 100644 +index 6dc5929..617ef2f 100644 --- a/Sources/FoundationEssentials/CMakeLists.txt +++ b/Sources/FoundationEssentials/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(FoundationEssentials @@ -31,10 +31,10 @@ index d9ebc2ebb..836d849ae 100644 add_subdirectory(AttributedString) diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -index 302d3c62d..78b86c266 100644 +index 2e809fa..30591b5 100644 --- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -@@ -129,7 +129,7 @@ final class _ProcessInfo: Sendable { +@@ -131,7 +131,7 @@ final class _ProcessInfo: Sendable { #else var ts: timespec = timespec() clock_gettime(CLOCK_MONOTONIC_RAW, &ts) @@ -45,7 +45,7 @@ index 302d3c62d..78b86c266 100644 let padding = String(repeating: "0", count: 16 - timeString.count) diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift new file mode 100644 -index 000000000..1c05f9958 +index 0000000..1c05f99 --- /dev/null +++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift @@ -0,0 +1,32 @@ @@ -82,7 +82,7 @@ index 000000000..1c05f9958 + +#endif // os(WASI) diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h -index 911fc9e7a..9c7e95925 100644 +index 911fc9e..9c7e959 100644 --- a/Sources/_FoundationCShims/include/platform_shims.h +++ b/Sources/_FoundationCShims/include/platform_shims.h @@ -68,4 +68,17 @@ typedef enum { @@ -103,3 +103,6 @@ index 911fc9e7a..9c7e95925 100644 +#endif + #endif /* CSHIMS_PLATFORM_SHIMS */ +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0002-Add-explicit-include-of-wasi-libc-environ.h-786.patch b/schemes/release-6.0/swift-foundation/0002-Add-explicit-include-of-wasi-libc-environ.h-786.patch new file mode 100644 index 00000000..b6c57c58 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0002-Add-explicit-include-of-wasi-libc-environ.h-786.patch @@ -0,0 +1,31 @@ +From 32526a30ecfb17d64f60dcf4658e2c5f8b745628 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 00:50:27 +0900 +Subject: [PATCH] Add explicit include of `wasi/libc-environ.h` (#786) + +This is necessary to get the `__wasilibc_get_environ` function +declaration. + +(cherry picked from commit 243066f12d0b6f1ab8ad9fefd8526b2383641892) +--- + Sources/_FoundationCShims/platform_shims.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c +index 5a400a4..6f79224 100644 +--- a/Sources/_FoundationCShims/platform_shims.c ++++ b/Sources/_FoundationCShims/platform_shims.c +@@ -21,6 +21,10 @@ + extern char **environ; + #endif + ++#if __wasi__ ++#include // for __wasilibc_get_environ ++#endif ++ + #if __has_include() + #import + void _platform_shims_lock_environ() { +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0003-Add-explicit-void-type-parameter-to-C-functions-with.patch b/schemes/release-6.0/swift-foundation/0003-Add-explicit-void-type-parameter-to-C-functions-with.patch new file mode 100644 index 00000000..8e185d42 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0003-Add-explicit-void-type-parameter-to-C-functions-with.patch @@ -0,0 +1,109 @@ +From a415338a471ec82fcd6fab4e4905b927f5165cc3 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:02:11 +0900 +Subject: [PATCH] Add explicit void type parameter to C functions without + parameters (#775) + +C functions with `()` as parameter list can take any number of +parameters. But WebAssembly requires static signature information for +every function call, so we need to explicitly specify `(void)` to +indicate that the function takes no parameters. +--- + .../_FoundationCShims/include/platform_shims.h | 12 ++++++------ + Sources/_FoundationCShims/platform_shims.c | 16 ++++++++-------- + 2 files changed, 14 insertions(+), 14 deletions(-) + +diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h +index 9c7e959..f45f5fd 100644 +--- a/Sources/_FoundationCShims/include/platform_shims.h ++++ b/Sources/_FoundationCShims/include/platform_shims.h +@@ -31,19 +31,19 @@ + #include + #endif + +-INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(); ++INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(void); + +-INTERNAL void _platform_shims_lock_environ(); +-INTERNAL void _platform_shims_unlock_environ(); ++INTERNAL void _platform_shims_lock_environ(void); ++INTERNAL void _platform_shims_unlock_environ(void); + + #if __has_include() + #include +-INTERNAL vm_size_t _platform_shims_vm_size(); ++INTERNAL vm_size_t _platform_shims_vm_size(void); + #endif + + #if __has_include() + #include +-INTERNAL mach_port_t _platform_mach_task_self(); ++INTERNAL mach_port_t _platform_mach_task_self(void); + #endif + + #if __has_include() +@@ -65,7 +65,7 @@ typedef enum { + } _platform_shims_OSThermalPressureLevel; + + +-INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); ++INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(void); + #endif + + #if TARGET_OS_WASI +diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c +index 6f79224..556bc94 100644 +--- a/Sources/_FoundationCShims/platform_shims.c ++++ b/Sources/_FoundationCShims/platform_shims.c +@@ -27,19 +27,19 @@ extern char **environ; + + #if __has_include() + #import +-void _platform_shims_lock_environ() { ++void _platform_shims_lock_environ(void) { + environ_lock_np(); + } + +-void _platform_shims_unlock_environ() { ++void _platform_shims_unlock_environ(void) { + environ_unlock_np(); + } + #else +-void _platform_shims_lock_environ() { /* noop */ } +-void _platform_shims_unlock_environ() { /* noop */ } ++void _platform_shims_lock_environ(void) { /* noop */ } ++void _platform_shims_unlock_environ(void) { /* noop */ } + #endif + +-char ** _platform_shims_get_environ() { ++char ** _platform_shims_get_environ(void) { + #if __has_include() + return *_NSGetEnviron(); + #elif defined(_WIN32) +@@ -52,20 +52,20 @@ char ** _platform_shims_get_environ() { + } + + #if __has_include() +-const char * _platform_shims_kOSThermalNotificationPressureLevelName() { ++const char * _platform_shims_kOSThermalNotificationPressureLevelName(void) { + return kOSThermalNotificationPressureLevelName; + } + #endif + + #if __has_include() +-vm_size_t _platform_shims_vm_size() { ++vm_size_t _platform_shims_vm_size(void) { + // This shim exists because vm_page_size is not marked const, and therefore looks like global mutable state to Swift. + return vm_page_size; + } + #endif + + #if __has_include() +-mach_port_t _platform_mach_task_self() { ++mach_port_t _platform_mach_task_self(void) { + // This shim exists because mach_task_self_ is not marked const, and therefore looks like global mutable state to Swift. + return mach_task_self(); + } +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/835.patch b/schemes/release-6.0/swift-foundation/0004-wasm-Fall-back-to-a-default-chunk-size-when-st_blksi.patch similarity index 88% rename from schemes/release-6.0/swift-foundation/835.patch rename to schemes/release-6.0/swift-foundation/0004-wasm-Fall-back-to-a-default-chunk-size-when-st_blksi.patch index 23101fd5..f1819565 100644 --- a/schemes/release-6.0/swift-foundation/835.patch +++ b/schemes/release-6.0/swift-foundation/0004-wasm-Fall-back-to-a-default-chunk-size-when-st_blksi.patch @@ -1,4 +1,4 @@ -From abd2b40e2553a3ce05a1ab4db4ee96bd29d26ecc Mon Sep 17 00:00:00 2001 +From 40f92f2c616d48cc11f561c3250baa9ef34555ad Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 8 Aug 2024 18:31:56 +0000 Subject: [PATCH] [wasm] Fall back to a default chunk size when `st_blksize` is @@ -11,10 +11,10 @@ fall back to a default chunk size 4KB, which is a common page size. 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index d3fc7b2d5..d93468554 100644 +index 03adcc6..71ef113 100644 --- a/Sources/FoundationEssentials/FileManager/FileOperations.swift +++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift -@@ -872,7 +872,16 @@ enum _FileOperations { +@@ -879,7 +879,16 @@ enum _FileOperations { } let total: Int = Int(fileInfo.st_size) @@ -31,4 +31,7 @@ index d3fc7b2d5..d93468554 100644 + } var current: off_t = 0 - #if os(WASI) + while current < total { +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/836.patch b/schemes/release-6.0/swift-foundation/0005-Port-directory-enumeration-related-code-to-WASI.patch similarity index 80% rename from schemes/release-6.0/swift-foundation/836.patch rename to schemes/release-6.0/swift-foundation/0005-Port-directory-enumeration-related-code-to-WASI.patch index 67ea838a..8a0bea88 100644 --- a/schemes/release-6.0/swift-foundation/836.patch +++ b/schemes/release-6.0/swift-foundation/0005-Port-directory-enumeration-related-code-to-WASI.patch @@ -1,7 +1,7 @@ -From 08af69e0991f1ae8b3a68c8144fab987987d8e63 Mon Sep 17 00:00:00 2001 +From 036e01c531fc050b44da00253201eef5bfd55d16 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 26 Jul 2024 07:41:19 +0000 -Subject: [PATCH 1/2] Port directory enumeration related code to WASI +Subject: [PATCH] Port directory enumeration related code to WASI For now wasi-libc does not include fts(3) implementation, so mark features depending on it as unsupported on WASI. Once wasi-libc includes @@ -23,7 +23,7 @@ function call form. 5 files changed, 113 insertions(+), 3 deletions(-) diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift -index a29b31195..bcbb5e998 100644 +index 0941e51..ed1b445 100644 --- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift @@ -193,6 +193,9 @@ extension _FileManagerImpl { @@ -37,12 +37,12 @@ index a29b31195..bcbb5e998 100644 return try path.withFileSystemRepresentation { fileSystemRep in guard let fileSystemRep else { diff --git a/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift b/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift -index 22111f52c..904f5fb75 100644 +index 2c9a02f..aae9824 100644 --- a/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift +++ b/Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift -@@ -115,10 +115,16 @@ import posix_filesystem.dirent - #elseif canImport(Glibc) - import Glibc +@@ -118,10 +118,16 @@ internal import _FoundationCShims + #elseif canImport(Musl) + import Musl internal import _FoundationCShims +#elseif os(WASI) +import WASILibc @@ -57,7 +57,7 @@ index 22111f52c..904f5fb75 100644 struct _FTSSequence: Sequence { enum Element { struct SwiftFTSENT { -@@ -315,10 +321,12 @@ extension Sequence<_FTSSequence.Element> { +@@ -318,10 +324,12 @@ extension Sequence<_FTSSequence.Element> { } } @@ -66,12 +66,12 @@ index 22111f52c..904f5fb75 100644 struct _POSIXDirectoryContentsSequence: Sequence { #if canImport(Darwin) typealias DirectoryEntryPtr = UnsafeMutablePointer -- #elseif os(Android) || canImport(Glibc) -+ #elseif os(Android) || canImport(Glibc) || os(WASI) +- #elseif os(Android) || canImport(Glibc) || canImport(Musl) ++ #elseif os(Android) || canImport(Glibc) || canImport(Musl) || os(WASI) typealias DirectoryEntryPtr = OpaquePointer #endif -@@ -343,10 +351,18 @@ struct _POSIXDirectoryContentsSequence: Sequence { +@@ -346,10 +354,18 @@ struct _POSIXDirectoryContentsSequence: Sequence { continue } // Use name @@ -92,7 +92,7 @@ index 22111f52c..904f5fb75 100644 if fileName == "." || fileName == ".." || fileName == "._" { continue diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index d3fc7b2d5..dc51ba55b 100644 +index 71ef113..528a7c9 100644 --- a/Sources/FoundationEssentials/FileManager/FileOperations.swift +++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift @@ -512,7 +512,14 @@ enum _FileOperations { @@ -119,7 +119,7 @@ index d3fc7b2d5..dc51ba55b 100644 } #endif -@@ -903,6 +911,44 @@ enum _FileOperations { +@@ -900,6 +908,44 @@ enum _FileOperations { } #endif @@ -164,7 +164,7 @@ index d3fc7b2d5..dc51ba55b 100644 private static func _linkOrCopyFile(_ srcPtr: UnsafePointer, _ dstPtr: UnsafePointer, with fileManager: FileManager, delegate: some LinkOrCopyDelegate) throws { try withUnsafeTemporaryAllocation(of: CChar.self, capacity: FileManager.MAX_PATH_SIZE) { buffer in let dstLen = Platform.copyCString(dst: buffer.baseAddress!, src: dstPtr, size: FileManager.MAX_PATH_SIZE) -@@ -1015,6 +1061,7 @@ enum _FileOperations { +@@ -1012,6 +1058,7 @@ enum _FileOperations { } } } @@ -173,7 +173,7 @@ index d3fc7b2d5..dc51ba55b 100644 private static func linkOrCopyFile(_ src: String, dst: String, with fileManager: FileManager, delegate: some LinkOrCopyDelegate) throws { try src.withFileSystemRepresentation { srcPtr in diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift -index 1c05f9958..351fe19f2 100644 +index 1c05f99..351fe19 100644 --- a/Sources/FoundationEssentials/WASILibc+Extensions.swift +++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift @@ -29,4 +29,25 @@ internal var CLOCK_MONOTONIC_RAW: clockid_t { @@ -203,7 +203,7 @@ index 1c05f9958..351fe19f2 100644 + #endif // os(WASI) diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h -index f45f5fd55..6bc0a0e15 100644 +index f45f5fd..6bc0a0e 100644 --- a/Sources/_FoundationCShims/include/platform_shims.h +++ b/Sources/_FoundationCShims/include/platform_shims.h @@ -79,6 +79,29 @@ static inline _Nonnull clockid_t _platform_shims_clock_monotonic(void) { @@ -236,36 +236,6 @@ index f45f5fd55..6bc0a0e15 100644 #endif #endif /* CSHIMS_PLATFORM_SHIMS */ +-- +2.43.2 -From 36fed41f7b5b91e4733b9120c144309aba121c2d Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Mon, 12 Aug 2024 17:32:17 +0000 -Subject: [PATCH 2/2] Delegate error throwing decisions to the delegate - ---- - .../FileManager/FileOperations.swift | 10 ++++++++-- - 1 file changed, 8 insertions(+), 2 deletions(-) - -diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index dc51ba55b..3da9c5022 100644 ---- a/Sources/FoundationEssentials/FileManager/FileOperations.swift -+++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift -@@ -918,10 +918,16 @@ enum _FileOperations { - guard delegate.shouldPerformOnItemAtPath(src, to: dst) else { return } - - var stat = stat() -- guard lstat(srcPtr, &stat) == 0, !stat.isDirectory else { -+ guard lstat(srcPtr, &stat) == 0 else { -+ try delegate.throwIfNecessary(errno, src, dst) -+ return -+ } -+ guard !stat.isDirectory else { - // wasi-libc does not support FTS for now, so we don't support copying/linking - // directories on WASI for now. -- throw CocoaError.errorWithFilePath(.featureUnsupported, String(cString: srcPtr)) -+ let error = CocoaError.fileOperationError(.featureUnsupported, src, dst) -+ try delegate.throwIfNecessary(error, src, dst) -+ return - } - - // For now, we support only copying regular files and symlinks. diff --git a/schemes/release-6.0/swift-foundation/0006-Enable-wasi-libc-emulation-features-777.patch b/schemes/release-6.0/swift-foundation/0006-Enable-wasi-libc-emulation-features-777.patch new file mode 100644 index 00000000..652c5929 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0006-Enable-wasi-libc-emulation-features-777.patch @@ -0,0 +1,166 @@ +From f2e4534e75a972a96b802dc682e870f142b72fe9 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Tue, 6 Aug 2024 02:20:54 +0900 +Subject: [PATCH] Enable wasi-libc emulation features (#777) + +* Enable wasi-libc emulation features + +Those features require explicit macro definitions to be enabled, so add +them to the package definition. Only affects WASI builds. + +* Prefer `TARGET_OS_WASI` over `__wasi__` + +And explain why we need definition checks for `signal.h` and `sys/mman.h` +--- + CMakeLists.txt | 8 +++++ + Package.swift | 35 ++++++++++++++----- + Sources/FoundationEssentials/CMakeLists.txt | 1 + + .../CMakeLists.txt | 1 + + Sources/_FoundationCShims/include/_CStdlib.h | 16 ++++++++- + 5 files changed, 51 insertions(+), 10 deletions(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index a8dc410..3243e53 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -116,6 +116,14 @@ foreach(version ${_SwiftFoundation_versions}) + endforeach() + endforeach() + ++# wasi-libc emulation feature flags ++set(_SwiftFoundation_wasi_libc_flags) ++if(CMAKE_SYSTEM_NAME STREQUAL "WASI") ++ list(APPEND _SwiftFoundation_wasi_libc_flags ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") ++endif() ++ + include(GNUInstallDirs) + include(SwiftFoundationSwiftSupport) + +diff --git a/Package.swift b/Package.swift +index daaf638..67bce50 100644 +--- a/Package.swift ++++ b/Package.swift +@@ -70,6 +70,11 @@ var dependencies: [Package.Dependency] { + } + } + ++let wasiLibcCSettings: [CSetting] = [ ++ .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), ++ .define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])), ++] ++ + let package = Package( + name: "FoundationPreview", + platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")], +@@ -91,15 +96,23 @@ let package = Package( + path: "Sources/Foundation"), + + // _FoundationCShims (Internal) +- .target(name: "_FoundationCShims", +- cSettings: [.define("_CRT_SECURE_NO_WARNINGS", +- .when(platforms: [.windows]))]), ++ .target( ++ name: "_FoundationCShims", ++ cSettings: [ ++ .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])) ++ ] + wasiLibcCSettings ++ ), + + // TestSupport (Internal) +- .target(name: "TestSupport", dependencies: [ +- "FoundationEssentials", +- "FoundationInternationalization", +- ], swiftSettings: availabilityMacros + concurrencyChecking), ++ .target( ++ name: "TestSupport", ++ dependencies: [ ++ "FoundationEssentials", ++ "FoundationInternationalization", ++ ], ++ cSettings: wasiLibcCSettings, ++ swiftSettings: availabilityMacros + concurrencyChecking ++ ), + + // FoundationEssentials + .target( +@@ -130,11 +143,14 @@ let package = Package( + ], + cSettings: [ + .define("_GNU_SOURCE", .when(platforms: [.linux])) +- ], ++ ] + wasiLibcCSettings, + swiftSettings: [ + .enableExperimentalFeature("VariadicGenerics"), + .enableExperimentalFeature("AccessLevelOnImport") +- ] + availabilityMacros + concurrencyChecking ++ ] + availabilityMacros + concurrencyChecking, ++ linkerSettings: [ ++ .linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])), ++ ] + ), + .testTarget( + name: "FoundationEssentialsTests", +@@ -166,6 +182,7 @@ let package = Package( + "CMakeLists.txt", + "Predicate/CMakeLists.txt" + ], ++ cSettings: wasiLibcCSettings, + swiftSettings: [ + .enableExperimentalFeature("AccessLevelOnImport") + ] + availabilityMacros + concurrencyChecking +diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt +index 617ef2f..98c419a 100644 +--- a/Sources/FoundationEssentials/CMakeLists.txt ++++ b/Sources/FoundationEssentials/CMakeLists.txt +@@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE + "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" + "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") + target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros}) ++target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc_flags}) + target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation") + + target_link_libraries(FoundationEssentials PUBLIC +diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt +index 5a89ceb..857db9c 100644 +--- a/Sources/FoundationInternationalization/CMakeLists.txt ++++ b/Sources/FoundationInternationalization/CMakeLists.txt +@@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE + "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" + "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") + target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros}) ++target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags}) + target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation") + + target_link_libraries(FoundationInternationalization PUBLIC +diff --git a/Sources/_FoundationCShims/include/_CStdlib.h b/Sources/_FoundationCShims/include/_CStdlib.h +index 8967eb7..0337393 100644 +--- a/Sources/_FoundationCShims/include/_CStdlib.h ++++ b/Sources/_FoundationCShims/include/_CStdlib.h +@@ -60,7 +60,21 @@ + #endif + + #if __has_include() +-#include ++/// Guard against including `signal.h` on WASI. The `signal.h` header file ++/// itself is available in wasi-libc, but it's just a stub that doesn't actually ++/// do anything. And also including it requires a special macro definition ++/// (`_WASI_EMULATED_SIGNAL`) and it causes compilation errors without the macro. ++# if !TARGET_OS_WASI || defined(_WASI_EMULATED_SIGNAL) ++# include ++# endif ++#endif ++ ++#if __has_include() ++/// Similar to `signal.h`, guard against including `sys/mman.h` on WASI unless ++/// `_WASI_EMULATED_MMAN` is enabled. ++# if !TARGET_OS_WASI || defined(_WASI_EMULATED_MMAN) ++# include ++# endif + #endif + + #if __has_include() +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0007-Autolink-wasi-libc-emulation-libraries.patch b/schemes/release-6.0/swift-foundation/0007-Autolink-wasi-libc-emulation-libraries.patch new file mode 100644 index 00000000..1bd98bd6 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0007-Autolink-wasi-libc-emulation-libraries.patch @@ -0,0 +1,35 @@ +From 30fd04588f22951c10c6904e4f2f8752988e3156 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 16:13:28 +0000 +Subject: [PATCH] Autolink wasi-libc emulation libraries + +`_WASI_EMULATED_XXX` macros require linking against the corresponding +emulation libraries. This patch adds `-Xfrontend -public-autolink-library` +flags to the Swift compiler invocation to automatically link against the +emulation libraries. Also enable getpid emulation explicitly, as it is +available by default but emiting a warning. +--- + CMakeLists.txt | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 3243e53..bd9854e 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -120,8 +120,12 @@ endforeach() + set(_SwiftFoundation_wasi_libc_flags) + if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _SwiftFoundation_wasi_libc_flags ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" + "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" +- "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" ++ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" ++ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") + endif() + + include(GNUInstallDirs) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/825.patch b/schemes/release-6.0/swift-foundation/825.patch deleted file mode 100644 index 1bae05b9..00000000 --- a/schemes/release-6.0/swift-foundation/825.patch +++ /dev/null @@ -1,1150 +0,0 @@ -From cfb5a0220a2c859e8f2f58d52de98fc2171e3cfb Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 00:50:27 +0900 -Subject: [PATCH 01/12] Add explicit include of `wasi/libc-environ.h` (#786) - -This is necessary to get the `__wasilibc_get_environ` function -declaration. - -(cherry picked from commit 243066f12d0b6f1ab8ad9fefd8526b2383641892) ---- - Sources/_FoundationCShims/platform_shims.c | 4 ++++ - 1 file changed, 4 insertions(+) - -diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c -index 5a400a468..6f792241c 100644 ---- a/Sources/_FoundationCShims/platform_shims.c -+++ b/Sources/_FoundationCShims/platform_shims.c -@@ -21,6 +21,10 @@ - extern char **environ; - #endif - -+#if __wasi__ -+#include // for __wasilibc_get_environ -+#endif -+ - #if __has_include() - #import - void _platform_shims_lock_environ() { - -From 189cfd856357620038849645f5ca9ba077837266 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:02:11 +0900 -Subject: [PATCH 02/12] Add explicit void type parameter to C functions without - parameters (#775) - -C functions with `()` as parameter list can take any number of -parameters. But WebAssembly requires static signature information for -every function call, so we need to explicitly specify `(void)` to -indicate that the function takes no parameters. - -(cherry picked from commit 8f34f38f30858f7b9dfa9a40b07c18fd7a7b93ae) ---- - .../_FoundationCShims/include/platform_shims.h | 12 ++++++------ - Sources/_FoundationCShims/platform_shims.c | 16 ++++++++-------- - 2 files changed, 14 insertions(+), 14 deletions(-) - -diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h -index 911fc9e7a..f8048e678 100644 ---- a/Sources/_FoundationCShims/include/platform_shims.h -+++ b/Sources/_FoundationCShims/include/platform_shims.h -@@ -31,19 +31,19 @@ - #include - #endif - --INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(); -+INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ(void); - --INTERNAL void _platform_shims_lock_environ(); --INTERNAL void _platform_shims_unlock_environ(); -+INTERNAL void _platform_shims_lock_environ(void); -+INTERNAL void _platform_shims_unlock_environ(void); - - #if __has_include() - #include --INTERNAL vm_size_t _platform_shims_vm_size(); -+INTERNAL vm_size_t _platform_shims_vm_size(void); - #endif - - #if __has_include() - #include --INTERNAL mach_port_t _platform_mach_task_self(); -+INTERNAL mach_port_t _platform_mach_task_self(void); - #endif - - #if __has_include() -@@ -65,7 +65,7 @@ typedef enum { - } _platform_shims_OSThermalPressureLevel; - - --INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(); -+INTERNAL const char * _Nonnull _platform_shims_kOSThermalNotificationPressureLevelName(void); - #endif - - #endif /* CSHIMS_PLATFORM_SHIMS */ -diff --git a/Sources/_FoundationCShims/platform_shims.c b/Sources/_FoundationCShims/platform_shims.c -index 6f792241c..556bc9416 100644 ---- a/Sources/_FoundationCShims/platform_shims.c -+++ b/Sources/_FoundationCShims/platform_shims.c -@@ -27,19 +27,19 @@ extern char **environ; - - #if __has_include() - #import --void _platform_shims_lock_environ() { -+void _platform_shims_lock_environ(void) { - environ_lock_np(); - } - --void _platform_shims_unlock_environ() { -+void _platform_shims_unlock_environ(void) { - environ_unlock_np(); - } - #else --void _platform_shims_lock_environ() { /* noop */ } --void _platform_shims_unlock_environ() { /* noop */ } -+void _platform_shims_lock_environ(void) { /* noop */ } -+void _platform_shims_unlock_environ(void) { /* noop */ } - #endif - --char ** _platform_shims_get_environ() { -+char ** _platform_shims_get_environ(void) { - #if __has_include() - return *_NSGetEnviron(); - #elif defined(_WIN32) -@@ -52,20 +52,20 @@ char ** _platform_shims_get_environ() { - } - - #if __has_include() --const char * _platform_shims_kOSThermalNotificationPressureLevelName() { -+const char * _platform_shims_kOSThermalNotificationPressureLevelName(void) { - return kOSThermalNotificationPressureLevelName; - } - #endif - - #if __has_include() --vm_size_t _platform_shims_vm_size() { -+vm_size_t _platform_shims_vm_size(void) { - // This shim exists because vm_page_size is not marked const, and therefore looks like global mutable state to Swift. - return vm_page_size; - } - #endif - - #if __has_include() --mach_port_t _platform_mach_task_self() { -+mach_port_t _platform_mach_task_self(void) { - // This shim exists because mach_task_self_ is not marked const, and therefore looks like global mutable state to Swift. - return mach_task_self(); - } - -From 9f3ab600d3062e8de6c79d306449faad8a194449 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:06:57 +0900 -Subject: [PATCH 03/12] Exclude EREMOTE definition for WASI platform (#778) - -WASI does not define the EREMOTE error code. - -(cherry picked from commit 6bb5ff7b29ed65a722119057d406ffd4bdcdf1b9) ---- - Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift | 2 ++ - 1 file changed, 2 insertions(+) - -diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -index a7e01952c..d6977ba47 100644 ---- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -+++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -@@ -465,11 +465,13 @@ extension POSIXError { - return .ESTALE - } - -+ #if !os(WASI) - /// Too many levels of remote in path. - public static var EREMOTE: POSIXErrorCode { - return .EREMOTE - } - #endif -+ #endif - - #if canImport(Darwin) - /// RPC struct is bad. - -From 9c6d53cb0b49bb7b79eda03b7dcd4aacee064118 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:12:39 +0900 -Subject: [PATCH 04/12] Throw `.featureUnsupported` when attempting to create - temp files on WASI (#779) - -WASI does not have temp directory concept, and does not provide mktemp -family of functions, so attempting to create a temporary file should be -considered a feature unsupported. - -(cherry picked from commit fb11420dc6f546e67fce249c15bb17e208c40aff) ---- - Sources/FoundationEssentials/Data/Data+Writing.swift | 5 +++++ - 1 file changed, 5 insertions(+) - -diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift -index 4f92cb1a6..44cf53d64 100644 ---- a/Sources/FoundationEssentials/Data/Data+Writing.swift -+++ b/Sources/FoundationEssentials/Data/Data+Writing.swift -@@ -127,6 +127,10 @@ private func cleanupTemporaryDirectory(at inPath: String?) { - - /// Caller is responsible for calling `close` on the `Int32` file descriptor. - private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, prefix: String, options: Data.WritingOptions) throws -> (Int32, String) { -+#if os(WASI) -+ // WASI does not have temp directories -+ throw CocoaError(.featureUnsupported) -+#else - var directoryPath = destinationPath - if !directoryPath.isEmpty && directoryPath.last! != "/" { - directoryPath.append("/") -@@ -181,6 +185,7 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, - } - } - } while true -+#endif // os(WASI) - } - - /// Returns `(file descriptor, temporary file path, temporary directory path)` - -From 1b8c399cd90a8d6e960a18c5edfda427ec2ba461 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:15:23 +0900 -Subject: [PATCH 05/12] Fix `operatingSystemVersion` on WASI (#782) - -The `operatingSystemVersion` property type is a tuple but the it was -returning an `OperatingSystemVersion` instance on unknown platforms. - -(cherry picked from commit a8f12255dbf98e0899af84d0e674ab71be30bd85) ---- - Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -index 302d3c62d..a3e6b6c60 100644 ---- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -+++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -@@ -389,7 +389,7 @@ extension _ProcessInfo { - patch: Int(osVersionInfo.dwBuildNumber) - ) - #else -- return OperatingSystemVersion(majorVersion: -1, minorVersion: 0, patchVersion: 0) -+ return (major: -1, minor: 0, patch: 0) - #endif - } - - -From 0f1c377bc3935e2060848f234a20ac89ef085040 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:20:05 +0900 -Subject: [PATCH 06/12] Guard out extended or fs attributes related code on - WASI (#784) - -This commit guards out the extended attributes and file system -attributes related code on WASI as WASI does not support these -features. Just return nothing or ignore the set request. - -(cherry picked from commit fab7195ea2503d296ce80ff6bc5cdb6da8c71b9c) ---- - .../FileManager/FileManager+Files.swift | 13 +++++++++++-- - .../FileManager/FileManager+Utilities.swift | 2 +- - 2 files changed, 12 insertions(+), 3 deletions(-) - -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -index 72e05f937..cf5cb79a4 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -@@ -484,7 +484,7 @@ extension _FileManagerImpl { - #endif - } - --#if !os(Windows) -+#if !os(Windows) && !os(WASI) - private func _extendedAttribute(_ key: UnsafePointer, at path: UnsafePointer, followSymlinks: Bool) throws -> Data? { - #if canImport(Darwin) - var size = getxattr(path, key, nil, 0, 0, followSymlinks ? 0 : XATTR_NOFOLLOW) -@@ -638,10 +638,11 @@ extension _FileManagerImpl { - - var attributes = statAtPath.fileAttributes - try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes) -- -+ #if !os(WASI) // WASI does not support extended attributes - if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) { - attributes[._extendedAttributes] = extendedAttrs - } -+ #endif - - #if !targetEnvironment(simulator) && FOUNDATION_FRAMEWORK - if statAtPath.isRegular || statAtPath.isDirectory { -@@ -703,6 +704,9 @@ extension _FileManagerImpl { - ] - } - } -+#elseif os(WASI) -+ // WASI does not support file system attributes -+ return [:] - #else - try fileManager.withFileSystemRepresentation(for: path) { rep in - guard let rep else { -@@ -930,7 +934,12 @@ extension _FileManagerImpl { - try Self._setCatInfoAttributes(attributes, path: path) - - if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] { -+ #if os(WASI) -+ // WASI does not support extended attributes -+ throw CocoaError.errorWithFilePath(.featureUnsupported, path) -+ #else - try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false) -+ #endif - } - - if let date = attributes[.modificationDate] as? Date { -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -index bba8ed5c9..eb430baaf 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -@@ -173,7 +173,7 @@ extension _FileManagerImpl { - #endif - } - --#if !os(Windows) -+#if !os(Windows) && !os(WASI) - static func _setAttribute(_ key: UnsafePointer, value: Data, at path: UnsafePointer, followSymLinks: Bool) throws { - try value.withUnsafeBytes { buffer in - #if canImport(Darwin) - -From 0e3174f872f9d1a48daf6aa9e6f80adda77e16f3 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:23:13 +0900 -Subject: [PATCH 07/12] Guard out user/group related code on WASI (#783) - -* Guard out user/group related code on WASI - -This change guards out the user/group related code on WASI, as WASI does -not have the concept of users or groups. - -* Throw explicit unsupported error if trying to set user or group on WASI - -Instead of implicitly ignoring user-given values, we should throw -exception to make it clear that those values cannot be set on WASI. - -(cherry picked from commit 0b3974d35103fd1e3e5213f2cdcefc1fd7fa84f4) ---- - Sources/FoundationEssentials/Data/Data+Writing.swift | 2 ++ - .../FoundationEssentials/FileManager/FileManager+Files.swift | 5 +++++ - .../FileManager/FileManager+Utilities.swift | 2 +- - .../FoundationEssentials/FileManager/FileOperations.swift | 2 ++ - Sources/FoundationEssentials/Platform.swift | 4 ++-- - Sources/FoundationEssentials/String/String+Path.swift | 2 ++ - 6 files changed, 14 insertions(+), 3 deletions(-) - -diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift -index 44cf53d64..c86f27bc3 100644 ---- a/Sources/FoundationEssentials/Data/Data+Writing.swift -+++ b/Sources/FoundationEssentials/Data/Data+Writing.swift -@@ -519,6 +519,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint - - cleanupTemporaryDirectory(at: temporaryDirectoryPath) - -+#if !os(WASI) // WASI does not support fchmod for now - if let mode { - // Try to change the mode if the path has not changed. Do our best, but don't report an error. - #if FOUNDATION_FRAMEWORK -@@ -542,6 +543,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint - fchmod(fd, mode) - #endif - } -+#endif // os(WASI) - } - } - } -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -index cf5cb79a4..acf7d3c51 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -@@ -922,6 +922,10 @@ extension _FileManagerImpl { - let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self) - - if user != nil || userID != nil || group != nil || groupID != nil { -+ #if os(WASI) -+ // WASI does not have the concept of users or groups -+ throw CocoaError.errorWithFilePath(.featureUnsupported, path) -+ #else - // Bias toward userID & groupID - try to prevent round trips to getpwnam if possible. - var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) } - let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Self._userAccountNameToNumber) ?? leaveUnchanged -@@ -929,6 +933,7 @@ extension _FileManagerImpl { - if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 { - throw CocoaError.errorWithFilePath(path, errno: errno, reading: false) - } -+ #endif - } - - try Self._setCatInfoAttributes(attributes, path: path) -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -index eb430baaf..45f498937 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -@@ -271,7 +271,7 @@ extension _FileManagerImpl { - } - #endif - --#if !os(Windows) -+#if !os(Windows) && !os(WASI) - static func _userAccountNameToNumber(_ name: String) -> uid_t? { - name.withCString { ptr in - getpwnam(ptr)?.pointee.pw_uid -diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index 8e67a771f..130a07ba4 100644 ---- a/Sources/FoundationEssentials/FileManager/FileOperations.swift -+++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift -@@ -855,12 +855,14 @@ enum _FileOperations { - } - defer { close(dstfd) } - -+ #if !os(WASI) // WASI doesn't have fchmod for now - // Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues - let permissions = fileInfo.st_mode & ~S_IFMT - guard fchmod(dstfd, permissions) == 0 else { - try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) - return - } -+ #endif - - if fileInfo.st_size == 0 { - // no copying required -diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift -index 20dc561af..bf2e652d3 100644 ---- a/Sources/FoundationEssentials/Platform.swift -+++ b/Sources/FoundationEssentials/Platform.swift -@@ -113,7 +113,7 @@ private let _cachedUGIDs: (uid_t, gid_t) = { - }() - #endif - --#if !os(Windows) -+#if !os(Windows) && !os(WASI) - extension Platform { - private static var ROOT_USER: UInt32 { 0 } - static func getUGIDs(allowEffectiveRootUID: Bool = true) -> (uid: UInt32, gid: UInt32) { -@@ -174,7 +174,7 @@ extension Platform { - // FIXME: bionic implements this as `return 0;` and does not expose the - // function via headers. We should be able to shim this and use the call - // if it is available. --#if !os(Android) -+#if !os(Android) && !os(WASI) - guard issetugid() == 0 else { return nil } - #endif - if let value = getenv(name) { -diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift -index 79bcbfa76..5e42aef4c 100644 ---- a/Sources/FoundationEssentials/String/String+Path.swift -+++ b/Sources/FoundationEssentials/String/String+Path.swift -@@ -450,6 +450,7 @@ extension String { - return envVar.standardizingPath - } - -+ #if !os(WASI) // WASI does not have user concept - // Next, attempt to find the home directory via getpwnam/getpwuid - var pass: UnsafeMutablePointer? - if let user { -@@ -463,6 +464,7 @@ extension String { - if let dir = pass?.pointee.pw_dir { - return String(cString: dir).standardizingPath - } -+ #endif - - // Fallback to HOME for the current user if possible - if user == nil, let home = getenv("HOME") { - -From 05fc5ebc4c0b92438498ce8a4e38270761b0545f Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 01:30:17 +0900 -Subject: [PATCH 08/12] Skip sticky-bit check in `isDeletableFile` on WASI - (#785) - -WASI does not surface the sticky bit and getuid, so we cannot check -whether the file is actually deletable before attempting to delete it. - -(cherry picked from commit e90b6c3f90e52f840fc087b05468f430eae1d05a) ---- - .../FoundationEssentials/FileManager/FileManager+Files.swift | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -index acf7d3c51..99f1b32c6 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -@@ -461,7 +461,7 @@ extension _FileManagerImpl { - parent = fileManager.currentDirectoryPath - } - --#if os(Windows) -+#if os(Windows) || os(WASI) - return fileManager.isWritableFile(atPath: parent) && fileManager.isWritableFile(atPath: path) - #else - guard fileManager.isWritableFile(atPath: parent), - -From 779e11a4c35584a7a5e43fbc4ca88bfd8a570aca Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 02:50:04 +0900 -Subject: [PATCH 09/12] Implement `_copyRegularFile` for WASI without - `sendfile` (#787) - -WASI doesn't have `sendfile`, so we need to implement the copy in user -space with `read` and `write`. It's not as efficient as `sendfile`, but -it's the best we can do. - -(cherry picked from commit 2a6afeb50aecc5fdfaa7b739399afff1eca024d1) ---- - .../FileManager/FileOperations.swift | 19 +++++++++++++++++++ - 1 file changed, 19 insertions(+) - -diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index 130a07ba4..51f4f7ea4 100644 ---- a/Sources/FoundationEssentials/FileManager/FileOperations.swift -+++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift -@@ -873,12 +873,31 @@ enum _FileOperations { - let chunkSize: Int = Int(fileInfo.st_blksize) - var current: off_t = 0 - -+ #if os(WASI) -+ // WASI doesn't have sendfile, so we need to do it in user space with read/write -+ try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: chunkSize) { buffer in -+ while current < total { -+ let readSize = Swift.min(total - Int(current), chunkSize) -+ let bytesRead = read(srcfd, buffer.baseAddress, readSize) -+ guard bytesRead >= 0 else { -+ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) -+ return -+ } -+ guard write(dstfd, buffer.baseAddress, bytesRead) == bytesRead else { -+ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) -+ return -+ } -+ current += off_t(bytesRead) -+ } -+ } -+ #else - while current < total { - guard sendfile(dstfd, srcfd, ¤t, Swift.min(total - Int(current), chunkSize)) != -1 else { - try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) - return - } - } -+ #endif - } - #endif - - -From 03c088435bf1300b0b5fe914a7d100d756c19234 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 2 Aug 2024 05:57:20 +0900 -Subject: [PATCH 10/12] Port `LockedState` and `_ThreadLocal` to WASI without - any locking (#780) - -(cherry picked from commit aa68eebebcb8dd16b9636c54e580e0ad32bb57e3) ---- - Sources/FoundationEssentials/LockedState.swift | 9 +++++++++ - Sources/FoundationEssentials/_ThreadLocal.swift | 8 ++++++++ - 2 files changed, 17 insertions(+) - -diff --git a/Sources/FoundationEssentials/LockedState.swift b/Sources/FoundationEssentials/LockedState.swift -index ad432c704..59fbd8b8e 100644 ---- a/Sources/FoundationEssentials/LockedState.swift -+++ b/Sources/FoundationEssentials/LockedState.swift -@@ -33,6 +33,9 @@ package struct LockedState { - typealias Primitive = pthread_mutex_t - #elseif canImport(WinSDK) - typealias Primitive = SRWLOCK -+#elseif os(WASI) -+ // WASI is single-threaded, so we don't need a lock. -+ typealias Primitive = Void - #endif - - typealias PlatformLock = UnsafeMutablePointer -@@ -45,6 +48,8 @@ package struct LockedState { - pthread_mutex_init(platformLock, nil) - #elseif canImport(WinSDK) - InitializeSRWLock(platformLock) -+#elseif os(WASI) -+ // no-op - #endif - } - -@@ -62,6 +67,8 @@ package struct LockedState { - pthread_mutex_lock(platformLock) - #elseif canImport(WinSDK) - AcquireSRWLockExclusive(platformLock) -+#elseif os(WASI) -+ // no-op - #endif - } - -@@ -72,6 +79,8 @@ package struct LockedState { - pthread_mutex_unlock(platformLock) - #elseif canImport(WinSDK) - ReleaseSRWLockExclusive(platformLock) -+#elseif os(WASI) -+ // no-op - #endif - } - } -diff --git a/Sources/FoundationEssentials/_ThreadLocal.swift b/Sources/FoundationEssentials/_ThreadLocal.swift -index 67f5dfaae..df5bad8d7 100644 ---- a/Sources/FoundationEssentials/_ThreadLocal.swift -+++ b/Sources/FoundationEssentials/_ThreadLocal.swift -@@ -30,6 +30,8 @@ struct _ThreadLocal { - fileprivate typealias PlatformKey = tss_t - #elseif canImport(WinSDK) - fileprivate typealias PlatformKey = DWORD -+#elseif os(WASI) -+ fileprivate typealias PlatformKey = UnsafeMutablePointer - #endif - - struct Key { -@@ -46,6 +48,8 @@ struct _ThreadLocal { - self.key = key - #elseif canImport(WinSDK) - key = FlsAlloc(nil) -+#elseif os(WASI) -+ key = UnsafeMutablePointer.allocate(capacity: 1) - #endif - } - } -@@ -58,6 +62,8 @@ struct _ThreadLocal { - tss_get(key) - #elseif canImport(WinSDK) - FlsGetValue(key) -+#elseif os(WASI) -+ key.pointee - #endif - } - -@@ -68,6 +74,8 @@ struct _ThreadLocal { - tss_set(key, newValue) - #elseif canImport(WinSDK) - FlsSetValue(key, newValue) -+#elseif os(WASI) -+ key.pointee = newValue - #endif - } - } - -From 2ca6906f97be3110a101a23c452d3389432a98ff Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Sat, 3 Aug 2024 01:55:56 +0900 -Subject: [PATCH 11/12] Add WASI platform conditions for libc imports and word - size (#776) - -* Add `import WASILibc` statements to libc import chains - -* Declare wasm32 arch as 32-bit environment - -* Switch to _pointerBitWidth for architecture checks - -This change switches the architecture checks in Data.swift to use the -_pointerBitWidth instead of the arch() checks for consistency with newer -platforms. - -(cherry picked from commit c82d1673eb112ac62e4f770947c8a238a7659163) ---- - .../FoundationEssentials/Calendar/Calendar.swift | 2 ++ - .../Calendar/Calendar_Gregorian.swift | 2 ++ - .../FoundationEssentials/Data/Data+Reading.swift | 2 ++ - .../FoundationEssentials/Data/Data+Writing.swift | 2 ++ - Sources/FoundationEssentials/Data/Data.swift | 14 ++++++++------ - Sources/FoundationEssentials/Date.swift | 2 ++ - .../Decimal/Decimal+Math.swift | 2 ++ - .../Error/CocoaError+FilePath.swift | 2 ++ - .../Error/ErrorCodes+POSIX.swift | 2 ++ - .../FileManager/FileManager+Basics.swift | 2 ++ - .../FileManager/FileManager+Directories.swift | 2 ++ - .../FileManager/FileManager+Files.swift | 3 +++ - .../FileManager/FileManager+SymbolicLinks.swift | 2 ++ - .../FileManager/FileManager+Utilities.swift | 2 ++ - .../FileManager/FileOperations.swift | 2 ++ - ...BinaryInteger+NumericStringRepresentation.swift | 2 ++ - .../ProcessInfo/ProcessInfo.swift | 2 ++ - .../PropertyList/OpenStepPlist.swift | 2 ++ - .../FoundationEssentials/String/String+Path.swift | 2 ++ - .../Calendar/Calendar_ICU.swift | 2 ++ - .../Formatting/Duration+Formatting.swift | 2 ++ - 21 files changed, 49 insertions(+), 6 deletions(-) - -diff --git a/Sources/FoundationEssentials/Calendar/Calendar.swift b/Sources/FoundationEssentials/Calendar/Calendar.swift -index 99d72e161..94a67d60a 100644 ---- a/Sources/FoundationEssentials/Calendar/Calendar.swift -+++ b/Sources/FoundationEssentials/Calendar/Calendar.swift -@@ -18,6 +18,8 @@ import Android - import Glibc - #elseif canImport(CRT) - import CRT -+#elseif os(WASI) -+import WASILibc - #endif - - #if FOUNDATION_FRAMEWORK -diff --git a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift -index fd863cef8..a7cc7386f 100644 ---- a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift -+++ b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift -@@ -18,6 +18,8 @@ import Android - import Glibc - #elseif canImport(CRT) - import CRT -+#elseif os(WASI) -+import WASILibc - #endif - - -diff --git a/Sources/FoundationEssentials/Data/Data+Reading.swift b/Sources/FoundationEssentials/Data/Data+Reading.swift -index eb66e3f7c..4c893f6cb 100644 ---- a/Sources/FoundationEssentials/Data/Data+Reading.swift -+++ b/Sources/FoundationEssentials/Data/Data+Reading.swift -@@ -25,6 +25,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - func _fgetxattr(_ fd: Int32, _ name: UnsafePointer!, _ value: UnsafeMutableRawPointer!, _ size: Int, _ position: UInt32, _ options: Int32) -> Int { -diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift -index c86f27bc3..7150157aa 100644 ---- a/Sources/FoundationEssentials/Data/Data+Writing.swift -+++ b/Sources/FoundationEssentials/Data/Data+Writing.swift -@@ -27,6 +27,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if !NO_FILESYSTEM -diff --git a/Sources/FoundationEssentials/Data/Data.swift b/Sources/FoundationEssentials/Data/Data.swift -index a88478261..40fee4a2c 100644 ---- a/Sources/FoundationEssentials/Data/Data.swift -+++ b/Sources/FoundationEssentials/Data/Data.swift -@@ -76,6 +76,8 @@ import Glibc - import Musl - #elseif canImport(ucrt) - import ucrt -+#elseif canImport(WASILibc) -+import WASILibc - #endif - - #if os(Windows) -@@ -580,11 +582,11 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect - @usableFromInline - @frozen - internal struct InlineData : Sendable { --#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) -+#if _pointerBitWidth(_64) - @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, - UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum - @usableFromInline var bytes: Buffer --#elseif arch(i386) || arch(arm) || arch(arm64_32) -+#elseif _pointerBitWidth(_32) - @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, - UInt8, UInt8) //len //enum - @usableFromInline var bytes: Buffer -@@ -615,9 +617,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect - @inlinable // This is @inlinable as a trivial initializer. - init(count: Int = 0) { - assert(count <= MemoryLayout.size) --#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) -+#if _pointerBitWidth(_64) - bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) --#elseif arch(i386) || arch(arm) || arch(arm64_32) -+#elseif _pointerBitWidth(_32) - bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) - #else - #error ("Unsupported architecture: initialization for Buffer is required for this architecture") -@@ -802,9 +804,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect - } - } - --#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) -+#if _pointerBitWidth(_64) - @usableFromInline internal typealias HalfInt = Int32 --#elseif arch(i386) || arch(arm) || arch(arm64_32) -+#elseif _pointerBitWidth(_32) - @usableFromInline internal typealias HalfInt = Int16 - #else - #error ("Unsupported architecture: a definition of half of the pointer sized Int needs to be defined for this architecture") -diff --git a/Sources/FoundationEssentials/Date.swift b/Sources/FoundationEssentials/Date.swift -index 8811aa433..66e778c1c 100644 ---- a/Sources/FoundationEssentials/Date.swift -+++ b/Sources/FoundationEssentials/Date.swift -@@ -18,6 +18,8 @@ import Bionic - import Glibc - #elseif canImport(WinSDK) - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if !FOUNDATION_FRAMEWORK -diff --git a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift -index 172454463..38ef0cf5e 100644 ---- a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift -+++ b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift -@@ -18,6 +18,8 @@ import Android - import Glibc - #elseif canImport(CRT) - import CRT -+#elseif os(WASI) -+import WASILibc - #endif - - private let powerOfTen: [Decimal.VariableLengthInteger] = [ -diff --git a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift -index fbce0f6f8..3b60afe22 100644 ---- a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift -+++ b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift -@@ -22,6 +22,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - extension CocoaError.Code { -diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -index d6977ba47..9fae00120 100644 ---- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -+++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift -@@ -19,6 +19,8 @@ - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if FOUNDATION_FRAMEWORK -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift -index 03ca025a2..d7a4de859 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift -@@ -19,6 +19,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if os(Windows) -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift -index 89e3f1f97..a29b31195 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift -@@ -26,6 +26,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - internal import _FoundationCShims -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -index 99f1b32c6..5d7c4a2c0 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift -@@ -26,6 +26,9 @@ internal import _FoundationCShims - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+internal import _FoundationCShims -+import WASILibc - #endif - - extension Date { -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift -index fc9b8f70e..92eb3accb 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift -@@ -21,6 +21,8 @@ import Glibc - import CRT - import WinSDK - internal import _FoundationCShims -+#elseif os(WASI) -+import WASILibc - #endif - - extension _FileManagerImpl { -diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -index 45f498937..870e453ff 100644 ---- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -+++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift -@@ -31,6 +31,8 @@ internal import _FoundationCShims - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if os(Windows) -diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift -index 51f4f7ea4..d3fc7b2d5 100644 ---- a/Sources/FoundationEssentials/FileManager/FileOperations.swift -+++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift -@@ -19,6 +19,8 @@ import Glibc - #elseif os(Windows) - import CRT - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if FOUNDATION_FRAMEWORK -diff --git a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift -index db02789e2..b9a60576a 100644 ---- a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift -+++ b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift -@@ -18,6 +18,8 @@ import Android - import Glibc - #elseif os(Windows) - import CRT -+#elseif os(WASI) -+import WASILibc - #endif - - // MARK: - BinaryInteger + Numeric string representation -diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -index a3e6b6c60..bd3f042fe 100644 ---- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -+++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift -@@ -21,6 +21,8 @@ import unistd - import Glibc - #elseif os(Windows) - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - #if !NO_PROCESS -diff --git a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift -index 174a6edda..42982deeb 100644 ---- a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift -+++ b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift -@@ -16,6 +16,8 @@ import Darwin - import Bionic - #elseif canImport(Glibc) - import Glibc -+#elseif os(WASI) -+import WASILibc - #endif - - #if canImport(CRT) -diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift -index 5e42aef4c..5d4207fb4 100644 ---- a/Sources/FoundationEssentials/String/String+Path.swift -+++ b/Sources/FoundationEssentials/String/String+Path.swift -@@ -18,6 +18,8 @@ import Android - import Glibc - #elseif os(Windows) - import WinSDK -+#elseif os(WASI) -+import WASILibc - #endif - - internal import _FoundationCShims -diff --git a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift -index 0f0b97386..86d1d8d6d 100644 ---- a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift -+++ b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift -@@ -22,6 +22,8 @@ import Glibc - import CRT - #elseif canImport(Darwin) - import Darwin -+#elseif os(WASI) -+import WASILibc - #endif - - internal import _FoundationICU -diff --git a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift -index ccfea7873..41d14b388 100644 ---- a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift -+++ b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift -@@ -22,6 +22,8 @@ import Android - import Glibc - #elseif os(Windows) - import CRT -+#elseif os(WASI) -+import WASILibc - #endif - - @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) - -From fafdf905c4b70652d3a1573ed0e537135c52d891 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Tue, 6 Aug 2024 02:20:54 +0900 -Subject: [PATCH 12/12] Enable wasi-libc emulation features (#777) - -* Enable wasi-libc emulation features - -Those features require explicit macro definitions to be enabled, so add -them to the package definition. Only affects WASI builds. - -* Prefer `TARGET_OS_WASI` over `__wasi__` - -And explain why we need definition checks for `signal.h` and `sys/mman.h` - -(cherry picked from commit c86692f7e7b6d7cb1625d66ee6ff2618011f22f1) ---- - CMakeLists.txt | 8 +++++ - Package.swift | 35 ++++++++++++++----- - Sources/FoundationEssentials/CMakeLists.txt | 1 + - .../CMakeLists.txt | 1 + - Sources/_FoundationCShims/include/_CStdlib.h | 16 ++++++++- - 5 files changed, 51 insertions(+), 10 deletions(-) - -diff --git a/CMakeLists.txt b/CMakeLists.txt -index b99fcfa8f..2afb0787e 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -104,6 +104,14 @@ foreach(version ${_SwiftFoundation_versions}) - endforeach() - endforeach() - -+# wasi-libc emulation feature flags -+set(_SwiftFoundation_wasi_libc_flags) -+if(CMAKE_SYSTEM_NAME STREQUAL "WASI") -+ list(APPEND _SwiftFoundation_wasi_libc_flags -+ "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" -+ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") -+endif() -+ - include(GNUInstallDirs) - include(SwiftFoundationSwiftSupport) - -diff --git a/Package.swift b/Package.swift -index 70f77537b..6477b7ffb 100644 ---- a/Package.swift -+++ b/Package.swift -@@ -70,6 +70,11 @@ var dependencies: [Package.Dependency] { - } - } - -+let wasiLibcCSettings: [CSetting] = [ -+ .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), -+ .define("_WASI_EMULATED_MMAN", .when(platforms: [.wasi])), -+] -+ - let package = Package( - name: "FoundationPreview", - platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")], -@@ -91,15 +96,23 @@ let package = Package( - path: "Sources/Foundation"), - - // _FoundationCShims (Internal) -- .target(name: "_FoundationCShims", -- cSettings: [.define("_CRT_SECURE_NO_WARNINGS", -- .when(platforms: [.windows]))]), -+ .target( -+ name: "_FoundationCShims", -+ cSettings: [ -+ .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])) -+ ] + wasiLibcCSettings -+ ), - - // TestSupport (Internal) -- .target(name: "TestSupport", dependencies: [ -- "FoundationEssentials", -- "FoundationInternationalization", -- ], swiftSettings: availabilityMacros + concurrencyChecking), -+ .target( -+ name: "TestSupport", -+ dependencies: [ -+ "FoundationEssentials", -+ "FoundationInternationalization", -+ ], -+ cSettings: wasiLibcCSettings, -+ swiftSettings: availabilityMacros + concurrencyChecking -+ ), - - // FoundationEssentials - .target( -@@ -130,11 +143,14 @@ let package = Package( - ], - cSettings: [ - .define("_GNU_SOURCE", .when(platforms: [.linux])) -- ], -+ ] + wasiLibcCSettings, - swiftSettings: [ - .enableExperimentalFeature("VariadicGenerics"), - .enableExperimentalFeature("AccessLevelOnImport") -- ] + availabilityMacros + concurrencyChecking -+ ] + availabilityMacros + concurrencyChecking, -+ linkerSettings: [ -+ .linkedLibrary("wasi-emulated-getpid", .when(platforms: [.wasi])), -+ ] - ), - .testTarget( - name: "FoundationEssentialsTests", -@@ -166,6 +182,7 @@ let package = Package( - "CMakeLists.txt", - "Predicate/CMakeLists.txt" - ], -+ cSettings: wasiLibcCSettings, - swiftSettings: [ - .enableExperimentalFeature("AccessLevelOnImport") - ] + availabilityMacros + concurrencyChecking -diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt -index d9ebc2ebb..7cddff682 100644 ---- a/Sources/FoundationEssentials/CMakeLists.txt -+++ b/Sources/FoundationEssentials/CMakeLists.txt -@@ -66,6 +66,7 @@ target_compile_options(FoundationEssentials PRIVATE - "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" - "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") - target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros}) -+target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_wasi_libc_flags}) - target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation") - - target_link_libraries(FoundationEssentials PUBLIC -diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt -index 5a89ceb88..857db9cac 100644 ---- a/Sources/FoundationInternationalization/CMakeLists.txt -+++ b/Sources/FoundationInternationalization/CMakeLists.txt -@@ -33,6 +33,7 @@ target_compile_options(FoundationInternationalization PRIVATE - "SHELL:$<$:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>" - "SHELL:$<$:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>") - target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros}) -+target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_wasi_libc_flags}) - target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation") - - target_link_libraries(FoundationInternationalization PUBLIC -diff --git a/Sources/_FoundationCShims/include/_CStdlib.h b/Sources/_FoundationCShims/include/_CStdlib.h -index 8967eb7f4..03373934b 100644 ---- a/Sources/_FoundationCShims/include/_CStdlib.h -+++ b/Sources/_FoundationCShims/include/_CStdlib.h -@@ -60,7 +60,21 @@ - #endif - - #if __has_include() --#include -+/// Guard against including `signal.h` on WASI. The `signal.h` header file -+/// itself is available in wasi-libc, but it's just a stub that doesn't actually -+/// do anything. And also including it requires a special macro definition -+/// (`_WASI_EMULATED_SIGNAL`) and it causes compilation errors without the macro. -+# if !TARGET_OS_WASI || defined(_WASI_EMULATED_SIGNAL) -+# include -+# endif -+#endif -+ -+#if __has_include() -+/// Similar to `signal.h`, guard against including `sys/mman.h` on WASI unless -+/// `_WASI_EMULATED_MMAN` is enabled. -+# if !TARGET_OS_WASI || defined(_WASI_EMULATED_MMAN) -+# include -+# endif - #endif - - #if __has_include() diff --git a/schemes/release-6.0/swift-foundation/843.patch b/schemes/release-6.0/swift-foundation/843.patch deleted file mode 100644 index 28e2e6f5..00000000 --- a/schemes/release-6.0/swift-foundation/843.patch +++ /dev/null @@ -1,104 +0,0 @@ -From ac455270257a1b27bfb7a8d85c4b296e93ba03bf Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Sat, 10 Aug 2024 16:13:28 +0000 -Subject: [PATCH 1/2] Autolink wasi-libc emulation libraries - -`_WASI_EMULATED_XXX` macros require linking against the corresponding -emulation libraries. This patch adds `-Xfrontend -public-autolink-library` -flags to the Swift compiler invocation to automatically link against the -emulation libraries. Also enable getpid emulation explicitly, as it is -available by default but emiting a warning. ---- - CMakeLists.txt | 6 +++++- - 1 file changed, 5 insertions(+), 1 deletion(-) - -diff --git a/CMakeLists.txt b/CMakeLists.txt -index 108b6572..a2024859 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -118,8 +118,12 @@ endforeach() - set(_SwiftFoundation_wasi_libc_flags) - if(CMAKE_SYSTEM_NAME STREQUAL "WASI") - list(APPEND _SwiftFoundation_wasi_libc_flags -+ "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" - "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" -- "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" -+ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") - endif() - - include(GNUInstallDirs) - -From 7f4abde248649fab83966ddc36df5e65827e663d Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Fri, 16 Aug 2024 09:52:40 +0000 -Subject: [PATCH 2/2] Link emulation libraries directly if building shared - libraries - ---- - CMakeLists.txt | 19 ++++++++++++++----- - Sources/FoundationEssentials/CMakeLists.txt | 1 + - .../CMakeLists.txt | 1 + - 3 files changed, 16 insertions(+), 5 deletions(-) - -diff --git a/CMakeLists.txt b/CMakeLists.txt -index a2024859..79ad98d2 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -114,16 +114,25 @@ foreach(version ${_SwiftFoundation_versions}) - endforeach() - endforeach() - --# wasi-libc emulation feature flags -+# wasi-libc emulation feature flags passed to the Swift compiler - set(_SwiftFoundation_wasi_libc_flags) -+# wasi-libc emulation libraries to link against when building a shared library -+set(_SwiftFoundation_wasi_libc_libraries) - if(CMAKE_SYSTEM_NAME STREQUAL "WASI") - list(APPEND _SwiftFoundation_wasi_libc_flags - "SHELL:$<$:-Xcc -D_WASI_EMULATED_GETPID>" -- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" - "SHELL:$<$:-Xcc -D_WASI_EMULATED_SIGNAL>" -- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" -- "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>" -- "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") -+ "SHELL:$<$:-Xcc -D_WASI_EMULATED_MMAN>") -+ if(BUILD_SHARED_LIBS) -+ # Link emulation libraries to the shared library directly when building a shared library -+ list(APPEND _SwiftFoundation_wasi_libc_libraries wasi-emulated-getpid wasi-emulated-signal wasi-emulated-mman) -+ else() -+ # Emit autolink entries to let clients to link against the emulation libraries -+ list(APPEND _SwiftFoundation_wasi_libc_flags -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-getpid>" -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-signal>" -+ "SHELL:$<$:-Xfrontend -public-autolink-library -Xfrontend wasi-emulated-mman>") -+ endif() - endif() - - include(GNUInstallDirs) -diff --git a/Sources/FoundationEssentials/CMakeLists.txt b/Sources/FoundationEssentials/CMakeLists.txt -index e8417d19..006b6caf 100644 ---- a/Sources/FoundationEssentials/CMakeLists.txt -+++ b/Sources/FoundationEssentials/CMakeLists.txt -@@ -73,6 +73,7 @@ target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundati - target_link_libraries(FoundationEssentials PUBLIC - _FoundationCShims - _FoundationCollections) -+target_link_libraries(FoundationEssentials PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) - - if(NOT BUILD_SHARED_LIBS) - target_compile_options(FoundationEssentials PRIVATE -diff --git a/Sources/FoundationInternationalization/CMakeLists.txt b/Sources/FoundationInternationalization/CMakeLists.txt -index 857db9ca..baaf7451 100644 ---- a/Sources/FoundationInternationalization/CMakeLists.txt -+++ b/Sources/FoundationInternationalization/CMakeLists.txt -@@ -40,6 +40,7 @@ target_link_libraries(FoundationInternationalization PUBLIC - FoundationEssentials - _FoundationCShims - _FoundationICU) -+target_link_libraries(FoundationInternationalization PUBLIC ${_SwiftFoundation_wasi_libc_libraries}) - - if(NOT BUILD_SHARED_LIBS) - target_compile_options(FoundationInternationalization PRIVATE From dc2703dfab50c25e9b2301e4acf9a3243ca50ab0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 16:09:04 +0000 Subject: [PATCH 4/9] Update foundation build script for swift-foundation --- schemes/release-6.0/build/build-foundation.sh | 66 +++++++++++++++++++ .../build/build-target-toolchain.sh | 2 +- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100755 schemes/release-6.0/build/build-foundation.sh diff --git a/schemes/release-6.0/build/build-foundation.sh b/schemes/release-6.0/build/build-foundation.sh new file mode 100755 index 00000000..2565b43f --- /dev/null +++ b/schemes/release-6.0/build/build-foundation.sh @@ -0,0 +1,66 @@ +#!/bin/bash +set -ex +DESTINATION_TOOLCHAIN=$1 +LLVM_BIN_DIR=$2 +CLANG_BIN_DIR=$3 +SWIFT_BIN_DIR=$4 +WASI_SYSROOT_PATH=$5 +TRIPLE="wasm32-unknown-wasi" + +SOURCE_PATH="$(cd "$(dirname $0)/../../../.." && pwd)" +SCHEME_BUILD_PATH="$(cd "$(dirname $0)" && pwd)" +BUILD_SDK_PATH="$SOURCE_PATH/build-sdk" +LIBXML2_PATH="$BUILD_SDK_PATH/libxml2" + +FOUNDATION_BUILD="$SOURCE_PATH/build/WebAssembly/foundation-wasi-wasm32" +FOUNDATION_MACROS_BUILD="$SOURCE_PATH/build/WebAssembly/foundation-macros-wasi-wasm32" + +swift_extra_flags="" +c_extra_flags="" +if [[ "$TRIPLE" == "wasm32-unknown-wasip1-threads" ]]; then + swift_extra_flags="-Xcc -matomics -Xcc -mbulk-memory -Xcc -mthread-model -Xcc posix -Xcc -pthread -Xcc -ftls-model=local-exec" + c_extra_flags="-mthread-model posix -pthread -ftls-model=local-exec" +fi + +cmake -G Ninja \ + -D CMAKE_C_COMPILER="$CLANG_BIN_DIR/clang" \ + -D CMAKE_CXX_COMPILER="$CLANG_BIN_DIR/clang++" \ + -D CMAKE_Swift_COMPILER="$SWIFT_BIN_DIR/swiftc" \ + -D BUILD_SHARED_LIBS=ON \ + -B "$FOUNDATION_MACROS_BUILD" \ + "${SOURCE_PATH}/swift-foundation/Sources/FoundationMacros" + +cmake --build "$FOUNDATION_MACROS_BUILD" + +cmake -G Ninja \ + -D CMAKE_BUILD_TYPE="Release" \ + -D CMAKE_SYSROOT="$WASI_SYSROOT_PATH" \ + -D CMAKE_Swift_COMPILER="$SWIFT_BIN_DIR/swiftc" \ + -D CMAKE_STAGING_PREFIX="$DESTINATION_TOOLCHAIN/usr" \ + -D CMAKE_SYSTEM_NAME=WASI \ + -D CMAKE_SYSTEM_PROCESSOR=wasm32 \ + -D CMAKE_C_COMPILER_TARGET="$TRIPLE" \ + -D CMAKE_CXX_COMPILER_TARGET="$TRIPLE" \ + -D CMAKE_Swift_COMPILER_TARGET="$TRIPLE" \ + -D CMAKE_C_COMPILER="$CLANG_BIN_DIR/clang" \ + -D CMAKE_CXX_COMPILER="$CLANG_BIN_DIR/clang++" \ + -D CMAKE_AR="$LLVM_BIN_DIR/llvm-ar" \ + -D CMAKE_RANLIB="$LLVM_BIN_DIR/llvm-ranlib" \ + -D LIBXML2_INCLUDE_DIR="$LIBXML2_PATH/include/libxml2" \ + -D LIBXML2_LIBRARY="$LIBXML2_PATH/lib" \ + -D BUILD_SHARED_LIBS=OFF \ + -D FOUNDATION_BUILD_TOOLS=OFF \ + -D CMAKE_Swift_COMPILER_FORCED=ON \ + -D CMAKE_C_COMPILER_FORCED=ON \ + -D CMAKE_CXX_COMPILER_FORCED=ON \ + -D CMAKE_Swift_FLAGS="-sdk $WASI_SYSROOT_PATH -resource-dir $DESTINATION_TOOLCHAIN/usr/lib/swift_static $swift_extra_flags" \ + -D CMAKE_C_FLAGS="-resource-dir $DESTINATION_TOOLCHAIN/usr/lib/swift_static/clang -B $LLVM_BIN_DIR $c_extra_flags" \ + -D _SwiftCollections_SourceDIR="$SOURCE_PATH/swift-collections" \ + -D _SwiftFoundation_SourceDIR="$SOURCE_PATH/swift-foundation" \ + -D _SwiftFoundationICU_SourceDIR="$SOURCE_PATH/swift-foundation-icu" \ + -D SwiftFoundation_MACRO="$FOUNDATION_MACROS_BUILD/lib" \ + -B "$FOUNDATION_BUILD" \ + "${SOURCE_PATH}/swift-corelibs-foundation" + +cmake --build "$FOUNDATION_BUILD" +cmake --install "$FOUNDATION_BUILD" diff --git a/schemes/release-6.0/build/build-target-toolchain.sh b/schemes/release-6.0/build/build-target-toolchain.sh index b71e3caf..9e7e4f7c 100755 --- a/schemes/release-6.0/build/build-target-toolchain.sh +++ b/schemes/release-6.0/build/build-target-toolchain.sh @@ -144,7 +144,7 @@ build_target_toolchain() { "$SWIFT_BIN_DIR" "$WASI_SYSROOT_PATH" ) - "$TOOLS_BUILD_PATH/build-foundation.sh" "${CORELIBS_ARGS[@]}" + "$SCHEMES_BUILD_PATH/build-foundation.sh" "${CORELIBS_ARGS[@]}" "$SCHEMES_BUILD_PATH/build-xctest.sh" "${CORELIBS_ARGS[@]}" } From 3517f1da657bf1834161d42e0eed314d4ea7ee60 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 16:25:16 +0000 Subject: [PATCH 5/9] Enable synchronization module and add missing patches --- .../build/build-target-toolchain.sh | 1 + ...-user-group-related-code-on-WASI-783.patch | 143 +++++++ ...m-conditions-for-libc-imports-and-wo.patch | 354 ++++++++++++++++++ ...x-operatingSystemVersion-on-WASI-782.patch | 27 ++ ...egularFile-for-WASI-without-sendfile.patch | 52 +++ ...ed-or-fs-attributes-related-code-on-.patch | 79 ++++ ...check-in-isDeletableFile-on-WASI-785.patch | 27 ++ ...-and-_ThreadLocal-to-WASI-without-an.patch | 95 +++++ ...supported-when-attempting-to-create-.patch | 39 ++ ...OTE-definition-for-WASI-platform-778.patch | 31 ++ 10 files changed, 848 insertions(+) create mode 100644 schemes/release-6.0/swift-foundation/0008-Guard-out-user-group-related-code-on-WASI-783.patch create mode 100644 schemes/release-6.0/swift-foundation/0009-Add-WASI-platform-conditions-for-libc-imports-and-wo.patch create mode 100644 schemes/release-6.0/swift-foundation/0010-Fix-operatingSystemVersion-on-WASI-782.patch create mode 100644 schemes/release-6.0/swift-foundation/0011-Implement-_copyRegularFile-for-WASI-without-sendfile.patch create mode 100644 schemes/release-6.0/swift-foundation/0012-Guard-out-extended-or-fs-attributes-related-code-on-.patch create mode 100644 schemes/release-6.0/swift-foundation/0013-Skip-sticky-bit-check-in-isDeletableFile-on-WASI-785.patch create mode 100644 schemes/release-6.0/swift-foundation/0014-Port-LockedState-and-_ThreadLocal-to-WASI-without-an.patch create mode 100644 schemes/release-6.0/swift-foundation/0015-Throw-.featureUnsupported-when-attempting-to-create-.patch create mode 100644 schemes/release-6.0/swift-foundation/0016-Exclude-EREMOTE-definition-for-WASI-platform-778.patch diff --git a/schemes/release-6.0/build/build-target-toolchain.sh b/schemes/release-6.0/build/build-target-toolchain.sh index 9e7e4f7c..0294df3e 100755 --- a/schemes/release-6.0/build/build-target-toolchain.sh +++ b/schemes/release-6.0/build/build-target-toolchain.sh @@ -110,6 +110,7 @@ build_target_toolchain() { -D SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING=YES \ -D SWIFT_ENABLE_EXPERIMENTAL_REFLECTION=YES \ -D SWIFT_ENABLE_EXPERIMENTAL_CXX_INTEROP=YES \ + -D SWIFT_ENABLE_SYNCHRONIZATION=YES \ -D SWIFT_PRIMARY_VARIANT_SDK=WASI \ -D SWIFT_PRIMARY_VARIANT_ARCH=wasm32 \ -D SWIFT_SDKS:STRING=WASI \ diff --git a/schemes/release-6.0/swift-foundation/0008-Guard-out-user-group-related-code-on-WASI-783.patch b/schemes/release-6.0/swift-foundation/0008-Guard-out-user-group-related-code-on-WASI-783.patch new file mode 100644 index 00000000..7a959909 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0008-Guard-out-user-group-related-code-on-WASI-783.patch @@ -0,0 +1,143 @@ +From 1f5e0a8cc6e45e6be7a6d983877dbd602e5c0ff8 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:23:13 +0900 +Subject: [PATCH] Guard out user/group related code on WASI (#783) + +* Guard out user/group related code on WASI + +This change guards out the user/group related code on WASI, as WASI does +not have the concept of users or groups. + +* Throw explicit unsupported error if trying to set user or group on WASI + +Instead of implicitly ignoring user-given values, we should throw +exception to make it clear that those values cannot be set on WASI. +--- + Sources/FoundationEssentials/Data/Data+Writing.swift | 2 ++ + .../FoundationEssentials/FileManager/FileManager+Files.swift | 5 +++++ + .../FileManager/FileManager+Utilities.swift | 2 +- + .../FoundationEssentials/FileManager/FileOperations.swift | 2 ++ + Sources/FoundationEssentials/Platform.swift | 4 ++-- + Sources/FoundationEssentials/String/String+Path.swift | 2 ++ + 6 files changed, 14 insertions(+), 3 deletions(-) + +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index 1e75b43..78a9a98 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -516,6 +516,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint + + cleanupTemporaryDirectory(at: temporaryDirectoryPath) + ++#if !os(WASI) // WASI does not support fchmod for now + if let mode { + // Try to change the mode if the path has not changed. Do our best, but don't report an error. + #if FOUNDATION_FRAMEWORK +@@ -539,6 +540,7 @@ private func writeToFileAux(path inPath: PathOrURL, buffer: UnsafeRawBufferPoint + fchmod(fd, mode) + #endif + } ++#endif // os(WASI) + } + } + } +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index b8cd50a..943ac9e 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -928,6 +928,10 @@ extension _FileManagerImpl { + let groupID = _readFileAttributePrimitive(attributes[.groupOwnerAccountID], as: UInt.self) + + if user != nil || userID != nil || group != nil || groupID != nil { ++ #if os(WASI) ++ // WASI does not have the concept of users or groups ++ throw CocoaError.errorWithFilePath(.featureUnsupported, path) ++ #else + // Bias toward userID & groupID - try to prevent round trips to getpwnam if possible. + var leaveUnchanged: UInt32 { UInt32(bitPattern: -1) } + let rawUserID = userID.flatMap(uid_t.init) ?? user.flatMap(Self._userAccountNameToNumber) ?? leaveUnchanged +@@ -935,6 +939,7 @@ extension _FileManagerImpl { + if chown(fileSystemRepresentation, rawUserID, rawGroupID) != 0 { + throw CocoaError.errorWithFilePath(path, errno: errno, reading: false) + } ++ #endif + } + + try Self._setCatInfoAttributes(attributes, path: path) +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index 9bac967..4f42f19 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -274,7 +274,7 @@ extension _FileManagerImpl { + } + #endif + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + static func _userAccountNameToNumber(_ name: String) -> uid_t? { + name.withCString { ptr in + getpwnam(ptr)?.pointee.pw_uid +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 528a7c9..8734fc0 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -874,12 +874,14 @@ enum _FileOperations { + } + defer { close(dstfd) } + ++ #if !os(WASI) // WASI doesn't have fchmod for now + // Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues + let permissions = fileInfo.st_mode & ~S_IFMT + guard fchmod(dstfd, permissions) == 0 else { + try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) + return + } ++ #endif + + if fileInfo.st_size == 0 { + // no copying required +diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift +index 9c3f2d7..4549a45 100644 +--- a/Sources/FoundationEssentials/Platform.swift ++++ b/Sources/FoundationEssentials/Platform.swift +@@ -114,7 +114,7 @@ private let _cachedUGIDs: (uid_t, gid_t) = { + }() + #endif + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + extension Platform { + private static var ROOT_USER: UInt32 { 0 } + static func getUGIDs(allowEffectiveRootUID: Bool = true) -> (uid: UInt32, gid: UInt32) { +@@ -175,7 +175,7 @@ extension Platform { + // FIXME: bionic implements this as `return 0;` and does not expose the + // function via headers. We should be able to shim this and use the call + // if it is available. +-#if !os(Android) ++#if !os(Android) && !os(WASI) + guard issetugid() == 0 else { return nil } + #endif + if let value = getenv(name) { +diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift +index 477d5d3..aaf62d1 100644 +--- a/Sources/FoundationEssentials/String/String+Path.swift ++++ b/Sources/FoundationEssentials/String/String+Path.swift +@@ -452,6 +452,7 @@ extension String { + return envVar.standardizingPath + } + ++ #if !os(WASI) // WASI does not have user concept + // Next, attempt to find the home directory via getpwnam/getpwuid + var pass: UnsafeMutablePointer? + if let user { +@@ -465,6 +466,7 @@ extension String { + if let dir = pass?.pointee.pw_dir { + return String(cString: dir).standardizingPath + } ++ #endif + + // Fallback to HOME for the current user if possible + if user == nil, let home = getenv("HOME") { +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0009-Add-WASI-platform-conditions-for-libc-imports-and-wo.patch b/schemes/release-6.0/swift-foundation/0009-Add-WASI-platform-conditions-for-libc-imports-and-wo.patch new file mode 100644 index 00000000..7877e137 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0009-Add-WASI-platform-conditions-for-libc-imports-and-wo.patch @@ -0,0 +1,354 @@ +From 8e83fc7f48dc5410b7b3fabaf5b0deb121488cea Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 3 Aug 2024 01:55:56 +0900 +Subject: [PATCH] Add WASI platform conditions for libc imports and word size + (#776) + +* Add `import WASILibc` statements to libc import chains + +* Declare wasm32 arch as 32-bit environment + +* Switch to _pointerBitWidth for architecture checks + +This change switches the architecture checks in Data.swift to use the +_pointerBitWidth instead of the arch() checks for consistency with newer +platforms. +--- + .../FoundationEssentials/Calendar/Calendar.swift | 2 ++ + .../Calendar/Calendar_Gregorian.swift | 2 ++ + .../FoundationEssentials/Data/Data+Reading.swift | 2 ++ + .../FoundationEssentials/Data/Data+Writing.swift | 2 ++ + Sources/FoundationEssentials/Data/Data.swift | 14 ++++++++------ + Sources/FoundationEssentials/Date.swift | 2 ++ + .../Decimal/Decimal+Math.swift | 2 ++ + .../Error/CocoaError+FilePath.swift | 2 ++ + .../Error/ErrorCodes+POSIX.swift | 2 ++ + .../FileManager/FileManager+Basics.swift | 2 ++ + .../FileManager/FileManager+Directories.swift | 2 ++ + .../FileManager/FileManager+Files.swift | 3 +++ + .../FileManager/FileManager+SymbolicLinks.swift | 2 ++ + .../FileManager/FileManager+Utilities.swift | 2 ++ + .../FileManager/FileOperations.swift | 2 ++ + ...BinaryInteger+NumericStringRepresentation.swift | 2 ++ + .../ProcessInfo/ProcessInfo.swift | 2 ++ + .../PropertyList/OpenStepPlist.swift | 2 ++ + .../FoundationEssentials/String/String+Path.swift | 2 ++ + .../Calendar/Calendar_ICU.swift | 2 ++ + .../Formatting/Duration+Formatting.swift | 2 ++ + 21 files changed, 49 insertions(+), 6 deletions(-) + +diff --git a/Sources/FoundationEssentials/Calendar/Calendar.swift b/Sources/FoundationEssentials/Calendar/Calendar.swift +index 9de55b3..257b742 100644 +--- a/Sources/FoundationEssentials/Calendar/Calendar.swift ++++ b/Sources/FoundationEssentials/Calendar/Calendar.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift +index 797a8e8..8c25c77 100644 +--- a/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift ++++ b/Sources/FoundationEssentials/Calendar/Calendar_Gregorian.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + +diff --git a/Sources/FoundationEssentials/Data/Data+Reading.swift b/Sources/FoundationEssentials/Data/Data+Reading.swift +index 2540b14..48b9521 100644 +--- a/Sources/FoundationEssentials/Data/Data+Reading.swift ++++ b/Sources/FoundationEssentials/Data/Data+Reading.swift +@@ -27,6 +27,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + func _fgetxattr(_ fd: Int32, _ name: UnsafePointer!, _ value: UnsafeMutableRawPointer!, _ size: Int, _ position: UInt32, _ options: Int32) -> Int { +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index 78a9a98..1acd412 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -29,6 +29,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !NO_FILESYSTEM +diff --git a/Sources/FoundationEssentials/Data/Data.swift b/Sources/FoundationEssentials/Data/Data.swift +index 8bded85..ad3ac42 100644 +--- a/Sources/FoundationEssentials/Data/Data.swift ++++ b/Sources/FoundationEssentials/Data/Data.swift +@@ -76,6 +76,8 @@ import Glibc + import Musl + #elseif canImport(ucrt) + import ucrt ++#elseif canImport(WASILibc) ++import WASILibc + #endif + + #if os(Windows) +@@ -580,11 +582,11 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + @usableFromInline + @frozen + internal struct InlineData : Sendable { +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, + UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) //len //enum + @usableFromInline var bytes: Buffer +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + @usableFromInline typealias Buffer = (UInt8, UInt8, UInt8, UInt8, + UInt8, UInt8) //len //enum + @usableFromInline var bytes: Buffer +@@ -615,9 +617,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + @inlinable // This is @inlinable as a trivial initializer. + init(count: Int = 0) { + assert(count <= MemoryLayout.size) +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + bytes = (UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0), UInt8(0)) + #else + #error ("Unsupported architecture: initialization for Buffer is required for this architecture") +@@ -802,9 +804,9 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect + } + } + +-#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) ++#if _pointerBitWidth(_64) + @usableFromInline internal typealias HalfInt = Int32 +-#elseif arch(i386) || arch(arm) || arch(arm64_32) ++#elseif _pointerBitWidth(_32) + @usableFromInline internal typealias HalfInt = Int16 + #else + #error ("Unsupported architecture: a definition of half of the pointer sized Int needs to be defined for this architecture") +diff --git a/Sources/FoundationEssentials/Date.swift b/Sources/FoundationEssentials/Date.swift +index b65066f..37548e4 100644 +--- a/Sources/FoundationEssentials/Date.swift ++++ b/Sources/FoundationEssentials/Date.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif canImport(WinSDK) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift +index 7b35f11..eb344b2 100644 +--- a/Sources/FoundationEssentials/Decimal/Decimal+Math.swift ++++ b/Sources/FoundationEssentials/Decimal/Decimal+Math.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif canImport(CRT) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + private let powerOfTen: [Decimal.VariableLengthInteger] = [ +diff --git a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift +index d9b2497..586c781 100644 +--- a/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift ++++ b/Sources/FoundationEssentials/Error/CocoaError+FilePath.swift +@@ -24,6 +24,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + extension CocoaError.Code { +diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +index 048cd29..c940a30 100644 +--- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift ++++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +@@ -21,6 +21,8 @@ + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift +index 991c5e8..9896b35 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Basics.swift +@@ -21,6 +21,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if os(Windows) +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +index ed1b445..b987ee8 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift +@@ -28,6 +28,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationCShims +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index 943ac9e..1999b6a 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -29,6 +29,9 @@ internal import _FoundationCShims + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++internal import _FoundationCShims ++import WASILibc + #endif + + extension Date { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift +index a1355e7..12d32e5 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+SymbolicLinks.swift +@@ -23,6 +23,8 @@ import Musl + import CRT + import WinSDK + internal import _FoundationCShims ++#elseif os(WASI) ++import WASILibc + #endif + + extension _FileManagerImpl { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index 4f42f19..4d487b5 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -34,6 +34,8 @@ internal import _FoundationCShims + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if os(Windows) +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 8734fc0..6ba5bb7 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -21,6 +21,8 @@ import Musl + #elseif os(Windows) + import CRT + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if FOUNDATION_FRAMEWORK +diff --git a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift +index 43e9fcd..663509d 100644 +--- a/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift ++++ b/Sources/FoundationEssentials/Formatting/BinaryInteger+NumericStringRepresentation.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif os(Windows) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + // MARK: - BinaryInteger + Numeric string representation +diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +index 30591b5..93eabb9 100644 +--- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift ++++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +@@ -23,6 +23,8 @@ import Glibc + import Musl + #elseif os(Windows) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + #if !NO_PROCESS +diff --git a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift +index c042820..61b6d80 100644 +--- a/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift ++++ b/Sources/FoundationEssentials/PropertyList/OpenStepPlist.swift +@@ -18,6 +18,8 @@ import Bionic + import Glibc + #elseif canImport(Musl) + import Musl ++#elseif os(WASI) ++import WASILibc + #endif + + #if canImport(CRT) +diff --git a/Sources/FoundationEssentials/String/String+Path.swift b/Sources/FoundationEssentials/String/String+Path.swift +index aaf62d1..7047b9b 100644 +--- a/Sources/FoundationEssentials/String/String+Path.swift ++++ b/Sources/FoundationEssentials/String/String+Path.swift +@@ -20,6 +20,8 @@ import Glibc + import Musl + #elseif os(Windows) + import WinSDK ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationCShims +diff --git a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift +index 0d3c371..01895b8 100644 +--- a/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift ++++ b/Sources/FoundationInternationalization/Calendar/Calendar_ICU.swift +@@ -24,6 +24,8 @@ import Musl + import CRT + #elseif canImport(Darwin) + import Darwin ++#elseif os(WASI) ++import WASILibc + #endif + + internal import _FoundationICU +diff --git a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift +index a94f571..dfe2fad 100644 +--- a/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift ++++ b/Sources/FoundationInternationalization/Formatting/Duration+Formatting.swift +@@ -24,6 +24,8 @@ import Glibc + import Musl + #elseif os(Windows) + import CRT ++#elseif os(WASI) ++import WASILibc + #endif + + @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0010-Fix-operatingSystemVersion-on-WASI-782.patch b/schemes/release-6.0/swift-foundation/0010-Fix-operatingSystemVersion-on-WASI-782.patch new file mode 100644 index 00000000..e3b56f2b --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0010-Fix-operatingSystemVersion-on-WASI-782.patch @@ -0,0 +1,27 @@ +From b68fa674093de0bc6225a299045dbd2ac4dcf340 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:15:23 +0900 +Subject: [PATCH] Fix `operatingSystemVersion` on WASI (#782) + +The `operatingSystemVersion` property type is a tuple but the it was +returning an `OperatingSystemVersion` instance on unknown platforms. +--- + Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +index 93eabb9..74d60a4 100644 +--- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift ++++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +@@ -393,7 +393,7 @@ extension _ProcessInfo { + patch: Int(osVersionInfo.dwBuildNumber) + ) + #else +- return OperatingSystemVersion(majorVersion: -1, minorVersion: 0, patchVersion: 0) ++ return (major: -1, minor: 0, patch: 0) + #endif + } + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0011-Implement-_copyRegularFile-for-WASI-without-sendfile.patch b/schemes/release-6.0/swift-foundation/0011-Implement-_copyRegularFile-for-WASI-without-sendfile.patch new file mode 100644 index 00000000..130ff6da --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0011-Implement-_copyRegularFile-for-WASI-without-sendfile.patch @@ -0,0 +1,52 @@ +From 66bb63386eb0d3a1b9aba1759f9e0d965884eb4d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 02:50:04 +0900 +Subject: [PATCH] Implement `_copyRegularFile` for WASI without `sendfile` + (#787) + +WASI doesn't have `sendfile`, so we need to implement the copy in user +space with `read` and `write`. It's not as efficient as `sendfile`, but +it's the best we can do. +--- + .../FileManager/FileOperations.swift | 19 +++++++++++++++++++ + 1 file changed, 19 insertions(+) + +diff --git a/Sources/FoundationEssentials/FileManager/FileOperations.swift b/Sources/FoundationEssentials/FileManager/FileOperations.swift +index 6ba5bb7..41f0b65 100644 +--- a/Sources/FoundationEssentials/FileManager/FileOperations.swift ++++ b/Sources/FoundationEssentials/FileManager/FileOperations.swift +@@ -903,12 +903,31 @@ enum _FileOperations { + } + var current: off_t = 0 + ++ #if os(WASI) ++ // WASI doesn't have sendfile, so we need to do it in user space with read/write ++ try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: chunkSize) { buffer in ++ while current < total { ++ let readSize = Swift.min(total - Int(current), chunkSize) ++ let bytesRead = read(srcfd, buffer.baseAddress, readSize) ++ guard bytesRead >= 0 else { ++ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) ++ return ++ } ++ guard write(dstfd, buffer.baseAddress, bytesRead) == bytesRead else { ++ try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) ++ return ++ } ++ current += off_t(bytesRead) ++ } ++ } ++ #else + while current < total { + guard sendfile(dstfd, srcfd, ¤t, Swift.min(total - Int(current), chunkSize)) != -1 else { + try delegate.throwIfNecessary(errno, String(cString: srcPtr), String(cString: dstPtr)) + return + } + } ++ #endif + } + #endif + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0012-Guard-out-extended-or-fs-attributes-related-code-on-.patch b/schemes/release-6.0/swift-foundation/0012-Guard-out-extended-or-fs-attributes-related-code-on-.patch new file mode 100644 index 00000000..30988024 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0012-Guard-out-extended-or-fs-attributes-related-code-on-.patch @@ -0,0 +1,79 @@ +From d177f42ef0423eb2daacb0e2b32434214f766a78 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:20:05 +0900 +Subject: [PATCH] Guard out extended or fs attributes related code on WASI + (#784) + +This commit guards out the extended attributes and file system +attributes related code on WASI as WASI does not support these +features. Just return nothing or ignore the set request. +--- + .../FileManager/FileManager+Files.swift | 13 +++++++++++-- + .../FileManager/FileManager+Utilities.swift | 2 +- + 2 files changed, 12 insertions(+), 3 deletions(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index 1999b6a..b1e6f34 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -497,7 +497,7 @@ extension _FileManagerImpl { + #endif + } + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + private func _extendedAttribute(_ key: UnsafePointer, at path: UnsafePointer, followSymlinks: Bool) throws -> Data? { + #if canImport(Darwin) + var size = getxattr(path, key, nil, 0, 0, followSymlinks ? 0 : XATTR_NOFOLLOW) +@@ -651,10 +651,11 @@ extension _FileManagerImpl { + + var attributes = statAtPath.fileAttributes + try? Self._catInfo(for: URL(filePath: path, directoryHint: .isDirectory), statInfo: statAtPath, into: &attributes) +- ++ #if !os(WASI) // WASI does not support extended attributes + if let extendedAttrs = try? _extendedAttributes(at: fsRep, followSymlinks: false) { + attributes[._extendedAttributes] = extendedAttrs + } ++ #endif + + #if !targetEnvironment(simulator) && FOUNDATION_FRAMEWORK + if statAtPath.isRegular || statAtPath.isDirectory { +@@ -716,6 +717,9 @@ extension _FileManagerImpl { + ] + } + } ++#elseif os(WASI) ++ // WASI does not support file system attributes ++ return [:] + #else + try fileManager.withFileSystemRepresentation(for: path) { rep in + guard let rep else { +@@ -948,7 +952,12 @@ extension _FileManagerImpl { + try Self._setCatInfoAttributes(attributes, path: path) + + if let extendedAttrs = attributes[.init("NSFileExtendedAttributes")] as? [String : Data] { ++ #if os(WASI) ++ // WASI does not support extended attributes ++ throw CocoaError.errorWithFilePath(.featureUnsupported, path) ++ #else + try Self._setAttributes(extendedAttrs, at: fileSystemRepresentation, followSymLinks: false) ++ #endif + } + + if let date = attributes[.modificationDate] as? Date { +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +index 4d487b5..036f50c 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift +@@ -178,7 +178,7 @@ extension _FileManagerImpl { + #endif + } + +-#if !os(Windows) ++#if !os(Windows) && !os(WASI) + static func _setAttribute(_ key: UnsafePointer, value: Data, at path: UnsafePointer, followSymLinks: Bool) throws { + try value.withUnsafeBytes { buffer in + #if canImport(Darwin) +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0013-Skip-sticky-bit-check-in-isDeletableFile-on-WASI-785.patch b/schemes/release-6.0/swift-foundation/0013-Skip-sticky-bit-check-in-isDeletableFile-on-WASI-785.patch new file mode 100644 index 00000000..81fed4f2 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0013-Skip-sticky-bit-check-in-isDeletableFile-on-WASI-785.patch @@ -0,0 +1,27 @@ +From 6e6fdb07fd6facfcf49e239a0cd118967026a764 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:30:17 +0900 +Subject: [PATCH] Skip sticky-bit check in `isDeletableFile` on WASI (#785) + +WASI does not surface the sticky bit and getuid, so we cannot check +whether the file is actually deletable before attempting to delete it. +--- + .../FoundationEssentials/FileManager/FileManager+Files.swift | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +index b1e6f34..9cd9752 100644 +--- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift ++++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +@@ -474,7 +474,7 @@ extension _FileManagerImpl { + parent = fileManager.currentDirectoryPath + } + +-#if os(Windows) ++#if os(Windows) || os(WASI) + return fileManager.isWritableFile(atPath: parent) && fileManager.isWritableFile(atPath: path) + #else + guard fileManager.isWritableFile(atPath: parent), +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0014-Port-LockedState-and-_ThreadLocal-to-WASI-without-an.patch b/schemes/release-6.0/swift-foundation/0014-Port-LockedState-and-_ThreadLocal-to-WASI-without-an.patch new file mode 100644 index 00000000..99130cf8 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0014-Port-LockedState-and-_ThreadLocal-to-WASI-without-an.patch @@ -0,0 +1,95 @@ +From 65d09d9b7a536833e3e57f7545dcfe7be5d02b24 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 05:57:20 +0900 +Subject: [PATCH] Port `LockedState` and `_ThreadLocal` to WASI without any + locking (#780) + +--- + Sources/FoundationEssentials/LockedState.swift | 9 +++++++++ + Sources/FoundationEssentials/_ThreadLocal.swift | 8 ++++++++ + 2 files changed, 17 insertions(+) + +diff --git a/Sources/FoundationEssentials/LockedState.swift b/Sources/FoundationEssentials/LockedState.swift +index 6eb9ad8..4e6aefa 100644 +--- a/Sources/FoundationEssentials/LockedState.swift ++++ b/Sources/FoundationEssentials/LockedState.swift +@@ -35,6 +35,9 @@ package struct LockedState { + typealias Primitive = pthread_mutex_t + #elseif canImport(WinSDK) + typealias Primitive = SRWLOCK ++#elseif os(WASI) ++ // WASI is single-threaded, so we don't need a lock. ++ typealias Primitive = Void + #endif + + typealias PlatformLock = UnsafeMutablePointer +@@ -47,6 +50,8 @@ package struct LockedState { + pthread_mutex_init(platformLock, nil) + #elseif canImport(WinSDK) + InitializeSRWLock(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + +@@ -64,6 +69,8 @@ package struct LockedState { + pthread_mutex_lock(platformLock) + #elseif canImport(WinSDK) + AcquireSRWLockExclusive(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + +@@ -74,6 +81,8 @@ package struct LockedState { + pthread_mutex_unlock(platformLock) + #elseif canImport(WinSDK) + ReleaseSRWLockExclusive(platformLock) ++#elseif os(WASI) ++ // no-op + #endif + } + } +diff --git a/Sources/FoundationEssentials/_ThreadLocal.swift b/Sources/FoundationEssentials/_ThreadLocal.swift +index aea9c41..ffe010c 100644 +--- a/Sources/FoundationEssentials/_ThreadLocal.swift ++++ b/Sources/FoundationEssentials/_ThreadLocal.swift +@@ -32,6 +32,8 @@ struct _ThreadLocal { + fileprivate typealias PlatformKey = tss_t + #elseif canImport(WinSDK) + fileprivate typealias PlatformKey = DWORD ++#elseif os(WASI) ++ fileprivate typealias PlatformKey = UnsafeMutablePointer + #endif + + struct Key { +@@ -48,6 +50,8 @@ struct _ThreadLocal { + self.key = key + #elseif canImport(WinSDK) + key = FlsAlloc(nil) ++#elseif os(WASI) ++ key = UnsafeMutablePointer.allocate(capacity: 1) + #endif + } + } +@@ -60,6 +64,8 @@ struct _ThreadLocal { + tss_get(key) + #elseif canImport(WinSDK) + FlsGetValue(key) ++#elseif os(WASI) ++ key.pointee + #endif + } + +@@ -70,6 +76,8 @@ struct _ThreadLocal { + tss_set(key, newValue) + #elseif canImport(WinSDK) + FlsSetValue(key, newValue) ++#elseif os(WASI) ++ key.pointee = newValue + #endif + } + } +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0015-Throw-.featureUnsupported-when-attempting-to-create-.patch b/schemes/release-6.0/swift-foundation/0015-Throw-.featureUnsupported-when-attempting-to-create-.patch new file mode 100644 index 00000000..a0ac0f09 --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0015-Throw-.featureUnsupported-when-attempting-to-create-.patch @@ -0,0 +1,39 @@ +From f8a181d7ffd5de758c0c57f00aa69016f3fecf0e Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:12:39 +0900 +Subject: [PATCH] Throw `.featureUnsupported` when attempting to create temp + files on WASI (#779) + +WASI does not have temp directory concept, and does not provide mktemp +family of functions, so attempting to create a temporary file should be +considered a feature unsupported. +--- + Sources/FoundationEssentials/Data/Data+Writing.swift | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/Sources/FoundationEssentials/Data/Data+Writing.swift b/Sources/FoundationEssentials/Data/Data+Writing.swift +index 1acd412..0256e51 100644 +--- a/Sources/FoundationEssentials/Data/Data+Writing.swift ++++ b/Sources/FoundationEssentials/Data/Data+Writing.swift +@@ -131,6 +131,10 @@ private func cleanupTemporaryDirectory(at inPath: String?) { + + /// Caller is responsible for calling `close` on the `Int32` file descriptor. + private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, prefix: String, options: Data.WritingOptions) throws -> (Int32, String) { ++#if os(WASI) ++ // WASI does not have temp directories ++ throw CocoaError(.featureUnsupported) ++#else + var directoryPath = destinationPath + if !directoryPath.isEmpty && directoryPath.last! != "/" { + directoryPath.append("/") +@@ -185,6 +189,7 @@ private func createTemporaryFile(at destinationPath: String, inPath: PathOrURL, + } + } + } while true ++#endif // os(WASI) + } + + /// Returns `(file descriptor, temporary file path, temporary directory path)` +-- +2.43.2 + diff --git a/schemes/release-6.0/swift-foundation/0016-Exclude-EREMOTE-definition-for-WASI-platform-778.patch b/schemes/release-6.0/swift-foundation/0016-Exclude-EREMOTE-definition-for-WASI-platform-778.patch new file mode 100644 index 00000000..621c6a8a --- /dev/null +++ b/schemes/release-6.0/swift-foundation/0016-Exclude-EREMOTE-definition-for-WASI-platform-778.patch @@ -0,0 +1,31 @@ +From 5d333819a3ffb36996b4332750d5bd25c1ecea00 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Fri, 2 Aug 2024 01:06:57 +0900 +Subject: [PATCH] Exclude EREMOTE definition for WASI platform (#778) + +WASI does not define the EREMOTE error code. +--- + Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +index c940a30..e1bfffa 100644 +--- a/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift ++++ b/Sources/FoundationEssentials/Error/ErrorCodes+POSIX.swift +@@ -469,11 +469,13 @@ extension POSIXError { + return .ESTALE + } + ++ #if !os(WASI) + /// Too many levels of remote in path. + public static var EREMOTE: POSIXErrorCode { + return .EREMOTE + } + #endif ++ #endif + + #if canImport(Darwin) + /// RPC struct is bad. +-- +2.43.2 + From 00a5848b678ae3b89c68fb5c323c225a6f2f8e4c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 16:33:25 +0000 Subject: [PATCH 6/9] Apply Synchronization fixes --- ...te-errno-as-SwiftPrivate-by-apinotes.patch | 97 +++++++++++++++++++ ...Synchronization-Fix-Wasm-mutex-build.patch | 29 ++++++ ...Fix-wasm-atomic-intrinsic-declaratio.patch | 51 ++++++++++ ...Skip-atomic-operations-in-single-thr.patch | 43 ++++++++ 4 files changed, 220 insertions(+) create mode 100644 schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch create mode 100644 schemes/release-6.0/swift/0003-Synchronization-Fix-Wasm-mutex-build.patch create mode 100644 schemes/release-6.0/swift/0004-Synchronization-Fix-wasm-atomic-intrinsic-declaratio.patch create mode 100644 schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch diff --git a/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch b/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch new file mode 100644 index 00000000..cd959c81 --- /dev/null +++ b/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch @@ -0,0 +1,97 @@ +From c6b128ca7b89d0c42563d4a1d53602fcfd48bea6 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Sat, 10 Aug 2024 13:50:57 +0000 +Subject: [PATCH] [wasm] Annotate errno as SwiftPrivate by apinotes + +This patch adds an apinotes file for SwiftWASILibc clang module to mark +`errno` macro hidden from Swift code. This resolves ambiguity between +the C macro definition and the Swift wrapper in WASILibc overlay module. + +This change installs the apinotes file to the resource directories for +both lib/swift/apinotes and lib/swift_static/apinotes. +--- + stdlib/public/Platform/CMakeLists.txt | 28 +++++++++++++++++++ + stdlib/public/Platform/SwiftWASILibc.apinotes | 5 ++++ + test/stdlib/WASILibcAPI.swift | 19 +++++++++++++ + 3 files changed, 52 insertions(+) + create mode 100644 stdlib/public/Platform/SwiftWASILibc.apinotes + create mode 100644 test/stdlib/WASILibcAPI.swift + +diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt +index 7e709923ffc..3d798e48304 100644 +--- a/stdlib/public/Platform/CMakeLists.txt ++++ b/stdlib/public/Platform/CMakeLists.txt +@@ -508,6 +508,34 @@ if("WASI" IN_LIST SWIFT_SDKS) + DESTINATION "lib/swift_static/${arch_subdir}" + COMPONENT sdk-overlay) + endif() ++ ++ set(wasilibc_apinotes_source "SwiftWASILibc.apinotes") ++ add_custom_command_target( ++ copy_wasilibc_apinotes_resource ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "make_directory" ${SWIFTLIB_DIR}/apinotes ${SWIFTSTATICLIB_DIR}/apinotes ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "copy_if_different" ++ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTLIB_DIR}/apinotes ++ COMMAND ++ "${CMAKE_COMMAND}" "-E" "copy_if_different" ++ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTSTATICLIB_DIR}/apinotes ++ OUTPUT ++ ${SWIFTLIB_DIR}/apinotes/${wasilibc_apinotes_source} ++ ${SWIFTSTATICLIB_DIR}/apinotes/${wasilibc_apinotes_source} ++ COMMENT "Copying WASILibc API notes to resource directories") ++ ++ list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_apinotes_resource}) ++ add_dependencies(sdk-overlay ${copy_wasilibc_apinotes_resource}) ++ swift_install_in_component(FILES "${wasilibc_apinotes_source}" ++ DESTINATION "lib/swift/apinotes" ++ COMPONENT sdk-overlay) ++ if(SWIFT_BUILD_STATIC_STDLIB) ++ swift_install_in_component(FILES "${wasilibc_apinotes_source}" ++ DESTINATION "lib/swift_static/apinotes" ++ COMPONENT sdk-overlay) ++ endif() ++ + endforeach() + endif() + add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list}) +diff --git a/stdlib/public/Platform/SwiftWASILibc.apinotes b/stdlib/public/Platform/SwiftWASILibc.apinotes +new file mode 100644 +index 00000000000..001acc7ebb5 +--- /dev/null ++++ b/stdlib/public/Platform/SwiftWASILibc.apinotes +@@ -0,0 +1,5 @@ ++Name: SwiftWASILibc ++Globals: ++ # errno macro is importable but we provide explicit Swift wrapper ++ - Name: errno ++ SwiftPrivate: true +diff --git a/test/stdlib/WASILibcAPI.swift b/test/stdlib/WASILibcAPI.swift +new file mode 100644 +index 00000000000..fe599bb9f38 +--- /dev/null ++++ b/test/stdlib/WASILibcAPI.swift +@@ -0,0 +1,19 @@ ++// RUN: %target-swift-frontend -typecheck -swift-version 6 %s -verify ++// REQUIRES: executable_test ++// REQUIRES: OS=wasi ++ ++import WASILibc ++ ++// errno is a global thread-local variable, so it should be accessible ++// from any context. ++ ++enum TestErrno { ++ static func testSyncContext() { ++ _ = errno ++ errno = 0 ++ } ++ static func testAsyncContext() async { ++ _ = errno ++ errno = 0 ++ } ++} +-- +2.43.2 + diff --git a/schemes/release-6.0/swift/0003-Synchronization-Fix-Wasm-mutex-build.patch b/schemes/release-6.0/swift/0003-Synchronization-Fix-Wasm-mutex-build.patch new file mode 100644 index 00000000..89bdfe57 --- /dev/null +++ b/schemes/release-6.0/swift/0003-Synchronization-Fix-Wasm-mutex-build.patch @@ -0,0 +1,29 @@ +From 261c0c6bf343aee915e5969e417c13e7bdd86912 Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Wed, 5 Jun 2024 17:15:43 +0000 +Subject: [PATCH] [Synchronization] Fix Wasm mutex build + +This change fixes the following build error happening on Wasm: +``` +error: referencing instance method '_wait(expected:)' on 'Atomic' requires the types '_MutexHandle.State' and 'UInt32' be equivalent +``` +--- + stdlib/public/Synchronization/Mutex/WasmImpl.swift | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +index 807eb3d8c64..8fa84b0ff48 100644 +--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift ++++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +@@ -23,7 +23,7 @@ internal func _swift_stdlib_wait( + @_extern(c, "llvm.wasm32.memory.atomic.notify") + internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) + +-extension Atomic where Value == UInt32 { ++extension Atomic where Value == _MutexHandle.State { + internal borrowing func _wait(expected: _MutexHandle.State) { + _swift_stdlib_wait( + on: .init(_rawAddress), +-- +2.43.2 + diff --git a/schemes/release-6.0/swift/0004-Synchronization-Fix-wasm-atomic-intrinsic-declaratio.patch b/schemes/release-6.0/swift/0004-Synchronization-Fix-wasm-atomic-intrinsic-declaratio.patch new file mode 100644 index 00000000..bb360078 --- /dev/null +++ b/schemes/release-6.0/swift/0004-Synchronization-Fix-wasm-atomic-intrinsic-declaratio.patch @@ -0,0 +1,51 @@ +From bcbf0f86c459cb9b8ffefc7bea47cd4bea04fbeb Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Thu, 6 Jun 2024 04:13:08 +0000 +Subject: [PATCH] [Synchronization] Fix wasm atomic intrinsic declarations + +Otherwise, isel will not be able to select the desired atomic +instructions. +--- + stdlib/public/Synchronization/Mutex/WasmImpl.swift | 10 +++++----- + 1 file changed, 5 insertions(+), 5 deletions(-) + +diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +index 8fa84b0ff48..d2729f4d54c 100644 +--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift ++++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +@@ -13,19 +13,19 @@ + // Note: All atomic accesses on WASM are sequentially consistent regardless of + // what ordering we tell LLVM to use. + +-@_extern(c, "llvm.wasm32.memory.atomic.wait32") ++@_extern(c, "llvm.wasm.memory.atomic.wait32") + internal func _swift_stdlib_wait( + on: UnsafePointer, + expected: UInt32, + timeout: Int64 + ) -> UInt32 + +-@_extern(c, "llvm.wasm32.memory.atomic.notify") +-internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) ++@_extern(c, "llvm.wasm.memory.atomic.notify") ++internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) -> UInt32 + + extension Atomic where Value == _MutexHandle.State { + internal borrowing func _wait(expected: _MutexHandle.State) { +- _swift_stdlib_wait( ++ _ = _swift_stdlib_wait( + on: .init(_rawAddress), + expected: expected.rawValue, + +@@ -36,7 +36,7 @@ extension Atomic where Value == _MutexHandle.State { + + internal borrowing func _wake() { + // Only wake up 1 thread +- _swift_stdlib_wake(on: .init(_rawAddress), count: 1) ++ _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1) + } + } + +-- +2.43.2 + diff --git a/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch b/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch new file mode 100644 index 00000000..cbdf7754 --- /dev/null +++ b/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch @@ -0,0 +1,43 @@ +From def82827e4bb3274e0ac3a86e761cb46b2a28a6d Mon Sep 17 00:00:00 2001 +From: Yuta Saito +Date: Thu, 6 Jun 2024 04:14:46 +0000 +Subject: [PATCH] [Synchronization] Skip atomic operations in single-threaded + mode on WebAssembly + +Use of atomics instructions requires the support of threads proposal and +it's not widely supported yet. So we should enable actual atomic +operations only when targeting wasm32-uknown-wasip1-threads. +--- + stdlib/public/Synchronization/Mutex/WasmImpl.swift | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/stdlib/public/Synchronization/Mutex/WasmImpl.swift b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +index d2729f4d54c..6c58628c40e 100644 +--- a/stdlib/public/Synchronization/Mutex/WasmImpl.swift ++++ b/stdlib/public/Synchronization/Mutex/WasmImpl.swift +@@ -25,6 +25,7 @@ internal func _swift_stdlib_wake(on: UnsafePointer, count: UInt32) -> UI + + extension Atomic where Value == _MutexHandle.State { + internal borrowing func _wait(expected: _MutexHandle.State) { ++ #if _runtime(_multithreaded) + _ = _swift_stdlib_wait( + on: .init(_rawAddress), + expected: expected.rawValue, +@@ -32,11 +33,14 @@ extension Atomic where Value == _MutexHandle.State { + // A timeout of < 0 means indefinitely. + timeout: -1 + ) ++ #endif + } + + internal borrowing func _wake() { ++ #if _runtime(_multithreaded) + // Only wake up 1 thread + _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1) ++ #endif + } + } + +-- +2.43.2 + From ce089c1da9d9600b9cabe802714c8a1610642964 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 16:55:23 +0000 Subject: [PATCH 7/9] Remove duplicated patch --- ...te-errno-as-SwiftPrivate-by-apinotes.patch | 97 ------------------- 1 file changed, 97 deletions(-) delete mode 100644 schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch diff --git a/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch b/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch deleted file mode 100644 index cd959c81..00000000 --- a/schemes/release-6.0/swift/0002-wasm-Annotate-errno-as-SwiftPrivate-by-apinotes.patch +++ /dev/null @@ -1,97 +0,0 @@ -From c6b128ca7b89d0c42563d4a1d53602fcfd48bea6 Mon Sep 17 00:00:00 2001 -From: Yuta Saito -Date: Sat, 10 Aug 2024 13:50:57 +0000 -Subject: [PATCH] [wasm] Annotate errno as SwiftPrivate by apinotes - -This patch adds an apinotes file for SwiftWASILibc clang module to mark -`errno` macro hidden from Swift code. This resolves ambiguity between -the C macro definition and the Swift wrapper in WASILibc overlay module. - -This change installs the apinotes file to the resource directories for -both lib/swift/apinotes and lib/swift_static/apinotes. ---- - stdlib/public/Platform/CMakeLists.txt | 28 +++++++++++++++++++ - stdlib/public/Platform/SwiftWASILibc.apinotes | 5 ++++ - test/stdlib/WASILibcAPI.swift | 19 +++++++++++++ - 3 files changed, 52 insertions(+) - create mode 100644 stdlib/public/Platform/SwiftWASILibc.apinotes - create mode 100644 test/stdlib/WASILibcAPI.swift - -diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt -index 7e709923ffc..3d798e48304 100644 ---- a/stdlib/public/Platform/CMakeLists.txt -+++ b/stdlib/public/Platform/CMakeLists.txt -@@ -508,6 +508,34 @@ if("WASI" IN_LIST SWIFT_SDKS) - DESTINATION "lib/swift_static/${arch_subdir}" - COMPONENT sdk-overlay) - endif() -+ -+ set(wasilibc_apinotes_source "SwiftWASILibc.apinotes") -+ add_custom_command_target( -+ copy_wasilibc_apinotes_resource -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "make_directory" ${SWIFTLIB_DIR}/apinotes ${SWIFTSTATICLIB_DIR}/apinotes -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "copy_if_different" -+ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTLIB_DIR}/apinotes -+ COMMAND -+ "${CMAKE_COMMAND}" "-E" "copy_if_different" -+ "${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_apinotes_source}" ${SWIFTSTATICLIB_DIR}/apinotes -+ OUTPUT -+ ${SWIFTLIB_DIR}/apinotes/${wasilibc_apinotes_source} -+ ${SWIFTSTATICLIB_DIR}/apinotes/${wasilibc_apinotes_source} -+ COMMENT "Copying WASILibc API notes to resource directories") -+ -+ list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_apinotes_resource}) -+ add_dependencies(sdk-overlay ${copy_wasilibc_apinotes_resource}) -+ swift_install_in_component(FILES "${wasilibc_apinotes_source}" -+ DESTINATION "lib/swift/apinotes" -+ COMPONENT sdk-overlay) -+ if(SWIFT_BUILD_STATIC_STDLIB) -+ swift_install_in_component(FILES "${wasilibc_apinotes_source}" -+ DESTINATION "lib/swift_static/apinotes" -+ COMPONENT sdk-overlay) -+ endif() -+ - endforeach() - endif() - add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list}) -diff --git a/stdlib/public/Platform/SwiftWASILibc.apinotes b/stdlib/public/Platform/SwiftWASILibc.apinotes -new file mode 100644 -index 00000000000..001acc7ebb5 ---- /dev/null -+++ b/stdlib/public/Platform/SwiftWASILibc.apinotes -@@ -0,0 +1,5 @@ -+Name: SwiftWASILibc -+Globals: -+ # errno macro is importable but we provide explicit Swift wrapper -+ - Name: errno -+ SwiftPrivate: true -diff --git a/test/stdlib/WASILibcAPI.swift b/test/stdlib/WASILibcAPI.swift -new file mode 100644 -index 00000000000..fe599bb9f38 ---- /dev/null -+++ b/test/stdlib/WASILibcAPI.swift -@@ -0,0 +1,19 @@ -+// RUN: %target-swift-frontend -typecheck -swift-version 6 %s -verify -+// REQUIRES: executable_test -+// REQUIRES: OS=wasi -+ -+import WASILibc -+ -+// errno is a global thread-local variable, so it should be accessible -+// from any context. -+ -+enum TestErrno { -+ static func testSyncContext() { -+ _ = errno -+ errno = 0 -+ } -+ static func testAsyncContext() async { -+ _ = errno -+ errno = 0 -+ } -+} --- -2.43.2 - From 90aa14592cd4179124b96d0d7828fd589057f65a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 17:17:29 +0000 Subject: [PATCH 8/9] `_runtime(_multithreaded)` is not available in 6.0 --- ...Synchronization-Skip-atomic-operations-in-single-thr.patch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch b/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch index cbdf7754..9aaabf59 100644 --- a/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch +++ b/schemes/release-6.0/swift/0005-Synchronization-Skip-atomic-operations-in-single-thr.patch @@ -19,7 +19,7 @@ index d2729f4d54c..6c58628c40e 100644 extension Atomic where Value == _MutexHandle.State { internal borrowing func _wait(expected: _MutexHandle.State) { -+ #if _runtime(_multithreaded) ++ #if false _ = _swift_stdlib_wait( on: .init(_rawAddress), expected: expected.rawValue, @@ -31,7 +31,7 @@ index d2729f4d54c..6c58628c40e 100644 } internal borrowing func _wake() { -+ #if _runtime(_multithreaded) ++ #if false // Only wake up 1 thread _ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1) + #endif From 93b08c2f843d05e2e89e2ce29fd07da5ec8fc2c8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Aug 2024 17:46:45 +0000 Subject: [PATCH 9/9] Update CMake version in CI image --- .github/scripts/build-matrix.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/scripts/build-matrix.rb b/.github/scripts/build-matrix.rb index 2bb8c2f0..3acbca78 100644 --- a/.github/scripts/build-matrix.rb +++ b/.github/scripts/build-matrix.rb @@ -27,7 +27,7 @@ "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", # "release-5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:cc1b99e352ee207da2c75c7bcf81aa8b1d2c08215fd1d05dc0777c40a62f31f1", "release-5.10": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:cc1b99e352ee207da2c75c7bcf81aa8b1d2c08215fd1d05dc0777c40a62f31f1", - "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:cc1b99e352ee207da2c75c7bcf81aa8b1d2c08215fd1d05dc0777c40a62f31f1", + "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:9007661fb6d57ebef902618e831458e88068350b0ecc8d85e56441d25a9f4312", }, "run_stdlib_test": true, "run_full_test": false, @@ -45,7 +45,7 @@ "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04", # "release-5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:adfa0a8fbc6e5cc7ce5e38a5a9406d4fa5c557871204a65f0690478022d6b359", "release-5.10": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:adfa0a8fbc6e5cc7ce5e38a5a9406d4fa5c557871204a65f0690478022d6b359", - "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:adfa0a8fbc6e5cc7ce5e38a5a9406d4fa5c557871204a65f0690478022d6b359", + "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:8060adba9c044b1fc751b9671167320c9cc0094d0a45d7b8fedb707c49b764c2", }, "run_stdlib_test": true, "run_full_test": false, @@ -63,7 +63,7 @@ "main": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2", # "release-5.9": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2@sha256:d5264ac43e935249b1c8777f6809ebbd2836cb0e8f7dac3bfeeb0b3cdb479b70", "release-5.10": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2@sha256:d5264ac43e935249b1c8777f6809ebbd2836cb0e8f7dac3bfeeb0b3cdb479b70", - "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2@sha256:d5264ac43e935249b1c8777f6809ebbd2836cb0e8f7dac3bfeeb0b3cdb479b70", + "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-amazon-linux-2@sha256:fc95912c595faecacbb869635fa871f8f31b8d6ed2f0df6792b53628db7ada94", }, "run_stdlib_test": false, "run_full_test": false, @@ -116,7 +116,7 @@ "target": "ubuntu22.04_x86_64", "containers": { "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-22.04", - "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:adfa0a8fbc6e5cc7ce5e38a5a9406d4fa5c557871204a65f0690478022d6b359", + "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:8060adba9c044b1fc751b9671167320c9cc0094d0a45d7b8fedb707c49b764c2", }, "run_stdlib_test": true, "run_full_test": false, @@ -187,7 +187,7 @@ def main "main": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04", # "release-5.9": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:0e04dd550557d9f4f773bda55a6ac355c7c9696ea6efc3e59318bd49569aa00e", "release-5.10": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:0e04dd550557d9f4f773bda55a6ac355c7c9696ea6efc3e59318bd49569aa00e", - "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:0e04dd550557d9f4f773bda55a6ac355c7c9696ea6efc3e59318bd49569aa00e", + "release-6.0": "ghcr.io/swiftwasm/swift-ci:main-ubuntu-20.04@sha256:2a977060476a0f6f702d59ed0cd4e8506c7c1f1ed48d85a1a684d2d3fb62c297", }, "run_stdlib_test": false, "run_full_test": false,