Skip to content

Commit 3bd939d

Browse files
committed
Switch to toolchains
1 parent bc1a0e3 commit 3bd939d

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

bazel/private/BUILD

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load(":current_protoc.bzl", "current_protoc")
32
load(":native_bool_flag.bzl", "native_bool_flag")
43

54
package(default_applicable_licenses = ["//:license"])
@@ -29,11 +28,6 @@ toolchain_type(
2928
visibility = ["//visibility:public"],
3029
)
3130

32-
current_protoc(
33-
name = "current_protoc",
34-
visibility = ["//:__subpackages__"],
35-
)
36-
3731
bzl_library(
3832
name = "upb_proto_library_internal_bzl",
3933
srcs = [

protobuf.bzl

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
load("@bazel_skylib//lib:versions.bzl", "versions")
22
load("@rules_cc//cc:defs.bzl", "objc_library")
33
load("@rules_python//python:defs.bzl", "py_library")
4+
load("//bazel/common:proto_common.bzl", "proto_common")
45
load("//bazel/common:proto_info.bzl", "ProtoInfo")
5-
load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun")
6+
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
67

78
def _GetPath(ctx, path):
89
if ctx.label.workspace_root:
@@ -72,6 +73,26 @@ def _CsharpOuts(srcs):
7273
for src in srcs
7374
]
7475

76+
_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
77+
"_proto_compiler": attr.label(
78+
cfg = "exec",
79+
executable = True,
80+
allow_files = True,
81+
default = configuration_field("proto", "proto_compiler"),
82+
),
83+
})
84+
_PROTOC_FRAGMENTS = ["proto"]
85+
_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
86+
87+
def _protoc_files_to_run(ctx):
88+
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
89+
toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
90+
if not toolchain:
91+
fail("Protocol compiler toolchain could not be resolved.")
92+
return toolchain.proto.proto_compiler
93+
else:
94+
return ctx.attr._proto_compiler[DefaultInfo].files_to_run
95+
7596
ProtoGenInfo = provider(
7697
fields = ["srcs", "import_flags", "deps"],
7798
)
@@ -311,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
311332
args.add_all([src.path[offset:] for src in dep.direct_sources])
312333

313334
ctx.actions.run(
314-
executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
335+
executable = _protoc_files_to_run(ctx),
315336
inputs = descriptors,
316337
outputs = [srcjar],
317338
arguments = [args],
@@ -335,10 +356,9 @@ internal_gen_well_known_protos_java = rule(
335356
"javalite": attr.bool(
336357
default = False,
337358
),
338-
"_protoc": attr.label(
339-
default = "//bazel/private:current_protoc",
340-
),
341-
},
359+
} | _PROTOC_ATTRS,
360+
fragments = _PROTOC_FRAGMENTS,
361+
toolchains = _PROTOC_TOOLCHAINS,
342362
)
343363

344364
def _internal_gen_kt_protos(ctx):
@@ -372,7 +392,7 @@ def _internal_gen_kt_protos(ctx):
372392
args.add_all([src.path[offset:] for src in dep.direct_sources])
373393

374394
ctx.actions.run(
375-
executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
395+
executable = _protoc_files_to_run(ctx),
376396
inputs = descriptors,
377397
outputs = [srcjar],
378398
arguments = [args],
@@ -396,10 +416,9 @@ internal_gen_kt_protos = rule(
396416
"lite": attr.bool(
397417
default = False,
398418
),
399-
"_protoc": attr.label(
400-
default = "//bazel/private:current_protoc",
401-
),
402-
},
419+
} | _PROTOC_ATTRS,
420+
fragments = _PROTOC_FRAGMENTS,
421+
toolchains = _PROTOC_TOOLCHAINS,
403422
)
404423

405424
def internal_objc_proto_library(

0 commit comments

Comments
 (0)