From 1b28e3b28b86de40ceb6d402b6f41f30eb0c8b59 Mon Sep 17 00:00:00 2001 From: Kelly Thompson Date: Wed, 17 Dec 2014 10:10:23 -0500 Subject: [PATCH] FindGSL: Add module to find the GNU Scientific Library --- Help/manual/cmake-modules.7.rst | 1 + Help/module/FindGSL.rst | 1 + Help/release/dev/add-FindGSL.rst | 5 + Modules/FindGSL.cmake | 238 +++++++++++++++++++++++++++++++ Tests/CMakeLists.txt | 4 + Tests/FindGSL/CMakeLists.txt | 9 ++ Tests/FindGSL/rng/CMakeLists.txt | 14 ++ Tests/FindGSL/rng/main.cc | 24 ++++ 8 files changed, 296 insertions(+) create mode 100644 Help/module/FindGSL.rst create mode 100644 Help/release/dev/add-FindGSL.rst create mode 100644 Modules/FindGSL.cmake create mode 100644 Tests/FindGSL/CMakeLists.txt create mode 100644 Tests/FindGSL/rng/CMakeLists.txt create mode 100644 Tests/FindGSL/rng/main.cc diff --git a/Help/manual/cmake-modules.7.rst b/Help/manual/cmake-modules.7.rst index 83371188c..51964856e 100644 --- a/Help/manual/cmake-modules.7.rst +++ b/Help/manual/cmake-modules.7.rst @@ -108,6 +108,7 @@ All Modules /module/FindGLUT /module/FindGnuplot /module/FindGnuTLS + /module/FindGSL /module/FindGTest /module/FindGTK2 /module/FindGTK diff --git a/Help/module/FindGSL.rst b/Help/module/FindGSL.rst new file mode 100644 index 000000000..baf221368 --- /dev/null +++ b/Help/module/FindGSL.rst @@ -0,0 +1 @@ +.. cmake-module:: ../../Modules/FindGSL.cmake diff --git a/Help/release/dev/add-FindGSL.rst b/Help/release/dev/add-FindGSL.rst new file mode 100644 index 000000000..47a0a25ce --- /dev/null +++ b/Help/release/dev/add-FindGSL.rst @@ -0,0 +1,5 @@ +add-FindGSL +----------- + +* A :module:`FindGSL` module was introduced to find the + GNU Scientific Library. diff --git a/Modules/FindGSL.cmake b/Modules/FindGSL.cmake new file mode 100644 index 000000000..ef125c061 --- /dev/null +++ b/Modules/FindGSL.cmake @@ -0,0 +1,238 @@ +#.rst: +# FindGSL +# -------- +# +# Find the native GSL includes and libraries. +# +# The GNU Scientific Library (GSL) is a numerical library for C and C++ +# programmers. It is free software under the GNU General Public +# License. +# +# Imported Targets +# ^^^^^^^^^^^^^^^^ +# +# If GSL is found, this module defines the following :prop_tgt:`IMPORTED` +# targets:: +# +# GSL::gsl - The main GSL library. +# GSL::gslcblas - The CBLAS support library used by GSL. +# +# Result Variables +# ^^^^^^^^^^^^^^^^ +# +# This module will set the following variables in your project:: +# +# GSL_FOUND - True if GSL found on the local system +# GSL_INCLUDE_DIRS - Location of GSL header files. +# GSL_LIBRARIES - The GSL libraries. +# GSL_VERSION - The version of the discovered GSL install. +# +# Hints +# ^^^^^ +# +# Set ``GSL_ROOT_DIR`` to a directory that contains a GSL installation. +# +# This script expects to find libraries at ``$GSL_ROOT_DIR/lib`` and the GSL +# headers at ``$GSL_ROOT_DIR/include/gsl``. The library directory may +# optionally provide Release and Debug folders. For Unix-like systems, this +# script will use ``$GSL_ROOT_DIR/bin/gsl-config`` (if found) to aid in the +# discovery GSL. +# +# Cache Variables +# ^^^^^^^^^^^^^^^ +# +# This module may set the following variables depending on platform and type +# of GSL installation discovered. These variables may optionally be set to +# help this module find the correct files:: +# +# GSL_CLBAS_LIBRARY - Location of the GSL CBLAS library. +# GSL_CBLAS_LIBRARY_DEBUG - Location of the debug GSL CBLAS library (if any). +# GSL_CONFIG_EXECUTABLE - Location of the ``gsl-config`` script (if any). +# GSL_LIBRARY - Location of the GSL library. +# GSL_LIBRARY_DEBUG - Location of the debug GSL library (if any). +# + +#============================================================================= +# Copyright 2014 Kelly Thompson +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# Include these modules to handle the QUIETLY and REQUIRED arguments. +include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) + +#============================================================================= +# If the user has provided ``GSL_ROOT_DIR``, use it! Choose items found +# at this location over system locations. +if( EXISTS "$ENV{GSL_ROOT_DIR}" ) + file( TO_CMAKE_PATH "$ENV{GSL_ROOT_DIR}" GSL_ROOT_DIR ) + set( GSL_ROOT_DIR "${GSL_ROOT_DIR}" CACHE PATH "Prefix for GSL installation." ) +endif() +if( NOT EXISTS "${GSL_ROOT_DIR}" ) + set( GSL_USE_PKGCONFIG ON ) +endif() + +#============================================================================= +# As a first try, use the PkgConfig module. This will work on many +# *NIX systems. See :module:`findpkgconfig` +# This will return ``GSL_INCLUDEDIR`` and ``GSL_LIBDIR`` used below. +if( GSL_USE_PKGCONFIG ) + find_package(PkgConfig) + pkg_check_modules( GSL QUIET gsl ) + + if( EXISTS "${GSL_INCLUDEDIR}" ) + get_filename_component( GSL_ROOT_DIR "${GSL_INCLUDEDIR}" DIRECTORY CACHE) + endif() +endif() + +#============================================================================= +# Set GSL_INCLUDE_DIRS and GSL_LIBRARIES. If we skipped the PkgConfig step, try +# to find the libraries at $GSL_ROOT_DIR (if provided) or in standard system +# locations. These find_library and find_path calls will prefer custom +# locations over standard locations (HINTS). If the requested file is not found +# at the HINTS location, standard system locations will be still be searched +# (/usr/lib64 (Redhat), lib/i386-linux-gnu (Debian)). + +find_path( GSL_INCLUDE_DIR + NAMES gsl/gsl_sf.h + HINTS ${GSL_ROOT_DIR}/include ${GSL_INCLUDEDIR} +) +find_library( GSL_LIBRARY + NAMES gsl + HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR} + PATH_SUFFIXES Release Debug +) +find_library( GSL_CBLAS_LIBRARY + NAMES gslcblas cblas + HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR} + PATH_SUFFIXES Release Debug +) +# Do we also have debug versions? +find_library( GSL_LIBRARY_DEBUG + NAMES gsl + HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR} + PATH_SUFFIXES Debug +) +find_library( GSL_CBLAS_LIBRARY_DEBUG + NAMES gslcblas cblas + HINTS ${GSL_ROOT_DIR}/lib ${GSL_LIBDIR} + PATH_SUFFIXES Debug +) +set( GSL_INCLUDE_DIRS ${GSL_INCLUDE_DIR} ) +set( GSL_LIBRARIES ${GSL_LIBRARY} ${GSL_CBLAS_LIBRARY} ) + +# If we didn't use PkgConfig, try to find the version via gsl-config or by +# reading gsl_version.h. +if( NOT GSL_VERSION ) + # 1. If gsl-config exists, query for the version. + find_program( GSL_CONFIG_EXECUTABLE + NAMES gsl-config + HINTS "${GSL_ROOT_DIR}/bin" + ) + if( EXISTS "${GSL_CONFIG_EXECUTABLE}" ) + execute_process( + COMMAND "${GSL_CONFIG_EXECUTABLE}" --version + OUTPUT_VARIABLE GSL_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE ) + endif() + + # 2. If gsl-config is not available, try looking in gsl/gsl_version.h + if( NOT GSL_VERSION AND EXISTS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" ) + file( STRINGS "${GSL_INCLUDE_DIRS}/gsl/gsl_version.h" gsl_version_h_contents REGEX "define GSL_VERSION" ) + string( REGEX REPLACE ".*([0-9].[0-9][0-9]).*" "\\1" GSL_VERSION ${gsl_version_h_contents} ) + endif() + + # might also try scraping the directory name for a regex match "gsl-X.X" +endif() + +#============================================================================= +# handle the QUIETLY and REQUIRED arguments and set GSL_FOUND to TRUE if all +# listed variables are TRUE +find_package_handle_standard_args( GSL + FOUND_VAR + GSL_FOUND + REQUIRED_VARS + GSL_INCLUDE_DIR + GSL_LIBRARY + GSL_CBLAS_LIBRARY + VERSION_VAR + GSL_VERSION + ) + +mark_as_advanced( GSL_ROOT_DIR GSL_VERSION GSL_LIBRARY GSL_INCLUDE_DIR + GSL_CBLAS_LIBRARY GSL_LIBRARY_DEBUG GSL_CBLAS_LIBRARY_DEBUG + GSL_USE_PKGCONFIG GSL_CONFIG ) + +#============================================================================= +# Register imported libraries: +# 1. If we can find a Windows .dll file (or if we can find both Debug and +# Release libraries), we will set appropriate target properties for these. +# 2. However, for most systems, we will only register the import location and +# include directory. + +# Look for dlls, or Release and Debug libraries. +if(WIN32) + string( REPLACE ".lib" ".dll" GSL_LIBRARY_DLL "${GSL_LIBRARY}" ) + string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DLL "${GSL_CBLAS_LIBRARY}" ) + string( REPLACE ".lib" ".dll" GSL_LIBRARY_DEBUG_DLL "${GSL_LIBRARY_DEBUG}" ) + string( REPLACE ".lib" ".dll" GSL_CBLAS_LIBRARY_DEBUG_DLL "${GSL_CBLAS_LIBRARY_DEBUG}" ) +endif() + +if( GSL_FOUND AND NOT TARGET GSL::gsl ) + if( EXISTS "${GSL_LIBRARY_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DLL}") + + # Windows systems with dll libraries. + add_library( GSL::gsl SHARED IMPORTED ) + add_library( GSL::gslcblas SHARED IMPORTED ) + + # Windows with dlls, but only Release libraries. + set_target_properties( GSL::gslcblas PROPERTIES + IMPORTED_LOCATION_RELEASE "${GSL_CBLAS_LIBRARY_DLL}" + IMPORTED_IMPLIB "${GSL_CBLAS_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}" + IMPORTED_CONFIGURATIONS Release + IMPORTED_LINK_INTERFACE_LANGUAGES "C" ) + set_target_properties( GSL::gsl PROPERTIES + IMPORTED_LOCATION_RELEASE "${GSL_LIBRARY_DLL}" + IMPORTED_IMPLIB "${GSL_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}" + IMPORTED_CONFIGURATIONS Release + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + INTERFACE_LINK_LIBRARIES GSL::gslcblas ) + + # If we have both Debug and Release libraries + if( EXISTS "${GSL_LIBRARY_DEBUG_DLL}" AND EXISTS "${GSL_CBLAS_LIBRARY_DEBUG_DLL}") + set_property( TARGET GSL::gslcblas APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug ) + set_target_properties( GSL::gslcblas PROPERTIES + IMPORTED_LOCATION_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG_DLL}" + IMPORTED_IMPLIB_DEBUG "${GSL_CBLAS_LIBRARY_DEBUG}" ) + set_property( TARGET GSL::gsl APPEND PROPERTY IMPORTED_CONFIGURATIONS Debug ) + set_target_properties( GSL::gsl PROPERTIES + IMPORTED_LOCATION_DEBUG "${GSL_LIBRARY_DEBUG_DLL}" + IMPORTED_IMPLIB_DEBUG "${GSL_LIBRARY_DEBUG}" ) + endif() + + else() + + # For all other environments (ones without dll libraries), create + # the imported library targets. + add_library( GSL::gsl UNKNOWN IMPORTED ) + add_library( GSL::gslcblas UNKNOWN IMPORTED ) + set_target_properties( GSL::gslcblas PROPERTIES + IMPORTED_LOCATION "${GSL_CBLAS_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" ) + set_target_properties( GSL::gsl PROPERTIES + IMPORTED_LOCATION "${GSL_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${GSL_INCLUDE_DIRS}" + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + INTERFACE_LINK_LIBRARIES GSL::gslcblas ) + endif() +endif() diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 33c18ce2a..123e5fd5a 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1247,6 +1247,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release endif() endif() + if(CMake_TEST_FindGSL) + add_subdirectory(FindGSL) + endif() + find_package(GTK2 QUIET) if(GTK2_FOUND) add_subdirectory(FindGTK2) diff --git a/Tests/FindGSL/CMakeLists.txt b/Tests/FindGSL/CMakeLists.txt new file mode 100644 index 000000000..45a347102 --- /dev/null +++ b/Tests/FindGSL/CMakeLists.txt @@ -0,0 +1,9 @@ +add_test(NAME FindGSL.rng COMMAND ${CMAKE_CTEST_COMMAND} + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindGSL/rng" + "${CMake_BINARY_DIR}/Tests/FindGSL/rng" + ${build_generator_args} + --build-project FindGSL_rng + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V + ) diff --git a/Tests/FindGSL/rng/CMakeLists.txt b/Tests/FindGSL/rng/CMakeLists.txt new file mode 100644 index 000000000..b15d6acab --- /dev/null +++ b/Tests/FindGSL/rng/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.0) +project(FindGSL_rng CXX) +include(CTest) + +find_package(GSL REQUIRED) + +add_executable(tstgslrng_tgt main.cc) +target_link_libraries(tstgslrng_tgt GSL::gsl) +add_test(NAME tstgslrng_tgt COMMAND tstgslrng_tgt) + +add_executable(tstgslrng_var main.cc) +target_link_libraries(tstgslrng_var ${GSL_LIBRARIES}) +target_include_directories(tstgslrng_var PRIVATE ${GSL_INCLUDE_DIRS}) +add_test(NAME tstgslrng_var COMMAND tstgslrng_var) diff --git a/Tests/FindGSL/rng/main.cc b/Tests/FindGSL/rng/main.cc new file mode 100644 index 000000000..72543be77 --- /dev/null +++ b/Tests/FindGSL/rng/main.cc @@ -0,0 +1,24 @@ +#include +#include "gsl/gsl_rng.h" + +int main() +{ + // return code + int retval = 1; + + // create a generator + gsl_rng *generator; + generator = gsl_rng_alloc(gsl_rng_mt19937); + + // Read a value. + double const Result = gsl_rng_uniform(generator); + + // Check value + double const expectedResult( 0.999741748906672 ); + if( fabs( expectedResult - Result ) < 1.0e-6 ) + retval = 0; + + // free allocated memory + gsl_rng_free(generator); + return retval; +}