test.lua 10 KB

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