Skip to content

Commit 0d758a7

Browse files
author
Stuart Alldritt
committed
Correct behavior for CMakeLists.txt on Linux
1 parent c19f2a6 commit 0d758a7

File tree

1 file changed

+76
-43
lines changed

1 file changed

+76
-43
lines changed

CMakeLists.txt

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,7 @@ set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX ""
7777
#### AZURE KINECT SDK ######
7878
############################
7979

80-
# Define the names of some files that we are going to try and find
81-
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
82-
set(DEPTHENGINE_DLL "depthengine_1_0.dll")
83-
set(LIBUSB_DLL "libusb-1.0.dll")
84-
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
85-
set(DEPTHENGINE_DLL "libdepthengine.so.1.0")
86-
else()
87-
message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported")
88-
endif()
89-
90-
message("Finding K4A SDK binaries")
91-
92-
# First try to find an SDK in the ext/sdk folder
93-
# Looking in this folder should be platform agnostic
94-
find_package(k4a 1.1.0 QUIET
95-
PATHS "${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk")
96-
97-
if (${k4a_FOUND})
80+
macro(process_ext_sdk)
9881
message(STATUS "K4A SDK found in ext/sdk!")
9982
message(STATUS "!! Not searching for SDK in system path !!")
10083

@@ -128,23 +111,62 @@ if (${k4a_FOUND})
128111
"${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk/*/${DEPTHENGINE_DLL}" )
129112
list(APPEND K4A_DLL_FILES ${DEPTHENGINE_DLL_FILE})
130113
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()
131159

132-
if(DEFINED LIBUSB_DLL)
133-
## Find LibUSB
134-
file(GLOB_RECURSE LIBUSB_DLL_FILE
135-
"${CMAKE_CURRENT_SOURCE_DIR}/ext/sdk/*/${LIBUSB_DLL}" )
136-
list(APPEND K4A_DLL_FILES ${LIBUSB_DLL_FILE})
137160
endif()
138161

139-
else()
140-
141-
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
142-
# Windows will always need K4A install
143-
set(K4A_INSTALL_NEEDED true)
162+
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
163+
# Windows will always need K4A install for all targets
164+
set(K4A_INSTALL_NEEDED true)
144165

145-
find_package(azure-kinect-sensor-sdk 1.1.0 EXACT REQUIRED
146-
PATHS "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
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")
147168

169+
if (${k4a_FOUND})
148170
message("K4A SDK found in Program Files!")
149171

150172
list(APPEND K4A_LIBS ${azure-kinect-sensor-sdk_LIBRARIES})
@@ -166,11 +188,17 @@ else()
166188

167189
list(APPEND K4A_DLL_FILES ${prop})
168190
endforeach(DLL)
169-
170-
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
171-
# TODO: linux support for preinstalled packages
172-
message(FATAL_ERROR "Platform ${CMAKE_SYSTEM_NAME} is not supported for system installs yet. Please extract an SDK and Depth Engine to ext/sdk")
173-
endif()
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()
174202
endif()
175203

176204
message("K4A Include Dirs: ${K4A_INCLUDE_DIRS}")
@@ -179,11 +207,11 @@ message("K4A DLLs: ${K4A_DLL_FILES}")
179207
message("K4A Install Needed: ${K4A_INSTALL_NEEDED}")
180208

181209
if (${K4A_INSTALL_NEEDED})
182-
## Tell cmake that we need to reconfigure if any of the DLL files change
210+
# Tell cmake that we need to reconfigure if any of the DLL files change
183211
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${K4A_DLL_FILES})
184212

185-
## We need to copy the DLLs into the CATKIN_PACKAGE_LIB_DESTINATION so
186-
## the node executable can find them on launch
213+
# We need to copy the DLLs into the CATKIN_PACKAGE_LIB_DESTINATION so
214+
# the node executable can find them on launch
187215
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
188216
set(DLL_COPY_DIRECTORY "${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_BIN_DESTINATION}")
189217
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
@@ -192,14 +220,19 @@ if (${K4A_INSTALL_NEEDED})
192220

193221
file(MAKE_DIRECTORY "${DLL_COPY_DIRECTORY}")
194222

195-
# TODO: this code only runs on initial compilation. If the underlying SDK is updated,
196-
# the new binaries aren't copied into the ROS devel directory. Somehow, cmake needs to be made
197-
# aware that these files are dependencies to be tracked.
198-
199223
foreach(DLL ${K4A_DLL_FILES})
200224
file(COPY "${DLL}" DESTINATION "${DLL_COPY_DIRECTORY}")
201-
message(STATUS "Copied dll from ${DLL} to ${DLL_COPY_DIRECTORY}")
225+
226+
get_filename_component(DLL_NAME ${DLL} NAME)
227+
228+
message(STATUS "Copied dll from ${DLL_NAME} to ${DLL_COPY_DIRECTORY}")
229+
230+
# Tell cmake that we need to clean up these DLLs on a "make clean"
231+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${DLL_COPY_DIRECTORY}/${DLL_NAME}")
202232
endforeach(DLL)
233+
234+
235+
203236
endif()
204237

205238
##################################

0 commit comments

Comments
 (0)