Skip to content

Commit 379f3b6

Browse files
committed
Workaround SwiftPM
1 parent 1fcd91b commit 379f3b6

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

.github/workflows/pull_request.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
1212
with:
1313
license_header_check_project_name: "gRPC"
14+
# See Package.swift for an explanation of this.
15+
linux_pre_build_command: "export GRPC_SWIFT_PROTOBUF_NO_VERSION=1"
1416

1517
grpc-soundness:
1618
name: Soundness

Package.swift

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,12 @@ let defaultSwiftSettings: [SwiftSetting] = [
5454
.enableUpcomingFeature("MemberImportVisibility"),
5555
]
5656

57-
extension Context {
58-
fileprivate static var versionString: String {
59-
guard let git = Self.gitInformation else { return "" }
60-
61-
if let tag = git.currentTag {
62-
return tag
63-
} else {
64-
let suffix = git.hasUncommittedChanges ? " (modified)" : ""
65-
return git.currentCommit + suffix
66-
}
67-
}
68-
}
69-
70-
let targets: [Target] = [
57+
var targets: [Target] = [
7158
// protoc plugin for grpc-swift
7259
.executableTarget(
7360
name: "protoc-gen-grpc-swift",
7461
dependencies: [
7562
.target(name: "GRPCProtobufCodeGen"),
76-
.target(name: "CGRPCProtobuf"),
7763
.product(name: "GRPCCodeGen", package: "grpc-swift"),
7864
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
7965
.product(name: "SwiftProtobufPluginLibrary", package: "swift-protobuf"),
@@ -124,12 +110,6 @@ let targets: [Target] = [
124110
swiftSettings: defaultSwiftSettings
125111
),
126112

127-
.target(
128-
name: "CGRPCProtobuf",
129-
cSettings: [
130-
.define("CGRPC_GRPC_SWIFT_PROTOBUF_VERSION", to: "\"\(Context.versionString)\"")
131-
]
132-
),
133113

134114
// Code generator build plugin
135115
.plugin(
@@ -164,6 +144,51 @@ let targets: [Target] = [
164144
),
165145
]
166146

147+
// -------------------------------------------------------------------------------------------------
148+
149+
extension Context {
150+
fileprivate static var versionString: String {
151+
guard let git = Self.gitInformation else { return "" }
152+
153+
if let tag = git.currentTag {
154+
return tag
155+
} else {
156+
let suffix = git.hasUncommittedChanges ? " (modified)" : ""
157+
return git.currentCommit + suffix
158+
}
159+
}
160+
161+
fileprivate static var buildCGRPCProtobuf: Bool {
162+
let noVersion = Context.environment.keys.contains("GRPC_SWIFT_PROTOBUF_NO_VERSION")
163+
return !noVersion
164+
}
165+
}
166+
167+
// Having a C module as a transitive dependency of a plugin seems to trip up the API breakage
168+
// checking tool. See also https://github.com/swiftlang/swift-package-manager/issues/8081
169+
//
170+
// The CGRPCProtobuf module (which only includes package version information) is conditionally
171+
// compiled and included based on an environment variable. This is set in CI only for the API
172+
// breakage checking job to avoid tripping up SwiftPM.
173+
if Context.buildCGRPCProtobuf {
174+
targets.append(
175+
.target(
176+
name: "CGRPCProtobuf",
177+
cSettings: [
178+
.define("CGRPC_GRPC_SWIFT_PROTOBUF_VERSION", to: "\"\(Context.versionString)\"")
179+
]
180+
)
181+
)
182+
183+
for target in targets {
184+
if target.name == "protoc-gen-grpc-swift" {
185+
target.dependencies.append(.target(name: "CGRPCProtobuf"))
186+
}
187+
}
188+
}
189+
190+
// -------------------------------------------------------------------------------------------------
191+
167192
let package = Package(
168193
name: "grpc-swift-protobuf",
169194
platforms: [

Sources/protoc-gen-grpc-swift/Version.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
#if canImport(CGRPCProtobuf)
1718
private import CGRPCProtobuf
19+
#endif
1820

1921
internal enum Version {
2022
/// The version string.
2123
internal static var versionString: String {
24+
#if canImport(CGRPCProtobuf)
2225
String(cString: cgrprc_grpc_swift_protobuf_version())
26+
#else
27+
"unknown"
28+
#endif
2329
}
2430
}

0 commit comments

Comments
 (0)