16008105bSBenjamin Tissoires# SPDX-License-Identifier: GPL-2.0 26008105bSBenjamin Tissoires 36008105bSBenjamin TissoiresHID_SAMPLES_PATH ?= $(abspath $(srctree)/$(src)) 46008105bSBenjamin TissoiresTOOLS_PATH := $(HID_SAMPLES_PATH)/../../tools 56008105bSBenjamin Tissoires 66008105bSBenjamin Tissoirespound := \# 76008105bSBenjamin Tissoires 86008105bSBenjamin Tissoires# List of programs to build 96008105bSBenjamin Tissoirestprogs-y += hid_mouse 10a56a2569SBenjamin Tissoirestprogs-y += hid_surface_dial 116008105bSBenjamin Tissoires 126008105bSBenjamin Tissoires# Libbpf dependencies 136008105bSBenjamin TissoiresLIBBPF_SRC = $(TOOLS_PATH)/lib/bpf 146008105bSBenjamin TissoiresLIBBPF_OUTPUT = $(abspath $(HID_SAMPLES_PATH))/libbpf 156008105bSBenjamin TissoiresLIBBPF_DESTDIR = $(LIBBPF_OUTPUT) 166008105bSBenjamin TissoiresLIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include 176008105bSBenjamin TissoiresLIBBPF = $(LIBBPF_OUTPUT)/libbpf.a 186008105bSBenjamin Tissoires 196008105bSBenjamin TissoiresEXTRA_HEADERS := hid_bpf_attach.h 206008105bSBenjamin TissoiresEXTRA_BPF_HEADERS := hid_bpf_helpers.h 216008105bSBenjamin Tissoires 226008105bSBenjamin Tissoireshid_mouse-objs := hid_mouse.o 23a56a2569SBenjamin Tissoireshid_surface_dial-objs := hid_surface_dial.o 246008105bSBenjamin Tissoires 256008105bSBenjamin Tissoires# Tell kbuild to always build the programs 266008105bSBenjamin Tissoiresalways-y := $(tprogs-y) 276008105bSBenjamin Tissoires 286008105bSBenjamin Tissoiresifeq ($(ARCH), arm) 296008105bSBenjamin Tissoires# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux 306008105bSBenjamin Tissoires# headers when arm instruction set identification is requested. 316008105bSBenjamin TissoiresARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS)) 326008105bSBenjamin TissoiresBPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR) 336008105bSBenjamin TissoiresTPROGS_CFLAGS += $(ARM_ARCH_SELECTOR) 346008105bSBenjamin Tissoiresendif 356008105bSBenjamin Tissoires 366008105bSBenjamin Tissoiresifeq ($(ARCH), mips) 376008105bSBenjamin TissoiresTPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__ 386008105bSBenjamin Tissoiresifdef CONFIG_MACH_LOONGSON64 396008105bSBenjamin TissoiresBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64 406008105bSBenjamin TissoiresBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic 416008105bSBenjamin Tissoiresendif 426008105bSBenjamin Tissoiresendif 436008105bSBenjamin Tissoires 446008105bSBenjamin TissoiresTPROGS_CFLAGS += -Wall -O2 456008105bSBenjamin TissoiresTPROGS_CFLAGS += -Wmissing-prototypes 466008105bSBenjamin TissoiresTPROGS_CFLAGS += -Wstrict-prototypes 476008105bSBenjamin Tissoires 486008105bSBenjamin TissoiresTPROGS_CFLAGS += -I$(objtree)/usr/include 496008105bSBenjamin TissoiresTPROGS_CFLAGS += -I$(LIBBPF_INCLUDE) 506008105bSBenjamin TissoiresTPROGS_CFLAGS += -I$(srctree)/tools/include 516008105bSBenjamin Tissoires 526008105bSBenjamin Tissoiresifdef SYSROOT 536008105bSBenjamin TissoiresTPROGS_CFLAGS += --sysroot=$(SYSROOT) 546008105bSBenjamin TissoiresTPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib 556008105bSBenjamin Tissoiresendif 566008105bSBenjamin Tissoires 576008105bSBenjamin TissoiresTPROGS_LDLIBS += $(LIBBPF) -lelf -lz 586008105bSBenjamin Tissoires 596008105bSBenjamin Tissoires# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: 606008105bSBenjamin Tissoires# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang 616008105bSBenjamin TissoiresLLC ?= llc 626008105bSBenjamin TissoiresCLANG ?= clang 636008105bSBenjamin TissoiresOPT ?= opt 646008105bSBenjamin TissoiresLLVM_DIS ?= llvm-dis 656008105bSBenjamin TissoiresLLVM_OBJCOPY ?= llvm-objcopy 666008105bSBenjamin TissoiresLLVM_READELF ?= llvm-readelf 676008105bSBenjamin TissoiresBTF_PAHOLE ?= pahole 686008105bSBenjamin Tissoires 696008105bSBenjamin Tissoires# Detect that we're cross compiling and use the cross compiler 706008105bSBenjamin Tissoiresifdef CROSS_COMPILE 716008105bSBenjamin TissoiresCLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%)) 726008105bSBenjamin Tissoiresendif 736008105bSBenjamin Tissoires 746008105bSBenjamin Tissoires# Don't evaluate probes and warnings if we need to run make recursively 756008105bSBenjamin Tissoiresifneq ($(src),) 766008105bSBenjamin TissoiresHDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \ 776008105bSBenjamin Tissoires $(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \ 786008105bSBenjamin Tissoires -o /dev/null 2>/dev/null && echo okay) 796008105bSBenjamin Tissoires 806008105bSBenjamin Tissoiresifeq ($(HDR_PROBE),) 816008105bSBenjamin Tissoires$(warning WARNING: Detected possible issues with include path.) 826008105bSBenjamin Tissoires$(warning WARNING: Please install kernel headers locally (make headers_install).) 836008105bSBenjamin Tissoiresendif 846008105bSBenjamin Tissoires 856008105bSBenjamin TissoiresBTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) 866008105bSBenjamin TissoiresBTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) 876008105bSBenjamin TissoiresBTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm') 886008105bSBenjamin TissoiresBTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ 89*bbaf1ff0SFangrui Song $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \ 906008105bSBenjamin Tissoires $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \ 916008105bSBenjamin Tissoires /bin/rm -f ./llvm_btf_verify.o) 926008105bSBenjamin Tissoires 936008105bSBenjamin TissoiresBPF_EXTRA_CFLAGS += -fno-stack-protector 946008105bSBenjamin Tissoiresifneq ($(BTF_LLVM_PROBE),) 956008105bSBenjamin Tissoires BPF_EXTRA_CFLAGS += -g 966008105bSBenjamin Tissoireselse 976008105bSBenjamin Tissoiresifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),) 986008105bSBenjamin Tissoires BPF_EXTRA_CFLAGS += -g 996008105bSBenjamin Tissoires LLC_FLAGS += -mattr=dwarfris 1006008105bSBenjamin Tissoires DWARF2BTF = y 1016008105bSBenjamin Tissoiresendif 1026008105bSBenjamin Tissoiresendif 1036008105bSBenjamin Tissoiresendif 1046008105bSBenjamin Tissoires 1056008105bSBenjamin Tissoires# Trick to allow make to be run from this directory 1066008105bSBenjamin Tissoiresall: 1076008105bSBenjamin Tissoires $(MAKE) -C ../../ M=$(CURDIR) HID_SAMPLES_PATH=$(CURDIR) 1086008105bSBenjamin Tissoires 1096008105bSBenjamin Tissoiresclean: 1106008105bSBenjamin Tissoires $(MAKE) -C ../../ M=$(CURDIR) clean 1116008105bSBenjamin Tissoires @find $(CURDIR) -type f -name '*~' -delete 1126008105bSBenjamin Tissoires @$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool 1136008105bSBenjamin Tissoires 1146008105bSBenjamin Tissoires$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT) 1156008105bSBenjamin Tissoires# Fix up variables inherited from Kbuild that tools/ build system won't like 1166008105bSBenjamin Tissoires $(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \ 1176008105bSBenjamin Tissoires LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(HID_SAMPLES_PATH)/../../ \ 1186008105bSBenjamin Tissoires O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \ 1196008105bSBenjamin Tissoires $@ install_headers 1206008105bSBenjamin Tissoires 1216008105bSBenjamin TissoiresBPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool 1226008105bSBenjamin TissoiresBPFTOOL_OUTPUT := $(abspath $(HID_SAMPLES_PATH))/bpftool 1236008105bSBenjamin TissoiresBPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool 1246008105bSBenjamin Tissoires$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) 1256008105bSBenjamin Tissoires $(MAKE) -C $(BPFTOOLDIR) srctree=$(HID_SAMPLES_PATH)/../../ \ 1266008105bSBenjamin Tissoires OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap 1276008105bSBenjamin Tissoires 1286008105bSBenjamin Tissoires$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT): 1296008105bSBenjamin Tissoires $(call msg,MKDIR,$@) 1306008105bSBenjamin Tissoires $(Q)mkdir -p $@ 1316008105bSBenjamin Tissoires 1326008105bSBenjamin TissoiresFORCE: 1336008105bSBenjamin Tissoires 1346008105bSBenjamin Tissoires 1356008105bSBenjamin Tissoires# Verify LLVM compiler tools are available and bpf target is supported by llc 1366008105bSBenjamin Tissoires.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) 1376008105bSBenjamin Tissoires 1386008105bSBenjamin Tissoiresverify_cmds: $(CLANG) $(LLC) 1396008105bSBenjamin Tissoires @for TOOL in $^ ; do \ 1406008105bSBenjamin Tissoires if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ 1416008105bSBenjamin Tissoires echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ 1426008105bSBenjamin Tissoires exit 1; \ 1436008105bSBenjamin Tissoires else true; fi; \ 1446008105bSBenjamin Tissoires done 1456008105bSBenjamin Tissoires 1466008105bSBenjamin Tissoiresverify_target_bpf: verify_cmds 1476008105bSBenjamin Tissoires @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ 1486008105bSBenjamin Tissoires echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ 1496008105bSBenjamin Tissoires echo " NOTICE: LLVM version >= 3.7.1 required" ;\ 1506008105bSBenjamin Tissoires exit 2; \ 1516008105bSBenjamin Tissoires else true; fi 1526008105bSBenjamin Tissoires 1536008105bSBenjamin Tissoires$(HID_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF) 1546008105bSBenjamin Tissoires$(src)/*.c: verify_target_bpf $(LIBBPF) 1556008105bSBenjamin Tissoires 1566008105bSBenjamin Tissoireslibbpf_hdrs: $(LIBBPF) 1576008105bSBenjamin Tissoires 1586008105bSBenjamin Tissoires.PHONY: libbpf_hdrs 1596008105bSBenjamin Tissoires 1606008105bSBenjamin Tissoires$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h 161a56a2569SBenjamin Tissoires$(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h 1626008105bSBenjamin Tissoires 1636008105bSBenjamin Tissoires-include $(HID_SAMPLES_PATH)/Makefile.target 1646008105bSBenjamin Tissoires 1656008105bSBenjamin TissoiresVMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux)) \ 1666008105bSBenjamin Tissoires $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)) \ 1676008105bSBenjamin Tissoires $(abspath ./vmlinux) 1686008105bSBenjamin TissoiresVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 1696008105bSBenjamin Tissoires 1706008105bSBenjamin Tissoires$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) 1716008105bSBenjamin Tissoiresifeq ($(VMLINUX_H),) 1726008105bSBenjamin Tissoiresifeq ($(VMLINUX_BTF),) 1736008105bSBenjamin Tissoires $(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\ 1746008105bSBenjamin Tissoires build the kernel or set VMLINUX_BTF or VMLINUX_H variable) 1756008105bSBenjamin Tissoiresendif 1766008105bSBenjamin Tissoires $(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@ 1776008105bSBenjamin Tissoireselse 1786008105bSBenjamin Tissoires $(Q)cp "$(VMLINUX_H)" $@ 1796008105bSBenjamin Tissoiresendif 1806008105bSBenjamin Tissoires 1816008105bSBenjamin Tissoiresclean-files += vmlinux.h 1826008105bSBenjamin Tissoires 1836008105bSBenjamin Tissoires# Get Clang's default includes on this system, as opposed to those seen by 184*bbaf1ff0SFangrui Song# '--target=bpf'. This fixes "missing" files on some architectures/distros, 1856008105bSBenjamin Tissoires# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. 1866008105bSBenjamin Tissoires# 1876008105bSBenjamin Tissoires# Use '-idirafter': Don't interfere with include mechanics except where the 1886008105bSBenjamin Tissoires# build would have failed anyways. 1896008105bSBenjamin Tissoiresdefine get_sys_includes 1906008105bSBenjamin Tissoires$(shell $(1) -v -E - </dev/null 2>&1 \ 1916008105bSBenjamin Tissoires | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ 1926008105bSBenjamin Tissoires$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/') 1936008105bSBenjamin Tissoiresendef 1946008105bSBenjamin Tissoires 1956008105bSBenjamin TissoiresCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) 1966008105bSBenjamin Tissoires 1976008105bSBenjamin TissoiresEXTRA_BPF_HEADERS_SRC := $(addprefix $(src)/,$(EXTRA_BPF_HEADERS)) 1986008105bSBenjamin Tissoires 1996008105bSBenjamin Tissoires$(obj)/%.bpf.o: $(src)/%.bpf.c $(EXTRA_BPF_HEADERS_SRC) $(obj)/vmlinux.h 2006008105bSBenjamin Tissoires @echo " CLANG-BPF " $@ 201*bbaf1ff0SFangrui Song $(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \ 2026008105bSBenjamin Tissoires -Wno-compare-distinct-pointer-types -I$(srctree)/include \ 2036008105bSBenjamin Tissoires -I$(srctree)/samples/bpf -I$(srctree)/tools/include \ 2046008105bSBenjamin Tissoires -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \ 2056008105bSBenjamin Tissoires -c $(filter %.bpf.c,$^) -o $@ 2066008105bSBenjamin Tissoires 207a56a2569SBenjamin TissoiresLINKED_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h 2086008105bSBenjamin Tissoiresclean-files += $(LINKED_SKELS) 2096008105bSBenjamin Tissoires 2106008105bSBenjamin Tissoireshid_mouse.skel.h-deps := hid_mouse.bpf.o hid_bpf_attach.bpf.o 211a56a2569SBenjamin Tissoireshid_surface_dial.skel.h-deps := hid_surface_dial.bpf.o hid_bpf_attach.bpf.o 2126008105bSBenjamin Tissoires 2136008105bSBenjamin TissoiresLINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps))) 2146008105bSBenjamin Tissoires 2156008105bSBenjamin TissoiresBPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c)) 2166008105bSBenjamin TissoiresBPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED)) 2176008105bSBenjamin TissoiresBPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS)) 2186008105bSBenjamin Tissoires 2196008105bSBenjamin Tissoires$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL) 2206008105bSBenjamin Tissoires @echo " BPF GEN-OBJ " $(@:.skel.h=) 2216008105bSBenjamin Tissoires $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) 2226008105bSBenjamin Tissoires @echo " BPF GEN-SKEL" $(@:.skel.h=) 2236008105bSBenjamin Tissoires $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@ 2246008105bSBenjamin Tissoires 2256008105bSBenjamin Tissoires# asm/sysreg.h - inline assembly used by it is incompatible with llvm. 2266008105bSBenjamin Tissoires# But, there is no easy way to fix it, so just exclude it since it is 2276008105bSBenjamin Tissoires# useless for BPF samples. 2286008105bSBenjamin Tissoires# below we use long chain of commands, clang | opt | llvm-dis | llc, 2296008105bSBenjamin Tissoires# to generate final object file. 'clang' compiles the source into IR 2306008105bSBenjamin Tissoires# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin 2316008105bSBenjamin Tissoires# processing (llvm12) and IR optimizations. 'llvm-dis' converts 2326008105bSBenjamin Tissoires# 'opt' output to IR, and finally 'llc' generates bpf byte code. 2336008105bSBenjamin Tissoires$(obj)/%.o: $(src)/%.c 2346008105bSBenjamin Tissoires @echo " CLANG-bpf " $@ 2356008105bSBenjamin Tissoires $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \ 2366008105bSBenjamin Tissoires -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \ 2376008105bSBenjamin Tissoires -I$(LIBBPF_INCLUDE) \ 2386008105bSBenjamin Tissoires -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \ 2396008105bSBenjamin Tissoires -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \ 2406008105bSBenjamin Tissoires -Wno-gnu-variable-sized-type-not-at-end \ 2416008105bSBenjamin Tissoires -Wno-address-of-packed-member -Wno-tautological-compare \ 2426008105bSBenjamin Tissoires -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ 2436008105bSBenjamin Tissoires -fno-asynchronous-unwind-tables \ 2446008105bSBenjamin Tissoires -I$(srctree)/samples/hid/ \ 2456008105bSBenjamin Tissoires -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \ 2466008105bSBenjamin Tissoires $(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \ 2476008105bSBenjamin Tissoires $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@ 2486008105bSBenjamin Tissoiresifeq ($(DWARF2BTF),y) 2496008105bSBenjamin Tissoires $(BTF_PAHOLE) -J $@ 2506008105bSBenjamin Tissoiresendif 251