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