Skip to content

Commit 5bf0267

Browse files
build: Elide NIOPosix for WASI platforms only (#3485)
### Motivation: Packages that consume NIOPosix should be able to compile to WASI platforms without special configurations. This change elides the NIOPosix source from WASI platforms to simplify configuration. Without this change: ```swift dependencies: [ // Without this PR, downstream packages must maintain exhaustive platform list to exclude `.wasi`: .product( name: "NIOPosix", package: "swift-nio", condition: .when(platforms: [ .macOS, .macCatalyst, .iOS, .tvOS, .watchOS, .visionOS, .driverKit, .linux, .windows, .android, .openbsd, // .wasi // <-- Need to exclude this, because there is no exclusion list api for SPM conditionals ]) ), ] ``` With this change: ```swift dependencies: [ // Without this PR, downstream packages can consume NIOPosix simply: .product(name: "NIOPosix", package: "swift-nio"), ] ``` ### Modifications: - Add compiler directives (`#if !os(WASI)`) to all source files in NIOPosix ### Result: Downstream packages can compile to wasm without manually excluding the NIOPosix dependency. ### Testing performed - Verified `swift build --swift-sdk wasm32-unknown-wasip1 --target NIOPosix` compiles, which demonstrates proper elision of source files in NIOPosix that aren't wasm-ready. - Confirmed GitHub [checks pass](https://github.com/PassiveLogic/swift-nio/actions/runs/21151341738).
1 parent 3f80264 commit 5bf0267

55 files changed

Lines changed: 177 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Sources/NIOPosix/BSDSocketAPICommon.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import NIOCore
1518

1619
#if os(Windows)
@@ -391,3 +394,4 @@ enum NIOBSDSocketControlMessage: _BSDSocketControlMessageProtocol {}
391394

392395
/// The requested UDS path exists and has wrong type (not a socket).
393396
public struct UnixDomainSocketPathWrongType: Error {}
397+
#endif // !os(WASI)

Sources/NIOPosix/BSDSocketAPIPosix.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import NIOCore
1518

1619
#if os(Linux) || os(Android) || os(FreeBSD) || canImport(Darwin) || os(OpenBSD)
@@ -426,3 +429,4 @@ extension msghdr {
426429
}
427430
}
428431
#endif
432+
#endif // !os(WASI)

Sources/NIOPosix/BSDSocketAPIWindows.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#if !os(WASI)
16+
1517
import NIOCore
1618

1719
#if os(Windows)
@@ -803,3 +805,4 @@ extension WSAMSG {
803805
}
804806
}
805807
#endif
808+
#endif // !os(WASI)

Sources/NIOPosix/BaseSocket.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#if !os(WASI)
16+
1517
import CNIOLinux
1618
import CNIOOpenBSD
1719
import NIOConcurrencyHelpers
@@ -420,3 +422,4 @@ func __testOnly_withMutableSockAddr<ReturnType>(
420422
) rethrows -> ReturnType {
421423
try addr.withMutableSockAddr(body)
422424
}
425+
#endif // !os(WASI)

Sources/NIOPosix/BaseSocketChannel+SocketOptionProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import NIOCore
1518

1619
extension BaseSocketChannel: SocketOptionProvider {
@@ -83,3 +86,4 @@ extension BaseSocketChannel: SocketOptionProvider {
8386
try self.socket.getOption(level: level, name: name)
8487
}
8588
}
89+
#endif // !os(WASI)

Sources/NIOPosix/BaseSocketChannel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
#if !os(WASI)
16+
1517
import Atomics
1618
import NIOConcurrencyHelpers
1719
import NIOCore
@@ -1435,3 +1437,4 @@ func executeAndComplete<Value: Sendable>(_ promise: EventLoopPromise<Value>?, _
14351437
promise?.fail(e)
14361438
}
14371439
}
1440+
#endif // !os(WASI)

Sources/NIOPosix/BaseStreamSocketChannel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import NIOCore
1518

1619
class BaseStreamSocketChannel<Socket: SocketProtocol>: BaseSocketChannel<Socket>, @unchecked Sendable {
@@ -301,3 +304,4 @@ class BaseStreamSocketChannel<Socket: SocketProtocol>: BaseSocketChannel<Socket>
301304
}
302305
}
303306
}
307+
#endif // !os(WASI)

Sources/NIOPosix/Bootstrap.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import CNIOLinux
1518
import CNIOOpenBSD
1619
import NIOCore
@@ -2701,3 +2704,4 @@ private struct DefaultNIOPipeBootstrapHooks: NIOPipeBootstrapHooks {
27012704
try PipeChannel(eventLoop: eventLoop, input: input, output: output)
27022705
}
27032706
}
2707+
#endif // !os(WASI)

Sources/NIOPosix/ControlMessage.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import NIOCore
1518

1619
#if canImport(Darwin)
@@ -438,3 +441,4 @@ extension AddressedEnvelope.Metadata {
438441
)
439442
}
440443
}
444+
#endif // !os(WASI)

Sources/NIOPosix/DatagramVectorReadManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
15+
#if !os(WASI)
16+
1417
import CNIODarwin
1518
import CNIOLinux
1619
import CNIOOpenBSD
@@ -296,3 +299,4 @@ extension UnsafeMutableBufferPointer {
296299
rawPointer.deallocate()
297300
}
298301
}
302+
#endif // !os(WASI)

0 commit comments

Comments
 (0)