xmake.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package("opencc")
  2. set_homepage("https://github.com/BYVoid/OpenCC")
  3. set_description("Conversion between Traditional and Simplified Chinese.")
  4. set_license("Apache-2.0")
  5. add_urls("https://github.com/BYVoid/OpenCC/archive/refs/tags/ver.$(version).tar.gz")
  6. add_urls("https://github.com/BYVoid/OpenCC.git", {alias = "git"})
  7. add_versions("1.1.9", "ad4bcd8d87219a240a236d4a55c9decd2132a9436697d2882ead85c8939b0a99")
  8. add_versions("git:1.1.9", "ver.1.1.9")
  9. add_configs("darts", {description = "Build DartsDict (ocd format)", default = true, type = "boolean"})
  10. add_deps("cmake", "python 3.x", {kind = "binary"})
  11. if is_subhost("windows") then
  12. add_deps("pkgconf")
  13. else
  14. add_deps("pkg-config")
  15. end
  16. add_deps("marisa v0.2.6", "rapidjson", "tclap")
  17. on_load(function (package)
  18. if package:config("darts") then
  19. package:add("deps", "darts")
  20. package:add("defines", "ENABLE_DARTS")
  21. end
  22. if package:is_cross() then
  23. -- use host opencc_dict for cross build
  24. package:add("deps", "opencc~host", {kind = "binary", host = true})
  25. else
  26. package:addenv("PATH", "bin")
  27. end
  28. if not package:config("shared") then
  29. package:add("defines", "Opencc_BUILT_AS_STATIC")
  30. end
  31. end)
  32. on_install(function (package)
  33. -- If system have node, cmake will use node to run opencc_dict.js and failed to build
  34. if package:is_cross() then
  35. io.replace("data/CMakeLists.txt",
  36. "COMMAND\n ${OPENCC_DICT_BIN}",
  37. format("COMMAND\n %s/bin/opencc_dict", path.unix(package:dep("opencc"):installdir())), {plain = true})
  38. end
  39. io.replace("src/SerializedValues.hpp", "#pragma once", "#pragma once\n#include <cstdint>", {plain = true}) -- fix gcc15
  40. io.replace("CMakeLists.txt", "-pthread", "", {plain = true}) -- break opencc tool on wasm
  41. io.replace("src/CMakeLists.txt", "set_target_properties(libopencc PROPERTIES POSITION_INDEPENDENT_CODE ON)", "", {plain = true})
  42. io.replace("src/CMakeLists.txt", "target_link_libraries(libopencc marisa)", "", {plain = true})
  43. local file = io.open("src/CMakeLists.txt", "a")
  44. if file then
  45. file:write([[
  46. if (USE_SYSTEM_MARISA)
  47. include(FindPkgConfig)
  48. pkg_search_module(marisa REQUIRED IMPORTED_TARGET marisa)
  49. target_link_libraries(libopencc PUBLIC PkgConfig::marisa)
  50. endif()
  51. if (USE_SYSTEM_RAPIDJSON)
  52. find_package(RapidJSON CONFIG REQUIRED)
  53. target_link_libraries(libopencc PUBLIC rapidjson)
  54. endif()
  55. if (USE_SYSTEM_TCLAP)
  56. include(FindPkgConfig)
  57. pkg_search_module(tclap REQUIRED IMPORTED_TARGET tclap)
  58. target_link_libraries(libopencc PUBLIC PkgConfig::tclap)
  59. endif()
  60. if (USE_SYSTEM_DARTS)
  61. include(FindPkgConfig)
  62. pkg_search_module(darts REQUIRED IMPORTED_TARGET darts)
  63. target_link_libraries(libopencc PUBLIC PkgConfig::darts)
  64. endif()
  65. ]])
  66. file:close()
  67. end
  68. local configs = {
  69. "-DBUILD_DOCUMENTATION=OFF",
  70. "-DENABLE_GTEST=OFF",
  71. "-DENABLE_BENCHMARK=OFF",
  72. "-DBUILD_PYTHON=OFF",
  73. "-DUSE_SYSTEM_DARTS=ON",
  74. "-DUSE_SYSTEM_RAPIDJSON=ON",
  75. "-DUSE_SYSTEM_TCLAP=ON",
  76. "-DUSE_SYSTEM_MARISA=ON",
  77. }
  78. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  79. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  80. table.insert(configs, "-DENABLE_DARTS=" .. (package:config("darts") and "ON" or "OFF"))
  81. import("package.tools.cmake").install(package, configs)
  82. end)
  83. on_test(function (package)
  84. if not package:is_cross() then
  85. os.vrun("opencc_dict --version")
  86. end
  87. if package:is_library() then
  88. assert(package:has_cfuncs("opencc_open", {includes = "opencc/opencc.h"}))
  89. assert(package:check_cxxsnippets({test = [[
  90. void test() {
  91. opencc::Config config;
  92. }
  93. ]]}, {includes = {"opencc/Config.hpp"}}))
  94. end
  95. end)