Skip to content

Commit ba16fde

Browse files
authored
Fix cmake for building pybinds on macOS. (#216)
Fix cmake for building pybinds on macOS. The recommended platform-independent way to create a pybind module is pybind11_add_module. To make this work, we need to make sure the full pybind11 cmake folder is found, the old fallback only set PYBIND11_INCLUDE_DIR. Instead of a find_package+fallback pattern, provide the cmake folder as a HINT and only do one find_package. A user can additionally override the hint by setting pybind11_DIR. ---- Additionaly simplify cmake for building pybinds. Remove a highly unlikely fallback for Python3_INCLUDE_DIRS. If we run into this, we likely have other bigger issues. Combine target_include_directories and link_libraries calls.
1 parent 9d0477f commit ba16fde

1 file changed

Lines changed: 18 additions & 41 deletions

File tree

CMakeLists.txt

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,23 @@ if(CMSISNN_BUILD_PYBIND)
4343
)
4444
target_compile_options(cmsis-nn-shared PRIVATE ${CMSIS_OPTIMIZATION_LEVEL})
4545

46+
set(Python3_FIND_VIRTUALENV FIRST)
4647
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
4748

48-
find_package(pybind11 CONFIG QUIET)
49-
50-
if(pybind11_FOUND)
51-
message(STATUS "Found pybind11 via CMake package")
52-
else()
53-
execute_process(
54-
COMMAND ${Python3_EXECUTABLE} -c "import pybind11; print(pybind11.get_include())"
55-
OUTPUT_VARIABLE PYBIND11_INCLUDE_DIR
56-
OUTPUT_STRIP_TRAILING_WHITESPACE
57-
)
58-
endif()
49+
execute_process(
50+
COMMAND ${Python3_EXECUTABLE} -m pybind11 --cmakedir
51+
OUTPUT_VARIABLE pybind11_PYTHON_CMAKE_DIR
52+
OUTPUT_STRIP_TRAILING_WHITESPACE
53+
)
5954

60-
if(PYBIND11_INCLUDE_DIR)
61-
message(STATUS "pybind11 include: ${PYBIND11_INCLUDE_DIR}")
62-
elseif(NOT pybind11_FOUND)
63-
message(FATAL_ERROR "pybind11 not found. Install pybind11 or set CMSISNN_BUILD_PYBIND=OFF.")
55+
find_package(pybind11 CONFIG
56+
HINTS "${pybind11_PYTHON_CMAKE_DIR}"
57+
)
58+
if(NOT pybind11_FOUND)
59+
message(FATAL_ERROR "pybind11 not found. Install pybind11, set pybind11_DIR, or set CMSISNN_BUILD_PYBIND=OFF.")
6460
endif()
6561

66-
add_library(cmsis_nn MODULE
62+
pybind11_add_module(cmsis_nn MODULE
6763
Source/Bindings/arm_py_module.cpp
6864
Source/Bindings/arm_py_backend.cpp
6965
Source/Bindings/arm_py_avgpool_buffer_size.cpp
@@ -74,34 +70,15 @@ if(CMSISNN_BUILD_PYBIND)
7470
Source/Bindings/arm_py_transpose_conv_buffer_size.cpp
7571
)
7672
set_target_properties(cmsis_nn PROPERTIES PREFIX "")
77-
78-
if(Python3_INCLUDE_DIRS)
79-
target_include_directories(cmsis_nn PRIVATE
73+
target_include_directories(cmsis_nn PRIVATE
8074
${Python3_INCLUDE_DIRS}
81-
)
82-
else()
83-
execute_process(
84-
COMMAND ${Python3_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_paths()['include'])"
85-
OUTPUT_VARIABLE PYTHON_INCLUDE_DIR
86-
OUTPUT_STRIP_TRAILING_WHITESPACE
87-
)
88-
target_include_directories(cmsis_nn PRIVATE
89-
${PYTHON_INCLUDE_DIR}
90-
)
91-
endif()
92-
93-
target_link_libraries(cmsis_nn PRIVATE cmsis-nn)
75+
${CMAKE_CURRENT_SOURCE_DIR}/Include
76+
)
9477

95-
target_include_directories(cmsis_nn PRIVATE
96-
${CMAKE_CURRENT_SOURCE_DIR}/Include
78+
target_link_libraries(cmsis_nn PRIVATE
79+
cmsis-nn
80+
pybind11::headers
9781
)
98-
if(pybind11_FOUND)
99-
target_link_libraries(cmsis_nn PRIVATE pybind11::headers)
100-
else()
101-
target_include_directories(cmsis_nn PRIVATE
102-
${PYBIND11_INCLUDE_DIR}
103-
)
104-
endif()
10582

10683
target_compile_options(cmsis_nn PRIVATE ${CMSIS_OPTIMIZATION_LEVEL})
10784

0 commit comments

Comments
 (0)