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