Handle possible presence of library prefixes in MSVC.

- On MSVC, a Ceres dependency such as glog, may be named glog.lib, or
  libglog.lib.  By default, CMake assumes no prefix for libraries on
  MSVC when using find_library(), thus find_library(glog) would fail
  if glog was named libglog.lib.
- This patch caches & updates CMAKE_FIND_LIBRARY_PREFIXES in all of
  Ceres' find_package scripts to include lib & "" (no prefix) on MSVC
  and then returns CMAKE_FIND_LIBRARY_PREFIXES to its original state
  before returning.

Change-Id: Ic82799e3b786cfb7228a51183bc189578b072bbe
diff --git a/cmake/FindCXSparse.cmake b/cmake/FindCXSparse.cmake
index 8ffbae9..5eef530 100644
--- a/cmake/FindCXSparse.cmake
+++ b/cmake/FindCXSparse.cmake
@@ -67,6 +67,14 @@
 # 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.
@@ -78,6 +86,9 @@
   # 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)
@@ -91,6 +102,17 @@
   ENDIF ()
 ENDMACRO(CXSPARSE_REPORT_NOT_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.
 #
@@ -190,6 +212,8 @@
   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
diff --git a/cmake/FindGflags.cmake b/cmake/FindGflags.cmake
index 959b4e3..b144a05 100644
--- a/cmake/FindGflags.cmake
+++ b/cmake/FindGflags.cmake
@@ -65,6 +65,14 @@
 # GFLAGS_LIBRARY: gflags library, not including the libraries of any
 #                 dependencies.
 
+# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
+# FindGflags was invoked.
+MACRO(GFLAGS_RESET_FIND_LIBRARY_PREFIX)
+  IF (MSVC)
+    SET(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  ENDIF (MSVC)
+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.
@@ -82,6 +90,9 @@
   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)
@@ -177,6 +188,17 @@
   ENDIF()
 ENDMACRO()
 
+# 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.
 #
@@ -396,6 +418,8 @@
   SET(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} ${GFLAGS_LINK_LIBRARIES})
 ENDIF (GFLAGS_FOUND)
 
+GFLAGS_RESET_FIND_LIBRARY_PREFIX()
+
 # Handle REQUIRED / QUIET optional arguments.
 INCLUDE(FindPackageHandleStandardArgs)
 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Gflags DEFAULT_MSG
diff --git a/cmake/FindGlog.cmake b/cmake/FindGlog.cmake
index 9759c6b..3180c1f 100644
--- a/cmake/FindGlog.cmake
+++ b/cmake/FindGlog.cmake
@@ -60,6 +60,14 @@
 # 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)
+    SET(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
+  ENDIF (MSVC)
+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.
@@ -71,6 +79,9 @@
   # 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)
@@ -84,6 +95,17 @@
   ENDIF ()
 ENDMACRO(GLOG_REPORT_NOT_FOUND)
 
+# 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.
 #
@@ -159,6 +181,8 @@
   SET(GLOG_LIBRARIES ${GLOG_LIBRARY})
 ENDIF (GLOG_FOUND)
 
+GLOG_RESET_FIND_LIBRARY_PREFIX()
+
 # Handle REQUIRED / QUIET optional arguments.
 INCLUDE(FindPackageHandleStandardArgs)
 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Glog DEFAULT_MSG
diff --git a/cmake/FindSuiteSparse.cmake b/cmake/FindSuiteSparse.cmake
index 881786a..3b5e814 100644
--- a/cmake/FindSuiteSparse.cmake
+++ b/cmake/FindSuiteSparse.cmake
@@ -110,6 +110,14 @@
 # TBB_FOUND
 # TBB_LIBRARIES
 
+# 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.
@@ -125,6 +133,8 @@
   # 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)
@@ -140,6 +150,17 @@
   # Do not call RETURN(), s/t we keep processing if not called with REQUIRED.
 ENDMACRO(SUITESPARSE_REPORT_NOT_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
@@ -611,6 +632,8 @@
 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)