Skip to content

Add library-source modes and read symbols from the binary - #94

Merged
python-templates-automerge[bot] merged 5 commits into
mainfrom
split-library
Nov 10, 2026
Merged

python-templates-automerge[bot] merged 5 commits into
mainfrom
split-library

Conversation

@timkpaine

Copy link
Copy Markdown
Member

Description

Symbol validation now reads the export table of the copied ELF, Mach-O, PE, or WebAssembly file (hatch_rs/symbols.py, no dependencies) instead of loading it with ctypes.CDLL, so expected-symbols and header-symbol-prefix work for cross builds and Pyodide. Only validate = true still loads the library.

A Python extension artifact can link a cdylib artifact through links. library-source (or HATCH_RS_LIBRARY_SOURCE) selects bundled, external, static, or direct:

  • bundled builds the library, gives it -soname libfoo.so on Linux or an @rpath/libfoo.dylib install name on macOS (rustc sets neither), and links the extension with -L/-l dylib plus an rpath from the extension to the library ($ORIGIN/lib, @loader_path/lib). Windows packages the import library.
  • external skips the build and resolves the library from HATCH_RS_LIBRARY_ROOT, library-root, pkg-config, or $PREFIX/$CONDA_PREFIX, adds no rpath, and still runs header, symbol, and validation-command checks against it. The artifact manifest records it as external-library.
  • static and direct skip the library and link nothing.
  • library-source-features maps each mode to the extension crate's Cargo features, since Cargo cannot switch dependencies from an environment variable. HATCH_RS_LIBRARY_<NAME>_DIR and _INCLUDE_DIR reach build.rs.

Fixtures: the C ABI fixture generates its header with the cbindgen CLI (header no longer checked in) and its extension links the library; tests build it in bundled, external (against a separate prefix, imported with only the loader path set), and direct mode, with symbol inspection showing the library is compiled in only under direct. A new fixture writes its header from build.rs through the cbindgen build-dependency and validates it on the library artifact.

Removed the reserved cpp-compat, depfile, and cbindgen-mode fields. A build.rs header needs no mode: it exists after the Cargo build, so expected-headers on the library artifact validates it. CI installs cbindgen@0.29.4 next to the license collector.

This is the first step of the split-library pattern needed by fsspec-rs (1kbgz/fsspec-rs#78).

Type of Change

  • New feature
  • Documentation update
  • CI / build configuration

Checklist

  • Linting passes (make lint)
  • Tests pass (make test)
  • New tests added for new functionality
  • Documentation updated (if applicable)
  • Changelog / version bump (if applicable)

Symbol validation reads the export table of the ELF, Mach-O, PE, or
WebAssembly file instead of loading it with ctypes.CDLL, so
expected-symbols and header-symbol-prefix work for cross builds and
Pyodide. Only validate = true still loads the library.

A Python extension artifact can now link a cdylib artifact through
`links`. `library-source` (or HATCH_RS_LIBRARY_SOURCE) picks between
bundled, external, static, and direct. Bundled builds get a soname or
@rpath install name and an rpath from the extension to the library.
External builds skip the library and resolve it from HATCH_RS_LIBRARY_ROOT,
library-root, pkg-config, or the conda prefix, with no rpath.
library-source-features maps each mode to the extension crate's Cargo
features, since Cargo cannot switch dependencies from the environment.

The C ABI fixture generates its header with the cbindgen CLI and the
extension links the library; a second fixture writes its header from
build.rs. The reserved cpp-compat, depfile, and cbindgen-mode fields are
removed.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
A library shipped under lib/ needs a layout-neutral identity whether or not an extension in the same wheel links it yet.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.33858% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.77%. Comparing base (572dfc4) to head (f4c9829).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
hatch_rs/structs.py 81.62% 34 Missing ⚠️
hatch_rs/symbols.py 92.42% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #94      +/-   ##
==========================================
+ Coverage   91.67%   91.77%   +0.10%     
==========================================
  Files          13       15       +2     
  Lines        2079     2518     +439     
==========================================
+ Hits         1906     2311     +405     
- Misses        173      207      +34     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

A vtable field such as foo_status (*open)(...) names a type before the parenthesis, not an exported function.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
Compare link arguments as argv entries instead of the shell-quoted display, and resolve the fixture package directory so os.add_dll_directory gets an absolute path under python -c.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@timkpaine timkpaine mentioned this pull request Nov 10, 2026
7 of 8 tasks
Cargo keeps its metadata hash in the DLL name unless the crate's Cargo.toml declares cdylib, and the import library records that hashed name, so the extension loaded a DLL the wheel never ships. Check the import library names the shipped DLL and declare cdylib in the fixture crate.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@timkpaine timkpaine added the tag: automerge Automatically merge this PR when status checks pass label Nov 10, 2026
@python-templates-automerge
python-templates-automerge Bot merged commit a9571e8 into main Nov 10, 2026
7 of 10 checks passed
@python-templates-automerge
python-templates-automerge Bot deleted the split-library branch November 10, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: automerge Automatically merge this PR when status checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant