NetdataProtobuf.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Macros and functions for handling of Protobuf
  2. #
  3. # Copyright (c) 2024 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. # Prepare a vendored copy of Protobuf for use with Netdata.
  6. function(netdata_bundle_protobuf)
  7. include(FetchContent)
  8. include(NetdataFetchContentExtra)
  9. set(PROTOBUF_TAG f0dc78d7e6e331b8c6bb2d5283e06aa26883ca7c) # v21.12
  10. set(NEED_ABSL False)
  11. if(CMAKE_CXX_STANDARD GREATER_EQUAL 14)
  12. set(PROTOBUF_TAG 4a2aef570deb2bfb8927426558701e8bfc26f2a4) # v25.3
  13. set(NEED_ABSL True)
  14. set(ABSL_TAG 2f9e432cce407ce0ae50676696666f33a77d42ac) # 20240116.1
  15. endif()
  16. set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE NEVER)
  17. string(REPLACE "-fsanitize=address" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  18. string(REPLACE "-fsanitize=address" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  19. # ignore debhelper
  20. set(FETCHCONTENT_FULLY_DISCONNECTED Off)
  21. if(NEED_ABSL)
  22. set(ABSL_PROPAGATE_CXX_STD On)
  23. set(ABSL_ENABLE_INSTALL Off)
  24. set(BUILD_SHARED_LIBS Off)
  25. message(STATUS "Preparing bundled Abseil (required by bundled Protobuf)")
  26. FetchContent_Declare(absl
  27. GIT_REPOSITORY https://github.com/abseil/abseil-cpp
  28. GIT_TAG ${ABSL_TAG}
  29. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  30. )
  31. FetchContent_MakeAvailable_NoInstall(absl)
  32. message(STATUS "Finished preparing bundled Abseil")
  33. endif()
  34. set(protobuf_INSTALL Off)
  35. set(protobuf_BUILD_LIBPROTOC Off)
  36. set(protobuf_BUILD_TESTS Off)
  37. set(protobuf_BUILD_SHARED_LIBS Off)
  38. message(STATUS "Preparing bundled Protobuf")
  39. FetchContent_Declare(protobuf
  40. GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
  41. GIT_TAG ${PROTOBUF_TAG}
  42. CMAKE_ARGS ${NETDATA_CMAKE_PROPAGATE_TOOLCHAIN_ARGS}
  43. )
  44. FetchContent_MakeAvailable_NoInstall(protobuf)
  45. message(STATUS "Finished preparing bundled Protobuf.")
  46. set(BUNDLED_PROTOBUF True PARENT_SCOPE)
  47. endfunction()
  48. # Handle detection of Protobuf
  49. macro(netdata_detect_protobuf)
  50. if(OS_WINDOWS)
  51. set(PROTOBUF_PROTOC_EXECUTABLE "$ENV{PROTOBUF_PROTOC_EXECUTABLE}")
  52. if(NOT PROTOBUF_PROTOC_EXECUTABLE)
  53. set(PROTOBUF_PROTOC_EXECUTABLE "/bin/protoc")
  54. endif()
  55. set(PROTOBUF_CFLAGS_OTHER "")
  56. set(PROTOBUF_INCLUDE_DIRS "")
  57. set(PROTOBUF_LIBRARIES "-lprotobuf")
  58. set(ENABLE_PROTOBUF True)
  59. set(HAVE_PROTOBUF True)
  60. else()
  61. if(NOT ENABLE_BUNDLED_PROTOBUF)
  62. if (NOT BUILD_SHARED_LIBS)
  63. set(Protobuf_USE_STATIC_LIBS On)
  64. endif()
  65. # The FindProtobuf CMake module shipped by upstream CMake is
  66. # broken for Protobuf version 22.0 and newer because it does
  67. # not correctly pull in the new Abseil dependencies. Protobuf
  68. # itself sometimes ships a CMake Package Configuration module
  69. # that _does_ work correctly, so use that in preference to the
  70. # Find module shipped with CMake.
  71. #
  72. # The code below works by first attempting to use find_package
  73. # in config mode, and then checking for the existence of the
  74. # target we actually use that gets defined by the protobuf
  75. # CMake Package Configuration Module to determine if that
  76. # worked. A bit of extra logic is required in the case of the
  77. # config mode working, because some systems ship compatibility
  78. # logic for the old FindProtobuf module while others do not.
  79. #
  80. # Upstream bug reference: https://gitlab.kitware.com/cmake/cmake/-/issues/24321
  81. find_package(Protobuf CONFIG)
  82. if(NOT TARGET protobuf::libprotobuf)
  83. message(STATUS "Could not find Protobuf using Config mode, falling back to Module mode")
  84. find_package(Protobuf REQUIRED)
  85. endif()
  86. endif()
  87. if(TARGET protobuf::libprotobuf)
  88. if(NOT Protobuf_PROTOC_EXECUTABLE AND TARGET protobuf::protoc)
  89. set(Protobuf_PROTOC_EXECUTABLE protobuf::protoc)
  90. endif()
  91. # It is technically possible that this may still not
  92. # be set by this point, so we need to check it and
  93. # fail noisily if it isn't because the build won't
  94. # work without it.
  95. if(NOT Protobuf_PROTOC_EXECUTABLE)
  96. message(FATAL_ERROR "Could not determine the location of the protobuf compiler for the detected version of protobuf.")
  97. endif()
  98. set(PROTOBUF_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE})
  99. set(PROTOBUF_LIBRARIES protobuf::libprotobuf)
  100. endif()
  101. set(ENABLE_PROTOBUF True)
  102. set(HAVE_PROTOBUF True)
  103. endif()
  104. endmacro()
  105. # Helper function to compile protocol definitions into C++ code.
  106. function(netdata_protoc_generate_cpp INC_DIR OUT_DIR SRCS HDRS)
  107. if(NOT ARGN)
  108. message(SEND_ERROR "Error: protoc_generate_cpp() called without any proto files")
  109. return()
  110. endif()
  111. set(${INC_DIR})
  112. set(${OUT_DIR})
  113. set(${SRCS})
  114. set(${HDRS})
  115. foreach(FIL ${ARGN})
  116. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  117. get_filename_component(DIR ${ABS_FIL} DIRECTORY)
  118. get_filename_component(FIL_WE ${FIL} NAME_WE)
  119. set(GENERATED_PB_CC "${DIR}/${FIL_WE}.pb.cc")
  120. list(APPEND ${SRCS} ${GENERATED_PB_CC})
  121. set(GENERATED_PB_H "${DIR}/${FIL_WE}.pb.h")
  122. list(APPEND ${HDRS} ${GENERATED_PB_H})
  123. list(APPEND _PROTOC_INCLUDE_DIRS ${INC_DIR})
  124. if(ENABLE_BUNDLED_PROTOBUF)
  125. list(APPEND _PROTOC_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/_deps/protobuf-src/src/)
  126. endif()
  127. add_custom_command(OUTPUT ${GENERATED_PB_CC} ${GENERATED_PB_H}
  128. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  129. ARGS "-I$<JOIN:${_PROTOC_INCLUDE_DIRS},;-I>" --cpp_out=${OUT_DIR} ${ABS_FIL}
  130. DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
  131. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  132. COMMAND_EXPAND_LISTS)
  133. endforeach()
  134. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  135. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES COMPILE_OPTIONS -Wno-deprecated-declarations)
  136. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  137. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  138. endfunction()
  139. # Add protobuf to a specified target.
  140. function(netdata_add_protobuf _target)
  141. target_compile_options(${_target} PRIVATE ${PROTOBUF_CFLAGS_OTHER})
  142. target_include_directories(${_target} PRIVATE ${PROTOBUF_INCLUDE_DIRS})
  143. target_link_libraries(${_target} PRIVATE ${PROTOBUF_LIBRARIES})
  144. endfunction()