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