1# SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2include ../../scripts/Makefile.include
3
4OUTPUT ?= $(abspath .output)/
5
6BPFTOOL_OUTPUT := $(OUTPUT)bpftool/
7DEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)bootstrap/bpftool
8BPFTOOL ?= $(DEFAULT_BPFTOOL)
9LIBBPF_SRC := $(abspath ../../lib/bpf)
10BPFOBJ_OUTPUT := $(OUTPUT)libbpf/
11BPFOBJ := $(BPFOBJ_OUTPUT)libbpf.a
12BPF_DESTDIR := $(BPFOBJ_OUTPUT)
13BPF_INCLUDE := $(BPF_DESTDIR)/include
14INCLUDES := -I$(OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../include/uapi)
15CFLAGS := -g -Wall $(CLANG_CROSS_FLAGS)
16CFLAGS += $(EXTRA_CFLAGS)
17LDFLAGS += $(EXTRA_LDFLAGS)
18LDLIBS += -lelf -lz
19
20# Try to detect best kernel BTF source
21KERNEL_REL := $(shell uname -r)
22VMLINUX_BTF_PATHS := $(if $(O),$(O)/vmlinux)		\
23	$(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
24	../../../vmlinux /sys/kernel/btf/vmlinux	\
25	/boot/vmlinux-$(KERNEL_REL)
26VMLINUX_BTF_PATH := $(or $(VMLINUX_BTF),$(firstword			       \
27					  $(wildcard $(VMLINUX_BTF_PATHS))))
28
29ifeq ($(V),1)
30Q =
31else
32Q = @
33MAKEFLAGS += --no-print-directory
34submake_extras := feature_display=0
35endif
36
37.DELETE_ON_ERROR:
38
39.PHONY: all clean runqslower libbpf_hdrs
40all: runqslower
41
42runqslower: $(OUTPUT)/runqslower
43
44clean:
45	$(call QUIET_CLEAN, runqslower)
46	$(Q)$(RM) -r $(BPFOBJ_OUTPUT) $(BPFTOOL_OUTPUT)
47	$(Q)$(RM) $(OUTPUT)*.o $(OUTPUT)*.d
48	$(Q)$(RM) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
49	$(Q)$(RM) $(OUTPUT)runqslower
50	$(Q)$(RM) -r .output
51
52libbpf_hdrs: $(BPFOBJ)
53
54$(OUTPUT)/runqslower: $(OUTPUT)/runqslower.o $(BPFOBJ)
55	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
56
57$(OUTPUT)/runqslower.o: runqslower.h $(OUTPUT)/runqslower.skel.h	      \
58			$(OUTPUT)/runqslower.bpf.o | libbpf_hdrs
59
60$(OUTPUT)/runqslower.bpf.o: $(OUTPUT)/vmlinux.h runqslower.h | libbpf_hdrs
61
62$(OUTPUT)/%.skel.h: $(OUTPUT)/%.bpf.o | $(BPFTOOL)
63	$(QUIET_GEN)$(BPFTOOL) gen skeleton $< > $@
64
65$(OUTPUT)/%.bpf.o: %.bpf.c $(BPFOBJ) | $(OUTPUT)
66	$(QUIET_GEN)$(CLANG) -g -O2 --target=bpf $(INCLUDES)		      \
67		 -c $(filter %.c,$^) -o $@ &&				      \
68	$(LLVM_STRIP) -g $@
69
70$(OUTPUT)/%.o: %.c | $(OUTPUT)
71	$(QUIET_CC)$(CC) $(CFLAGS) $(INCLUDES) -c $(filter %.c,$^) -o $@
72
73$(OUTPUT) $(BPFOBJ_OUTPUT) $(BPFTOOL_OUTPUT):
74	$(QUIET_MKDIR)mkdir -p $@
75
76$(OUTPUT)/vmlinux.h: $(VMLINUX_BTF_PATH) | $(OUTPUT) $(BPFTOOL)
77ifeq ($(VMLINUX_H),)
78	$(Q)if [ ! -e "$(VMLINUX_BTF_PATH)" ] ; then \
79		echo "Couldn't find kernel BTF; set VMLINUX_BTF to"	       \
80			"specify its location." >&2;			       \
81		exit 1;\
82	fi
83	$(QUIET_GEN)$(BPFTOOL) btf dump file $(VMLINUX_BTF_PATH) format c > $@
84else
85	$(Q)cp "$(VMLINUX_H)" $@
86endif
87
88$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(BPFOBJ_OUTPUT)
89	$(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) OUTPUT=$(BPFOBJ_OUTPUT) \
90		    DESTDIR=$(BPFOBJ_OUTPUT) prefix= $(abspath $@) install_headers
91
92$(DEFAULT_BPFTOOL): | $(BPFTOOL_OUTPUT)
93	$(Q)$(MAKE) $(submake_extras) -C ../bpftool OUTPUT=$(BPFTOOL_OUTPUT) bootstrap
94