FindTBB.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2015 Justus Calvin
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. #
  23. # FindTBB
  24. # -------
  25. #
  26. # Find TBB include directories and libraries.
  27. #
  28. # Usage:
  29. #
  30. # find_package(TBB [major[.minor]] [EXACT]
  31. # [QUIET] [REQUIRED]
  32. # [[COMPONENTS] [components...]]
  33. # [OPTIONAL_COMPONENTS components...])
  34. #
  35. # where the allowed components are tbbmalloc and tbb_preview. Users may modify
  36. # the behavior of this module with the following variables:
  37. #
  38. # * TBB_ROOT_DIR - The base directory the of TBB installation.
  39. # * TBB_INCLUDE_DIR - The directory that contains the TBB headers files.
  40. # * TBB_LIBRARY - The directory that contains the TBB library files.
  41. # * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library.
  42. # These libraries, if specified, override the
  43. # corresponding library search results, where <library>
  44. # may be tbb, tbb_debug, tbbmalloc, tbbmalloc_debug,
  45. # tbb_preview, or tbb_preview_debug.
  46. # * TBB_USE_DEBUG_BUILD - The debug version of tbb libraries, if present, will
  47. # be used instead of the release version.
  48. # * TBB_STATIC - Static linking of libraries with a _static suffix.
  49. # For example, on Windows a tbb_static.lib will be searched for
  50. # instead of tbb.lib.
  51. #
  52. # Users may modify the behavior of this module with the following environment
  53. # variables:
  54. #
  55. # * TBB_INSTALL_DIR
  56. # * TBBROOT
  57. # * LIBRARY_PATH
  58. #
  59. # This module will set the following variables:
  60. #
  61. # * TBB_FOUND - Set to false, or undefined, if we haven’t found, or
  62. # don’t want to use TBB.
  63. # * TBB_<component>_FOUND - If False, optional <component> part of TBB sytem is
  64. # not available.
  65. # * TBB_VERSION - The full version string
  66. # * TBB_VERSION_MAJOR - The major version
  67. # * TBB_VERSION_MINOR - The minor version
  68. # * TBB_INTERFACE_VERSION - The interface version number defined in
  69. # tbb/tbb_stddef.h.
  70. # * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of
  71. # <library>, where <library> may be tbb, tbb_debug,
  72. # tbbmalloc, tbbmalloc_debug, tbb_preview, or
  73. # tbb_preview_debug.
  74. # * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of
  75. # <library>, where <library> may be tbb, tbb_debug,
  76. # tbbmalloc, tbbmalloc_debug, tbb_preview, or
  77. # tbb_preview_debug.
  78. #
  79. # The following varibles should be used to build and link with TBB:
  80. #
  81. # * TBB_INCLUDE_DIRS - The include directory for TBB.
  82. # * TBB_LIBRARIES - The libraries to link against to use TBB.
  83. # * TBB_LIBRARIES_RELEASE - The release libraries to link against to use TBB.
  84. # * TBB_LIBRARIES_DEBUG - The debug libraries to link against to use TBB.
  85. # * TBB_DEFINITIONS - Definitions to use when compiling code that uses
  86. # TBB.
  87. # * TBB_DEFINITIONS_RELEASE - Definitions to use when compiling release code that
  88. # uses TBB.
  89. # * TBB_DEFINITIONS_DEBUG - Definitions to use when compiling debug code that
  90. # uses TBB.
  91. #
  92. # This module will also create the "tbb" target that may be used when building
  93. # executables and libraries.
  94. include(FindPackageHandleStandardArgs)
  95. if(NOT TBB_FOUND)
  96. ##################################
  97. # Check the build type
  98. ##################################
  99. if(NOT DEFINED TBB_USE_DEBUG_BUILD)
  100. if(CMAKE_BUILD_TYPE MATCHES "(Debug|DEBUG|debug)")
  101. set(TBB_BUILD_TYPE DEBUG)
  102. else()
  103. set(TBB_BUILD_TYPE RELEASE)
  104. endif()
  105. elseif(TBB_USE_DEBUG_BUILD)
  106. set(TBB_BUILD_TYPE DEBUG)
  107. else()
  108. set(TBB_BUILD_TYPE RELEASE)
  109. endif()
  110. ##################################
  111. # Set the TBB search directories
  112. ##################################
  113. # Define search paths based on user input and environment variables
  114. set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
  115. # Define the search directories based on the current platform
  116. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  117. set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
  118. "C:/Program Files (x86)/Intel/TBB")
  119. # Set the target architecture
  120. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  121. set(TBB_ARCHITECTURE "intel64")
  122. else()
  123. set(TBB_ARCHITECTURE "ia32")
  124. endif()
  125. # Set the TBB search library path search suffix based on the version of VC
  126. if(WINDOWS_STORE)
  127. set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11_ui")
  128. elseif(MSVC14)
  129. set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc14")
  130. elseif(MSVC12)
  131. set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc12")
  132. elseif(MSVC11)
  133. set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc11")
  134. elseif(MSVC10)
  135. set(TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc10")
  136. endif()
  137. # Add the library path search suffix for the VC independent version of TBB
  138. list(APPEND TBB_LIB_PATH_SUFFIX "lib/${TBB_ARCHITECTURE}/vc_mt")
  139. elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  140. # OS X
  141. set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
  142. # TODO: Check to see which C++ library is being used by the compiler.
  143. if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
  144. # The default C++ library on OS X 10.9 and later is libc++
  145. set(TBB_LIB_PATH_SUFFIX "lib/libc++" "lib")
  146. else()
  147. set(TBB_LIB_PATH_SUFFIX "lib")
  148. endif()
  149. elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  150. # Linux
  151. set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
  152. # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
  153. # <arch>/gcc4.1. For now, assume that the compiler is more recent than
  154. # gcc 4.4.x or later.
  155. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  156. set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
  157. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
  158. set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
  159. endif()
  160. endif()
  161. ##################################
  162. # Find the TBB include dir
  163. ##################################
  164. find_path(TBB_INCLUDE_DIRS tbb/tbb.h
  165. HINTS ${TBB_INCLUDE_DIR} ${TBB_SEARCH_DIR}
  166. PATHS ${TBB_DEFAULT_SEARCH_DIR}
  167. PATH_SUFFIXES include)
  168. ##################################
  169. # Set version strings
  170. ##################################
  171. if(TBB_INCLUDE_DIRS)
  172. file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
  173. string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
  174. TBB_VERSION_MAJOR "${_tbb_version_file}")
  175. string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
  176. TBB_VERSION_MINOR "${_tbb_version_file}")
  177. string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
  178. TBB_INTERFACE_VERSION "${_tbb_version_file}")
  179. set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
  180. endif()
  181. ##################################
  182. # Find TBB components
  183. ##################################
  184. if(TBB_VERSION VERSION_LESS 4.3)
  185. set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc tbb)
  186. else()
  187. set(TBB_SEARCH_COMPOMPONENTS tbb_preview tbbmalloc_proxy tbbmalloc tbb)
  188. endif()
  189. if(TBB_STATIC)
  190. set(TBB_STATIC_SUFFIX "_static")
  191. endif()
  192. # Find each component
  193. foreach(_comp ${TBB_SEARCH_COMPOMPONENTS})
  194. if(";${TBB_FIND_COMPONENTS};tbb;" MATCHES ";${_comp};")
  195. # Search for the libraries
  196. find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}${TBB_STATIC_SUFFIX}
  197. HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
  198. PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
  199. PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
  200. find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}${TBB_STATIC_SUFFIX}_debug
  201. HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
  202. PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
  203. PATH_SUFFIXES ${TBB_LIB_PATH_SUFFIX})
  204. if(TBB_${_comp}_LIBRARY_DEBUG)
  205. list(APPEND TBB_LIBRARIES_DEBUG "${TBB_${_comp}_LIBRARY_DEBUG}")
  206. endif()
  207. if(TBB_${_comp}_LIBRARY_RELEASE)
  208. list(APPEND TBB_LIBRARIES_RELEASE "${TBB_${_comp}_LIBRARY_RELEASE}")
  209. endif()
  210. if(TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE} AND NOT TBB_${_comp}_LIBRARY)
  211. set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_${TBB_BUILD_TYPE}}")
  212. endif()
  213. if(TBB_${_comp}_LIBRARY AND EXISTS "${TBB_${_comp}_LIBRARY}")
  214. set(TBB_${_comp}_FOUND TRUE)
  215. else()
  216. set(TBB_${_comp}_FOUND FALSE)
  217. endif()
  218. # Mark internal variables as advanced
  219. mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
  220. mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
  221. mark_as_advanced(TBB_${_comp}_LIBRARY)
  222. endif()
  223. endforeach()
  224. unset(TBB_STATIC_SUFFIX)
  225. ##################################
  226. # Set compile flags and libraries
  227. ##################################
  228. set(TBB_DEFINITIONS_RELEASE "")
  229. set(TBB_DEFINITIONS_DEBUG "-DTBB_USE_DEBUG=1")
  230. if(TBB_LIBRARIES_${TBB_BUILD_TYPE})
  231. set(TBB_DEFINITIONS "${TBB_DEFINITIONS_${TBB_BUILD_TYPE}}")
  232. set(TBB_LIBRARIES "${TBB_LIBRARIES_${TBB_BUILD_TYPE}}")
  233. elseif(TBB_LIBRARIES_RELEASE)
  234. set(TBB_DEFINITIONS "${TBB_DEFINITIONS_RELEASE}")
  235. set(TBB_LIBRARIES "${TBB_LIBRARIES_RELEASE}")
  236. elseif(TBB_LIBRARIES_DEBUG)
  237. set(TBB_DEFINITIONS "${TBB_DEFINITIONS_DEBUG}")
  238. set(TBB_LIBRARIES "${TBB_LIBRARIES_DEBUG}")
  239. endif()
  240. find_package_handle_standard_args(TBB
  241. REQUIRED_VARS TBB_INCLUDE_DIRS TBB_LIBRARIES
  242. HANDLE_COMPONENTS
  243. VERSION_VAR TBB_VERSION)
  244. ##################################
  245. # Create targets
  246. ##################################
  247. if(NOT CMAKE_VERSION VERSION_LESS 3.0 AND TBB_FOUND)
  248. add_library(tbb SHARED IMPORTED)
  249. set_target_properties(tbb PROPERTIES
  250. INTERFACE_INCLUDE_DIRECTORIES ${TBB_INCLUDE_DIRS}
  251. IMPORTED_LOCATION ${TBB_LIBRARIES})
  252. if(TBB_LIBRARIES_RELEASE AND TBB_LIBRARIES_DEBUG)
  253. set_target_properties(tbb PROPERTIES
  254. INTERFACE_COMPILE_DEFINITIONS "$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:TBB_USE_DEBUG=1>"
  255. IMPORTED_LOCATION_DEBUG ${TBB_LIBRARIES_DEBUG}
  256. IMPORTED_LOCATION_RELWITHDEBINFO ${TBB_LIBRARIES_DEBUG}
  257. IMPORTED_LOCATION_RELEASE ${TBB_LIBRARIES_RELEASE}
  258. IMPORTED_LOCATION_MINSIZEREL ${TBB_LIBRARIES_RELEASE}
  259. )
  260. elseif(TBB_LIBRARIES_RELEASE)
  261. set_target_properties(tbb PROPERTIES IMPORTED_LOCATION ${TBB_LIBRARIES_RELEASE})
  262. else()
  263. set_target_properties(tbb PROPERTIES
  264. INTERFACE_COMPILE_DEFINITIONS "${TBB_DEFINITIONS_DEBUG}"
  265. IMPORTED_LOCATION ${TBB_LIBRARIES_DEBUG}
  266. )
  267. endif()
  268. endif()
  269. mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES)
  270. unset(TBB_ARCHITECTURE)
  271. unset(TBB_BUILD_TYPE)
  272. unset(TBB_LIB_PATH_SUFFIX)
  273. unset(TBB_DEFAULT_SEARCH_DIR)
  274. if(TBB_DEBUG)
  275. message(STATUS " TBB_INCLUDE_DIRS = ${TBB_INCLUDE_DIRS}")
  276. message(STATUS " TBB_DEFINITIONS = ${TBB_DEFINITIONS}")
  277. message(STATUS " TBB_LIBRARIES = ${TBB_LIBRARIES}")
  278. message(STATUS " TBB_DEFINITIONS_DEBUG = ${TBB_DEFINITIONS_DEBUG}")
  279. message(STATUS " TBB_LIBRARIES_DEBUG = ${TBB_LIBRARIES_DEBUG}")
  280. message(STATUS " TBB_DEFINITIONS_RELEASE = ${TBB_DEFINITIONS_RELEASE}")
  281. message(STATUS " TBB_LIBRARIES_RELEASE = ${TBB_LIBRARIES_RELEASE}")
  282. endif()
  283. endif()