Make gflags a public dependency of Ceres if it and glog are found.

- Previously we were not listing gflags as a public dependency of Ceres
  if it and glog were found (and MINIGLOG was not being used). This
  does not reflect that if glog was compiled with gflags then it will
  #include gflags/gflags.h in glog/logging.h, thus making gflags a
  public dependency of anything linking against glog.
- On *nix OSs if glog/gflags are shared libraries this did not result
  in a link error when compiling Ceres as the gflags symbols were
  indirectly resolved.  However, on MSVC this is not the case, and this
  could result in unresolved gflags symbol link errors when compiling
  Ceres.
- Now we add gflags to the list of public Ceres dependencies if both
  glog and gflags are found (and MINIGLOG is not enabled).

Change-Id: I5ce6038fa816781cc81b378522068dc563d29c51
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 97a1726..89d0bf0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -348,6 +348,7 @@
   if (GFLAGS_FOUND)
     message("-- Found Google Flags header in: ${GFLAGS_INCLUDE_DIRS}, "
       "in namespace: ${GFLAGS_NAMESPACE}")
+    add_definitions(-DCERES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
   else (GFLAGS_FOUND)
     message("-- Did not find Google Flags (gflags), Building without gflags "
       "- no tests or tools will be built!")
@@ -386,6 +387,18 @@
       "GLOG_LIBRARY or enable MINIGLOG option to use minimal glog "
       "implementation.")
   endif(NOT GLOG_FOUND)
+  # By default, assume gflags was found, updating the message if it was not.
+  set(GLOG_GFLAGS_DEPENDENCY_MESSAGE
+    " Assuming glog was built with gflags support as gflags was found. "
+    "This will make gflags a public dependency of Ceres.")
+  if (NOT GFLAGS_FOUND)
+    set(GLOG_GFLAGS_DEPENDENCY_MESSAGE
+      " Assuming glog was NOT built with gflags support as gflags was "
+      "not found.  If glog was built with gflags, please set the "
+      "gflags search locations such that it can be found by Ceres.  "
+      "Otherwise, Ceres may fail to link due to missing gflags symbols.")
+  endif(NOT GFLAGS_FOUND)
+  message("-- Found Google Log (glog)." ${GLOG_GFLAGS_DEPENDENCY_MESSAGE})
 endif (MINIGLOG)
 
 if (NOT SCHUR_SPECIALIZATIONS)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index b589b45..eef5e5e 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -73,11 +73,6 @@
 target_link_libraries(simple_bundle_adjuster ceres)
 
 if (GFLAGS)
-  # The CERES_GFLAGS_NAMESPACE compile definition is NOT stored in
-  # CERES_COMPILE_OPTIONS (and thus config.h) as Ceres itself does not
-  # require gflags, only the tests and examples do.
-  add_definitions(-DCERES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
-
   add_executable(powell powell.cc)
   target_link_libraries(powell ceres ${GFLAGS_LIBRARIES})
 
@@ -115,4 +110,4 @@
 endif (GFLAGS)
 
 add_subdirectory(sampled_function)
-add_subdirectory(slam)
\ No newline at end of file
+add_subdirectory(slam)
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt
index c352e0e..bdcbdd1 100644
--- a/internal/ceres/CMakeLists.txt
+++ b/internal/ceres/CMakeLists.txt
@@ -132,6 +132,14 @@
 # Build the list of dependencies for Ceres based on the current configuration.
 if (NOT MINIGLOG AND GLOG_FOUND)
   list(APPEND CERES_LIBRARY_PUBLIC_DEPENDENCIES ${GLOG_LIBRARIES})
+  if (GFLAGS_FOUND)
+    # If glog & gflags are both found, we assume that glog was built with
+    # gflags, as it is awkward to perform a try_compile() to verify this
+    # when gflags is an imported target (as it is in newer versions).
+    # As glog #includes gflags/gflags.h in glog/logging.h if compiled with
+    # gflags, it is thus a public dependency for Ceres in this case.
+    list(APPEND CERES_LIBRARY_PUBLIC_DEPENDENCIES ${GFLAGS_LIBRARIES})
+  endif()
 endif (NOT MINIGLOG AND GLOG_FOUND)
 
 if (SUITESPARSE AND SUITESPARSE_FOUND)
@@ -242,11 +250,6 @@
         ARCHIVE DESTINATION lib${LIB_SUFFIX})
 
 if (BUILD_TESTING AND GFLAGS)
-  # The CERES_GFLAGS_NAMESPACE compile definition is NOT stored in
-  # CERES_COMPILE_OPTIONS (and thus config.h) as Ceres itself does not
-  # require gflags, only the tests and examples do.
-  add_definitions(-DCERES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
-
   add_library(gtest gmock_gtest_all.cc gmock_main.cc)
   if (BUILD_SHARED_LIBS)
     # Define gtest-specific shared library flags for compilation.