1# SPDX-License-Identifier: GPL-2.0
2OUTPUT := .output
3abs_out := $(abspath $(OUTPUT))
4
5CLANG ?= clang
6LLC ?= llc
7LLVM_STRIP ?= llvm-strip
8
9TOOLS_PATH := $(abspath ../../../../tools)
10BPFTOOL_SRC := $(TOOLS_PATH)/bpf/bpftool
11BPFTOOL_OUTPUT := $(abs_out)/bpftool
12DEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
13BPFTOOL ?= $(DEFAULT_BPFTOOL)
14
15LIBBPF_SRC := $(TOOLS_PATH)/lib/bpf
16LIBBPF_OUTPUT := $(abs_out)/libbpf
17LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
18LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)/include
19BPFOBJ := $(LIBBPF_OUTPUT)/libbpf.a
20
21INCLUDES := -I$(OUTPUT) -I$(LIBBPF_INCLUDE) -I$(TOOLS_PATH)/include/uapi
22CFLAGS := -g -Wall
23
24VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
25		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
26		     ../../../../vmlinux				\
27		     /sys/kernel/btf/vmlinux				\
28		     /boot/vmlinux-$(shell uname -r)
29VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
30ifeq ($(VMLINUX_BTF),)
31$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
32endif
33
34ifeq ($(V),1)
35Q =
36msg =
37else
38Q = @
39msg = @printf '  %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))";
40MAKEFLAGS += --no-print-directory
41submake_extras := feature_display=0
42endif
43
44.DELETE_ON_ERROR:
45
46.PHONY: all clean
47
48all: entrypoints.lskel.h
49
50clean:
51	$(call msg,CLEAN)
52	$(Q)rm -rf $(OUTPUT) entrypoints
53
54entrypoints.lskel.h: $(OUTPUT)/entrypoints.bpf.o | $(BPFTOOL)
55	$(call msg,GEN-SKEL,$@)
56	$(Q)$(BPFTOOL) gen skeleton -L $< > $@
57
58
59$(OUTPUT)/entrypoints.bpf.o: entrypoints.bpf.c $(OUTPUT)/vmlinux.h $(BPFOBJ) | $(OUTPUT)
60	$(call msg,BPF,$@)
61	$(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES)			      \
62		 -c $(filter %.c,$^) -o $@ &&				      \
63	$(LLVM_STRIP) -g $@
64
65$(OUTPUT)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
66ifeq ($(VMLINUX_H),)
67	$(call msg,GEN,,$@)
68	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
69else
70	$(call msg,CP,,$@)
71	$(Q)cp "$(VMLINUX_H)" $@
72endif
73
74$(OUTPUT) $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
75	$(call msg,MKDIR,$@)
76	$(Q)mkdir -p $@
77
78$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
79	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC)			       \
80		    OUTPUT=$(abspath $(dir $@))/ prefix=		       \
81		    DESTDIR=$(LIBBPF_DESTDIR) $(abspath $@) install_headers
82
83ifeq ($(CROSS_COMPILE),)
84$(DEFAULT_BPFTOOL): $(BPFOBJ) | $(BPFTOOL_OUTPUT)
85	$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOL_SRC)			       \
86		    OUTPUT=$(BPFTOOL_OUTPUT)/				       \
87		    LIBBPF_BOOTSTRAP_OUTPUT=$(LIBBPF_OUTPUT)/		       \
88		    LIBBPF_BOOTSTRAP_DESTDIR=$(LIBBPF_DESTDIR)/ bootstrap
89else
90$(DEFAULT_BPFTOOL): | $(BPFTOOL_OUTPUT)
91	$(Q)$(MAKE) $(submake_extras) -C $(BPFTOOL_SRC)			       \
92		    OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
93endif
94