Skip to content

Commit

Permalink
Support --incompatible_enable_proto_toolchain_resolution
Browse files Browse the repository at this point in the history
Move toolchain types into protobuf repo
Add default registrations for them (building from sources)

PiperOrigin-RevId: 674801639
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 15, 2024
1 parent 48ff232 commit 372ddb3
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 9 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/test_bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
3 changes: 3 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion bazel/common/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
),
)
16 changes: 16 additions & 0 deletions bazel/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion bazel/private/bazel_cc_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
3 changes: 1 addition & 2 deletions bazel/private/bazel_java_proto_library_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
3 changes: 1 addition & 2 deletions bazel/private/java_lite_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion bazel/private/proto_toolchain_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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 {})
),
),
]
Expand Down
48 changes: 48 additions & 0 deletions bazel/private/toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
1 change: 1 addition & 0 deletions protobuf_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,4 @@ def protobuf_deps():
name = "nuget_python_x86-64_3.10.0",
sha256 = "4474c83c25625d93e772e926f95f4cd398a0abbb52793625fa30f39af3d2cc00",
)
native.register_toolchains("//bazel/private/toolchains:all")

0 comments on commit 372ddb3

Please sign in to comment.