@cryptotaxi247 / netdata-1 / commits / 90552bd50

Fix handling of vendored eBPF code in CMake. (#17556)

* Fix logic error in CMake code for kernel version detection. * Force legacy libbpf for CentOS 7 and AL 2 package builds. * Check target kernel version instead of host kernel version. This should help when building in containers. * Fix up handling for older versions of CMake. * Use CMake C compiler for libbpf builds. * Fix selection of legacy eBPF code in static builds. * Explicitly pull in kernel headers on CentOS. * Fix typo in FORCE_LEGACY_LIBBPF option. * Only enable eBPF by default on x86. * Fix detection of static builds. * Fix libc detection logic. * Fix handling of static builds. * Fix musl libc detection. * Fix check messages for libc detection.

Austin S. Hemmelgarn committed May 6, 2024 at 07:05 UTC 90552bd5012f98e56e874afce6bf8fdc0cae52b5
7 files changed +88 -23
CMakeLists.txt
+1 -1
@@ -171,7 +171,7 @@ mark_as_advanced(ENABLE_SENTRY)
171 option(BUILD_FOR_PACKAGING "Include component files for native packages" False)
172 mark_as_advanced(BUILD_FOR_PACKAGING)
173
174 -cmake_dependent_option(FORCE_LEGACY_LIBBPF "Force usage of libbpf 0.0.9 instead of the latest version." False "ENABLE_PLUGIN_LIBBPF" False)
174 +cmake_dependent_option(FORCE_LEGACY_LIBBPF "Force usage of libbpf 0.0.9 instead of the latest version." False "ENABLE_PLUGIN_EBPF" False)
175 mark_as_advanced(FORCE_LEGACY_LIBBPF)
176
177 if(ENABLE_ACLK OR ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE)
netdata-installer.sh
+3 -1
@@ -246,7 +246,9 @@ HEREDOC
246 }
247
248 if [ "$(uname -s)" = "Linux" ]; then
249 - ENABLE_EBPF=1
249 + case "$(uname -m)" in
250 + x86_64|i?86) ENABLE_EBPF=1 ;;
251 + esac
252 fi
253
254 DONOTSTART=0
netdata.spec.in
+5
@@ -332,6 +332,11 @@ happened, on your systems and applications.
332 %endif
333 %if %{_have_ebpf}
334 -DENABLE_PLUGIN_EBPF=On \
335 + %if 0%{?centos_ver:1}
336 + %if 0%{?centos_ver} < 8
337 + -DFORCE_LEGACY_LIBBPF=On \
338 + %endif
339 + %endif
340 %else
341 -DENABLE_PLUGIN_EBPF=Off \
342 %endif
packaging/cmake/Modules/NetdataEBPFLegacy.cmake
+10 -1
@@ -13,7 +13,16 @@ set(ebpf-legacy_BUILD_DIR "${CMAKE_BINARY_DIR}/ebpf-legacy-build")
13 function(netdata_fetch_legacy_ebpf_code)
14 netdata_identify_libc(_libc)
15
16 - if(_libc STREQUAL "glibc")
16 + if(DEFINED BUILD_SHARED_LIBS)
17 + if(NOT BUILD_SHARED_LIBS)
18 + set(need_static TRUE)
19 + endif()
20 + endif()
21 +
22 + if(need_static)
23 + set(_hash 302714979470e300b81a64a4ebef9fac1f3488488482cf30c926fa98f73cabc1)
24 + set(_libc "static")
25 + elseif(_libc STREQUAL "glibc")
26 set(_hash 7013753ef85c2d3681bdcfdd7544705cdaf5b640b70734ca223f43cc5adcdc53)
27 elseif(_libc STREQUAL "musl")
28 set(_hash b524d1fcbd67c82cdfc9f46d55f67c0c0c0412c3356c85b38c03ce03f6068747)
packaging/cmake/Modules/NetdataLibBPF.cmake
+13 -5
@@ -34,12 +34,20 @@ function(netdata_bundle_libbpf)
34 set(_libbpf_tag b981a3a138e3a30024e4e143d62cff2dc307121e) # v1.4.0p_netdata
35 endif()
36
37 - netdata_identify_libc(_libc)
37 + if(DEFINED BUILD_SHARED_LIBS)
38 + if(NOT BUILD_SHARED_LIBS)
39 + set(need_static TRUE)
40 + endif()
41 + endif()
42 +
43 + if(NOT need_static)
44 + netdata_identify_libc(_libc)
45
39 - string(REGEX MATCH "glibc|musl" _libc_supported "${_libc}")
46 + string(REGEX MATCH "glibc|musl" _libc_supported "${_libc}")
47
41 - if(NOT _libc_supported)
42 - message(FATAL_ERROR "This system’s libc (detected: ${_libc}) is not not supported by the eBPF plugin.")
48 + if(NOT _libc_supported)
49 + message(FATAL_ERROR "This system’s libc (detected: ${_libc}) is not not supported by the eBPF plugin.")
50 + endif()
51 endif()
52
53 find_program(MAKE_COMMAND make)
@@ -65,7 +73,7 @@ function(netdata_bundle_libbpf)
73 GIT_TAG ${_libbpf_tag}
74 SOURCE_DIR "${libbpf_SOURCE_DIR}"
75 CONFIGURE_COMMAND ""
68 - BUILD_COMMAND ${MAKE_COMMAND} -C src BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install
76 + BUILD_COMMAND ${MAKE_COMMAND} -C src CC=${CMAKE_C_COMPILER} BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install
77 BUILD_IN_SOURCE 1
78 BUILD_BYPRODUCTS "${_libbpf_library}"
79 INSTALL_COMMAND ""
packaging/cmake/Modules/NetdataUtil.cmake
+55 -15
@@ -16,17 +16,55 @@ function(netdata_detect_host_kernel_version)
16
17 message(CHECK_START "Determining host kernel version")
18
19 - execute_process(COMMAND uname -r
20 - RESULT_VARIABLE _uname_result
21 - OUTPUT_VARIABLE _uname_output)
19 + if(NOT CMAKE_CROSSCOMPILING)
20 + include(CheckIncludeFile)
21
23 - if(NOT _uname_result)
24 - message(CHECK_FAIL "unknown")
25 - set(HOST_KERNEL_VERSION "0.0.0" CACHE STRING "Detected host kernel version")
26 - return()
22 + check_include_file("linux/version.h" CAN_USE_VERSION_H)
23 +
24 + if(CAN_USE_VERSION_H)
25 + message(CHECK_START "Checking version using linux/version.h")
26 + file(WRITE "${CMAKE_BINARY_DIR}/kversion-test.c" "
27 + #include <stdio.h>
28 + #include <linux/version.h>
29 +
30 + int main() {
31 + printf(\"%i.%i.%i\", LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, LINUX_VERSION_SUBLEVEL);
32 + }
33 + ")
34 +
35 + try_run(_run_success _compile_success
36 + ${CMAKE_BINARY_DIR}
37 + SOURCES ${CMAKE_BINARY_DIR}/kversion-test.c
38 + RUN_OUTPUT_VARIABLE _kversion_output)
39 +
40 + if(_compile_success AND _run_success EQUAL 0)
41 + message(CHECK_PASS "success")
42 + set(_kversion_value "${_kversion_output}")
43 + else()
44 + message(CHECK_FAIL "failed")
45 + endif()
46 + endif()
47 endif()
48
29 - string(REGEX REPLACE "-.+$" "" _kversion "${_uname_output}")
49 + if(NOT DEFINED _kversion_value)
50 + message(CHECK_START "Checking version using uname")
51 + execute_process(COMMAND uname -r
52 + RESULT_VARIABLE _uname_result
53 + OUTPUT_VARIABLE _uname_output)
54 +
55 + if(NOT _uname_result EQUAL 0)
56 + message(CHECK_FAIL "failed")
57 + message(CHECK_FAIL "unknown")
58 + set(HOST_KERNEL_VERSION "0.0.0" CACHE STRING "Detected host kernel version")
59 + return()
60 + else()
61 + message(CHECK_PASS "success")
62 + endif()
63 +
64 + set(_kversion_value "${_uname_output}")
65 + endif()
66 +
67 + string(REGEX REPLACE "-.+$" "" _kversion "${_kversion_value}")
68 message(CHECK_PASS "${_kversion}")
69 set(HOST_KERNEL_VERSION "${_kversion}" CACHE STRING "Detected host kernel version")
70 endfunction()
@@ -36,34 +74,36 @@ endfunction()
74 # Sets the specified variable to the name of the libc or "unknown"
75 function(netdata_identify_libc _libc_name)
76 if(NOT DEFINED _ND_DETECTED_LIBC)
39 - message(INFO "Detecting libc implementation")
77 + message(CHECK_START "Detecting libc implementation")
78
79 execute_process(COMMAND ldd --version
80 COMMAND grep -q -i -E "glibc|gnu libc"
43 - RESULT_VARIABLE LDD_IS_GLIBC
81 + RESULT_VARIABLE LDD_RESULT
82 OUTPUT_VARIABLE LDD_OUTPUT
83 ERROR_VARIABLE LDD_OUTPUT)
84
47 - if(LDD_IS_GLIBC)
85 + if(NOT LDD_RESULT)
86 set(${_libc_name} glibc PARENT_SCOPE)
87 set(_ND_DETECTED_LIBC glibc CACHE INTERNAL "")
88 + message(CHECK_PASS "glibc")
89 return()
90 endif()
91
53 - execute_process(COMMAND ldd --version
54 - COMMAND grep -q -i -E "musl"
55 - RESULT_VARIABLE LDD_IS_MUSL
92 + execute_process(COMMAND sh -c "ldd --version 2>&1 | grep -q -i 'musl'"
93 + RESULT_VARIABLE LDD_RESULT
94 OUTPUT_VARIABLE LDD_OUTPUT
95 ERROR_VARIABLE LDD_OUTPUT)
96
59 - if(LDD_IS_MUSL)
97 + if(NOT LDD_RESULT)
98 set(${_libc_name} musl PARENT_SCOPE)
99 set(_ND_DETECTED_LIBC musl CACHE INTERNAL "")
100 + message(CHECK_PASS "musl")
101 return()
102 endif()
103
104 set(${_libc_name} unknown PARENT_SCOPE)
105 set(_ND_DETECTED_LIBC unknown CACHE INTERNAL "")
106 + message(CHECK_FAIL "unknown")
107 else()
108 set(${_libc_name} ${_ND_DETECTED_LIBC} PARENT_SCOPE)
109 endif()
packaging/installer/install-required-packages.sh
+1
@@ -623,6 +623,7 @@ declare -A pkg_find=(
623
624 declare -A pkg_distro_sdk=(
625 ['alpine']="alpine-sdk"
626 + ['centos']="kernel-headers"
627 ['default']="NOTREQUIRED"
628 )
629