|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +cmake_minimum_required(VERSION 2.8.3) |
| 5 | +project(azure_kinect_ros_driver LANGUAGES C CXX) |
| 6 | + |
| 7 | + |
| 8 | +if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") |
| 9 | + add_compile_options(/std:c++latest) |
| 10 | +else() |
| 11 | + add_compile_options(-std=c++11) |
| 12 | +endif() |
| 13 | + |
| 14 | +list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_CURRENT_LIST_DIR}/cmake) |
| 15 | + |
| 16 | +## Find catkin macros and libraries |
| 17 | +## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) |
| 18 | +## is used, also find other catkin packages |
| 19 | +find_package(catkin REQUIRED COMPONENTS |
| 20 | + roscpp |
| 21 | + std_msgs |
| 22 | + sensor_msgs |
| 23 | + image_transport |
| 24 | + nodelet |
| 25 | + tf2 |
| 26 | + tf2_ros |
| 27 | + tf2_geometry_msgs |
| 28 | + geometry_msgs |
| 29 | + nodelet |
| 30 | +) |
| 31 | + |
| 32 | +################################### |
| 33 | +## catkin specific configuration ## |
| 34 | +################################### |
| 35 | +## The catkin_package macro generates cmake config files for your package |
| 36 | +## Declare things to be passed to dependent projects |
| 37 | +## INCLUDE_DIRS: uncomment this if your package contains header files |
| 38 | +## LIBRARIES: libraries you create in this project that dependent projects also need |
| 39 | +## CATKIN_DEPENDS: catkin_packages dependent projects also need |
| 40 | +## DEPENDS: system dependencies of this project that dependent projects also need |
| 41 | +catkin_package( |
| 42 | +# INCLUDE_DIRS include |
| 43 | +# LIBRARIES k4a_ros_bridge |
| 44 | +# CATKIN_DEPENDS roscpp std_msgs |
| 45 | +# DEPENDS system_lib |
| 46 | +) |
| 47 | + |
| 48 | +########### |
| 49 | +## Build ## |
| 50 | +########### |
| 51 | + |
| 52 | + |
| 53 | +## Declare a C++ executable |
| 54 | +## With catkin_make all packages are built within a single CMake context |
| 55 | +## The recommended prefix ensures that target names across packages don't collide |
| 56 | +add_executable(${PROJECT_NAME}_node |
| 57 | + src/k4a_ros_bridge_node.cpp |
| 58 | + src/k4a_ros_device.cpp |
| 59 | + src/k4a_ros_device_params.cpp |
| 60 | + src/k4a_calibration_transform_data.cpp |
| 61 | + ) |
| 62 | + |
| 63 | +add_library(${PROJECT_NAME}_nodelet |
| 64 | + src/k4a_ros_bridge_nodelet.cpp |
| 65 | + src/k4a_ros_device.cpp |
| 66 | + src/k4a_ros_device_params.cpp |
| 67 | + src/k4a_calibration_transform_data.cpp |
| 68 | + ) |
| 69 | + |
| 70 | +## Rename C++ executable without prefix |
| 71 | +## The above recommended prefix causes long target names, the following renames the |
| 72 | +## target back to the shorter version for ease of user use |
| 73 | +## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" |
| 74 | +set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") |
| 75 | + |
| 76 | +############################ |
| 77 | +#### AZURE KINECT SDK ###### |
| 78 | +############################ |
| 79 | + |
| 80 | +macro(process_ext_sdk) |
| 81 | + message(STATUS "K4A SDK found in ext/sdk!") |
| 82 | + message(STATUS "!! Not searching for SDK in system path !!") |
| 83 | + |
| 84 | + # mark that the K4A SDK will need to be copied to the output binary folder |
| 85 | + # this is needed even on Linux if the SDK is not installed to the system path |
| 86 | + set(K4A_INSTALL_NEEDED true) |
| 87 | + |
| 88 | + get_target_property(K4A_INCLUDE_DIRS k4a::k4a INTERFACE_INCLUDE_DIRECTORIES) |
| 89 | + get_target_property(K4A_CONFIGS k4a::k4a IMPORTED_CONFIGURATIONS) |
| 90 | + message("Configs: ${K4A_CONFIGS}") |
| 91 | + |
| 92 | + # TODO: if we find more than one configuration, we should fail |
| 93 | + # TODO: potentially clean this logic up |
| 94 | + foreach(imported_config ${K4A_CONFIGS}) |
| 95 | + if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") |
| 96 | + get_target_property(K4A_CONFIG_LIBS k4a::k4a IMPORTED_IMPLIB_${imported_config}) |
| 97 | + get_target_property(K4A_CONFIG_DLLS k4a::k4a IMPORTED_LOCATION_${imported_config}) |
| 98 | + list(APPEND K4A_LIBS ${K4A_CONFIG_LIBS}) |
| 99 | + list(APPEND K4A_DLL_FILES ${K4A_CONFIG_DLLS}) |
| 100 | + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") |
| 101 | + get_target_property(K4A_CONFIG_LIBS k4a::k4a IMPORTED_LOCATION_${imported_config}) |
| 102 | + get_target_property(K4A_CONFIG_DLLS k4a::k4a IMPORTED_LOCATION_${imported_config}) |
| 103 | + list(APPEND K4A_LIBS ${K4A_CONFIG_LIBS}) |
| 104 | + list(APPEND K4A_DLL_FILES ${K4A_CONFIG_DLLS}) |
| 105 | + endif() |
| 106 | + endforeach() |
| 107 | + |
| 108 | + if(DEFINED DEPTHENGINE_DLL) |
| 109 | + ## Find the depth engine DLL |
| 110 | + file(GLOB_RECURSE DEPTHENGINE_DLL_FILE |
| 111 | + "${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk/*/${DEPTHENGINE_DLL}" ) |
| 112 | + list(APPEND K4A_DLL_FILES ${DEPTHENGINE_DLL_FILE}) |
| 113 | + endif() |
| 114 | +endmacro() |
| 115 | + |
| 116 | +# Define the names of some files that we are going to try and find |
| 117 | +if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows") |
| 118 | + set(DEPTHENGINE_DLL "depthengine_1_0.dll") |
| 119 | +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") |
| 120 | + set(DEPTHENGINE_DLL "libdepthengine.so.1.0") |
| 121 | +else() |
| 122 | + message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported") |
| 123 | +endif() |
| 124 | + |
| 125 | +message("Finding K4A SDK binaries") |
| 126 | + |
| 127 | +# Disable cached locations for K4A SDK binaries. |
| 128 | +# Do this to force the search logic to happen correctly. |
| 129 | +# If we don't disable these cached directories, we |
| 130 | +# won't be able to tell the difference between the ext/sdk location |
| 131 | +# and the system installed version on linux. Since we have to treat these |
| 132 | +# differently (one needs install, one doesn't) we must disable the cache |
| 133 | +# so that find_package(k4a) will fail in all cases if not installed via the .deb. |
| 134 | +unset(k4a_DIR CACHE) |
| 135 | +unset(azure-kinect-sensor-sdk_DIR CACHE) |
| 136 | + |
| 137 | +if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") |
| 138 | + message("Linux mode: Searching for K4A in system path and ./ext/sdk") |
| 139 | + |
| 140 | + # Test if we can find the k4a library in the system path |
| 141 | + find_package(k4a 1.1.0 QUIET) |
| 142 | + |
| 143 | + if (${k4a_FOUND}) |
| 144 | + message("K4A SDK found in system libraries!") |
| 145 | + # For system libraries on linux, we can just append the target name (k4a::k4a) |
| 146 | + # to what will be passed into target_link_libraries() |
| 147 | + list(APPEND K4A_LIBS k4a::k4a) |
| 148 | + else() |
| 149 | + message("K4A SDK not found in system libraries. Searching ./ext/sdk...") |
| 150 | + find_package(k4a 1.1.0 PATHS "${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk") |
| 151 | + |
| 152 | + if(${k4a_FOUND}) |
| 153 | + set(K4A_INSTALL_NEEDED true) |
| 154 | + message("K4A SDK found in ./ext/sdk!") |
| 155 | + process_ext_sdk() |
| 156 | + else() |
| 157 | + message(FATAL_ERROR "K4A SDK not found in system libraries or ./ext/sdk. Please install the Azure Kinect SDK.") |
| 158 | + endif() |
| 159 | + |
| 160 | + endif() |
| 161 | + |
| 162 | +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") |
| 163 | + # Windows will always need K4A install for all targets |
| 164 | + set(K4A_INSTALL_NEEDED true) |
| 165 | + |
| 166 | + # Try to find K4A installed to Program Files |
| 167 | + find_package(azure-kinect-sensor-sdk 1.1.0 EXACT QUIET PATHS "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 168 | + |
| 169 | + if (${azure-kinect-sensor-sdk_FOUND}) |
| 170 | + message("K4A SDK found in Program Files!") |
| 171 | + |
| 172 | + list(APPEND K4A_LIBS ${azure-kinect-sensor-sdk_LIBRARIES}) |
| 173 | + list(APPEND K4A_BINS ${azure-kinect-sensor-sdk_BINARIES}) |
| 174 | + set(K4A_INCLUDE_DIRS ${azure-kinect-sensor-sdk_INCLUDE_DIRS}) |
| 175 | + |
| 176 | + # Create a list of all K4A DLLs |
| 177 | + list(APPEND K4A_DLLS ${azure-kinect-sensor-sdk_LIBRARIES}) |
| 178 | + list(APPEND K4A_DLLS ${azure-kinect-sensor-sdk_BINARIES}) |
| 179 | + |
| 180 | + foreach(DLL ${K4A_DLLS}) |
| 181 | + set(prop "PROPERTY-NOTFOUND") |
| 182 | + get_property(prop TARGET ${DLL} PROPERTY IMPORTED_LOCATION) |
| 183 | + message(STATUS "IMPORTED_LOCATION: ${prop}") |
| 184 | + |
| 185 | + if((NOT prop) OR (prop EQUAL "PROPERTY-NOTFOUND")) |
| 186 | + message(FATAL_ERROR "Target '${DLL}' in package azure-kinect-sensor-sdk does not contain an IMPORTED_LOCATION property") |
| 187 | + endif() |
| 188 | + |
| 189 | + list(APPEND K4A_DLL_FILES ${prop}) |
| 190 | + endforeach(DLL) |
| 191 | + else() |
| 192 | + # Try to find K4A installed to ./ext/sdk |
| 193 | + find_package(k4a 1.1.0 QUIET PATHS "${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk") |
| 194 | + |
| 195 | + if (${k4a_FOUND}) |
| 196 | + message("K4A SDK found in ./ext/sdk!") |
| 197 | + process_ext_sdk() |
| 198 | + else() |
| 199 | + message(FATAL_ERROR "K4A SDK not found in Program Files or ./ext/sdk. Please install the Azure Kinect SDK.") |
| 200 | + endif() |
| 201 | + endif() |
| 202 | +endif() |
| 203 | + |
| 204 | +message("K4A Include Dirs: ${K4A_INCLUDE_DIRS}") |
| 205 | +message("K4A Libs: ${K4A_LIBS}") |
| 206 | +message("K4A DLLs: ${K4A_DLL_FILES}") |
| 207 | +message("K4A Install Needed: ${K4A_INSTALL_NEEDED}") |
| 208 | + |
| 209 | +if (${K4A_INSTALL_NEEDED}) |
| 210 | + # Tell cmake that we need to reconfigure if any of the DLL files change |
| 211 | + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${K4A_DLL_FILES}) |
| 212 | + |
| 213 | + # We need to copy the DLLs into the CATKIN_PACKAGE_LIB_DESTINATION so |
| 214 | + # the node executable can find them on launch, and CATKIN_PACKAGE_BIN_DESTINATION |
| 215 | + # so the nodelet can find them on launch |
| 216 | + set(DLL_COPY_DIRECTORY "${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_BIN_DESTINATION};${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_LIB_DESTINATION}") |
| 217 | + |
| 218 | + foreach(DIRECTORY ${DLL_COPY_DIRECTORY}) |
| 219 | + file(MAKE_DIRECTORY "${DIRECTORY}") |
| 220 | + endforeach(DIRECTORY) |
| 221 | + |
| 222 | + foreach(DLL ${K4A_DLL_FILES}) |
| 223 | + foreach(DIRECTORY ${DLL_COPY_DIRECTORY}) |
| 224 | + file(COPY "${DLL}" DESTINATION "${DIRECTORY}") |
| 225 | + get_filename_component(DLL_NAME ${DLL} NAME) |
| 226 | + message(STATUS "Copied dll from ${DLL_NAME} to ${DIRECTORY}") |
| 227 | + # Tell cmake that we need to clean up these DLLs on a "make clean" |
| 228 | + set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${DIRECTORY}/${DLL_NAME}") |
| 229 | + endforeach(DIRECTORY) |
| 230 | + endforeach(DLL) |
| 231 | + |
| 232 | +endif() |
| 233 | + |
| 234 | +################################## |
| 235 | +###### END AZURE KINECT SDK ###### |
| 236 | +################################## |
| 237 | + |
| 238 | +include_directories( |
| 239 | + ${catkin_INCLUDE_DIRS} |
| 240 | + ${K4A_INCLUDE_DIRS} |
| 241 | + "include" |
| 242 | +) |
| 243 | + |
| 244 | +target_link_libraries(${PROJECT_NAME}_node |
| 245 | + ${K4A_LIBS} |
| 246 | + ${catkin_LIBRARIES} |
| 247 | +) |
| 248 | + |
| 249 | +target_link_libraries(${PROJECT_NAME}_nodelet |
| 250 | + ${K4A_LIBS} |
| 251 | + ${catkin_LIBRARIES} |
| 252 | +) |
| 253 | + |
| 254 | +############# |
| 255 | +## Install ## |
| 256 | +############# |
| 257 | + |
| 258 | +# all install targets should use catkin DESTINATION variables |
| 259 | +# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html |
| 260 | + |
| 261 | +## Mark executables and/or libraries for installation |
| 262 | +install(TARGETS ${PROJECT_NAME}_node ${PROJECT_NAME}_nodelet |
| 263 | + ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} |
| 264 | + LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} |
| 265 | + RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} |
| 266 | +) |
| 267 | + |
| 268 | +## Mark other files for installation (e.g. launch and bag files, etc.) |
| 269 | +install( |
| 270 | + DIRECTORY |
| 271 | + launch |
| 272 | + DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} |
| 273 | +) |
| 274 | + |
| 275 | +## Run a custom install script for the K4A components |
| 276 | +## Running the two "CODE" blocks populates the cmake_install.cmake script with the information |
| 277 | +## about which DLLs to install, and where to install them. |
| 278 | +## We then run the more complex script to actually perform the installation. |
| 279 | +if (${K4A_INSTALL_NEEDED}) |
| 280 | + message("Installing K4A SDK to binary output folder") |
| 281 | + install(CODE "set(K4A_DLL_FILES \"${K4A_DLL_FILES}\")") |
| 282 | + install(CODE "set(DLL_COPY_DIRECTORY \"${CMAKE_INSTALL_PREFIX}/${CATKIN_PACKAGE_BIN_DESTINATION}\")") |
| 283 | + install(SCRIPT "./cmake/azure-kinect-sensor-sdk-install.cmake") |
| 284 | +endif() |
0 commit comments