Skip to content
Snippets Groups Projects
Commit a8e4ff18 authored by Simon Praetorius's avatar Simon Praetorius
Browse files

Rewrite FindGMP to provide imported targets

Link GMPxx library to C library GMP
parent 30cbe040
No related branches found
No related tags found
1 merge request!818Rewrite FindGMP to provide imported targets
# .. cmake_module::
#
# Find the GNU MULTI-Precision Bignum (GMP) library
# and the corresponding C++ bindings GMPxx
#
# You may set the following variables to modify the
# behaviour of this module:
#
# :ref:`GMP_ROOT`
# Path list to search for GMP and GMPxx
#
# Sets the following variables:
#
# :code:`GMP_FOUND`
# True if the GMP library, the GMPxx headers and
# the GMPxx library were found.
#
# .. cmake_variable:: GMP_ROOT
#
# You may set this variable to have :ref:`FindGMP` look
# for the gmp and gmpxx packages in the given path before
# inspecting system paths.
#
# search for location of header gmpxx.h", only at positions given by the user
find_path(GMPXX_INCLUDE_DIR
NAMES "gmpxx.h"
PATHS ${GMP_PREFIX} ${GMP_ROOT}
PATH_SUFFIXES include
NO_DEFAULT_PATH)
# try default paths now
find_path(GMPXX_INCLUDE_DIR
NAMES "gmpxx.h")
# check if header is accepted
include(CMakePushCheckState)
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${GMPXX_INCLUDE_DIR})
include(CheckIncludeFileCXX)
check_include_file_cxx("gmpxx.h" GMP_HEADER_WORKS)
# look for library gmp, only at positions given by the user
find_library(GMP_LIB gmp
PATHS ${GMP_PREFIX} ${GMP_ROOT}
PATH_SUFFIXES lib lib64
NO_DEFAULT_PATH
DOC "GNU GMP library")
# try default paths now
find_library(GMP_LIB gmp)
# look for library gmpxx, only at positions given by the user
find_library(GMPXX_LIB gmpxx
PATHS ${GMP_PREFIX} ${GMP_ROOT}
PATH_SUFFIXES lib lib64
NO_DEFAULT_PATH
DOC "GNU GMPXX library")
# try default paths now
find_library(GMPXX_LIB gmpxx)
# check if library works
if(GMP_LIB AND GMPXX_LIB)
include(CheckSymbolExists)
check_library_exists(${GMP_LIB} __gmpz_abs "" GMPXX_LIB_WORKS)
endif(GMP_LIB AND GMPXX_LIB)
cmake_pop_check_state()
# behave like a CMake module is supposed to behave
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
"GMP"
DEFAULT_MSG
GMPXX_INCLUDE_DIR GMP_LIB GMPXX_LIB GMP_HEADER_WORKS GMPXX_LIB_WORKS
#[=======================================================================[.rst:
FindGMP
-------
Find the GNU MULTI-Precision Bignum (GMP) library
and the corresponding C++ bindings GMPxx.
This module searches for both libraries and only considers the package
found if both can be located. It then defines separate targets for the C
and the C++ library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``GMP::gmp``
Library target of the C library.
``GMP::gmpxx``
Library target of the C++ library, which also links to the C library.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``GMP_FOUND``
True if the GMP library, the GMPxx headers and
the GMPxx library were found.
Cache Variables
^^^^^^^^^^^^^^^
You may set the following variables to modify the behaviour of
this module:
``GMP_INCLUDE_DIR``
The directory containing ``gmp.h``.
``GMP_LIB``
The path to the gmp library.
``GMPXX_INCLUDE_DIR``
The directory containing ``gmpxx.h``.
``GMPXX_LIB``
The path to the gmpxx library.
#]=======================================================================]
# Add a feature summary for this package
include(FeatureSummary)
set_package_properties(GMP PROPERTIES
DESCRIPTION "GNU multi-precision library"
URL "https://gmplib.org"
)
mark_as_advanced(GMP_LIB GMPXX_LIB GMPXX_INCLUDE_DIR)
# Try finding the package with pkg-config
find_package(PkgConfig QUIET)
pkg_check_modules(PKG QUIET gmp gmpxx)
# Try to locate the libraries and their headers, using pkg-config hints
find_path(GMP_INCLUDE_DIR gmp.h HINTS ${PKG_gmp_INCLUDEDIR})
find_library(GMP_LIB gmp HINTS ${PKG_gmp_LIBDIR})
find_path(GMPXX_INCLUDE_DIR gmpxx.h HINTS ${PKG_gmpxx_INCLUDEDIR})
find_library(GMPXX_LIB gmpxx HINTS ${PKG_gmpxx_LIBDIR})
# text for feature summary
set_package_properties("GMP" PROPERTIES
DESCRIPTION "GNU multi-precision library including the C++ bindings GMPxx"
PURPOSE "Multi-precision quadrature rules, basis function evaluation etc.")
# Remove these variables from cache inspector
mark_as_advanced(GMP_INCLUDE_DIR GMP_LIB GMPXX_INCLUDE_DIR GMPXX_LIB)
# if GMPxx headers, GMP library, and GMPxx library are found, store results
# Report if package was found
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GMP
DEFAULT_MSG
GMPXX_LIB GMPXX_INCLUDE_DIR GMP_INCLUDE_DIR GMP_LIB
)
# Set targets
if(GMP_FOUND)
set(GMP_INCLUDE_DIRS ${GMPXX_INCLUDE_DIR})
set(GMP_LIBRARIES ${GMP_LIB} ${GMPXX_LIB})
set(GMP_COMPILE_FLAGS "-DENABLE_GMP=1")
# log result
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
"Determining location of GMP, GMPxx succeeded:\n"
"Include directory: ${GMP_INCLUDE_DIRS}\n"
"Library directory: ${GMP_LIBRARIES}\n\n")
else()
# log errornous result
file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
"Determining location of GMP, GMPxx failed:\n"
"Include directory: ${GMPXX_INCLUDE_DIR}\n"
"gmp library directory: ${GMP_LIB}\n"
"gmpxx library directory: ${GMPXX_LIB}\n\n")
endif()
# C library
if(NOT TARGET GMP::gmp)
add_library(GMP::gmp UNKNOWN IMPORTED)
set_target_properties(GMP::gmp PROPERTIES
IMPORTED_LOCATION ${GMP_LIB}
INTERFACE_INCLUDE_DIRECTORIES ${GMP_INCLUDE_DIR}
)
endif()
# set HAVE_GMP for config.h
set(HAVE_GMP ${GMP_FOUND})
# C++ library, which requires a link to the C library
if(NOT TARGET GMP::gmpxx)
add_library(GMP::gmpxx UNKNOWN IMPORTED)
set_target_properties(GMP::gmpxx PROPERTIES
IMPORTED_LOCATION ${GMPXX_LIB}
INTERFACE_INCLUDE_DIRECTORIES ${GMPXX_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES GMP::gmp
)
endif()
endif()
# register all GMP related flags
if(HAVE_GMP)
set(HAVE_GMP ${GMP_FOUND})
if(GMP_FOUND)
dune_register_package_flags(COMPILE_DEFINITIONS "ENABLE_GMP=1"
LIBRARIES "${GMP_LIB};${GMPXX_LIB}"
INCLUDE_DIRS "${GMPXX_INCLUDE_DIR}")
LIBRARIES "GMP::gmp;GMP::gmpxx")
endif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment