xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.git")
  6. add_urls("https://github.com/protocolbuffers/protobuf/releases/download/v$(version)", {version = function (version)
  7. if version:le("3.19.4") then
  8. return version .. "/protobuf-cpp-" .. version .. ".zip"
  9. else
  10. return version .. "/protobuf-" .. version .. ".zip"
  11. end
  12. end})
  13. -- TODO: Use x.y.z version? https://protobuf.dev/support/version-support
  14. add_versions("33.2", "d0c6246dc7817d26e809cae88f69b019a92827096811c85e65d3d01102974710")
  15. add_versions("33.1", "801c7d44b2ec2ffaaf94555eda48a2239ef21e6602808ca8d22a9005fc2c03ef")
  16. add_versions("32.1", "2d25be4d5bf3bf28a97de553ae76c49f2a6fa3c21b04d3ccd5b3e0abc9262d00")
  17. add_versions("32.0", "62b6b80ab91a5379c03533bd6d59b6932b4385f0b0dd5299990bbe5e2cc6c428")
  18. add_versions("31.1", "554e847e46c705bfc44fb2d0ae5bf78f34395fcbfd86ba747338b570eef26771")
  19. add_versions("31.0", "3fea4fad0fd2d89e0e79937bc4b3083d483d7e5bc5fec2b8a4158916cd9478dd")
  20. add_versions("30.2", "6544e5ceec7f29d00397193360435ca8b3c4e843de3cf5698a99d36b72d65342")
  21. add_versions("29.3", "e9b9ac1910b1041065839850603caf36e29d3d3d230ddf52bd13778dd31b9046")
  22. add_versions("29.2", "60c1ab4befe9d0a975c2344b5511bf6b44f91ec3e1426c878f56bf30a0589c43")
  23. add_versions("28.1", "0ac35978514f3e868181ea60237e695d892d4748ac03fb926a26ac7e2698aa29")
  24. add_versions("28.0", "979027233837dceaf927402e789261e46d4ff87ce45b3e38be8b15c4a1f696a3")
  25. add_versions("27.3", "a49147217f69e8d19aab0cc5c0059d6201261f5cb62145f8ab4ac8b94e7ffa86")
  26. add_versions("27.2", "7b4554f730a41f5c595cef3502038a69b8954c30d8ec9c62a167d5e1ebd8c210")
  27. add_versions("27.0", "3e1148db090ff21226c1888ef39fa7bc7790042be21ff4289fd21ce1735f3455")
  28. add_versions("26.1", "e15c272392df84ae95797759c685a9225fe5e88838bab3e0650c29239bdfccdd")
  29. add_versions("3.8.0", "91ea92a8c37825bd502d96af9054064694899c5c7ecea21b8d11b1b5e7e993b5")
  30. add_versions("3.11.2", "f5cd6f514b80ea3c324b2c83da829b1424b9fd8fe36d468b6e44cd8f3da0b0ff")
  31. add_versions("3.12.0", "da826a3c48a9cae879928202d6fe06afb15aaee129e9035d6510cc776ddfa925")
  32. add_versions("3.12.3", "74da289e0d0c24b2cb097f30fdc09fa30754175fd5ebb34fae4032c6d95d4ce3")
  33. add_versions("3.13.0", "f7b99f47822b0363175a6751ab59ccaa4ee980bf1198f11a4c3cef162698dde3")
  34. add_versions("3.14.0", "87d6e96166cf5cafc16f2bcfa91c0b54f48bab38538285bee1b9331d992569fa")
  35. add_versions("3.15.5", "cdd7d3925240af541a95a4361ab100b703bee3a9df0d7e9e05c069cf2c76a039")
  36. add_versions("3.15.8", "093e0dca5277b377c36a48a3633325dca3d92d68ac17d5700a1f7e1c3eca2793")
  37. add_versions("3.17.3", "fe65f4bfbd6cbb8c23de052f218cbe4ebfeb72c630847e0cca63eb27616c952a")
  38. add_versions("3.19.4", "a11a262a395f999f9dca83e195cc15b6c23b6d5e74133f8e3250ad0950485da1")
  39. add_patches("3.11.2", "patches/3.11.2/ndk-link-log.diff", "5564ae57562a2d6262e0837afd9645a6be2d4b52a52b7212fa6452f11f50af4a")
  40. add_patches("3.17.3", "patches/3.17.3/field_access_listener.patch", "ac9bdf49611b01e563fe74b2aaf1398214129454c3e18f1198245549eb281e85")
  41. add_patches("3.19.4", "patches/3.19.4/vs_runtime.patch", "8e73e585d29f3b9dca3c279df0b11b3ee7651728c07f51381a69e5899b93c367")
  42. -- https://github.com/msys2/MINGW-packages/blob/e77de8e92025175ffa0a217c3444249aa6f8f4a9/mingw-w64-protobuf/0004-fix-build-with-gcc-15.patch#L7
  43. add_patches(">=31.0<32.0", "patches/31.0/gcc15.patch", "6475e824fabf7835f77e0410830c80b23e4c7a71fa5d7f4867ee7235942b167f")
  44. add_configs("rtti", {description = "Enable runtime type information", default = true, type = "boolean"})
  45. add_configs("zlib", {description = "Enable zlib", default = false, type = "boolean"})
  46. add_configs("lite", {description = "Build lite version", default = true, type = "boolean", readonly = true})
  47. add_configs("upb", {description = "Build upb", default = not is_plat("android"), type = "boolean"})
  48. add_configs("tools", {description = "Build libprotoc and protoc compiler", default = not is_plat("android"), type = "boolean"})
  49. if is_plat("mingw") and is_subhost("msys") then
  50. add_extsources("pacman::protobuf")
  51. elseif is_plat("linux") then
  52. add_extsources("pacman::protobuf")
  53. elseif is_plat("macosx") then
  54. add_extsources("brew::protobuf")
  55. end
  56. add_deps("cmake")
  57. add_components("protobuf", "protoc", "utf8_range")
  58. on_load(function (package)
  59. -- Fix MSVC 2019 arm64 error LNK2019: unresolved external symbol __popcnt referenced in function _upb_log2_table_size
  60. if package:version() and package:version():eq("31.0") and package:is_plat("windows") then
  61. local msvc = package:toolchain("msvc")
  62. local vs = msvc:config("vs")
  63. if vs and tonumber(vs) < 2022 and package:is_arch("arm64") then
  64. package:add("patches", "31.0", path.join(os.scriptdir(), "patches", "31.0", "msvc2019-arm64.patch"), "3b3fa6e7936df5f10da1bb0303060736a40d55e55055f7bc3b376d7a697c093d")
  65. end
  66. end
  67. if package:is_plat("android") and is_host("windows") then
  68. package:add("deps", "ninja")
  69. package:set("policy", "package.cmake_generator.ninja", true)
  70. end
  71. if package:is_plat("linux", "bsd", "mingw") then
  72. package:add("syslinks", "m", "pthread")
  73. end
  74. if package:is_plat("linux") then
  75. if package:is_binary() then
  76. package:add("extsources", "apt::protobuf-compiler")
  77. elseif package:is_library() then
  78. package:add("extsources", "apt::libprotobuf-dev", "apt::libprotoc-dev")
  79. end
  80. end
  81. if not package:is_cross() then
  82. package:addenv("PATH", "bin")
  83. end
  84. if package:config("zlib") then
  85. package:add("deps", "zlib")
  86. end
  87. if package:version():ge("22.0") then
  88. if package:version():lt("30.0") then
  89. package:add("deps", "abseil <=20250127.0")
  90. else
  91. package:add("deps", "abseil")
  92. end
  93. end
  94. if package:is_plat("windows") and package:config("shared") then
  95. package:add("defines", "PROTOBUF_USE_DLLS")
  96. end
  97. if package:config("upb") then
  98. package:add("components", "upb")
  99. end
  100. if package:config("lite") then
  101. package:add("components", "protobuf-lite")
  102. end
  103. end)
  104. -- ref: https://github.com/conan-io/conan-center-index/blob/19c9de61cce5a5089ce42b0cf15a88ade7763275/recipes/protobuf/all/conanfile.py
  105. on_component("utf8_range", function (package, component)
  106. component:add("extsources", "pkgconfig::utf8_range")
  107. if package:is_plat("windows") then
  108. component:add("links", "libutf8_range", "libutf8_validity")
  109. end
  110. component:add("links", "utf8_validity", "utf8_range")
  111. end)
  112. on_component("protobuf", function (package, component)
  113. component:add("extsources", "pkgconfig::protobuf")
  114. component:add("deps", "utf8_range")
  115. if package:is_plat("windows") then
  116. component:add("links", "libprotobuf")
  117. else
  118. component:add("links", "protobuf")
  119. end
  120. end)
  121. on_component("protobuf-lite", function (package, component)
  122. component:add("extsources", "pkgconfig::protobuf-lite")
  123. if package:is_plat("windows") then
  124. component:add("links", "libprotobuf-lite", "utf8_validity")
  125. else
  126. component:add("links", "protobuf-lite", "utf8_validity")
  127. end
  128. end)
  129. on_component("protoc", function (package, component)
  130. component:add("deps", "protobuf")
  131. if package:is_plat("windows") then
  132. component:add("links", "libprotoc")
  133. else
  134. component:add("links", "protoc")
  135. end
  136. end)
  137. on_component("upb", function (package, component)
  138. if package:is_plat("windows") then
  139. component:add("links", "libupb", "utf8_range")
  140. else
  141. component:add("links", "upb", "utf8_range")
  142. end
  143. end)
  144. on_install(function (package)
  145. if package:is_plat("windows", "mingw") then
  146. io.replace("src/google/protobuf/port_def.inc", "#define PROTOBUF_DESCRIPTOR_WEAK_MESSAGES_ALLOWED", "", {plain = true})
  147. end
  148. local version = package:version()
  149. if version:le("3.19.4") then
  150. os.cd("cmake")
  151. end
  152. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)", "", {plain = true})
  153. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)", "", {plain = true})
  154. io.replace("CMakeLists.txt", "set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)", "", {plain = true})
  155. if version:ge("26.1") then
  156. io.replace("cmake/abseil-cpp.cmake", "BUILD_SHARED_LIBS AND MSVC", "FALSE", {plain = true})
  157. end
  158. local configs = {
  159. "-DCMAKE_POLICY_DEFAULT_CMP0057=NEW",
  160. "-Dprotobuf_BUILD_TESTS=OFF",
  161. "-Dprotobuf_LOCAL_DEPENDENCIES_ONLY=ON",
  162. "-Dprotobuf_DEBUG_POSTFIX=''",
  163. }
  164. table.insert(configs, "-Dprotobuf_BUILD_PROTOC_BINARIES=" .. (package:config("tools") and "ON" or "OFF"))
  165. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  166. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  167. table.insert(configs, "-Dprotobuf_DISABLE_RTTI=" .. (package:config("rtti") and "OFF" or "ON"))
  168. if package:is_plat("windows") then
  169. table.insert(configs, "-Dprotobuf_MSVC_STATIC_RUNTIME=" .. (package:has_runtime("MT", "MTd") and "ON" or "OFF"))
  170. end
  171. if package:dep("abseil") then
  172. local std = package:dep("abseil"):config("cxx_standard")
  173. table.insert(configs, "-DCMAKE_CXX_STANDARD=" .. std)
  174. package:data_set("cxx_standard", std)
  175. end
  176. table.insert(configs, "-Dprotobuf_WITH_ZLIB=" .. (package:config("zlib") and "ON" or "OFF"))
  177. table.insert(configs, "-Dprotobuf_BUILD_LIBUPB=" .. (package:config("upb") and "ON" or "OFF"))
  178. local opt = {}
  179. opt.buildir = "build"
  180. if version:ge("22.0") then
  181. opt.packagedeps = "abseil"
  182. table.insert(configs, "-Dprotobuf_ABSL_PROVIDER=package")
  183. end
  184. import("package.tools.cmake").install(package, configs, opt)
  185. if package:is_cross() then
  186. os.tryrm(package:installdir("bin/*.exe"))
  187. else
  188. os.trycp("build/Release/protoc.exe", package:installdir("bin"))
  189. end
  190. end)
  191. on_test(function (package)
  192. if package:config("tools") and not package:is_cross() and
  193. -- Missing libgcc_s_xxx.dll, Maybe msys2 bug
  194. not (is_subhost("msys") and package:is_plat("mingw", "msys") and package:is_arch("i386")) then
  195. io.writefile("test.proto", [[
  196. syntax = "proto3";
  197. package test;
  198. message TestCase {
  199. string name = 4;
  200. }
  201. message Test {
  202. repeated TestCase case = 1;
  203. }
  204. ]])
  205. os.vrun("protoc test.proto --cpp_out=.")
  206. end
  207. local std = package:data("cxx_standard")
  208. local languages = "c++" .. (std and std or "17")
  209. if package:is_library() then
  210. assert(package:check_cxxsnippets({test = [[
  211. #include <google/protobuf/timestamp.pb.h>
  212. #include <google/protobuf/util/time_util.h>
  213. void test() {
  214. google::protobuf::Timestamp ts;
  215. google::protobuf::util::TimeUtil::FromString("1972-01-01T10:00:20.021Z", &ts);
  216. }
  217. ]]}, {configs = {languages = languages}}))
  218. end
  219. end)