Skip to content

ENH: Replace the shell wheel build system with Python (supersedes #309) - #312

Open
hjmjohnson wants to merge 29 commits into
InsightSoftwareConsortium:mainfrom
BRAINSia:itk6-build-system-v3
Open

hjmjohnson wants to merge 29 commits into
InsightSoftwareConsortium:mainfrom
BRAINSia:itk6-build-system-v3

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Issues closed when this merges:

Partly addresses #310 (stays open): the Sphinx docs/make.bat is removed. make_tarballs.sh and make_windows_zip.ps1 stay, because they enforce the directory layout the ITKPythonBuilds caches must be built at, and the three remote-module entry scripts stay until ITKRemoteModuleBuildTestPackageAction calls build_wheels.py directly.

Related issues that stay open

Replace the shell-based wheel build with a typed pixi and Python build system
for ITK 6. Supersedes #309: the same content, organized for easier review
as 29 commits, the Scientific Python rules first, then one commit per
feature, then two bug fixes (#247, #251) and the link limit (#311). Since #309 closed, six features were added: publish-wheels --repository <pypirc section>, an AGENTS.md guide to building every wheel,
a PyPI project description of the wheels' own in place of ITK's GitHub
README, whose CI badges and relative links are broken on PyPI, compiler
cache keys that no longer depend on the clone or build directory, MSVC
__FILE__ strings relative to the build directories, and CFLAGS, CXXFLAGS,
and LDFLAGS passed to CMake so a reused build tree follows them. Review fixes went
into the commits they belong to: Windows --exclude-libs works again, and
macOS wrappers now get the LTO that CMake's IPO check had silently refused,
and the build no longer lowers a larger ccache size limit configured by the user.
Merge danger: two-way door; blast radius: every ITK Python wheel build (Linux,
macOS, Windows), the release machines, and remote-module CI that calls these
scripts.

Remote-module maintainers: on manylinux, --exclude_libs (-x) is accepted
but has no effect and now says so (ITKIMPACT uses it); on Windows,
-setup_options "--exclude-libs nvcuda.dll" still reaches delvewheel
--no-dll; positional CMake options must be
-D<KEY>=<VALUE>; Intel macOS and i686 hosts are not supported.

Shape of the change
main (69a3a13)
├─ Scientific Python rules, applied to the code on main (7 commits)
│   pre-commit hygiene, ruff-format, and shell hooks · pytest settings,
│   warnings as errors · ruff rule list · codespell, rst, prettier,
│   blacken-docs for .rst · Dependabot and zizmor · strict mypy · nox
└─ Features, in dependency order (19 commits)
    target platform (OS, Arch) → module deps
    → builder hierarchy (BuildConfig, PythonEnvironment, typed build report)
    → build_wheels.py driver and publishing → pixi, CMake, entry points
    → remove the shell build system → docs re-platform
    → ingested remote modules → release-wheels (ReleaseTarget) → dead-strip
    → compiler cache (ccache, sccache) → CONTAINER_DNS
    → reproducible wheels (SOURCE_DATE_EPOCH, /Brepro) → build-tree reuse
    → AGENTS.md: building, uploading, and testing every PyPI wheel
    → PyPI description (scripts/pypi_description.md)
    → cache base dirs (CCACHE_BASEDIR, SCCACHE_BASEDIRS)
    → MSVC -d1trimfile (scripts/msvc_path_trim.py)
    → environment flags as CMAKE_*_FLAGS (scripts/compiler_flags.py)

Builder classes:

BuildPythonInstanceBase (abstract)
├─ PosixBuildPythonInstance
│   ├─ LinuxBuildPythonInstance    auditwheel
│   └─ MacOSBuildPythonInstance    delocate
└─ WindowsBuildPythonInstance      delvewheel

Code arrives in its final form: every commit passes ruff, ruff-format, and
strict mypy as written, and the series has no later fix, rename, or style
commits.

Evidence
  • git diff 6cc5470 <this head> (ENH: Replace the shell wheel build system with Python (supersedes #302) #309's last head) shows the six features
    above and the review fixes: --exclude-libs, the macOS LTO archiver,
    BuildConfig.target, merged report validators, shorter comments, and one
    renamed test fixture.
  • pre-commit run --all-files (ruff, ruff-format, strict mypy, ShellCheck,
    zizmor, and the rest) passes at all 29 commits; the test suite passes at
    all 22 feature and fix commits, 437 tests at the tip (no tests exist before the
    first feature).
  • Refactors were checked by comparison, not by inspection: CMake configure,
    python -m build, and auditwheel/delocate/delvewheel commands, subprocess
    environments for each platform, and the generated pyproject.toml,
    itkVersion.py, and LICENSE for all 8 ITK wheels are unchanged across
    them.
  • Release builds on 2026-11-20, at earlier commits of this work: a warm
    compiler cache saved 35% (Linux x86_64), 34% (Windows), and 26% (macOS
    plus Linux aarch64); reusing the build tree for a one-commit ITK change cut
    a Linux release build from 1,865 s to 957 s; two clean Windows builds gave
    byte-identical wheels.
  • 2026-11-21, at da0aca8: all 32 wheels of 6.0b3.dev0 (ITK 957816d)
    built with release-wheels on four targets, uploaded to
    https://test.pypi.org/project/itk/6.0b3.dev0/, installed from there, and
    passed pip check and a filter and NRRD I/O smoke test on macOS arm64,
    Linux x86_64, Linux aarch64, and Windows x64 (Python 3.11 and 3.13, plus
    3.14 on Windows). Native compiler caches missed from a new clone
    directory (macOS 0%, Windows 36%); the manylinux builds hit 99.9%.
  • 2026-11-22, cache base dirs measured on ITKCommon at ITK 957816d, built
    from directory A and then from directory B with one cache: macOS ccache
    4.14.3 went from 0% hits (150 s) to 99.9% (10 s), with no absolute paths
    left in B's libraries; Windows sccache 0.19.0 with MSVC went from 55% hits
    (129 s) to 99.9% (18 s). ccache makes __FILE__ relative; on Windows,
    -d1trimfile: does (below). At this head, full macOS wheel
    builds from two clones sharing one cache: the second got 3,662 of 3,664
    hits (99.95%), no path of the first clone in its wheels, and passed an
    import and filter test; wall time fell only 20% (6,255 s to 4,994 s),
    since links and the wrapping generators are not cached.
  • 2026-11-22, MSVC -d1trimfile: on coe138 (ITKCommon, A then B, one
    sccache): B hit 99.91% (17.9 s), and strings naming A's directories in
    B's libraries fell from 2,551 to 1,113, one per object (the .obj path
    from /Fo); __FILE__ reads ITK-source\Modules\.... The /d1trimfile:
    spelling made sccache skip every call, so the dash form is used.
  • macOS LTO: with the archiver fix, ITK enables thin LTO for the wrapper modules on macOS as it
    already does on Linux. Cost: build +14% cold (7,145 vs 6,255 s), +5% warm (5,229 vs 4,994 s),
    wheels +1.1%. Runtime, 256^3 float image, best of 3 rounds: within about 5% either way
    (Gaussian 8-9% faster, gradient magnitude 6-14% slower), import time unchanged.
  • A GitHub Actions job runs pytest on Python 3.11 through 3.14.
  • Not tested here: publishing to PyPI itself.
Reverting one feature

git revert of one commit at the tip applies cleanly for: removal of the
shell build system, ingested remote modules, the PyPI description, and
the environment flags. CONTAINER_DNS, reproducible wheels, dead-strip, the
compiler cache, its base dirs, build-tree reuse, and the MSVC trim conflict
where later features share a file (option tables in README.md and the docs,
tests, and the base builder). The earlier commits, target platform through release-wheels,
are built on by everything after them.

hjmjohnson and others added 26 commits November 22, 2026 12:08
Add pre-commit with the file hygiene hooks, ruff (formatter,
pycodestyle, pyflakes, isort, pyupgrade, bugbear), ShellCheck, shfmt,
and taplo, and a root pyproject.toml that configures tooling only. Their
fixes are applied to the existing code. ShellCheck and the ruff findings
that need hand edits skip the superseded build scripts, which later
commits rewrite or remove. CONTRIBUTING.md describes the hooks and the
ITK commit message convention.
Run pytest with strict markers and config, strict xfail, and warnings
as errors, and add the dev dependency group the tests need. PP002 and
PP003 do not apply: the root pyproject.toml configures tooling and
defines no package.
Enable the rule families the guide recommends and fix their findings.
PLR09, PLR2004, T201, and PLC0415 are accepted for build drivers;
ARG001 and ARG002 stay parked because fixing them changes signatures.
Add codespell, the pygrep reStructuredText hooks, prettier for Markdown,
and blacken-docs for the reStructuredText docs, and apply their fixes.
Dependabot groups action updates monthly after a seven-day cooldown,
and zizmor audits every workflow added to the repository.
mypy runs from pre-commit with strict = true, the guide's extra error
codes, and unreachable-code warnings. The superseded build scripts are
skipped until rewritten.
nox runs the hooks, the tests on Python 3.11 to 3.14, and the Sphinx
build, each in its own environment. CONTRIBUTING.md lists the sessions.
TargetPlatform resolves the operating system (OS), the architecture
(Arch, from TARGET_ARCH or the host), and the manylinux version, and
derives from them the pixi environment, compiler triple, wheel tag, and
container image, so every builder takes these from one place.

GitHub Actions runs these first unit tests, and the later ones, with
pytest on Python 3.11 through 3.14.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
Resolve the chain of remote modules a module depends on, fetch each at
its pinned tag, and pin the module's itk requirement to the ITK version
it is built against.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
BuildPythonInstanceBase builds from a BuildConfig and runs the
ITK and wheel build as resumable steps that BuildManager records in
a typed, versioned report; Linux, macOS, and Windows builders supply the
platform parts through hooks, and PythonEnvironment holds the validated
interpreter paths. The builders bound ninja jobs by physical memory, use
Ninja on every platform, can build a remote module against an installed
ITK, and stop at the step that fails. pyproject_configure.py generates
each wheel's pyproject.toml, and wheel_packaging.py repairs and checks
the wheels.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
build_wheels.py validates the options and environment into a
BuildConfig, naming the first setting it cannot use, prepares the ITK
source (falling back to the local revision when tags cannot be
fetched), and runs the builder for the target platform. install_pixi.py
bootstraps pixi, and Utilities/scripts builds and publishes wheels and
the ITK tarball cache.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
pixi.toml defines the build environments and tasks for each platform
and Python version. The CMake files drive the superbuild and the
per-wheel builds, reading wheel module names from itk-module.cmake. The
dockcross, macOS, and PowerShell entry points run the pixi build, and
the shell ones check their required variables with one sourced
function; the manylinux container gets its own pixi environments and
cache, and TARGET_ARCH defaults to the host.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
The pixi and Python build replaces the old CMake superbuild, the shell
and Python build scripts, and the Travis configuration. The lint and
type-check exclusions that covered those scripts go with them.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
Rewrite the README and the Sphinx pages for the pixi build, add the
documentation and pre-commit workflows (read-only permissions, pinned
actions, no persisted token), a pull request template, Read the Docs
configuration, and AGENTS.md for coding agents.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
ITK marks the ingested modules EXCLUDE_FROM_DEFAULT, so the wheels left
them out; -DModule_<name>:BOOL=OFF still turns one off. The filtering
modules they add go into a new itk-filtering-extra wheel, itk-io
requires itk-numerics, and the build fails when a wheel exceeds the
100 MB PyPI limit.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
pixi run release-wheels <itk-ref> [<package-version>] builds every wheel
the host can build: Windows x64 on Windows, macOS arm64 and Linux
aarch64 on an Apple Silicon Mac, and Linux x86_64 or aarch64 on Linux,
the Linux wheels in the manylinux container. Each target is one
ReleaseTarget record. It keeps its own ITK clone and resolves the ref to
one commit for all targets.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
--dead-strip, the default on Linux and macOS, links the Python modules
with section garbage collection and exports only PyInit_*, so the
linker drops the statically linked ITK code a module does not reach.
User -D flags are merged with its flags; --no-dead-strip restores the
previous link.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
compiler_cache.py picks the tool by platform and returns the launcher
definitions, which also reach the superbuild's projects. The pixi
environments install both tools, each manylinux image and architecture
keeps its own cache, and every build reports its hit rate.
--no-use-ccache overrides USE_CCACHE in the environment.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
On some hosts the build container cannot resolve names through the
runtime's default resolver. CONTAINER_DNS takes a space- or
comma-separated list of servers and adds one --dns per entry.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
SOURCE_DATE_EPOCH is set once on the host to the committer time of the
ITK revision and forwarded into the containers, so the zip entries
carry it, and Windows binaries link with /Brepro so the PE timestamps
are a hash of the binary rather than the link time.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
BuildManager records the ITK commit, the package version, and a
fingerprint of the build inputs in the build report, and runs every
step again when any of them changes. Wheels from earlier runs move out
of dist/ before the wheel step, so they are not repaired, tested, or
reported again.

Co-Authored-By: Cavan Riley <cavan-riley@uiowa.edu>
Co-Authored-By: Dženan Zukić <dzenan.zukic@kitware.com>
AGENTS.md now walks a new developer through a full release: which host
builds each of the four targets, the release-wheels commands, where the
wheels land, how to keep compiler-cache rebuilds fast, and how to upload
to TestPyPI and install from it on each platform before publishing.
Every wheel took ITK's README.md as its project page. That file is
written for GitHub: its Azure Pipelines badges point at retired
pipelines (one returns 404, two say "never built"), its NumFOCUS logo
URL returns 404, and its links to CONTRIBUTING.md, GOVERNANCE.md,
CITATION.cff, and itk.bib are relative, so they break on PyPI.

scripts/pypi_description.md now describes the wheels for someone
installing them: what ITK is, how to install it and which packages make
it up, the supported platforms, a quick start, links, citation, and
license. Every link is absolute; the quick start was run against the
6.0b3.dev0 wheels from TestPyPI, and readme_renderer renders the page.
ccache and sccache put the absolute paths of each compile command in
their cache keys, so a cache filled from one clone or build root missed
from another: a release from a fresh macOS clone got 0% hits and a
Windows release outside C:\BDR got 36%.

The build now sets CCACHE_BASEDIR to the common parent of the checkout,
the build root, and the ITK source, or SCCACHE_BASEDIRS to each of them,
unless the caller set one. ITKCommon built from a second directory went
from 0% to 99.9% hits with ccache 4.14.3 (150 s to 10 s), and from 55% to
99.91% with sccache 0.19.0 and MSVC (129 s to 18 s). ccache passes the
compiler relative paths, so __FILE__ no longer records the build
location; sccache normalizes only its hash.

The first build after this change misses existing caches once, because
their keys hold absolute paths.
An sccache hit returns the object the filling build compiled, and with
SCCACHE_BASEDIRS a build in another directory hits it, so its __FILE__
strings named the first build's directories. On Windows, add
-d1trimfile:<root>\ for the checkout, the build root, and the ITK source
(absolute, longest first) to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS.

The flags are CMake definitions, not CFLAGS: CMake reads the environment
only when it creates the cache, so a reused build tree would keep its
old flags. Each starts from the user's -D value, else from CFLAGS or
CXXFLAGS and CMake's MSVC defaults, and a value that already trims is
left alone. The platform builders now supply such flags through one
hook, compiler_flag_definitions(), which the dead-strip flags share.

The dash form is required: sccache 0.19 reads an unknown /option as a
second input file and refuses to cache the call.

Measured on coe138 (MSVC 14.51, sccache 0.19.0), ITKCommon at ITK
957816d built from D:\ExpTrim\A and then D:\ExpTrim\B with one cache:
B hit 1,114 of 1,115 compiles (17.9 s against 140.6 s for A), and the
strings naming A's directories in B's libraries fell from 2,551 to
1,113, one per object: the .obj path cl records from /Fo, which
-d1trimfile does not cover. __FILE__ now reads ITK-source\Modules\...
Existing sccache entries miss once, since the compile flags changed.
CMake reads these variables only when it creates a cache. A reused build
tree (when the build inputs change, every step runs again in the same
tree) kept the flags of its first configure, and ninja recompiled
nothing, so a changed CFLAGS reported "Build inputs changed" and
produced the same objects.

On Linux and macOS, whose compilers have empty CMake flag defaults, the
builders now pass all five CMAKE_*_FLAGS variables from the
environment, the user's -D value winning. They enter the CMake cache on
every configure and the build fingerprint, so a change reconfigures and
recompiles. On Windows the C and C++ flags already arrive this way with
the -d1trimfile prefixes; LDFLAGS stays out, since a definition would
replace CMake's /machine:x64 default.

The environment-to-CMake map and user_flag_key move from dead_strip.py
to compiler_flags.py, which dead stripping and the MSVC trim share.
@hjmjohnson
hjmjohnson marked this pull request as ready for review November 22, 2026 21:26
@hjmjohnson

Copy link
Copy Markdown
Member Author

@dzenanz This is a complete rewrite. I also applied the scientific Python best practices rules to this repo.

I have been able to build Python packages and push to TestPyPI for macOS, Linux x86_64, Linux ARM64, and Windows, and tested against Python 3.11 and 3.13 environments.

@hjmjohnson hjmjohnson self-assigned this Nov 22, 2026
The module wheel was built into the module's dist/ and every wheel found
there was verified and repaired, so wheels left by an earlier build were
repaired again or failed the abi3 check (issue InsightSoftwareConsortium#247). Build into a fresh
directory inside dist/, move the produced wheels into dist/, and repair
only those.
GitHub release downloads of the ITKPythonBuilds archive sometimes end
prematurely, and the module build scripts tried once (issue InsightSoftwareConsortium#251). The
Windows, manylinux, and macOS scripts now retry up to five times with a
growing delay, download to a .part file that is renamed only on success
so a partial archive is never taken for a cached one, and pass curl -f
so an HTTP error page is not saved as the archive.
Every ITK wrapper module links with GCC LTO, and CMake's IPO option
-flto=auto starts one LTRANS process per hardware thread in each link,
so -j48 with tens of modules ready ran hundreds of processes (issue InsightSoftwareConsortium#311).

Add CMAKE_JOB_POOLS compile=<jobs>;link=<links> to every Ninja build
(superbuild, ITK, wheels, remote modules). Links default to jobs / 4,
at most one per 8 GB, overridable with ITK_BUILD_LINK_JOBS (0 disables).
On manylinux, append -flto=<jobs / links> to the shared and module
linker flags; GCC honors the last -flto=, which follows CMake's. The
manylinux scripts now forward ITK_BUILD_JOBS, ITK_BUILD_LOAD_LIMIT, and
ITK_BUILD_LINK_JOBS into the container.

Measured on a 48-thread, 251 GB host, manylinux_2_28 x64, warm ccache,
all wrapper modules relinked, release-wheels wall time:
  no pool:              1039 s, load1 peak 261, container mem peak 33.2 GB
  pool 12, -flto=4:      980 s, load1 peak  56, container mem peak 14.0 GB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant