FindGSL.cmake 8.8 KB

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