xmake.lua 13 KB

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