Bypass Ceres' FindGlog.cmake in CeresConfig.cmake if possible

- If the version of glog detected and used to build Ceres was built with
  CMake (i.e. Config mode) then we now use Config mode directly in
  CeresConfig.cmake and do not install Ceres' FindGlog.cmake module.
- This has the benefit of removing any hard-coded paths from
  CeresConfig.cmake provided that all dependencies were also built with
  CMake.

Change-Id: I85af8a953fd6d300e8bc0cdeb0b3636fec182f68
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2a6b376..ea39ad4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -759,8 +759,13 @@
         RENAME CeresConfig.cmake
         DESTINATION ${RELATIVE_CMAKECONFIG_INSTALL_DIR})
 install(FILES "${Ceres_BINARY_DIR}/CeresConfigVersion.cmake"
-              "${Ceres_SOURCE_DIR}/cmake/FindGlog.cmake"
         DESTINATION ${RELATIVE_CMAKECONFIG_INSTALL_DIR})
+if (GLOG_FOUND AND NOT FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION)
+  # Version of glog detected was not built with CMake, install our glog module
+  # file to enable detection in CeresConfig.
+  install(FILES "${Ceres_SOURCE_DIR}/cmake/FindGlog.cmake"
+          DESTINATION ${RELATIVE_CMAKECONFIG_INSTALL_DIR})
+endif()
 
 if (PROVIDE_UNINSTALL_TARGET)
   # Create an uninstall target to remove all installed files.
diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in
index 7ad5bed..81c0511 100644
--- a/cmake/CeresConfig.cmake.in
+++ b/cmake/CeresConfig.cmake.in
@@ -178,6 +178,13 @@
 include(CMakeFindDependencyMacro)
 find_dependency(Threads)
 
+# 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.
+
 # Eigen.
 # Flag set during configuration and build of Ceres.
 set(CERES_EIGEN_VERSION @EIGEN3_VERSION_STRING@)
@@ -205,42 +212,41 @@
     "Eigen3_DIR.")
 endif (EIGEN3_FOUND)
 
-# Glog.
-# Flag set during configuration and build of Ceres.
+# glog (and maybe gflags).
+#
+# Flags set during configuration and build of Ceres.
 set(CERES_USES_MINIGLOG @MINIGLOG@)
+set(CERES_GLOG_VERSION @glog_VERSION@)
+set(CERES_GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
+
 set(CERES_USES_GFLAGS @GFLAGS@)
 set(CERES_GFLAGS_VERSION @gflags_VERSION@)
+
 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.
   ceres_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)
+  if (CERES_GLOG_WAS_BUILT_WITH_CMAKE)
+    find_package(glog ${CERES_GLOG_VERSION} CONFIG QUIET)
+    set(GLOG_FOUND ${glog_FOUND})
   else()
+    # Version of glog against which Ceres was built was not built with CMake,
+    # use the exported glog find_package() module from Ceres to find it again.
+    # Append the locations of glog when Ceres was built to the search path hints.
     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})
+
+    # Search quietly s/t we control the timing of the error message if not found.
+    find_package(Glog QUIET)
   endif()
-  # Search quietly s/t we control the timing of the error message if not found.
-  find_package(Glog QUIET)
+
   if (GLOG_FOUND)
     ceres_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}.")
+    ceres_report_not_found("Missing required Ceres dependency: glog.")
   endif()
 
   # gflags is only a public dependency of Ceres via glog, thus is not required