Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanobind: add a new recipe #20297

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions recipes/nanobind/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"2.4.0":
url: "https://github.com/wjakob/nanobind/archive/refs/tags/v2.4.0.tar.gz"
sha256: "bb35deaed7efac5029ed1e33880a415638352f757d49207a8e6013fefb6c49a7"
67 changes: 67 additions & 0 deletions recipes/nanobind/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os

from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import get, copy, rename

required_conan_version = ">=2.0"


class PackageConan(ConanFile):
name = "nanobind"
description = "Tiny and efficient C++/Python bindings"
license = "BSD-3-Clause"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/wjakob/nanobind"
topics = ("python", "bindings", "pybind11", "header-only")

package_type = "header-library"
settings = "os", "arch", "compiler", "build_type"

def layout(self):
cmake_layout(self, src_folder="src")

def package_id(self):
self.info.clear()

def requirements(self):
self.requires("tsl-robin-map/1.3.0")

def validate(self):
check_min_cppstd(self, 17)

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.cache_variables["NB_TEST"] = False
tc.cache_variables["NB_USE_SUBMODULE_DEPS"] = False
tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

@property
def _cmake_rel_dir(self):
return os.path.join("lib", "cmake")

def package(self):
copy(self, "LICENSE", self.source_folder, os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
rename(self,
os.path.join(self.package_folder, "nanobind"),
os.path.join(self.package_folder, "lib"))
rename(self,
os.path.join(self.package_folder, self._cmake_rel_dir, "nanobind-config.cmake"),
os.path.join(self.package_folder, self._cmake_rel_dir, "nanobind.cmake"))

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "nanobind")
self.cpp_info.includedirs = [os.path.join("lib", "include")]
self.cpp_info.builddirs = [self._cmake_rel_dir]
self.cpp_info.set_property("cmake_build_modules", [os.path.join(self._cmake_rel_dir, "nanobind.cmake")])
12 changes: 12 additions & 0 deletions recipes/nanobind/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.18)
project(test_package CXX)

find_package(Python 3.8 REQUIRED COMPONENTS Development.Module)

if(Python_NATIVE_EXECUTABLE)
set(Python_EXECUTABLE ${Python_NATIVE_EXECUTABLE})
endif()

find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(test_package NB_STATIC test_package.cpp)
39 changes: 39 additions & 0 deletions recipes/nanobind/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake, CMakeToolchain, CMakeDeps


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"

def requirements(self):
self.requires(self.tested_reference_str)
self.requires("cpython/[~3.12]")

def build_requirements(self):
self.tool_requires("cpython/<host_version>")
# Required for Development.Module in find_package(Python)
self.tool_requires("cmake/[>=3.18 <4]")

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
if not can_run(self):
tc.variables["Python_NATIVE_EXECUTABLE"] = os.path.join(self.dependencies.build["cpython"].package_folder, "bin", "python").replace("\\", "/")
tc.generate()
deps = CMakeDeps(self)
deps.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if can_run(self):
self.run('python -c "import test_package; print(test_package.add(2, 3))"',
env="conanrun", cwd=self.cpp.build.bindir)
8 changes: 8 additions & 0 deletions recipes/nanobind/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <nanobind/nanobind.h>

namespace nb = nanobind;
using namespace nb::literals;

NB_MODULE(test_package, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
}
3 changes: 3 additions & 0 deletions recipes/nanobind/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"2.4.0":
folder: all