Cleaning up exported CeresConfig.cmake.

- Adding FindPackage scripts for all of Ceres dependencies.
- Moving depend.cmake contents to CeresConfig.cmake and cleaning up
  search for Ceres & required dependencies, no longer push Ceres
  options into client.
- Fixing uninstall to remove ceres include root directory.
- Fixing main CMakeLists to install miniglog header if enabled.
- Making miniglog library shared/static with Ceres library.

Change-Id: If926bebd11720230c5136597ccba672394ed9777
diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in
index d000046..6044700 100644
--- a/cmake/CeresConfig.cmake.in
+++ b/cmake/CeresConfig.cmake.in
@@ -26,24 +26,161 @@
 # 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)
+# Authors: pablo.speciale@gmail.com (Pablo Speciale)
+#          alexs.mac@gmail.com (Alex Stewart)
 #
 
-# Get the directory
-GET_FILENAME_COMPONENT(currentDir ${CMAKE_CURRENT_LIST_FILE} PATH)
+# Config file for Ceres Solver - Find Ceres & dependencies.
+#
+# This file is used by CMake when FIND_PACKAGE( Ceres ) is invoked (and
+# the directory containing this file is present in CMAKE_MODULE_PATH).
+#
+# This module defines the following variables:
+#
+# CERES_VERSION
+# CERES_INCLUDE_DIRS: Include directories for Ceres and the dependencies which
+#                     appear in the Ceres public API and are thus required to
+#                     use Ceres.
+# 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.
+#
+# The following variables are also defined for legacy compatibility only.
+# Any new code should not use them as they do not conform to the standard CMake
+# FindPackage naming conventions.
+#
+# CERES_INCLUDES = ${CERES_INCLUDE_DIRS}.
 
-# Get the chosen install prefix
-GET_FILENAME_COMPONENT(rootDir ${currentDir}/@relInstallDir@ ABSOLUTE)
+# Called if we failed to find Ceres 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(CERES_REPORT_NOT_FOUND REASON_MSG)
+  UNSET(CERES_FOUND)
+  UNSET(CERES_INCLUDE_DIRS)
+  UNSET(CERES_LIBRARIES)
 
-# Set the version
+  # 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})
+  ELSE (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)
+
+# Get the (current, i.e. installed) directory containing this file.
+SET(CURRENT_CONFIG_INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+# 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})
+# 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 ${CURRENT_CONFIG_INSTALL_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
+  ${CURRENT_CONFIG_INSTALL_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 CeresConfg.cmake install location: "
+    "${CURRENT_CONFIG_INSTALL_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})
+
+# Set the version.
 SET(CERES_VERSION @CERES_VERSION@ )
 
-# What is my include directory
-SET(CERES_INCLUDES "${rootDir}/@INCLUDE_INSTALL_DIR@")
+# Set the include directories for Ceres (itself).
+SET(CERES_INCLUDE_DIR "${CURRENT_ROOT_INSTALL_DIR}/@INCLUDE_INSTALL_DIR@")
+IF (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
+  CERES_REPORT_NOT_FOUND(
+    "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
+    "determined from relative path from CeresConfg.cmake install location: "
+    "${CURRENT_CONFIG_INSTALL_DIR}, does not contain Ceres headers. "
+    "Either the install directory was deleted, or the install tree was only "
+    "partially relocated outside of CMake after Ceres was built.")
+ENDIF (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
 
-# Import the exported targets
-INCLUDE(${currentDir}/CeresTargets.cmake)
-INCLUDE(${currentDir}/depend.cmake)
+# Append the include directories for all (potentially optional) dependencies
+# with which Ceres was compiled, the libraries themselves come in via
+# CeresTargets-<release/debug>.cmake as link libraries for Ceres target.
+SET(CERES_INCLUDE_DIRS ${CERES_INCLUDE_DIR})
 
-# Set the expected library variable
+# Eigen.
+# Flag set during configuration and build of Ceres.
+SET(CERES_EIGEN_VERSION @EIGEN_VERSION@)
+# Search quietly s/t we control the timing of the error message if not found.
+FIND_PACKAGE(Eigen ${CERES_EIGEN_VERSION} EXACT QUIET)
+IF (EIGEN_FOUND)
+  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)
+LIST(APPEND CERES_INCLUDE_DIRS ${EIGEN_INCLUDE_DIRS})
+
+# Glog.
+# Flag set during configuration and build of Ceres.
+SET(CERES_USES_MINIGLOG @MINIGLOG@)
+IF (CERES_USES_MINIGLOG)
+  SET(MINIGLOG_INCLUDE_DIR ${CERES_INCLUDE_DIR}/ceres/internal/miniglog)
+  IF (NOT EXISTS ${MINIGLOG_INCLUDE_DIR})
+    CERES_REPORT_NOT_FOUND(
+      "Ceres install include directory: "
+      "${CERES_INCLUDE_DIR} does not include miniglog, but Ceres was "
+      "compiled with MINIGLOG enabled (in place of Glog).")
+  ENDIF (NOT EXISTS ${MINIGLOG_INCLUDE_DIR})
+  LIST(APPEND CERES_INCLUDE_DIRS ${MINIGLOG_INCLUDE_DIR})
+  # 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 installation compiled with miniglog substitute "
+    "for glog, beware this will likely cause problems if glog is later linked.")
+ELSE (CERES_USES_MINIGLOG)
+  # 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 in ${GLOG_INCLUDE_DIRS}")
+  ELSE (GLOG_FOUND)
+    CERES_REPORT_NOT_FOUND("Missing required Ceres "
+      "dependency: Glog, please set GLOG_INCLUDE_DIR.")
+  ENDIF (GLOG_FOUND)
+  LIST(APPEND CERES_INCLUDE_DIRS ${GLOG_INCLUDE_DIRS})
+ENDIF (CERES_USES_MINIGLOG)
+
+# Import exported Ceres targets.
+IF (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
+  INCLUDE(${CURRENT_CONFIG_INSTALL_DIR}/CeresTargets.cmake)
+ENDIF (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
+# Set the expected XX_LIBRARIES variable for FindPackage().
 SET(CERES_LIBRARIES ceres)
+
+# Set legacy include directories variable for backwards compatibility.
+SET(CERES_INCLUDES ${CERES_INCLUDE_DIRS})
+
+# Reset CMake module path to its state when this script was called.
+SET(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
+
+# 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 version: ${CERES_VERSION} "
+    "installed in: ${CURRENT_ROOT_INSTALL_DIR}")