Merge pull request #16122 from alalek:cmake_update_cpu_compiler_detection
[platform/upstream/opencv.git] / cmake / OpenCVFindIPP.cmake
1 #
2 # The script to detect Intel(R) Integrated Performance Primitives (IPP)
3 # installation/package
4 #
5 # By default, ICV version will be used.
6 # To use standalone IPP update cmake command line:
7 # cmake ... -DIPPROOT=<path> ...
8 #
9 # Note: Backward compatibility is broken, IPPROOT environment path is ignored
10 #
11 #
12 # On return this will define:
13 #
14 # HAVE_IPP            - True if Intel IPP found
15 # HAVE_IPP_ICV        - True if Intel IPP ICV version is available
16 # IPP_ROOT_DIR        - root of IPP installation
17 # IPP_INCLUDE_DIRS    - IPP include folder
18 # IPP_LIBRARIES       - IPP libraries that are used by OpenCV
19 # IPP_VERSION_STR     - string with the newest detected IPP version
20 # IPP_VERSION_MAJOR   - numbers of IPP version (MAJOR.MINOR.BUILD)
21 # IPP_VERSION_MINOR
22 # IPP_VERSION_BUILD
23 #
24 # Created: 30 Dec 2010 by Vladimir Dudnik (vladimir.dudnik@intel.com)
25 #
26
27 unset(HAVE_IPP CACHE)
28 unset(HAVE_IPP_ICV)
29 unset(IPP_ROOT_DIR)
30 unset(IPP_INCLUDE_DIRS)
31 unset(IPP_LIBRARIES)
32 unset(IPP_VERSION_STR)
33 unset(IPP_VERSION_MAJOR)
34 unset(IPP_VERSION_MINOR)
35 unset(IPP_VERSION_BUILD)
36
37 if (X86 AND UNIX AND NOT APPLE AND NOT ANDROID AND BUILD_SHARED_LIBS)
38     message(STATUS "On 32-bit Linux Intel IPP can not currently be used with dynamic libs because of linker errors. Set BUILD_SHARED_LIBS=OFF")
39     return()
40 endif()
41
42 set(IPP_X64 0)
43 if(X86_64)
44   set(IPP_X64 1)
45 endif()
46
47 # This function detects Intel IPP version by analyzing .h file
48 macro(ipp_get_version VERSION_FILE)
49   unset(_VERSION_STR)
50   unset(_MAJOR)
51   unset(_MINOR)
52   unset(_BUILD)
53
54   # read Intel IPP version info from file
55   file(STRINGS ${VERSION_FILE} STR1 REGEX "IPP_VERSION_MAJOR")
56   file(STRINGS ${VERSION_FILE} STR2 REGEX "IPP_VERSION_MINOR")
57   file(STRINGS ${VERSION_FILE} STR3 REGEX "IPP_VERSION_BUILD")
58   if("${STR3}" STREQUAL "")
59     file(STRINGS ${VERSION_FILE} STR3 REGEX "IPP_VERSION_UPDATE")
60   endif()
61   file(STRINGS ${VERSION_FILE} STR4 REGEX "IPP_VERSION_STR")
62
63   # extract info and assign to variables
64   string(REGEX MATCHALL "[0-9]+" _MAJOR ${STR1})
65   string(REGEX MATCHALL "[0-9]+" _MINOR ${STR2})
66   string(REGEX MATCHALL "[0-9]+" _BUILD ${STR3})
67   string(REGEX MATCHALL "[0-9]+[.]+[0-9]+[^\"]+|[0-9]+[.]+[0-9]+" _VERSION_STR ${STR4})
68
69   # export info to parent scope
70   set(IPP_VERSION_STR   ${_VERSION_STR})
71   set(IPP_VERSION_MAJOR ${_MAJOR})
72   set(IPP_VERSION_MINOR ${_MINOR})
73   set(IPP_VERSION_BUILD ${_BUILD})
74 endmacro()
75
76 macro(_ipp_not_supported)
77   message(STATUS ${ARGN})
78   unset(HAVE_IPP)
79   unset(HAVE_IPP_ICV)
80   unset(IPP_VERSION_STR)
81   return()
82 endmacro()
83
84 # This macro uses IPP_ROOT_DIR variable
85 # TODO Cleanup code after ICV package stabilization
86 macro(ipp_detect_version)
87   set(IPP_INCLUDE_DIRS ${IPP_ROOT_DIR}/include)
88
89   set(__msg)
90   if(EXISTS ${IPP_ROOT_DIR}/include/ippicv_redefs.h)
91     set(__msg " (ICV version)")
92     set(HAVE_IPP_ICV 1)
93   elseif(EXISTS ${IPP_ROOT_DIR}/include/ipp.h)
94     # nothing
95   else()
96     _ipp_not_supported("Can't resolve Intel IPP directory: ${IPP_ROOT_DIR}")
97   endif()
98
99   ipp_get_version(${IPP_INCLUDE_DIRS}/ippversion.h)
100   ocv_assert(IPP_VERSION_STR VERSION_GREATER "1.0")
101
102   message(STATUS "found Intel IPP${__msg}: ${_MAJOR}.${_MINOR}.${_BUILD} [${IPP_VERSION_STR}]")
103   message(STATUS "at: ${IPP_ROOT_DIR}")
104
105   if(IPP_VERSION_STR VERSION_LESS "7.0")
106     _ipp_not_supported("Intel IPP ${IPP_VERSION_STR} is not supported")
107   endif()
108
109   set(HAVE_IPP 1)
110
111   macro(_ipp_set_library_dir DIR)
112     if(NOT EXISTS ${DIR})
113       _ipp_not_supported("Intel IPP library directory not found")
114     endif()
115     set(IPP_LIBRARY_DIR ${DIR})
116   endmacro()
117
118   if(APPLE AND NOT HAVE_IPP_ICV)
119     _ipp_set_library_dir(${IPP_ROOT_DIR}/lib)
120   elseif(IPP_X64)
121     _ipp_set_library_dir(${IPP_ROOT_DIR}/lib/intel64)
122   else()
123     _ipp_set_library_dir(${IPP_ROOT_DIR}/lib/ia32)
124   endif()
125
126   macro(_ipp_add_library name)
127     # dynamic linking is only supported for standalone version of Intel IPP
128     if (BUILD_WITH_DYNAMIC_IPP AND NOT HAVE_IPP_ICV)
129       if (WIN32)
130         set(IPP_LIB_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX})
131         set(IPP_LIB_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX})
132       else (WIN32)
133         set(IPP_LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
134         set(IPP_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
135       endif (WIN32)
136     else ()
137       set(IPP_LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
138       set(IPP_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
139     endif ()
140     if (EXISTS ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX})
141       if (BUILD_WITH_DYNAMIC_IPP AND NOT HAVE_IPP_ICV)
142         # When using dynamic libraries from standalone Intel IPP it is your responsibility to install those on the target system
143         list(APPEND IPP_LIBRARIES ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX})
144       else ()
145         add_library(ipp${name} STATIC IMPORTED)
146         set_target_properties(ipp${name} PROPERTIES
147           IMPORTED_LINK_INTERFACE_LIBRARIES ""
148           IMPORTED_LOCATION ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}
149         )
150         list(APPEND IPP_LIBRARIES ipp${name})
151         if (NOT BUILD_SHARED_LIBS)
152           # CMake doesn't support "install(TARGETS ${IPP_PREFIX}${name} " command with imported targets
153           install(FILES ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}
154                   DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
155           string(TOUPPER ${name} uname)
156           set(IPP${uname}_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_3P_LIB_INSTALL_PATH}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE)
157           set(IPP${uname}_LOCATION_PATH "${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE)
158         endif()
159       endif()
160     else()
161       message(STATUS "Can't find Intel IPP library: ${name} at ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}")
162     endif()
163   endmacro()
164
165   set(IPP_PREFIX "ipp")
166   if(IPP_VERSION_STR VERSION_LESS "8.0")
167     if (BUILD_WITH_DYNAMIC_IPP AND NOT HAVE_IPP_ICV)
168       set(IPP_SUFFIX "")      # dynamic not threaded libs suffix Intel IPP 7.x
169     else ()
170       set(IPP_SUFFIX "_l")    # static not threaded libs suffix Intel IPP 7.x
171     endif ()
172   else ()
173     if(WIN32)
174       if (BUILD_WITH_DYNAMIC_IPP AND NOT HAVE_IPP_ICV)
175         set(IPP_SUFFIX "")    # dynamic not threaded libs suffix Intel IPP 8.x for Windows
176       else ()
177         set(IPP_SUFFIX "mt")  # static not threaded libs suffix Intel IPP 8.x for Windows
178       endif ()
179     else()
180       set(IPP_SUFFIX "")      # static not threaded libs suffix Intel IPP 8.x for Linux/OS X
181     endif()
182   endif()
183
184   if(HAVE_IPP_ICV)
185     _ipp_add_library(icv)
186   else()
187     _ipp_add_library(cv)
188     _ipp_add_library(i)
189     _ipp_add_library(cc)
190     _ipp_add_library(s)
191     _ipp_add_library(vm)
192     _ipp_add_library(core)
193
194     if(UNIX AND IPP_VERSION_MAJOR LESS 2017)
195       get_filename_component(INTEL_COMPILER_LIBRARY_DIR ${IPP_ROOT_DIR}/../lib REALPATH)
196       if(NOT EXISTS ${INTEL_COMPILER_LIBRARY_DIR})
197         get_filename_component(INTEL_COMPILER_LIBRARY_DIR ${IPP_ROOT_DIR}/../compiler/lib REALPATH)
198       endif()
199       if(NOT EXISTS ${INTEL_COMPILER_LIBRARY_DIR})
200         _ipp_not_supported("Intel IPP configuration error: can't find Intel compiler library dir ${INTEL_COMPILER_LIBRARY_DIR}")
201       endif()
202       if(NOT APPLE)
203         if(IPP_X64)
204           if(NOT EXISTS ${INTEL_COMPILER_LIBRARY_DIR}/intel64)
205             message(SEND_ERROR "Intel compiler EM64T libraries not found")
206           endif()
207           set(INTEL_COMPILER_LIBRARY_DIR ${INTEL_COMPILER_LIBRARY_DIR}/intel64)
208         else()
209           if(NOT EXISTS ${INTEL_COMPILER_LIBRARY_DIR}/ia32)
210             message(SEND_ERROR "Intel compiler IA32 libraries not found")
211           endif()
212           set(INTEL_COMPILER_LIBRARY_DIR ${INTEL_COMPILER_LIBRARY_DIR}/ia32)
213         endif()
214       endif()
215
216       macro(_ipp_add_compiler_library name)
217         if (EXISTS ${INTEL_COMPILER_LIBRARY_DIR}/${IPP_LIB_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX})
218           list(APPEND IPP_LIBRARIES ${INTEL_COMPILER_LIBRARY_DIR}/${IPP_LIB_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX})
219         else()
220           message(STATUS "Can't find compiler library: ${name} at ${INTEL_COMPILER_LIBRARY_DIR}/${IPP_LIB_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
221         endif()
222       endmacro()
223
224       _ipp_add_compiler_library(irc)
225       _ipp_add_compiler_library(imf)
226       _ipp_add_compiler_library(svml)
227     endif()
228   endif()
229
230   #message(STATUS "Intel IPP libs: ${IPP_LIBRARIES}")
231 endmacro()
232
233 # OPENCV_IPP_PATH is an environment variable for internal usage only, do not use it
234 if(DEFINED ENV{OPENCV_IPP_PATH} AND NOT DEFINED IPPROOT)
235   set(IPPROOT "$ENV{OPENCV_IPP_PATH}")
236 endif()
237
238 if(NOT DEFINED IPPROOT)
239   include("${OpenCV_SOURCE_DIR}/3rdparty/ippicv/ippicv.cmake")
240   download_ippicv(ICV_PACKAGE_ROOT)
241   if(NOT ICV_PACKAGE_ROOT)
242     return()
243   endif()
244   set(IPPROOT "${ICV_PACKAGE_ROOT}/icv")
245   ocv_install_3rdparty_licenses(ippicv "${IPPROOT}/readme.htm")
246   if(WIN32)
247     ocv_install_3rdparty_licenses(ippicv "${ICV_PACKAGE_ROOT}/EULA.rtf")
248   else()
249     ocv_install_3rdparty_licenses(ippicv "${ICV_PACKAGE_ROOT}/EULA.txt")
250   endif()
251 endif()
252
253 file(TO_CMAKE_PATH "${IPPROOT}" __IPPROOT)
254 if(EXISTS "${__IPPROOT}/include/ippversion.h")
255   set(IPP_ROOT_DIR ${__IPPROOT})
256   ipp_detect_version()
257 endif()
258
259
260 if(WIN32 AND MINGW AND NOT IPP_VERSION_MAJOR LESS 7)
261     # Since Intel IPP built with Microsoft compiler and /GS option
262     # ======================================================
263     # From Windows SDK 7.1
264     #   (usually in "C:\Program Files\Microsoft Visual Studio 10.0\VC\lib"),
265     # to avoid undefined reference to __security_cookie and _chkstk:
266     set(MSV_RUNTMCHK "RunTmChk")
267     set(IPP_LIBRARIES ${IPP_LIBRARIES} ${MSV_RUNTMCHK}${IPP_LIB_SUFFIX})
268
269     # To avoid undefined reference to _alldiv and _chkstk
270     # ===================================================
271     # NB: it may require a recompilation of w32api (after having modified
272     #     the file ntdll.def) to export the required functions
273     #     See http://code.opencv.org/issues/1906 for additional details
274     set(MSV_NTDLL    "ntdll")
275     set(IPP_LIBRARIES ${IPP_LIBRARIES} ${MSV_NTDLL}${IPP_LIB_SUFFIX})
276 endif()