xmake.lua 13 KB

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