2
0

test.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. -- imports
  2. import("core.base.option")
  3. import("core.platform.platform")
  4. import("core.package.package", {alias = "core_package"})
  5. import("packages", {alias = "get_packages"})
  6. -- the options
  7. local options =
  8. {
  9. {'v', "verbose", "k", nil, "Enable verbose information." }
  10. , {'D', "diagnosis", "k", nil, "Enable diagnosis information." }
  11. , {nil, "shallow", "k", nil, "Only install the root packages." }
  12. , {'k', "kind", "kv", nil, "Enable static/shared library." }
  13. , {'p', "plat", "kv", nil, "Set the given platform." }
  14. , {'a', "arch", "kv", nil, "Set the given architecture." }
  15. , {'m', "mode", "kv", nil, "Set the given mode." }
  16. , {'j', "jobs", "kv", nil, "Set the build jobs." }
  17. , {'f', "configs", "kv", nil, "Set the configs." }
  18. , {'d', "debugdir", "kv", nil, "Set the debug source directory." }
  19. , {nil, "policies", "kv", nil, "Set the policies." }
  20. , {nil, "fetch", "k", nil, "Fetch package only." }
  21. , {nil, "precompiled", "k", nil, "Attemp to install the precompiled package." }
  22. , {nil, "remote", "k", nil, "Test package on the remote server." }
  23. , {nil, "linkjobs", "kv", nil, "Set the link jobs." }
  24. , {nil, "cflags", "kv", nil, "Set the cflags." }
  25. , {nil, "cxxflags", "kv", nil, "Set the cxxflags." }
  26. , {nil, "ldflags", "kv", nil, "Set the ldflags." }
  27. , {nil, "ndk", "kv", nil, "Set the Android NDK directory." }
  28. , {nil, "ndk_sdkver", "kv", nil, "Set the Android NDK platform sdk version." }
  29. , {nil, "sdk", "kv", nil, "Set the SDK directory of cross toolchain." }
  30. , {nil, "vs", "kv", nil, "Set the VS Compiler version." }
  31. , {nil, "vs_sdkver", "kv", nil, "Set the Windows SDK version." }
  32. , {nil, "vs_toolset", "kv", nil, "Set the Windows Toolset version." }
  33. , {nil, "vs_runtime", "kv", nil, "Set the VS Runtime library (deprecated)." }
  34. , {nil, "runtimes", "kv", nil, "Set the Runtime libraries." }
  35. , {nil, "xcode_sdkver", "kv", nil, "The SDK Version for Xcode" }
  36. , {nil, "target_minver", "kv", nil, "The Target Minimal Version" }
  37. , {nil, "appledev", "kv", nil, "The Apple Device Type" }
  38. , {nil, "mingw", "kv", nil, "Set the MingW directory." }
  39. , {nil, "toolchain", "kv", nil, "Set the toolchain name." }
  40. , {nil, "toolchain_host", "kv", nil, "Set the host toolchain name." }
  41. , {nil, "packages", "vs", nil, "The package list." }
  42. }
  43. -- check package is supported?
  44. function _check_package_is_supported()
  45. for _, names in pairs(core_package.apis()) do
  46. for _, name in ipairs(names) do
  47. if type(name) == "string" and name == "package.on_check" then
  48. return true
  49. end
  50. end
  51. end
  52. end
  53. -- require packages
  54. function _require_packages(argv, packages)
  55. local config_argv = {"f", "-c"}
  56. if argv.verbose then
  57. table.insert(config_argv, "-v")
  58. end
  59. if argv.diagnosis then
  60. table.insert(config_argv, "-D")
  61. end
  62. if argv.plat then
  63. table.insert(config_argv, "--plat=" .. argv.plat)
  64. end
  65. if argv.arch then
  66. table.insert(config_argv, "--arch=" .. argv.arch)
  67. end
  68. if argv.mode then
  69. table.insert(config_argv, "--mode=" .. argv.mode)
  70. end
  71. if argv.policies then
  72. table.insert(config_argv, "--policies=" .. argv.policies)
  73. end
  74. if argv.ndk then
  75. table.insert(config_argv, "--ndk=" .. argv.ndk)
  76. end
  77. if argv.sdk then
  78. table.insert(config_argv, "--sdk=" .. argv.sdk)
  79. end
  80. if argv.ndk_sdkver then
  81. table.insert(config_argv, "--ndk_sdkver=" .. argv.ndk_sdkver)
  82. end
  83. if argv.vs then
  84. table.insert(config_argv, "--vs=" .. argv.vs)
  85. end
  86. if argv.vs_sdkver then
  87. table.insert(config_argv, "--vs_sdkver=" .. argv.vs_sdkver)
  88. end
  89. if argv.vs_toolset then
  90. table.insert(config_argv, "--vs_toolset=" .. argv.vs_toolset)
  91. end
  92. local runtimes = argv.runtimes or argv.vs_runtime
  93. if runtimes then
  94. if is_host("windows") then
  95. table.insert(config_argv, "--vs_runtime=" .. runtimes)
  96. else
  97. table.insert(config_argv, "--runtimes=" .. runtimes)
  98. end
  99. end
  100. if argv.xcode_sdkver then
  101. table.insert(config_argv, "--xcode_sdkver=" .. argv.xcode_sdkver)
  102. end
  103. if argv.target_minver then
  104. table.insert(config_argv, "--target_minver=" .. argv.target_minver)
  105. end
  106. if argv.appledev then
  107. table.insert(config_argv, "--appledev=" .. argv.appledev)
  108. end
  109. if argv.mingw then
  110. table.insert(config_argv, "--mingw=" .. argv.mingw)
  111. end
  112. if argv.toolchain then
  113. table.insert(config_argv, "--toolchain=" .. argv.toolchain)
  114. end
  115. if argv.toolchain_host then
  116. table.insert(config_argv, "--toolchain_host=" .. argv.toolchain_host)
  117. end
  118. if argv.cflags then
  119. table.insert(config_argv, "--cflags=" .. argv.cflags)
  120. end
  121. if argv.cxxflags then
  122. table.insert(config_argv, "--cxxflags=" .. argv.cxxflags)
  123. end
  124. if argv.ldflags then
  125. table.insert(config_argv, "--ldflags=" .. argv.ldflags)
  126. end
  127. os.vexecv(os.programfile(), config_argv)
  128. local require_argv = {"require", "-f", "-y"}
  129. local check_argv = {"require", "-f", "-y", "--check"}
  130. if not argv.precompiled then
  131. table.insert(require_argv, "--build")
  132. end
  133. if argv.verbose then
  134. table.insert(require_argv, "-v")
  135. table.insert(check_argv, "-v")
  136. end
  137. if argv.diagnosis then
  138. table.insert(require_argv, "-D")
  139. table.insert(check_argv, "-D")
  140. end
  141. local is_debug = false
  142. if argv.debugdir then
  143. is_debug = true
  144. table.insert(require_argv, "--debugdir=" .. argv.debugdir)
  145. end
  146. if argv.shallow or is_debug then
  147. table.insert(require_argv, "--shallow")
  148. end
  149. if argv.jobs then
  150. table.insert(require_argv, "--jobs=" .. argv.jobs)
  151. end
  152. if argv.linkjobs then
  153. table.insert(require_argv, "--linkjobs=" .. argv.linkjobs)
  154. end
  155. if argv.fetch then
  156. table.insert(require_argv, "--fetch")
  157. end
  158. local extra = {}
  159. if argv.mode == "debug" then
  160. extra.debug = true
  161. end
  162. -- Some packages set shared=true as default, so we need to force set
  163. -- shared=false to test static build.
  164. extra.configs = extra.configs or {}
  165. extra.configs.shared = argv.kind == "shared"
  166. local configs = argv.configs
  167. if configs then
  168. extra.system = false
  169. extra.configs = extra.configs or {}
  170. local extra_configs, errors = ("{" .. configs .. "}"):deserialize()
  171. if extra_configs then
  172. table.join2(extra.configs, extra_configs)
  173. else
  174. raise(errors)
  175. end
  176. end
  177. local extra_str = string.serialize(extra, {indent = false, strip = true})
  178. table.insert(require_argv, "--extra=" .. extra_str)
  179. table.insert(check_argv, "--extra=" .. extra_str)
  180. local install_packages = {}
  181. if _check_package_is_supported() then
  182. for _, package in ipairs(packages) do
  183. local ok = os.vexecv(os.programfile(), table.join(check_argv, package), {try = true})
  184. if ok == 0 then
  185. table.insert(install_packages, package)
  186. end
  187. end
  188. else
  189. install_packages = packages
  190. end
  191. if #install_packages > 0 then
  192. os.vexecv(os.programfile(), table.join(require_argv, install_packages))
  193. else
  194. print("no testable packages on %s or you're using lower version xmake!", argv.plat or os.subhost())
  195. end
  196. end
  197. -- the given package is supported?
  198. function _package_is_supported(argv, packagename)
  199. local packages = get_packages()
  200. if packages then
  201. local plat = argv.plat or os.subhost()
  202. local packages_plat = packages[plat]
  203. for _, package in ipairs(packages_plat) do
  204. if package and packagename:split("%s+")[1] == package.name then
  205. local arch = argv.arch
  206. if not arch and plat ~= os.subhost() then
  207. arch = table.wrap(platform.archs(plat))[1]
  208. end
  209. if not arch then
  210. arch = os.subarch()
  211. end
  212. for _, package_arch in ipairs(package.archs) do
  213. if arch == package_arch then
  214. return true
  215. end
  216. end
  217. end
  218. end
  219. end
  220. end
  221. function get_modified_packages()
  222. local packages = {}
  223. local diff = os.iorun("git --no-pager diff HEAD^")
  224. for _, line in ipairs(diff:split("\n")) do
  225. if line:startswith("+++ b/") then
  226. local file = line:sub(7)
  227. if file:startswith("packages") then
  228. assert(file == file:lower(), "%s must be lower case!", file)
  229. local package = file:match("packages/%w/(%S-)/")
  230. table.insert(packages, package)
  231. end
  232. elseif line:startswith("+") and line:find("add_versions") then
  233. local version = line:match("add_versions%(\"(.-)\"")
  234. if version:find(":", 1, true) then
  235. version = version:split(":")[2]
  236. end
  237. if #packages > 0 and version then
  238. local lastpackage = packages[#packages]
  239. local splitinfo = lastpackage:split("%s+")
  240. table.insert(packages, splitinfo[1] .. " " .. version)
  241. end
  242. end
  243. end
  244. return table.unique(packages)
  245. end
  246. -- the main entry
  247. function main(...)
  248. -- parse arguments
  249. local argv = option.parse({...}, options, "Test all the given or changed packages.")
  250. -- get packages
  251. local packages = argv.packages or {}
  252. if #packages == 0 then
  253. packages = get_modified_packages()
  254. end
  255. if #packages == 0 then
  256. table.insert(packages, "tbox dev")
  257. end
  258. -- remove unsupported packages
  259. for idx, package in irpairs(packages) do
  260. assert(package == package:lower(), "package(%s) must be lower case!", package)
  261. if not _package_is_supported(argv, package) then
  262. table.remove(packages, idx)
  263. end
  264. end
  265. if #packages == 0 then
  266. print("no testable packages on %s!", argv.plat or os.subhost())
  267. return
  268. end
  269. -- prepare test project
  270. local repodir = os.curdir()
  271. local workdir = path.join(os.tmpdir(), "xmake-repo")
  272. print(packages)
  273. os.setenv("XMAKE_STATS", "false")
  274. if not os.isfile(path.join(workdir, "test", "xmake.lua")) then
  275. os.tryrm(workdir)
  276. os.mkdir(workdir)
  277. os.cd(workdir)
  278. os.execv(os.programfile(), {"create", "test"})
  279. else
  280. os.cd(workdir)
  281. end
  282. os.cd("test")
  283. print(os.curdir())
  284. -- do action for remote?
  285. if os.isdir("xmake-repo") then
  286. os.execv(os.programfile(), {"service", "--disconnect"})
  287. end
  288. if argv.remote then
  289. os.tryrm("xmake-repo")
  290. os.cp(path.join(repodir, "packages"), "xmake-repo/packages")
  291. os.execv(os.programfile(), {"service", "--connect"})
  292. repodir = "xmake-repo"
  293. end
  294. os.execv(os.programfile(), {"repo", "--add", "local-repo", repodir})
  295. os.execv(os.programfile(), {"repo", "-l"})
  296. -- require packages
  297. _require_packages(argv, packages)
  298. --[[for _, package in ipairs(packages) do
  299. _require_packages(argv, package)
  300. end]]
  301. end