From 90c183256e2f579027dcb873e48c06c9e74d794b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 8 Mar 2024 09:43:48 +0000 Subject: [PATCH] [wasm] Import `sys/mman.h` through CoreFoundation Importing `wasi_emulated_mman` in Swift code requires the client to define `_WASI_EMULATED_MMAN`. `@_implementationOnly import` was the only way to work around this, but it's now declared as unsafe if it's used in non-resilient moules (even though it's actually safe as long as the module does not use type layout of the imported module). The new scoped import feature always requires the transitive clients to load privately imported modules, so it's not a good fit for this tricky case where a special macro must be defined before importing the module. This patch imports `sys/mman.h` through CoreFoundation while defining `_WASI_EMULATED_MMAN` in the header exposed by modulemap. This way, the clients don't need to define it. --- CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h | 5 +++++ Sources/Foundation/Data.swift | 5 ----- Sources/Foundation/FileHandle.swift | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h index c6e63fac8e..1a7eb3e073 100644 --- a/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h +++ b/CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h @@ -66,6 +66,11 @@ #include #elif TARGET_OS_WASI #include +// Define _WASI_EMULATED_MMAN here to use the emulated mman functions in +// Foundation-side without requiring transitive clients to define it. +#undef _WASI_EMULATED_MMAN +#define _WASI_EMULATED_MMAN +#include #elif TARGET_OS_LINUX #include #include diff --git a/Sources/Foundation/Data.swift b/Sources/Foundation/Data.swift index b76fef915c..9e5c9faf38 100644 --- a/Sources/Foundation/Data.swift +++ b/Sources/Foundation/Data.swift @@ -36,11 +36,6 @@ @usableFromInline let memcpy = Musl.memcpy @usableFromInline let memcmp = Musl.memcmp #elseif canImport(WASILibc) -#if swift(>=6.0) -private import wasi_emulated_mman -#else -import wasi_emulated_mman -#endif @usableFromInline let calloc = WASILibc.calloc @usableFromInline let malloc = WASILibc.malloc @usableFromInline let free = WASILibc.free diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index 5fb7258ca8..6307b300d7 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -29,7 +29,6 @@ fileprivate let _write = Musl.write(_:_:_:) fileprivate let _close = Musl.close(_:) #elseif canImport(WASILibc) import WASILibc -@_implementationOnly import wasi_emulated_mman fileprivate let _read = WASILibc.read(_:_:_:) fileprivate let _write = WASILibc.write(_:_:_:) fileprivate let _close = WASILibc.close(_:)