Build file macro for testing unordered_map C++ container support.
authorLukas Tönne <lukas.toenne@gmail.com>
Tue, 25 Mar 2014 08:21:30 +0000 (09:21 +0100)
committerLukas Tönne <lukas.toenne@gmail.com>
Tue, 25 Mar 2014 08:25:00 +0000 (09:25 +0100)
Using unordered_map and unordered_set C++ container types currently
requires careful testing or usage of boost, due to the various confusing
C++ version differences in include paths and namespaces.

Libmv defines tests for these cases in cmake and scons, such that ceres
can use any available implementation, or fall back too std::map/std::set
if none can be found.

This patch generalizes this buildfile code by providing a Blender macro.
* cmake: defines both the variables used by libmv at them moment as well
as 2 variables UNORDERED_MAP_INCLUDE_PREFIX and UNORDERED_MAP_NAMESPACE,
which can later be used in other C++ parts for convenience.
* scons: adds a tool script returning the include prefix and namespace.
Libmv checks these to define the appropriate definitions for ceres.

Differential Revision: https://developer.blender.org/D425

build_files/cmake/macros.cmake
build_files/scons/tools/unordered_map.py [new file with mode: 0644]
extern/libmv/third_party/ceres/CMakeLists.txt
extern/libmv/third_party/ceres/SConscript

index 28af1c1486ca71c6c41838fccf15fc2bed14e3af..772ff6d7596221a124445217b0465e03d8241659 100644 (file)
@@ -483,6 +483,77 @@ macro(TEST_STDBOOL_SUPPORT)
        HAVE_STDBOOL_H)
 endmacro()
 
        HAVE_STDBOOL_H)
 endmacro()
 
+macro(TEST_UNORDERED_MAP_SUPPORT)
+       # - Detect unordered_map availability
+       # Test if a valid implementation of unordered_map exists
+       # and define the include path
+       # This module defines
+       #  HAVE_UNORDERED_MAP, whether unordered_map implementation was found
+       #  
+       #  HAVE_STD_UNORDERED_MAP_HEADER, <unordered_map.h> was found
+       #  HAVE_UNORDERED_MAP_IN_STD_NAMESPACE, unordered_map is in namespace std
+       #  HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE, unordered_map is in namespace std::tr1
+       #  
+       #  UNORDERED_MAP_INCLUDE_PREFIX, include path prefix for unordered_map, if found
+       #  UNORDERED_MAP_NAMESPACE, namespace for unordered_map, if found
+
+       include(CheckIncludeFileCXX)
+       CHECK_INCLUDE_FILE_CXX("unordered_map" HAVE_STD_UNORDERED_MAP_HEADER)
+       if(HAVE_STD_UNORDERED_MAP_HEADER)
+               # Even so we've found unordered_map header file it doesn't
+               # mean unordered_map and unordered_set will be declared in
+               # std namespace.
+               #
+               # Namely, MSVC 2008 have unordered_map header which declares
+               # unordered_map class in std::tr1 namespace. In order to support
+               # this, we do extra check to see which exactly namespace is
+               # to be used.
+
+               include(CheckCXXSourceCompiles)
+               CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                         int main() {
+                                           std::unordered_map<int, int> map;
+                                           return 0;
+                                         }"
+                                         HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
+               if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
+                       message(STATUS "Found unordered_map/set in std namespace.")
+
+                       set(HAVE_UNORDERED_MAP "TRUE")
+                       set(UNORDERED_MAP_INCLUDE_PREFIX "")
+                       set(UNORDERED_MAP_NAMESPACE "std")
+               else()
+                       CHECK_CXX_SOURCE_COMPILES("#include <unordered_map>
+                                                 int main() {
+                                                   std::tr1::unordered_map<int, int> map;
+                                                   return 0;
+                                                 }"
+                                                 HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+                       if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+                               message(STATUS "Found unordered_map/set in std::tr1 namespace.")
+
+                               set(HAVE_UNORDERED_MAP "TRUE")
+                               set(UNORDERED_MAP_INCLUDE_PREFIX "")
+                               set(UNORDERED_MAP_NAMESPACE "std::tr1")
+                       else()
+                               message(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
+                                       "or std::tr1::unordered_map.")
+                       endif()
+               endif()
+       else()
+               CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+               if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+                       message(STATUS "Found unordered_map/set in std::tr1 namespace.")
+
+                       set(HAVE_UNORDERED_MAP "TRUE")
+                       set(UNORDERED_MAP_INCLUDE_PREFIX "tr1")
+                       set(UNORDERED_MAP_NAMESPACE "std::tr1")
+               else()
+                       message(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
+               endif()
+       endif()
+endmacro()
+
 # when we have warnings as errors applied globally this
 # needs to be removed for some external libs which we dont maintain.
 
 # when we have warnings as errors applied globally this
 # needs to be removed for some external libs which we dont maintain.
 
diff --git a/build_files/scons/tools/unordered_map.py b/build_files/scons/tools/unordered_map.py
new file mode 100644 (file)
index 0000000..d314a77
--- /dev/null
@@ -0,0 +1,32 @@
+def test_unordered_map(conf):
+    """
+    Detect unordered_map availability
+    
+    Returns (True/False, namespace, include prefix)
+    """
+
+    if conf.CheckCXXHeader("unordered_map"):
+        # Even so we've found unordered_map header file it doesn't
+        # mean unordered_map and unordered_set will be declared in
+        # std namespace.
+        #
+        # Namely, MSVC 2008 have unordered_map header which declares
+        # unordered_map class in std::tr1 namespace. In order to support
+        # this, we do extra check to see which exactly namespace is
+        # to be used.
+
+        if conf.CheckType('std::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+            print("-- Found unordered_map/set in std namespace.")
+            return True, 'std', ''
+        elif conf.CheckType('std::tr1::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
+            print("-- Found unordered_map/set in std::tr1 namespace.")
+            return True, 'std::tr1', ''
+        else:
+            print("-- Found <unordered_map> but can not find neither std::unordered_map nor std::tr1::unordered_map.")
+            return False, '', ''
+    elif conf.CheckCXXHeader("tr1/unordered_map"):
+        print("-- Found unordered_map/set in std::tr1 namespace.")
+        return True, 'std::tr1', 'tr1/'
+    else:
+        print("-- Unable to find <unordered_map> or <tr1/unordered_map>. ")
+        return False, '', ''
index 7fd86b69dcfb1eb09d69fec62428be547f5c9bcc..baa619f930cd1080b173e8a09b637325ea997aad 100644 (file)
@@ -309,58 +309,25 @@ if(WITH_OPENMP)
        )
 endif()
 
        )
 endif()
 
-include(CheckIncludeFileCXX)
-CHECK_INCLUDE_FILE_CXX(unordered_map HAVE_STD_UNORDERED_MAP_HEADER)
+TEST_UNORDERED_MAP_SUPPORT()
 if(HAVE_STD_UNORDERED_MAP_HEADER)
 if(HAVE_STD_UNORDERED_MAP_HEADER)
-       # Even so we've found unordered_map header file it doesn't
-       # mean unordered_map and unordered_set will be declared in
-       # std namespace.
-       #
-       # Namely, MSVC 2008 have unordered_map header which declares
-       # unordered_map class in std::tr1 namespace. In order to support
-       # this, we do extra check to see which exactly namespace is
-       # to be used.
-
-       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)
+       if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
                add_definitions(-DCERES_STD_UNORDERED_MAP)
                add_definitions(-DCERES_STD_UNORDERED_MAP)
-               message(STATUS "Found unordered_map/set in std namespace.")
        else()
        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)
+               if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
                        add_definitions(-DCERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
                        add_definitions(-DCERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
-                       message(STATUS "Found unordered_map/set in std::tr1 namespace.")
                else()
                else()
-                       message(STATUS "Found <unordered_map> but cannot find either std::unordered_map "
-                               "or std::tr1::unordered_map.")
-                       message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
                        add_definitions(-DCERES_NO_UNORDERED_MAP)
                        add_definitions(-DCERES_NO_UNORDERED_MAP)
+                       message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
                endif()
        endif()
 else()
                endif()
        endif()
 else()
-       CHECK_INCLUDE_FILE_CXX("tr1/unordered_map" UNORDERED_MAP_IN_TR1_NAMESPACE)
-       if(UNORDERED_MAP_IN_TR1_NAMESPACE)
-               add_definitions(-DCERES_TR1_UNORDERED_MAP)
-               message(STATUS "Found unordered_map/set in std::tr1 namespace.")
+       if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
+               add_definitions(-DCERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
        else()
        else()
-               message(STATUS "Unable to find <unordered_map> or <tr1/unordered_map>. ")
-               message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
                add_definitions(-DCERES_NO_UNORDERED_MAP)
                add_definitions(-DCERES_NO_UNORDERED_MAP)
+               message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
        endif()
 endif()
 
        endif()
 endif()
 
-unset(HAVE_UNURDERED_MAP_IN_TR1_NAMESPACE)
-unset(HAVE_STD_UNORDERED_MAP_HEADER)
-
 blender_add_lib(extern_ceres "${SRC}" "${INC}" "${INC_SYS}")
 blender_add_lib(extern_ceres "${SRC}" "${INC}" "${INC_SYS}")
index 2573a9742ad6f093ff0876f9db8304f9539eb021..406e1593ded0339814002ce64c3fc61a4f99179f 100644 (file)
@@ -7,6 +7,8 @@
 import sys
 import os
 
 import sys
 import os
 
+from unordered_map import test_unordered_map
+
 Import('env')
 
 src = []
 Import('env')
 
 src = []
@@ -27,35 +29,26 @@ defs.append('CERES_HAVE_RWLOCK')
 if env['WITH_BF_OPENMP']:
     defs.append('CERES_USE_OPENMP')
 
 if env['WITH_BF_OPENMP']:
     defs.append('CERES_USE_OPENMP')
 
-conf = Configure(env)
-if conf.CheckCXXHeader("unordered_map"):
-    # Even so we've found unordered_map header file it doesn't
-    # mean unordered_map and unordered_set will be declared in
-    # std namespace.
-    #
-    # Namely, MSVC 2008 have unordered_map header which declares
-    # unordered_map class in std::tr1 namespace. In order to support
-    # this, we do extra check to see which exactly namespace is
-    # to be used.
+def define_unordered_map(conf):
+    found, namespace, include_prefix = test_unordered_map(conf)
+    if found:
+        if not include_prefix:
+            if namespace == 'std':
+                defs.append('CERES_STD_UNORDERED_MAP')
+                return True
+            elif namespace == 'std::tr1':
+                defs.append('CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE')
+                return True
+        else:
+            if namespace == 'std::tr1':
+                defs.append('CERES_TR1_UNORDERED_MAP')
+                return True
+    return False
 
 
-    if conf.CheckType('std::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
-        defs.append('CERES_STD_UNORDERED_MAP')
-        print("-- Found unordered_map/set in std namespace.")
-    elif conf.CheckType('std::tr1::unordered_map<int, int>', language = 'CXX', includes="#include <unordered_map>"):
-        defs.append('CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE')
-        print("-- Found unordered_map/set in std::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')
-    print("-- Found unordered_map/set in std::tr1 namespace.")
-else:
-    print("-- Unable to find <unordered_map> or <tr1/unordered_map>. ")
+conf = Configure(env)
+if not define_unordered_map(conf):
     print("-- Replacing unordered_map/set with map/set (warning: slower!)")
     defs.append('CERES_NO_UNORDERED_MAP')
     print("-- Replacing unordered_map/set with map/set (warning: slower!)")
     defs.append('CERES_NO_UNORDERED_MAP')
-
 env = conf.Finish()
 
 incs = '. ../../ ../../../Eigen3 ./include ./internal ../gflags'
 env = conf.Finish()
 
 incs = '. ../../ ../../../Eigen3 ./include ./internal ../gflags'