Netcat-eBPF-Demo/Makefile

104 lines
3.1 KiB
Makefile
Raw Normal View History

2024-06-09 18:07:50 +02:00
# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
OUTPUT := .output
CLANG ?= clang
LLVM_STRIP ?= llvm-strip
SHELL := /bin/bash
LIBLOG_OBJ := $(abspath $(OUTPUT)/liblog.o)
LIBLOG_SRC := $(abspath ./includes/liblog/src/log.c)
LIBLOG_HDR := $(abspath ./includes/liblog/src/)
INC := $(abspath ./includes/)
BPFTOOL := sudo bpftool
ARCH := $(shell uname -m | sed 's/x86_64/x86/' | sed 's/aarch64/arm64/' | sed 's/ppc64le/powerpc/' | sed 's/mips.*/mips/')
INCLUDES := -I$(OUTPUT) -I$(LIBLOG_HDR) -I$(INC)
CFLAGS := -g -Wall -DLOG_USE_COLOR
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
APPS = netcat_ebpf_demo
SOURCES := $(filter-out src/$(APPS).c,$(wildcard src/*.c))
ALL_LDFLAGS += -lrt -ldl -lpthread -lm -lbpf -lelf -lz
# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
# architecture-specific dirs will be "missing" on some architectures/distros -
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
# sys/cdefs.h etc. might be missing.
#
# Use '-idirafter': Don't interfere with include mechanics except where the
# build would have failed anyways.
CLANG_BPF_SYS_INCLUDES = $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
ifeq ($(V),1)
Q =
msg =
else
Q = @
msg = @printf ' %-8s %s%s\n' \
"$(1)" \
"$(patsubst $(abspath $(OUTPUT))/%,%,$(2))" \
"$(if $(3), $(3))";
MAKEFLAGS += --no-print-directory
endif
define allow-override
$(if $(or $(findstring environment,$(origin $(1))),\
$(findstring command line,$(origin $(1)))),,\
$(eval $(1) = $(2)))
endef
$(call allow-override,CC,$(CROSS_COMPILE)cc)
$(call allow-override,LD,$(CROSS_COMPILE)ld)
.PHONY: all
all: $(APPS)
.PHONY: clean
clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS)
clean-app:
$(call msg,CLEAN-APP)
$(Q)rm -rf $(APPS)
$(Q)rm -rf $(OUTPUT)/*.skel.h
$(Q)rm -rf $(OUTPUT)/*.o
$(OUTPUT):
$(call msg,MKDIR,$@)
$(Q)mkdir -p $@
# Build liblog
$(LIBLOG_OBJ):
$(call msg,LIBLOG,$@)
$(Q)$(CC) $(CFLAGS) $(INCLUDES) -c $(LIBLOG_SRC) -o $@
# Build BPF code
$(OUTPUT)/%.bpf.o: src/ebpf/%.bpf.c $(wildcard src/ebpf/%.h) $(VMLINUX) | $(OUTPUT)
$(call msg,BPF,$@)
2024-06-09 18:28:02 +02:00
$(Q)$(CLANG) -Wno-int-to-void-pointer-cast -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) -c $(filter %.c,$^) -o $@
2024-06-09 18:07:50 +02:00
$(Q)$(LLVM_STRIP) -g $@ # strip useless DWARF info
# Generate BPF skeletons
$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(OUTPUT)
$(call msg,GEN-SKEL,$@)
$(Q)$(BPFTOOL) gen skeleton $< > $@
# Build user-space code
$(patsubst %,$(OUTPUT)/%.o,$(APPS)): %.o: %.skel.h
$(OUTPUT)/%.o: src/%.c $(wildcard %.h) | $(OUTPUT)
$(call msg,CC,$@)
$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(INCLUDES) -o $@
# Build application binary
$(APPS): %: $(OUTPUT)/%.o $(LIBLOG_OBJ) | $(OUTPUT)
$(call msg,BINARY,$@)
$(Q)$(CC) $(CFLAGS) $(SOURCES) $^ $(ALL_LDFLAGS) -o $@
# delete failed targets
.DELETE_ON_ERROR:
# keep intermediate (.skel.h, .bpf.o, etc) targets
.SECONDARY: