Skip to content

Commit 3b18d3f

Browse files
comiuscopybara-github
authored andcommitted
Refactor proto toolchainsation support utilities
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
1 parent c1627af commit 3b18d3f

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

src/main/starlark/builtins_bzl/common/proto/proto_common.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,34 @@ def _declare_generated_files(
350350

351351
return outputs
352352

353+
def _find_toolchain(ctx, legacy_attr, toolchain_type):
354+
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
355+
toolchain = ctx.toolchains[toolchain_type]
356+
if not toolchain:
357+
fail("No toolchains registered for '%s'." % toolchain_type)
358+
return toolchain.proto
359+
else:
360+
return getattr(ctx.attr, legacy_attr)[ProtoLangToolchainInfo]
361+
362+
def _use_toolchain(toolchain_type):
363+
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
364+
return [_builtins.toplevel.config_common.toolchain_type(toolchain_type, mandatory = False)]
365+
else:
366+
return []
367+
368+
def _if_legacy_toolchain(legacy_attr_dict):
369+
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
370+
return {}
371+
else:
372+
return legacy_attr_dict
373+
374+
toolchains = struct(
375+
use_toolchain = _use_toolchain,
376+
find_toolchain = _find_toolchain,
377+
if_legacy_toolchain = _if_legacy_toolchain,
378+
INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution(),
379+
)
380+
353381
proto_common_do_not_use = struct(
354382
compile = _compile,
355383
declare_generated_files = _declare_generated_files,

src/main/starlark/builtins_bzl/common/proto/proto_lang_toolchain.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

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

17+
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo", "toolchains")
1718
load(":common/proto/proto_info.bzl", "ProtoInfo")
18-
load(":common/proto/proto_common.bzl", "ProtoLangToolchainInfo")
1919
load(":common/proto/proto_semantics.bzl", "semantics")
2020

2121
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo
@@ -32,9 +32,9 @@ def _rule_impl(ctx):
3232
if ctx.attr.plugin != None:
3333
plugin = ctx.attr.plugin[DefaultInfo].files_to_run
3434

35-
if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
36-
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.proto_compiler
37-
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE].proto.protoc_opts
35+
if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
36+
proto_compiler = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.proto_compiler
37+
protoc_opts = ctx.toolchains[semantics.PROTO_TOOLCHAIN].proto.protoc_opts
3838
else:
3939
proto_compiler = ctx.attr._proto_compiler.files_to_run
4040
protoc_opts = ctx.fragments.proto.experimental_protoc_opts
@@ -81,7 +81,7 @@ proto_lang_toolchain = rule(
8181
cfg = "exec",
8282
providers = [PackageSpecificationInfo],
8383
),
84-
} | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
84+
} | ({} if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
8585
"_proto_compiler": attr.label(
8686
cfg = "exec",
8787
executable = True,
@@ -91,5 +91,5 @@ proto_lang_toolchain = rule(
9191
}),
9292
provides = [ProtoLangToolchainInfo],
9393
fragments = ["proto"],
94-
toolchains = semantics.PROTO_TOOLCHAIN, # Used to obtain protoc
94+
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN), # Used to obtain protoc
9595
)

src/main/starlark/builtins_bzl/common/proto/proto_library.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Definition of proto_library rule.
1717
"""
1818

1919
load(":common/paths.bzl", "paths")
20-
load(":common/proto/proto_common.bzl", proto_common = "proto_common_do_not_use")
20+
load(":common/proto/proto_common.bzl", "toolchains", proto_common = "proto_common_do_not_use")
2121
load(":common/proto/proto_info.bzl", "ProtoInfo")
2222
load(":common/proto/proto_semantics.bzl", "semantics")
2323

@@ -208,8 +208,8 @@ def _write_descriptor_set(ctx, proto_info, deps, exports, descriptor_set):
208208
map_each = proto_common.get_import_path,
209209
join_with = ":",
210210
)
211-
if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
212-
toolchain = ctx.toolchains[semantics.PROTO_TOOLCHAIN_TYPE]
211+
if toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
212+
toolchain = ctx.toolchains[semantics.PROTO_TOOLCHAIN]
213213
if not toolchain:
214214
fail("Protocol compiler toolchain could not be resolved.")
215215
proto_lang_toolchain_info = toolchain.proto
@@ -256,7 +256,7 @@ proto_library = rule(
256256
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
257257
),
258258
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
259-
} | ({} if semantics.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION else {
259+
} | toolchains.if_legacy_toolchain({
260260
"_proto_compiler": attr.label(
261261
cfg = "exec",
262262
executable = True,
@@ -267,5 +267,5 @@ proto_library = rule(
267267
fragments = ["proto"] + semantics.EXTRA_FRAGMENTS,
268268
provides = [ProtoInfo],
269269
exec_groups = semantics.EXEC_GROUPS,
270-
toolchains = semantics.PROTO_TOOLCHAIN,
270+
toolchains = toolchains.use_toolchain(semantics.PROTO_TOOLCHAIN),
271271
)

src/main/starlark/builtins_bzl/common/proto/proto_semantics.bzl

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,8 @@ Proto Semantics
1919
def _preprocess(ctx):
2020
pass
2121

22-
_PROTO_TOOLCHAIN_TYPE = "@rules_proto//proto:toolchain_type"
23-
24-
def _get_proto_toolchain():
25-
if _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution():
26-
return [_builtins.toplevel.config_common.toolchain_type(_PROTO_TOOLCHAIN_TYPE, mandatory = False)]
27-
else:
28-
return []
29-
3022
semantics = struct(
31-
PROTO_TOOLCHAIN_TYPE = _PROTO_TOOLCHAIN_TYPE,
32-
PROTO_TOOLCHAIN = _get_proto_toolchain(),
33-
INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = _builtins.toplevel.proto_common.incompatible_enable_proto_toolchain_resolution(),
23+
PROTO_TOOLCHAIN = "@rules_proto//proto:toolchain_type",
3424
PROTO_COMPILER_LABEL = "@bazel_tools//tools/proto:protoc",
3525
EXTRA_ATTRIBUTES = {
3626
"import_prefix": attr.string(),

0 commit comments

Comments
 (0)