FindGSL.cmake 8.6 KB

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