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

[!819] Rewrite FindQuadMath to provide imported targets

Merge branch 'feature/find_quadmath' into 'master'

ref:core/dune-common\>

### Summary

This MR adds imported-targets to the FindQuadMath.cmake module, following
modern cmake guidelines.

### Details

Instead of exporting include directories and libraries as variables, the find
modules exports an imported target that contains all the necessary information
to include and link QuadMath. The imported target is named:
QuadMath::QuadMath.

### Acknowledgements

This find-module is developed together with @lukas.riedel

See merge request [!819]

  [!819]: gitlab.dune-project.org/core/dune-common/merge_requests/819
parents 9cb5e9e7 a4fada60
No related branches found
No related tags found
1 merge request!819Rewrite FindQuadMath to provide imported targets
Pipeline #27423 passed
......@@ -10,19 +10,23 @@
# A list of targets to use QuadMath with.
#
# set HAVE_QUADMATH for config.h
set(HAVE_QUADMATH ${QuadMath_FOUND})
# register the QuadMath imported target
if(QuadMath_FOUND)
dune_register_package_flags(
LIBRARIES QuadMath::QuadMath
COMPILE_DEFINITIONS "ENABLE_QUADMATH=1"
)
endif()
# add function to link against QuadMath::QuadMath
function(add_dune_quadmath_flags _targets)
if(QUADMATH_FOUND)
if(QuadMath_FOUND)
foreach(_target ${_targets})
target_link_libraries(${_target} "quadmath")
set_property(TARGET ${_target}
APPEND_STRING
PROPERTY COMPILE_FLAGS "-DENABLE_QUADMATH=1 -D_GLIBCXX_USE_FLOAT128=1 ")
if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
set_property(TARGET ${_target}
APPEND_STRING
PROPERTY COMPILE_FLAGS "-fext-numeric-literals ")
endif()
target_link_libraries(${_target} QuadMath::QuadMath)
target_compile_definitions(${_target} "ENABLE_QUADMATH=1")
endforeach(_target ${_targets})
endif(QUADMATH_FOUND)
endif()
endfunction(add_dune_quadmath_flags)
# .. cmake_module::
#
# Find the GCC Quad-Precision library
#
# Sets the following variables:
#
# :code:`QUADMATH_FOUND`
# True if the Quad-Precision library was found.
#
#
# search for the header quadmath.h
include(CheckIncludeFile)
check_include_file(quadmath.h QUADMATH_HEADER)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
#[=======================================================================[.rst:
FindQuadMath
------------
Find the GCC Quad-Precision library
This module checks if the used compiler has built-in support for QuadMath
by compiling a small source file.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``QuadMath::QuadMath``
Library to link against if QuadMath should be used.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``QuadMath_FOUND``
True if the Quad-Precision library was found.
#]=======================================================================]
cmake_push_check_state() # Save variables
# Add a feature summary for this package
include(FeatureSummary)
set_package_properties(QuadMath PROPERTIES
DESCRIPTION "GCC Quad-Precision Math Library"
URL "https://gcc.gnu.org/onlinedocs/libquadmath"
)
# Check if QuadMath support is built into the compiler
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES quadmath)
check_c_source_compiles("
if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
set(CMAKE_REQUIRED_FLAGS "-fext-numeric-literals")
endif()
check_cxx_source_compiles("
#include <quadmath.h>
int main ()
......@@ -27,33 +48,31 @@ int main ()
__float128 r = 1.0q;
r = strtoflt128(\"1.2345678\", NULL);
return 0;
}" QUADMATH_COMPILES)
cmake_pop_check_state()
}" QuadMath_COMPILES)
cmake_pop_check_state() # Reset CMAKE_REQUIRED_XXX variables
if(QuadMath_COMPILES)
# Use additional variable for better report message
set(QuadMath_VAR "(Supported by compiler)")
endif()
# Report that package was found
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
"QuadMath"
find_package_handle_standard_args(QuadMath
DEFAULT_MSG
QUADMATH_HEADER
QUADMATH_COMPILES
QuadMath_VAR QuadMath_COMPILES
)
# text for feature summary
set_package_properties("QuadMath" PROPERTIES
DESCRIPTION "GCC Quad-Precision library")
# set HAVE_QUADMATH for config.h
set(HAVE_QUADMATH ${QUADMATH_FOUND})
# -fext-numeric-literals is a GCC extension not available in other compilers like clang
if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
set(_QUADMATH_EXT_NUMERIC_LITERALS "-fext-numeric-literals")
endif()
# add imported target for quadmath
if(QuadMath_FOUND AND NOT TARGET QuadMath::QuadMath)
# Compiler supports QuadMath: Add appropriate linker flag
add_library(QuadMath::QuadMath INTERFACE IMPORTED)
target_link_libraries(QuadMath::QuadMath INTERFACE quadmath)
# register all QuadMath related flags
if(HAVE_QUADMATH)
dune_register_package_flags(COMPILE_DEFINITIONS "ENABLE_QUADMATH=1" "_GLIBCXX_USE_FLOAT128=1"
COMPILE_OPTIONS ${_QUADMATH_EXT_NUMERIC_LITERALS}
LIBRARIES "quadmath")
target_compile_definitions(QuadMath::QuadMath INTERFACE
_GLIBCXX_USE_FLOAT128
)
target_compile_options(QuadMath::QuadMath INTERFACE
$<$<CXX_COMPILER_ID:GNU>:-fext-numeric-literals>
)
endif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment