Debian Codegraph @

cmake/modules/FindGSL.cmake

raw
1 # SPDX-FileCopyrightText: 2000-2023 Kitware, Inc. and Contributors
2 # SPDX-FileCopyrightText: 2023 L. E. Segovia <[email protected]>
3 # SPDX-License-Ref: BSD-3-Clause
4
5 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
6 # file Copyright.txt or https://cmake.org/licensing for details.
7
8 #[=======================================================================[.rst:
9 FindGSL
10 --------
11
12 .. versionadded:: 3.2
13
14 Find the native GNU Scientific Library (GSL) includes and libraries.
15
16 The GNU Scientific Library (GSL) is a numerical library for C and C++
17 programmers. It is free software under the GNU General Public
18 License.
19
20 Imported Targets
21 ^^^^^^^^^^^^^^^^
22
23 If GSL is found, this module defines the following :prop_tgt:`IMPORTED`
24 targets::
25
26 GSL::gsl - The main GSL library.
27 GSL::gslcblas - The CBLAS support library used by GSL.
28
29 Result Variables
30 ^^^^^^^^^^^^^^^^
31
32 This module will set the following variables in your project::
33
34 GSL_FOUND - True if GSL found on the local system
35 GSL_INCLUDE_DIRS - Location of GSL header files.
36 GSL_LIBRARIES - The GSL libraries.
37 GSL_VERSION - The version of the discovered GSL install.
38
39 Hints
40 ^^^^^
41
42 Set ``GSL_ROOT_DIR`` to a directory that contains a GSL installation.
43
44 This script expects to find libraries at ``$GSL_ROOT_DIR/lib`` and the GSL
45 headers at ``$GSL_ROOT_DIR/include/gsl``. The library directory may
46 optionally provide Release and Debug folders. If available, the libraries
47 named ``gsld``, ``gslblasd`` or ``cblasd`` are recognized as debug libraries.
48 For Unix-like systems, this script will use ``$GSL_ROOT_DIR/bin/gsl-config``
49 (if found) to aid in the discovery of GSL.
50
51 Cache Variables
52 ^^^^^^^^^^^^^^^
53
54 This module may set the following variables depending on platform and type
55 of GSL installation discovered. These variables may optionally be set to
56 help this module find the correct files::
57
58 GSL_CBLAS_LIBRARY - Location of the GSL CBLAS library.
59 GSL_CBLAS_LIBRARY_DEBUG - Location of the debug GSL CBLAS library (if any).
60 GSL_CONFIG_EXECUTABLE - Location of the ``gsl-config`` script (if any).
61 GSL_LIBRARY - Location of the GSL library.
62 GSL_LIBRARY_DEBUG - Location of the debug GSL library (if any).
63
64 #]=======================================================================]
65
66 include(FindPackageHandleStandardArgs)
67
68 #=============================================================================
69 # If the user has provided ``GSL_ROOT_DIR``, use it! Choose items found
70 # at this location over system locations.
71 if( EXISTS "$ENV{GSL_ROOT_DIR}" )
72 file( TO_CMAKE_PATH "$ENV{GSL_ROOT_DIR}" GSL_ROOT_DIR )
73 set( GSL_ROOT_DIR "${GSL_ROOT_DIR}" CACHE PATH "Prefix for GSL installation." )
74 endif()
75 if( NOT EXISTS "${GSL_ROOT_DIR}" )
76 set( GSL_USE_PKGCONFIG ON )
77 endif()
78
79 #=============================================================================
80 # As a first try, use the PkgConfig module. This will work on many
81 # *NIX systems. See :module:`findpkgconfig`
82 # This will return ``GSL_INCLUDEDIR`` and ``GSL_LIBDIR`` used below.
83 if( GSL_USE_PKGCONFIG )
84 find_package(PkgConfig QUIET)
85 pkg_check_modules( GSL QUIET gsl )
86
87 if( EXISTS "${GSL_INCLUDEDIR}" )
88 get_filename_component( GSL_ROOT_DIR "${GSL_INCLUDEDIR}" DIRECTORY CACHE)
89 endif()
90 endif()
91
92 #=============================================================================
93 # Set GSL_INCLUDE_DIRS and GSL_LIBRARIES. If we skipped the PkgConfig step, try
94 # to find the libraries at $GSL_ROOT_DIR (if provided) or in standard system
95 # locations. These find_library and find_path calls will prefer custom
96 # locations over standard locations (HINTS). If the requested file is not found
97 # at the HINTS location, standard system locations will be still be searched
98 # (/usr/lib64 (Redhat), lib/i386-linux-gnu (Debian)).
99
100 find_path( GSL_INCLUDE_DIR
101 NAMES gsl/gsl_sf.h
102 HINTS ${GSL_ROOT_DIR}/include ${GSL_INCLUDEDIR}
103 )
104 find_library( GSL_LIBRARY
105 NAMES gsl
106 HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
107 PATH_SUFFIXES Release Debug
108 )
109 find_library( GSL_CBLAS_LIBRARY
110 NAMES gslcblas cblas
111 HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
112 PATH_SUFFIXES Release Debug
113 )
114 # Do we also have debug versions?
115 find_library( GSL_LIBRARY_DEBUG
116 NAMES gsld gsl
117 HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
118 PATH_SUFFIXES Debug
119 )
120 find_library( GSL_CBLAS_LIBRARY_DEBUG
121 NAMES gslcblasd cblasd gslcblas cblas
122 HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
123 PATH_SUFFIXES Debug
124 )
125 set( GSL_INCLUDE_DIRS ${GSL_INCLUDE_DIR} )
126 set( GSL_LIBRARIES ${GSL_LIBRARY} ${GSL_CBLAS_LIBRARY} )
127
128 # If we didn't use PkgConfig, try to find the version via gsl-config or by
129 # reading gsl_version.h.
130 if( NOT GSL_VERSION )
131 # 1. If gsl-config exists, query for the version.
132 find_program( GSL_CONFIG_EXECUTABLE
133 NAMES gsl-config
134 HINTS "${GSL_ROOT_DIR}/bin"
135 )
136 if( EXISTS "${GSL_CONFIG_EXECUTABLE}" )
137 execute_process(
138 COMMAND "${GSL_CONFIG_EXECUTABLE}" --version
139 OUTPUT_VARIABLE GSL_VERSION
140 OUTPUT_STRIP_TRAILING_WHITESPACE )
141 endif()
142
143 # 2. If gsl-config is not available, try looking in gsl/gsl_version.h
144 if( NOT GSL_VERSION AND EXISTS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" )
145 file( STRINGS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" gsl_version_h_contents REGEX "define GSL_VERSION" )
146 string( REGEX REPLACE ".*define[ ]+GSL_VERSION[ ]+\"([^\"]*)\".*" "\\1" GSL_VERSION ${gsl_version_h_contents} )
147 endif()
148
149 # might also try scraping the directory name for a regex match "gsl-X.X"
150 endif()
151
152 #=============================================================================
153 # handle the QUIETLY and REQUIRED arguments and set GSL_FOUND to TRUE if all
154 # listed variables are TRUE
155 find_package_handle_standard_args( GSL
156 FOUND_VAR
157 GSL_FOUND
158 REQUIRED_VARS
159 GSL_INCLUDE_DIR
160 GSL_LIBRARY
161 GSL_CBLAS_LIBRARY
162 VERSION_VAR
163 GSL_VERSION
164 )
165
166 mark_as_advanced( GSL_ROOT_DIR GSL_VERSION GSL_LIBRARY GSL_INCLUDE_DIR
167 GSL_CBLAS_LIBRARY GSL_LIBRARY_DEBUG GSL_CBLAS_LIBRARY_DEBUG
168 GSL_USE_PKGCONFIG GSL_CONFIG )
169
170 #=============================================================================
171 # Register imported libraries:
172 # 1. If we can find a Windows .dll file (or if we can find both Debug and
173 # Release libraries), we will set appropriate target properties for these.
174 # 2. However, for most systems, we will only register the import location and
175 # include directory.
176
177 # Look for dlls, or Release and Debug libraries.
178 # XXX: CMake's logic here is wrong, it assumes Windows == MSVC *and*
179 # that the library will always be built dynamically
180 # if(WIN32)
181 # string( REPLACE ".lib" ".dll" GSL_LIBRARY_DLL "${GSL_LIBRARY}" )
182 # string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" )
183 # string( REPLACE ".lib" ".dll" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" )
184 # string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" )
185 # endif()
186 if(WIN32)
187 if (MSVC)
188 string( REGEX REPLACE ".lib$" ".dll" GSL_LIBRARY_DLL "${GSL_LIBRARY}" )
189 string( REGEX REPLACE ".lib$" ".dll" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" )
190 string( REGEX REPLACE ".lib$" ".dll" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" )
191 string( REGEX REPLACE ".lib$" ".dll" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" )
192 elseif ("${GSL_LIBRARY}" MATCHES "\.dll\.a$")
193 string( REGEX REPLACE ".a$" "" GSL_LIBRARY_DLL "${GSL_LIBRARY}" )
194 string( REGEX REPLACE ".a$" "" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" )
195 string( REGEX REPLACE ".a$" "" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" )
196 string( REGEX REPLACE ".a$" "" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" )
197 else()
198 string( REGEX REPLACE ".a$" ".dll" GSL_LIBRARY_DLL "${GSL_LIBRARY}" )
199 string( REGEX REPLACE ".a$" ".dll" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" )
200 string( REGEX REPLACE ".a$" ".dll" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" )
201 string( REGEX REPLACE ".a$" ".dll" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" )
202 endif()
203 endif()
204
205 if( GSL_FOUND AND NOT TARGET GSL::gsl )
206 if( EXISTS "${GSL_LIBRARY_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DLL}")
207
208 # Windows systems with dll libraries.
209 add_library( GSL::gsl SHARED IMPORTED )
210 add_library( GSL::gslcblas SHARED IMPORTED )
211
212 # Windows with dlls, but only Release libraries.
213 set_target_properties( GSL::gslcblas PROPERTIES
214 IMPORTED_LOCATION_RELEASE "${GSL_CBLAS_LIBRARY_DLL}"
215 IMPORTED_IMPLIB "${GSL_CBLAS_LIBRARY}"
216 INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
217 IMPORTED_CONFIGURATIONS Release
218 IMPORTED_LINK_INTERFACE_LANGUAGES "C" )
219 set_target_properties( GSL::gsl PROPERTIES
220 IMPORTED_LOCATION_RELEASE "${GSL_LIBRARY_DLL}"
221 IMPORTED_IMPLIB "${GSL_LIBRARY}"
222 INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
223 IMPORTED_CONFIGURATIONS Release
224 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
225 INTERFACE_LINK_LIBRARIES GSL::gslcblas )
226
227 # If we have both Debug and Release libraries
228 if( EXISTS "${GSL_LIBRARY_DEBUG_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DEBUG_DLL}")
229 set_property( TARGET GSL::gslcblas APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug )
230 set_target_properties( GSL::gslcblas PROPERTIES
231 IMPORTED_LOCATION_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG_DLL}"
232 IMPORTED_IMPLIB_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG}" )
233 set_property( TARGET GSL::gsl APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug )
234 set_target_properties( GSL::gsl PROPERTIES
235 IMPORTED_LOCATION_DEBUG "${GSL_LIBRARY_DEBUG_DLL}"
236 IMPORTED_IMPLIB_DEBUG "${GSL_LIBRARY_DEBUG}" )
237 endif()
238
239 else()
240
241 # For all other environments (ones without dll libraries), create
242 # the imported library targets.
243 add_library( GSL::gsl UNKNOWN IMPORTED )
244 add_library( GSL::gslcblas UNKNOWN IMPORTED )
245 set_target_properties( GSL::gslcblas PROPERTIES
246 IMPORTED_LOCATION "${GSL_CBLAS_LIBRARY}"
247 INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
248 IMPORTED_LINK_INTERFACE_LANGUAGES "C" )
249 set_target_properties( GSL::gsl PROPERTIES
250 IMPORTED_LOCATION "${GSL_LIBRARY}"
251 INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
252 IMPORTED_LINK_INTERFACE_LANGUAGES "C"
253 INTERFACE_LINK_LIBRARIES GSL::gslcblas )
254 endif()
255 endif()