mpfr.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. include(FetchContent)
  16. include(ProcessorCount)
  17. ProcessorCount(Ncpu)
  18. include(ExternalProject)
  19. set(prefix ${FETCHCONTENT_BASE_DIR}/mpfr)
  20. set(mpfr_INSTALL ${prefix}/install)
  21. set(mpfr_LIBRARY ${mpfr_INSTALL}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}mpfr${CMAKE_STATIC_LIBRARY_SUFFIX})
  22. set(mpfr_INCLUDE_DIR ${mpfr_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(mpfr_ExternalProject_Add_extra_options)
  27. else()
  28. # CMake >= 3.20
  29. set(mpfr_ExternalProject_Add_extra_options "CONFIGURE_HANDLED_BY_BUILD;ON")
  30. endif()
  31. ExternalProject_Add(mpfr
  32. PREFIX ${prefix}
  33. DEPENDS gmp
  34. URL https://ftp.gnu.org/gnu/mpfr/mpfr-4.1.0.tar.xz
  35. URL_MD5 bdd3d5efba9c17da8d83a35ec552baef
  36. UPDATE_DISCONNECTED true # need this to avoid constant rebuild
  37. ${mpfr_ExternalProject_Add_extra_options} # avoid constant reconfigure
  38. CONFIGURE_COMMAND
  39. ${prefix}/src/mpfr/configure
  40. --disable-debug --disable-dependency-tracking --disable-silent-rules --enable-cxx --with-pic
  41. --with-gmp-include=${gmp_INCLUDE_DIR} --with-gmp-lib=${gmp_LIB_DIR}
  42. --disable-shared
  43. --prefix=${mpfr_INSTALL}
  44. --disable-shared
  45. BUILD_COMMAND make -j${Ncpu}
  46. INSTALL_COMMAND make -j${Ncpu} install
  47. INSTALL_DIR ${mpfr_INSTALL}
  48. TEST_COMMAND ""
  49. BUILD_BYPRODUCTS ${mpfr_LIBRARY}
  50. )
  51. #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
  52. ExternalProject_Get_Property(mpfr SOURCE_DIR)
  53. set(mpfr_LIBRARIES ${mpfr_LIBRARY})
  54. add_library(mpfr::mpfr INTERFACE IMPORTED GLOBAL)
  55. file(MAKE_DIRECTORY ${mpfr_INCLUDE_DIR}) # avoid race condition
  56. target_include_directories(mpfr::mpfr INTERFACE ${mpfr_INCLUDE_DIR})
  57. target_link_libraries(mpfr::mpfr INTERFACE "${mpfr_LIBRARIES}") # need the quotes to expand list
  58. # This is necessary to ensure that mpfr appears before gmp in link order.
  59. # Otherwise undefined reference errors occur at link time on Linux with gcc
  60. target_link_libraries(mpfr::mpfr INTERFACE "${gmp_LIBRARIES}")
  61. add_dependencies(mpfr::mpfr mpfr)
  62. endif()
  63. if(NOT TARGET mpfr::mpfr)
  64. message(FATAL_ERROR "Creation of target 'mpfr::mpfr' failed")
  65. endif()