You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c4c5605
Browse filesBrowse the repository at this point in the historyBrowse files
Validate C ABI libraries and link extensions against them (#141)
* Validate C ABI libraries and write an artifact manifest
Libraries entries accept the hatch-rs validation fields through
hatch-native: expected headers, ABI strings, a header symbol prefix,
expected exports, an optional ctypes load, and validation commands. A
top-level artifacts list validates files that libraries does not name,
such as CMake install output, by path or by library name. The
artifact-manifest option records each packaged binary in a JSON file in
the wheel. Library output directories are created before compiling, so
a library can be built into a lib/ subdirectory.
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
* Link libraries entries against generic libraries through depends
An entry's depends names generic libraries declared before it. The
entry links each one's output and gets an $ORIGIN or @loader_path rpath
to its directory. On Windows it links the import library, which is
packaged next to the DLL. Generic libraries get a SONAME or an @rpath
install name unless the project sets one, so the link does not record a
build-tree path. depends raises for Emscripten builds.
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
* Add library-source for libraries named in depends
library-source, or HATCH_CPP_LIBRARY_SOURCE, picks bundled (the default
and the existing behavior), external, or direct. In external mode the
library is not built: hatch-native finds it under library-root,
HATCH_CPP_LIBRARY_ROOT, pkg-config, or the conda prefix, the consumer
links it without an rpath, and validation checks the external file. In
direct mode its sources are compiled into each consumer. static and
python-package raise until hatch-cpp supports them.
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
* Add a CMake fixture that validates an installed C ABI library by name
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
* Rename build plan validate to validate_artifacts
A validate instance method is an incompatible override of pydantic's BaseModel.validate classmethod, which fails ty.
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
---------
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
A `generic` library can ship next to the extension that links it, in the layout pyarrow uses. List the library first and name it in the extension's `depends`:
89
+
90
+
```toml
91
+
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
92
+
name = "project/lib/libdemo"
93
+
sources = ["cpp/demo.c"]
94
+
language = "c"
95
+
binding = "generic"
96
+
include-dirs = ["cpp/include"]
97
+
define-macros = ["DEMO_BUILD"]
98
+
99
+
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
100
+
name = "project/extension"
101
+
sources = ["cpp/extension.c"]
102
+
language = "c"
103
+
include-dirs = ["cpp/include"]
104
+
depends = ["project/lib/libdemo"]
105
+
```
106
+
107
+
The library builds first. On Linux it gets `-soname libdemo.so` and on macOS the install name `@rpath/libdemo.dylib`, unless `extra-link-args` already set one. The extension links the built file and gets an rpath to its directory (`$ORIGIN/lib`, `@loader_path/lib`), so the wheel works wherever it is installed. On Windows the extension links the import library `project/lib/libdemo.lib`, which is packaged next to the DLL. Windows has no rpath, so the package `__init__.py` must call `os.add_dll_directory` on `lib/` before importing the extension, and the library's header must mark its functions `__declspec(dllexport)` when building it and `__declspec(dllimport)` otherwise. `depends` is not supported for Emscripten builds.
108
+
109
+
`library-source` (or `HATCH_CPP_LIBRARY_SOURCE`, which wins) picks how libraries named in `depends` are provided:
110
+
111
+
-`bundled` (default): built and shipped in the wheel as above.
112
+
-`external`: not built. Each one is found under `library-root` (or `HATCH_CPP_LIBRARY_ROOT`) with `lib/` and `include/`, through `pkg-config = "demo"`, or under `$PREFIX`/`$CONDA_PREFIX` (`Library/` on Windows), by its name without the `lib` prefix. The extension links it with `-L`/`-l` and gets no rpath, so the packager supplies the runtime path; this is the mode a conda recipe uses. Validation fields on the entry check the external library.
113
+
-`direct`: not built. Its sources, include directories, and macros are compiled into each entry that depends on it, and no library ships. Use this for Pyodide.
114
+
115
+
`static` and `python-package` are not supported yet.
116
+
117
+
Wheel repair tools need to leave the bundled library in place: `auditwheel repair --exclude 'libdemo.so'`, `delocate-wheel --exclude libdemo`, `delvewheel repair --exclude libdemo.dll`.
118
+
119
+
### C ABI validation
120
+
121
+
A library that other code links against can be checked after it is built. Validation reads the export table from the binary, so it also works for cross builds and Pyodide. The field names match [hatch-rs](https://github.com/python-project-templates/hatch-rs) and come from [hatch-native](https://github.com/python-project-templates/hatch-native).
122
+
123
+
Set the fields on a `libraries` entry:
124
+
125
+
```toml
126
+
[[tool.hatch.build.hooks.hatch-cpp.libraries]]
127
+
name = "project/lib/libdemo"
128
+
sources = ["cpp/demo.c"]
129
+
language = "c"
130
+
binding = "generic"
131
+
include-dirs = ["cpp/include"]
132
+
expected-headers = ["cpp/include/demo/demo.h"]
133
+
header-symbol-prefix = "demo_"# every demo_* function in the headers must be exported
134
+
expected-abi-strings = ["DEMO_ABI_VERSION 1"]
135
+
expected-symbols = [] # extra symbols to require
136
+
validate = false# also load the library with ctypes
137
+
```
138
+
139
+
Or list `artifacts` to validate files that `libraries` does not name, such as the output of a CMake install. Each entry takes the same fields plus `name` and an optional `path` relative to the project root. Without `path`, `name` matches a `libraries` entry, or else the one packaged file called `lib<name>.so`, `lib<name>.dylib`, or `<name>.dll`:
`validation-commands` run after the checks, with `{name}`, `{path}`, and `{header}` substituted and `HATCH_CPP_ARTIFACT_NAME` and `HATCH_CPP_ARTIFACT_PATH` in the environment.
149
+
150
+
To record what was packaged, set `artifact-manifest = true` and `artifact-manifest-destination = "project/lib/hatch-cpp-artifacts.json"`. The JSON lists each packaged binary with its role (`python-extension` or `library`), platform, and profile, and is added to the wheel.
151
+
86
152
### CLI
87
153
88
154
`hatch-cpp` is integrated with [`hatch-build`](https://github.com/python-project-templates/hatch-build) to allow easy configuration of options via command line:
0 commit comments