-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
53 lines (48 loc) · 1.89 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
cmake_minimum_required(VERSION 3.26)
project(nanobind-project VERSION 0.2.0)
set(PY_VERSION_SUFFIX "a4")
set(PY_FULL_VERSION ${PROJECT_VERSION}${PY_VERSION_SUFFIX})
# Make sure that the Python and CMake versions match
if (DEFINED PY_BUILD_CMAKE_PACKAGE_VERSION)
if (NOT "${PY_BUILD_CMAKE_PACKAGE_VERSION}" MATCHES "^${PY_FULL_VERSION}$")
message(FATAL_ERROR "Version number does not match "
"(${PY_BUILD_CMAKE_PACKAGE_VERSION} - ${PY_FULL_VERSION}).")
endif()
endif()
# Find the nanobind package
include(cmake/QueryPythonForNanobind.cmake)
find_nanobind_python_first()
# Compile the example Python module
nanobind_add_module(_add_module STABLE_ABI "src/add_module.cpp")
target_compile_definitions(_add_module PRIVATE
MODULE_NAME=$<TARGET_FILE_BASE_NAME:_add_module>
VERSION_INFO="${PY_FULL_VERSION}"
)
# Hide all symbols by default (including external libraries on Linux)
set_target_properties(_add_module PROPERTIES
CXX_VISIBILITY_PRESET "hidden"
VISIBILITY_INLINES_HIDDEN true)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_options(_add_module PRIVATE "LINKER:--exclude-libs,ALL")
endif()
# Install the Python module
install(TARGETS _add_module
EXCLUDE_FROM_ALL
COMPONENT python_modules
DESTINATION ${PY_BUILD_CMAKE_MODULE_NAME})
# Install the debug file for the Python module (Windows only)
if (WIN32)
install(FILES $<TARGET_PDB_FILE:_add_module>
EXCLUDE_FROM_ALL
COMPONENT python_modules
DESTINATION ${PY_BUILD_CMAKE_MODULE_NAME}
OPTIONAL)
endif()
# Generate stubs for the Python module
option(WITH_PY_STUBS
"Generate Python stub files (.pyi) for the Python module." On)
if (WITH_PY_STUBS AND NOT CMAKE_CROSSCOMPILING)
include(cmake/NanobindStubgen.cmake)
nanobind_stubgen(_add_module)
nanobind_stubgen_install(_add_module ${PY_BUILD_CMAKE_MODULE_NAME})
endif()