Skip to content

bazel: Remove hardcoded dependency on //:protoc from language runtimes #19679

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 30 additions & 14 deletions protobuf.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@rules_cc//cc:defs.bzl", "objc_library")
load("@rules_python//python:defs.bzl", "py_library")
load("//bazel/common:proto_common.bzl", "proto_common")
load("//bazel/common:proto_info.bzl", "ProtoInfo")
load("//bazel/private:toolchain_helpers.bzl", "toolchains")

def _GetPath(ctx, path):
if ctx.label.workspace_root:
Expand Down Expand Up @@ -71,6 +73,26 @@ def _CsharpOuts(srcs):
for src in srcs
]

_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
"_proto_compiler": attr.label(
cfg = "exec",
executable = True,
allow_files = True,
default = configuration_field("proto", "proto_compiler"),
),
})
_PROTOC_FRAGMENTS = ["proto"]
_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)

def _protoc_files_to_run(ctx):
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
if not toolchain:
fail("Protocol compiler toolchain could not be resolved.")
return toolchain.proto.proto_compiler
else:
return ctx.attr._proto_compiler[DefaultInfo].files_to_run

ProtoGenInfo = provider(
fields = ["srcs", "import_flags", "deps"],
)
Expand Down Expand Up @@ -310,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
args.add_all([src.path[offset:] for src in dep.direct_sources])

ctx.actions.run(
executable = ctx.executable._protoc,
executable = _protoc_files_to_run(ctx),
inputs = descriptors,
outputs = [srcjar],
arguments = [args],
Expand All @@ -334,12 +356,9 @@ internal_gen_well_known_protos_java = rule(
"javalite": attr.bool(
default = False,
),
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "//:protoc",
),
},
} | _PROTOC_ATTRS,
fragments = _PROTOC_FRAGMENTS,
toolchains = _PROTOC_TOOLCHAINS,
)

def _internal_gen_kt_protos(ctx):
Expand Down Expand Up @@ -373,7 +392,7 @@ def _internal_gen_kt_protos(ctx):
args.add_all([src.path[offset:] for src in dep.direct_sources])

ctx.actions.run(
executable = ctx.executable._protoc,
executable = _protoc_files_to_run(ctx),
inputs = descriptors,
outputs = [srcjar],
arguments = [args],
Expand All @@ -397,12 +416,9 @@ internal_gen_kt_protos = rule(
"lite": attr.bool(
default = False,
),
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "//:protoc",
),
},
} | _PROTOC_ATTRS,
fragments = _PROTOC_FRAGMENTS,
toolchains = _PROTOC_TOOLCHAINS,
)

def internal_objc_proto_library(
Expand Down
Loading