diff --git a/.github/workflows/test_bazel.yml b/.github/workflows/test_bazel.yml index 081fedcdfc8f..778fc9e12278 100644 --- a/.github/workflows/test_bazel.yml +++ b/.github/workflows/test_bazel.yml @@ -29,14 +29,21 @@ jobs: runner: [ ubuntu, windows, macos ] bazelversion: [ '7.1.2' ] bzlmod: [true, false ] + toolchain_resolution: [false ] include: - runner: ubuntu bazelversion: '6.4.0' # Not running Bazel 6 with bzlmod, because it doesn't support use_repo_rule in rules_jvm_external bzlmod: false continuous-only: true + - runner: ubuntu + bzlmod: false + toolchain_resolution: true + - runner: ubuntu + bzlmod: true + toolchain_resolution: true runs-on: ${{ matrix.runner }}-latest - name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Examples ${{ matrix.runner }} ${{ matrix.bazelversion }}${{ matrix.bzlmod && ' (bzlmod)' || '' }} + name: ${{ matrix.continuous-only && inputs.continuous-prefix || '' }} Examples ${{ matrix.runner }} ${{ matrix.bazelversion }}${{ matrix.bzlmod && ' (bzlmod)' || '' }} ${{ matrix.toolchain_resolution && ' (toolchain resolution)' || '' }} steps: - name: Checkout pending changes if: ${{ !matrix.continuous-only || inputs.continuous-run }} @@ -63,4 +70,4 @@ jobs: credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }} bazel-cache: examples version: ${{ matrix.bazelversion }} - bash: cd examples && bazel build //... $BAZEL_FLAGS --enable_bzlmod=${{ matrix.bzlmod }} + bash: cd examples && bazel build //... $BAZEL_FLAGS --enable_bzlmod=${{ matrix.bzlmod }} --incompatible_enable_proto_toolchain_resolution=${{ matrix.toolchain_resolution }} diff --git a/MODULE.bazel b/MODULE.bazel index c2b91dd29591..213d3c2823b1 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -89,6 +89,9 @@ bazel_dep( repo_name = "proto_bazel_features", ) +# Proto toolchains +register_toolchains("//bazel/private/toolchains:all") + SUPPORTED_PYTHON_VERSIONS = [ "3.8", "3.9", diff --git a/bazel/common/proto_common.bzl b/bazel/common/proto_common.bzl index 639cf4c8a4ff..a54f577c421c 100644 --- a/bazel/common/proto_common.bzl +++ b/bazel/common/proto_common.bzl @@ -9,6 +9,7 @@ load("@proto_bazel_features//:features.bzl", "bazel_features") load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") +load("//bazel/private:native.bzl", "native_proto_common") load("//bazel/private:toolchain_helpers.bzl", "toolchains") def _import_virtual_proto_path(path): @@ -347,5 +348,8 @@ proto_common = struct( get_import_path = _get_import_path, ProtoLangToolchainInfo = ProtoLangToolchainInfo, INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION = toolchains.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION, - INCOMPATIBLE_PASS_TOOLCHAIN_TYPE = True, + INCOMPATIBLE_PASS_TOOLCHAIN_TYPE = ( + getattr(native_proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False) or + not hasattr(native_proto_common, "ProtoLangToolchainInfo") + ), ) diff --git a/bazel/private/BUILD b/bazel/private/BUILD index 1369c7941848..9b04c1f8638b 100644 --- a/bazel/private/BUILD +++ b/bazel/private/BUILD @@ -5,6 +5,22 @@ package(default_applicable_licenses = ["//:license"]) toolchain_type( name = "proto_toolchain_type", + visibility = ["//visibility:public"], +) + +toolchain_type( + name = "cc_toolchain_type", + visibility = ["//visibility:public"], +) + +toolchain_type( + name = "java_toolchain_type", + visibility = ["//visibility:public"], +) + +toolchain_type( + name = "javalite_toolchain_type", + visibility = ["//visibility:public"], ) toolchain_type( diff --git a/bazel/private/bazel_cc_proto_library.bzl b/bazel/private/bazel_cc_proto_library.bzl index 74e346f855a0..20803ad527e5 100644 --- a/bazel/private/bazel_cc_proto_library.bzl +++ b/bazel/private/bazel_cc_proto_library.bzl @@ -13,7 +13,7 @@ load("//bazel/common:proto_info.bzl", "ProtoInfo") load("//bazel/private:cc_proto_support.bzl", "cc_proto_compile_and_link") load("//bazel/private:toolchain_helpers.bzl", "toolchains") -_CC_PROTO_TOOLCHAIN = "@rules_cc//cc/proto:toolchain_type" +_CC_PROTO_TOOLCHAIN = "//bazel/private:cc_toolchain_type" _ProtoCcFilesInfo = provider(fields = ["files"], doc = "Provide cc proto files.") _ProtoCcHeaderInfo = provider(fields = ["headers"], doc = "Provide cc proto headers.") diff --git a/bazel/private/bazel_java_proto_library_rule.bzl b/bazel/private/bazel_java_proto_library_rule.bzl index 906a69b46f91..adfbe68d8a1d 100644 --- a/bazel/private/bazel_java_proto_library_rule.bzl +++ b/bazel/private/bazel_java_proto_library_rule.bzl @@ -12,8 +12,7 @@ load("//bazel/common:proto_info.bzl", "ProtoInfo") load("//bazel/private:java_proto_support.bzl", "java_compile_for_protos", "java_info_merge_for_protos") load("//bazel/private:toolchain_helpers.bzl", "toolchains") -# TODO: replace with toolchain type located in protobuf -_JAVA_PROTO_TOOLCHAIN = "@rules_java//java/proto:toolchain_type" +_JAVA_PROTO_TOOLCHAIN = "//bazel/private:java_toolchain_type" # The provider is used to collect source and runtime jars in the `proto_library` dependency graph. JavaProtoAspectInfo = provider("JavaProtoAspectInfo", fields = ["jars"]) diff --git a/bazel/private/java_lite_proto_library.bzl b/bazel/private/java_lite_proto_library.bzl index fb86354a8116..1a7ba1a5ad7b 100644 --- a/bazel/private/java_lite_proto_library.bzl +++ b/bazel/private/java_lite_proto_library.bzl @@ -16,8 +16,7 @@ load("//bazel/private:toolchain_helpers.bzl", "toolchains") _PROTO_TOOLCHAIN_ATTR = "_aspect_proto_toolchain_for_javalite" -# TODO: replace with toolchain type located in protobuf -_JAVA_LITE_PROTO_TOOLCHAIN = "@rules_java//java/proto:lite_toolchain_type" +_JAVA_LITE_PROTO_TOOLCHAIN = "//bazel/private:javalite_toolchain_type" def _aspect_impl(target, ctx): """Generates and compiles Java code for a proto_library dependency graph. diff --git a/bazel/private/proto_toolchain_rule.bzl b/bazel/private/proto_toolchain_rule.bzl index 3a4ee72a7c06..700bb146432d 100644 --- a/bazel/private/proto_toolchain_rule.bzl +++ b/bazel/private/proto_toolchain_rule.bzl @@ -7,6 +7,7 @@ # """A Starlark implementation of the proto_toolchain rule.""" +load("//bazel/common:proto_common.bzl", "proto_common") load("//bazel/common:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo") load("//bazel/private:toolchain_helpers.bzl", "toolchains") @@ -26,7 +27,7 @@ def _impl(ctx): protoc_opts = ctx.fragments.proto.experimental_protoc_opts, progress_message = ctx.attr.progress_message, mnemonic = ctx.attr.mnemonic, - toolchain_type = toolchains.PROTO_TOOLCHAIN, + **(dict(toolchain_type = toolchains.PROTO_TOOLCHAIN) if proto_common.INCOMPATIBLE_PASS_TOOLCHAIN_TYPE else {}) ), ), ] diff --git a/bazel/private/toolchains/BUILD.bazel b/bazel/private/toolchains/BUILD.bazel new file mode 100644 index 000000000000..1f30c35d65f4 --- /dev/null +++ b/bazel/private/toolchains/BUILD.bazel @@ -0,0 +1,48 @@ +load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain") +load("//bazel/toolchains:proto_toolchain.bzl", "proto_toolchain") + +# Keep this file as small as possible and free of any unnecessary loads +# It is loaded by every use of protobuf repository, and loads here can force +# fetching of additional external repositories + +# It's also intentionally using toolchain instead of proto_lang_toolchain, +# because the former does not resolve dependencies until toolchain resolution +# needs them + +proto_toolchain( + name = "protoc_sources", + exec_compatible_with = [], + proto_compiler = "//:protoc", +) + +toolchain( + name = "cc_source_toolchain", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//:cc_toolchain", + toolchain_type = "//bazel/private:cc_toolchain_type", +) + +toolchain( + name = "java_source_toolchain", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//java/core:toolchain", + toolchain_type = "//bazel/private:java_toolchain_type", +) + +toolchain( + name = "javalite_source_toolchain", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//java/lite:toolchain", + toolchain_type = "//bazel/private:javalite_toolchain_type", +) + +toolchain( + name = "python_source_toolchain", + exec_compatible_with = [], + target_compatible_with = [], + toolchain = "//python:python_toolchain", + toolchain_type = "//bazel/private:python_toolchain_type", +) diff --git a/protobuf_deps.bzl b/protobuf_deps.bzl index 22408a5d852e..2fbd28d85fdb 100644 --- a/protobuf_deps.bzl +++ b/protobuf_deps.bzl @@ -200,3 +200,4 @@ def protobuf_deps(): name = "nuget_python_x86-64_3.10.0", sha256 = "4474c83c25625d93e772e926f95f4cd398a0abbb52793625fa30f39af3d2cc00", ) + native.register_toolchains("//bazel/private/toolchains:all")