xmake.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package("gmp")
  2. set_homepage("https://gmplib.org/")
  3. set_description("GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.")
  4. set_license("LGPL-3.0")
  5. add_urls("https://ftpmirror.gnu.org/gmp/gmp-$(version).tar.xz")
  6. add_urls("https://ftp.gnu.org/gnu/gmp/gmp-$(version).tar.xz")
  7. add_urls("https://gmplib.org/download/gmp/gmp-$(version).tar.xz")
  8. add_versions("6.3.0", "a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898")
  9. add_patches("6.3.0", "patches/6.3.0/c23.patch", "24eb6ad75fb2552db247d3c5c522d30f221cca23a0fdc925b2684af44d51b7b3")
  10. add_configs("cpp_api", {description = "Enable C++ support", default = false, type = "boolean"})
  11. if is_plat("windows") and is_arch("arm64") then
  12. add_configs("assembly", {description = "Enable the use of assembly loops", default = false, type = "boolean"})
  13. else
  14. add_configs("assembly", {description = "Enable the use of assembly loops", default = true, type = "boolean"})
  15. end
  16. add_configs("fat", {description = "Build fat libraries on systems that support it", default = false, type = "boolean"})
  17. if is_plat("mingw") and is_subhost("msys") then
  18. add_extsources("pacman::gmp")
  19. elseif is_plat("linux") then
  20. add_extsources("pacman::gmp", "apt::libgmp-dev")
  21. elseif is_plat("macosx") then
  22. add_extsources("brew::gmp")
  23. end
  24. if not is_subhost("windows") then
  25. add_deps("m4")
  26. end
  27. add_links("gmpxx", "gmp")
  28. if on_check then
  29. on_check(function (package)
  30. if package:is_plat("windows") then
  31. if package:has_tool("cxx", "clang_cl") then
  32. raise("package(gmp) unsupported clang-cl toolchain now, you can use clang toolchain\nadd_requires(\"gmp\", {configs = {toolchains = \"clang\"}}))")
  33. end
  34. end
  35. end)
  36. end
  37. on_fetch(function (package, opt)
  38. if opt.system then
  39. return package:find_package("system::gmp", {includes = "gmp.h"})
  40. end
  41. end)
  42. on_load(function (package)
  43. if is_subhost("windows") and os.arch() == "x64" then
  44. local msystem = "MINGW" .. (package:is_arch64() and "64" or "32")
  45. package:add("deps", "msys2", {configs = {msystem = msystem, base_devel = true}})
  46. end
  47. if package:is_plat("windows") then
  48. -- msvc toolchain require other tool to build asm
  49. -- x86, x64 -> yasm, arm64 -> clang
  50. if package:is_arch("x64", "x86") then
  51. package:add("deps", "yasm")
  52. end
  53. package:add("defines", "__GMP_WITHIN_CONFIGURE")
  54. if package:is_arch("x64", "arm64") then
  55. package:add("defines", "_LONG_LONG_LIMB") -- mp_limb_t type
  56. end
  57. -- if package:config("shared") then
  58. -- package:add("defines", "__GMP_LIBGMP_DLL")
  59. -- end
  60. end
  61. end)
  62. on_install("!wasm and (!android or android@!windows)", function (package)
  63. import("package.tools.autoconf")
  64. import("lib.detect.find_tool")
  65. -- ref https://github.com/microsoft/vcpkg/blob/4ed84798137bcf664989fa432d41d278d7ad3b25/ports/gmp/subdirs.patch
  66. io.replace("Makefile.am",
  67. "SUBDIRS = tests mpn mpz mpq mpf printf scanf rand cxx demos tune doc",
  68. "SUBDIRS = mpn mpz mpq mpf printf scanf rand cxx tune", {plain = true})
  69. if is_host("windows") then
  70. io.replace("configure", "LIBTOOL='$(SHELL) $(top_builddir)/libtool'", "LIBTOOL='\"$(SHELL)\" $(top_builddir)/libtool'", {plain = true})
  71. end
  72. if package:is_plat("windows") then
  73. local obj_file_suffix = package:has_tool("cxx", "cl") and ".obj" or ".o"
  74. io.replace("configure", "$CCAS $CFLAGS $CPPFLAGS", "$CCAS $CCASFLAGS -o conftest" .. obj_file_suffix, {plain = true})
  75. -- Remove error flags for asm build
  76. io.replace("mpn/Makefile.in", "$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(ASMFLAGS)", "$(AM_CFLAGS) ${CCASFLAGS}", {plain = true})
  77. if package:has_tool("ld", "link") then
  78. -- `lib /out: xxx` -> `lib /out:xxx`
  79. io.replace("configure", "$AR $AR_FLAGS ", "$AR $AR_FLAGS", {plain = true})
  80. end
  81. if package:config("shared") then
  82. -- error: duplicate symbol: __gmpn_add
  83. io.replace("gmp-h.in", "#define __GMP_EXTERN_INLINE __inline", "#define __GMP_EXTERN_INLINE static __inline", {plain = true})
  84. -- export symbol macro
  85. io.replace("gmp-h.in", "#define __GMP_LIBGMP_DLL @LIBGMP_DLL@", "#define __GMP_LIBGMP_DLL 1", {plain = true})
  86. end
  87. end
  88. local configs = {}
  89. table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))
  90. table.insert(configs, "--enable-static=" .. (package:config("shared") and "no" or "yes"))
  91. table.insert(configs, "--enable-cxx=" .. (package:config("cpp_api") and "yes" or "no"))
  92. table.insert(configs, "--enable-assembly=" .. (package:config("assembly") and "yes" or "no"))
  93. table.insert(configs, "--enable-fat=" .. (package:config("fat") and "yes" or "no"))
  94. if package:is_debug() then
  95. table.insert(configs, "--enable-debug")
  96. end
  97. if not package:is_plat("windows") and package:config("pic") ~= false then
  98. table.insert(configs, "--with-pic")
  99. end
  100. local opt = {}
  101. if package:is_plat("macosx") and package:is_arch("arm64") and os.arch() == "x86_64" then
  102. table.insert(configs, "--build=x86_64-apple-darwin")
  103. table.insert(configs, "--host=arm64-apple-darwin")
  104. opt.envs = autoconf.buildenvs(package, {cflags = "--target=arm64-apple-darwin"})
  105. opt.envs.CC = package:build_getenv("cc") .. " -arch arm64" -- for linker flags
  106. elseif package:is_plat("windows") then
  107. local msvc = package:toolchain("msvc") or package:toolchain("clang") or package:toolchain("clang-cl")
  108. assert(msvc:check(), "msvs not found!")
  109. -- buildenvs maybe missing deps bin dir
  110. opt.envs = os.joinenvs(os.joinenvs(msvc:runenvs()), autoconf.buildenvs(package))
  111. if package:has_tool("cxx", "cl") then
  112. opt.envs.CC = "cl -nologo"
  113. opt.envs.CXX = "cl -nologo"
  114. opt.envs.AR = "lib -nologo"
  115. opt.envs.LD = "link -nologo"
  116. opt.envs.NM = "dumpbin -nologo -symbols"
  117. opt.envs.AR_FLAGS = "-out:" -- override `cq` flag
  118. table.insert(configs, "gmp_cv_asm_w32=.word") -- fix detect
  119. elseif package:has_tool("cxx", "clang") then
  120. local suffix = opt.envs.CC:split("-")
  121. if #suffix > 1 then
  122. suffix = "-" .. suffix[#suffix]
  123. else
  124. suffix = ""
  125. end
  126. opt.envs.NM = "llvm-nm" .. suffix
  127. opt.envs.LDFLAGS = opt.envs.LDFLAGS:gsub("-nostdlib", "")
  128. if package:is_cross() then
  129. -- require -fuse-ld=lld-link
  130. opt.envs.CFLAGS = opt.envs.CFLAGS .. " " .. opt.envs.LDFLAGS
  131. opt.envs.CXXFLAGS = opt.envs.CXXFLAGS .. " " .. opt.envs.LDFLAGS
  132. end
  133. end
  134. -- Maybe missing ucrt flags
  135. opt.envs.CFLAGS = opt.envs.CFLAGS .. " " .. opt.envs.CXXFLAGS
  136. -- Fix mp_limb_t
  137. -- msvc sizeof long == 4 unmatch gcc sizeof long == 8
  138. if package:is_arch("x64", "arm64") then
  139. opt.envs.CFLAGS = opt.envs.CFLAGS .. " -D_LONG_LONG_LIMB"
  140. opt.envs.CXXFLAGS = opt.envs.CXXFLAGS .. " -D_LONG_LONG_LIMB"
  141. end
  142. local clang_archs = {
  143. ["x86"] = "i686",
  144. ["x64"] = "x86_64",
  145. ["arm64"] = "aarch64",
  146. }
  147. local target = clang_archs[package:arch()] .. "-windows-msvc"
  148. if package:is_arch("x64", "x86") then
  149. local yasm_machine = {
  150. ["x86"] = "x86",
  151. ["x64"] = "amd64",
  152. }
  153. local yasm_format = {
  154. ["x86"] = "win32",
  155. ["x64"] = "win64",
  156. }
  157. opt.envs.CCAS = "yasm"
  158. opt.envs.CCASFLAGS = format("-a x86 -m %s -p gas -r raw -f %s -g null -X gnu", yasm_machine[package:arch()], yasm_format[package:arch()])
  159. elseif package:is_arch("arm64") then
  160. if package:has_tool("cxx", "clang") then
  161. opt.envs.CCAS = opt.envs.CC
  162. else
  163. local llvm_nm = assert(find_tool("llvm-nm"), "windows arm64 require llvm-nm to detect")
  164. local clang = assert(find_tool("clang"), "windows arm64 require clang to build asm")
  165. opt.envs.CCAS = path.unix(clang.program)
  166. opt.envs.NM = path.unix(llvm_nm.program)
  167. end
  168. opt.envs.CCASFLAGS = table.concat({"--target=" .. target, "-c"}, " ")
  169. end
  170. table.insert(configs, "--host=" .. target)
  171. end
  172. -- Can't generate correct gmp.lib with lib.exe
  173. if package:is_plat("windows") then
  174. autoconf.build(package, configs, opt)
  175. io.writefile("xmake.lua", [[
  176. option("cpp_api", {default = false})
  177. add_rules("mode.debug", "mode.release")
  178. target("gmp")
  179. set_kind("$(kind)")
  180. add_rules("c++")
  181. add_files("**.obj|gen-*.obj|cxx/*.obj", "**.o|gen-*.o|cxx/*.o")
  182. remove_files("dummy.obj") -- I don't know why, it only happen on ci
  183. add_headerfiles("gmp.h")
  184. if has_config("cpp_api") then
  185. target("gmpxx")
  186. set_kind("$(kind)")
  187. add_rules("c++")
  188. add_files("cxx/*.obj", "cxx/*.o")
  189. add_headerfiles("gmpxx.h")
  190. add_deps("gmp")
  191. end
  192. ]])
  193. import("package.tools.xmake").install(package, {cpp_api = package:config("cpp_api")})
  194. else
  195. autoconf.install(package, configs, opt)
  196. end
  197. end)
  198. on_test(function (package)
  199. assert(package:has_cfuncs("gmp_randinit", {includes = "gmp.h"}))
  200. end)