xmake.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package("libsdl2_ttf")
  2. set_homepage("https://github.com/libsdl-org/SDL_ttf/")
  3. set_description("Simple DirectMedia Layer text rendering library")
  4. set_license("zlib")
  5. if is_plat("mingw") and is_subhost("msys") then
  6. add_extsources("pacman::SDL2_ttf")
  7. elseif is_plat("linux") then
  8. add_extsources("pacman::sdl2_ttf", "apt::libsdl2-ttf-dev")
  9. elseif is_plat("macosx") then
  10. add_extsources("brew::sdl2_ttf")
  11. end
  12. add_urls("https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-$(version).zip",
  13. "https://github.com/libsdl-org/SDL_ttf/releases/download/release-$(version)/SDL2_ttf-$(version).zip")
  14. add_versions("2.24.0", "bef50614acb63347fe1612facabb31b0f1a05ca2b5c271a619c26fdae561a1c9")
  15. add_versions("2.20.0", "04e94fc5ecac3475ab35c1d5cf52650df691867e7e4befcc861bf982a747111a")
  16. add_versions("2.20.1", "18d81ab399c8e39adababe8918691830ba6e0d6448e5baa141ee0ddf87ede2dc")
  17. add_versions("2.20.2", "aa6256bfcffd8381a75b3a2a2384ac12109b5b148e72722a19b0ede54c4051dc")
  18. add_versions("2.22.0", "5766070145111cb047807fa3f91c2c6b81bc1008d63817e100417959b42a2484")
  19. add_patches(">=2.20.0 <=2.20.1", path.join(os.scriptdir(), "patches", "2.20.1", "cmakelists.patch"), "fe04ada62d9ed70029c0efb3c04bfec22fc7596bd6b73a567beb964e61ebd82c")
  20. add_deps("cmake", "freetype")
  21. add_includedirs("include", "include/SDL2")
  22. if is_plat("wasm") then
  23. add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
  24. end
  25. on_load(function (package)
  26. package:add("deps", "libsdl2", { configs = { shared = package:config("shared") }})
  27. end)
  28. on_install(function (package)
  29. if package:is_plat("wasm") then
  30. io.replace("CMakeLists.txt", "sdl_find_sdl2(${sdl2_target_name} ${SDL_REQUIRED_VERSION})", "", {plain = true})
  31. io.replace("CMakeLists.txt", "target_link_libraries(SDL2_ttf PRIVATE $<BUILD_INTERFACE:${sdl2_target_name}>)", [[
  32. target_include_directories(SDL2_ttf PRIVATE ${SDL2_INCLUDE_DIR})
  33. target_link_libraries(SDL2_ttf PRIVATE $<BUILD_INTERFACE:${SDL2_LIBRARY}>)
  34. ]], {plain = true})
  35. io.replace("CMakeLists.txt", "target_link_libraries(SDL2_ttf PRIVATE ${sdl2_target_name})", [[
  36. target_include_directories(SDL2_ttf PRIVATE ${SDL2_INCLUDE_DIR})
  37. target_link_libraries(SDL2_ttf PRIVATE ${SDL2_LIBRARY})
  38. ]], {plain = true})
  39. end
  40. local configs = {"-DSDL2TTF_SAMPLES=OFF"}
  41. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  42. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  43. table.insert(configs, "-DSDL2TTF_VENDORED=OFF")
  44. local libsdl2 = package:dep("libsdl2")
  45. if libsdl2 and not libsdl2:is_system() then
  46. table.insert(configs, "-DSDL2_DIR=" .. libsdl2:installdir())
  47. local fetchinfo = libsdl2:fetch()
  48. if fetchinfo then
  49. for _, dir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
  50. if os.isfile(path.join(dir, "SDL_version.h")) then
  51. table.insert(configs, "-DSDL2_INCLUDE_DIR=" .. dir)
  52. break
  53. end
  54. end
  55. local libfiles = {}
  56. for _, libfile in ipairs(fetchinfo.libfiles) do
  57. if libfile:match("SDL2%..+$") or libfile:match("SDL2-static%..+$") then
  58. if not (package:config("shared") and libfile:endswith(".dll")) then
  59. table.insert(libfiles, libfile)
  60. end
  61. end
  62. end
  63. table.insert(configs, "-DSDL2_LIBRARY=" .. table.concat(libfiles, ";"))
  64. end
  65. end
  66. local freetype = package:dep("freetype")
  67. if freetype then
  68. local fetchinfo = freetype:fetch()
  69. if fetchinfo then
  70. local includedirs = table.wrap(fetchinfo.includedirs or fetchinfo.sysincludedirs)
  71. if #includedirs > 0 then
  72. table.insert(configs, "-DFREETYPE_INCLUDE_DIRS=" .. table.concat(includedirs, ";"))
  73. end
  74. local libfiles = table.wrap(fetchinfo.libfiles)
  75. if #libfiles > 0 then
  76. table.insert(configs, "-DFREETYPE_LIBRARY=" .. libfiles[1])
  77. end
  78. if not freetype:config("shared") then
  79. local libfiles = {}
  80. for _, dep in ipairs(freetype:librarydeps()) do
  81. local depinfo = dep:fetch()
  82. if depinfo then
  83. table.join2(libfiles, depinfo.libfiles)
  84. end
  85. end
  86. if #libfiles > 0 then
  87. local libraries = ""
  88. for _, libfile in ipairs(libfiles) do
  89. libraries = libraries .. " " .. (libfile:gsub("\\", "/"))
  90. end
  91. io.replace("CMakeLists.txt", "target_link_libraries(SDL2_ttf PRIVATE Freetype::Freetype)",
  92. "target_link_libraries(SDL2_ttf PRIVATE Freetype::Freetype " .. libraries .. ")", {plain = true})
  93. end
  94. end
  95. end
  96. end
  97. import("package.tools.cmake").install(package, configs)
  98. end)
  99. on_test(function (package)
  100. assert(package:check_cxxsnippets({test = [[
  101. #include <SDL2/SDL.h>
  102. #include <SDL2/SDL_ttf.h>
  103. int main(int argc, char** argv) {
  104. TTF_Init();
  105. TTF_Quit();
  106. return 0;
  107. }
  108. ]]}, {configs = {defines = "SDL_MAIN_HANDLED"}}));
  109. end)