Files
swift-nio/Sources/NIOPosix/BaseSocketChannel+SocketOptionProvider.swift
scottmarchant 5bf0267df4 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).
2026-01-23 16:39:32 +00:00

90 lines
2.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if !os(WASI)
import NIOCore
extension BaseSocketChannel: SocketOptionProvider {
#if !os(Windows)
func unsafeSetSocketOption<Value: Sendable>(
level: SocketOptionLevel,
name: SocketOptionName,
value: Value
) -> EventLoopFuture<Void> {
unsafeSetSocketOption(
level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)),
name: NIOBSDSocket.Option(rawValue: CInt(name)),
value: value
)
}
#endif
func unsafeSetSocketOption<Value: Sendable>(
level: NIOBSDSocket.OptionLevel,
name: NIOBSDSocket.Option,
value: Value
) -> EventLoopFuture<Void> {
if eventLoop.inEventLoop {
let promise = eventLoop.makePromise(of: Void.self)
executeAndComplete(promise) {
try setSocketOption0(level: level, name: name, value: value)
}
return promise.futureResult
} else {
return eventLoop.submit {
try self.setSocketOption0(level: level, name: name, value: value)
}
}
}
#if !os(Windows)
func unsafeGetSocketOption<Value: Sendable>(
level: SocketOptionLevel,
name: SocketOptionName
) -> EventLoopFuture<Value> {
unsafeGetSocketOption(
level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)),
name: NIOBSDSocket.Option(rawValue: CInt(name))
)
}
#endif
func unsafeGetSocketOption<Value: Sendable>(
level: NIOBSDSocket.OptionLevel,
name: NIOBSDSocket.Option
) -> EventLoopFuture<Value> {
if eventLoop.inEventLoop {
let promise = eventLoop.makePromise(of: Value.self)
executeAndComplete(promise) {
try getSocketOption0(level: level, name: name)
}
return promise.futureResult
} else {
return eventLoop.submit {
try self.getSocketOption0(level: level, name: name)
}
}
}
func setSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws {
try self.socket.setOption(level: level, name: name, value: value)
}
func getSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value {
try self.socket.getOption(level: level, name: name)
}
}
#endif // !os(WASI)