| 1 |
if(COLMAP_FIND_QUIETLY) |
| 2 |
set(COLMAP_FIND_TYPE QUIET) |
| 3 |
else() |
| 4 |
set(COLMAP_FIND_TYPE REQUIRED) |
| 5 |
endif() |
| 6 |
|
| 7 |
# Track all the compile definitions |
| 8 |
set(COLMAP_COMPILE_DEFINITIONS) |
| 9 |
if(LSD_ENABLED) |
| 10 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_LSD_ENABLED) |
| 11 |
message(STATUS "Enabling LSD support") |
| 12 |
else() |
| 13 |
message(STATUS "Disabling LSD support") |
| 14 |
endif() |
| 15 |
|
| 16 |
find_package(OpenMP REQUIRED COMPONENTS C CXX) |
| 17 |
|
| 18 |
find_package(Boost ${COLMAP_FIND_TYPE} COMPONENTS |
| 19 |
graph |
| 20 |
program_options |
| 21 |
OPTIONAL_COMPONENTS |
| 22 |
system) |
| 23 |
|
| 24 |
# Hash map backend selection for the scene/SfM containers. Adds the compile |
| 25 |
# definition consumed by src/colmap/util/hash_containers.h. Both backends are |
| 26 |
# header-only (boost-unordered is provided by the Boost::boost target), so no |
| 27 |
# extra linking is required. |
| 28 |
# |
| 29 |
# BOOST (boost::unordered_flat/node maps) is preferred, but its node maps |
| 30 |
# (boost::unordered_node_map) require Boost >= 1.84. When COLMAP_HASH_MAP_BACKEND |
| 31 |
# is empty we auto-select BOOST if the found Boost is new enough, else STD (so |
| 32 |
# e.g. builds against the system Boost on older distributions keep working). |
| 33 |
# |
| 34 |
# Note: downstream consumers re-run this file via find_package(colmap) with |
| 35 |
# COLMAP_HASH_MAP_BACKEND unset; they get the actual COLMAP_HASH_* macro from the |
| 36 |
# exported colmap targets, so the value re-derived here is only used to keep the |
| 37 |
# selection message and any local sources consistent. |
| 38 |
set(COLMAP_HASH_MAP_BACKEND_MIN_BOOST_VERSION "1.84.0") |
| 39 |
if(DEFINED Boost_VERSION_STRING AND Boost_VERSION_STRING) |
| 40 |
set(_colmap_boost_version "${Boost_VERSION_STRING}") |
| 41 |
else() |
| 42 |
set(_colmap_boost_version "${Boost_VERSION}") |
| 43 |
endif() |
| 44 |
string(TOUPPER "${COLMAP_HASH_MAP_BACKEND}" COLMAP_HASH_MAP_BACKEND) |
| 45 |
if(NOT COLMAP_HASH_MAP_BACKEND) |
| 46 |
if(_colmap_boost_version VERSION_GREATER_EQUAL |
| 47 |
"${COLMAP_HASH_MAP_BACKEND_MIN_BOOST_VERSION}") |
| 48 |
set(COLMAP_HASH_MAP_BACKEND "BOOST") |
| 49 |
else() |
| 50 |
set(COLMAP_HASH_MAP_BACKEND "STD") |
| 51 |
endif() |
| 52 |
endif() |
| 53 |
if(COLMAP_HASH_MAP_BACKEND STREQUAL "STD") |
| 54 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_HASH_STD) |
| 55 |
elseif(COLMAP_HASH_MAP_BACKEND STREQUAL "BOOST") |
| 56 |
if(_colmap_boost_version VERSION_LESS |
| 57 |
"${COLMAP_HASH_MAP_BACKEND_MIN_BOOST_VERSION}") |
| 58 |
message(FATAL_ERROR |
| 59 |
"COLMAP_HASH_MAP_BACKEND=BOOST requires Boost >= " |
| 60 |
"${COLMAP_HASH_MAP_BACKEND_MIN_BOOST_VERSION} " |
| 61 |
"(boost::unordered_node_map), but found Boost " |
| 62 |
"${_colmap_boost_version}. Upgrade Boost or set " |
| 63 |
"-DCOLMAP_HASH_MAP_BACKEND=STD.") |
| 64 |
endif() |
| 65 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_HASH_BOOST) |
| 66 |
else() |
| 67 |
message(FATAL_ERROR "Unknown COLMAP_HASH_MAP_BACKEND " |
| 68 |
"'${COLMAP_HASH_MAP_BACKEND}' (expected STD, BOOST or empty)") |
| 69 |
endif() |
| 70 |
message(STATUS "Using ${COLMAP_HASH_MAP_BACKEND} hash map backend " |
| 71 |
"(Boost ${_colmap_boost_version})") |
| 72 |
|
| 73 |
find_package(Eigen3 ${COLMAP_FIND_TYPE}) |
| 74 |
|
| 75 |
find_package(OpenImageIO ${COLMAP_FIND_TYPE}) |
| 76 |
|
| 77 |
find_package(Metis ${COLMAP_FIND_TYPE}) |
| 78 |
|
| 79 |
find_package(SQLite3 ${COLMAP_FIND_TYPE}) |
| 80 |
# Older CMake versions define SQLite::SQLite3 instead of SQLite3::SQLite3. |
| 81 |
if(NOT TARGET SQLite3::SQLite3 AND TARGET SQLite::SQLite3) |
| 82 |
add_library(SQLite3::SQLite3 ALIAS SQLite::SQLite3) |
| 83 |
endif() |
| 84 |
|
| 85 |
set(OpenGL_GL_PREFERENCE GLVND) |
| 86 |
find_package(OpenGL ${COLMAP_FIND_TYPE}) |
| 87 |
|
| 88 |
find_package(Glew ${COLMAP_FIND_TYPE}) |
| 89 |
|
| 90 |
find_package(Git) |
| 91 |
|
| 92 |
find_package(CHOLMOD REQUIRED) |
| 93 |
|
| 94 |
# Ceres and glog expose gflags::gflags in their interface dependencies, but gflags |
| 95 |
# only defines the namespaced target when GFLAGS_USE_TARGET_NAMESPACE is ON. Bridge |
| 96 |
# the gap so consumers can resolve the expected target when only the plain target exists. |
| 97 |
find_package(gflags CONFIG QUIET) |
| 98 |
if(NOT TARGET gflags::gflags AND TARGET gflags) |
| 99 |
add_library(gflags::gflags ALIAS gflags) |
| 100 |
endif() |
| 101 |
|
| 102 |
# Ceres is found before Glog on purpose. Some distributions (e.g. Fedora) ship a |
| 103 |
# Ceres whose bundled FindGlog.cmake unconditionally calls add_library(glog::glog) |
| 104 |
# in module mode. If we created the glog::glog target first, that call collides |
| 105 |
# with a "target already exists" error (see issue #3347). By finding Ceres first, |
| 106 |
# Ceres creates glog::glog itself, and our subsequent find_package(Glog) reuses |
| 107 |
# the existing target instead. |
| 108 |
find_package(Ceres ${COLMAP_FIND_TYPE}) |
| 109 |
if(NOT TARGET Ceres::ceres) |
| 110 |
# Older Ceres versions don't come with an imported interface target. |
| 111 |
add_library(Ceres::ceres INTERFACE IMPORTED) |
| 112 |
target_include_directories( |
| 113 |
Ceres::ceres INTERFACE ${CERES_INCLUDE_DIRS}) |
| 114 |
target_link_libraries( |
| 115 |
Ceres::ceres INTERFACE ${CERES_LIBRARIES}) |
| 116 |
endif() |
| 117 |
|
| 118 |
find_package(Glog ${COLMAP_FIND_TYPE}) |
| 119 |
if(DEFINED glog_VERSION_MAJOR) |
| 120 |
# Older versions of glog don't export version variables. |
| 121 |
list(APPEND COLMAP_COMPILE_DEFINITIONS GLOG_VERSION_MAJOR=${glog_VERSION_MAJOR}) |
| 122 |
list(APPEND COLMAP_COMPILE_DEFINITIONS GLOG_VERSION_MINOR=${glog_VERSION_MINOR}) |
| 123 |
endif() |
| 124 |
|
| 125 |
if(TESTS_ENABLED) |
| 126 |
find_package(GTest ${COLMAP_FIND_TYPE}) |
| 127 |
endif() |
| 128 |
|
| 129 |
if(HIP_ENABLED) |
| 130 |
# Locate the ROCm installation. Precedence: an explicit -DROCM_PATH, then the |
| 131 |
# ROCM_PATH environment variable, then a pip/venv ROCm install (AMD's TheRock |
| 132 |
# packaging exposes a "rocm-sdk" helper that reports its own root), then the |
| 133 |
# system default /opt/rocm. This lets a non-default install (e.g. a Python |
| 134 |
# virtualenv) be picked up without hand-setting paths. |
| 135 |
find_program(ROCM_SDK_EXECUTABLE rocm-sdk) |
| 136 |
set(_rocm_path_default "/opt/rocm") |
| 137 |
if(DEFINED ENV{ROCM_PATH}) |
| 138 |
set(_rocm_path_default "$ENV{ROCM_PATH}") |
| 139 |
elseif(ROCM_SDK_EXECUTABLE) |
| 140 |
execute_process( |
| 141 |
COMMAND "${ROCM_SDK_EXECUTABLE}" path --root |
| 142 |
OUTPUT_VARIABLE _rocm_sdk_root |
| 143 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 144 |
ERROR_QUIET |
| 145 |
RESULT_VARIABLE _rocm_sdk_root_result) |
| 146 |
if(_rocm_sdk_root_result EQUAL 0 AND IS_DIRECTORY "${_rocm_sdk_root}") |
| 147 |
set(_rocm_path_default "${_rocm_sdk_root}") |
| 148 |
endif() |
| 149 |
endif() |
| 150 |
set(ROCM_PATH "${_rocm_path_default}" CACHE PATH "Path to ROCm installation") |
| 151 |
list(APPEND CMAKE_PREFIX_PATH "${ROCM_PATH}") |
| 152 |
find_package(hip REQUIRED) |
| 153 |
find_package(hiprand REQUIRED) |
| 154 |
find_package(rocrand REQUIRED) |
| 155 |
# enable_language(HIP) introduces a separate CMake HIP language with its |
| 156 |
# own flag namespace (CMAKE_HIP_FLAGS / CMAKE_HIP_ARCHITECTURES). Only |
| 157 |
# files marked with set_source_files_properties(... LANGUAGE HIP) are |
| 158 |
# compiled by the HIP toolchain; ordinary C++ files keep using the host |
| 159 |
# compiler. This is the same pattern PyTorch uses to compile a small |
| 160 |
# number of HIP translation units inside an otherwise plain C++ build. |
| 161 |
enable_language(HIP) |
| 162 |
if(NOT DEFINED CMAKE_HIP_ARCHITECTURES OR CMAKE_HIP_ARCHITECTURES STREQUAL "") |
| 163 |
# When the user does not pin the target architectures, try to discover |
| 164 |
# them from the local ROCm install via "rocm-sdk targets" (TheRock); it |
| 165 |
# prints the gfx IDs the SDK was built for as a Python-style list, e.g. |
| 166 |
# ['gfx1100', 'gfx1101']. Extract the gfx tokens regardless of quoting or |
| 167 |
# separators, and fall back to a portable default set of validated parts. |
| 168 |
set(_hip_archs "") |
| 169 |
if(ROCM_SDK_EXECUTABLE) |
| 170 |
execute_process( |
| 171 |
COMMAND "${ROCM_SDK_EXECUTABLE}" targets |
| 172 |
OUTPUT_VARIABLE _rocm_sdk_targets |
| 173 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 174 |
ERROR_QUIET |
| 175 |
RESULT_VARIABLE _rocm_sdk_targets_result) |
| 176 |
if(_rocm_sdk_targets_result EQUAL 0) |
| 177 |
string(REGEX MATCHALL "gfx[0-9a-fA-F]+" _hip_archs "${_rocm_sdk_targets}") |
| 178 |
endif() |
| 179 |
endif() |
| 180 |
if(NOT _hip_archs) |
| 181 |
set(_hip_archs "gfx90a;gfx942;gfx1100") |
| 182 |
endif() |
| 183 |
set(CMAKE_HIP_ARCHITECTURES "${_hip_archs}" CACHE STRING |
| 184 |
"AMD GPU architectures to compile HIP code for" FORCE) |
| 185 |
endif() |
| 186 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_HIP_ENABLED) |
| 187 |
endif() |
| 188 |
|
| 189 |
if(CGAL_ENABLED) |
| 190 |
set(CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE TRUE) |
| 191 |
# We do not use CGAL data. This prevents an unnecessary warning by CMake. |
| 192 |
set(CGAL_DATA_DIR "unused") |
| 193 |
find_package(CGAL ${COLMAP_FIND_TYPE}) |
| 194 |
endif() |
| 195 |
|
| 196 |
if(CGAL_FOUND) |
| 197 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_CGAL_ENABLED) |
| 198 |
list(APPEND CGAL_LIBRARY ${CGAL_LIBRARIES}) |
| 199 |
message(STATUS "Found CGAL") |
| 200 |
message(STATUS " Includes : ${CGAL_INCLUDE_DIRS}") |
| 201 |
message(STATUS " Libraries : ${CGAL_LIBRARY}") |
| 202 |
if(NOT TARGET CGAL) |
| 203 |
# Older CGAL versions don't come with an imported interface target. |
| 204 |
add_library(CGAL INTERFACE IMPORTED) |
| 205 |
target_include_directories( |
| 206 |
CGAL INTERFACE ${CGAL_INCLUDE_DIRS} ${GMP_INCLUDE_DIR}) |
| 207 |
target_link_libraries( |
| 208 |
CGAL INTERFACE ${CGAL_LIBRARY} ${GMP_LIBRARIES}) |
| 209 |
endif() |
| 210 |
list(APPEND COLMAP_LINK_DIRS ${CGAL_LIBRARIES_DIR}) |
| 211 |
else() |
| 212 |
if(CGAL_ENABLED) |
| 213 |
set(CGAL_ENABLED OFF) |
| 214 |
message(STATUS "Disabling CGAL support (not found)") |
| 215 |
else() |
| 216 |
message(STATUS "Disabling CGAL support") |
| 217 |
endif() |
| 218 |
endif() |
| 219 |
|
| 220 |
if(DOWNLOAD_ENABLED) |
| 221 |
# The OpenSSL package in vcpkg seems broken under Windows and leads to |
| 222 |
# missing certificate verification when connecting to SSL servers. We |
| 223 |
# therefore use curl[sspi] (i.e., native Windows SSL/TLS) under Windows |
| 224 |
# and curl[openssl] otherwise. |
| 225 |
find_package(CURL QUIET) |
| 226 |
set(CRYPTO_FOUND FALSE) |
| 227 |
if(IS_MSVC AND IS_ARM64) |
| 228 |
# OpenSSL crashes for ARM64 under Windows. We therefore fall back to |
| 229 |
# CryptoPP as an alternative to OpenSSL for SHA256 computation. |
| 230 |
find_package(CryptoPP QUIET) |
| 231 |
if(CryptoPP_FOUND) |
| 232 |
set(CRYPTO_FOUND TRUE) |
| 233 |
else() |
| 234 |
message(STATUS "CryptoPP not found") |
| 235 |
endif() |
| 236 |
else() |
| 237 |
find_package(OpenSSL QUIET COMPONENTS Crypto) |
| 238 |
if(OpenSSL_FOUND) |
| 239 |
set(CRYPTO_FOUND TRUE) |
| 240 |
else() |
| 241 |
message(STATUS "OpenSSL::Crypto not found") |
| 242 |
endif() |
| 243 |
endif() |
| 244 |
if(CURL_FOUND AND CRYPTO_FOUND) |
| 245 |
message(STATUS "Enabling download support") |
| 246 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_DOWNLOAD_ENABLED) |
| 247 |
else() |
| 248 |
set(DOWNLOAD_ENABLED OFF) |
| 249 |
message(STATUS "Disabling download support (Curl/Crypto not found)") |
| 250 |
endif() |
| 251 |
else() |
| 252 |
message(STATUS "Disabling download support") |
| 253 |
endif() |
| 254 |
|
| 255 |
if(NOT FETCH_POSELIB) |
| 256 |
find_package(PoseLib ${COLMAP_FIND_TYPE}) |
| 257 |
endif() |
| 258 |
|
| 259 |
if(NOT FETCH_FAISS) |
| 260 |
find_package(faiss ${COLMAP_FIND_TYPE}) |
| 261 |
endif() |
| 262 |
|
| 263 |
set(COLMAP_LINK_DIRS ${Boost_LIBRARY_DIRS}) |
| 264 |
|
| 265 |
set(CUDA_MIN_VERSION "7.0") |
| 266 |
if(CUDA_ENABLED) |
| 267 |
if(CMAKE_VERSION VERSION_LESS 3.17) |
| 268 |
find_package(CUDA QUIET) |
| 269 |
if(CUDA_FOUND) |
| 270 |
message(STATUS "Found CUDA version ${CUDA_VERSION} installed in " |
| 271 |
"${CUDA_TOOLKIT_ROOT_DIR} via legacy CMake (<3.17) module. " |
| 272 |
"Using the legacy CMake module means that any installation of " |
| 273 |
"COLMAP will require that the CUDA libraries are " |
| 274 |
"available under LD_LIBRARY_PATH.") |
| 275 |
message(STATUS "Found CUDA ") |
| 276 |
message(STATUS " Includes : ${CUDA_INCLUDE_DIRS}") |
| 277 |
message(STATUS " Libraries : ${CUDA_LIBRARIES}") |
| 278 |
|
| 279 |
enable_language(CUDA) |
| 280 |
|
| 281 |
macro(declare_imported_cuda_target module) |
| 282 |
add_library(CUDA::${module} INTERFACE IMPORTED) |
| 283 |
target_include_directories( |
| 284 |
CUDA::${module} INTERFACE ${CUDA_INCLUDE_DIRS}) |
| 285 |
target_link_libraries( |
| 286 |
CUDA::${module} INTERFACE ${CUDA_${module}_LIBRARY} ${ARGN}) |
| 287 |
endmacro() |
| 288 |
|
| 289 |
declare_imported_cuda_target(cudart ${CUDA_LIBRARIES}) |
| 290 |
declare_imported_cuda_target(curand ${CUDA_LIBRARIES}) |
| 291 |
|
| 292 |
set(CUDAToolkit_VERSION "${CUDA_VERSION_STRING}") |
| 293 |
set(CUDAToolkit_BIN_DIR "${CUDA_TOOLKIT_ROOT_DIR}/bin") |
| 294 |
else() |
| 295 |
message(STATUS "Disabling CUDA support (not found)") |
| 296 |
endif() |
| 297 |
else() |
| 298 |
find_package(CUDAToolkit QUIET) |
| 299 |
if(CUDAToolkit_FOUND) |
| 300 |
set(CUDA_FOUND ON) |
| 301 |
enable_language(CUDA) |
| 302 |
else() |
| 303 |
message(STATUS "Disabling CUDA support (not found)") |
| 304 |
endif() |
| 305 |
endif() |
| 306 |
else() |
| 307 |
message(STATUS "Disabling CUDA support") |
| 308 |
endif() |
| 309 |
|
| 310 |
if(CUDA_ENABLED AND CUDA_FOUND) |
| 311 |
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) |
| 312 |
set(CMAKE_CUDA_ARCHITECTURES "native") |
| 313 |
endif() |
| 314 |
|
| 315 |
# Caspar's Symforce-generated kernels use cooperative_groups::labeled_partition |
| 316 |
# and atomicAdd_block, which require compute capability >= 7.0. Fail early with |
| 317 |
# a clear message instead of a cryptic nvcc error deep in the kernel build. The |
| 318 |
# numeric check handles list entries and -real/-virtual suffixes; the special |
| 319 |
# values native/all/all-major cannot be resolved statically here, so they only |
| 320 |
# get a warning (nvcc may fall back to an older default arch in build |
| 321 |
# environments without a visible GPU >= 7.0, e.g. containerized builds). |
| 322 |
if(CASPAR_ENABLED) |
| 323 |
foreach(_caspar_arch IN LISTS CMAKE_CUDA_ARCHITECTURES) |
| 324 |
string(REGEX MATCH "^([0-9]+)" _caspar_arch_num "${_caspar_arch}") |
| 325 |
if(_caspar_arch_num AND _caspar_arch_num LESS 70) |
| 326 |
message(FATAL_ERROR |
| 327 |
"CASPAR_ENABLED requires CUDA architecture >= 70 (compute " |
| 328 |
"capability 7.0), but CMAKE_CUDA_ARCHITECTURES contains " |
| 329 |
"'${_caspar_arch}'. Set -DCMAKE_CUDA_ARCHITECTURES to 70+.") |
| 330 |
endif() |
| 331 |
endforeach() |
| 332 |
if(CMAKE_CUDA_ARCHITECTURES MATCHES "native|all|all-major") |
| 333 |
message(WARNING |
| 334 |
"CASPAR_ENABLED with CMAKE_CUDA_ARCHITECTURES='${CMAKE_CUDA_ARCHITECTURES}': " |
| 335 |
"Caspar requires compute capability >= 7.0, which cannot be " |
| 336 |
"verified statically for this value. In an environment without a " |
| 337 |
"visible GPU >= 7.0 (e.g. containerized builds) nvcc may fall back " |
| 338 |
"to an older default arch and fail with cryptic kernel errors. " |
| 339 |
"Set -DCMAKE_CUDA_ARCHITECTURES explicitly (e.g. 75, 86).") |
| 340 |
endif() |
| 341 |
endif() |
| 342 |
|
| 343 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_CUDA_ENABLED) |
| 344 |
|
| 345 |
# Do not show warnings if the architectures are deprecated. |
| 346 |
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets") |
| 347 |
# Suppress warnings related to Eigen: |
| 348 |
# Calling a constexpr __host__ function from a __host__ __device__ function is not allowed. |
| 349 |
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr") |
| 350 |
# Explicitly set PIC flags for CUDA targets. |
| 351 |
if(NOT IS_MSVC) |
| 352 |
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --compiler-options -fPIC") |
| 353 |
endif() |
| 354 |
|
| 355 |
# Handle MSVC runtime library for CUDA to support static CRT linking. |
| 356 |
# CMake's default CUDA flags use /MD (dynamic), but if the user is building |
| 357 |
# with static CRT (/MT), we need to override the CUDA flags to match. |
| 358 |
if(IS_MSVC) |
| 359 |
# Detect the runtime library from CMAKE_MSVC_RUNTIME_LIBRARY or CXX flags |
| 360 |
set(_COLMAP_USE_STATIC_RUNTIME OFF) |
| 361 |
|
| 362 |
if(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) |
| 363 |
if(CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "MultiThreaded" AND |
| 364 |
NOT CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL") |
| 365 |
set(_COLMAP_USE_STATIC_RUNTIME ON) |
| 366 |
endif() |
| 367 |
elseif(CMAKE_CXX_FLAGS_DEBUG MATCHES "/MTd" OR |
| 368 |
CMAKE_CXX_FLAGS_RELEASE MATCHES "/MT[^d]" OR |
| 369 |
CMAKE_CXX_FLAGS MATCHES "/MT") |
| 370 |
set(_COLMAP_USE_STATIC_RUNTIME ON) |
| 371 |
endif() |
| 372 |
|
| 373 |
if(_COLMAP_USE_STATIC_RUNTIME) |
| 374 |
message(STATUS "CUDA: Using static MSVC runtime library (/MT)") |
| 375 |
# Replace /MD with /MT in CUDA flags for each build type |
| 376 |
foreach(_BUILD_TYPE DEBUG RELEASE RELWITHDEBINFO MINSIZEREL) |
| 377 |
if(DEFINED CMAKE_CUDA_FLAGS_${_BUILD_TYPE}) |
| 378 |
string(REPLACE "-MDd" "-MTd" CMAKE_CUDA_FLAGS_${_BUILD_TYPE} |
| 379 |
"${CMAKE_CUDA_FLAGS_${_BUILD_TYPE}}") |
| 380 |
string(REPLACE "-MD" "-MT" CMAKE_CUDA_FLAGS_${_BUILD_TYPE} |
| 381 |
"${CMAKE_CUDA_FLAGS_${_BUILD_TYPE}}") |
| 382 |
string(REPLACE "/MDd" "/MTd" CMAKE_CUDA_FLAGS_${_BUILD_TYPE} |
| 383 |
"${CMAKE_CUDA_FLAGS_${_BUILD_TYPE}}") |
| 384 |
string(REPLACE "/MD" "/MT" CMAKE_CUDA_FLAGS_${_BUILD_TYPE} |
| 385 |
"${CMAKE_CUDA_FLAGS_${_BUILD_TYPE}}") |
| 386 |
endif() |
| 387 |
endforeach() |
| 388 |
endif() |
| 389 |
|
| 390 |
unset(_COLMAP_USE_STATIC_RUNTIME) |
| 391 |
endif() |
| 392 |
|
| 393 |
message(STATUS "Enabling CUDA support (version: ${CUDAToolkit_VERSION}, " |
| 394 |
"archs: ${CMAKE_CUDA_ARCHITECTURES})") |
| 395 |
else() |
| 396 |
set(CUDA_ENABLED OFF) |
| 397 |
endif() |
| 398 |
|
| 399 |
if(ONNX_ENABLED) |
| 400 |
if(FETCH_ONNX) |
| 401 |
include(FetchContent) |
| 402 |
|
| 403 |
message(STATUS "Configuring onnxruntime...") |
| 404 |
|
| 405 |
set(ONNX_VERSION "1.27.1") |
| 406 |
# ONNX Runtime now ships separate GPU binaries per CUDA major version |
| 407 |
# (gpu_cuda12 / gpu_cuda13). We consume the CUDA 12 build below, so CUDA |
| 408 |
# >= 12 is required for the GPU execution provider. |
| 409 |
if(ONNX_VERSION VERSION_GREATER_EQUAL "1.22" |
| 410 |
AND CUDA_ENABLED AND CUDA_FOUND AND CUDAToolkit_VERSION VERSION_LESS "12.0") |
| 411 |
message(WARNING |
| 412 |
"ONNX Runtime ${ONNX_VERSION} GPU binary is built with CUDA >= 12, " |
| 413 |
"but CUDA ${CUDAToolkit_VERSION} was detected. The ONNX Runtime CUDA " |
| 414 |
"execution provider may fail at runtime, CPU execution will continue to work. " |
| 415 |
"Consider upgrading CUDA to >= 12 or using a source-built onnxruntime.") |
| 416 |
endif() |
| 417 |
|
| 418 |
if(IS_MACOS) |
| 419 |
if(CMAKE_OSX_ARCHITECTURES) |
| 420 |
set(_COLMAP_MACOS_ARCH ${CMAKE_OSX_ARCHITECTURES}) |
| 421 |
else() |
| 422 |
set(_COLMAP_MACOS_ARCH ${CMAKE_SYSTEM_PROCESSOR}) |
| 423 |
endif() |
| 424 |
if(_COLMAP_MACOS_ARCH STREQUAL "x86_64") |
| 425 |
message(FATAL_ERROR "x86_64 is not supported for onnxruntime") |
| 426 |
else() |
| 427 |
FetchContent_Declare(onnxruntime |
| 428 |
URL https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-osx-arm64-${ONNX_VERSION}.tgz |
| 429 |
URL_HASH SHA256=e42b77a7281cc6e55141bf44fcfbac2c782b823a491bbb6ac33c781dd991f8a6 |
| 430 |
${_fetch_content_declare_args} |
| 431 |
) |
| 432 |
endif() |
| 433 |
elseif(IS_LINUX) |
| 434 |
if(IS_ARM64) |
| 435 |
FetchContent_Declare(onnxruntime |
| 436 |
URL https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-linux-aarch64-${ONNX_VERSION}.tgz |
| 437 |
URL_HASH SHA256=33c67e33d1e25b816878366ea276589a024f71f000e7ff955c4b33224d639edd |
| 438 |
${_fetch_content_declare_args} |
| 439 |
) |
| 440 |
else() |
| 441 |
if(CUDA_ENABLED) |
| 442 |
FetchContent_Declare(onnxruntime |
| 443 |
URL https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-linux-x64-gpu_cuda12-${ONNX_VERSION}.tgz |
| 444 |
URL_HASH SHA256=08b568bd69500c36606aff7c3896ee4fa7d3531719f6b00f43e6a34db41dc4bf |
| 445 |
${_fetch_content_declare_args} |
| 446 |
) |
| 447 |
else() |
| 448 |
FetchContent_Declare(onnxruntime |
| 449 |
URL https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-linux-x64-${ONNX_VERSION}.tgz |
| 450 |
URL_HASH SHA256=25b1ef1fea1acd210d63f8f24dc870ad6e077795ce1f54876252c6d3803c15af |
| 451 |
${_fetch_content_declare_args} |
| 452 |
) |
| 453 |
endif() |
| 454 |
endif() |
| 455 |
elseif(IS_WINDOWS) |
| 456 |
FetchContent_Declare(onnxruntime |
| 457 |
URL https://github.com/microsoft/onnxruntime/releases/download/v${ONNX_VERSION}/onnxruntime-win-x64-gpu_cuda12-${ONNX_VERSION}.zip |
| 458 |
URL_HASH SHA256=78d4de5ab262f79ac5dd59f08ff0d049b1cea605497f375f8df5ba1a52f26111 |
| 459 |
${_fetch_content_declare_args} |
| 460 |
) |
| 461 |
endif() |
| 462 |
|
| 463 |
FetchContent_MakeAvailable(onnxruntime) |
| 464 |
|
| 465 |
set(ONNX_INCLUDE_DIR ${onnxruntime_BINARY_DIR}/include/onnxruntime) |
| 466 |
if(NOT EXISTS ${ONNX_INCLUDE_DIR}) |
| 467 |
file(MAKE_DIRECTORY ${ONNX_INCLUDE_DIR}) |
| 468 |
file(COPY ${onnxruntime_SOURCE_DIR}/include/ DESTINATION ${ONNX_INCLUDE_DIR}/) |
| 469 |
endif() |
| 470 |
set(onnxruntime_LIB_DIR ${onnxruntime_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) |
| 471 |
if(NOT EXISTS ${onnxruntime_LIB_DIR}) |
| 472 |
file(MAKE_DIRECTORY ${onnxruntime_LIB_DIR}) |
| 473 |
file(COPY ${onnxruntime_SOURCE_DIR}/lib/ DESTINATION ${onnxruntime_LIB_DIR}) |
| 474 |
file(REMOVE_RECURSE ${onnxruntime_LIB_DIR}/cmake) |
| 475 |
file(REMOVE_RECURSE ${onnxruntime_LIB_DIR}/pkgconfig) |
| 476 |
endif() |
| 477 |
if(NOT IS_WINDOWS) |
| 478 |
set(ONNX_DATA_DIR ${onnxruntime_BINARY_DIR}/share/onnxruntime) |
| 479 |
if(NOT EXISTS ${ONNX_DATA_DIR}) |
| 480 |
file(MAKE_DIRECTORY ${ONNX_DATA_DIR}) |
| 481 |
file(COPY ${onnxruntime_SOURCE_DIR}/lib/cmake/onnxruntime/ DESTINATION ${ONNX_DATA_DIR}/cmake/) |
| 482 |
file(REMOVE_RECURSE ${onnxruntime_SOURCE_DIR}/lib/cmake) |
| 483 |
# The downloaded cmake configs may reference lib64/ (e.g. on Linux x64), |
| 484 |
# but the actual install directory depends on CMAKE_INSTALL_LIBDIR |
| 485 |
# (lib/ or lib64/ depending on the distro). Patch the configs to match. |
| 486 |
if(IS_LINUX AND NOT IS_ARM64) |
| 487 |
file(GLOB _onnx_cmake_configs "${ONNX_DATA_DIR}/cmake/*.cmake") |
| 488 |
foreach(_config_file ${_onnx_cmake_configs}) |
| 489 |
file(READ "${_config_file}" _config_content) |
| 490 |
string(REPLACE "/lib64/" "/${CMAKE_INSTALL_LIBDIR}/" _config_content "${_config_content}") |
| 491 |
file(WRITE "${_config_file}" "${_config_content}") |
| 492 |
endforeach() |
| 493 |
endif() |
| 494 |
endif() |
| 495 |
set(onnxruntime_CONFIG_DIR_HINTS ${ONNX_DATA_DIR}/cmake CACHE PATH "ONNX Runtime config directory hints") |
| 496 |
endif() |
| 497 |
|
| 498 |
set(onnxruntime_INCLUDE_DIR_HINTS ${onnxruntime_BINARY_DIR}/include CACHE PATH "ONNX Runtime include directory hints") |
| 499 |
set(onnxruntime_LIBRARY_DIR_HINTS ${onnxruntime_BINARY_DIR}/lib CACHE PATH "ONNX Runtime library directory hints") |
| 500 |
find_package(onnxruntime ${COLMAP_FIND_TYPE}) |
| 501 |
|
| 502 |
install(DIRECTORY "${onnxruntime_BINARY_DIR}/include/" TYPE INCLUDE) |
| 503 |
if(IS_WINDOWS) |
| 504 |
# On Windows, selectively install Libs to lib/. Always install core Libs. |
| 505 |
# For not supporting TensorRT/ROCM/etc. as a runtime, so not installing it intentionally. |
| 506 |
install(FILES |
| 507 |
"${onnxruntime_LIB_DIR}/onnxruntime.lib" |
| 508 |
"${onnxruntime_LIB_DIR}/onnxruntime_providers_shared.lib" |
| 509 |
TYPE LIB) |
| 510 |
# Only install CUDA provider Lib if CUDA is enabled. |
| 511 |
if(CUDA_ENABLED) |
| 512 |
install(FILES |
| 513 |
"${onnxruntime_LIB_DIR}/onnxruntime_providers_cuda.lib" |
| 514 |
TYPE LIB) |
| 515 |
endif() |
| 516 |
# On Windows, selectively install DLLs to bin/. Always install core DLLs. |
| 517 |
# For not supporting TensorRT/ROCM/etc. as a runtime, so not installing it intentionally. |
| 518 |
install(FILES |
| 519 |
"${onnxruntime_LIB_DIR}/onnxruntime.dll" |
| 520 |
"${onnxruntime_LIB_DIR}/onnxruntime_providers_shared.dll" |
| 521 |
TYPE BIN) |
| 522 |
# Only install CUDA provider DLL if CUDA is enabled. |
| 523 |
if(CUDA_ENABLED) |
| 524 |
install(FILES |
| 525 |
"${onnxruntime_LIB_DIR}/onnxruntime_providers_cuda.dll" |
| 526 |
TYPE BIN) |
| 527 |
endif() |
| 528 |
else() |
| 529 |
# On Linux/macOS, selectively install library files. Always install core libraries. |
| 530 |
# Not supporting TensorRT/ROCM/etc. as a runtime, so not installing them. |
| 531 |
if(IS_MACOS) |
| 532 |
file(GLOB onnxruntime_CORE_LIBS |
| 533 |
"${onnxruntime_LIB_DIR}/libonnxruntime.dylib" |
| 534 |
"${onnxruntime_LIB_DIR}/libonnxruntime.*.dylib" |
| 535 |
"${onnxruntime_LIB_DIR}/libonnxruntime_providers_shared.dylib") |
| 536 |
install(FILES ${onnxruntime_CORE_LIBS} TYPE LIB) |
| 537 |
else() |
| 538 |
file(GLOB onnxruntime_CORE_LIBS |
| 539 |
"${onnxruntime_LIB_DIR}/libonnxruntime.so*" |
| 540 |
"${onnxruntime_LIB_DIR}/libonnxruntime_providers_shared.so*") |
| 541 |
install(FILES ${onnxruntime_CORE_LIBS} TYPE LIB) |
| 542 |
# Only install CUDA provider if CUDA is enabled. |
| 543 |
if(CUDA_ENABLED) |
| 544 |
file(GLOB onnxruntime_CUDA_LIBS |
| 545 |
"${onnxruntime_LIB_DIR}/libonnxruntime_providers_cuda.so*") |
| 546 |
install(FILES ${onnxruntime_CUDA_LIBS} TYPE LIB) |
| 547 |
endif() |
| 548 |
endif() |
| 549 |
endif() |
| 550 |
if(EXISTS "${onnxruntime_BINARY_DIR}/share") |
| 551 |
install(DIRECTORY "${onnxruntime_BINARY_DIR}/share/" TYPE DATA) |
| 552 |
endif() |
| 553 |
|
| 554 |
message(STATUS "Configuring onnxruntime... done") |
| 555 |
else() |
| 556 |
find_package(onnxruntime ${COLMAP_FIND_TYPE}) |
| 557 |
if(NOT onnxruntime_FOUND) |
| 558 |
message(STATUS "Disabling ONNX support (not found)") |
| 559 |
set(ONNX_ENABLED OFF) |
| 560 |
endif() |
| 561 |
endif() |
| 562 |
else() |
| 563 |
message(STATUS "Disabling ONNX support") |
| 564 |
endif() |
| 565 |
|
| 566 |
if(TARGET onnxruntime::onnxruntime) |
| 567 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_ONNX_ENABLED) |
| 568 |
message(STATUS "Enabling ONNX support") |
| 569 |
# The prebuilt macOS onnxruntime binaries ship with the CoreML execution |
| 570 |
# provider, which accelerates ONNX inference on the GPU / Apple Neural |
| 571 |
# Engine. Enable it as the GPU backend on Apple platforms (CUDA is |
| 572 |
# unavailable there). |
| 573 |
if(IS_MACOS) |
| 574 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_COREML_ENABLED) |
| 575 |
message(STATUS "Enabling ONNX CoreML execution provider") |
| 576 |
endif() |
| 577 |
endif() |
| 578 |
|
| 579 |
if(GUI_ENABLED) |
| 580 |
find_package(QT NAMES Qt5 Qt6 REQUIRED) |
| 581 |
set(COLMAP_QT_COMPONENTS Core OpenGL Svg Widgets) |
| 582 |
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) |
| 583 |
list(APPEND COLMAP_QT_COMPONENTS OpenGLWidgets) |
| 584 |
endif() |
| 585 |
find_package(Qt${QT_VERSION_MAJOR} ${COLMAP_FIND_TYPE} COMPONENTS ${COLMAP_QT_COMPONENTS}) |
| 586 |
message(STATUS "Found Qt") |
| 587 |
message(STATUS " Module : ${Qt${QT_VERSION_MAJOR}Core_DIR}") |
| 588 |
message(STATUS " Module : ${Qt${QT_VERSION_MAJOR}OpenGL_DIR}") |
| 589 |
message(STATUS " Module : ${Qt${QT_VERSION_MAJOR}Svg_DIR}") |
| 590 |
message(STATUS " Module : ${Qt${QT_VERSION_MAJOR}Widgets_DIR}") |
| 591 |
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) |
| 592 |
message(STATUS " Module : ${Qt${QT_VERSION_MAJOR}OpenGLWidgets_DIR}") |
| 593 |
endif() |
| 594 |
if(Qt5_FOUND) |
| 595 |
# Qt5 was built with -reduce-relocations. |
| 596 |
if(Qt5_POSITION_INDEPENDENT_CODE) |
| 597 |
set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 598 |
# Workaround for Qt5 CMake config bug under Ubuntu 20.04: https://gitlab.kitware.com/cmake/cmake/-/issues/16915 |
| 599 |
if(TARGET Qt5::Core) |
| 600 |
get_property(core_options TARGET Qt5::Core PROPERTY INTERFACE_COMPILE_OPTIONS) |
| 601 |
string(REPLACE "-fPIC" "" new_qt5_core_options "${core_options}") |
| 602 |
set_property(TARGET Qt5::Core PROPERTY INTERFACE_COMPILE_OPTIONS ${new_qt5_core_options}) |
| 603 |
set_property(TARGET Qt5::Core PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE "ON") |
| 604 |
if(NOT IS_MSVC) |
| 605 |
set(CMAKE_CXX_COMPILE_OPTIONS_PIE "-fPIC") |
| 606 |
endif() |
| 607 |
endif() |
| 608 |
endif() |
| 609 |
endif() |
| 610 |
|
| 611 |
if(QT_FOUND) |
| 612 |
# Enable automatic compilation of Qt resource files. |
| 613 |
set(CMAKE_AUTORCC ON) |
| 614 |
endif() |
| 615 |
endif() |
| 616 |
|
| 617 |
if(GUI_ENABLED AND Qt${QT_VERSION_MAJOR}_FOUND) |
| 618 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_GUI_ENABLED) |
| 619 |
message(STATUS "Enabling GUI support") |
| 620 |
else() |
| 621 |
set(GUI_ENABLED OFF) |
| 622 |
message(STATUS "Disabling GUI support") |
| 623 |
endif() |
| 624 |
|
| 625 |
if(MVS_ENABLED) |
| 626 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_MVS_ENABLED) |
| 627 |
message(STATUS "Enabling MVS support") |
| 628 |
else() |
| 629 |
message(STATUS "Disabling MVS support") |
| 630 |
endif() |
| 631 |
|
| 632 |
if(OPENGL_ENABLED) |
| 633 |
if(NOT GUI_ENABLED) |
| 634 |
message(STATUS "Disabling GUI also disables OpenGL") |
| 635 |
set(OPENGL_ENABLED OFF) |
| 636 |
else() |
| 637 |
message(STATUS "Enabling OpenGL support") |
| 638 |
endif() |
| 639 |
else() |
| 640 |
message(STATUS "Disabling OpenGL support") |
| 641 |
endif() |
| 642 |
|
| 643 |
set(GPU_ENABLED OFF) |
| 644 |
if(OPENGL_ENABLED OR CUDA_ENABLED) |
| 645 |
list(APPEND COLMAP_COMPILE_DEFINITIONS COLMAP_GPU_ENABLED) |
| 646 |
message(STATUS "Enabling GPU support (OpenGL: ${OPENGL_ENABLED}, CUDA: ${CUDA_ENABLED})") |
| 647 |
set(GPU_ENABLED ON) |
| 648 |
endif() |