xmake.lua 14 KB

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