Skip to content
30 changes: 3 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -896,36 +896,13 @@ if(DEPTHAI_USB2_PATCH_ONLY_MODE)
target_compile_definitions(${TARGET_CORE_NAME} PRIVATE DEPTHAI_PATCH_ONLY_MODE)
endif()

# Include depthaiHelpers.cmake
include(${CMAKE_CURRENT_LIST_DIR}/cmake/depthaiHelpers.cmake)

if(DEPTHAI_INTERNAL_DEVICE_BUILD_RVC4)
target_compile_definitions(${TARGET_CORE_NAME} PRIVATE DEPTHAI_INTERNAL_DEVICE_BUILD_RVC4)
endif()

# Helper function
macro(add_runtime_dependencies depending_target dependency)
if(TARGET ${dependency})
get_property(imported_configs TARGET ${dependency} PROPERTY IMPORTED_CONFIGURATIONS)
set(dlls "")
message(STATUS "Adding runtime dependencies for ${depending_target} on ${dependency}. Imported configurations: ${imported_configs}")
foreach(cfg ${imported_configs})
message(STATUS "Adding runtime dependencies for ${depending_target} on ${dependency} (${cfg})")
get_property(dll TARGET ${dependency} PROPERTY IMPORTED_LOCATION_${cfg})
message(STATUS "Retrieved dll for ${cfg}: '${dll}'")
list(APPEND dlls $<$<CONFIG:${cfg}>:${dll}>)
endforeach()
message(STATUS "Required dlls for ${depending_target} on ${dependency} are: ${dlls}")
endif()
# Create a list of required dll files
set(required_dll_files ${dlls})
# Copy the required dlls
if(WIN32)
add_custom_command(TARGET ${depending_target} POST_BUILD COMMAND
"$<$<BOOL:${required_dll_files}>:${CMAKE_COMMAND};-E;copy_if_different;${required_dll_files};$<TARGET_FILE_DIR:${depending_target}>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
message(STATUS "Required dlls for core are: ${required_dll_files}")
endif()
endmacro()
# Add libusb dll in build time
add_runtime_dependencies(${TARGET_CORE_NAME} usb-1.0)
# Add dynamic calibration dll in build time
Expand Down Expand Up @@ -1313,5 +1290,4 @@ if(DEPTHAI_INSTALL)
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)

endif()
31 changes: 31 additions & 0 deletions cmake/CreateSymlinks.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# CreateSymlinks.cmake - Creates symbolic links for DLLs
# Usage: cmake -DDLLS="dll1;dll2;..." -DDEST_DIR="path" -P CreateSymlinks.cmake

message(DEBUG "Creating symlinks: ${DLLS} \n Destination path: ${DEST_DIR}")

if(NOT DEFINED DLLS OR NOT DEFINED DEST_DIR)
return()
endif()

foreach(dll IN LISTS DLLS)
# Extract dll_name (without path)
get_filename_component(dll_name "${dll}" NAME)

# Create destination
set(link_path "${DEST_DIR}/${dll_name}")

# Remove original symlink if it exists
if(EXISTS "${link_path}")
file(REMOVE "${link_path}")
endif()

message(DEBUG "Creating symlink: ${link_path} ---> ${dll}")

# Create symbolic link
file(CREATE_LINK "${dll}" "${link_path}" SYMBOLIC RESULT result_var)

if(result_var)
message(WARNING "Failed to create symlink from '${dll}' to '${link_path}': ${result_var}")
endif()

endforeach()
8 changes: 8 additions & 0 deletions cmake/depthaiConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
include(CMakeFindDependencyMacro)

get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(${CMAKE_CURRENT_LIST_DIR}/@[email protected])

message(STATUS "Found depthai: ${CMAKE_CURRENT_LIST_DIR} (found version: ${PACKAGE_VERSION})")

# Assume we are in <install-prefix>/lib/cmake/depthai/depthaiConfig.cmake
get_filename_component(DEPTHAI_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

find_dependency(nlohmann_json CONFIG REQUIRED)
find_dependency(libnop CONFIG REQUIRED)
find_dependency(XLink CONFIG REQUIRED COMPONENTS XLinkPublic)
Expand Down
60 changes: 60 additions & 0 deletions cmake/depthaiHelpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
macro(add_runtime_dependencies depending_target dependency)
if(TARGET ${dependency})
get_property(imported_configs TARGET ${dependency} PROPERTY IMPORTED_CONFIGURATIONS)
set(dlls "")
message(DEBUG "Adding runtime dependencies for ${depending_target} on ${dependency}. Imported configurations: ${imported_configs}")
foreach(cfg ${imported_configs})
message(DEBUG "Adding runtime dependencies for ${depending_target} on ${dependency} (${cfg})")
get_property(dll TARGET ${dependency} PROPERTY IMPORTED_LOCATION_${cfg})
message(DEBUG "Retrieved dll for ${cfg}: '${dll}'")
list(APPEND dlls $<$<CONFIG:${cfg}>:${dll}>)
endforeach()
message(DEBUG "Required dlls for ${depending_target} on ${dependency} are: ${dlls}")
Comment on lines +34 to +41
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug messages contain inconsistent terminology - some refer to 'core' while others use the actual target name. Line 23 should use '${depending_target}' instead of hardcoded 'core' for consistency.

Copilot uses AI. Check for mistakes.
endif()
# Create a list of required dll files
if(DEFINED required_dll_files)
list(APPEND required_dll_files ${dlls})
else()
set(required_dll_files ${dlls})
endif()
# Copy the required dlls
if(WIN32)
add_custom_command(TARGET ${depending_target} POST_BUILD COMMAND
${CMAKE_COMMAND} -E copy_if_different ${dlls} ${depthai_dll_libraries} $<TARGET_FILE_DIR:${depending_target}>
COMMAND_EXPAND_LISTS
)
message(DEBUG "Required dlls for ${depending_target} are: ${required_dll_files}")
endif()
endmacro()

macro(add_runtime_dependencies_symlinked depending_target dependency)
if(TARGET ${dependency})
get_property(imported_configs TARGET ${dependency} PROPERTY IMPORTED_CONFIGURATIONS)
set(dlls "")
message(DEBUG "Adding runtime dependencies for ${depending_target} on ${dependency}. Imported configurations: ${imported_configs}")
foreach(cfg ${imported_configs})
message(DEBUG "Adding runtime dependencies for ${depending_target} on ${dependency} (${cfg})")
get_property(dll TARGET ${dependency} PROPERTY IMPORTED_LOCATION_${cfg})
message(DEBUG "Retrieved dll for ${cfg}: '${dll}'")
list(APPEND dlls $<$<CONFIG:${cfg}>:${dll}>)
endforeach()
message(DEBUG "Required dlls for ${depending_target} on ${dependency} are: ${dlls}")
endif()
# Create a list of required dll files
if(DEFINED required_dll_files)
list(APPEND required_dll_files ${dlls})
else()
set(required_dll_files ${dlls})
endif()
# Copy the required dlls
if(WIN32)
foreach(dll_path IN LISTS required_dll_files)
add_custom_command(TARGET ${depending_target} POST_BUILD COMMAND
${CMAKE_COMMAND} -DDLLS=${dll_path} -DDEST_DIR=$<TARGET_FILE_DIR:${depending_target}>
-P "${CMAKE_SOURCE_DIR}/cmake/CreateSymlinks.cmake"
VERBATIM
)
endforeach()
message(DEBUG "Required dlls for ${depending_target} are: ${required_dll_files}")
endif()
endmacro()
16 changes: 11 additions & 5 deletions examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ target_include_directories(utility PUBLIC "utility" "$<BUILD_INTERFACE:${FP16_IN
add_default_flags(utility LEAN)
target_link_libraries(utility ${OpenCV_LIBS})

# Include depthaiHelpers.cmake
include(${CMAKE_CURRENT_LIST_DIR}/../../cmake/depthaiHelpers.cmake)

set(test_env
# Misc
"UBSAN_OPTIONS=halt_on_error=1"
Expand Down Expand Up @@ -89,11 +92,14 @@ function(dai_add_example example_name example_src enable_test use_pcl)
else()
set(depthai_dll_libraries "$<TARGET_RUNTIME_DLLS:${example_name}>")
endif()
add_custom_command(TARGET ${example_name} POST_BUILD COMMAND
"$<$<BOOL:${depthai_dll_libraries}>:${CMAKE_COMMAND};-E;copy_if_different;${depthai_dll_libraries};$<TARGET_FILE_DIR:${example_name}>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
foreach(dll_path IN LISTS depthai_dll_libraries)
add_custom_command(TARGET ${example_name} POST_BUILD COMMAND
${CMAKE_COMMAND} -DDLLS=${dll_path} -DDEST_DIR=$<TARGET_FILE_DIR:${example_name}>
-P "${CMAKE_SOURCE_DIR}/cmake/CreateSymlinks.cmake"
VERBATIM
)
endforeach()
add_runtime_dependencies_symlinked(${example_name} usb-1.0)
endif()
endfunction()

Expand Down
12 changes: 7 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ function(dai_add_test test_name test_src)
else()
set(depthai_dll_libraries "$<TARGET_RUNTIME_DLLS:${test_name}>")
endif()
add_custom_command(TARGET ${test_name} POST_BUILD COMMAND
"$<$<BOOL:${depthai_dll_libraries}>:${CMAKE_COMMAND};-E;copy_if_different;${depthai_dll_libraries};$<TARGET_FILE_DIR:${test_name}>>"
COMMAND_EXPAND_LISTS
VERBATIM
)
foreach(dll_path IN LISTS depthai_dll_libraries)
add_custom_command(TARGET ${test_name} POST_BUILD COMMAND
${CMAKE_COMMAND} -DDLLS=${dll_path} -DDEST_DIR=$<TARGET_FILE_DIR:${test_name}>
-P "${CMAKE_SOURCE_DIR}/cmake/CreateSymlinks.cmake"
VERBATIM
)
endforeach()
endif()
endfunction()

Expand Down
Loading