-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCMakeLists.txt
60 lines (54 loc) · 2.12 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
54
55
56
57
58
59
60
cmake_minimum_required(VERSION 3.18)
project(pybind11-project VERSION 0.3.4)
set(PY_VERSION_SUFFIX "")
set(PY_FULL_VERSION ${PROJECT_VERSION}${PY_VERSION_SUFFIX})
# Make sure that the Python and CMake versions match
if (DEFINED PY_BUILD_CMAKE_PROJECT_VERSION)
if (NOT "${PY_BUILD_CMAKE_PROJECT_VERSION}" MATCHES "^${PY_FULL_VERSION}$")
message(FATAL_ERROR "Version number does not match "
"(${PY_BUILD_CMAKE_PROJECT_VERSION} - ${PY_FULL_VERSION}).")
endif()
endif()
# Find the Python interpreter and the development files
if (CMAKE_CROSSCOMPILING AND NOT (APPLE AND "$ENV{CIBUILDWHEEL}" STREQUAL "1"))
find_package(Python3 REQUIRED COMPONENTS Development.Module)
else()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
endif()
# Find the pybind11 package
include(cmake/QueryPythonForPybind11.cmake)
find_pybind11_python_first()
# Compile the example Python module
pybind11_add_module(_add_module MODULE "src/add_module.cpp")
set_target_properties(_add_module PROPERTIES
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}${PYTHON_MODULE_DEBUG_POSTFIX}")
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_IMPORT_NAME})
# Generate stubs for the Python module
set(WITH_PY_STUBS_DEFAULT On)
if (CMAKE_CROSSCOMPILING)
set(WITH_PY_STUBS_DEFAULT Off)
endif()
option(WITH_PY_STUBS
"Generate Python stub files (.pyi) for the Python module."
${WITH_PY_STUBS_DEFAULT})
if (WITH_PY_STUBS)
include(cmake/Pybind11Stubgen.cmake)
pybind11_stubgen(_add_module
PACKAGE ${PY_BUILD_CMAKE_IMPORT_NAME}
COMPONENT python_stubs)
endif()