mpfr.cmake 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Expects
  2. # gmp_INCLUDE_DIR
  3. # gmp_LIB_DIR
  4. # gmp_LIBRARIES
  5. if(TARGET mpfr::mpfr)
  6. return()
  7. endif()
  8. # Download precompiled .dll on Windows
  9. if(WIN32)
  10. include(gmp_mpfr)
  11. # Find_package will look for our downloaded lib on Windows, and system-wide on Linux/macOS
  12. find_package(MPFR REQUIRED)
  13. else()
  14. message(STATUS "Third-party: creating target 'mpfr::mpfr'")
  15. # Praying this will work the same as gmp
  16. if(APPLE)
  17. # https://gmplib.org/list-archives/gmp-discuss/2020-November/006607.html
  18. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
  19. set(mpfr_BUILD "x86_64-apple-darwin")
  20. set(mpfr_HOST "arm64-apple-darwin")
  21. set(mpfr_CFLAGS "--target=arm64-apple-darwin")
  22. set(mpfr_LDFLAGS "-arch arm64")
  23. elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
  24. set(mpfr_HOST "x86_64-apple-darwin")
  25. set(mpfr_BUILD "arm64-apple-darwin")
  26. set(mpfr_CFLAGS "--target=x86_64-apple-darwin13.0.0")
  27. set(mpfr_LDFLAGS "")
  28. endif()
  29. endif()
  30. include(FetchContent)
  31. include(ProcessorCount)
  32. ProcessorCount(Ncpu)
  33. include(ExternalProject)
  34. set(prefix ${FETCHCONTENT_BASE_DIR}/mpfr)
  35. set(mpfr_INSTALL ${prefix}/install)
  36. set(mpfr_LIBRARY ${mpfr_INSTALL}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mpfr${CMAKE_STATIC_LIBRARY_SUFFIX})
  37. set(mpfr_INCLUDE_DIR ${mpfr_INSTALL}/include)
  38. # Try to use CONFIGURE_HANDLED_BY_BUILD ON to avoid constantly reconfiguring
  39. if(${CMAKE_VERSION} VERSION_LESS 3.20)
  40. # CMake < 3.20, do not use any extra option
  41. set(mpfr_ExternalProject_Add_extra_options)
  42. else()
  43. # CMake >= 3.20
  44. set(mpfr_ExternalProject_Add_extra_options "CONFIGURE_HANDLED_BY_BUILD;ON")
  45. endif()
  46. ExternalProject_Add(mpfr
  47. PREFIX ${prefix}
  48. DEPENDS gmp
  49. URL https://ftp.gnu.org/gnu/mpfr/mpfr-4.2.0.tar.xz
  50. URL_MD5 a25091f337f25830c16d2054d74b5af7
  51. UPDATE_DISCONNECTED true # need this to avoid constant rebuild
  52. ${mpfr_ExternalProject_Add_extra_options} # avoid constant reconfigure
  53. CONFIGURE_COMMAND
  54. ${CMAKE_COMMAND} -E env
  55. CFLAGS=${gmp_CFLAGS}
  56. LDFLAGS=${gmp_LDFLAGS}
  57. ${prefix}/src/mpfr/configure
  58. --disable-debug --disable-dependency-tracking --disable-silent-rules --enable-cxx --with-pic
  59. --with-gmp-include=${gmp_INCLUDE_DIR} --with-gmp-lib=${gmp_LIB_DIR}
  60. --disable-shared
  61. --prefix=${mpfr_INSTALL}
  62. --build=${gmp_BUILD}
  63. --host=${gmp_HOST}
  64. --disable-shared
  65. BUILD_COMMAND make -j${Ncpu}
  66. INSTALL_COMMAND make -j${Ncpu} install
  67. INSTALL_DIR ${mpfr_INSTALL}
  68. TEST_COMMAND ""
  69. BUILD_BYPRODUCTS ${mpfr_LIBRARY}
  70. )
  71. #PATCH_COMMAND curl "https://raw.githubusercontent.com/Homebrew/formula-patches/03cf8088210822aa2c1ab544ed58ea04c897d9c4/libtool/configure-big_sur.diff" "|" sed -e "s/configure.orig/configure/g" "|" git apply -v
  72. ExternalProject_Get_Property(mpfr SOURCE_DIR)
  73. set(mpfr_LIBRARIES ${mpfr_LIBRARY})
  74. add_library(mpfr::mpfr INTERFACE IMPORTED GLOBAL)
  75. file(MAKE_DIRECTORY ${mpfr_INCLUDE_DIR}) # avoid race condition
  76. target_include_directories(mpfr::mpfr INTERFACE ${mpfr_INCLUDE_DIR})
  77. target_link_libraries(mpfr::mpfr INTERFACE "${mpfr_LIBRARIES}") # need the quotes to expand list
  78. # This is necessary to ensure that mpfr appears before gmp in link order.
  79. # Otherwise undefined reference errors occur at link time on Linux with gcc
  80. target_link_libraries(mpfr::mpfr INTERFACE "${gmp_LIBRARIES}")
  81. add_dependencies(mpfr::mpfr mpfr)
  82. endif()
  83. if(NOT TARGET mpfr::mpfr)
  84. message(FATAL_ERROR "Creation of target 'mpfr::mpfr' failed")
  85. endif()