Replace our fftpack version with Numpy's version
[gromacs.git] / cmake / FindLAPACK.cmake
blob58dd7e752a6f80982ac69c537628845079611595
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 distributed this file outside of CMake, substitute the full
37 #  License text for the above reference.)
39 get_property(_LANGUAGES_ GLOBAL PROPERTY ENABLED_LANGUAGES)
40 if(NOT _LANGUAGES_ MATCHES Fortran)
41   if(LAPACK_FIND_REQUIRED)
42     message(FATAL_ERROR
43       "FindLAPACK is Fortran-only so Fortran must be enabled.")
44   else(LAPACK_FIND_REQUIRED)
45     message(STATUS "Looking for LAPACK... - NOT found (Fortran not enabled)")
46     return()
47   endif(LAPACK_FIND_REQUIRED)
48 endif(NOT _LANGUAGES_ MATCHES Fortran)
50 include(CheckFortranFunctionExists)
51 set(LAPACK_FOUND FALSE)
52 set(LAPACK95_FOUND FALSE)
54 macro(Check_Lapack_Libraries LIBRARIES _prefix _name _flags _list _blas _threads)
55 # This macro checks for the existence of the combination of fortran libraries
56 # given by _list.  If the combination is found, this macro checks (using the
57 # Check_Fortran_Function_Exists macro) whether can link against that library
58 # combination using the name of a routine given by _name using the linker
59 # flags given by _flags.  If the combination of libraries is found and passes
60 # the link test, LIBRARIES is set to the list of complete library paths that
61 # have been found.  Otherwise, LIBRARIES is set to FALSE.
63 # N.B. _prefix is the prefix applied to the names of all cached variables that
64 # are generated internally and marked advanced by this macro.
66 set(_libraries_work TRUE)
67 set(${LIBRARIES})
68 set(_combined_name)
69 foreach(_library ${_list})
70   set(_combined_name ${_combined_name}_${_library})
72   if(_libraries_work)
73   IF (WIN32)
74     if(BLA_STATIC)
75       set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib;.dll")
76     endif(BLA_STATIC)
77     find_library(${_prefix}_${_library}_LIBRARY
78     NAMES ${_library}
79     PATHS ENV LIB
80     )
81   ENDIF (WIN32)
83   if(APPLE)
84     if(BLA_STATIC)
85       set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so;.dylib")
86     endif(BLA_STATIC)
87     find_library(${_prefix}_${_library}_LIBRARY
88     NAMES ${_library}
89     PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV DYLD_LIBRARY_PATH
90     )
91     else(APPLE)
92     if(BLA_STATIC)
93      set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
94     endif(BLA_STATIC)
95     find_library(${_prefix}_${_library}_LIBRARY
96     NAMES ${_library}
97     PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV LD_LIBRARY_PATH
98     )
99     endif(APPLE)
101     mark_as_advanced(${_prefix}_${_library}_LIBRARY)
102     set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
103     set(_libraries_work ${${_prefix}_${_library}_LIBRARY})
104   endif(_libraries_work)
105 endforeach(_library ${_list})
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(UNIX AND BLA_STATIC)
112     set(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}} ${_blas} ${_threads})
113   endif(UNIX AND BLA_STATIC)
114 #  message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
115   check_fortran_function_exists(${_name} ${_prefix}${_combined_name}_WORKS)
116   set(CMAKE_REQUIRED_LIBRARIES)
117   mark_as_advanced(${_prefix}${_combined_name}_WORKS)
118   set(_libraries_work ${${_prefix}${_combined_name}_WORKS})
119   #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
120 endif(_libraries_work)
122  if(_libraries_work)
123    set(${LIBRARIES} ${${LIBRARIES}} ${_blas})
124  else(_libraries_work)
125     set(${LIBRARIES} FALSE)
126  endif(_libraries_work)
128 endmacro(Check_Lapack_Libraries)
131 set(LAPACK_LINKER_FLAGS)
132 set(LAPACK_LIBRARIES)
133 set(LAPACK95_LIBRARIES)
136 if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
137   find_package(BLAS)
138 else(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
139   find_package(BLAS REQUIRED)
140 endif(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
143 if(BLAS_FOUND)
144   set(LAPACK_LINKER_FLAGS ${BLAS_LINKER_FLAGS})
145   if ($ENV{BLA_VENDOR} MATCHES ".+")
146     set(BLA_VENDOR $ENV{BLA_VENDOR})
147   else ($ENV{BLA_VENDOR} MATCHES ".+")
148     if(NOT BLA_VENDOR)
149       set(BLA_VENDOR "All")
150     endif(NOT BLA_VENDOR)
151   endif ($ENV{BLA_VENDOR} MATCHES ".+")
152 #acml lapack
153  if (BLA_VENDOR STREQUAL "ACML" OR BLA_VENDOR STREQUAL "All")
154   if(NOT LAPACK_LIBRARIES)
155    check_lapack_libraries(
156     LAPACK_LIBRARIES
157     LAPACK
158     cheev
159     ""
160     "acml"
161     ""
162     ""
163     )
164   endif(NOT LAPACK_LIBRARIES)
165  endif (BLA_VENDOR STREQUAL "ACML" OR BLA_VENDOR STREQUAL "All")
167 # Apple LAPACK library?
168 if (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
169  if(NOT LAPACK_LIBRARIES)
170   check_lapack_libraries(
171   LAPACK_LIBRARIES
172   LAPACK
173   cheev
174   ""
175   "Accelerate"
176   "${BLAS_LIBRARIES}"
177   ""
178   )
179  endif(NOT LAPACK_LIBRARIES)
180 endif (BLA_VENDOR STREQUAL "Apple" OR BLA_VENDOR STREQUAL "All")
181 if (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
182   if ( NOT LAPACK_LIBRARIES )
183     check_lapack_libraries(
184     LAPACK_LIBRARIES
185     LAPACK
186     cheev
187     ""
188     "vecLib"
189     "${BLAS_LIBRARIES}"
190     ""
191     )
192   endif ( NOT LAPACK_LIBRARIES )
193 endif (BLA_VENDOR STREQUAL "NAS" OR BLA_VENDOR STREQUAL "All")
194 # Generic LAPACK library?
195 if (BLA_VENDOR STREQUAL "Generic" OR BLA_VENDOR STREQUAL "All")
196   if ( NOT LAPACK_LIBRARIES )
197     check_lapack_libraries(
198     LAPACK_LIBRARIES
199     LAPACK
200     cheev
201     ""
202     "lapack"
203     "${BLAS_LIBRARIES}"
204     ""
205     )
206   endif ( NOT LAPACK_LIBRARIES )
207 endif (BLA_VENDOR STREQUAL "Generic" OR BLA_VENDOR STREQUAL "All")
208 #intel lapack
209  if (BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
210   if (_LANGUAGES_ MATCHES C OR _LANGUAGES_ MATCHES CXX)
211    if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
212       find_PACKAGE(Threads)
213    else(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
214        find_package(Threads REQUIRED)
215    endif(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
216    if (BLA_F95)
217     if(NOT LAPACK95_LIBRARIES)
218      check_lapack_libraries(
219      LAPACK95_LIBRARIES
220      LAPACK
221      cheev
222      ""
223      "mkl_lapack95"
224      "${BLAS95_LIBRARIES}"
225      "${CMAKE_THREAD_LIBS_INIT}"
226      )
227     endif(NOT LAPACK95_LIBRARIES)
228    else(BLA_F95)
229     if(NOT LAPACK_LIBRARIES)
230      check_lapack_libraries(
231      LAPACK_LIBRARIES
232      LAPACK
233      cheev
234      ""
235      "mkl_lapack"
236      "${BLAS_LIBRARIES}"
237      "${CMAKE_THREAD_LIBS_INIT}"
238      )
239     endif(NOT LAPACK_LIBRARIES)
240    endif(BLA_F95)
241   endif (_LANGUAGES_ MATCHES C OR _LANGUAGES_ MATCHES CXX)
242  endif(BLA_VENDOR MATCHES "Intel*" OR BLA_VENDOR STREQUAL "All")
243 else(BLAS_FOUND)
244   message(STATUS "LAPACK requires BLAS")
245 endif(BLAS_FOUND)
247 if(BLA_F95)
248  if(LAPACK95_LIBRARIES)
249   set(LAPACK95_FOUND TRUE)
250  else(LAPACK95_LIBRARIES)
251   set(LAPACK95_FOUND FALSE)
252  endif(LAPACK95_LIBRARIES)
253  if(NOT LAPACK_FIND_QUIETLY)
254   if(LAPACK95_FOUND)
255     message(STATUS "A library with LAPACK95 API found.")
256   else(LAPACK95_FOUND)
257     if(LAPACK_FIND_REQUIRED)
258       message(FATAL_ERROR
259       "A required library with LAPACK95 API not found. Please specify library location."
260       )
261     else(LAPACK_FIND_REQUIRED)
262       message(STATUS
263       "A library with LAPACK95 API not found. Please specify library location."
264       )
265     endif(LAPACK_FIND_REQUIRED)
266   endif(LAPACK95_FOUND)
267  endif(NOT LAPACK_FIND_QUIETLY)
268  set(LAPACK_FOUND "${LAPACK95_FOUND}")
269  set(LAPACK_LIBRARIES "${LAPACK95_LIBRARIES}")
270 else(BLA_F95)
271  if(LAPACK_LIBRARIES)
272   set(LAPACK_FOUND TRUE)
273  else(LAPACK_LIBRARIES)
274   set(LAPACK_FOUND FALSE)
275  endif(LAPACK_LIBRARIES)
277  if(NOT LAPACK_FIND_QUIETLY)
278   if(LAPACK_FOUND)
279     message(STATUS "A library with LAPACK API found.")
280   else(LAPACK_FOUND)
281     if(LAPACK_FIND_REQUIRED)
282       message(FATAL_ERROR
283       "A required library with LAPACK API not found. Please specify library location."
284       )
285     else(LAPACK_FIND_REQUIRED)
286       message(STATUS
287       "A library with LAPACK API not found. Please specify library location."
288       )
289     endif(LAPACK_FIND_REQUIRED)
290   endif(LAPACK_FOUND)
291  endif(NOT LAPACK_FIND_QUIETLY)
292 endif(BLA_F95)