FindGSL.cmake 9.0 KB

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