mesonbuild / mesonbuild/wrapdb

nanobind: `limited_api` not propagated to nanobind static library build

Open
#1,758 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
Meson
Stars
118
Forks
298
Avg merge
2d 2h
Merged PRs (30d)
24

Description

Hi @WillAyd,

Thank you for the excellent nanobind wrapper! I'm running into an issue when compiling limited API extensions that you might have some insight into. If I have a `meson.build` snippet like this:

```meson
nanobind_dep = dependency('nanobind', static: true)
py.extension_module(
'my_module_name',
sources: ['src/example.cpp'],
dependencies: [nanobind_dep],
install: true,
limited_api : '3.12'
)
```

The compiler output shows that `src/example.cpp` is built with `-DPy_LIMITED_API=0x030c0000`, but nanobind itself is not. For example:

```
[1/13] ccache c++ -Imy_module_name.abi3.so.p -I. -I.. -I../subprojects/nanobind-2.2.0/include -I../subprojects/robin-map-1.3.0/include -I/install/include/python3.12 -I/mnt/home/lgarrison/.local/share/uv/python/cpython-3.12.7-linux-x86_64-gnu/include/python3.12 -fvisibility=hidden -fvisibility-inlines-hidden -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++17 -O0 -g -fPIC -ffunction-sections -fdata-sections -DPy_LIMITED_API=0x030c0000 -MD -MQ my_module_name.abi3.so.p/src_example.cpp.o -MF my_module_name.abi3.so.p/src_example.cpp.o.d -o my_module_name.abi3.so.p/src_example.cpp.o -c ../src/example.cpp
[2/13] ccache c++ -Isubprojects/nanobind-2.2.0/libnanobind.a.p -Isubprojects/nanobind-2.2.0 -I../subprojects/nanobind-2.2.0 -I../subprojects/nanobind-2.2.0/include -I../subprojects/robin-map-1.3.0/include -I/install/include/python3.12 -I/mnt/home/lgarrison/.local/share/uv/python/cpython-3.12.7-linux-x86_64-gnu/include/python3.12 -fvisibility=hidden -fdiagnostics-color=always -D_GLIBCXX_ASSERTIONS=1 -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++17 -O0 -g -fPIC -MD -MQ subprojects/nanobind-2.2.0/libnanobind.a.p/src_nb_static_property.cpp.o -MF subprojects/nanobind-2.2.0/libnanobind.a.p/src_nb_static_property.cpp.o.d -o subprojects/nanobind-2.2.0/libnanobind.a.p/src_nb_static_property.cpp.o -c ../subprojects/nanobind-2.2.0/src/nb_static_property.cpp
```

When building wheels this way, this leads to `abi3audit` violations, which is how I noticed this.

So my question is: what's the right way to build the `nanobind_dep` static lib itself with the limited API flag?

I poked around in the Meson source a bit, and it looks like `limited_api` mostly just sets `-DPy_LIMITED_API` to the right hex value in the `c_args` and `cpp_args` of the shared extension ([here](https://github.com/mesonbuild/meson/blob/c02e0b7b1e2499f3ae18d26e443e18043fff3046/mesonbuild/modules/python.py#L177-L186)). It doesn't seem like anything there knows about dependencies.

So the question seems to be how to get the right `Py_LIMITED_API` value to pass to `nanobind_dep`. Maybe it could be extracted from the `extension_module` args (doesn't seem super clean), or maybe it could be exposed somewhere by Meson itself?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Meson Python module code linked at mesonbuild/modules/python.py#L177-L186, then compare how limited_api sets extension c_args and cpp_args with how the nanobind static library is configured in the provided meson.build snippet. Done means the nanobind static library receives the appropriate Py_LIMITED_API value so the resulting limited-API wheel no longer produces the reported abi3audit violations.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.