Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
basic custom python 3 toolchain setup with bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
sha1n committed Aug 12, 2020
1 parent 41fe547 commit 956fd08
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.1
7 changes: 7 additions & 0 deletions WORKSPACE
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()
8 changes: 8 additions & 0 deletions tests/BUILD.bazel
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 added tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions tests/toolchain_test.py
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()
21 changes: 21 additions & 0 deletions tools/python/BUILD.bazel
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",
)
30 changes: 30 additions & 0 deletions tools/python/py3_macros.bzl
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
)
13 changes: 13 additions & 0 deletions tools/python/python.bzl
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],
)
46 changes: 46 additions & 0 deletions tools/python/toolchain.bzl
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")

0 comments on commit 956fd08

Please sign in to comment.