Move C++11 checks to after external find_package() calls.

- Testing for C++11, and subsequent testing for components using C++11
  requires adding -std=c++11 to CMAKE_REQUIRED_FLAGS used by the
  check_cxx_source_compiles() (and similar) macros if required by the
  compiler.
- This variable is also used for the C equivalent macros, which are used
  by find_package(BLAS), and would cause the detection to fail if
  present.
- Thus if CXX11 was forced ON via -D (not via the GUI) then detection
  would fail (with the GUI an initial configure without CXX11 would
  initialise the variables and no errors would occur in later
  reconfigures).

Change-Id: I08d300baf3730e926fb8e853a384badcceefeaf5
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 325ce97..02c72b5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -208,87 +208,6 @@
 
 unset(CERES_COMPILE_OPTIONS)
 
-# Initialise CMAKE_REQUIRED_FLAGS used by CheckCXXSourceCompiles with the
-# contents of CMAKE_CXX_FLAGS such that if the user has passed extra flags
-# they are used when discovering shared_ptr/unordered_map.
-set(CMAKE_REQUIRED_FLAGS ${CMAKE_CXX_FLAGS})
-include(CheckCXXCompilerFlag)
-check_cxx_compiler_flag("-std=c++11" COMPILER_HAS_CXX11_FLAG)
-if (CXX11 AND COMPILER_HAS_CXX11_FLAG)
-  # Update CMAKE_REQUIRED_FLAGS used by CheckCXXSourceCompiles to include
-  # -std=c++11 s/t we will detect the C++11 versions of unordered_map &
-  # shared_ptr if they exist.
-  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
-endif (CXX11 AND COMPILER_HAS_CXX11_FLAG)
-
-# Set the Ceres compile definitions for the unordered_map configuration.
-include(FindUnorderedMap)
-find_unordered_map()
-if (UNORDERED_MAP_FOUND)
-  if (HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
-    list(APPEND CERES_COMPILE_OPTIONS CERES_STD_UNORDERED_MAP)
-  endif(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
-  if (HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
-    list(APPEND CERES_COMPILE_OPTIONS CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
-  endif(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
-  if (HAVE_TR1_UNORDERED_MAP_IN_TR1_NAMESPACE)
-    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_UNORDERED_MAP)
-  endif(HAVE_TR1_UNORDERED_MAP_IN_TR1_NAMESPACE)
-else (UNORDERED_MAP_FOUND)
-  message("-- Replacing unordered_map/set with map/set (warning: slower!), "
-    "try enabling CXX11 option if you expect C++11 to be available.")
-  list(APPEND CERES_COMPILE_OPTIONS CERES_NO_UNORDERED_MAP)
-endif()
-
-# Set the Ceres compile definitions for the shared_ptr configuration.
-include(FindSharedPtr)
-find_shared_ptr()
-if (SHARED_PTR_FOUND)
-  if (SHARED_PTR_TR1_MEMORY_HEADER)
-    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_MEMORY_HEADER)
-  endif (SHARED_PTR_TR1_MEMORY_HEADER)
-  if (SHARED_PTR_TR1_NAMESPACE)
-    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_SHARED_PTR)
-  endif (SHARED_PTR_TR1_NAMESPACE)
-else (SHARED_PTR_FOUND)
-  message(FATAL_ERROR "Unable to find shared_ptr, try enabling CXX11 option "
-    "if you expect C++11 to be available.")
-endif (SHARED_PTR_FOUND)
-
-include(FindCXX11MathFunctions)
-find_cxx11_math_functions()
-if (CXX11 AND NOT CXX11_MATH_FUNCTIONS_FOUND)
-  message("-- Failed to find C++11 math functions (cbrt(), exp2() etc). "
-    "Disabling C++11.")
-  update_cache_variable(CXX11 OFF)
-endif()
-
-# To ensure that CXX11 accurately reflects whether we are using C++11,
-# check if it is required given where the potentially C++11 features Ceres
-# uses were found, and disable it if C++11 is not being used.
-if (CXX11)
-  if (NOT HAVE_SHARED_PTR_IN_STD_NAMESPACE AND
-      NOT HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
-    message("-- Failed to find C++11 components in C++11 locations & "
-      "namespaces, disabling CXX11.")
-    update_cache_variable(CXX11 OFF)
-  else()
-    message("   ==============================================================")
-    message("   Compiling Ceres using C++11.  This will result in a version ")
-    message("   of Ceres that will require the use of C++11 in client code.")
-    message("   ==============================================================")
-    list(APPEND CERES_COMPILE_OPTIONS CERES_USE_CXX11)
-    if (COMPILER_HAS_CXX11_FLAG AND
-        CMAKE_VERSION VERSION_LESS "2.8.12")
-      # For CMake versions > 2.8.12, the C++11 dependency is rolled into the
-      # Ceres target, and all dependent targets, but for older versions of CMake
-      # the flag must be specified explicitly both for Ceres and the
-      # examples/tests.
-      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
-    endif()
-  endif()
-endif(CXX11)
-
 # Eigen.
 find_package(Eigen REQUIRED)
 if (EIGEN_FOUND)
@@ -506,6 +425,87 @@
   message("-- Building without OpenMP, disabling.")
 endif (OPENMP)
 
+# Initialise CMAKE_REQUIRED_FLAGS used by CheckCXXSourceCompiles with the
+# contents of CMAKE_CXX_FLAGS such that if the user has passed extra flags
+# they are used when discovering shared_ptr/unordered_map.
+set(CMAKE_REQUIRED_FLAGS ${CMAKE_CXX_FLAGS})
+include(CheckCXXCompilerFlag)
+check_cxx_compiler_flag("-std=c++11" COMPILER_HAS_CXX11_FLAG)
+if (CXX11 AND COMPILER_HAS_CXX11_FLAG)
+  # Update CMAKE_REQUIRED_FLAGS used by CheckCXXSourceCompiles to include
+  # -std=c++11 s/t we will detect the C++11 versions of unordered_map &
+  # shared_ptr if they exist.
+  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
+endif (CXX11 AND COMPILER_HAS_CXX11_FLAG)
+
+# Set the Ceres compile definitions for the unordered_map configuration.
+include(FindUnorderedMap)
+find_unordered_map()
+if (UNORDERED_MAP_FOUND)
+  if (HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
+    list(APPEND CERES_COMPILE_OPTIONS CERES_STD_UNORDERED_MAP)
+  endif(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
+  if (HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+    list(APPEND CERES_COMPILE_OPTIONS CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
+  endif(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+  if (HAVE_TR1_UNORDERED_MAP_IN_TR1_NAMESPACE)
+    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_UNORDERED_MAP)
+  endif(HAVE_TR1_UNORDERED_MAP_IN_TR1_NAMESPACE)
+else (UNORDERED_MAP_FOUND)
+  message("-- Replacing unordered_map/set with map/set (warning: slower!), "
+    "try enabling CXX11 option if you expect C++11 to be available.")
+  list(APPEND CERES_COMPILE_OPTIONS CERES_NO_UNORDERED_MAP)
+endif()
+
+# Set the Ceres compile definitions for the shared_ptr configuration.
+include(FindSharedPtr)
+find_shared_ptr()
+if (SHARED_PTR_FOUND)
+  if (SHARED_PTR_TR1_MEMORY_HEADER)
+    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_MEMORY_HEADER)
+  endif (SHARED_PTR_TR1_MEMORY_HEADER)
+  if (SHARED_PTR_TR1_NAMESPACE)
+    list(APPEND CERES_COMPILE_OPTIONS CERES_TR1_SHARED_PTR)
+  endif (SHARED_PTR_TR1_NAMESPACE)
+else (SHARED_PTR_FOUND)
+  message(FATAL_ERROR "Unable to find shared_ptr, try enabling CXX11 option "
+    "if you expect C++11 to be available.")
+endif (SHARED_PTR_FOUND)
+
+include(FindCXX11MathFunctions)
+find_cxx11_math_functions()
+if (CXX11 AND NOT CXX11_MATH_FUNCTIONS_FOUND)
+  message("-- Failed to find C++11 math functions (cbrt(), exp2() etc). "
+    "Disabling C++11.")
+  update_cache_variable(CXX11 OFF)
+endif()
+
+# To ensure that CXX11 accurately reflects whether we are using C++11,
+# check if it is required given where the potentially C++11 features Ceres
+# uses were found, and disable it if C++11 is not being used.
+if (CXX11)
+  if (NOT HAVE_SHARED_PTR_IN_STD_NAMESPACE AND
+      NOT HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
+    message("-- Failed to find C++11 components in C++11 locations & "
+      "namespaces, disabling CXX11.")
+    update_cache_variable(CXX11 OFF)
+  else()
+    message("   ==============================================================")
+    message("   Compiling Ceres using C++11.  This will result in a version ")
+    message("   of Ceres that will require the use of C++11 in client code.")
+    message("   ==============================================================")
+    list(APPEND CERES_COMPILE_OPTIONS CERES_USE_CXX11)
+    if (COMPILER_HAS_CXX11_FLAG AND
+        CMAKE_VERSION VERSION_LESS "2.8.12")
+      # For CMake versions > 2.8.12, the C++11 dependency is rolled into the
+      # Ceres target, and all dependent targets, but for older versions of CMake
+      # the flag must be specified explicitly both for Ceres and the
+      # examples/tests.
+      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+    endif()
+  endif()
+endif(CXX11)
+
 if (CXX11 AND (TBB OR CXX11_THREADS))
   # We require <atomic> for both TBB & CXX11_THREADS. Not all compilers
   # (MSVC 2010) have <atomic> even if they have other C++11 features.