test.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. -- config packages
  54. function _config_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. table.insert(config_argv, "--runtimes=" .. runtimes)
  95. end
  96. if argv.xcode_sdkver then
  97. table.insert(config_argv, "--xcode_sdkver=" .. argv.xcode_sdkver)
  98. end
  99. if argv.target_minver then
  100. table.insert(config_argv, "--target_minver=" .. argv.target_minver)
  101. end
  102. if argv.appledev then
  103. table.insert(config_argv, "--appledev=" .. argv.appledev)
  104. end
  105. if argv.mingw then
  106. table.insert(config_argv, "--mingw=" .. argv.mingw)
  107. end
  108. if argv.toolchain then
  109. table.insert(config_argv, "--toolchain=" .. argv.toolchain)
  110. end
  111. if argv.toolchain_host then
  112. table.insert(config_argv, "--toolchain_host=" .. argv.toolchain_host)
  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(os.programfile(), config_argv)
  124. end
  125. -- get extra string
  126. function _get_extra_str(argv)
  127. local extra = {}
  128. if argv.mode == "debug" then
  129. extra.debug = true
  130. end
  131. -- Some packages set shared=true as default, so we need to force set
  132. -- shared=false to test static build.
  133. extra.configs = extra.configs or {}
  134. extra.configs.shared = argv.kind == "shared"
  135. local configs = argv.configs
  136. if configs then
  137. extra.system = false
  138. extra.configs = extra.configs or {}
  139. local extra_configs, errors = ("{" .. configs .. "}"):deserialize()
  140. if extra_configs then
  141. table.join2(extra.configs, extra_configs)
  142. else
  143. raise(errors)
  144. end
  145. end
  146. return string.serialize(extra, {indent = false, strip = true})
  147. end
  148. -- load packages
  149. function _load_packages(argv, packages)
  150. _config_packages(argv, packages)
  151. local info_argv = {"require", "-f", "-y", "--info"}
  152. if argv.verbose then
  153. table.insert(info_argv, "-v")
  154. end
  155. if argv.diagnosis then
  156. table.insert(info_argv, "-D")
  157. end
  158. local extra_str = _get_extra_str(argv)
  159. table.insert(info_argv, "--extra=" .. extra_str)
  160. -- call `xrepo info` to test on_load
  161. if #packages > 0 then
  162. print("testing to load packages ...")
  163. print(" > if it causes errors, please remove assert/raise() to on_check.")
  164. os.vexecv(os.programfile(), table.join(info_argv, packages))
  165. end
  166. end
  167. -- require packages
  168. function _require_packages(argv, packages)
  169. _config_packages(argv, packages)
  170. local require_argv = {"require", "-f", "-y"}
  171. local check_argv = {"require", "-f", "-y", "--check"}
  172. if not argv.precompiled then
  173. table.insert(require_argv, "--build")
  174. end
  175. if argv.verbose then
  176. table.insert(require_argv, "-v")
  177. table.insert(check_argv, "-v")
  178. end
  179. if argv.diagnosis then
  180. table.insert(require_argv, "-D")
  181. table.insert(check_argv, "-D")
  182. end
  183. local is_debug = false
  184. if argv.debugdir then
  185. is_debug = true
  186. table.insert(require_argv, "--debugdir=" .. argv.debugdir)
  187. end
  188. if argv.shallow or is_debug then
  189. table.insert(require_argv, "--shallow")
  190. end
  191. if argv.jobs then
  192. table.insert(require_argv, "--jobs=" .. argv.jobs)
  193. end
  194. if argv.linkjobs then
  195. table.insert(require_argv, "--linkjobs=" .. argv.linkjobs)
  196. end
  197. if argv.fetch then
  198. table.insert(require_argv, "--fetch")
  199. end
  200. local extra_str = _get_extra_str(argv)
  201. table.insert(require_argv, "--extra=" .. extra_str)
  202. table.insert(check_argv, "--extra=" .. extra_str)
  203. -- test on_check
  204. local install_packages = {}
  205. if _check_package_is_supported() then
  206. print("testing to check packages ...")
  207. for _, package in ipairs(packages) do
  208. local ok = os.vexecv(os.programfile(), table.join(check_argv, package), {try = true})
  209. if ok == 0 then
  210. table.insert(install_packages, package)
  211. end
  212. end
  213. else
  214. install_packages = packages
  215. end
  216. -- test installation
  217. if #install_packages > 0 then
  218. print("testing to install packages ...")
  219. os.vexecv(os.programfile(), table.join(require_argv, install_packages))
  220. else
  221. print("no testable packages on %s or you're using lower version xmake!", argv.plat or os.subhost())
  222. end
  223. end
  224. -- the given package is supported?
  225. function _package_is_supported(argv, packagename)
  226. local packages = get_packages()
  227. if packages then
  228. local plat = argv.plat or os.subhost()
  229. local packages_plat = packages[plat]
  230. for _, package in ipairs(packages_plat) do
  231. if package and packagename:split("%s+")[1] == package.name then
  232. local arch = argv.arch
  233. if not arch and plat ~= os.subhost() then
  234. arch = table.wrap(platform.archs(plat))[1]
  235. end
  236. if not arch then
  237. arch = os.subarch()
  238. end
  239. for _, package_arch in ipairs(package.archs) do
  240. print(package_arch, package.archs)
  241. if arch == package_arch then
  242. return true
  243. end
  244. end
  245. end
  246. end
  247. end
  248. end
  249. function append_package_version(packages, line)
  250. local version = line:match("add_versions%(['\"](.-)['\"]") or line:match("package:add%(['\"]versions['\"],%s*['\"](.-)['\"]")
  251. if version then
  252. if version:find(":", 1, true) then
  253. version = version:split(":")[2]
  254. end
  255. if #packages > 0 and version then
  256. local lastpackage = packages[#packages]
  257. local splitinfo = lastpackage:split("%s+")
  258. table.insert(packages, splitinfo[1] .. " " .. version)
  259. end
  260. end
  261. end
  262. function get_modified_packages()
  263. local new_packages = {}
  264. local old_packages = {}
  265. local diff = os.iorun("git --no-pager diff HEAD^")
  266. for _, line in ipairs(diff:split("\n")) do
  267. if line:startswith("+++ b/") then
  268. local file = line:sub(7)
  269. if file:startswith("packages") then
  270. assert(file == file:lower(), "%s must be lower case!", file)
  271. local package = file:match("packages/%w/(%S-)/")
  272. table.insert(new_packages, package)
  273. table.insert(old_packages, package)
  274. end
  275. elseif line:startswith("+") and (line:find("add_versions", 1, true) or line:find("package:add%((['\"])versions%1")) then
  276. append_package_version(new_packages, line)
  277. elseif line:startswith("-") and (line:find("add_versions", 1, true) or line:find("package:add%((['\"])versions%1")) then
  278. append_package_version(old_packages, line)
  279. end
  280. end
  281. if #old_packages > 0 then
  282. table.remove_if(old_packages, function (_, package)
  283. local splitinfo = package:split("%s+")
  284. return #splitinfo == 1
  285. end)
  286. table.remove_if(new_packages, function (_, package)
  287. return table.contains(old_packages, package)
  288. end)
  289. -- {
  290. -- "pkgname", <-- remove this
  291. -- "pkgname pkgver"
  292. -- }
  293. for i = #new_packages - 1, 1, -1 do
  294. if new_packages[i + 1]:startswith(new_packages[i] .. " ") then
  295. table.remove(new_packages, i)
  296. end
  297. end
  298. end
  299. return table.unique(new_packages)
  300. end
  301. -- @see https://github.com/xmake-io/xmake-repo/issues/6940
  302. function _lock_packages(packages)
  303. local locked_packages = {}
  304. for _, package in ipairs(packages) do
  305. if table.contains(locked_packages, package) then
  306. raise("package(%s) has been locked, please do not submit it, @see https://github.com/xmake-io/xmake-repo/issues/6940", package)
  307. end
  308. end
  309. end
  310. -- the main entry
  311. function main(...)
  312. -- parse arguments
  313. local argv = option.parse({...}, options, "Test all the given or changed packages.")
  314. -- get packages
  315. local packages = argv.packages or {}
  316. if #packages == 0 then
  317. packages = get_modified_packages()
  318. end
  319. if #packages == 0 then
  320. table.insert(packages, "tbox dev")
  321. end
  322. -- prepare test project
  323. local repodir = os.curdir()
  324. local workdir = path.join(os.tmpdir(), "xmake-repo")
  325. print(packages)
  326. os.setenv("XMAKE_STATS", "false")
  327. if not os.isfile(path.join(workdir, "test", "xmake.lua")) then
  328. os.tryrm(workdir)
  329. os.mkdir(workdir)
  330. os.cd(workdir)
  331. os.execv(os.programfile(), {"create", "test"})
  332. else
  333. os.cd(workdir)
  334. end
  335. os.cd("test")
  336. print(os.curdir())
  337. -- do action for remote?
  338. if os.isdir("xmake-repo") then
  339. os.execv(os.programfile(), {"service", "--disconnect"})
  340. end
  341. if argv.remote then
  342. os.tryrm("xmake-repo")
  343. os.cp(path.join(repodir, "packages"), "xmake-repo/packages")
  344. os.execv(os.programfile(), {"service", "--connect"})
  345. repodir = "xmake-repo"
  346. end
  347. os.execv(os.programfile(), {"repo", "--add", "local-repo", repodir})
  348. os.execv(os.programfile(), {"repo", "-l"})
  349. local packages_original = table.clone(packages)
  350. -- load packages
  351. _load_packages(argv, packages_original)
  352. local old_dir = os.cd(repodir)
  353. -- remove unsupported packages
  354. for idx, package in irpairs(packages) do
  355. assert(package == package:lower(), "package(%s) must be lower case!", package)
  356. if not _package_is_supported(argv, package) then
  357. table.remove(packages, idx)
  358. end
  359. end
  360. os.cd(old_dir)
  361. -- no testable packages
  362. if #packages == 0 then
  363. print("no testable packages on %s!", argv.plat or os.subhost())
  364. return
  365. end
  366. -- lock packages
  367. -- _lock_packages(packages)
  368. -- require packages
  369. _require_packages(argv, packages)
  370. end