Replace use of GFLAGS_LIBRARIES with export gflags target

- As our minimum required version of gflags (2.2) exports itself as
  a CMake package and this is the case for the default 18.04 package
  we can use the gflags target directly.
- Replaces forced use of CONFIG in find_package(gflags) with a check
  that the gflags imported target exists to avoid ambiguity with
  libgflags if installed in a default location.  This permits users to
  override the gflags detection should they so choose, provided that
  they do so via an imported target.
- Also removes some previously removed legacy GLAGS_ vars from the
  installation docs.

Change-Id: I015f5a751e5b22f956bbf9df692e63a6825c9f0d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 948bddb..7354c43 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -391,25 +391,24 @@
 # GFlags.
 if (GFLAGS)
   # Don't search with REQUIRED as we can continue without gflags.
-  find_package(gflags 2.2.0 CONFIG
-    HINTS "${HOMEBREW_INCLUDE_DIR_HINTS}")
+  find_package(gflags 2.2.0 HINTS "${HOMEBREW_INCLUDE_DIR_HINTS}")
   if (gflags_FOUND)
-    message("-- Found Google Flags version ${gflags_VERSION}: ${GFLAGS_INCLUDE_DIR}")
+    if (TARGET gflags)
+      message("-- Found Google Flags (gflags) version ${gflags_VERSION}: ${gflags_DIR}")
+    else()
+      message("-- Detected version of gflags: ${gflags_VERSION} does not define "
+        "expected gflags CMake target which should be exported by gflags 2.2+. "
+        "Building without gflags.")
+      update_cache_variable(GFLAGS OFF)
+    endif()
   else (gflags_FOUND)
-    message("-- Did not find Google Flags (gflags), Building without gflags "
-      "- no tests or tools will be built!")
+    message("-- Did not find Google Flags (gflags), Building without gflags.")
     update_cache_variable(GFLAGS OFF)
   endif (gflags_FOUND)
-else (GFLAGS)
-  message("-- Google Flags disabled; no tests or tools will be built!")
-  # Mark as advanced (remove from default GUI view) the gflags search
-  # variables in case user enabled GFLAGS, FindGflags did not find it, so
-  # made search variables visible in GUI for user to set, but then user disables
-  # GFLAGS instead of setting them.
-  mark_as_advanced(FORCE GFLAGS_INCLUDE_DIR
-                         GFLAGS_LIBRARY
-                         GFLAGS_NAMESPACE)
-endif (GFLAGS)
+endif()
+if (NOT GFLAGS)
+  message("-- Use of gflags disabled - no tests or tools will be built!")
+endif()
 
 # MiniGLog.
 if (MINIGLOG)
diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in
index 81c0511..ae94833 100644
--- a/cmake/CeresConfig.cmake.in
+++ b/cmake/CeresConfig.cmake.in
@@ -253,12 +253,12 @@
   # if Ceres was built with MINIGLOG.
   if (CERES_USES_GFLAGS)
     # Search quietly s/t we control the timing of the error message if not found.
-    find_package(gflags ${CERES_GFLAGS_VERSION} CONFIG QUIET)
-    if (gflags_FOUND)
+    find_package(gflags ${CERES_GFLAGS_VERSION} QUIET)
+    if (gflags_FOUND AND TARGET gflags)
       ceres_message(STATUS "Found required Ceres dependency: gflags")
     else()
       ceres_report_not_found("Missing required Ceres "
-        "dependency: gflags.")
+        "dependency: gflags (not found, or not found as exported CMake target).")
     endif()
   endif()
 endif(CERES_USES_MINIGLOG)
diff --git a/docs/source/installation.rst b/docs/source/installation.rst
index 2d844cb..db12cce 100644
--- a/docs/source/installation.rst
+++ b/docs/source/installation.rst
@@ -447,8 +447,6 @@
    #. ``EIGEN_INCLUDE_DIR_HINTS``
    #. ``GLOG_INCLUDE_DIR_HINTS``
    #. ``GLOG_LIBRARY_DIR_HINTS``
-   #. ``GFLAGS_INCLUDE_DIR_HINTS``
-   #. ``GFLAGS_LIBRARY_DIR_HINTS``
    #. (Optional) ``SUITESPARSE_INCLUDE_DIR_HINTS``
    #. (Optional) ``SUITESPARSE_LIBRARY_DIR_HINTS``
    #. (Optional) ``CXSPARSE_INCLUDE_DIR_HINTS``
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 27e736a..7f9b117 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -72,41 +72,41 @@
 
 if (GFLAGS)
   add_executable(powell powell.cc)
-  target_link_libraries(powell Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(powell Ceres::ceres gflags)
 
   add_executable(nist nist.cc)
-  target_link_libraries(nist Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(nist Ceres::ceres gflags)
   if (MSVC)
     target_compile_options(nist PRIVATE "/bigobj")
   endif()
 
   add_executable(more_garbow_hillstrom more_garbow_hillstrom.cc)
-  target_link_libraries(more_garbow_hillstrom Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(more_garbow_hillstrom Ceres::ceres gflags)
 
   add_executable(circle_fit circle_fit.cc)
-  target_link_libraries(circle_fit Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(circle_fit Ceres::ceres gflags)
 
   add_executable(bundle_adjuster
                  bundle_adjuster.cc
                  bal_problem.cc)
-  target_link_libraries(bundle_adjuster Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(bundle_adjuster Ceres::ceres gflags)
 
   add_executable(libmv_bundle_adjuster
                  libmv_bundle_adjuster.cc)
-  target_link_libraries(libmv_bundle_adjuster Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(libmv_bundle_adjuster Ceres::ceres gflags)
 
   add_executable(libmv_homography
                  libmv_homography.cc)
-  target_link_libraries(libmv_homography Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(libmv_homography Ceres::ceres gflags)
 
   add_executable(denoising
                  denoising.cc
                  fields_of_experts.cc)
-  target_link_libraries(denoising Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(denoising Ceres::ceres gflags)
 
   add_executable(robot_pose_mle
                  robot_pose_mle.cc)
-  target_link_libraries(robot_pose_mle Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(robot_pose_mle Ceres::ceres gflags)
 
 endif (GFLAGS)
 
diff --git a/examples/slam/pose_graph_2d/CMakeLists.txt b/examples/slam/pose_graph_2d/CMakeLists.txt
index 2b35c7f..20af056 100644
--- a/examples/slam/pose_graph_2d/CMakeLists.txt
+++ b/examples/slam/pose_graph_2d/CMakeLists.txt
@@ -35,5 +35,5 @@
     pose_graph_2d.cc
     pose_graph_2d_error_term.h
     types.h)
-  target_link_libraries(pose_graph_2d Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(pose_graph_2d Ceres::ceres gflags)
 endif (GFLAGS)
diff --git a/examples/slam/pose_graph_3d/CMakeLists.txt b/examples/slam/pose_graph_3d/CMakeLists.txt
index b75e2ce..b6421cc 100644
--- a/examples/slam/pose_graph_3d/CMakeLists.txt
+++ b/examples/slam/pose_graph_3d/CMakeLists.txt
@@ -30,5 +30,5 @@
 
 if (GFLAGS)
   add_executable(pose_graph_3d pose_graph_3d.cc)
-  target_link_libraries(pose_graph_3d Ceres::ceres ${GFLAGS_LIBRARIES})
+  target_link_libraries(pose_graph_3d Ceres::ceres gflags)
 endif (GFLAGS)