/tiran-skills:port-to-scikit-build-corefrom tiran-skills
Migrate a Python package with compiled C/C++/CUDA extensions from setuptools/setup.py to scikit-build-core + CMake (PEP 517/518/621). Covers pyproject.toml, CMakeLists install rules, dynamic versioning, editable installs, wheels/CI, and optional abi3 via nanobind.
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 — this workflow is distilled from two real but
experimental migrations (see reference/examples.md); the reference branches
are not yet merged. Follow it, but expect to adapt, and have a human review the
build on every target platform.
Move a package from hand-written setup.py (setuptools Extension /
CUDAExtension, MANIFEST.in, custom build_ext) to a declarative
pyproject.toml where CMake builds the native code and scikit-build-core
turns it into wheels and sdists. Work in the order below.
setuptools was never a native-build system; every hybrid project reinvents the same glue. scikit-build-core is the maintained, standards-based PEP 517 backend purpose-built for the CMake+Python hybrid. Concretely you gain:
pyproject.toml config. setup.py goes away.find_package, incremental rebuilds — the CMake cache and
ccache/sccache carry over between builds.-Ccmake.define.USE_CUDA=ON; no bespoke --global-option hacks.Rationale and quotes: reference/benefits.md. PyTorch's own switch (setup.py
removed 2026-07-20): https://dev-discuss.pytorch.org/t/3414.
Authoritative sources (if this skill disagrees with them, they win):
FindPython
(https://cmake.org/cmake/help/latest/module/FindPython.html) and install
(https://cmake.org/cmake/help/latest/command/install.html)find_package(Torch)
(https://docs.pytorch.org/cppdocs/installing.html) and
torch.utils.cpp_extension (https://docs.pytorch.org/docs/stable/cpp_extension.html)Guardrails. 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 scikit-build-core / CMake 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
scikit-build, cmake, pyproject, meson. Report what you find before porting.
Then inventory the current build:
ls setup.py setup.cfg pyproject.toml MANIFEST.in CMakeLists.txt 2>/dev/null
grep -nE 'Extension|CUDAExtension|CppExtension|build_ext|cmdclass|ext_modules' setup.py
find . -name CMakeLists.txt
Classify into one of two tracks — the destination (steps 2–11) is identical; only steps 1 and 4 differ:
setup.py glue (PyTorch, coremltools). The
native build already lives in CMakeLists.txt; setup.py only orchestrates it.
Easiest path: keep the CMake build, add install() rules (step 4), delete the
glue, point scikit-build-core at it.cpp_extension only, no CMake (CppExtension /
CUDAExtension in setup.py, no CMakeLists.txt). You must author a
CMakeLists.txt (step 4) that reproduces what the Extension(...) implied:
sources, torch headers/libs, include dirs, defines, and compiler flags.Note anything the old build did imperatively: generated files (protobuf, version),
vendored deps, data files, package_data, custom compiler flags.
[build-system]
requires = ["scikit-build-core>=1.0"] # add nanobind / pybind11 / numpy / torch as needed
build-backend = "scikit_build_core.build"
Everything else lives under [project] (PEP 621 metadata) and [tool.scikit-build].
[project] (PEP 621)Translate setup(name=…, version=…, install_requires=…, packages=…) into a
[project] table: name, description, readme, authors, license +
license-files, requires-python, dependencies, classifiers,
[project.urls]. If the version is derived from git, mark it dynamic (step 6).
CMakeLists.txtThe contract with scikit-build-core is simple: whatever CMake install()s lands
in the wheel.
Track A (existing CMake): you likely only need to add the install rules —
point each existing target at its package dir (install(TARGETS foo LIBRARY DESTINATION mypkg)) and make find_package(Python …) use Development.Module.
Skip the target-authoring below.
Track B (torch cpp_extension → CMake): author the target. Minimum viable native module:
cmake_minimum_required(VERSION 3.18)
project(mypkg LANGUAGES CXX)
# scikit-build-core supplies the hints; use the modern FindPython.
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
# pick ONE binding path:
# pybind11: find_package(pybind11 CONFIG REQUIRED); pybind11_add_module(_C src...)
# nanobind: find_package(nanobind CONFIG REQUIRED); nanobind_add_module(_C src...)
# plain: Python_add_library(_C MODULE src...)
Python_add_library(_C MODULE src/mymod.cpp WITH_SOABI)
# The extension must be installed INTO the package dir so it ships in the wheel.
install(TARGETS _C LIBRARY DESTINATION mypkg)
Details in reference/examples.md (real coremltools + nanobind CMake). Common
Extension(...) → CMake mappings: sources → target sources; include_dirs →
target_include_directories; define_macros → target_compile_definitions;
libraries/library_dirs → target_link_libraries / find_package;
extra_compile_args → target_compile_options. For CUDA add
LANGUAGES CXX CUDA and find_package(CUDAToolkit REQUIRED).
Track B, torch specifics. CppExtension/CUDAExtension silently injected
torch's include dirs, libraries, ABI flags, and a -DTORCH_EXTENSION_NAME. In
CMake, reproduce them with torch's own CMake package:
# expose torch's CMake config: -Ccmake.define.CMAKE_PREFIX_PATH=$(python -c
# 'import torch;print(torch.utils.cmake_prefix_path)') — or find it in CMake.
find_package(Torch REQUIRED)
target_link_libraries(_C PRIVATE ${TORCH_LIBRARIES}) # classic ABI: link torch
target_compile_definitions(_C PRIVATE TORCH_EXTENSION_NAME=_C)
If you are also porting to the stable ABI (port-to-torch-stable-abi), do
not link torch: use find_package(Torch REQUIRED) for headers/flags only,
drop ${TORCH_LIBRARIES}, and add -DTORCH_TARGET_VERSION=… — the kvcached
nanobind branch in reference/examples.md is exactly this case.
[tool.scikit-build][tool.scikit-build]
minimum-version = "1.0"
build-dir = "build/{wheel_tag}" # per-tag cache dir → fast incremental rebuilds
cmake.version = ">=3.18"
cmake.build-type = "Release"
wheel.packages = ["mypkg"] # pure-Python sources collected from the tree
Pass CMake options at build time instead of editing files:
-Ccmake.define.USE_CUDA=ON, -Ccmake.args=-DFOO=BAR.
Strongly prefer a src/ layout (src/mypkg/, wheel.packages = ["src/mypkg"] — scikit-build-core auto-detects src/). Without it, the project
root is on sys.path, so tests and tools import from your checkout instead of
the installed wheel — exactly the packaging bugs this migration is meant to
catch (a compiled module not installed into the package, a missing sub-package,
data files left out of the wheel) stay hidden until a user hits them. A src/
layout forces every import to go through the built artifact, so uv build
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.scikit-build-core has a dynamic-metadata
system: any PEP 621 field listed in dynamic can be filled at build time by a
provider (built-ins live under scikit_build_core.metadata.*). A static
version = "…" is perfectly valid; deriving it is optional, but it keeps one
source of truth and avoids stale edits. Two common scenarios:
A. From VCS (git tags), via setuptools_scm. Add "setuptools-scm>=8" to
build-system.requires.
[project]
dynamic = ["version"]
[[tool.dynamic-metadata]]
provider = "scikit_build_core.metadata.setuptools_scm" # version-specific: no `field` key
[tool.setuptools_scm]
# optional: also write the resolved version to an importable file
version_file = "mypkg/_version.py" # generated at build time (gitignore it)
If you set version_file (optional — omit it if the package doesn't need
__version__ at runtime), the file is gitignored, so force-include it (step 7)
or scikit-build-core will skip it.
B. From a static file already in the tree, via the regex provider. Keep the
version in e.g. mypkg/__init__.py and read it out — no setuptools-scm, no git
needed.
[project]
dynamic = ["version"]
[[tool.dynamic-metadata]]
field = "version"
provider = "scikit_build_core.metadata.regex"
input = "mypkg/__init__.py" # regex defaults to __version__ = "…"
Pick A when the git tag is the source of truth (releases from CI), B when a
checked-in file is. Other built-in providers: fancy_pypi_readme (render the
README) and template (compose one field from others).
scikit-build-core honors .gitignore when collecting package files. For
generated-but-shipped files (protobuf *_pb2.py, version.py) use:
[tool.scikit-build.wheel.force-include]
"mypkg/version.py" = "mypkg/version.py"
Control the sdist contents under [tool.scikit-build.sdist] (include/exclude);
the sdist is built from the git tree, which must contain the CMake build system and
all C++/CUDA sources.
pip install scikit-build-core cmake ninja # build deps present for --no-build-isolation
pip install -e . --no-build-isolation # editable dev install
pip install -e . --no-build-isolation -Ceditable.rebuild=true # auto-rebuild on import
uv build --wheel --no-build-isolation # build a wheel (or: python -m build --wheel --no-isolation)
--no-build-isolation needs the backend and native toolchain already in the
env (first line); the editable auto-rebuild also needs them present at import
time. Replace old python setup.py … invocations: develop → editable install above;
bdist_wheel → uv build (or python -m build); install → pip install ..
If you also want one wheel across Python versions (abi3), nanobind is the natural
fit and is CMake-native — this is where this skill meets
port-to-torch-stable-abi (step 13/17). Pattern (kvcached nanobind branch):
nanobind_add_module(_C STABLE_ABI NB_STATIC ${SOURCES}) # STABLE_ABI => .abi3.so on ≥3.12
install(TARGETS _C LIBRARY DESTINATION mypkg)
[tool.scikit-build]
wheel.py-api = "cp312" # emit cp312-abi3 on 3.12+; per-version wheels below
Disable abi3 on free-threaded builds before CPython 3.15 (pip refuses abi3
there). From 3.15, the combined abi3.abi3t tag (wheel.py-api = "cp315.cp315t", CMake ≥4.4) ships one wheel for regular and free-threaded
Python — see PEP 803 and the full abi3/abi3t
model in
port-to-python-limited-api.
pybind11 is not abi3-capable; use nanobind or a hand-written PyType_FromSpec
module.
Drive cibuildwheel from pyproject.toml ([tool.cibuildwheel]): select
Python versions, test-requires, and an audit gate (e.g. abi3audit for abi3
wheels, auditwheel/delocate for manylinux/macOS). CMake caching + ccache
still applies inside the CI containers. For the publish/release half — building
wheels from the sdist, Trusted Publishing to PyPI, hardened permissions — see
secure-python-release-pipeline.
uv build → produces both sdist and wheel with no setup.py. uv build
(like python -m build) builds the wheel from the sdist by default, so this
already catches a broken sdist — missing CMake sources, or a VCS-version
fallback to PKG-INFO — before you install anything.pip install dist/*.whl in a clean env; import mypkg; run the test suite.setup.py, setup.cfg (if fully migrated), and MANIFEST.in; update
BUILDING.md/docs and any python setup.py calls in scripts/CI.Definition of done: uv build yields installable sdist + wheel with no
setup.py; the package imports and tests pass from the wheel; editable install and
incremental rebuild work; docs/CI no longer invoke setup.py.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub tiran/agent-skills --plugin tiran-skills12 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./crypto-fips-auditAudit a Python package — and any C/C++/Go/Rust code it ships — for its use of cryptography, across three use cases: inventory what crypto a package uses at all (algorithms, providers, linkage, RNG, CA stores), find insecure or weak cryptography, and assess FIPS 140-3 / NIST compliance. Flags insecure use of approved primitives (AES-ECB, RSA PKCS#1 v1.5, static IV/nonce, `random` for secrets, timing-unsafe compares, disabled TLS verification), weak RSA/EC key sizes, and — for FIPS — refused hashes (MD5, MD4, RIPEMD-160), restricted ones (SHA-1), non-approved primitives (BLAKE2/3, ChaCha20-Poly1305, 3DES, scrypt/Argon2/bcrypt, X25519/X448 key agreement, secp256k1), vendored or statically linked crypto (bundled OpenSSL/BoringSSL/AWS-LC/NSS/GnuTLS/libgcrypt/ nettle/libsodium) that escapes the system validated module, hardcoded TLS versions/ciphers that bypass crypto-policies, bundled CA trust stores, and problematic PyPI deps (bcrypt, pycryptodome, pynacl, ecdsa, rsa, m2crypto, …). Source-first, then confirmed against the built wheel/binaries. Gathers evidence; does not certify compliance./modernize-python-metadataImprove a Python package's metadata and move it into a PEP 621 [project] table in pyproject.toml — filling in what's missing along the way. Covers the readme field (dropping the open().read() hack), SPDX license expressions, trove classifiers, well-known project URLs, authors/keywords/requires-python, dependency version constraints (avoid upper caps), PEP 735 dependency groups vs extras, and the build-system table. Keeps the existing build backend (setuptools is fine). Includes a Poetry → PEP 621 migration lens (the [tool.poetry] field map and caret/tilde → floor translation; PDM too). Use when asked to clean up packaging metadata, move metadata out of setup.py/setup.cfg, migrate a Poetry (or PDM) project to [project]/PEP 621, or improve a project's PyPI page./port-to-free-threaded-pythonMake a Python package work correctly on the free-threaded (no-GIL) CPython build (PEP 703; the cp313t/cp314t "t" ABI) and declare that it does, so importing it does not silently re-enable the GIL. Covers the pure-Python thread-safety pass (mutable global/class state, caches, contextvars vs threading.local) and, for C/C++/Cython/pybind11/nanobind/Rust-PyO3 extensions, declaring support (Py_mod_gil / PyUnstable_Module_SetGIL / the per-binding flag), fixing data races with critical sections (Py_BEGIN_CRITICAL_SECTION), PyMutex, atomics and thread-local state, the borrowed-reference hazards and their strong-reference replacements (PyList_GetItemRef, PyDict_GetItemRef), building cp3Xt wheels (meson-python, scikit-build-core, maturin, setuptools, cibuildwheel), and testing under load with ThreadSanitizer and pytest-run-parallel. Use to add free-threading / no-GIL / nogil / free-threaded / PEP 703 support, ship a cp313t/cp314t wheel, or stop the GIL being re-enabled. For a single stable-ABI abi3t wheel (3.15+) pair it with port-to-python-limited-api./port-to-meson-pythonBuild a Python package (usually with a compiled C/C++/Cython/Fortran/Rust extension) with the meson-python PEP 517 backend, or port one off a bespoke setuptools setup.py. Covers the pyproject.toml backend, the meson.build (project / extension_module / install_sources), VCS/dynamic versioning computed in meson.build, editable installs, config-settings, and abi3 / limited-API wheels. Use when asked to adopt meson-python or Meson, scaffold a new meson-python project, drop setup.py, or produce standards-based wheels.