eBPF Toolchain / compiling ebpf with CMake using clang and llc / two staged build process

1.3k views Asked by At

I have a bunch of ebpf-programs (using c and libbpf; no BCC/bpftrace) in a folder, let's call them File1.bpf.c, File2.bpf.c, .... Usually, I compile bpf programs with clang and llc, every file by itself:

  1. clang -I [...]/libbpf/build/root/usr/include/ -target bpf -S -D BPF_PROG -D __BPF_TRACING__ -D__TARGET_ARCH_x86 -Wall -O2 -emit-llvm -c -g File1.bpf.c
  2. llc-11 -march=bpf -filetype=obj -o File1.bpf.o File1.bpf.ll

With this, nothing is linked. I might want use bpftool to link some of the files afterwards.

So my questions are:

  1. How to do this two-staged build process in cmake? I have seen some snippets using functions, but it did not work for me and feels very "hacky". Current idea is to use a python-script as a fake-Compiler. For cmake, the compiler is set to be the Python script, which invokes clang and llc. I think this is a possible solution, but it does not "feel right" and has problems, too. Additionally, I wonder why I can not find a Tutorial or a question on Stackoverflow about this topic, which leads to my second question:
  2. Is this procedure up to date? If not: What is the preferred way to handle bpf compilation?

A visualization of the build process I want to achieve in cmake with every row being independent: Structure

I do not want to use skeletons; the bpf objects should be independently loadable afterwards.

When looking online, usually in blogposts clang and llvm are used when it is c code. However, it seems like clang can directly compile to a bpf object, which has the problem that -mcpu cannot be specified 1. With mcpu the instruction set version is set. Therefore, another question is:

  1. When should -mcpu be specified?

Thanks in advance for any help :)

0

There are 0 answers