xmake.lua 5.6 KB

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