How to package binaries alongside python code when building wheel using scikit-build and nanobind

121 views Asked by At

Trying to package a python wheel with C++ binaries on Ubuntu and ninja-build. Using a combination of scikit-build-core + nanobind. The CMake below compiles correctly, and produces working.so files, however after installing the created wheel, the binaries are copied to the venv/mypkg rather than site packages. After install, python is unable to access the binary library unless I manually copy the .so file inside mypkg in site-packages, or manually add the venv path to LD_LIBRARY_PATH (I won't be able to do this when I distribute the package).

Looking inside the whl, the binaries are stored in the mypkg.data directory, rather than with the rest of the source files. Have build similar packages from this repo https://github.com/scikit-build/scikit-build-sample-projects and the resultant wheel has the .so next to the source code, which is then copied to site-packages - hoping to mimic this behaviour.

Is there anything I'm missing from the cmake file? I have a minimal pyproject.toml.

This is being triggered from CI using python3 -m setup bdist_wheel

Tried removing pyproject.toml to revert to setup.py. Have also tried setting include-package-data = true/false

and various permutations of [tool.setuptools.package-data] mypkg = ["*.so"]

Any solution will need to be able to install the package and copy the .so to somewhere that default python can access it with no modifying env variables on the user machine.

cmake_minimum_required(VERSION 3.26...3.27)
project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX)

set(EXTERNAL_DEPS_INSTALL_DIR
            "${CMAKE_CURRENT_SOURCE_DIR}/libs/${CMAKE_CXX_COMPILER_ID}"
            CACHE PATH "Installation directory for external dependencies")

find_package(
        Python 3.10 REQUIRED
        COMPONENTS Interpreter Development.Module
        OPTIONAL_COMPONENTS Development.SABIModule)
find_package(OpenMP)        
find_package(TBB)
find_package(Eigen3 REQUIRED PATHS "${EXTERNAL_DEPS_INSTALL_DIR}/share/eigen3/cmake")
set_target_properties(Python::Module PROPERTIES MAP_IMPORTED_CONFIG_DEBUG";RELEASE")

add_subdirectory(an/libs/nanobind EXCLUDE_FROM_ALL)
include_directories("${NB_DIR}/include")
include_directories("${_Python_INCLUDE_DIR}")
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(an)

file(GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/an/python/*.cpp")
nanobind_add_module(_C NB_STATIC ${SRC_FILES})

target_link_libraries(_C PUBLIC analytics)
target_include_directories(
    _C PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/an/python/include>
                        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
install(TARGETS _C DESTINATION .)
0

There are 0 answers