Don't copy CMake scripts to install folder
[junction.git] / cmake / modules / FindTBB.cmake
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 #
24 # FindTBB
25 # -------
26 #
27 # Find TBB include directories and libraries.
28 #
29 # Usage:
30 #
31 #  find_package(TBB [major[.minor]] [EXACT]
32 #               [QUIET] [REQUIRED]
33 #               [[COMPONENTS] [components...]]
34 #               [OPTIONAL_COMPONENTS components...]) 
35 #
36 # where the allowed components are tbbmalloc and tbb_preview. Users may modify 
37 # the behavior of this module with the following variables:
38 #
39 # * TBB_ROOT_DIR          - The base directory the of TBB installation.
40 # * TBB_INCLUDE_DIR       - The directory that contains the TBB headers files.
41 # * TBB_LIBRARY           - The directory that contains the TBB library files.
42 # * TBB_<library>_LIBRARY - The path of the TBB the corresponding TBB library. 
43 #                           These libraries, if specified, override the 
44 #                           corresponding library search results, where <library>
45 #                           may be tbb, tbb_debug, tbbmalloc, tbbmalloc_debug,
46 #                           tbb_preview, or tbb_preview_debug.
47 # * TBB_USE_DEBUG_BUILD   - The debug version of tbb libraries, if present, will
48 #                           be used instead of the release version.
49 #
50 # Users may modify the behavior of this module with the following environment
51 # variables:
52 #
53 # * TBB_INSTALL_DIR 
54 # * TBBROOT
55 # * LIBRARY_PATH
56 #
57 # This module will set the following variables:
58 #
59 # * TBB_FOUND             - Set to false, or undefined, if we haven’t found, or
60 #                           don’t want to use TBB.
61 # * TBB_<component>_FOUND - If False, optional <component> part of TBB sytem is
62 #                           not available.
63 # * TBB_VERSION           - The full version string
64 # * TBB_VERSION_MAJOR     - The major version
65 # * TBB_VERSION_MINOR     - The minor version
66 # * TBB_INTERFACE_VERSION - The interface version number defined in 
67 #                           tbb/tbb_stddef.h.
68 # * TBB_<library>_LIBRARY_RELEASE - The path of the TBB release version of 
69 #                           <library>, where <library> may be tbb, tbb_debug,
70 #                           tbbmalloc, tbbmalloc_debug, tbb_preview, or 
71 #                           tbb_preview_debug.
72 # * TBB_<library>_LIBRARY_DEGUG - The path of the TBB release version of 
73 #                           <library>, where <library> may be tbb, tbb_debug,
74 #                           tbbmalloc, tbbmalloc_debug, tbb_preview, or 
75 #                           tbb_preview_debug.
76 #
77 # The following varibles should be used to build and link with TBB:
78 #
79 # * TBB_INCLUDE_DIRS - The include directory for TBB.
80 # * TBB_LIBRARIES    - The libraries to link against to use TBB.
81 # * TBB_DEFINITIONS  - Definitions to use when compiling code that uses TBB.
82
83 include(FindPackageHandleStandardArgs)
84
85 if(NOT TBB_FOUND)
86
87   ##################################
88   # Check the build type
89   ##################################
90   
91   if(NOT DEFINED TBB_USE_DEBUG_BUILD)
92     if(CMAKE_BUILD_TYPE MATCHES "[Debug|DEBUG|debug|RelWithDebInfo|RELWITHDEBINFO|relwithdebinfo]")
93       set(TBB_USE_DEBUG_BUILD FALSE)
94     else()
95       set(TBB_USE_DEBUG_BUILD FALSE)
96     endif()
97   endif()
98   
99   ##################################
100   # Set the TBB search directories
101   ##################################
102   
103   # Define search paths based on user input and environment variables
104   set(TBB_SEARCH_DIR ${TBB_ROOT_DIR} $ENV{TBB_INSTALL_DIR} $ENV{TBBROOT})
105   
106   # Define the search directories based on the current platform
107   if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
108     set(TBB_DEFAULT_SEARCH_DIR "C:/Program Files/Intel/TBB"
109                                "C:/Program Files (x86)/Intel/TBB")
110     # TODO: Set the proper suffix paths based on compiler introspection.
111   
112   elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
113     # OS X
114     set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
115     
116     # TODO: Check to see which C++ library is being used by the compiler.
117     if(NOT ${CMAKE_SYSTEM_VERSION} VERSION_LESS 13.0)
118       # The default C++ library on OS X 10.9 and later is libc++
119       set(TBB_LIB_PATH_SUFFIX "lib/libc++")
120     else()
121       set(TBB_LIB_PATH_SUFFIX "lib")
122     endif()
123   elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
124     # Linux
125     set(TBB_DEFAULT_SEARCH_DIR "/opt/intel/tbb")
126     
127     # TODO: Check compiler version to see the suffix should be <arch>/gcc4.1 or
128     #       <arch>/gcc4.1. For now, assume that the compiler is more recent than
129     #       gcc 4.4.x or later.
130     if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
131       set(TBB_LIB_PATH_SUFFIX "lib/intel64/gcc4.4")
132     elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
133       set(TBB_LIB_PATH_SUFFIX "lib/ia32/gcc4.4")
134     endif()
135   endif()
136   
137   ##################################
138   # Find the TBB include dir
139   ##################################
140   
141   find_path(TBB_INCLUDE_DIRS tbb/tbb.h
142       HINTS ${TBB_INCLUDE_DIR} ${TBB_SEARCH_DIR}
143       PATHS ${TBB_DEFAULT_SEARCH_DIR}
144       PATH_SUFFIXES include)
145   if(TBB_INCLUDE_DIR)
146     set(TBB_INCLUDE_DIRS ${TBB_INCLUDE_DIR})  # Set it directly!!
147   endif()
148
149   ##################################
150   # Find TBB components
151   ##################################
152
153   # Find each component
154   foreach(_comp tbb_preview tbbmalloc tbb)
155     # Search for the libraries
156     find_library(TBB_${_comp}_LIBRARY_RELEASE ${_comp}
157         HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
158         PATHS ${TBB_DEFAULT_SEARCH_DIR}
159         PATH_SUFFIXES "${TBB_LIB_PATH_SUFFIX}")
160
161     find_library(TBB_${_comp}_LIBRARY_DEBUG ${_comp}_debug
162         HINTS ${TBB_LIBRARY} ${TBB_SEARCH_DIR}
163         PATHS ${TBB_DEFAULT_SEARCH_DIR} ENV LIBRARY_PATH
164         PATH_SUFFIXES "${TBB_LIB_PATH_SUFFIX}")
165     
166     # Set the library to be used for the component
167     if(NOT TBB_${_comp}_LIBRARY)
168       if(TBB_USE_DEBUG_BUILD AND TBB_${_comp}_LIBRARY_DEBUG)
169         set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_DEBUG}")
170       elseif(TBB_${_comp}_LIBRARY_RELEASE)
171         set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_RELEASE}")
172       elseif(TBB_${_comp}_LIBRARY_DEBUG)
173         set(TBB_${_comp}_LIBRARY "${TBB_${_comp}_LIBRARY_DEBUG}")
174       endif()
175     endif()
176     
177     # Set the TBB library list and component found variables
178     if(TBB_${_comp}_LIBRARY)
179       list(APPEND TBB_LIBRARIES "${TBB_${_comp}_LIBRARY}")
180       set(TBB_${_comp}_FOUND TRUE)
181     else()
182       set(TBB_${_comp}_FOUND FALSE)
183     endif()
184     
185     mark_as_advanced(TBB_${_comp}_LIBRARY_RELEASE)
186     mark_as_advanced(TBB_${_comp}_LIBRARY_DEBUG)
187     mark_as_advanced(TBB_${_comp}_LIBRARY)
188     
189   endforeach()
190   
191   ##################################
192   # Set compile flags
193   ##################################
194   
195   if(TBB_tbb_LIBRARY MATCHES "debug")
196     set(TBB_DEFINITIONS "-DTBB_USE_DEBUG=1")
197   endif()
198   
199   ##################################
200   # Set version strings
201   ##################################
202   
203   if(TBB_INCLUDE_DIRS)
204     file(READ "${TBB_INCLUDE_DIRS}/tbb/tbb_stddef.h" _tbb_version_file)
205     string(REGEX REPLACE ".*#define TBB_VERSION_MAJOR ([0-9]+).*" "\\1"
206             TBB_VERSION_MAJOR "${_tbb_version_file}")
207     string(REGEX REPLACE ".*#define TBB_VERSION_MINOR ([0-9]+).*" "\\1"
208             TBB_VERSION_MINOR "${_tbb_version_file}")
209     string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1"
210             TBB_INTERFACE_VERSION "${_tbb_version_file}")
211     set(TBB_VERSION "${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR}")
212   endif()
213   
214   find_package_handle_standard_args(TBB 
215       REQUIRED_VARS TBB_INCLUDE_DIRS TBB_LIBRARIES
216       HANDLE_COMPONENTS
217       VERSION_VAR TBB_VERSION)
218   
219   mark_as_advanced(TBB_INCLUDE_DIRS TBB_LIBRARIES)
220
221 endif()