-
Notifications
You must be signed in to change notification settings - Fork 541
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
refactor(toolchains): split the implementation of toolchain rules to separate files #2232
Merged
rickeylev
merged 5 commits into
bazelbuild:main
from
aignas:refactor/pythons_hub_config_settings
Sep 19, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f5cb171
refactor: split out the py_repositories call to a separate file
aignas bd5a35c
refactor: split out the python_repository rule to a separate file
aignas 496332b
refactor: split out the standalone interpreter utility function
aignas d2c43cd
refactor: split out the python_register_toolchains function
aignas 15ca0ea
refactor: rename the remaining file
aignas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This file contains repository rules and macros to support toolchain registration. | ||
""" | ||
|
||
load(":repo_utils.bzl", "repo_utils") | ||
|
||
STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER" | ||
|
||
def is_standalone_interpreter(rctx, python_interpreter_path, *, logger = None): | ||
"""Query a python interpreter target for whether or not it's a rules_rust provided toolchain | ||
|
||
Args: | ||
rctx: {type}`repository_ctx` The repository rule's context object. | ||
python_interpreter_path: {type}`path` A path representing the interpreter. | ||
logger: Optional logger to use for operations. | ||
|
||
Returns: | ||
{type}`bool` Whether or not the target is from a rules_python generated toolchain. | ||
""" | ||
|
||
# Only update the location when using a hermetic toolchain. | ||
if not python_interpreter_path: | ||
return False | ||
|
||
# This is a rules_python provided toolchain. | ||
return repo_utils.execute_unchecked( | ||
rctx, | ||
op = "IsStandaloneInterpreter", | ||
arguments = [ | ||
"ls", | ||
"{}/{}".format( | ||
python_interpreter_path.dirname, | ||
STANDALONE_INTERPRETER_FILENAME, | ||
), | ||
], | ||
logger = logger, | ||
).return_code == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This file contains macros to be called during WORKSPACE evaluation.""" | ||
|
||
load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive") | ||
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") | ||
load("//python/private/pypi:deps.bzl", "pypi_deps") | ||
load(":internal_config_repo.bzl", "internal_config_repo") | ||
|
||
def http_archive(**kwargs): | ||
maybe(_http_archive, **kwargs) | ||
|
||
def py_repositories(): | ||
"""Runtime dependencies that users must install. | ||
|
||
This function should be loaded and called in the user's WORKSPACE. | ||
With bzlmod enabled, this function is not needed since MODULE.bazel handles transitive deps. | ||
""" | ||
maybe( | ||
internal_config_repo, | ||
name = "rules_python_internal", | ||
) | ||
http_archive( | ||
name = "bazel_skylib", | ||
sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", | ||
urls = [ | ||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", | ||
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", | ||
], | ||
) | ||
http_archive( | ||
name = "rules_cc", | ||
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"], | ||
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf", | ||
strip_prefix = "rules_cc-0.0.9", | ||
) | ||
pypi_deps() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""This file contains repository rules and macros to support toolchain registration. | ||
""" | ||
|
||
load("//python:versions.bzl", "MINOR_MAPPING") | ||
load(":python_register_toolchains.bzl", "python_register_toolchains") | ||
load(":toolchains_repo.bzl", "multi_toolchain_aliases") | ||
|
||
def python_register_multi_toolchains( | ||
name, | ||
python_versions, | ||
default_version = None, | ||
minor_mapping = None, | ||
**kwargs): | ||
"""Convenience macro for registering multiple Python toolchains. | ||
|
||
Args: | ||
name: {type}`str` base name for each name in {obj}`python_register_toolchains` call. | ||
python_versions: {type}`list[str]` the Python versions. | ||
default_version: {type}`str` the default Python version. If not set, | ||
the first version in python_versions is used. | ||
minor_mapping: {type}`dict[str, str]` mapping between `X.Y` to `X.Y.Z` | ||
format. Defaults to the value in `//python:versions.bzl`. | ||
**kwargs: passed to each {obj}`python_register_toolchains` call. | ||
""" | ||
if len(python_versions) == 0: | ||
fail("python_versions must not be empty") | ||
|
||
minor_mapping = minor_mapping or MINOR_MAPPING | ||
|
||
if not default_version: | ||
default_version = python_versions.pop(0) | ||
for python_version in python_versions: | ||
if python_version == default_version: | ||
# We register the default version lastly so that it's not picked first when --platforms | ||
# is set with a constraint during toolchain resolution. This is due to the fact that | ||
# Bazel will match the unconstrained toolchain if we register it before the constrained | ||
# ones. | ||
continue | ||
python_register_toolchains( | ||
name = name + "_" + python_version.replace(".", "_"), | ||
python_version = python_version, | ||
set_python_version_constraint = True, | ||
minor_mapping = minor_mapping, | ||
**kwargs | ||
) | ||
python_register_toolchains( | ||
name = name + "_" + default_version.replace(".", "_"), | ||
python_version = default_version, | ||
set_python_version_constraint = False, | ||
minor_mapping = minor_mapping, | ||
**kwargs | ||
) | ||
|
||
multi_toolchain_aliases( | ||
name = name, | ||
python_versions = { | ||
python_version: name + "_" + python_version.replace(".", "_") | ||
for python_version in (python_versions + [default_version]) | ||
}, | ||
minor_mapping = minor_mapping, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you happen to know the history of this function? It's always struck me as a bit strange.