Skip to content

Commit

Permalink
Add GMP support via custom FindGMP module
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
inakleinbottle committed Nov 4, 2024
1 parent 078901c commit 3afe673
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 57 deletions.
65 changes: 8 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 "$<TARGET_FILE:PkgConfig::gmp>")
#set(Bignum_INCLUDE_DIR "$<TARGET_PROPERTY:PkgConfig::gmp,INTERFACE_INCLUDE_DIRECTORY>")


#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)
Expand Down
111 changes: 111 additions & 0 deletions cmake/Modules/FindGMP.cmake
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 3afe673

Please sign in to comment.