Skip to content
33 changes: 7 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -889,32 +889,9 @@ if(DEPTHAI_USB2_PATCH_ONLY_MODE)
target_compile_definitions(${TARGET_CORE_NAME} PRIVATE DEPTHAI_PATCH_ONLY_MODE)
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()
# Include depthaiHelpers.cmake
include(${CMAKE_CURRENT_LIST_DIR}/cmake/depthaiHelpers.cmake)

# 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 @@ -1297,4 +1274,8 @@ if(DEPTHAI_INSTALL)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)

# Install vcpkg libraries that we link against dynamically
if(DEPTHAI_ENABLE_LIBUSB)
install(IMPORTED_RUNTIME_ARTIFACTS usb-1.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we also need to do that for dynamic calibration library? I also think this makes add_runtime_dependencies obsolete (at least that was the case when building deb packages for ROS)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're making a really good point here. The issue was that when I tried installing the project, libusb-1.0.so would not be present as a runtime dependency but the dynamic_calibration library would. Turns out there was a bug in the add_runtime_dependencies macro that would override the required_dll_files variable every time it was invoked, thereby causing that only the last added runtime dependency would be added.

I guess this macro is still needed for libraries that we link against with PRIVATE visibility.

endif()
endif()
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}/@PROJECT_NAME@ConfigVersion.cmake)

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
25 changes: 25 additions & 0 deletions cmake/depthaiHelpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
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(DEBUG "Required dlls for core are: ${required_dll_files}")
endif()
endmacro()
4 changes: 4 additions & 0 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 @@ -94,6 +97,7 @@ function(dai_add_example example_name example_src enable_test use_pcl)
COMMAND_EXPAND_LISTS
VERBATIM
)
add_runtime_dependencies(${example_name} usb-1.0)
endif()
endfunction()

Expand Down
Loading