epf: improving the ebpf makefile

The current ebpf/Makefile.am have the problem that clang compile
errors still result in an ELF .bpf output file.  This is obviously
problematic as the problem is first seen runtime when loading
the bpf-prog.  This is caused by the uses of a pipe from
clang to llc.

To address this problem, split up the clang and llc invocations
up into two separate commands, to get proper reaction based on
the compiler exit code. The clang compiler is used as a
frontend (+ optimizer) and instructed (via -S -emit-llvm) to
generate LLVM IR (Intermediate Representation) with suffix .ll.
The LLVM llc command is used as a compiler backend taking IR and
producing BPF machine bytecode, and storing this into a ELF
object.  In the last step the IR .ll suffix code it removed.

The official documentation of the IR language:
 http://llvm.org/docs/LangRef.html

Also fix the previous make portability warning:
 '%-style pattern rules are a GNU make extension'
I instead use some static pattern rules:
 https://www.gnu.org/software/make/manual/html_node/Static-Usage.html

Signed-off-by: Jesper Dangaard Brouer <netoptimizer@brouer.com>
pull/3263/head
Jesper Dangaard Brouer 8 years ago committed by Victor Julien
parent 3f4c7efa72
commit 39754a976a

@ -3,11 +3,26 @@ if BUILD_EBPF
# Maintaining a local copy of UAPI linux/bpf.h
BPF_CFLAGS = -Iinclude
all: lb.bpf filter.bpf bypass_filter.bpf xdp_filter.bpf vlan_filter.bpf
CLANG = ${CC}
%.bpf: %.c
${CC} -Wall $(BPF_CFLAGS) -O2 -I/usr/include/$(build_cpu)-$(build_os)/ -D__KERNEL__ -D__ASM_SYSREG_H -target bpf -emit-llvm -c $< -o - | ${LLC} -march=bpf -filetype=obj -o $@
BPF_TARGETS = lb.bpf
BPF_TARGETS += filter.bpf
BPF_TARGETS += bypass_filter.bpf
BPF_TARGETS += xdp_filter.bpf
BPF_TARGETS += vlan_filter.bpf
CLEANFILES = *.bpf
all: $(BPF_TARGETS)
$(BPF_TARGETS): %.bpf: %.c
# From C-code to LLVM-IR format suffix .ll (clang -S -emit-llvm)
${CLANG} -Wall $(BPF_CFLAGS) -O2 \
-I/usr/include/$(build_cpu)-$(build_os)/ \
-D__KERNEL__ -D__ASM_SYSREG_H \
-target bpf -S -emit-llvm $< -o ${@:.bpf=.ll}
# From LLVM-IR to BPF-bytecode in ELF-obj file
${LLC} -march=bpf -filetype=obj ${@:.bpf=.ll} -o $@
${RM} ${@:.bpf=.ll}
CLEANFILES = *.bpf *.ll
endif

Loading…
Cancel
Save