|
| 1 | +# Copyright (c) 2025-present WATonomous. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +cmake_minimum_required(VERSION 3.22) |
| 16 | +project(deep_ort_gpu_backend_plugin) |
| 17 | + |
| 18 | +if(NOT CMAKE_CXX_STANDARD) |
| 19 | + set(CMAKE_CXX_STANDARD 17) |
| 20 | +endif() |
| 21 | + |
| 22 | +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 23 | + add_compile_options(-Wall -Wextra -Wpedantic) |
| 24 | + add_link_options(-Wl,-no-undefined) |
| 25 | +endif() |
| 26 | + |
| 27 | +find_package(ament_cmake REQUIRED) |
| 28 | +find_package(deep_core REQUIRED) |
| 29 | + |
| 30 | +# Create dummy CUDA targets early to prevent onnxruntime from expecting real ones |
| 31 | +if(NOT TARGET CUDA::cudart) |
| 32 | + add_library(CUDA::cudart INTERFACE IMPORTED) |
| 33 | + message(STATUS "Created dummy CUDA::cudart target") |
| 34 | +endif() |
| 35 | +if(NOT TARGET CUDA::cublas) |
| 36 | + add_library(CUDA::cublas INTERFACE IMPORTED) |
| 37 | + message(STATUS "Created dummy CUDA::cublas target") |
| 38 | +endif() |
| 39 | +if(NOT TARGET CUDA::curand) |
| 40 | + add_library(CUDA::curand INTERFACE IMPORTED) |
| 41 | + message(STATUS "Created dummy CUDA::curand target") |
| 42 | +endif() |
| 43 | +if(NOT TARGET CUDA::cufft) |
| 44 | + add_library(CUDA::cufft INTERFACE IMPORTED) |
| 45 | + message(STATUS "Created dummy CUDA::cufft target") |
| 46 | +endif() |
| 47 | +if(NOT TARGET CUDA::cusparse) |
| 48 | + add_library(CUDA::cusparse INTERFACE IMPORTED) |
| 49 | + message(STATUS "Created dummy CUDA::cusparse target") |
| 50 | +endif() |
| 51 | +if(NOT TARGET CUDA::cusolver) |
| 52 | + add_library(CUDA::cusolver INTERFACE IMPORTED) |
| 53 | + message(STATUS "Created dummy CUDA::cusolver target") |
| 54 | +endif() |
| 55 | + |
| 56 | +find_package(onnxruntime_gpu_vendor REQUIRED) |
| 57 | +find_package(pluginlib REQUIRED) |
| 58 | +find_package(rclcpp REQUIRED) |
| 59 | +find_package(rclcpp_lifecycle REQUIRED) |
| 60 | + |
| 61 | +# Use CMake's built-in CUDA support with minimal requirements |
| 62 | +find_package(CUDAToolkit QUIET) |
| 63 | + |
| 64 | +# Check for specific CUDA libraries that might be required |
| 65 | +if(CUDAToolkit_FOUND) |
| 66 | + # Check if all required CUDA components are available |
| 67 | + set(CUDA_LIBS_AVAILABLE TRUE) |
| 68 | + |
| 69 | + if(NOT TARGET CUDA::cudart) |
| 70 | + message(WARNING "CUDA::cudart not found") |
| 71 | + set(CUDA_LIBS_AVAILABLE FALSE) |
| 72 | + endif() |
| 73 | + |
| 74 | + # Some dependencies might require these, so check if they exist |
| 75 | + if(NOT TARGET CUDA::cublas) |
| 76 | + message(WARNING "CUDA::cublas not found - will try to continue without it") |
| 77 | + endif() |
| 78 | + |
| 79 | + if(NOT TARGET CUDA::curand) |
| 80 | + message(WARNING "CUDA::curand not found - will try to continue without it") |
| 81 | + endif() |
| 82 | + |
| 83 | + if(CUDA_LIBS_AVAILABLE) |
| 84 | + message(STATUS "Found CUDA using CUDAToolkit with cudart") |
| 85 | + set(CUDA_FOUND TRUE) |
| 86 | + else() |
| 87 | + message(WARNING "CUDAToolkit found but missing essential components") |
| 88 | + set(CUDAToolkit_FOUND FALSE) |
| 89 | + set(CUDA_FOUND FALSE) |
| 90 | + endif() |
| 91 | +endif() |
| 92 | + |
| 93 | +# Fallback: Try to find CUDA manually if CUDAToolkit fails or is incomplete |
| 94 | +if(NOT CUDAToolkit_FOUND) |
| 95 | + # Find CUDA runtime libraries (without requiring nvcc/development toolkit) |
| 96 | + # Try multiple possible locations for CUDA headers and libraries |
| 97 | + find_path(CUDA_INCLUDE_DIR |
| 98 | + NAMES cuda_runtime.h |
| 99 | + PATHS |
| 100 | + /local/cuda/include |
| 101 | + /local/cuda-12.6/include |
| 102 | + /usr/local/cuda-12.6/targets/x86_64-linux/include |
| 103 | + /usr/local/cuda/targets/x86_64-linux/include |
| 104 | + /usr/include |
| 105 | + /usr/include/cuda |
| 106 | + /usr/local/cuda/include |
| 107 | + /usr/local/cuda-12/include |
| 108 | + /usr/local/cuda-12.6/include |
| 109 | + /opt/cuda/include |
| 110 | + DOC "CUDA include directory" |
| 111 | + ) |
| 112 | + |
| 113 | + find_library(CUDA_RUNTIME_LIBRARY |
| 114 | + NAMES cudart |
| 115 | + PATHS |
| 116 | + /local/cuda/lib64 |
| 117 | + /local/cuda-12.6/lib64 |
| 118 | + /usr/lib/x86_64-linux-gnu |
| 119 | + /usr/local/cuda/lib64 |
| 120 | + /usr/local/cuda-12/lib64 |
| 121 | + /usr/local/cuda-12.6/lib64 |
| 122 | + /usr/local/cuda/lib |
| 123 | + /opt/cuda/lib64 |
| 124 | + DOC "CUDA runtime library" |
| 125 | + ) |
| 126 | + |
| 127 | + if(CUDA_INCLUDE_DIR AND CUDA_RUNTIME_LIBRARY) |
| 128 | + message(STATUS "Found CUDA runtime: ${CUDA_RUNTIME_LIBRARY}") |
| 129 | + message(STATUS "Found CUDA headers: ${CUDA_INCLUDE_DIR}") |
| 130 | + set(CUDA_FOUND TRUE) |
| 131 | + else() |
| 132 | + message(WARNING "CUDA runtime not found - skipping tests that require CUDA") |
| 133 | + set(CUDA_FOUND FALSE) |
| 134 | + endif() |
| 135 | +else() |
| 136 | + message(STATUS "Found CUDA using CUDAToolkit") |
| 137 | + set(CUDA_FOUND TRUE) |
| 138 | +endif() |
| 139 | + |
| 140 | +set(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 141 | + |
| 142 | +# deep_ort_gpu_backend_plugin library |
| 143 | +set(DEEP_ORT_LIB ${PROJECT_NAME}_lib) |
| 144 | +add_library(${DEEP_ORT_LIB} SHARED |
| 145 | + src/ort_gpu_memory_allocator.cpp |
| 146 | + src/ort_gpu_backend_executor.cpp |
| 147 | + src/ort_gpu_backend_plugin.cpp |
| 148 | +) |
| 149 | + |
| 150 | +target_include_directories(${DEEP_ORT_LIB} PUBLIC |
| 151 | + $<BUILD_INTERFACE:${include_dir}> |
| 152 | + $<INSTALL_INTERFACE:include> |
| 153 | +) |
| 154 | + |
| 155 | +# Add common link libraries (always present) |
| 156 | +target_link_libraries(${DEEP_ORT_LIB} |
| 157 | + PUBLIC |
| 158 | + pluginlib::pluginlib |
| 159 | + rclcpp::rclcpp |
| 160 | + PRIVATE |
| 161 | + deep_core::deep_core_lib |
| 162 | + onnxruntime_gpu_vendor::onnxruntime_gpu_lib |
| 163 | +) |
| 164 | + |
| 165 | +# CUDA runtime is required since code calls CUDA functions directly |
| 166 | +# Try multiple strategies to find and link CUDA runtime |
| 167 | +set(CUDA_RUNTIME_LINKED FALSE) |
| 168 | + |
| 169 | +# Strategy 1: Use manually found CUDA runtime |
| 170 | +if(CUDA_FOUND AND CUDA_RUNTIME_LIBRARY AND NOT CUDAToolkit_FOUND) |
| 171 | + target_link_libraries(${DEEP_ORT_LIB} PRIVATE ${CUDA_RUNTIME_LIBRARY}) |
| 172 | + message(STATUS "Linking manual CUDA runtime: ${CUDA_RUNTIME_LIBRARY}") |
| 173 | + set(CUDA_RUNTIME_LINKED TRUE) |
| 174 | +endif() |
| 175 | + |
| 176 | +# Strategy 2: Use CUDAToolkit's cudart target |
| 177 | +if(NOT CUDA_RUNTIME_LINKED AND CUDAToolkit_FOUND) |
| 178 | + # Override the dummy target with the real one if available |
| 179 | + if(TARGET CUDA::cudart_static) |
| 180 | + target_link_libraries(${DEEP_ORT_LIB} PRIVATE CUDA::cudart_static) |
| 181 | + message(STATUS "Linking CUDAToolkit cudart_static") |
| 182 | + set(CUDA_RUNTIME_LINKED TRUE) |
| 183 | + elseif(TARGET CUDA::cudart) |
| 184 | + # Check if it's not just our dummy target |
| 185 | + get_target_property(CUDART_TYPE CUDA::cudart TYPE) |
| 186 | + if(NOT CUDART_TYPE STREQUAL "INTERFACE_LIBRARY") |
| 187 | + target_link_libraries(${DEEP_ORT_LIB} PRIVATE CUDA::cudart) |
| 188 | + message(STATUS "Linking CUDAToolkit cudart") |
| 189 | + set(CUDA_RUNTIME_LINKED TRUE) |
| 190 | + endif() |
| 191 | + endif() |
| 192 | +endif() |
| 193 | + |
| 194 | +# Strategy 3: Search more aggressively for any CUDA runtime library |
| 195 | +if(NOT CUDA_RUNTIME_LINKED) |
| 196 | + find_library(AGGRESSIVE_CUDA_RUNTIME_LIBRARY |
| 197 | + NAMES cudart libcudart cudart.so cudart.so.12 cudart.so.11 cudart.so.10 |
| 198 | + PATHS |
| 199 | + /local/cuda/lib64 |
| 200 | + /local/cuda-12.6/lib64 |
| 201 | + /local/cuda-12/lib64 |
| 202 | + /local/cuda-11/lib64 |
| 203 | + /usr/lib/x86_64-linux-gnu |
| 204 | + /usr/local/cuda/lib64 |
| 205 | + /usr/local/cuda-12/lib64 |
| 206 | + /usr/local/cuda-12.6/lib64 |
| 207 | + /usr/local/cuda-11/lib64 |
| 208 | + /usr/local/cuda/lib |
| 209 | + /opt/cuda/lib64 |
| 210 | + /usr/lib64 |
| 211 | + /lib64 |
| 212 | + /lib/x86_64-linux-gnu |
| 213 | + NO_DEFAULT_PATH |
| 214 | + ) |
| 215 | + |
| 216 | + if(AGGRESSIVE_CUDA_RUNTIME_LIBRARY) |
| 217 | + target_link_libraries(${DEEP_ORT_LIB} PRIVATE ${AGGRESSIVE_CUDA_RUNTIME_LIBRARY}) |
| 218 | + message(STATUS "Linking aggressive search CUDA runtime: ${AGGRESSIVE_CUDA_RUNTIME_LIBRARY}") |
| 219 | + set(CUDA_RUNTIME_LINKED TRUE) |
| 220 | + endif() |
| 221 | +endif() |
| 222 | + |
| 223 | +# Strategy 4: Try system-wide search as last resort |
| 224 | +if(NOT CUDA_RUNTIME_LINKED) |
| 225 | + find_library(SYSTEM_CUDA_RUNTIME_LIBRARY NAMES cudart) |
| 226 | + if(SYSTEM_CUDA_RUNTIME_LIBRARY) |
| 227 | + target_link_libraries(${DEEP_ORT_LIB} PRIVATE ${SYSTEM_CUDA_RUNTIME_LIBRARY}) |
| 228 | + message(STATUS "Linking system CUDA runtime: ${SYSTEM_CUDA_RUNTIME_LIBRARY}") |
| 229 | + set(CUDA_RUNTIME_LINKED TRUE) |
| 230 | + endif() |
| 231 | +endif() |
| 232 | + |
| 233 | +if(NOT CUDA_RUNTIME_LINKED) |
| 234 | + message(FATAL_ERROR "No CUDA runtime found - required for GPU functionality") |
| 235 | +endif() |
| 236 | + |
| 237 | +# Add CUDA include directories if found |
| 238 | +if(CUDAToolkit_FOUND) |
| 239 | + target_include_directories(${DEEP_ORT_LIB} PRIVATE ${CUDAToolkit_INCLUDE_DIRS}) |
| 240 | +elseif(CUDA_FOUND) |
| 241 | + target_include_directories(${DEEP_ORT_LIB} PRIVATE ${CUDA_INCLUDE_DIR}) |
| 242 | +endif() |
| 243 | + |
| 244 | +install(TARGETS |
| 245 | + ${DEEP_ORT_LIB} |
| 246 | + EXPORT ${PROJECT_NAME}Targets |
| 247 | + ARCHIVE DESTINATION lib |
| 248 | + LIBRARY DESTINATION lib |
| 249 | + RUNTIME DESTINATION bin |
| 250 | +) |
| 251 | + |
| 252 | +install(EXPORT ${PROJECT_NAME}Targets |
| 253 | + NAMESPACE ${PROJECT_NAME}:: |
| 254 | + DESTINATION share/${PROJECT_NAME}/cmake |
| 255 | +) |
| 256 | + |
| 257 | +install(DIRECTORY include/ |
| 258 | + DESTINATION include |
| 259 | +) |
| 260 | + |
| 261 | +install(FILES plugins.xml |
| 262 | + DESTINATION share/${PROJECT_NAME} |
| 263 | +) |
| 264 | + |
| 265 | +# Export plugin description file to ament index |
| 266 | +pluginlib_export_plugin_description_file(deep_core plugins.xml) |
| 267 | + |
| 268 | +# if(BUILD_TESTING) |
| 269 | +# find_package(deep_test REQUIRED) |
| 270 | + |
| 271 | +# # add_deep_test(test_ort_gpu_backend test/test_ort_gpu_backend.cpp |
| 272 | +# # LIBRARIES |
| 273 | +# # ${DEEP_ORT_LIB} |
| 274 | +# # deep_core::deep_core_lib |
| 275 | +# # onnxruntime_gpu_vendor::onnxruntime_gpu_lib |
| 276 | +# # ) |
| 277 | + |
| 278 | +# endif() |
| 279 | + |
| 280 | +ament_export_targets(${PROJECT_NAME}Targets HAS_LIBRARY_TARGET) |
| 281 | +ament_export_libraries(${DEEP_ORT_LIB}) |
| 282 | +ament_package() |
0 commit comments