From ff9a33e0bd068bb78354c00b4c14d94323d00687 Mon Sep 17 00:00:00 2001 From: Kerstin Keller Date: Tue, 12 Mar 2024 16:43:17 +0100 Subject: [PATCH] First small example for nanobind module. --- .gitmodules | 3 ++ CMakeLists.txt | 4 +++ doc/rst/license/thirdparty_licenses.rst | 7 +++++ lang/python/CMakeLists.txt | 1 + lang/python/nanobind_core/CMakeLists.txt | 28 +++++++++++++++++++ lang/python/nanobind_core/main.cpp | 5 ++++ .../nanobind/Modules/Findnanobind.cmake | 1 + thirdparty/nanobind/build-nanobind.cmake | 10 +++++++ thirdparty/nanobind/nanobind | 1 + 9 files changed, 60 insertions(+) create mode 100644 lang/python/nanobind_core/CMakeLists.txt create mode 100644 lang/python/nanobind_core/main.cpp create mode 100644 thirdparty/nanobind/Modules/Findnanobind.cmake create mode 100644 thirdparty/nanobind/build-nanobind.cmake create mode 160000 thirdparty/nanobind/nanobind diff --git a/.gitmodules b/.gitmodules index fd27987533..79d21b4aab 100644 --- a/.gitmodules +++ b/.gitmodules @@ -55,3 +55,6 @@ [submodule "thirdparty/udpcap"] path = thirdparty/udpcap/udpcap url = https://github.com/eclipse-ecal/udpcap.git +[submodule "thirdparty/nanobind/nanobind"] + path = thirdparty/nanobind/nanobind + url = https://github.com/wjakob/nanobind.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c42672281..88925bc8b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,9 @@ option(ECAL_THIRDPARTY_BUILD_CMAKE_FUNCTIONS "Build CMakeFunctions with eCAL" option(ECAL_THIRDPARTY_BUILD_FINEFTP "Build fineFTP with eCAL" ON) option(ECAL_THIRDPARTY_BUILD_FTXUI "Build ftxui with eCAL" ON) option(ECAL_THIRDPARTY_BUILD_GTEST "Build gtest with eCAL" OFF) +if (BUILD_PY_BINDING) +option(ECAL_THIRDPARTY_BUILD_NANOBIND "Build nanobind with eCAL" ON) +endif() option(ECAL_THIRDPARTY_BUILD_RECYCLE "Build steinwurf::recylce with eCAL" ON) option(ECAL_THIRDPARTY_BUILD_SPDLOG "Build spdlog with eCAL" ON) option(ECAL_THIRDPARTY_BUILD_SIMPLEINI "Build simpleini with eCAL" ON) @@ -223,6 +226,7 @@ set(possible_subprojects GTest HDF5 #libssh2 + nanobind Protobuf qwt recycle diff --git a/doc/rst/license/thirdparty_licenses.rst b/doc/rst/license/thirdparty_licenses.rst index 7c17a4c96c..9bb5750eae 100644 --- a/doc/rst/license/thirdparty_licenses.rst +++ b/doc/rst/license/thirdparty_licenses.rst @@ -147,6 +147,13 @@ They are used by Eclipse eCAL, but not associated to it in any way. - - |fa-file-alt| Copy in repository: :file:`/cpack/innosetup/modpath.iss` - |fa-windows| Binary distributions for Windows (Installer only) + * - `nanobind `_ + - :ref:`bsd_3` + - 2022, Wenzel Jakob + - - |fa-github| Git submodule ``/tirdparty/nanobind`` + - |fa-windows| Binary distributions for Windows + - |fa-ubuntu| Binary distributions for Linux + * - `npcap `_ - :ref:`npcap_license` - 2013-2021 by Insecure.Com LLC diff --git a/lang/python/CMakeLists.txt b/lang/python/CMakeLists.txt index be9c65a944..a5fb711ca5 100644 --- a/lang/python/CMakeLists.txt +++ b/lang/python/CMakeLists.txt @@ -64,6 +64,7 @@ else() endif() add_subdirectory(core) +add_subdirectory(nanobind_core) if(HAS_HDF5) add_subdirectory(ecalhdf5) endif() diff --git a/lang/python/nanobind_core/CMakeLists.txt b/lang/python/nanobind_core/CMakeLists.txt new file mode 100644 index 0000000000..2cb06d3142 --- /dev/null +++ b/lang/python/nanobind_core/CMakeLists.txt @@ -0,0 +1,28 @@ +# Try to import all Python components potentially needed by nanobind +find_package(Python + REQUIRED COMPONENTS Interpreter Development.Module + OPTIONAL_COMPONENTS Development.SABIModule) + +# Import nanobind through CMake's find_package mechanism +find_package(nanobind CONFIG REQUIRED) + +# We are now ready to compile the actual extension module +nanobind_add_module( + # Name of the extension + nanobind_core + + # Target the stable ABI for Python 3.12+, which reduces + # the number of binary wheels that must be built. This + # does nothing on older Python versions + STABLE_ABI + + # Source code goes here + main.cpp +) + +# Install directive for scikit-build-core +install(TARGETS nanobind_core LIBRARY DESTINATION nanobind_core) + +if(WIN32) + set_property(TARGET nanobind_core PROPERTY FOLDER lang/python/core) +endif() \ No newline at end of file diff --git a/lang/python/nanobind_core/main.cpp b/lang/python/nanobind_core/main.cpp new file mode 100644 index 0000000000..377ae791d1 --- /dev/null +++ b/lang/python/nanobind_core/main.cpp @@ -0,0 +1,5 @@ +#include + +NB_MODULE(nanobind_core, m) { + m.def("hello", []() { return "Hello world!"; }); +} \ No newline at end of file diff --git a/thirdparty/nanobind/Modules/Findnanobind.cmake b/thirdparty/nanobind/Modules/Findnanobind.cmake new file mode 100644 index 0000000000..196771e13f --- /dev/null +++ b/thirdparty/nanobind/Modules/Findnanobind.cmake @@ -0,0 +1 @@ +set(nanobind_FOUND TRUE) \ No newline at end of file diff --git a/thirdparty/nanobind/build-nanobind.cmake b/thirdparty/nanobind/build-nanobind.cmake new file mode 100644 index 0000000000..47f166fa13 --- /dev/null +++ b/thirdparty/nanobind/build-nanobind.cmake @@ -0,0 +1,10 @@ +message(STATUS "building nanobind") + +if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.25.0") +add_subdirectory(thirdparty/nanobind/nanobind EXCLUDE_FROM_ALL SYSTEM) +else () +add_subdirectory(thirdparty/nanobind/nanobind EXCLUDE_FROM_ALL) +endif () + + +list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Modules) \ No newline at end of file diff --git a/thirdparty/nanobind/nanobind b/thirdparty/nanobind/nanobind new file mode 160000 index 0000000000..df8996aafe --- /dev/null +++ b/thirdparty/nanobind/nanobind @@ -0,0 +1 @@ +Subproject commit df8996aafed8d891cbf1ac21bb828afc3f900ae1