FindLAPACK.cmake 10 KB

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