xref: /openbmc/linux/tools/testing/selftests/hid/Makefile (revision cd1e565a5b7fa60c349ca8a16db1e61715fe8230)
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
21TEST_FILES += tests
22
23CXX ?= $(CROSS_COMPILE)g++
24
25HOSTPKG_CONFIG := pkg-config
26
27CFLAGS += -g -O0 -rdynamic -Wall -Werror -I$(KHDR_INCLUDES) -I$(OUTPUT)
28LDLIBS += -lelf -lz -lrt -lpthread
29
30# Silence some warnings when compiled with clang
31ifneq ($(LLVM),)
32CFLAGS += -Wno-unused-command-line-argument
33endif
34
35# Order correspond to 'make run_tests' order
36TEST_GEN_PROGS = hid_bpf
37
38# Emit succinct information message describing current building step
39# $1 - generic step name (e.g., CC, LINK, etc);
40# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
41# $3 - target (assumed to be file); only file name will be emitted;
42# $4 - optional extra arg, emitted as-is, if provided.
43ifeq ($(V),1)
44Q =
45msg =
46else
47Q = @
48msg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
49MAKEFLAGS += --no-print-directory
50submake_extras := feature_display=0
51endif
52
53# override lib.mk's default rules
54OVERRIDE_TARGETS := 1
55override define CLEAN
56	$(call msg,CLEAN)
57	$(Q)$(RM) -r $(TEST_GEN_PROGS)
58	$(Q)$(RM) -r $(EXTRA_CLEAN)
59endef
60
61include ../lib.mk
62
63TOOLSDIR := $(top_srcdir)/tools
64LIBDIR := $(TOOLSDIR)/lib
65BPFDIR := $(LIBDIR)/bpf
66TOOLSINCDIR := $(TOOLSDIR)/include
67BPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
68SCRATCH_DIR := $(OUTPUT)/tools
69BUILD_DIR := $(SCRATCH_DIR)/build
70INCLUDE_DIR := $(SCRATCH_DIR)/include
71KHDR_INCLUDES := $(SCRATCH_DIR)/uapi/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$(KHDR_INCLUDES)/linux/hid.h: $(top_srcdir)/include/uapi/linux/hid.h
158	$(MAKE) -C $(top_srcdir) INSTALL_HDR_PATH=$(SCRATCH_DIR)/uapi headers_install
159
160$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
161		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
162		       $(TOOLSDIR)/lib/rbtree.c			\
163		       $(TOOLSDIR)/lib/zalloc.c			\
164		       $(TOOLSDIR)/lib/string.c			\
165		       $(TOOLSDIR)/lib/ctype.c			\
166		       $(TOOLSDIR)/lib/str_error_r.c
167	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
168		CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) \
169		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
170		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
171
172# Get Clang's default includes on this system, as opposed to those seen by
173# '--target=bpf'. This fixes "missing" files on some architectures/distros,
174# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
175#
176# Use '-idirafter': Don't interfere with include mechanics except where the
177# build would have failed anyways.
178define get_sys_includes
179$(shell $(1) -v -E - </dev/null 2>&1 \
180	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
181$(shell $(1) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
182endef
183
184# Determine target endianness.
185IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
186			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
187MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
188
189CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
190BPF_CFLAGS = -g -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 		\
191	     -I$(INCLUDE_DIR)
192
193CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
194	       -Wno-compare-distinct-pointer-types
195
196# Build BPF object using Clang
197# $1 - input .c file
198# $2 - output .o file
199# $3 - CFLAGS
200define CLANG_BPF_BUILD_RULE
201	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
202	$(Q)$(CLANG) $3 -O2 --target=bpf -c $1 -mcpu=v3 -o $2
203endef
204# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
205define CLANG_NOALU32_BPF_BUILD_RULE
206	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
207	$(Q)$(CLANG) $3 -O2 --target=bpf -c $1 -mcpu=v2 -o $2
208endef
209# Build BPF object using GCC
210define GCC_BPF_BUILD_RULE
211	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
212	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
213endef
214
215BPF_PROGS_DIR := progs
216BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
217BPF_SRCS := $(notdir $(wildcard $(BPF_PROGS_DIR)/*.c))
218BPF_OBJS := $(patsubst %.c,$(OUTPUT)/%.bpf.o, $(BPF_SRCS))
219BPF_SKELS := $(patsubst %.c,$(OUTPUT)/%.skel.h, $(BPF_SRCS))
220TEST_GEN_FILES += $(BPF_OBJS)
221
222$(BPF_PROGS_DIR)-bpfobjs := y
223$(BPF_OBJS): $(OUTPUT)/%.bpf.o:				\
224	     $(BPF_PROGS_DIR)/%.c			\
225	     $(wildcard $(BPF_PROGS_DIR)/*.h)		\
226	     $(INCLUDE_DIR)/vmlinux.h				\
227	     $(wildcard $(BPFDIR)/hid_bpf_*.h)			\
228	     $(wildcard $(BPFDIR)/*.bpf.h)			\
229	     | $(OUTPUT) $(BPFOBJ)
230	$(call $(BPF_BUILD_RULE),$<,$@, $(BPF_CFLAGS))
231
232$(BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(OUTPUT)
233	$(call msg,GEN-SKEL,$(BINARY),$@)
234	$(Q)$(BPFTOOL) gen object $(<:.o=.linked1.o) $<
235	$(Q)$(BPFTOOL) gen skeleton $(<:.o=.linked1.o) name $(notdir $(<:.bpf.o=)) > $@
236
237$(OUTPUT)/%.o: %.c $(BPF_SKELS) $(KHDR_INCLUDES)/linux/hid.h
238	$(call msg,CC,,$@)
239	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
240
241$(OUTPUT)/%: $(OUTPUT)/%.o
242	$(call msg,BINARY,,$@)
243	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
244
245EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) feature bpftool	\
246	$(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32)
247