@@ -23,50 +23,6 @@ available, it might help to examine the directory tree in the external
2323repository created for the package by rules_python. The external repository is
2424created 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
229163def _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