-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathCMakeLists.txt
393 lines (347 loc) · 14.7 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
cmake_minimum_required(VERSION 3.4) # For Hunter
# Set policies
# CMP0074 dictates that find_package searches environment variable "[packageName]_ROOT" along with regular variable [packageName]_ROOT
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW) # Only introduced in 3.12
endif()
if(POLICY CMP0028)
cmake_policy(SET CMP0028 NEW)
endif()
# Specify path separator
set(SYS_PATH_SEPARATOR ";")
if(UNIX)
set(SYS_PATH_SEPARATOR ":")
endif()
# Move binary dir if windows, to shorten the path
if(WIN32)
set(HUNTER_BINARY_DIR "${HUNTER_GATE_ROOT}/_bin" CACHE STRING "Hunter binary directory")
endif()
# Pybindings project
set(TARGET_NAME depthai)
project(depthai VERSION "0") # revision of bindings [depthai-core].[rev]
# Add module paths
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
# Constants
set(DOCSTRINGS_INCLUDE_PLACEHOLDER_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/include)
set(DOCSTRINGS_INCLUDE_PLACEHOLDER_PATH ${DOCSTRINGS_INCLUDE_PLACEHOLDER_DIR}/docstring.hpp)
set(DEFAULT_DOCSTRINGS_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/depthai_python_docstring.hpp)
# First specify options
# option(DEPTHAI_PYTHON_USE_FIND_PACKAGE "Use find_package for depthai-core" OFF)
option(DEPTHAI_PYTHON_ENABLE_TESTS "Enable tests" OFF)
option(DEPTHAI_PYTHON_ENABLE_EXAMPLES "Enable examples" OFF)
option(DEPTHAI_PYTHON_BUILD_DOCS "Build documentation - see docs/requirements.txt for needed dependencies" OFF)
option(DEPTHAI_PYTHON_BUILD_DOCSTRINGS "Generate docstrings from header files if module 'pybind11_mkdoc' available" ON)
option(DEPTHAI_PYTHON_EMBEDDED_MODULE "Create an embeddable module" OFF)
# Add pybind11 dependency
#add_subdirectory(pybind11-2.5.0)
hunter_add_package(pybind11)
# Disable LTO if MINGW compiler
if(MINGW)
set(PYBIND11_LTO_CXX_FLAGS "" CACHE STRING "" FORCE)
endif()
find_package(pybind11 CONFIG REQUIRED)
# Print out the pybind11 version that was found
message(STATUS "Found pybind11 v${pybind11_VERSION}")
# Add external dependencies
add_subdirectory(external)
set(SOURCE_LIST
src/py_bindings.cpp
src/XLinkBindings.cpp
src/DeviceBindings.cpp
src/CalibrationHandlerBindings.cpp
src/DeviceBootloaderBindings.cpp
src/DatatypeBindings.cpp
src/MessageQueueBindings.cpp
src/pipeline/PipelineBindings.cpp
src/pipeline/CommonBindings.cpp
src/pipeline/AssetManagerBindings.cpp
src/openvino/OpenVINOBindings.cpp
src/nn_archive/NNArchiveBindings.cpp
src/log/LogBindings.cpp
src/VersionBindings.cpp
src/common/ModelTypeBindings.cpp
src/pipeline/node/NodeBindings.cpp
src/pipeline/node/XLinkInBindings.cpp
src/pipeline/node/XLinkOutBindings.cpp
src/pipeline/node/ColorCameraBindings.cpp
src/pipeline/node/CameraBindings.cpp
src/pipeline/node/MonoCameraBindings.cpp
src/pipeline/node/StereoDepthBindings.cpp
src/pipeline/node/NeuralNetworkBindings.cpp
src/pipeline/node/VideoEncoderBindings.cpp
src/pipeline/node/ImageManipBindings.cpp
src/pipeline/node/ImageManipV2Bindings.cpp
src/pipeline/node/SPIOutBindings.cpp
src/pipeline/node/SPIInBindings.cpp
src/pipeline/node/DetectionNetworkBindings.cpp
src/pipeline/node/SystemLoggerBindings.cpp
src/pipeline/node/ScriptBindings.cpp
src/pipeline/node/SpatialLocationCalculatorBindings.cpp
src/pipeline/node/SpatialDetectionNetworkBindings.cpp
src/pipeline/node/ObjectTrackerBindings.cpp
src/pipeline/node/IMUBindings.cpp
src/pipeline/node/EdgeDetectorBindings.cpp
src/pipeline/node/FeatureTrackerBindings.cpp
src/pipeline/node/ToFBindings.cpp
src/pipeline/node/AprilTagBindings.cpp
src/pipeline/node/DetectionParserBindings.cpp
src/pipeline/node/WarpBindings.cpp
src/pipeline/node/SyncBindings.cpp
src/pipeline/node/BenchmarkBindings.cpp
src/pipeline/node/UVCBindings.cpp
src/pipeline/node/ToFBindings.cpp
src/pipeline/node/PointCloudBindings.cpp
src/pipeline/node/SyncBindings.cpp
src/pipeline/node/MessageDemuxBindings.cpp
src/pipeline/node/HostNodeBindings.cpp
src/pipeline/node/RecordBindings.cpp
src/pipeline/node/ReplayBindings.cpp
src/pipeline/node/AudioInBindings.cpp
src/pipeline/node/AudioOutBindings.cpp
src/pipeline/node/AudioMixerBindings.cpp
src/pipeline/node/AudioEncoderBindings.cpp
src/pipeline/node/AudioReplayBindings.cpp
src/pipeline/datatype/ADatatypeBindings.cpp
src/pipeline/datatype/AprilTagConfigBindings.cpp
src/pipeline/datatype/AprilTagsBindings.cpp
src/pipeline/datatype/BufferBindings.cpp
src/pipeline/datatype/CameraControlBindings.cpp
src/pipeline/datatype/EdgeDetectorConfigBindings.cpp
src/pipeline/datatype/FeatureTrackerConfigBindings.cpp
src/pipeline/datatype/ToFConfigBindings.cpp
src/pipeline/datatype/ImageManipConfigBindings.cpp
src/pipeline/datatype/ImageManipConfigV2Bindings.cpp
src/pipeline/datatype/ImgDetectionsBindings.cpp
src/pipeline/datatype/ImgFrameBindings.cpp
src/pipeline/datatype/EncodedFrameBindings.cpp
src/pipeline/datatype/IMUDataBindings.cpp
src/pipeline/datatype/MessageGroupBindings.cpp
src/pipeline/datatype/NNDataBindings.cpp
src/pipeline/datatype/SpatialImgDetectionsBindings.cpp
src/pipeline/datatype/SpatialLocationCalculatorConfigBindings.cpp
src/pipeline/datatype/SpatialLocationCalculatorDataBindings.cpp
src/pipeline/datatype/StereoDepthConfigBindings.cpp
src/pipeline/datatype/SystemInformationBindings.cpp
src/pipeline/datatype/TrackedFeaturesBindings.cpp
src/pipeline/datatype/TrackletsBindings.cpp
src/pipeline/datatype/BenchmarkReportBindings.cpp
src/pipeline/datatype/PointCloudConfigBindings.cpp
src/pipeline/datatype/PointCloudDataBindings.cpp
src/pipeline/datatype/TransformDataBindings.cpp
src/pipeline/datatype/AudioFrameBindings.cpp
src/capabilities/CapabilityBindings.cpp
src/capabilities/CapabilityRangeBindings.cpp
src/capabilities/ImgFrameCapabilityBindings.cpp
src/modelzoo/NNModelDescriptionBindings.cpp
src/modelzoo/ZooBindings.cpp
)
if(DEPTHAI_BASALT_SUPPORT)
list(APPEND SOURCE_LIST
src/pipeline/node/BasaltVIOBindings.cpp
)
endif()
if(DEPTHAI_RTABMAP_SUPPORT)
list(APPEND SOURCE_LIST
src/pipeline/node/RTABMapVIOBindings.cpp
src/pipeline/node/RTABMapSLAMBindings.cpp
)
endif()
# Add files for python module
if(DEPTHAI_PYTHON_EMBEDDED_MODULE)
add_library(${TARGET_NAME} ${SOURCE_LIST})
else()
pybind11_add_module(${TARGET_NAME} ${SOURCE_LIST})
endif()
if(WIN32)
# Copy dlls to target directory - Windows only
# TARGET_RUNTIME_DLLS generator expression available since CMake 3.21
if(CMAKE_VERSION VERSION_LESS "3.21")
file(GLOB depthai_dll_libraries "${HUNTER_INSTALL_PREFIX}/bin/*.dll")
else()
set(depthai_dll_libraries "$<TARGET_RUNTIME_DLLS:${TARGET_NAME}>")
endif()
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND
"$<$<BOOL:${depthai_dll_libraries}>:${CMAKE_COMMAND};-E;copy_if_different;${depthai_dll_libraries};$<TARGET_FILE_DIR:${TARGET_NAME}>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
# Disable "d" postfix, so python can import the library as is
set_target_properties(${TARGET_NAME} PROPERTIES DEBUG_POSTFIX "")
endif()
# Add stubs (pyi) generation step after building bindings
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "from mypy import api" RESULT_VARIABLE error OUTPUT_QUIET ERROR_QUIET)
if(error OR CMAKE_CROSSCOMPILING)
message(WARNING "Mypy not available or cross compiling - stubs won't be generated or checked")
else()
get_target_property(bindings_directory ${TARGET_NAME} LIBRARY_OUTPUT_DIRECTORY)
if(NOT bindings_directory)
set(bindings_directory ${CMAKE_CURRENT_BINARY_DIR})
endif()
message(STATUS "Mypy available, creating and checking stubs. Running with generate_stubs.py ${TARGET_NAME} ${bindings_directory}")
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND
${CMAKE_COMMAND} -E env
# Python path (to find compiled module)
"PYTHONPATH=$<TARGET_FILE_DIR:${TARGET_NAME}>${SYS_PATH_SEPARATOR}$ENV{PYTHONPATH}"
${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_LIST_DIR}/generate_stubs.py" "${TARGET_NAME}" "$<TARGET_FILE_DIR:${TARGET_NAME}>"
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/generate_stubs.py"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
endif()
# Docstring options
if(DEPTHAI_PYTHON_DOCSTRINGS_INPUT AND DEPTHAI_PYTHON_DOCSTRINGS_OUTPUT)
message(FATAL_ERROR "DEPTHAI_PYTHON_DOCSTRINGS_INPUT and DEPTHAI_PYTHON_DOCSTRINGS_OUTPUT are mutually exclusive")
endif()
if(DEPTHAI_PYTHON_DOCSTRINGS_OUTPUT)
# If output is specified set both input and output to same the path
set(docstring_input_path ${DEPTHAI_PYTHON_DOCSTRINGS_OUTPUT})
set(docstring_output_path ${DEPTHAI_PYTHON_DOCSTRINGS_OUTPUT})
else()
# If input docstrings explicitly specified, use those and disable building
if(DEPTHAI_PYTHON_DOCSTRINGS_INPUT)
set(docstring_input_path ${DEPTHAI_PYTHON_DOCSTRINGS_INPUT})
message(STATUS "Disabled building of docstrings - using docstrings specified by DEPTHAI_PYTHON_DOCSTRINGS_INPUT (${DEPTHAI_PYTHON_DOCSTRINGS_INPUT})")
set(DEPTHAI_PYTHON_BUILD_DOCSTRINGS OFF CACHE BOOL "Generate docstrings from header files if module 'pybind11_mkdoc' available" FORCE)
else()
# Otherwise set default location as input
set(docstring_input_path ${DEFAULT_DOCSTRINGS_OUTPUT})
endif()
# Set default output location
set(docstring_output_path ${DEFAULT_DOCSTRINGS_OUTPUT})
endif()
if(DEPTHAI_PYTHON_BUILD_DOCSTRINGS)
option(DEPTHAI_PYTHON_FORCE_DOCSTRINGS "Force that docstrings are generated, module 'pybind11_mkdoc' required" OFF)
endif()
# Configure include placeholder with INPUT path
configure_file(cmake/docstring.hpp.in ${DOCSTRINGS_INCLUDE_PLACEHOLDER_PATH})
# Add target to generate docstrings
if (DEPTHAI_PYTHON_BUILD_DOCSTRINGS)
include(pybind11-mkdoc)
set(DEPTHAI_MKDOC_TARGETS depthai::core)
if(DEPTHAI_BASALT_SUPPORT)
list(APPEND DEPTHAI_MKDOC_TARGETS depthai::basalt)
endif()
if(DEPTHAI_RTABMAP_SUPPORT)
list(APPEND DEPTHAI_MKDOC_TARGETS depthai::rtabmap)
endif()
# Set up docstrings for combined targets
target_pybind11_mkdoc_setup_combined(${docstring_output_path} ${DEPTHAI_PYTHON_FORCE_DOCSTRINGS} ${DEPTHAI_MKDOC_TARGETS})
if(TARGET pybind11_mkdoc)
# Add dependency to mkdoc target (makes sure that mkdoc is executed, and docstrings available)
add_dependencies(${TARGET_NAME} pybind11_mkdoc)
else()
# Generate default docstrings to OUTPUT path
configure_file(cmake/default_docstring.hpp.in ${docstring_output_path} COPYONLY)
endif()
else()
# Generate default docstrings to OUTPUT path
configure_file(cmake/default_docstring.hpp.in ${docstring_output_path} COPYONLY)
endif()
# Add include directory
target_include_directories(${TARGET_NAME} PRIVATE src ${DOCSTRINGS_INCLUDE_PLACEHOLDER_DIR})
set(DEPTHAI_LINK_TARGET depthai::core)
if(NOT DEPTHAI_MERGED_TARGET)
list(APPEND DEPTHAI_LINK_TARGET depthai::opencv)
endif()
if(DEPTHAI_BASALT_SUPPORT)
list(APPEND DEPTHAI_LINK_TARGET depthai::basalt)
endif()
if(DEPTHAI_RTABMAP_SUPPORT AND DEPTHAI_PCL_SUPPORT)
list(APPEND DEPTHAI_LINK_TARGET depthai::rtabmap)
endif()
# Link with libraries
target_link_libraries(${TARGET_NAME}
PUBLIC
# pybind11
pybind11::pybind11
${DEPTHAI_LINK_TARGET}
hedley
pybind11_json
xtensor-python
)
# Add embedded module option, otherwise link to pybind11 as usual
if(DEPTHAI_PYTHON_EMBEDDED_MODULE)
target_compile_definitions(${TARGET_NAME} PRIVATE DEPTHAI_PYTHON_EMBEDDED_MODULE)
endif()
# Find Git
find_package(Git)
# Add build information (commit, date)
set(BUILD_COMMIT "dev")
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE BUILD_COMMIT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND ${GIT_EXECUTABLE} show -s --format=%ci ${BUILD_COMMIT}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE BUILD_COMMIT_DATETIME
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
# Add local commit hash (or 'dev' if unable to retrieve) if not build by CI
if(NOT DEFINED ENV{CI} AND NOT DEPTHAI_PYTHON_COMMIT_HASH)
set(DEPTHAI_PYTHON_COMMIT_HASH ${BUILD_COMMIT})
endif()
# Get version to use
set(version_command "import find_version as v; print(v.get_package_version())")
if(DEPTHAI_PYTHON_COMMIT_HASH)
set(version_command "import find_version as v; print(v.get_package_dev_version('${DEPTHAI_PYTHON_COMMIT_HASH}'))")
endif()
execute_process(COMMAND ${PYTHON_EXECUTABLE} "-c" "${version_command}"
OUTPUT_VARIABLE DEPTHAI_PYTHON_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)
string(TIMESTAMP BUILD_DATETIME "%Y-%m-%d %H:%M:%S +0000" UTC)
target_compile_definitions(${TARGET_NAME}
PRIVATE
DEPTHAI_PYTHON_VERSION="${DEPTHAI_PYTHON_VERSION}"
DEPTHAI_PYTHON_COMMIT_HASH="${BUILD_COMMIT}"
DEPTHAI_PYTHON_COMMIT_DATETIME="${BUILD_COMMIT_DATETIME}"
DEPTHAI_PYTHON_BUILD_DATETIME="${BUILD_DATETIME}"
)
# ASAN Settings as we are building and using shared library
if(SANITIZE_ADDRESS OR SANITIZE_MEMORY OR SANITIZE_THREAD OR SANITIZE_UNDEFINED)
# Get asan library to preload
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libclang_rt.asan-${CMAKE_HOST_SYSTEM_PROCESSOR}.so OUTPUT_VARIABLE LIBASAN_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libasan.so OUTPUT_VARIABLE LIBASAN_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
# Set preload env variable
if(APPLE)
set(ASAN_ENVIRONMENT_VARS "DYLD_INSERT_LIBRARIES=${LIBASAN_PATH}" "ASAN_OPTIONS=leak_check_at_exit=0")
elseif(UNIX)
set(ASAN_ENVIRONMENT_VARS "LD_PRELOAD=${LIBASAN_PATH}" "ASAN_OPTIONS=leak_check_at_exit=0")
endif()
message(STATUS "ASAN environment variables: ${ASAN_ENVIRONMENT_VARS}")
endif()
########################
# Testing
########################
if(DEPTHAI_PYTHON_ENABLE_TESTS OR DEPTHAI_PYTHON_ENABLE_EXAMPLES)
include(CTest)
enable_testing()
endif()
########################
# Tests
########################
if (DEPTHAI_PYTHON_ENABLE_TESTS)
add_subdirectory(tests)
endif()
########################
# Examples (can also act as tests)
########################
if (DEPTHAI_PYTHON_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
########################
# Documentation
########################
if(DEPTHAI_PYTHON_BUILD_DOCS)
add_subdirectory(docs)
endif()