test.lua 11 KB

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