FindLAPACK.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. if("${_list}${_blas}" STREQUAL "")
  146. set(${LIBRARIES} "${LIBRARIES}-PLACEHOLDER-FOR-EMPTY-LIBRARIES")
  147. else()
  148. set(${LIBRARIES} ${${LIBRARIES}} ${_blas} ${_threads})
  149. endif()
  150. else()
  151. set(${LIBRARIES} FALSE)
  152. endif()
  153. endmacro()
  154. set(LAPACK_LINKER_FLAGS)
  155. set(LAPACK_LIBRARIES)
  156. set(LAPACK95_LIBRARIES)
  157. if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
  158. find_package(BLAS)
  159. else()
  160. find_package(BLAS REQUIRED)
  161. endif()
  162. if(BLAS_FOUND)
  163. set(LAPACK_LINKER_FLAGS ${BLAS_LINKER_FLAGS})
  164. if (NOT $ENV{BLA_VENDOR} STREQUAL "")
  165. set(BLA_VENDOR $ENV{BLA_VENDOR})
  166. else ()
  167. if(NOT BLA_VENDOR)
  168. set(BLA_VENDOR "All")
  169. endif()
  170. endif ()
  171. #intel lapack
  172. if (BLA_VENDOR MATCHES "Intel" OR BLA_VENDOR STREQUAL "All")
  173. if(NOT LAPACK_LIBRARIES)
  174. if (NOT WIN32)
  175. set(LAPACK_mkl_LM "-lm")
  176. set(LAPACK_mkl_LDL "-ldl")
  177. endif ()
  178. if (CMAKE_C_COMPILER_LOADED OR CMAKE_CXX_COMPILER_LOADED)
  179. if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
  180. find_PACKAGE(Threads)
  181. else()
  182. find_package(Threads REQUIRED)
  183. endif()
  184. if (BLA_VENDOR MATCHES "_64ilp")
  185. set(LAPACK_mkl_ILP_MODE "ilp64")
  186. else ()
  187. set(LAPACK_mkl_ILP_MODE "lp64")
  188. endif ()
  189. set(LAPACK_SEARCH_LIBS "")
  190. if (BLA_F95)
  191. set(LAPACK_mkl_SEARCH_SYMBOL "cheev_f95")
  192. set(_LIBRARIES LAPACK95_LIBRARIES)
  193. set(_BLAS_LIBRARIES ${BLAS95_LIBRARIES})
  194. # old
  195. list(APPEND LAPACK_SEARCH_LIBS
  196. "mkl_lapack95")
  197. # new >= 10.3
  198. list(APPEND LAPACK_SEARCH_LIBS
  199. "mkl_intel_c")
  200. list(APPEND LAPACK_SEARCH_LIBS
  201. "mkl_lapack95_${LAPACK_mkl_ILP_MODE}")
  202. else()
  203. set(LAPACK_mkl_SEARCH_SYMBOL "cheev")
  204. set(_LIBRARIES LAPACK_LIBRARIES)
  205. set(_BLAS_LIBRARIES ${BLAS_LIBRARIES})
  206. # old
  207. list(APPEND LAPACK_SEARCH_LIBS
  208. "mkl_lapack")
  209. endif()
  210. # First try empty lapack libs
  211. if (NOT ${_LIBRARIES})
  212. check_lapack_libraries(
  213. ${_LIBRARIES}
  214. LAPACK
  215. ${LAPACK_mkl_SEARCH_SYMBOL}
  216. ""
  217. ""
  218. "${_BLAS_LIBRARIES}"
  219. ""
  220. )
  221. endif ()
  222. # Then try the search libs
  223. foreach (IT ${LAPACK_SEARCH_LIBS})
  224. if (NOT ${_LIBRARIES})
  225. check_lapack_libraries(
  226. ${_LIBRARIES}
  227. LAPACK
  228. ${LAPACK_mkl_SEARCH_SYMBOL}
  229. ""
  230. "${IT}"
  231. "${_BLAS_LIBRARIES}"
  232. "${CMAKE_THREAD_LIBS_INIT};${LAPACK_mkl_LM};${LAPACK_mkl_LDL}"
  233. )
  234. endif ()
  235. endforeach ()
  236. unset(LAPACK_mkl_ILP_MODE)
  237. unset(LAPACK_mkl_SEARCH_SYMBOL)
  238. unset(LAPACK_mkl_LM)
  239. unset(LAPACK_mkl_LDL)
  240. endif ()
  241. endif()
  242. endif()
  243. if (BLA_VENDOR STREQUAL "Goto" OR BLA_VENDOR STREQUAL "All")
  244. if(NOT LAPACK_LIBRARIES)
  245. check_lapack_libraries(
  246. LAPACK_LIBRARIES
  247. LAPACK
  248. cheev
  249. ""
  250. "goto2"
  251. "${BLAS_LIBRARIES}"
  252. ""
  253. )
  254. endif()
  255. endif ()
  256. if (BLA_VENDOR STREQUAL "OpenBLAS" OR BLA_VENDOR STREQUAL "All")
  257. if(NOT LAPACK_LIBRARIES)
  258. check_lapack_libraries(
  259. LAPACK_LIBRARIES
  260. LAPACK
  261. cheev
  262. ""
  263. "openblas"
  264. "${BLAS_LIBRARIES}"
  265. ""
  266. )
  267. endif()
  268. endif ()
  269. if (BLA_VENDOR STREQUAL "FLAME" OR BLA_VENDOR STREQUAL "All")
  270. if(NOT LAPACK_LIBRARIES)
  271. check_lapack_libraries(
  272. LAPACK_LIBRARIES
  273. LAPACK
  274. cheev
  275. ""
  276. "flame"
  277. "${BLAS_LIBRARIES}"
  278. ""
  279. )
  280. endif()
  281. endif ()
  282. #acml lapack
  283. if (BLA_VENDOR MATCHES "ACML" OR BLA_VENDOR STREQUAL "All")
  284. if (BLAS_LIBRARIES MATCHES ".+acml.+")
  285. set (LAPACK_LIBRARIES ${BLAS_LIBRARIES})
  286. endif ()
  287. endif ()
  288. # Apple LAPACK library?
  289. if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
  290. if(NOT LAPACK_LIBRARIES)
  291. check_lapack_libraries(
  292. LAPACK_LIBRARIES
  293. LAPACK
  294. cheev
  295. ""
  296. "Accelerate"
  297. "${BLAS_LIBRARIES}"
  298. ""
  299. )
  300. endif()
  301. endif ()
  302. if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
  303. if ( NOT LAPACK_LIBRARIES )
  304. check_lapack_libraries(
  305. LAPACK_LIBRARIES
  306. LAPACK
  307. cheev
  308. ""
  309. "vecLib"
  310. "${BLAS_LIBRARIES}"
  311. ""
  312. )
  313. endif ()
  314. endif ()
  315. # Generic LAPACK library?
  316. if (BLA_VENDOR STREQUAL "Generic" OR
  317. BLA_VENDOR STREQUAL "ATLAS" OR
  318. BLA_VENDOR STREQUAL "All")
  319. if ( NOT LAPACK_LIBRARIES )
  320. check_lapack_libraries(
  321. LAPACK_LIBRARIES
  322. LAPACK
  323. cheev
  324. ""
  325. "lapack"
  326. "${BLAS_LIBRARIES}"
  327. ""
  328. )
  329. endif ()
  330. endif ()
  331. else()
  332. message(STATUS "LAPACK requires BLAS")
  333. endif()
  334. if(BLA_F95)
  335. if(LAPACK95_LIBRARIES)
  336. set(LAPACK95_FOUND TRUE)
  337. else()
  338. set(LAPACK95_FOUND FALSE)
  339. endif()
  340. if(NOT LAPACK_FIND_QUIETLY)
  341. if(LAPACK95_FOUND)
  342. message(STATUS "A library with LAPACK95 API found.")
  343. else()
  344. if(LAPACK_FIND_REQUIRED)
  345. message(FATAL_ERROR
  346. "A required library with LAPACK95 API not found. Please specify library location."
  347. )
  348. else()
  349. message(STATUS
  350. "A library with LAPACK95 API not found. Please specify library location."
  351. )
  352. endif()
  353. endif()
  354. endif()
  355. set(LAPACK_FOUND "${LAPACK95_FOUND}")
  356. set(LAPACK_LIBRARIES "${LAPACK95_LIBRARIES}")
  357. else()
  358. if(LAPACK_LIBRARIES)
  359. set(LAPACK_FOUND TRUE)
  360. else()
  361. set(LAPACK_FOUND FALSE)
  362. endif()
  363. if(NOT LAPACK_FIND_QUIETLY)
  364. if(LAPACK_FOUND)
  365. message(STATUS "A library with LAPACK API found.")
  366. else()
  367. if(LAPACK_FIND_REQUIRED)
  368. message(FATAL_ERROR
  369. "A required library with LAPACK API not found. Please specify library location."
  370. )
  371. else()
  372. message(STATUS
  373. "A library with LAPACK API not found. Please specify library location."
  374. )
  375. endif()
  376. endif()
  377. endif()
  378. endif()
  379. # On compilers that implicitly link LAPACK (such as ftn, cc, and CC on Cray HPC machines)
  380. # we used a placeholder for empty LAPACK_LIBRARIES to get through our logic above.
  381. if (LAPACK_LIBRARIES STREQUAL "LAPACK_LIBRARIES-PLACEHOLDER-FOR-EMPTY-LIBRARIES")
  382. set(LAPACK_LIBRARIES "")
  383. endif()
  384. cmake_pop_check_state()
  385. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_lapack_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})