Philipp Schrader | 1f0a3d0 | 2022-12-21 20:30:42 -0800 | [diff] [blame] | 1 | def _extract_numpy_headers_impl(ctx): |
| 2 | files = ctx.attr.numpy[DefaultInfo].default_runfiles.files.to_list() |
| 3 | prefix = ctx.attr.header_prefix |
| 4 | |
| 5 | hdrs = [] |
| 6 | for file in files: |
| 7 | _, partition, include_file = file.path.partition("/numpy/core/include/numpy/") |
| 8 | if partition: |
| 9 | hdr = ctx.actions.declare_file("%s/numpy/%s" % (prefix, include_file)) |
| 10 | ctx.actions.run( |
| 11 | inputs = [file], |
| 12 | outputs = [hdr], |
| 13 | executable = "cp", |
| 14 | arguments = [file.path, hdr.path], |
| 15 | ) |
| 16 | hdrs.append(hdr) |
| 17 | |
| 18 | return [DefaultInfo(files=depset(hdrs))] |
| 19 | |
| 20 | extract_numpy_headers = rule( |
| 21 | implementation = _extract_numpy_headers_impl, |
| 22 | doc = "Extracts the numpy headers from the corresponding py_library target.", |
| 23 | attrs = { |
| 24 | "numpy": attr.label( |
| 25 | mandatory = True, |
| 26 | providers = [PyInfo], |
| 27 | doc = "The label for the numpy py_library target.", |
| 28 | ), |
| 29 | "header_prefix": attr.string( |
| 30 | mandatory = True, |
| 31 | doc = "The directory to copy the headers into.", |
| 32 | ), |
| 33 | }, |
| 34 | ) |