From 98ecdee8e651d850117fdc57286a37d05e734d97 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Sat, 5 Oct 2019 01:15:18 -0700 Subject: [PATCH] Add python toolchains definition in python configuration --python_path will be removed in future Bazel, we should switch to use python toolchain. But currently we want Bazel to always use the same python binary specified in configure.py regardless of what's specified in py_binary rule (PY2 or PY3). So we point both py2 and py3 runtime to the same PYTHON_BIN_PATH. PiperOrigin-RevId: 273032026 --- WORKSPACE | 2 ++ third_party/py/BUILD.tpl | 28 +++++++++++++++++++ third_party/py/python_configure.bzl | 1 + .../toolchains/preconfig/ubuntu16.04/py/BUILD | 28 +++++++++++++++++++ .../preconfig/ubuntu16.04/py3/BUILD | 28 +++++++++++++++++++ .../toolchains/preconfig/win_1803/py36/BUILD | 28 +++++++++++++++++++ .../toolchains/preconfig/win_1803/py37/BUILD | 28 +++++++++++++++++++ 7 files changed, 143 insertions(+) diff --git a/WORKSPACE b/WORKSPACE index 622fa4d1412..babb14b509e 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -18,6 +18,8 @@ load("//tensorflow:workspace.bzl", "tf_repositories") # Please add all new TensorFlow dependencies in workspace.bzl. tf_repositories() +register_toolchains("@local_config_python//:py_toolchain") + load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories") closure_repositories() diff --git a/third_party/py/BUILD.tpl b/third_party/py/BUILD.tpl index 1dd8ab433a3..3a0be1b0173 100644 --- a/third_party/py/BUILD.tpl +++ b/third_party/py/BUILD.tpl @@ -2,6 +2,34 @@ licenses(["restricted"]) package(default_visibility = ["//visibility:public"]) +# Point both runtimes to the same python binary to ensure we always +# use the python binary specified by ./configure.py script. +load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") + +py_runtime( + name = "py2_runtime", + interpreter_path = "%{PYTHON_BIN_PATH}", + python_version = "PY2", +) + +py_runtime( + name = "py3_runtime", + interpreter_path = "%{PYTHON_BIN_PATH}", + python_version = "PY3", +) + +py_runtime_pair( + name = "py_runtime_pair", + py2_runtime = ":py2_runtime", + py3_runtime = ":py3_runtime", +) + +toolchain( + name = "py_toolchain", + toolchain = ":py_runtime_pair", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) + # To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib # See https://docs.python.org/3/extending/windows.html cc_import( diff --git a/third_party/py/python_configure.bzl b/third_party/py/python_configure.bzl index d1b1e594921..e07aaf687a2 100644 --- a/third_party/py/python_configure.bzl +++ b/third_party/py/python_configure.bzl @@ -323,6 +323,7 @@ def _create_local_python_repository(repository_ctx): "numpy_include", ) _tpl(repository_ctx, "BUILD", { + "%{PYTHON_BIN_PATH}": python_bin, "%{PYTHON_INCLUDE_GENRULE}": python_include_rule, "%{PYTHON_IMPORT_LIB_GENRULE}": python_import_lib_genrule, "%{NUMPY_INCLUDE_GENRULE}": numpy_include_rule, diff --git a/third_party/toolchains/preconfig/ubuntu16.04/py/BUILD b/third_party/toolchains/preconfig/ubuntu16.04/py/BUILD index 3cd5fdd6f21..74e6964e66e 100755 --- a/third_party/toolchains/preconfig/ubuntu16.04/py/BUILD +++ b/third_party/toolchains/preconfig/ubuntu16.04/py/BUILD @@ -2,6 +2,34 @@ licenses(["restricted"]) package(default_visibility = ["//visibility:public"]) +# Point both runtimes to the same python binary to ensure we always +# use the python binary specified by ./configure.py script. +load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") + +py_runtime( + name = "py2_runtime", + interpreter_path = "/usr/bin/python2", + python_version = "PY2", +) + +py_runtime( + name = "py3_runtime", + interpreter_path = "/usr/bin/python2", + python_version = "PY3", +) + +py_runtime_pair( + name = "py_runtime_pair", + py2_runtime = ":py2_runtime", + py3_runtime = ":py3_runtime", +) + +toolchain( + name = "py_toolchain", + toolchain = ":py_runtime_pair", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) + # To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib # See https://docs.python.org/3/extending/windows.html cc_import( diff --git a/third_party/toolchains/preconfig/ubuntu16.04/py3/BUILD b/third_party/toolchains/preconfig/ubuntu16.04/py3/BUILD index a56e284d904..81bd7358254 100755 --- a/third_party/toolchains/preconfig/ubuntu16.04/py3/BUILD +++ b/third_party/toolchains/preconfig/ubuntu16.04/py3/BUILD @@ -2,6 +2,34 @@ licenses(["restricted"]) package(default_visibility = ["//visibility:public"]) +# Point both runtimes to the same python binary to ensure we always +# use the python binary specified by ./configure.py script. +load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") + +py_runtime( + name = "py2_runtime", + interpreter_path = "/usr/bin/python3", + python_version = "PY2", +) + +py_runtime( + name = "py3_runtime", + interpreter_path = "/usr/bin/python3", + python_version = "PY3", +) + +py_runtime_pair( + name = "py_runtime_pair", + py2_runtime = ":py2_runtime", + py3_runtime = ":py3_runtime", +) + +toolchain( + name = "py_toolchain", + toolchain = ":py_runtime_pair", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) + # To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib # See https://docs.python.org/3/extending/windows.html cc_import( diff --git a/third_party/toolchains/preconfig/win_1803/py36/BUILD b/third_party/toolchains/preconfig/win_1803/py36/BUILD index 7b2e84bb38e..1f879b4bfe8 100644 --- a/third_party/toolchains/preconfig/win_1803/py36/BUILD +++ b/third_party/toolchains/preconfig/win_1803/py36/BUILD @@ -2,6 +2,34 @@ licenses(["restricted"]) package(default_visibility = ["//visibility:public"]) +# Point both runtimes to the same python binary to ensure we always +# use the python binary specified by ./configure.py script. +load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") + +py_runtime( + name = "py2_runtime", + interpreter_path = "C:/Python36/python.exe", + python_version = "PY2", +) + +py_runtime( + name = "py3_runtime", + interpreter_path = "C:/Python36/python.exe", + python_version = "PY3", +) + +py_runtime_pair( + name = "py_runtime_pair", + py2_runtime = ":py2_runtime", + py3_runtime = ":py3_runtime", +) + +toolchain( + name = "py_toolchain", + toolchain = ":py_runtime_pair", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) + # To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib # See https://docs.python.org/3/extending/windows.html cc_import( diff --git a/third_party/toolchains/preconfig/win_1803/py37/BUILD b/third_party/toolchains/preconfig/win_1803/py37/BUILD index a120ff11e10..58ee164a7c1 100644 --- a/third_party/toolchains/preconfig/win_1803/py37/BUILD +++ b/third_party/toolchains/preconfig/win_1803/py37/BUILD @@ -2,6 +2,34 @@ licenses(["restricted"]) package(default_visibility = ["//visibility:public"]) +# Point both runtimes to the same python binary to ensure we always +# use the python binary specified by ./configure.py script. +load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") + +py_runtime( + name = "py2_runtime", + interpreter_path = "C:/Python37/python.exe", + python_version = "PY2", +) + +py_runtime( + name = "py3_runtime", + interpreter_path = "C:/Python37/python.exe", + python_version = "PY3", +) + +py_runtime_pair( + name = "py_runtime_pair", + py2_runtime = ":py2_runtime", + py3_runtime = ":py3_runtime", +) + +toolchain( + name = "py_toolchain", + toolchain = ":py_runtime_pair", + toolchain_type = "@bazel_tools//tools/python:toolchain_type", +) + # To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib # See https://docs.python.org/3/extending/windows.html cc_import(