Skip to content

Add support for Windows #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
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
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ jobs:
linux_6_0_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
linux_nightly_next_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
linux_nightly_main_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
windows_6_0_enabled: true
windows_nightly_6_1_enabled: true
windows_nightly_main_enabled: true
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error"
windows_nightly_6_1_arguments_override: "--explicit-target-dependency-import-check error"
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"

macos-tests:
name: macOS tests
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ jobs:
linux_6_0_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
linux_nightly_next_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
linux_nightly_main_arguments_override: "-Xswiftc -strict-concurrency=complete --explicit-target-dependency-import-check error"
windows_6_0_enabled: true
windows_nightly_6_1_enabled: true
windows_nightly_main_enabled: true
windows_6_0_arguments_override: "--explicit-target-dependency-import-check error"
windows_nightly_6_1_arguments_override: "--explicit-target-dependency-import-check error"
windows_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"

cxx-interop:
name: Cxx interop
Expand Down
11 changes: 9 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let package = Package(
platforms: [.macOS(.v10_15), .macCatalyst(.v13), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)],
products: [.library(name: "OpenAPIURLSession", targets: ["OpenAPIURLSession"])],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.8.2"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
],
Expand All @@ -50,14 +50,21 @@ let package = Package(
),
.testTarget(
name: "OpenAPIURLSessionTests",
dependencies: ["OpenAPIURLSession", .product(name: "NIOTestUtils", package: "swift-nio")],
dependencies: ["OpenAPIURLSession"],
swiftSettings: swiftSettings
),
]
)

#if !os(Windows) // NIO not yet supported on Windows
// Test-only dependencies.
package.dependencies += [.package(url: "https://github.com/apple/swift-nio", from: "2.62.0")]
package.targets.forEach { target in
if target.name == "OpenAPIURLSessionTests" {
target.dependencies += [.product(name: "NIOTestUtils", package: "swift-nio")]
}
}
#endif

// --- STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //
for target in package.targets {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Use the transport with client code generated by [Swift OpenAPI Generator](https:

## Supported platforms and minimum versions

| macOS | Linux | iOS | tvOS | watchOS | visionOS |
| macOS | Linux, Windows | iOS | tvOS | watchOS | visionOS |
| :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ 10.15+ | ✅ | ✅ 13+ | ✅ 13+ | ✅ 6+ | ✅ 1+ |

Expand Down
23 changes: 23 additions & 0 deletions Sources/OpenAPIURLSession/BufferedStream/Lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(Windows)
import WinSDK
#endif

#if os(Windows)
@usableFromInline
typealias LockPrimitive = SRWLOCK
#else
@usableFromInline
typealias LockPrimitive = pthread_mutex_t
#endif

@usableFromInline
enum LockOperations {}
Expand All @@ -43,35 +50,51 @@ extension LockOperations {
static func create(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
mutex.assertValidAlignment()

#if os(Windows)
InitializeSRWLock(mutex)
#else
var attr = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)

let err = pthread_mutex_init(mutex, &attr)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#endif
}

@inlinable
static func destroy(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
mutex.assertValidAlignment()

#if os(Windows)
// SRWLOCK does not need to be freed
#else
let err = pthread_mutex_destroy(mutex)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#endif
}

@inlinable
static func lock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
mutex.assertValidAlignment()

#if os(Windows)
AcquireSRWLockExclusive(mutex)
#else
let err = pthread_mutex_lock(mutex)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#endif
}

@inlinable
static func unlock(_ mutex: UnsafeMutablePointer<LockPrimitive>) {
mutex.assertValidAlignment()

#if os(Windows)
ReleaseSRWLockExclusive(mutex)
#else
let err = pthread_mutex_unlock(mutex)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#endif
}
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/OpenAPIURLSessionTests/NIOAsyncHTTP1TestServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if !os(Windows) // NIO not yet supported on Windows
import NIOCore
import NIOPosix
import NIOHTTP1
Expand Down Expand Up @@ -93,3 +94,4 @@ extension AsyncTestHTTP1Server {
}

}
#endif
1 change: 1 addition & 0 deletions Tests/OpenAPIURLSessionTests/TaskCancellationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import Foundation
import HTTPTypes
import NIO
import NIOHTTP1
import OpenAPIRuntime
import XCTest
@testable import OpenAPIURLSession
Expand Down
36 changes: 20 additions & 16 deletions Tests/OpenAPIURLSessionTests/URLSessionTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import Foundation
import FoundationNetworking
#endif
import HTTPTypes
#if !os(Windows) // NIO not yet supported on Windows
import NIO
import NIOHTTP1
#endif
import OpenAPIRuntime
import XCTest
@testable import OpenAPIURLSession
Expand Down Expand Up @@ -59,6 +61,7 @@ class URLSessionTransportConverterTests: XCTestCase {
}
}

#if !os(Windows) // NIO not yet supported on Windows
// swift-format-ignore: AllPublicDeclarationsHaveDocumentation
class URLSessionTransportBufferedTests: XCTestCase {
var transport: URLSessionTransport!
Expand Down Expand Up @@ -133,22 +136,6 @@ class URLSessionTransportStreamingTests: XCTestCase {
#endif
}

class URLSessionTransportPlatformSupportTests: XCTestCase {
func testDefaultsToStreamingIfSupported() {
if URLSessionTransport.Configuration.Implementation.platformSupportsStreaming {
guard case .streaming = URLSessionTransport.Configuration.Implementation.platformDefault else {
XCTFail()
return
}
} else {
guard case .buffering = URLSessionTransport.Configuration.Implementation.platformDefault else {
XCTFail()
return
}
}
}
}

func testHTTPRedirect(
transport: any ClientTransport,
requestBodyIterationBehavior: IterationBehavior,
Expand Down Expand Up @@ -315,6 +302,23 @@ func testHTTPBasicPost(transport: any ClientTransport) async throws {
group.cancelAll()
}
}
#endif

class URLSessionTransportPlatformSupportTests: XCTestCase {
func testDefaultsToStreamingIfSupported() {
if URLSessionTransport.Configuration.Implementation.platformSupportsStreaming {
guard case .streaming = URLSessionTransport.Configuration.Implementation.platformDefault else {
XCTFail()
return
}
} else {
guard case .buffering = URLSessionTransport.Configuration.Implementation.platformDefault else {
XCTFail()
return
}
}
}
}

class URLSessionTransportDebugLoggingTests: XCTestCase {
func testDebugLoggingEnabled() {
Expand Down