Skip to content

Commit 7a1ba0e

Browse files
committed
Fixes to allow downstream project to set a different Python toolchain.
Previously, in the typical use case where `python_interpreter_target` is passed, the @bazel_rules_hdl//:init.bzl init macro would always call the vendored `install_deps`. However, this would only work if the passed in toolchain name matched the one used to generate the vendored requirements.bzl (since @python39 is hardcoded as a default inside). Now, we pass along any interpreter args to the vendored `install_deps` to override the defaults as documented at: https://github.com/bazelbuild/rules_python/blob/main/examples/pip_parse_vendored/README.md. This also eliminates the documented requirement to use the generated pip deps instead of the vendored version if an interpreter arg is set. We also update rules_python to the latest version, allowing the use of the "_host" toolchain label to avoid loading the interpreter per bazel-contrib/rules_python#1644. Finally, we clean up other references to the pip_parse repo (which was only intended for the vendoring) and move them to use the vendored deps directly.
1 parent c176607 commit 7a1ba0e

File tree

9 files changed

+112
-60
lines changed

9 files changed

+112
-60
lines changed

WORKSPACE

+23-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ bazel_skylib_workspace()
4747
maybe(
4848
http_archive,
4949
name = "rules_python",
50-
sha256 = "5868e73107a8e85d8f323806e60cad7283f34b32163ea6ff1020cf27abef6036",
51-
strip_prefix = "rules_python-0.25.0",
52-
url = "https://github.com/bazelbuild/rules_python/releases/download/0.25.0/rules_python-0.25.0.tar.gz",
50+
sha256 = "e3f1cc7a04d9b09635afb3130731ed82b5f58eadc8233d4efb59944d92ffc06f",
51+
strip_prefix = "rules_python-0.33.2",
52+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.33.2/rules_python-0.33.2.tar.gz",
5353
)
5454

55-
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
55+
load(
56+
"@rules_python//python:repositories.bzl",
57+
"py_repositories",
58+
"python_register_toolchains",
59+
)
60+
61+
# Must be called before using anything from rules_python.
62+
# https://github.com/bazelbuild/rules_python/issues/1560#issuecomment-1815118394
63+
py_repositories()
5664

5765
python_register_toolchains(
5866
name = "python39",
@@ -157,7 +165,16 @@ load("//dependency_support:dependency_support.bzl", "dependency_support")
157165
dependency_support()
158166
# Third Party
159167

160-
load("@python39//:defs.bzl", "interpreter")
161168
load("//:init.bzl", "init")
162169

163-
init(python_interpreter_target = interpreter)
170+
init(python_interpreter_target = "@python39_host//:python")
171+
172+
load("@rules_python//python:pip.bzl", "pip_parse")
173+
174+
# Used only by the rules that vendor requirements.bzl
175+
# Not needed by users of rules_hdl.
176+
pip_parse(
177+
name = "rules_hdl_pip_deps_to_vendor",
178+
python_interpreter_target = "@python39_host//:python",
179+
requirements_lock = "//dependency_support:pip_requirements.txt",
180+
)

cocotb/tests/BUILD.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_hdl_pip_deps//:requirements.bzl", "requirement")
1615
load("//cocotb:cocotb.bzl", "cocotb_test")
16+
load("//dependency_support:requirements.bzl", "requirement")
1717
load("//verilog:defs.bzl", "verilog_library")
1818

1919
package(

dependency_support/BUILD.bazel

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# See https://github.com/bazelbuild/rules_python/blob/main/examples/pip_parse_vendored/BUILD.bazel
16+
# for canonical vendoring setup.
17+
1518
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
1619
load("@bazel_skylib//rules:write_file.bzl", "write_file")
1720
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
@@ -21,14 +24,25 @@ package(
2124
default_visibility = ["//visibility:private"],
2225
)
2326

24-
compile_pip_requirements(name = "pip_requirements")
27+
compile_pip_requirements(
28+
name = "pip_requirements",
29+
src = "pip_requirements.in",
30+
)
2531

2632
genrule(
2733
name = "clean_requirements",
28-
srcs = ["@rules_hdl_pip_deps//:requirements.bzl"],
34+
srcs = ["@rules_hdl_pip_deps_to_vendor//:requirements.bzl"],
2935
outs = ["requirements.clean.bzl"],
3036
cmd = " | ".join([
3137
"cat $<",
38+
# Substitute for vendored dependencies directly.
39+
# We want to remove any references to the pip_parse repo used to generate
40+
# the dependencies, otherwise the vendoring is not useful.
41+
"sed -e 's/rules_hdl_pip_deps_to_vendor/rules_hdl_pip_deps_vendored/g'",
42+
"sed -e 's|//\\([^:]*\\):pkg|_\\1//:pkg|g'",
43+
"sed -e 's|//\\([^:]*\\):whl|_\\1//:whl|g'",
44+
"sed -e 's|//\\([^:]*\\):data|_\\1//:data|g'",
45+
"sed -e 's|//{}:{}|_{}//:{}|g' >$@",
3246
# Replace the bazel 6.0.0 specific comment with something that bazel 5.4.0 would produce.
3347
# This enables this example to be run as a test under bazel 5.4.0.
3448
"""sed -e 's#@rules_hdl//#@//#'""",

dependency_support/com_google_skywater_pdk/bundled.BUILD.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
""" BUILD file for Skywater PDK """
1616

17-
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
17+
load("@rules_hdl//dependency_support:requirements.bzl", "requirement")
1818
load("@rules_hdl//dependency_support/com_google_skywater_pdk:cell_libraries.bzl", "CELL_LIBRARIES")
19-
load("@rules_hdl_pip_deps//:requirements.bzl", "requirement")
19+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
2020

2121
licenses(["notice"])
2222

dependency_support/requirements.bzl

+45-16
Large diffs are not rendered by default.

gds_write/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
# GDS write tool package.
1616

17-
load("@rules_hdl_pip_deps//:requirements.bzl", "requirement")
1817
load("@rules_python//python:defs.bzl", "py_binary")
18+
load("//dependency_support:requirements.bzl", "requirement")
1919

2020
package(
2121
default_applicable_licenses = ["//:package_license"],

init.bzl

+22-30
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
1818
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
19-
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
20-
load("@rules_python//python:pip.bzl", "pip_parse")
21-
load("@rules_python//python:repositories.bzl", "py_repositories")
19+
load(
20+
"@rules_proto//proto:repositories.bzl",
21+
"rules_proto_dependencies",
22+
"rules_proto_toolchains",
23+
)
2224
load("//dependency_support:requirements.bzl", install_pip_deps = "install_deps")
2325
load("//dependency_support/boost:init_boost.bzl", "init_boost")
2426
load("//dependency_support/pybind11:init_pybind11.bzl", "init_pybind11")
@@ -30,41 +32,31 @@ def init(python_interpreter = None, python_interpreter_target = None):
3032
must call `init` to allow @bazel_rules_hdl to set itself up.
3133
3234
`python_interpreter` and `python_interpreter_target` are passed to
33-
@bazel_rules_hdl's instance of `pip_parse`. They can normally be set to
34-
the default None value, but if the outside workspace has a custom Python
35-
toolchain configured, these must be set, otherwise @bazel_rules_hdl will
36-
not use the right Python toolchain when installing pip dependencies.
37-
38-
If either is set, the outside workspace must also include:
39-
40-
load(
41-
"@rules_hdl_pip_deps//:requirements.bzl",
42-
rules_hdl_install_pip_deps = "install_deps",
43-
)
44-
rules_hdl_install_pip_deps()
35+
@bazel_rules_hdl's vendored requirements.bzl `install_deps`. If unspecified,
36+
this project's default toolchain will be used. If the outside workspace has
37+
a custom Python toolchain configured, these should be set, otherwise
38+
@bazel_rules_hdl will not use the right Python toolchain when installing
39+
pip dependencies.
4540
4641
Args:
4742
python_interpreter: Path to external Python interpreter to use with
48-
`pip_parse`. This can be an absolute path or relative to the host's
49-
`PATH` environment variable.
43+
`install_deps` for PyPI dependencies. This can be an absolute path or
44+
relative to the host's `PATH` environment variable.
5045
python_interpreter_target: Bazel target of a Python interpreter to build
51-
to use with `pip_parse`. Using `python_interpreter_target` makes it
52-
possible to have a hermetic Python toolchain. `python_interpreter_target`
53-
takes precedence over `python_interpreter` if both are set.
46+
to use with `install_deps` for PyPI dependencies. Using
47+
`python_interpreter_target` makes it possible to have a hermetic
48+
Python toolchain. `python_interpreter_target` takes precedence over
49+
`python_interpreter` if both are set.
5450
"""
5551
rules_proto_dependencies()
5652
rules_proto_toolchains()
57-
py_repositories()
5853

59-
# Used only by the rules that vendor requirements.bzl
60-
pip_parse(
61-
name = "rules_hdl_pip_deps",
62-
requirements_lock = "@rules_hdl//dependency_support:pip_requirements.txt",
63-
python_interpreter = python_interpreter,
64-
python_interpreter_target = python_interpreter_target,
65-
)
66-
if (not python_interpreter) or (python_interpreter_target != "@python39//:install/bin/python3"):
67-
install_pip_deps()
54+
install_deps_kwargs = {}
55+
if python_interpreter:
56+
install_deps_kwargs["python_interpreter"] = python_interpreter
57+
if python_interpreter_target:
58+
install_deps_kwargs["python_interpreter_target"] = python_interpreter_target
59+
install_pip_deps(**install_deps_kwargs)
6860

6961
init_boost()
7062
init_pybind11(python_interpreter_target = python_interpreter_target)

pdk/liberty/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
"""ASAP7 PDK Package"""
1616

17-
load("@rules_hdl_pip_deps//:requirements.bzl", "requirement")
1817
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
18+
load("//dependency_support:requirements.bzl", "requirement")
1919

2020
package(
2121
default_applicable_licenses = ["//:package_license"],

tools/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_hdl_pip_deps//:requirements.bzl", "requirement")
1615
load("@rules_python//python:defs.bzl", "py_binary")
16+
load("//dependency_support:requirements.bzl", "requirement")
1717

1818
package(
1919
default_applicable_licenses = ["//:package_license"],

0 commit comments

Comments
 (0)