From 9b11c9db809e29911ec448e3c1d57b138aeb42f5 Mon Sep 17 00:00:00 2001 From: Eric Kilmer Date: Thu, 21 Mar 2024 22:29:04 -0400 Subject: [PATCH] [nanobind] New port (#35488) --- ports/nanobind/find_dependency_python.patch | 62 +++++++++ ports/nanobind/move_include_dir.patch | 129 ++++++++++++++++++ ports/nanobind/portfile.cmake | 28 ++++ ports/nanobind/usage | 8 ++ ports/nanobind/vcpkg.json | 15 ++ .../vcpkg-ci-nanobind/portfile.cmake | 25 ++++ .../test_ports/vcpkg-ci-nanobind/vcpkg.json | 15 ++ versions/baseline.json | 4 + versions/n-/nanobind.json | 9 ++ 9 files changed, 295 insertions(+) create mode 100644 ports/nanobind/find_dependency_python.patch create mode 100644 ports/nanobind/move_include_dir.patch create mode 100644 ports/nanobind/portfile.cmake create mode 100644 ports/nanobind/usage create mode 100644 ports/nanobind/vcpkg.json create mode 100644 scripts/test_ports/vcpkg-ci-nanobind/portfile.cmake create mode 100644 scripts/test_ports/vcpkg-ci-nanobind/vcpkg.json create mode 100644 versions/n-/nanobind.json diff --git a/ports/nanobind/find_dependency_python.patch b/ports/nanobind/find_dependency_python.patch new file mode 100644 index 0000000000..9fcb019fcc --- /dev/null +++ b/ports/nanobind/find_dependency_python.patch @@ -0,0 +1,62 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 919ce1d..25956c7 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -107,29 +107,11 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") + add_compile_options(-Wall -Wextra -Wno-unused-local-typedefs) + endif() + +-# --------------------------------------------------------------------------- +-# Find the Python interpreter and development libraries +-# --------------------------------------------------------------------------- +- +-if (NOT TARGET Python::Module OR NOT TARGET Python::Interpreter) +- set(Python_FIND_FRAMEWORK LAST) # Prefer Brew/Conda to Apple framework python +- +- if (CMAKE_VERSION VERSION_LESS 3.18) +- set(NB_PYTHON_DEV_MODULE Development) +- else() +- set(NB_PYTHON_DEV_MODULE Development.Module) +- endif() +- +- find_package(Python 3.8 +- REQUIRED COMPONENTS Interpreter ${NB_PYTHON_DEV_MODULE} +- OPTIONAL_COMPONENTS Development.SABIModule) +-endif() +- + # --------------------------------------------------------------------------- + # Include nanobind cmake functionality + # --------------------------------------------------------------------------- + +-find_package(nanobind ++find_package(nanobind REQUIRED + PATHS ${CMAKE_CURRENT_SOURCE_DIR}/cmake + NO_DEFAULT_PATH) + +diff --git a/cmake/nanobind-config.cmake b/cmake/nanobind-config.cmake +index cb6cfb0..a456f3f 100644 +--- a/cmake/nanobind-config.cmake ++++ b/cmake/nanobind-config.cmake +@@ -1,9 +1,20 @@ + include_guard(GLOBAL) + +-if (NOT TARGET Python::Module) +- message(FATAL_ERROR "You must invoke 'find_package(Python COMPONENTS Interpreter Development REQUIRED)' prior to including nanobind.") ++# --------------------------------------------------------------------------- ++# Find the Python interpreter and development libraries ++# --------------------------------------------------------------------------- ++if (CMAKE_VERSION VERSION_LESS 3.18) ++ set(NB_PYTHON_DEV_MODULE Development) ++else() ++ set(NB_PYTHON_DEV_MODULE Development.Module) + endif() + ++include(CMakeFindDependencyMacro) ++find_dependency(Python 3.8 ++ COMPONENTS Interpreter ${NB_PYTHON_DEV_MODULE} ++ OPTIONAL_COMPONENTS Development.SABIModule ++) ++ + # Determine the right suffix for ordinary and stable ABI extensions. + + # We always need to know the extension diff --git a/ports/nanobind/move_include_dir.patch b/ports/nanobind/move_include_dir.patch new file mode 100644 index 0000000000..7fdd1a02a5 --- /dev/null +++ b/ports/nanobind/move_include_dir.patch @@ -0,0 +1,129 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index e8fda9f..b27dc07 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -52,7 +52,7 @@ if(NB_CREATE_INSTALL_RULES AND NOT CMAKE_SKIP_INSTALL_RULES) + + # Normally these would be configurable by the user, but we can't allow that + # because the lookup paths are hard-coded in 'cmake/nanobind-config.cmake' +- set(NB_INSTALL_INC_DIR "${NB_INSTALL_DATADIR}/include") ++ set(NB_INSTALL_INC_DIR "${NB_INSTALL_DATADIR}/../../include") + set(NB_INSTALL_SRC_DIR "${NB_INSTALL_DATADIR}/src") + set(NB_INSTALL_EXT_DIR "${NB_INSTALL_DATADIR}/ext") + set(NB_INSTALL_MODULE_DIR "${NB_INSTALL_DATADIR}") +diff --git a/cmake/nanobind-config.cmake b/cmake/nanobind-config.cmake +index a456f3f..92406dc 100644 +--- a/cmake/nanobind-config.cmake ++++ b/cmake/nanobind-config.cmake +@@ -113,54 +113,54 @@ function (nanobind_build_library TARGET_NAME) + + add_library(${TARGET_NAME} ${TARGET_TYPE} + EXCLUDE_FROM_ALL +- ${NB_DIR}/include/nanobind/make_iterator.h +- ${NB_DIR}/include/nanobind/nanobind.h +- ${NB_DIR}/include/nanobind/nb_accessor.h +- ${NB_DIR}/include/nanobind/nb_attr.h +- ${NB_DIR}/include/nanobind/nb_call.h +- ${NB_DIR}/include/nanobind/nb_cast.h +- ${NB_DIR}/include/nanobind/nb_class.h +- ${NB_DIR}/include/nanobind/nb_defs.h +- ${NB_DIR}/include/nanobind/nb_descr.h +- ${NB_DIR}/include/nanobind/nb_enums.h +- ${NB_DIR}/include/nanobind/nb_error.h +- ${NB_DIR}/include/nanobind/nb_func.h +- ${NB_DIR}/include/nanobind/nb_lib.h +- ${NB_DIR}/include/nanobind/nb_misc.h +- ${NB_DIR}/include/nanobind/nb_python.h +- ${NB_DIR}/include/nanobind/nb_traits.h +- ${NB_DIR}/include/nanobind/nb_tuple.h +- ${NB_DIR}/include/nanobind/nb_types.h +- ${NB_DIR}/include/nanobind/ndarray.h +- ${NB_DIR}/include/nanobind/trampoline.h +- ${NB_DIR}/include/nanobind/operators.h +- ${NB_DIR}/include/nanobind/stl/array.h +- ${NB_DIR}/include/nanobind/stl/bind_map.h +- ${NB_DIR}/include/nanobind/stl/bind_vector.h +- ${NB_DIR}/include/nanobind/stl/detail +- ${NB_DIR}/include/nanobind/stl/detail/nb_array.h +- ${NB_DIR}/include/nanobind/stl/detail/nb_dict.h +- ${NB_DIR}/include/nanobind/stl/detail/nb_list.h +- ${NB_DIR}/include/nanobind/stl/detail/nb_set.h +- ${NB_DIR}/include/nanobind/stl/detail/traits.h +- ${NB_DIR}/include/nanobind/stl/filesystem.h +- ${NB_DIR}/include/nanobind/stl/function.h +- ${NB_DIR}/include/nanobind/stl/list.h +- ${NB_DIR}/include/nanobind/stl/map.h +- ${NB_DIR}/include/nanobind/stl/optional.h +- ${NB_DIR}/include/nanobind/stl/pair.h +- ${NB_DIR}/include/nanobind/stl/set.h +- ${NB_DIR}/include/nanobind/stl/shared_ptr.h +- ${NB_DIR}/include/nanobind/stl/string.h +- ${NB_DIR}/include/nanobind/stl/string_view.h +- ${NB_DIR}/include/nanobind/stl/tuple.h +- ${NB_DIR}/include/nanobind/stl/unique_ptr.h +- ${NB_DIR}/include/nanobind/stl/unordered_map.h +- ${NB_DIR}/include/nanobind/stl/unordered_set.h +- ${NB_DIR}/include/nanobind/stl/variant.h +- ${NB_DIR}/include/nanobind/stl/vector.h +- ${NB_DIR}/include/nanobind/eigen/dense.h +- ${NB_DIR}/include/nanobind/eigen/sparse.h ++ ${NB_DIR}/../../include/nanobind/make_iterator.h ++ ${NB_DIR}/../../include/nanobind/nanobind.h ++ ${NB_DIR}/../../include/nanobind/nb_accessor.h ++ ${NB_DIR}/../../include/nanobind/nb_attr.h ++ ${NB_DIR}/../../include/nanobind/nb_call.h ++ ${NB_DIR}/../../include/nanobind/nb_cast.h ++ ${NB_DIR}/../../include/nanobind/nb_class.h ++ ${NB_DIR}/../../include/nanobind/nb_defs.h ++ ${NB_DIR}/../../include/nanobind/nb_descr.h ++ ${NB_DIR}/../../include/nanobind/nb_enums.h ++ ${NB_DIR}/../../include/nanobind/nb_error.h ++ ${NB_DIR}/../../include/nanobind/nb_func.h ++ ${NB_DIR}/../../include/nanobind/nb_lib.h ++ ${NB_DIR}/../../include/nanobind/nb_misc.h ++ ${NB_DIR}/../../include/nanobind/nb_python.h ++ ${NB_DIR}/../../include/nanobind/nb_traits.h ++ ${NB_DIR}/../../include/nanobind/nb_tuple.h ++ ${NB_DIR}/../../include/nanobind/nb_types.h ++ ${NB_DIR}/../../include/nanobind/ndarray.h ++ ${NB_DIR}/../../include/nanobind/trampoline.h ++ ${NB_DIR}/../../include/nanobind/operators.h ++ ${NB_DIR}/../../include/nanobind/stl/array.h ++ ${NB_DIR}/../../include/nanobind/stl/bind_map.h ++ ${NB_DIR}/../../include/nanobind/stl/bind_vector.h ++ ${NB_DIR}/../../include/nanobind/stl/detail ++ ${NB_DIR}/../../include/nanobind/stl/detail/nb_array.h ++ ${NB_DIR}/../../include/nanobind/stl/detail/nb_dict.h ++ ${NB_DIR}/../../include/nanobind/stl/detail/nb_list.h ++ ${NB_DIR}/../../include/nanobind/stl/detail/nb_set.h ++ ${NB_DIR}/../../include/nanobind/stl/detail/traits.h ++ ${NB_DIR}/../../include/nanobind/stl/filesystem.h ++ ${NB_DIR}/../../include/nanobind/stl/function.h ++ ${NB_DIR}/../../include/nanobind/stl/list.h ++ ${NB_DIR}/../../include/nanobind/stl/map.h ++ ${NB_DIR}/../../include/nanobind/stl/optional.h ++ ${NB_DIR}/../../include/nanobind/stl/pair.h ++ ${NB_DIR}/../../include/nanobind/stl/set.h ++ ${NB_DIR}/../../include/nanobind/stl/shared_ptr.h ++ ${NB_DIR}/../../include/nanobind/stl/string.h ++ ${NB_DIR}/../../include/nanobind/stl/string_view.h ++ ${NB_DIR}/../../include/nanobind/stl/tuple.h ++ ${NB_DIR}/../../include/nanobind/stl/unique_ptr.h ++ ${NB_DIR}/../../include/nanobind/stl/unordered_map.h ++ ${NB_DIR}/../../include/nanobind/stl/unordered_set.h ++ ${NB_DIR}/../../include/nanobind/stl/variant.h ++ ${NB_DIR}/../../include/nanobind/stl/vector.h ++ ${NB_DIR}/../../include/nanobind/eigen/dense.h ++ ${NB_DIR}/../../include/nanobind/eigen/sparse.h + + ${NB_DIR}/src/buffer.h + ${NB_DIR}/src/hash.h +@@ -230,7 +230,7 @@ function (nanobind_build_library TARGET_NAME) + + target_include_directories(${TARGET_NAME} PUBLIC + ${Python_INCLUDE_DIRS} +- ${NB_DIR}/include) ++ ${NB_DIR}/../../include) + + target_compile_features(${TARGET_NAME} PUBLIC cxx_std_17) + nanobind_set_visibility(${TARGET_NAME}) diff --git a/ports/nanobind/portfile.cmake b/ports/nanobind/portfile.cmake new file mode 100644 index 0000000000..dd8eefc397 --- /dev/null +++ b/ports/nanobind/portfile.cmake @@ -0,0 +1,28 @@ +# nanobind distributes source code to build on-demand. +# The source code is installed into the 'share/${PORT}' directory with +# subdirectories for source `src` and header `include` files +set(VCPKG_POLICY_EMPTY_PACKAGE enabled) +set(VCPKG_BUILD_TYPE release) + +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO wjakob/nanobind + REF "v${VERSION}" + SHA512 40311f6416b9fdce764bf80baf156b42e1f00e03f3427b9f9db401fa4eeeb9db83b79c04ebefec2f6ed185419d1b22065f8f12eba3ad57056d2e0f825444b785 + HEAD_REF master + PATCHES + find_dependency_python.patch + move_include_dir.patch +) + +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" + OPTIONS + -DNB_USE_SUBMODULE_DEPS:BOOL=OFF + -DNB_TEST:BOOL=OFF +) + +vcpkg_cmake_install() + +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") diff --git a/ports/nanobind/usage b/ports/nanobind/usage new file mode 100644 index 0000000000..81a3259d31 --- /dev/null +++ b/ports/nanobind/usage @@ -0,0 +1,8 @@ +The package nanobind provides CMake functions and source code rather than +libraries: + + find_package(nanobind REQUIRED) + nanobind_add_module(my_ext source.cpp) + + # See docs for more configuration options + # https://nanobind.readthedocs.io/en/latest/api_cmake.html diff --git a/ports/nanobind/vcpkg.json b/ports/nanobind/vcpkg.json new file mode 100644 index 0000000000..58318e3778 --- /dev/null +++ b/ports/nanobind/vcpkg.json @@ -0,0 +1,15 @@ +{ + "name": "nanobind", + "version-semver": "1.9.2", + "description": "Tiny and efficient C++/Python bindings", + "homepage": "https://nanobind.readthedocs.io/en/latest/", + "license": "BSD-3-Clause", + "dependencies": [ + "python3", + "robin-map", + { + "name": "vcpkg-cmake", + "host": true + } + ] +} diff --git a/scripts/test_ports/vcpkg-ci-nanobind/portfile.cmake b/scripts/test_ports/vcpkg-ci-nanobind/portfile.cmake new file mode 100644 index 0000000000..c8d43663dd --- /dev/null +++ b/scripts/test_ports/vcpkg-ci-nanobind/portfile.cmake @@ -0,0 +1,25 @@ +set(VCPKG_POLICY_EMPTY_PACKAGE enabled) + +# This test does not support cross-compilation due to nanobind's usage of the +# Python interpreter to figure out Python module suffix. +if(VCPKG_CROSSCOMPILING) + message(WARNING "Skipping vcpkg-ci-nanobind because it is not expected to work when cross-compiling") + return() +endif() + +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO wjakob/nanobind_example + REF 4b5c9bd484dec77e085a188dcefc536aed69aae9 + SHA512 ec7eeb25b5c5ee2e8bbcc48e78719dc6e5211cf54794dd3c370ad3e8d685fbc8b79435890da3b9481656169efaa87b77e3ea55ce864efd670dd9ea0600dee77d + HEAD_REF master +) + +# This is needed to correctly build/link against a debug build of Python on +# Windows +string(APPEND VCPKG_CXX_FLAGS_DEBUG " -DPy_DEBUG") +string(APPEND VCPKG_C_FLAGS_DEBUG " -DPy_DEBUG") + +vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") + +vcpkg_cmake_build() diff --git a/scripts/test_ports/vcpkg-ci-nanobind/vcpkg.json b/scripts/test_ports/vcpkg-ci-nanobind/vcpkg.json new file mode 100644 index 0000000000..5cd7db5522 --- /dev/null +++ b/scripts/test_ports/vcpkg-ci-nanobind/vcpkg.json @@ -0,0 +1,15 @@ +{ + "name": "vcpkg-ci-nanobind", + "version": "0.0.1", + "description": "A nanobind example project", + "homepage": "https://nanobind.readthedocs.io/en/latest/", + "license": "BSD-3-Clause", + "dependencies": [ + "nanobind", + { + "name": "vcpkg-cmake", + "host": true + } + ] +} + diff --git a/versions/baseline.json b/versions/baseline.json index 96ed041b77..bf1aebdd20 100644 --- a/versions/baseline.json +++ b/versions/baseline.json @@ -5972,6 +5972,10 @@ "baseline": "4.3.11", "port-version": 0 }, + "nanobind": { + "baseline": "1.9.2", + "port-version": 0 + }, "nanodbc": { "baseline": "2.13.0", "port-version": 8 diff --git a/versions/n-/nanobind.json b/versions/n-/nanobind.json new file mode 100644 index 0000000000..4f6f7114bf --- /dev/null +++ b/versions/n-/nanobind.json @@ -0,0 +1,9 @@ +{ + "versions": [ + { + "git-tree": "14a09776a3248629b8efbf096dcb787c01abcfe9", + "version-semver": "1.9.2", + "port-version": 0 + } + ] +}