FindLAPACK.cmake 9.2 KB

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