Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose protoc-gen-grpc-swift as a gRPC generator #711

Closed
wants to merge 13 commits into from
97 changes: 78 additions & 19 deletions swift/internal/swift_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def _register_grpcswift_generate_action(
generate_from_proto_sources,
mkdir_and_run,
protoc_executable,
protoc_plugin_executable,
protoc_plugin_name,
protoc_gen_swiftgrpc,
protoc_gen_grpc_swift,
flavor,
extra_module_imports):
"""Registers actions to generate `.grpc.swift` files from `.proto` files.
Expand Down Expand Up @@ -107,33 +109,46 @@ def _register_grpcswift_generate_action(
protoc_args.set_param_file_format("multiline")
protoc_args.use_param_file("@%s")

if protoc_plugin_name == "swiftgrpc":
protoc_plugin_executable = protoc_gen_swiftgrpc
elif protoc_plugin_name == "grpc-swift":
protoc_plugin_executable = protoc_gen_grpc_swift
else:
fail("Unrecognized protoc_plugin", attr = "protoc_plugin")
protoc_args.add(
protoc_plugin_executable,
format = "--plugin=protoc-gen-swiftgrpc=%s",
format = "--plugin=protoc-gen-{}=%s".format(protoc_plugin_name),
)
protoc_args.add(generated_dir_path, format = "--swiftgrpc_out=%s")
protoc_args.add("--swiftgrpc_opt=Visibility=Public")

protoc_args.add(generated_dir_path, format = "--{}_out=%s".format(protoc_plugin_name))
protoc_args.add("--{}_opt=Visibility=Public".format(protoc_plugin_name))
if flavor == "client":
protoc_args.add("--swiftgrpc_opt=Client=true")
protoc_args.add("--swiftgrpc_opt=Server=false")
protoc_args.add("--{}_opt=Client=true".format(protoc_plugin_name))
protoc_args.add("--{}_opt=Server=false".format(protoc_plugin_name))
elif flavor == "client_stubs":
protoc_args.add("--swiftgrpc_opt=Client=true")
protoc_args.add("--swiftgrpc_opt=Server=false")
protoc_args.add("--swiftgrpc_opt=TestStubs=true")
protoc_args.add("--swiftgrpc_opt=Implementations=false")
protoc_args.add("--{}_opt=Client=true".format(protoc_plugin_name))
protoc_args.add("--{}_opt=Server=false".format(protoc_plugin_name))

if protoc_plugin_name == "swiftgrpc":
protoc_args.add("--swiftgrpc_opt=TestStubs=true")
protoc_args.add("--swiftgrpc_opt=Implementations=false")
elif protoc_plugin_name == "grpc-swift":
protoc_args.add("--grpc-swift_opt=ClientStubs=true")
else:
fail("Unsupported swift_grpc_library protoc_plugin_name", attr = "protoc_plugin_name")
elif flavor == "server":
protoc_args.add("--swiftgrpc_opt=Client=false")
protoc_args.add("--swiftgrpc_opt=Server=true")
protoc_args.add("--{}_opt=Client=false".format(protoc_plugin_name))
protoc_args.add("--{}_opt=Server=true".format(protoc_plugin_name))
else:
fail("Unsupported swift_grpc_library flavor", attr = "flavor")
protoc_args.add_all(
extra_module_imports,
format_each = "--swiftgrpc_opt=ExtraModuleImports=%s",
format_each = "--{}_opt=ExtraModuleImports=%s".format(protoc_plugin_name),
)
if module_mapping_file:
protoc_args.add(
module_mapping_file,
format = "--swiftgrpc_opt=ProtoPathModuleMappings=%s",
format = "--{}_opt=ProtoPathModuleMappings=%s".format(protoc_plugin_name),
)

protoc_args.add_joined(
Expand Down Expand Up @@ -249,6 +264,17 @@ def _swift_grpc_library_impl(ctx):
if ctx.attr.flavor == "client_stubs":
extra_module_imports.append(swift_common.derive_module_name(deps[0].label))

# grpc-swift depends on SwiftProtobuf to determine which modules need to be imported
# into the generated .grpc.swift file for the generated .gRPC.swift file
#
# SwiftProtobuf explicitly disallows the current .proto file's module name
# but when generating the .grpc.swift and .pb.swift files to different locations
# one MUST import the current .proto file's module name
if ctx.attr.protoc_plugin_name == "grpc-swift":
proto_module_name = deps[0][SwiftInfo].direct_modules[0].name
extra_module_imports.append(proto_module_name)
Comment on lines +267 to +275
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a hack that i don't want to commit to rules_swift. we should have a better way to determine if we want to exclude the current module or not, but that involves at least two upstream tickets (against grpc-swift and swift-protobuf).



# Generate the Swift sources from the .proto files.
generated_files = _register_grpcswift_generate_action(
ctx.label,
Expand All @@ -260,7 +286,9 @@ def _swift_grpc_library_impl(ctx):
generate_from_proto_sources,
ctx.executable._mkdir_and_run,
ctx.executable._protoc,
ctx.attr.protoc_plugin_name,
ctx.executable._protoc_gen_swiftgrpc,
ctx.executable._protoc_gen_grpc_swift,
ctx.attr.flavor,
extra_module_imports,
)
Expand All @@ -269,7 +297,13 @@ def _swift_grpc_library_impl(ctx):
# .swiftmodule as outputs. In addition to the other proto deps, we also pass
# support libraries like the SwiftProtobuf runtime as deps to the compile
# action.
compile_deps = deps + ctx.attr._proto_support
if ctx.attr.protoc_plugin_name == "swiftgrpc":
proto_support = ctx.attr._protoc_gen_swiftgrpc_support
elif ctx.attr.protoc_plugin_name == "grpc-swift":
proto_support = ctx.attr._protoc_gen_grpc_swift_support
else:
fail("Unsupported swift_grpc_library flavor", attr = "flavor")
compile_deps = deps + proto_support

module_name = swift_common.derive_module_name(ctx.label)

Expand Down Expand Up @@ -375,6 +409,17 @@ The kind of definitions that should be generated:
* `"client_stubs"` to generate client test stubs.

* `"server"` to generate server definitions.
""",
),
"protoc_plugin_name": attr.string(
values = ["grpc-swift", "swiftgrpc"],
default = "swiftgrpc",
doc = """\
The swift-grpc gRPC protoc plugin to use:

* `"grpc-swift"` to generate SwiftNIO-based clients.

* `"swiftgrpc"` to generate gRPC C library-based clients.
""",
),
"_mkdir_and_run": attr.label(
Expand All @@ -384,10 +429,6 @@ The kind of definitions that should be generated:
),
executable = True,
),
# TODO(b/63389580): Migrate to proto_lang_toolchain.
"_proto_support": attr.label_list(
default = [Label("@com_github_grpc_grpc_swift//:SwiftGRPC")],
),
"_protoc": attr.label(
cfg = "exec",
default = Label(
Expand All @@ -402,6 +443,24 @@ The kind of definitions that should be generated:
),
executable = True,
),
# TODO(b/63389580): Migrate to proto_lang_toolchain.
"_protoc_gen_swiftgrpc_support": attr.label_list(
default = [
Label("@com_github_grpc_grpc_swift//:SwiftGRPC"),
],
),
"_protoc_gen_grpc_swift": attr.label(
cfg = "exec",
default = Label(
"@com_github_grpc_grpc_swift_swiftnio//:protoc-gen-grpc-swift",
),
executable = True,
),
"_protoc_gen_grpc_swift_support": attr.label_list(
default = [
Label("@com_github_grpc_grpc_swift_swiftnio//:GRPC"),
],
),
},
),
doc = """\
Expand Down
69 changes: 69 additions & 0 deletions swift/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,66 @@ def swift_rules_dependencies():
strip_prefix = "rules_cc-1477dbab59b401daa94acedbeaefe79bf9112167/",
)

_maybe(
http_archive,
name = "com_github_apple_swift_log",
urls = ["https://github.com/apple/swift-log/archive/refs/tags/1.4.2.zip"],
sha256 = "9fd608037153fa3944d212bb2082458343adf52bdc2b5060a319e197b77d6a82",
strip_prefix = "swift-log-1.4.2/",
type = "zip",
build_file="@build_bazel_rules_swift//third_party:com_github_apple_swift_log/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_nio",
urls = ["https://github.com/apple/swift-nio/archive/refs/tags/2.33.0.zip"],
sha256 = "c9d586c0d53a49877214bd3a3c3c45986d5c1409c83dc7c6f135e47467f47963",
strip_prefix = "swift-nio-2.33.0/",
type = "zip",
build_file="@build_bazel_rules_swift//third_party:com_github_apple_swift_nio/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_nio_extras",
urls = ["https://github.com/apple/swift-nio-extras/archive/refs/tags/1.10.2.zip"],
sha256 = "7efb3e5b97b596b78561838770221146ac2dd5f33f92036cb11e8e35cb14d3ce",
strip_prefix="swift-nio-extras-1.10.2/",
type="zip",
build_file="@build_bazel_rules_swift//third_party:com_github_apple_swift_nio_extras/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_nio_http2",
urls = ["https://github.com/apple/swift-nio-http2/archive/refs/tags/1.18.4.zip"],
sha256 = "296447db362e6d3ad357b160c3b9f2e9ed96852039e8e5817dcc4012737cd72c",
strip_prefix="swift-nio-http2-1.18.4/",
type="zip",
build_file="@build_bazel_rules_swift//third_party:com_github_apple_swift_nio_http2/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_nio_ssl",
urls = ["https://github.com/apple/swift-nio-ssl/archive/2.16.1.zip"],
sha256 = "573b1c67429a85c32878b8d3979fa58987ac850dc11db76697d7b1bf44057843",
strip_prefix = "swift-nio-ssl-2.16.1/",
type = "zip",
build_file = "@build_bazel_rules_swift//third_party:com_github_apple_swift_nio_ssl/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_nio_transport_services",
urls = ["https://github.com/apple/swift-nio-transport-services/archive/1.11.3.zip"],
sha256 = "1c9036131370a82f48577342aad700ee6afb955c99d513b3f1b626bc086d7e3d",
strip_prefix = "swift-nio-transport-services-1.11.3/",
type = "zip",
build_file = "@build_bazel_rules_swift//third_party:com_github_apple_swift_nio_transport_services/BUILD.overlay",
)

_maybe(
http_archive,
name = "com_github_apple_swift_protobuf",
Expand All @@ -84,6 +144,15 @@ def swift_rules_dependencies():
type = "zip",
build_file = "@build_bazel_rules_swift//third_party:com_github_grpc_grpc_swift/BUILD.overlay",
)
_maybe(
http_archive,
name = "com_github_grpc_grpc_swift_swiftnio",
urls = ["https://github.com/grpc/grpc-swift/archive/1.5.0.zip"],
sha256 = "573b12ca8f5c6848c503300df8d0a667d729d1457b925f5278f01497d90a9b30",
strip_prefix = "grpc-swift-1.5.0/",
type = "zip",
build_file = "@build_bazel_rules_swift//third_party:com_github_grpc_grpc_swift_swiftnio/BUILD.overlay",
)

_maybe(
http_archive,
Expand Down
9 changes: 9 additions & 0 deletions third_party/com_github_apple_swift_log/BUILD.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

swift_library(
name = "Logging",
srcs = glob([
"Sources/Logging/**/*.swift",
]),
visibility = ["//visibility:public"],
)
Loading