1# SPDX-License-Identifier: GPL-2.0
2
3# based on tools/testing/selftest/bpf/Makefile
4include ../../../build/Build.include
5include ../../../scripts/Makefile.arch
6include ../../../scripts/Makefile.include
7
8CXX ?= $(CROSS_COMPILE)g++
9
10HOSTPKG_CONFIG := pkg-config
11
12CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(KHDR_INCLUDES) -I$(OUTPUT)
13LDLIBS += -lelf -lz -lrt -lpthread
14
15# Silence some warnings when compiled with clang
16ifneq ($(LLVM),)
17CFLAGS += -Wno-unused-command-line-argument
18endif
19
20# Order correspond to 'make run_tests' order
21TEST_GEN_PROGS = hid_bpf
22
23# Emit succinct information message describing current building step
24# $1 - generic step name (e.g., CC, LINK, etc);
25# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
26# $3 - target (assumed to be file); only file name will be emitted;
27# $4 - optional extra arg, emitted as-is, if provided.
28ifeq ($(V),1)
29Q =
30msg =
31else
32Q = @
33msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
34MAKEFLAGS += --no-print-directory
35submake_extras := feature_display=0
36endif
37
38# override lib.mk's default rules
39OVERRIDE_TARGETS := 1
40override define CLEAN
41	$(call msg,CLEAN)
42	$(Q)$(RM) -r $(TEST_GEN_PROGS)
43	$(Q)$(RM) -r $(EXTRA_CLEAN)
44endef
45
46include ../lib.mk
47
48TOOLSDIR := $(top_srcdir)/tools
49LIBDIR := $(TOOLSDIR)/lib
50BPFDIR := $(LIBDIR)/bpf
51TOOLSINCDIR := $(TOOLSDIR)/include
52BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
53SCRATCH_DIR := $(OUTPUT)/tools
54BUILD_DIR := $(SCRATCH_DIR)/build
55INCLUDE_DIR := $(SCRATCH_DIR)/include
56KHDR_INCLUDES := $(SCRATCH_DIR)/uapi/include
57BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
58ifneq ($(CROSS_COMPILE),)
59HOST_BUILD_DIR		:= $(BUILD_DIR)/host
60HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
61HOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
62else
63HOST_BUILD_DIR		:= $(BUILD_DIR)
64HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
65HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
66endif
67HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
68RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
69
70VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
71		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
72		     ../../../../vmlinux				\
73		     /sys/kernel/btf/vmlinux				\
74		     /boot/vmlinux-$(shell uname -r)
75VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
76ifeq ($(VMLINUX_BTF),)
77$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
78endif
79
80# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
81# to build individual tests.
82# NOTE: Semicolon at the end is critical to override lib.mk's default static
83# rule for binaries.
84$(notdir $(TEST_GEN_PROGS)): %: $(OUTPUT)/% ;
85
86# sort removes libbpf duplicates when not cross-building
87MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf		\
88	       $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids	\
89	       $(INCLUDE_DIR))
90$(MAKE_DIRS):
91	$(call msg,MKDIR,,$@)
92	$(Q)mkdir -p $@
93
94# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
95ifeq ($(SRCARCH),x86)
96LLD := lld
97else
98LLD := ld
99endif
100
101DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
102
103TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
104
105$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
106
107BPFTOOL ?= $(DEFAULT_BPFTOOL)
108$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
109		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
110	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
111		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) 	       \
112		    EXTRA_CFLAGS='-g -O0'				       \
113		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
114		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
115		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
116		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
117
118$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
119	   | $(BUILD_DIR)/libbpf
120	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
121		    EXTRA_CFLAGS='-g -O0'				       \
122		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
123
124ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
125$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
126		| $(HOST_BUILD_DIR)/libbpf
127	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
128		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
129		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
130		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
131endif
132
133$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
134ifeq ($(VMLINUX_H),)
135	$(call msg,GEN,,$@)
136	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
137else
138	$(call msg,CP,,$@)
139	$(Q)cp "$(VMLINUX_H)" $@
140endif
141
142$(KHDR_INCLUDES)/linux/hid.h: $(top_srcdir)/include/uapi/linux/hid.h
143	$(MAKE) -C $(top_srcdir) INSTALL_HDR_PATH=$(SCRATCH_DIR)/uapi headers_install
144
145$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
146		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
147		       $(TOOLSDIR)/lib/rbtree.c			\
148		       $(TOOLSDIR)/lib/zalloc.c			\
149		       $(TOOLSDIR)/lib/string.c			\
150		       $(TOOLSDIR)/lib/ctype.c			\
151		       $(TOOLSDIR)/lib/str_error_r.c
152	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
153		CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
154		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
155		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
156
157# Get Clang's default includes on this system, as opposed to those seen by
158# '-target bpf'. This fixes "missing" files on some architectures/distros,
159# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
160#
161# Use '-idirafter': Don't interfere with include mechanics except where the
162# build would have failed anyways.
163define get_sys_includes
164$(shell $(1) -v -E - </dev/null 2>&1 \
165	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
166$(shell $(1) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
167endef
168
169# Determine target endianness.
170IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
171			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
172MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
173
174CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
175BPF_CFLAGS = -g -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 		\
176	     -I$(INCLUDE_DIR)
177
178CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
179	       -Wno-compare-distinct-pointer-types
180
181# Build BPF object using Clang
182# $1 - input .c file
183# $2 - output .o file
184# $3 - CFLAGS
185define CLANG_BPF_BUILD_RULE
186	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
187	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v3 -o $2
188endef
189# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
190define CLANG_NOALU32_BPF_BUILD_RULE
191	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
192	$(Q)$(CLANG) $3 -O2 -target bpf -c $1 -mcpu=v2 -o $2
193endef
194# Build BPF object using GCC
195define GCC_BPF_BUILD_RULE
196	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
197	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
198endef
199
200BPF_PROGS_DIR := progs
201BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
202BPF_SRCS := $(notdir $(wildcard $(BPF_PROGS_DIR)/*.c))
203BPF_OBJS := $(patsubst %.c,$(OUTPUT)/%.bpf.o, $(BPF_SRCS))
204BPF_SKELS := $(patsubst %.c,$(OUTPUT)/%.skel.h, $(BPF_SRCS))
205TEST_GEN_FILES += $(BPF_OBJS)
206
207$(BPF_PROGS_DIR)-bpfobjs := y
208$(BPF_OBJS): $(OUTPUT)/%.bpf.o:				\
209	     $(BPF_PROGS_DIR)/%.c			\
210	     $(wildcard $(BPF_PROGS_DIR)/*.h)		\
211	     $(INCLUDE_DIR)/vmlinux.h				\
212	     $(wildcard $(BPFDIR)/hid_bpf_*.h)			\
213	     $(wildcard $(BPFDIR)/*.bpf.h)			\
214	     | $(OUTPUT) $(BPFOBJ)
215	$(call $(BPF_BUILD_RULE),$<,$@, $(BPF_CFLAGS))
216
217$(BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(OUTPUT)
218	$(call msg,GEN-SKEL,$(BINARY),$@)
219	$(Q)$(BPFTOOL) gen object $(<:.o=.linked1.o) $<
220	$(Q)$(BPFTOOL) gen skeleton $(<:.o=.linked1.o) name $(notdir $(<:.bpf.o=)) > $@
221
222$(OUTPUT)/%.o: %.c $(BPF_SKELS) $(KHDR_INCLUDES)/linux/hid.h
223	$(call msg,CC,,$@)
224	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
225
226$(OUTPUT)/%: $(OUTPUT)/%.o
227	$(call msg,BINARY,,$@)
228	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
229
230EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) feature bpftool	\
231	$(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32)
232