Squashed 'third_party/ceres/' content from commit e51e9b4

Change-Id: I763587619d57e594d3fa158dc3a7fe0b89a1743b
git-subtree-dir: third_party/ceres
git-subtree-split: e51e9b46f6ca88ab8b2266d0e362771db6d98067
diff --git a/cmake/AddGerritCommitHook.cmake b/cmake/AddGerritCommitHook.cmake
new file mode 100644
index 0000000..65b2fab
--- /dev/null
+++ b/cmake/AddGerritCommitHook.cmake
@@ -0,0 +1,84 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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.
+#
+# Authors: keir@google.com (Keir Mierle)
+#          alexs.mac@gmail.com (Alex Stewart)
+
+# Set up the git hook to make Gerrit Change-Id: lines in commit messages.
+function(ADD_GERRIT_COMMIT_HOOK SOURCE_DIR BINARY_DIR)
+  if (NOT (EXISTS ${SOURCE_DIR} AND IS_DIRECTORY ${SOURCE_DIR}))
+    message(FATAL_ERROR "Specified SOURCE_DIR: ${SOURCE_DIR} does not exist, "
+      "or is not a directory, cannot add Gerrit commit hook.")
+  endif()
+  if (NOT (EXISTS ${BINARY_DIR} AND IS_DIRECTORY ${BINARY_DIR}))
+    message(FATAL_ERROR "Specified BINARY_DIR: ${BINARY_DIR} does not exist, "
+      "or is not a directory, cannot add Gerrit commit hook.")
+  endif()
+  unset (LOCAL_GIT_DIRECTORY)
+  if (EXISTS ${SOURCE_DIR}/.git)
+    if (IS_DIRECTORY ${SOURCE_DIR}/.git)
+      # .git directory can be found on Unix based system, or on Windows with
+      # Git Bash (shipped with msysgit).
+      set (LOCAL_GIT_DIRECTORY ${SOURCE_DIR}/.git)
+    else(IS_DIRECTORY ${SOURCE_DIR}/.git)
+      # .git is a file, this means Ceres is a git submodule of another project
+      # and our .git file contains the path to the git directory which manages
+      # Ceres, so we should add the gerrit hook there.
+      file(READ ${SOURCE_DIR}/.git GIT_SUBMODULE_FILE_CONTENTS)
+      # Strip any trailing newline characters, s/t we get a valid path.
+      string(REGEX REPLACE "gitdir:[ ]*([^$].*)[\n].*" "${SOURCE_DIR}/\\1"
+        GIT_SUBMODULE_GIT_DIRECTORY_PATH "${GIT_SUBMODULE_FILE_CONTENTS}")
+      get_filename_component(GIT_SUBMODULE_GIT_DIRECTORY_PATH
+        "${GIT_SUBMODULE_GIT_DIRECTORY_PATH}" ABSOLUTE)
+      if (EXISTS ${GIT_SUBMODULE_GIT_DIRECTORY_PATH}
+          AND IS_DIRECTORY ${GIT_SUBMODULE_GIT_DIRECTORY_PATH})
+        set(LOCAL_GIT_DIRECTORY "${GIT_SUBMODULE_GIT_DIRECTORY_PATH}")
+      endif()
+    endif()
+  else (EXISTS ${SOURCE_DIR}/.git)
+    # TODO(keir) Add proper Windows support.
+  endif (EXISTS ${SOURCE_DIR}/.git)
+
+  if (EXISTS ${LOCAL_GIT_DIRECTORY})
+    if (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg)
+      message(STATUS "Detected Ceres being used as a git submodule, adding "
+        "commit hook for Gerrit to: ${LOCAL_GIT_DIRECTORY}")
+      # Download the hook only if it is not already present.
+      file(DOWNLOAD https://ceres-solver-review.googlesource.com/tools/hooks/commit-msg
+        ${BINARY_DIR}/commit-msg)
+
+      # Make the downloaded file executable, since it is not by default.
+      file(COPY ${BINARY_DIR}/commit-msg
+        DESTINATION ${LOCAL_GIT_DIRECTORY}/hooks/
+        FILE_PERMISSIONS
+        OWNER_READ OWNER_WRITE OWNER_EXECUTE
+        GROUP_READ GROUP_WRITE GROUP_EXECUTE
+        WORLD_READ WORLD_EXECUTE)
+    endif (NOT EXISTS ${LOCAL_GIT_DIRECTORY}/hooks/commit-msg)
+  endif (EXISTS ${LOCAL_GIT_DIRECTORY})
+endfunction()
diff --git a/cmake/AppendTargetProperty.cmake b/cmake/AppendTargetProperty.cmake
new file mode 100644
index 0000000..e0bc3a4
--- /dev/null
+++ b/cmake/AppendTargetProperty.cmake
@@ -0,0 +1,61 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+
+# Append item(s) to a property on a declared CMake target:
+#
+#    append_target_property(target property item_to_append1
+#                                           [... item_to_appendN])
+#
+# The set_target_properties() CMake function will overwrite the contents of the
+# specified target property.  This function instead appends to it, so can
+# be called multiple times with the same target & property to iteratively
+# populate it.
+function(append_target_property TARGET PROPERTY)
+  if (NOT TARGET ${TARGET})
+    message(FATAL_ERROR "Invalid target: ${TARGET} cannot append: ${ARGN} "
+      "to property: ${PROPERTY}")
+  endif()
+  if (NOT PROPERTY)
+    message(FATAL_ERROR "Invalid property to update for target: ${TARGET}")
+  endif()
+  # Get the initial state of the specified property for the target s/t
+  # we can append to it (not overwrite it).
+  get_target_property(INITIAL_PROPERTY_STATE ${TARGET} ${PROPERTY})
+  if (NOT INITIAL_PROPERTY_STATE)
+    # Ensure that if the state is unset, we do not insert the XXX-NOTFOUND
+    # returned by CMake into the property.
+    set(INITIAL_PROPERTY_STATE "")
+  endif()
+  # Delistify (remove ; separators) the potentially set of items to append
+  # to the specified target property.
+  string(REPLACE ";" " " ITEMS_TO_APPEND "${ARGN}")
+  set_target_properties(${TARGET} PROPERTIES ${PROPERTY}
+    "${INITIAL_PROPERTY_STATE} ${ITEMS_TO_APPEND}")
+endfunction()
diff --git a/cmake/CeresCompileOptionsToComponents.cmake b/cmake/CeresCompileOptionsToComponents.cmake
new file mode 100644
index 0000000..ccf0fa2
--- /dev/null
+++ b/cmake/CeresCompileOptionsToComponents.cmake
@@ -0,0 +1,91 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2016 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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.
+#
+# Authors: alexs.mac@gmail.com (Alex Stewart)
+#
+
+# Conditionally add a value to the output list based on whether the specified
+# value is found in the input list.
+function(update_output_if_found INPUT_LIST_VAR OUTPUT_LIST_VAR ITEM_TO_FIND VAR_TO_COPY_IF_FOUND VAR_TO_COPY_IF_NOT_FOUND)
+  list(FIND ${INPUT_LIST_VAR} "${ITEM_TO_FIND}" HAVE_ITEM)
+  # list(FIND ..) returns -1 if the element was not in the list, but CMake
+  # interprets if (VAR) to be true if VAR is any non-zero number, even
+  # negative ones, hence we have to explicitly check for >= 0.
+  if (HAVE_ITEM GREATER -1)
+    list(APPEND ${OUTPUT_LIST_VAR} "${VAR_TO_COPY_IF_FOUND}")
+  else()
+    list(APPEND ${OUTPUT_LIST_VAR} "${VAR_TO_COPY_IF_NOT_FOUND}")
+  endif()
+  set(${OUTPUT_LIST_VAR} ${${OUTPUT_LIST_VAR}} PARENT_SCOPE)
+endfunction()
+
+# Helpers for update_output_if_found() to improve legibility when dealing with
+# USE_XXX & NO_XXX option types in ceres_compile_options_to_components().
+macro(add_to_output_if_found INPUT_LIST_VAR OUTPUT_LIST_VAR ITEM_TO_FIND VAR_TO_COPY_IF_FOUND)
+  update_output_if_found(${INPUT_LIST_VAR}
+    ${OUTPUT_LIST_VAR}
+    "${ITEM_TO_FIND}"
+    "${VAR_TO_COPY_IF_FOUND}"
+    "") # Copy nothing if not found.
+endmacro()
+
+macro(add_to_output_if_not_found INPUT_LIST_VAR OUTPUT_LIST_VAR ITEM_TO_FIND VAR_TO_COPY_IF_NOT_FOUND)
+  update_output_if_found(${INPUT_LIST_VAR}
+    ${OUTPUT_LIST_VAR}
+    "${ITEM_TO_FIND}"
+    "" # Copy nothing if found
+    "${VAR_TO_COPY_IF_NOT_FOUND}")
+endmacro()
+
+# Convert the Ceres compile options specified by: CURRENT_CERES_COMPILE_OPTIONS
+# into the corresponding list of Ceres components (names), which may be used in:
+# find_package(Ceres COMPONENTS <XXX>).
+function(ceres_compile_options_to_components CURRENT_CERES_COMPILE_OPTIONS CERES_COMPONENTS_VAR)
+  # To enable users to specify that they want *a* sparse linear algebra backend
+  # without having to specify explicitly which one, for each sparse library we
+  # add the 'meta-module': SparseLinearAlgebraLibrary in addition to their own
+  # module name.
+  add_to_output_if_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_USE_EIGEN_SPARSE "EigenSparse;SparseLinearAlgebraLibrary")
+  add_to_output_if_not_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_NO_LAPACK "LAPACK")
+  add_to_output_if_not_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_NO_SUITESPARSE "SuiteSparse;SparseLinearAlgebraLibrary")
+  add_to_output_if_not_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_NO_CXSPARSE "CXSparse;SparseLinearAlgebraLibrary")
+  add_to_output_if_not_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_NO_ACCELERATE_SPARSE "AccelerateSparse;SparseLinearAlgebraLibrary")
+  add_to_output_if_not_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_RESTRICT_SCHUR_SPECIALIZATION "SchurSpecializations")
+  add_to_output_if_found(CURRENT_CERES_COMPILE_OPTIONS ${CERES_COMPONENTS_VAR}
+    CERES_USE_OPENMP "OpenMP;Multithreading")
+  # Remove duplicates of SparseLinearAlgebraLibrary if multiple sparse backends
+  # are present.
+  list(REMOVE_DUPLICATES ${CERES_COMPONENTS_VAR})
+  set(${CERES_COMPONENTS_VAR} "${${CERES_COMPONENTS_VAR}}" PARENT_SCOPE)
+endfunction()
diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in
new file mode 100644
index 0000000..0ef7021
--- /dev/null
+++ b/cmake/CeresConfig.cmake.in
@@ -0,0 +1,325 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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.
+#
+# Authors: pablo.speciale@gmail.com (Pablo Speciale)
+#          alexs.mac@gmail.com (Alex Stewart)
+#
+
+# Config file for Ceres Solver - Find Ceres & dependencies.
+#
+# This file is used by CMake when find_package(Ceres) is invoked and either
+# the directory containing this file either is present in CMAKE_MODULE_PATH
+# (if Ceres was installed), or exists in the local CMake package registry if
+# the Ceres build directory was exported.
+#
+# This module defines the following variables:
+#
+# Ceres_FOUND / CERES_FOUND: True if Ceres has been successfully
+#                            found. Both variables are set as although
+#                            FindPackage() only references Ceres_FOUND
+#                            in Config mode, given the conventions for
+#                            <package>_FOUND when FindPackage() is
+#                            called in Module mode, users could
+#                            reasonably expect to use CERES_FOUND
+#                            instead.
+#
+# CERES_VERSION: Version of Ceres found.
+#
+# CERES_LIBRARIES: Libraries for Ceres and all
+#                  dependencies against which Ceres was
+#                  compiled. This will not include any optional
+#                  dependencies that were disabled when Ceres was
+#                  compiled.
+#
+# NOTE: There is no equivalent of CERES_INCLUDE_DIRS as the exported
+#       CMake target already includes the definition of its public
+#       include directories.
+
+# Called if we failed to find Ceres or any of its required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(CERES_REPORT_NOT_FOUND REASON_MSG)
+  # FindPackage() only references Ceres_FOUND, and requires it to be
+  # explicitly set FALSE to denote not found (not merely undefined).
+  set(Ceres_FOUND FALSE)
+  set(CERES_FOUND FALSE)
+  unset(CERES_INCLUDE_DIR)
+  unset(CERES_INCLUDE_DIRS)
+  unset(CERES_LIBRARIES)
+
+  # Reset the CMake module path to its state when this script was called.
+  set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
+  # FindPackage() use the camelcase library name, not uppercase.
+  if (Ceres_FIND_QUIETLY)
+    message(STATUS "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
+  elseif (Ceres_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
+    # that prevents generation, but continues configuration.
+    message(SEND_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
+  endif ()
+  return()
+endmacro(CERES_REPORT_NOT_FOUND)
+
+# ceres_pretty_print_cmake_list( OUTPUT_VAR [item1 [item2 ... ]] )
+#
+# Sets ${OUTPUT_VAR} in the caller's scope to a human-readable string
+# representation of the list passed as the remaining arguments formed
+# as: "[item1, item2, ..., itemN]".
+function(ceres_pretty_print_cmake_list OUTPUT_VAR)
+  string(REPLACE ";" ", " PRETTY_LIST_STRING "[${ARGN}]")
+  set(${OUTPUT_VAR} "${PRETTY_LIST_STRING}" PARENT_SCOPE)
+endfunction()
+
+# The list of (optional) components this version of Ceres was compiled with.
+set(CERES_COMPILED_COMPONENTS "@CERES_COMPILED_COMPONENTS@")
+
+# If Ceres was not installed, then by definition it was exported
+# from a build directory.
+set(CERES_WAS_INSTALLED @SETUP_CERES_CONFIG_FOR_INSTALLATION@)
+
+# Record the state of the CMake module path when this script was
+# called so that we can ensure that we leave it in the same state on
+# exit as it was on entry, but modify it locally.
+set(CALLERS_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
+
+# Get the (current, i.e. installed) directory containing this file.
+get_filename_component(CERES_CURRENT_CONFIG_DIR
+  "${CMAKE_CURRENT_LIST_FILE}" PATH)
+
+if (CERES_WAS_INSTALLED)
+  # Reset CMake module path to the installation directory of this
+  # script, thus we will use the FindPackage() scripts shipped with
+  # Ceres to find Ceres' dependencies, even if the user has equivalently
+  # named FindPackage() scripts in their project.
+  set(CMAKE_MODULE_PATH ${CERES_CURRENT_CONFIG_DIR})
+
+  # Build the absolute root install directory as a relative path
+  # (determined when Ceres was configured & built) from the current
+  # install directory for this this file.  This allows for the install
+  # tree to be relocated, after Ceres was built, outside of CMake.
+  get_filename_component(CURRENT_ROOT_INSTALL_DIR
+    ${CERES_CURRENT_CONFIG_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
+    ABSOLUTE)
+  if (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
+    ceres_report_not_found(
+      "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
+      "determined from relative path from CeresConfig.cmake install location: "
+      "${CERES_CURRENT_CONFIG_DIR}, does not exist. Either the install "
+      "directory was deleted, or the install tree was only partially relocated "
+      "outside of CMake after Ceres was built.")
+  endif (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
+
+else(CERES_WAS_INSTALLED)
+  # Ceres was exported from the build tree.
+  set(CERES_EXPORTED_BUILD_DIR ${CERES_CURRENT_CONFIG_DIR})
+  get_filename_component(CERES_EXPORTED_SOURCE_DIR
+    ${CERES_EXPORTED_BUILD_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
+    ABSOLUTE)
+  if (NOT EXISTS ${CERES_EXPORTED_SOURCE_DIR})
+    ceres_report_not_found(
+      "Ceres exported source directory: ${CERES_EXPORTED_SOURCE_DIR}, "
+      "determined from relative path from CeresConfig.cmake exported build "
+      "directory: ${CERES_EXPORTED_BUILD_DIR} does not exist.")
+  endif()
+
+  # Reset CMake module path to the cmake directory in the Ceres source
+  # tree which was exported, thus we will use the FindPackage() scripts shipped
+  # with Ceres to find Ceres' dependencies, even if the user has equivalently
+  # named FindPackage() scripts in their project.
+  set(CMAKE_MODULE_PATH ${CERES_EXPORTED_SOURCE_DIR}/cmake)
+endif(CERES_WAS_INSTALLED)
+
+# Set the version.
+set(CERES_VERSION @CERES_VERSION@ )
+
+include(CMakeFindDependencyMacro)
+find_dependency(Threads)
+
+# Eigen.
+# Flag set during configuration and build of Ceres.
+set(CERES_EIGEN_VERSION @EIGEN_VERSION@)
+set(EIGEN_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION@)
+# Append the locations of Eigen when Ceres was built to the search path hints.
+if (EIGEN_WAS_BUILT_WITH_CMAKE)
+  set(Eigen3_DIR @Eigen3_DIR@)
+  set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
+else()
+  list(APPEND EIGEN_INCLUDE_DIR_HINTS @EIGEN_INCLUDE_DIR@)
+endif()
+# Search quietly to control the timing of the error message if not found. The
+# search should be for an exact match, but for usability reasons do a soft
+# match and reject with an explanation below.
+find_package(Eigen ${CERES_EIGEN_VERSION} QUIET)
+if (EIGEN_FOUND)
+  if (NOT EIGEN_VERSION VERSION_EQUAL CERES_EIGEN_VERSION)
+    # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the
+    # specified version. However, only version = is supported. Improve
+    # usability by explaining why we don't accept non-exact version matching.
+    ceres_report_not_found("Found Eigen dependency, but the version of Eigen "
+      "found (${EIGEN_VERSION}) does not exactly match the version of Eigen "
+      "Ceres was compiled with (${CERES_EIGEN_VERSION}). This can cause subtle "
+      "bugs by triggering violations of the One Definition Rule. See the "
+      "Wikipedia article http://en.wikipedia.org/wiki/One_Definition_Rule "
+      "for more details")
+  endif ()
+  message(STATUS "Found required Ceres dependency: "
+    "Eigen version ${CERES_EIGEN_VERSION} in ${EIGEN_INCLUDE_DIRS}")
+else (EIGEN_FOUND)
+  ceres_report_not_found("Missing required Ceres "
+    "dependency: Eigen version ${CERES_EIGEN_VERSION}, please set "
+    "EIGEN_INCLUDE_DIR.")
+endif (EIGEN_FOUND)
+
+# Glog.
+# Flag set during configuration and build of Ceres.
+set(CERES_USES_MINIGLOG @MINIGLOG@)
+set(CERES_USES_GFLAGS @GFLAGS@)
+if (CERES_USES_MINIGLOG)
+  # Output message at standard log level (not the lower STATUS) so that
+  # the message is output in GUI during configuration to warn user.
+  message("-- Found Ceres compiled with miniglog substitute "
+    "for glog, beware this will likely cause problems if glog is later linked.")
+else(CERES_USES_MINIGLOG)
+  # As imported CMake targets are not re-exported when a dependent target is
+  # exported, we must invoke find_package(XXX) here to reload the definition
+  # of their targets.  Without this, the dependency target names (e.g.
+  # 'gflags-shared') which will be present in the ceres target would not be
+  # defined, and so CMake will assume that they refer to a library name and
+  # fail to link correctly.
+
+  # Append the locations of glog when Ceres was built to the search path hints.
+  set(GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
+  if (GLOG_WAS_BUILT_WITH_CMAKE)
+    set(glog_DIR @glog_DIR@)
+    set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
+  else()
+    list(APPEND GLOG_INCLUDE_DIR_HINTS @GLOG_INCLUDE_DIR@)
+    get_filename_component(CERES_BUILD_GLOG_LIBRARY_DIR @GLOG_LIBRARY@ PATH)
+    list(APPEND GLOG_LIBRARY_DIR_HINTS ${CERES_BUILD_GLOG_LIBRARY_DIR})
+  endif()
+  # Search quietly s/t we control the timing of the error message if not found.
+  find_package(Glog QUIET)
+  if (GLOG_FOUND)
+    message(STATUS "Found required Ceres dependency: glog")
+  else()
+    ceres_report_not_found("Missing required Ceres "
+      "dependency: glog. Searched using GLOG_INCLUDE_DIR_HINTS: "
+      "${GLOG_INCLUDE_DIR_HINTS} and glog_DIR: ${glog_DIR}.")
+  endif()
+
+  # gflags is only a public dependency of Ceres via glog, thus is not required
+  # if Ceres was built with MINIGLOG.
+  if (CERES_USES_GFLAGS)
+    set(GFLAGS_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION@)
+    if (GFLAGS_WAS_BUILT_WITH_CMAKE)
+      set(gflags_DIR @gflags_DIR@)
+      set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
+    else()
+      list(APPEND GFLAGS_INCLUDE_DIR_HINTS @GFLAGS_INCLUDE_DIR@)
+      get_filename_component(CERES_BUILD_GFLAGS_LIBRARY_DIR @GFLAGS_LIBRARY@ PATH)
+      list(APPEND GFLAGS_LIBRARY_DIR_HINTS ${CERES_BUILD_GFLAGS_LIBRARY_DIR})
+    endif()
+    # Search quietly s/t we control the timing of the error message if not found.
+    find_package(Gflags QUIET)
+    if (GFLAGS_FOUND)
+      message(STATUS "Found required Ceres dependency: gflags")
+    else()
+      ceres_report_not_found("Missing required Ceres "
+        "dependency: gflags. Searched using GFLAGS_INCLUDE_DIR_HINTS: "
+        "${GFLAGS_INCLUDE_DIR_HINTS} and gflags_DIR: ${gflags_DIR}.")
+    endif()
+  endif()
+endif(CERES_USES_MINIGLOG)
+
+# Import exported Ceres targets, if they have not already been imported.
+if (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
+  include(${CERES_CURRENT_CONFIG_DIR}/CeresTargets.cmake)
+endif (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
+# Set the expected XX_LIBRARIES variable for FindPackage().
+set(CERES_LIBRARIES ceres)
+
+# Reset CMake module path to its state when this script was called.
+set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
+
+# Build the detected Ceres version string to correctly capture whether it
+# was installed, or exported.
+ceres_pretty_print_cmake_list(CERES_COMPILED_COMPONENTS_STRING
+  ${CERES_COMPILED_COMPONENTS})
+if (CERES_WAS_INSTALLED)
+  set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
+    "installed in: ${CURRENT_ROOT_INSTALL_DIR} with components: "
+    "${CERES_COMPILED_COMPONENTS_STRING}")
+else (CERES_WAS_INSTALLED)
+  set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
+    "exported from build directory: ${CERES_EXPORTED_BUILD_DIR} with "
+    "components: ${CERES_COMPILED_COMPONENTS_STRING}")
+endif()
+
+# If the user called this script through find_package() whilst specifying
+# particular Ceres components that should be found via:
+# find_package(Ceres COMPONENTS XXX YYY), check the requested components against
+# those with which Ceres was compiled.  In this case, we should only report
+# Ceres as found if all the requested components have been found.
+if (Ceres_FIND_COMPONENTS)
+  foreach (REQUESTED_COMPONENT ${Ceres_FIND_COMPONENTS})
+    list(FIND CERES_COMPILED_COMPONENTS ${REQUESTED_COMPONENT} HAVE_REQUESTED_COMPONENT)
+    # list(FIND ..) returns -1 if the element was not in the list, but CMake
+    # interprets if (VAR) to be true if VAR is any non-zero number, even
+    # negative ones, hence we have to explicitly check for >= 0.
+    if (HAVE_REQUESTED_COMPONENT EQUAL -1)
+      # Check for the presence of all requested components before reporting
+      # not found, such that we report all of the missing components rather
+      # than just the first.
+      list(APPEND MISSING_CERES_COMPONENTS ${REQUESTED_COMPONENT})
+    endif()
+  endforeach()
+  if (MISSING_CERES_COMPONENTS)
+    ceres_pretty_print_cmake_list(REQUESTED_CERES_COMPONENTS_STRING
+      ${Ceres_FIND_COMPONENTS})
+    ceres_pretty_print_cmake_list(MISSING_CERES_COMPONENTS_STRING
+      ${MISSING_CERES_COMPONENTS})
+    ceres_report_not_found("Missing requested Ceres components: "
+      "${MISSING_CERES_COMPONENTS_STRING} (components requested: "
+      "${REQUESTED_CERES_COMPONENTS_STRING}). Detected "
+      "${CERES_DETECTED_VERSION_STRING}.")
+  endif()
+endif()
+
+# As we use CERES_REPORT_NOT_FOUND() to abort, if we reach this point we have
+# found Ceres and all required dependencies.
+message(STATUS "Found " ${CERES_DETECTED_VERSION_STRING})
+
+# Set CERES_FOUND to be equivalent to Ceres_FOUND, which is set to
+# TRUE by FindPackage() if this file is found and run, and after which
+# Ceres_FOUND is not (explicitly, i.e. undefined does not count) set
+# to FALSE.
+set(CERES_FOUND TRUE)
diff --git a/cmake/CeresConfigVersion.cmake.in b/cmake/CeresConfigVersion.cmake.in
new file mode 100644
index 0000000..5ff36eb
--- /dev/null
+++ b/cmake/CeresConfigVersion.cmake.in
@@ -0,0 +1,50 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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: pablo.speciale@gmail.com (Pablo Speciale)
+#
+# FIND_PACKAGE() searches for a <package>Config.cmake file and an associated
+# <package>Version.cmake file, which it loads to check the version number.
+#
+# This file can be used with CONFIGURE_FILE() to generate such a file for a
+# project with very basic logic.
+#
+# It sets PACKAGE_VERSION_EXACT if the current version string and the requested
+# version string are exactly the same and it sets PACKAGE_VERSION_COMPATIBLE
+# if the current version is >= requested version.
+
+set(PACKAGE_VERSION @CERES_VERSION@)
+
+if ("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
+   set(PACKAGE_VERSION_COMPATIBLE FALSE)
+else ("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
+   set(PACKAGE_VERSION_COMPATIBLE TRUE)
+   if ("${PACKAGE_FIND_VERSION}" STREQUAL "${PACKAGE_VERSION}")
+      set(PACKAGE_VERSION_EXACT TRUE)
+   endif ("${PACKAGE_FIND_VERSION}" STREQUAL "${PACKAGE_VERSION}")
+endif ("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
diff --git a/cmake/CeresThreadingModels.cmake b/cmake/CeresThreadingModels.cmake
new file mode 100644
index 0000000..24e8aab
--- /dev/null
+++ b/cmake/CeresThreadingModels.cmake
@@ -0,0 +1,82 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2018 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+
+# Ordered by expected preference.
+set(CERES_THREADING_MODELS "CXX11_THREADS;OPENMP;NO_THREADS")
+
+function(find_available_ceres_threading_models CERES_THREADING_MODELS_AVAILABLE_VAR)
+  set(CERES_THREADING_MODELS_AVAILABLE ${CERES_THREADING_MODELS})
+  # Remove any threading models for which the dependencies are not available.
+  find_package(OpenMP QUIET)
+  if (NOT OPENMP_FOUND)
+    list(REMOVE_ITEM CERES_THREADING_MODELS_AVAILABLE "OPENMP")
+  endif()
+  if (NOT CERES_THREADING_MODELS_AVAILABLE)
+    # At least NO_THREADS should never be removed.  This check is purely
+    # protective against future threading model updates.
+    message(FATAL_ERROR "Ceres bug: Removed all threading models.")
+  endif()
+  set(${CERES_THREADING_MODELS_AVAILABLE_VAR}
+    ${CERES_THREADING_MODELS_AVAILABLE} PARENT_SCOPE)
+endfunction()
+
+macro(set_ceres_threading_model_to_cxx11_threads)
+  list(APPEND CERES_COMPILE_OPTIONS CERES_USE_CXX11_THREADS)
+endmacro()
+
+macro(set_ceres_threading_model_to_openmp)
+  find_package(OpenMP REQUIRED)
+  list(APPEND CERES_COMPILE_OPTIONS CERES_USE_OPENMP)
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
+endmacro()
+
+macro(set_ceres_threading_model_to_no_threads)
+  list(APPEND CERES_COMPILE_OPTIONS CERES_NO_THREADS)
+endmacro()
+
+macro(set_ceres_threading_model CERES_THREADING_MODEL_TO_SET)
+  if ("${CERES_THREADING_MODEL_TO_SET}" STREQUAL "CXX11_THREADS")
+    set_ceres_threading_model_to_cxx11_threads()
+  elseif ("${CERES_THREADING_MODEL_TO_SET}" STREQUAL "OPENMP")
+    set_ceres_threading_model_to_openmp()
+  elseif ("${CERES_THREADING_MODEL_TO_SET}" STREQUAL "NO_THREADS")
+    set_ceres_threading_model_to_no_threads()
+  else()
+    include(PrettyPrintCMakeList)
+    find_available_ceres_threading_models(_AVAILABLE_THREADING_MODELS)
+    pretty_print_cmake_list(
+      _AVAILABLE_THREADING_MODELS ${_AVAILABLE_THREADING_MODELS})
+    message(FATAL_ERROR "Unknown threading model specified: "
+      "'${CERES_THREADING_MODEL_TO_SET}'. Available threading models for "
+      "this platform are: ${_AVAILABLE_THREADING_MODELS}")
+  endif()
+  message("-- Using Ceres threading model: ${CERES_THREADING_MODEL_TO_SET}")
+endmacro()
diff --git a/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake b/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake
new file mode 100644
index 0000000..a05721c
--- /dev/null
+++ b/cmake/CheckIfUnderscorePrefixedBesselFunctionsExist.cmake
@@ -0,0 +1,54 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2017 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+
+# Microsoft deprecated the POSIX Bessel functions: j[0,1,n]() in favour
+# of _j[0,1,n](), it appears since at least MSVC 2005 [1].  This function
+# checks if the underscore prefixed versions of the Bessel functions are
+# defined, and sets ${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR} to
+# TRUE if they do.
+#
+# [1] https://msdn.microsoft.com/en-us/library/ms235384(v=vs.100).aspx
+function(check_if_underscore_prefixed_bessel_functions_exist
+    HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR)
+  include(CheckCXXSourceCompiles)
+  check_cxx_source_compiles(
+    "#include <math.h>
+     int main(int argc, char * argv[]) {
+       double result;
+       result = _j0(1.2345);
+       result = _j1(1.2345);
+       result = _jn(2, 1.2345);
+       return 0;
+     }"
+     HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS)
+   set(${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS_VAR}
+     ${HAVE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS}
+     PARENT_SCOPE)
+endfunction()
diff --git a/cmake/CreateCeresConfig.cmake b/cmake/CreateCeresConfig.cmake
new file mode 100644
index 0000000..89db68c
--- /dev/null
+++ b/cmake/CreateCeresConfig.cmake
@@ -0,0 +1,115 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+
+# This must take place outside of CONFIGURE_CERES_CONFIG() in order that
+# we can determine where *this* file is, and thus the relative path to
+# config.h.in.  Inside of CONFIGURE_CERES_CONFIG(), CMAKE_CURRENT_LIST_DIR
+# refers to the caller of CONFIGURE_CERES_CONFIG(), not this file.
+set(CERES_CONFIG_IN_FILE "${CMAKE_CURRENT_LIST_DIR}/config.h.in")
+
+# CreateCeresConfig.cmake - Create the config.h for Ceres.
+#
+# This function configures the Ceres config.h file based on the current
+# compile options and copies it into the specified location.  It should be
+# called before Ceres is built so that the correct config.h is used when
+# Ceres is compiled.
+#
+# INPUTS:
+#   CURRENT_CERES_COMPILE_OPTIONS: List of currently enabled Ceres compile
+#                                  options. These are compared against the
+#                                  full list of valid options, which are read
+#                                  from config.h.in.  Any options present
+#                                  which are not part of the valid set will
+#                                  invoke an error.  Any valid option present
+#                                  will be enabled in the resulting config.h,
+#                                  all other options will be disabled.
+#
+#   CERES_CONFIG_OUTPUT_DIRECTORY: Path to output directory in which to save
+#                                  the configured config.h.  Typically this
+#                                  will be <src>/include/ceres/internal.
+
+function(CREATE_CERES_CONFIG CURRENT_CERES_COMPILE_OPTIONS CERES_CONFIG_OUTPUT_DIRECTORY)
+  # Create the specified output directory if it does not exist.
+  if (NOT EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+    message(STATUS "Creating configured Ceres config.h output directory: "
+      "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+    file(MAKE_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+  endif()
+  if (EXISTS "${CERES_CONFIG_OUTPUT_DIRECTORY}" AND
+      NOT IS_DIRECTORY "${CERES_CONFIG_OUTPUT_DIRECTORY}")
+    message(FATAL_ERROR "Ceres Bug: Specified CERES_CONFIG_OUTPUT_DIRECTORY: "
+      "${CERES_CONFIG_OUTPUT_DIRECTORY} exists, but is not a directory.")
+  endif()
+
+  # Read all possible configurable compile options from config.h.in, this avoids
+  # us having to hard-code in this file what the valid options are.
+  file(READ ${CERES_CONFIG_IN_FILE} CERES_CONFIG_IN_CONTENTS)
+  string(REGEX MATCHALL "@[^@ $]+@"
+    ALL_CONFIGURABLE_CERES_OPTIONS "${CERES_CONFIG_IN_CONTENTS}")
+  # Removing @ symbols at beginning and end of each option.
+  string(REPLACE "@" ""
+    ALL_CONFIGURABLE_CERES_OPTIONS "${ALL_CONFIGURABLE_CERES_OPTIONS}")
+
+  # Ensure that there are no repetitions in the current compile options.
+  list(REMOVE_DUPLICATES CURRENT_CERES_COMPILE_OPTIONS)
+
+  foreach (CERES_OPTION ${ALL_CONFIGURABLE_CERES_OPTIONS})
+    # Try and find the option in the list of current compile options, if it
+    # is present, then the option is enabled, otherwise it is disabled.
+    list(FIND CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION} OPTION_ENABLED)
+
+    # list(FIND ..) returns -1 if the element was not in the list, but CMake
+    # interprets if (VAR) to be true if VAR is any non-zero number, even
+    # negative ones, hence we have to explicitly check for >= 0.
+    if (OPTION_ENABLED GREATER -1)
+      message(STATUS "Enabling ${CERES_OPTION} in Ceres config.h")
+      set(${CERES_OPTION} "#define ${CERES_OPTION}")
+
+      # Remove the item from the list of current options so that we can identify
+      # any options that were in CURRENT_CERES_COMPILE_OPTIONS, but not in
+      # ALL_CONFIGURABLE_CERES_OPTIONS (which is an error).
+      list(REMOVE_ITEM CURRENT_CERES_COMPILE_OPTIONS ${CERES_OPTION})
+    else()
+      set(${CERES_OPTION} "// #define ${CERES_OPTION}")
+    endif()
+  endforeach()
+
+  # CURRENT_CERES_COMPILE_OPTIONS should now be an empty list, any elements
+  # remaining were not present in ALL_CONFIGURABLE_CERES_OPTIONS read from
+  # config.h.in.
+  if (CURRENT_CERES_COMPILE_OPTIONS)
+    message(FATAL_ERROR "Ceres Bug: CURRENT_CERES_COMPILE_OPTIONS contained "
+      "the following options which were not present in config.h.in: "
+      "${CURRENT_CERES_COMPILE_OPTIONS}")
+  endif()
+
+  configure_file(${CERES_CONFIG_IN_FILE}
+    "${CERES_CONFIG_OUTPUT_DIRECTORY}/config.h" @ONLY)
+endfunction()
diff --git a/cmake/FindAccelerateSparse.cmake b/cmake/FindAccelerateSparse.cmake
new file mode 100644
index 0000000..f2f4340
--- /dev/null
+++ b/cmake/FindAccelerateSparse.cmake
@@ -0,0 +1,145 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2018 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindAccelerateSparse.cmake - Find the sparse solvers in Apple's Accelerate
+#                              framework, introduced in Xcode 9.0 (2017).
+#                              Note that this is distinct from the Accelerate
+#                              framework on its own, which existed in previous
+#                              versions but without the sparse solvers.
+#
+# This module defines the following variables which should be referenced
+# by the caller to use the library.
+#
+# AccelerateSparse_FOUND: TRUE iff an Accelerate framework including the sparse
+#                         solvers, and all dependencies, has been found.
+# AccelerateSparse_INCLUDE_DIRS: Include directories for Accelerate framework.
+# AccelerateSparse_LIBRARIES: Libraries for Accelerate framework and all
+#                             dependencies.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# AccelerateSparse_INCLUDE_DIR: Include directory for Accelerate framework, not
+#                               including the include directory of any
+#                               dependencies.
+# AccelerateSparse_LIBRARY: Accelerate framework, not including the libraries of
+#                           any dependencies.
+
+# Called if we failed to find the Accelerate framework with the sparse solvers.
+# Unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(accelerate_sparse_report_not_found REASON_MSG)
+  unset(AccelerateSparse_FOUND)
+  unset(AccelerateSparse_INCLUDE_DIRS)
+  unset(AccelerateSparse_LIBRARIES)
+  # Make results of search visible in the CMake GUI if Accelerate has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR AccelerateSparse_INCLUDE_DIR
+                         AccelerateSparse_LIBRARY)
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (AccelerateSparse_FIND_QUIETLY)
+    message(STATUS "Failed to find Accelerate framework with sparse solvers - "
+      ${REASON_MSG} ${ARGN})
+  elseif (AccelerateSparse_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find Accelerate framework with sparse solvers - "
+      ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find Accelerate framework with sparse solvers - "
+      ${REASON_MSG} ${ARGN})
+  endif()
+  return()
+endmacro()
+
+unset(AccelerateSparse_FOUND)
+
+find_path(AccelerateSparse_INCLUDE_DIR NAMES Accelerate.h)
+if (NOT AccelerateSparse_INCLUDE_DIR OR
+    NOT EXISTS ${AccelerateSparse_INCLUDE_DIR})
+  accelerate_sparse_report_not_found(
+    "Could not find Accelerate framework headers. Set "
+    "AccelerateSparse_INCLUDE_DIR to the directory containing Accelerate.h")
+endif()
+
+find_library(AccelerateSparse_LIBRARY NAMES Accelerate)
+if (NOT AccelerateSparse_LIBRARY OR
+    NOT EXISTS ${AccelerateSparse_LIBRARY})
+  accelerate_sparse_report_not_found(
+    "Could not find Accelerate framework. Set AccelerateSparse_LIBRARY "
+    "to the Accelerate.framework directory")
+endif()
+
+set(AccelerateSparse_FOUND TRUE)
+
+# Determine if the Accelerate framework detected includes the sparse solvers.
+include(CheckCXXSourceCompiles)
+set(CMAKE_REQUIRED_INCLUDES ${AccelerateSparse_INCLUDE_DIR})
+set(CMAKE_REQUIRED_LIBRARIES ${AccelerateSparse_LIBRARY})
+check_cxx_source_compiles(
+  "#include <Accelerate.h>
+   int main() {
+     SparseMatrix_Double A;
+     SparseFactor(SparseFactorizationCholesky, A);
+     return 0;
+   }"
+   ACCELERATE_FRAMEWORK_HAS_SPARSE_SOLVER)
+unset(CMAKE_REQUIRED_INCLUDES)
+unset(CMAKE_REQUIRED_LIBRARIES)
+if (NOT ACCELERATE_FRAMEWORK_HAS_SPARSE_SOLVER)
+  accelerate_sparse_report_not_found(
+    "Detected Accelerate framework: ${AccelerateSparse_LIBRARY} does not "
+    "include the sparse solvers.")
+endif()
+
+if (AccelerateSparse_FOUND)
+  set(AccelerateSparse_INCLUDE_DIRS ${AccelerateSparse_INCLUDE_DIR})
+  set(AccelerateSparse_LIBRARIES ${AccelerateSparse_LIBRARY})
+endif()
+
+# Handle REQUIRED / QUIET optional arguments and version.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(AccelerateSparse
+  REQUIRED_VARS AccelerateSparse_INCLUDE_DIRS AccelerateSparse_LIBRARIES)
+if (AccelerateSparse_FOUND)
+  mark_as_advanced(FORCE AccelerateSparse_INCLUDE_DIR
+                         AccelerateSparse_LIBRARY)
+endif()
diff --git a/cmake/FindCXSparse.cmake b/cmake/FindCXSparse.cmake
new file mode 100644
index 0000000..3f4b015
--- /dev/null
+++ b/cmake/FindCXSparse.cmake
@@ -0,0 +1,240 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindCXSparse.cmake - Find CXSparse libraries & dependencies.
+#
+# This module defines the following variables which should be referenced
+# by the caller to use the library.
+#
+# CXSPARSE_FOUND: TRUE iff CXSparse and all dependencies have been found.
+# CXSPARSE_INCLUDE_DIRS: Include directories for CXSparse.
+# CXSPARSE_LIBRARIES: Libraries for CXSparse and all dependencies.
+#
+# CXSPARSE_VERSION: Extracted from cs.h.
+# CXSPARSE_MAIN_VERSION: Equal to 3 if CXSPARSE_VERSION = 3.1.2
+# CXSPARSE_SUB_VERSION: Equal to 1 if CXSPARSE_VERSION = 3.1.2
+# CXSPARSE_SUBSUB_VERSION: Equal to 2 if CXSPARSE_VERSION = 3.1.2
+#
+# The following variables control the behaviour of this module:
+#
+# CXSPARSE_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                             search for CXSparse includes,
+#                             e.g: /timbuktu/include.
+# CXSPARSE_LIBRARY_DIR_HINTS: List of additional directories in which to
+#                             search for CXSparse libraries, e.g: /timbuktu/lib.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# CXSPARSE_INCLUDE_DIR: Include directory for CXSparse, not including the
+#                       include directory of any dependencies.
+# CXSPARSE_LIBRARY: CXSparse library, not including the libraries of any
+#                   dependencies.
+
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
+# FindCXSparse was invoked.
+macro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
+  if (MSVC)
+    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif (MSVC)
+endmacro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
+
+# Called if we failed to find CXSparse or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(CXSPARSE_REPORT_NOT_FOUND REASON_MSG)
+  unset(CXSPARSE_FOUND)
+  unset(CXSPARSE_INCLUDE_DIRS)
+  unset(CXSPARSE_LIBRARIES)
+  # Make results of search visible in the CMake GUI if CXSparse has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR CXSPARSE_INCLUDE_DIR
+                         CXSPARSE_LIBRARY)
+
+  cxsparse_reset_find_library_prefix()
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (CXSparse_FIND_QUIETLY)
+    message(STATUS "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
+  elseif (CXSparse_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
+  endif ()
+  return()
+endmacro(CXSPARSE_REPORT_NOT_FOUND)
+
+# Protect against any alternative find_package scripts for this library having
+# been called previously (in a client project) which set CXSPARSE_FOUND, but not
+# the other variables we require / set here which could cause the search logic
+# here to fail.
+unset(CXSPARSE_FOUND)
+
+# Handle possible presence of lib prefix for libraries on MSVC, see
+# also CXSPARSE_RESET_FIND_LIBRARY_PREFIX().
+if (MSVC)
+  # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+  # s/t we can set it back before returning.
+  set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+  # The empty string in this list is important, it represents the case when
+  # the libraries have no prefix (shared libraries / DLLs).
+  set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+endif (MSVC)
+
+# Search user-installed locations first, so that we prefer user installs
+# to system installs where both exist.
+#
+# TODO: Add standard Windows search locations for CXSparse.
+list(APPEND CXSPARSE_CHECK_INCLUDE_DIRS
+  /usr/local/include
+  /usr/local/homebrew/include # Mac OS X
+  /opt/local/var/macports/software # Mac OS X.
+  /opt/local/include
+  /usr/include)
+list(APPEND CXSPARSE_CHECK_LIBRARY_DIRS
+  /usr/local/lib
+  /usr/local/homebrew/lib # Mac OS X.
+  /opt/local/lib
+  /usr/lib)
+# Additional suffixes to try appending to each search path.
+list(APPEND CXSPARSE_CHECK_PATH_SUFFIXES
+  suitesparse) # Linux/Windows
+
+# Search supplied hint directories first if supplied.
+find_path(CXSPARSE_INCLUDE_DIR
+  NAMES cs.h
+  HINTS ${CXSPARSE_INCLUDE_DIR_HINTS}
+  PATHS ${CXSPARSE_CHECK_INCLUDE_DIRS}
+  PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
+if (NOT CXSPARSE_INCLUDE_DIR OR
+    NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
+  cxsparse_report_not_found(
+    "Could not find CXSparse include directory, set CXSPARSE_INCLUDE_DIR "
+    "to directory containing cs.h")
+endif (NOT CXSPARSE_INCLUDE_DIR OR
+       NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
+
+find_library(CXSPARSE_LIBRARY NAMES cxsparse
+  HINTS ${CXSPARSE_LIBRARY_DIR_HINTS}
+  PATHS ${CXSPARSE_CHECK_LIBRARY_DIRS}
+  PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
+if (NOT CXSPARSE_LIBRARY OR
+    NOT EXISTS ${CXSPARSE_LIBRARY})
+  cxsparse_report_not_found(
+    "Could not find CXSparse library, set CXSPARSE_LIBRARY "
+    "to full path to libcxsparse.")
+endif (NOT CXSPARSE_LIBRARY OR
+       NOT EXISTS ${CXSPARSE_LIBRARY})
+
+# Mark internally as found, then verify. CXSPARSE_REPORT_NOT_FOUND() unsets
+# if called.
+set(CXSPARSE_FOUND TRUE)
+
+# Extract CXSparse version from cs.h
+if (CXSPARSE_INCLUDE_DIR)
+  set(CXSPARSE_VERSION_FILE ${CXSPARSE_INCLUDE_DIR}/cs.h)
+  if (NOT EXISTS ${CXSPARSE_VERSION_FILE})
+    cxsparse_report_not_found(
+      "Could not find file: ${CXSPARSE_VERSION_FILE} "
+      "containing version information in CXSparse install located at: "
+      "${CXSPARSE_INCLUDE_DIR}.")
+  else (NOT EXISTS ${CXSPARSE_VERSION_FILE})
+    file(READ ${CXSPARSE_INCLUDE_DIR}/cs.h CXSPARSE_VERSION_FILE_CONTENTS)
+
+    string(REGEX MATCH "#define CS_VER [0-9]+"
+      CXSPARSE_MAIN_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define CS_VER ([0-9]+)" "\\1"
+      CXSPARSE_MAIN_VERSION "${CXSPARSE_MAIN_VERSION}")
+
+    string(REGEX MATCH "#define CS_SUBVER [0-9]+"
+      CXSPARSE_SUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define CS_SUBVER ([0-9]+)" "\\1"
+      CXSPARSE_SUB_VERSION "${CXSPARSE_SUB_VERSION}")
+
+    string(REGEX MATCH "#define CS_SUBSUB [0-9]+"
+      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define CS_SUBSUB ([0-9]+)" "\\1"
+      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_SUBSUB_VERSION}")
+
+    # This is on a single line s/t CMake does not interpret it as a list of
+    # elements and insert ';' separators which would result in 3.;1.;2 nonsense.
+    set(CXSPARSE_VERSION "${CXSPARSE_MAIN_VERSION}.${CXSPARSE_SUB_VERSION}.${CXSPARSE_SUBSUB_VERSION}")
+  endif (NOT EXISTS ${CXSPARSE_VERSION_FILE})
+endif (CXSPARSE_INCLUDE_DIR)
+
+# Catch the case when the caller has set CXSPARSE_LIBRARY in the cache / GUI and
+# thus FIND_LIBRARY was not called, but specified library is invalid, otherwise
+# we would report CXSparse as found.
+# TODO: This regex for CXSparse library is pretty primitive, we use lowercase
+#       for comparison to handle Windows using CamelCase library names, could
+#       this check be better?
+string(TOLOWER "${CXSPARSE_LIBRARY}" LOWERCASE_CXSPARSE_LIBRARY)
+if (CXSPARSE_LIBRARY AND
+    EXISTS ${CXSPARSE_LIBRARY} AND
+    NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
+  cxsparse_report_not_found(
+    "Caller defined CXSPARSE_LIBRARY: "
+    "${CXSPARSE_LIBRARY} does not match CXSparse.")
+endif (CXSPARSE_LIBRARY AND
+       EXISTS ${CXSPARSE_LIBRARY} AND
+       NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
+
+# Set standard CMake FindPackage variables if found.
+if (CXSPARSE_FOUND)
+  set(CXSPARSE_INCLUDE_DIRS ${CXSPARSE_INCLUDE_DIR})
+  set(CXSPARSE_LIBRARIES ${CXSPARSE_LIBRARY})
+endif (CXSPARSE_FOUND)
+
+cxsparse_reset_find_library_prefix()
+
+# Handle REQUIRED / QUIET optional arguments and version.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(CXSparse
+  REQUIRED_VARS CXSPARSE_INCLUDE_DIRS CXSPARSE_LIBRARIES
+  VERSION_VAR CXSPARSE_VERSION)
+
+# Only mark internal variables as advanced if we found CXSparse, otherwise
+# leave them visible in the standard GUI for the user to set manually.
+if (CXSPARSE_FOUND)
+  mark_as_advanced(FORCE CXSPARSE_INCLUDE_DIR
+                         CXSPARSE_LIBRARY)
+endif (CXSPARSE_FOUND)
diff --git a/cmake/FindEigen.cmake b/cmake/FindEigen.cmake
new file mode 100644
index 0000000..f6d2664
--- /dev/null
+++ b/cmake/FindEigen.cmake
@@ -0,0 +1,263 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindEigen.cmake - Find Eigen library, version >= 3.
+#
+# This module defines the following variables:
+#
+# EIGEN_FOUND: TRUE iff Eigen is found.
+# EIGEN_INCLUDE_DIRS: Include directories for Eigen.
+# EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
+# EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
+# EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
+# EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
+# FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION: True iff the version of Eigen
+#                                            found was built & installed /
+#                                            exported as a CMake package.
+#
+# The following variables control the behaviour of this module:
+#
+# EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
+#                           then prefer using an exported CMake configuration
+#                           generated by Eigen over searching for the
+#                           Eigen components manually.  Otherwise (FALSE)
+#                           ignore any exported Eigen CMake configurations and
+#                           always perform a manual search for the components.
+#                           Default: TRUE iff user does not define this variable
+#                           before we are called, and does NOT specify
+#                           EIGEN_INCLUDE_DIR_HINTS, otherwise FALSE.
+# EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                          search for eigen includes, e.g: /timbuktu/eigen3.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
+#                    include directory of any dependencies.
+
+# Called if we failed to find Eigen or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(EIGEN_REPORT_NOT_FOUND REASON_MSG)
+  unset(EIGEN_FOUND)
+  unset(EIGEN_INCLUDE_DIRS)
+  unset(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION)
+  # Make results of search visible in the CMake GUI if Eigen has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR EIGEN_INCLUDE_DIR)
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (Eigen_FIND_QUIETLY)
+    message(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
+  elseif (Eigen_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find Eigen - " ${REASON_MSG} ${ARGN})
+  endif ()
+  return()
+endmacro(EIGEN_REPORT_NOT_FOUND)
+
+# Protect against any alternative find_package scripts for this library having
+# been called previously (in a client project) which set EIGEN_FOUND, but not
+# the other variables we require / set here which could cause the search logic
+# here to fail.
+unset(EIGEN_FOUND)
+
+# -----------------------------------------------------------------
+# By default, if the user has expressed no preference for using an exported
+# Eigen CMake configuration over performing a search for the installed
+# components, and has not specified any hints for the search locations, then
+# prefer an exported configuration if available.
+if (NOT DEFINED EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION
+    AND NOT EIGEN_INCLUDE_DIR_HINTS)
+  message(STATUS "No preference for use of exported Eigen CMake configuration "
+    "set, and no hints for include directory provided. "
+    "Defaulting to preferring an installed/exported Eigen CMake configuration "
+    "if available.")
+  set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
+endif()
+
+if (EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION)
+  # Try to find an exported CMake configuration for Eigen.
+  #
+  # We search twice, s/t we can invert the ordering of precedence used by
+  # find_package() for exported package build directories, and installed
+  # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
+  # respectively in [1].
+  #
+  # By default, exported build directories are (in theory) detected first, and
+  # this is usually the case on Windows.  However, on OS X & Linux, the install
+  # path (/usr/local) is typically present in the PATH environment variable
+  # which is checked in item 4) in [1] (i.e. before both of the above, unless
+  # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
+  # packages are usually detected in preference to exported package build
+  # directories.
+  #
+  # To ensure a more consistent response across all OSs, and as users usually
+  # want to prefer an installed version of a package over a locally built one
+  # where both exist (esp. as the exported build directory might be removed
+  # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
+  # means any build directories exported by the user are ignored, and thus
+  # installed directories are preferred.  If this fails to find the package
+  # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
+  # exported build directories will now be detected.
+  #
+  # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
+  # is item 5) in [1]), to not preferentially use projects that were built
+  # recently with the CMake GUI to ensure that we always prefer an installed
+  # version if available.
+  #
+  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
+  find_package(Eigen3 QUIET
+                      NO_MODULE
+                      NO_CMAKE_PACKAGE_REGISTRY
+                      NO_CMAKE_BUILDS_PATH)
+  if (EIGEN3_FOUND)
+    message(STATUS "Found installed version of Eigen: ${Eigen3_DIR}")
+  else()
+    # Failed to find an installed version of Eigen, repeat search allowing
+    # exported build directories.
+    message(STATUS "Failed to find installed Eigen CMake configuration, "
+      "searching for Eigen build directories exported with CMake.")
+    # Again pass NO_CMAKE_BUILDS_PATH, as we know that Eigen is exported and
+    # do not want to treat projects built with the CMake GUI preferentially.
+    find_package(Eigen3 QUIET
+                        NO_MODULE
+                        NO_CMAKE_BUILDS_PATH)
+    if (EIGEN3_FOUND)
+      message(STATUS "Found exported Eigen build directory: ${Eigen3_DIR}")
+    endif()
+  endif()
+  if (EIGEN3_FOUND)
+    set(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION TRUE)
+    set(EIGEN_FOUND ${EIGEN3_FOUND})
+    set(EIGEN_INCLUDE_DIR "${EIGEN3_INCLUDE_DIR}" CACHE STRING
+      "Eigen include directory" FORCE)
+  else()
+    message(STATUS "Failed to find an installed/exported CMake configuration "
+      "for Eigen, will perform search for installed Eigen components.")
+  endif()
+endif()
+
+if (NOT EIGEN_FOUND)
+  # Search user-installed locations first, so that we prefer user installs
+  # to system installs where both exist.
+  list(APPEND EIGEN_CHECK_INCLUDE_DIRS
+    /usr/local/include
+    /usr/local/homebrew/include # Mac OS X
+    /opt/local/var/macports/software # Mac OS X.
+    /opt/local/include
+    /usr/include)
+  # Additional suffixes to try appending to each search path.
+  list(APPEND EIGEN_CHECK_PATH_SUFFIXES
+    eigen3 # Default root directory for Eigen.
+    Eigen/include/eigen3 # Windows (for C:/Program Files prefix) < 3.3
+    Eigen3/include/eigen3 ) # Windows (for C:/Program Files prefix) >= 3.3
+
+  # Search supplied hint directories first if supplied.
+  find_path(EIGEN_INCLUDE_DIR
+    NAMES Eigen/Core
+    HINTS ${EIGEN_INCLUDE_DIR_HINTS}
+    PATHS ${EIGEN_CHECK_INCLUDE_DIRS}
+    PATH_SUFFIXES ${EIGEN_CHECK_PATH_SUFFIXES})
+
+  if (NOT EIGEN_INCLUDE_DIR OR
+      NOT EXISTS ${EIGEN_INCLUDE_DIR})
+    eigen_report_not_found(
+      "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
+      "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
+  endif (NOT EIGEN_INCLUDE_DIR OR
+    NOT EXISTS ${EIGEN_INCLUDE_DIR})
+
+  # Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
+  # if called.
+  set(EIGEN_FOUND TRUE)
+endif()
+
+# Extract Eigen version from Eigen/src/Core/util/Macros.h
+if (EIGEN_INCLUDE_DIR)
+  set(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
+  if (NOT EXISTS ${EIGEN_VERSION_FILE})
+    eigen_report_not_found(
+      "Could not find file: ${EIGEN_VERSION_FILE} "
+      "containing version information in Eigen install located at: "
+      "${EIGEN_INCLUDE_DIR}.")
+  else (NOT EXISTS ${EIGEN_VERSION_FILE})
+    file(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
+
+    string(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
+      EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
+      EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
+
+    string(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
+      EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
+      EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
+
+    string(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
+      EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
+    string(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
+      EIGEN_MINOR_VERSION "${EIGEN_MINOR_VERSION}")
+
+    # This is on a single line s/t CMake does not interpret it as a list of
+    # elements and insert ';' separators which would result in 3.;2.;0 nonsense.
+    set(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
+  endif (NOT EXISTS ${EIGEN_VERSION_FILE})
+endif (EIGEN_INCLUDE_DIR)
+
+# Set standard CMake FindPackage variables if found.
+if (EIGEN_FOUND)
+  set(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
+endif (EIGEN_FOUND)
+
+# Handle REQUIRED / QUIET optional arguments and version.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Eigen
+  REQUIRED_VARS EIGEN_INCLUDE_DIRS
+  VERSION_VAR EIGEN_VERSION)
+
+# Only mark internal variables as advanced if we found Eigen, otherwise
+# leave it visible in the standard GUI for the user to set manually.
+if (EIGEN_FOUND)
+  mark_as_advanced(FORCE EIGEN_INCLUDE_DIR
+    Eigen3_DIR) # Autogenerated by find_package(Eigen3)
+endif (EIGEN_FOUND)
diff --git a/cmake/FindGflags.cmake b/cmake/FindGflags.cmake
new file mode 100644
index 0000000..32c04ea
--- /dev/null
+++ b/cmake/FindGflags.cmake
@@ -0,0 +1,591 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindGflags.cmake - Find Google gflags logging library.
+#
+# This module will attempt to find gflags, either via an exported CMake
+# configuration (generated by gflags >= 2.1 which are built with CMake), or
+# by performing a standard search for all gflags components.  The order of
+# precedence for these two methods of finding gflags is controlled by:
+# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION.
+#
+# This module defines the following variables:
+#
+# GFLAGS_FOUND: TRUE iff gflags is found.
+# GFLAGS_INCLUDE_DIRS: Include directories for gflags.
+# GFLAGS_LIBRARIES: Libraries required to link gflags.
+# GFLAGS_NAMESPACE: The namespace in which gflags is defined.  In versions of
+#                   gflags < 2.1, this was google, for versions >= 2.1 it is
+#                   by default gflags, although can be configured when building
+#                   gflags to be something else (i.e. google for legacy
+#                   compatibility).
+# FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION: True iff the version of gflags
+#                                             found was built & installed /
+#                                             exported as a CMake package.
+#
+# The following variables control the behaviour of this module when an exported
+# gflags CMake configuration is not found.
+#
+# GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
+#                           then prefer using an exported CMake configuration
+#                           generated by gflags >= 2.1 over searching for the
+#                           gflags components manually.  Otherwise (FALSE)
+#                           ignore any exported gflags CMake configurations and
+#                           always perform a manual search for the components.
+#                           Default: TRUE iff user does not define this variable
+#                           before we are called, and does NOT specify either
+#                           GFLAGS_INCLUDE_DIR_HINTS or GFLAGS_LIBRARY_DIR_HINTS
+#                           otherwise FALSE.
+# GFLAGS_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                           search for gflags includes, e.g: /timbuktu/include.
+# GFLAGS_LIBRARY_DIR_HINTS: List of additional directories in which to
+#                           search for gflags libraries, e.g: /timbuktu/lib.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# GFLAGS_INCLUDE_DIR: Include directory for gflags, not including the
+#                     include directory of any dependencies.
+# GFLAGS_LIBRARY: gflags library, not including the libraries of any
+#                 dependencies.
+
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when FindGflags was
+# invoked, necessary for MSVC.
+macro(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
+  if (MSVC AND CALLERS_CMAKE_FIND_LIBRARY_PREFIXES)
+    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif()
+endmacro(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
+
+# Called if we failed to find gflags or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(GFLAGS_REPORT_NOT_FOUND REASON_MSG)
+  unset(GFLAGS_FOUND)
+  unset(GFLAGS_INCLUDE_DIRS)
+  unset(GFLAGS_LIBRARIES)
+  # Do not use unset, as we want to keep GFLAGS_NAMESPACE in the cache,
+  # but simply clear its value.
+  set(GFLAGS_NAMESPACE "" CACHE STRING
+    "gflags namespace (google or gflags)" FORCE)
+
+  # Make results of search visible in the CMake GUI if gflags has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR GFLAGS_INCLUDE_DIR
+                         GFLAGS_LIBRARY
+                         GFLAGS_NAMESPACE)
+
+  gflags_reset_find_library_prefix()
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (Gflags_FIND_QUIETLY)
+    message(STATUS "Failed to find gflags - " ${REASON_MSG} ${ARGN})
+  elseif (Gflags_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find gflags - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find gflags - " ${REASON_MSG} ${ARGN})
+  endif ()
+  return()
+endmacro(GFLAGS_REPORT_NOT_FOUND)
+
+# Verify that all variable names passed as arguments are defined (can be empty
+# but must be defined) or raise a fatal error.
+macro(GFLAGS_CHECK_VARS_DEFINED)
+  foreach(CHECK_VAR ${ARGN})
+    if (NOT DEFINED ${CHECK_VAR})
+      message(FATAL_ERROR "Ceres Bug: ${CHECK_VAR} is not defined.")
+    endif()
+  endforeach()
+endmacro(GFLAGS_CHECK_VARS_DEFINED)
+
+# Use check_cxx_source_compiles() to compile trivial test programs to determine
+# the gflags namespace.  This works on all OSs except Windows.  If using Visual
+# Studio, it fails because msbuild forces check_cxx_source_compiles() to use
+# CMAKE_BUILD_TYPE=Debug for the test project, which usually breaks detection
+# because MSVC requires that the test project use the same build type as gflags,
+# which would normally be built in Release.
+#
+# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
+#          which is blank (empty string, will test FALSE is CMake conditionals)
+#          if detection failed.
+function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
+  # Verify that all required variables are defined.
+  gflags_check_vars_defined(
+    GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)
+  # Ensure that GFLAGS_NAMESPACE is always unset on completion unless
+  # we explicitly set if after having the correct namespace.
+  set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
+
+  include(CheckCXXSourceCompiles)
+  # Setup include path & link library for gflags for CHECK_CXX_SOURCE_COMPILES.
+  set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
+  set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
+  # First try the (older) google namespace.  Note that the output variable
+  # MUST be unique to the build type as otherwise the test is not repeated as
+  # it is assumed to have already been performed.
+  check_cxx_source_compiles(
+    "#include <gflags/gflags.h>
+     int main(int argc, char * argv[]) {
+       google::ParseCommandLineFlags(&argc, &argv, true);
+       return 0;
+     }"
+     GFLAGS_IN_GOOGLE_NAMESPACE)
+  if (GFLAGS_IN_GOOGLE_NAMESPACE)
+    set(GFLAGS_NAMESPACE google PARENT_SCOPE)
+    return()
+  endif()
+
+  # Try (newer) gflags namespace instead.  Note that the output variable
+  # MUST be unique to the build type as otherwise the test is not repeated as
+  # it is assumed to have already been performed.
+  set(CMAKE_REQUIRED_INCLUDES ${GFLAGS_INCLUDE_DIR})
+  set(CMAKE_REQUIRED_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
+  check_cxx_source_compiles(
+    "#include <gflags/gflags.h>
+     int main(int argc, char * argv[]) {
+        gflags::ParseCommandLineFlags(&argc, &argv, true);
+        return 0;
+     }"
+     GFLAGS_IN_GFLAGS_NAMESPACE)
+  if (GFLAGS_IN_GFLAGS_NAMESPACE)
+    set(GFLAGS_NAMESPACE gflags PARENT_SCOPE)
+    return()
+  endif (GFLAGS_IN_GFLAGS_NAMESPACE)
+endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE)
+
+# Use regex on the gflags headers to attempt to determine the gflags namespace.
+# Checks both gflags.h (contained namespace on versions < 2.1.2) and
+# gflags_declare.h, which contains the namespace on versions >= 2.1.2.
+# In general, this method should only be used when
+# GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_TRY_COMPILE() cannot be used, or has
+# failed.
+#
+# Defines: GFLAGS_NAMESPACE in the caller's scope with the detected namespace,
+#          which is blank (empty string, will test FALSE is CMake conditionals)
+#          if detection failed.
+function(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
+  # Verify that all required variables are defined.
+  gflags_check_vars_defined(GFLAGS_INCLUDE_DIR)
+  # Ensure that GFLAGS_NAMESPACE is always undefined on completion unless
+  # we explicitly set if after having the correct namespace.
+  set(GFLAGS_NAMESPACE "" PARENT_SCOPE)
+
+  # Scan gflags.h to identify what namespace gflags was built with.  On
+  # versions of gflags < 2.1.2, gflags.h was configured with the namespace
+  # directly, on >= 2.1.2, gflags.h uses the GFLAGS_NAMESPACE #define which
+  # is defined in gflags_declare.h, we try each location in turn.
+  set(GFLAGS_HEADER_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
+  if (NOT EXISTS ${GFLAGS_HEADER_FILE})
+    gflags_report_not_found(
+      "Could not find file: ${GFLAGS_HEADER_FILE} "
+      "containing namespace information in gflags install located at: "
+      "${GFLAGS_INCLUDE_DIR}.")
+  endif()
+  file(READ ${GFLAGS_HEADER_FILE} GFLAGS_HEADER_FILE_CONTENTS)
+
+  string(REGEX MATCH "namespace [A-Za-z]+"
+    GFLAGS_NAMESPACE "${GFLAGS_HEADER_FILE_CONTENTS}")
+  string(REGEX REPLACE "namespace ([A-Za-z]+)" "\\1"
+    GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
+
+  if (NOT GFLAGS_NAMESPACE)
+    gflags_report_not_found(
+      "Failed to extract gflags namespace from header file: "
+      "${GFLAGS_HEADER_FILE}.")
+  endif (NOT GFLAGS_NAMESPACE)
+
+  if (GFLAGS_NAMESPACE STREQUAL "google" OR
+      GFLAGS_NAMESPACE STREQUAL "gflags")
+    # Found valid gflags namespace from gflags.h.
+    set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
+    return()
+  endif()
+
+  # Failed to find gflags namespace from gflags.h, gflags is likely a new
+  # version, check gflags_declare.h, which in newer versions (>= 2.1.2) contains
+  # the GFLAGS_NAMESPACE #define, which is then referenced in gflags.h.
+  set(GFLAGS_DECLARE_FILE ${GFLAGS_INCLUDE_DIR}/gflags/gflags_declare.h)
+  if (NOT EXISTS ${GFLAGS_DECLARE_FILE})
+    gflags_report_not_found(
+      "Could not find file: ${GFLAGS_DECLARE_FILE} "
+      "containing namespace information in gflags install located at: "
+      "${GFLAGS_INCLUDE_DIR}.")
+  endif()
+  file(READ ${GFLAGS_DECLARE_FILE} GFLAGS_DECLARE_FILE_CONTENTS)
+
+  string(REGEX MATCH "#define GFLAGS_NAMESPACE [A-Za-z]+"
+    GFLAGS_NAMESPACE "${GFLAGS_DECLARE_FILE_CONTENTS}")
+  string(REGEX REPLACE "#define GFLAGS_NAMESPACE ([A-Za-z]+)" "\\1"
+    GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}")
+
+  if (NOT GFLAGS_NAMESPACE)
+    gflags_report_not_found(
+      "Failed to extract gflags namespace from declare file: "
+      "${GFLAGS_DECLARE_FILE}.")
+  endif (NOT GFLAGS_NAMESPACE)
+
+  if (GFLAGS_NAMESPACE STREQUAL "google" OR
+      GFLAGS_NAMESPACE STREQUAL "gflags")
+    # Found valid gflags namespace from gflags.h.
+    set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" PARENT_SCOPE)
+    return()
+  endif()
+endfunction(GFLAGS_CHECK_GFLAGS_NAMESPACE_USING_REGEX)
+
+# Protect against any alternative find_package scripts for this library having
+# been called previously (in a client project) which set GFLAGS_FOUND, but not
+# the other variables we require / set here which could cause the search logic
+# here to fail.
+unset(GFLAGS_FOUND)
+
+# -----------------------------------------------------------------
+# By default, if the user has expressed no preference for using an exported
+# gflags CMake configuration over performing a search for the installed
+# components, and has not specified any hints for the search locations, then
+# prefer a gflags exported configuration if available.
+if (NOT DEFINED GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION
+    AND NOT GFLAGS_INCLUDE_DIR_HINTS
+    AND NOT GFLAGS_LIBRARY_DIR_HINTS)
+  message(STATUS "No preference for use of exported gflags CMake configuration "
+    "set, and no hints for include/library directories provided. "
+    "Defaulting to preferring an installed/exported gflags CMake configuration "
+    "if available.")
+  set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
+endif()
+
+if (GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
+  # Try to find an exported CMake configuration for gflags, as generated by
+  # gflags versions >= 2.1.
+  #
+  # We search twice, s/t we can invert the ordering of precedence used by
+  # find_package() for exported package build directories, and installed
+  # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
+  # respectively in [1].
+  #
+  # By default, exported build directories are (in theory) detected first, and
+  # this is usually the case on Windows.  However, on OS X & Linux, the install
+  # path (/usr/local) is typically present in the PATH environment variable
+  # which is checked in item 4) in [1] (i.e. before both of the above, unless
+  # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
+  # packages are usually detected in preference to exported package build
+  # directories.
+  #
+  # To ensure a more consistent response across all OSs, and as users usually
+  # want to prefer an installed version of a package over a locally built one
+  # where both exist (esp. as the exported build directory might be removed
+  # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
+  # means any build directories exported by the user are ignored, and thus
+  # installed directories are preferred.  If this fails to find the package
+  # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
+  # exported build directories will now be detected.
+  #
+  # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
+  # is item 5) in [1]), to not preferentially use projects that were built
+  # recently with the CMake GUI to ensure that we always prefer an installed
+  # version if available.
+  #
+  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
+  find_package(gflags QUIET
+                      NO_MODULE
+                      NO_CMAKE_PACKAGE_REGISTRY
+                      NO_CMAKE_BUILDS_PATH)
+  if (gflags_FOUND)
+    message(STATUS "Found installed version of gflags: ${gflags_DIR}")
+  else(gflags_FOUND)
+    # Failed to find an installed version of gflags, repeat search allowing
+    # exported build directories.
+    message(STATUS "Failed to find installed gflags CMake configuration, "
+      "searching for gflags build directories exported with CMake.")
+    # Again pass NO_CMAKE_BUILDS_PATH, as we know that gflags is exported and
+    # do not want to treat projects built with the CMake GUI preferentially.
+    find_package(gflags QUIET
+                        NO_MODULE
+                        NO_CMAKE_BUILDS_PATH)
+    if (gflags_FOUND)
+      message(STATUS "Found exported gflags build directory: ${gflags_DIR}")
+    endif(gflags_FOUND)
+  endif(gflags_FOUND)
+
+  set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION ${gflags_FOUND})
+
+  # gflags v2.1 - 2.1.2 shipped with a bug in their gflags-config.cmake [1]
+  # whereby gflags_LIBRARIES = "gflags", but there was no imported target
+  # called "gflags", they were called: gflags[_nothreads]-[static/shared].
+  # As this causes linker errors when gflags is not installed in a location
+  # on the current library paths, detect if this problem is present and
+  # fix it.
+  #
+  # [1] https://github.com/gflags/gflags/issues/110
+  if (gflags_FOUND)
+    # NOTE: This is not written as additional conditions in the outer
+    #       if (gflags_FOUND) as the NOT TARGET "${gflags_LIBRARIES}"
+    #       condition causes problems if gflags is not found.
+    if (${gflags_VERSION} VERSION_LESS 2.1.3 AND
+        NOT TARGET "${gflags_LIBRARIES}")
+      message(STATUS "Detected broken gflags install in: ${gflags_DIR}, "
+        "version: ${gflags_VERSION} <= 2.1.2 which defines gflags_LIBRARIES = "
+        "${gflags_LIBRARIES} which is not an imported CMake target, see: "
+        "https://github.com/gflags/gflags/issues/110.  Attempting to fix by "
+        "detecting correct gflags target.")
+      # Ordering here expresses preference for detection, specifically we do not
+      # want to use the _nothreads variants if the full library is available.
+      list(APPEND CHECK_GFLAGS_IMPORTED_TARGET_NAMES
+        gflags-shared gflags-static
+        gflags_nothreads-shared gflags_nothreads-static)
+      foreach(CHECK_GFLAGS_TARGET ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES})
+        if (TARGET ${CHECK_GFLAGS_TARGET})
+          message(STATUS "Found valid gflags target: ${CHECK_GFLAGS_TARGET}, "
+            "updating gflags_LIBRARIES.")
+          set(gflags_LIBRARIES ${CHECK_GFLAGS_TARGET})
+          break()
+        endif()
+      endforeach()
+      if (NOT TARGET ${gflags_LIBRARIES})
+        message(STATUS "Failed to fix detected broken gflags install in: "
+          "${gflags_DIR}, version: ${gflags_VERSION} <= 2.1.2, none of the "
+          "imported targets for gflags: ${CHECK_GFLAGS_IMPORTED_TARGET_NAMES} "
+          "are defined.  Will continue with a manual search for gflags "
+          "components.  We recommend you build/install a version of gflags > "
+          "2.1.2 (or master).")
+        set(FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION FALSE)
+      endif()
+    endif()
+  endif()
+
+  if (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+    message(STATUS "Detected gflags version: ${gflags_VERSION}")
+    set(GFLAGS_FOUND ${gflags_FOUND})
+    set(GFLAGS_INCLUDE_DIR ${gflags_INCLUDE_DIR})
+    set(GFLAGS_LIBRARY ${gflags_LIBRARIES})
+
+    # gflags does not export the namespace in their CMake configuration, so
+    # use our function to determine what it should be, as it can be either
+    # gflags or google dependent upon version & configuration.
+    #
+    # NOTE: We use the regex method to determine the namespace here, as
+    #       check_cxx_source_compiles() will not use imported targets, which
+    #       is what gflags will be in this case.
+    gflags_check_gflags_namespace_using_regex()
+
+    if (NOT GFLAGS_NAMESPACE)
+      gflags_report_not_found(
+        "Failed to determine gflags namespace using regex for gflags "
+        "version: ${gflags_VERSION} exported here: ${gflags_DIR} using CMake.")
+    endif (NOT GFLAGS_NAMESPACE)
+  else (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+    message(STATUS "Failed to find an installed/exported CMake configuration "
+      "for gflags, will perform search for installed gflags components.")
+  endif (FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION)
+endif(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION)
+
+if (NOT GFLAGS_FOUND)
+  # Either failed to find an exported gflags CMake configuration, or user
+  # told us not to use one.  Perform a manual search for all gflags components.
+
+  # Handle possible presence of lib prefix for libraries on MSVC, see
+  # also GFLAGS_RESET_FIND_LIBRARY_PREFIX().
+  if (MSVC)
+    # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+    # s/t we can set it back before returning.
+    set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+    # The empty string in this list is important, it represents the case when
+    # the libraries have no prefix (shared libraries / DLLs).
+    set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif (MSVC)
+
+  # Search user-installed locations first, so that we prefer user installs
+  # to system installs where both exist.
+  list(APPEND GFLAGS_CHECK_INCLUDE_DIRS
+    /usr/local/include
+    /usr/local/homebrew/include # Mac OS X
+    /opt/local/var/macports/software # Mac OS X.
+    /opt/local/include
+    /usr/include)
+  list(APPEND GFLAGS_CHECK_PATH_SUFFIXES
+    gflags/include # Windows (for C:/Program Files prefix).
+    gflags/Include ) # Windows (for C:/Program Files prefix).
+
+  list(APPEND GFLAGS_CHECK_LIBRARY_DIRS
+    /usr/local/lib
+    /usr/local/homebrew/lib # Mac OS X.
+    /opt/local/lib
+    /usr/lib)
+  list(APPEND GFLAGS_CHECK_LIBRARY_SUFFIXES
+    gflags/lib # Windows (for C:/Program Files prefix).
+    gflags/Lib ) # Windows (for C:/Program Files prefix).
+
+  # Search supplied hint directories first if supplied.
+  find_path(GFLAGS_INCLUDE_DIR
+    NAMES gflags/gflags.h
+    HINTS ${GFLAGS_INCLUDE_DIR_HINTS}
+    PATHS ${GFLAGS_CHECK_INCLUDE_DIRS}
+    PATH_SUFFIXES ${GFLAGS_CHECK_PATH_SUFFIXES})
+  if (NOT GFLAGS_INCLUDE_DIR OR
+      NOT EXISTS ${GFLAGS_INCLUDE_DIR})
+    gflags_report_not_found(
+      "Could not find gflags include directory, set GFLAGS_INCLUDE_DIR "
+      "to directory containing gflags/gflags.h")
+  endif (NOT GFLAGS_INCLUDE_DIR OR
+    NOT EXISTS ${GFLAGS_INCLUDE_DIR})
+
+  find_library(GFLAGS_LIBRARY NAMES gflags
+    HINTS ${GFLAGS_LIBRARY_DIR_HINTS}
+    PATHS ${GFLAGS_CHECK_LIBRARY_DIRS}
+    PATH_SUFFIXES ${GFLAGS_CHECK_LIBRARY_SUFFIXES})
+  if (NOT GFLAGS_LIBRARY OR
+      NOT EXISTS ${GFLAGS_LIBRARY})
+    gflags_report_not_found(
+      "Could not find gflags library, set GFLAGS_LIBRARY "
+      "to full path to libgflags.")
+  endif (NOT GFLAGS_LIBRARY OR
+    NOT EXISTS ${GFLAGS_LIBRARY})
+
+  # gflags typically requires a threading library (which is OS dependent), note
+  # that this defines the CMAKE_THREAD_LIBS_INIT variable.  If we are able to
+  # detect threads, we assume that gflags requires it.
+  find_package(Threads QUIET)
+  set(GFLAGS_LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
+  # On Windows (including MinGW), the Shlwapi library is used by gflags if
+  # available.
+  if (WIN32)
+    include(CheckIncludeFileCXX)
+    check_include_file_cxx("shlwapi.h" HAVE_SHLWAPI)
+    if (HAVE_SHLWAPI)
+      list(APPEND GFLAGS_LINK_LIBRARIES shlwapi.lib)
+    endif(HAVE_SHLWAPI)
+  endif (WIN32)
+
+  # Mark internally as found, then verify. GFLAGS_REPORT_NOT_FOUND() unsets
+  # if called.
+  set(GFLAGS_FOUND TRUE)
+
+  # Identify what namespace gflags was built with.
+  if (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
+    # To handle Windows peculiarities / CMake bugs on MSVC we try two approaches
+    # to detect the gflags namespace:
+    #
+    # 1) Try to use check_cxx_source_compiles() to compile a trivial program
+    #    with the two choices for the gflags namespace.
+    #
+    # 2) [In the event 1) fails] Use regex on the gflags headers to try to
+    #    determine the gflags namespace.  Whilst this is less robust than 1),
+    #    it does avoid any interaction with msbuild.
+    gflags_check_gflags_namespace_using_try_compile()
+
+    if (NOT GFLAGS_NAMESPACE)
+      # Failed to determine gflags namespace using check_cxx_source_compiles()
+      # method, try and obtain it using regex on the gflags headers instead.
+      message(STATUS "Failed to find gflags namespace using using "
+        "check_cxx_source_compiles(), trying namespace regex instead, "
+        "this is expected on Windows.")
+      gflags_check_gflags_namespace_using_regex()
+
+      if (NOT GFLAGS_NAMESPACE)
+        gflags_report_not_found(
+          "Failed to determine gflags namespace either by "
+          "check_cxx_source_compiles(), or namespace regex.")
+      endif (NOT GFLAGS_NAMESPACE)
+    endif (NOT GFLAGS_NAMESPACE)
+  endif (GFLAGS_INCLUDE_DIR AND NOT GFLAGS_NAMESPACE)
+
+  # Make the GFLAGS_NAMESPACE a cache variable s/t the user can view it, and could
+  # overwrite it in the CMake GUI.
+  set(GFLAGS_NAMESPACE "${GFLAGS_NAMESPACE}" CACHE STRING
+    "gflags namespace (google or gflags)" FORCE)
+
+  # gflags does not seem to provide any record of the version in its
+  # source tree, thus cannot extract version.
+
+  # Catch case when caller has set GFLAGS_NAMESPACE in the cache / GUI
+  # with an invalid value.
+  if (GFLAGS_NAMESPACE AND
+      NOT GFLAGS_NAMESPACE STREQUAL "google" AND
+      NOT GFLAGS_NAMESPACE STREQUAL "gflags")
+    gflags_report_not_found(
+      "Caller defined GFLAGS_NAMESPACE:"
+      " ${GFLAGS_NAMESPACE} is not valid, not google or gflags.")
+  endif ()
+  # Catch case when caller has set GFLAGS_INCLUDE_DIR in the cache / GUI and
+  # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
+  # invalid, otherwise we would report the library as found.
+  if (GFLAGS_INCLUDE_DIR AND
+      NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
+    gflags_report_not_found(
+      "Caller defined GFLAGS_INCLUDE_DIR:"
+      " ${GFLAGS_INCLUDE_DIR} does not contain gflags/gflags.h header.")
+  endif (GFLAGS_INCLUDE_DIR AND
+    NOT EXISTS ${GFLAGS_INCLUDE_DIR}/gflags/gflags.h)
+  # TODO: This regex for gflags library is pretty primitive, we use lowercase
+  #       for comparison to handle Windows using CamelCase library names, could
+  #       this check be better?
+  string(TOLOWER "${GFLAGS_LIBRARY}" LOWERCASE_GFLAGS_LIBRARY)
+  if (GFLAGS_LIBRARY AND
+      NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
+    gflags_report_not_found(
+      "Caller defined GFLAGS_LIBRARY: "
+      "${GFLAGS_LIBRARY} does not match gflags.")
+  endif (GFLAGS_LIBRARY AND
+    NOT "${LOWERCASE_GFLAGS_LIBRARY}" MATCHES ".*gflags[^/]*")
+
+  gflags_reset_find_library_prefix()
+
+endif(NOT GFLAGS_FOUND)
+
+# Set standard CMake FindPackage variables if found.
+if (GFLAGS_FOUND)
+  set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})
+  set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
+endif (GFLAGS_FOUND)
+
+# Handle REQUIRED / QUIET optional arguments.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Gflags DEFAULT_MSG
+  GFLAGS_INCLUDE_DIRS GFLAGS_LIBRARIES GFLAGS_NAMESPACE)
+
+# Only mark internal variables as advanced if we found gflags, otherwise
+# leave them visible in the standard GUI for the user to set manually.
+if (GFLAGS_FOUND)
+  mark_as_advanced(FORCE GFLAGS_INCLUDE_DIR
+    GFLAGS_LIBRARY
+    GFLAGS_NAMESPACE
+    gflags_DIR) # Autogenerated by find_package(gflags)
+endif (GFLAGS_FOUND)
diff --git a/cmake/FindGlog.cmake b/cmake/FindGlog.cmake
new file mode 100644
index 0000000..979dced
--- /dev/null
+++ b/cmake/FindGlog.cmake
@@ -0,0 +1,346 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindGlog.cmake - Find Google glog logging library.
+#
+# This module defines the following variables:
+#
+# GLOG_FOUND: TRUE iff glog is found.
+# GLOG_INCLUDE_DIRS: Include directories for glog.
+# GLOG_LIBRARIES: Libraries required to link glog.
+# FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION: True iff the version of glog found
+#                                           was built & installed / exported
+#                                           as a CMake package.
+#
+# The following variables control the behaviour of this module:
+#
+# GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
+#                           then prefer using an exported CMake configuration
+#                           generated by glog > 0.3.4 over searching for the
+#                           glog components manually.  Otherwise (FALSE)
+#                           ignore any exported glog CMake configurations and
+#                           always perform a manual search for the components.
+#                           Default: TRUE iff user does not define this variable
+#                           before we are called, and does NOT specify either
+#                           GLOG_INCLUDE_DIR_HINTS or GLOG_LIBRARY_DIR_HINTS
+#                           otherwise FALSE.
+# GLOG_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                         search for glog includes, e.g: /timbuktu/include.
+# GLOG_LIBRARY_DIR_HINTS: List of additional directories in which to
+#                         search for glog libraries, e.g: /timbuktu/lib.
+#
+# The following variables are also defined by this module, but in line with
+# CMake recommended FindPackage() module style should NOT be referenced directly
+# by callers (use the plural variables detailed above instead).  These variables
+# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
+# are NOT re-called (i.e. search for library is not repeated) if these variables
+# are set with valid values _in the CMake cache_. This means that if these
+# variables are set directly in the cache, either by the user in the CMake GUI,
+# or by the user passing -DVAR=VALUE directives to CMake when called (which
+# explicitly defines a cache variable), then they will be used verbatim,
+# bypassing the HINTS variables and other hard-coded search locations.
+#
+# GLOG_INCLUDE_DIR: Include directory for glog, not including the
+#                   include directory of any dependencies.
+# GLOG_LIBRARY: glog library, not including the libraries of any
+#               dependencies.
+
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
+# FindGlog was invoked.
+macro(GLOG_RESET_FIND_LIBRARY_PREFIX)
+  if (MSVC AND CALLERS_CMAKE_FIND_LIBRARY_PREFIXES)
+    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif()
+endmacro(GLOG_RESET_FIND_LIBRARY_PREFIX)
+
+# Called if we failed to find glog or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(GLOG_REPORT_NOT_FOUND REASON_MSG)
+  unset(GLOG_FOUND)
+  unset(GLOG_INCLUDE_DIRS)
+  unset(GLOG_LIBRARIES)
+  # Make results of search visible in the CMake GUI if glog has not
+  # been found so that user does not have to toggle to advanced view.
+  mark_as_advanced(CLEAR GLOG_INCLUDE_DIR
+                         GLOG_LIBRARY)
+
+  glog_reset_find_library_prefix()
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (Glog_FIND_QUIETLY)
+    message(STATUS "Failed to find glog - " ${REASON_MSG} ${ARGN})
+  elseif (Glog_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find glog - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find glog - " ${REASON_MSG} ${ARGN})
+  endif ()
+  return()
+endmacro(GLOG_REPORT_NOT_FOUND)
+
+# Protect against any alternative find_package scripts for this library having
+# been called previously (in a client project) which set GLOG_FOUND, but not
+# the other variables we require / set here which could cause the search logic
+# here to fail.
+unset(GLOG_FOUND)
+
+# -----------------------------------------------------------------
+# By default, if the user has expressed no preference for using an exported
+# glog CMake configuration over performing a search for the installed
+# components, and has not specified any hints for the search locations, then
+# prefer a glog exported configuration if available.
+if (NOT DEFINED GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION
+    AND NOT GLOG_INCLUDE_DIR_HINTS
+    AND NOT GLOG_LIBRARY_DIR_HINTS)
+  message(STATUS "No preference for use of exported glog CMake configuration "
+    "set, and no hints for include/library directories provided. "
+    "Defaulting to preferring an installed/exported glog CMake configuration "
+    "if available.")
+  set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
+endif()
+
+if (GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
+  # Try to find an exported CMake configuration for glog, as generated by
+  # glog versions > 0.3.4
+  #
+  # We search twice, s/t we can invert the ordering of precedence used by
+  # find_package() for exported package build directories, and installed
+  # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
+  # respectively in [1].
+  #
+  # By default, exported build directories are (in theory) detected first, and
+  # this is usually the case on Windows.  However, on OS X & Linux, the install
+  # path (/usr/local) is typically present in the PATH environment variable
+  # which is checked in item 4) in [1] (i.e. before both of the above, unless
+  # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
+  # packages are usually detected in preference to exported package build
+  # directories.
+  #
+  # To ensure a more consistent response across all OSs, and as users usually
+  # want to prefer an installed version of a package over a locally built one
+  # where both exist (esp. as the exported build directory might be removed
+  # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
+  # means any build directories exported by the user are ignored, and thus
+  # installed directories are preferred.  If this fails to find the package
+  # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
+  # exported build directories will now be detected.
+  #
+  # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
+  # is item 5) in [1]), to not preferentially use projects that were built
+  # recently with the CMake GUI to ensure that we always prefer an installed
+  # version if available.
+  #
+  # NOTE: We use the NAMES option as glog erroneously uses 'google-glog' as its
+  #       project name when built with CMake, but exports itself as just 'glog'.
+  #       On Linux/OS X this does not break detection as the project name is
+  #       not used as part of the install path for the CMake package files,
+  #       e.g. /usr/local/lib/cmake/glog, where the <glog> suffix is hardcoded
+  #       in glog's CMakeLists.  However, on Windows the project name *is*
+  #       part of the install prefix: C:/Program Files/google-glog/[include,lib].
+  #       However, by default CMake checks:
+  #       C:/Program Files/<FIND_PACKAGE_ARGUMENT_NAME='glog'> which does not
+  #       exist and thus detection fails.  Thus we use the NAMES to force the
+  #       search to use both google-glog & glog.
+  #
+  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
+  find_package(glog QUIET
+                    NAMES google-glog glog
+                    NO_MODULE
+                    NO_CMAKE_PACKAGE_REGISTRY
+                    NO_CMAKE_BUILDS_PATH)
+  if (glog_FOUND)
+    message(STATUS "Found installed version of glog: ${glog_DIR}")
+  else()
+    # Failed to find an installed version of glog, repeat search allowing
+    # exported build directories.
+    message(STATUS "Failed to find installed glog CMake configuration, "
+      "searching for glog build directories exported with CMake.")
+    # Again pass NO_CMAKE_BUILDS_PATH, as we know that glog is exported and
+    # do not want to treat projects built with the CMake GUI preferentially.
+    find_package(glog QUIET
+                      NAMES google-glog glog
+                      NO_MODULE
+                      NO_CMAKE_BUILDS_PATH)
+    if (glog_FOUND)
+      message(STATUS "Found exported glog build directory: ${glog_DIR}")
+    endif(glog_FOUND)
+  endif(glog_FOUND)
+
+  set(FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION ${glog_FOUND})
+
+  if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
+    message(STATUS "Detected glog version: ${glog_VERSION}")
+    set(GLOG_FOUND ${glog_FOUND})
+    # glog wraps the include directories into the exported glog::glog target.
+    set(GLOG_INCLUDE_DIR "")
+    set(GLOG_LIBRARY glog::glog)
+  else (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
+    message(STATUS "Failed to find an installed/exported CMake configuration "
+      "for glog, will perform search for installed glog components.")
+  endif (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
+endif(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION)
+
+if (NOT GLOG_FOUND)
+  # Either failed to find an exported glog CMake configuration, or user
+  # told us not to use one.  Perform a manual search for all glog components.
+
+  # Handle possible presence of lib prefix for libraries on MSVC, see
+  # also GLOG_RESET_FIND_LIBRARY_PREFIX().
+  if (MSVC)
+    # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+    # s/t we can set it back before returning.
+    set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+    # The empty string in this list is important, it represents the case when
+    # the libraries have no prefix (shared libraries / DLLs).
+    set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif (MSVC)
+
+  # Search user-installed locations first, so that we prefer user installs
+  # to system installs where both exist.
+  list(APPEND GLOG_CHECK_INCLUDE_DIRS
+    /usr/local/include
+    /usr/local/homebrew/include # Mac OS X
+    /opt/local/var/macports/software # Mac OS X.
+    /opt/local/include
+    /usr/include)
+  # Windows (for C:/Program Files prefix).
+  list(APPEND GLOG_CHECK_PATH_SUFFIXES
+    glog/include
+    glog/Include
+    Glog/include
+    Glog/Include
+    google-glog/include # CMake installs with project name prefix.
+    google-glog/Include)
+
+  list(APPEND GLOG_CHECK_LIBRARY_DIRS
+    /usr/local/lib
+    /usr/local/homebrew/lib # Mac OS X.
+    /opt/local/lib
+    /usr/lib)
+  # Windows (for C:/Program Files prefix).
+  list(APPEND GLOG_CHECK_LIBRARY_SUFFIXES
+    glog/lib
+    glog/Lib
+    Glog/lib
+    Glog/Lib
+    google-glog/lib # CMake installs with project name prefix.
+    google-glog/Lib)
+
+  # Search supplied hint directories first if supplied.
+  find_path(GLOG_INCLUDE_DIR
+    NAMES glog/logging.h
+    HINTS ${GLOG_INCLUDE_DIR_HINTS}
+    PATHS ${GLOG_CHECK_INCLUDE_DIRS}
+    PATH_SUFFIXES ${GLOG_CHECK_PATH_SUFFIXES})
+  if (NOT GLOG_INCLUDE_DIR OR
+      NOT EXISTS ${GLOG_INCLUDE_DIR})
+    glog_report_not_found(
+      "Could not find glog include directory, set GLOG_INCLUDE_DIR "
+      "to directory containing glog/logging.h")
+  endif (NOT GLOG_INCLUDE_DIR OR
+    NOT EXISTS ${GLOG_INCLUDE_DIR})
+
+  find_library(GLOG_LIBRARY NAMES glog
+    HINTS ${GLOG_LIBRARY_DIR_HINTS}
+    PATHS ${GLOG_CHECK_LIBRARY_DIRS}
+    PATH_SUFFIXES ${GLOG_CHECK_LIBRARY_SUFFIXES})
+  if (NOT GLOG_LIBRARY OR
+      NOT EXISTS ${GLOG_LIBRARY})
+    glog_report_not_found(
+      "Could not find glog library, set GLOG_LIBRARY "
+      "to full path to libglog.")
+  endif (NOT GLOG_LIBRARY OR
+    NOT EXISTS ${GLOG_LIBRARY})
+
+  # Mark internally as found, then verify. GLOG_REPORT_NOT_FOUND() unsets
+  # if called.
+  set(GLOG_FOUND TRUE)
+
+  # Glog does not seem to provide any record of the version in its
+  # source tree, thus cannot extract version.
+
+  # Catch case when caller has set GLOG_INCLUDE_DIR in the cache / GUI and
+  # thus FIND_[PATH/LIBRARY] are not called, but specified locations are
+  # invalid, otherwise we would report the library as found.
+  if (GLOG_INCLUDE_DIR AND
+      NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
+    glog_report_not_found(
+      "Caller defined GLOG_INCLUDE_DIR:"
+      " ${GLOG_INCLUDE_DIR} does not contain glog/logging.h header.")
+  endif (GLOG_INCLUDE_DIR AND
+    NOT EXISTS ${GLOG_INCLUDE_DIR}/glog/logging.h)
+  # TODO: This regex for glog library is pretty primitive, we use lowercase
+  #       for comparison to handle Windows using CamelCase library names, could
+  #       this check be better?
+  string(TOLOWER "${GLOG_LIBRARY}" LOWERCASE_GLOG_LIBRARY)
+  if (GLOG_LIBRARY AND
+      NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
+    glog_report_not_found(
+      "Caller defined GLOG_LIBRARY: "
+      "${GLOG_LIBRARY} does not match glog.")
+  endif (GLOG_LIBRARY AND
+    NOT "${LOWERCASE_GLOG_LIBRARY}" MATCHES ".*glog[^/]*")
+
+  glog_reset_find_library_prefix()
+
+endif(NOT GLOG_FOUND)
+
+# Set standard CMake FindPackage variables if found.
+if (GLOG_FOUND)
+  set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
+  set(GLOG_LIBRARIES ${GLOG_LIBRARY})
+endif (GLOG_FOUND)
+
+# If we are using an exported CMake glog target, the include directories are
+# wrapped into the target itself, and do not have to be (and are not)
+# separately specified.  In which case, we should not add GLOG_INCLUDE_DIRS
+# to the list of required variables in order that glog be reported as found.
+if (FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
+  set(GLOG_REQUIRED_VARIABLES GLOG_LIBRARIES)
+else()
+  set(GLOG_REQUIRED_VARIABLES GLOG_INCLUDE_DIRS GLOG_LIBRARIES)
+endif()
+
+# Handle REQUIRED / QUIET optional arguments.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Glog DEFAULT_MSG
+  ${GLOG_REQUIRED_VARIABLES})
+
+# Only mark internal variables as advanced if we found glog, otherwise
+# leave them visible in the standard GUI for the user to set manually.
+if (GLOG_FOUND)
+  mark_as_advanced(FORCE GLOG_INCLUDE_DIR
+                         GLOG_LIBRARY
+                         glog_DIR) # Autogenerated by find_package(glog)
+endif (GLOG_FOUND)
diff --git a/cmake/FindSphinx.cmake b/cmake/FindSphinx.cmake
new file mode 100644
index 0000000..220108d
--- /dev/null
+++ b/cmake/FindSphinx.cmake
@@ -0,0 +1,66 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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: pablo.speciale@gmail.com (Pablo Speciale)
+#
+
+# Find the Sphinx documentation generator
+#
+# This modules defines
+#  SPHINX_EXECUTABLE
+#  SPHINX_FOUND
+
+find_program(SPHINX_EXECUTABLE
+             NAMES sphinx-build
+             PATHS
+               /usr/bin
+               /usr/local/bin
+               /opt/local/bin
+             DOC "Sphinx documentation generator")
+
+if (NOT SPHINX_EXECUTABLE)
+  set(_Python_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.6 1.5)
+
+  foreach (_version ${_Python_VERSIONS})
+    set(_sphinx_NAMES sphinx-build-${_version})
+
+    find_program(SPHINX_EXECUTABLE
+                 NAMES ${_sphinx_NAMES}
+                 PATHS
+                   /usr/bin
+                   /usr/local/bin
+                   /opt/local/bin
+                 DOC "Sphinx documentation generator")
+  endforeach ()
+endif ()
+
+include(FindPackageHandleStandardArgs)
+
+find_package_handle_standard_args(Sphinx DEFAULT_MSG SPHINX_EXECUTABLE)
+
+mark_as_advanced(SPHINX_EXECUTABLE)
diff --git a/cmake/FindSuiteSparse.cmake b/cmake/FindSuiteSparse.cmake
new file mode 100644
index 0000000..e4de8b0
--- /dev/null
+++ b/cmake/FindSuiteSparse.cmake
@@ -0,0 +1,509 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# FindSuiteSparse.cmake - Find SuiteSparse libraries & dependencies.
+#
+# This module defines the following variables:
+#
+# SUITESPARSE_FOUND: TRUE iff SuiteSparse and all dependencies have been found.
+# SUITESPARSE_INCLUDE_DIRS: Include directories for all SuiteSparse components.
+# SUITESPARSE_LIBRARIES: Libraries for all SuiteSparse component libraries and
+#                        dependencies.
+# SUITESPARSE_VERSION: Extracted from UFconfig.h (<= v3) or
+#                      SuiteSparse_config.h (>= v4).
+# SUITESPARSE_MAIN_VERSION: Equal to 4 if SUITESPARSE_VERSION = 4.2.1
+# SUITESPARSE_SUB_VERSION: Equal to 2 if SUITESPARSE_VERSION = 4.2.1
+# SUITESPARSE_SUBSUB_VERSION: Equal to 1 if SUITESPARSE_VERSION = 4.2.1
+#
+# SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION: TRUE iff running
+#     on Ubuntu, SUITESPARSE_VERSION is 3.4.0 and found SuiteSparse is a system
+#     install, in which case found version of SuiteSparse cannot be used to link
+#     a shared library due to a bug (static linking is unaffected).
+#
+# The following variables control the behaviour of this module:
+#
+# SUITESPARSE_INCLUDE_DIR_HINTS: List of additional directories in which to
+#                                search for SuiteSparse includes,
+#                                e.g: /timbuktu/include.
+# SUITESPARSE_LIBRARY_DIR_HINTS: List of additional directories in which to
+#                                search for SuiteSparse libraries,
+#                                e.g: /timbuktu/lib.
+#
+# The following variables define the presence / includes & libraries for the
+# SuiteSparse components searched for, the SUITESPARSE_XX variables are the
+# union of the variables for all components.
+#
+# == Symmetric Approximate Minimum Degree (AMD)
+# AMD_FOUND
+# AMD_INCLUDE_DIR
+# AMD_LIBRARY
+#
+# == Constrained Approximate Minimum Degree (CAMD)
+# CAMD_FOUND
+# CAMD_INCLUDE_DIR
+# CAMD_LIBRARY
+#
+# == Column Approximate Minimum Degree (COLAMD)
+# COLAMD_FOUND
+# COLAMD_INCLUDE_DIR
+# COLAMD_LIBRARY
+#
+# Constrained Column Approximate Minimum Degree (CCOLAMD)
+# CCOLAMD_FOUND
+# CCOLAMD_INCLUDE_DIR
+# CCOLAMD_LIBRARY
+#
+# == Sparse Supernodal Cholesky Factorization and Update/Downdate (CHOLMOD)
+# CHOLMOD_FOUND
+# CHOLMOD_INCLUDE_DIR
+# CHOLMOD_LIBRARY
+#
+# == Multifrontal Sparse QR (SuiteSparseQR)
+# SUITESPARSEQR_FOUND
+# SUITESPARSEQR_INCLUDE_DIR
+# SUITESPARSEQR_LIBRARY
+#
+# == Common configuration for all but CSparse (SuiteSparse version >= 4).
+# SUITESPARSE_CONFIG_FOUND
+# SUITESPARSE_CONFIG_INCLUDE_DIR
+# SUITESPARSE_CONFIG_LIBRARY
+#
+# == Common configuration for all but CSparse (SuiteSparse version < 4).
+# UFCONFIG_FOUND
+# UFCONFIG_INCLUDE_DIR
+#
+# Optional SuiteSparse Dependencies:
+#
+# == Serial Graph Partitioning and Fill-reducing Matrix Ordering (METIS)
+# METIS_FOUND
+# METIS_LIBRARY
+
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
+# FindSuiteSparse was invoked.
+macro(SUITESPARSE_RESET_FIND_LIBRARY_PREFIX)
+  if (MSVC)
+    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  endif (MSVC)
+endmacro(SUITESPARSE_RESET_FIND_LIBRARY_PREFIX)
+
+# Called if we failed to find SuiteSparse or any of it's required dependencies,
+# unsets all public (designed to be used externally) variables and reports
+# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
+macro(SUITESPARSE_REPORT_NOT_FOUND REASON_MSG)
+  unset(SUITESPARSE_FOUND)
+  unset(SUITESPARSE_INCLUDE_DIRS)
+  unset(SUITESPARSE_LIBRARIES)
+  unset(SUITESPARSE_VERSION)
+  unset(SUITESPARSE_MAIN_VERSION)
+  unset(SUITESPARSE_SUB_VERSION)
+  unset(SUITESPARSE_SUBSUB_VERSION)
+  # Do NOT unset SUITESPARSE_FOUND_REQUIRED_VARS here, as it is used by
+  # FindPackageHandleStandardArgs() to generate the automatic error message on
+  # failure which highlights which components are missing.
+
+  suitesparse_reset_find_library_prefix()
+
+  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
+  # use the camelcase library name, not uppercase.
+  if (SuiteSparse_FIND_QUIETLY)
+    message(STATUS "Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
+  elseif (SuiteSparse_FIND_REQUIRED)
+    message(FATAL_ERROR "Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
+  else()
+    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
+    # but continues configuration and allows generation.
+    message("-- Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
+  endif (SuiteSparse_FIND_QUIETLY)
+
+  # Do not call return(), s/t we keep processing if not called with REQUIRED
+  # and report all missing components, rather than bailing after failing to find
+  # the first.
+endmacro(SUITESPARSE_REPORT_NOT_FOUND)
+
+# Protect against any alternative find_package scripts for this library having
+# been called previously (in a client project) which set SUITESPARSE_FOUND, but
+# not the other variables we require / set here which could cause the search
+# logic here to fail.
+unset(SUITESPARSE_FOUND)
+
+# Handle possible presence of lib prefix for libraries on MSVC, see
+# also SUITESPARSE_RESET_FIND_LIBRARY_PREFIX().
+if (MSVC)
+  # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
+  # s/t we can set it back before returning.
+  set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
+  # The empty string in this list is important, it represents the case when
+  # the libraries have no prefix (shared libraries / DLLs).
+  set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
+endif (MSVC)
+
+# Specify search directories for include files and libraries (this is the union
+# of the search directories for all OSs).  Search user-specified hint
+# directories first if supplied, and search user-installed locations first
+# so that we prefer user installs to system installs where both exist.
+list(APPEND SUITESPARSE_CHECK_INCLUDE_DIRS
+  /opt/local/include
+  /opt/local/include/ufsparse # Mac OS X
+  /usr/local/homebrew/include # Mac OS X
+  /usr/local/include
+  /usr/include)
+list(APPEND SUITESPARSE_CHECK_LIBRARY_DIRS
+  /opt/local/lib
+  /opt/local/lib/ufsparse # Mac OS X
+  /usr/local/homebrew/lib # Mac OS X
+  /usr/local/lib
+  /usr/lib)
+# Additional suffixes to try appending to each search path.
+list(APPEND SUITESPARSE_CHECK_PATH_SUFFIXES
+  suitesparse) # Windows/Ubuntu
+
+# Wrappers to find_path/library that pass the SuiteSparse search hints/paths.
+#
+# suitesparse_find_component(<component> [FILES name1 [name2 ...]]
+#                                        [LIBRARIES name1 [name2 ...]]
+#                                        [REQUIRED])
+macro(suitesparse_find_component COMPONENT)
+  include(CMakeParseArguments)
+  set(OPTIONS REQUIRED)
+  set(MULTI_VALUE_ARGS FILES LIBRARIES)
+  cmake_parse_arguments(SUITESPARSE_FIND_${COMPONENT}
+    "${OPTIONS}" "" "${MULTI_VALUE_ARGS}" ${ARGN})
+
+  if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
+    list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS ${COMPONENT}_FOUND)
+  endif()
+
+  set(${COMPONENT}_FOUND TRUE)
+  if (SUITESPARSE_FIND_${COMPONENT}_FILES)
+    find_path(${COMPONENT}_INCLUDE_DIR
+      NAMES ${SUITESPARSE_FIND_${COMPONENT}_FILES}
+      HINTS ${SUITESPARSE_INCLUDE_DIR_HINTS}
+      PATHS ${SUITESPARSE_CHECK_INCLUDE_DIRS}
+      PATH_SUFFIXES ${SUITESPARSE_CHECK_PATH_SUFFIXES})
+    if (${COMPONENT}_INCLUDE_DIR)
+      message(STATUS "Found ${COMPONENT} headers in: "
+        "${${COMPONENT}_INCLUDE_DIR}")
+      mark_as_advanced(${COMPONENT}_INCLUDE_DIR)
+    else()
+      # Specified headers not found.
+      set(${COMPONENT}_FOUND FALSE)
+      if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
+        suitesparse_report_not_found(
+          "Did not find ${COMPONENT} header (required SuiteSparse component).")
+      else()
+        message(STATUS "Did not find ${COMPONENT} header (optional "
+          "SuiteSparse component).")
+        # Hide optional vars from CMake GUI even if not found.
+        mark_as_advanced(${COMPONENT}_INCLUDE_DIR)
+      endif()
+    endif()
+  endif()
+
+  if (SUITESPARSE_FIND_${COMPONENT}_LIBRARIES)
+    find_library(${COMPONENT}_LIBRARY
+      NAMES ${SUITESPARSE_FIND_${COMPONENT}_LIBRARIES}
+      HINTS ${SUITESPARSE_LIBRARY_DIR_HINTS}
+      PATHS ${SUITESPARSE_CHECK_LIBRARY_DIRS}
+      PATH_SUFFIXES ${SUITESPARSE_CHECK_PATH_SUFFIXES})
+    if (${COMPONENT}_LIBRARY)
+      message(STATUS "Found ${COMPONENT} library: ${${COMPONENT}_LIBRARY}")
+      mark_as_advanced(${COMPONENT}_LIBRARY)
+    else ()
+      # Specified libraries not found.
+      set(${COMPONENT}_FOUND FALSE)
+      if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
+        suitesparse_report_not_found(
+          "Did not find ${COMPONENT} library (required SuiteSparse component).")
+      else()
+        message(STATUS "Did not find ${COMPONENT} library (optional SuiteSparse "
+          "dependency)")
+        # Hide optional vars from CMake GUI even if not found.
+        mark_as_advanced(${COMPONENT}_LIBRARY)
+      endif()
+    endif()
+  endif()
+endmacro()
+
+# Given the number of components of SuiteSparse, and to ensure that the
+# automatic failure message generated by FindPackageHandleStandardArgs()
+# when not all required components are found is helpful, we maintain a list
+# of all variables that must be defined for SuiteSparse to be considered found.
+unset(SUITESPARSE_FOUND_REQUIRED_VARS)
+
+# BLAS.
+find_package(BLAS QUIET)
+if (NOT BLAS_FOUND)
+  suitesparse_report_not_found(
+    "Did not find BLAS library (required for SuiteSparse).")
+endif (NOT BLAS_FOUND)
+list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS BLAS_FOUND)
+
+# LAPACK.
+find_package(LAPACK QUIET)
+if (NOT LAPACK_FOUND)
+  suitesparse_report_not_found(
+    "Did not find LAPACK library (required for SuiteSparse).")
+endif (NOT LAPACK_FOUND)
+list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS LAPACK_FOUND)
+
+suitesparse_find_component(AMD REQUIRED FILES amd.h LIBRARIES amd)
+suitesparse_find_component(CAMD REQUIRED FILES camd.h LIBRARIES camd)
+suitesparse_find_component(COLAMD REQUIRED FILES colamd.h LIBRARIES colamd)
+suitesparse_find_component(CCOLAMD REQUIRED FILES ccolamd.h LIBRARIES ccolamd)
+suitesparse_find_component(CHOLMOD REQUIRED FILES cholmod.h LIBRARIES cholmod)
+suitesparse_find_component(
+  SUITESPARSEQR REQUIRED FILES SuiteSparseQR.hpp LIBRARIES spqr)
+if (SUITESPARSEQR_FOUND)
+  # SuiteSparseQR may be compiled with Intel Threading Building Blocks,
+  # we assume that if TBB is installed, SuiteSparseQR was compiled with
+  # support for it, this will do no harm if it wasn't.
+  find_package(TBB QUIET)
+  if (TBB_FOUND)
+    message(STATUS "Found Intel Thread Building Blocks (TBB) library "
+      "(${TBB_VERSION}) assuming SuiteSparseQR was compiled "
+      "with TBB.")
+    # Add the TBB libraries to the SuiteSparseQR libraries (the only
+    # libraries to optionally depend on TBB).
+    list(APPEND SUITESPARSEQR_LIBRARY ${TBB_LIBRARIES})
+  else()
+    message(STATUS "Did not find Intel TBB library, assuming SuiteSparseQR was "
+      "not compiled with TBB.")
+  endif()
+endif(SUITESPARSEQR_FOUND)
+
+# UFconfig / SuiteSparse_config.
+#
+# If SuiteSparse version is >= 4 then SuiteSparse_config is required.
+# For SuiteSparse 3, UFconfig.h is required.
+suitesparse_find_component(
+  SUITESPARSE_CONFIG FILES SuiteSparse_config.h LIBRARIES suitesparseconfig)
+
+if (SUITESPARSE_CONFIG_FOUND)
+  # SuiteSparse_config (SuiteSparse version >= 4) requires librt library for
+  # timing by default when compiled on Linux or Unix, but not on OSX (which
+  # does not have librt).
+  if (CMAKE_SYSTEM_NAME MATCHES "Linux" OR UNIX AND NOT APPLE)
+    suitesparse_find_component(LIBRT LIBRARIES rt)
+    if (LIBRT_FOUND)
+      message(STATUS "Adding librt: ${LIBRT_LIBRARY} to "
+        "SuiteSparse_config libraries (required on Linux & Unix [not OSX] if "
+        "SuiteSparse is compiled with timing).")
+      list(APPEND SUITESPARSE_CONFIG_LIBRARY ${LIBRT_LIBRARY})
+    else()
+      message(STATUS "Could not find librt, but found SuiteSparse_config, "
+        "assuming that SuiteSparse was compiled without timing.")
+    endif ()
+  endif (CMAKE_SYSTEM_NAME MATCHES "Linux" OR UNIX AND NOT APPLE)
+else()
+  # Failed to find SuiteSparse_config (>= v4 installs), instead look for
+  # UFconfig header which should be present in < v4 installs.
+  suitesparse_find_component(UFCONFIG FILES UFconfig.h)
+endif ()
+
+if (NOT SUITESPARSE_CONFIG_FOUND AND
+    NOT UFCONFIG_FOUND)
+  suitesparse_report_not_found(
+    "Failed to find either: SuiteSparse_config header & library (should be "
+    "present in all SuiteSparse >= v4 installs), or UFconfig header (should "
+    "be present in all SuiteSparse < v4 installs).")
+endif()
+
+# Extract the SuiteSparse version from the appropriate header (UFconfig.h for
+# <= v3, SuiteSparse_config.h for >= v4).
+list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS SUITESPARSE_VERSION)
+
+if (UFCONFIG_FOUND)
+  # SuiteSparse version <= 3.
+  set(SUITESPARSE_VERSION_FILE ${UFCONFIG_INCLUDE_DIR}/UFconfig.h)
+  if (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+    suitesparse_report_not_found(
+      "Could not find file: ${SUITESPARSE_VERSION_FILE} containing version "
+      "information for <= v3 SuiteSparse installs, but UFconfig was found "
+      "(only present in <= v3 installs).")
+  else (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+    file(READ ${SUITESPARSE_VERSION_FILE} UFCONFIG_CONTENTS)
+
+    string(REGEX MATCH "#define SUITESPARSE_MAIN_VERSION [0-9]+"
+      SUITESPARSE_MAIN_VERSION "${UFCONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_MAIN_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_MAIN_VERSION}")
+
+    string(REGEX MATCH "#define SUITESPARSE_SUB_VERSION [0-9]+"
+      SUITESPARSE_SUB_VERSION "${UFCONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_SUB_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_SUB_VERSION "${SUITESPARSE_SUB_VERSION}")
+
+    string(REGEX MATCH "#define SUITESPARSE_SUBSUB_VERSION [0-9]+"
+      SUITESPARSE_SUBSUB_VERSION "${UFCONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_SUBSUB_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_SUBSUB_VERSION}")
+
+    # This is on a single line s/t CMake does not interpret it as a list of
+    # elements and insert ';' separators which would result in 4.;2.;1 nonsense.
+    set(SUITESPARSE_VERSION
+      "${SUITESPARSE_MAIN_VERSION}.${SUITESPARSE_SUB_VERSION}.${SUITESPARSE_SUBSUB_VERSION}")
+  endif (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+endif (UFCONFIG_FOUND)
+
+if (SUITESPARSE_CONFIG_FOUND)
+  # SuiteSparse version >= 4.
+  set(SUITESPARSE_VERSION_FILE
+    ${SUITESPARSE_CONFIG_INCLUDE_DIR}/SuiteSparse_config.h)
+  if (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+    suitesparse_report_not_found(
+      "Could not find file: ${SUITESPARSE_VERSION_FILE} containing version "
+      "information for >= v4 SuiteSparse installs, but SuiteSparse_config was "
+      "found (only present in >= v4 installs).")
+  else (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+    file(READ ${SUITESPARSE_VERSION_FILE} SUITESPARSE_CONFIG_CONTENTS)
+
+    string(REGEX MATCH "#define SUITESPARSE_MAIN_VERSION [0-9]+"
+      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_MAIN_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_MAIN_VERSION}")
+
+    string(REGEX MATCH "#define SUITESPARSE_SUB_VERSION [0-9]+"
+      SUITESPARSE_SUB_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_SUB_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_SUB_VERSION "${SUITESPARSE_SUB_VERSION}")
+
+    string(REGEX MATCH "#define SUITESPARSE_SUBSUB_VERSION [0-9]+"
+      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
+    string(REGEX REPLACE "#define SUITESPARSE_SUBSUB_VERSION ([0-9]+)" "\\1"
+      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_SUBSUB_VERSION}")
+
+    # This is on a single line s/t CMake does not interpret it as a list of
+    # elements and insert ';' separators which would result in 4.;2.;1 nonsense.
+    set(SUITESPARSE_VERSION
+      "${SUITESPARSE_MAIN_VERSION}.${SUITESPARSE_SUB_VERSION}.${SUITESPARSE_SUBSUB_VERSION}")
+  endif (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
+endif (SUITESPARSE_CONFIG_FOUND)
+
+# METIS (Optional dependency).
+suitesparse_find_component(METIS LIBRARIES metis)
+
+# Only mark SuiteSparse as found if all required components and dependencies
+# have been found.
+set(SUITESPARSE_FOUND TRUE)
+foreach(REQUIRED_VAR ${SUITESPARSE_FOUND_REQUIRED_VARS})
+  if (NOT ${REQUIRED_VAR})
+    set(SUITESPARSE_FOUND FALSE)
+  endif (NOT ${REQUIRED_VAR})
+endforeach(REQUIRED_VAR ${SUITESPARSE_FOUND_REQUIRED_VARS})
+
+if (SUITESPARSE_FOUND)
+  list(APPEND SUITESPARSE_INCLUDE_DIRS
+    ${AMD_INCLUDE_DIR}
+    ${CAMD_INCLUDE_DIR}
+    ${COLAMD_INCLUDE_DIR}
+    ${CCOLAMD_INCLUDE_DIR}
+    ${CHOLMOD_INCLUDE_DIR}
+    ${SUITESPARSEQR_INCLUDE_DIR})
+  # Handle config separately, as otherwise at least one of them will be set
+  # to NOTFOUND which would cause any check on SUITESPARSE_INCLUDE_DIRS to fail.
+  if (SUITESPARSE_CONFIG_FOUND)
+    list(APPEND SUITESPARSE_INCLUDE_DIRS
+      ${SUITESPARSE_CONFIG_INCLUDE_DIR})
+  endif (SUITESPARSE_CONFIG_FOUND)
+  if (UFCONFIG_FOUND)
+    list(APPEND SUITESPARSE_INCLUDE_DIRS
+      ${UFCONFIG_INCLUDE_DIR})
+  endif (UFCONFIG_FOUND)
+  # As SuiteSparse includes are often all in the same directory, remove any
+  # repetitions.
+  list(REMOVE_DUPLICATES SUITESPARSE_INCLUDE_DIRS)
+
+  # Important: The ordering of these libraries is *NOT* arbitrary, as these
+  # could potentially be static libraries their link ordering is important.
+  list(APPEND SUITESPARSE_LIBRARIES
+    ${SUITESPARSEQR_LIBRARY}
+    ${CHOLMOD_LIBRARY}
+    ${CCOLAMD_LIBRARY}
+    ${CAMD_LIBRARY}
+    ${COLAMD_LIBRARY}
+    ${AMD_LIBRARY}
+    ${LAPACK_LIBRARIES}
+    ${BLAS_LIBRARIES})
+  if (SUITESPARSE_CONFIG_FOUND)
+    list(APPEND SUITESPARSE_LIBRARIES
+      ${SUITESPARSE_CONFIG_LIBRARY})
+  endif (SUITESPARSE_CONFIG_FOUND)
+  if (METIS_FOUND)
+    list(APPEND SUITESPARSE_LIBRARIES
+      ${METIS_LIBRARY})
+  endif (METIS_FOUND)
+endif()
+
+# Determine if we are running on Ubuntu with the package install of SuiteSparse
+# which is broken and does not support linking a shared library.
+set(SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION FALSE)
+if (CMAKE_SYSTEM_NAME MATCHES "Linux" AND
+    SUITESPARSE_VERSION VERSION_EQUAL 3.4.0)
+  find_program(LSB_RELEASE_EXECUTABLE lsb_release)
+  if (LSB_RELEASE_EXECUTABLE)
+    # Any even moderately recent Ubuntu release (likely to be affected by
+    # this bug) should have lsb_release, if it isn't present we are likely
+    # on a different Linux distribution (should be fine).
+    execute_process(COMMAND ${LSB_RELEASE_EXECUTABLE} -si
+      OUTPUT_VARIABLE LSB_DISTRIBUTOR_ID
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    if (LSB_DISTRIBUTOR_ID MATCHES "Ubuntu" AND
+        SUITESPARSE_LIBRARIES MATCHES "/usr/lib/libamd")
+      # We are on Ubuntu, and the SuiteSparse version matches the broken
+      # system install version and is a system install.
+      set(SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION TRUE)
+      message(STATUS "Found system install of SuiteSparse "
+        "${SUITESPARSE_VERSION} running on Ubuntu, which has a known bug "
+        "preventing linking of shared libraries (static linking unaffected).")
+    endif (LSB_DISTRIBUTOR_ID MATCHES "Ubuntu" AND
+      SUITESPARSE_LIBRARIES MATCHES "/usr/lib/libamd")
+  endif (LSB_RELEASE_EXECUTABLE)
+endif (CMAKE_SYSTEM_NAME MATCHES "Linux" AND
+  SUITESPARSE_VERSION VERSION_EQUAL 3.4.0)
+
+suitesparse_reset_find_library_prefix()
+
+# Handle REQUIRED and QUIET arguments to FIND_PACKAGE
+include(FindPackageHandleStandardArgs)
+if (SUITESPARSE_FOUND)
+  find_package_handle_standard_args(SuiteSparse
+    REQUIRED_VARS ${SUITESPARSE_FOUND_REQUIRED_VARS}
+    VERSION_VAR SUITESPARSE_VERSION
+    FAIL_MESSAGE "Failed to find some/all required components of SuiteSparse.")
+else (SUITESPARSE_FOUND)
+  # Do not pass VERSION_VAR to FindPackageHandleStandardArgs() if we failed to
+  # find SuiteSparse to avoid a confusing autogenerated failure message
+  # that states 'not found (missing: FOO) (found version: x.y.z)'.
+  find_package_handle_standard_args(SuiteSparse
+    REQUIRED_VARS ${SUITESPARSE_FOUND_REQUIRED_VARS}
+    FAIL_MESSAGE "Failed to find some/all required components of SuiteSparse.")
+endif (SUITESPARSE_FOUND)
diff --git a/cmake/FindTBB.cmake b/cmake/FindTBB.cmake
new file mode 100644
index 0000000..d3e2852
--- /dev/null
+++ b/cmake/FindTBB.cmake
@@ -0,0 +1,245 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2015 Justus Calvin
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
+#
+# FindTBB
+# -------
+#
+# Find TBB include directories and libraries.
+#
+# Usage:
+#
+#  find_package(TBB [major[.minor]] [EXACT]
+#               [QUIET] [REQUIRED]
+#               [[COMPONENTS] [components...]]
+#               [OPTIONAL_COMPONENTS components...])
+#
+# where the allowed components are tbbmalloc and tbb_preview. Users may modify
+# the behavior of this module with the following variables:
+#
+# * TBB_ROOT_DIR          - The base directory the of TBB installation.
+# * TBB_INCLUDE_DIR       - The directory that contains the TBB headers files.
+# * TBB_LIBRARY           - The directory that contains the TBB library files.
+# * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library.
+#                           These libraries, if specified, override the
+#                           corresponding library search results, where <library>
+#                           may be tbb, tbb_debug, tbbmalloc, tbbmalloc_debug,
+#                           tbb_preview, or tbb_preview_debug.
+# * TBB_USE_DEBUG_BUILD   - The debug version of tbb libraries, if present, will
+#                           be used instead of the release version.
+#
+# Users may modify the behavior of this module with the following environment
+# variables:
+#
+# * TBB_INSTALL_DIR
+# * TBBROOT
+# * LIBRARY_PATH
+#
+# This module will set the following variables:
+#
+# * TBB_FOUND             - Set to false, or undefined, if we haven’t found, or
+#                           don’t want to use TBB.
+# * TBB_<component>_FOUND - If False, optional <component> part of TBB sytem is
+#                           not available.
+# * TBB_VERSION           - The full version string
+# * TBB_VERSION_MAJOR     - The major version
+# * TBB_VERSION_MINOR     - The minor version
+# * TBB_INTERFACE_VERSION - The interface version number defined in
+#                           tbb/tbb_stddef.h.
+# * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of
+#                           <library>, where <library> may be tbb, tbb_debug,
+#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or
+#                           tbb_preview_debug.
+# * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of
+#                           <library>, where <library> may be tbb, tbb_debug,
+#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or
+#                           tbb_preview_debug.
+#
+# The following varibles should be used to build and link with TBB:
+#
+# * TBB_INCLUDE_DIRS - The include directory for TBB.
+# * TBB_LIBRARIES    - The libraries to link against to use TBB.
+# * TBB_DEFINITIONS  - Definitions to use when compiling code that uses TBB.
+
+include(FindPackageHandleStandardArgs)
+
+if(NOT TBB_FOUND)
+
+  ##################################
+  # Check the build type
+  ##################################
+
+  if(NOT DEFINED TBB_USE_DEBUG_BUILD)
+    if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug|RelWithDebInfo|RELWITHDEBINFO|relwithdebinfo)")
+      set(TBB_USE_DEBUG_BUILD TRUE)
+    else()
+      set(TBB_USE_DEBUG_BUILD FALSE)
+    endif()
+  endif()
+
+  ##################################
+  # Set the TBB search directories
+  ##################################
+
+  # Define search paths based on user input and environment variables
+  set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
+
+  # Define the search directories based on the current platform
+  if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
+    set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
+                               "C:/Program Files (x86)/Intel/TBB")
+
+    # Set the target architecture
+    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+      set(TBB_ARCHITECTURE "intel64")
+    else()
+      set(TBB_ARCHITECTURE "ia32")
+    endif()
+
+    # Set the TBB search library path search suffix based on the version of VC
+    if(WINDOWS_STORE)
+      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11_ui")
+    elseif(MSVC14)
+      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc14")
+    elseif(MSVC12)
+      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc12")
+    elseif(MSVC11)
+      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11")
+    elseif(MSVC10)
+      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc10")
+    endif()
+
+    # Add the library path search suffix for the VC independent version of TBB
+    list(APPEND TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc_mt")
+
+  elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
+    # OS X
+    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
+
+    # TODO: Check to see which C++ library is being used by the compiler.
+    if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
+      # The default C++ library on OS X 10.9 and later is libc++
+      set(TBB_LIB_PATH_SUFFIX "lib/libc++")
+    else()
+      set(TBB_LIB_PATH_SUFFIX "lib")
+    endif()
+  elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+    # Linux
+    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
+
+    # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
+    #       <arch>/gcc4.1. For now, assume that the compiler is more recent than
+    #       gcc 4.4.x or later.
+    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
+      set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
+    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
+      set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
+    endif()
+  endif()
+
+  ##################################
+  # Find the TBB include dir
+  ##################################
+
+  find_path(TBB_INCLUDE_DIRS tbb/tbb.h
+      HINTS ${TBB_INCLUDE_DIR} ${TBB_SEARCH_DIR}
+      PATHS ${TBB_DEFAULT_SEARCH_DIR}
+      PATH_SUFFIXES include)
+
+  ##################################
+  # Find TBB components
+  ##################################
+
+  # Find each component
+  foreach(_comp tbb_preview tbbmalloc tbb)
+    # Search for the libraries
+    find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}
+        HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
+        PATHS ${TBB_DEFAULT_SEARCH_DIR}
+        PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
+
+    find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}_debug
+        HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
+        PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
+        PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
+
+
+    # Set the library to be used for the component
+    if(NOT TBB_${_comp}_LIBRARY)
+      if(TBB_USE_DEBUG_BUILD AND TBB_${_comp}_LIBRARY_DEBUG)
+        set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_DEBUG}")
+      elseif(TBB_${_comp}_LIBRARY_RELEASE)
+        set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_RELEASE}")
+      elseif(TBB_${_comp}_LIBRARY_DEBUG)
+        set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_DEBUG}")
+      endif()
+    endif()
+
+    # Set the TBB library list and component found variables
+    if(TBB_${_comp}_LIBRARY)
+      list(APPEND TBB_LIBRARIES "${TBB_${_comp}_LIBRARY}")
+      set(TBB_${_comp}_FOUND TRUE)
+    else()
+      set(TBB_${_comp}_FOUND FALSE)
+    endif()
+
+    mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
+    mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
+    mark_as_advanced(TBB_${_comp}_LIBRARY)
+
+  endforeach()
+
+  ##################################
+  # Set compile flags
+  ##################################
+
+  if(TBB_tbb_LIBRARY MATCHES "debug")
+    set(TBB_DEFINITIONS "-DTBB_USE_DEBUG=1")
+  endif()
+
+  ##################################
+  # Set version strings
+  ##################################
+
+  if(TBB_INCLUDE_DIRS)
+    file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
+    string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
+            TBB_VERSION_MAJOR "${_tbb_version_file}")
+    string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
+            TBB_VERSION_MINOR "${_tbb_version_file}")
+    string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
+            TBB_INTERFACE_VERSION "${_tbb_version_file}")
+    set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
+  endif()
+
+  find_package_handle_standard_args(TBB
+      REQUIRED_VARS TBB_INCLUDE_DIRS TBB_LIBRARIES
+      HANDLE_COMPONENTS
+      VERSION_VAR TBB_VERSION)
+
+  mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES)
+
+  unset(TBB_ARCHITECTURE)
+  unset(TBB_LIB_PATH_SUFFIX)
+  unset(TBB_DEFAULT_SEARCH_DIR)
+
+endif()
diff --git a/cmake/PrettyPrintCMakeList.cmake b/cmake/PrettyPrintCMakeList.cmake
new file mode 100644
index 0000000..067883c
--- /dev/null
+++ b/cmake/PrettyPrintCMakeList.cmake
@@ -0,0 +1,39 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2018 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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.
+#
+# Authors: alexs.mac@gmail.com (Alex Stewart)
+
+# pretty_print_cmake_list( OUTPUT_VAR [item1 [item2 ... ]] )
+#
+# Sets ${OUTPUT_VAR} in the caller's scope to a human-readable string
+# representation of the list passed as the remaining arguments formed
+# as: "[item1, item2, ..., itemN]".
+function(pretty_print_cmake_list OUTPUT_VAR)
+  string(REPLACE ";" ", " PRETTY_LIST_STRING "[${ARGN}]")
+  set(${OUTPUT_VAR} "${PRETTY_LIST_STRING}" PARENT_SCOPE)
+endfunction()
diff --git a/cmake/ReadCeresVersionFromSource.cmake b/cmake/ReadCeresVersionFromSource.cmake
new file mode 100644
index 0000000..2859744
--- /dev/null
+++ b/cmake/ReadCeresVersionFromSource.cmake
@@ -0,0 +1,81 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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)
+#
+
+# Extract Ceres version from <CERES_SOURCE_ROOT>/include/ceres/version.h
+# so that we only have a single definition of the Ceres version, not two
+# one in the source and one in the CMakeLists.txt.
+macro(read_ceres_version_from_source CERES_SOURCE_ROOT)
+  set(CERES_VERSION_FILE ${CERES_SOURCE_ROOT}/include/ceres/version.h)
+  if (NOT EXISTS ${CERES_VERSION_FILE})
+    message(FATAL_ERROR "Cannot find Ceres version.h file in specified "
+      "Ceres source directory: ${CERES_SOURCE_ROOT}, it is not here: "
+      "${CERES_VERSION_FILE}")
+  endif()
+
+  file(READ ${CERES_VERSION_FILE} CERES_VERSION_FILE_CONTENTS)
+
+  string(REGEX MATCH "#define CERES_VERSION_MAJOR [0-9]+"
+    CERES_VERSION_MAJOR "${CERES_VERSION_FILE_CONTENTS}")
+  string(REGEX REPLACE "#define CERES_VERSION_MAJOR ([0-9]+)" "\\1"
+    CERES_VERSION_MAJOR "${CERES_VERSION_MAJOR}")
+  # NOTE: if (VAR) is FALSE if VAR is numeric and <= 0, as such we cannot use
+  #       it for testing version numbers, which might well be zero, at least
+  #       for the patch version, hence check for empty string explicitly.
+  if ("${CERES_VERSION_MAJOR}" STREQUAL "")
+    message(FATAL_ERROR "Failed to extract Ceres major version from "
+      "${CERES_VERSION_FILE}")
+  endif()
+
+  string(REGEX MATCH "#define CERES_VERSION_MINOR [0-9]+"
+    CERES_VERSION_MINOR "${CERES_VERSION_FILE_CONTENTS}")
+  string(REGEX REPLACE "#define CERES_VERSION_MINOR ([0-9]+)" "\\1"
+    CERES_VERSION_MINOR "${CERES_VERSION_MINOR}")
+  if ("${CERES_VERSION_MINOR}" STREQUAL "")
+    message(FATAL_ERROR "Failed to extract Ceres minor version from "
+      "${CERES_VERSION_FILE}")
+  endif()
+
+  string(REGEX MATCH "#define CERES_VERSION_REVISION [0-9]+"
+    CERES_VERSION_PATCH "${CERES_VERSION_FILE_CONTENTS}")
+  string(REGEX REPLACE "#define CERES_VERSION_REVISION ([0-9]+)" "\\1"
+    CERES_VERSION_PATCH "${CERES_VERSION_PATCH}")
+  if ("${CERES_VERSION_PATCH}" STREQUAL "")
+    message(FATAL_ERROR "Failed to extract Ceres patch version from "
+      "${CERES_VERSION_FILE}")
+  endif()
+
+  # This is on a single line s/t CMake does not interpret it as a list of
+  # elements and insert ';' separators which would result in 3.;2.;0 nonsense.
+  set(CERES_VERSION "${CERES_VERSION_MAJOR}.${CERES_VERSION_MINOR}.${CERES_VERSION_PATCH}")
+
+  message(STATUS "Detected Ceres version: ${CERES_VERSION} from "
+    "${CERES_VERSION_FILE}")
+endmacro()
diff --git a/cmake/UpdateCacheVariable.cmake b/cmake/UpdateCacheVariable.cmake
new file mode 100644
index 0000000..82ae571
--- /dev/null
+++ b/cmake/UpdateCacheVariable.cmake
@@ -0,0 +1,48 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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(IS_DEFINED_IN_CACHE CACHE ${VAR_NAME} PROPERTY VALUE SET)
+  if (NOT IS_DEFINED_IN_CACHE)
+    message(FATAL_ERROR "Specified variable to update in cache: "
+      "${VAR_NAME} has not been set in the cache.")
+  endif()
+  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()
diff --git a/cmake/config.h.in b/cmake/config.h.in
new file mode 100644
index 0000000..1caacfe
--- /dev/null
+++ b/cmake/config.h.in
@@ -0,0 +1,89 @@
+// Ceres Solver - A fast non-linear least squares minimizer
+// Copyright 2015 Google Inc. All rights reserved.
+// http://ceres-solver.org/
+//
+// 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)
+
+// Configuration options for Ceres.
+//
+// Do not edit this file, it was automatically configured by CMake when
+// Ceres was compiled with the relevant configuration for the machine
+// on which Ceres was compiled.
+//
+// Ceres Developers: All options should have the same name as their mapped
+//                   CMake options, in the preconfigured version of this file
+//                   all options should be enclosed in '@'.
+
+#ifndef CERES_PUBLIC_INTERNAL_CONFIG_H_
+#define CERES_PUBLIC_INTERNAL_CONFIG_H_
+
+// If defined, use the LGPL code in Eigen.
+@CERES_USE_EIGEN_SPARSE@
+
+// If defined, Ceres was compiled without LAPACK.
+@CERES_NO_LAPACK@
+
+// If defined, Ceres was compiled without SuiteSparse.
+@CERES_NO_SUITESPARSE@
+
+// If defined, Ceres was compiled without CXSparse.
+@CERES_NO_CXSPARSE@
+
+// If defined, Ceres was compiled without Apple's Accelerate framework solvers.
+@CERES_NO_ACCELERATE_SPARSE@
+
+#if defined(CERES_NO_SUITESPARSE) &&              \
+    defined(CERES_NO_ACCELERATE_SPARSE) &&        \
+    defined(CERES_NO_CXSPARSE) &&                 \
+    !defined(CERES_USE_EIGEN_SPARSE)  // NOLINT
+// If defined Ceres was compiled without any sparse linear algebra support.
+#define CERES_NO_SPARSE
+#endif
+
+// If defined, Ceres was compiled without Schur specializations.
+@CERES_RESTRICT_SCHUR_SPECIALIZATION@
+
+// If defined, Ceres was compiled to use Eigen instead of hardcoded BLAS
+// routines.
+@CERES_NO_CUSTOM_BLAS@
+
+// If defined, Ceres was compiled without multithreading support.
+@CERES_NO_THREADS@
+// If defined Ceres was compiled with OpenMP multithreading support.
+@CERES_USE_OPENMP@
+// If defined Ceres was compiled with C++11 thread support.
+@CERES_USE_CXX11_THREADS@
+
+// If defined, Ceres was built as a shared library.
+@CERES_USING_SHARED_LIBRARY@
+
+// If defined, Ceres was compiled with a version MSVC >= 2005 which
+// deprecated the standard POSIX names for bessel functions, replacing them
+// with underscore prefixed versions (e.g. j0() -> _j0()).
+@CERES_MSVC_USE_UNDERSCORE_PREFIXED_BESSEL_FUNCTIONS@
+
+#endif  // CERES_PUBLIC_INTERNAL_CONFIG_H_
diff --git a/cmake/iOS.cmake b/cmake/iOS.cmake
new file mode 100644
index 0000000..bb42802
--- /dev/null
+++ b/cmake/iOS.cmake
@@ -0,0 +1,336 @@
+# This file is part of the ios-cmake project. It was retrieved from
+# https://github.com/cristeab/ios-cmake.git, which is a fork of
+# https://code.google.com/p/ios-cmake/. Which in turn is based off of
+# the Platform/Darwin.cmake and Platform/UnixPaths.cmake files which
+# are included with CMake 2.8.4
+#
+# The ios-cmake project is licensed under the new BSD license.
+#
+# Copyright (c) 2014, Bogdan Cristea and LTE Engineering Software,
+# Kitware, Inc., Insight Software Consortium.  All rights reserved.
+
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. 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.
+#
+# 3. Neither the name of the copyright holder 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 HOLDER 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.
+#
+# This file is based off of the Platform/Darwin.cmake and
+# Platform/UnixPaths.cmake files which are included with CMake 2.8.4
+# It has been altered for iOS development.
+#
+# Updated by Alex Stewart (alexs.mac@gmail.com).
+
+# The following variables control the behaviour of this toolchain:
+#
+# IOS_PLATFORM: OS (default) or SIMULATOR or SIMULATOR64
+#    OS = Build for iPhoneOS.
+#    SIMULATOR = Build for x86 i386 iPhone Simulator.
+#    SIMULATOR64 = Build for x86 x86_64 iPhone Simulator.
+# CMAKE_OSX_SYSROOT: Path to the iOS SDK to use.  By default this is
+#    automatically determined from IOS_PLATFORM and xcodebuild, but
+#    can also be manually specified (although this should not be required).
+# CMAKE_IOS_DEVELOPER_ROOT: Path to the Developer directory for the iOS platform
+#    being compiled for.  By default this is automatically determined from
+#    CMAKE_OSX_SYSROOT, but can also be manually specified (although this should
+#    not be required).
+#
+# This toolchain defines the following variables for use externally:
+#
+# XCODE_VERSION: Version number (not including Build version) of Xcode detected.
+# IOS_SDK_VERSION: Version of iOS SDK being used.
+# CMAKE_OSX_ARCHITECTURES: Architectures being compiled for (generated from
+#    IOS_PLATFORM).
+#
+# This toolchain defines the following macros for use externally:
+#
+# set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE)
+#   A convenience macro for setting xcode specific properties on targets
+#   example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1").
+#
+# find_host_package (PROGRAM ARGS)
+#   A macro used to find executable programs on the host system, not within the
+#   iOS environment.  Thanks to the android-cmake project for providing the
+#   command.
+
+# Get the Xcode version being used.
+execute_process(COMMAND xcodebuild -version
+  OUTPUT_VARIABLE XCODE_VERSION
+  ERROR_QUIET
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION "${XCODE_VERSION}")
+string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION "${XCODE_VERSION}")
+message(STATUS "Building with Xcode version: ${XCODE_VERSION}")
+
+# Default to building for iPhoneOS if not specified otherwise, and we cannot
+# determine the platform from the CMAKE_OSX_ARCHITECTURES variable.  The use
+# of CMAKE_OSX_ARCHITECTURES is such that try_compile() projects can correctly
+# determine the value of IOS_PLATFORM from the root project, as
+# CMAKE_OSX_ARCHITECTURES is propagated to them by CMake.
+if (NOT DEFINED IOS_PLATFORM)
+  if (CMAKE_OSX_ARCHITECTURES)
+    if (CMAKE_OSX_ARCHITECTURES MATCHES ".*arm.*")
+      set(IOS_PLATFORM "OS")
+    elseif (CMAKE_OSX_ARCHITECTURES MATCHES "i386")
+      set(IOS_PLATFORM "SIMULATOR")
+    elseif (CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")
+      set(IOS_PLATFORM "SIMULATOR64")
+    endif()
+  endif()
+  if (NOT IOS_PLATFORM)
+    set(IOS_PLATFORM "OS")
+  endif()
+endif()
+set(IOS_PLATFORM ${IOS_PLATFORM} CACHE STRING
+  "Type of iOS platform for which to build.")
+
+# Determine the platform name and architectures for use in xcodebuild commands
+# from the specified IOS_PLATFORM name.
+if (IOS_PLATFORM STREQUAL "OS")
+  set(XCODE_IOS_PLATFORM iphoneos)
+  set(IOS_ARCH armv7 armv7s arm64)
+elseif (IOS_PLATFORM STREQUAL "SIMULATOR")
+  set(XCODE_IOS_PLATFORM iphonesimulator)
+  set(IOS_ARCH i386)
+elseif(IOS_PLATFORM STREQUAL "SIMULATOR64")
+  set(XCODE_IOS_PLATFORM iphonesimulator)
+  set(IOS_ARCH x86_64)
+else()
+  message(FATAL_ERROR "Invalid IOS_PLATFORM: ${IOS_PLATFORM}")
+endif()
+
+# If user did not specify the SDK root to use, then query xcodebuild for it.
+if (NOT CMAKE_OSX_SYSROOT)
+  execute_process(COMMAND xcodebuild -version -sdk ${XCODE_IOS_PLATFORM} Path
+    OUTPUT_VARIABLE CMAKE_OSX_SYSROOT
+    ERROR_QUIET
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+  message(STATUS "Using SDK: ${CMAKE_OSX_SYSROOT} for platform: ${IOS_PLATFORM}")
+endif()
+if (NOT EXISTS ${CMAKE_OSX_SYSROOT})
+  message(FATAL_ERROR "Invalid CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT} "
+    "does not exist.")
+endif()
+# Get the SDK version information.
+execute_process(COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version SDKVersion
+  OUTPUT_VARIABLE IOS_SDK_VERSION
+  ERROR_QUIET
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+# Find the Developer root for the specific iOS platform being compiled for
+# from CMAKE_OSX_SYSROOT.  Should be ../../ from SDK specified in
+# CMAKE_OSX_SYSROOT.  There does not appear to be a direct way to obtain
+# this information from xcrun or xcodebuild.
+if (NOT CMAKE_IOS_DEVELOPER_ROOT)
+  get_filename_component(IOS_PLATFORM_SDK_DIR ${CMAKE_OSX_SYSROOT} PATH)
+  get_filename_component(CMAKE_IOS_DEVELOPER_ROOT ${IOS_PLATFORM_SDK_DIR} PATH)
+endif()
+if (NOT EXISTS ${CMAKE_IOS_DEVELOPER_ROOT})
+  message(FATAL_ERROR "Invalid CMAKE_IOS_DEVELOPER_ROOT: "
+    "${CMAKE_IOS_DEVELOPER_ROOT} does not exist.")
+endif()
+
+# Find the C & C++ compilers for the specified SDK.
+if (NOT CMAKE_C_COMPILER)
+  execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang
+    OUTPUT_VARIABLE CMAKE_C_COMPILER
+    ERROR_QUIET
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+  message(STATUS "Using C compiler: ${CMAKE_C_COMPILER}")
+endif()
+if (NOT CMAKE_CXX_COMPILER)
+  execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find clang++
+    OUTPUT_VARIABLE CMAKE_CXX_COMPILER
+    ERROR_QUIET
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+  message(STATUS "Using CXX compiler: ${CMAKE_CXX_COMPILER}")
+endif()
+
+# Find (Apple's) libtool.
+execute_process(COMMAND xcrun -sdk ${CMAKE_OSX_SYSROOT} -find libtool
+  OUTPUT_VARIABLE IOS_LIBTOOL
+  ERROR_QUIET
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+message(STATUS "Using libtool: ${IOS_LIBTOOL}")
+# Configure libtool to be used instead of ar + ranlib to build static libraries.
+# This is required on Xcode 7+, but should also work on previous versions of
+# Xcode.
+set(CMAKE_C_CREATE_STATIC_LIBRARY
+  "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
+set(CMAKE_CXX_CREATE_STATIC_LIBRARY
+  "${IOS_LIBTOOL} -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
+
+# Get the version of Darwin (OS X) of the host.
+execute_process(COMMAND uname -r
+  OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
+  ERROR_QUIET
+  OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+# Specify minimum version of deployment target.
+# Unless specified, the latest SDK version is used by default.
+set(IOS_DEPLOYMENT_TARGET "${IOS_SDK_VERSION}"
+    CACHE STRING "Minimum iOS version to build for." )
+message(STATUS "Building for minimum iOS version: ${IOS_DEPLOYMENT_TARGET}"
+               " (SDK version: ${IOS_SDK_VERSION})")
+if (NOT IOS_DEPLOYMENT_TARGET VERSION_LESS 11.0)
+  # iOS 11+ does not support 32-bit architectures (armv7).
+  foreach(ARCH ${IOS_ARCH})
+    if (ARCH MATCHES "armv7*")
+      message(STATUS "Removing iOS architecture: ${ARCH} from build as it is "
+        "not supported by the minimum iOS version to build for: "
+        "${IOS_DEPLOYMENT_TARGET} (iOS >= 11 requires 64-bit).")
+    else()
+      list(APPEND VALID_IOS_ARCH_FOR_SDK_VERSION ${ARCH})
+    endif()
+  endforeach()
+  set(IOS_ARCH ${VALID_IOS_ARCH_FOR_SDK_VERSION})
+endif()
+
+message(STATUS "Configuring iOS build for platform: ${IOS_PLATFORM}, "
+  "architecture(s): ${IOS_ARCH}")
+
+# Standard settings.
+set(CMAKE_SYSTEM_NAME Darwin)
+set(CMAKE_SYSTEM_VERSION ${IOS_SDK_VERSION})
+set(UNIX TRUE)
+set(APPLE TRUE)
+set(IOS TRUE)
+# Force unset of OS X-specific deployment target (otherwise autopopulated),
+# required as of cmake 2.8.10.
+set(CMAKE_OSX_DEPLOYMENT_TARGET "" CACHE STRING
+  "Must be empty for iOS builds." FORCE)
+# Set the architectures for which to build.
+set(CMAKE_OSX_ARCHITECTURES ${IOS_ARCH} CACHE STRING "Build architecture for iOS")
+
+# All iOS/Darwin specific settings - some may be redundant.
+set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
+set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
+set(CMAKE_SHARED_MODULE_PREFIX "lib")
+set(CMAKE_SHARED_MODULE_SUFFIX ".so")
+set(CMAKE_MODULE_EXISTS 1)
+set(CMAKE_DL_LIBS "")
+
+set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
+set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
+set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
+set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
+
+# Note that only Xcode 7+ supports the newer more specific:
+# -m${XCODE_IOS_PLATFORM}-version-min flags, older versions of Xcode use:
+# -m(ios/ios-simulator)-version-min instead.
+if (XCODE_VERSION VERSION_LESS 7.0)
+  if (IOS_PLATFORM STREQUAL "OS")
+    set(XCODE_IOS_PLATFORM_VERSION_FLAGS
+      "-mios-version-min=${IOS_DEPLOYMENT_TARGET}")
+  else()
+    # SIMULATOR or SIMULATOR64 both use -mios-simulator-version-min.
+    set(XCODE_IOS_PLATFORM_VERSION_FLAGS
+      "-mios-simulator-version-min=${IOS_DEPLOYMENT_TARGET}")
+  endif()
+else()
+  # Xcode 7.0+ uses flags we can build directly from XCODE_IOS_PLATFORM.
+  set(XCODE_IOS_PLATFORM_VERSION_FLAGS
+    "-m${XCODE_IOS_PLATFORM}-version-min=${IOS_DEPLOYMENT_TARGET}")
+endif()
+
+set(CMAKE_C_FLAGS
+  "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fobjc-abi-version=2 -fobjc-arc ${CMAKE_C_FLAGS}")
+# Hidden visibilty is required for C++ on iOS.
+set(CMAKE_CXX_FLAGS
+  "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden -fobjc-abi-version=2 -fobjc-arc ${CMAKE_CXX_FLAGS}")
+set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O3 -fomit-frame-pointer -ffast-math ${CMAKE_CXX_FLAGS_RELEASE}")
+
+set(CMAKE_C_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS} -Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
+set(CMAKE_CXX_LINK_FLAGS "${XCODE_IOS_PLATFORM_VERSION_FLAGS}  -Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
+
+# In order to ensure that the updated compiler flags are used in try_compile()
+# tests, we have to forcibly set them in the CMake cache, not merely set them
+# in the local scope.
+list(APPEND VARS_TO_FORCE_IN_CACHE
+  CMAKE_C_FLAGS
+  CMAKE_CXX_FLAGS
+  CMAKE_CXX_RELEASE
+  CMAKE_C_LINK_FLAGS
+  CMAKE_CXX_LINK_FLAGS)
+foreach(VAR_TO_FORCE ${VARS_TO_FORCE_IN_CACHE})
+  set(${VAR_TO_FORCE} "${${VAR_TO_FORCE}}" CACHE STRING "" FORCE)
+endforeach()
+
+set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
+set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
+set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
+set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
+set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
+set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
+
+# Hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old
+# build tree (where install_name_tool was hardcoded) and where
+# CMAKE_INSTALL_NAME_TOOL isn't in the cache and still cmake didn't fail in
+# CMakeFindBinUtils.cmake (because it isn't rerun) hardcode
+# CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did
+# before, Alex.
+if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
+  find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
+endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
+
+# Set the find root to the iOS developer roots and to user defined paths.
+set(CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_OSX_SYSROOT}
+  ${CMAKE_PREFIX_PATH} CACHE string  "iOS find search path root" FORCE)
+
+# Default to searching for frameworks first.
+set(CMAKE_FIND_FRAMEWORK FIRST)
+
+# Set up the default search directories for frameworks.
+set(CMAKE_SYSTEM_FRAMEWORK_PATH
+  ${CMAKE_OSX_SYSROOT}/System/Library/Frameworks
+  ${CMAKE_OSX_SYSROOT}/System/Library/PrivateFrameworks
+  ${CMAKE_OSX_SYSROOT}/Developer/Library/Frameworks)
+
+# Only search the specified iOS SDK, not the remainder of the host filesystem.
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+# This little macro lets you set any XCode specific property.
+macro(set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
+  set_property(TARGET ${TARGET} PROPERTY
+    XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
+endmacro(set_xcode_property)
+
+# This macro lets you find executable programs on the host system.
+macro(find_host_package)
+  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
+  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
+  set(IOS FALSE)
+
+  find_package(${ARGN})
+
+  set(IOS TRUE)
+  set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
+  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+  set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+endmacro(find_host_package)
diff --git a/cmake/uninstall.cmake.in b/cmake/uninstall.cmake.in
new file mode 100644
index 0000000..124deb1
--- /dev/null
+++ b/cmake/uninstall.cmake.in
@@ -0,0 +1,92 @@
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2015 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# 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: arnaudgelas@gmail.com (Arnaud Gelas)
+#         alexs.mac@gmail.com (Alex Stewart)
+
+if (NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+  message(FATAL_ERROR "Cannot find install manifest: "
+                      "\"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
+endif (NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+
+file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" INSTALL_MANIFEST)
+string(REGEX REPLACE "\n" ";" INSTALL_MANIFEST "${INSTALL_MANIFEST}")
+list(REVERSE INSTALL_MANIFEST)
+
+foreach (INSTALLED_FILE ${INSTALL_MANIFEST})
+  # Save the root ceres include install directory, e.g. /usr/local/include/ceres
+  # so that we can remove it at the end.
+  if (NOT CERES_INCLUDE_INSTALL_ROOT)
+    get_filename_component(FILE_NAME ${INSTALLED_FILE} NAME)
+    if (FILE_NAME STREQUAL ceres.h)
+      # Ensure that the directory is nested as we expect, as we are going to
+      # remove it, and we do not want to remove files pertaining to anyone else.
+      get_filename_component(PARENT_DIR ${INSTALLED_FILE} PATH)
+      get_filename_component(PARENT_DIR_NAME ${PARENT_DIR} NAME)
+      if (PARENT_DIR_NAME STREQUAL ceres AND IS_DIRECTORY ${PARENT_DIR})
+        set(CERES_INCLUDE_INSTALL_ROOT ${PARENT_DIR})
+      endif (PARENT_DIR_NAME STREQUAL ceres AND IS_DIRECTORY ${PARENT_DIR})
+    endif (FILE_NAME STREQUAL ceres.h)
+  endif (NOT CERES_INCLUDE_INSTALL_ROOT)
+
+  message(STATUS "Uninstalling \"$ENV{DESTDIR}${INSTALLED_FILE}\"")
+  if (EXISTS "$ENV{DESTDIR}${INSTALLED_FILE}")
+    execute_process(COMMAND @CMAKE_COMMAND@
+                    -E remove "$ENV{DESTDIR}${INSTALLED_FILE}"
+                    OUTPUT_VARIABLE RM_OUT
+                    RESULT_VARIABLE RM_RETVAL)
+    if (NOT ${RM_RETVAL} EQUAL 0)
+      message(FATAL_ERROR
+              "Problem when removing \"$ENV{DESTDIR}${INSTALLED_FILE}\"")
+    endif (NOT ${RM_RETVAL} EQUAL 0)
+  else (EXISTS "$ENV{DESTDIR}${INSTALLED_FILE}")
+    message(STATUS "File \"$ENV{DESTDIR}${INSTALLED_FILE}\" does not exist.")
+  endif (EXISTS "$ENV{DESTDIR}${INSTALLED_FILE}")
+endforeach(INSTALLED_FILE)
+
+# Removing Ceres include install directory.
+if (CERES_INCLUDE_INSTALL_ROOT AND
+    EXISTS ${CERES_INCLUDE_INSTALL_ROOT})
+  message(STATUS "Removing Ceres include install directory: "
+                 "\"$ENV{DESTDIR}${CERES_INCLUDE_INSTALL_ROOT}\"")
+  execute_process(COMMAND @CMAKE_COMMAND@
+                  -E remove_directory
+                  "$ENV{DESTDIR}${CERES_INCLUDE_INSTALL_ROOT}"
+                  OUTPUT_VARIABLE RM_OUT
+                  RESULT_VARIABLE RM_RETVAL)
+  if (NOT ${RM_RETVAL} EQUAL 0)
+    message(FATAL_ERROR
+      "Failed to remove: \"$ENV{DESTDIR}${CERES_INCLUDE_INSTALL_ROOT\"")
+  endif (NOT ${RM_RETVAL} EQUAL 0)
+else (CERES_INCLUDE_INSTALL_ROOT AND
+    EXISTS ${CERES_INCLUDE_INSTALL_ROOT})
+  message(FATAL_ERROR "Failed to find Ceres installed include directory "
+                      "(e.g. /usr/local/include/ceres), candidate: "
+                      "\"$ENV{DESTDIR}${CERES_INCLUDE_INSTALL_ROOT}\"")
+endif (CERES_INCLUDE_INSTALL_ROOT AND
+  EXISTS ${CERES_INCLUDE_INSTALL_ROOT})