2024-05-30 22:03:07 +08:00
if ( CMAKE_SYSTEM_NAME STREQUAL "Darwin" )
set ( _apple_device_min_target_os_version "13.3" )
elseif ( CMAKE_SYSTEM_NAME STREQUAL "iOS" )
set ( _apple_device_min_target_os_version "16.4" )
elseif ( CMAKE_SYSTEM_NAME STREQUAL "watchOS" )
set ( _apple_device_min_target_os_version "9.4" )
elseif ( CMAKE_SYSTEM_NAME STREQUAL "tvOS" )
set ( _apple_device_min_target_os_version "16.4" )
elseif ( CMAKE_SYSTEM_NAME STREQUAL "visionOS" )
set ( _apple_device_min_target_os_version "1.0" )
endif ( )
if ( DEFINED _apple_device_min_target_os_version AND
( " $ { C M A K E _ O S X _ D E P L O Y M E N T _ T A R G E T } " V E R S I O N _ G R E A T E R " $ { _ a p p l e _ d e v i c e _ m i n _ t a r g e t _ o s _ v e r s i o n } " O R
" $ { C M A K E _ O S X _ D E P L O Y M E N T _ T A R G E T } " V E R S I O N _ E Q U A L " $ { _ a p p l e _ d e v i c e _ m i n _ t a r g e t _ o s _ v e r s i o n } " ) )
set ( _apple_device_has_required_min_os_version ON )
else ( )
set ( _apple_device_has_required_min_os_version OFF )
endif ( )
OCV_OPTION ( OPENCV_OSX_USE_ACCELERATE_NEW_LAPACK "Use new BLAS/LAPACK interfaces from Accelerate framework on Apple platform" _apple_device_has_required_min_os_version
V I S I B L E _ I F APPLE )
2016-12-23 19:50:33 +08:00
macro ( _find_header_file_in_dirs VAR NAME )
unset ( ${ VAR } )
unset ( ${ VAR } CACHE )
if ( " ${ARGN}" STREQUAL " " )
check_include_file ( "${NAME}" HAVE_ ${ VAR } )
if ( HAVE_ ${ VAR } )
set ( ${ VAR } "${NAME}" ) # fallback
else ( )
set ( ${ VAR } "" )
endif ( )
else ( )
find_path ( ${ VAR } "${NAME}" ${ ARGN } NO_DEFAULT_PATH )
if ( ${ VAR } )
set ( ${ VAR } "${${VAR}}/${NAME}" )
unset ( ${ VAR } CACHE )
else ( )
unset ( ${ VAR } CACHE )
set ( ${ VAR } "" )
endif ( )
endif ( )
endmacro ( )
macro ( ocv_lapack_check )
string ( REGEX REPLACE "[^a-zA-Z0-9_]" "_" _lapack_impl "${LAPACK_IMPL}" )
message ( STATUS "LAPACK(${LAPACK_IMPL}): LAPACK_LIBRARIES: ${LAPACK_LIBRARIES}" )
_find_header_file_in_dirs ( OPENCV_CBLAS_H_PATH_ ${ _lapack_impl } "${LAPACK_CBLAS_H}" "${LAPACK_INCLUDE_DIR}" )
_find_header_file_in_dirs ( OPENCV_LAPACKE_H_PATH_ ${ _lapack_impl } "${LAPACK_LAPACKE_H}" "${LAPACK_INCLUDE_DIR}" )
if ( NOT OPENCV_CBLAS_H_PATH_ ${ _lapack_impl } OR NOT OPENCV_LAPACKE_H_PATH_ ${ _lapack_impl } )
message ( WARNING "LAPACK(${LAPACK_IMPL}): CBLAS/LAPACK headers are not found in '${LAPACK_INCLUDE_DIR}'" )
unset ( LAPACK_LIBRARIES )
else ( )
# adding proxy opencv_lapack.h header
set ( CBLAS_H_PROXY_PATH ${ CMAKE_BINARY_DIR } /opencv_lapack.h )
2019-05-14 19:08:16 +08:00
set ( _lapack_add_extern_c NOT ( APPLE OR OPENCV_SKIP_LAPACK_EXTERN_C ) OR OPENCV_FORCE_LAPACK_EXTERN_C )
set ( _lapack_content "// This file is auto-generated\n" )
if ( ${ _lapack_add_extern_c } )
list ( APPEND _lapack_content "extern \" C\ " {" )
2017-12-05 20:45:37 +08:00
endif ( )
2019-05-14 19:08:16 +08:00
if ( NOT OPENCV_SKIP_LAPACK_MSVC_FIX )
list ( APPEND _lapack_content "
#ifdef _MSC_VER
#include <complex.h>
#define lapack_complex_float _Fcomplex
#define lapack_complex_double _Dcomplex
#endif
" )
2016-12-23 19:50:33 +08:00
endif ( )
2019-05-14 19:08:16 +08:00
list ( APPEND _lapack_content "#include \" ${ OPENCV_CBLAS_H_PATH_${_lapack_impl } }\ "" )
if ( NOT "${OPENCV_CBLAS_H_PATH_${_lapack_impl}}" STREQUAL "${OPENCV_LAPACKE_H_PATH_${_lapack_impl}}" )
list ( APPEND _lapack_content "#include \" ${ OPENCV_LAPACKE_H_PATH_${_lapack_impl } }\ "" )
2016-12-23 19:50:33 +08:00
endif ( )
2021-12-22 20:00:00 +08:00
list ( APPEND _lapack_content "
#if defined(LAPACK_GLOBAL) || defined(LAPACK_NAME)
/ *
* U s i n g n e t l i b ' s r e f e r e n c e L A P A C K i m p l e m e n t a t i o n v e r s i o n > = 3 . 4 . 0 ( first with C interface ) .
* U s e L A P A C K _ x x x x t o transparently ( via predefined lapack macros ) d e a l w i t h p r e a n d p o s t 3 . 9 . 1 v e r s i o n s .
* L A P A C K 3 . 9 . 1 i n t r o d u c e s L A P A C K _ F O R T R A N _ S T R L E N _ E N D a n d modifies ( through preprocessing ) t h e d e c l a r a t i o n s o f t h e f o l l o w i n g f u n c t i o n s u s e d i n o p e n c v
* s p o s v _ , d p o s v _ , s p o t r f _ , d p o t r f _ , s g e s d d _ , d g e s d d _ , s g e l s _ , d g e l s _
* w h i c h e n d u p w i t h a n e x t r a p a r a m e t e r .
* S o w e a l s o n e e d t o p r e p r o c e s s t h e f u n c t i o n c a l l s i n o p e n c v c o d i n g b y p r e f i x i n g t h e m w i t h L A P A C K _ .
* T h e g o o d n e w s i s t h e p r e p r o c e s s i n g w o r k s f i n e w h a t e v e r n e t l i b ' s L A P A C K v e r s i o n .
* /
#define OCV_LAPACK_FUNC(f) LAPACK_##f
#else
/ * U s i n g o t h e r L A P A C K i m p l e m e n t a t i o n s s o f a l l b a c k t o o p e n c v ' s a s s u m p t i o n u n t i l n o w * /
#define OCV_LAPACK_FUNC(f) f##_
#endif
" )
2019-05-14 19:08:16 +08:00
if ( ${ _lapack_add_extern_c } )
list ( APPEND _lapack_content "}" )
2016-12-23 19:50:33 +08:00
endif ( )
2019-05-14 19:08:16 +08:00
string ( REPLACE ";" "\n" _lapack_content "${_lapack_content}" )
ocv_update_file ( "${CBLAS_H_PROXY_PATH}" "${_lapack_content}" )
2021-02-03 23:49:59 +08:00
if ( CMAKE_GENERATOR MATCHES "Visual Studio" # MSBuild
A N D L A P A C K _ I M P L S T R E Q U A L " M K L "
A N D " ; $ { L A P A C K _ L I B R A R I E S } ; " M A T C H E S " ; t b b ; " A N D T A R G E T t b b
A N D D E F I N E D T B B _ I N T E R F A C E _ V E R S I O N A N D NOT ( TBB_INTERFACE_VERSION LESS 12000 ) # oneTBB/oneAPI workaround
)
# workaround DEFAULTLIB:tbb12.lib issue
get_target_property ( _tbb_lib tbb IMPORTED_LOCATION )
if ( NOT _tbb_lib )
get_target_property ( _tbb_lib tbb IMPORTED_LOCATION_RELEASE )
endif ( )
if ( _tbb_lib AND NOT OPENCV_SKIP_WORKAROUND_MKL_LINK_DIRECTORIES_TBB )
# MSBuild drops content of 'LIB' environment variable,
# so pass TBB library directory through `link_directories()`
get_filename_component ( _tbb_lib_dir "${_tbb_lib}" DIRECTORY )
message ( STATUS "MKL: adding '${_tbb_lib_dir}' to link directories (workaround DEFAULTLIB issue)" )
link_directories ( "${_tbb_lib_dir}" )
elseif ( NOT OPENCV_SKIP_WORKAROUND_MKL_DEFAULTLIB )
# We may have tbb.lib for 'tbb' target, but not 'tbb12.lib'
ocv_update ( OPENCV_MKL_IGNORE_DEFAULTLIB_TBB "tbb12.lib" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NODEFAULTLIB:${OPENCV_MKL_IGNORE_DEFAULTLIB_TBB}" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:${OPENCV_MKL_IGNORE_DEFAULTLIB_TBB}" )
endif ( )
endif ( )
# TODO add cache for try_compile() inputs/results
get_property ( __link_directories DIRECTORY PROPERTY LINK_DIRECTORIES )
if ( LAPACK_LINK_LIBRARIES )
list ( APPEND __link_directories ${ LAPACK_LINK_LIBRARIES } )
endif ( )
2024-05-21 21:58:16 +08:00
set ( LAPACK_TRY_COMPILE_DEF "" )
2024-05-30 22:03:07 +08:00
if ( LAPACK_IMPL STREQUAL "LAPACK/Apple" AND OPENCV_OSX_USE_ACCELERATE_NEW_LAPACK )
message ( STATUS "LAPACK(${LAPACK_IMPL}): Accelerate New LAPACK is enabled." )
set ( LAPACK_TRY_COMPILE_DEF "-DACCELERATE_NEW_LAPACK" )
add_compile_definitions ( ACCELERATE_NEW_LAPACK )
add_compile_definitions ( ACCELERATE_LAPACK_ILP64 )
2024-05-21 21:58:16 +08:00
endif ( )
2016-12-23 19:50:33 +08:00
try_compile ( __VALID_LAPACK
" $ { O p e n C V _ B I N A R Y _ D I R } "
" $ { O p e n C V _ S O U R C E _ D I R } / c m a k e / c h e c k s / l a p a c k _ c h e c k . c p p "
C M A K E _ F L A G S " - D I N C L U D E _ D I R E C T O R I E S : S T R I N G = $ { L A P A C K _ I N C L U D E _ D I R } \ ; $ { C M A K E _ B I N A R Y _ D I R } "
2021-02-03 23:49:59 +08:00
" - D L I N K _ D I R E C T O R I E S : S T R I N G = $ { _ _ l i n k _ d i r e c t o r i e s } "
2024-05-21 21:58:16 +08:00
C O M P I L E _ D E F I N I T I O N S $ { L A P A C K _ T R Y _ C O M P I L E _ D E F }
2021-02-03 23:49:59 +08:00
L I N K _ L I B R A R I E S $ { L A P A C K _ L I B R A R I E S }
2016-12-23 19:50:33 +08:00
O U T P U T _ V A R I A B L E T R Y _ O U T
)
if ( NOT __VALID_LAPACK )
2021-02-03 23:49:59 +08:00
file ( APPEND ${ CMAKE_BINARY_DIR } ${ CMAKE_FILES_DIRECTORY } /CMakeError.log
" \ nLAPACK ( ${ LAPACK_IMPL } ) c h e c k F A I L E D : \ n "
" L A P A C K _ I N C L U D E _ D I R : ' $ { L A P A C K _ I N C L U D E _ D I R } ' \ n "
" L A P A C K _ L I B R A R I E S : ' $ { L A P A C K _ L I B R A R I E S } ' \ n "
" L A P A C K _ L I N K _ L I B R A R I E S : ' $ { _ _ l i n k _ d i r e c t o r i e s } ' \ n "
" O u t p u t : \ n $ { T R Y _ O U T } \ n \ n " )
2016-12-23 19:50:33 +08:00
message ( STATUS "LAPACK(${LAPACK_IMPL}): Can't build LAPACK check code. This LAPACK version is not supported." )
unset ( LAPACK_LIBRARIES )
else ( )
message ( STATUS "LAPACK(${LAPACK_IMPL}): Support is enabled." )
ocv_include_directories ( ${ LAPACK_INCLUDE_DIR } )
set ( HAVE_LAPACK 1 )
endif ( )
endif ( )
2016-12-23 03:57:44 +08:00
endmacro ( )
if ( WITH_LAPACK )
2016-12-23 19:50:33 +08:00
ocv_update ( LAPACK_IMPL "Unknown" )
if ( NOT OPENCV_LAPACK_FIND_PACKAGE_ONLY )
if ( NOT LAPACK_LIBRARIES AND NOT OPENCV_LAPACK_DISABLE_MKL )
include ( cmake/OpenCVFindMKL.cmake )
if ( HAVE_MKL )
set ( LAPACK_INCLUDE_DIR ${ MKL_INCLUDE_DIRS } )
set ( LAPACK_LIBRARIES ${ MKL_LIBRARIES } )
set ( LAPACK_CBLAS_H "mkl_cblas.h" )
set ( LAPACK_LAPACKE_H "mkl_lapack.h" )
set ( LAPACK_IMPL "MKL" )
ocv_lapack_check ( )
endif ( )
2016-12-23 03:57:44 +08:00
endif ( )
2016-12-23 20:40:51 +08:00
if ( NOT LAPACK_LIBRARIES )
2016-12-23 19:50:33 +08:00
include ( cmake/OpenCVFindOpenBLAS.cmake )
if ( OpenBLAS_FOUND )
set ( LAPACK_INCLUDE_DIR ${ OpenBLAS_INCLUDE_DIR } )
set ( LAPACK_LIBRARIES ${ OpenBLAS_LIB } )
set ( LAPACK_CBLAS_H "cblas.h" )
set ( LAPACK_LAPACKE_H "lapacke.h" )
set ( LAPACK_IMPL "OpenBLAS" )
ocv_lapack_check ( )
endif ( )
2016-12-23 20:40:51 +08:00
endif ( )
2016-12-23 03:57:44 +08:00
if ( NOT LAPACK_LIBRARIES AND UNIX )
2016-12-23 19:50:33 +08:00
include ( cmake/OpenCVFindAtlas.cmake )
if ( ATLAS_FOUND )
set ( LAPACK_INCLUDE_DIR ${ Atlas_INCLUDE_DIR } )
set ( LAPACK_LIBRARIES ${ Atlas_LIBRARIES } )
set ( LAPACK_CBLAS_H "cblas.h" )
set ( LAPACK_LAPACKE_H "lapacke.h" )
set ( LAPACK_IMPL "Atlas" )
ocv_lapack_check ( )
endif ( )
2016-12-23 03:57:44 +08:00
endif ( )
2016-12-23 19:50:33 +08:00
endif ( )
2016-12-23 03:57:44 +08:00
2016-12-23 19:50:33 +08:00
if ( NOT LAPACK_LIBRARIES )
if ( WIN32 AND NOT OPENCV_LAPACK_SHARED_LIBS )
set ( BLA_STATIC 1 )
endif ( )
find_package ( LAPACK )
if ( LAPACK_FOUND )
if ( NOT DEFINED LAPACKE_INCLUDE_DIR )
find_path ( LAPACKE_INCLUDE_DIR "lapacke.h" )
endif ( )
if ( NOT DEFINED MKL_LAPACKE_INCLUDE_DIR )
find_path ( MKL_LAPACKE_INCLUDE_DIR "mkl_lapack.h" )
endif ( )
if ( MKL_LAPACKE_INCLUDE_DIR AND NOT OPENCV_LAPACK_DISABLE_MKL )
set ( LAPACK_INCLUDE_DIR ${ MKL_LAPACKE_INCLUDE_DIR } )
set ( LAPACK_CBLAS_H "mkl_cblas.h" )
set ( LAPACK_LAPACKE_H "mkl_lapack.h" )
set ( LAPACK_IMPL "LAPACK/MKL" )
ocv_lapack_check ( )
endif ( )
2017-10-13 20:40:37 +08:00
if ( NOT HAVE_LAPACK )
if ( LAPACKE_INCLUDE_DIR )
set ( LAPACK_INCLUDE_DIR ${ LAPACKE_INCLUDE_DIR } )
set ( LAPACK_CBLAS_H "cblas.h" )
set ( LAPACK_LAPACKE_H "lapacke.h" )
set ( LAPACK_IMPL "LAPACK/Generic" )
ocv_lapack_check ( )
elseif ( APPLE )
set ( LAPACK_CBLAS_H "Accelerate/Accelerate.h" )
set ( LAPACK_LAPACKE_H "Accelerate/Accelerate.h" )
set ( LAPACK_IMPL "LAPACK/Apple" )
ocv_lapack_check ( )
endif ( )
2016-12-23 19:50:33 +08:00
endif ( )
2017-10-13 20:40:37 +08:00
endif ( )
if ( NOT HAVE_LAPACK )
2017-04-18 18:31:57 +08:00
unset ( LAPACK_LIBRARIES )
unset ( LAPACK_LIBRARIES CACHE )
2016-12-23 03:57:44 +08:00
endif ( )
2016-12-23 19:50:33 +08:00
endif ( )
2016-12-23 03:57:44 +08:00
2016-12-23 19:50:33 +08:00
if ( NOT LAPACK_LIBRARIES AND APPLE AND NOT OPENCV_LAPACK_FIND_PACKAGE_ONLY )
set ( LAPACK_INCLUDE_DIR "" )
set ( LAPACK_LIBRARIES "-framework Accelerate" )
set ( LAPACK_CBLAS_H "Accelerate/Accelerate.h" )
set ( LAPACK_LAPACKE_H "Accelerate/Accelerate.h" )
set ( LAPACK_IMPL "Apple" )
ocv_lapack_check ( )
endif ( )
2016-12-23 03:57:44 +08:00
2017-01-19 18:33:39 +08:00
if ( NOT HAVE_LAPACK AND LAPACK_LIBRARIES AND LAPACK_CBLAS_H AND LAPACK_LAPACKE_H )
2016-12-23 19:50:33 +08:00
ocv_lapack_check ( )
endif ( )
2016-12-23 03:57:44 +08:00
2016-12-23 19:50:33 +08:00
set ( LAPACK_INCLUDE_DIR ${ LAPACK_INCLUDE_DIR } CACHE PATH "Path to BLAS include dir" FORCE )
set ( LAPACK_CBLAS_H ${ LAPACK_CBLAS_H } CACHE STRING "Alternative name of cblas.h" FORCE )
set ( LAPACK_LAPACKE_H ${ LAPACK_LAPACKE_H } CACHE STRING "Alternative name of lapacke.h" FORCE )
set ( LAPACK_LIBRARIES ${ LAPACK_LIBRARIES } CACHE STRING "Names of BLAS & LAPACK binaries (.so, .dll, .a, .lib)" FORCE )
set ( LAPACK_IMPL ${ LAPACK_IMPL } CACHE STRING "Lapack implementation id" FORCE )
2016-12-23 03:57:44 +08:00
endif ( )