xmake.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package("qtbase")
  2. set_kind("template")
  3. set_homepage("https://www.qt.io")
  4. set_description("Qt is the faster, smarter way to create innovative devices, modern UIs & applications for multiple screens. Cross-platform software development at its best.")
  5. set_license("LGPL-3")
  6. add_configs("shared", {description = "Download shared binaries.", default = true, type = "boolean", readonly = true})
  7. add_configs("runtimes", {description = "Set compiler runtimes.", default = "MD", readonly = true})
  8. add_configs("exact_version", {description = "Require exact version match if Qt is system-installed.", default = false, type = "boolean"})
  9. add_configs("tools_only", {description = "Install only SDK tools, primarily for cross-compilation scenarios.", default = false, type = "boolean"})
  10. add_deps("aqt")
  11. if on_check then
  12. on_check(function (package)
  13. local version = package:version()
  14. -- Chech for 32bits support removal in Qt6
  15. if package:is_plat("windows", "mingw") and version then
  16. if version:ge("6.0") and package:is_arch("x86", "i386") then
  17. raise("package(qt6base): 32bits support was removed in Qt6")
  18. end
  19. end
  20. -- Check for Visual Studio version requirements
  21. if package:is_plat("windows") and version then
  22. local vs = package:toolchain("msvc"):config("vs")
  23. if version:ge("6.8") and tonumber(vs) < 2022 then
  24. raise("package(qt6base): Qt 6.8+ requires Visual Studio 2022")
  25. elseif version:ge("6.0") and tonumber(vs) < 2019 then
  26. raise("package(qt6base): Qt 6.0+ requires Visual Studio 2019")
  27. end
  28. end
  29. -- Check for symbolic link creation issues when cross-compiling on Windows
  30. if is_host("windows") and package:is_plat("linux") then
  31. wprint("It seems that you are installing Qt SDK on Windows for a non-Windows platform. If you encounter issues related to symbolic link creation, " ..
  32. "please try enabling Developer Mode in Windows Settings or run xmake with administrator privileges.")
  33. end
  34. -- Check for WASM support
  35. if package:is_plat("wasm") and version then
  36. -- For more information on WASM support, visit:
  37. -- https://doc.qt.io/qt-6/wasm.html
  38. -- https://aqtinstall.readthedocs.io/en/latest/getting_started.html#installing-qt-for-wasm
  39. if version:ge("6.8") then
  40. -- TODO: Update this check when aqtinstall supports Qt 6.8+ for WASM
  41. raise("package(qt6base): Qt 6.8+ for WASM is not supported by aqtinstall yet")
  42. elseif (version:ge("6.0") and version:lt("6.2")) or version:lt("5.13") then
  43. raise("package(qtbase): WASM support requires Qt 5.13+ or Qt 6.2+")
  44. end
  45. end
  46. -- Check for ARM64 support
  47. if package:is_arch("arm64.*", "aarch64") and package:is_plat("windows", "mingw", "linux") and version then
  48. if package:is_plat("windows", "mingw") and version:lt("6.8") then
  49. raise("package(qt6base): Windows on ARM64 support was added in Qt 6.8")
  50. elseif package:is_plat("linux") and version:lt("6.7") then
  51. raise("package(qt6base): Linux on ARM64 support was added in Qt 6.7")
  52. end
  53. end
  54. end)
  55. end
  56. on_load(function (package)
  57. package:addenv("PATH", "bin")
  58. if package:is_cross() then
  59. local host_qt_depname = "qt-tools"
  60. if package:version() then
  61. host_qt_depname = host_qt_depname .. " " .. package:version():shortstr()
  62. end
  63. package:add("deps", host_qt_depname, {configs = {tools_only = true, exact_version = true}})
  64. end
  65. end)
  66. on_fetch(function (package, opt)
  67. import("core.base.semver")
  68. import("detect.sdks.find_qt")
  69. local qt = package:data("qt")
  70. if qt then
  71. return qt
  72. end
  73. local sdkdir
  74. if not opt.system then
  75. sdkdir = package:installdir()
  76. end
  77. local qt
  78. local version = package:version()
  79. local find_opt = {force = opt.force}
  80. if package:config("exact_version") and version then
  81. find_opt.version = version:shortstr()
  82. end
  83. if not package:is_cross() then
  84. qt = find_qt(sdkdir, find_opt)
  85. else
  86. local host_qt = package:dep("qt-tools")
  87. if not host_qt then
  88. return
  89. end
  90. local host_qt_data = host_qt:data("qt") or find_qt(nil, {version = host_qt:version() and host_qt:version():shortstr()})
  91. if not host_qt_data then
  92. return
  93. end
  94. qt = find_qt(sdkdir, table.join(find_opt, {sdkdir_host = host_qt_data.sdkdir}))
  95. if qt then
  96. -- Avoid mistakenly identifying "host_qt" as the target SDK
  97. if host_qt_data.sdkdir and host_qt_data.sdkdir == qt.sdkdir then
  98. return
  99. end
  100. end
  101. end
  102. if not qt then
  103. return
  104. end
  105. local qtversion = semver.new(qt.sdkver)
  106. if version and not qtversion:eq(version) then
  107. if package:config("exact_version") or not qtversion:ge(version) then
  108. return
  109. end
  110. end
  111. qt.version = qt.sdkver
  112. package:data_set("qt", qt)
  113. return qt
  114. end)
  115. on_install(function (package)
  116. import("core.base.semver")
  117. import("core.project.config")
  118. import("core.tool.toolchain")
  119. local version = package:version()
  120. local versionstr = version:shortstr()
  121. -- Usage of `aqtinstall`: aqt install-qt <host> <target> <Qt version> [<arch>]
  122. -- <host> options: {linux, linux_arm64, mac, windows, windows_arm64}
  123. -- <target> options: {desktop, winrt, android, ios}
  124. --
  125. -- The `pseudo_host` variable (used as <host>) is determined based on the target platform
  126. --
  127. -- Behavior:
  128. -- 1. No cross-compilation (package:plat() == os.host()):
  129. -- - Downloads Qt libraries and SDK tools specific to the host platform.
  130. --
  131. -- 2. Cross-compilation (package:plat() ~= os.host()):
  132. -- - Downloads Qt libraries and incompatible SDK tools.
  133. -- - Usable SDK tools are retrieved from package:dep("qt-tools") or the "qt_host" configuration.
  134. local pseudo_host
  135. local platform_map = {windows = "windows", mingw = "windows", linux = "linux", macosx = "mac"}
  136. if package:is_plat("windows", "mingw", "linux", "macosx") then
  137. pseudo_host = platform_map[package:plat()]
  138. elseif package:is_plat("android") then
  139. pseudo_host = platform_map[os.host()]
  140. elseif package:is_plat("iphoneos") then
  141. pseudo_host = "mac"
  142. elseif package:is_plat("wasm") then
  143. if version:ge("6.8") then
  144. pseudo_host = "all_os"
  145. else
  146. pseudo_host = platform_map[os.host()]
  147. end
  148. else
  149. raise("unhandled platform " .. package:plat())
  150. end
  151. if package:is_arch("arm64.*", "aarch64") and package:is_plat("windows", "mingw", "linux") then
  152. pseudo_host = pseudo_host .. "_arm64"
  153. end
  154. local target
  155. if package:is_plat("windows", "mingw", "linux", "macosx") then
  156. target = "desktop"
  157. elseif package:is_plat("android") then
  158. target = "android"
  159. elseif package:is_plat("iphoneos") then
  160. target = "ios"
  161. elseif package:is_plat("wasm") then
  162. if version:ge("6.8") then
  163. target = "wasm"
  164. else
  165. target = "desktop"
  166. end
  167. else
  168. raise("unhandled plat " .. package:plat())
  169. end
  170. local arch
  171. if package:is_plat("windows", "mingw") then
  172. local winarch
  173. if package:is_arch("x64", "x86_64", "arm64") then
  174. winarch = "64"
  175. elseif version:lt("6.0") and package:is_arch("x86", "i386") then -- 32bits support was removed in Qt6
  176. winarch = "32"
  177. else
  178. raise("unhandled arch " .. package:targetarch())
  179. end
  180. local compiler_version
  181. if package:is_plat("windows") then
  182. local vs = package:toolchain("msvc"):config("vs")
  183. if version:ge("6.8") then
  184. compiler_version = "msvc2022"
  185. elseif tonumber(vs) >= 2019 and version:ge("5.15") then
  186. compiler_version = "msvc2019"
  187. elseif tonumber(vs) >= 2017 then
  188. compiler_version = "msvc2017"
  189. elseif tonumber(vs) >= 2015 then
  190. compiler_version = "msvc2015"
  191. else
  192. raise("unhandled msvc version " .. vs)
  193. end
  194. if package:is_arch("x64", "x86_64") then
  195. compiler_version = compiler_version .. "_64"
  196. elseif package:is_arch("arm64") then -- arm64 support was added in Qt6.2
  197. compiler_version = compiler_version .. "_arm64"
  198. end
  199. else
  200. local cc = package:tool("cc")
  201. local ccversion = os.iorunv(cc, {"-dumpversion"}):trim()
  202. local mingw_version = semver.new(ccversion)
  203. if version:ge("6.2.2") then
  204. compiler_version = "mingw"
  205. elseif mingw_version:ge("8.1") then
  206. compiler_version = "mingw81"
  207. elseif mingw_version:ge("7.3") then
  208. compiler_version = "mingw73"
  209. elseif mingw_version:ge("5.3") then
  210. compiler_version = "mingw53"
  211. else
  212. raise("unhandled mingw version " .. version)
  213. end
  214. end
  215. arch = "win" .. winarch .. "_" .. compiler_version
  216. elseif package:is_plat("linux") then
  217. if package:is_arch("arm64.*", "aarch64") then
  218. arch = "linux_gcc_arm64"
  219. elseif version:ge("6.7.0") then
  220. arch = "linux_gcc_64"
  221. else
  222. arch = "gcc_64"
  223. end
  224. elseif package:is_plat("macosx") then
  225. arch = "clang_64"
  226. elseif package:is_plat("android") then
  227. if version:le("5.13") or version:ge("6.0") then
  228. if package:is_arch("x86_64", "x64") then
  229. arch = "android_x86_64"
  230. elseif package:is_arch("arm64", "arm64-v8a") then
  231. arch = "android_arm64_v8a"
  232. elseif package:is_arch("armv7", "armv7-a", "armeabi", "armeabi-v7a") then
  233. arch = "android_armv7"
  234. elseif package:is_arch("x86") then
  235. arch = "android_x86"
  236. else
  237. raise("unhandled arch " .. package:targetarch())
  238. end
  239. else
  240. arch = "android"
  241. end
  242. elseif package:is_plat("iphoneos") then
  243. arch = "ios"
  244. elseif package:is_plat("wasm") then
  245. if version:lt("6.5") then
  246. arch = "wasm_32"
  247. else
  248. arch = "wasm_multithread" -- "wasm_singlethread" is also available as an option.
  249. end
  250. end
  251. local installdir = package:installdir()
  252. local aqt_args = {"install-qt", "-O", installdir, pseudo_host, target, versionstr, arch}
  253. if package:config("tools_only") then
  254. -- Attempt to reduce the installation size by specifying only the required archives
  255. local archives = {
  256. "qtbase", -- For qmake, moc, rcc, uic, windeployqt, androiddeployqt
  257. "qttools", -- For lupdate, lrelease
  258. "qtdeclarative" -- For qml
  259. }
  260. if is_host("linux") then
  261. table.join2(archives, {"icu"})
  262. end
  263. local available_archives = try {
  264. function()
  265. return os.iorunv("aqt", {"list-qt", pseudo_host, target, "--archives", versionstr, arch}):split(" ")
  266. end
  267. } or {}
  268. local specify_archives = true
  269. for _, archive in ipairs(archives) do
  270. if not table.contains(available_archives, archive) then
  271. specify_archives = false
  272. break
  273. end
  274. end
  275. if specify_archives then
  276. table.insert(aqt_args, "--archives")
  277. table.join2(aqt_args, archives)
  278. end
  279. end
  280. os.vrunv("aqt", aqt_args)
  281. -- move files to root
  282. os.mv(path.join(installdir, versionstr, "*", "*"), installdir)
  283. os.rmdir(path.join(installdir, versionstr))
  284. end)
  285. on_test(function (package)
  286. import("lib.detect.find_file")
  287. local qt = assert(package:data("qt"))
  288. local search_dirs = {}
  289. if qt.bindir_host then table.insert(search_dirs, qt.bindir_host) end
  290. if qt.bindir then table.insert(search_dirs, qt.bindir) end
  291. if qt.libexecdir_host then table.insert(search_dirs, qt.libexecdir_host) end
  292. if qt.libexecdir then table.insert(search_dirs, qt.libexecdir) end
  293. local function getbin(name)
  294. name = name .. (is_host("windows") and ".exe" or "")
  295. return assert(find_file(name, search_dirs), name .. " not found!")
  296. end
  297. os.vrun(getbin("qmake") .. " -v")
  298. os.vrun(getbin("moc") .. " -v")
  299. -- rcc -v and uic -v seems to hang CI forever
  300. --os.vrun(getbin("rcc") .. " -v") -- rcc -v hangs CI
  301. --os.vrun(getbin("uic") .. " -v") -- uic -v seems to hang on CI
  302. end)