Need to run [Make] when building wheel from sdist or installing the latter, how?

I haven’t looked into Make integration. But for CMake integration I found the established very powerful and sophisticated tools, were far far harder than home brewing a simple script runner using a hook in Hatchling, which was super easy following Ofek’s suggestion. It should work for anything that needs to be run from the command line.

I’ve used this on a couple of projects now and it works great. The minimal example is something like:

pyproject.toml

[build-system]
requires = ["hatchling", "hatch-requirements-txt"]
build-backend = "hatchling.build"

# ...
# Normal project metadata
# ...

[tool.hatch.build.hooks.custom]
path = "hatch_build.py"

hatch_build.py


import subprocess

# https://discuss.python.org/t/custom-build-steps-moving-bokeh-off-setup-py/16128/3
from hatchling.builders.hooks.plugin.interface import BuildHookInterface


class CustomHook(BuildHookInterface):
    def initialize(self, version, build_data):
        subprocess.run(command)

I found copying in the env was a good idea for what we needed. And unlike on Windows, building on Linux needed shell=True. But I was calling subprocess.run with carefully crafted build commands, hardcoded in string literals, not arbitrary user input.

1 Like