test.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. -- imports
  2. import("core.base.option")
  3. import("core.platform.platform")
  4. import("packages", {alias = "get_packages"})
  5. -- the options
  6. local options =
  7. {
  8. {'v', "verbose", "k", nil, "Enable verbose information." }
  9. , {'D', "diagnosis", "k", nil, "Enable diagnosis information." }
  10. , {nil, "shallow", "k", nil, "Only install the root packages." }
  11. , {'p', "plat", "kv", nil, "Set the given platform." }
  12. , {'a', "arch", "kv", nil, "Set the given architecture." }
  13. , {nil, "cflags", "kv", nil, "Set the cflags." }
  14. , {nil, "cxxflags", "kv", nil, "Set the cxxflags." }
  15. , {nil, "ldflags", "kv", nil, "Set the ldflags." }
  16. , {nil, "ndk", "kv", nil, "Set the android NDK directory." }
  17. , {nil, "mingw", "kv", nil, "Set the MingW directory." }
  18. , {nil, "packages", "vs", nil, "The package list." }
  19. }
  20. -- require packages
  21. function _require_packages(argv, packages)
  22. local config_argv = {"f", "-c"}
  23. if argv.verbose then
  24. table.insert(config_argv, "-v")
  25. end
  26. if argv.diagnosis then
  27. table.insert(config_argv, "-D")
  28. end
  29. if argv.plat then
  30. table.insert(config_argv, "--plat=" .. argv.plat)
  31. end
  32. if argv.arch then
  33. table.insert(config_argv, "--arch=" .. argv.arch)
  34. end
  35. if argv.ndk then
  36. table.insert(config_argv, "--ndk=" .. argv.ndk)
  37. end
  38. if argv.mingw then
  39. table.insert(config_argv, "--mingw=" .. argv.mingw)
  40. end
  41. if argv.cflags then
  42. table.insert(config_argv, "--cflags=" .. argv.cflags)
  43. end
  44. if argv.cxxflags then
  45. table.insert(config_argv, "--cxxflags=" .. argv.cxxflags)
  46. end
  47. if argv.ldflags then
  48. table.insert(config_argv, "--ldflags=" .. argv.ldflags)
  49. end
  50. os.vexecv("xmake", config_argv)
  51. local require_argv = {"require", "-f", "-y"}
  52. if argv.verbose then
  53. table.insert(require_argv, "-v")
  54. end
  55. if argv.diagnosis then
  56. table.insert(require_argv, "-D")
  57. end
  58. if argv.shallow then
  59. table.insert(require_argv, "--shallow")
  60. end
  61. table.join2(require_argv, packages)
  62. os.vexecv("xmake", require_argv)
  63. end
  64. -- the given package is supported?
  65. function _package_is_supported(argv, packagename)
  66. local packages = get_packages()
  67. if packages then
  68. local plat = argv.plat or os.host()
  69. local packages_plat = packages[plat]
  70. for _, package in ipairs(packages_plat) do
  71. if package and packagename:split("%s+")[1] == package.name then
  72. local arch = argv.arch or platform.archs(plat)[1] or os.arch()
  73. for _, package_arch in ipairs(package.archs) do
  74. if arch == package_arch then
  75. return true
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82. -- the main entry
  83. function main(...)
  84. -- parse arguments
  85. local argv = option.parse({...}, options, "Test all the given or changed packages.")
  86. -- get packages
  87. local packages = argv.packages or {}
  88. if #packages == 0 then
  89. local files = os.iorun("git diff --name-only HEAD^")
  90. for _, file in ipairs(files:split('\n'), string.trim) do
  91. if file:find("packages", 1, true) and path.filename(file) == "xmake.lua" then
  92. local package = path.filename(path.directory(file))
  93. table.insert(packages, package)
  94. end
  95. end
  96. end
  97. if #packages == 0 then
  98. table.insert(packages, "tbox dev")
  99. end
  100. -- remove unsupported packages
  101. for idx, package in irpairs(packages) do
  102. if not _package_is_supported(argv, package) then
  103. table.remove(packages, idx)
  104. end
  105. end
  106. if #packages == 0 then
  107. print("no testable packages on %s!", argv.plat or os.host())
  108. return
  109. end
  110. -- prepare test project
  111. local repodir = os.curdir()
  112. local workdir = path.join(os.tmpdir(), "xmake-repo")
  113. print(packages)
  114. os.setenv("XMAKE_STATS", "false")
  115. os.tryrm(workdir)
  116. os.mkdir(workdir)
  117. os.cd(workdir)
  118. os.exec("xmake create test")
  119. os.cd("test")
  120. print(os.curdir())
  121. os.exec("xmake repo --add local-repo %s", repodir)
  122. os.exec("xmake repo -l")
  123. -- require packages
  124. _require_packages(argv, packages)
  125. end