xmake.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package("sfml")
  2. set_homepage("https://www.sfml-dev.org")
  3. set_description("Simple and Fast Multimedia Library")
  4. set_license("zlib")
  5. add_urls("https://github.com/SFML/SFML/archive/refs/tags/$(version).tar.gz",
  6. "https://github.com/SFML/SFML.git")
  7. -- Before 2.6.0 only x86 is supported for Mac
  8. if not is_plat("macosx") or not is_arch("arm.*") then
  9. add_versions("2.5.1", "438c91a917cc8aa19e82c6f59f8714da353c488584a007d401efac8368e1c785")
  10. end
  11. add_versions("3.0.0", "37506fafbd618b1f8e153bbca8811e62203a70b32a1183279fb9612fd0501d2b")
  12. add_versions("2.6.1", "82535db9e57105d4f3a8aedabd138631defaedc593cab589c924b7d7a11ffb9d")
  13. add_versions("2.6.0", "0c3f84898ea1db07dc46fa92e85038d8c449e3c8653fe09997383173de96bc06")
  14. add_configs("graphics", {description = "Use the graphics module", default = true, type = "boolean"})
  15. add_configs("window", {description = "Use the window module", default = true, type = "boolean"})
  16. add_configs("audio", {description = "Use the audio module", default = true, type = "boolean"})
  17. add_configs("network", {description = "Use the network module", default = true, type = "boolean"})
  18. if is_plat("windows", "mingw") then
  19. add_configs("main", {description = "Link to the sfml-main library", default = true, type = "boolean"})
  20. end
  21. if is_plat("mingw") and is_subhost("msys") then
  22. add_extsources("pacman::sfml")
  23. elseif is_plat("linux") then
  24. add_extsources("pacman::sfml", "apt::libsfml-dev")
  25. elseif is_plat("macosx") then
  26. add_extsources("brew::sfml/sfml-all")
  27. end
  28. if not is_host("windows") then
  29. add_extsources("pkgconfig::sfml-all")
  30. end
  31. on_component("graphics", function (package, component)
  32. local e = package:config("shared") and "" or "-s"
  33. if package:debug() then
  34. e = e .. "-d"
  35. end
  36. component:add("links", "sfml-graphics" .. e)
  37. component:add("deps", "window", "system")
  38. component:add("extsources", "brew::sfml/sfml-graphics")
  39. component:add("extsources", "pkgconfig::sfml-graphics")
  40. end)
  41. on_component("window", function (package, component)
  42. local e = package:config("shared") and "" or "-s"
  43. if package:debug() then
  44. e = e .. "-d"
  45. end
  46. component:add("links", "sfml-window" .. e)
  47. component:add("deps", "system")
  48. component:add("extsources", "brew::sfml/sfml-window")
  49. component:add("extsources", "pkgconfig::sfml-window")
  50. if not package:config("shared") then
  51. if package:is_plat("windows", "mingw") then
  52. component:add("syslinks", "opengl32", "gdi32", "advapi32", "user32")
  53. elseif package:is_plat("linux") then
  54. component:add("syslinks", "dl")
  55. elseif package:is_plat("bsd") then
  56. component:add("syslinks", "usbhid")
  57. elseif package:is_plat("macosx") then
  58. component:add("frameworks", "Foundation", "AppKit", "IOKit", "Carbon")
  59. elseif package:is_plat("iphoneos") then
  60. component:add("frameworks", "Foundation", "UIKit", "CoreGraphics", "QuartzCore", "CoreMotion")
  61. end
  62. end
  63. end)
  64. on_component("audio", function (package, component)
  65. local e = package:config("shared") and "" or "-s"
  66. if package:debug() then
  67. e = e .. "-d"
  68. end
  69. component:add("links", "sfml-audio" .. e)
  70. component:add("deps", "system")
  71. component:add("extsources", "brew::sfml/sfml-audio")
  72. component:add("extsources", "pkgconfig::sfml-audio")
  73. if not package:config("shared") and package:is_plat("windows", "mingw") then
  74. component:add("links", "flac", "vorbisenc", "vorbisfile", "vorbis", "ogg")
  75. if not (package:gitref() or package:version():ge("3.0.0")) then
  76. component:add("links", "openal32")
  77. end
  78. end
  79. end)
  80. on_component("network", function (package, component)
  81. local e = package:config("shared") and "" or "-s"
  82. if package:debug() then
  83. e = e .. "-d"
  84. end
  85. component:add("links", "sfml-network" .. e)
  86. component:add("deps", "system")
  87. component:add("extsources", "brew::sfml/sfml-network")
  88. component:add("extsources", "pkgconfig::sfml-network")
  89. component:add("extsources", "apt::sfml-network")
  90. if not package:config("shared") and package:is_plat("windows", "mingw") then
  91. component:add("syslinks", "ws2_32")
  92. end
  93. end)
  94. on_component("system", function (package, component)
  95. local e = package:config("shared") and "" or "-s"
  96. if package:debug() then
  97. e = e .. "-d"
  98. end
  99. component:add("links", "sfml-system" .. e)
  100. if package:is_plat("windows", "mingw") then
  101. component:add("syslinks", "winmm")
  102. end
  103. if package:is_plat("windows", "mingw") and package:config("main") then
  104. component:add("deps", "main")
  105. end
  106. component:add("extsources", "brew::sfml/sfml-system")
  107. component:add("extsources", "pkgconfig::sfml-system")
  108. if not package:config("shared") then
  109. if package:is_plat("windows", "mingw") then
  110. component:add("syslinks", "winmm")
  111. elseif package:is_plat("linux") then
  112. component:add("syslinks", "rt", "pthread")
  113. elseif package:is_plat("bsd", "macosx") then
  114. component:add("syslinks", "pthread")
  115. end
  116. end
  117. end)
  118. on_component("main", function (package, component)
  119. if package:is_plat("windows", "mingw") then
  120. local main_module = "sfml-main"
  121. if package:debug() then
  122. main_module = main_module .. "-d"
  123. end
  124. component:add("links", main_module)
  125. end
  126. end)
  127. on_load("windows", "linux", "macosx", "mingw", function (package)
  128. if package:is_plat("windows", "linux", "macosx") then
  129. package:add("deps", "cmake")
  130. end
  131. if not package:config("shared") then
  132. package:add("defines", "SFML_STATIC")
  133. end
  134. if package:config("graphics") then
  135. package:add("deps", "freetype")
  136. end
  137. if package:is_plat("linux") then
  138. if package:config("window") or package:config("graphics") then
  139. package:add("deps", "libx11", "libxcursor", "libxrandr", "libxrender", "libxfixes", "libxext", "eudev", "libxi")
  140. package:add("deps", "opengl", "glx", {optional = true})
  141. end
  142. end
  143. if package:config("audio") then
  144. package:add("deps", "libogg", "libflac", "libvorbis")
  145. if not (package:gitref() or package:version():ge("3.0.0")) then
  146. package:add("deps", "openal-soft")
  147. end
  148. end
  149. package:add("components", "system")
  150. for _, component in ipairs({"graphics", "window", "audio", "network"}) do
  151. if package:config(component) then
  152. package:add("components", component)
  153. end
  154. end
  155. if package:is_plat("windows", "mingw") and package:config("main") then
  156. package:add("components", "main")
  157. end
  158. end)
  159. on_install("windows", "linux", "macosx", "mingw", function (package)
  160. local configs = {"-DSFML_BUILD_DOC=OFF", "-DSFML_BUILD_EXAMPLES=OFF"}
  161. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  162. if package:config("shared") then
  163. table.insert(configs, "-DBUILD_SHARED_LIBS=ON")
  164. -- Fix missing system libs
  165. if package:config("audio") and not (package:gitref() or package:version():ge("3.0.0")) then
  166. if package:is_plat("windows", "mingw") then
  167. local file = io.open("src/SFML/Audio/CMakeLists.txt", "a")
  168. file:print("target_link_libraries(OpenAL INTERFACE winmm)")
  169. file:close()
  170. end
  171. end
  172. if package:config("graphics") then
  173. local freetype = package:dep("freetype")
  174. if freetype then
  175. local fetchinfo = freetype:fetch()
  176. if fetchinfo then
  177. if not freetype:config("shared") then
  178. local libfiles = {}
  179. for _, dep in ipairs(freetype:librarydeps()) do
  180. local depinfo = dep:fetch()
  181. if depinfo then
  182. table.join2(libfiles, depinfo.libfiles)
  183. end
  184. end
  185. if #libfiles > 0 then
  186. local libraries = {}
  187. for _, libfile in ipairs(libfiles) do
  188. table.insert(libraries, (libfile:gsub("\\", "/")))
  189. end
  190. local lib_name = (package:gitref() or package:version():ge("3.0.0")) and "freetype" or "Freetype"
  191. local file = io.open("src/SFML/Graphics/CMakeLists.txt", "a")
  192. file:print("target_link_libraries(%s INTERFACE %s)", lib_name, table.concat(libraries, " "))
  193. file:close()
  194. end
  195. end
  196. end
  197. end
  198. end
  199. if package:config("window") and package:is_plat("linux") then
  200. local libfiles = {}
  201. for _, name in ipairs({"libx11", "libxcursor", "libxrandr", "libxrender", "libxfixes", "libxext"}) do
  202. local dep = package:dep(name)
  203. if dep then
  204. local fetchinfo = dep:fetch()
  205. if fetchinfo then
  206. table.join2(libfiles, fetchinfo.libfiles)
  207. end
  208. end
  209. end
  210. if #libfiles > 0 then
  211. libfiles = table.reverse_unique(libfiles)
  212. local libraries = {}
  213. for _, libfile in ipairs(libfiles) do
  214. table.insert(libraries, (libfile:gsub("\\", "/")))
  215. end
  216. local file = io.open("src/SFML/Window/CMakeLists.txt", "a")
  217. file:print("target_link_libraries(sfml-window PRIVATE " .. table.concat(libraries, " ") .. ")")
  218. file:close()
  219. end
  220. end
  221. else
  222. table.insert(configs, "-DBUILD_SHARED_LIBS=OFF")
  223. -- SFML overrides MSVC runtimes if set to ON, xmake already takes cares of setting it
  224. table.insert(configs, "-DSFML_USE_STATIC_STD_LIBS=OFF")
  225. end
  226. table.insert(configs, "-DSFML_BUILD_AUDIO=" .. (package:config("audio") and "ON" or "OFF"))
  227. table.insert(configs, "-DSFML_BUILD_GRAPHICS=" .. (package:config("graphics") and "ON" or "OFF"))
  228. table.insert(configs, "-DSFML_BUILD_WINDOW=" .. (package:config("window") and "ON" or "OFF"))
  229. table.insert(configs, "-DSFML_BUILD_NETWORK=" .. (package:config("network") and "ON" or "OFF"))
  230. table.insert(configs, "-DWARNINGS_AS_ERRORS=OFF")
  231. if not is_plat("mingw") then
  232. table.insert(configs, "-DSFML_USE_SYSTEM_DEPS=TRUE")
  233. end
  234. if package:gitref() or package:version():ge("3.0.0") then
  235. table.insert(configs, "-DCMAKE_CXX_STANDARD=17")
  236. end
  237. local packagedeps
  238. if package:config("audio") and not (package:gitref() or package:version():ge("3.0.0")) then
  239. packagedeps = packagedeps or {}
  240. table.insert(packagedeps, "openal-soft")
  241. end
  242. if package:config("graphics") then
  243. packagedeps = packagedeps or {}
  244. table.insert(packagedeps, "freetype")
  245. table.insert(packagedeps, "zlib")
  246. end
  247. import("package.tools.cmake").install(package, configs, {packagedeps = packagedeps})
  248. end)
  249. on_test(function (package)
  250. local configs = (package:gitref() or package:version():ge("3.0.0")) and {languages = "c++17"} or {}
  251. assert(package:check_cxxsnippets({test = [[
  252. void test(int args, char** argv) {
  253. sf::Clock c;
  254. c.restart();
  255. }
  256. ]]}, {configs = configs, includes = "SFML/System.hpp"}))
  257. if package:config("graphics") then
  258. assert(package:check_cxxsnippets({test = [[
  259. void test(const sf::Texture& texture, const sf::Color& color) {
  260. sf::Sprite sprite(texture);
  261. sprite.setColor(color);
  262. }
  263. ]]}, {configs = configs, includes = "SFML/Graphics.hpp"}))
  264. end
  265. if package:config("window") or package:config("graphics") then
  266. assert(package:check_cxxsnippets({test = [[
  267. void test(int args, char** argv) {
  268. sf::Window window(sf::VideoMode(), "Title");
  269. window.close();
  270. }
  271. ]]}, {configs = configs, includes = "SFML/Window.hpp"}))
  272. end
  273. if package:config("audio") then
  274. assert(package:check_cxxsnippets({test = [[
  275. void test(int args, char** argv) {
  276. sf::Music music;
  277. auto res = music.openFromFile("music.ogg");
  278. music.play();
  279. }
  280. ]]}, {configs = configs, includes = "SFML/Audio.hpp"}))
  281. end
  282. if package:config("network") then
  283. assert(package:check_cxxsnippets({test = [[
  284. void test(sf::UdpSocket& socket, const sf::IpAddress& remoteAddress) {
  285. char data[100];
  286. unsigned short remotePort = 54000;
  287. auto status = socket.send(data, 100, remoteAddress, remotePort);
  288. }
  289. ]]}, {configs = configs, includes = "SFML/Network.hpp"}))
  290. end
  291. end)