test.lua 13 KB

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