Metadata-Version: 2.5
Name: hatch-prebuilt
Version: 0.2.0
Summary: Build Python wheels that install prebuilt executables unchanged, with hatchling
Project-URL: Source, https://github.com/cpp-linter/hatch-prebuilt
Project-URL: Issues, https://github.com/cpp-linter/hatch-prebuilt/issues
Author-email: Xianpeng Shen <xianpeng.shen@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: binary,hatch,hatchling,packaging,wheel
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Hatch
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: System :: Archiving :: Packaging
Requires-Python: >=3.9
Requires-Dist: hatchling>=1.27
Provides-Extra: minisign
Requires-Dist: cryptography>=42; extra == 'minisign'
Description-Content-Type: text/markdown

# hatch-prebuilt

[![PyPI](https://img.shields.io/pypi/v/hatch-prebuilt?labelColor=454a63&color=007ec6)](https://pypi.org/project/hatch-prebuilt/)
[![ci](https://img.shields.io/github/actions/workflow/status/cpp-linter/hatch-prebuilt/test.yml?branch=main&label=ci&labelColor=454a63)](https://github.com/cpp-linter/hatch-prebuilt/actions/workflows/test.yml)
[![part of cpp-linter](https://img.shields.io/badge/part%20of-cpp--linter-ffc20a?labelColor=454a63)](https://cpp-linter.github.io/)

Build Python wheels that install prebuilt executables unchanged, with
[hatchling](https://hatch.pypa.io/latest/). `pip install` or `uv pip install` puts the executable
straight into the environment's `bin/` (`Scripts\` on Windows), with no Python wrapper in
between, so starting it costs nothing extra.

A tool that finds its own files through the path of its executable, such as clang's builtin
headers in `../lib/clang/<major>/include`, sets `package` in its recipe instead. The wheel then
keeps the executable and its files together in that Python package, and a small console script
starts the executable (`os.execv` on Linux and macOS). That way the tool finds its files however
it is installed, including `uv tool install` and `pip install --user` on Windows, which put the
scripts away from the environment's other files. pyproject.toml then lists `scripts` in
`project.dynamic`.

The [cpp-linter](https://github.com/cpp-linter) wheel repositories use it:
[ccache-wheel](https://github.com/cpp-linter/ccache-wheel) (`ccache-bin`),
[clang-apply-replacements](https://github.com/cpp-linter/clang-apply-replacements),
[clang-include-cleaner](https://github.com/cpp-linter/clang-include-cleaner),
[llvm-cov-wheel](https://github.com/cpp-linter/llvm-cov-wheel) (`llvm-cov`) and
[clangd-wheel](https://github.com/cpp-linter/clangd-wheel) (`clangd-bin`).

## What it does

A wheel repository describes its tool in `hatch_build.py`: which platforms it has, and how to
download and verify the files of one version for one platform. hatch-prebuilt does the rest:

- picks the version (from a version file, or an environment variable so CI can build older
  versions) and the platform (from an environment variable, or the build machine);
- checks that every executable is a binary for that platform: ELF machine and static linking,
  Mach-O architecture or universal slices, PE machine;
- works out the wheel tag, including the minimum macOS version the binaries declare;
- stages the license files where `license-files` finds them, and gives hatchling the executables
  as shared scripts and any other files (man pages, headers) as shared data.

It also has the download helpers the recipes share: retrying downloads, a download cache, SHA-512
pins, minisign signatures (`hatch-prebuilt[minisign]`) and archive extraction.

## A recipe

```python
# hatch_build.py
from pathlib import Path

from hatch_prebuilt import Platform, Recipe, Staged, cached_download, check_sha512

URL = "https://example.com/mytool-{version}-{platform}{exe}"
SHA512 = {...}


class MyTool(Recipe):
    name = "mytool"
    version_file = "mytool_version.txt"
    platform_variable = "MYTOOL_PLATFORM"
    platforms = (
        Platform(
            "linux-x86_64",
            "manylinux_2_17_x86_64.musllinux_1_1_x86_64",
            "elf-x86_64",
            "linux",
            "x86_64",
        ),
        Platform("macos-arm64", "macosx_{macos}_arm64", "macho-arm64", "macos", "arm64"),
        Platform("windows-x86_64", "win_amd64", "pe-x86_64", "windows", "x86_64"),
    )

    def stage(self, platform, version):
        url = URL.format(version=version, platform=platform.name, exe=platform.exe)
        binary = cached_download(url, self.cache / Path(url).name)
        check_sha512(binary, SHA512[platform.name], url)
        return Staged(scripts={f"mytool{platform.exe}": binary}, licenses={"LICENSE": ...})


RECIPE = MyTool(Path(__file__).parent)
get_metadata_hook, get_build_hook = RECIPE.hooks()

if __name__ == "__main__":
    raise SystemExit(RECIPE.main())
```

```toml
# pyproject.toml
[build-system]
requires = ["hatchling>=1.27", "hatch-prebuilt>=0.1,<1"]
build-backend = "hatchling.build"

[project]
name = "mytool-bin"
dynamic = ["version"]
license-files = ["upstream/LICENSE"]

[tool.hatch.metadata.hooks.custom]

[tool.hatch.build.targets.wheel]
bypass-selection = true

[tool.hatch.build.targets.wheel.hooks.custom]
```

Then `python hatch_build.py platforms` lists the platforms, and
`MYTOOL_PLATFORM=<platform> python -m build --wheel` builds one wheel, for any platform, on
Linux or macOS (Windows cannot give the executables their executable bit). The shared
[`binary-wheels.yml`](https://github.com/cpp-linter/.github/blob/main/.github/workflows/binary-wheels.yml)
workflow builds and tests all of them. To start a new wheel repository, copy
[clang-apply-replacements](https://github.com/cpp-linter/clang-apply-replacements) for a tool from
the static binaries, or [ccache-wheel](https://github.com/cpp-linter/ccache-wheel) for a tool with
its own upstream releases.

Tools from the
[clang-tools-static-binaries](https://github.com/cpp-linter/clang-tools-static-binaries)
releases need even less: `hatch_prebuilt.static_binaries.StaticBinariesRecipe` knows the
platforms, the file names and the SHA-512 pins in `sources.json`, so the recipe only names the
tools. With `resource_headers = True` (clangd, clang-include-cleaner) it adds clang's builtin
headers and packages the tools as described above.

## Contributing

See the [contributing guide](https://github.com/cpp-linter/.github/blob/main/CONTRIBUTING.md).
`uv run --group test pytest` runs the tests.

## License

[MIT](LICENSE)
