Skip to content

Commit

Permalink
Refactor proto toolchainsation support utilities
Browse files Browse the repository at this point in the history
Implement reusable utils as part of proto_common and move out of semantics. Define toolchains that will be needed for lang_proto_libraries.

Because there are so many toolchains this will make the future code more readable.

Issue: bazelbuild/rules_proto#179
PiperOrigin-RevId: 570019432
Change-Id: Ie1675f1d847872bab3a250270eaa451ef4816ed8
  • Loading branch information
comius committed Jan 17, 2024
1 parent 36394f9 commit 0895719
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
28 changes: 28 additions & 0 deletions src/main/starlark/builtins_bzl/common/proto/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ def _declare_generated_files(

return outputs

def _find_toolchain(ctx, legacy_attr, toolchain_type):
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
toolchain = ctx.toolchains[toolchain_type]
if not toolchain:
fail("No toolchains registered for '%s'." % toolchain_type)
return toolchain.proto
else:
return getattr(ctx.attr, legacy_attr)[ProtoLangToolchainInfo]

def _use_toolchain(toolchain_type):
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
return [_builtins.toplevel.config_common.toolchain_type(toolchain_type, mandatory = False)]
else:
return []

def _if_legacy_toolchain(legacy_attr_dict):
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
return {}
else:
return legacy_attr_dict

toolchains = struct(
use_toolchain = _use_toolchain,
find_toolchain = _find_toolchain,
if_legacy_toolchain = _if_legacy_toolchain,
INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution(),
)

proto_common_do_not_use = struct(
compile = _compile,
declare_generated_files = _declare_generated_files,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""A Starlark implementation of the proto_lang_toolchain rule."""

load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo")
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", "toolchains")
load(":common/proto/proto_semantics.bzl", "semantics")

ProtoInfo = _builtins.toplevel.ProtoInfo
Expand All @@ -31,9 +31,9 @@ def _rule_impl(ctx):
if ctx.attr.plugin != None:
plugin = ctx.attr.plugin[DefaultInfo].files_to_run

if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.proto_compiler
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.protoc_opts
if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.proto_compiler
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.protoc_opts
else:
proto_compiler = ctx.attr._proto_compiler.files_to_run
protoc_opts = ctx.fragments.proto.experimental_protoc_opts
Expand Down Expand Up @@ -74,7 +74,7 @@ proto_lang_toolchain = rule(
"blacklisted_protos": attr.label_list(
providers = [ProtoInfo],
),
} | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
} | ({} if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
"_proto_compiler": attr.label(
cfg = "exec",
executable = True,
Expand All @@ -84,5 +84,5 @@ proto_lang_toolchain = rule(
}),
provides = [ProtoLangToolchainInfo],
fragments = ["proto"],
toolchains = semantics.PROTO_TOOLCHAIN, # Used to obtain protoc
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN), # Used to obtain protoc
)
10 changes: 5 additions & 5 deletions src/main/starlark/builtins_bzl/common/proto/proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
Definition of proto_library rule.
"""

load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use")
load(":common/proto/proto_semantics.bzl", "semantics")
load(":common/proto/proto_common.bzl", proto_common = "proto_common_do_not_use")
load(":common/paths.bzl", "paths")

ProtoInfo = _builtins.toplevel.ProtoInfo
Expand Down Expand Up @@ -251,8 +251,8 @@ def _write_descriptor_set(ctx, direct_sources, deps, exports, proto_info, descri
args.add("--allowed_public_imports=")
else:
args.add_joined("--allowed_public_imports", public_import_protos, map_each = _get_import_path, join_with = ":")
if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
toolchain = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE]
if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
toolchain = ctx.toolchains[semantics.PROTO_TOOLCHAIN]
if not toolchain:
fail("Protocol compiler toolchain could not be resolved.")
proto_lang_toolchain_info = toolchain.proto
Expand Down Expand Up @@ -295,7 +295,7 @@ proto_library = rule(
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
),
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
} | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
} | toolchains.if_legacy_toolchain({
"_proto_compiler": attr.label(
cfg = "exec",
executable = True,
Expand All @@ -306,5 +306,5 @@ proto_library = rule(
fragments = ["proto"] + semantics.EXTRA_FRAGMENTS,
provides = [ProtoInfo],
output_to_genfiles = True, # TODO(b/204266604) move to bin dir
toolchains = semantics.PROTO_TOOLCHAIN,
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN),
)
12 changes: 1 addition & 11 deletions src/main/starlark/builtins_bzl/common/proto/proto_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@ Proto Semantics
def _preprocess(ctx):
pass

_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"

def _get_proto_toolchain():
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
return [_builtins.toplevel.config_common.toolchain_type(_PROTO_TOOLCHAIN_TYPE, mandatory = False)]
else:
return []

semantics = struct(
PROTO_TOOLCHAIN_TYPE = _PROTO_TOOLCHAIN_TYPE,
PROTO_TOOLCHAIN = _get_proto_toolchain(),
INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution(),
PROTO_TOOLCHAIN = "@rules_proto//proto:toolchain_type",
PROTO_COMPILER_LABEL = "@bazel_tools//tools/proto:protoc",
EXTRA_ATTRIBUTES = {
"import_prefix": attr.string(),
Expand Down

0 comments on commit 0895719

Please sign in to comment.