test.lua 7.9 KB

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