From 3afe673fe36479a8004c4841d513f43d9d3c7354 Mon Sep 17 00:00:00 2001 From: Sam Morley Date: Mon, 4 Nov 2024 19:44:31 +0000 Subject: [PATCH] Add GMP support via custom FindGMP module Introduce a custom FindGMP.cmake module to locate and configure GMP library dependencies. This change streamlines GMP integration by handling both pkg-config and direct library searches, ensuring compatibility across different platforms and setups. The CMakeLists.txt is updated to use this new module for improved maintainability. --- CMakeLists.txt | 65 +++------------------ cmake/Modules/FindGMP.cmake | 111 ++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 57 deletions(-) create mode 100644 cmake/Modules/FindGMP.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0374f6f3c..1a724cb03 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,14 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/Modules) # and text editors. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +# Now we get to adding our components. Let's do some global setup such as +# setting the CXX standard and the shared library details. +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +# I don't think we have any pure C anywhere, but we set the standard anyway +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) # TODO: Handle these more delicately. @@ -242,71 +249,15 @@ find_package(OpenCL CONFIG REQUIRED) find_package(pybind11 CONFIG REQUIRED) find_package(cereal CONFIG REQUIRED) find_package(PCGRandom REQUIRED) +find_package(GMP REQUIRED) message(STATUS "Target architecture ${RPY_ARCH}") -#find_package(PkgConfig REQUIRED) -#pkg_check_modules(gmp REQUIRED IMPORTED_TARGET gmp) -#add_library(Bignum::Bignum ALIAS PkgConfig::gmp) -#set(Bignum_LIBRARY "$") -#set(Bignum_INCLUDE_DIR "$") - - -#if (APPLE AND NOT DEFINED MKL_OMP_LIB) -# get_brew_prefix(_libomp_prefix libomp VERBOSE) -# -# if (_libomp_prefix AND EXISTS ${_libomp_prefix}) -# list(APPEND CMAKE_PREFIX_PATH ${_libomp_prefix}) -# endif() -#endif() -# -#find_package(OpenMP REQUIRED COMPONENTS CXX) - -#if (MSVC) -# # MSVC has openmp from the stone ages, so add the experimental flag -# get_target_property(_omp_flags OpenMP::OpenMP_CXX INTERFACE_COMPILE_OPTIONS) -# string(REPLACE "/openmp" "/openmp:experimental" _omp_flags "${_omp_flags}") -# set_target_properties(OpenMP::OpenMP_CXX PROPERTIES -# INTERFACE_COMPILE_OPTIONS "${_omp_flags}") -#endif() - - -#find_package(Libalgebra_lite REQUIRED) - -find_package(PkgConfig REQUIRED) -pkg_check_modules(GMP QUIET IMPORTED_TARGET gmp) -if (TARGET PkgConfig::GMP) - add_library(GMP::GMP ALIAS PkgConfig::GMP) -elseif(DEFINED VCPKG_INSTALLED_DIR) - find_path(GMP_INCLUDES NAMES gmp.h - PATHS ${VCPKG_INSTALLED_DIR}) - - find_library(GMP_LIBRARIES gmp mpir PATHS ${VCPKG_INSTALLED_DIR}) - - message(STATUS "GMP HEADERS: ${GMP_INCLUDES}") - message(STATUS "GMP LIBRARIES: ${GMP_LIBRARIES}") - if(GMP_INCLUDES AND GMP_LIBRARIES) - add_library(GMP::GMP UNKNOWN IMPORTED GLOBAL) - target_include_directories(GMP::GMP INTERFACE ${GMP_INCLUDES}) - - set_target_properties(GMP::GMP PROPERTIES - IMPORTED_LOCATION "${GMP_LIBRARIES}" - ) - else() - message(FATAL_ERROR "Could not locate gmp") - endif() -else() - message(FATAL_ERROR "Could not locate gmp, no vcpkg") -endif() -# Now we get to adding our components. Let's do some global setup such as -# setting the CXX standard and the shared library details. -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) if (APPLE) set(CMAKE_MACOSX_RPATH ON) diff --git a/cmake/Modules/FindGMP.cmake b/cmake/Modules/FindGMP.cmake new file mode 100644 index 000000000..52e492cee --- /dev/null +++ b/cmake/Modules/FindGMP.cmake @@ -0,0 +1,111 @@ + +if (GMP_FOUND) + return() +endif() + +include(FindPackageHandleStandardArgs) +include(SelectLibraryConfigurations) + +find_package(PkgConfig QUIET) + +if (PkgConfig_FOUND) + pkg_check_modules(PC_GMP QUIET gmp) +endif () + +if (GMP_PKG_FOUND) + set(GMP_INCLUDE_DIR "${PC_GMP_INCLUDE_DIRS}") + set(GMP_LIBRARY_DIRS "${PC_GMP_LIBRARY_DIRS}") + set(GMP_LIBRARIES "${PC_GMP_LIBRARIES}") + set(GMP_COMPILE_OPTIONS ${PC_GMP_CFLAGS} ${PC_GMP_CFLAGS_OTHER}) + set(GMP_LINK_OPTIONS ${PC_GMP_LDFLAGS} ${PC_GMP_LDFLAGS_OTHER}) +else() + + set(_gmp_lib_names gmp libgmp mpir libmpir) + + find_library(GMP_LIBRARY_RELEASE + NAMES ${_gmp_lib_names} + HINTS "${VCPKG_INSTALLED_DIR}" + ) + + if (WIN32) + # VCPKG installs debug libraries in ${VCPKG_INSTALLED_DIR}/debug on Windows + find_library(GMP_LIBRARY_DEBUG + NAMES ${_gmp_lib_names} + HINTS "${VCPKG_INSTALLED_DIR}/debug" + ) + else() + foreach(_name IN ITEMS ${_gmp_lib_names}) + list(APPEND _gmp_lib_names_debug "${_name}d") + endforeach() + + # Almost surely, sensible platforms will have pkgconfig find the libraries + find_library(GMP_LIBRARY_DEBUG + NAMES ${_gmp_lib_names_debug} + HINTS "${VCPKG_INSTALLED_DIR}" + ) + endif() + + set(GMP_COMPILE_OPTIONS "") + set(GMP_LINK_OPTIONS "") + + find_path(GMP_INCLUDE_DIR NAMES gmp.h HINTS "${VCPKG_INSTALLED_DIR}") + + + select_library_configurations(GMP) + +endif () + + + + +find_package_handle_standard_args(GMP + FOUND_VAR GMP_FOUND + REQUIRED_VARS + GMP_LIBRARY GMP_INCLUDE_DIR) + +mark_as_advanced( + GMP_INCLUDE_DIR + GMP_LIBRARIES + GMP_LIBRARY_DIRS + GMP_COMPILE_OPTIONS + GMP_LINK_OPTIONS +) + + +if (GMP_FOUND AND NOT TARGET GMP::GMP) + add_library(GMP::GMP UNKNOWN IMPORTED GLOBAL) + set_target_properties(GMP::GMP PROPERTIES + IMPORTED_LOCATION "${GMP_LIBRARY}" + INTERFACE_INCLUDE_DIRECTORIES "${GMP_INCLUDE_DIR}" + IMPORTED_IMPLIB "${GMP_LIBRARY_DIRS}" + ) + if (GMP_COMPILE_OPTIONS) + set_target_properties(GMP::GMP PROPERTIES + INTERFACE_COMPILE_OPTIONS "${GMP_COMPILE_OPTIONS}" + ) + endif() + if(GMP_LINK_OPTIONS) + set_target_properties(GMP::GMP PROPERTIES + INTERFACE_LINK_OPTIONS "${GMP_LINK_OPTIONS}" + ) + endif() + + if (GMP_LIBRARY_RELEASE) + set_property(TARGET GMP::GMP APPEND PROPERTY + IMPORTED_CONFIGURATIONS RELEASE + ) + set_target_properties(GMP::GMP PROPERTIES + IMPORTED_LOCATION_RELEASE "${GMP_LIBRARY_RELEASE}" + ) + endif() + if (GMP_LIBRARY_RELEASE) + set_property(TARGET GMP::GMP APPEND PROPERTY + IMPORTED_CONFIGURATIONS DEBUG + ) + set_target_properties(GMP::GMP PROPERTIES + IMPORTED_LOCATION_DEBUG "${GMP_LIBRARY_DEBUG}" + ) + endif() + + +endif()