NetdataLibBPF.cmake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Handling for libbpf (used by the eBPF plugin)
  2. #
  3. # Copyright (c) 2024 Netdata Inc.
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. include(ExternalProject)
  6. include(NetdataUtil)
  7. set(libbpf_SOURCE_DIR "${CMAKE_BINARY_DIR}/libbpf")
  8. # Check if the kernel is old enough that we need to use a legacy copy of eBPF.
  9. function(_need_legacy_libbpf _var)
  10. if(FORCE_LEGACY_LIBBPF)
  11. set(${_var} TRUE PARENT_SCOPE)
  12. return()
  13. endif()
  14. netdata_detect_host_kernel_version()
  15. if(HOST_KERNEL_VERSION VERSION_LESS "4.14.0")
  16. set(${_var} TRUE PARENT_SCOPE)
  17. else()
  18. set(${_var} FALSE PARENT_SCOPE)
  19. endif()
  20. endfunction()
  21. # Prepare a vendored copy of libbpf
  22. function(netdata_bundle_libbpf)
  23. _need_legacy_libbpf(USE_LEGACY_LIBBPF)
  24. if(USE_LEGACY_LIBBPF)
  25. set(_libbpf_tag 673424c56127bb556e64095f41fd60c26f9083ec) # v0.0.9_netdata-1
  26. else()
  27. set(_libbpf_tag ad7c3a4266bf5ce301a5691eb7b405dbb27c7f3d) # v1.5.0p_netdata
  28. endif()
  29. if(DEFINED BUILD_SHARED_LIBS)
  30. if(NOT BUILD_SHARED_LIBS)
  31. set(need_static TRUE)
  32. endif()
  33. endif()
  34. if(NOT need_static)
  35. netdata_identify_libc(_libc)
  36. string(REGEX MATCH "glibc|musl" _libc_supported "${_libc}")
  37. if(NOT _libc_supported)
  38. message(FATAL_ERROR "This system’s libc (detected: ${_libc}) is not not supported by the eBPF plugin.")
  39. endif()
  40. endif()
  41. find_program(MAKE_COMMAND make)
  42. if(MAKE_COMMAND STREQUAL MAKE_COMMAND-NOTFOUND)
  43. message(FATAL_ERROR "GNU Make is required when building the eBPF plugin, but could not be found.")
  44. endif()
  45. pkg_check_modules(ELF REQUIRED libelf)
  46. pkg_check_modules(ZLIB REQUIRED zlib)
  47. set(_libbpf_lib_dir lib)
  48. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  49. set(_libbpf_lib_dir lib64)
  50. endif()
  51. set(_libbpf_library "${libbpf_SOURCE_DIR}/usr/${_libbpf_lib_dir}/libbpf.a")
  52. ExternalProject_Add(
  53. libbpf
  54. GIT_REPOSITORY https://github.com/netdata/libbpf.git
  55. GIT_TAG ${_libbpf_tag}
  56. SOURCE_DIR "${libbpf_SOURCE_DIR}"
  57. CONFIGURE_COMMAND ""
  58. BUILD_COMMAND ${MAKE_COMMAND} -C src CC=${CMAKE_C_COMPILER} BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install
  59. BUILD_IN_SOURCE 1
  60. BUILD_BYPRODUCTS "${_libbpf_library}"
  61. INSTALL_COMMAND ""
  62. EXCLUDE_FROM_ALL 1
  63. )
  64. add_library(libbpf_library STATIC IMPORTED GLOBAL)
  65. set_property(
  66. TARGET libbpf_library
  67. PROPERTY IMPORTED_LOCATION "${_libbpf_library}"
  68. )
  69. set_property(
  70. TARGET libbpf_library
  71. PROPERTY INTERFACE_LINK_LIBRARIES "${ELF_LIBRARIES};${ZLIB_LIBRARIES}"
  72. )
  73. set(NETDATA_LIBBPF_INCLUDE_DIRECTORIES "${libbpf_SOURCE_DIR}/usr/include;${libbpf_SOURCE_DIR}/include;${ELF_INCLUDE_DIRECTORIES};${ZLIB_INCLUDE_DIRECTORIES}" PARENT_SCOPE)
  74. set(NETDATA_LIBBPF_COMPILE_OPTIONS "${ELF_CFLAGS_OTHER};${ZLIB_CFLAGS_OTHER}" PARENT_SCOPE)
  75. endfunction()
  76. # Add libbpf as a link dependency for the given target.
  77. function(netdata_add_libbpf_to_target _target)
  78. target_link_libraries(${_target} PUBLIC libbpf_library)
  79. target_include_directories(${_target} BEFORE PUBLIC "${NETDATA_LIBBPF_INCLUDE_DIRECTORIES}")
  80. target_compile_options(${_target} PUBLIC "${NETDATA_LIBBPF_COMPILE_OPTIONS}")
  81. add_dependencies(${_target} libbpf)
  82. endfunction()