/tiran-skills:port-to-meson-pythonfrom tiran-skills
Builds Python packages with compiled C/C++/Cython/Fortran/Rust extensions using the meson-python PEP 517 backend, or ports from setuptools setup.py. Covers pyproject.toml, meson.build, VCS versioning, editable installs, config-settings, and abi3 wheels.
Ships in tiran-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Status: Experimental — distilled from a real shipping project (pycxxfilt,
see reference/example.md) plus the upstream docs, but the workflow itself is a
new draft. Follow it, adapt, and have a human review the wheel on every target
platform.
meson-python is a PEP 517 backend that turns a Meson build into wheels and
sdists. It is the Meson-based sibling of scikit-build-core (which uses CMake);
reach for it when the native build is (or should be) Meson. Two entry points,
one destination — a declarative pyproject.toml + meson.build:
pyproject.toml + meson.build from
scratch (steps 2–5, 8).setup.py (setuptools
Extension, MANIFEST.in, custom build_ext) by translating it to Meson
(adds step 6), then delete the old build (step 11).build_ext.pip, build, uv,
cibuildwheel all target it directly.limited-api = true) — one wheel across
Python versions.Which backend? meson-python if the project uses/wants Meson;
port-to-scikit-build-core if it uses/wants
CMake (e.g. it consumes a CMake package like find_package(Torch)). Same
PEP 517/621 shape either way.
Authoritative sources (if this skill disagrees with them, they win):
[tool.meson-python] reference —
https://mesonbuild.com/meson-python/reference/pyproject-settings.htmlGuardrails. Read
../GUARDRAILS.mdand follow it unless the user says otherwise: work in a project-local.venvwith uv (never global/user site-packages); don't delete content or commit/push without approval (never straight tomain); ask before installing heavy packages or starting a heavy compile (torch/CUDA are multi-GB); match the project's existing style and keep comments/docstrings terse.
First, check upstream for prior work (../GUARDRAILS.md →
Prior work): someone may already have a meson-python branch, PR, or a discussion
about why it stalled. Find the upstream repo (git remote -v, [project.urls])
and search its issues + PRs (open/merged/closed) for meson, meson-python,
pyproject, scikit-build. Report what you find before porting.
Then inventory locally:
ls setup.py setup.cfg pyproject.toml MANIFEST.in meson.build 2>/dev/null
grep -nE 'Extension|ext_modules|build_ext|cmdclass|package_data' setup.py 2>/dev/null
No setup.py → Track N. A setup.py with Extension(...) / custom
build_ext → Track P; inventory what it does imperatively (generated files,
include dirs, link libs, compiler flags, data files, version source). Confirm
Meson is the right native tool (see "Which backend?" above).
[build-system]
build-backend = "mesonpy"
requires = ["meson-python>=0.17", "meson>=1.6"]
Add any tools the build needs (Cython, a VCS-version tool, numpy for headers)
to requires.
[project], PEP 621)meson-python fills name/version from project() in meson.build by default;
richer metadata lives in [project] (description, readme, license +
license-files, requires-python, dependencies, classifiers,
[project.urls], [project.scripts]). If the version comes from VCS, declare it
dynamic and compute it in meson.build (step 5):
[project]
name = "mypkg"
dynamic = ["version"]
meson.buildMinimal native module (from the tutorial):
project('mypkg', 'c',
meson_version: '>=1.6.0',
version: '0.1.0', # or dynamic — see step 5
)
py = import('python').find_installation(pure: false) # pure:false => platlib
py.extension_module('_core', '_core.c',
install: true,
subdir: 'mypkg', # install into the package dir
)
py.install_sources( # pure-Python sources
'mypkg/__init__.py', 'mypkg/api.py',
subdir: 'mypkg',
)
Only installed targets land in the wheel — this is the packaging contract.
Use install_subdir for a whole tree, subdir: to place artifacts in the right
package. See reference/meson-cookbook.md for dependencies, generators, Cython,
and the setuptools→Meson mapping.
Strongly prefer a src/ layout — keep sources under src/mypkg/ and install
into subdir: 'mypkg' (reference the files as src/mypkg/...). Without it the
project root is on sys.path, so tests and tools import from your checkout, not
the installed wheel — precisely the packaging bugs meson-python's
"only installed targets ship" contract is meant to expose (a source file never
listed in install_sources, a missing sub-package, data files left out) stay
hidden until a user hits them. A src/ layout forces imports through the built
artifact, so build-install-test catches them in CI. Endorsed by the Scientific
Python guide, pyOpenSci, and Hynek Schlawack. Do not move files into src/
automatically — a relayout rewrites paths and can obscure git blame/log
across the renames. Recommend it, and only do it if the user asks; when they
do, use git mv in a dedicated, rename-only commit so history follows.
A static version = "…" in project() is perfectly valid. Deriving it from the
git tag is optional, but useful — one source of truth, no stale version edits.
Unlike scikit-build-core (which has a
dynamic-metadata provider
system), meson-python reads the version straight from project(... version: ...), so the natural place to derive it is at configure time inside
meson.build. The pattern from pycxxfilt (reference/example.md), using the
vcs-versioning tool:
project('mypkg', 'c',
meson_version: '>=1.6.0',
# Derive the version from the latest git tag; falls back to the sdist's
# PKG-INFO outside a git checkout. find_program picks meson-python's build
# interpreter (which has the versioning dep) over a bare PATH python3.
version: run_command(
find_program('python3', 'python', version: '>= 3.11'),
'-m', 'vcs_versioning',
check: true,
).stdout().strip(),
)
[project]
dynamic = ["version"] # required: version not static
[build-system]
requires = ["meson-python>=0.17", "meson>=1.6", "vcs-versioning>=2.3.2"]
Two things make this robust: run_command(..., check: true) fails the build
loudly if versioning breaks, and the sdist ships PKG-INFO, so the same
meson.build resolves a version when built from an sdist with no .git. (Any
tool that prints a version to stdout works here; setuptools-scm has a
--version-style entry point too.)
Extension(...) → MesonTranslate the setuptools build into meson.build:
| setuptools | Meson |
|---|---|
Extension(sources=[...]) | py.extension_module('_x', ['a.c', ...]) |
include_dirs | include_directories(...) → include_directories: kwarg |
libraries / library_dirs | dependency(...) / cc.find_library(...) |
define_macros | c_args: ['-DFOO=1'] |
extra_compile_args | c_args: / cpp_args: |
package_data / MANIFEST.in | install_sources / install_subdir / install_data |
| generated files (Cython, protobuf) | generator() / custom_target() |
Prefer dependency('foo') (pkg-config/CMake discovery) over hard-coded paths.
Details and examples: reference/meson-cookbook.md.
[tool.meson-python] and passing Meson argsBackend options live under [tool.meson-python]; forward args to the Meson
stages there or per-build via config-settings:
[tool.meson-python.args]
setup = ["-Dfeature=enabled"] # meson setup
compile = ["-j8"] # ninja
pip install . -Csetup-args=-Dfoo=bar -Ccompile-args=-j8 # one -C per arg
Keys: args.setup / args.compile / args.install / args.dist, plus
limited-api, editable-verbose, wheel.exclude/wheel.include. Full table:
reference/meson-cookbook.md.
pip install meson-python meson ninja # build deps present
pip install --no-build-isolation --editable . # rebuilds on import
uv build # sdist + wheel (or: python -m build)
--no-build-isolation is required: rebuild-on-import needs the build deps at
runtime. The build dir is named per ABI tag (build/cp311/), so multiple
interpreters coexist. MESONPY_EDITABLE_VERBOSE=1 (or
-Ceditable-verbose=true) shows rebuild output. Caveat: don't locate data
files with __file__ under an editable install — use importlib.resources.
Metadata changes (deps, entry points) need a reinstall.
Gotcha — the sdist packages the git commit, not your working tree. meson-python builds the sdist by running
meson dist --allow-dirty, which archives the latest committed revision. Uncommitted edits and untracked files are silently excluded —--allow-dirtysuppresses the usual dirty-tree error, so there is no warning. Becauseuv build/python -m buildbuild the wheel from that sdist, the released wheel reflects only committed state — so commit before youuv build, tag, or release. (Direct wheel builds —pip install .,pip install -e .— use the working tree instead and do see local changes, so the editable dev loop is unaffected; the trap only bites at sdist/uv build/release time.) Record this in the project'sAGENTS.mdand its developer/release docs (e.g.CONTRIBUTING.md, a RELEASING checklist) so contributors commit before cutting a release and don't chase "my fix isn't in the wheel" bugs.
Three coordinated pieces (per the limited-API guide):
py.extension_module('_core', '_core.c',
limited_api: '3.11', # sets Py_LIMITED_API; oldest supported minor
install: true, subdir: 'mypkg',
)
[tool.meson-python]
limited-api = true # wheel ABI tag becomes abi3
Make it opt-in and free-threading-safe by adding
default_options: ['python.allow_limited_api=false'] to project(), then enable
per build: -Csetup-args=-Dpython.allow_limited_api=true. Free-threaded CPython
(3.13/3.14) does not support the limited API — keep it off there. The wheel's
Python tag follows the build interpreter, so build on the oldest CPython you
support (e.g. cp311-abi3). pycxxfilt does exactly this (reference/example.md).
Drive cibuildwheel from pyproject.toml. For abi3, build the stable-ABI wheel
only on your floor Python and audit it:
[[tool.cibuildwheel.overrides]]
select = "cp311-*"
config-settings = { setup-args = "-Dpython.allow_limited_api=true" }
Add abi3audit (abi3) or auditwheel/delocate (manylinux/macOS) as the gate.
For the publish/release half — building wheels from the sdist, Trusted Publishing
to PyPI, hardened permissions — see
secure-python-release-pipeline.
uv build → sdist and wheel, no setup.py
involved. uv build (like python -m build) builds the wheel from the sdist
by default, so this already exercises the VCS-version fallback to PKG-INFO
(step 5) and catches files missing from the sdist.pip install dist/*.whl in a clean env; import mypkg; run the tests.setup.py, setup.cfg, MANIFEST.in; update docs/CI that called
python setup.py.Definition of done: uv build yields an installable sdist + wheel
with no setup.py (the wheel built from that sdist); the package imports and
tests pass; editable install rebuilds on import; and, if targeted, one abi3 wheel loads
across Python versions.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
meson-python, meson-build, python-packaging, pep-517, compiled-extensions, porting
Other plugins' skills for the same job
12 skills in this plugin
/bootstrap-python-projectScaffold a new Python project from zero: src/ layout, PEP 621 metadata, a chosen build backend (hatchling for pure Python; meson-python / scikit-build-core / maturin for compiled) with dynamic VCS versioning, py.typed, ruff, a type checker, pytest + coverage, tox or nox via uv, and hardened GitHub Actions CI (SHA-pinned, Trusted Publisher, CodeQL/Scorecard/Dependabot). Always writes a README, LICENSE, and .gitignore. Orchestrates the choose-python-build-backend, modernize-python-metadata, ship-type-information, and secure-python-release-pipeline skills. Use when asked to start a new package, scaffold/bootstrap a Python project, set up a repo from zero, turn a loose script into a proper package, or bring an existing project up to this standard (adopt the layout, metadata, tooling, or CI it lacks)./choose-python-build-backendRecommend the right PEP 517 build backend for a new or existing Python package, and give enough context to migrate. Chooses by three axes: purelib (pure Python) vs platlib (compiled C/C++/Rust/Torch), static vs dynamic/VCS metadata, and native language/build system. Covers uv-build, flit-core, hatchling (incl. compiled via scikit-build-core[hatchling] / a custom build hook), meson-python, scikit-build-core, maturin, and setuptools (legacy). Weighs compiled-project concerns: Cython, PyTorch extensions, pybind11/nanobind bindings, the stable Python ABI (abi3 / limited API), and fast parallel builds (CMake+Ninja, Meson). Use when asked "which build backend should I use", to pick a backend for a new project, or to decide whether to move an existing project off setuptools.npx claudepluginhub tiran/agent-skills --plugin tiran-skills