1# Ceres Solver - A fast non-linear least squares minimizer 2# Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 3# http://code.google.com/p/ceres-solver/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are met: 7# 8# * Redistributions of source code must retain the above copyright notice, 9# this list of conditions and the following disclaimer. 10# * Redistributions in binary form must reproduce the above copyright notice, 11# this list of conditions and the following disclaimer in the documentation 12# and/or other materials provided with the distribution. 13# * Neither the name of Google Inc. nor the names of its contributors may be 14# used to endorse or promote products derived from this software without 15# specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28# 29# Author: keir@google.com (Keir Mierle) 30 31CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0) 32CMAKE_POLICY(VERSION 2.8) 33 34IF (COMMAND cmake_policy) 35 CMAKE_POLICY(SET CMP0003 NEW) 36ENDIF (COMMAND cmake_policy) 37 38PROJECT(CERES C CXX) 39 40# Set up the git hook to make Gerrit Change-Id: lines in commit messages. 41SET (LOCAL_GIT_DIRECTORY) 42IF (EXISTS ${CMAKE_SOURCE_DIR}/.git) 43 # .git directory can be found on Unix based system, or on Windows with 44 # Git Bash (shipped with msysgit) 45 SET (LOCAL_GIT_DIRECTORY ${CMAKE_SOURCE_DIR}/.git) 46ELSE (EXISTS ${CMAKE_SOURCE_DIR}/.git) 47 # TODO(keir) Add proper Windows support 48ENDIF (EXISTS ${CMAKE_SOURCE_DIR}/.git) 49 50IF (EXISTS ${LOCAL_GIT_DIRECTORY}) 51 IF (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg) 52 # Download the hook only if it is not already present 53 FILE(DOWNLOAD https://ceres-solver-review.googlesource.com/tools/hooks/commit-msg 54 ${CMAKE_BINARY_DIR}/commit-msg) 55 56 # Make the downloaded file executable, since it is not by default. 57 FILE(COPY ${CMAKE_BINARY_DIR}/commit-msg 58 DESTINATION ${LOCAL_GIT_DIRECTORY}/hooks/ 59 FILE_PERMISSIONS 60 OWNER_READ OWNER_WRITE OWNER_EXECUTE 61 GROUP_READ GROUP_WRITE GROUP_EXECUTE 62 WORLD_READ WORLD_EXECUTE) 63 ENDIF (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg) 64ENDIF (EXISTS ${LOCAL_GIT_DIRECTORY}) 65 66SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 67SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 68SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 69 70# Important: Always bump the second number (e.g. 1.3.x to 1.4.0) for any 71# release that changes the ABI. The ABI changes for almost any modification to 72# include/ceres (e.g. the public API). If you are unsure about whether 73# something is an ABI change, please ask on the list. 74# 75# For versions without ABI changes, bump the smallest number in CERES_VERSION, 76# but leave the CERES_ABI_VERSION unchanged. 77SET(CERES_VERSION_MAJOR 1) 78SET(CERES_VERSION_MINOR 7) 79SET(CERES_VERSION_PATCH 0) 80SET(CERES_VERSION 81 ${CERES_VERSION_MAJOR}.${CERES_VERSION_MINOR}.${CERES_VERSION_PATCH}) 82SET(CERES_ABI_VERSION 1.7.0) 83 84ENABLE_TESTING() 85 86OPTION(MINIGLOG "Use a stripped down version of glog" OFF) 87OPTION(GFLAGS "Enable Google Flags." ON) 88# Template specializations for the Schur complement based solvers. If 89# compile time, binary size or compiler performance is an issue, you 90# may consider disabling this. 91OPTION(SCHUR_SPECIALIZATIONS "Enable fixed-size schur specializations." ON) 92OPTION(CUSTOM_BLAS 93 "Use handcoded BLAS routines (usually faster) instead of Eigen." 94 ON) 95# Multithreading using OpenMP 96OPTION(OPENMP "Enable threaded solving in Ceres (requires OpenMP)" ON) 97# TODO(sameeragarwal): Replace this with a positive option instead? 98OPTION(DISABLE_TR1 99 "Don't use TR1. This replaces some hash tables with sets. Slower." 100 OFF) 101# Line search minimizer is useful for large scale problems or when 102# sparse linear algebra libraries are not available. If compile time, 103# binary size or compiler performance is an issue, consider disabling 104# this. 105OPTION(LINE_SEARCH_MINIMIZER "Enable the line search minimizer." ON) 106OPTION(BUILD_TESTING "Enable tests" ON) 107OPTION(BUILD_DOCUMENTATION "Build User's Guide (html)" OFF) 108OPTION(BUILD_EXAMPLES "Build examples" ON) 109 110# Default locations to search for on various platforms. 111 112# Libraries 113LIST(APPEND CMAKE_LIBRARY_PATH /opt/local/lib) 114LIST(APPEND CMAKE_LIBRARY_PATH /opt/local/lib/ufsparse) # Mac OS X 115LIST(APPEND CMAKE_LIBRARY_PATH /usr/lib) 116LIST(APPEND CMAKE_LIBRARY_PATH /usr/lib/atlas) 117LIST(APPEND CMAKE_LIBRARY_PATH /usr/lib/suitesparse) # Ubuntu 118LIST(APPEND CMAKE_LIBRARY_PATH /usr/lib64/atlas) 119LIST(APPEND CMAKE_LIBRARY_PATH /usr/local/homebrew/lib) # Mac OS X 120LIST(APPEND CMAKE_LIBRARY_PATH /usr/local/lib) 121LIST(APPEND CMAKE_LIBRARY_PATH /usr/local/lib/suitesparse) 122 123# Headers 124LIST(APPEND CMAKE_INCLUDE_PATH /opt/local/include) 125LIST(APPEND CMAKE_INCLUDE_PATH /opt/local/include/ufsparse) # Mac OS X 126LIST(APPEND CMAKE_INCLUDE_PATH /opt/local/var/macports/software/eigen3/opt/local/include/eigen3) # Mac OS X 127LIST(APPEND CMAKE_INCLUDE_PATH /usr/include) 128LIST(APPEND CMAKE_INCLUDE_PATH /usr/include/eigen3) # Ubuntu 10.04's default location. 129LIST(APPEND CMAKE_INCLUDE_PATH /usr/include/suitesparse) # Ubuntu 130LIST(APPEND CMAKE_INCLUDE_PATH /usr/local/homebrew/include) # Mac OS X 131LIST(APPEND CMAKE_INCLUDE_PATH /usr/local/homebrew/include/eigen3) # Mac OS X 132LIST(APPEND CMAKE_INCLUDE_PATH /usr/local/include) 133LIST(APPEND CMAKE_INCLUDE_PATH /usr/local/include/eigen3) 134LIST(APPEND CMAKE_INCLUDE_PATH /usr/local/include/suitesparse) 135 136# Eigen 137FIND_PATH(EIGEN_INCLUDE NAMES Eigen/Core) 138IF (NOT EXISTS ${EIGEN_INCLUDE}) 139 MESSAGE(FATAL_ERROR "Can't find Eigen. Try passing -DEIGEN_INCLUDE=...") 140ELSE (NOT EXISTS ${EIGEN_INCLUDE}) 141 MESSAGE("-- Found Eigen 3.x: ${EIGEN_INCLUDE}") 142ENDIF (NOT EXISTS ${EIGEN_INCLUDE}) 143 144SET(BLAS_AND_LAPACK_FOUND TRUE) 145IF ((NOT DEFINED LAPACK) OR (DEFINED LAPACK AND LAPACK)) 146 FIND_PACKAGE(LAPACK) 147 IF (LAPACK_FOUND) 148 MESSAGE("-- Found LAPACK library: ${LAPACK_LIBRARIES}") 149 ELSE (LAPACK_FOUND) 150 MESSAGE("-- Did not find LAPACK library") 151 SET(BLAS_AND_LAPACK_FOUND FALSE) 152 ENDIF (LAPACK_FOUND) 153 154 FIND_PACKAGE(BLAS) 155 IF (BLAS_FOUND) 156 MESSAGE("-- Found BLAS library: ${BLAS_LIBRARIES}") 157 ELSE (BLAS_FOUND) 158 MESSAGE("-- Did not find BLAS library") 159 SET(BLAS_AND_BLAS_FOUND FALSE) 160 ENDIF (BLAS_FOUND) 161 162ELSE ((NOT DEFINED LAPACK) OR (DEFINED LAPACK AND LAPACK)) 163 SET(BLAS_AND_LAPACK_FOUND FALSE) 164ENDIF ((NOT DEFINED LAPACK) OR (DEFINED LAPACK AND LAPACK)) 165 166IF (NOT BLAS_AND_LAPACK_FOUND) 167 ADD_DEFINITIONS(-DCERES_NO_LAPACK) 168ENDIF (NOT BLAS_AND_LAPACK_FOUND) 169 170IF ((NOT DEFINED SUITESPARSE) OR (DEFINED SUITESPARSE AND SUITESPARSE)) 171# Check for SuiteSparse dependencies 172 173SET(AMD_FOUND TRUE) 174FIND_LIBRARY(AMD_LIB NAMES amd) 175IF (EXISTS ${AMD_LIB}) 176 MESSAGE("-- Found AMD library: ${AMD_LIB}") 177ELSE (EXISTS ${AMD_LIB}) 178 MESSAGE("-- Did not find AMD library") 179 SET(AMD_FOUND FALSE) 180ENDIF (EXISTS ${AMD_LIB}) 181 182FIND_PATH(AMD_INCLUDE NAMES amd.h) 183IF (EXISTS ${AMD_INCLUDE}) 184 MESSAGE("-- Found AMD header in: ${AMD_INCLUDE}") 185ELSE (EXISTS ${AMD_INCLUDE}) 186 MESSAGE("-- Did not find AMD header") 187 SET(AMD_FOUND FALSE) 188ENDIF (EXISTS ${AMD_INCLUDE}) 189 190SET(CAMD_FOUND TRUE) 191FIND_LIBRARY(CAMD_LIB NAMES camd) 192IF (EXISTS ${CAMD_LIB}) 193 MESSAGE("-- Found CAMD library: ${CAMD_LIB}") 194ELSE (EXISTS ${CAMD_LIB}) 195 MESSAGE("-- Did not find CAMD library") 196 SET(CAMD_FOUND FALSE) 197ENDIF (EXISTS ${CAMD_LIB}) 198 199FIND_PATH(CAMD_INCLUDE NAMES camd.h) 200IF (EXISTS ${CAMD_INCLUDE}) 201 MESSAGE("-- Found CAMD header in: ${CAMD_INCLUDE}") 202ELSE (EXISTS ${CAMD_INCLUDE}) 203 MESSAGE("-- Did not find CAMD header") 204 SET(CAMD_FOUND FALSE) 205ENDIF (EXISTS ${CAMD_INCLUDE}) 206 207SET(COLAMD_FOUND TRUE) 208FIND_LIBRARY(COLAMD_LIB NAMES colamd) 209IF (EXISTS ${COLAMD_LIB}) 210 MESSAGE("-- Found COLAMD library: ${COLAMD_LIB}") 211ELSE (EXISTS ${COLAMD_LIB}) 212 MESSAGE("-- Did not find COLAMD library") 213 SET(COLAMD_FOUND FALSE) 214ENDIF (EXISTS ${COLAMD_LIB}) 215 216FIND_PATH(COLAMD_INCLUDE NAMES colamd.h) 217IF (EXISTS ${COLAMD_INCLUDE}) 218 MESSAGE("-- Found COLAMD header in: ${COLAMD_INCLUDE}") 219ELSE (EXISTS ${COLAMD_INCLUDE}) 220 MESSAGE("-- Did not find COLAMD header") 221 SET(COLAMD_FOUND FALSE) 222ENDIF (EXISTS ${COLAMD_INCLUDE}) 223 224SET(CCOLAMD_FOUND TRUE) 225FIND_LIBRARY(CCOLAMD_LIB NAMES ccolamd) 226IF (EXISTS ${CCOLAMD_LIB}) 227 MESSAGE("-- Found CCOLAMD library: ${CCOLAMD_LIB}") 228ELSE (EXISTS ${CCOLAMD_LIB}) 229 MESSAGE("-- Did not find CCOLAMD library") 230 SET(CCOLAMD_FOUND FALSE) 231ENDIF (EXISTS ${CCOLAMD_LIB}) 232 233FIND_PATH(CCOLAMD_INCLUDE NAMES ccolamd.h) 234IF (EXISTS ${CCOLAMD_INCLUDE}) 235 MESSAGE("-- Found CCOLAMD header in: ${CCOLAMD_INCLUDE}") 236ELSE (EXISTS ${CCOLAMD_INCLUDE}) 237 MESSAGE("-- Did not find CCOLAMD header") 238 SET(CCOLAMD_FOUND FALSE) 239ENDIF (EXISTS ${CCOLAMD_INCLUDE}) 240 241SET(CHOLMOD_FOUND TRUE) 242FIND_LIBRARY(CHOLMOD_LIB NAMES cholmod) 243IF (EXISTS ${CHOLMOD_LIB}) 244 MESSAGE("-- Found CHOLMOD library: ${CHOLMOD_LIB}") 245ELSE (EXISTS ${CHOLMOD_LIB}) 246 MESSAGE("-- Did not find CHOLMOD library") 247 SET(CHOLMOD_FOUND FALSE) 248ENDIF (EXISTS ${CHOLMOD_LIB}) 249 250FIND_PATH(CHOLMOD_INCLUDE NAMES cholmod.h) 251IF (EXISTS ${CHOLMOD_INCLUDE}) 252 MESSAGE("-- Found CHOLMOD header in: ${CHOLMOD_INCLUDE}") 253ELSE (EXISTS ${CHOLMOD_INCLUDE}) 254 MESSAGE("-- Did not find CHOLMOD header") 255 SET(CHOLMOD_FOUND FALSE) 256ENDIF (EXISTS ${CHOLMOD_INCLUDE}) 257 258SET(SUITESPARSEQR_FOUND TRUE) 259FIND_LIBRARY(SUITESPARSEQR_LIB NAMES spqr) 260IF (EXISTS ${SUITESPARSEQR_LIB}) 261 MESSAGE("-- Found SUITESPARSEQR library: ${SUITESPARSEQR_LIB}") 262ELSE (EXISTS ${SUITESPARSEQR_LIB}) 263 MESSAGE("-- Did not find SUITESPARSEQR library") 264 SET(SUITESPARSEQR_FOUND FALSE) 265ENDIF (EXISTS ${SUITESPARSEQR_LIB}) 266 267FIND_PATH(SUITESPARSEQR_INCLUDE NAMES SuiteSparseQR.hpp) 268IF (EXISTS ${SUITESPARSEQR_INCLUDE}) 269 MESSAGE("-- Found SUITESPARSEQR header in: ${SUITESPARSEQR_INCLUDE}") 270ELSE (EXISTS ${SUITESPARSEQR_INCLUDE}) 271 MESSAGE("-- Did not find SUITESPARSEQR header") 272 SET(SUITESPARSEQR_FOUND FALSE) 273ENDIF (EXISTS ${SUITESPARSEQR_INCLUDE}) 274 275# If SuiteSparse version is >= 4 then SuiteSparse_config is required. 276# For SuiteSparse 3, UFconfig.h is required. 277SET(SUITESPARSE_CONFIG_FOUND TRUE) 278SET(UFCONFIG_FOUND TRUE) 279 280FIND_LIBRARY(SUITESPARSE_CONFIG_LIB NAMES suitesparseconfig) 281IF (EXISTS ${SUITESPARSE_CONFIG_LIB}) 282 MESSAGE("-- Found SuiteSparse_config library: ${SUITESPARSE_CONFIG_LIB}") 283ELSE (EXISTS ${SUITESPARSE_CONFIG_LIB}) 284 MESSAGE("-- Did not find SuiteSparse_config library") 285ENDIF (EXISTS ${SUITESPARSE_CONFIG_LIB}) 286 287FIND_PATH(SUITESPARSE_CONFIG_INCLUDE NAMES SuiteSparse_config.h) 288IF (EXISTS ${SUITESPARSE_CONFIG_INCLUDE}) 289 MESSAGE("-- Found SuiteSparse_config header in: ${SUITESPARSE_CONFIG_INCLUDE}") 290 SET(UFCONFIG_FOUND FALSE) 291ELSE (EXISTS ${SUITESPARSE_CONFIG_INCLUDE}) 292 MESSAGE("-- Did not find SuiteSparse_config header") 293ENDIF (EXISTS ${SUITESPARSE_CONFIG_INCLUDE}) 294 295IF (NOT EXISTS ${SUITESPARSE_CONFIG_LIB} OR 296 NOT EXISTS ${SUITESPARSE_CONFIG_INCLUDE}) 297 SET(SUITESPARSE_CONFIG_FOUND FALSE) 298 FIND_PATH(UFCONFIG_INCLUDE NAMES UFconfig.h) 299 IF (EXISTS ${UFCONFIG_INCLUDE}) 300 MESSAGE("-- Found UFconfig header in: ${UFCONFIG_INCLUDE}") 301 ELSE (EXISTS ${UFCONFIG_INCLUDE}) 302 MESSAGE("-- Did not find UFconfig header") 303 SET(UFCONFIG_FOUND FALSE) 304 ENDIF (EXISTS ${UFCONFIG_INCLUDE}) 305ENDIF (NOT EXISTS ${SUITESPARSE_CONFIG_LIB} OR 306 NOT EXISTS ${SUITESPARSE_CONFIG_INCLUDE}) 307 308FIND_LIBRARY(METIS_LIB NAMES metis) 309IF (EXISTS ${METIS_LIB}) 310 MESSAGE("-- Found METIS library: ${METIS_LIB}") 311ELSE (EXISTS ${METIS_LIB}) 312 MESSAGE("-- Did not find METIS library") 313ENDIF (EXISTS ${METIS_LIB}) 314 315# SuiteSparseQR may be compiled with Intel Threading Building Blocks. 316SET(TBB_FOUND TRUE) 317FIND_LIBRARY(TBB_LIB NAMES tbb) 318IF (EXISTS ${TBB_LIB}) 319 MESSAGE("-- Found TBB library: ${TBB_LIB}") 320ELSE (EXISTS ${TBB_LIB}) 321 MESSAGE("-- Did not find TBB library") 322 SET(TBB_FOUND FALSE) 323ENDIF (EXISTS ${TBB_LIB}) 324 325FIND_LIBRARY(TBB_MALLOC_LIB NAMES tbbmalloc) 326IF (EXISTS ${TBB_MALLOC_LIB}) 327 MESSAGE("-- Found TBB Malloc library: ${TBB_MALLOC_LIB}") 328ELSE (EXISTS ${TBB_MALLOC_LIB}) 329 MESSAGE("-- Did not find TBB library") 330 SET(TBB_FOUND FALSE) 331ENDIF (EXISTS ${TBB_MALLOC_LIB}) 332 333# We don't use SET(SUITESPARSE_FOUND ${AMD_FOUND} ...) in order to be 334# able to check whether SuiteSparse is available without expanding 335# SUITESPARSE_FOUND with ${}. This means further checks could be: 336# 337# IF (SUITESPARSE_FOUND) 338# 339# and not: 340# 341# IF (${SUITESPARSE_FOUND}) 342# 343IF (${AMD_FOUND} AND 344 ${CAMD_FOUND} AND 345 ${COLAMD_FOUND} AND 346 ${CCOLAMD_FOUND} AND 347 ${CHOLMOD_FOUND} AND 348 (${SUITESPARSE_CONFIG_FOUND} OR ${UFCONFIG_FOUND}) AND 349 ${BLAS_AND_LAPACK_FOUND}) 350 SET(SUITESPARSE_FOUND TRUE) 351ELSE () 352 SET(SUITESPARSE_FOUND FALSE) 353ENDIF () 354 355ENDIF ((NOT DEFINED SUITESPARSE) OR (DEFINED SUITESPARSE AND SUITESPARSE)) 356# By default, if all of SuiteSparse's dependencies are found, Ceres is 357# built with SuiteSparse support. -DSUITESPARSE=ON/OFF can be used to 358# enable/disable SuiteSparse explicitly. 359IF (DEFINED SUITESPARSE) 360 IF (SUITESPARSE) 361 IF (NOT SUITESPARSE_FOUND) 362 MESSAGE(FATAL_ERROR "One or more of SuiteSparse's dependencies was not found") 363 ENDIF (NOT SUITESPARSE_FOUND) 364 ELSE (SUITESPARSE) 365 ADD_DEFINITIONS(-DCERES_NO_SUITESPARSE) 366 ENDIF (SUITESPARSE) 367ELSE (DEFINED SUITESPARSE) 368 IF (SUITESPARSE_FOUND) 369 MESSAGE("-- Found all SuiteSparse dependencies. Building with SuiteSparse") 370 SET(SUITESPARSE ON) 371 ELSE (SUITESPARSE_FOUND) 372 MESSAGE("-- Did not find all SuiteSparse dependencies. Building without SuiteSparse") 373 SET(SUITESPARSE OFF) 374 ADD_DEFINITIONS(-DCERES_NO_SUITESPARSE) 375 ENDIF (SUITESPARSE_FOUND) 376ENDIF (DEFINED SUITESPARSE) 377 378# By default, if all of CXSparse's dependencies are found, Ceres is 379# built with CXSparse support. -DCXSPARSE=ON/OFF can be used to 380# enable/disable CXSparse explicitly. 381IF ((NOT DEFINED CXSPARSE) OR (DEFINED CXSPARSE AND CXSPARSE)) 382 383SET(CXSPARSE_FOUND ON) 384FIND_LIBRARY(CXSPARSE_LIB NAMES cxsparse) 385IF (EXISTS ${CXSPARSE_LIB}) 386 MESSAGE("-- Found CXSparse library in: ${CXSPARSE_LIB}") 387ELSE (EXISTS ${CXSPARSE_LIB}) 388 MESSAGE("-- Did not find CXSparse header") 389 SET(CXSPARSE_FOUND FALSE) 390ENDIF (EXISTS ${CXSPARSE_LIB}) 391 392FIND_PATH(CXSPARSE_INCLUDE NAMES cs.h) 393IF (EXISTS ${CXSPARSE_INCLUDE}) 394 MESSAGE("-- Found CXSparse header in: ${CXSPARSE_INCLUDE}") 395ELSE (EXISTS ${CXSPARSE_INCLUDE}) 396 MESSAGE("-- Did not find CXSparse header") 397 SET(CXSPARSE_FOUND FALSE) 398ENDIF (EXISTS ${CXSPARSE_INCLUDE}) 399ENDIF ((NOT DEFINED CXSPARSE) OR (DEFINED CXSPARSE AND CXSPARSE)) 400 401IF (DEFINED CXSPARSE) 402 IF (CXSPARSE) 403 IF (NOT CXSPARSE_FOUND) 404 MESSAGE(FATAL_ERROR "-- CXSparse not found.") 405 ENDIF (NOT CXSPARSE_FOUND) 406 ELSE (CXSPARSE) 407 ADD_DEFINITIONS(-DCERES_NO_CXSPARSE) 408 ENDIF (CXSPARSE) 409ELSE (DEFINED CXSPARSE) 410 IF (CXSPARSE_FOUND) 411 MESSAGE("-- Building with CXSparse support.") 412 SET(CXSPARSE ON) 413 ELSE (CXSPARSE_FOUND) 414 MESSAGE("-- Building without CXSparse.") 415 SET(CXSPARSE OFF) 416 ADD_DEFINITIONS(-DCERES_NO_CXSPARSE) 417 ENDIF (CXSPARSE_FOUND) 418ENDIF (DEFINED CXSPARSE) 419 420IF (GFLAGS) 421 FIND_LIBRARY(GFLAGS_LIB NAMES gflags) 422 IF (NOT EXISTS ${GFLAGS_LIB}) 423 MESSAGE(FATAL_ERROR 424 "Can't find Google Flags. Please specify: " 425 "-DGFLAGS_LIB=...") 426 ENDIF (NOT EXISTS ${GFLAGS_LIB}) 427 MESSAGE("-- Found Google Flags library: ${GFLAGS_LIB}") 428 FIND_PATH(GFLAGS_INCLUDE NAMES gflags/gflags.h) 429 IF (NOT EXISTS ${GFLAGS_INCLUDE}) 430 MESSAGE(FATAL_ERROR 431 "Can't find Google Flags. Please specify: " 432 "-DGFLAGS_INCLUDE=...") 433 ENDIF (NOT EXISTS ${GFLAGS_INCLUDE}) 434 MESSAGE("-- Found Google Flags header in: ${GFLAGS_INCLUDE}") 435ELSE (GFLAGS) 436 MESSAGE("-- Google Flags disabled; no tests or tools will be built!") 437 ADD_DEFINITIONS(-DCERES_NO_GFLAGS) 438ENDIF (GFLAGS) 439 440IF (MINIGLOG) 441 SET(GLOG_LIB miniglog) 442 MESSAGE("-- Using minimal Glog substitute (library): ${GLOG_LIB}") 443 SET(GLOG_INCLUDE internal/ceres/miniglog) 444 MESSAGE("-- Using minimal Glog substitute (include): ${GLOG_INCLUDE}") 445ELSE (MINIGLOG) 446 FIND_LIBRARY(GLOG_LIB NAMES glog) 447 IF (EXISTS ${GLOG_LIB}) 448 MESSAGE("-- Found Google Log library: ${GLOG_LIB}") 449 ELSE (EXISTS ${GLOG_LIB}) 450 MESSAGE(FATAL_ERROR 451 "Can't find Google Log. Please specify: -DGLOG_LIB=...") 452 ENDIF (EXISTS ${GLOG_LIB}) 453 454 FIND_PATH(GLOG_INCLUDE NAMES glog/logging.h) 455 IF (EXISTS ${GLOG_INCLUDE}) 456 MESSAGE("-- Found Google Log header in: ${GLOG_INCLUDE}") 457 ELSE (EXISTS ${GLOG_INCLUDE}) 458 MESSAGE(FATAL_ERROR 459 "Can't find Google Log. Please specify: -DGLOG_INCLUDE=...") 460 ENDIF (EXISTS ${GLOG_INCLUDE}) 461ENDIF (MINIGLOG) 462 463IF (NOT SCHUR_SPECIALIZATIONS) 464 ADD_DEFINITIONS(-DCERES_RESTRICT_SCHUR_SPECIALIZATION) 465 MESSAGE("-- Disabling Schur specializations (faster compiles)") 466ENDIF (NOT SCHUR_SPECIALIZATIONS) 467 468IF (NOT LINE_SEARCH_MINIMIZER) 469 ADD_DEFINITIONS(-DCERES_NO_LINE_SEARCH_MINIMIZER) 470 MESSAGE("-- Disabling line search minimizer") 471ENDIF (NOT LINE_SEARCH_MINIMIZER) 472 473IF (NOT CUSTOM_BLAS) 474 ADD_DEFINITIONS(-DCERES_NO_CUSTOM_BLAS) 475 MESSAGE("-- Disabling custom blas") 476ENDIF (NOT CUSTOM_BLAS) 477 478IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 479 SET(OPENMP_FOUND FALSE) 480ELSE (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 481 IF (OPENMP) 482 FIND_PACKAGE(OpenMP) 483 ENDIF (OPENMP) 484ENDIF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 485 486IF (OPENMP_FOUND) 487 MESSAGE("-- Found OpenMP.") 488 ADD_DEFINITIONS(-DCERES_USE_OPENMP) 489 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") 490 IF (UNIX) 491 # At least on Linux, we need pthreads to be enabled for mutex to 492 # compile. This may not work on Windows or Android. 493 FIND_PACKAGE(Threads REQUIRED) 494 SET(STATIC_LIBRARY_FLAGS 495 "${STATIC_LIBRARY_FLAGS} ${CMAKE_THREAD_LIBS_INIT}") 496 SET(CMAKE_SHARED_LINKER_FLAGS 497 "${CMAKE_SHARED_LINKER_FLAGS} ${CMAKE_THREAD_LIBS_INIT}") 498 ADD_DEFINITIONS(-DCERES_HAVE_PTHREAD) 499 ADD_DEFINITIONS(-DCERES_HAVE_RWLOCK) 500 ENDIF (UNIX) 501ELSE (OPENMP_FOUND) 502 MESSAGE("-- Can't find OpenMP. Disabling multithreading.") 503 ADD_DEFINITIONS(-DCERES_NO_THREADS) 504ENDIF (OPENMP_FOUND) 505 506IF (DISABLE_TR1) 507 MESSAGE("-- Replacing unordered_map/set with map/set (warning: slower!)") 508 ADD_DEFINITIONS(-DCERES_NO_TR1) 509ELSE (DISABLE_TR1) 510 MESSAGE("-- Using normal TR1 unordered_map/set") 511 # Use the std namespace for the hash<> and related templates. This may vary by 512 # system. 513 IF (MSVC) 514 IF (MSVC90) 515 # Special case for Visual Studio 2008. 516 # Newer versions have got tr1 symbols in another namespace, 517 # and this is being handled in Else branch of this condition. 518 # Probably Visual studio 2003 and 2005 also shall be handled here, 519 # but don't have by hand to verify and most likely they're not 520 # used by Ceres users anyway. 521 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std { namespace tr1 {\"") 522 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}}\"") 523 ELSE (MSVC90) 524 # This is known to work with Visual Studio 2010 Express. 525 # Further, for as long Visual Studio 2012 didn't move tr1 to 526 # just another namespace, the same define will work for it as well. 527 # Hopefully all further versions will also keep working with this define. 528 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std {\"") 529 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}\"") 530 ENDIF(MSVC90) 531 ELSE (MSVC) 532 # This is known to work with recent versions of Linux and Mac OS X. 533 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_START=namespace std { namespace tr1 {\"") 534 ADD_DEFINITIONS("\"-DCERES_HASH_NAMESPACE_END=}}\"") 535 ENDIF (MSVC) 536ENDIF (DISABLE_TR1) 537 538INCLUDE_DIRECTORIES( 539 include 540 internal 541 internal/ceres 542 ${GLOG_INCLUDE} 543 ${EIGEN_INCLUDE} 544 ) 545 546FILE(GLOB CERES_HDRS ${CMAKE_SOURCE_DIR}/include/ceres/*.h) 547INSTALL(FILES ${CERES_HDRS} DESTINATION include/ceres) 548 549FILE(GLOB CERES_PUBLIC_INTERNAL_HDRS ${CMAKE_SOURCE_DIR}/include/ceres/internal/*.h) 550INSTALL(FILES ${CERES_PUBLIC_INTERNAL_HDRS} DESTINATION include/ceres/internal) 551 552IF (SUITESPARSE) 553 INCLUDE_DIRECTORIES(${AMD_INCLUDE}) 554 INCLUDE_DIRECTORIES(${CAMD_INCLUDE}) 555 INCLUDE_DIRECTORIES(${COLAMD_INCLUDE}) 556 INCLUDE_DIRECTORIES(${CCOLAMD_INCLUDE}) 557 INCLUDE_DIRECTORIES(${CHOLMOD_INCLUDE}) 558 INCLUDE_DIRECTORIES(${SUITESPARSEQR_INCLUDE}) 559 IF (SUITESPARSE_CONFIG_FOUND) 560 INCLUDE_DIRECTORIES(${SUITESPARSE_CONFIG_INCLUDE}) 561 ENDIF (SUITESPARSE_CONFIG_FOUND) 562 IF (UFCONFIG_FOUND) 563 INCLUDE_DIRECTORIES(${UFCONFIG_INCLUDE}) 564 ENDIF (UFCONFIG_FOUND) 565ENDIF (SUITESPARSE) 566 567IF (CXSPARSE) 568 INCLUDE_DIRECTORIES(${CXSPARSE_INCLUDE}) 569ENDIF (CXSPARSE) 570 571IF (GFLAGS) 572 INCLUDE_DIRECTORIES(${GFLAGS_INCLUDE}) 573ENDIF (GFLAGS) 574 575# Change the default build type from Debug to Release, while still 576# supporting overriding the build type. 577# 578# The CACHE STRING logic here and elsewhere is needed to force CMake 579# to pay attention to the value of these variables. 580IF (NOT CMAKE_BUILD_TYPE) 581 MESSAGE("-- No build type specified; defaulting to CMAKE_BUILD_TYPE=Release.") 582 SET(CMAKE_BUILD_TYPE Release CACHE STRING 583 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." 584 FORCE) 585ELSE (NOT CMAKE_BUILD_TYPE) 586 IF (CMAKE_BUILD_TYPE STREQUAL "Debug") 587 MESSAGE("\n=================================================================================") 588 MESSAGE("\n-- Build type: Debug. Performance will be terrible!") 589 MESSAGE("-- Add -DCMAKE_BUILD_TYPE=Release to the CMake command line to get an optimized build.") 590 MESSAGE("\n=================================================================================") 591 ENDIF (CMAKE_BUILD_TYPE STREQUAL "Debug") 592ENDIF (NOT CMAKE_BUILD_TYPE) 593 594# Set the default Ceres flags to an empty string. 595SET (CERES_CXX_FLAGS) 596 597IF (CMAKE_BUILD_TYPE STREQUAL "Release") 598 IF (CMAKE_COMPILER_IS_GNUCXX) 599 # Linux 600 IF (CMAKE_SYSTEM_NAME MATCHES "Linux") 601 SET (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -march=native -mtune=native") 602 ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux") 603 # Mac OS X 604 IF (CMAKE_SYSTEM_NAME MATCHES "Darwin") 605 SET (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -msse3") 606 # Use of -fast only applicable for Apple's GCC 607 # Assume this is being used if GCC version < 4.3 on OSX 608 EXECUTE_PROCESS(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) 609 IF (GCC_VERSION VERSION_LESS 4.3) 610 SET (CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -fast") 611 ENDIF (GCC_VERSION VERSION_LESS 4.3) 612 ENDIF (CMAKE_SYSTEM_NAME MATCHES "Darwin") 613 ENDIF (CMAKE_COMPILER_IS_GNUCXX) 614 IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 615 # Use of -O4 requires use of gold linker & LLVM-gold plugin, which might 616 # well not be present / in use and without which files will compile, but 617 # not link ('file not recognized') so explicitly check for support 618 INCLUDE(CheckCXXCompilerFlag) 619 CHECK_CXX_COMPILER_FLAG("-O4" HAVE_LTO_SUPPORT) 620 IF (HAVE_LTO_SUPPORT) 621 MESSAGE(STATUS "Enabling link-time optimization (-O4)") 622 SET(CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -O4") 623 ELSE () 624 MESSAGE(STATUS "Compiler/linker does not support link-time optimization (-O4), disabling.") 625 ENDIF (HAVE_LTO_SUPPORT) 626 ENDIF () 627ENDIF (CMAKE_BUILD_TYPE STREQUAL "Release") 628 629SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${CERES_CXX_FLAGS}") 630 631# After the tweaks for the compile settings, disable some warnings on MSVC. 632IF (MSVC) 633 # Disable signed/unsigned int conversion warnings. 634 ADD_DEFINITIONS("/wd4018") 635 # Disable warning about using struct/class for the same symobl. 636 ADD_DEFINITIONS("/wd4099") 637 # Disable warning about the insecurity of using "std::copy". 638 ADD_DEFINITIONS("/wd4996") 639 # Disable performance warning about int-to-bool conversion. 640 ADD_DEFINITIONS("/wd4800") 641 # Disable performance warning about fopen insecurity. 642 ADD_DEFINITIONS("/wd4996") 643 # Disable warning about int64 to int32 conversion. Disabling 644 # this warning may not be correct; needs investigation. 645 # TODO(keir): Investigate these warnings in more detail. 646 ADD_DEFINITIONS("/wd4244") 647 # It's not possible to use STL types in DLL interfaces in a portable and 648 # reliable way. However, that's what happens with Google Log and Google Flags 649 # on Windows. MSVC gets upset about this and throws warnings that we can't do 650 # much about. The real solution is to link static versions of Google Log and 651 # Google Test, but that seems tricky on Windows. So, disable the warning. 652 ADD_DEFINITIONS("/wd4251") 653 654 # Google Flags doesn't have their DLL import/export stuff set up correctly, 655 # which results in linker warnings. This is irrelevant for Ceres, so ignore 656 # the warnings. 657 SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ignore:4049") 658 659 # Tuple sizes of 10 are used by Gtest. 660 ADD_DEFINITIONS("-D_VARIADIC_MAX=10") 661ENDIF (MSVC) 662 663IF (UNIX) 664 # GCC is not strict enough by default, so enable most of the warnings. 665 SET(CMAKE_CXX_FLAGS 666 "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-missing-field-initializers") 667ENDIF (UNIX) 668 669# Use a larger inlining threshold for Clang, since it hobbles Eigen, 670# resulting in an unreasonably slow version of the blas routines. The 671# -Qunused-arguments is needed because CMake passes the inline 672# threshold to the linker and clang complains about it and dies. 673IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 674 SET(CMAKE_CXX_FLAGS 675 "${CMAKE_CXX_FLAGS} -Qunused-arguments -mllvm -inline-threshold=600 -Wno-return-type-c-linkage") 676ENDIF () 677 678ADD_SUBDIRECTORY(internal/ceres) 679 680IF (BUILD_DOCUMENTATION) 681 MESSAGE("-- Documentation building is enabled") 682 683 # Make CMake aware of the cmake folder, in order to find 'FindSphinx.cmake' 684 SET (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") 685 686 # Generate the User's Guide (html). 687 # The corresponding target is UserGuide, but is included in ALL. 688 ADD_SUBDIRECTORY(docs) 689ENDIF (BUILD_DOCUMENTATION) 690 691IF (BUILD_EXAMPLES) 692 MESSAGE("-- Build the examples.") 693 ADD_SUBDIRECTORY(examples) 694ELSE (BUILD_EXAMPLES) 695 MESSAGE("-- Do not build any example.") 696ENDIF (BUILD_EXAMPLES) 697 698# Add an uninstall target to remove all installed files. 699CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/cmake/uninstall.cmake.in" 700 "${CMAKE_BINARY_DIR}/cmake/uninstall.cmake" 701 IMMEDIATE @ONLY) 702 703ADD_CUSTOM_TARGET(uninstall 704 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake/uninstall.cmake) 705 706# Set up install directories. INCLUDE_INSTALL_DIR, LIB_INSTALL_DIR and 707# CMAKECONFIG_INSTALL_DIR must not be absolute paths. 708SET(INCLUDE_INSTALL_DIR include) 709SET(LIB_INSTALL_DIR lib) 710SET(CMAKECONFIG_INSTALL_DIR share/Ceres) 711 712# This "exports" all targets which have been put into the export set 713# "CeresExport". This means that CMake generates a file with the given 714# filename, which can later on be loaded by projects using this package. 715# This file contains ADD_LIBRARY(bar IMPORTED) statements for each target 716# in the export set, so when loaded later on CMake will create "imported" 717# library targets from these, which can be used in many ways in the same way 718# as a normal library target created via a normal ADD_LIBRARY(). 719INSTALL(EXPORT CeresExport 720 DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE CeresTargets.cmake) 721 722# Figure out the relative path from the installed Config.cmake file to the 723# install prefix (which may be at runtime different from the chosen 724# CMAKE_INSTALL_PREFIX if under Windows the package was installed anywhere) 725# This relative path will be configured into the CeresConfig.cmake. 726FILE(RELATIVE_PATH relInstallDir 727 ${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR} ${CMAKE_INSTALL_PREFIX}) 728 729# Create a CeresConfig.cmake file. <name>Config.cmake files are searched by 730# FIND_PACKAGE() automatically. We configure that file so that we can put any 731# information we want in it, e.g. version numbers, include directories, etc. 732CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/cmake/CeresConfig.cmake.in" 733 "${CMAKE_CURRENT_BINARY_DIR}/CeresConfig.cmake" @ONLY) 734 735# Additionally, when CMake has found a CeresConfig.cmake, it can check for a 736# CeresConfigVersion.cmake in the same directory when figuring out the version 737# of the package when a version has been specified in the FIND_PACKAGE() call, 738# e.g. FIND_PACKAGE(Ceres [1.4.2] REQUIRED). The version argument is optional. 739CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/cmake/CeresConfigVersion.cmake.in" 740 "${CMAKE_CURRENT_BINARY_DIR}/CeresConfigVersion.cmake" @ONLY) 741 742# Install these two files into the same directory as the generated exports-file. 743INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/CeresConfig.cmake" 744 "${CMAKE_CURRENT_BINARY_DIR}/CeresConfigVersion.cmake" 745 "${CMAKE_SOURCE_DIR}/cmake/depend.cmake" 746 DESTINATION ${CMAKECONFIG_INSTALL_DIR}) 747