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