xmake.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package("protobuf-cpp")
  2. set_homepage("https://developers.google.com/protocol-buffers/")
  3. set_description("Google's data interchange format for cpp")
  4. set_license("BSD-3-Clause")
  5. add_urls("https://github.com/protocolbuffers/protobuf/releases/download/v$(version)", {version = function (version)
  6. if version:le("3.19.4") then
  7. return version .. "/protobuf-cpp-" .. version .. ".zip"
  8. else
  9. return version .. "/protobuf-" .. version .. ".zip"
  10. end
  11. end})
  12. -- TODO: Use x.y.z version? https://protobuf.dev/support/version-support
  13. add_versions("28.1", "0ac35978514f3e868181ea60237e695d892d4748ac03fb926a26ac7e2698aa29")
  14. add_versions("28.0", "979027233837dceaf927402e789261e46d4ff87ce45b3e38be8b15c4a1f696a3")
  15. add_versions("27.3", "a49147217f69e8d19aab0cc5c0059d6201261f5cb62145f8ab4ac8b94e7ffa86")
  16. add_versions("27.2", "7b4554f730a41f5c595cef3502038a69b8954c30d8ec9c62a167d5e1ebd8c210")
  17. add_versions("27.0", "3e1148db090ff21226c1888ef39fa7bc7790042be21ff4289fd21ce1735f3455")
  18. add_versions("26.1", "e15c272392df84ae95797759c685a9225fe5e88838bab3e0650c29239bdfccdd")
  19. add_versions("3.8.0", "91ea92a8c37825bd502d96af9054064694899c5c7ecea21b8d11b1b5e7e993b5")
  20. add_versions("3.12.0", "da826a3c48a9cae879928202d6fe06afb15aaee129e9035d6510cc776ddfa925")
  21. add_versions("3.12.3", "74da289e0d0c24b2cb097f30fdc09fa30754175fd5ebb34fae4032c6d95d4ce3")
  22. add_versions("3.13.0", "f7b99f47822b0363175a6751ab59ccaa4ee980bf1198f11a4c3cef162698dde3")
  23. add_versions("3.14.0", "87d6e96166cf5cafc16f2bcfa91c0b54f48bab38538285bee1b9331d992569fa")
  24. add_versions("3.15.5", "cdd7d3925240af541a95a4361ab100b703bee3a9df0d7e9e05c069cf2c76a039")
  25. add_versions("3.15.8", "093e0dca5277b377c36a48a3633325dca3d92d68ac17d5700a1f7e1c3eca2793")
  26. add_versions("3.17.3", "fe65f4bfbd6cbb8c23de052f218cbe4ebfeb72c630847e0cca63eb27616c952a")
  27. add_versions("3.19.4", "a11a262a395f999f9dca83e195cc15b6c23b6d5e74133f8e3250ad0950485da1")
  28. add_patches("3.17.3", path.join(os.scriptdir(), "patches", "3.17.3", "field_access_listener.patch"), "ac9bdf49611b01e563fe74b2aaf1398214129454c3e18f1198245549eb281e85")
  29. add_patches("3.19.4", path.join(os.scriptdir(), "patches", "3.19.4", "vs_runtime.patch"), "8e73e585d29f3b9dca3c279df0b11b3ee7651728c07f51381a69e5899b93c367")
  30. add_configs("rtti", {description = "Enable runtime type information", default = true, type = "boolean"})
  31. add_configs("zlib", {description = "Enable zlib", default = false, type = "boolean"})
  32. add_configs("lite", {description = "Build lite version", default = true, type = "boolean", readonly = true})
  33. add_configs("upb", {description = "Build upb", default = false, type = "boolean"})
  34. add_deps("cmake")
  35. if is_plat("android") and is_host("windows") then
  36. add_deps("ninja")
  37. set_policy("package.cmake_generator.ninja", true)
  38. end
  39. if is_plat("windows") then
  40. add_links("libprotoc", "libprotobuf", "utf8_range", "utf8_validity")
  41. else
  42. add_links("protoc", "protobuf", "utf8_range", "utf8_validity")
  43. end
  44. if is_plat("linux", "bsd", "mingw") then
  45. add_syslinks("m", "pthread")
  46. end
  47. on_load(function (package)
  48. package:addenv("PATH", "bin")
  49. if package:config("zlib") then
  50. package:add("deps", "zlib")
  51. end
  52. if package:version():ge("22.0") then
  53. package:add("deps", "abseil")
  54. end
  55. if package:is_plat("windows") and package:config("shared") then
  56. package:add("defines", "PROTOBUF_USE_DLLS")
  57. end
  58. end)
  59. -- ref: https://github.com/conan-io/conan-center-index/blob/19c9de61cce5a5089ce42b0cf15a88ade7763275/recipes/protobuf/all/conanfile.py
  60. on_component("utf8_range", function (package, component)
  61. component:add("extsources", "pkgconfig::utf8_range")
  62. component:add("links", "utf8_validity", "utf8_range")
  63. end)
  64. on_component("protobuf", function (package, component)
  65. component:add("extsources", "pkgconfig::protobuf")
  66. if is_plat("windows") then
  67. component:add("links", "libprotobuf", "utf8_validity")
  68. else
  69. component:add("links", "protobuf", "utf8_validity")
  70. end
  71. end)
  72. on_component("protobuf-lite", function (package, component)
  73. component:add("extsources", "pkgconfig::protobuf-lite")
  74. if is_plat("windows") then
  75. component:add("links", "libprotobuf-lite", "utf8_validity")
  76. else
  77. component:add("links", "protobuf-lite", "utf8_validity")
  78. end
  79. end)
  80. on_component("protoc", function (package, component)
  81. component:add("deps", "protobuf")
  82. if is_plat("windows") then
  83. component:add("links", "libprotoc")
  84. else
  85. component:add("links", "protoc")
  86. end
  87. end)
  88. on_component("upb", function (package, component)
  89. if is_plat("windows") then
  90. component:add("links", "libupb", "utf8_range")
  91. else
  92. component:add("links", "upb", "utf8_range")
  93. end
  94. end)
  95. on_install(function (package)
  96. local version = package:version()
  97. if version:le("3.19.4") then
  98. os.cd("cmake")
  99. end
  100. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)", "", {plain = true})
  101. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)", "", {plain = true})
  102. io.replace("CMakeLists.txt", "set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)", "", {plain = true})
  103. if version:ge("26.1") then
  104. io.replace("cmake/abseil-cpp.cmake", "BUILD_SHARED_LIBS AND MSVC", "FALSE", {plain = true})
  105. end
  106. if package:is_plat("windows", "mingw") then
  107. io.replace("src/google/protobuf/port_def.inc", "#define PROTOBUF_DESCRIPTOR_WEAK_MESSAGES_ALLOWED", "", {plain = true})
  108. end
  109. local configs = {
  110. "-Dprotobuf_BUILD_TESTS=OFF",
  111. "-Dprotobuf_LOCAL_DEPENDENCIES_ONLY=ON",
  112. "-Dprotobuf_BUILD_PROTOC_BINARIES=ON",
  113. "-Dprotobuf_DEBUG_POSTFIX=''",
  114. }
  115. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  116. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  117. table.insert(configs, "-Dprotobuf_DISABLE_RTTI=" .. (package:config("rtti") and "OFF" or "ON"))
  118. if package:is_plat("windows") then
  119. table.insert(configs, "-DCMAKE_COMPILE_PDB_OUTPUT_DIRECTORY=''")
  120. table.insert(configs, "-Dprotobuf_MSVC_STATIC_RUNTIME=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
  121. end
  122. table.insert(configs, "-Dprotobuf_WITH_ZLIB=" .. (package:config("zlib") and "ON" or "OFF"))
  123. table.insert(configs, "-Dprotobuf_BUILD_LIBUPB=" .. (package:config("upb") and "ON" or "OFF"))
  124. local opt = {}
  125. opt.buildir = "build"
  126. if version:ge("22.0") then
  127. opt.packagedeps = "abseil"
  128. table.insert(configs, "-Dprotobuf_ABSL_PROVIDER=package")
  129. end
  130. import("package.tools.cmake").install(package, configs, opt)
  131. os.trycp("build/Release/protoc.exe", package:installdir("bin"))
  132. end)
  133. on_test(function (package)
  134. if not package:is_cross() and
  135. -- Missing libgcc_s_xxx.dll, Maybe msys2 bug
  136. not (is_subhost("msys") and package:is_plat("mingw", "msys") and package:is_arch("i386")) then
  137. io.writefile("test.proto", [[
  138. syntax = "proto3";
  139. package test;
  140. message TestCase {
  141. string name = 4;
  142. }
  143. message Test {
  144. repeated TestCase case = 1;
  145. }
  146. ]])
  147. os.vrun("protoc test.proto --cpp_out=.")
  148. end
  149. if package:is_library() then
  150. assert(package:check_cxxsnippets({test = [[
  151. #include <google/protobuf/timestamp.pb.h>
  152. #include <google/protobuf/util/time_util.h>
  153. void test() {
  154. google::protobuf::Timestamp ts;
  155. google::protobuf::util::TimeUtil::FromString("1972-01-01T10:00:20.021Z", &ts);
  156. }
  157. ]]}, {configs = {languages = "c++17"}}))
  158. end
  159. end)