Skip to content

Commit b89d8bb

Browse files
committed
Use GRPC from swift package manager
1 parent 1f05889 commit b89d8bb

21 files changed

+569
-20
lines changed

Diff for: examples/grpc_example/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ tidy(
1616
# gazelle:proto_import_prefix example
1717
# gazelle:swift_module_naming_convention pascal_case
1818
# gazelle:swift_generate_proto_libraries true
19+
# gazelle:swift_proto_compiler swift_proto=//compilers:swift_proto
20+
# gazelle:swift_proto_compiler swift_client_proto=//compilers:swift_client_proto
21+
# gazelle:swift_proto_compiler swift_server_proto=//compilers:swift_server_proto
1922
# gazelle:swift_generate_grpc_libraries_with_flavors swift_client_proto,swift_server_proto
2023
# gazelle:exclude .build
2124

Diff for: examples/grpc_example/MODULE.bazel

+17
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,20 @@ apple_cc_configure = use_extension(
6060
"apple_cc_configure_extension",
6161
)
6262
use_repo(apple_cc_configure, "local_config_apple_cc")
63+
64+
swift_deps = use_extension(
65+
"@rules_swift_package_manager//:extensions.bzl",
66+
"swift_deps",
67+
)
68+
swift_deps.from_package(
69+
declare_swift_deps_info = True,
70+
resolved = "//:Package.resolved",
71+
swift = "//:Package.swift",
72+
)
73+
use_repo(
74+
swift_deps,
75+
"swift_deps_info",
76+
"swift_package",
77+
"swiftpkg_grpc_swift",
78+
"swiftpkg_swift_protobuf",
79+
)

Diff for: examples/grpc_example/Package.resolved

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"pins" : [
3+
{
4+
"identity" : "grpc-swift",
5+
"kind" : "remoteSourceControl",
6+
"location" : "https://github.com/grpc/grpc-swift.git",
7+
"state" : {
8+
"revision" : "4ab02e1ae5b4dfdd723773e955b62f35ccbaa7c7",
9+
"version" : "1.16.0"
10+
}
11+
},
12+
{
13+
"identity" : "swift-atomics",
14+
"kind" : "remoteSourceControl",
15+
"location" : "https://github.com/apple/swift-atomics.git",
16+
"state" : {
17+
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
18+
"version" : "1.2.0"
19+
}
20+
},
21+
{
22+
"identity" : "swift-collections",
23+
"kind" : "remoteSourceControl",
24+
"location" : "https://github.com/apple/swift-collections.git",
25+
"state" : {
26+
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
27+
"version" : "1.1.0"
28+
}
29+
},
30+
{
31+
"identity" : "swift-http-types",
32+
"kind" : "remoteSourceControl",
33+
"location" : "https://github.com/apple/swift-http-types",
34+
"state" : {
35+
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65",
36+
"version" : "1.0.3"
37+
}
38+
},
39+
{
40+
"identity" : "swift-log",
41+
"kind" : "remoteSourceControl",
42+
"location" : "https://github.com/apple/swift-log.git",
43+
"state" : {
44+
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
45+
"version" : "1.5.4"
46+
}
47+
},
48+
{
49+
"identity" : "swift-nio",
50+
"kind" : "remoteSourceControl",
51+
"location" : "https://github.com/apple/swift-nio.git",
52+
"state" : {
53+
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982",
54+
"version" : "2.64.0"
55+
}
56+
},
57+
{
58+
"identity" : "swift-nio-extras",
59+
"kind" : "remoteSourceControl",
60+
"location" : "https://github.com/apple/swift-nio-extras.git",
61+
"state" : {
62+
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
63+
"version" : "1.22.0"
64+
}
65+
},
66+
{
67+
"identity" : "swift-nio-http2",
68+
"kind" : "remoteSourceControl",
69+
"location" : "https://github.com/apple/swift-nio-http2.git",
70+
"state" : {
71+
"revision" : "0904bf0feb5122b7e5c3f15db7df0eabe623dd87",
72+
"version" : "1.30.0"
73+
}
74+
},
75+
{
76+
"identity" : "swift-nio-ssl",
77+
"kind" : "remoteSourceControl",
78+
"location" : "https://github.com/apple/swift-nio-ssl.git",
79+
"state" : {
80+
"revision" : "7c381eb6083542b124a6c18fae742f55001dc2b5",
81+
"version" : "2.26.0"
82+
}
83+
},
84+
{
85+
"identity" : "swift-nio-transport-services",
86+
"kind" : "remoteSourceControl",
87+
"location" : "https://github.com/apple/swift-nio-transport-services.git",
88+
"state" : {
89+
"revision" : "6cbe0ed2b394f21ab0d46b9f0c50c6be964968ce",
90+
"version" : "1.20.1"
91+
}
92+
},
93+
{
94+
"identity" : "swift-protobuf",
95+
"kind" : "remoteSourceControl",
96+
"location" : "https://github.com/apple/swift-protobuf.git",
97+
"state" : {
98+
"revision" : "88c7d15e1242fdb6ecbafbc7926426a19be1e98a",
99+
"version" : "1.20.2"
100+
}
101+
},
102+
{
103+
"identity" : "swift-system",
104+
"kind" : "remoteSourceControl",
105+
"location" : "https://github.com/apple/swift-system.git",
106+
"state" : {
107+
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
108+
"version" : "1.2.1"
109+
}
110+
}
111+
],
112+
"version" : 2
113+
}

Diff for: examples/grpc_example/Package.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import PackageDescription
55
let package = Package(
66
name: "grpc_example",
77
dependencies: [
8-
// NOTE: The https://github.com/grpc/grpc-swift are currently coming from rules_swift.
9-
// Related Slack thread: https://bazelbuild.slack.com/archives/CD3QY5C2X/p1692055426375909
8+
// These are the versions used by rules_swift 1.18.0
9+
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "1.16.0"),
10+
.package(url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
1011
]
1112
)

Diff for: examples/grpc_example/compilers/BUILD.bazel

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load(
3+
"@build_bazel_rules_swift//proto:proto.bzl",
4+
"swift_proto_compiler",
5+
)
6+
load(
7+
"//compilers:swift_proto_compiler_macros.bzl",
8+
"GRPC_VARIANT_CLIENT",
9+
"GRPC_VARIANT_SERVER",
10+
"GRPC_VARIANT_TEST_CLIENT",
11+
"PROTO_PLUGIN_OPTIONS",
12+
"PROTO_PLUGIN_OPTION_ALLOWLIST",
13+
"make_grpc_swift_proto_compiler",
14+
)
15+
16+
# Swift Protobuf
17+
18+
swift_proto_compiler(
19+
name = "swift_proto",
20+
plugin = "@swiftpkg_swift_protobuf//:protoc-gen-swift",
21+
plugin_name = "swift",
22+
plugin_option_allowlist = PROTO_PLUGIN_OPTION_ALLOWLIST,
23+
plugin_options = PROTO_PLUGIN_OPTIONS,
24+
protoc = "@com_google_protobuf//:protoc",
25+
suffixes = [".pb.swift"],
26+
visibility = ["//visibility:public"],
27+
deps = [
28+
"@swiftpkg_swift_protobuf//:SwiftProtobuf",
29+
],
30+
)
31+
32+
# Swift GRPC
33+
34+
make_grpc_swift_proto_compiler(
35+
name = "swift_server_proto",
36+
variants = [GRPC_VARIANT_SERVER],
37+
)
38+
39+
make_grpc_swift_proto_compiler(
40+
name = "swift_client_proto",
41+
variants = [GRPC_VARIANT_CLIENT],
42+
)
43+
44+
make_grpc_swift_proto_compiler(
45+
name = "swift_test_client_proto",
46+
variants = [GRPC_VARIANT_TEST_CLIENT],
47+
)
48+
49+
bzl_library(
50+
name = "swift_proto_compiler_macros",
51+
srcs = ["swift_proto_compiler_macros.bzl"],
52+
visibility = ["//visibility:public"],
53+
deps = [
54+
"@bazel_skylib//lib:dicts",
55+
"@build_bazel_rules_swift//swift",
56+
],
57+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Utilities for proto compiler rules.
17+
"""
18+
19+
load(
20+
"@bazel_skylib//lib:dicts.bzl",
21+
"dicts",
22+
)
23+
load(
24+
"@build_bazel_rules_swift//proto:proto.bzl",
25+
"swift_proto_compiler",
26+
)
27+
28+
# NOTE: The ProtoPathModuleMappings option is set internally for all plugins.
29+
# This is used to inform the plugins which Swift module the generated code for each plugin is located in.
30+
PROTO_PLUGIN_OPTION_ALLOWLIST = [
31+
"FileNaming",
32+
"Visibility",
33+
]
34+
PROTO_PLUGIN_OPTIONS = {
35+
"Visibility": "Public",
36+
}
37+
GRPC_VARIANT_SERVER = "Server"
38+
GRPC_VARIANT_CLIENT = "Client"
39+
GRPC_VARIANT_TEST_CLIENT = "TestClient"
40+
GRPC_VARIANTS = [
41+
GRPC_VARIANT_SERVER,
42+
GRPC_VARIANT_CLIENT,
43+
GRPC_VARIANT_TEST_CLIENT,
44+
]
45+
GRPC_PLUGIN_OPTION_ALLOWLIST = PROTO_PLUGIN_OPTION_ALLOWLIST + [
46+
"KeepMethodCasing",
47+
"ExtraModuleImports",
48+
"GRPCModuleName",
49+
"SwiftProtobufModuleName",
50+
] + GRPC_VARIANTS
51+
52+
def make_grpc_swift_proto_compiler(
53+
name,
54+
variants,
55+
plugin_options = PROTO_PLUGIN_OPTIONS):
56+
"""Generates a GRPC swift_proto_compiler target for the given variants.
57+
58+
Args:
59+
name: The name of the generated swift proto compiler target.
60+
variants: The list of variants the compiler should generate.
61+
plugin_options: Additional options to pass to the plugin.
62+
"""
63+
64+
# Merge the plugin options to include the variants:
65+
merged_plugin_options = dicts.add(
66+
plugin_options,
67+
{variant: "false" for variant in GRPC_VARIANTS},
68+
)
69+
for variant in variants:
70+
merged_plugin_options[variant] = "true"
71+
72+
swift_proto_compiler(
73+
name = name,
74+
protoc = "@com_google_protobuf//:protoc",
75+
plugin = "@swiftpkg_grpc_swift//:protoc-gen-grpc-swift",
76+
plugin_name = name.removesuffix("_proto"),
77+
plugin_option_allowlist = GRPC_PLUGIN_OPTION_ALLOWLIST,
78+
plugin_options = merged_plugin_options,
79+
suffixes = [".grpc.swift"],
80+
deps = [
81+
"@swiftpkg_swift_protobuf//:SwiftProtobuf",
82+
"@swiftpkg_grpc_swift//:GRPC",
83+
],
84+
visibility = ["//visibility:public"],
85+
)

Diff for: examples/grpc_example/protos/BUILD.bazel

Whitespace-only changes.

Diff for: examples/grpc_example/protos/echo_service/BUILD.bazel

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ proto_library(
1616
swift_proto_library(
1717
name = "echo_service_swift_client_proto",
1818
compilers = [
19-
"@build_bazel_rules_swift//proto/compilers:swift_proto",
20-
"@build_bazel_rules_swift//proto/compilers:swift_client_proto",
19+
"//compilers:swift_proto",
20+
"//compilers:swift_client_proto",
2121
],
2222
module_name = "EchoServiceClient",
2323
protos = [":echo_service_proto"],
@@ -31,8 +31,8 @@ swift_proto_library(
3131
swift_proto_library(
3232
name = "echo_service_swift_server_proto",
3333
compilers = [
34-
"@build_bazel_rules_swift//proto/compilers:swift_proto",
35-
"@build_bazel_rules_swift//proto/compilers:swift_server_proto",
34+
"//compilers:swift_proto",
35+
"//compilers:swift_server_proto",
3636
],
3737
module_name = "EchoServiceServer",
3838
protos = [":echo_service_proto"],

Diff for: examples/grpc_example/protos/echo_service/requests/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ proto_library(
1212

1313
swift_proto_library(
1414
name = "echo_request_swift_proto",
15-
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
15+
compilers = ["//compilers:swift_proto"],
1616
module_name = "EchoRequest",
1717
protos = [":echo_request_proto"],
1818
visibility = ["//visibility:public"],

Diff for: examples/grpc_example/protos/echo_service/responses/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ proto_library(
1111

1212
swift_proto_library(
1313
name = "echo_response_swift_proto",
14-
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
14+
compilers = ["//compilers:swift_proto"],
1515
module_name = "EchoResponse",
1616
protos = [":echo_response_proto"],
1717
visibility = ["//visibility:public"],

Diff for: examples/grpc_example/sources/BUILD.bazel

Whitespace-only changes.

Diff for: examples/grpc_package_example/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ tidy(
1515
# gazelle:swift_proto_generation_mode package
1616
# gazelle:swift_module_naming_convention pascal_case
1717
# gazelle:swift_generate_proto_libraries true
18+
# gazelle:swift_proto_compiler swift_proto=//compilers:swift_proto
19+
# gazelle:swift_proto_compiler swift_client_proto=//compilers:swift_client_proto
20+
# gazelle:swift_proto_compiler swift_server_proto=//compilers:swift_server_proto
1821
# gazelle:swift_generate_grpc_libraries_with_flavors swift_client_proto,swift_server_proto
1922
# gazelle:exclude .build
2023

Diff for: examples/grpc_package_example/MODULE.bazel

+19
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,22 @@ apple_cc_configure = use_extension(
6060
"apple_cc_configure_extension",
6161
)
6262
use_repo(apple_cc_configure, "local_config_apple_cc")
63+
64+
# swift_deps START
65+
swift_deps = use_extension(
66+
"@rules_swift_package_manager//:extensions.bzl",
67+
"swift_deps",
68+
)
69+
swift_deps.from_package(
70+
declare_swift_deps_info = True,
71+
resolved = "//:Package.resolved",
72+
swift = "//:Package.swift",
73+
)
74+
use_repo(
75+
swift_deps,
76+
"swift_deps_info",
77+
"swift_package",
78+
"swiftpkg_grpc_swift",
79+
"swiftpkg_swift_protobuf",
80+
)
81+
# swift_deps END

0 commit comments

Comments
 (0)