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