2
0

xmake.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package("rustlib")
  2. set_kind("template")
  3. set_description("Template to help with Rust libraries in xrepo")
  4. on_load(function (package)
  5. local toolchainconfigs = {}
  6. toolchainconfigs.target_plat = package:plat()
  7. toolchainconfigs.target_arch = package:arch()
  8. if package:is_plat("cross") then
  9. -- detect cross configuration from the compiler, if possible
  10. local compiler, toolname = package:tool("cc")
  11. if toolname == "clang" or toolname == "gcc" then
  12. local outdata, errdata = os.iorunv(compiler, {"-v"})
  13. local output = #outdata:trim() > 0 and outdata or errdata
  14. local target = output:match("Target: ([^\r\n]*)")
  15. if target then
  16. if toolname == "gcc" then
  17. target = target:replace("-none-", "-unknown-", {plain = true})
  18. end
  19. package:data_set("cross_target", target)
  20. local parts = target:split("-", {plain = true})
  21. if #parts >= 3 then
  22. toolchainconfigs.target_arch = parts[1]
  23. toolchainconfigs.target_system = table.concat(parts, "-", 2, #parts - 1)
  24. toolchainconfigs.target_abi = parts[#parts]
  25. end
  26. end
  27. end
  28. end
  29. package:add("deps", "rust", {configs = toolchainconfigs})
  30. end)
  31. on_check("mingw|i386", function (package)
  32. -- MinGW 32bits exception model must match rustc LLVM exception model (dwarf2)
  33. local mingw = package:toolchain("mingw")
  34. if not mingw then
  35. return
  36. end
  37. local compiler, toolname = mingw:tool("cc")
  38. if toolname ~= "gcc" then
  39. return
  40. end
  41. local output, errdata = os.iorunv(compiler, {"-v"})
  42. -- for some reason the output is in stderr
  43. if #output:trim() == 0 then
  44. output = errdata
  45. end
  46. assert(output:find("--with-dwarf2", 1, true), "rustc is only compatible with dwarf2 exception model in 32bits mode, please use dwarf2 MinGW")
  47. end)
  48. on_install(function (package)
  49. -- pass rust toolchain configuration
  50. local rust = package:dep("rust")
  51. local rcfile_path = os.tmpfile() .. ".lua"
  52. local rcfile = io.open(rcfile_path, 'w')
  53. rcfile:print("add_requires(\"rust\", %s)", string.serialize(rust:requireinfo(), {strip = true, indent = false}))
  54. local cross_target = package:data("cross_target")
  55. if cross_target then
  56. rcfile:print("add_requireconfs(\"cargo::naga\", {arch = \"%s\", override = true})", cross_target)
  57. rcfile:print("add_rcflags(\"--target=%s\")", cross_target)
  58. end
  59. rcfile:close()
  60. local envs = import("package.tools.xmake").buildenvs(package)
  61. table.insert(envs.XMAKE_RCFILES, rcfile_path)
  62. package:data_set("xmake_envs", envs)
  63. end)