Skip to content

Commit

Permalink
Update example to reproduce wjakob/nanobind#69
Browse files Browse the repository at this point in the history
  • Loading branch information
qnzhou committed Oct 13, 2022
1 parent fd6bfb1 commit 9f3802b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 17 deletions.
14 changes: 2 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@ elseif (MSVC)
find_package(Python)
endif()

find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)

# Run `nanobind.cmake_dir()` from Python to detect install location
execute_process(
COMMAND
"${PYTHON_EXECUTABLE}" -c "import nanobind; print(nanobind.cmake_dir())"
OUTPUT_VARIABLE _tmp_dir
OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT)
list(APPEND CMAKE_PREFIX_PATH "${_tmp_dir}")

# Now import nanobind from CMake
find_package(nanobind CONFIG REQUIRED)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(nanobind)

# Build the actual extension module
nanobind_add_module(
Expand Down
31 changes: 31 additions & 0 deletions cmake/nanobind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# Copyright 2022 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
#
if(COMMAND nanobind_add_module)
return()
endif()

message(STATUS "Third-party (external): creating target 'nanobind::nanobind'")

include(FetchContent)
FetchContent_Declare(
nanobind
GIT_REPOSITORY https://github.com/wjakob/nanobind.git
GIT_TAG master
)

include(python)
set(NB_SHARED OFF CACHE INTERNAL "")

# Note that we do not use FetchContent_MakeAvailable here because nanobind's cmake changes
# CMAKE_CXX_FLAGS and attempts to refind python, which can leads to cmake error.
FetchContent_Populate(nanobind)
find_package(nanobind PATHS ${nanobind_SOURCE_DIR}/cmake NO_DEFAULT_PATH)
17 changes: 17 additions & 0 deletions cmake/python.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2022 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
#
if(TARGET Python::Module)
return()
endif()

set(Python_FIND_VIRTUALENV FIRST)
find_package(Python 3.8 COMPONENTS Interpreter Development.Module REQUIRED)
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
requires = [
"setuptools>=42",
"wheel",
"scikit-build==0.14.0",
"scikit-build==0.15.0",
"cmake>=3.18",
"nanobind>=0.0.3",
"ninja; platform_system!='Windows'"
]

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

try:
from skbuild import setup
import nanobind
except ImportError:
print("The preferred way to invoke 'setup.py' is via pip, as in 'pip "
"install .'. If you wish to run the setup script directly, you must "
Expand Down
2 changes: 1 addition & 1 deletion src/nanobind_example/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .nanobind_example_ext import add
from .nanobind_example_ext import add, Color, print_color, ColorMap
52 changes: 51 additions & 1 deletion src/nanobind_example_ext.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
#include <nanobind/nanobind.h>

#include <iostream>

namespace nb = nanobind;

using namespace nb::literals;

enum Color { Red, Green, Blue };

class ColorMap {
public:
ColorMap() = default;
};

NB_MODULE(nanobind_example_ext, m) {
m.def("add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
nb::enum_<Color>(m, "Color")
.value("Red", Color::Red)
.value("Green", Color::Green)
.value("Blue", Color::Blue);

m.def(
"add", [](int a, int b) { return a + b; }, "a"_a, "b"_a);
m.def(
"print_color",
[](Color c) {
switch (c) {
case Color::Red:
std::cout << "red" << std::endl;
break;
case Color::Green:
std::cout << "green" << std::endl;
break;
case Color::Blue:
std::cout << "blue" << std::endl;
break;
}
},
"color"_a = Color::Red);

nb::class_<ColorMap>(m, "ColorMap")
.def(nb::init<>())
.def(
"print_color",
[](ColorMap& self, Color c) {
switch (c) {
case Color::Red:
std::cout << "red" << std::endl;
break;
case Color::Green:
std::cout << "green" << std::endl;
break;
case Color::Blue:
std::cout << "blue" << std::endl;
break;
}
},
"color"_a = Color::Red);
}

0 comments on commit 9f3802b

Please sign in to comment.