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

Add current_proto_toolchain #214

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_license//rules:license.bzl", "license")
load("//proto:defs.bzl", "proto_toolchain")

license(
name = "license",
Expand All @@ -8,3 +9,8 @@ license(
licenses(["notice"])

exports_files(["LICENSE"])

proto_toolchain(
thesayyn marked this conversation as resolved.
Show resolved Hide resolved
name = "proto_toolchain",
proto_compiler = "@com_google_protobuf//:protoc",
)
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ bazel_dep(name = "rules_cc", version = "0.0.1", dev_dependency = True)
bazel_dep(name = "googletest", version = "1.11.0", dev_dependency = True, repo_name = "com_google_googletest")
bazel_dep(name = "protobuf", version = "23.1", dev_dependency = True, repo_name = "com_google_protobuf")
bazel_dep(name = "platforms", version = "0.0.8", dev_dependency = True)

register_toolchains(
"//:proto_toolchain_toolchain",
dev_dependency = True,
)
10 changes: 6 additions & 4 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ rbe_preconfig(
name = "buildkite_config",
toolchain = "ubuntu1804-bazel-java11",
)

register_toolchains(
"//:proto_toolchain_toolchain",
)
23 changes: 23 additions & 0 deletions docs/defs.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Starlark rules for building protocol buffers."""

load("//proto:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
load("//proto:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")
load("//proto:proto_toolchain.bzl", _current_proto_toolchain = "current_proto_toolchain", _proto_toolchain = "proto_toolchain")
load("//proto/private:native.bzl", "NativeProtoInfo", "native_proto_common")
load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")

Expand Down Expand Up @@ -45,6 +45,7 @@ proto_descriptor_set = _proto_descriptor_set
proto_lang_toolchain = _proto_lang_toolchain

proto_toolchain = _proto_toolchain
current_proto_toolchain = _current_proto_toolchain

# Encapsulates information provided by `proto_library`.
#
Expand Down
33 changes: 33 additions & 0 deletions proto/private/rules/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,36 @@ def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):
target_compatible_with = [],
toolchain = name,
)

def _current_proto_toolchain_impl(ctx):
toolchain = ctx.toolchains[ctx.attr._toolchain]

direct = [toolchain.proto.proto_compiler.executable]
transitive = []
files = depset(direct, transitive = transitive)
return [
toolchain,
platform_common.TemplateVariableInfo({
"PROTOC": str(toolchain.proto.proto_compiler.executable.path),
}),
DefaultInfo(
runfiles = ctx.runfiles(transitive_files = files),
files = files,
),
]

current_proto_toolchain = rule(
doc = """
This rule exists so that the current protoc toolchain can be used in the `toolchains` attribute of
other rules, such as genrule. It allows exposing a protoc toolchain after toolchain resolution has
happened, to a rule which expects a concrete implementation of a toolchain, rather than a
toolchain_type which could be resolved to that toolchain.
""",
implementation = _current_proto_toolchain_impl,
attrs = {
"_toolchain": attr.string(default = str(Label("@rules_proto//proto:toolchain_type"))),
},
toolchains = [
str(Label("@rules_proto//proto:toolchain_type")),
],
)
3 changes: 2 additions & 1 deletion proto/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Export for proto_toolchain"""

load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain_macro = "proto_toolchain")
load("//proto/private/rules:proto_toolchain.bzl", _current_proto_toolchain = "current_proto_toolchain", _proto_toolchain_macro = "proto_toolchain")

proto_toolchain = _proto_toolchain_macro
current_proto_toolchain = _current_proto_toolchain
35 changes: 35 additions & 0 deletions tests/current_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("//proto:defs.bzl", "current_proto_toolchain")

current_proto_toolchain(
name = "current_proto_toolchain",
)

genrule(
name = "gen_protoc_help_from_current_proto_toolchain",
srcs = [],
outs = ["protoc_help_from_current_proto_toolchain"],
cmd = """
v=$$($(PROTOC) --help)
echo $$v > $(OUTS)
""",
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
toolchains = [":current_proto_toolchain"],
)

# The genrule above will fail if PROTOC cannot be found, but using genrules for tests isn't recommended.
# This sh_test just checks if the output of the genrule has any content.
sh_test(
name = "test_protoc_help_from_current_proto_toolchain",
srcs = ["test_protoc_help_from_current_proto_toolchain.sh"],
data = [":protoc_help_from_current_proto_toolchain"],
env = {
"PROTOC_HELP_OUTPUT": "$(location :protoc_help_from_current_proto_toolchain)",
},
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh

# Fail if the file in the PROTOC_HELP_OUTPUT environment variable is empty
[ -s "$PROTOC_HELP_OUTPUT" ]