Skip to content

Commit

Permalink
makefile: fix Makefile to check file existence, and add idempotency f…
Browse files Browse the repository at this point in the history
…or vmlinux

Signed-off-by: shun159 <[email protected]>
  • Loading branch information
shun159 committed Sep 11, 2023
1 parent 3f53ea8 commit 460b060
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

export GOPROXY = direct

CURDIR := $(abspath .)
TESTDATADIR := $(CURDIR)/test-data
BPFTOOL := bpftool
CLANG := clang
LOGFILE_PATH ?= stdout

UNAME_ARCH = $(shell uname -m)
ARCH = $(lastword $(subst :, ,$(filter $(UNAME_ARCH):%,x86_64:x86 aarch64:arm64)))

Expand Down Expand Up @@ -29,35 +35,42 @@ check-format: format
vet: ## Run go vet on source code.
go vet $(ALLPKGS)


# Build BPF
CLANG := clang
CLANG_INCLUDE := -I../../..
BPF_CFLAGS := -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -D__TARGET_ARCH_$(ARCH)
TARGETS := \
test-data/tc.ingress \
test-data/tc \
test-data/test.map \
test-data/test_license \
test-data/invalid_map \
test-data/recoverydata \
test-data/test-kprobe \
test-data/xdp \
test-data/ring_buffer
$(TESTDATADIR)/tc.ingress \
$(TESTDATADIR)/tc \
$(TESTDATADIR)/test.map \
$(TESTDATADIR)/test_license \
$(TESTDATADIR)/invalid_map \
$(TESTDATADIR)/recoverydata \
$(TESTDATADIR)/test-kprobe \
$(TESTDATADIR)/xdp \
$(TESTDATADIR)/ring_buffer

%.bpf.elf: %.bpf.c
$(CLANG) $(CLANG_INCLUDE) $(BPF_CFLAGS) -c $< -o $@

vmlinuxh:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(abspath ./test-data/vmlinux.h)
## check if the vmlinux exists in /sys/kernel/btf directory
VMLINUX_BTF ?= $(wildcard /sys/kernel/btf/vmlinux)
ifeq ($(VMLINUX_BTF),)
$(error Cannot find a vmlinux)
endif

$(TESTDATADIR)/vmlinux.h:
$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@

##@ Run Unit Tests
# Run unit tests
unit-test: vmlinuxh
unit-test: $(TESTDATADIR)/vmlinux.h
unit-test: $(addsuffix .bpf.elf,$(TARGETS))
unit-test: export AWS_EBPF_SDK_LOG_FILE=stdout
unit-test: export AWS_EBPF_SDK_LOG_FILE=$(LOGFILE_PATH)
unit-test: ## Run unit tests
go test -v -coverprofile=coverage.txt -covermode=atomic ./pkg/...

.PHONY: clean
clean:
-@rm -f test-data/*.elf
-@rm -f $(TESTDATADIR)/vmlinux.h
-@rm -f $(TESTDATADIR)/*.elf

0 comments on commit 460b060

Please sign in to comment.