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