Andrii Danylchenko 4 ani în urmă
părinte
comite
b00a1dee28

+ 2 - 1
.github/workflows/github.yml

@@ -31,6 +31,7 @@ jobs:
             mxe: i686-w64-mingw32.shared
             mxe: i686-w64-mingw32.shared
             test: 0
             test: 0
             pack: 1
             pack: 1
+            cpack_args: -D CPACK_NSIS_EXECUTABLE=`which makensis`
             extension: exe
             extension: exe
             
             
     runs-on: ${{ matrix.os }}
     runs-on: ${{ matrix.os }}
@@ -85,7 +86,7 @@ jobs:
       if: ${{ matrix.pack == 1 }}
       if: ${{ matrix.pack == 1 }}
       run: |
       run: |
         cd ${{github.workspace}}/build
         cd ${{github.workspace}}/build
-        cpack
+        cpack ${{ matrix.cpack_args }}
         
         
     - name: Additional logs
     - name: Additional logs
       if: ${{ failure() && steps.cpack.outcome == 'failure' && matrix.platform == 'mxe' }}
       if: ${{ failure() && steps.cpack.outcome == 'failure' && matrix.platform == 'mxe' }}

+ 0 - 3
AI/Nullkiller/AIUtility.h

@@ -29,15 +29,12 @@
 
 
 /*********************** TBB.h ********************/
 /*********************** TBB.h ********************/
 
 
-#include "tbb/atomic.h"
 #include "tbb/blocked_range.h"
 #include "tbb/blocked_range.h"
 #include "tbb/concurrent_hash_map.h"
 #include "tbb/concurrent_hash_map.h"
 #include "tbb/concurrent_unordered_map.h"
 #include "tbb/concurrent_unordered_map.h"
 #include "tbb/concurrent_unordered_set.h"
 #include "tbb/concurrent_unordered_set.h"
 #include "tbb/concurrent_vector.h"
 #include "tbb/concurrent_vector.h"
-#include "tbb/parallel_do.h"
 #include "tbb/parallel_for.h"
 #include "tbb/parallel_for.h"
-#include "tbb/parallel_for_each.h"
 #include "tbb/parallel_invoke.h"
 #include "tbb/parallel_invoke.h"
 
 
 /*********************** TBB.h ********************/
 /*********************** TBB.h ********************/

+ 1 - 5
AI/Nullkiller/CMakeLists.txt

@@ -141,11 +141,7 @@ else()
 	target_link_libraries(Nullkiller PRIVATE fl-static vcmi)
 	target_link_libraries(Nullkiller PRIVATE fl-static vcmi)
 endif()
 endif()
 
 
-if(TBB_FOUND)
-	target_link_libraries(Nullkiller PRIVATE ${TBB_LIBRARIES})
-else()
-	target_link_libraries(Nullkiller PRIVATE tbb)
-endif()
+target_link_libraries(Nullkiller PRIVATE TBB::tbb)
 
 
 vcmi_set_output_dir(Nullkiller "AI")
 vcmi_set_output_dir(Nullkiller "AI")
 
 

+ 399 - 468
cmake_modules/FindTBB.cmake

@@ -1,135 +1,57 @@
-#.rst:
-# FindTBB
-# -------
+# - Find ThreadingBuildingBlocks include dirs and libraries
+# Use this module by invoking find_package with the form:
+#  find_package(TBB
+#    [REQUIRED]             # Fail with error if TBB is not found
+#    )                      #
+# Once done, this will define 
+#
+#  TBB_FOUND - system has TBB
+#  TBB_INCLUDE_DIRS - the TBB include directories
+#  TBB_LIBRARIES - TBB libraries to be lined, doesn't include malloc or
+#                  malloc proxy
+#  TBB::tbb - imported target for the TBB library
+#
+#  TBB_VERSION_MAJOR - Major Product Version Number
+#  TBB_VERSION_MINOR - Minor Product Version Number
+#  TBB_INTERFACE_VERSION - Engineering Focused Version Number
+#  TBB_COMPATIBLE_INTERFACE_VERSION - The oldest major interface version
+#                                     still supported. This uses the engineering
+#                                     focused interface version numbers.
+#
+#  TBB_MALLOC_FOUND - system has TBB malloc library
+#  TBB_MALLOC_INCLUDE_DIRS - the TBB malloc include directories
+#  TBB_MALLOC_LIBRARIES - The TBB malloc libraries to be lined
+#  TBB::malloc - imported target for the TBB malloc library
+#
+#  TBB_MALLOC_PROXY_FOUND - system has TBB malloc proxy library
+#  TBB_MALLOC_PROXY_INCLUDE_DIRS = the TBB malloc proxy include directories
+#  TBB_MALLOC_PROXY_LIBRARIES - The TBB malloc proxy libraries to be lined
+#  TBB::malloc_proxy - imported target for the TBB malloc proxy library
 #
 #
-# Find Intel's Threading Building Blocks (TBB) include path and libraries.
 #
 #
 # This module reads hints about search locations from variables:
 # This module reads hints about search locations from variables:
+#  ENV TBB_ARCH_PLATFORM - for eg. set it to "mic" for Xeon Phi builds
+#  ENV TBB_ROOT or just TBB_ROOT - root directory of tbb installation
+#  ENV TBB_BUILD_PREFIX - specifies the build prefix for user built tbb
+#                         libraries. Should be specified with ENV TBB_ROOT
+#                         and optionally...
+#  ENV TBB_BUILD_DIR - if build directory is different than ${TBB_ROOT}/build
 #
 #
-# ::
 #
 #
-#    TBB_ROOT                         - Root directory of pre-built TBB package.
-#                                       Can be an environment variable instead. It is
-#                                       derived from the found TBB_INCLUDE_DIR if unset.
-#    TBB_ARCH_PLATFORM                - Environment variable which can be used to specify
-#                                       architecture and platform specific library path
-#                                       suffix (excluding "/lib/" suffix or prefix).
-#                                       For MSVC, the appropriate link library path of the
-#                                       official pre-built download package from the TBB
-#                                       web site is chosen by this module. The path suffix
-#                                       derived from this variable takes precedence.
+# Modified by Robert Maynard from the original OGRE source
 #
 #
-# This module considers the following CMake variables set by find_package:
+#-------------------------------------------------------------------
+# This file is part of the CMake build system for OGRE
+#     (Object-oriented Graphics Rendering Engine)
+# For the latest info, see http://www.ogre3d.org/
 #
 #
-# ::
+# The contents of this file are placed in the public domain. Feel
+# free to make use of it in any way you like.
+#-------------------------------------------------------------------
 #
 #
-#    TBB_FIND_COMPONENTS              - Case-insensitive names of requested libraries:
-#                                       tbb, [tbb]malloc, [tbb]malloc_proxy
-#    TBB_FIND_REQUIRED_<C>            - Whether TBB library component <C> is required.
-#                                       TBB is considered to be not found when at least
-#                                       one required library or its include path is missing.
-#                                       When no TBB_FIND_COMPONENTS are specified, only the
-#                                       threading library "tbb" is required.
-#    TBB_FIND_REQUIRED                - Raise FATAL_ERROR when required components not found.
-#    TBB_FIND_QUIETLY                 - Suppress all other (status) messages.
-#
-# The TBB_DEBUG variable can be set to TRUE before find_package(TBB) to
-# enable verbose output which helps to debug the processing of this module:
-#
-# ::
-#
-#    set(TBB_DEBUG TRUE)
-#    find_package(TBB)
-#
-# This module defines the following variables:
-#
-# ::
-#
-#    TBB_FOUND                        - Whether TBB libraries were found.
-#    TBB_INCLUDE_DIR                  - TBB library include path where tbb/tbb_stddef.h is located.
-#                                       Used as HINTS for find_path of TBB_<C>_INCLUDE_DIR.
-#    TBB_INCLUDE_DIRS                 - Include paths of found TBB libraries.
-#    TBB_LIBRARIES                    - File paths of found TBB libraries.
-#    TBB_VERSION                      - Version for use in VERSION_LESS et al. comparisons.
-#    TBB_VERSION_MAJOR                - Major library version number.
-#    TBB_VERSION_MINOR                - Minor library version number.
-#    TBB_VERSION_STRING               - Version string for output messages.
-#    TBB_INTERFACE_VERSION            - API version number.
-#    TBB_COMPATIBLE_INTERFACE_VERSION - The oldest major version still supported.
-#
-# Additionally, for each requested component, this module defines the following variables:
-#
-# ::
-#
-#    TBB_TBB_FOUND                    - Whether TBB threading library was found.
-#    TBB_TBB_INCLUDE_DIR              - Include path of TBB threading library.
-#    TBB_TBB_INCLUDE_DIRS             - Include paths for use of TBB library.
-#    TBB_TBB_LIBRARIES                - TBB threading library and transitive link dependencies.
-#    TBB_TBB_LIBRARY_RELEASE          - File path of optimized TBB link library.
-#    TBB_TBB_LIBRARY_DEBUG            - File path of TBB link library with debug symbols.
-#    TBB_TBB_LIBRARY                  - File paths of both "optimized" and "debug" TBB threading link libraries.
-#                                       When only one of these is found, this variable is set to either
-#                                       TBB_TBB_LIBRARY_RELEASE or TBB_TBB_LIBRARY_DEBUG.
-#
-#    TBB_MALLOC_FOUND                 - Whether TBB malloc library was found.
-#    TBB_MALLOC_INCLUDE_DIR           - Include path of TBB malloc library.
-#    TBB_MALLOC_INCLUDE_DIRS          - Include paths for use of TBB malloc library.
-#    TBB_MALLOC_LIBRARIES             - TBB malloc library and transitive link dependencies.
-#    TBB_MALLOC_LIBRARY_RELEASE       - File path of optimized TBB malloc link library.
-#    TBB_MALLOC_LIBRARY_DEBUG         - File path of TBB malloc link library with debug symbols.
-#    TBB_MALLOC_LIBRARY               - File paths of both "optimized" and "debug" TBB malloc link libraries.
-#                                       When only one of these is found, this variable is set to either
-#                                       TBB_MALLOC_LIBRARY_RELEASE or TBB_MALLOC_LIBRARY_DEBUG.
-#
-#    TBB_MALLOC_PROXY_FOUND           - Whether TBB malloc proxy library was found.
-#    TBB_MALLOC_PROXY_INCLUDE_DIR     - Include path of TBB malloc proxy library.
-#    TBB_MALLOC_PROXY_INCLUDE_DIRS    - Include paths for use of TBB malloc proxy library.
-#    TBB_MALLOC_PROXY_LIBRARIES       - TBB malloc proxy library and transitive link dependencies.
-#    TBB_MALLOC_PROXY_LIBRARY_RELEASE - File path of optimized TBB malloc proxy link library.
-#    TBB_MALLOC_PROXY_LIBRARY_DEBUG   - File path of TBB malloc proxy link library with debug symbols.
-#    TBB_MALLOC_PROXY_LIBRARY         - File paths of both "optimized" and "debug" TBB malloc proxy link libraries.
-#                                       When only one of these is found, this variable is set to either
-#                                       TBB_MALLOC_PROXY_LIBRARY_RELEASE or TBB_MALLOC_PROXY_LIBRARY_DEBUG.
-#
-# of these, the following variables are added as advanced cache entries:
-#
-# ::
-#
-#    TBB_INCLUDE_DIR
-#    TBB_<C>_INCLUDE_DIR
-#    TBB_<C>_LIBRARY_RELEASE
-#    TBB_<C>_LIBRARY_DEBUG
-#
-# This module further defines the following import targets with the IMPORTED and INTERFACE
-# properties set appropriately such that only a target_link_libraries command is required
-# to declare the dependency of another target on the respective TBB library component.
-# The use of these import targets instead of above defined variables is recommended.
-#
-# ::
-#
-#    TBB::tbb          - TBB threading library.
-#    TBB::malloc       - TBB malloc library.
-#    TBB::malloc_proxy - TBB malloc proxy library.
-#
-# Example usage:
-#
-# ::
-#
-#    find_package(TBB REQUIRED COMPONENTS tbb OPTIONAL_COMPONENTS malloc)
-#
-#    add_executable(foo foo.cc)
-#    target_link_libraries(foo TBB::tbb)
-#    if (TARGET TBB::malloc)
-#    # or if (TBB_MALLOC_FOUND)
-#      target_link_libraries(foo TBB::malloc)
-#    endif ()
-#
-# This module was written by Andreas Schuh for CMake BASIS with inspiration
-# from the FindTBB module which was originally part of the Object-oriented
-# Graphics Rendering Engine (OGRE) project with modifications by Robert Maynard.
-
 #=============================================================================
 #=============================================================================
-# Copyright 2016 Andreas Schuh <[email protected]>
+# Copyright 2010-2012 Kitware, Inc.
+# Copyright 2012      Rolf Eike Beer <[email protected]>
 #
 #
 # Distributed under the OSI-approved BSD License (the "License");
 # Distributed under the OSI-approved BSD License (the "License");
 # see accompanying file Copyright.txt for details.
 # see accompanying file Copyright.txt for details.
@@ -141,394 +63,403 @@
 # (To distribute this file outside of CMake, substitute the full
 # (To distribute this file outside of CMake, substitute the full
 #  License text for the above reference.)
 #  License text for the above reference.)
 
 
-if (NOT TBB_FIND_QUIETLY)
-  set(_TBB_FIND_STATUS "Looking for TBB")
-  if (TBB_FIND_COMPONENTS)
-    set(_TBB_FIND_STATUS "${_TBB_FIND_STATUS} [${TBB_FIND_COMPONENTS}]")
-  endif ()
-  if (NOT TBB_FIND_REQUIRED)
-    set(_TBB_FIND_STATUS "${_TBB_FIND_STATUS} (optional)")
-  endif ()
-  message(STATUS "${_TBB_FIND_STATUS}...")
-endif ()
 
 
-# ------------------------------------------------------------------------------
-# Default required/optional components
-if (NOT TBB_FIND_COMPONENTS)
-  set(TBB_FIND_COMPONENTS tbb malloc malloc_proxy)
-  set(TBB_FIND_REQUIRED_tbb          TRUE)
-  set(TBB_FIND_REQUIRED_malloc       FALSE)
-  set(TBB_FIND_REQUIRED_malloc_proxy FALSE)
+#=============================================================================
+#  FindTBB helper functions and macros
+#
+message("** Looking for TBB...")
+
+# Use TBBConfig.cmake if possible.
+set(_tbb_find_quiet)
+
+if (TBB_FIND_QUIETLY)
+  set(_tbb_find_quiet QUIET)
 endif ()
 endif ()
 
 
-# ------------------------------------------------------------------------------
-# Normalize component names
-set(_TBB_FIND_COMPONENTS)
-foreach (__TBB_COMPONENT IN LISTS TBB_FIND_COMPONENTS)
-  string(TOUPPER "${__TBB_COMPONENT}" _TBB_COMPONENT)
-  string(REGEX REPLACE "^TBB_?([A-Z_]+)$" "\\1" _TBB_COMPONENT "${_TBB_COMPONENT}")
-  if (_TBB_COMPONENT MATCHES "^(TBB|MALLOC|MALLOC_PROXY)$")
-    set(_TBB_${_TBB_COMPONENT}_NAME ${__TBB_COMPONENT})
-    list(APPEND _TBB_FIND_COMPONENTS ${_TBB_COMPONENT})
-    if (TBB_FIND_REQUIRED_${__TBB_COMPONENT})
-      set(_TBB_FIND_REQUIRED_${_TBB_COMPONENT} TRUE)
-    else ()
-      set(_TBB_FIND_REQUIRED_${_TBB_COMPONENT} FALSE)
-    endif ()
+set(_tbb_find_components)
+set(_tbb_find_optional_components)
+
+foreach (_tbb_find_component IN LISTS TBB_FIND_COMPONENTS)
+  if (TBB_FIND_REQUIRED_${_tbb_find_component})
+	list(APPEND _tbb_find_components "${_tbb_find_component}")
   else ()
   else ()
-    message(FATAL_ERROR "Unknown TBB library component: ${__TBB_COMPONENT}\n"
-                        "Valid component names are: tbb, [tbb]malloc, [tbb]malloc_proxy")
+	list(APPEND _tbb_find_optional_components "${_tbb_find_component}")
   endif ()
   endif ()
 endforeach ()
 endforeach ()
-unset(__TBB_COMPONENT)
 
 
-if (TBB_DEBUG)
-  message("** FindTBB: Components = [${_TBB_FIND_COMPONENTS}]")
-endif ()
+unset(_tbb_find_component)
 
 
-# ------------------------------------------------------------------------------
-# Names of headers and libraries for each component
-set(_TBB_TBB_LIB_NAMES_RELEASE          tbb)
-set(_TBB_TBB_LIB_NAMES_DEBUG            tbb_debug)
-set(_TBB_TBB_INC_NAMES                  tbb/tbb.h)
-
-set(_TBB_MALLOC_LIB_NAMES_RELEASE       tbbmalloc)
-set(_TBB_MALLOC_LIB_NAMES_DEBUG         tbbmalloc_debug)
-set(_TBB_MALLOC_INC_NAMES               tbb/tbb.h)
-
-set(_TBB_MALLOC_PROXY_LIB_NAMES_RELEASE tbbmalloc_proxy)
-set(_TBB_MALLOC_PROXY_LIB_NAMES_DEBUG   tbbmalloc_proxy_debug)
-set(_TBB_MALLOC_PROXY_INC_NAMES         tbb/tbbmalloc_proxy.h)
-
-# ------------------------------------------------------------------------------
-# Transitive link dependencies
-set(_TBB_TBB_LIB_LINK_DEPENDS)
-set(_TBB_MALLOC_LIB_LINK_DEPENDS)
-set(_TBB_MALLOC_PROXY_LIB_LINK_DEPENDS)
-
-if (UNIX AND NOT APPLE)
-  # On Linux, the TBB threading library requires librt.so
-  list(APPEND _TBB_TBB_LIB_LINK_DEPENDS rt)
-endif ()
+find_package(TBB CONFIG ${_tbb_find_quiet}
+  COMPONENTS ${_tbb_find_components}
+  OPTIONAL_COMPONENTS ${_tbb_find_optional_components})
+  
+unset(_tbb_find_quiet)
+unset(_tbb_find_components)
+unset(_tbb_find_optional_components)
 
 
-# ------------------------------------------------------------------------------
-# Construct a set of search paths
-set(_TBB_ARCH_PLATFORM $ENV{TBB_ARCH_PLATFORM})
+set(TBB_LIBRARIES ${TBB_IMPORTED_TARGETS})
 
 
-if (NOT TBB_ROOT)
-  file(TO_CMAKE_PATH "$ENV{TBB_ROOT}" TBB_ROOT)
+if (TBB_FOUND)
+  return ()
 endif ()
 endif ()
 
 
-set(_TBB_INC_PATH_SUFFIXES include)
-
-set(_TBB_LIB_PATH_SUFFIXES)
-if (_TBB_ARCH_PLATFORM)
-  list(APPEND _TBB_LIB_PATH_SUFFIXES lib/${_TBB_ARCH_PLATFORM})
-  list(APPEND _TBB_LIB_PATH_SUFFIXES ${_TBB_ARCH_PLATFORM}/lib)
-endif ()
-list(APPEND _TBB_LIB_PATH_SUFFIXES lib)
-
-if (WIN32 AND MSVC AND CMAKE_GENERATOR MATCHES "Visual Studio ([0-9]+)")
-  set(_TBB_MSVS_VERSION ${CMAKE_MATCH_1})
-  if (CMAKE_CL_64)
-    list(APPEND _TBB_LIB_PATH_SUFFIXES lib/intel64/vc${_TBB_MSVS_VERSION})
-    list(APPEND _TBB_LIB_PATH_SUFFIXES intel64/vc${_TBB_MSVS_VERSION}/lib)
-    list(APPEND _TBB_LIB_PATH_SUFFIXES lib/ia64/vc${_TBB_MSVS_VERSION})
-    list(APPEND _TBB_LIB_PATH_SUFFIXES ia64/vc${_TBB_MSVS_VERSION}/lib)
+#====================================================
+# Fix the library path in case it is a linker script
+#====================================================
+function(tbb_extract_real_library library real_library)
+  if(NOT UNIX OR NOT EXISTS ${library})
+    set(${real_library} "${library}" PARENT_SCOPE)
+    return()
+  endif()
+
+  #Read in the first 4 bytes and see if they are the ELF magic number
+  set(_elf_magic "7f454c46")
+  file(READ ${library} _hex_data OFFSET 0 LIMIT 4 HEX)
+  if(_hex_data STREQUAL _elf_magic)
+    #we have opened a elf binary so this is what
+    #we should link to
+    set(${real_library} "${library}" PARENT_SCOPE)
+    return()
+  endif()
+
+  file(READ ${library} _data OFFSET 0 LIMIT 1024)
+  if("${_data}" MATCHES "INPUT \\(([^(]+)\\)")
+    #extract out the .so name from REGEX MATCH command
+    set(_proper_so_name "${CMAKE_MATCH_1}")
+
+    #construct path to the real .so which is presumed to be in the same directory
+    #as the input file
+    get_filename_component(_so_dir "${library}" DIRECTORY)
+    set(${real_library} "${_so_dir}/${_proper_so_name}" PARENT_SCOPE)
+  else()
+    #unable to determine what this library is so just hope everything works
+    #and pass it unmodified.
+    set(${real_library} "${library}" PARENT_SCOPE)
+  endif()
+endfunction()
+
+#===============================================
+# Do the final processing for the package find.
+#===============================================
+macro(findpkg_finish PREFIX TARGET_NAME)
+  if (${PREFIX}_INCLUDE_DIR AND ${PREFIX}_LIBRARY)
+    set(${PREFIX}_FOUND TRUE)
+    set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIR})
+    set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARY})
   else ()
   else ()
-    list(APPEND _TBB_LIB_PATH_SUFFIXES lib/ia32/vc${_TBB_MSVS_VERSION})
-    list(APPEND _TBB_LIB_PATH_SUFFIXES ia32/vc${_TBB_MSVS_VERSION}/lib)
+    if (${PREFIX}_FIND_REQUIRED AND NOT ${PREFIX}_FIND_QUIETLY)
+      message(FATAL_ERROR "Required library ${PREFIX} not found.")
+    endif ()
   endif ()
   endif ()
-  unset(_TBB_MSVS_VERSION)
-endif ()
 
 
-if (TBB_DEBUG)
-  message("** FindTBB: Initial search paths:")
-  message("** FindTBB: - Root directory hints  = [${TBB_ROOT}]")
-  message("** FindTBB: - Include path suffixes = [${_TBB_INC_PATH_SUFFIXES}]")
-  message("** FindTBB: - Library path suffixes = [${_TBB_LIB_PATH_SUFFIXES}]")
-endif ()
+  if (NOT TARGET "TBB::${TARGET_NAME}")
+    if (${PREFIX}_LIBRARY_RELEASE)
+      tbb_extract_real_library(${${PREFIX}_LIBRARY_RELEASE} real_release)
+    endif ()
+    if (${PREFIX}_LIBRARY_DEBUG)
+      tbb_extract_real_library(${${PREFIX}_LIBRARY_DEBUG} real_debug)
+    endif ()
+    add_library(TBB::${TARGET_NAME} UNKNOWN IMPORTED)
+    set_target_properties(TBB::${TARGET_NAME} PROPERTIES
+      INTERFACE_INCLUDE_DIRECTORIES "${${PREFIX}_INCLUDE_DIR}")
+    if (${PREFIX}_LIBRARY_DEBUG AND ${PREFIX}_LIBRARY_RELEASE)
+      set_target_properties(TBB::${TARGET_NAME} PROPERTIES
+        IMPORTED_LOCATION "${real_release}"
+        IMPORTED_LOCATION_DEBUG "${real_debug}"
+        IMPORTED_LOCATION_RELEASE "${real_release}")
+    elseif (${PREFIX}_LIBRARY_RELEASE)
+      set_target_properties(TBB::${TARGET_NAME} PROPERTIES
+        IMPORTED_LOCATION "${real_release}")
+    elseif (${PREFIX}_LIBRARY_DEBUG)
+      set_target_properties(TBB::${TARGET_NAME} PROPERTIES
+        IMPORTED_LOCATION "${real_debug}")
+    endif ()
+  endif ()
 
 
-# ------------------------------------------------------------------------------
-# Find common include directory 
-#
-# Looking for tbb/tbb_stddef.h because we use this path later to read this file
-# in order to extract the version information. The tbb.h header should be in the
-# same directory and is searched for separately as part of the "tbb" and "malloc"
-# component search. The TBB_INCLUDE_DIR is then used as HINTS.
-find_path(TBB_INCLUDE_DIR
-  NAMES tbb/tbb_stddef.h
-  HINTS ${TBB_ROOT}
-  PATH_SUFFIXES ${_TBB_INC_PATH_SUFFIXES}
-)
-
-mark_as_advanced(TBB_INCLUDE_DIR)
-
-# ------------------------------------------------------------------------------
-# Derive TBB_ROOT from TBB_INCLUDE_DIR if unset
-if (TBB_INCLUDE_DIR AND NOT TBB_ROOT)
-  if (_TBB_INC_PATH_SUFFIXES MATCHES "[^/;]/[^/;]")
-    string(LENGTH "${TBB_INCLUDE_DIR}" _TBB_INCLUDE_DIR_LENGTH)
-    foreach (_TBB_INC_PATH_SUFFIX IN LISTS _TBB_INC_PATH_SUFFIXES)
-      string(LENGTH "${_TBB_INC_PATH_SUFFIX}" _TBB_INC_PATH_SUFFIX_LENGTH)
-      if (_TBB_INC_PATH_SUFFIX_LENGTH GREATER 0)
-        math(EXPR _TBB_SUBSTRING_START "${_TBB_INCLUDE_DIR_LENGTH} - ${_TBB_INC_PATH_SUFFIX_LENGTH}")
-        string(SUBSTRING "${TBB_INCLUDE_DIR}" _TBB_SUBSTRING_START -1 _TBB_SUBSTRING)
-        if (_TBB_SUBSTRING STREQUAL _TBB_INC_PATH_SUFFIX)
-          if (_TBB_SUBSTRING_START GREATER 0)
-            string(SUBSTRING "${TBB_INCLUDE_DIR}" 0 _TBB_SUBSTRING_START TBB_ROOT)
-            string(REGEX REPLACE "/+$" "" TBB_ROOT "${TBB_ROOT}")
-          else ()
-            set(TBB_ROOT "/")
-          endif ()
-          break()
-        endif ()
-      endif ()
-    endforeach ()
-    unset(_TBB_SUBSTRING)
-    unset(_TBB_SUBSTRING_START)
-    unset(_TBB_INCLUDE_DIR_LENGTH)
-    unset(_TBB_INC_PATH_SUFFIX_LENGTH)
-  else ()
-    get_filename_component(TBB_ROOT "${TBB_INCLUDE_DIR}" DIRECTORY)
+  #mark the following variables as internal variables
+  mark_as_advanced(${PREFIX}_INCLUDE_DIR
+                   ${PREFIX}_LIBRARY
+                   ${PREFIX}_LIBRARY_DEBUG
+                   ${PREFIX}_LIBRARY_RELEASE)
+endmacro()
+
+#===============================================
+# Generate debug names from given release names
+#===============================================
+macro(get_debug_names PREFIX)
+  foreach(i ${${PREFIX}})
+    set(${PREFIX}_DEBUG ${${PREFIX}_DEBUG} ${i}d ${i}D ${i}_d ${i}_D ${i}_debug ${i})
+  endforeach()
+endmacro()
+
+#===============================================
+# See if we have env vars to help us find tbb
+#===============================================
+macro(getenv_path VAR)
+   set(ENV_${VAR} $ENV{${VAR}})
+   # replace won't work if var is blank
+   if (ENV_${VAR})
+     string( REGEX REPLACE "\\\\" "/" ENV_${VAR} ${ENV_${VAR}} )
+   endif ()
+endmacro()
+
+#===============================================
+# Couple a set of release AND debug libraries
+#===============================================
+macro(make_library_set PREFIX)
+  if (${PREFIX}_RELEASE AND ${PREFIX}_DEBUG)
+    set(${PREFIX} optimized ${${PREFIX}_RELEASE} debug ${${PREFIX}_DEBUG})
+  elseif (${PREFIX}_RELEASE)
+    set(${PREFIX} ${${PREFIX}_RELEASE})
+  elseif (${PREFIX}_DEBUG)
+    set(${PREFIX} ${${PREFIX}_DEBUG})
   endif ()
   endif ()
-endif ()
+endmacro()
 
 
-if (TBB_DEBUG)
-  message("** FindTBB: After initial search of TBB include path")
-  message("** FindTBB: - TBB_INCLUDE_DIR = ${TBB_INCLUDE_DIR}")
-  message("** FindTBB: - TBB_ROOT        = [${TBB_ROOT}]")
-endif ()
 
 
-# ------------------------------------------------------------------------------
-# Find library components
-set(TBB_INCLUDE_DIRS)
-set(TBB_LIBRARIES)
+#=============================================================================
+#  Now to actually find TBB
+#
 
 
-foreach (_TBB_COMPONENT IN LISTS _TBB_FIND_COMPONENTS)
-  if (TBB_DEBUG)
-    message("** FindTBB: Looking for component ${_TBB_COMPONENT}...")
-  endif ()
+# Get path, convert backslashes as ${ENV_${var}}
+getenv_path(TBB_ROOT)
 
 
-  # Find include path and library files of this component
-  find_path(TBB_${_TBB_COMPONENT}_INCLUDE_DIR
-    NAMES ${_TBB_${_TBB_COMPONENT}_INC_NAMES}
-    HINTS ${TBB_INCLUDE_DIR} ${TBB_ROOT}
-    PATH_SUFFIXES ${_TBB_INC_PATH_SUFFIXES}
-  )
-
-  find_library(TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE
-    NAMES ${_TBB_${_TBB_COMPONENT}_LIB_NAMES_RELEASE}
-    HINTS ${TBB_ROOT}
-    PATH_SUFFIXES ${_TBB_LIB_PATH_SUFFIXES}
-  )
-
-  find_library(TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG
-    NAMES ${_TBB_${_TBB_COMPONENT}_LIB_NAMES_DEBUG}
-    HINTS ${TBB_ROOT}
-    PATH_SUFFIXES ${_TBB_LIB_PATH_SUFFIXES}
-  )
-
-  if (TBB_DEBUG)
-    message("** FindTBB: - TBB_${_TBB_COMPONENT}_INCLUDE_DIR     = ${TBB_${_TBB_COMPONENT}_INCLUDE_DIR}")
-    message("** FindTBB: - TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE = ${TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE}")
-    message("** FindTBB: - TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG   = ${TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG}")
-  endif ()
+# initialize search paths
+set(TBB_PREFIX_PATH ${TBB_ROOT} ${ENV_TBB_ROOT})
+set(TBB_INC_SEARCH_PATH "")
+set(TBB_LIB_SEARCH_PATH "")
 
 
-  # Mark cache entries as advanced
-  mark_as_advanced(TBB_${_TBB_COMPONENT}_INCLUDE_DIR)
-  mark_as_advanced(TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE)
-  mark_as_advanced(TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG)
-
-  # Set TBB_<C>_LIBRARY
-  if (TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE AND TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG)
-    set(TBB_${_TBB_COMPONENT}_LIBRARY
-      optimized ${TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE}
-      debug     ${TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG}
-    )
-  elseif (TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE)
-    set(TBB_${_TBB_COMPONENT}_LIBRARY ${TBB_${_TBB_COMPONENT}_LIBRARY_RELEASE})
-  elseif (TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG)
-    set(TBB_${_TBB_COMPONENT}_LIBRARY ${TBB_${_TBB_COMPONENT}_LIBRARY_DEBUG})
-  else ()
-    set(TBB_${_TBB_COMPONENT}_LIBRARY TBB_${_TBB_COMPONENT}_LIBRARY-NOTFOUND)
-  endif ()
 
 
-  # Set TBB_<C>_FOUND
-  if (TBB_${_TBB_COMPONENT}_INCLUDE_DIR AND TBB_${_TBB_COMPONENT}_LIBRARY)
-    set(TBB_${_TBB_COMPONENT}_FOUND TRUE)
-  else ()
-    set(TBB_${_TBB_COMPONENT}_FOUND FALSE)
+# If user built from sources
+set(TBB_BUILD_PREFIX $ENV{TBB_BUILD_PREFIX})
+if (TBB_BUILD_PREFIX AND ENV_TBB_ROOT)
+  getenv_path(TBB_BUILD_DIR)
+  if (NOT ENV_TBB_BUILD_DIR)
+    set(ENV_TBB_BUILD_DIR ${ENV_TBB_ROOT}/build)
   endif ()
   endif ()
-  set(TBB_${_TBB_${_TBB_COMPONENT}_NAME}_FOUND ${TBB_${_TBB_COMPONENT}_FOUND})
 
 
-  if (TBB_${_TBB_COMPONENT}_FOUND)
+  # include directory under ${ENV_TBB_ROOT}/include
+  list(APPEND TBB_LIB_SEARCH_PATH
+    ${ENV_TBB_BUILD_DIR}/${TBB_BUILD_PREFIX}_release
+    ${ENV_TBB_BUILD_DIR}/${TBB_BUILD_PREFIX}_debug)
+endif ()
 
 
-    # Add transitive dependencies
-    set(TBB_${_TBB_COMPONENT}_INCLUDE_DIRS ${TBB_${_TBB_COMPONENT}_INCLUDE_DIR})
-    set(TBB_${_TBB_COMPONENT}_LIBRARIES    ${TBB_${_TBB_COMPONENT}_LIBRARY})
-    if (_TBB_${_TBB_COMPONENT}_LIB_LINK_DEPENDS)
-      list(APPEND TBB_${_TBB_COMPONENT}_LIBRARIES "${_TBB_${_TBB_COMPONENT}_LIB_LINK_DEPENDS}")
-    endif ()
 
 
-    if (TBB_DEBUG)
-      message("** FindTBB: - TBB_${_TBB_COMPONENT}_INCLUDE_DIRS    = [${TBB_${_TBB_COMPONENT}_INCLUDE_DIRS}]")
-      message("** FindTBB: - TBB_${_TBB_COMPONENT}_LIBRARIES       = [${TBB_${_TBB_COMPONENT}_LIBRARIES}]")
-    endif ()
+# For Windows, let's assume that the user might be using the precompiled
+# TBB packages from the main website. These use a rather awkward directory
+# structure (at least for automatically finding the right files) depending
+# on platform and compiler, but we'll do our best to accommodate it.
+# Not adding the same effort for the precompiled linux builds, though. Those
+# have different versions for CC compiler versions and linux kernels which
+# will never adequately match the user's setup, so there is no feasible way
+# to detect the "best" version to use. The user will have to manually
+# select the right files. (Chances are the distributions are shipping their
+# custom version of tbb, anyway, so the problem is probably nonexistent.)
+if (WIN32 AND MSVC)
+  set(COMPILER_PREFIX "vc7.1")
+  if (MSVC_VERSION EQUAL 1400)
+    set(COMPILER_PREFIX "vc8")
+  elseif(MSVC_VERSION EQUAL 1500)
+    set(COMPILER_PREFIX "vc9")
+  elseif(MSVC_VERSION EQUAL 1600)
+    set(COMPILER_PREFIX "vc10")
+  elseif(MSVC_VERSION EQUAL 1700)
+    set(COMPILER_PREFIX "vc11")
+  elseif(MSVC_VERSION EQUAL 1800)
+    set(COMPILER_PREFIX "vc12")
+  elseif(MSVC_VERSION GREATER_EQUAL 1900)
+    set(COMPILER_PREFIX "vc14")
+  endif ()
 
 
-    # Add to TBB_INCLUDE_DIRS and TBB_LIBRARIES
-    list(APPEND TBB_INCLUDE_DIRS ${TBB_${_TBB_COMPONENT}_INCLUDE_DIRS})
-    list(APPEND TBB_LIBRARIES    ${TBB_${_TBB_COMPONENT}_LIBRARIES})
-
-    # Add TBB::<C> import target
-    string(TOLOWER ${_TBB_COMPONENT} _TBB_TARGET_NAME)
-    set(_TBB_TARGET_NAME "TBB::${_TBB_TARGET_NAME}")
-    add_library(${_TBB_TARGET_NAME} SHARED IMPORTED)
-
-    set_target_properties(${_TBB_TARGET_NAME} PROPERTIES
-      INTERFACE_INCLUDE_DIRECTORIES     "${TBB_${_TBB_COMPONENT}_INCLUDE_DIRS}"
-      IMPORTED_LINK_INTERFACE_LANGUAGES CXX
-      IMPORTED_NO_SONAME                TRUE
-    )
-    if (_TBB_${_TBB_COMPONENT}_LIB_LINK_DEPENDS)
-      set_target_properties(${_TBB_TARGET_NAME} PROPERTIES
-        INTERFACE_LINK_LIBRARIES "${_TBB_${_TBB_COMPONENT}_LIB_LINK_DEPENDS}"
-      )
+  # for each prefix path, add ia32/64\${COMPILER_PREFIX}\lib to the lib search path
+  foreach (dir IN LISTS TBB_PREFIX_PATH)
+    if (CMAKE_CL_64)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/ia64/${COMPILER_PREFIX}/lib)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/ia64/${COMPILER_PREFIX})
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/intel64/${COMPILER_PREFIX}/lib)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/intel64/${COMPILER_PREFIX})
+    else ()
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/ia32/${COMPILER_PREFIX}/lib)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/ia32/${COMPILER_PREFIX})
     endif ()
     endif ()
+  endforeach ()
+endif ()
 
 
-    foreach (_TBB_CONFIGURATION IN ITEMS DEBUG RELEASE)
-      if (TBB_${_TBB_COMPONENT}_LIBRARY_${_TBB_CONFIGURATION})
-        set_property(TARGET ${_TBB_TARGET_NAME} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${_TBB_CONFIGURATION})
-        if (WIN32)
-          set_target_properties(${_TBB_TARGET_NAME} PROPERTIES
-            IMPORTED_IMPLIB_${_TBB_CONFIGURATION} "${TBB_${_TBB_COMPONENT}_LIBRARY_${_TBB_CONFIGURATION}}"
-          )
-          string(REPLACE       "/lib/"   "/bin/" _TBB_LIB_PATH_DLL "${TBB_${_TBB_COMPONENT}_LIBRARY_${_TBB_CONFIGURATION}}")
-          string(REGEX REPLACE "\\.lib$" ".dll"  _TBB_LIB_PATH_DLL "${_TBB_LIB_PATH_DLL}")
-          if (EXISTS "${_TBB_LIB_PATH_DLL}")
-            set_target_properties(${_TBB_TARGET_NAME} PROPERTIES
-              IMPORTED_LOCATION_${_TBB_CONFIGURATION} "${_TBB_LIB_PATH_DLL}"
-            )
-            if (TBB_DEBUG)
-              message("** FindTBB: - IMPORTED_LOCATION_${_TBB_CONFIGURATION} = ${_TBB_LIB_PATH_DLL}")
-            endif ()
-          elseif (TBB_DEBUG)
-            message("** FindTBB: Could not determine ${_TBB_CONFIGURATION} DLL path from import library, tried: "
-                    "\n\t${_TBB_LIB_PATH_DLL}")
-          endif ()
-        else ()
-          set_target_properties(${_TBB_TARGET_NAME} PROPERTIES
-            IMPORTED_LOCATION_${_TBB_CONFIGURATION} "${TBB_${_TBB_COMPONENT}_LIBRARY_${_TBB_CONFIGURATION}}"
-          )
-        endif ()
-      endif ()
-    endforeach ()
+# For OS X binary distribution, choose libc++ based libraries for Mavericks (10.9)
+# and above and AppleClang
+if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND
+    NOT CMAKE_SYSTEM_VERSION VERSION_LESS 13.0)
+  set (USE_LIBCXX OFF)
+  cmake_policy(GET CMP0025 POLICY_VAR)
 
 
-    if (TBB_DEBUG)
-      message("** FindTBB: Looking for component ${_TBB_COMPONENT}... - found")
+  if (POLICY_VAR STREQUAL "NEW")
+    if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
+      set (USE_LIBCXX ON)
     endif ()
     endif ()
-
   else ()
   else ()
-
-    if (TBB_DEBUG)
-      message("** FindTBB: Looking for component ${_TBB_COMPONENT}... - not found")
+    if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
+      set (USE_LIBCXX ON)
     endif ()
     endif ()
-    unset(TBB_${_TBB_COMPONENT}_INCLUDE_DIRS)
-    unset(TBB_${_TBB_COMPONENT}_LIBRARIES)
-
   endif ()
   endif ()
-endforeach ()
 
 
-if (TBB_INCLUDE_DIRS)
-  list(REMOVE_DUPLICATES TBB_INCLUDE_DIRS)
+  if (USE_LIBCXX)
+    foreach (dir IN LISTS TBB_PREFIX_PATH)
+      list (APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/libc++ ${dir}/libc++/lib)
+    endforeach ()
+  endif ()
 endif ()
 endif ()
 
 
-if (TBB_DEBUG)
-  message("** FindTBB: Include paths and libraries of all found components:")
-  message("** FindTBB: - TBB_INCLUDE_DIRS = [${TBB_INCLUDE_DIRS}]")
-  message("** FindTBB: - TBB_LIBRARIES    = [${TBB_LIBRARIES}]")
+# check compiler ABI
+if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+  set(COMPILER_PREFIX)
+  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
+    list(APPEND COMPILER_PREFIX "gcc4.8")
+  endif()
+  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
+    list(APPEND COMPILER_PREFIX "gcc4.7")
+  endif()
+  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.4)
+    list(APPEND COMPILER_PREFIX "gcc4.4")
+  endif()
+  list(APPEND COMPILER_PREFIX "gcc4.1")
+elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  set(COMPILER_PREFIX)
+  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.0) # Complete guess
+    list(APPEND COMPILER_PREFIX "gcc4.8")
+  endif()
+  if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.6)
+    list(APPEND COMPILER_PREFIX "gcc4.7")
+  endif()
+  list(APPEND COMPILER_PREFIX "gcc4.4")
+else() # Assume compatibility with 4.4 for other compilers
+  list(APPEND COMPILER_PREFIX "gcc4.4")
 endif ()
 endif ()
 
 
-# ------------------------------------------------------------------------------
-# Extract library version from start of tbb_stddef.h
-if (TBB_INCLUDE_DIR)
-  if (NOT DEFINED TBB_VERSION_MAJOR OR
-      NOT DEFINED TBB_VERSION_MINOR OR
-      NOT DEFINED TBB_INTERFACE_VERSION OR
-      NOT DEFINED TBB_COMPATIBLE_INTERFACE_VERSION)
-    file(READ "${TBB_INCLUDE_DIR}/tbb/tbb_stddef.h" _TBB_VERSION_CONTENTS LIMIT 2048)
-    string(REGEX REPLACE
-      ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
-      TBB_VERSION_MAJOR "${_TBB_VERSION_CONTENTS}"
-    )
-    string(REGEX REPLACE
-      ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
-      TBB_VERSION_MINOR "${_TBB_VERSION_CONTENTS}"
-    )
-    string(REGEX REPLACE
-      ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
-      TBB_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}"
-    )
-    string(REGEX REPLACE
-      ".*#define TBB_COMPATIBLE_INTERFACE_VERSION ([0-9]+).*" "\\1"
-      TBB_COMPATIBLE_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}"
-    )
-    unset(_TBB_VERSION_CONTENTS)
-  endif ()
-  set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
-  set(TBB_VERSION_STRING "${TBB_VERSION}")
-else ()
-  unset(TBB_VERSION)
-  unset(TBB_VERSION_MAJOR)
-  unset(TBB_VERSION_MINOR)
-  unset(TBB_VERSION_STRING)
-  unset(TBB_INTERFACE_VERSION)
-  unset(TBB_COMPATIBLE_INTERFACE_VERSION)
+# if platform architecture is explicitly specified
+set(TBB_ARCH_PLATFORM $ENV{TBB_ARCH_PLATFORM})
+if (TBB_ARCH_PLATFORM)
+  foreach (dir IN LISTS TBB_PREFIX_PATH)
+    list(APPEND TBB_LIB_SEARCH_PATH ${dir}/${TBB_ARCH_PLATFORM}/lib)
+    list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/${TBB_ARCH_PLATFORM})
+  endforeach ()
 endif ()
 endif ()
 
 
-if (TBB_DEBUG)
-  message("** FindTBB: Version information from ${TBB_INCLUDE_DIR}/tbb/tbb_stddef.h")
-  message("** FindTBB: - TBB_VERSION_STRING               = ${TBB_VERSION_STRING}")
-  message("** FindTBB: - TBB_VERSION_MAJOR                = ${TBB_VERSION_MAJOR}")
-  message("** FindTBB: - TBB_VERSION_MINOR                = ${TBB_VERSION_MINOR}")
-  message("** FindTBB: - TBB_INTERFACE_VERSION            = ${TBB_INTERFACE_VERSION}")
-  message("** FindTBB: - TBB_COMPATIBLE_INTERFACE_VERSION = ${TBB_COMPATIBLE_INTERFACE_VERSION}")
-endif ()
+foreach (dir IN LISTS TBB_PREFIX_PATH)
+  foreach (prefix IN LISTS COMPILER_PREFIX)
+    if (CMAKE_SIZEOF_VOID_P EQUAL 8)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/intel64)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/intel64/${prefix})
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/intel64/lib)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/intel64/${prefix}/lib)
+    else ()
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/ia32)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib/ia32/${prefix})
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/ia32/lib)
+      list(APPEND TBB_LIB_SEARCH_PATH ${dir}/ia32/${prefix}/lib)
+    endif ()
+  endforeach()
+endforeach ()
 
 
-# ------------------------------------------------------------------------------
-# Handle QUIET, REQUIRED, and [EXACT] VERSION arguments and set TBB_FOUND
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(TBB
-  REQUIRED_VARS TBB_INCLUDE_DIR
-  VERSION_VAR   TBB_VERSION
-  HANDLE_COMPONENTS
-)
-
-if (NOT TBB_FIND_QUIETLY)
-  if (TBB_FOUND)
-    message(STATUS "${_TBB_FIND_STATUS}... - found v${TBB_VERSION_STRING}")
-  else ()
-    message(STATUS "${_TBB_FIND_STATUS}... - not found")
-  endif ()
+# add general search paths
+foreach (dir IN LISTS TBB_PREFIX_PATH)
+  list(APPEND TBB_LIB_SEARCH_PATH ${dir}/lib ${dir}/Lib ${dir}/lib/tbb
+    ${dir}/Libs)
+  list(APPEND TBB_INC_SEARCH_PATH ${dir}/include ${dir}/Include
+    ${dir}/include/tbb)
+endforeach ()
+
+set(TBB_LIBRARY_NAMES tbb)
+get_debug_names(TBB_LIBRARY_NAMES)
+
+
+find_path(TBB_INCLUDE_DIR
+          NAMES tbb/tbb_stddef.h
+          PATHS ${TBB_INC_SEARCH_PATH})
+
+find_library(TBB_LIBRARY_RELEASE
+             NAMES ${TBB_LIBRARY_NAMES}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+find_library(TBB_LIBRARY_DEBUG
+             NAMES ${TBB_LIBRARY_NAMES_DEBUG}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+make_library_set(TBB_LIBRARY)
+
+findpkg_finish(TBB tbb)
+
+#if we haven't found TBB no point on going any further
+if (NOT TBB_FOUND)
+  return()
 endif ()
 endif ()
 
 
-# ------------------------------------------------------------------------------
-# Unset local auxiliary variables
-foreach (_TBB_COMPONENT IN ITEMS TBB MALLOC MALLOC_PROXY)
-  unset(_TBB_FIND_REQUIRED_${_TBB_COMPONENT})
-  unset(_TBB_${_TBB_COMPONENT}_LIB_NAMES_RELEASE)
-  unset(_TBB_${_TBB_COMPONENT}_LIB_NAMES_DEBUG)
-  unset(_TBB_${_TBB_COMPONENT}_LIB_LINK_DEPENDS)
-  unset(_TBB_${_TBB_COMPONENT}_INC_NAMES)
-  unset(_TBB_${_TBB_COMPONENT}_NAME)
-endforeach ()
+#=============================================================================
+# Look for TBB's malloc package
+set(TBB_MALLOC_LIBRARY_NAMES tbbmalloc)
+get_debug_names(TBB_MALLOC_LIBRARY_NAMES)
+
+find_path(TBB_MALLOC_INCLUDE_DIR
+          NAMES tbb/tbb.h
+          PATHS ${TBB_INC_SEARCH_PATH})
+
+find_library(TBB_MALLOC_LIBRARY_RELEASE
+             NAMES ${TBB_MALLOC_LIBRARY_NAMES}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+find_library(TBB_MALLOC_LIBRARY_DEBUG
+             NAMES ${TBB_MALLOC_LIBRARY_NAMES_DEBUG}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+make_library_set(TBB_MALLOC_LIBRARY)
+
+findpkg_finish(TBB_MALLOC tbbmalloc)
+
+#=============================================================================
+# Look for TBB's malloc proxy package
+set(TBB_MALLOC_PROXY_LIBRARY_NAMES tbbmalloc_proxy)
+get_debug_names(TBB_MALLOC_PROXY_LIBRARY_NAMES)
 
 
-unset(_TBB_COMPONENT)
-unset(_TBB_TARGET_NAME)
-unset(_TBB_FIND_COMPONENTS)
-unset(_TBB_FIND_STATUS)
-unset(_TBB_INC_PATH_SUFFIXES)
-unset(_TBB_LIB_PATH_SUFFIXES)
-unset(_TBB_LIB_PATH_DLL)
-unset(_TBB_LIB_NAME)
-unset(_TBB_ARCH_PLATFORM)
+find_path(TBB_MALLOC_PROXY_INCLUDE_DIR
+          NAMES tbb/tbbmalloc_proxy.h
+          PATHS ${TBB_INC_SEARCH_PATH})
+
+find_library(TBB_MALLOC_PROXY_LIBRARY_RELEASE
+             NAMES ${TBB_MALLOC_PROXY_LIBRARY_NAMES}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+find_library(TBB_MALLOC_PROXY_LIBRARY_DEBUG
+             NAMES ${TBB_MALLOC_PROXY_LIBRARY_NAMES_DEBUG}
+             PATHS ${TBB_LIB_SEARCH_PATH})
+make_library_set(TBB_MALLOC_PROXY_LIBRARY)
+
+findpkg_finish(TBB_MALLOC_PROXY tbbmalloc_proxy)
+
+
+#=============================================================================
+#parse all the version numbers from tbb
+if(NOT TBB_VERSION)
+  if (EXISTS "${TBB_INCLUDE_DIR}/oneapi/tbb/version.h")
+    file(STRINGS
+      "${TBB_INCLUDE_DIR}/oneapi/tbb/version.h"
+      TBB_VERSION_CONTENTS
+      REGEX "VERSION")
+  else()
+    #only read the start of the file
+    file(STRINGS
+      "${TBB_INCLUDE_DIR}/tbb/tbb_stddef.h"
+      TBB_VERSION_CONTENTS
+      REGEX "VERSION")
+  endif()
+
+  string(REGEX REPLACE
+    ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
+    TBB_VERSION_MAJOR "${TBB_VERSION_CONTENTS}")
+
+  string(REGEX REPLACE
+    ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
+    TBB_VERSION_MINOR "${TBB_VERSION_CONTENTS}")
+
+  string(REGEX REPLACE
+        ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
+        TBB_INTERFACE_VERSION "${TBB_VERSION_CONTENTS}")
+
+  string(REGEX REPLACE
+        ".*#define TBB_COMPATIBLE_INTERFACE_VERSION ([0-9]+).*" "\\1"
+        TBB_COMPATIBLE_INTERFACE_VERSION "${TBB_VERSION_CONTENTS}")
+
+endif()