test.lua 9.2 KB

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