Skip to content

Commit

Permalink
Use GRPC and SwiftProtobuf from Swift Package Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
AttilaTheFun committed Nov 5, 2024
1 parent 19ef290 commit 676c95e
Show file tree
Hide file tree
Showing 27 changed files with 583 additions and 22 deletions.
3 changes: 3 additions & 0 deletions examples/grpc_example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ tidy(
# gazelle:proto_import_prefix example
# gazelle:swift_module_naming_convention pascal_case
# gazelle:swift_generate_proto_libraries true
# gazelle:swift_proto_compiler swift_proto=//compilers:swift_proto
# gazelle:swift_proto_compiler swift_client_proto=//compilers:swift_client_proto
# gazelle:swift_proto_compiler swift_server_proto=//compilers:swift_server_proto
# gazelle:swift_generate_grpc_libraries_with_flavors swift_client_proto,swift_server_proto
# gazelle:exclude .build

Expand Down
17 changes: 17 additions & 0 deletions examples/grpc_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ apple_cc_configure = use_extension(
"apple_cc_configure_extension",
)
use_repo(apple_cc_configure, "local_config_apple_cc")

swift_deps = use_extension(
"@rules_swift_package_manager//:extensions.bzl",
"swift_deps",
)
swift_deps.from_package(
declare_swift_deps_info = True,
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_grpc_swift",
"swiftpkg_swift_protobuf",
)
113 changes: 113 additions & 0 deletions examples/grpc_example/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"pins" : [
{
"identity" : "grpc-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/grpc/grpc-swift.git",
"state" : {
"revision" : "4ab02e1ae5b4dfdd723773e955b62f35ccbaa7c7",
"version" : "1.16.0"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65",
"version" : "1.0.3"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982",
"version" : "2.64.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
"version" : "1.22.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "0904bf0feb5122b7e5c3f15db7df0eabe623dd87",
"version" : "1.30.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "7c381eb6083542b124a6c18fae742f55001dc2b5",
"version" : "2.26.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "6cbe0ed2b394f21ab0d46b9f0c50c6be964968ce",
"version" : "1.20.1"
}
},
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "88c7d15e1242fdb6ecbafbc7926426a19be1e98a",
"version" : "1.20.2"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
"version" : "1.2.1"
}
}
],
"version" : 2
}
5 changes: 3 additions & 2 deletions examples/grpc_example/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import PackageDescription
let package = Package(
name: "grpc_example",
dependencies: [
// NOTE: The https://github.com/grpc/grpc-swift are currently coming from rules_swift.
// Related Slack thread: https://bazelbuild.slack.com/archives/CD3QY5C2X/p1692055426375909
// These are the versions used by rules_swift 1.18.0
.package(url: "https://github.com/grpc/grpc-swift.git", exact: "1.16.0"),
.package(url: "https://github.com/apple/swift-protobuf.git", exact: "1.20.2"),
]
)
57 changes: 57 additions & 0 deletions examples/grpc_example/compilers/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(
"@build_bazel_rules_swift//proto:proto.bzl",
"swift_proto_compiler",
)
load(
"//compilers:swift_proto_compiler_macros.bzl",
"GRPC_VARIANT_CLIENT",
"GRPC_VARIANT_SERVER",
"GRPC_VARIANT_TEST_CLIENT",
"PROTO_PLUGIN_OPTIONS",
"PROTO_PLUGIN_OPTION_ALLOWLIST",
"make_grpc_swift_proto_compiler",
)

# Swift Protobuf

swift_proto_compiler(
name = "swift_proto",
plugin = "@swiftpkg_swift_protobuf//:protoc-gen-swift",
plugin_name = "swift",
plugin_option_allowlist = PROTO_PLUGIN_OPTION_ALLOWLIST,
plugin_options = PROTO_PLUGIN_OPTIONS,
protoc = "@com_google_protobuf//:protoc",
suffixes = [".pb.swift"],
visibility = ["//visibility:public"],
deps = [
"@swiftpkg_swift_protobuf//:SwiftProtobuf",
],
)

# Swift GRPC

make_grpc_swift_proto_compiler(
name = "swift_server_proto",
variants = [GRPC_VARIANT_SERVER],
)

make_grpc_swift_proto_compiler(
name = "swift_client_proto",
variants = [GRPC_VARIANT_CLIENT],
)

make_grpc_swift_proto_compiler(
name = "swift_test_client_proto",
variants = [GRPC_VARIANT_TEST_CLIENT],
)

bzl_library(
name = "swift_proto_compiler_macros",
srcs = ["swift_proto_compiler_macros.bzl"],
visibility = ["//visibility:public"],
deps = [
"@bazel_skylib//lib:dicts",
"@build_bazel_rules_swift//swift",
],
)
85 changes: 85 additions & 0 deletions examples/grpc_example/compilers/swift_proto_compiler_macros.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Utilities for proto compiler rules.
"""

load(
"@bazel_skylib//lib:dicts.bzl",
"dicts",
)
load(
"@build_bazel_rules_swift//proto:proto.bzl",
"swift_proto_compiler",
)

# NOTE: The ProtoPathModuleMappings option is set internally for all plugins.
# This is used to inform the plugins which Swift module the generated code for each plugin is located in.
PROTO_PLUGIN_OPTION_ALLOWLIST = [
"FileNaming",
"Visibility",
]
PROTO_PLUGIN_OPTIONS = {
"Visibility": "Public",
}
GRPC_VARIANT_SERVER = "Server"
GRPC_VARIANT_CLIENT = "Client"
GRPC_VARIANT_TEST_CLIENT = "TestClient"
GRPC_VARIANTS = [
GRPC_VARIANT_SERVER,
GRPC_VARIANT_CLIENT,
GRPC_VARIANT_TEST_CLIENT,
]
GRPC_PLUGIN_OPTION_ALLOWLIST = PROTO_PLUGIN_OPTION_ALLOWLIST + [
"KeepMethodCasing",
"ExtraModuleImports",
"GRPCModuleName",
"SwiftProtobufModuleName",
] + GRPC_VARIANTS

def make_grpc_swift_proto_compiler(
name,
variants,
plugin_options = PROTO_PLUGIN_OPTIONS):
"""Generates a GRPC swift_proto_compiler target for the given variants.
Args:
name: The name of the generated swift proto compiler target.
variants: The list of variants the compiler should generate.
plugin_options: Additional options to pass to the plugin.
"""

# Merge the plugin options to include the variants:
merged_plugin_options = dicts.add(
plugin_options,
{variant: "false" for variant in GRPC_VARIANTS},
)
for variant in variants:
merged_plugin_options[variant] = "true"

swift_proto_compiler(
name = name,
protoc = "@com_google_protobuf//:protoc",
plugin = "@swiftpkg_grpc_swift//:protoc-gen-grpc-swift",
plugin_name = name.removesuffix("_proto"),
plugin_option_allowlist = GRPC_PLUGIN_OPTION_ALLOWLIST,
plugin_options = merged_plugin_options,
suffixes = [".grpc.swift"],
deps = [
"@swiftpkg_swift_protobuf//:SwiftProtobuf",
"@swiftpkg_grpc_swift//:GRPC",
],
visibility = ["//visibility:public"],
)
Empty file.
8 changes: 4 additions & 4 deletions examples/grpc_example/protos/echo_service/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ proto_library(
swift_proto_library(
name = "echo_service_swift_client_proto",
compilers = [
"@build_bazel_rules_swift//proto/compilers:swift_proto",
"@build_bazel_rules_swift//proto/compilers:swift_client_proto",
"//compilers:swift_proto",
"//compilers:swift_client_proto",
],
module_name = "EchoServiceClient",
protos = [":echo_service_proto"],
Expand All @@ -31,8 +31,8 @@ swift_proto_library(
swift_proto_library(
name = "echo_service_swift_server_proto",
compilers = [
"@build_bazel_rules_swift//proto/compilers:swift_proto",
"@build_bazel_rules_swift//proto/compilers:swift_server_proto",
"//compilers:swift_proto",
"//compilers:swift_server_proto",
],
module_name = "EchoServiceServer",
protos = [":echo_service_proto"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ proto_library(

swift_proto_library(
name = "echo_request_swift_proto",
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
compilers = ["//compilers:swift_proto"],
module_name = "EchoRequest",
protos = [":echo_request_proto"],
visibility = ["//visibility:public"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ proto_library(

swift_proto_library(
name = "echo_response_swift_proto",
compilers = ["@build_bazel_rules_swift//proto/compilers:swift_proto"],
compilers = ["//compilers:swift_proto"],
module_name = "EchoResponse",
protos = [":echo_response_proto"],
visibility = ["//visibility:public"],
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions examples/grpc_example/sources/client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ swift_binary(
"//protos/echo_service:echo_service_swift_client_proto",
"//protos/echo_service/requests:echo_request_swift_proto",
"//protos/echo_service/responses:echo_response_swift_proto",
"@swiftpkg_grpc_swift//:GRPC",
"@swiftpkg_swift_protobuf//:SwiftProtobuf",
],
)
1 change: 1 addition & 0 deletions examples/grpc_example/sources/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ swift_binary(
"//protos/echo_service:echo_service_swift_server_proto",
"//protos/echo_service/requests:echo_request_swift_proto",
"//protos/echo_service/responses:echo_response_swift_proto",
"@swiftpkg_grpc_swift//:GRPC",
],
)
1 change: 1 addition & 0 deletions examples/grpc_example/sources/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ swift_test(
"//protos/echo_service:echo_service_swift_server_proto",
"//protos/echo_service/requests:echo_request_swift_proto",
"//protos/echo_service/responses:echo_response_swift_proto",
"@swiftpkg_grpc_swift//:GRPC",
],
)
3 changes: 3 additions & 0 deletions examples/grpc_package_example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tidy(
# gazelle:swift_proto_generation_mode package
# gazelle:swift_module_naming_convention pascal_case
# gazelle:swift_generate_proto_libraries true
# gazelle:swift_proto_compiler swift_proto=//compilers:swift_proto
# gazelle:swift_proto_compiler swift_client_proto=//compilers:swift_client_proto
# gazelle:swift_proto_compiler swift_server_proto=//compilers:swift_server_proto
# gazelle:swift_generate_grpc_libraries_with_flavors swift_client_proto,swift_server_proto
# gazelle:exclude .build

Expand Down
19 changes: 19 additions & 0 deletions examples/grpc_package_example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ apple_cc_configure = use_extension(
"apple_cc_configure_extension",
)
use_repo(apple_cc_configure, "local_config_apple_cc")

# swift_deps START
swift_deps = use_extension(
"@rules_swift_package_manager//:extensions.bzl",
"swift_deps",
)
swift_deps.from_package(
declare_swift_deps_info = True,
resolved = "//:Package.resolved",
swift = "//:Package.swift",
)
use_repo(
swift_deps,
"swift_deps_info",
"swift_package",
"swiftpkg_grpc_swift",
"swiftpkg_swift_protobuf",
)
# swift_deps END
Loading

0 comments on commit 676c95e

Please sign in to comment.