summaryrefslogtreecommitdiffstats
path: root/packaging/cmake/Modules/NetdataLibBPF.cmake
blob: 9c3bf6d2f4cb511b3331ccb3a59566db38ef26bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Handling for libbpf (used by the eBPF plugin)
#
# Copyright (c) 2024 Netdata Inc.
# SPDX-License-Identifier: GPL-3.0-or-later

include(ExternalProject)
include(NetdataUtil)

set(libbpf_SOURCE_DIR "${CMAKE_BINARY_DIR}/libbpf")

# Check if the kernel is old enough that we need to use a legacy copy of eBPF.
function(_need_legacy_libbpf _var)
    if(FORCE_LEGACY_LIBBPF)
        set(${_var} TRUE PARENT_SCOPE)
        return()
    endif()

    netdata_detect_host_kernel_version()

    if(HOST_KERNEL_VERSION VERSION_LESS "4.14.0")
        set(${_var} TRUE PARENT_SCOPE)
    else()
        set(${_var} FALSE PARENT_SCOPE)
    endif()
endfunction()

# Prepare a vendored copy of libbpf
function(netdata_bundle_libbpf)
    _need_legacy_libbpf(USE_LEGACY_LIBBPF)

    if(USE_LEGACY_LIBBPF)
        set(_libbpf_tag 673424c56127bb556e64095f41fd60c26f9083ec) # v0.0.9_netdata-1
    else()
        set(_libbpf_tag 6923eb970e22682eaedff79f5be4f9934b99cf50) # v1.4.5p_netdata
    endif()

    if(DEFINED BUILD_SHARED_LIBS)
        if(NOT BUILD_SHARED_LIBS)
            set(need_static TRUE)
        endif()
    endif()

    if(NOT need_static)
        netdata_identify_libc(_libc)

        string(REGEX MATCH "glibc|musl" _libc_supported "${_libc}")

        if(NOT _libc_supported)
            message(FATAL_ERROR "This system’s libc (detected: ${_libc}) is not not supported by the eBPF plugin.")
        endif()
    endif()

    find_program(MAKE_COMMAND make)

    if(MAKE_COMMAND STREQUAL MAKE_COMMAND-NOTFOUND)
        message(FATAL_ERROR "GNU Make is required when building the eBPF plugin, but could not be found.")
    endif()

    pkg_check_modules(ELF REQUIRED libelf)
    pkg_check_modules(ZLIB REQUIRED zlib)

    set(_libbpf_lib_dir lib)

    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
        set(_libbpf_lib_dir lib64)
    endif()

    set(_libbpf_library "${libbpf_SOURCE_DIR}/usr/${_libbpf_lib_dir}/libbpf.a")

    ExternalProject_Add(
        libbpf
        GIT_REPOSITORY https://github.com/netdata/libbpf.git
        GIT_TAG ${_libbpf_tag}
        SOURCE_DIR "${libbpf_SOURCE_DIR}"
        CONFIGURE_COMMAND ""
        BUILD_COMMAND ${MAKE_COMMAND} -C src CC=${CMAKE_C_COMPILER} BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install
        BUILD_IN_SOURCE 1
        BUILD_BYPRODUCTS "${_libbpf_library}"
        INSTALL_COMMAND ""
        EXCLUDE_FROM_ALL 1
    )

    add_library(libbpf_library STATIC IMPORTED GLOBAL)
    set_property(
        TARGET libbpf_library
        PROPERTY IMPORTED_LOCATION "${_libbpf_library}"
    )
    set_property(
        TARGET libbpf_library
        PROPERTY INTERFACE_LINK_LIBRARIES "${ELF_LIBRARIES};${ZLIB_LIBRARIES}"
    )
    set(NETDATA_LIBBPF_INCLUDE_DIRECTORIES "${libbpf_SOURCE_DIR}/usr/include;${libbpf_SOURCE_DIR}/include;${ELF_INCLUDE_DIRECTORIES};${ZLIB_INCLUDE_DIRECTORIES}" PARENT_SCOPE)
    set(NETDATA_LIBBPF_COMPILE_OPTIONS "${ELF_CFLAGS_OTHER};${ZLIB_CFLAGS_OTHER}" PARENT_SCOPE)
endfunction()

# Add libbpf as a link dependency for the given target.
function(netdata_add_libbpf_to_target _target)
    target_link_libraries(${_target} PUBLIC libbpf_library)
    target_include_directories(${_target} BEFORE PUBLIC "${NETDATA_LIBBPF_INCLUDE_DIRECTORIES}")
    target_compile_options(${_target} PUBLIC "${NETDATA_LIBBPF_COMPILE_OPTIONS}")
    add_dependencies(${_target} libbpf)
endfunction()