2
0

xmake.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package("libsdl")
  2. set_homepage("https://www.libsdl.org/")
  3. set_description("Simple DirectMedia Layer")
  4. set_license("zlib")
  5. if is_plat("mingw") and is_subhost("msys") then
  6. add_extsources("pacman::SDL2")
  7. elseif is_plat("linux") then
  8. add_extsources("pacman::sdl2", "apt::libsdl2-dev")
  9. elseif is_plat("macosx") then
  10. add_extsources("brew::sdl2")
  11. end
  12. add_urls("https://www.libsdl.org/release/SDL2-$(version).zip",
  13. "https://github.com/libsdl-org/SDL/releases/download/release-$(version)/SDL2-$(version).zip", { alias = "archive" })
  14. add_urls("https://github.com/libsdl-org/SDL.git", { alias = "github" })
  15. add_versions("archive:2.0.8", "e6a7c71154c3001e318ba7ed4b98582de72ff970aca05abc9f45f7cbdc9088cb")
  16. add_versions("archive:2.0.12", "476e84d6fcbc499cd1f4a2d3fd05a924abc165b5d0e0d53522c9604fe5a021aa")
  17. add_versions("archive:2.0.14", "2c1e870d74e13dfdae870600bfcb6862a5eab4ea5b915144aff8d75a0f9bf046")
  18. add_versions("archive:2.0.16", "010148866e2226e5469f2879425d28ff7c572c736cb3fb65a0604c3cde6bfab9")
  19. add_versions("archive:2.0.18", "2d96cc82020341f7f5957c42001ad526e15fbb7056be8a74dab302483e97aa24")
  20. add_versions("archive:2.0.20", "cc8b16a326eb082c1f48ca30fdf471acfd2334b69bd7527e65ac58369013a1ba")
  21. add_versions("archive:2.0.22", "9a81ab724e6dcef96c61a4a2ebe7758e5b8bb191794650d276a20d5148fbd50c")
  22. add_versions("archive:2.24.0", "4b065503d45652d5f65d807fe98c757c73af2968727945b596861995bc3b69c2")
  23. add_versions("archive:2.24.2", "7fae98ac4e7b39eb2511fc27c2e84b220ac69b5296ff41f833b967c891f9d2ac")
  24. add_versions("archive:2.26.0", "4a181f158f88676816e4993d7e97e7b48ef273aa6f4e2909c6a85497e9af3e9f")
  25. add_versions("archive:2.26.1", "c038222fcac6ccc448daaa3febcae93fdac401aed12fd60da3b7939529276b1b")
  26. add_versions("archive:2.26.2", "31510e53266c9e4730070ec20543c25642a85db7f678445cd9cfc61c7b6eb94b")
  27. add_versions("archive:2.26.3", "3e46df1eb1be30448cf7c7f3fc0991980f0ef867c2412ab7bc925b631e5dc09c")
  28. add_versions("archive:2.26.4", "f22fd1410a4b4345f2da679b372629da38f644a686660f1ebadc5e0cb05a7369")
  29. add_versions("archive:2.26.5", "d88362fc3ee350a037e31381db00df764a294244bac8e427b8c67c6ca4d7e6fd")
  30. add_versions("archive:2.28.0", "a3fd9394093e08ae47233353c1efb07b28514fe63d7caed34b7811e8a17e5731")
  31. add_versions("archive:2.28.1", "b34b6f5a4d38191491724698a62241f0264c8a56c7d550fd49d1daf49261ae46")
  32. add_versions("archive:2.28.2", "22383a6b242bac072f949d2b3854cf04c6856cae7a87eaa78c60dd733b71e41e")
  33. add_versions("archive:2.28.3", "2308d4e4cd5852b3b81934dcc94603454834c14bef49de1cb1230c37ea6dc15c")
  34. add_versions("archive:2.28.4", "b53b9b42e731a33552d0a533316a88009b423c16a8a3a418df9ffe498c37da3d")
  35. add_versions("archive:2.28.5", "97bd14ee0ec67494d2b93f1a4f7da2bf891103c57090d96fdcc2b019d885c76a")
  36. add_versions("archive:2.30.0", "80b0c02b6018630cd40639ac9fc8e5c1d8eec14d8fe3e6dfa76343e3ba8b78d9")
  37. add_versions("archive:2.30.1", "c15ded54e9f32f8a1f9ed3e3dc072837a320ed23c5d0e95b7c18ecbe05c1187b")
  38. add_versions("archive:2.30.2", "09a822abf6e97f80d09cf9c46115faebb3476b0d56c1c035aec8ec3f88382ae7")
  39. add_versions("archive:2.30.3", "c5d78a9e0346c6695f03df8ba25e5e111a1e23c8aefa8372a1c5a0dd79acaf10")
  40. add_versions("archive:2.30.4", "292d5e2f897aa3acb6b365b605c3249c92916fbe7eba4a2e57573ada3855d7cb")
  41. add_versions("archive:2.30.5", "688d3da2bf7e887d0ba8e0f81c926119f85029544f4f6da8dea96db70f9d28e3")
  42. add_versions("archive:2.30.6", "6d4e00fcbee9fd8985cc2869edeb0b1a751912b87506cf2fb6471e73d981e1f4")
  43. add_versions("github:2.0.8", "release-2.0.8")
  44. add_versions("github:2.0.12", "release-2.0.12")
  45. add_versions("github:2.0.14", "release-2.0.14")
  46. add_versions("github:2.0.16", "release-2.0.16")
  47. add_versions("github:2.0.18", "release-2.0.18")
  48. add_versions("github:2.0.20", "release-2.0.20")
  49. add_versions("github:2.0.22", "release-2.0.22")
  50. add_versions("github:2.24.0", "release-2.24.0")
  51. add_versions("github:2.24.2", "release-2.24.2")
  52. add_versions("github:2.26.0", "release-2.26.0")
  53. add_versions("github:2.26.1", "release-2.26.1")
  54. add_versions("github:2.26.2", "release-2.26.2")
  55. add_versions("github:2.26.3", "release-2.26.3")
  56. add_versions("github:2.26.4", "release-2.26.4")
  57. add_versions("github:2.26.5", "release-2.26.5")
  58. add_versions("github:2.28.0", "release-2.28.0")
  59. add_versions("github:2.28.1", "release-2.28.1")
  60. add_versions("github:2.28.2", "release-2.28.2")
  61. add_versions("github:2.28.3", "release-2.28.3")
  62. add_versions("github:2.28.4", "release-2.28.4")
  63. add_versions("github:2.28.5", "release-2.28.5")
  64. add_versions("github:2.30.0", "release-2.30.0")
  65. add_versions("github:2.30.1", "release-2.30.1")
  66. add_versions("github:2.30.2", "release-2.30.2")
  67. add_versions("github:2.30.3", "release-2.30.3")
  68. add_versions("github:2.30.4", "release-2.30.4")
  69. add_versions("github:2.30.5", "release-2.30.5")
  70. add_versions("github:2.30.6", "release-2.30.6")
  71. add_patches("2.30.0", path.join(os.scriptdir(), "patches", "2.30.0", "fix_mingw.patch"), "ab54eebc2e58d88653b257bc5b48a232c5fb0e6fad5d63661b6388215a7b0cd0")
  72. add_patches("2.30.6", "https://github.com/libsdl-org/SDL/commit/7cf3234efeb7a68636bcfdfb3b1507b43fbb0845.patch", "c2fba1e76f8f10631544b63e8ce105a67d582b23bba7c96bdef5f135bd6b4cad")
  73. add_deps("cmake")
  74. add_includedirs("include", "include/SDL2")
  75. if is_plat("android") then
  76. add_configs("sdlmain", {description = "Use SDL_main entry point", default = false, type = "boolean", readonly = true})
  77. else
  78. add_configs("sdlmain", {description = "Use SDL_main entry point", default = true, type = "boolean"})
  79. end
  80. if is_plat("linux", "bsd") then
  81. add_configs("x11", {description = "Enables X11 support (requires it on the system)", default = true, type = "boolean"})
  82. add_configs("wayland", {description = "Enables Wayland support", default = true, type = "boolean"})
  83. -- @note deprecated
  84. add_configs("with_x", {description = "Enables X support (requires it on the system)", default = true, type = "boolean"})
  85. end
  86. if is_plat("wasm") then
  87. add_cxflags("-sUSE_SDL=0")
  88. end
  89. on_load(function (package)
  90. if package:config("sdlmain") then
  91. package:add("components", "main")
  92. if package:is_plat("mingw") then
  93. -- MinGW requires linking mingw32 before SDL2main
  94. local libsuffix = package:is_debug() and "d" or ""
  95. package:add("linkorders", "mingw32", "SDL2main" .. libsuffix)
  96. end
  97. else
  98. package:add("defines", "SDL_MAIN_HANDLED")
  99. end
  100. package:add("components", "lib")
  101. if package:is_plat("linux", "bsd") and (package:config("x11") or package:config("with_x")) then
  102. package:add("deps", "libxext", {private = true})
  103. end
  104. if package:is_plat("linux", "bsd") and package:config("wayland") then
  105. package:add("deps", "wayland", {private = true})
  106. end
  107. end)
  108. on_component("main", function (package, component)
  109. local libsuffix = package:is_debug() and "d" or ""
  110. component:add("links", "SDL2main" .. libsuffix)
  111. if package:is_plat("windows") then
  112. component:add("syslinks", "shell32")
  113. elseif package:is_plat("mingw") then
  114. component:add("syslinks", "mingw32")
  115. end
  116. component:add("deps", "lib")
  117. end)
  118. on_component("lib", function (package, component)
  119. local libsuffix = package:is_debug() and "d" or ""
  120. if package:config("shared") then
  121. component:add("links", "SDL2" .. libsuffix)
  122. else
  123. component:add("links", (package:is_plat("windows") and "SDL2-static" or "SDL2") .. libsuffix)
  124. if package:is_plat("windows", "mingw") then
  125. component:add("syslinks", "user32", "gdi32", "winmm", "imm32", "ole32", "oleaut32", "version", "uuid", "advapi32", "setupapi", "shell32")
  126. elseif package:is_plat("linux", "bsd") then
  127. component:add("syslinks", "pthread", "dl")
  128. if package:is_plat("bsd") then
  129. component:add("syslinks", "usbhid")
  130. end
  131. elseif package:is_plat("android") then
  132. component:add("syslinks", "dl", "log", "android", "GLESv1_CM", "GLESv2", "OpenSLES")
  133. elseif package:is_plat("iphoneos", "macosx") then
  134. component:add("frameworks", "AudioToolbox", "AVFoundation", "CoreAudio", "CoreVideo", "Foundation", "Metal", "QuartzCore", "CoreFoundation")
  135. component:add("syslinks", "iconv")
  136. if package:is_plat("macosx") then
  137. component:add("frameworks", "Cocoa", "Carbon", "ForceFeedback", "IOKit")
  138. else
  139. component:add("frameworks", "CoreBluetooth", "CoreGraphics", "CoreMotion", "OpenGLES", "UIKit")
  140. end
  141. if package:version():ge("2.0.14") then
  142. package:add("frameworks", "CoreHaptics", "GameController")
  143. end
  144. end
  145. end
  146. end)
  147. on_fetch("linux", "macosx", "bsd", function (package, opt)
  148. if opt.system then
  149. -- use sdl2-config
  150. local sdl2conf = try {function() return os.iorunv("sdl2-config", {"--version", "--cflags", "--libs"}) end}
  151. if sdl2conf then
  152. sdl2conf = os.argv(sdl2conf)
  153. local sdl2ver = table.remove(sdl2conf, 1)
  154. local result = {version = sdl2ver}
  155. for _, flag in ipairs(sdl2conf) do
  156. if flag:startswith("-L") and #flag > 2 then
  157. -- get linkdirs
  158. local linkdir = flag:sub(3)
  159. if linkdir and os.isdir(linkdir) then
  160. result.linkdirs = result.linkdirs or {}
  161. table.insert(result.linkdirs, linkdir)
  162. end
  163. elseif flag:startswith("-I") and #flag > 2 then
  164. -- get includedirs
  165. local includedir = flag:sub(3)
  166. if includedir and os.isdir(includedir) then
  167. result.includedirs = result.includedirs or {}
  168. table.insert(result.includedirs, includedir)
  169. end
  170. elseif flag:startswith("-l") and #flag > 2 then
  171. -- get links
  172. local link = flag:sub(3)
  173. result.links = result.links or {}
  174. table.insert(result.links, link)
  175. elseif flag:startswith("-D") and #flag > 2 then
  176. -- get defines
  177. local define = flag:sub(3)
  178. result.defines = result.defines or {}
  179. table.insert(result.defines, define)
  180. end
  181. end
  182. return result
  183. end
  184. -- finding using sdl2-config didn't work, fallback on pkgconfig
  185. if package.find_package then
  186. return package:find_package("pkgconfig::sdl2", opt)
  187. else
  188. return find_package("pkgconfig::sdl2", opt)
  189. end
  190. end
  191. end)
  192. on_install(function (package)
  193. local configs = {}
  194. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  195. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  196. table.insert(configs, "-DSDL_TEST=OFF")
  197. local opt
  198. if package:is_plat("linux", "bsd", "cross") then
  199. local includedirs = {}
  200. for _, depname in ipairs({"libxext", "libx11", "xorgproto"}) do
  201. local dep = package:dep(depname)
  202. if dep then
  203. local depfetch = dep:fetch()
  204. if depfetch then
  205. for _, includedir in ipairs(depfetch.includedirs or depfetch.sysincludedirs) do
  206. table.insert(includedirs, includedir)
  207. end
  208. end
  209. end
  210. end
  211. if #includedirs > 0 then
  212. includedirs = table.unique(includedirs)
  213. local cflags = {}
  214. opt = opt or {}
  215. opt.cflags = cflags
  216. for _, includedir in ipairs(includedirs) do
  217. table.insert(cflags, "-I" .. includedir)
  218. end
  219. table.insert(configs, "-DCMAKE_INCLUDE_PATH=" .. table.concat(includedirs, ";"))
  220. end
  221. elseif package:is_plat("wasm") then
  222. -- emscripten enables USE_SDL by default which will conflict with the sdl headers
  223. opt = opt or {}
  224. opt.cflags = {"-sUSE_SDL=0"}
  225. end
  226. import("package.tools.cmake").install(package, configs, opt)
  227. end)
  228. on_test(function (package)
  229. assert(package:check_cxxsnippets({test = [[
  230. #include <SDL2/SDL.h>
  231. int main(int argc, char** argv) {
  232. SDL_Init(0);
  233. return 0;
  234. }
  235. ]]}, {configs = {defines = "SDL_MAIN_HANDLED"}}));
  236. end)