Skip to main content

Hatch plugin for Rust builds

Project description

hatch rs

Hatch plugin for Rust builds

Build Status codecov License PyPI

Overview

A simple, extensible Rust build plugin for hatch.

[tool.hatch.build.hooks.hatch-rs]
verbose = true
path = "."
module = "project"

Rust artifacts and C ABI libraries

Projects can declare multiple Rust artifacts in one hook. Artifact name is the Cargo output stem used for exact file discovery, and crate-type defaults to cdylib. A destination containing {python_extension_name} packages that cdylib as a Python extension module; other Rust artifact destinations package the platform shared library name for standalone C ABI consumers.

[tool.hatch.build.hooks.hatch-rs]
verbose = true
path = "."
module = "project"
target-dir = "isolated"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project"
manifest = "Cargo.toml"
destination = "project/{python_extension_name}"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"

Destination templates support {module}, {target}, {profile}, {name}, {shared_library}, {import_library}, and {python_extension_name}.

Generated files and headers

Artifacts with command run an argv-list command and then validate explicit outputs. Outputs can be packaged into the wheel, installed as wheel shared data, or used only as required validation checks. The same outputs table can be used for generated headers, either on a command artifact or on the Rust artifact whose build produced the file.

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "generated-package-files"
command = ["python", "scripts/write_generated_files.py"]
inputs = ["scripts/write_generated_files.py"]

[[tool.hatch.build.hooks.hatch-rs.artifacts.outputs]]
source = "project/generated/package.txt"
destination = "project/generated/package.txt"
install-scheme = "package"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "public-c-header"

[[tool.hatch.build.hooks.hatch-rs.artifacts.outputs]]
source = "project/include/project.h"
destination = "include/project/project.h"
install-scheme = "shared-data"

ABI validation and artifact metadata

cdylib artifacts can validate the copied C ABI library before the wheel is finalized. The hook can check expected exported symbols, verify headers and ABI strings/macros, load the copied library with ctypes.CDLL when validate = true, run project validation commands, include Windows import libraries, and emit a package-local artifact manifest.

[tool.hatch.build.hooks.hatch-rs]
module = "project"
target-dir = "isolated"
artifact-manifest = true

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"
expected-symbols = ["project_ffi_answer"]
expected-headers = ["project/include/project.h"]
expected-abi-strings = ["PROJECT_ABI_VERSION"]
validate = true
include-import-lib = true

[[tool.hatch.build.hooks.hatch-rs.artifacts.validation-commands]]
command = ["python", "scripts/validate_abi.py", "{destination}", "{header}"]

include-import-lib only packages an import library on Windows targets, where Cargo emits .dll.lib or .dll.a files for downstream native linkers.

Platform tags and cibuildwheel

Binary wheel tags are generated with packaging.tags from the resolved Rust target. Linux builds default to linux_<arch> unless AUDITWHEEL_PLAT is set by auditwheel/cibuildwheel or wheel-platform-tag is configured explicitly. Rust targets should use concrete triples such as x86_64-unknown-linux-gnu or x86_64-unknown-linux-musl; manylinux and musllinux are wheel platform tags, not Rust target triples.

For cibuildwheel, keep Cargo outputs isolated so repeated platform builds do not reuse stale artifacts from another target:

[tool.hatch.build.hooks.hatch-rs]
module = "project"
target-dir = "isolated"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project"
manifest = "Cargo.toml"
destination = "project/{python_extension_name}"

[[tool.hatch.build.hooks.hatch-rs.artifacts]]
name = "project_ffi"
manifest = "rust/Cargo.toml"
destination = "project/lib/{shared_library}"

[tool.cibuildwheel]
build = "cp311-*"
test-command = "python -c \"import project\""

When cross-building outside cibuildwheel, set wheel-platform-tag only if the final wheel platform tag is known, for example manylinux_2_28_x86_64.

[!NOTE] This library was generated using copier from the Base Python Project Template repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hatch_rs-0.2.1.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hatch_rs-0.2.1-py3-none-any.whl (42.2 kB view details)

Uploaded Python 3

File details

Details for the file hatch_rs-0.2.1.tar.gz.

File metadata

  • Download URL: hatch_rs-0.2.1.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for hatch_rs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 085d8a1e59a927c00b61b82e4d1ca4d18151de08cc55bbb4ddb3aa2980c3b21a
MD5 4fb2f0701180c984a36ce1f81af121fc
BLAKE2b-256 a891fcb1c5091f2e32f50d2329220c7b2bed9fb622ceb796e96fc99233a7ead7

See more details on using hashes here.

File details

Details for the file hatch_rs-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: hatch_rs-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 42.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for hatch_rs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e64c9c4fc69d8646d622654232597148eb225d4b28819f62c7cc165f1ae9649
MD5 18c20ff21c709ccb5bdde4d760038ab1
BLAKE2b-256 ad9486c2d6808e77954cc41c74879c5ef2ac3241f99efc1a73c62d2d83d3b712

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page