Skip to content

nanobind: limited_api not propagated to nanobind static library build #1758

Open
@lgarrison

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:

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). 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?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions