blob: 3fd738807090183de5f57ac2466582cc7126c4a2 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001# 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)
Austin Schuhc55b0172022-02-20 17:52:35 -080029include(CMakeFindDependencyMacro)
Brian Silverman72890c22015-09-19 14:37:37 -040030
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})
Austin Schuhc55b0172022-02-20 17:52:35 -080079 endif()
80 endforeach()
Brian Silverman72890c22015-09-19 14:37:37 -040081 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.
Austin Schuhc55b0172022-02-20 17:52:35 -080092 find_dependency( F2C QUIET )
Brian Silverman72890c22015-09-19 14:37:37 -040093 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})
Austin Schuhc55b0172022-02-20 17:52:35 -0800107 endif()
Brian Silverman72890c22015-09-19 14:37:37 -0400108
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})
Austin Schuhc55b0172022-02-20 17:52:35 -0800121 endif()
Brian Silverman72890c22015-09-19 14:37:37 -0400122
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}}")
Austin Schuhc55b0172022-02-20 17:52:35 -0800130endmacro()
Brian Silverman72890c22015-09-19 14:37:37 -0400131
132
133#
134# main
135#
136
137# LAPACK requires BLAS
138if(LAPACK_FIND_QUIETLY OR NOT LAPACK_FIND_REQUIRED)
Austin Schuhc55b0172022-02-20 17:52:35 -0800139 find_dependency(BLAS)
Brian Silverman72890c22015-09-19 14:37:37 -0400140else()
Austin Schuhc55b0172022-02-20 17:52:35 -0800141 find_dependency(BLAS REQUIRED)
Brian Silverman72890c22015-09-19 14:37:37 -0400142endif()
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 )
Austin Schuhc55b0172022-02-20 17:52:35 -0800220 endif ()
Brian Silverman72890c22015-09-19 14:37:37 -0400221
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.")
Austin Schuhc55b0172022-02-20 17:52:35 -0800246 else()
Brian Silverman72890c22015-09-19 14:37:37 -0400247 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()
Austin Schuhc55b0172022-02-20 17:52:35 -0800252 endif()
253 endif()
Brian Silverman72890c22015-09-19 14:37:37 -0400254
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
Austin Schuhc55b0172022-02-20 17:52:35 -0800274endif()