xref: /aosp_15_r20/external/eigen/cmake/FindLAPACK.cmake (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1# Find LAPACK library
2#
3# This module finds an installed library that implements the LAPACK
4# linear-algebra interface (see http://www.netlib.org/lapack/).
5# The approach follows mostly 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_INCLUDE_DIR - Directories containing the LAPACK header files
12#  LAPACK_DEFINITIONS - Compilation options to use LAPACK
13#  LAPACK_LINKER_FLAGS - Linker flags to use LAPACK (excluding -l
14#    and -L).
15#  LAPACK_LIBRARIES_DIR - Directories containing the LAPACK libraries.
16#     May be null if LAPACK_LIBRARIES contains libraries name using full path.
17#  LAPACK_LIBRARIES - List of libraries to link against LAPACK interface.
18#     May be null if the compiler supports auto-link (e.g. VC++).
19#  LAPACK_USE_FILE - The name of the cmake module to include to compile
20#     applications or libraries using LAPACK.
21#
22# This module was modified by CGAL team:
23# - find libraries for a C++ compiler, instead of Fortran
24# - added LAPACK_INCLUDE_DIR, LAPACK_DEFINITIONS and LAPACK_LIBRARIES_DIR
25# - removed LAPACK95_LIBRARIES
26
27
28include(CheckFunctionExists)
29include(CMakeFindDependencyMacro)
30
31# This macro checks for the existence of the combination of fortran libraries
32# given by _list.  If the combination is found, this macro checks (using the
33# check_function_exists macro) whether can link against that library
34# combination using the name of a routine given by _name using the linker
35# flags given by _flags.  If the combination of libraries is found and passes
36# the link test, LIBRARIES is set to the list of complete library paths that
37# have been found and DEFINITIONS to the required definitions.
38# Otherwise, LIBRARIES is set to FALSE.
39# N.B. _prefix is the prefix applied to the names of all cached variables that
40# are generated internally and marked advanced by this macro.
41macro(check_lapack_libraries DEFINITIONS LIBRARIES _prefix _name _flags _list _blas _path)
42  #message("DEBUG: check_lapack_libraries(${_list} in ${_path} with ${_blas})")
43
44  # Check for the existence of the libraries given by _list
45  set(_libraries_found TRUE)
46  set(_libraries_work FALSE)
47  set(${DEFINITIONS} "")
48  set(${LIBRARIES} "")
49  set(_combined_name)
50  foreach(_library ${_list})
51    set(_combined_name ${_combined_name}_${_library})
52
53    if(_libraries_found)
54      # search first in ${_path}
55      find_library(${_prefix}_${_library}_LIBRARY
56                  NAMES ${_library}
57                  PATHS ${_path} NO_DEFAULT_PATH
58                  )
59      # if not found, search in environment variables and system
60      if ( WIN32 )
61        find_library(${_prefix}_${_library}_LIBRARY
62                    NAMES ${_library}
63                    PATHS ENV LIB
64                    )
65      elseif ( APPLE )
66        find_library(${_prefix}_${_library}_LIBRARY
67                    NAMES ${_library}
68                    PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV DYLD_LIBRARY_PATH
69                    )
70      else ()
71        find_library(${_prefix}_${_library}_LIBRARY
72                    NAMES ${_library}
73                    PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV LD_LIBRARY_PATH
74                    )
75      endif()
76      mark_as_advanced(${_prefix}_${_library}_LIBRARY)
77      set(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
78      set(_libraries_found ${${_prefix}_${_library}_LIBRARY})
79    endif()
80  endforeach()
81  if(_libraries_found)
82    set(_libraries_found ${${LIBRARIES}})
83  endif()
84
85  # Test this combination of libraries with the Fortran/f2c interface.
86  # We test the Fortran interface first as it is well standardized.
87  if(_libraries_found AND NOT _libraries_work)
88    set(${DEFINITIONS}  "-D${_prefix}_USE_F2C")
89    set(${LIBRARIES}    ${_libraries_found})
90    # Some C++ linkers require the f2c library to link with Fortran libraries.
91    # I do not know which ones, thus I just add the f2c library if it is available.
92    find_dependency( F2C QUIET )
93    if ( F2C_FOUND )
94      set(${DEFINITIONS}  ${${DEFINITIONS}} ${F2C_DEFINITIONS})
95      set(${LIBRARIES}    ${${LIBRARIES}} ${F2C_LIBRARIES})
96    endif()
97    set(CMAKE_REQUIRED_DEFINITIONS  ${${DEFINITIONS}})
98    set(CMAKE_REQUIRED_LIBRARIES    ${_flags} ${${LIBRARIES}} ${_blas})
99    #message("DEBUG: CMAKE_REQUIRED_DEFINITIONS = ${CMAKE_REQUIRED_DEFINITIONS}")
100    #message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
101    # Check if function exists with f2c calling convention (ie a trailing underscore)
102    check_function_exists(${_name}_ ${_prefix}_${_name}_${_combined_name}_f2c_WORKS)
103    set(CMAKE_REQUIRED_DEFINITIONS} "")
104    set(CMAKE_REQUIRED_LIBRARIES    "")
105    mark_as_advanced(${_prefix}_${_name}_${_combined_name}_f2c_WORKS)
106    set(_libraries_work ${${_prefix}_${_name}_${_combined_name}_f2c_WORKS})
107  endif()
108
109  # If not found, test this combination of libraries with a C interface.
110  # A few implementations (ie ACML) provide a C interface. Unfortunately, there is no standard.
111  if(_libraries_found AND NOT _libraries_work)
112    set(${DEFINITIONS} "")
113    set(${LIBRARIES}   ${_libraries_found})
114    set(CMAKE_REQUIRED_DEFINITIONS "")
115    set(CMAKE_REQUIRED_LIBRARIES   ${_flags} ${${LIBRARIES}} ${_blas})
116    #message("DEBUG: CMAKE_REQUIRED_LIBRARIES = ${CMAKE_REQUIRED_LIBRARIES}")
117    check_function_exists(${_name} ${_prefix}_${_name}${_combined_name}_WORKS)
118    set(CMAKE_REQUIRED_LIBRARIES "")
119    mark_as_advanced(${_prefix}_${_name}${_combined_name}_WORKS)
120    set(_libraries_work ${${_prefix}_${_name}${_combined_name}_WORKS})
121  endif()
122
123  # on failure
124  if(NOT _libraries_work)
125    set(${DEFINITIONS} "")
126    set(${LIBRARIES}   FALSE)
127  endif()
128  #message("DEBUG: ${DEFINITIONS} = ${${DEFINITIONS}}")
129  #message("DEBUG: ${LIBRARIES} = ${${LIBRARIES}}")
130endmacro()
131
132
133#
134# main
135#
136
137# LAPACK requires BLAS
138if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
139  find_dependency(BLAS)
140else()
141  find_dependency(BLAS REQUIRED)
142endif()
143
144if (NOT BLAS_FOUND)
145
146  message(STATUS "LAPACK requires BLAS.")
147  set(LAPACK_FOUND FALSE)
148
149# Is it already configured?
150elseif (LAPACK_LIBRARIES_DIR OR LAPACK_LIBRARIES)
151
152  set(LAPACK_FOUND TRUE)
153
154else()
155
156  # reset variables
157  set( LAPACK_INCLUDE_DIR "" )
158  set( LAPACK_DEFINITIONS "" )
159  set( LAPACK_LINKER_FLAGS "" ) # unused (yet)
160  set( LAPACK_LIBRARIES "" )
161  set( LAPACK_LIBRARIES_DIR "" )
162
163    #
164    # If Unix, search for LAPACK function in possible libraries
165    #
166
167    #intel mkl lapack?
168    if(NOT LAPACK_LIBRARIES)
169      check_lapack_libraries(
170      LAPACK_DEFINITIONS
171      LAPACK_LIBRARIES
172      LAPACK
173      cheev
174      ""
175      "mkl_lapack"
176      "${BLAS_LIBRARIES}"
177      "${CGAL_TAUCS_LIBRARIES_DIR} ENV LAPACK_LIB_DIR"
178      )
179    endif()
180
181    #acml lapack?
182    if(NOT LAPACK_LIBRARIES)
183      check_lapack_libraries(
184      LAPACK_DEFINITIONS
185      LAPACK_LIBRARIES
186      LAPACK
187      cheev
188      ""
189      "acml"
190      "${BLAS_LIBRARIES}"
191      "${CGAL_TAUCS_LIBRARIES_DIR} ENV LAPACK_LIB_DIR"
192      )
193    endif()
194
195    # Apple LAPACK library?
196    if(NOT LAPACK_LIBRARIES)
197      check_lapack_libraries(
198      LAPACK_DEFINITIONS
199      LAPACK_LIBRARIES
200      LAPACK
201      cheev
202      ""
203      "Accelerate"
204      "${BLAS_LIBRARIES}"
205      "${CGAL_TAUCS_LIBRARIES_DIR} ENV LAPACK_LIB_DIR"
206      )
207    endif()
208
209    if ( NOT LAPACK_LIBRARIES )
210      check_lapack_libraries(
211      LAPACK_DEFINITIONS
212      LAPACK_LIBRARIES
213      LAPACK
214      cheev
215      ""
216      "vecLib"
217      "${BLAS_LIBRARIES}"
218      "${CGAL_TAUCS_LIBRARIES_DIR} ENV LAPACK_LIB_DIR"
219      )
220    endif ()
221
222    # Generic LAPACK library?
223    # This configuration *must* be the last try as this library is notably slow.
224    if ( NOT LAPACK_LIBRARIES )
225      check_lapack_libraries(
226      LAPACK_DEFINITIONS
227      LAPACK_LIBRARIES
228      LAPACK
229      cheev
230      ""
231      "lapack"
232      "${BLAS_LIBRARIES}"
233      "${CGAL_TAUCS_LIBRARIES_DIR} ENV LAPACK_LIB_DIR"
234      )
235    endif()
236
237  if(LAPACK_LIBRARIES_DIR OR LAPACK_LIBRARIES)
238    set(LAPACK_FOUND TRUE)
239  else()
240    set(LAPACK_FOUND FALSE)
241  endif()
242
243  if(NOT LAPACK_FIND_QUIETLY)
244    if(LAPACK_FOUND)
245      message(STATUS "A library with LAPACK API found.")
246    else()
247      if(LAPACK_FIND_REQUIRED)
248        message(FATAL_ERROR "A required library with LAPACK API not found. Please specify library location.")
249      else()
250        message(STATUS "A library with LAPACK API not found. Please specify library location.")
251      endif()
252    endif()
253  endif()
254
255  # Add variables to cache
256  set( LAPACK_INCLUDE_DIR   "${LAPACK_INCLUDE_DIR}"
257                            CACHE PATH "Directories containing the LAPACK header files" FORCE )
258  set( LAPACK_DEFINITIONS   "${LAPACK_DEFINITIONS}"
259                            CACHE STRING "Compilation options to use LAPACK" FORCE )
260  set( LAPACK_LINKER_FLAGS  "${LAPACK_LINKER_FLAGS}"
261                            CACHE STRING "Linker flags to use LAPACK" FORCE )
262  set( LAPACK_LIBRARIES     "${LAPACK_LIBRARIES}"
263                            CACHE FILEPATH "LAPACK libraries name" FORCE )
264  set( LAPACK_LIBRARIES_DIR "${LAPACK_LIBRARIES_DIR}"
265                            CACHE PATH "Directories containing the LAPACK libraries" FORCE )
266
267  #message("DEBUG: LAPACK_INCLUDE_DIR = ${LAPACK_INCLUDE_DIR}")
268  #message("DEBUG: LAPACK_DEFINITIONS = ${LAPACK_DEFINITIONS}")
269  #message("DEBUG: LAPACK_LINKER_FLAGS = ${LAPACK_LINKER_FLAGS}")
270  #message("DEBUG: LAPACK_LIBRARIES = ${LAPACK_LIBRARIES}")
271  #message("DEBUG: LAPACK_LIBRARIES_DIR = ${LAPACK_LIBRARIES_DIR}")
272  #message("DEBUG: LAPACK_FOUND = ${LAPACK_FOUND}")
273
274endif()
275