Skip to content

Question: Wrapping nanobind / subproject extensions via meson-python #588

@WillAyd

Description

@WillAyd

I am trying to integrate some nanobind modules into a meson project. As things stand today, nanobind only supports CMake as a build system, and I am not aware of another project that has converted that to Meson.

As far as I can tell, the best way to structure this is to put the nanobind code that requires CMake into a subproject. So I end up with a structure like this:

project
  /src
    meson.build
  /subprojects
    /nb_libs
      my_ext.cpp

Is there any way at all to have the extensions built from subprojects into the /src build tree? I've tried something like this in meson.build:

cmake = import('cmake')
sub_proj = cmake.subproject('nb_libs')
my_ext_dep = sub_proj.dependency('my_ext')
py.extension_module('my_ext', dependencies: [my_ext_dep])

This gets me the structure I need, but generates a warning that it will be broken in a future version of meson:

WARNING: Build target new_vector.cpython-310-x86_64-linux-gnu has no sources. This was never supposed to be allowed but did because of a bug, support will be removed in a future release of Meson

and doesn't work at runtime I think because the module export function gets mangled in the process

>>> import my_ext
ImportError: dynamic module does not define module export function (PyInit_my_ext)

Another option I've considered is doing a custom_target that moves the subproject dependency output:

custom_target('move_new_vector',
    command : ['mv', '@INPUT@', 'src/my_ext.cpython-310-x86_64-linux-gnu.so'],
    output: 'my_ext.cpython-310-x86_64-linux-gnu.so',
    install_dir : 'src',
    input: vec_my_ext_dep
)

But that does not work because my_ext_dep is an InternalDependency, not a string (maybe meson has a way to convert this). It also seems pretty fragile, so hoping for a better way

Metadata

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