This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic custom python 3 toolchain setup with bazel
- Loading branch information
Showing
9 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
3.3.1 |
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,7 @@ | ||
workspace(name = "custom_python_toolchain") | ||
|
||
load("//tools/python:toolchain.bzl", "register_python_toolchain") | ||
register_python_toolchain() | ||
|
||
load("//tools/python:python.bzl", "rules_python") | ||
rules_python() |
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,8 @@ | ||
load("//tools/python:py3_macros.bzl", "py3_test") | ||
|
||
py3_test( | ||
name = "toolchain_test", | ||
srcs = [ | ||
"toolchain_test.py", | ||
], | ||
) |
Empty file.
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,18 @@ | ||
import unittest | ||
import sys | ||
|
||
|
||
class PythonToolchainRuntimeTest(unittest.TestCase): | ||
|
||
def test_toolchain_runtime_interpreter(self): | ||
interpreter_path = python3_path() | ||
print(f"Interpreter path: {interpreter_path}") | ||
self.assertTrue(interpreter_path.__contains__("execroot/custom_python_toolchain")) | ||
|
||
|
||
def python3_path(): | ||
return sys.executable | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,21 @@ | ||
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") | ||
|
||
py_runtime( | ||
name = "python3_runtime", | ||
files = ["@python_interpreter//:files"], | ||
interpreter = "@python_interpreter//:python_bin", | ||
python_version = "PY3", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
py_runtime_pair( | ||
name = "sourced_py_runtime_pair", | ||
py2_runtime = None, | ||
py3_runtime = ":python3_runtime", | ||
) | ||
|
||
toolchain( | ||
name = "py_toolchain", | ||
toolchain = ":sourced_py_runtime_pair", | ||
toolchain_type = "@bazel_tools//tools/python:toolchain_type", | ||
) |
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,30 @@ | ||
load("@rules_python//python:python.bzl", "py_binary", "py_library", "py_test") | ||
|
||
def py3_library(*args, **kwargs): | ||
py_library( | ||
srcs_version = "PY3", | ||
*args, | ||
**kwargs | ||
) | ||
|
||
def py3_binary(name, main = None, *args, **kwargs): | ||
if main == None: | ||
main = "%s.py" % (name) | ||
|
||
py_binary( | ||
name = name, | ||
main = main, | ||
legacy_create_init = False, | ||
python_version = "PY3", | ||
*args, | ||
**kwargs | ||
) | ||
|
||
def py3_test(*args, **kwargs): | ||
py_test( | ||
legacy_create_init = False, | ||
python_version = "PY3", | ||
srcs_version = "PY3", | ||
*args, | ||
**kwargs | ||
) |
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,13 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
rules_python_version = "dd7f9c5f01bafbfea08c44092b6b0c8fc8fcb77f" | ||
rules_python_sha256 = "0aa9ec790a58053e3ab5af397879b267a625955f8297c239b2d8559c6773397b" | ||
|
||
|
||
def rules_python(): | ||
http_archive( | ||
name = "rules_python", | ||
sha256 = rules_python_sha256, | ||
strip_prefix= "rules_python-%s" % rules_python_version, | ||
urls = ["https://github.com/bazelbuild/rules_python/archive/%s.tar.gz" % rules_python_version], | ||
) |
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,46 @@ | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
_interpreter_version = "3.8.5" | ||
_interpreter_package_sha256 = "e3003ed57db17e617acb382b0cade29a248c6026b1bd8aad1f976e9af66a83b0" | ||
|
||
|
||
def register_python_toolchain(): | ||
# NOTE: macOS's the default openssl binaries are not sufficient to build the SSL module. It is possible to change | ||
# the patch command to configure the build to use a custom SSL library by specifying '--with-openssl=<SSL lib path>' | ||
# to the configure script: | ||
# | ||
# "./configure --prefix=$(pwd)/python_home --with-openssl=/path/to/openssl/1.1", | ||
# | ||
# | ||
# Here is the message yield by the build on macOS: | ||
# | ||
# Could not build the ssl module! | ||
# Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host(). | ||
# LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381 | ||
# | ||
http_archive( | ||
name = "python_interpreter", | ||
urls = [ | ||
"https://www.python.org/ftp/python/%s/Python-%s.tar.xz"%(_interpreter_version, _interpreter_version) | ||
], | ||
sha256 = _interpreter_package_sha256, | ||
strip_prefix = "Python-%s"%_interpreter_version, | ||
patch_cmds = [ | ||
"mkdir $(pwd)/python_home", | ||
"./configure --prefix=$(pwd)/python_home", | ||
"make", | ||
"make install", | ||
"ln -s python_home/bin/python3 python_bin", | ||
], | ||
build_file_content = """ | ||
exports_files(["python_bin"]) | ||
filegroup( | ||
name = "files", | ||
srcs = glob(["python_home/**"], exclude = ["**/* *"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
""", | ||
) | ||
|
||
native.register_toolchains("//tools/python:py_toolchain") |