Skip to content

Commit ea27055

Browse files
committed
Simplify Python
1 parent 6040475 commit ea27055

File tree

2 files changed

+54
-157
lines changed

2 files changed

+54
-157
lines changed

MODULE.bazel

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,27 @@ python = use_extension("@rules_python//python/extensions:python.bzl", "python")
1919

2020
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
2121

22-
python.toolchain(
23-
python_version = "3.10",
24-
)
25-
26-
pip.parse(
27-
hub_name = "tflm_pip_deps",
28-
python_version = "3.10",
29-
requirements_lock = "//python:python_requirements_3_10.txt",
30-
)
31-
32-
python.toolchain(python_version = "3.11")
33-
34-
pip.parse(
35-
hub_name = "tflm_pip_deps",
36-
python_version = "3.11",
37-
requirements_lock = "//python:python_requirements_3_11.txt",
38-
)
39-
40-
python.toolchain(
41-
python_version = "3.12",
42-
)
43-
44-
pip.parse(
45-
hub_name = "tflm_pip_deps",
46-
python_version = "3.12",
47-
requirements_lock = "//python:python_requirements_3_12.txt",
48-
)
49-
50-
python.toolchain(
51-
is_default = True,
52-
python_version = "3.13",
53-
)
54-
55-
pip.parse(
56-
hub_name = "tflm_pip_deps",
57-
python_version = "3.13",
58-
requirements_lock = "//python:python_requirements_3_13.txt",
59-
)
60-
61-
# python.toolchain(python_version = "3.14")
62-
# pip.parse(
63-
# hub_name = "tflm_pip_deps",
64-
# python_version = "3.14",
65-
# requirements_lock = "//third_party:python_requirements_3_14.txt",
66-
# )
22+
PYTHON_VERSIONS = [
23+
"3.10",
24+
"3.11",
25+
"3.12",
26+
"3.13",
27+
]
28+
29+
[
30+
(
31+
python.toolchain(
32+
is_default = (version == "3.13"),
33+
python_version = version,
34+
),
35+
pip.parse(
36+
hub_name = "tflm_pip_deps",
37+
python_version = version,
38+
requirements_lock = "//python:python_requirements_%s.txt" % version.replace(".", "_"),
39+
),
40+
)
41+
for version in PYTHON_VERSIONS
42+
]
6743

6844
use_repo(
6945
python,
@@ -138,24 +114,14 @@ cc_deps.config(
138114
],
139115
pip_repo_template = "tflm_pip_deps_{version}_{pkg}",
140116
pkg_name = "numpy",
141-
versions = [
142-
"3.10",
143-
"3.11",
144-
"3.12",
145-
"3.13",
146-
],
117+
versions = PYTHON_VERSIONS,
147118
)
148119
cc_deps.config(
149120
name = "tensorflow_cc_deps",
150121
includes = ["tensorflow/include"],
151122
libs = ["tensorflow/libtensorflow_framework.so.2"],
152123
pip_repo_template = "tflm_pip_deps_{version}_{pkg}",
153124
pkg_name = "tensorflow",
154-
versions = [
155-
"3.10",
156-
"3.11",
157-
"3.12",
158-
"3.13",
159-
],
125+
versions = PYTHON_VERSIONS,
160126
)
161127
use_repo(cc_deps, "numpy_cc_deps", "tensorflow_cc_deps")

python/py_pkg_cc_deps.bzl

Lines changed: 31 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,6 @@ available, it might help to examine the directory tree in the external
2323
repository created for the package by rules_python. The external repository is
2424
created in the bazel cache; in the example below, in a subdirectory
2525
`external/tflm_pip_deps_numpy`.
26-
27-
For example, to use the headers from NumPy:
28-
29-
1. Add Python dependencies (numpy is named in python_requirements.txt), via the
30-
usual method, to an external repository named `tflm_pip_deps` via @rules_python
31-
in the WORKSPACE:
32-
```
33-
load("@rules_python//python:pip.bzl", "pip_parse")
34-
pip_parse(
35-
name = "tflm_pip_deps",
36-
requirements_lock = "@//third_party:python_requirements.txt",
37-
)
38-
load("@tflm_pip_deps//:requirements.bzl", "install_deps")
39-
install_deps()
40-
```
41-
42-
2. Use the repository rule `py_pkg_cc_deps` in the WORKSPACE to create an
43-
external repository with a target `@numpy_cc_deps//:cc_headers`, passing the
44-
`:pkg` target from @tflm_pip_deps, obtained via requirement(), and an
45-
`includes` path based on an examination of the package and the desired #include
46-
paths in the C code:
47-
```
48-
load("@tflm_pip_deps//:requirements.bzl", "requirement")
49-
load("@//python:py_pkg_cc_deps.bzl", "py_pkg_cc_deps")
50-
py_pkg_cc_deps(
51-
name = "numpy_cc_deps",
52-
pkg = requirement("numpy"),
53-
includes = ["numpy/core/include"],
54-
)
55-
```
56-
57-
3. Use the cc_library target `@numpy_cc_deps//:cc_headers` in a BUILD file as
58-
a dependency to a rule that needs the headers, e.g., the cc_library()-based
59-
pybind_library():
60-
```
61-
pybind_library(
62-
name = "your_extension_lib",
63-
srcs = [...],
64-
deps = ["@numpy_cc_deps//:cc_headers", ...],
65-
)
66-
```
67-
68-
See the test target //python/tests:cc_dep_link_test elsewhere for an example
69-
which links against a library shipped in a Python package.
7026
"""
7127

7228
# This extends the standard rules_python rules to expose C-language dependences
@@ -176,55 +132,33 @@ def _cc_deps_impl(ctx):
176132
groups[config.name].append(config)
177133

178134
for name, configs in groups.items():
179-
if len(configs) == 1 and not configs[0].python_version and not configs[0].versions:
180-
# Legacy behavior for a single unversioned config
181-
py_pkg_cc_deps(
182-
name = name,
183-
pkg = configs[0].pkg,
184-
includes = configs[0].includes,
185-
libs = configs[0].libs,
186-
)
187-
else:
188-
# Create a hub repository that selects between them
189-
name_version_map = {}
190-
191-
for config in configs:
192-
# Handle old style individual config
193-
if config.python_version:
194-
vname = "%s_%s" % (name, config.python_version.replace(".", ""))
195-
py_pkg_cc_deps(
196-
name = vname,
197-
pkg = config.pkg,
198-
includes = config.includes,
199-
libs = config.libs,
200-
)
201-
name_version_map[config.python_version] = vname
202-
203-
# Handle new style automated matrix config
204-
if config.versions:
205-
for version in config.versions:
206-
vname = "%s_%s" % (name, version.replace(".", ""))
207-
208-
# Construct package label from template
209-
# e.g. @tflm_pip_deps_310_numpy//:pkg
210-
pip_repo = config.pip_repo_template.format(
211-
version = version.replace(".", ""),
212-
pkg = config.pkg_name,
213-
)
214-
pkg_label = Label("@%s//:pkg" % pip_repo)
215-
216-
py_pkg_cc_deps(
217-
name = vname,
218-
pkg = pkg_label,
219-
includes = config.includes,
220-
libs = config.libs,
221-
)
222-
name_version_map[version] = vname
223-
224-
_py_pkg_cc_deps_hub(
225-
name = name,
226-
name_version_map = name_version_map,
227-
)
135+
# Create a hub repository that selects between versions
136+
name_version_map = {}
137+
138+
for config in configs:
139+
for version in config.versions:
140+
vname = "%s_%s" % (name, version.replace(".", ""))
141+
142+
# Construct package label from template
143+
# e.g. @tflm_pip_deps_310_numpy//:pkg
144+
pip_repo = config.pip_repo_template.format(
145+
version = version.replace(".", ""),
146+
pkg = config.pkg_name,
147+
)
148+
pkg_label = Label("@%s//:pkg" % pip_repo)
149+
150+
py_pkg_cc_deps(
151+
name = vname,
152+
pkg = pkg_label,
153+
includes = config.includes,
154+
libs = config.libs,
155+
)
156+
name_version_map[version] = vname
157+
158+
_py_pkg_cc_deps_hub(
159+
name = name,
160+
name_version_map = name_version_map,
161+
)
228162

229163
def _py_pkg_cc_deps_hub_impl(ctx):
230164
# Map from python version string to repo name
@@ -280,15 +214,12 @@ cc_deps = module_extension(
280214
"config": tag_class(
281215
attrs = {
282216
"name": attr.string(mandatory = True),
283-
"pkg": attr.label(), # Optional now
284217
"includes": attr.string_list(mandatory = True),
285218
"libs": attr.string_list(),
286-
"python_version": attr.string(),
287-
# New attributes for automation
288-
"versions": attr.string_list(),
289-
"pkg_name": attr.string(),
290-
"pip_repo_template": attr.string(),
219+
"versions": attr.string_list(mandatory = True),
220+
"pkg_name": attr.string(mandatory = True),
221+
"pip_repo_template": attr.string(mandatory = True),
291222
},
292223
),
293224
},
294-
)
225+
)

0 commit comments

Comments
 (0)