gmp.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # SERIOUSLY !?! CMAKE and configure use transposed definitions of "build" and
  12. # "host"?
  13. #
  14. # https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_NAME.html#variable:CMAKE_SYSTEM_NAME
  15. # https://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html
  16. #
  17. # Seems these aren't to be trusted much
  18. # https://gitlab.kitware.com/cmake/cmake/-/issues/20989
  19. if(APPLE)
  20. # https://gmplib.org/list-archives/gmp-discuss/2020-November/006607.html
  21. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
  22. set(gmp_BUILD "x86_64-apple-darwin")
  23. set(gmp_HOST "arm64-apple-darwin")
  24. set(gmp_CFLAGS "--target=arm64-apple-darwin")
  25. set(gmp_LDFLAGS "-arch arm64")
  26. message(STATUS "GMP Recipe notices building on ${gmp_BUILD} for ${gmp_HOST}")
  27. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
  28. set(gmp_HOST "x86_64-apple-darwin")
  29. set(gmp_BUILD "arm64-apple-darwin")
  30. set(gmp_CFLAGS "--target=x86_64-apple-darwin13.0.0")
  31. set(gmp_LDFLAGS "")
  32. message(STATUS "GMP Recipe notices building on ${gmp_HOST} for ${gmp_BUILD}")
  33. endif()
  34. endif()
  35. include(FetchContent)
  36. include(ProcessorCount)
  37. ProcessorCount(Ncpu)
  38. include(ExternalProject)
  39. set(prefix ${FETCHCONTENT_BASE_DIR}/gmp)
  40. set(gmp_INSTALL ${prefix}/install)
  41. set(gmp_LIB_DIR ${gmp_INSTALL}/lib)
  42. set(gmp_LIBRARY
  43. ${gmp_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gmp${CMAKE_STATIC_LIBRARY_SUFFIX}
  44. ${gmp_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gmpxx${CMAKE_STATIC_LIBRARY_SUFFIX}
  45. )
  46. set(gmp_INCLUDE_DIR ${gmp_INSTALL}/include)
  47. # Try to use CONFIGURE_HANDLED_BY_BUILD ON to avoid constantly reconfiguring
  48. if(${CMAKE_VERSION} VERSION_LESS 3.20)
  49. # CMake < 3.20, do not use any extra option
  50. set(gmp_ExternalProject_Add_extra_options)
  51. else()
  52. # CMake >= 3.20
  53. set(gmp_ExternalProject_Add_extra_options "CONFIGURE_HANDLED_BY_BUILD;ON")
  54. endif()
  55. ExternalProject_Add(gmp
  56. PREFIX ${prefix}
  57. URL https://github.com/alisw/GMP/archive/refs/tags/v6.2.1.tar.gz
  58. URL_MD5 f060ad4e762ae550d16f1bb477aadba5
  59. UPDATE_DISCONNECTED true # need this to avoid constant rebuild
  60. PATCH_COMMAND
  61. curl "https://gist.githubusercontent.com/alecjacobson/d34d9307c17d1b853571699b9786e9d1/raw/8d14fc21cb7654f51c2e8df4deb0f82f9d0e8355/gmp-patch" "|" git apply -v
  62. ${gmp_ExternalProject_Add_extra_options}
  63. CONFIGURE_COMMAND
  64. ${CMAKE_COMMAND} -E env
  65. CFLAGS=${gmp_CFLAGS}
  66. LDFLAGS=${gmp_LDFLAGS}
  67. ${prefix}/src/gmp/configure
  68. --disable-debug --disable-dependency-tracking --enable-cxx --with-pic
  69. --prefix=${gmp_INSTALL}
  70. --build=${gmp_BUILD}
  71. --host=${gmp_HOST}
  72. --disable-shared
  73. BUILD_COMMAND make -j${Ncpu}
  74. INSTALL_COMMAND make -j${Ncpu} install
  75. INSTALL_DIR ${gmp_INSTALL}
  76. TEST_COMMAND ""
  77. BUILD_BYPRODUCTS ${gmp_LIBRARY}
  78. )
  79. ExternalProject_Get_Property(gmp SOURCE_DIR)
  80. set(gmp_LIBRARIES ${gmp_LIBRARY})
  81. add_library(gmp::gmp INTERFACE IMPORTED GLOBAL)
  82. file(MAKE_DIRECTORY ${gmp_INCLUDE_DIR}) # avoid race condition
  83. target_include_directories(gmp::gmp INTERFACE ${gmp_INCLUDE_DIR})
  84. target_link_libraries(gmp::gmp INTERFACE "${gmp_LIBRARIES}") # need the quotes to expand list
  85. add_dependencies(gmp::gmp gmp)
  86. endif()
  87. if(NOT TARGET gmp::gmp)
  88. message(FATAL_ERROR "Creation of target 'gmp::gmp' failed")
  89. endif()