162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciHID_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
462306a36Sopenharmony_ciTOOLS_PATH := $(HID_SAMPLES_PATH)/../../tools
562306a36Sopenharmony_ci
662306a36Sopenharmony_cipound := \#
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci# List of programs to build
962306a36Sopenharmony_citprogs-y += hid_mouse
1062306a36Sopenharmony_citprogs-y += hid_surface_dial
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci# Libbpf dependencies
1362306a36Sopenharmony_ciLIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
1462306a36Sopenharmony_ciLIBBPF_OUTPUT = $(abspath $(HID_SAMPLES_PATH))/libbpf
1562306a36Sopenharmony_ciLIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
1662306a36Sopenharmony_ciLIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
1762306a36Sopenharmony_ciLIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciEXTRA_HEADERS := hid_bpf_attach.h
2062306a36Sopenharmony_ciEXTRA_BPF_HEADERS := hid_bpf_helpers.h
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cihid_mouse-objs := hid_mouse.o
2362306a36Sopenharmony_cihid_surface_dial-objs := hid_surface_dial.o
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci# Tell kbuild to always build the programs
2662306a36Sopenharmony_cialways-y := $(tprogs-y)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciifeq ($(ARCH), arm)
2962306a36Sopenharmony_ci# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
3062306a36Sopenharmony_ci# headers when arm instruction set identification is requested.
3162306a36Sopenharmony_ciARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
3262306a36Sopenharmony_ciBPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
3362306a36Sopenharmony_ciTPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
3462306a36Sopenharmony_ciendif
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciifeq ($(ARCH), mips)
3762306a36Sopenharmony_ciTPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
3862306a36Sopenharmony_ciifdef CONFIG_MACH_LOONGSON64
3962306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
4062306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
4162306a36Sopenharmony_ciendif
4262306a36Sopenharmony_ciendif
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciTPROGS_CFLAGS += -Wall -O2
4562306a36Sopenharmony_ciTPROGS_CFLAGS += -Wmissing-prototypes
4662306a36Sopenharmony_ciTPROGS_CFLAGS += -Wstrict-prototypes
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(objtree)/usr/include
4962306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
5062306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(srctree)/tools/include
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciifdef SYSROOT
5362306a36Sopenharmony_ciTPROGS_CFLAGS += --sysroot=$(SYSROOT)
5462306a36Sopenharmony_ciTPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
5562306a36Sopenharmony_ciendif
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciTPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
6062306a36Sopenharmony_ci# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
6162306a36Sopenharmony_ciLLC ?= llc
6262306a36Sopenharmony_ciCLANG ?= clang
6362306a36Sopenharmony_ciOPT ?= opt
6462306a36Sopenharmony_ciLLVM_DIS ?= llvm-dis
6562306a36Sopenharmony_ciLLVM_OBJCOPY ?= llvm-objcopy
6662306a36Sopenharmony_ciLLVM_READELF ?= llvm-readelf
6762306a36Sopenharmony_ciBTF_PAHOLE ?= pahole
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci# Detect that we're cross compiling and use the cross compiler
7062306a36Sopenharmony_ciifdef CROSS_COMPILE
7162306a36Sopenharmony_ciCLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
7262306a36Sopenharmony_ciendif
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci# Don't evaluate probes and warnings if we need to run make recursively
7562306a36Sopenharmony_ciifneq ($(src),)
7662306a36Sopenharmony_ciHDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
7762306a36Sopenharmony_ci	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
7862306a36Sopenharmony_ci	-o /dev/null 2>/dev/null && echo okay)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ciifeq ($(HDR_PROBE),)
8162306a36Sopenharmony_ci$(warning WARNING: Detected possible issues with include path.)
8262306a36Sopenharmony_ci$(warning WARNING: Please install kernel headers locally (make headers_install).)
8362306a36Sopenharmony_ciendif
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciBTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
8662306a36Sopenharmony_ciBTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
8762306a36Sopenharmony_ciBTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
8862306a36Sopenharmony_ciBTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
8962306a36Sopenharmony_ci			  $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
9062306a36Sopenharmony_ci			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
9162306a36Sopenharmony_ci			  /bin/rm -f ./llvm_btf_verify.o)
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -fno-stack-protector
9462306a36Sopenharmony_ciifneq ($(BTF_LLVM_PROBE),)
9562306a36Sopenharmony_ci	BPF_EXTRA_CFLAGS += -g
9662306a36Sopenharmony_cielse
9762306a36Sopenharmony_ciifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
9862306a36Sopenharmony_ci	BPF_EXTRA_CFLAGS += -g
9962306a36Sopenharmony_ci	LLC_FLAGS += -mattr=dwarfris
10062306a36Sopenharmony_ci	DWARF2BTF = y
10162306a36Sopenharmony_ciendif
10262306a36Sopenharmony_ciendif
10362306a36Sopenharmony_ciendif
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci# Trick to allow make to be run from this directory
10662306a36Sopenharmony_ciall:
10762306a36Sopenharmony_ci	$(MAKE) -C ../../ M=$(CURDIR) HID_SAMPLES_PATH=$(CURDIR)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciclean:
11062306a36Sopenharmony_ci	$(MAKE) -C ../../ M=$(CURDIR) clean
11162306a36Sopenharmony_ci	@find $(CURDIR) -type f -name '*~' -delete
11262306a36Sopenharmony_ci	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
11562306a36Sopenharmony_ci# Fix up variables inherited from Kbuild that tools/ build system won't like
11662306a36Sopenharmony_ci	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
11762306a36Sopenharmony_ci		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(HID_SAMPLES_PATH)/../../ \
11862306a36Sopenharmony_ci		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
11962306a36Sopenharmony_ci		$@ install_headers
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciBPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
12262306a36Sopenharmony_ciBPFTOOL_OUTPUT := $(abspath $(HID_SAMPLES_PATH))/bpftool
12362306a36Sopenharmony_ciBPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
12462306a36Sopenharmony_ci$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
12562306a36Sopenharmony_ci	$(MAKE) -C $(BPFTOOLDIR) srctree=$(HID_SAMPLES_PATH)/../../ 		\
12662306a36Sopenharmony_ci		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
12962306a36Sopenharmony_ci	$(call msg,MKDIR,$@)
13062306a36Sopenharmony_ci	$(Q)mkdir -p $@
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciFORCE:
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci# Verify LLVM compiler tools are available and bpf target is supported by llc
13662306a36Sopenharmony_ci.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_civerify_cmds: $(CLANG) $(LLC)
13962306a36Sopenharmony_ci	@for TOOL in $^ ; do \
14062306a36Sopenharmony_ci		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
14162306a36Sopenharmony_ci			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
14262306a36Sopenharmony_ci			exit 1; \
14362306a36Sopenharmony_ci		else true; fi; \
14462306a36Sopenharmony_ci	done
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_civerify_target_bpf: verify_cmds
14762306a36Sopenharmony_ci	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
14862306a36Sopenharmony_ci		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
14962306a36Sopenharmony_ci		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
15062306a36Sopenharmony_ci		exit 2; \
15162306a36Sopenharmony_ci	else true; fi
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci$(HID_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
15462306a36Sopenharmony_ci$(src)/*.c: verify_target_bpf $(LIBBPF)
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cilibbpf_hdrs: $(LIBBPF)
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci.PHONY: libbpf_hdrs
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h
16162306a36Sopenharmony_ci$(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci-include $(HID_SAMPLES_PATH)/Makefile.target
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciVMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\
16662306a36Sopenharmony_ci		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\
16762306a36Sopenharmony_ci		     $(abspath ./vmlinux)
16862306a36Sopenharmony_ciVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
17162306a36Sopenharmony_ciifeq ($(VMLINUX_H),)
17262306a36Sopenharmony_ciifeq ($(VMLINUX_BTF),)
17362306a36Sopenharmony_ci	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
17462306a36Sopenharmony_ci		build the kernel or set VMLINUX_BTF or VMLINUX_H variable)
17562306a36Sopenharmony_ciendif
17662306a36Sopenharmony_ci	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
17762306a36Sopenharmony_cielse
17862306a36Sopenharmony_ci	$(Q)cp "$(VMLINUX_H)" $@
17962306a36Sopenharmony_ciendif
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciclean-files += vmlinux.h
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci# Get Clang's default includes on this system, as opposed to those seen by
18462306a36Sopenharmony_ci# '--target=bpf'. This fixes "missing" files on some architectures/distros,
18562306a36Sopenharmony_ci# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
18662306a36Sopenharmony_ci#
18762306a36Sopenharmony_ci# Use '-idirafter': Don't interfere with include mechanics except where the
18862306a36Sopenharmony_ci# build would have failed anyways.
18962306a36Sopenharmony_cidefine get_sys_includes
19062306a36Sopenharmony_ci$(shell $(1) -v -E - </dev/null 2>&1 \
19162306a36Sopenharmony_ci        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
19262306a36Sopenharmony_ci$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
19362306a36Sopenharmony_ciendef
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciEXTRA_BPF_HEADERS_SRC := $(addprefix $(src)/,$(EXTRA_BPF_HEADERS))
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci$(obj)/%.bpf.o: $(src)/%.bpf.c $(EXTRA_BPF_HEADERS_SRC) $(obj)/vmlinux.h
20062306a36Sopenharmony_ci	@echo "  CLANG-BPF " $@
20162306a36Sopenharmony_ci	$(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \
20262306a36Sopenharmony_ci		-Wno-compare-distinct-pointer-types -I$(srctree)/include \
20362306a36Sopenharmony_ci		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
20462306a36Sopenharmony_ci		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
20562306a36Sopenharmony_ci		-c $(filter %.bpf.c,$^) -o $@
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ciLINKED_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h
20862306a36Sopenharmony_ciclean-files += $(LINKED_SKELS)
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cihid_mouse.skel.h-deps := hid_mouse.bpf.o hid_bpf_attach.bpf.o
21162306a36Sopenharmony_cihid_surface_dial.skel.h-deps := hid_surface_dial.bpf.o hid_bpf_attach.bpf.o
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciLINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciBPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
21662306a36Sopenharmony_ciBPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
21762306a36Sopenharmony_ciBPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
22062306a36Sopenharmony_ci	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
22162306a36Sopenharmony_ci	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
22262306a36Sopenharmony_ci	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
22362306a36Sopenharmony_ci	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
22662306a36Sopenharmony_ci# But, there is no easy way to fix it, so just exclude it since it is
22762306a36Sopenharmony_ci# useless for BPF samples.
22862306a36Sopenharmony_ci# below we use long chain of commands, clang | opt | llvm-dis | llc,
22962306a36Sopenharmony_ci# to generate final object file. 'clang' compiles the source into IR
23062306a36Sopenharmony_ci# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
23162306a36Sopenharmony_ci# processing (llvm12) and IR optimizations. 'llvm-dis' converts
23262306a36Sopenharmony_ci# 'opt' output to IR, and finally 'llc' generates bpf byte code.
23362306a36Sopenharmony_ci$(obj)/%.o: $(src)/%.c
23462306a36Sopenharmony_ci	@echo "  CLANG-bpf " $@
23562306a36Sopenharmony_ci	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
23662306a36Sopenharmony_ci		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
23762306a36Sopenharmony_ci		-I$(LIBBPF_INCLUDE) \
23862306a36Sopenharmony_ci		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
23962306a36Sopenharmony_ci		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
24062306a36Sopenharmony_ci		-Wno-gnu-variable-sized-type-not-at-end \
24162306a36Sopenharmony_ci		-Wno-address-of-packed-member -Wno-tautological-compare \
24262306a36Sopenharmony_ci		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
24362306a36Sopenharmony_ci		-fno-asynchronous-unwind-tables \
24462306a36Sopenharmony_ci		-I$(srctree)/samples/hid/ \
24562306a36Sopenharmony_ci		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
24662306a36Sopenharmony_ci		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
24762306a36Sopenharmony_ci		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
24862306a36Sopenharmony_ciifeq ($(DWARF2BTF),y)
24962306a36Sopenharmony_ci	$(BTF_PAHOLE) -J $@
25062306a36Sopenharmony_ciendif
251