xref: /linux/tools/testing/selftests/hid/Makefile (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
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
20TEST_FILES := run-hid-tools-tests.sh
21
22CXX ?= $(CROSS_COMPILE)g++
23
24HOSTPKG_CONFIG := pkg-config
25
26CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(OUTPUT)
27CFLAGS += -I$(OUTPUT)/tools/include
28
29LDLIBS += -lelf -lz -lrt -lpthread
30
31# Silence some warnings when compiled with clang
32ifneq ($(LLVM),)
33CFLAGS += -Wno-unused-command-line-argument
34endif
35
36# Order correspond to 'make run_tests' order
37TEST_GEN_PROGS = hid_bpf hidraw
38
39# Emit succinct information message describing current building step
40# $1 - generic step name (e.g., CC, LINK, etc);
41# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
42# $3 - target (assumed to be file); only file name will be emitted;
43# $4 - optional extra arg, emitted as-is, if provided.
44ifeq ($(V),1)
45Q =
46msg =
47else
48Q = @
49msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
50MAKEFLAGS += --no-print-directory
51submake_extras := feature_display=0
52endif
53
54# override lib.mk's default rules
55OVERRIDE_TARGETS := 1
56override define CLEAN
57	$(call msg,CLEAN)
58	$(Q)$(RM) -r $(TEST_GEN_PROGS)
59	$(Q)$(RM) -r $(EXTRA_CLEAN)
60endef
61
62include ../lib.mk
63
64TOOLSDIR := $(top_srcdir)/tools
65LIBDIR := $(TOOLSDIR)/lib
66BPFDIR := $(LIBDIR)/bpf
67TOOLSINCDIR := $(TOOLSDIR)/include
68BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
69SCRATCH_DIR := $(OUTPUT)/tools
70BUILD_DIR := $(SCRATCH_DIR)/build
71INCLUDE_DIR := $(SCRATCH_DIR)/include
72BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
73ifneq ($(CROSS_COMPILE),)
74HOST_BUILD_DIR		:= $(BUILD_DIR)/host
75HOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
76HOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
77else
78HOST_BUILD_DIR		:= $(BUILD_DIR)
79HOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
80HOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
81endif
82HOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
83RESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
84
85VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
86		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
87		     ../../../../vmlinux				\
88		     /sys/kernel/btf/vmlinux				\
89		     /boot/vmlinux-$(shell uname -r)
90VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
91ifeq ($(VMLINUX_BTF),)
92$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
93endif
94
95# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
96# to build individual tests.
97# NOTE: Semicolon at the end is critical to override lib.mk's default static
98# rule for binaries.
99$(notdir $(TEST_GEN_PROGS)): %: $(OUTPUT)/% ;
100
101# sort removes libbpf duplicates when not cross-building
102MAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf		\
103	       $(HOST_BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/resolve_btfids	\
104	       $(INCLUDE_DIR))
105$(MAKE_DIRS):
106	$(call msg,MKDIR,,$@)
107	$(Q)mkdir -p $@
108
109# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
110ifeq ($(SRCARCH),x86)
111LLD := lld
112else
113LLD := ld
114endif
115
116DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
117
118TEST_GEN_PROGS_EXTENDED += $(DEFAULT_BPFTOOL)
119
120$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
121
122BPFTOOL ?= $(DEFAULT_BPFTOOL)
123$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
124		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
125	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
126		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) 	       \
127		    EXTRA_CFLAGS='-g -O0'				       \
128		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
129		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
130		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
131		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
132
133$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
134	   | $(BUILD_DIR)/libbpf
135	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
136		    EXTRA_CFLAGS='-g -O0'				       \
137		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
138
139ifneq ($(BPFOBJ),$(HOST_BPFOBJ))
140$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
141		| $(HOST_BUILD_DIR)/libbpf
142	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
143		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
144		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
145		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
146endif
147
148$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
149ifeq ($(VMLINUX_H),)
150	$(call msg,GEN,,$@)
151	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
152else
153	$(call msg,CP,,$@)
154	$(Q)cp "$(VMLINUX_H)" $@
155endif
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)
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