FindGSL.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  45. #=============================================================================
  46. # If the user has provided ``GSL_ROOT_DIR``, use it! Choose items found
  47. # at this location over system locations.
  48. if( EXISTS "$ENV{GSL_ROOT_DIR}" )
  49. file( TO_CMAKE_PATH "$ENV{GSL_ROOT_DIR}" GSL_ROOT_DIR )
  50. set( GSL_ROOT_DIR "${GSL_ROOT_DIR}" CACHE PATH "Prefix for GSL installation." )
  51. endif()
  52. if( NOT EXISTS "${GSL_ROOT_DIR}" )
  53. set( GSL_USE_PKGCONFIG ON )
  54. endif()
  55. #=============================================================================
  56. # As a first try, use the PkgConfig module. This will work on many
  57. # *NIX systems. See :module:`findpkgconfig`
  58. # This will return ``GSL_INCLUDEDIR`` and ``GSL_LIBDIR`` used below.
  59. if( GSL_USE_PKGCONFIG )
  60. find_package(PkgConfig QUIET)
  61. pkg_check_modules( GSL QUIET gsl )
  62. if( EXISTS "${GSL_INCLUDEDIR}" )
  63. get_filename_component( GSL_ROOT_DIR "${GSL_INCLUDEDIR}" DIRECTORY CACHE)
  64. endif()
  65. endif()
  66. #=============================================================================
  67. # Set GSL_INCLUDE_DIRS and GSL_LIBRARIES. If we skipped the PkgConfig step, try
  68. # to find the libraries at $GSL_ROOT_DIR (if provided) or in standard system
  69. # locations. These find_library and find_path calls will prefer custom
  70. # locations over standard locations (HINTS). If the requested file is not found
  71. # at the HINTS location, standard system locations will be still be searched
  72. # (/usr/lib64 (Redhat), lib/i386-linux-gnu (Debian)).
  73. find_path( GSL_INCLUDE_DIR
  74. NAMES gsl/gsl_sf.h
  75. HINTS ${GSL_ROOT_DIR}/include ${GSL_INCLUDEDIR}
  76. )
  77. find_library( GSL_LIBRARY
  78. NAMES gsl
  79. HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
  80. PATH_SUFFIXES Release Debug
  81. )
  82. find_library( GSL_CBLAS_LIBRARY
  83. NAMES gslcblas cblas
  84. HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
  85. PATH_SUFFIXES Release Debug
  86. )
  87. # Do we also have debug versions?
  88. find_library( GSL_LIBRARY_DEBUG
  89. NAMES gsld gsl
  90. HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
  91. PATH_SUFFIXES Debug
  92. )
  93. find_library( GSL_CBLAS_LIBRARY_DEBUG
  94. NAMES gslcblasd cblasd gslcblas cblas
  95. HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR}
  96. PATH_SUFFIXES Debug
  97. )
  98. set( GSL_INCLUDE_DIRS ${GSL_INCLUDE_DIR} )
  99. set( GSL_LIBRARIES ${GSL_LIBRARY} ${GSL_CBLAS_LIBRARY} )
  100. # If we didn't use PkgConfig, try to find the version via gsl-config or by
  101. # reading gsl_version.h.
  102. if( NOT GSL_VERSION )
  103. # 1. If gsl-config exists, query for the version.
  104. find_program( GSL_CONFIG_EXECUTABLE
  105. NAMES gsl-config
  106. HINTS "${GSL_ROOT_DIR}/bin"
  107. )
  108. if( EXISTS "${GSL_CONFIG_EXECUTABLE}" )
  109. execute_process(
  110. COMMAND "${GSL_CONFIG_EXECUTABLE}" --version
  111. OUTPUT_VARIABLE GSL_VERSION
  112. OUTPUT_STRIP_TRAILING_WHITESPACE )
  113. endif()
  114. # 2. If gsl-config is not available, try looking in gsl/gsl_version.h
  115. if( NOT GSL_VERSION AND EXISTS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" )
  116. file( STRINGS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" gsl_version_h_contents REGEX "define GSL_VERSION" )
  117. string( REGEX REPLACE ".*define[ ]+GSL_VERSION[ ]+\"([^\"]*)\".*" "\\1" GSL_VERSION ${gsl_version_h_contents} )
  118. endif()
  119. # might also try scraping the directory name for a regex match "gsl-X.X"
  120. endif()
  121. #=============================================================================
  122. # handle the QUIETLY and REQUIRED arguments and set GSL_FOUND to TRUE if all
  123. # listed variables are TRUE
  124. find_package_handle_standard_args( GSL
  125. FOUND_VAR
  126. GSL_FOUND
  127. REQUIRED_VARS
  128. GSL_INCLUDE_DIR
  129. GSL_LIBRARY
  130. GSL_CBLAS_LIBRARY
  131. VERSION_VAR
  132. GSL_VERSION
  133. )
  134. mark_as_advanced( GSL_ROOT_DIR GSL_VERSION GSL_LIBRARY GSL_INCLUDE_DIR
  135. GSL_CBLAS_LIBRARY GSL_LIBRARY_DEBUG GSL_CBLAS_LIBRARY_DEBUG
  136. GSL_USE_PKGCONFIG GSL_CONFIG )
  137. #=============================================================================
  138. # Register imported libraries:
  139. # 1. If we can find a Windows .dll file (or if we can find both Debug and
  140. # Release libraries), we will set appropriate target properties for these.
  141. # 2. However, for most systems, we will only register the import location and
  142. # include directory.
  143. # Look for dlls, or Release and Debug libraries.
  144. if(WIN32)
  145. string( REPLACE ".lib" ".dll" GSL_LIBRARY_DLL "${GSL_LIBRARY}" )
  146. string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" )
  147. string( REPLACE ".lib" ".dll" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" )
  148. string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" )
  149. endif()
  150. if( GSL_FOUND AND NOT TARGET GSL::gsl )
  151. if( EXISTS "${GSL_LIBRARY_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DLL}")
  152. # Windows systems with dll libraries.
  153. add_library( GSL::gsl SHARED IMPORTED )
  154. add_library( GSL::gslcblas SHARED IMPORTED )
  155. # Windows with dlls, but only Release libraries.
  156. set_target_properties( GSL::gslcblas PROPERTIES
  157. IMPORTED_LOCATION_RELEASE "${GSL_CBLAS_LIBRARY_DLL}"
  158. IMPORTED_IMPLIB "${GSL_CBLAS_LIBRARY}"
  159. INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
  160. IMPORTED_CONFIGURATIONS Release
  161. IMPORTED_LINK_INTERFACE_LANGUAGES "C" )
  162. set_target_properties( GSL::gsl PROPERTIES
  163. IMPORTED_LOCATION_RELEASE "${GSL_LIBRARY_DLL}"
  164. IMPORTED_IMPLIB "${GSL_LIBRARY}"
  165. INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
  166. IMPORTED_CONFIGURATIONS Release
  167. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  168. INTERFACE_LINK_LIBRARIES GSL::gslcblas )
  169. # If we have both Debug and Release libraries
  170. if( EXISTS "${GSL_LIBRARY_DEBUG_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DEBUG_DLL}")
  171. set_property( TARGET GSL::gslcblas APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug )
  172. set_target_properties( GSL::gslcblas PROPERTIES
  173. IMPORTED_LOCATION_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG_DLL}"
  174. IMPORTED_IMPLIB_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG}" )
  175. set_property( TARGET GSL::gsl APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug )
  176. set_target_properties( GSL::gsl PROPERTIES
  177. IMPORTED_LOCATION_DEBUG "${GSL_LIBRARY_DEBUG_DLL}"
  178. IMPORTED_IMPLIB_DEBUG "${GSL_LIBRARY_DEBUG}" )
  179. endif()
  180. else()
  181. # For all other environments (ones without dll libraries), create
  182. # the imported library targets.
  183. add_library( GSL::gsl UNKNOWN IMPORTED )
  184. add_library( GSL::gslcblas UNKNOWN IMPORTED )
  185. set_target_properties( GSL::gslcblas PROPERTIES
  186. IMPORTED_LOCATION "${GSL_CBLAS_LIBRARY}"
  187. INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
  188. IMPORTED_LINK_INTERFACE_LANGUAGES "C" )
  189. set_target_properties( GSL::gsl PROPERTIES
  190. IMPORTED_LOCATION "${GSL_LIBRARY}"
  191. INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}"
  192. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  193. INTERFACE_LINK_LIBRARIES GSL::gslcblas )
  194. endif()
  195. endif()