xmake.lua 12 KB

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