Skip to content

Commit 98ecdee

Browse files
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
1 parent 441d866 commit 98ecdee

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ load("//tensorflow:workspace.bzl", "tf_repositories")
1818
# Please add all new TensorFlow dependencies in workspace.bzl.
1919
tf_repositories()
2020

21+
register_toolchains("@local_config_python//:py_toolchain")
22+
2123
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_repositories")
2224

2325
closure_repositories()

third_party/py/BUILD.tpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ licenses(["restricted"])
22

33
package(default_visibility = ["//visibility:public"])
44

5+
# Point both runtimes to the same python binary to ensure we always
6+
# use the python binary specified by ./configure.py script.
7+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
8+
9+
py_runtime(
10+
name = "py2_runtime",
11+
interpreter_path = "%{PYTHON_BIN_PATH}",
12+
python_version = "PY2",
13+
)
14+
15+
py_runtime(
16+
name = "py3_runtime",
17+
interpreter_path = "%{PYTHON_BIN_PATH}",
18+
python_version = "PY3",
19+
)
20+
21+
py_runtime_pair(
22+
name = "py_runtime_pair",
23+
py2_runtime = ":py2_runtime",
24+
py3_runtime = ":py3_runtime",
25+
)
26+
27+
toolchain(
28+
name = "py_toolchain",
29+
toolchain = ":py_runtime_pair",
30+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
)
32+
533
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
634
# See https://docs.python.org/3/extending/windows.html
735
cc_import(

third_party/py/python_configure.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def _create_local_python_repository(repository_ctx):
323323
"numpy_include",
324324
)
325325
_tpl(repository_ctx, "BUILD", {
326+
"%{PYTHON_BIN_PATH}": python_bin,
326327
"%{PYTHON_INCLUDE_GENRULE}": python_include_rule,
327328
"%{PYTHON_IMPORT_LIB_GENRULE}": python_import_lib_genrule,
328329
"%{NUMPY_INCLUDE_GENRULE}": numpy_include_rule,

third_party/toolchains/preconfig/ubuntu16.04/py/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ licenses(["restricted"])
22

33
package(default_visibility = ["//visibility:public"])
44

5+
# Point both runtimes to the same python binary to ensure we always
6+
# use the python binary specified by ./configure.py script.
7+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
8+
9+
py_runtime(
10+
name = "py2_runtime",
11+
interpreter_path = "/usr/bin/python2",
12+
python_version = "PY2",
13+
)
14+
15+
py_runtime(
16+
name = "py3_runtime",
17+
interpreter_path = "/usr/bin/python2",
18+
python_version = "PY3",
19+
)
20+
21+
py_runtime_pair(
22+
name = "py_runtime_pair",
23+
py2_runtime = ":py2_runtime",
24+
py3_runtime = ":py3_runtime",
25+
)
26+
27+
toolchain(
28+
name = "py_toolchain",
29+
toolchain = ":py_runtime_pair",
30+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
)
32+
533
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
634
# See https://docs.python.org/3/extending/windows.html
735
cc_import(

third_party/toolchains/preconfig/ubuntu16.04/py3/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ licenses(["restricted"])
22

33
package(default_visibility = ["//visibility:public"])
44

5+
# Point both runtimes to the same python binary to ensure we always
6+
# use the python binary specified by ./configure.py script.
7+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
8+
9+
py_runtime(
10+
name = "py2_runtime",
11+
interpreter_path = "/usr/bin/python3",
12+
python_version = "PY2",
13+
)
14+
15+
py_runtime(
16+
name = "py3_runtime",
17+
interpreter_path = "/usr/bin/python3",
18+
python_version = "PY3",
19+
)
20+
21+
py_runtime_pair(
22+
name = "py_runtime_pair",
23+
py2_runtime = ":py2_runtime",
24+
py3_runtime = ":py3_runtime",
25+
)
26+
27+
toolchain(
28+
name = "py_toolchain",
29+
toolchain = ":py_runtime_pair",
30+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
)
32+
533
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
634
# See https://docs.python.org/3/extending/windows.html
735
cc_import(

third_party/toolchains/preconfig/win_1803/py36/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ licenses(["restricted"])
22

33
package(default_visibility = ["//visibility:public"])
44

5+
# Point both runtimes to the same python binary to ensure we always
6+
# use the python binary specified by ./configure.py script.
7+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
8+
9+
py_runtime(
10+
name = "py2_runtime",
11+
interpreter_path = "C:/Python36/python.exe",
12+
python_version = "PY2",
13+
)
14+
15+
py_runtime(
16+
name = "py3_runtime",
17+
interpreter_path = "C:/Python36/python.exe",
18+
python_version = "PY3",
19+
)
20+
21+
py_runtime_pair(
22+
name = "py_runtime_pair",
23+
py2_runtime = ":py2_runtime",
24+
py3_runtime = ":py3_runtime",
25+
)
26+
27+
toolchain(
28+
name = "py_toolchain",
29+
toolchain = ":py_runtime_pair",
30+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
)
32+
533
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
634
# See https://docs.python.org/3/extending/windows.html
735
cc_import(

third_party/toolchains/preconfig/win_1803/py37/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ licenses(["restricted"])
22

33
package(default_visibility = ["//visibility:public"])
44

5+
# Point both runtimes to the same python binary to ensure we always
6+
# use the python binary specified by ./configure.py script.
7+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
8+
9+
py_runtime(
10+
name = "py2_runtime",
11+
interpreter_path = "C:/Python37/python.exe",
12+
python_version = "PY2",
13+
)
14+
15+
py_runtime(
16+
name = "py3_runtime",
17+
interpreter_path = "C:/Python37/python.exe",
18+
python_version = "PY3",
19+
)
20+
21+
py_runtime_pair(
22+
name = "py_runtime_pair",
23+
py2_runtime = ":py2_runtime",
24+
py3_runtime = ":py3_runtime",
25+
)
26+
27+
toolchain(
28+
name = "py_toolchain",
29+
toolchain = ":py_runtime_pair",
30+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
31+
)
32+
533
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
634
# See https://docs.python.org/3/extending/windows.html
735
cc_import(

0 commit comments

Comments
 (0)