FindLAPACK.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindLAPACK
  5. ----------
  6. Find Linear Algebra PACKage (LAPACK) library
  7. This module finds an installed fortran library that implements the
  8. LAPACK linear-algebra interface (see http://www.netlib.org/lapack/).
  9. The approach follows that taken for the autoconf macro file,
  10. ``acx_lapack.m4`` (distributed at
  11. http://ac-archive.sourceforge.net/ac-archive/acx_lapack.html).
  12. Input Variables
  13. ^^^^^^^^^^^^^^^
  14. The following variables may be set to influence this module's behavior:
  15. ``BLA_STATIC``
  16. if ``ON`` use static linkage
  17. ``BLA_VENDOR``
  18. If set, checks only the specified vendor, if not set checks all the
  19. possibilities. List of vendors valid in this module:
  20. * ``Intel10_32`` (intel mkl v10 32 bit)
  21. * ``Intel10_64lp`` (intel mkl v10+ 64 bit, threaded code, lp64 model)
  22. * ``Intel10_64lp_seq`` (intel mkl v10+ 64 bit, sequential code, lp64 model)
  23. * ``Intel10_64ilp`` (intel mkl v10+ 64 bit, threaded code, ilp64 model)
  24. * ``Intel10_64ilp_seq`` (intel mkl v10+ 64 bit, sequential code, ilp64 model)
  25. * ``Intel`` (obsolete versions of mkl 32 and 64 bit)
  26. * ``OpenBLAS``
  27. * ``FLAME``
  28. * ``ACML``
  29. * ``Apple``
  30. * ``NAS``
  31. * ``Generic``
  32. ``BLA_F95``
  33. if ``ON`` tries to find BLAS95/LAPACK95
  34. Result Variables
  35. ^^^^^^^^^^^^^^^^
  36. This module defines the following variables:
  37. ``LAPACK_FOUND``
  38. library implementing the LAPACK interface is found
  39. ``LAPACK_LINKER_FLAGS``
  40. uncached list of required linker flags (excluding -l and -L).
  41. ``LAPACK_LIBRARIES``
  42. uncached list of libraries (using full path name) to link against
  43. to use LAPACK
  44. ``LAPACK95_LIBRARIES``
  45. uncached list of libraries (using full path name) to link against
  46. to use LAPACK95
  47. ``LAPACK95_FOUND``
  48. library implementing the LAPACK95 interface is found
  49. .. note::
  50. C or CXX must be enabled to use Intel MKL
  51. For example, to use Intel MKL libraries and/or Intel compiler:
  52. .. code-block:: cmake
  53. set(BLA_VENDOR Intel10_64lp)
  54. find_package(LAPACK)
  55. #]=======================================================================]
  56. set(_lapack_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  57. # Check the language being used
  58. if( NOT (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED OR CMAKE_Fortran_COMPILER_LOADED) )
  59. if(LAPACK_FIND_REQUIRED)
  60. message(FATAL_ERROR "FindLAPACK requires Fortran, C, or C++ to be enabled.")
  61. else()
  62. message(STATUS "Looking for LAPACK... - NOT found (Unsupported languages)")
  63. return()
  64. endif()
  65. endif()
  66. if (CMAKE_Fortran_COMPILER_LOADED)
  67. include(${CMAKE_CURRENT_LIST_DIR}/CheckFortranFunctionExists.cmake)
  68. else ()
  69. include(${CMAKE_CURRENT_LIST_DIR}/CheckFunctionExists.cmake)
  70. endif ()
  71. include(${CMAKE_CURRENT_LIST_DIR}/CMakePushCheckState.cmake)
  72. cmake_push_check_state()
  73. set(CMAKE_REQUIRED_QUIET ${LAPACK_FIND_QUIETLY})
  74. set(LAPACK_FOUND FALSE)
  75. set(LAPACK95_FOUND FALSE)
  76. # TODO: move this stuff to separate module
  77. macro(Check_Lapack_Libraries LIBRARIES _prefix _name _flags _list _blas _threads)
  78. # This macro checks for the existence of the combination of fortran libraries
  79. # given by _list. If the combination is found, this macro checks (using the
  80. # Check_Fortran_Function_Exists macro) whether can link against that library
  81. # combination using the name of a routine given by _name using the linker
  82. # flags given by _flags. If the combination of libraries is found and passes
  83. # the link test, LIBRARIES is set to the list of complete library paths that
  84. # have been found. Otherwise, LIBRARIES is set to FALSE.
  85. # N.B. _prefix is the prefix applied to the names of all cached variables that
  86. # are generated internally and marked advanced by this macro.
  87. set(_libraries_work TRUE)
  88. set(${LIBRARIES})
  89. set(_combined_name)
  90. if (NOT _libdir)
  91. if (WIN32)
  92. set(_libdir ENV LIB)
  93. elseif (APPLE)
  94. set(_libdir ENV DYLD_LIBRARY_PATH)
  95. else ()
  96. set(_libdir ENV LD_LIBRARY_PATH)
  97. endif ()
  98. endif ()
  99. list(APPEND _libdir "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
  100. foreach(_library ${_list})
  101. set(_combined_name ${_combined_name}_${_library})
  102. if(_libraries_work)
  103. if (BLA_STATIC)
  104. if (WIN32)
  105. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
  106. endif ()
  107. if (APPLE)
  108. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
  109. else ()
  110. set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  111. endif ()
  112. else ()
  113. if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
  114. # for ubuntu's libblas3gf and liblapack3gf packages
  115. set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .so.3gf)
  116. endif ()
  117. endif ()
  118. find_library(${_prefix}_${_library}_LIBRARY
  119. NAMES ${_library}
  120. PATHS ${_libdir}
  121. )
  122. mark_as_advanced(${_prefix}_${_library}_LIBRARY)
  123. set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
  124. set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
  125. endif()
  126. endforeach()
  127. if(_libraries_work)
  128. # Test this combination of libraries.
  129. if(UNIX AND BLA_STATIC)
  130. set(CMAKE_REQUIRED_LIBRARIES ${_flags} "-Wl,--start-group" ${${LIBRARIES}} ${_blas} "-Wl,--end-group" ${_threads})
  131. else()
  132. set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_blas} ${_threads})
  133. endif()
  134. # message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
  135. if (NOT CMAKE_Fortran_COMPILER_LOADED)
  136. check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
  137. else ()
  138. check_fortran_function_exists(${_name} ${_prefix}${_combined_name}_WORKS)
  139. endif ()
  140. set(CMAKE_REQUIRED_LIBRARIES)
  141. set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
  142. #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
  143. endif()
  144. if(_libraries_work)
  145. set(${LIBRARIES} ${${LIBRARIES}} ${_blas} ${_threads})
  146. else()
  147. set(${LIBRARIES} FALSE)
  148. endif()
  149. endmacro()
  150. set(LAPACK_LINKER_FLAGS)
  151. set(LAPACK_LIBRARIES)
  152. set(LAPACK95_LIBRARIES)
  153. if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
  154. find_package(BLAS)
  155. else()
  156. find_package(BLAS REQUIRED)
  157. endif()
  158. if(BLAS_FOUND)
  159. set(LAPACK_LINKER_FLAGS ${BLAS_LINKER_FLAGS})
  160. if (NOT $ENV{BLA_VENDOR} STREQUAL "")
  161. set(BLA_VENDOR $ENV{BLA_VENDOR})
  162. else ()
  163. if(NOT BLA_VENDOR)
  164. set(BLA_VENDOR "All")
  165. endif()
  166. endif ()
  167. #intel lapack
  168. if (BLA_VENDOR MATCHES "Intel" OR BLA_VENDOR STREQUAL "All")
  169. if (NOT WIN32)
  170. set(LAPACK_mkl_LM "-lm")
  171. set(LAPACK_mkl_LDL "-ldl")
  172. endif ()
  173. if (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED)
  174. if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
  175. find_PACKAGE(Threads)
  176. else()
  177. find_package(Threads REQUIRED)
  178. endif()
  179. if (BLA_VENDOR MATCHES "_64ilp")
  180. set(LAPACK_mkl_ILP_MODE "ilp64")
  181. else ()
  182. set(LAPACK_mkl_ILP_MODE "lp64")
  183. endif ()
  184. set(LAPACK_SEARCH_LIBS "")
  185. if (BLA_F95)
  186. set(LAPACK_mkl_SEARCH_SYMBOL "cheev_f95")
  187. set(_LIBRARIES LAPACK95_LIBRARIES)
  188. set(_BLAS_LIBRARIES ${BLAS95_LIBRARIES})
  189. # old
  190. list(APPEND LAPACK_SEARCH_LIBS
  191. "mkl_lapack95")
  192. # new >= 10.3
  193. list(APPEND LAPACK_SEARCH_LIBS
  194. "mkl_intel_c")
  195. list(APPEND LAPACK_SEARCH_LIBS
  196. "mkl_lapack95_${LAPACK_mkl_ILP_MODE}")
  197. else()
  198. set(LAPACK_mkl_SEARCH_SYMBOL "cheev")
  199. set(_LIBRARIES LAPACK_LIBRARIES)
  200. set(_BLAS_LIBRARIES ${BLAS_LIBRARIES})
  201. # old
  202. list(APPEND LAPACK_SEARCH_LIBS
  203. "mkl_lapack")
  204. endif()
  205. # First try empty lapack libs
  206. if (NOT ${_LIBRARIES})
  207. check_lapack_libraries(
  208. ${_LIBRARIES}
  209. LAPACK
  210. ${LAPACK_mkl_SEARCH_SYMBOL}
  211. ""
  212. ""
  213. "${_BLAS_LIBRARIES}"
  214. ""
  215. )
  216. endif ()
  217. # Then try the search libs
  218. foreach (IT ${LAPACK_SEARCH_LIBS})
  219. if (NOT ${_LIBRARIES})
  220. check_lapack_libraries(
  221. ${_LIBRARIES}
  222. LAPACK
  223. ${LAPACK_mkl_SEARCH_SYMBOL}
  224. ""
  225. "${IT}"
  226. "${_BLAS_LIBRARIES}"
  227. "${CMAKE_THREAD_LIBS_INIT};${LAPACK_mkl_LM};${LAPACK_mkl_LDL}"
  228. )
  229. endif ()
  230. endforeach ()
  231. unset(LAPACK_mkl_ILP_MODE)
  232. unset(LAPACK_mkl_SEARCH_SYMBOL)
  233. unset(LAPACK_mkl_LM)
  234. unset(LAPACK_mkl_LDL)
  235. endif ()
  236. endif()
  237. if (BLA_VENDOR STREQUAL "Goto" OR BLA_VENDOR STREQUAL "All")
  238. if(NOT LAPACK_LIBRARIES)
  239. check_lapack_libraries(
  240. LAPACK_LIBRARIES
  241. LAPACK
  242. cheev
  243. ""
  244. "goto2"
  245. "${BLAS_LIBRARIES}"
  246. ""
  247. )
  248. endif()
  249. endif ()
  250. if (BLA_VENDOR STREQUAL "OpenBLAS" OR BLA_VENDOR STREQUAL "All")
  251. if(NOT LAPACK_LIBRARIES)
  252. check_lapack_libraries(
  253. LAPACK_LIBRARIES
  254. LAPACK
  255. cheev
  256. ""
  257. "openblas"
  258. "${BLAS_LIBRARIES}"
  259. ""
  260. )
  261. endif()
  262. endif ()
  263. if (BLA_VENDOR STREQUAL "FLAME" OR BLA_VENDOR STREQUAL "All")
  264. if(NOT LAPACK_LIBRARIES)
  265. check_lapack_libraries(
  266. LAPACK_LIBRARIES
  267. LAPACK
  268. cheev
  269. ""
  270. "flame"
  271. "${BLAS_LIBRARIES}"
  272. ""
  273. )
  274. endif()
  275. endif ()
  276. #acml lapack
  277. if (BLA_VENDOR MATCHES "ACML" OR BLA_VENDOR STREQUAL "All")
  278. if (BLAS_LIBRARIES MATCHES ".+acml.+")
  279. set (LAPACK_LIBRARIES ${BLAS_LIBRARIES})
  280. endif ()
  281. endif ()
  282. # Apple LAPACK library?
  283. if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
  284. if(NOT LAPACK_LIBRARIES)
  285. check_lapack_libraries(
  286. LAPACK_LIBRARIES
  287. LAPACK
  288. cheev
  289. ""
  290. "Accelerate"
  291. "${BLAS_LIBRARIES}"
  292. ""
  293. )
  294. endif()
  295. endif ()
  296. if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
  297. if ( NOT LAPACK_LIBRARIES )
  298. check_lapack_libraries(
  299. LAPACK_LIBRARIES
  300. LAPACK
  301. cheev
  302. ""
  303. "vecLib"
  304. "${BLAS_LIBRARIES}"
  305. ""
  306. )
  307. endif ()
  308. endif ()
  309. # Generic LAPACK library?
  310. if (BLA_VENDOR STREQUAL "Generic" OR
  311. BLA_VENDOR STREQUAL "ATLAS" OR
  312. BLA_VENDOR STREQUAL "All")
  313. if ( NOT LAPACK_LIBRARIES )
  314. check_lapack_libraries(
  315. LAPACK_LIBRARIES
  316. LAPACK
  317. cheev
  318. ""
  319. "lapack"
  320. "${BLAS_LIBRARIES}"
  321. ""
  322. )
  323. endif ()
  324. endif ()
  325. else()
  326. message(STATUS "LAPACK requires BLAS")
  327. endif()
  328. if(BLA_F95)
  329. if(LAPACK95_LIBRARIES)
  330. set(LAPACK95_FOUND TRUE)
  331. else()
  332. set(LAPACK95_FOUND FALSE)
  333. endif()
  334. if(NOT LAPACK_FIND_QUIETLY)
  335. if(LAPACK95_FOUND)
  336. message(STATUS "A library with LAPACK95 API found.")
  337. else()
  338. if(LAPACK_FIND_REQUIRED)
  339. message(FATAL_ERROR
  340. "A required library with LAPACK95 API not found. Please specify library location."
  341. )
  342. else()
  343. message(STATUS
  344. "A library with LAPACK95 API not found. Please specify library location."
  345. )
  346. endif()
  347. endif()
  348. endif()
  349. set(LAPACK_FOUND "${LAPACK95_FOUND}")
  350. set(LAPACK_LIBRARIES "${LAPACK95_LIBRARIES}")
  351. else()
  352. if(LAPACK_LIBRARIES)
  353. set(LAPACK_FOUND TRUE)
  354. else()
  355. set(LAPACK_FOUND FALSE)
  356. endif()
  357. if(NOT LAPACK_FIND_QUIETLY)
  358. if(LAPACK_FOUND)
  359. message(STATUS "A library with LAPACK API found.")
  360. else()
  361. if(LAPACK_FIND_REQUIRED)
  362. message(FATAL_ERROR
  363. "A required library with LAPACK API not found. Please specify library location."
  364. )
  365. else()
  366. message(STATUS
  367. "A library with LAPACK API not found. Please specify library location."
  368. )
  369. endif()
  370. endif()
  371. endif()
  372. endif()
  373. cmake_pop_check_state()
  374. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_lapack_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})