Skip to content

Python Build - #292

Open
johannes-spies wants to merge 18 commits into
metatomic-corefrom
metatomic-core-python-build
Open

Python Build#292
johannes-spies wants to merge 18 commits into
metatomic-corefrom
metatomic-core-python-build

Conversation

@johannes-spies

@johannes-spies johannes-spies commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

This PR adds the infrastructure needed to build the metatomic_core Python wheel. Essentially, it's a port of the same functionality in metatensor (setup.py, CMakeLists.txt, and package-core.sh). The only difference is that we import metatensor in cmake_ext's run method to get the cmake_prefix_path which is required to build the native library.

The PR also makes an important change to the CI setup: now, containers for testing are built with Rust, similar to what metatensor does.

📚 Download documentation for this pull-request

⚙️ Download Python wheels for this pull-request (you can install these with pip)

@johannes-spies
johannes-spies changed the base branch from main to metatomic-core July 21, 2026 09:17
@johannes-spies
johannes-spies requested a review from Luthaf July 22, 2026 09:02

@Luthaf Luthaf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, I'm not convinced we should switch to uv in CI, and we should for sure do it in a separate PR anyway

Comment thread .github/workflows/build-wheels.yml Outdated
Comment thread .github/workflows/torch-tests.yml Outdated
Comment thread .github/workflows/torch-tests.yml Outdated
Comment thread python/metatomic_ase/setup.py Outdated
Comment thread python/metatomic_core/CMakeLists.txt Outdated
Comment thread python/metatomic_torch/setup.py Outdated
Comment thread python/metatomic_torchsim/setup.py Outdated
Comment thread python/scripts/gcc11-manylinux_2_28_aarch64/Dockerfile
Comment thread scripts/git-version-info.py
@johannes-spies

Copy link
Copy Markdown
Contributor Author

Thank you for the review! You're right, the switch to uv was not necessary and I will revert it. I just tried to make the CI as similar as possible to track down the failure in the windows tests.

@johannes-spies
johannes-spies requested a review from Luthaf July 22, 2026 23:09

@Luthaf Luthaf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wheel (as created by python -m build python/metatomic_core) contains configuration files to find nlohmann::json, which I don't think we want to make available through the to external users here.

[...]
adding 'metatomic/share/cmake/nlohmann_json/nlohmann_jsonConfig.cmake'
adding 'metatomic/share/cmake/nlohmann_json/nlohmann_jsonConfigVersion.cmake'
adding 'metatomic/share/cmake/nlohmann_json/nlohmann_jsonTargets.cmake'
adding 'metatomic/share/pkgconfig/nlohmann_json.pc'
[...]

Ideally they should not even be installed by the main metatomic CMakeLists.txt (this is not specific to Python) when we fetch the code from github ourself.

Can you have a look?

Comment on lines +80 to +85
- name: setup rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
target: ${{ matrix.rust-target }}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be required, Rust is only used inside the container

Comment on lines +94 to +96
- name: Set up Docker Buildx
if: matrix.os == 'ubuntu-24.04' || matrix.os == 'ubuntu-24.04-arm'
run: |
docker buildx build \
-t gcc11-manylinux_2_28_${{ matrix.cibw-arch }} \
python/scripts/gcc11-manylinux_2_28_${{ matrix.cibw-arch }}
uses: docker/setup-buildx-action@v4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed now when it worked without it before?


- name: build manylinux with rust docker image
if: matrix.os == 'ubuntu-24.04' || matrix.os == 'ubuntu-24.04-arm'
uses: docker/build-push-action@v7

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use a custom action?

@johannes-spies

Copy link
Copy Markdown
Contributor Author

Yes, you're right. 27675fd sets JSON_Install to OFF.

@Luthaf

Luthaf commented Jul 27, 2026

Copy link
Copy Markdown
Member

We still need to install the headers, just not the cmake config / .pc files

johannes-spies and others added 18 commits July 28, 2026 12:49
metatomic-core now actually compiles via cargo (see previous commit),
but the manylinux Docker images used for Linux wheel builds never
installed Rust. Mirrors metatensor's rustc-manylinux_2_28_* setup:
renamed Dockerfiles, added rustup install, and switched to
dtolnay/rust-toolchain + uv + docker/build-push-action (with GHA
layer caching) to match.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…p.py

This was inconsistent with metatomic_torch's ROOT-anchored implementation
and is suspected to cause the Windows torch-tests/docs-tests failures
where the python package version and the compiled C++ library version
disagree.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The Rust test harness in metatomic-torch/tests/utils/mod.rs already
prefers `uv pip install` over plain pip when `uv` is available on PATH,
but the workflow never installed it, so it always fell back to plain
pip. metatensor hit the same version-mismatch failure on Windows and
fixed it by installing uv in CI (originally as a speed optimization),
which this mirrors.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
PIP_EXTRA_INDEX_URL only affects pip; uv needs its own env var, or it
falls back to PyPI's default CUDA build, which fails to configure on
runners without CUDA. Matches metatensor's setup.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
`git_version_info()` treated any output on stderr as a failure and fell
back to `n_commits = 0`, while the CMake side only warned and kept using
the result. On Windows, `git add --all` emits `LF will be replaced by
CRLF` warnings (`* text=auto`), so the Python package was built as
v0.1.16 while the C++ library was v0.2.0-dev51, and importing
metatomic_torch failed the version compatibility check.

Detect the early-exit case from the shape of stdout instead, and keep
forwarding warnings to stderr.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
`git add --all` is only used here to stage files in a temporary index so
we can compute a hash of the working tree; the files are never written
back. On Windows this still triggers git's `core.safecrlf` check, which
warns once per file (~300 lines per invocation) about LF being replaced
by CRLF. Disable the check for this call.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The uv changes were collateral from porting metatensor's workflow, and
the torch-tests ones were a failed attempt at fixing the Windows version
mismatch -- the actual fix was in `git_version_info()`. Reverting them
here as requested; uv can be revisited in a separate PR.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Guillaume Fraux <guillaume.fraux@epfl.ch>
Co-authored-by: Guillaume Fraux <guillaume.fraux@epfl.ch>
Co-authored-by: Guillaume Fraux <guillaume.fraux@epfl.ch>
…files

JSON_Install=OFF stopped installing nlohmann_json entirely, including the
headers metatomic's own public API needs (metadata.hpp, system.hpp), which
broke find_package(metatomic) for downstream consumers since
metatomic-config.cmake still called find_dependency(nlohmann_json).

Headers are installed into the same include dir as metatomic's own, so
downstream consumers don't need to locate a separate nlohmann_json package
for them. find_dependency/linking is now only done when nlohmann_json was
found as a system package rather than vendored by us.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@johannes-spies
johannes-spies force-pushed the metatomic-core-python-build branch 2 times, most recently from 765ae6c to 8dc3972 Compare July 28, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants