|
| 1 | +:::{default-domain} bzl |
| 2 | +::: |
| 3 | + |
| 4 | +# How to expose headers from a PyPI package |
| 5 | + |
| 6 | +When you depend on a PyPI package that includes C headers (like `numpy`), you |
| 7 | +need to make those headers available to your `cc_library` or |
| 8 | +`cc_binary` targets. |
| 9 | + |
| 10 | +The recommended way to do this is to inject a `BUILD.bazel` file into the |
| 11 | +external repository for the package. This `BUILD` file will create |
| 12 | +a `cc_library` target that exposes the header files. |
| 13 | + |
| 14 | +First, create a `.bzl` file that has the extra logic we'll inject. Putting it |
| 15 | +in a separate bzl file avoids having to redownload and extract the whl file |
| 16 | +when our logic changes. |
| 17 | + |
| 18 | +```bzl |
| 19 | + |
| 20 | +# pypi_extra_targets.bzl |
| 21 | +load("@rules_cc//cc:cc_library.bzl", "cc_library") |
| 22 | + |
| 23 | +def extra_numpy_targets(): |
| 24 | + cc_library( |
| 25 | + name = "headers", |
| 26 | + hdrs = glob(["**/*.h"]), |
| 27 | + visibility = ["//visibility:public"], |
| 28 | + ) |
| 29 | +``` |
| 30 | + |
| 31 | +## Bzlmod setup |
| 32 | + |
| 33 | +In your `MODULE.bazel` file, use the `build_file_content` attribute of |
| 34 | +`pip.parse` to inject the `BUILD` file content for the `numpy` package. |
| 35 | + |
| 36 | +```bazel |
| 37 | +# MODULE.bazel |
| 38 | +load("@rules_python//python/extensions:pip.bzl", "parse", "whl_mods") |
| 39 | +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") |
| 40 | +whl_mods = use_extension("@rules_python//python/extensions:pip.bzl", "whl_mods") |
| 41 | + |
| 42 | + |
| 43 | +# Define a specific modification for a wheel |
| 44 | +whl_mods( |
| 45 | + hub_name = "pypi_mods", |
| 46 | + whl_name = "numpy-1.0.0-py3-none-any.whl", # The exact wheel filename |
| 47 | + additive_build_content = """ |
| 48 | +load("@//:pypi_extra_targets.bzl", "numpy_hdrs") |
| 49 | +
|
| 50 | +extra_numpy_targets() |
| 51 | +""", |
| 52 | +) |
| 53 | +pip.parse( |
| 54 | + hub_name = "pypi", |
| 55 | + wheel_name = "numpy", |
| 56 | + requirements_lock = "//:requirements.txt", |
| 57 | + whl_modifications = { |
| 58 | + "@pypi_mods//:numpy.json": "numpy", |
| 59 | + }, |
| 60 | + extra_hub_aliases = { |
| 61 | + "numpy": ["headers"], |
| 62 | + } |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +## WORKSPACE setup |
| 67 | + |
| 68 | +In your `WORKSPACE` file, use the `annotations` attribute of `pip_parse` to |
| 69 | +inject additional `BUILD` file content, then use `extra_hub_targets` to expose |
| 70 | +that target in the `@pypi` hub repo. |
| 71 | + |
| 72 | +The {obj}`package_annotation` helper can be used to construct the value for the |
| 73 | +`annotations` attribute. |
| 74 | + |
| 75 | +```starlark |
| 76 | +# WORKSPACE |
| 77 | +load("@rules_python//python:pip.bzl", "package_annotation", "pip_parse") |
| 78 | + |
| 79 | +pip_parse( |
| 80 | + name = "pypi", |
| 81 | + requirements_lock = "//:requirements.txt", |
| 82 | + annotations = { |
| 83 | + "numpy": package_annotation( |
| 84 | + additive_build_content = """\ |
| 85 | +load("@//:pypi_extra_targets.bzl", "numpy_hdrs") |
| 86 | +
|
| 87 | +extra_numpy_targets() |
| 88 | +""" |
| 89 | + ), |
| 90 | + }, |
| 91 | + extra_hub_targets = { |
| 92 | + "numpy": ["headers"], |
| 93 | + }, |
| 94 | +) |
| 95 | +``` |
| 96 | + |
| 97 | +## Using the headers |
| 98 | + |
| 99 | +In your `BUILD.bazel` file, you can now depend on the generated `headers` |
| 100 | +target. |
| 101 | + |
| 102 | +```bazel |
| 103 | +# BUILD.bazel |
| 104 | +cc_library( |
| 105 | + name = "my_c_extension", |
| 106 | + srcs = ["my_c_extension.c"], |
| 107 | + deps = ["@pypi//numpy:headers"], |
| 108 | +) |
| 109 | +``` |
0 commit comments