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