Metadata-Version: 2.5
Name: hatch-prebuilt
Version: 0.3.0
Summary: Build Python wheels that install prebuilt executables unchanged, with hatchling
Project-URL: Source, https://github.com/cpp-linter/wheels/tree/main/hatch-prebuilt
Project-URL: Issues, https://github.com/cpp-linter/wheels/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/wheels/test.yml?branch=main&label=ci&labelColor=454a63)](https://github.com/cpp-linter/wheels/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-wheel),
[clang-include-cleaner](https://github.com/cpp-linter/clang-include-cleaner-wheel),
[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`).

## Tools from the static binaries: pyproject.toml only

For a tool from the
[clang-tools-static-binaries](https://github.com/cpp-linter/clang-tools-static-binaries)
releases, pyproject.toml is the whole recipe. hatch-prebuilt already knows the platforms, the file
names and the SHA-512 pins in `sources.json`:

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

[project]
name = "clang-include-cleaner"
dynamic = ["version", "scripts"]
license-files = ["upstream/LICENSE.TXT"]

[tool.hatch.metadata.hooks.prebuilt]
recipe = "static-binaries"
tools = ["clang-include-cleaner"]
resource-headers = true  # clang's builtin headers, for clangd and clang-include-cleaner

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

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

| Option | Default | Meaning |
| --- | --- | --- |
| `recipe` | `static-binaries` | Where the files come from |
| `tools` | (required) | The executables to package |
| `name` | the first tool | The name in messages |
| `resource-headers` | `false` | Add clang's builtin headers and keep the tools with them in a package |
| `package` | the first tool, `_` for `-` | That package's name, when another project uses the default one |
| `min-major` | `12` | The oldest LLVM major version that `update` adds to `sources.json` |

The version comes from `llvm_version.txt`, or `LLVM_VERSION` when it is set, and the platform
from `LLVM_PLATFORM`, or the build machine:

```bash
python -m hatch_prebuilt update      # add new LLVM releases to sources.json
python -m hatch_prebuilt versions    # the versions in sources.json
python -m hatch_prebuilt platforms   # the platforms to build
python -m hatch_prebuilt info        # what CI needs, as JSON
LLVM_VERSION=22.1.8 LLVM_PLATFORM=windows-amd64 python -m build --wheel
```

## Other tools: a recipe in hatch_build.py

A tool with its own upstream releases describes them 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.

```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.3,<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 -m hatch_prebuilt platforms` (or `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).

## In CI

The shared
[`binary-wheels.yml`](https://github.com/cpp-linter/wheels/blob/main/.github/workflows/binary-wheels.yml)
workflow builds and tests every wheel. The
[runbooks](https://github.com/cpp-linter/wheels/tree/main/runbooks) next to it in cpp-linter/wheels say how
to start a new wheel repository and how releases work.

## Contributing

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

## License

[MIT](https://github.com/cpp-linter/wheels/blob/main/hatch-prebuilt/LICENSE)
