-
-
Notifications
You must be signed in to change notification settings - Fork 636
feat: cpython toolchains #618
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
Merged
Merged
Changes from 38 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
e86473f
feat: cpython toolchains for linux and macos
f0rmiga eabb3e2
feat: compile zstd if missing
f0rmiga 371573d
fix: buildifier
f0rmiga 768f2d4
fix: make python_repositories reproducible
f0rmiga a0717d9
rename: python_repositories -> python_repository
f0rmiga 44e6c2d
fix: linter
f0rmiga 72b1ff1
feat: make interpreter files publicly visible
f0rmiga 327275f
fix: add files to py_runtime
f0rmiga 0e91655
Account for some platforms not having all versions
alexeagle 4a8b718
Added windows support to hermetic toolchains (#628)
UebelAndre d7ac042
refactor: simplify logic for release urls
f0rmiga 346c78c
feat: Provide a host platform alias (#635)
alexeagle fbc7934
fix: files excludes
f0rmiga 936821f
fix: macOS dislikes --recursive
f0rmiga 01df1f1
fix: buildifier issues
f0rmiga e5664e2
Allow previous indygreg releases (#636)
alexeagle 66c8e5c
fix: put back zstd support for older releases
f0rmiga a2b0eeb
fix: hash calculator
f0rmiga d121436
feat: use hermetic interpreter with pip_parse and pip_install
f0rmiga d61f2f7
fix: add missing attrs back for zstd
f0rmiga 77383e3
fix: expose zstd attributes
f0rmiga 3e349ea
fix: normalize OS names
f0rmiga fb22d57
fix: linting issues
f0rmiga cf7ce1d
fix: support windows in the aliases
f0rmiga d7cec7f
fix: linting issues
f0rmiga f42657f
fix: windows python.exe instead of python3.exe
f0rmiga e550a77
fix: use consts for OS names
f0rmiga 4a5a50b
feat: always use latest toolchain for test
f0rmiga 4373058
fix: expose versions.bzl
f0rmiga 4c7fce1
refactor: move toolchain tests out of private
f0rmiga 97b909a
feat: acceptance tests for the toolchains
f0rmiga 3d7e6a6
fix: rewrite test in py to work on windows
f0rmiga 00ca705
fix: README example
f0rmiga 651ce7f
fix: use toolchain to run acceptance tests
f0rmiga 9549c4a
feat: use matrix for acceptance tests
f0rmiga 2258d33
fix: support acceptance_tests on windows
f0rmiga fd9f983
feat: alias for pip
f0rmiga 102439b
fix?: include call to windows cmd
f0rmiga 8a6bcdd
Fix windows acceptance tests (#641)
UebelAndre 93be625
refactor: polishing Windows testing support
f0rmiga f509b46
fix: unset py2_runtime
f0rmiga 7eacdf1
rename: host -> resolved_interpreter
f0rmiga 9200d66
doc: add reference to quirks in python-build-standalone
f0rmiga 3d2c89c
Merge remote-tracking branch 'origin/main' into f0rmiga/cpython-toolc…
f0rmiga 8c85ec5
feat: allow a distutils.cfg to be passed
f0rmiga 13ab11e
fix: buildifier (again)
f0rmiga ceb2c35
Minor code review suggestions (#642)
alexeagle acd6666
fix: depset concat
f0rmiga 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,158 @@ | ||
# Copyright 2022 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. | ||
|
||
"""Create a repository to hold the toolchains. | ||
|
||
This follows guidance here: | ||
https://docs.bazel.build/versions/main/skylark/deploying.html#registering-toolchains | ||
|
||
The "complex computation" in our case is simply downloading large artifacts. | ||
f0rmiga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
""" | ||
|
||
load( | ||
"//python:versions.bzl", | ||
"LINUX_NAME", | ||
"MACOS_NAME", | ||
"PLATFORMS", | ||
"WINDOWS_NAME", | ||
) | ||
|
||
def _toolchains_repo_impl(rctx): | ||
build_content = """\ | ||
# Generated by toolchains_repo.bzl | ||
# | ||
# These can be registered in the workspace file or passed to --extra_toolchains | ||
# flag. By default all these toolchains are registered by the | ||
# python_register_toolchains macro so you don't normally need to interact with | ||
# these targets. | ||
|
||
""" | ||
|
||
for [platform, meta] in PLATFORMS.items(): | ||
build_content += """\ | ||
toolchain( | ||
name = "{platform}_toolchain", | ||
exec_compatible_with = {compatible_with}, | ||
target_compatible_with = {compatible_with}, | ||
f0rmiga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
toolchain = "@{user_repository_name}_{platform}//:python_runtimes", | ||
toolchain_type = "@bazel_tools//tools/python:toolchain_type", | ||
) | ||
""".format( | ||
platform = platform, | ||
name = rctx.attr.name, | ||
user_repository_name = rctx.attr.user_repository_name, | ||
compatible_with = meta.compatible_with, | ||
) | ||
|
||
rctx.file("BUILD.bazel", build_content) | ||
|
||
toolchains_repo = repository_rule( | ||
_toolchains_repo_impl, | ||
doc = "Creates a repository with toolchain definitions for all known platforms " + | ||
"which can be registered or selected.", | ||
attrs = { | ||
"user_repository_name": attr.string(doc = "what the user chose for the base name"), | ||
}, | ||
) | ||
|
||
def _host_os_alias_impl(rctx): | ||
(os_name, arch) = _host_os_arch(rctx) | ||
|
||
host_platform = None | ||
for platform, meta in PLATFORMS.items(): | ||
if meta.os_name == os_name and meta.arch == arch: | ||
host_platform = platform | ||
if not host_platform: | ||
fail("No platform declared for host OS {} on arch {}".format(os_name, arch)) | ||
|
||
is_windows = (os_name == WINDOWS_NAME) | ||
python3_binary_path = "python.exe" if is_windows else "bin/python3" | ||
|
||
# Base BUILD file for this repository. | ||
build_contents = """\ | ||
# Generated by python/repositories.bzl | ||
package(default_visibility = ["//visibility:public"]) | ||
alias(name = "files", actual = "@{py_repository}_{host_platform}//:files") | ||
alias(name = "py3_runtime", actual = "@{py_repository}_{host_platform}//:py3_runtime") | ||
alias(name = "python_runtimes", actual = "@{py_repository}_{host_platform}//:python_runtimes") | ||
alias(name = "python3", actual = "@{py_repository}_{host_platform}//:{python3_binary_path}") | ||
f0rmiga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""".format( | ||
py_repository = rctx.attr.user_repository_name, | ||
host_platform = host_platform, | ||
python3_binary_path = python3_binary_path, | ||
) | ||
if not is_windows: | ||
build_contents += """\ | ||
alias(name = "pip", actual = "@{py_repository}_{host_platform}//:bin/pip") | ||
""".format( | ||
py_repository = rctx.attr.user_repository_name, | ||
host_platform = host_platform, | ||
) | ||
rctx.file("BUILD.bazel", build_contents) | ||
|
||
# Expose a Starlark file so rules can know what host platform we used and where to find an interpreter | ||
# when using repository_ctx.path, which doesn't understand aliases. | ||
rctx.file("defs.bzl", content = """\ | ||
# Generated by python/repositories.bzl | ||
host_platform = "{host_platform}" | ||
interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}" | ||
""".format( | ||
py_repository = rctx.attr.user_repository_name, | ||
host_platform = host_platform, | ||
python3_binary_path = python3_binary_path, | ||
)) | ||
|
||
host_os_alias = repository_rule( | ||
_host_os_alias_impl, | ||
doc = """Creates a repository with a shorter name meant for the host platform, which contains | ||
a BUILD.bazel file declaring aliases to the host platform's targets. | ||
""", | ||
attrs = { | ||
"user_repository_name": attr.string( | ||
mandatory = True, | ||
doc = "The base name for all created repositories, like 'python38'.", | ||
), | ||
}, | ||
) | ||
|
||
def _host_os_arch(rctx): | ||
"""Infer the host OS name and arch from a repository context. | ||
|
||
Args: | ||
rctx: Bazel's repository_ctx. | ||
Returns: | ||
A tuple with the host OS name and arch. | ||
""" | ||
os_name = rctx.os.name | ||
|
||
# We assume the arch for Windows is always x86_64. | ||
if "windows" in os_name.lower(): | ||
arch = "x86_64" | ||
|
||
# Normalize the os_name. E.g. os_name could be "OS windows server 2019". | ||
os_name = WINDOWS_NAME | ||
else: | ||
# This is not ideal, but bazel doesn't directly expose arch. | ||
arch = rctx.execute(["uname", "-m"]).stdout.strip() | ||
|
||
# Normalize the os_name. | ||
if "mac" in os_name.lower(): | ||
os_name = MACOS_NAME | ||
elif "linux" in os_name.lower(): | ||
os_name = LINUX_NAME | ||
|
||
return (os_name, arch) |
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.
Uh oh!
There was an error while loading. Please reload this page.