xref: /kernel/linux/linux-6.6/samples/bpf/Makefile (revision 62306a36)
162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciBPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
462306a36Sopenharmony_ciTOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
562306a36Sopenharmony_ci
662306a36Sopenharmony_cipound := \#
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci# List of programs to build
962306a36Sopenharmony_citprogs-y := test_lru_dist
1062306a36Sopenharmony_citprogs-y += sock_example
1162306a36Sopenharmony_citprogs-y += fds_example
1262306a36Sopenharmony_citprogs-y += sockex1
1362306a36Sopenharmony_citprogs-y += sockex2
1462306a36Sopenharmony_citprogs-y += sockex3
1562306a36Sopenharmony_citprogs-y += tracex1
1662306a36Sopenharmony_citprogs-y += tracex2
1762306a36Sopenharmony_citprogs-y += tracex3
1862306a36Sopenharmony_citprogs-y += tracex4
1962306a36Sopenharmony_citprogs-y += tracex5
2062306a36Sopenharmony_citprogs-y += tracex6
2162306a36Sopenharmony_citprogs-y += tracex7
2262306a36Sopenharmony_citprogs-y += test_probe_write_user
2362306a36Sopenharmony_citprogs-y += trace_output
2462306a36Sopenharmony_citprogs-y += lathist
2562306a36Sopenharmony_citprogs-y += offwaketime
2662306a36Sopenharmony_citprogs-y += spintest
2762306a36Sopenharmony_citprogs-y += map_perf_test
2862306a36Sopenharmony_citprogs-y += test_overhead
2962306a36Sopenharmony_citprogs-y += test_cgrp2_array_pin
3062306a36Sopenharmony_citprogs-y += test_cgrp2_attach
3162306a36Sopenharmony_citprogs-y += test_cgrp2_sock
3262306a36Sopenharmony_citprogs-y += test_cgrp2_sock2
3362306a36Sopenharmony_citprogs-y += xdp_router_ipv4
3462306a36Sopenharmony_citprogs-y += test_current_task_under_cgroup
3562306a36Sopenharmony_citprogs-y += trace_event
3662306a36Sopenharmony_citprogs-y += sampleip
3762306a36Sopenharmony_citprogs-y += tc_l2_redirect
3862306a36Sopenharmony_citprogs-y += lwt_len_hist
3962306a36Sopenharmony_citprogs-y += xdp_tx_iptunnel
4062306a36Sopenharmony_citprogs-y += test_map_in_map
4162306a36Sopenharmony_citprogs-y += per_socket_stats_example
4262306a36Sopenharmony_citprogs-y += syscall_tp
4362306a36Sopenharmony_citprogs-y += cpustat
4462306a36Sopenharmony_citprogs-y += xdp_adjust_tail
4562306a36Sopenharmony_citprogs-y += xdp_fwd
4662306a36Sopenharmony_citprogs-y += task_fd_query
4762306a36Sopenharmony_citprogs-y += ibumad
4862306a36Sopenharmony_citprogs-y += hbm
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci# Libbpf dependencies
5162306a36Sopenharmony_ciLIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
5262306a36Sopenharmony_ciLIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
5362306a36Sopenharmony_ciLIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
5462306a36Sopenharmony_ciLIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
5562306a36Sopenharmony_ciLIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciCGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
5862306a36Sopenharmony_ciTRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
5962306a36Sopenharmony_ciXDP_SAMPLE := xdp_sample_user.o
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cifds_example-objs := fds_example.o
6262306a36Sopenharmony_cisockex1-objs := sockex1_user.o
6362306a36Sopenharmony_cisockex2-objs := sockex2_user.o
6462306a36Sopenharmony_cisockex3-objs := sockex3_user.o
6562306a36Sopenharmony_citracex1-objs := tracex1_user.o $(TRACE_HELPERS)
6662306a36Sopenharmony_citracex2-objs := tracex2_user.o
6762306a36Sopenharmony_citracex3-objs := tracex3_user.o
6862306a36Sopenharmony_citracex4-objs := tracex4_user.o
6962306a36Sopenharmony_citracex5-objs := tracex5_user.o $(TRACE_HELPERS)
7062306a36Sopenharmony_citracex6-objs := tracex6_user.o
7162306a36Sopenharmony_citracex7-objs := tracex7_user.o
7262306a36Sopenharmony_citest_probe_write_user-objs := test_probe_write_user_user.o
7362306a36Sopenharmony_citrace_output-objs := trace_output_user.o
7462306a36Sopenharmony_cilathist-objs := lathist_user.o
7562306a36Sopenharmony_cioffwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
7662306a36Sopenharmony_cispintest-objs := spintest_user.o $(TRACE_HELPERS)
7762306a36Sopenharmony_cimap_perf_test-objs := map_perf_test_user.o
7862306a36Sopenharmony_citest_overhead-objs := test_overhead_user.o
7962306a36Sopenharmony_citest_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
8062306a36Sopenharmony_citest_cgrp2_attach-objs := test_cgrp2_attach.o
8162306a36Sopenharmony_citest_cgrp2_sock-objs := test_cgrp2_sock.o
8262306a36Sopenharmony_citest_cgrp2_sock2-objs := test_cgrp2_sock2.o
8362306a36Sopenharmony_citest_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \
8462306a36Sopenharmony_ci				       test_current_task_under_cgroup_user.o
8562306a36Sopenharmony_citrace_event-objs := trace_event_user.o $(TRACE_HELPERS)
8662306a36Sopenharmony_cisampleip-objs := sampleip_user.o $(TRACE_HELPERS)
8762306a36Sopenharmony_citc_l2_redirect-objs := tc_l2_redirect_user.o
8862306a36Sopenharmony_cilwt_len_hist-objs := lwt_len_hist_user.o
8962306a36Sopenharmony_cixdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
9062306a36Sopenharmony_citest_map_in_map-objs := test_map_in_map_user.o
9162306a36Sopenharmony_ciper_socket_stats_example-objs := cookie_uid_helper_example.o
9262306a36Sopenharmony_cisyscall_tp-objs := syscall_tp_user.o
9362306a36Sopenharmony_cicpustat-objs := cpustat_user.o
9462306a36Sopenharmony_cixdp_adjust_tail-objs := xdp_adjust_tail_user.o
9562306a36Sopenharmony_cixdp_fwd-objs := xdp_fwd_user.o
9662306a36Sopenharmony_citask_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
9762306a36Sopenharmony_ciibumad-objs := ibumad_user.o
9862306a36Sopenharmony_cihbm-objs := hbm.o $(CGROUP_HELPERS)
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_cixdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE)
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci# Tell kbuild to always build the programs
10362306a36Sopenharmony_cialways-y := $(tprogs-y)
10462306a36Sopenharmony_cialways-y += sockex1_kern.o
10562306a36Sopenharmony_cialways-y += sockex2_kern.o
10662306a36Sopenharmony_cialways-y += sockex3_kern.o
10762306a36Sopenharmony_cialways-y += tracex1.bpf.o
10862306a36Sopenharmony_cialways-y += tracex2.bpf.o
10962306a36Sopenharmony_cialways-y += tracex3.bpf.o
11062306a36Sopenharmony_cialways-y += tracex4.bpf.o
11162306a36Sopenharmony_cialways-y += tracex5.bpf.o
11262306a36Sopenharmony_cialways-y += tracex6.bpf.o
11362306a36Sopenharmony_cialways-y += tracex7.bpf.o
11462306a36Sopenharmony_cialways-y += sock_flags.bpf.o
11562306a36Sopenharmony_cialways-y += test_probe_write_user.bpf.o
11662306a36Sopenharmony_cialways-y += trace_output.bpf.o
11762306a36Sopenharmony_cialways-y += tcbpf1_kern.o
11862306a36Sopenharmony_cialways-y += tc_l2_redirect_kern.o
11962306a36Sopenharmony_cialways-y += lathist_kern.o
12062306a36Sopenharmony_cialways-y += offwaketime.bpf.o
12162306a36Sopenharmony_cialways-y += spintest.bpf.o
12262306a36Sopenharmony_cialways-y += map_perf_test.bpf.o
12362306a36Sopenharmony_cialways-y += test_overhead_tp.bpf.o
12462306a36Sopenharmony_cialways-y += test_overhead_raw_tp.bpf.o
12562306a36Sopenharmony_cialways-y += test_overhead_kprobe.bpf.o
12662306a36Sopenharmony_cialways-y += parse_varlen.o parse_simple.o parse_ldabs.o
12762306a36Sopenharmony_cialways-y += test_cgrp2_tc.bpf.o
12862306a36Sopenharmony_cialways-y += test_current_task_under_cgroup.bpf.o
12962306a36Sopenharmony_cialways-y += trace_event_kern.o
13062306a36Sopenharmony_cialways-y += sampleip_kern.o
13162306a36Sopenharmony_cialways-y += lwt_len_hist.bpf.o
13262306a36Sopenharmony_cialways-y += xdp_tx_iptunnel_kern.o
13362306a36Sopenharmony_cialways-y += test_map_in_map.bpf.o
13462306a36Sopenharmony_cialways-y += tcp_synrto_kern.o
13562306a36Sopenharmony_cialways-y += tcp_rwnd_kern.o
13662306a36Sopenharmony_cialways-y += tcp_bufs_kern.o
13762306a36Sopenharmony_cialways-y += tcp_cong_kern.o
13862306a36Sopenharmony_cialways-y += tcp_iw_kern.o
13962306a36Sopenharmony_cialways-y += tcp_clamp_kern.o
14062306a36Sopenharmony_cialways-y += tcp_basertt_kern.o
14162306a36Sopenharmony_cialways-y += tcp_tos_reflect_kern.o
14262306a36Sopenharmony_cialways-y += tcp_dumpstats_kern.o
14362306a36Sopenharmony_cialways-y += xdp2skb_meta_kern.o
14462306a36Sopenharmony_cialways-y += syscall_tp_kern.o
14562306a36Sopenharmony_cialways-y += cpustat_kern.o
14662306a36Sopenharmony_cialways-y += xdp_adjust_tail_kern.o
14762306a36Sopenharmony_cialways-y += xdp_fwd_kern.o
14862306a36Sopenharmony_cialways-y += task_fd_query_kern.o
14962306a36Sopenharmony_cialways-y += ibumad_kern.o
15062306a36Sopenharmony_cialways-y += hbm_out_kern.o
15162306a36Sopenharmony_cialways-y += hbm_edt_kern.o
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciifeq ($(ARCH), arm)
15462306a36Sopenharmony_ci# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
15562306a36Sopenharmony_ci# headers when arm instruction set identification is requested.
15662306a36Sopenharmony_ciARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
15762306a36Sopenharmony_ciBPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
15862306a36Sopenharmony_ciTPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
15962306a36Sopenharmony_ciendif
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ciifeq ($(ARCH), mips)
16262306a36Sopenharmony_ciTPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
16362306a36Sopenharmony_ciifdef CONFIG_MACH_LOONGSON64
16462306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
16562306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
16662306a36Sopenharmony_ciendif
16762306a36Sopenharmony_ciendif
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciTPROGS_CFLAGS += -Wall -O2
17062306a36Sopenharmony_ciTPROGS_CFLAGS += -Wmissing-prototypes
17162306a36Sopenharmony_ciTPROGS_CFLAGS += -Wstrict-prototypes
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(objtree)/usr/include
17462306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
17562306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
17662306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(srctree)/tools/include
17762306a36Sopenharmony_ciTPROGS_CFLAGS += -I$(srctree)/tools/perf
17862306a36Sopenharmony_ciTPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciifdef SYSROOT
18162306a36Sopenharmony_ciTPROGS_CFLAGS += --sysroot=$(SYSROOT)
18262306a36Sopenharmony_ciTPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
18362306a36Sopenharmony_ciendif
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ciTPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
18662306a36Sopenharmony_ciTPROGLDLIBS_xdp_router_ipv4	+= -lm -pthread
18762306a36Sopenharmony_ciTPROGLDLIBS_tracex4		+= -lrt
18862306a36Sopenharmony_ciTPROGLDLIBS_trace_output	+= -lrt
18962306a36Sopenharmony_ciTPROGLDLIBS_map_perf_test	+= -lrt
19062306a36Sopenharmony_ciTPROGLDLIBS_test_overhead	+= -lrt
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
19362306a36Sopenharmony_ci# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
19462306a36Sopenharmony_ciLLC ?= llc
19562306a36Sopenharmony_ciCLANG ?= clang
19662306a36Sopenharmony_ciOPT ?= opt
19762306a36Sopenharmony_ciLLVM_DIS ?= llvm-dis
19862306a36Sopenharmony_ciLLVM_OBJCOPY ?= llvm-objcopy
19962306a36Sopenharmony_ciLLVM_READELF ?= llvm-readelf
20062306a36Sopenharmony_ciBTF_PAHOLE ?= pahole
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci# Detect that we're cross compiling and use the cross compiler
20362306a36Sopenharmony_ciifdef CROSS_COMPILE
20462306a36Sopenharmony_ciCLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
20562306a36Sopenharmony_ciendif
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci# Don't evaluate probes and warnings if we need to run make recursively
20862306a36Sopenharmony_ciifneq ($(src),)
20962306a36Sopenharmony_ciHDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
21062306a36Sopenharmony_ci	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
21162306a36Sopenharmony_ci	-o /dev/null 2>/dev/null && echo okay)
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciifeq ($(HDR_PROBE),)
21462306a36Sopenharmony_ci$(warning WARNING: Detected possible issues with include path.)
21562306a36Sopenharmony_ci$(warning WARNING: Please install kernel headers locally (make headers_install).)
21662306a36Sopenharmony_ciendif
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciBTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
21962306a36Sopenharmony_ciBTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
22062306a36Sopenharmony_ciBTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
22162306a36Sopenharmony_ciBTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
22262306a36Sopenharmony_ci			  $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
22362306a36Sopenharmony_ci			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
22462306a36Sopenharmony_ci			  /bin/rm -f ./llvm_btf_verify.o)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ciBPF_EXTRA_CFLAGS += -fno-stack-protector
22762306a36Sopenharmony_ciifneq ($(BTF_LLVM_PROBE),)
22862306a36Sopenharmony_ci	BPF_EXTRA_CFLAGS += -g
22962306a36Sopenharmony_cielse
23062306a36Sopenharmony_ciifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
23162306a36Sopenharmony_ci	BPF_EXTRA_CFLAGS += -g
23262306a36Sopenharmony_ci	LLC_FLAGS += -mattr=dwarfris
23362306a36Sopenharmony_ci	DWARF2BTF = y
23462306a36Sopenharmony_ciendif
23562306a36Sopenharmony_ciendif
23662306a36Sopenharmony_ciendif
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci# Trick to allow make to be run from this directory
23962306a36Sopenharmony_ciall:
24062306a36Sopenharmony_ci	$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciclean:
24362306a36Sopenharmony_ci	$(MAKE) -C ../../ M=$(CURDIR) clean
24462306a36Sopenharmony_ci	@find $(CURDIR) -type f -name '*~' -delete
24562306a36Sopenharmony_ci	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
24862306a36Sopenharmony_ci# Fix up variables inherited from Kbuild that tools/ build system won't like
24962306a36Sopenharmony_ci	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
25062306a36Sopenharmony_ci		LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ \
25162306a36Sopenharmony_ci		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
25262306a36Sopenharmony_ci		$@ install_headers
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ciBPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
25562306a36Sopenharmony_ciBPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
25662306a36Sopenharmony_ciBPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
25762306a36Sopenharmony_ci$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
25862306a36Sopenharmony_ci	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
25962306a36Sopenharmony_ci		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
26262306a36Sopenharmony_ci	$(call msg,MKDIR,$@)
26362306a36Sopenharmony_ci	$(Q)mkdir -p $@
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci$(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE
26662306a36Sopenharmony_ci	$(call filechk,offsets,__SYSCALL_NRS_H__)
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_citargets += syscall_nrs.s
26962306a36Sopenharmony_ciclean-files += syscall_nrs.h
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ciFORCE:
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci# Verify LLVM compiler tools are available and bpf target is supported by llc
27562306a36Sopenharmony_ci.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_civerify_cmds: $(CLANG) $(LLC)
27862306a36Sopenharmony_ci	@for TOOL in $^ ; do \
27962306a36Sopenharmony_ci		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
28062306a36Sopenharmony_ci			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
28162306a36Sopenharmony_ci			exit 1; \
28262306a36Sopenharmony_ci		else true; fi; \
28362306a36Sopenharmony_ci	done
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_civerify_target_bpf: verify_cmds
28662306a36Sopenharmony_ci	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
28762306a36Sopenharmony_ci		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
28862306a36Sopenharmony_ci		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
28962306a36Sopenharmony_ci		exit 2; \
29062306a36Sopenharmony_ci	else true; fi
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
29362306a36Sopenharmony_ci$(src)/*.c: verify_target_bpf $(LIBBPF)
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_cilibbpf_hdrs: $(LIBBPF)
29662306a36Sopenharmony_ci$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci.PHONY: libbpf_hdrs
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci$(obj)/xdp_router_ipv4_user.o: $(obj)/xdp_router_ipv4.skel.h
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci$(obj)/tracex5.bpf.o: $(obj)/syscall_nrs.h
30362306a36Sopenharmony_ci$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
30462306a36Sopenharmony_ci$(obj)/hbm.o: $(src)/hbm.h
30562306a36Sopenharmony_ci$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
30862306a36Sopenharmony_ci# TPROGS_CFLAGS causes conflicts
30962306a36Sopenharmony_ciXDP_SAMPLE_CFLAGS += -Wall -O2 \
31062306a36Sopenharmony_ci		     -I$(src)/../../tools/include \
31162306a36Sopenharmony_ci		     -I$(src)/../../tools/include/uapi \
31262306a36Sopenharmony_ci		     -I$(LIBBPF_INCLUDE) \
31362306a36Sopenharmony_ci		     -I$(src)/../../tools/testing/selftests/bpf
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS)
31662306a36Sopenharmony_ci$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci-include $(BPF_SAMPLES_PATH)/Makefile.target
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ciVMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\
32162306a36Sopenharmony_ci		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\
32262306a36Sopenharmony_ci		     $(abspath ./vmlinux)
32362306a36Sopenharmony_ciVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
32662306a36Sopenharmony_ciifeq ($(VMLINUX_H),)
32762306a36Sopenharmony_ciifeq ($(VMLINUX_BTF),)
32862306a36Sopenharmony_ci	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
32962306a36Sopenharmony_ci		build the kernel or set VMLINUX_BTF or VMLINUX_H variable)
33062306a36Sopenharmony_ciendif
33162306a36Sopenharmony_ci	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
33262306a36Sopenharmony_cielse
33362306a36Sopenharmony_ci	$(Q)cp "$(VMLINUX_H)" $@
33462306a36Sopenharmony_ciendif
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ciclean-files += vmlinux.h
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci# Get Clang's default includes on this system, as opposed to those seen by
33962306a36Sopenharmony_ci# '--target=bpf'. This fixes "missing" files on some architectures/distros,
34062306a36Sopenharmony_ci# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
34162306a36Sopenharmony_ci#
34262306a36Sopenharmony_ci# Use '-idirafter': Don't interfere with include mechanics except where the
34362306a36Sopenharmony_ci# build would have failed anyways.
34462306a36Sopenharmony_cidefine get_sys_includes
34562306a36Sopenharmony_ci$(shell $(1) -v -E - </dev/null 2>&1 \
34662306a36Sopenharmony_ci        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
34762306a36Sopenharmony_ci$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
34862306a36Sopenharmony_ciendef
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ciCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci$(obj)/xdp_router_ipv4.bpf.o: $(obj)/xdp_sample.bpf.o
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
35562306a36Sopenharmony_ci	@echo "  CLANG-BPF " $@
35662306a36Sopenharmony_ci	$(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \
35762306a36Sopenharmony_ci		-Wno-compare-distinct-pointer-types -I$(srctree)/include \
35862306a36Sopenharmony_ci		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
35962306a36Sopenharmony_ci		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
36062306a36Sopenharmony_ci		-c $(filter %.bpf.c,$^) -o $@
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ciLINKED_SKELS := xdp_router_ipv4.skel.h
36362306a36Sopenharmony_ciclean-files += $(LINKED_SKELS)
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_cixdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ciLINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ciBPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
37062306a36Sopenharmony_ciBPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
37162306a36Sopenharmony_ciBPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
37462306a36Sopenharmony_ci	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
37562306a36Sopenharmony_ci	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
37662306a36Sopenharmony_ci	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
37762306a36Sopenharmony_ci	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
38062306a36Sopenharmony_ci# But, there is no easy way to fix it, so just exclude it since it is
38162306a36Sopenharmony_ci# useless for BPF samples.
38262306a36Sopenharmony_ci# below we use long chain of commands, clang | opt | llvm-dis | llc,
38362306a36Sopenharmony_ci# to generate final object file. 'clang' compiles the source into IR
38462306a36Sopenharmony_ci# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
38562306a36Sopenharmony_ci# processing (llvm12) and IR optimizations. 'llvm-dis' converts
38662306a36Sopenharmony_ci# 'opt' output to IR, and finally 'llc' generates bpf byte code.
38762306a36Sopenharmony_ci$(obj)/%.o: $(src)/%.c
38862306a36Sopenharmony_ci	@echo "  CLANG-bpf " $@
38962306a36Sopenharmony_ci	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
39062306a36Sopenharmony_ci		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
39162306a36Sopenharmony_ci		-I$(LIBBPF_INCLUDE) \
39262306a36Sopenharmony_ci		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
39362306a36Sopenharmony_ci		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
39462306a36Sopenharmony_ci		-Wno-gnu-variable-sized-type-not-at-end \
39562306a36Sopenharmony_ci		-Wno-address-of-packed-member -Wno-tautological-compare \
39662306a36Sopenharmony_ci		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
39762306a36Sopenharmony_ci		-fno-asynchronous-unwind-tables -fcf-protection \
39862306a36Sopenharmony_ci		-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
39962306a36Sopenharmony_ci		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
40062306a36Sopenharmony_ci		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
40162306a36Sopenharmony_ci		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
40262306a36Sopenharmony_ciifeq ($(DWARF2BTF),y)
40362306a36Sopenharmony_ci	$(BTF_PAHOLE) -J $@
40462306a36Sopenharmony_ciendif
405