2
0

xmake.lua 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package("libsdl2_net")
  2. set_homepage("https://www.libsdl.org/projects/SDL_net/")
  3. set_description("Simple DirectMedia Layer networking library")
  4. set_license("zlib")
  5. add_urls("https://www.libsdl.org/projects/SDL_net/release/SDL2_net-$(version).zip",
  6. "https://github.com/libsdl-org/SDL_net/releases/download/release-$(version)/SDL2_net-$(version).zip")
  7. add_versions("2.2.0", "1eec3a9d43df019d7916a6ecce32f2a3ad5248c82c9c237948afc712399be36d")
  8. if is_plat("mingw") and is_subhost("msys") then
  9. add_extsources("pacman::SDL2_net")
  10. elseif is_plat("linux") then
  11. add_extsources("pacman::sdl2_net", "apt::libsdl2-net-dev")
  12. elseif is_plat("macosx") then
  13. add_extsources("brew::sdl2_net")
  14. end
  15. add_deps("cmake")
  16. if is_plat("windows", "mingw") then
  17. add_syslinks("iphlpapi", "ws2_32")
  18. end
  19. if is_plat("wasm") then
  20. add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
  21. end
  22. add_includedirs("include", "include/SDL2")
  23. on_load(function (package)
  24. package:add("deps", "libsdl2", { configs = { shared = package:config("shared") }})
  25. end)
  26. on_install(function (package)
  27. if package:is_plat("wasm") then
  28. io.replace("CMakeLists.txt", "sdl_find_sdl2(${sdl2_target_name} ${SDL_REQUIRED_VERSION})", "", {plain = true})
  29. io.replace("CMakeLists.txt", "target_link_libraries(SDL2_net PRIVATE $<BUILD_INTERFACE:${sdl2_target_name}>)", [[
  30. target_include_directories(SDL2_net PRIVATE ${SDL2_INCLUDE_DIR})
  31. target_link_libraries(SDL2_net PRIVATE $<BUILD_INTERFACE:${SDL2_LIBRARY}>)
  32. ]], {plain = true})
  33. io.replace("CMakeLists.txt", "target_link_libraries(SDL2_net PRIVATE ${sdl2_target_name})", [[
  34. target_include_directories(SDL2_net PRIVATE ${SDL2_INCLUDE_DIR})
  35. target_link_libraries(SDL2_net PRIVATE ${SDL2_LIBRARY})
  36. ]], {plain = true})
  37. end
  38. local configs = {"-DSDL2NET_SAMPLES=OFF"}
  39. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  40. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  41. local libsdl2 = package:dep("libsdl2")
  42. if libsdl2 and not libsdl2:is_system() then
  43. table.insert(configs, "-DSDL2_DIR=" .. libsdl2:installdir())
  44. local fetchinfo = libsdl2:fetch()
  45. if fetchinfo then
  46. for _, dir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
  47. if os.isfile(path.join(dir, "SDL_version.h")) then
  48. table.insert(configs, "-DSDL2_INCLUDE_DIR=" .. dir)
  49. break
  50. end
  51. end
  52. local libfiles = {}
  53. for _, libfile in ipairs(fetchinfo.libfiles) do
  54. if libfile:match("SDL2%..+$") or libfile:match("SDL2-static%..+$") then
  55. if not (package:config("shared") and libfile:endswith(".dll")) then
  56. table.insert(libfiles, libfile)
  57. end
  58. end
  59. end
  60. table.insert(configs, "-DSDL2_LIBRARY=" .. table.concat(libfiles, ";"))
  61. end
  62. end
  63. io.replace("CMakeLists.txt", "find_package(SDL2test)", "", {plain = true})
  64. import("package.tools.cmake").install(package, configs)
  65. end)
  66. on_test(function (package)
  67. assert(package:check_cxxsnippets({test = [[
  68. #include <SDL2/SDL.h>
  69. #include <SDL2/SDL_net.h>
  70. int main(int argc, char** argv) {
  71. SDLNet_Init();
  72. SDLNet_Quit();
  73. return 0;
  74. }
  75. ]]}, {configs = {defines = "SDL_MAIN_HANDLED"}}));
  76. end)