FindLAPACK.cmake 10 KB

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