FindTBB.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # Locate Intel Threading Building Blocks include paths and libraries
  2. # FindTBB.cmake can be found at https://code.google.com/p/findtbb/
  3. # Written by Hannes Hofmann <hannes.hofmann _at_ informatik.uni-erlangen.de>
  4. # Improvements by Gino van den Bergen <gino _at_ dtecta.com>,
  5. # Florian Uhlig <F.Uhlig _at_ gsi.de>,
  6. # Jiri Marsik <jiri.marsik89 _at_ gmail.com>
  7. # The MIT License
  8. #
  9. # Copyright (c) 2011 Hannes Hofmann
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documentation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furnished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. # GvdB: This module uses the environment variable TBB_ARCH_PLATFORM which defines architecture and compiler.
  29. # e.g. "ia32/vc8" or "em64t/cc4.1.0_libc2.4_kernel2.6.16.21"
  30. # TBB_ARCH_PLATFORM is set by the build script `tbbvars[.bat|.sh|.csh], which can be found
  31. # in the TBB installation directory (TBB_INSTALL_DIR).
  32. #
  33. # GvdB: Mac OS X distribution places libraries directly in lib directory.
  34. #
  35. # For backwards compatibility, you may explicitely set the CMake variables TBB_ARCHITECTURE and TBB_COMPILER.
  36. # TBB_ARCHITECTURE [ ia32 | em64t | itanium ]
  37. # which architecture to use
  38. # TBB_COMPILER e.g. vc9 or cc3.2.3_libc2.3.2_kernel2.4.21 or cc4.0.1_os10.4.9
  39. # which compiler to use (detected automatically on Windows)
  40. # This module respects
  41. # TBB_INSTALL_DIR or $ENV{TBB21_INSTALL_DIR} or $ENV{TBB_INSTALL_DIR}
  42. # This module defines
  43. # TBB_INCLUDE_DIRS, where to find task_scheduler_init.h, etc.
  44. # TBB_LIBRARY_DIRS, where to find libtbb, libtbbmalloc
  45. # TBB_DEBUG_LIBRARY_DIRS, where to find libtbb_debug, libtbbmalloc_debug
  46. # TBB_INSTALL_DIR, the base TBB install directory
  47. # TBB_LIBRARIES, the libraries to link against to use TBB.
  48. # TBB_DEBUG_LIBRARIES, the libraries to link against to use TBB with debug symbols.
  49. # TBB_FOUND, If false, don't try to use TBB.
  50. # TBB_INTERFACE_VERSION, as defined in tbb/tbb_stddef.h
  51. if (WIN32)
  52. # has em64t/vc8 em64t/vc9
  53. # has ia32/vc7.1 ia32/vc8 ia32/vc9
  54. set(_TBB_DEFAULT_INSTALL_DIR "C:/Program Files/Intel/TBB" "C:/Program Files (x86)/Intel/TBB")
  55. set(_TBB_LIB_NAME "tbb")
  56. set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
  57. set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
  58. set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
  59. if (MSVC71)
  60. set (_TBB_COMPILER "vc7.1")
  61. endif(MSVC71)
  62. if (MSVC80)
  63. set(_TBB_COMPILER "vc8")
  64. endif(MSVC80)
  65. if (MSVC90)
  66. set(_TBB_COMPILER "vc9")
  67. endif(MSVC90)
  68. if(MSVC10)
  69. set(_TBB_COMPILER "vc10")
  70. endif(MSVC10)
  71. # Todo: add other Windows compilers such as ICL.
  72. set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
  73. endif (WIN32)
  74. if (UNIX)
  75. if (APPLE)
  76. # MAC
  77. set(_TBB_DEFAULT_INSTALL_DIR "/Library/Frameworks/Intel_TBB.framework/Versions")
  78. # libs: libtbb.dylib, libtbbmalloc.dylib, *_debug
  79. set(_TBB_LIB_NAME "tbb")
  80. set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
  81. set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
  82. set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
  83. # default flavor on apple: ia32/cc4.0.1_os10.4.9
  84. # Jiri: There is no reason to presume there is only one flavor and
  85. # that user's setting of variables should be ignored.
  86. if(NOT TBB_COMPILER)
  87. set(_TBB_COMPILER "cc4.0.1_os10.4.9")
  88. elseif (NOT TBB_COMPILER)
  89. set(_TBB_COMPILER ${TBB_COMPILER})
  90. endif(NOT TBB_COMPILER)
  91. if(NOT TBB_ARCHITECTURE)
  92. set(_TBB_ARCHITECTURE "ia32")
  93. elseif(NOT TBB_ARCHITECTURE)
  94. set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
  95. endif(NOT TBB_ARCHITECTURE)
  96. else (APPLE)
  97. # LINUX
  98. set(_TBB_DEFAULT_INSTALL_DIR "/opt/intel/tbb" "/usr/local/include" "/usr/include")
  99. set(_TBB_LIB_NAME "tbb")
  100. set(_TBB_LIB_MALLOC_NAME "${_TBB_LIB_NAME}malloc")
  101. set(_TBB_LIB_DEBUG_NAME "${_TBB_LIB_NAME}_debug")
  102. set(_TBB_LIB_MALLOC_DEBUG_NAME "${_TBB_LIB_MALLOC_NAME}_debug")
  103. # has em64t/cc3.2.3_libc2.3.2_kernel2.4.21 em64t/cc3.3.3_libc2.3.3_kernel2.6.5 em64t/cc3.4.3_libc2.3.4_kernel2.6.9 em64t/cc4.1.0_libc2.4_kernel2.6.16.21
  104. # has ia32/*
  105. # has itanium/*
  106. set(_TBB_COMPILER ${TBB_COMPILER})
  107. set(_TBB_ARCHITECTURE ${TBB_ARCHITECTURE})
  108. endif (APPLE)
  109. endif (UNIX)
  110. if (CMAKE_SYSTEM MATCHES "SunOS.*")
  111. # SUN
  112. # not yet supported
  113. # has em64t/cc3.4.3_kernel5.10
  114. # has ia32/*
  115. endif (CMAKE_SYSTEM MATCHES "SunOS.*")
  116. #-- Clear the public variables
  117. set (TBB_FOUND "NO")
  118. #-- Find TBB install dir and set ${_TBB_INSTALL_DIR} and cached ${TBB_INSTALL_DIR}
  119. # first: use CMake variable TBB_INSTALL_DIR
  120. if (TBB_INSTALL_DIR)
  121. set (_TBB_INSTALL_DIR ${TBB_INSTALL_DIR})
  122. endif (TBB_INSTALL_DIR)
  123. # second: use environment variable
  124. if (NOT _TBB_INSTALL_DIR)
  125. if (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "")
  126. set (_TBB_INSTALL_DIR $ENV{TBB_INSTALL_DIR})
  127. endif (NOT "$ENV{TBB_INSTALL_DIR}" STREQUAL "")
  128. # Intel recommends setting TBB21_INSTALL_DIR
  129. if (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "")
  130. set (_TBB_INSTALL_DIR $ENV{TBB21_INSTALL_DIR})
  131. endif (NOT "$ENV{TBB21_INSTALL_DIR}" STREQUAL "")
  132. if (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "")
  133. set (_TBB_INSTALL_DIR $ENV{TBB22_INSTALL_DIR})
  134. endif (NOT "$ENV{TBB22_INSTALL_DIR}" STREQUAL "")
  135. if (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "")
  136. set (_TBB_INSTALL_DIR $ENV{TBB30_INSTALL_DIR})
  137. endif (NOT "$ENV{TBB30_INSTALL_DIR}" STREQUAL "")
  138. endif (NOT _TBB_INSTALL_DIR)
  139. # third: try to find path automatically
  140. if (NOT _TBB_INSTALL_DIR)
  141. if (_TBB_DEFAULT_INSTALL_DIR)
  142. set (_TBB_INSTALL_DIR ${_TBB_DEFAULT_INSTALL_DIR})
  143. endif (_TBB_DEFAULT_INSTALL_DIR)
  144. endif (NOT _TBB_INSTALL_DIR)
  145. # sanity check
  146. if (NOT _TBB_INSTALL_DIR)
  147. message ("ERROR: Unable to find Intel TBB install directory. ${_TBB_INSTALL_DIR}")
  148. else (NOT _TBB_INSTALL_DIR)
  149. # finally: set the cached CMake variable TBB_INSTALL_DIR
  150. if (NOT TBB_INSTALL_DIR)
  151. set (TBB_INSTALL_DIR ${_TBB_INSTALL_DIR} CACHE PATH "Intel TBB install directory")
  152. mark_as_advanced(TBB_INSTALL_DIR)
  153. endif (NOT TBB_INSTALL_DIR)
  154. #-- A macro to rewrite the paths of the library. This is necessary, because
  155. # find_library() always found the em64t/vc9 version of the TBB libs
  156. macro(TBB_CORRECT_LIB_DIR var_name)
  157. # if (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t")
  158. string(REPLACE em64t "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
  159. # endif (NOT "${_TBB_ARCHITECTURE}" STREQUAL "em64t")
  160. string(REPLACE ia32 "${_TBB_ARCHITECTURE}" ${var_name} ${${var_name}})
  161. string(REPLACE vc7.1 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
  162. string(REPLACE vc8 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
  163. string(REPLACE vc9 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
  164. string(REPLACE vc10 "${_TBB_COMPILER}" ${var_name} ${${var_name}})
  165. endmacro(TBB_CORRECT_LIB_DIR var_content)
  166. #-- Look for include directory and set ${TBB_INCLUDE_DIR}
  167. set (TBB_INC_SEARCH_DIR ${_TBB_INSTALL_DIR}/include)
  168. # Jiri: tbbvars now sets the CPATH environment variable to the directory
  169. # containing the headers.
  170. find_path(TBB_INCLUDE_DIR
  171. tbb/task_scheduler_init.h
  172. PATHS ${TBB_INC_SEARCH_DIR} ENV CPATH
  173. )
  174. mark_as_advanced(TBB_INCLUDE_DIR)
  175. #-- Look for libraries
  176. # GvdB: $ENV{TBB_ARCH_PLATFORM} is set by the build script tbbvars[.bat|.sh|.csh]
  177. if (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "")
  178. set (_TBB_LIBRARY_DIR
  179. ${_TBB_INSTALL_DIR}/lib/$ENV{TBB_ARCH_PLATFORM}
  180. ${_TBB_INSTALL_DIR}/$ENV{TBB_ARCH_PLATFORM}/lib
  181. )
  182. endif (NOT $ENV{TBB_ARCH_PLATFORM} STREQUAL "")
  183. # Jiri: This block isn't mutually exclusive with the previous one
  184. # (hence no else), instead I test if the user really specified
  185. # the variables in question.
  186. if ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL ""))
  187. # HH: deprecated
  188. message(STATUS "[Warning] FindTBB.cmake: The use of TBB_ARCHITECTURE and TBB_COMPILER is deprecated and may not be supported in future versions. Please set \$ENV{TBB_ARCH_PLATFORM} (using tbbvars.[bat|csh|sh]).")
  189. # Jiri: It doesn't hurt to look in more places, so I store the hints from
  190. # ENV{TBB_ARCH_PLATFORM} and the TBB_ARCHITECTURE and TBB_COMPILER
  191. # variables and search them both.
  192. set (_TBB_LIBRARY_DIR "${_TBB_INSTALL_DIR}/${_TBB_ARCHITECTURE}/${_TBB_COMPILER}/lib" ${_TBB_LIBRARY_DIR})
  193. endif ((NOT ${TBB_ARCHITECTURE} STREQUAL "") AND (NOT ${TBB_COMPILER} STREQUAL ""))
  194. # GvdB: Mac OS X distribution places libraries directly in lib directory.
  195. list(APPEND _TBB_LIBRARY_DIR ${_TBB_INSTALL_DIR}/lib)
  196. # Jiri: No reason not to check the default paths. From recent versions,
  197. # tbbvars has started exporting the LIBRARY_PATH and LD_LIBRARY_PATH
  198. # variables, which now point to the directories of the lib files.
  199. # It all makes more sense to use the ${_TBB_LIBRARY_DIR} as a HINTS
  200. # argument instead of the implicit PATHS as it isn't hard-coded
  201. # but computed by system introspection. Searching the LIBRARY_PATH
  202. # and LD_LIBRARY_PATH environment variables is now even more important
  203. # that tbbvars doesn't export TBB_ARCH_PLATFORM and it facilitates
  204. # the use of TBB built from sources.
  205. find_library(TBB_LIBRARY ${_TBB_LIB_NAME} HINTS ${_TBB_LIBRARY_DIR}
  206. PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
  207. find_library(TBB_MALLOC_LIBRARY ${_TBB_LIB_MALLOC_NAME} HINTS ${_TBB_LIBRARY_DIR}
  208. PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
  209. #Extract path from TBB_LIBRARY name
  210. get_filename_component(TBB_LIBRARY_DIR ${TBB_LIBRARY} PATH)
  211. #TBB_CORRECT_LIB_DIR(TBB_LIBRARY)
  212. #TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY)
  213. mark_as_advanced(TBB_LIBRARY TBB_MALLOC_LIBRARY)
  214. #-- Look for debug libraries
  215. # Jiri: Changed the same way as for the release libraries.
  216. find_library(TBB_LIBRARY_DEBUG ${_TBB_LIB_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
  217. PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
  218. find_library(TBB_MALLOC_LIBRARY_DEBUG ${_TBB_LIB_MALLOC_DEBUG_NAME} HINTS ${_TBB_LIBRARY_DIR}
  219. PATHS ENV LIBRARY_PATH ENV LD_LIBRARY_PATH)
  220. # Jiri: Self-built TBB stores the debug libraries in a separate directory.
  221. # Extract path from TBB_LIBRARY_DEBUG name
  222. get_filename_component(TBB_LIBRARY_DEBUG_DIR ${TBB_LIBRARY_DEBUG} PATH)
  223. #TBB_CORRECT_LIB_DIR(TBB_LIBRARY_DEBUG)
  224. #TBB_CORRECT_LIB_DIR(TBB_MALLOC_LIBRARY_DEBUG)
  225. mark_as_advanced(TBB_LIBRARY_DEBUG TBB_MALLOC_LIBRARY_DEBUG)
  226. if (TBB_INCLUDE_DIR)
  227. if (TBB_LIBRARY)
  228. set (TBB_FOUND "YES")
  229. set (TBB_LIBRARIES ${TBB_LIBRARY} ${TBB_MALLOC_LIBRARY} ${TBB_LIBRARIES})
  230. set (TBB_DEBUG_LIBRARIES ${TBB_LIBRARY_DEBUG} ${TBB_MALLOC_LIBRARY_DEBUG} ${TBB_DEBUG_LIBRARIES})
  231. set (TBB_INCLUDE_DIRS ${TBB_INCLUDE_DIR} CACHE PATH "TBB include directory" FORCE)
  232. set (TBB_LIBRARY_DIRS ${TBB_LIBRARY_DIR} CACHE PATH "TBB library directory" FORCE)
  233. # Jiri: Self-built TBB stores the debug libraries in a separate directory.
  234. set (TBB_DEBUG_LIBRARY_DIRS ${TBB_LIBRARY_DEBUG_DIR} CACHE PATH "TBB debug library directory" FORCE)
  235. mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARY_DIRS TBB_DEBUG_LIBRARY_DIRS TBB_LIBRARIES TBB_DEBUG_LIBRARIES)
  236. message(STATUS "Found Intel TBB")
  237. endif (TBB_LIBRARY)
  238. endif (TBB_INCLUDE_DIR)
  239. if (NOT TBB_FOUND)
  240. message("ERROR: Intel TBB NOT found!")
  241. message(STATUS "Looked for Threading Building Blocks in ${_TBB_INSTALL_DIR}")
  242. # do only throw fatal, if this pkg is REQUIRED
  243. if (TBB_FIND_REQUIRED)
  244. message(FATAL_ERROR "Could NOT find TBB library.
  245. On ubuntu try
  246. apt-get install libtbb-dev
  247. ")
  248. endif (TBB_FIND_REQUIRED)
  249. endif (NOT TBB_FOUND)
  250. endif (NOT _TBB_INSTALL_DIR)
  251. if (TBB_FOUND)
  252. set(TBB_INTERFACE_VERSION 0)
  253. FILE(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _TBB_VERSION_CONTENTS)
  254. STRING(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_TBB_VERSION_CONTENTS}")
  255. set(TBB_INTERFACE_VERSION "${TBB_INTERFACE_VERSION}")
  256. endif (TBB_FOUND)