xref: /openbmc/linux/samples/hid/Makefile (revision 856caabf)
1# SPDX-License-Identifier: GPL-2.0
2
3HID_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
4TOOLS_PATH := $(HID_SAMPLES_PATH)/../../tools
5
6pound := \#
7
8# List of programs to build
9tprogs-y += hid_mouse
10tprogs-y += hid_surface_dial
11
12# Libbpf dependencies
13LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
14LIBBPF_OUTPUT = $(abspath $(HID_SAMPLES_PATH))/libbpf
15LIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
16LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
17LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
18
19EXTRA_HEADERS := hid_bpf_attach.h
20EXTRA_BPF_HEADERS := hid_bpf_helpers.h
21
22hid_mouse-objs := hid_mouse.o
23hid_surface_dial-objs := hid_surface_dial.o
24
25# Tell kbuild to always build the programs
26always-y := $(tprogs-y)
27
28ifeq ($(ARCH), arm)
29# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
30# headers when arm instruction set identification is requested.
31ARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
32BPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
33TPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
34endif
35
36ifeq ($(ARCH), mips)
37TPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
38ifdef CONFIG_MACH_LOONGSON64
39BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
40BPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
41endif
42endif
43
44TPROGS_CFLAGS += -Wall -O2
45TPROGS_CFLAGS += -Wmissing-prototypes
46TPROGS_CFLAGS += -Wstrict-prototypes
47
48TPROGS_CFLAGS += -I$(objtree)/usr/include
49TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
50TPROGS_CFLAGS += -I$(srctree)/tools/include
51
52ifdef SYSROOT
53TPROGS_CFLAGS += --sysroot=$(SYSROOT)
54TPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
55endif
56
57TPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
58
59# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
60# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
61LLC ?= llc
62CLANG ?= clang
63OPT ?= opt
64LLVM_DIS ?= llvm-dis
65LLVM_OBJCOPY ?= llvm-objcopy
66LLVM_READELF ?= llvm-readelf
67BTF_PAHOLE ?= pahole
68
69# Detect that we're cross compiling and use the cross compiler
70ifdef CROSS_COMPILE
71CLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
72endif
73
74# Don't evaluate probes and warnings if we need to run make recursively
75ifneq ($(src),)
76HDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
77	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
78	-o /dev/null 2>/dev/null && echo okay)
79
80ifeq ($(HDR_PROBE),)
81$(warning WARNING: Detected possible issues with include path.)
82$(warning WARNING: Please install kernel headers locally (make headers_install).)
83endif
84
85BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
86BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
87BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
88BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
89			  $(CLANG) -target bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
90			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
91			  /bin/rm -f ./llvm_btf_verify.o)
92
93BPF_EXTRA_CFLAGS += -fno-stack-protector
94ifneq ($(BTF_LLVM_PROBE),)
95	BPF_EXTRA_CFLAGS += -g
96else
97ifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
98	BPF_EXTRA_CFLAGS += -g
99	LLC_FLAGS += -mattr=dwarfris
100	DWARF2BTF = y
101endif
102endif
103endif
104
105# Trick to allow make to be run from this directory
106all:
107	$(MAKE) -C ../../ M=$(CURDIR) HID_SAMPLES_PATH=$(CURDIR)
108
109clean:
110	$(MAKE) -C ../../ M=$(CURDIR) clean
111	@find $(CURDIR) -type f -name '*~' -delete
112	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
113
114$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
115# Fix up variables inherited from Kbuild that tools/ build system won't like
116	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
117		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(HID_SAMPLES_PATH)/../../ \
118		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
119		$@ install_headers
120
121BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
122BPFTOOL_OUTPUT := $(abspath $(HID_SAMPLES_PATH))/bpftool
123BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
124$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
125	$(MAKE) -C $(BPFTOOLDIR) srctree=$(HID_SAMPLES_PATH)/../../ 		\
126		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
127
128$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
129	$(call msg,MKDIR,$@)
130	$(Q)mkdir -p $@
131
132FORCE:
133
134
135# Verify LLVM compiler tools are available and bpf target is supported by llc
136.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
137
138verify_cmds: $(CLANG) $(LLC)
139	@for TOOL in $^ ; do \
140		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
141			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
142			exit 1; \
143		else true; fi; \
144	done
145
146verify_target_bpf: verify_cmds
147	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
148		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
149		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
150		exit 2; \
151	else true; fi
152
153$(HID_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
154$(src)/*.c: verify_target_bpf $(LIBBPF)
155
156libbpf_hdrs: $(LIBBPF)
157
158.PHONY: libbpf_hdrs
159
160$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h
161$(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h
162
163-include $(HID_SAMPLES_PATH)/Makefile.target
164
165VMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\
166		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\
167		     $(abspath ./vmlinux)
168VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
169
170$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
171ifeq ($(VMLINUX_H),)
172ifeq ($(VMLINUX_BTF),)
173	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
174		build the kernel or set VMLINUX_BTF or VMLINUX_H variable)
175endif
176	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
177else
178	$(Q)cp "$(VMLINUX_H)" $@
179endif
180
181clean-files += vmlinux.h
182
183# Get Clang's default includes on this system, as opposed to those seen by
184# '-target bpf'. This fixes "missing" files on some architectures/distros,
185# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
186#
187# Use '-idirafter': Don't interfere with include mechanics except where the
188# build would have failed anyways.
189define get_sys_includes
190$(shell $(1) -v -E - </dev/null 2>&1 \
191        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
192$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
193endef
194
195CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
196
197EXTRA_BPF_HEADERS_SRC := $(addprefix $(src)/,$(EXTRA_BPF_HEADERS))
198
199$(obj)/%.bpf.o: $(src)/%.bpf.c $(EXTRA_BPF_HEADERS_SRC) $(obj)/vmlinux.h
200	@echo "  CLANG-BPF " $@
201	$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
202		-Wno-compare-distinct-pointer-types -I$(srctree)/include \
203		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
204		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
205		-c $(filter %.bpf.c,$^) -o $@
206
207LINKED_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h
208clean-files += $(LINKED_SKELS)
209
210hid_mouse.skel.h-deps := hid_mouse.bpf.o hid_bpf_attach.bpf.o
211hid_surface_dial.skel.h-deps := hid_surface_dial.bpf.o hid_bpf_attach.bpf.o
212
213LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
214
215BPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
216BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
217BPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
218
219$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
220	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
221	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
222	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
223	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
224
225# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
226# But, there is no easy way to fix it, so just exclude it since it is
227# useless for BPF samples.
228# below we use long chain of commands, clang | opt | llvm-dis | llc,
229# to generate final object file. 'clang' compiles the source into IR
230# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
231# processing (llvm12) and IR optimizations. 'llvm-dis' converts
232# 'opt' output to IR, and finally 'llc' generates bpf byte code.
233$(obj)/%.o: $(src)/%.c
234	@echo "  CLANG-bpf " $@
235	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
236		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
237		-I$(LIBBPF_INCLUDE) \
238		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
239		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
240		-Wno-gnu-variable-sized-type-not-at-end \
241		-Wno-address-of-packed-member -Wno-tautological-compare \
242		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
243		-fno-asynchronous-unwind-tables \
244		-I$(srctree)/samples/hid/ \
245		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
246		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
247		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
248ifeq ($(DWARF2BTF),y)
249	$(BTF_PAHOLE) -J $@
250endif
251