From 81b7f437da596e88712fb8af7fa2010241ae392c Mon Sep 17 00:00:00 2001
From: tamasmeszaros <meszaros.q@gmail.com>
Date: Thu, 23 Jan 2020 16:02:23 +0100
Subject: [PATCH] update openvdb build patch to solve latomic

---
 deps/openvdb-mods.patch | 653 ++++++++++++++++++++++++----------------
 1 file changed, 389 insertions(+), 264 deletions(-)

diff --git a/deps/openvdb-mods.patch b/deps/openvdb-mods.patch
index 023cb5308..c365a4305 100644
--- a/deps/openvdb-mods.patch
+++ b/deps/openvdb-mods.patch
@@ -1,24 +1,25 @@
-From dbe038fce8a15ddc9a5c83ec5156d7bc9e178015 Mon Sep 17 00:00:00 2001
+From c660786dbac9e4c284049af4da56c34a998a4b68 Mon Sep 17 00:00:00 2001
 From: tamasmeszaros <meszaros.q@gmail.com>
-Date: Wed, 16 Oct 2019 17:42:50 +0200
-Subject: [PATCH] Build fixes for PrusaSlicer integration
+Date: Thu, 23 Jan 2020 16:27:21 +0100
+Subject: [PATCH] openvdb-mods
 
-Signed-off-by: tamasmeszaros <meszaros.q@gmail.com>
 ---
  CMakeLists.txt                  |   3 -
- cmake/FindBlosc.cmake           | 218 ---------------
+ cmake/CheckAtomic.cmake         | 106 ++++++
+ cmake/FindBlosc.cmake           | 218 ------------
  cmake/FindCppUnit.cmake         |   4 +-
- cmake/FindIlmBase.cmake         | 337 ----------------------
- cmake/FindOpenEXR.cmake         | 329 ----------------------
+ cmake/FindIlmBase.cmake         | 337 ------------------
+ cmake/FindOpenEXR.cmake         | 329 ------------------
  cmake/FindOpenVDB.cmake         |  19 +-
- cmake/FindTBB.cmake             | 605 ++++++++++++++++++++--------------------
+ cmake/FindTBB.cmake             | 599 ++++++++++++++++----------------
  openvdb/CMakeLists.txt          |  13 +-
  openvdb/Grid.cc                 |   3 +
  openvdb/PlatformConfig.h        |   9 +-
- openvdb/cmd/CMakeLists.txt      |   4 +-
+ openvdb/cmd/CMakeLists.txt      |  11 +-
  openvdb/unittest/CMakeLists.txt |   3 +-
  openvdb/unittest/TestFile.cc    |   2 +-
- 13 files changed, 336 insertions(+), 1213 deletions(-)
+ 14 files changed, 446 insertions(+), 1210 deletions(-)
+ create mode 100644 cmake/CheckAtomic.cmake
  delete mode 100644 cmake/FindBlosc.cmake
  delete mode 100644 cmake/FindIlmBase.cmake
  delete mode 100644 cmake/FindOpenEXR.cmake
@@ -40,6 +41,119 @@ index 580b353..6d364c1 100644
      cmake/FindOpenVDB.cmake
      cmake/FindTBB.cmake
      cmake/OpenVDBGLFW3Setup.cmake
+diff --git a/cmake/CheckAtomic.cmake b/cmake/CheckAtomic.cmake
+new file mode 100644
+index 0000000..c045e30
+--- /dev/null
++++ b/cmake/CheckAtomic.cmake
+@@ -0,0 +1,106 @@
++# atomic builtins are required for threading support.
++
++INCLUDE(CheckCXXSourceCompiles)
++INCLUDE(CheckLibraryExists)
++
++# Sometimes linking against libatomic is required for atomic ops, if
++# the platform doesn't support lock-free atomics.
++
++function(check_working_cxx_atomics varname)
++  set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
++  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
++  CHECK_CXX_SOURCE_COMPILES("
++#include <atomic>
++std::atomic<int> x;
++int main() {
++  return x;
++}
++" ${varname})
++  set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
++endfunction(check_working_cxx_atomics)
++
++function(check_working_cxx_atomics64 varname)
++  set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
++  set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
++  CHECK_CXX_SOURCE_COMPILES("
++#include <atomic>
++#include <cstdint>
++std::atomic<uint64_t> x (0);
++int main() {
++  uint64_t i = x.load(std::memory_order_relaxed);
++  return 0;
++}
++" ${varname})
++  set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
++endfunction(check_working_cxx_atomics64)
++
++
++# This isn't necessary on MSVC, so avoid command-line switch annoyance
++# by only running on GCC-like hosts.
++if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
++  # First check if atomics work without the library.
++  check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
++  # If not, check if the library exists, and atomics work with it.
++  if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
++    check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
++    if( HAVE_LIBATOMIC )
++      list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
++      check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
++      if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
++	message(FATAL_ERROR "Host compiler must support std::atomic!")
++      endif()
++    else()
++      message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
++    endif()
++  endif()
++endif()
++
++# Check for 64 bit atomic operations.
++if(MSVC)
++  set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
++else()
++  check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
++endif()
++
++# If not, check if the library exists, and atomics work with it.
++if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
++  check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
++  if(HAVE_CXX_LIBATOMICS64)
++    list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
++    check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
++    if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
++      message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
++    endif()
++  else()
++    message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
++  endif()
++endif()
++
++## TODO: This define is only used for the legacy atomic operations in
++## llvm's Atomic.h, which should be replaced.  Other code simply
++## assumes C++11 <atomic> works.
++CHECK_CXX_SOURCE_COMPILES("
++#ifdef _MSC_VER
++#include <windows.h>
++#endif
++int main() {
++#ifdef _MSC_VER
++        volatile LONG val = 1;
++        MemoryBarrier();
++        InterlockedCompareExchange(&val, 0, 1);
++        InterlockedIncrement(&val);
++        InterlockedDecrement(&val);
++#else
++        volatile unsigned long val = 1;
++        __sync_synchronize();
++        __sync_val_compare_and_swap(&val, 1, 0);
++        __sync_add_and_fetch(&val, 1);
++        __sync_sub_and_fetch(&val, 1);
++#endif
++        return 0;
++      }
++" LLVM_HAS_ATOMICS)
++
++if( NOT LLVM_HAS_ATOMICS )
++  message(STATUS "Warning: LLVM will be built thread-unsafe because atomic builtins are missing")
++endif()
+\ No newline at end of file
 diff --git a/cmake/FindBlosc.cmake b/cmake/FindBlosc.cmake
 deleted file mode 100644
 index 5aacfdd..0000000
@@ -965,7 +1079,7 @@ index 339c1a2..0000000
 -  message(FATAL_ERROR "Unable to find OpenEXR")
 -endif()
 diff --git a/cmake/FindOpenVDB.cmake b/cmake/FindOpenVDB.cmake
-index 63a2eda..6211071 100644
+index 63a2eda..d9f6d07 100644
 --- a/cmake/FindOpenVDB.cmake
 +++ b/cmake/FindOpenVDB.cmake
 @@ -244,7 +244,7 @@ set(OpenVDB_LIB_COMPONENTS "")
@@ -1004,7 +1118,7 @@ index 63a2eda..6211071 100644
     )
 +
 +   if (OPENVDB_USE_STATIC_LIBS)
-+    set_target_properties(OpenVDB::${COMPONENT} PROPERTIES 
++    set_target_properties(OpenVDB::${COMPONENT} PROPERTIES
 +      INTERFACE_COMPILE_DEFINITIONS "OPENVDB_STATICLIB;OPENVDB_OPENEXR_STATICLIB"
 +    )
 +   endif()
@@ -1012,7 +1126,7 @@ index 63a2eda..6211071 100644
  endforeach()
  
 diff --git a/cmake/FindTBB.cmake b/cmake/FindTBB.cmake
-index bdf9c81..c6bdec9 100644
+index bdf9c81..06093a4 100644
 --- a/cmake/FindTBB.cmake
 +++ b/cmake/FindTBB.cmake
 @@ -1,333 +1,332 @@
@@ -1022,35 +1136,21 @@ index bdf9c81..c6bdec9 100644
 -# All rights reserved. This software is distributed under the
 -# Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
 +# Copyright (c) 2015 Justus Calvin
-+# 
+ #
+-# Redistributions of source code must retain the above copyright
+-# and license notice and the following restrictions and disclaimer.
 +# Permission is hereby granted, free of charge, to any person obtaining a copy
 +# of this software and associated documentation files (the "Software"), to deal
 +# in the Software without restriction, including without limitation the rights
 +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 +# copies of the Software, and to permit persons to whom the Software is
 +# furnished to do so, subject to the following conditions:
-+# 
-+# The above copyright notice and this permission notice shall be included in all
-+# copies or substantial portions of the Software.
-+# 
-+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-+# SOFTWARE.
-+
- #
--# Redistributions of source code must retain the above copyright
--# and license notice and the following restrictions and disclaimer.
-+# FindTBB
-+# -------
  #
 -# *     Neither the name of DreamWorks Animation nor the names of
 -# its contributors may be used to endorse or promote products derived
 -# from this software without specific prior written permission.
-+# Find TBB include directories and libraries.
++# The above copyright notice and this permission notice shall be included in all
++# copies or substantial portions of the Software.
  #
 -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -1065,7 +1165,14 @@ index bdf9c81..c6bdec9 100644
 -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 -# IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
 -# LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
-+# Usage:
++# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++# SOFTWARE.
++
  #
 -#[=======================================================================[.rst:
 -
@@ -1142,19 +1249,26 @@ index bdf9c81..c6bdec9 100644
 -if(POLICY CMP0057)
 -  cmake_policy(SET CMP0057 NEW)
 -endif()
++# FindTBB
++# -------
++#
++# Find TBB include directories and libraries.
++#
++# Usage:
++#
 +#  find_package(TBB [major[.minor]] [EXACT]
 +#               [QUIET] [REQUIRED]
 +#               [[COMPONENTS] [components...]]
-+#               [OPTIONAL_COMPONENTS components...]) 
++#               [OPTIONAL_COMPONENTS components...])
 +#
-+# where the allowed components are tbbmalloc and tbb_preview. Users may modify 
++# where the allowed components are tbbmalloc and tbb_preview. Users may modify
 +# the behavior of this module with the following variables:
 +#
 +# * TBB_ROOT_DIR          - The base directory the of TBB installation.
 +# * TBB_INCLUDE_DIR       - The directory that contains the TBB headers files.
 +# * TBB_LIBRARY           - The directory that contains the TBB library files.
-+# * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library. 
-+#                           These libraries, if specified, override the 
++# * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library.
++#                           These libraries, if specified, override the
 +#                           corresponding library search results, where <library>
 +#                           may be tbb, tbb_debug, tbbmalloc, tbbmalloc_debug,
 +#                           tbb_preview, or tbb_preview_debug.
@@ -1167,7 +1281,7 @@ index bdf9c81..c6bdec9 100644
 +# Users may modify the behavior of this module with the following environment
 +# variables:
 +#
-+# * TBB_INSTALL_DIR 
++# * TBB_INSTALL_DIR
 +# * TBBROOT
 +# * LIBRARY_PATH
 +#
@@ -1180,15 +1294,15 @@ index bdf9c81..c6bdec9 100644
 +# * TBB_VERSION           - The full version string
 +# * TBB_VERSION_MAJOR     - The major version
 +# * TBB_VERSION_MINOR     - The minor version
-+# * TBB_INTERFACE_VERSION - The interface version number defined in 
++# * TBB_INTERFACE_VERSION - The interface version number defined in
 +#                           tbb/tbb_stddef.h.
-+# * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of 
++# * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of
 +#                           <library>, where <library> may be tbb, tbb_debug,
-+#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or 
++#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or
 +#                           tbb_preview_debug.
-+# * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of 
++# * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of
 +#                           <library>, where <library> may be tbb, tbb_debug,
-+#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or 
++#                           tbbmalloc, tbbmalloc_debug, tbb_preview, or
 +#                           tbb_preview_debug.
 +#
 +# The following varibles should be used to build and link with TBB:
@@ -1244,12 +1358,10 @@ index bdf9c81..c6bdec9 100644
 -  set(TBB_FIND_COMPONENTS ${_TBB_COMPONENT_LIST})
 -endif()
 +include(FindPackageHandleStandardArgs)
-+
-+find_package(Threads QUIET REQUIRED)
  
 -# Append TBB_ROOT or $ENV{TBB_ROOT} if set (prioritize the direct cmake var)
 -set(_TBB_ROOT_SEARCH_DIR "")
-+if(NOT TBB_FOUND)
++find_package(Threads QUIET REQUIRED)
  
 -if(TBB_ROOT)
 -  list(APPEND _TBB_ROOT_SEARCH_DIR ${TBB_ROOT})
@@ -1257,41 +1369,9 @@ index bdf9c81..c6bdec9 100644
 -  set(_ENV_TBB_ROOT $ENV{TBB_ROOT})
 -  if(_ENV_TBB_ROOT)
 -    list(APPEND _TBB_ROOT_SEARCH_DIR ${_ENV_TBB_ROOT})
-+  ##################################
-+  # Check the build type
-+  ##################################
-+  
-+  if(NOT DEFINED TBB_USE_DEBUG_BUILD)
-+    if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug)")
-+      set(TBB_BUILD_TYPE DEBUG)
-+    else()
-+      set(TBB_BUILD_TYPE RELEASE)
-+    endif()
-+  elseif(TBB_USE_DEBUG_BUILD)
-+    set(TBB_BUILD_TYPE DEBUG)
-+  else()
-+    set(TBB_BUILD_TYPE RELEASE)
-   endif()
+-  endif()
 -endif()
-+  
-+  ##################################
-+  # Set the TBB search directories
-+  ##################################
-+  
-+  # Define search paths based on user input and environment variables
-+  set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
-+  
-+  # Define the search directories based on the current platform
-+  if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
-+    set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
-+                               "C:/Program Files (x86)/Intel/TBB")
-+
-+    # Set the target architecture
-+    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
-+      set(TBB_ARCHITECTURE "intel64")
-+    else()
-+      set(TBB_ARCHITECTURE "ia32")
-+    endif()
++if(NOT TBB_FOUND)
  
 -# Additionally try and use pkconfig to find Tbb
 -
@@ -1339,6 +1419,57 @@ index bdf9c81..c6bdec9 100644
 -
 -  set(Tbb_VERSION ${Tbb_VERSION_MAJOR}.${Tbb_VERSION_MINOR})
 -endif()
++  ##################################
++  # Check the build type
++  ##################################
++
++  if(NOT DEFINED TBB_USE_DEBUG_BUILD)
++    if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug)")
++      set(TBB_BUILD_TYPE DEBUG)
++    else()
++      set(TBB_BUILD_TYPE RELEASE)
++    endif()
++  elseif(TBB_USE_DEBUG_BUILD)
++    set(TBB_BUILD_TYPE DEBUG)
++  else()
++    set(TBB_BUILD_TYPE RELEASE)
++  endif()
+ 
+-# ------------------------------------------------------------------------
+-#  Search for TBB lib DIR
+-# ------------------------------------------------------------------------
++  ##################################
++  # Set the TBB search directories
++  ##################################
+ 
+-set(_TBB_LIBRARYDIR_SEARCH_DIRS "")
++  # Define search paths based on user input and environment variables
++  set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
+ 
+-# Append to _TBB_LIBRARYDIR_SEARCH_DIRS in priority order
++  # Define the search directories based on the current platform
++  if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
++    set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
++                               "C:/Program Files (x86)/Intel/TBB")
+ 
+-set(_TBB_LIBRARYDIR_SEARCH_DIRS "")
+-list(APPEND _TBB_LIBRARYDIR_SEARCH_DIRS
+-  ${TBB_LIBRARYDIR}
+-  ${_TBB_ROOT_SEARCH_DIR}
+-  ${PC_Tbb_LIBRARY_DIRS}
+-  ${SYSTEM_LIBRARY_PATHS}
+-)
++    # Set the target architecture
++    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
++      set(TBB_ARCHITECTURE "intel64")
++    else()
++      set(TBB_ARCHITECTURE "ia32")
++    endif()
+ 
+-set(TBB_PATH_SUFFIXES
+-  lib64
+-  lib
+-)
 +    # Set the TBB search library path search suffix based on the version of VC
 +    if(WINDOWS_STORE)
 +      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11_ui")
@@ -1352,104 +1483,16 @@ index bdf9c81..c6bdec9 100644
 +      set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc10")
 +    endif()
  
--# ------------------------------------------------------------------------
--#  Search for TBB lib DIR
--# ------------------------------------------------------------------------
+-# platform branching
 +    # Add the library path search suffix for the VC independent version of TBB
 +    list(APPEND TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc_mt")
-+
-+  elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
-+    # OS X
-+    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
-+    
-+    # TODO: Check to see which C++ library is being used by the compiler.
-+    if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
-+      # The default C++ library on OS X 10.9 and later is libc++
-+      set(TBB_LIB_PATH_SUFFIX "lib/libc++" "lib")
-+    else()
-+      set(TBB_LIB_PATH_SUFFIX "lib")
-+    endif()
-+  elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
-+    # Linux
-+    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
-+    
-+    # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
-+    #       <arch>/gcc4.1. For now, assume that the compiler is more recent than
-+    #       gcc 4.4.x or later.
-+    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
-+      set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
-+    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
-+      set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
-+    endif()
-+  endif()
-+  
-+  ##################################
-+  # Find the TBB include dir
-+  ##################################
-+  
-+  find_path(TBB_INCLUDE_DIRS tbb/tbb.h
-+      HINTS ${TBB_INCLUDE_DIR} ${TBB_SEARCH_DIR}
-+      PATHS ${TBB_DEFAULT_SEARCH_DIR}
-+      PATH_SUFFIXES include)
-+
-+  ##################################
-+  # Set version strings
-+  ##################################
-+
-+  if(TBB_INCLUDE_DIRS)
-+    file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
-+    string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
-+        TBB_VERSION_MAJOR "${_tbb_version_file}")
-+    string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
-+        TBB_VERSION_MINOR "${_tbb_version_file}")
-+    string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
-+        TBB_INTERFACE_VERSION "${_tbb_version_file}")
-+    set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
-+  endif()
- 
--set(_TBB_LIBRARYDIR_SEARCH_DIRS "")
-+  ##################################
-+  # Find TBB components
-+  ##################################
- 
--# Append to _TBB_LIBRARYDIR_SEARCH_DIRS in priority order
-+  if(TBB_VERSION VERSION_LESS 4.3)
-+    set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc tbb)
-+  else()
-+    set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc_proxy tbbmalloc tbb)
-+  endif()
- 
--set(_TBB_LIBRARYDIR_SEARCH_DIRS "")
--list(APPEND _TBB_LIBRARYDIR_SEARCH_DIRS
--  ${TBB_LIBRARYDIR}
--  ${_TBB_ROOT_SEARCH_DIR}
--  ${PC_Tbb_LIBRARY_DIRS}
--  ${SYSTEM_LIBRARY_PATHS}
--)
-+  if(TBB_STATIC)
-+    set(TBB_STATIC_SUFFIX "_static")
-+  endif()
- 
--set(TBB_PATH_SUFFIXES
--  lib64
--  lib
--)
-+  # Find each component
-+  foreach(_comp ${TBB_SEARCH_COMPOMPONENTS})
-+    if(";${TBB_FIND_COMPONENTS};tbb;" MATCHES ";${_comp};")
- 
--# platform branching
-+      unset(TBB_${_comp}_LIBRARY_DEBUG CACHE)
-+      unset(TBB_${_comp}_LIBRARY_RELEASE CACHE)
  
 -if(UNIX)
 -  list(INSERT TBB_PATH_SUFFIXES 0 lib/x86_64-linux-gnu)
 -endif()
-+      # Search for the libraries
-+      find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}${TBB_STATIC_SUFFIX}
-+          HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
-+          PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
-+          PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
++  elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
++    # OS X
++    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
  
 -if(APPLE)
 -  if(TBB_FOR_CLANG)
@@ -1471,29 +1514,33 @@ index bdf9c81..c6bdec9 100644
 -      list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
 -      list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)
 -      list(INSERT TBB_PATH_SUFFIXES 0 lib/intel64/gcc${GCC_MAJOR}.${GCC_MINOR})
--    else()
++    # TODO: Check to see which C++ library is being used by the compiler.
++    if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
++      # The default C++ library on OS X 10.9 and later is libc++
++      set(TBB_LIB_PATH_SUFFIX "lib/libc++" "lib")
+     else()
 -      list(INSERT TBB_PATH_SUFFIXES 0 lib/intel64/gcc4.4)
--    endif()
--  endif()
++      set(TBB_LIB_PATH_SUFFIX "lib")
++    endif()
++  elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
++    # Linux
++    set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
++
++    # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
++    #       <arch>/gcc4.1. For now, assume that the compiler is more recent than
++    #       gcc 4.4.x or later.
++    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
++      set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
++    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
++      set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
+     endif()
+   endif()
 -endif()
-+      find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}${TBB_STATIC_SUFFIX}_debug
-+          HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
-+          PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
-+          PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
- 
+-
 -if(UNIX AND TBB_USE_STATIC_LIBS)
 -  set(_TBB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
 -  set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
 -endif()
-+      if(TBB_${_comp}_LIBRARY_DEBUG)
-+        list(APPEND TBB_LIBRARIES_DEBUG "${TBB_${_comp}_LIBRARY_DEBUG}")
-+      endif()
-+      if(TBB_${_comp}_LIBRARY_RELEASE)
-+        list(APPEND TBB_LIBRARIES_RELEASE "${TBB_${_comp}_LIBRARY_RELEASE}")
-+      endif()
-+      if(TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE} AND NOT TBB_${_comp}_LIBRARY)
-+        set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE}}")
-+      endif()
  
 -set(Tbb_LIB_COMPONENTS "")
 -
@@ -1516,39 +1563,44 @@ index bdf9c81..c6bdec9 100644
 -        # Extract the directory and apply the matched text (in brackets)
 -        get_filename_component(Tbb_${COMPONENT}_DIR "${Tbb_${COMPONENT}_LIBRARY}" DIRECTORY)
 -        set(Tbb_${COMPONENT}_LIBRARY "${Tbb_${COMPONENT}_DIR}/${CMAKE_MATCH_1}")
-+      if(TBB_${_comp}_LIBRARY AND EXISTS "${TBB_${_comp}_LIBRARY}")
-+        set(TBB_${_comp}_FOUND TRUE)
-+      else()
-+        set(TBB_${_comp}_FOUND FALSE)
-       endif()
+-      endif()
+-    endif()
++  ##################################
++  # Find the TBB include dir
++  ##################################
 +
-+      # Mark internal variables as advanced
-+      mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
-+      mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
-+      mark_as_advanced(TBB_${_comp}_LIBRARY)
++  find_path(TBB_INCLUDE_DIRS tbb/tbb.h
++      HINTS ${TBB_INCLUDE_DIR} ${TBB_SEARCH_DIR}
++      PATHS ${TBB_DEFAULT_SEARCH_DIR}
++      PATH_SUFFIXES include)
 +
-     endif()
--  endif()
-+  endforeach()
++  ##################################
++  # Set version strings
++  ##################################
++
++  if(TBB_INCLUDE_DIRS)
++    file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
++    string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
++        TBB_VERSION_MAJOR "${_tbb_version_file}")
++    string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
++        TBB_VERSION_MINOR "${_tbb_version_file}")
++    string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
++        TBB_INTERFACE_VERSION "${_tbb_version_file}")
++    set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
+   endif()
  
 -  list(APPEND Tbb_LIB_COMPONENTS ${Tbb_${COMPONENT}_LIBRARY})
 +  ##################################
-+  # Set compile flags and libraries
++  # Find TBB components
 +  ##################################
  
 -  if(Tbb_${COMPONENT}_LIBRARY)
 -    set(TBB_${COMPONENT}_FOUND TRUE)
--  else()
++  if(TBB_VERSION VERSION_LESS 4.3)
++    set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc tbb)
+   else()
 -    set(TBB_${COMPONENT}_FOUND FALSE)
-+  set(TBB_DEFINITIONS_RELEASE "")
-+  set(TBB_DEFINITIONS_DEBUG "TBB_USE_DEBUG=1")
-+    
-+  if(TBB_LIBRARIES_${TBB_BUILD_TYPE})
-+    set(TBB_LIBRARIES "${TBB_LIBRARIES_${TBB_BUILD_TYPE}}")
-+  endif()
-+  
-+  if(NOT MSVC AND NOT TBB_LIBRARIES)
-+    set(TBB_LIBRARIES ${TBB_LIBRARIES_RELEASE})
++    set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc_proxy tbbmalloc tbb)
    endif()
 -endforeach()
  
@@ -1556,61 +1608,51 @@ index bdf9c81..c6bdec9 100644
 -  set(CMAKE_FIND_LIBRARY_SUFFIXES ${_TBB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
 -  unset(_TBB_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
 -endif()
-+  set(TBB_DEFINITIONS "")
-+  if (MSVC AND TBB_STATIC)
-+    set(TBB_DEFINITIONS __TBB_NO_IMPLICIT_LINKAGE)
-+  endif ()
-+
-+  unset (TBB_STATIC_SUFFIX)
-+
-+  find_package_handle_standard_args(TBB 
-+      REQUIRED_VARS TBB_INCLUDE_DIRS TBB_LIBRARIES
-+      FAIL_MESSAGE "TBB library cannot be found. Consider set TBBROOT environment variable."
-+      HANDLE_COMPONENTS
-+      VERSION_VAR TBB_VERSION)
-+
-+  ##################################
-+  # Create targets
-+  ##################################
-+
-+  if(NOT CMAKE_VERSION VERSION_LESS 3.0 AND TBB_FOUND)
-+    add_library(TBB::tbb UNKNOWN IMPORTED)
-+    set_target_properties(TBB::tbb PROPERTIES
-+          INTERFACE_COMPILE_DEFINITIONS "${TBB_DEFINITIONS}"
-+          INTERFACE_LINK_LIBRARIES  "Threads::Threads;${CMAKE_DL_LIBS}"
-+          INTERFACE_INCLUDE_DIRECTORIES  ${TBB_INCLUDE_DIRS}
-+          IMPORTED_LOCATION              ${TBB_LIBRARIES})
-+    if(TBB_LIBRARIES_RELEASE AND TBB_LIBRARIES_DEBUG)
-+      set_target_properties(TBB::tbb PROPERTIES
-+          INTERFACE_COMPILE_DEFINITIONS "${TBB_DEFINITIONS};$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:${TBB_DEFINITIONS_DEBUG}>;$<$<CONFIG:Release>:${TBB_DEFINITIONS_RELEASE}>"
-+          IMPORTED_LOCATION_DEBUG          ${TBB_LIBRARIES_DEBUG}
-+          IMPORTED_LOCATION_RELWITHDEBINFO ${TBB_LIBRARIES_RELEASE}
-+          IMPORTED_LOCATION_RELEASE        ${TBB_LIBRARIES_RELEASE}
-+          IMPORTED_LOCATION_MINSIZEREL     ${TBB_LIBRARIES_RELEASE}
-+          )
-+    endif()
++  if(TBB_STATIC)
++    set(TBB_STATIC_SUFFIX "_static")
 +  endif()
  
 -# ------------------------------------------------------------------------
 -#  Cache and set TBB_FOUND
 -# ------------------------------------------------------------------------
-+  mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES)
++  # Find each component
++  foreach(_comp ${TBB_SEARCH_COMPOMPONENTS})
++    if(";${TBB_FIND_COMPONENTS};tbb;" MATCHES ";${_comp};")
 +
-+  unset(TBB_ARCHITECTURE)
-+  unset(TBB_BUILD_TYPE)
-+  unset(TBB_LIB_PATH_SUFFIX)
-+  unset(TBB_DEFAULT_SEARCH_DIR)
++      unset(TBB_${_comp}_LIBRARY_DEBUG CACHE)
++      unset(TBB_${_comp}_LIBRARY_RELEASE CACHE)
 +
-+  if(TBB_DEBUG)
-+    message(STATUS "  TBB_FOUND               = ${TBB_FOUND}")
-+    message(STATUS "  TBB_INCLUDE_DIRS        = ${TBB_INCLUDE_DIRS}")
-+    message(STATUS "  TBB_DEFINITIONS         = ${TBB_DEFINITIONS}")
-+    message(STATUS "  TBB_LIBRARIES           = ${TBB_LIBRARIES}")
-+    message(STATUS "  TBB_DEFINITIONS_DEBUG   = ${TBB_DEFINITIONS_DEBUG}")
-+    message(STATUS "  TBB_LIBRARIES_DEBUG     = ${TBB_LIBRARIES_DEBUG}")
-+    message(STATUS "  TBB_DEFINITIONS_RELEASE = ${TBB_DEFINITIONS_RELEASE}")
-+    message(STATUS "  TBB_LIBRARIES_RELEASE   = ${TBB_LIBRARIES_RELEASE}")
-+  endif()
++      # Search for the libraries
++      find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}${TBB_STATIC_SUFFIX}
++          HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
++          PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
++          PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
++
++      find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}${TBB_STATIC_SUFFIX}_debug
++          HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
++          PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
++          PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
++
++      if(TBB_${_comp}_LIBRARY_DEBUG)
++        list(APPEND TBB_LIBRARIES_DEBUG "${TBB_${_comp}_LIBRARY_DEBUG}")
++      endif()
++      if(TBB_${_comp}_LIBRARY_RELEASE)
++        list(APPEND TBB_LIBRARIES_RELEASE "${TBB_${_comp}_LIBRARY_RELEASE}")
++      endif()
++      if(TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE} AND NOT TBB_${_comp}_LIBRARY)
++        set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE}}")
++      endif()
++
++      if(TBB_${_comp}_LIBRARY AND EXISTS "${TBB_${_comp}_LIBRARY}")
++        set(TBB_${_comp}_FOUND TRUE)
++      else()
++        set(TBB_${_comp}_FOUND FALSE)
++      endif()
++
++      # Mark internal variables as advanced
++      mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
++      mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
++      mark_as_advanced(TBB_${_comp}_LIBRARY)
  
 -include(FindPackageHandleStandardArgs)
 -find_package_handle_standard_args(TBB
@@ -1646,10 +1688,79 @@ index bdf9c81..c6bdec9 100644
 -        INTERFACE_COMPILE_OPTIONS "${Tbb_DEFINITIONS}"
 -        INTERFACE_INCLUDE_DIRECTORIES "${Tbb_INCLUDE_DIR}"
 -      )
--    endif()
--  endforeach()
+     endif()
+   endforeach()
 -elseif(TBB_FIND_REQUIRED)
 -  message(FATAL_ERROR "Unable to find TBB")
++
++  ##################################
++  # Set compile flags and libraries
++  ##################################
++
++  set(TBB_DEFINITIONS_RELEASE "")
++  set(TBB_DEFINITIONS_DEBUG "TBB_USE_DEBUG=1")
++
++  if(TBB_LIBRARIES_${TBB_BUILD_TYPE})
++    set(TBB_LIBRARIES "${TBB_LIBRARIES_${TBB_BUILD_TYPE}}")
++  endif()
++
++  if(NOT MSVC AND NOT TBB_LIBRARIES)
++    set(TBB_LIBRARIES ${TBB_LIBRARIES_RELEASE})
++  endif()
++
++  set(TBB_DEFINITIONS "")
++  if (MSVC AND TBB_STATIC)
++    set(TBB_DEFINITIONS __TBB_NO_IMPLICIT_LINKAGE)
++  endif ()
++
++  unset (TBB_STATIC_SUFFIX)
++
++  find_package_handle_standard_args(TBB
++      REQUIRED_VARS TBB_INCLUDE_DIRS TBB_LIBRARIES
++      FAIL_MESSAGE "TBB library cannot be found. Consider set TBBROOT environment variable."
++      HANDLE_COMPONENTS
++      VERSION_VAR TBB_VERSION)
++
++  ##################################
++  # Create targets
++  ##################################
++
++  if(NOT CMAKE_VERSION VERSION_LESS 3.0 AND TBB_FOUND)
++    add_library(TBB::tbb UNKNOWN IMPORTED)
++    set_target_properties(TBB::tbb PROPERTIES
++          INTERFACE_COMPILE_DEFINITIONS "${TBB_DEFINITIONS}"
++          INTERFACE_LINK_LIBRARIES  "Threads::Threads;${CMAKE_DL_LIBS}"
++          INTERFACE_INCLUDE_DIRECTORIES  ${TBB_INCLUDE_DIRS}
++          IMPORTED_LOCATION              ${TBB_LIBRARIES})
++    if(TBB_LIBRARIES_RELEASE AND TBB_LIBRARIES_DEBUG)
++      set_target_properties(TBB::tbb PROPERTIES
++          INTERFACE_COMPILE_DEFINITIONS "${TBB_DEFINITIONS};$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:${TBB_DEFINITIONS_DEBUG}>;$<$<CONFIG:Release>:${TBB_DEFINITIONS_RELEASE}>"
++          IMPORTED_LOCATION_DEBUG          ${TBB_LIBRARIES_DEBUG}
++          IMPORTED_LOCATION_RELWITHDEBINFO ${TBB_LIBRARIES_RELEASE}
++          IMPORTED_LOCATION_RELEASE        ${TBB_LIBRARIES_RELEASE}
++          IMPORTED_LOCATION_MINSIZEREL     ${TBB_LIBRARIES_RELEASE}
++          )
++    endif()
++  endif()
++
++  mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES)
++
++  unset(TBB_ARCHITECTURE)
++  unset(TBB_BUILD_TYPE)
++  unset(TBB_LIB_PATH_SUFFIX)
++  unset(TBB_DEFAULT_SEARCH_DIR)
++
++  if(TBB_DEBUG)
++    message(STATUS "  TBB_FOUND               = ${TBB_FOUND}")
++    message(STATUS "  TBB_INCLUDE_DIRS        = ${TBB_INCLUDE_DIRS}")
++    message(STATUS "  TBB_DEFINITIONS         = ${TBB_DEFINITIONS}")
++    message(STATUS "  TBB_LIBRARIES           = ${TBB_LIBRARIES}")
++    message(STATUS "  TBB_DEFINITIONS_DEBUG   = ${TBB_DEFINITIONS_DEBUG}")
++    message(STATUS "  TBB_LIBRARIES_DEBUG     = ${TBB_LIBRARIES_DEBUG}")
++    message(STATUS "  TBB_DEFINITIONS_RELEASE = ${TBB_DEFINITIONS_RELEASE}")
++    message(STATUS "  TBB_LIBRARIES_RELEASE   = ${TBB_LIBRARIES_RELEASE}")
++  endif()
++
  endif()
 diff --git a/openvdb/CMakeLists.txt b/openvdb/CMakeLists.txt
 index 89301bd..df27aae 100644
@@ -1728,7 +1839,7 @@ index 20ad9a3..c2dd1ef 100644
  #endif // _WIN32
  
 diff --git a/openvdb/cmd/CMakeLists.txt b/openvdb/cmd/CMakeLists.txt
-index 57fbec0..55b3850 100644
+index 57fbec0..0379756 100644
 --- a/openvdb/cmd/CMakeLists.txt
 +++ b/openvdb/cmd/CMakeLists.txt
 @@ -74,8 +74,9 @@ if(WIN32)
@@ -1750,6 +1861,20 @@ index 57fbec0..55b3850 100644
    )
    if(OPENVDB_BUILD_CORE)
      list(APPEND RPATHS ${CMAKE_INSTALL_PREFIX}/lib)
+@@ -116,6 +116,13 @@ if(OPENVDB_BUILD_VDB_PRINT)
+     )
+   endif()
+ 
++  include(CheckAtomic)
++
++  check_working_cxx_atomics(HAS_ATOMIC)
++  if (NOT HAS_ATOMIC)
++      target_link_libraries(vdb_print atomic)
++  endif()
++
+   install(TARGETS vdb_print DESTINATION bin)
+ endif()
+ 
 diff --git a/openvdb/unittest/CMakeLists.txt b/openvdb/unittest/CMakeLists.txt
 index c9e0c34..7e261c0 100644
 --- a/openvdb/unittest/CMakeLists.txt
@@ -1779,5 +1904,5 @@ index df51830..0ab0c12 100644
          /// @todo This changes the compressor setting globally.
          if (blosc_set_compressor(compname) < 0) continue;
 -- 
-2.16.2.windows.1
+2.17.1