Adding function to update CMake cache variables and preserve help.
- Previously we were replicating the same two lines to update a cache
variable whilst preserving its help string.
- This commit adds a function which wraps up this common operation into
a single line.
Change-Id: Ic78a5adf5d59262bbbcec1e353ded7620391e862
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 778682e..319407a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -66,6 +66,7 @@
# Make CMake aware of the cmake folder for local FindXXX scripts.
SET (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
+INCLUDE(UpdateCacheVariable)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
@@ -117,11 +118,8 @@
MESSAGE(
"-- Disabling tests. The flags BUILD_TESTING and BUILD_SHARED_LIBS"
" are incompatible with MSVC."
- )
- # Retain the help string associated with the BUILD_TESTING option
- # and turn testing off.
- GET_PROPERTY(HELP_STRING CACHE BUILD_TESTING PROPERTY HELPSTRING)
- SET(BUILD_TESTING OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ )
+ UPDATE_CACHE_VARIABLE(BUILD_TESTING OFF)
ENDIF (BUILD_TESTING AND BUILD_SHARED_LIBS)
ENDIF (MSVC)
@@ -206,10 +204,7 @@
ENDIF (BLAS_FOUND)
IF (NOT (LAPACK_FOUND AND BLAS_FOUND))
- # Retain the help string associated with the LAPACK option
- # when updating it to disable use of LAPACK.
- GET_PROPERTY(HELP_STRING CACHE LAPACK PROPERTY HELPSTRING)
- SET(LAPACK OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(LAPACK OFF)
LIST(APPEND CERES_COMPILE_OPTIONS CERES_NO_LAPACK)
ENDIF (NOT (LAPACK_FOUND AND BLAS_FOUND))
ELSE (LAPACK)
@@ -224,10 +219,7 @@
# via SuiteSparse to LAPACK.
MESSAGE("-- Disabling SuiteSparse as use of LAPACK has been disabled, "
"turn ON LAPACK to enable (optional) building with SuiteSparse.")
- # Retain the help string associated with the SUITESPARSE option
- # when updating it to disable use of SuiteSparse.
- GET_PROPERTY(HELP_STRING CACHE SUITESPARSE PROPERTY HELPSTRING)
- SET(SUITESPARSE OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(SUITESPARSE OFF)
ENDIF (SUITESPARSE AND NOT LAPACK)
IF (SUITESPARSE)
# By default, if SuiteSparse and all dependencies are found, Ceres is
@@ -261,10 +253,7 @@
# Disable use of SuiteSparse if it cannot be found and continue.
MESSAGE("-- Did not find all SuiteSparse dependencies, disabling "
"SuiteSparse support.")
- # Retain the help string associated with the SUITESPARSE option
- # when updating it to disable use of SuiteSparse.
- GET_PROPERTY(HELP_STRING CACHE SUITESPARSE PROPERTY HELPSTRING)
- SET(SUITESPARSE OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(SUITESPARSE OFF)
LIST(APPEND CERES_COMPILE_OPTIONS CERES_NO_SUITESPARSE)
ENDIF (SUITESPARSE_FOUND)
ELSE (SUITESPARSE)
@@ -284,10 +273,7 @@
ELSE (CXSPARSE_FOUND)
# Disable use of CXSparse if it cannot be found and continue.
MESSAGE("-- Did not find CXSparse, Building without CXSparse.")
- # Retain the help string associated with the CXSPARSE option
- # when updating it to disable use of CXSparse.
- GET_PROPERTY(HELP_STRING CACHE CXSPARSE PROPERTY HELPSTRING)
- SET(CXSPARSE OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(CXSPARSE OFF)
LIST(APPEND CERES_COMPILE_OPTIONS CERES_NO_CXSPARSE)
ENDIF (CXSPARSE_FOUND)
ELSE (CXSPARSE)
@@ -313,10 +299,7 @@
ELSE (GFLAGS_FOUND)
MESSAGE("-- Did not find Google Flags (gflags), Building without gflags "
"- no tests or tools will be built!")
- # Retain the help string associated with the GFLAGS option
- # when updating it to disable use of gflags.
- GET_PROPERTY(HELP_STRING CACHE GFLAGS PROPERTY HELPSTRING)
- SET(GFLAGS OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(GFLAGS OFF)
ENDIF (GFLAGS_FOUND)
ELSE (GFLAGS)
MESSAGE("-- Google Flags disabled; no tests or tools will be built!")
@@ -370,10 +353,7 @@
IF (OPENMP)
# Clang does not (yet) support OpenMP.
IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
- # Retain the help string associated with the OPENMP option
- # when updating it to disable use of OPENMP.
- GET_PROPERTY(HELP_STRING CACHE OPENMP PROPERTY HELPSTRING)
- SET(OPENMP OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(OPENMP OFF)
MESSAGE("-- Compiler is Clang, disabling OpenMP.")
LIST(APPEND CERES_COMPILE_OPTIONS CERES_NO_THREADS)
ELSE (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
@@ -392,10 +372,7 @@
ENDIF (UNIX)
ELSE (OPENMP_FOUND)
MESSAGE("-- Failed to find OpenMP, disabling.")
- # Retain the help string associated with the OPENMP option
- # when updating it to disable use of OPENMP.
- GET_PROPERTY(HELP_STRING CACHE OPENMP PROPERTY HELPSTRING)
- SET(OPENMP OFF CACHE BOOL "${HELP_STRING}" FORCE)
+ UPDATE_CACHE_VARIABLE(OPENMP OFF)
LIST(APPEND CERES_COMPILE_OPTIONS CERES_NO_THREADS)
ENDIF (OPENMP_FOUND)
ENDIF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
diff --git a/cmake/UpdateCacheVariable.cmake b/cmake/UpdateCacheVariable.cmake
new file mode 100644
index 0000000..759de2e
--- /dev/null
+++ b/cmake/UpdateCacheVariable.cmake
@@ -0,0 +1,43 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2014 Google Inc. All rights reserved.
+# http://code.google.com/p/ceres-solver/
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+# * Neither the name of Google Inc. nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# Author: alexs.mac@gmail.com (Alex Stewart)
+
+# By default, there is no easy way in CMake to set the value of a cache
+# variable without reinitialising it, which involves resetting its
+# associated help string. This is particularly annoying for CMake options
+# where they need to programmatically updated.
+#
+# This function automates this process by getting the current help string
+# for the cache variable to update, then reinitialising it with the new
+# value, but with the original help string.
+FUNCTION(UPDATE_CACHE_VARIABLE VAR_NAME VALUE)
+ GET_PROPERTY(HELP_STRING CACHE ${VAR_NAME} PROPERTY HELPSTRING)
+ GET_PROPERTY(VAR_TYPE CACHE ${VAR_NAME} PROPERTY TYPE)
+ SET(${VAR_NAME} ${VALUE} CACHE ${VAR_TYPE} "${HELP_STRING}" FORCE)
+ENDFUNCTION()