Merge "Merge release-4-5-patches into release-4-6" into release-4-6
[gromacs.git] / cmake / FindLAPACK.cmake
blob0ae98dfa7b9ceb0b6526c0427f89438bf547474e
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/).
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).
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
26 #=============================================================================
27 # Copyright 2007-2009 Kitware, Inc.
29 # Distributed under the OSI-approved BSD License (the "License");
30 # see accompanying file Copyright.txt for details.
32 # This software is distributed WITHOUT ANY WARRANTY; without even the
33 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
34 # See the License for more information.
35 #=============================================================================
36 # (To distribute this file outside of CMake, substitute the full
37 #  License text for the above reference.)
39 set(_lapack_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
41 get_property(_LANGUAGES_ GLOBAL PROPERTY ENABLED_LANGUAGES)
42 if (NOT _LANGUAGES_ MATCHES Fortran)
43 include(CheckFunctionExists)
44 else (NOT _LANGUAGES_ MATCHES Fortran)
45 include(CheckFortranFunctionExists)
46 endif (NOT _LANGUAGES_ MATCHES Fortran)
48 set(LAPACK_FOUND FALSE)
49 set(LAPACK95_FOUND FALSE)
51 # TODO: move this stuff to separate module
53 macro(Check_Lapack_Libraries LIBRARIES _prefix _name _flags _list _blas _threads)
54 # This macro checks for the existence of the combination of fortran libraries
55 # given by _list.  If the combination is found, this macro checks (using the
56 # Check_Fortran_Function_Exists macro) whether can link against that library
57 # combination using the name of a routine given by _name using the linker
58 # flags given by _flags.  If the combination of libraries is found and passes
59 # the link test, LIBRARIES is set to the list of complete library paths that
60 # have been found.  Otherwise, LIBRARIES is set to FALSE.
62 # N.B. _prefix is the prefix applied to the names of all cached variables that
63 # are generated internally and marked advanced by this macro.
65 set(_libraries_work TRUE)
66 set(${LIBRARIES})
67 set(_combined_name)
68 if (NOT _libdir)
69   if (WIN32)
70     set(_libdir ENV LIB)
71   elseif (APPLE)
72     set(_libdir /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV DYLD_LIBRARY_PATH)
73   else ()
74     set(_libdir /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV LD_LIBRARY_PATH)
75   endif ()
76 endif ()
77 foreach(_library ${_list})
78   set(_combined_name ${_combined_name}_${_library})
80   if(_libraries_work)
81     if (BLA_STATIC)
82       if (WIN32)
83         set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
84       endif ( WIN32 )
85       if (APPLE)
86         set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
87       else (APPLE)
88         set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
89       endif (APPLE)
90     else (BLA_STATIC)
91                         if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
92         # for ubuntu's libblas3gf and liblapack3gf packages
93         set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .so.3gf)
94       endif ()
95     endif (BLA_STATIC)
96     find_library(${_prefix}_${_library}_LIBRARY
97       NAMES ${_library}
98       PATHS ${_libdir}
99       )
100     mark_as_advanced(${_prefix}_${_library}_LIBRARY)
101     set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
102     set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
103   endif(_libraries_work)
104 endforeach(_library ${_list})
106 if(_libraries_work)
107   # Test this combination of libraries.
108   if(UNIX AND BLA_STATIC)
109     set(CMAKE_REQUIRED_LIBRARIES ${_flags} "-Wl,--start-group" ${${LIBRARIES}} ${_blas} "-Wl,--end-group" ${_threads})
110   else(UNIX AND BLA_STATIC)
111     set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_blas} ${_threads})
112   endif(UNIX AND BLA_STATIC)
113 #  message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
114   if (NOT _LANGUAGES_ MATCHES Fortran)
115     check_function_exists("${_name}_" ${_prefix}${_combined_name}_WORKS)
116   else (NOT _LANGUAGES_ MATCHES Fortran)
117     check_fortran_function_exists(${_name} ${_prefix}${_combined_name}_WORKS)
118   endif (NOT _LANGUAGES_ MATCHES Fortran)
119   set(CMAKE_REQUIRED_LIBRARIES)
120   mark_as_advanced(${_prefix}${_combined_name}_WORKS)
121   set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
122   #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
123 endif(_libraries_work)
125  if(_libraries_work)
126    set(${LIBRARIES} ${${LIBRARIES}} ${_blas} ${_threads})
127  else(_libraries_work)
128     set(${LIBRARIES} FALSE)
129  endif(_libraries_work)
131 endmacro(Check_Lapack_Libraries)
134 set(LAPACK_LINKER_FLAGS)
135 set(LAPACK_LIBRARIES)
136 set(LAPACK95_LIBRARIES)
139 if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
140   find_package(BLAS)
141 else(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
142   find_package(BLAS REQUIRED)
143 endif(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
146 if(BLAS_FOUND)
147   set(LAPACK_LINKER_FLAGS ${BLAS_LINKER_FLAGS})
148   if ($ENV{BLA_VENDOR} MATCHES ".+")
149     set(BLA_VENDOR $ENV{BLA_VENDOR})
150   else ($ENV{BLA_VENDOR} MATCHES ".+")
151     if(NOT BLA_VENDOR)
152       set(BLA_VENDOR "All")
153     endif(NOT BLA_VENDOR)
154   endif ($ENV{BLA_VENDOR} MATCHES ".+")
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(NOT LAPACK_LIBRARIES)
168 endif (BLA_VENDOR STREQUAL "Goto" OR BLA_VENDOR STREQUAL "All")
171 #acml lapack
172  if (BLA_VENDOR MATCHES "ACML.*" OR BLA_VENDOR STREQUAL "All")
173    if (BLAS_LIBRARIES MATCHES ".+acml.+")
174      set (LAPACK_LIBRARIES ${BLAS_LIBRARIES})
175    endif ()
176  endif ()
178 # Apple LAPACK library?
179 if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
180  if(NOT LAPACK_LIBRARIES)
181   check_lapack_libraries(
182   LAPACK_LIBRARIES
183   LAPACK
184   cheev
185   ""
186   "Accelerate"
187   "${BLAS_LIBRARIES}"
188   ""
189   )
190  endif(NOT LAPACK_LIBRARIES)
191 endif (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
192 if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
193   if ( NOT LAPACK_LIBRARIES )
194     check_lapack_libraries(
195     LAPACK_LIBRARIES
196     LAPACK
197     cheev
198     ""
199     "vecLib"
200     "${BLAS_LIBRARIES}"
201     ""
202     )
203   endif ( NOT LAPACK_LIBRARIES )
204 endif (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
205 # Generic LAPACK library?
206 if (BLA_VENDOR STREQUAL "Generic" OR
207     BLA_VENDOR STREQUAL "ATLAS" OR
208     BLA_VENDOR STREQUAL "All")
209   if ( NOT LAPACK_LIBRARIES )
210     check_lapack_libraries(
211     LAPACK_LIBRARIES
212     LAPACK
213     cheev
214     ""
215     "lapack"
216     "${BLAS_LIBRARIES}"
217     ""
218     )
219   endif ( NOT LAPACK_LIBRARIES )
220 endif ()
221 #intel lapack
222 if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
223   if (NOT WIN32)
224     set(LM "-lm")
225   endif ()
226   if (_LANGUAGES_ MATCHES C OR _LANGUAGES_ MATCHES CXX)
227     if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
228       find_PACKAGE(Threads)
229     else(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
230       find_package(Threads REQUIRED)
231     endif(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
232     if (BLA_F95)
233       if(NOT LAPACK95_LIBRARIES)
234         # old
235         check_lapack_libraries(
236           LAPACK95_LIBRARIES
237           LAPACK
238           cheev
239           ""
240           "mkl_lapack95"
241           "${BLAS95_LIBRARIES}"
242           "${CMAKE_THREAD_LIBS_INIT};${LM}"
243           )
244       endif(NOT LAPACK95_LIBRARIES)
245       if(NOT LAPACK95_LIBRARIES)
246         # new >= 10.3
247         check_lapack_libraries(
248           LAPACK95_LIBRARIES
249           LAPACK
250           CHEEV
251           ""
252           "mkl_intel_lp64"
253           "${BLAS95_LIBRARIES}"
254           "${CMAKE_THREAD_LIBS_INIT};${LM}"
255           )
256       endif(NOT LAPACK95_LIBRARIES)
257     else(BLA_F95)
258       if(NOT LAPACK_LIBRARIES)
259         # old
260         check_lapack_libraries(
261           LAPACK_LIBRARIES
262           LAPACK
263           cheev
264           ""
265           "mkl_lapack"
266           "${BLAS_LIBRARIES}"
267           "${CMAKE_THREAD_LIBS_INIT};${LM}"
268           )
269       endif(NOT LAPACK_LIBRARIES)
270       if(NOT LAPACK_LIBRARIES)
271         # new >= 10.3
272         check_lapack_libraries(
273           LAPACK_LIBRARIES
274           LAPACK
275           cheev
276           ""
277           "mkl_gf_lp64"
278           "${BLAS_LIBRARIES}"
279           "${CMAKE_THREAD_LIBS_INIT};${LM}"
280           )
281       endif(NOT LAPACK_LIBRARIES)
282     endif(BLA_F95)
283   endif (_LANGUAGES_ MATCHES C OR _LANGUAGES_ MATCHES CXX)
284 endif(BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
285 else(BLAS_FOUND)
286   message(STATUS "LAPACK requires BLAS")
287 endif(BLAS_FOUND)
289 if(BLA_F95)
290  if(LAPACK95_LIBRARIES)
291   set(LAPACK95_FOUND TRUE)
292  else(LAPACK95_LIBRARIES)
293   set(LAPACK95_FOUND FALSE)
294  endif(LAPACK95_LIBRARIES)
295  if(NOT LAPACK_FIND_QUIETLY)
296   if(LAPACK95_FOUND)
297     message(STATUS "A library with LAPACK95 API found.")
298   else(LAPACK95_FOUND)
299     if(LAPACK_FIND_REQUIRED)
300       message(FATAL_ERROR
301       "A required library with LAPACK95 API not found. Please specify library location."
302       )
303     else(LAPACK_FIND_REQUIRED)
304       message(STATUS
305       "A library with LAPACK95 API not found. Please specify library location."
306       )
307     endif(LAPACK_FIND_REQUIRED)
308   endif(LAPACK95_FOUND)
309  endif(NOT LAPACK_FIND_QUIETLY)
310  set(LAPACK_FOUND "${LAPACK95_FOUND}")
311  set(LAPACK_LIBRARIES "${LAPACK95_LIBRARIES}")
312 else(BLA_F95)
313  if(LAPACK_LIBRARIES)
314   set(LAPACK_FOUND TRUE)
315  else(LAPACK_LIBRARIES)
316   set(LAPACK_FOUND FALSE)
317  endif(LAPACK_LIBRARIES)
319  if(NOT LAPACK_FIND_QUIETLY)
320   if(LAPACK_FOUND)
321     message(STATUS "A library with LAPACK API found.")
322   else(LAPACK_FOUND)
323     if(LAPACK_FIND_REQUIRED)
324       message(FATAL_ERROR
325       "A required library with LAPACK API not found. Please specify library location."
326       )
327     else(LAPACK_FIND_REQUIRED)
328       message(STATUS
329       "A library with LAPACK API not found. Please specify library location."
330       )
331     endif(LAPACK_FIND_REQUIRED)
332   endif(LAPACK_FOUND)
333  endif(NOT LAPACK_FIND_QUIETLY)
334 endif(BLA_F95)
336 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_lapack_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})