|
| 1 | +cmake_minimum_required(VERSION 3.0) |
| 2 | + |
| 3 | +# Define source files for the main plugin |
| 4 | +file(GLOB_RECURSE src |
| 5 | + "in_ebpf.c" |
| 6 | + "traces/**/handler.c" |
| 7 | +) |
| 8 | + |
| 9 | +# Determine architecture and set flags accordingly |
| 10 | +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") |
| 11 | + set(ARCH_FLAG "-D__TARGET_ARCH_x86_64") |
| 12 | + set(VMLINUX_PATH "${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes/external/gadget/amd64") |
| 13 | +elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "arm64") |
| 14 | + set(ARCH_FLAG "-D__TARGET_ARCH_arm64") |
| 15 | + set(VMLINUX_PATH "${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes/external/gadget/arm64") |
| 16 | +else() |
| 17 | + message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}") |
| 18 | +endif() |
| 19 | + |
| 20 | +# Include directories for external headers, common headers, and generated skeletons |
| 21 | +include_directories( |
| 22 | + ${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes/external |
| 23 | + ${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes |
| 24 | +) |
| 25 | + |
| 26 | +# Create an interface library for gadget includes |
| 27 | +add_library(gadget INTERFACE) |
| 28 | +target_include_directories(gadget INTERFACE ${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes/external/gadget) |
| 29 | + |
| 30 | +# Find all bpf.c files in the traces directory |
| 31 | +file(GLOB_RECURSE TRACE_C_FILES ${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/*/bpf.c) |
| 32 | + |
| 33 | +# Create a list to hold all the object files and skeleton headers that will be generated |
| 34 | +set(TRACE_OBJ_FILES "") |
| 35 | +set(TRACE_SKEL_HEADERS "") |
| 36 | + |
| 37 | +# Iterate over each trace bpf.c file to generate corresponding .o and .skel.h files |
| 38 | +foreach(TRACE_C_FILE ${TRACE_C_FILES}) |
| 39 | + # Get the filename and parent directory name (for uniqueness) |
| 40 | + get_filename_component(TRACE_FILE_NAME ${TRACE_C_FILE} NAME_WE) |
| 41 | + get_filename_component(TRACE_PARENT_DIR ${TRACE_C_FILE} DIRECTORY) |
| 42 | + get_filename_component(TRACE_PARENT_DIR_NAME ${TRACE_PARENT_DIR} NAME) |
| 43 | + |
| 44 | + # Ensure the output filenames maintain the original "trace_" prefix |
| 45 | + set(TRACE_BASE_NAME "trace_${TRACE_PARENT_DIR_NAME}") |
| 46 | + |
| 47 | + # Set unique names by including the parent directory name in the output paths |
| 48 | + set(TRACE_OBJ_FILE ${CMAKE_BINARY_DIR}/plugins/in_ebpf/traces/includes/generated/${TRACE_BASE_NAME}.o) |
| 49 | + set(TRACE_SKEL_HEADER ${CMAKE_BINARY_DIR}/plugins/in_ebpf/traces/includes/generated/${TRACE_BASE_NAME}.skel.h) |
| 50 | + |
| 51 | + # Compile each bpf.c file to its corresponding .o file |
| 52 | + add_custom_command( |
| 53 | + OUTPUT ${TRACE_OBJ_FILE} |
| 54 | + COMMAND clang |
| 55 | + -target bpf |
| 56 | + ${ARCH_FLAG} # Use architecture-specific flag |
| 57 | + -O2 # Optional: Optimization level |
| 58 | + -g # Optional: Debug info |
| 59 | + -I${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes/external |
| 60 | + -I${CMAKE_SOURCE_DIR}/plugins/in_ebpf/traces/includes |
| 61 | + -I${VMLINUX_PATH} # Include the correct vmlinux.h based on architecture |
| 62 | + -c ${TRACE_C_FILE} |
| 63 | + -o ${TRACE_OBJ_FILE} |
| 64 | + DEPENDS ${TRACE_C_FILE} |
| 65 | + ) |
| 66 | + |
| 67 | + # Generate skeleton header for each compiled BPF object file |
| 68 | + add_custom_command( |
| 69 | + OUTPUT ${TRACE_SKEL_HEADER} |
| 70 | + COMMAND bpftool gen skeleton ${TRACE_OBJ_FILE} > ${TRACE_SKEL_HEADER} |
| 71 | + DEPENDS ${TRACE_OBJ_FILE} |
| 72 | + COMMENT "Generating skeleton ${TRACE_SKEL_HEADER} from ${TRACE_OBJ_FILE}" |
| 73 | + ) |
| 74 | + |
| 75 | + # Add generated object and skeleton files to their respective lists |
| 76 | + list(APPEND TRACE_OBJ_FILES ${TRACE_OBJ_FILE}) |
| 77 | + list(APPEND TRACE_SKEL_HEADERS ${TRACE_SKEL_HEADER}) |
| 78 | +endforeach() |
| 79 | + |
| 80 | +# Create a custom target specifically for generating eBPF skeletons |
| 81 | +add_custom_target(generate_skeletons DEPENDS ${TRACE_SKEL_HEADERS}) |
| 82 | + |
| 83 | +# Create a custom target to compile all eBPF programs (all trace bpf.c files) |
| 84 | +add_custom_target(compile_ebpf ALL DEPENDS ${TRACE_OBJ_FILES} ${TRACE_SKEL_HEADERS}) |
| 85 | + |
| 86 | +# Ensure that the custom target depends on the gadget interface library (for include paths) |
| 87 | +add_dependencies(compile_ebpf gadget) |
| 88 | + |
| 89 | +# Include generated skeleton headers in the main plugin |
| 90 | +include_directories(${CMAKE_BINARY_DIR}/plugins/in_ebpf/traces/includes/generated) |
| 91 | + |
| 92 | +# Declare the Fluent Bit plugin (using the default compiler for the main plugin) |
| 93 | +FLB_PLUGIN(in_ebpf "${src}" "") |
| 94 | + |
| 95 | +# Link necessary libraries |
| 96 | +target_link_libraries(flb-plugin-in_ebpf gadget -lbpf -lelf -lz) |
0 commit comments