test.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. , {'k', "kind", "kv", nil, "Enable static/shared library." }
  12. , {'p', "plat", "kv", nil, "Set the given platform." }
  13. , {'a', "arch", "kv", nil, "Set the given architecture." }
  14. , {'m', "mode", "kv", nil, "Set the given mode." }
  15. , {'j', "jobs", "kv", nil, "Set the build jobs." }
  16. , {'f', "configs", "kv", nil, "Set the configs." }
  17. , {nil, "linkjobs", "kv", nil, "Set the link jobs." }
  18. , {nil, "cflags", "kv", nil, "Set the cflags." }
  19. , {nil, "cxxflags", "kv", nil, "Set the cxxflags." }
  20. , {nil, "ldflags", "kv", nil, "Set the ldflags." }
  21. , {nil, "ndk", "kv", nil, "Set the Android NDK directory." }
  22. , {nil, "ndk_sdkver", "kv", nil, "Set the Android NDK platform sdk version." }
  23. , {nil, "sdk", "kv", nil, "Set the SDK directory of cross toolchain." }
  24. , {nil, "vs", "kv", nil, "Set the VS Compiler version." }
  25. , {nil, "vs_sdkver", "kv", nil, "Set the Windows SDK version." }
  26. , {nil, "vs_toolset", "kv", nil, "Set the Windows Toolset version." }
  27. , {nil, "vs_runtime", "kv", nil, "Set the VS Runtime library." }
  28. , {nil, "mingw", "kv", nil, "Set the MingW directory." }
  29. , {nil, "toolchain", "kv", nil, "Set the toolchain name." }
  30. , {nil, "packages", "vs", nil, "The package list." }
  31. }
  32. -- require packages
  33. function _require_packages(argv, packages)
  34. local config_argv = {"f", "-c"}
  35. if argv.verbose then
  36. table.insert(config_argv, "-v")
  37. end
  38. if argv.diagnosis then
  39. table.insert(config_argv, "-D")
  40. end
  41. if argv.plat then
  42. table.insert(config_argv, "--plat=" .. argv.plat)
  43. end
  44. if argv.arch then
  45. table.insert(config_argv, "--arch=" .. argv.arch)
  46. end
  47. if argv.mode then
  48. table.insert(config_argv, "--mode=" .. argv.mode)
  49. end
  50. if argv.ndk then
  51. table.insert(config_argv, "--ndk=" .. argv.ndk)
  52. end
  53. if argv.sdk then
  54. table.insert(config_argv, "--sdk=" .. argv.sdk)
  55. end
  56. if argv.ndk_sdkver then
  57. table.insert(config_argv, "--ndk_sdkver=" .. argv.ndk_sdkver)
  58. end
  59. if argv.vs then
  60. table.insert(config_argv, "--vs=" .. argv.vs)
  61. end
  62. if argv.vs_sdkver then
  63. table.insert(config_argv, "--vs_sdkver=" .. argv.vs_sdkver)
  64. end
  65. if argv.vs_toolset then
  66. table.insert(config_argv, "--vs_toolset=" .. argv.vs_toolset)
  67. end
  68. if argv.vs_runtime then
  69. table.insert(config_argv, "--vs_runtime=" .. argv.vs_runtime)
  70. end
  71. if argv.mingw then
  72. table.insert(config_argv, "--mingw=" .. argv.mingw)
  73. end
  74. if argv.toolchain then
  75. table.insert(config_argv, "--toolchain=" .. argv.toolchain)
  76. end
  77. if argv.cflags then
  78. table.insert(config_argv, "--cflags=" .. argv.cflags)
  79. end
  80. if argv.cxxflags then
  81. table.insert(config_argv, "--cxxflags=" .. argv.cxxflags)
  82. end
  83. if argv.ldflags then
  84. table.insert(config_argv, "--ldflags=" .. argv.ldflags)
  85. end
  86. os.vexecv("xmake", config_argv)
  87. local require_argv = {"require", "-f", "-y", "--build"}
  88. if argv.verbose then
  89. table.insert(require_argv, "-v")
  90. end
  91. if argv.diagnosis then
  92. table.insert(require_argv, "-D")
  93. end
  94. if argv.shallow then
  95. table.insert(require_argv, "--shallow")
  96. end
  97. if argv.jobs then
  98. table.insert(require_argv, "--jobs=" .. argv.jobs)
  99. end
  100. if argv.linkjobs then
  101. table.insert(require_argv, "--linkjobs=" .. argv.linkjobs)
  102. end
  103. local extra = {}
  104. if argv.mode == "debug" then
  105. extra.debug = true
  106. end
  107. if argv.kind == "shared" then
  108. extra.configs = extra.configs or {}
  109. extra.configs.shared = true
  110. end
  111. local configs = argv.configs
  112. if configs then
  113. extra.system = false
  114. extra.configs = extra.configs or {}
  115. local extra_configs, errors = ("{" .. configs .. "}"):deserialize()
  116. if extra_configs then
  117. table.join2(extra.configs, extra_configs)
  118. else
  119. raise(errors)
  120. end
  121. end
  122. local extra_str = string.serialize(extra, {indent = false, strip = true})
  123. table.insert(require_argv, "--extra=" .. extra_str)
  124. table.join2(require_argv, packages)
  125. os.vexecv("xmake", require_argv)
  126. end
  127. -- the given package is supported?
  128. function _package_is_supported(argv, packagename)
  129. local packages = get_packages()
  130. if packages then
  131. local plat = argv.plat or os.subhost()
  132. local packages_plat = packages[plat]
  133. for _, package in ipairs(packages_plat) do
  134. if package and packagename:split("%s+")[1] == package.name then
  135. local arch = argv.arch
  136. if not arch and plat ~= os.subhost() then
  137. arch = table.wrap(platform.archs(plat))[1]
  138. end
  139. if not arch then
  140. arch = os.subarch()
  141. end
  142. for _, package_arch in ipairs(package.archs) do
  143. if arch == package_arch then
  144. return true
  145. end
  146. end
  147. end
  148. end
  149. end
  150. end
  151. -- the main entry
  152. function main(...)
  153. -- parse arguments
  154. local argv = option.parse({...}, options, "Test all the given or changed packages.")
  155. -- get packages
  156. local packages = argv.packages or {}
  157. if #packages == 0 then
  158. local files = os.iorun("git diff --name-only HEAD^")
  159. for _, file in ipairs(files:split('\n'), string.trim) do
  160. if file:find("packages", 1, true) and path.filename(file) == "xmake.lua" then
  161. assert(file == file:lower(), "%s must be lower case!", file)
  162. local package = path.filename(path.directory(file))
  163. table.insert(packages, package)
  164. end
  165. end
  166. end
  167. if #packages == 0 then
  168. table.insert(packages, "tbox dev")
  169. end
  170. -- remove unsupported packages
  171. for idx, package in irpairs(packages) do
  172. assert(package == package:lower(), "package(%s) must be lower case!", package)
  173. if not _package_is_supported(argv, package) then
  174. table.remove(packages, idx)
  175. end
  176. end
  177. if #packages == 0 then
  178. print("no testable packages on %s!", argv.plat or os.subhost())
  179. return
  180. end
  181. -- prepare test project
  182. local repodir = os.curdir()
  183. local workdir = path.join(os.tmpdir(), "xmake-repo")
  184. print(packages)
  185. os.setenv("XMAKE_STATS", "false")
  186. os.tryrm(workdir)
  187. os.mkdir(workdir)
  188. os.cd(workdir)
  189. os.exec("xmake create test")
  190. os.cd("test")
  191. print(os.curdir())
  192. os.exec("xmake repo --add local-repo %s", repodir)
  193. os.exec("xmake repo -l")
  194. -- require packages
  195. _require_packages(argv, packages)
  196. --[[for _, package in ipairs(packages) do
  197. _require_packages(argv, package)
  198. end]]
  199. end