FindLAPACK.cmake 10 KB

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