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 13 commits into
base: master
Choose a base branch
from
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.1.0":
url: "https://github.com/wjakob/nanobind/archive/refs/tags/v2.1.0.tar.gz"
sha256: "c37c53c60ada5fe1c956e24bd4b83af669a2309bf952bd251f36a7d2fa3bacf0"
98 changes: 98 additions & 0 deletions recipes/nanobind/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
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, replace_in_file, mkdir
from conan.tools.scm import Version

required_conan_version = ">=1.53.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"

@property
def _min_cppstd(self):
return 17

@property
def _compilers_minimum_version(self):
return {
"gcc": "7",
"clang": "5",
"apple-clang": "10",
"msvc": "192",
"Visual Studio": "15",
}

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):
if self.settings.compiler.cppstd:
check_min_cppstd(self, self._min_cppstd)
minimum_version = self._compilers_minimum_version.get(str(self.settings.compiler), False)
if minimum_version and Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration(
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)

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 _patch_sources(self):
# Look for headers in <package_folder>/include
replace_in_file(self, os.path.join(self.source_folder, "cmake", "nanobind-config.cmake"),
"${NB_DIR}/include",
"${NB_DIR}/../../include")

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

@property
def _cmake_rel_dir(self):
return os.path.join("res", "nanobind", "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", "include"),
os.path.join(self.package_folder, "include"))
mkdir(self, os.path.join(self.package_folder, "res"))
rename(self,
os.path.join(self.package_folder, "nanobind"),
os.path.join(self.package_folder, "res", "nanobind"))
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.builddirs = [self._cmake_rel_dir]
self.cpp_info.set_property("cmake_build_modules", [os.path.join(self._cmake_rel_dir, "nanobind.cmake")])
10 changes: 10 additions & 0 deletions recipes/nanobind/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.15)
project(test_package CXX)

find_package(Python 3.8
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)

find_package(nanobind CONFIG REQUIRED)

nanobind_add_module(test_package STABLE_ABI test_package.cpp)
38 changes: 38 additions & 0 deletions recipes/nanobind/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
from conan.tools.env import VirtualRunEnv


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "CMakeDeps", "CMakeToolchain", "VirtualBuildEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)
self.requires("cpython/3.12.7", run=True)

def build_requirements(self):
# Required for find_package(Python)
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):
VirtualRunEnv(self).generate()
# Required for find_package(Python) to work with cpython/*:shared=True
VirtualRunEnv(self).generate(scope="build")

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.1.0":
folder: all
Loading