Skip to content

Conversation

@alexeagle
Copy link
Collaborator

@alexeagle alexeagle commented Oct 22, 2025

Upstreamed from my https://github.com/aspect-build/toolchains_protoc which provided this feature outside the protobuf repo.

Adds:

  • private API: repository rule prebuilt_protoc_repo to fetch a protoc binary from GitHub releases
  • public API: repository rule prebuilt_toolchains_repo that creates the "hub" letting Bazel's toolchain resolver download only for the execution platform
  • public API: module_extension + tag protoc.prebuilt_toolchain to wrap up the above for bzlmod users
  • WORKSPACE users can live with the inconvenience of having to create toolchains themselve
  • example of proto_library rule with intentionally non-functional cc toolchain, exercising the feature

This needs work with @thesayyn to:

Work towards #19558

Users can manually register this in their MODULE.bazel so that registration overrides the default, which is still building protoc from source.
@alexeagle alexeagle requested a review from a team as a code owner October 22, 2025 01:58
@alexeagle alexeagle requested review from Logofile and removed request for a team October 22, 2025 01:58
@Logofile Logofile requested review from honglooker and removed request for Logofile October 22, 2025 12:25

protoc = module_extension(
# TODO: replace version number here during release, maybe with git archive .gitattributes config
implementation = lambda module_ctx: create_all_toolchain_repos("prebuilt_protoc", "v33.0")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to know the version if there is only one set of hashes provided anyway?

If you do want them, you could get the version from module_ctx.modules (search for the protobuf module and get its version field).

version = version,
)

protoc = module_extension(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use skylibs modules.as_extension so that this is marked reproducible and you get bazel mod tidy fixes for use_repo while developing protobuf.

Comment on lines +1 to +20
"""Create lazy definitions to reference the pre-built protoc toolchains.
Ensures that Bazel only downloads required binaries for selected toolchains.
This follows guidance here:
https://docs.bazel.build/versions/main/skylark/deploying.html#registering-toolchains
"
Note that in order to resolve toolchains in the analysis phase
Bazel needs to analyze all toolchain targets that are registered.
Bazel will not need to analyze all targets referenced by toolchain.toolchain attribute.
If in order to register toolchains you need to perform complex computation in the repository,
consider splitting the repository with toolchain targets
from the repository with <LANG>_toolchain targets.
Former will be always fetched,
and the latter will only be fetched when user actually needs to build <LANG> code.
"
The "complex computation" in our case is simply downloading our pre-built protoc binaries.
This guidance tells us how to avoid that: we put the toolchain targets in the alias repository
with only the toolchain attribute pointing into the platform-specific repositories.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be dropped in favor of the two line comment on the toolchains attribute (but it would be worth adding the link to that comment)

@honglooker honglooker removed their request for review October 22, 2025 15:49
@alexeagle alexeagle requested a review from thesayyn October 22, 2025 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants