xmake.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package("rust")
  2. set_kind("toolchain")
  3. set_homepage("https://rust-lang.org")
  4. set_description("Rust is a general-purpose programming language emphasizing performance, type safety, and concurrency.")
  5. add_versions("1.86.0", "")
  6. add_deps("ca-certificates", {host = true, private = true})
  7. add_deps("rustup", {host = true, private = true, system = false})
  8. add_configs("target_plat", {description = "Target platform (for cross-compilation)", default = nil, type = "string"})
  9. add_configs("target_arch", {description = "Target arch (for cross-compilation)", default = nil, type = "string"})
  10. if is_plat("cross") then
  11. add_configs("target_system", {description = "Target system (for cross-compilation)", default = "unknown-linux", type = "string"})
  12. add_configs("target_abi", {description = "Target ABI (for cross-compilation)", default = "gnu", type = "string"})
  13. end
  14. on_load(function (package)
  15. if package:config("target_plat") == nil then
  16. package:config_set("target_plat", package:plat())
  17. end
  18. if package:config("target_arch") == nil then
  19. package:config_set("target_arch", package:arch())
  20. end
  21. end)
  22. on_install(function (package)
  23. import("core.tools.rustc.target_triple", {try = true}) -- introduced in xmake 3.0.0
  24. local plat = package:config("target_plat")
  25. local arch = package:config("target_arch")
  26. if not target_triple then
  27. if plat == "mingw" or plat == "msys" then
  28. plat = "windows"
  29. end
  30. local package_plat = package:plat()
  31. if package_plat == "mingw" or package_plat == "msys" then
  32. package_plat = "windows"
  33. end
  34. if plat ~= package_plat or arch ~= package:arch() then
  35. os.raise("rust cross-compilation requires xmake dev or xmake 3.0.0")
  36. end
  37. end
  38. local rustup = assert(os.getenv("RUSTUP_HOME"), "cannot find rustup home!")
  39. local version = package:version():shortstr()
  40. local toolchain_name = version
  41. local toolchain_dir
  42. if target_triple then
  43. local host_target = assert(target_triple(package:plat(), package:arch()), "failed to build target triple for plat/arch, if you think this is a bug please create an issue")
  44. toolchain_name = toolchain_name .. "-" .. host_target
  45. toolchain_dir = toolchain_name
  46. elseif package:is_plat("msys", "mingw") and is_host("windows") then
  47. -- we have to handle mingw case because rustup will install a msvc toolchain by default on Windows
  48. toolchain_name = toolchain_name .. (package:is_arch("i386") and "-x86_64-pc-windows-gnu" or "-i686-pc-windows-gnu")
  49. toolchain_dir = toolchain_name
  50. else
  51. toolchain_dir = toolchain_name .. "-*"
  52. end
  53. os.vrunv("rustup", {"install", "--no-self-update", toolchain_name})
  54. if target_triple or plat == "cross" then
  55. local target
  56. if plat == "cross" then
  57. local system = package:config("target_system")
  58. local abi = package:config("target_abi")
  59. target = arch .. "-" .. system .. "-" .. abi
  60. else
  61. target = assert(target_triple(plat, arch), "failed to build target triple for target_plat/target_arch, if you think this is a bug please create an issue")
  62. end
  63. if target ~= host_target then
  64. os.vrunv("rustup", {"target", "add", target})
  65. end
  66. end
  67. os.vmv(path.join(rustup, "toolchains", toolchain_dir, "*"), package:installdir())
  68. -- cleanup to prevent rustup to think the toolchain is still installed
  69. os.vrm(path.join(rustup, "toolchains", toolchain_dir))
  70. os.vrm(path.join(rustup, "update-hashes", toolchain_dir))
  71. package:addenv("RC", "bin/rustc" .. (is_host("windows") and ".exe" or ""))
  72. package:mark_as_pathenv("RC")
  73. end)
  74. on_test(function (package)
  75. os.vrun("cargo --version")
  76. os.vrun("rustc --version")
  77. end)