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