From bc1a0e3787c29d542d197c81a8a018d3d4432e9d Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 16 Dec 2024 15:55:24 +0100 Subject: [PATCH 1/2] bazel: Remove hardcoded dependency on `//:protoc` from language runtimes Without this change, language runtimes still result in a build of `//:protoc` even with a prebuilt `proto_toolchain` registered or `--proto_compiler` set to a precompiled protoc. --- bazel/private/BUILD | 6 ++++++ protobuf.bzl | 13 +++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bazel/private/BUILD b/bazel/private/BUILD index e05addd1b1e47..5fb468172e0ec 100644 --- a/bazel/private/BUILD +++ b/bazel/private/BUILD @@ -1,4 +1,5 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load(":current_protoc.bzl", "current_protoc") load(":native_bool_flag.bzl", "native_bool_flag") package(default_applicable_licenses = ["//:license"]) @@ -28,6 +29,11 @@ toolchain_type( visibility = ["//visibility:public"], ) +current_protoc( + name = "current_protoc", + visibility = ["//:__subpackages__"], +) + bzl_library( name = "upb_proto_library_internal_bzl", srcs = [ diff --git a/protobuf.bzl b/protobuf.bzl index 283c858507e47..06144dcb50ecf 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -2,6 +2,7 @@ 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_info.bzl", "ProtoInfo") +load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun") def _GetPath(ctx, path): if ctx.label.workspace_root: @@ -310,7 +311,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 = ctx.attr._protoc[ProtocFilesToRun].files_to_run, inputs = descriptors, outputs = [srcjar], arguments = [args], @@ -335,9 +336,7 @@ internal_gen_well_known_protos_java = rule( default = False, ), "_protoc": attr.label( - executable = True, - cfg = "exec", - default = "//:protoc", + default = "//bazel/private:current_protoc", ), }, ) @@ -373,7 +372,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 = ctx.attr._protoc[ProtocFilesToRun].files_to_run, inputs = descriptors, outputs = [srcjar], arguments = [args], @@ -398,9 +397,7 @@ internal_gen_kt_protos = rule( default = False, ), "_protoc": attr.label( - executable = True, - cfg = "exec", - default = "//:protoc", + default = "//bazel/private:current_protoc", ), }, ) From 3bd939debed27dc8eb092a8d4419cdcf5e571594 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 16 Dec 2024 22:20:13 +0100 Subject: [PATCH 2/2] Switch to toolchains --- bazel/private/BUILD | 6 ------ protobuf.bzl | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/bazel/private/BUILD b/bazel/private/BUILD index 5fb468172e0ec..e05addd1b1e47 100644 --- a/bazel/private/BUILD +++ b/bazel/private/BUILD @@ -1,5 +1,4 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") -load(":current_protoc.bzl", "current_protoc") load(":native_bool_flag.bzl", "native_bool_flag") package(default_applicable_licenses = ["//:license"]) @@ -29,11 +28,6 @@ toolchain_type( visibility = ["//visibility:public"], ) -current_protoc( - name = "current_protoc", - visibility = ["//:__subpackages__"], -) - bzl_library( name = "upb_proto_library_internal_bzl", srcs = [ diff --git a/protobuf.bzl b/protobuf.bzl index 06144dcb50ecf..ad91faba65570 100644 --- a/protobuf.bzl +++ b/protobuf.bzl @@ -1,8 +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:current_protoc.bzl", "ProtocFilesToRun") +load("//bazel/private:toolchain_helpers.bzl", "toolchains") def _GetPath(ctx, path): if ctx.label.workspace_root: @@ -72,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"], ) @@ -311,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.attr._protoc[ProtocFilesToRun].files_to_run, + executable = _protoc_files_to_run(ctx), inputs = descriptors, outputs = [srcjar], arguments = [args], @@ -335,10 +356,9 @@ internal_gen_well_known_protos_java = rule( "javalite": attr.bool( default = False, ), - "_protoc": attr.label( - default = "//bazel/private:current_protoc", - ), - }, + } | _PROTOC_ATTRS, + fragments = _PROTOC_FRAGMENTS, + toolchains = _PROTOC_TOOLCHAINS, ) def _internal_gen_kt_protos(ctx): @@ -372,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.attr._protoc[ProtocFilesToRun].files_to_run, + executable = _protoc_files_to_run(ctx), inputs = descriptors, outputs = [srcjar], arguments = [args], @@ -396,10 +416,9 @@ internal_gen_kt_protos = rule( "lite": attr.bool( default = False, ), - "_protoc": attr.label( - default = "//bazel/private:current_protoc", - ), - }, + } | _PROTOC_ATTRS, + fragments = _PROTOC_FRAGMENTS, + toolchains = _PROTOC_TOOLCHAINS, ) def internal_objc_proto_library(