Skip to content

Commit 4fb520b

Browse files
committed
tweak bazel to use python3
1 parent e06b7f7 commit 4fb520b

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

WORKSPACE

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ git_repository(
1717
shallow_since = "1535728917 -0400",
1818
)
1919

20-
load("//tools/build_defs:python_repo.bzl", "python_interpreter")
20+
# This allows using py_test and py_library against python3.
21+
register_toolchains("//platform_defs:default_python3_toolchain")
22+
23+
# This allows building C++ against python3 headers.
24+
load("//tools/build_defs:python_repo.bzl", "python_headers")
25+
python_headers(name = "default_python3_headers")
2126

22-
python_interpreter(name = "default_python")

platform_defs/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
2+
3+
py_runtime(
4+
name = "python3",
5+
interpreter_path = "/usr/bin/python3",
6+
stub_shebang = "#!/usr/bin/env python3",
7+
python_version = "PY3",
8+
)
9+
10+
py_runtime_pair(
11+
name = "just_python3",
12+
py2_runtime = None,
13+
py3_runtime = ":python3",
14+
)
15+
16+
toolchain(
17+
name = "default_python3_toolchain",
18+
toolchain = ":just_python3",
19+
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
20+
)
21+

python/BUILD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ cc_binary(
44
linkshared = 1,
55
deps = [
66
"//core:libjsonnet",
7-
"@default_python//:headers",
7+
"@default_python3_headers//:headers",
88
],
99
)
1010

1111
py_library(
1212
name = "_jsonnet",
1313
data = [":_jsonnet.so"],
1414
imports = ["."],
15+
srcs_version = "PY3",
1516
visibility = ["//visibility:public"],
1617
)
1718

1819
py_test(
1920
name = "_jsonnet_test",
2021
srcs = ["_jsonnet_test.py"],
2122
data = ["test.jsonnet"],
23+
python_version = "PY3",
2224
deps = [":_jsonnet"],
2325
)

tools/build_defs/python_repo.bzl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
build_file_contents = """\
22
package(default_visibility = ["//visibility:public"])
33
4-
sh_binary(
5-
name = "python",
6-
srcs = ["bin/python"],
7-
data = [":include"],
8-
)
9-
104
filegroup(
115
name = "include",
126
srcs = glob(["include/**"]),
@@ -19,7 +13,7 @@ cc_library(
1913
)
2014
"""
2115

22-
def _python_interpreter(repository_ctx):
16+
def _impl(repository_ctx):
2317
rctx = repository_ctx
2418
if "/" in rctx.attr.path or "\\" in rctx.attr.path:
2519
# Canonicalize the path
@@ -41,12 +35,12 @@ def _python_interpreter(repository_ctx):
4135
rctx.file("BUILD", build_file_contents)
4236

4337

44-
python_interpreter = repository_rule(
45-
implementation = _python_interpreter,
38+
python_headers = repository_rule(
39+
implementation = _impl,
4640
local = True,
4741
attrs = {
4842
"path": attr.string(
49-
default = "python",
43+
default = "python3",
5044
),
5145
},
5246
)

0 commit comments

Comments
 (0)