gmp.cmake 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. if(TARGET gmp::gmp)
  2. return()
  3. endif()
  4. # Download precompiled .dll on Windows
  5. if(WIN32)
  6. include(gmp_mpfr)
  7. # Find_package will look for our downloaded lib on Windows, and system-wide on Linux/macOS
  8. find_package(GMP REQUIRED)
  9. else()
  10. message(STATUS "Third-party: creating target 'gmp::gmp'")
  11. include(FetchContent)
  12. include(ProcessorCount)
  13. ProcessorCount(Ncpu)
  14. include(ExternalProject)
  15. set(prefix ${FETCHCONTENT_BASE_DIR}/gmp)
  16. set(gmp_INSTALL ${prefix}/install)
  17. set(gmp_LIB_DIR ${gmp_INSTALL}/lib)
  18. set(gmp_LIBRARY
  19. ${gmp_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gmp${CMAKE_STATIC_LIBRARY_SUFFIX}
  20. ${gmp_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gmpxx${CMAKE_STATIC_LIBRARY_SUFFIX}
  21. )
  22. set(gmp_INCLUDE_DIR ${gmp_INSTALL}/include)
  23. # Try to use CONFIGURE_HANDLED_BY_BUILD ON to avoid constantly reconfiguring
  24. if(${CMAKE_VERSION} VERSION_LESS 3.20)
  25. # CMake < 3.20, do not use any extra option
  26. set(gmp_ExternalProject_Add_extra_options)
  27. else()
  28. # CMake >= 3.20
  29. set(gmp_ExternalProject_Add_extra_options "CONFIGURE_HANDLED_BY_BUILD;ON")
  30. endif()
  31. ExternalProject_Add(gmp
  32. PREFIX ${prefix}
  33. URL https://gmplib.org/download/gmp/gmp-6.2.1.tar.xz
  34. URL_MD5 0b82665c4a92fd2ade7440c13fcaa42b
  35. UPDATE_DISCONNECTED true # need this to avoid constant rebuild
  36. PATCH_COMMAND
  37. curl "https://gmplib.org/repo/gmp/raw-rev/5f32dbc41afc" "|" git apply -v
  38. ${gmp_ExternalProject_Add_extra_options}
  39. CONFIGURE_COMMAND
  40. ${prefix}/src/gmp/configure
  41. --disable-debug --disable-dependency-tracking --enable-cxx --with-pic
  42. --prefix=${gmp_INSTALL}
  43. --disable-shared
  44. BUILD_COMMAND make -j${Ncpu}
  45. INSTALL_COMMAND make -j${Ncpu} install
  46. INSTALL_DIR ${gmp_INSTALL}
  47. TEST_COMMAND ""
  48. BUILD_BYPRODUCTS ${gmp_LIBRARY}
  49. )
  50. ExternalProject_Get_Property(gmp SOURCE_DIR)
  51. set(gmp_LIBRARIES ${gmp_LIBRARY})
  52. add_library(gmp::gmp INTERFACE IMPORTED GLOBAL)
  53. file(MAKE_DIRECTORY ${gmp_INCLUDE_DIR}) # avoid race condition
  54. target_include_directories(gmp::gmp INTERFACE ${gmp_INCLUDE_DIR})
  55. target_link_libraries(gmp::gmp INTERFACE "${gmp_LIBRARIES}") # need the quotes to expand list
  56. add_dependencies(gmp::gmp gmp)
  57. endif()
  58. if(NOT TARGET gmp::gmp)
  59. message(FATAL_ERROR "Creation of target 'gmp::gmp' failed")
  60. endif()