NetdataProtobuf.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. message(STATUS "Preparing bundled Abseil (required by bundled Protobuf)")
  25. FetchContent_Declare(absl
  26. GIT_REPOSITORY https://github.com/abseil/abseil-cpp
  27. GIT_TAG ${ABSL_TAG}
  28. )
  29. FetchContent_MakeAvailable_NoInstall(absl)
  30. message(STATUS "Finished preparing bundled Abseil")
  31. endif()
  32. set(protobuf_INSTALL Off)
  33. set(protobuf_BUILD_LIBPROTOC Off)
  34. set(protobuf_BUILD_TESTS Off)
  35. set(protobuf_BUILD_SHARED_LIBS Off)
  36. message(STATUS "Preparing bundled Protobuf")
  37. FetchContent_Declare(protobuf
  38. GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
  39. GIT_TAG ${PROTOBUF_TAG}
  40. )
  41. FetchContent_MakeAvailable_NoInstall(protobuf)
  42. message(STATUS "Finished preparing bundled Protobuf.")
  43. set(BUNDLED_PROTOBUF True PARENT_SCOPE)
  44. endfunction()
  45. # Handle detection of Protobuf
  46. macro(netdata_detect_protobuf)
  47. if(NOT ENABLE_BUNDLED_PROTOBUF)
  48. if (NOT BUILD_SHARED_LIBS)
  49. set(Protobuf_USE_STATIC_LIBS On)
  50. endif()
  51. # The FindProtobuf CMake module shipped by upstream CMake is
  52. # broken for Protobuf version 22.0 and newer because it does
  53. # not correctly pull in the new Abseil dependencies. Protobuf
  54. # itself sometimes ships a CMake Package Configuration module
  55. # that _does_ work correctly, so use that in preference to the
  56. # Find module shipped with CMake.
  57. #
  58. # The code below works by first attempting to use find_package
  59. # in config mode, and then checking for the existence of the
  60. # target we actually use that gets defined by the protobuf
  61. # CMake Package Configuration Module to determine if that
  62. # worked. A bit of extra logic is required in the case of the
  63. # config mode working, because some systems ship compatibility
  64. # logic for the old FindProtobuf module while others do not.
  65. #
  66. # Upstream bug reference: https://gitlab.kitware.com/cmake/cmake/-/issues/24321
  67. find_package(Protobuf CONFIG)
  68. if(NOT TARGET protobuf::libprotobuf)
  69. message(STATUS "Could not find Protobuf using Config mode, falling back to Module mode")
  70. find_package(Protobuf REQUIRED)
  71. endif()
  72. endif()
  73. if(TARGET protobuf::libprotobuf)
  74. if(NOT Protobuf_PROTOC_EXECUTABLE AND TARGET protobuf::protoc)
  75. set(Protobuf_PROTOC_EXECUTABLE protobuf::protoc)
  76. endif()
  77. # It is technically possible that this may still not
  78. # be set by this point, so we need to check it and
  79. # fail noisily if it isn't because the build won't
  80. # work without it.
  81. if(NOT Protobuf_PROTOC_EXECUTABLE)
  82. message(FATAL_ERROR "Could not determine the location of the protobuf compiler for the detected version of protobuf.")
  83. endif()
  84. set(PROTOBUF_PROTOC_EXECUTABLE ${Protobuf_PROTOC_EXECUTABLE})
  85. set(PROTOBUF_LIBRARIES protobuf::libprotobuf)
  86. endif()
  87. set(ENABLE_PROTOBUF True)
  88. set(HAVE_PROTOBUF True)
  89. endmacro()
  90. # Helper function to compile protocol definitions into C++ code.
  91. function(netdata_protoc_generate_cpp INC_DIR OUT_DIR SRCS HDRS)
  92. if(NOT ARGN)
  93. message(SEND_ERROR "Error: protoc_generate_cpp() called without any proto files")
  94. return()
  95. endif()
  96. set(${INC_DIR})
  97. set(${OUT_DIR})
  98. set(${SRCS})
  99. set(${HDRS})
  100. foreach(FIL ${ARGN})
  101. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  102. get_filename_component(DIR ${ABS_FIL} DIRECTORY)
  103. get_filename_component(FIL_WE ${FIL} NAME_WE)
  104. set(GENERATED_PB_CC "${DIR}/${FIL_WE}.pb.cc")
  105. list(APPEND ${SRCS} ${GENERATED_PB_CC})
  106. set(GENERATED_PB_H "${DIR}/${FIL_WE}.pb.h")
  107. list(APPEND ${HDRS} ${GENERATED_PB_H})
  108. list(APPEND _PROTOC_INCLUDE_DIRS ${INC_DIR})
  109. if(ENABLE_BUNDLED_PROTOBUF)
  110. list(APPEND _PROTOC_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/_deps/protobuf-src/src/)
  111. endif()
  112. add_custom_command(OUTPUT ${GENERATED_PB_CC} ${GENERATED_PB_H}
  113. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  114. ARGS "-I$<JOIN:${_PROTOC_INCLUDE_DIRS},;-I>" --cpp_out=${OUT_DIR} ${ABS_FIL}
  115. DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
  116. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  117. COMMAND_EXPAND_LISTS)
  118. endforeach()
  119. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  120. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES COMPILE_OPTIONS -Wno-deprecated-declarations)
  121. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  122. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  123. endfunction()
  124. # Add protobuf to a specified target.
  125. function(netdata_add_protobuf _target)
  126. target_compile_definitions(${_target} PRIVATE ${PROTOBUF_CFLAGS_OTHER})
  127. target_include_directories(${_target} PRIVATE ${PROTOBUF_INCLUDE_DIRS})
  128. target_link_libraries(${_target} PRIVATE ${PROTOBUF_LIBRARIES})
  129. endfunction()