FindGSL.cmake 8.8 KB

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