11#
2- # SPDX-FileCopyrightText: Copyright 2010-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
2+ # SPDX-FileCopyrightText: Copyright 2010-2023, 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
33#
44# SPDX-License-Identifier: Apache-2.0
55#
1919cmake_minimum_required (VERSION 3.15.6 )
2020set (CMSIS_OPTIMIZATION_LEVEL "-Ofast" CACHE STRING "Compiler optimization level." )
2121
22- project (CMSISNN)
22+ project (CMSISNN C CXX )
2323
2424add_library (cmsis-nn STATIC )
2525
@@ -28,3 +28,87 @@ target_compile_options(cmsis-nn PRIVATE ${CMSIS_OPTIMIZATION_LEVEL})
2828target_include_directories (cmsis-nn PUBLIC "Include" )
2929
3030add_subdirectory (Source )
31+
32+ option (CMSISNN_BUILD_PYBIND "Build pybind11 Python module for CMSIS-NN helpers" OFF )
33+
34+ if (CMSISNN_BUILD_PYBIND)
35+
36+ # Add shared library for testing bindings with ctest
37+ add_library (cmsis-nn-shared SHARED
38+ $<TARGET_PROPERTY :cmsis -nn ,SOURCES >
39+ )
40+ set_target_properties (cmsis-nn-shared PROPERTIES OUTPUT_NAME cmsis-nn )
41+ target_include_directories (cmsis-nn-shared PRIVATE
42+ $<TARGET_PROPERTY :cmsis -nn ,INCLUDE_DIRECTORIES >
43+ )
44+ target_compile_options (cmsis-nn-shared PRIVATE ${CMSIS_OPTIMIZATION_LEVEL} )
45+
46+ find_package (Python3 COMPONENTS Interpreter Development REQUIRED )
47+
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 ()
59+
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." )
64+ endif ()
65+
66+ add_library (cmsis_nn MODULE
67+ Source /Bindings/arm_py_module.cpp
68+ Source /Bindings/arm_py_backend.cpp
69+ Source /Bindings/arm_py_avgpool_buffer_size.cpp
70+ Source /Bindings/arm_py_conv_buffer_size.cpp
71+ Source /Bindings/arm_py_depthwise_conv_buffer_size.cpp
72+ Source /Bindings/arm_py_fully_connected_buffer_size.cpp
73+ Source /Bindings/arm_py_svdf_buffer_size.cpp
74+ Source /Bindings/arm_py_transpose_conv_buffer_size.cpp
75+ )
76+ set_target_properties (cmsis_nn PROPERTIES PREFIX "" )
77+
78+ if (Python3_INCLUDE_DIRS)
79+ target_include_directories (cmsis_nn PRIVATE
80+ ${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 )
94+
95+ target_include_directories (cmsis_nn PRIVATE
96+ ${CMAKE_CURRENT_SOURCE_DIR} /Include
97+ )
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 ()
105+
106+ target_compile_options (cmsis_nn PRIVATE ${CMSIS_OPTIMIZATION_LEVEL} )
107+
108+ # Put the built .so into the python package directory inside the wheel
109+ install (TARGETS cmsis_nn
110+ LIBRARY DESTINATION cmsis_nn
111+ RUNTIME DESTINATION cmsis_nn
112+ )
113+
114+ endif ()
0 commit comments