I am new to grpc and am planning to use it for my iOS client and Swift server. I have a Swift package repo consisting of just the .protos and generated swift code. I'm having trouble building the protoc plugins, because my my-project-grpc/Sources/MyLibrary/
directory is initially void of Swift code (it will be generated later); however, it is expected to contain Swift code, because I am declaring a .library
product in my Package.swift
. Is there a workaround for this, or is there a better way to manage and share my generated code between Swift projects? I am using v2 alpha of swift-grpc. Thanks!
Here's how I plan to use my grpc project / package
- Run build-protoc-plugins.sh script to build protoc plugins
- Generate swift code using protoc plugins with generate.sh script
- Commit/tag generated code version
- Import in client/server
Here's my project structure for the grpc repo
my-project-grpc/
|__Sources/
| |__MyLibraryGRPC/
| |__Model/
| | |__myservice.proto
| |__Generated/
| |__myservice.grpc.swift
| |__myservice.pb.swift
|__build-protoc-plugins.sh
|__generate.sh
|__Package.swift
Here's my Package.swift
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "my-project-grpc",
platforms: [.macOS("15.0")],
products: [
.library(
name: "MyLibraryGRPC",
targets: ["MyLibraryTarget"]),
],
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", from: "1.0.0-alpha.1"),
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", from: "1.0.0-alpha.1"),
],
targets: [
.target(
name: "MyLibraryGRPCTarget",
dependencies: [
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
]
)
]
)
Here's the build error:
sh build-protoc-plugins.sh
warning: 'my-project-grpc': Source files for target MyLibraryGRPCTarget should be located under 'Sources/MyLibraryGRPC', 'Sources', or a custom sources path can be set with the 'path' property in Package.swift
error: 'my-project-grpc': target 'MyLibraryGRPCTarget' referenced in product 'MyLibraryGRPC' is empty
This is the build-protoc-plugins.sh line that is failing:
swift build --product protoc-gen-swift --configuration release