Adds windows swift support (#8622)

Adding support for windows requires the code generations
to add a compiler statement to completely ignore GRPC code
generation on windows

Cleanup the project to use the main Package.swift to run tests
instead of having it separate and includes the imports for GRPC
within it.

Adds windows swift ci
This commit is contained in:
mustiikhalil
2025-06-30 14:45:48 +02:00
committed by GitHub
parent 75556437cc
commit b8db3a9a6a
38 changed files with 64 additions and 68 deletions

View File

@@ -492,10 +492,10 @@ jobs:
sh phpUnionVectorTest.sh
build-swift:
name: Build Swift
name: Test Swift
strategy:
matrix:
swift: ["5.9", "5.10", "6.0"]
swift: ["5.9", "5.10", "6.1"]
# Only 22.04 has swift at the moment https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md?plain=1#L30
runs-on: ubuntu-22.04
steps:
@@ -506,13 +506,23 @@ jobs:
- name: Get swift version
run: swift --version
- name: test
working-directory: tests/swift/tests
run: |
swift build --build-tests
swift test
build-swift-windows:
name: Test swift windows
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: SwiftyLab/setup-swift@latest
with:
swift-version: '6.1'
- run: swift build
- run: swift test
build-swift-wasm:
name: Build Swift Wasm
name: Test Swift Wasm
runs-on: ubuntu-24.04
container:
image: ghcr.io/swiftwasm/carton:0.20.1

View File

@@ -31,6 +31,7 @@ let package = Package(
name: "FlexBuffers",
targets: ["FlexBuffers"]),
],
dependencies: .dependencies,
targets: [
.target(
name: "FlatBuffers",
@@ -42,6 +43,40 @@ let package = Package(
path: "swift/Sources/FlexBuffers"),
.target(
name: "Common",
dependencies: [],
path: "swift/Sources/Common"),
.testTarget(
name: "FlatbuffersTests",
dependencies: .dependencies,
path: "tests/swift/Tests/Flatbuffers"
),
.testTarget(
name: "FlexbuffersTests",
dependencies: ["FlexBuffers"],
path: "tests/swift/Tests/Flexbuffers"
)
])
extension Array where Element == Package.Dependency {
static var dependencies: [Package.Dependency] {
#if os(Windows)
[]
#else
// Test only Dependency
[.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.4.1")]
#endif
}
}
extension Array where Element == PackageDescription.Target.Dependency {
static var dependencies: [PackageDescription.Target.Dependency] {
#if os(Windows)
["FlatBuffers"]
#else
// Test only Dependency
[
.product(name: "GRPC", package: "grpc-swift"),
"FlatBuffers"
]
#endif
}
}

View File

@@ -5,6 +5,7 @@
// swiftlint:disable all
// swiftformat:disable all
#if !os(Windows)
import Foundation
import GRPC
import NIO
@@ -142,3 +143,5 @@ public protocol models_GreeterServerInterceptorFactoryProtocol {
func makeSayManyHellosInterceptors() -> [ServerInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
}
#endif

View File

@@ -394,6 +394,8 @@ grpc::string Generate(grpc_generator::File *file,
GenerateClientClass(&*printer, &vars);
printer->Print("\n");
GenerateServerProtocol(service, &*printer, &vars);
printer->Print("\n");
printer->Print("#endif\n");
return output;
}
@@ -409,6 +411,7 @@ grpc::string GenerateHeader() {
code += "// swiftlint:disable all\n";
code += "// swiftformat:disable all\n";
code += "\n";
code += "#if !os(Windows)\n";
code += "import Foundation\n";
code += "import GRPC\n";
code += "import NIO\n";

View File

@@ -23,7 +23,7 @@ from pathlib import Path
from util import flatc, root_path, tests_path, args, flatc_path
# Specify the other paths that will be referenced
swift_code_gen = Path(root_path, "tests/swift/tests/CodeGenerationTests")
swift_code_gen = Path(root_path, "tests/swift/fuzzer/CodeGenerationTests")
ts_code_gen = Path(root_path, "tests/ts")
samples_path = Path(root_path, "samples")
reflection_path = Path(root_path, "reflection")
@@ -405,7 +405,7 @@ dictionary_lookup_schema = "dictionary_lookup.fbs"
flatc(["--java", "--kotlin"], schema=dictionary_lookup_schema)
# Swift Tests
swift_prefix = "swift/tests/Tests/FlatBuffers.Test.SwiftTests"
swift_prefix = "swift/Tests/Flatbuffers"
flatc(
SWIFT_OPTS + BASE_OPTS + ["--grpc"],
schema="monster_test.fbs",

View File

@@ -535,11 +535,10 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
// strips out the nested directories.
let filePath = URL(filePath: #file)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
return filePath.absoluteString
#else
return FileManager.default.currentDirectoryPath
.appending("/tests/swift/Tests/Flatbuffers")
#endif
}

View File

@@ -5,6 +5,7 @@
// swiftlint:disable all
// swiftformat:disable all
#if !os(Windows)
import Foundation
import GRPC
import NIO
@@ -205,3 +206,5 @@ public protocol MyGame_Example_MonsterStorageServerInterceptorFactoryProtocol {
func makeGetMinMaxHitPointsInterceptors() -> [ServerInterceptor<Message<MyGame_Example_Monster>, Message<MyGame_Example_Stat>>]
}
#endif

View File

@@ -18,36 +18,18 @@
import PackageDescription
let package = Package(
name: "FlatBuffers.Test.Swift",
name: "fuzzer",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
],
dependencies: [
.package(path: "../../.."),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.4.1"),
// Prevent the build system from pulling 2.29.1 to prevent Swift 5.8 build breaks.
// The patch update introduced code that uses "switch expression syntax" that wasn't valid until Swift 5.9 [1].
// [1] https://github.com/swiftlang/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md
.package(
url: "https://github.com/apple/swift-nio-ssl.git",
exact: "2.29.0"),
],
targets: [
.executableTarget(
name: "SwiftFlatBuffers",
name: "fuzzer",
dependencies: [
.product(name: "FlatBuffers", package: "flatbuffers"),
]),
.testTarget(
name: "FlatBuffers.Test.SwiftTests",
dependencies: [
.product(name: "FlatBuffers", package: "flatbuffers"),
.product(name: "GRPC", package: "grpc-swift"),
]),
.testTarget(
name: "FlexBuffers.Test.SwiftTests",
dependencies: [
.product(name: "FlexBuffers", package: "flatbuffers"),
]),
])

View File

@@ -1,39 +0,0 @@
current_dir=`pwd`
cd ..
swift_dir=`pwd`
cd ..
test_dir=`pwd`
alias fbc='${test_dir}/../flatc'
shopt -s expand_aliases
cd ${current_dir}/Tests/FlatBuffers.Test.SwiftTests
fbc --swift --gen-mutable --grpc --gen-json-emit --gen-object-api -I ${test_dir}/include_test ${test_dir}/monster_test.fbs ${test_dir}/union_vector/union_vector.fbs
fbc --swift --gen-json-emit ${test_dir}/optional_scalars.fbs
fbc --swift --gen-json-emit --gen-object-api ${test_dir}/more_defaults.fbs
fbc --swift --gen-json-emit --gen-mutable --gen-object-api ${test_dir}/MutatingBool.fbs
fbc --swift --gen-json-emit ${test_dir}/vector_has_test.fbs
cd ${current_dir}
# Goes into the code generation tests
cd CodeGenerationTests
fbc --swift --gen-mutable --grpc --gen-json-emit --gen-object-api --swift-implementation-only test_import.fbs
fbc --swift --gen-mutable --grpc --gen-json-emit --gen-object-api --no-includes test_no_include.fbs
cd ..
cd ${current_dir}/Sources/SwiftFlatBuffers
# create better fuzzing test file
fbc --swift --gen-json-emit fuzzer.fbs
cd ${current_dir}
cd ${swift_dir}/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests
fbc --swift --gen-mutable --gen-json-emit --gen-object-api -I ${test_dir}/include_test ${test_dir}/monster_test.fbs
cd ${current_dir}
swift build --build-tests
swift test
if [ $(uname -s) != Darwin ]; then
echo fuzzing
swift build -c debug -Xswiftc -sanitize=fuzzer,address -Xswiftc -parse-as-library
swift build -c release -Xswiftc -sanitize=fuzzer,address -Xswiftc -parse-as-library
fi