Made collections port compatible with MSVC2008
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 28 Nov 2013 17:24:55 +0000 (23:24 +0600)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 28 Nov 2013 17:28:11 +0000 (23:28 +0600)
The issue was caused by the fact that in this version
of MSVC unordered_map class is defined in <unordered_map>
header file, but this file declares the class int std::tr1
namespace.

This confused existing assumption that if there's an
existing <unordered_map> file then class is declared
in std namespace.

Added an extra check to CMake which detects whether
it's std or std::tr1 which actually contains class
of unordered_map.

This might be changed/cleaned in the future, for
now committing to our repository to solve compilation
error on windows.

Details of the patch in upstream can be found there:

  https://ceres-solver-review.googlesource.com/#/c/4371/

extern/libmv/third_party/ceres/CMakeLists.txt
extern/libmv/third_party/ceres/SConscript
extern/libmv/third_party/ceres/bundle.sh
extern/libmv/third_party/ceres/internal/ceres/collections_port.h

index a05c424df4921d739fe4a73ded41fbc08a430d1e..45e1f9b398dab4ebec7ab9f52016a311bafa0939 100644 (file)
@@ -310,9 +310,33 @@ if(WITH_OPENMP)
 endif()
 
 include(CheckIncludeFileCXX)
-CHECK_INCLUDE_FILE_CXX(unordered_map UNORDERED_MAP_IN_STD_NAMESPACE)
-if(UNORDERED_MAP_IN_STD_NAMESPACE)
-       ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP)
+CHECK_INCLUDE_FILE_CXX(unordered_map HAVE_STD_UNORDERED_MAP_HEADER)
+if(HAVE_STD_UNORDERED_MAP_HEADER)
+       INCLUDE (CheckCXXSourceCompiles)
+       CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                 int main() {
+                                   std::unordered_map<int, int> map;
+                                   return 0;
+                                 }"
+                                 HAVE_UNURDERED_MAP_IN_STD_NAMESPACE)
+       if(HAVE_UNURDERED_MAP_IN_STD_NAMESPACE)
+               ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP)
+       else()
+               CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                         int main() {
+                                           std::tr1::unordered_map<int, int> map;
+                                           return 0;
+                                         }"
+                                         HAVE_UNURDERED_MAP_IN_TR1_NAMESPACE)
+               if(HAVE_UNURDERED_MAP_IN_TR1_NAMESPACE)
+                       ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
+               else()
+                       MESSAGE("-- Found <unordered_map> but can not find neither std::unordered_map "
+                               "nor std::tr1::unordered_map.")
+                       MESSAGE("-- Replacing unordered_map/set with map/set (warning: slower!)")
+                       ADD_DEFINITIONS(-DCERES_NO_UNORDERED_MAP)
+               endif()
+       endif()
 else()
        CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" UNORDERED_MAP_IN_TR1_NAMESPACE)
        if(UNORDERED_MAP_IN_TR1_NAMESPACE)
index e61e979efd21be7a8e4de9db60877d4fab685096..22960e7b3bd2195789ec874ce634e5dc89356807 100644 (file)
@@ -29,7 +29,14 @@ if env['WITH_BF_OPENMP']:
 
 conf = Configure(env)
 if conf.CheckCXXHeader("unordered_map"):
-    defs.append('CERES_STD_UNORDERED_MAP')
+    if conf.CheckType('std::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+        defs.append('CERES_STD_UNORDERED_MAP')
+    elif conf.CheckType('std::tr1::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+        defs.append('CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE')
+    else:
+        print("-- Found <unordered_map> but can not find neither std::unordered_map nor std::tr1::unordered_map.")
+        print("-- Replacing unordered_map/set with map/set (warning: slower!)")
+        defs.append('CERES_NO_UNORDERED_MAP')
 elif conf.CheckCXXHeader("tr1/unordered_map"):
     defs.append('CERES_TR1_UNORDERED_MAP')
 else:
index 10c70ec51f72488805626c34c21b7db2e5ab0aa4..906c5d3dd48a1ef9d996adfa2e70e32bb463af78 100755 (executable)
@@ -175,9 +175,33 @@ if(WITH_OPENMP)
 endif()
 
 include(CheckIncludeFileCXX)
-CHECK_INCLUDE_FILE_CXX(unordered_map UNORDERED_MAP_IN_STD_NAMESPACE)
-if(UNORDERED_MAP_IN_STD_NAMESPACE)
-       ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP)
+CHECK_INCLUDE_FILE_CXX(unordered_map HAVE_STD_UNORDERED_MAP_HEADER)
+if(HAVE_STD_UNORDERED_MAP_HEADER)
+       INCLUDE (CheckCXXSourceCompiles)
+       CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                 int main() {
+                                   std::unordered_map<int, int> map;
+                                   return 0;
+                                 }"
+                                 HAVE_UNURDERED_MAP_IN_STD_NAMESPACE)
+       if(HAVE_UNURDERED_MAP_IN_STD_NAMESPACE)
+               ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP)
+       else()
+               CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                         int main() {
+                                           std::tr1::unordered_map<int, int> map;
+                                           return 0;
+                                         }"
+                                         HAVE_UNURDERED_MAP_IN_TR1_NAMESPACE)
+               if(HAVE_UNURDERED_MAP_IN_TR1_NAMESPACE)
+                       ADD_DEFINITIONS(-DCERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
+               else()
+                       MESSAGE("-- Found <unordered_map> but can not find neither std::unordered_map "
+                               "nor std::tr1::unordered_map.")
+                       MESSAGE("-- Replacing unordered_map/set with map/set (warning: slower!)")
+                       ADD_DEFINITIONS(-DCERES_NO_UNORDERED_MAP)
+               endif()
+       endif()
 else()
        CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" UNORDERED_MAP_IN_TR1_NAMESPACE)
        if(UNORDERED_MAP_IN_TR1_NAMESPACE)
@@ -224,7 +248,14 @@ if env['WITH_BF_OPENMP']:
 
 conf = Configure(env)
 if conf.CheckCXXHeader("unordered_map"):
-    defs.append('CERES_STD_UNORDERED_MAP')
+    if conf.CheckType('std::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+        defs.append('CERES_STD_UNORDERED_MAP')
+    elif conf.CheckType('std::tr1::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+        defs.append('CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE')
+    else:
+        print("-- Found <unordered_map> but can not find neither std::unordered_map nor std::tr1::unordered_map.")
+        print("-- Replacing unordered_map/set with map/set (warning: slower!)")
+        defs.append('CERES_NO_UNORDERED_MAP')
 elif conf.CheckCXXHeader("tr1/unordered_map"):
     defs.append('CERES_TR1_UNORDERED_MAP')
 else:
@@ -232,6 +263,8 @@ else:
     print("-- Replacing unordered_map/set with map/set (warning: slower!)")
     defs.append('CERES_NO_UNORDERED_MAP')
 
+env = conf.Finish()
+
 incs = '. ../../ ../../../Eigen3 ./include ./internal ../gflags'
 
 # work around broken hashtable in 10.5 SDK
index 8f345d4d70ac7b8f375eb029c65b5babafc64862..e37be528ecb97a4fc387c04d8fdaab2e5367367e 100644 (file)
 #  define CERES_HASH_NAMESPACE_END }
 #endif
 
-#if !defined(CERES_NO_UNORDERED_MAP) && !defined(CERES_TR1_UNORDERED_MAP) && !defined(CERES_STD_UNORDERED_MAP)
-#error One of: CERES_NO_UNORDERED_MAP, CERES_TR1_UNORDERED_MAP, CERES_STD_UNORDERED_MAP must be defined!
+#if defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
+#  include <unordered_map>
+#  include <unordered_set>
+#  define CERES_HASH_NAMESPACE_START namespace std { namespace tr1 {
+#  define CERES_HASH_NAMESPACE_END } }
+#endif
+
+#if !defined(CERES_NO_UNORDERED_MAP) && !defined(CERES_TR1_UNORDERED_MAP) && \
+    !defined(CERES_STD_UNORDERED_MAP) && !defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)  // NOLINT
+#  error One of: CERES_NO_UNORDERED_MAP, CERES_TR1_UNORDERED_MAP,\
+ CERES_STD_UNORDERED_MAP, CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE must be defined!  // NOLINT
 #endif
 
 #include <utility>
@@ -82,7 +91,8 @@ struct HashSet : set<K> {};
 namespace ceres {
 namespace internal {
 
-#if defined(CERES_TR1_UNORDERED_MAP)
+#if defined(CERES_TR1_UNORDERED_MAP) || \
+    defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
 template<typename K, typename V>
 struct HashMap : std::tr1::unordered_map<K, V> {};
 template<typename K>