Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions schemes/main/build/build-foundation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ WASI_SYSROOT_PATH=$5
TRIPLE="$6"

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-$TRIPLE"

FOUNDATION_BUILD="$SOURCE_PATH/build/WebAssembly/foundation-$TRIPLE"
FOUNDATION_MACROS_BUILD="$SOURCE_PATH/build/WebAssembly/foundation-macros-$TRIPLE"

swift_extra_flags=""
c_extra_flags=""
Expand All @@ -22,16 +20,6 @@ if [[ "$TRIPLE" == "wasm32-unknown-wasip1-threads" ]]; then
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" \
Expand All @@ -58,7 +46,7 @@ cmake -G Ninja \
-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" \
-D SwiftFoundation_MACRO="$SWIFT_BIN_DIR/../lib/swift/host/plugins" \
-B "$FOUNDATION_BUILD" \
"${SOURCE_PATH}/swift-corelibs-foundation"

Expand Down
2 changes: 1 addition & 1 deletion schemes/main/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"base-tag": "swift-DEVELOPMENT-SNAPSHOT-2025-03-25-a",
"base-tag": "swift-DEVELOPMENT-SNAPSHOT-2025-03-28-a",
"icu4c": [],
"libxml2": [
"https://github.com/swiftwasm/libxml2-wasm/releases/download/2.0.0/libxml2-wasm32-unknown-wasi.tar.gz",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
From 6e3a9e2ced5688bfe9e209633735bf6bdc85d64c Mon Sep 17 00:00:00 2001
From: Yuta Saito <[email protected]>
Date: Mon, 31 Mar 2025 11:07:42 +0000
Subject: [PATCH] [wasm] Gate atomic write option usages behind platform check

`Data.WritingOptions.atomic` is now unavailable on WASI: https://github.com/swiftlang/swift-foundation/pull/992
---
Sources/Foundation/NSData.swift | 11 +++++++++--
Sources/Foundation/NSString.swift | 7 ++++++-
2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/Sources/Foundation/NSData.swift b/Sources/Foundation/NSData.swift
index 1a076bef..e1499ae2 100644
--- a/Sources/Foundation/NSData.swift
+++ b/Sources/Foundation/NSData.swift
@@ -433,10 +433,12 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
#if os(WASI)
// WASI does not have permission concept
let permissions: Int? = nil
+ var atomicWrite: Bool { false }
#else
let permissions = try? fm.attributesOfItem(atPath: path)[.posixPermissions] as? Int
+ let atomicWrite = writeOptionsMask.contains(.atomic)
#endif
- if writeOptionsMask.contains(.atomic) {
+ if atomicWrite {
let (newFD, auxFilePath) = try _NSCreateTemporaryFile(path)
let fh = FileHandle(fileDescriptor: newFD, closeOnDealloc: true)
do {
@@ -489,7 +491,12 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
/// NOTE: the 'atomically' flag is ignored if the url is not of a type the supports atomic writes
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
do {
- try write(toFile: path, options: useAuxiliaryFile ? .atomic : [])
+ #if os(WASI)
+ let options: WritingOptions = []
+ #else
+ let options: WritingOptions = useAuxiliaryFile ? .atomic : []
+ #endif
+ try write(toFile: path, options: options)
} catch {
return false
}
diff --git a/Sources/Foundation/NSString.swift b/Sources/Foundation/NSString.swift
index ccd0ae08..fd1de6f3 100644
--- a/Sources/Foundation/NSString.swift
+++ b/Sources/Foundation/NSString.swift
@@ -1269,7 +1269,12 @@ extension NSString {
internal func _writeTo(_ url: URL, _ useAuxiliaryFile: Bool, _ enc: UInt) throws {
var data = Data()
try _getExternalRepresentation(&data, url, enc)
- try data.write(to: url, options: useAuxiliaryFile ? .atomic : [])
+ #if os(WASI)
+ let options: Data.WritingOptions = []
+ #else
+ let options: Data.WritingOptions = useAuxiliaryFile ? .atomic : []
+ #endif
+ try data.write(to: url, options: options)
}

public func write(to url: URL, atomically useAuxiliaryFile: Bool, encoding enc: UInt) throws {
--
2.48.1

39 changes: 39 additions & 0 deletions schemes/main/swift-foundation/0001-wasm-Add-O_NONBLOCK-shim.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
From 00b480096e31a7f9a70d6516b407cac2a88862cb Mon Sep 17 00:00:00 2001
From: Yuta Saito <[email protected]>
Date: Mon, 31 Mar 2025 11:06:50 +0000
Subject: [PATCH] [wasm] Add O_NONBLOCK shim

---
Sources/FoundationEssentials/WASILibc+Extensions.swift | 3 +++
Sources/_FoundationCShims/include/platform_shims.h | 1 +
2 files changed, 4 insertions(+)

diff --git a/Sources/FoundationEssentials/WASILibc+Extensions.swift b/Sources/FoundationEssentials/WASILibc+Extensions.swift
index 529ac77..0a420e3 100644
--- a/Sources/FoundationEssentials/WASILibc+Extensions.swift
+++ b/Sources/FoundationEssentials/WASILibc+Extensions.swift
@@ -49,6 +49,9 @@ internal var O_TRUNC: Int32 {
internal var O_WRONLY: Int32 {
return _platform_shims_O_WRONLY()
}
+internal var O_NONBLOCK: Int32 {
+ return _platform_shims_O_NONBLOCK()
+}
internal var O_RDONLY: Int32 {
return _platform_shims_O_RDONLY()
}
diff --git a/Sources/_FoundationCShims/include/platform_shims.h b/Sources/_FoundationCShims/include/platform_shims.h
index e02b581..37d0ca2 100644
--- a/Sources/_FoundationCShims/include/platform_shims.h
+++ b/Sources/_FoundationCShims/include/platform_shims.h
@@ -102,6 +102,7 @@ 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; }
+static inline int32_t _platform_shims_O_NONBLOCK(void) { return O_NONBLOCK; }
static inline int32_t _platform_shims_O_RDONLY(void) { return O_RDONLY; }
static inline int32_t _platform_shims_O_DIRECTORY(void) { return O_DIRECTORY; }
static inline int32_t _platform_shims_O_NOFOLLOW(void) { return O_NOFOLLOW; }
--
2.48.1

Loading