xmake.lua 13 KB

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