test.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if not argv.precompiled then
  111. table.insert(require_argv, "--build")
  112. end
  113. if argv.verbose then
  114. table.insert(require_argv, "-v")
  115. end
  116. if argv.diagnosis then
  117. table.insert(require_argv, "-D")
  118. end
  119. local is_debug = false
  120. if argv.debugdir then
  121. is_debug = true
  122. table.insert(require_argv, "--debugdir=" .. argv.debugdir)
  123. end
  124. if argv.shallow or is_debug then
  125. table.insert(require_argv, "--shallow")
  126. end
  127. if argv.jobs then
  128. table.insert(require_argv, "--jobs=" .. argv.jobs)
  129. end
  130. if argv.linkjobs then
  131. table.insert(require_argv, "--linkjobs=" .. argv.linkjobs)
  132. end
  133. if argv.fetch then
  134. table.insert(require_argv, "--fetch")
  135. end
  136. local extra = {}
  137. if argv.mode == "debug" then
  138. extra.debug = true
  139. end
  140. -- Some packages set shared=true as default, so we need to force set
  141. -- shared=false to test static build.
  142. extra.configs = extra.configs or {}
  143. extra.configs.shared = argv.kind == "shared"
  144. local configs = argv.configs
  145. if configs then
  146. extra.system = false
  147. extra.configs = extra.configs or {}
  148. local extra_configs, errors = ("{" .. configs .. "}"):deserialize()
  149. if extra_configs then
  150. table.join2(extra.configs, extra_configs)
  151. else
  152. raise(errors)
  153. end
  154. end
  155. local extra_str = string.serialize(extra, {indent = false, strip = true})
  156. table.insert(require_argv, "--extra=" .. extra_str)
  157. table.join2(require_argv, packages)
  158. os.vexecv("xmake", require_argv)
  159. end
  160. -- the given package is supported?
  161. function _package_is_supported(argv, packagename)
  162. local packages = get_packages()
  163. if packages then
  164. local plat = argv.plat or os.subhost()
  165. local packages_plat = packages[plat]
  166. for _, package in ipairs(packages_plat) do
  167. if package and packagename:split("%s+")[1] == package.name then
  168. local arch = argv.arch
  169. if not arch and plat ~= os.subhost() then
  170. arch = table.wrap(platform.archs(plat))[1]
  171. end
  172. if not arch then
  173. arch = os.subarch()
  174. end
  175. for _, package_arch in ipairs(package.archs) do
  176. if arch == package_arch then
  177. return true
  178. end
  179. end
  180. end
  181. end
  182. end
  183. end
  184. -- the main entry
  185. function main(...)
  186. -- parse arguments
  187. local argv = option.parse({...}, options, "Test all the given or changed packages.")
  188. -- get packages
  189. local packages = argv.packages or {}
  190. if #packages == 0 then
  191. local files = os.iorun("git diff --name-only HEAD^")
  192. for _, file in ipairs(files:split('\n'), string.trim) do
  193. if file:startswith("packages") then
  194. assert(file == file:lower(), "%s must be lower case!", file)
  195. local package = file:match("packages/%w/(%S-)/")
  196. table.insert(packages, package)
  197. end
  198. end
  199. end
  200. if #packages == 0 then
  201. table.insert(packages, "tbox dev")
  202. end
  203. -- remove unsupported packages
  204. for idx, package in irpairs(packages) do
  205. assert(package == package:lower(), "package(%s) must be lower case!", package)
  206. if not _package_is_supported(argv, package) then
  207. table.remove(packages, idx)
  208. end
  209. end
  210. if #packages == 0 then
  211. print("no testable packages on %s!", argv.plat or os.subhost())
  212. return
  213. end
  214. -- prepare test project
  215. local repodir = os.curdir()
  216. local workdir = path.join(os.tmpdir(), "xmake-repo")
  217. print(packages)
  218. os.setenv("XMAKE_STATS", "false")
  219. if not os.isfile(path.join(workdir, "test", "xmake.lua")) then
  220. os.tryrm(workdir)
  221. os.mkdir(workdir)
  222. os.cd(workdir)
  223. os.exec("xmake create test")
  224. else
  225. os.cd(workdir)
  226. end
  227. os.cd("test")
  228. print(os.curdir())
  229. -- do action for remote?
  230. if os.isdir("xmake-repo") then
  231. os.exec("xmake service --disconnect")
  232. end
  233. if argv.remote then
  234. os.tryrm("xmake-repo")
  235. os.cp(path.join(repodir, "packages"), "xmake-repo/packages")
  236. os.exec("xmake service --connect")
  237. repodir = "xmake-repo"
  238. end
  239. os.exec("xmake repo --add local-repo %s", repodir)
  240. os.exec("xmake repo -l")
  241. -- require packages
  242. _require_packages(argv, packages)
  243. --[[for _, package in ipairs(packages) do
  244. _require_packages(argv, package)
  245. end]]
  246. end