test.lua 11 KB

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