Wheel
A framework for defining wheels from a tree of Bazel targets.
Why use this over what rules_python
provides?
The rules here primarily wrap what rules_python provides but offers some improvements:
- Requirements are collected from transitive dependencies and added as
Require-Dist
to the wheel. - Sources and data are automatically collected.
Setup
Building wheels requires no setup, however, to activate all features of the wheel rules, a toolchain must be registered.
First some external python requirements will be required. There is guidance on
how to configure this in projects like rules_req_compile.
Once there is a means to fetch external dependencies within your repository, a
toolchain can be defined in a BUILD.bazel
file.
Example //:BUILD.bazel
:
load("@rules_venv//python/wheel:defs.bzl", "py_wheel_toolchain")
py_wheel_toolchain(
name = "py_wheel_toolchain_impl",
# Definable using bzlmod modules like: https://github.com/periareon/req-compile
twine = "@pip_deps//:twine",
visibility = ["//visibility:public"],
)
toolchain(
name = "py_wheel_toolchain",
toolchain = ":py_wheel_toolchain_impl",
toolchain_type = "@rules_venv//python/wheel:toolchain_type",
visibility = ["//visibility:public"],
)
Once the toolchain is defined, it should be registered in the MODULE.bazel
file
Example //:MODULE.bazel
:
register_toolchains(
"//:py_wheel_toolchain",
)
This will ensure all features of the wheel rules are available and usable.
Rules
Functions
py_wheel_publisher
load("@rules_venv//python/wheel:defs.bzl", "py_wheel_publisher") py_wheel_publisher(name, repository_url, wheel)
A rule for publishing wheels to pypi registries.
The rule uses twine to python registries. Users should refer to the documentation there for any configuration flags (such as auth) needed to deploy to the desired location.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
repository_url | The repository (package index) URL to upload the wheel to. If passed the twine arg --repository-url will be set to this value. | String | optional | "" |
wheel | The wheel to extract. | Label | required |
py_wheel_toolchain
load("@rules_venv//python/wheel:defs.bzl", "py_wheel_toolchain") py_wheel_toolchain(name, twine)
A toolchain for powering the py_wheel
rules.
load("@rules_venv//python/wheel:defs.bzl", "py_wheel_toolchain")
py_wheel_toolchain(
name = "py_wheel_toolchain_impl",
# Definable using bzlmod modules like: https://github.com/periareon/req-compile
twine = "@pip_deps//:twine",
visibility = ["//visibility:public"],
)
toolchain(
name = "py_wheel_toolchain",
toolchain = ":py_wheel_toolchain_impl",
toolchain_type = "@rules_venv//python/wheel:toolchain_type",
visibility = ["//visibility:public"],
)
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
twine | A py_library for twine. | Label | required |
package_tag
load("@rules_venv//python/wheel:defs.bzl", "package_tag") package_tag(name)
Generate a tag used to associate the target with a python package.
This tag should only be applied to other py_*
targets.
Example:
load("@rules_venv//python:defs.bzl", "py_library")
load("@rules_venv//python/wheel:defs.bzl", "package_tag")
py_library(
name = "my_target",
tags = [package_tag("my_python_package")],
# ...
# ...
# ...
)
PARAMETERS
RETURNS
str: The tag.
py_wheel_library
load("@rules_venv//python/wheel:defs.bzl", "py_wheel_library") py_wheel_library(name, srcs, deps, data, abi, author, author_email, classifiers, constraints_file, data_files, description_content_type, description_file, extra_distinfo_files, homepage, license, platform, project_urls, python_requires, python_tag, strip_path_prefixes, summary, version, distribution, repository_url, **kwargs)
Define a py_library
with an associated wheel.
This rule will traverse dependencies (deps
) to collect all data and dependencies
that belong to the current package. Any dependency tagged with the package_tag
whose
name matches this targets name will be considered a submodule and included in the package.
Example:
load("@rules_venv//python:defs.bzl", "py_library")
load("@rules_venv//python/wheel:defs.bzl", "package_tag", "py_wheel_library")
py_library(
name = "submodule",
srcs = ["submodule.py"],
tags = [
# Note this name matches the name of the `py_wheel_library` target
# and as a result will be included as a submodule within the wheel.
package_tag("my_py_package")
],
)
py_wheel_library(
name = "my_py_package",
deps = [":submodule"],
)
Targets created by this library:
name | details |
---|---|
{name} | The py_library target for the wheel. |
{name}.whl | The py_wheel target created from the {name} target. |
{name}.publish | A py_wheel_publisher target for publishing the wheel to a remote index. Defined only with repository_url . |
PARAMETERS