18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ciinclude ../../../../scripts/Kbuild.include
38c2ecf20Sopenharmony_ciinclude ../../../scripts/Makefile.arch
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciCXX ?= $(CROSS_COMPILE)g++
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciCURDIR := $(abspath .)
88c2ecf20Sopenharmony_ciTOOLSDIR := $(abspath ../../..)
98c2ecf20Sopenharmony_ciLIBDIR := $(TOOLSDIR)/lib
108c2ecf20Sopenharmony_ciBPFDIR := $(LIBDIR)/bpf
118c2ecf20Sopenharmony_ciTOOLSINCDIR := $(TOOLSDIR)/include
128c2ecf20Sopenharmony_ciBPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
138c2ecf20Sopenharmony_ciAPIDIR := $(TOOLSINCDIR)/uapi
148c2ecf20Sopenharmony_ciGENDIR := $(abspath ../../../../include/generated)
158c2ecf20Sopenharmony_ciGENHDR := $(GENDIR)/autoconf.h
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciifneq ($(wildcard $(GENHDR)),)
188c2ecf20Sopenharmony_ci  GENFLAGS := -DHAVE_GENHDR
198c2ecf20Sopenharmony_ciendif
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciCLANG		?= clang
228c2ecf20Sopenharmony_ciLLC		?= llc
238c2ecf20Sopenharmony_ciLLVM_OBJCOPY	?= llvm-objcopy
248c2ecf20Sopenharmony_ciBPF_GCC		?= $(shell command -v bpf-gcc;)
258c2ecf20Sopenharmony_ciSAN_CFLAGS	?=
268c2ecf20Sopenharmony_ciCFLAGS += -g -rdynamic -Wall -O2 $(GENFLAGS) $(SAN_CFLAGS)		\
278c2ecf20Sopenharmony_ci	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
288c2ecf20Sopenharmony_ci	  -I$(TOOLSINCDIR) -I$(APIDIR)					\
298c2ecf20Sopenharmony_ci	  -Dbpf_prog_load=bpf_prog_test_load				\
308c2ecf20Sopenharmony_ci	  -Dbpf_load_program=bpf_test_load_program
318c2ecf20Sopenharmony_ciLDLIBS += -lcap -lelf -lz -lrt -lpthread
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci# Order correspond to 'make run_tests' order
348c2ecf20Sopenharmony_ciTEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
358c2ecf20Sopenharmony_ci	test_verifier_log test_dev_cgroup test_tcpbpf_user \
368c2ecf20Sopenharmony_ci	test_sock test_sockmap get_cgroup_id_user test_socket_cookie \
378c2ecf20Sopenharmony_ci	test_cgroup_storage \
388c2ecf20Sopenharmony_ci	test_netcnt test_tcpnotify_user test_sysctl \
398c2ecf20Sopenharmony_ci	test_progs-no_alu32 \
408c2ecf20Sopenharmony_ci	test_current_pid_tgid_new_ns
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci# Also test bpf-gcc, if present
438c2ecf20Sopenharmony_ciifneq ($(BPF_GCC),)
448c2ecf20Sopenharmony_ciTEST_GEN_PROGS += test_progs-bpf_gcc
458c2ecf20Sopenharmony_ciendif
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciTEST_GEN_FILES =
488c2ecf20Sopenharmony_ciTEST_FILES = test_lwt_ip_encap.o \
498c2ecf20Sopenharmony_ci	test_tc_edt.o
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci# Order correspond to 'make run_tests' order
528c2ecf20Sopenharmony_ciTEST_PROGS := test_kmod.sh \
538c2ecf20Sopenharmony_ci	test_xdp_redirect.sh \
548c2ecf20Sopenharmony_ci	test_xdp_meta.sh \
558c2ecf20Sopenharmony_ci	test_xdp_veth.sh \
568c2ecf20Sopenharmony_ci	test_offload.py \
578c2ecf20Sopenharmony_ci	test_sock_addr.sh \
588c2ecf20Sopenharmony_ci	test_tunnel.sh \
598c2ecf20Sopenharmony_ci	test_lwt_seg6local.sh \
608c2ecf20Sopenharmony_ci	test_lirc_mode2.sh \
618c2ecf20Sopenharmony_ci	test_skb_cgroup_id.sh \
628c2ecf20Sopenharmony_ci	test_flow_dissector.sh \
638c2ecf20Sopenharmony_ci	test_xdp_vlan_mode_generic.sh \
648c2ecf20Sopenharmony_ci	test_xdp_vlan_mode_native.sh \
658c2ecf20Sopenharmony_ci	test_lwt_ip_encap.sh \
668c2ecf20Sopenharmony_ci	test_tcp_check_syncookie.sh \
678c2ecf20Sopenharmony_ci	test_tc_tunnel.sh \
688c2ecf20Sopenharmony_ci	test_tc_edt.sh \
698c2ecf20Sopenharmony_ci	test_xdping.sh \
708c2ecf20Sopenharmony_ci	test_bpftool_build.sh \
718c2ecf20Sopenharmony_ci	test_bpftool.sh \
728c2ecf20Sopenharmony_ci	test_bpftool_metadata.sh \
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciTEST_PROGS_EXTENDED := with_addr.sh \
758c2ecf20Sopenharmony_ci	with_tunnels.sh \
768c2ecf20Sopenharmony_ci	tcp_client.py \
778c2ecf20Sopenharmony_ci	tcp_server.py \
788c2ecf20Sopenharmony_ci	test_xdp_vlan.sh
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci# Compile but not part of 'make run_tests'
818c2ecf20Sopenharmony_ciTEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
828c2ecf20Sopenharmony_ci	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
838c2ecf20Sopenharmony_ci	test_lirc_mode2_user xdping test_cpp runqslower bench
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciTEST_CUSTOM_PROGS = urandom_read
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci# Emit succinct information message describing current building step
888c2ecf20Sopenharmony_ci# $1 - generic step name (e.g., CC, LINK, etc);
898c2ecf20Sopenharmony_ci# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
908c2ecf20Sopenharmony_ci# $3 - target (assumed to be file); only file name will be emitted;
918c2ecf20Sopenharmony_ci# $4 - optional extra arg, emitted as-is, if provided.
928c2ecf20Sopenharmony_ciifeq ($(V),1)
938c2ecf20Sopenharmony_ciQ =
948c2ecf20Sopenharmony_cimsg =
958c2ecf20Sopenharmony_cielse
968c2ecf20Sopenharmony_ciQ = @
978c2ecf20Sopenharmony_cimsg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
988c2ecf20Sopenharmony_ciMAKEFLAGS += --no-print-directory
998c2ecf20Sopenharmony_cisubmake_extras := feature_display=0
1008c2ecf20Sopenharmony_ciendif
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci# override lib.mk's default rules
1038c2ecf20Sopenharmony_ciOVERRIDE_TARGETS := 1
1048c2ecf20Sopenharmony_cioverride define CLEAN
1058c2ecf20Sopenharmony_ci	$(call msg,CLEAN)
1068c2ecf20Sopenharmony_ci	$(Q)$(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
1078c2ecf20Sopenharmony_ciendef
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciinclude ../lib.mk
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciSCRATCH_DIR := $(OUTPUT)/tools
1128c2ecf20Sopenharmony_ciBUILD_DIR := $(SCRATCH_DIR)/build
1138c2ecf20Sopenharmony_ciINCLUDE_DIR := $(SCRATCH_DIR)/include
1148c2ecf20Sopenharmony_ciBPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
1158c2ecf20Sopenharmony_ciRESOLVE_BTFIDS := $(BUILD_DIR)/resolve_btfids/resolve_btfids
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
1188c2ecf20Sopenharmony_ci# to build individual tests.
1198c2ecf20Sopenharmony_ci# NOTE: Semicolon at the end is critical to override lib.mk's default static
1208c2ecf20Sopenharmony_ci# rule for binaries.
1218c2ecf20Sopenharmony_ci$(notdir $(TEST_GEN_PROGS)						\
1228c2ecf20Sopenharmony_ci	 $(TEST_GEN_PROGS_EXTENDED)					\
1238c2ecf20Sopenharmony_ci	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci$(OUTPUT)/%.o: %.c
1268c2ecf20Sopenharmony_ci	$(call msg,CC,,$@)
1278c2ecf20Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci$(OUTPUT)/%:%.c
1308c2ecf20Sopenharmony_ci	$(call msg,BINARY,,$@)
1318c2ecf20Sopenharmony_ci	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci$(OUTPUT)/urandom_read: urandom_read.c
1348c2ecf20Sopenharmony_ci	$(call msg,BINARY,,$@)
1358c2ecf20Sopenharmony_ci	$(Q)$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS) -Wl,--build-id=sha1
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ)
1388c2ecf20Sopenharmony_ci	$(call msg,CC,,$@)
1398c2ecf20Sopenharmony_ci	$(Q)$(CC) -c $(CFLAGS) -o $@ $<
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciVMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
1428c2ecf20Sopenharmony_ci		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
1438c2ecf20Sopenharmony_ci		     ../../../../vmlinux				\
1448c2ecf20Sopenharmony_ci		     /sys/kernel/btf/vmlinux				\
1458c2ecf20Sopenharmony_ci		     /boot/vmlinux-$(shell uname -r)
1468c2ecf20Sopenharmony_ciVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
1478c2ecf20Sopenharmony_ciifeq ($(VMLINUX_BTF),)
1488c2ecf20Sopenharmony_ci$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
1498c2ecf20Sopenharmony_ciendif
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ciDEFAULT_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL)
1548c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	\
1558c2ecf20Sopenharmony_ci		    OUTPUT=$(SCRATCH_DIR)/ VMLINUX_BTF=$(VMLINUX_BTF)   \
1568c2ecf20Sopenharmony_ci		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR) &&	\
1578c2ecf20Sopenharmony_ci		    cp $(SCRATCH_DIR)/runqslower $@
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci$(OUTPUT)/test_dev_cgroup: cgroup_helpers.c
1628c2ecf20Sopenharmony_ci$(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c
1638c2ecf20Sopenharmony_ci$(OUTPUT)/test_sock: cgroup_helpers.c
1648c2ecf20Sopenharmony_ci$(OUTPUT)/test_sock_addr: cgroup_helpers.c
1658c2ecf20Sopenharmony_ci$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
1668c2ecf20Sopenharmony_ci$(OUTPUT)/test_sockmap: cgroup_helpers.c
1678c2ecf20Sopenharmony_ci$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
1688c2ecf20Sopenharmony_ci$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
1698c2ecf20Sopenharmony_ci$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
1708c2ecf20Sopenharmony_ci$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
1718c2ecf20Sopenharmony_ci$(OUTPUT)/test_netcnt: cgroup_helpers.c
1728c2ecf20Sopenharmony_ci$(OUTPUT)/test_sock_fields: cgroup_helpers.c
1738c2ecf20Sopenharmony_ci$(OUTPUT)/test_sysctl: cgroup_helpers.c
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ciBPFTOOL ?= $(DEFAULT_BPFTOOL)
1768c2ecf20Sopenharmony_ci$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
1778c2ecf20Sopenharmony_ci		    $(BPFOBJ) | $(BUILD_DIR)/bpftool
1788c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
1798c2ecf20Sopenharmony_ci		    OUTPUT=$(BUILD_DIR)/bpftool/			       \
1808c2ecf20Sopenharmony_ci		    prefix= DESTDIR=$(SCRATCH_DIR)/ install
1818c2ecf20Sopenharmony_ci	$(Q)mkdir -p $(BUILD_DIR)/bpftool/Documentation
1828c2ecf20Sopenharmony_ci	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	       \
1838c2ecf20Sopenharmony_ci		    -C $(BPFTOOLDIR)/Documentation			       \
1848c2ecf20Sopenharmony_ci		    OUTPUT=$(BUILD_DIR)/bpftool/Documentation/		       \
1858c2ecf20Sopenharmony_ci		    prefix= DESTDIR=$(SCRATCH_DIR)/ install
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
1888c2ecf20Sopenharmony_ci	   ../../../include/uapi/linux/bpf.h                                   \
1898c2ecf20Sopenharmony_ci	   | $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
1908c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
1918c2ecf20Sopenharmony_ci		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(BUILD_DIR)/resolve_btfids $(INCLUDE_DIR):
1948c2ecf20Sopenharmony_ci	$(call msg,MKDIR,,$@)
1958c2ecf20Sopenharmony_ci	$(Q)mkdir -p $@
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
1988c2ecf20Sopenharmony_ciifeq ($(VMLINUX_H),)
1998c2ecf20Sopenharmony_ci	$(call msg,GEN,,$@)
2008c2ecf20Sopenharmony_ci	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
2018c2ecf20Sopenharmony_cielse
2028c2ecf20Sopenharmony_ci	$(call msg,CP,,$@)
2038c2ecf20Sopenharmony_ci	$(Q)cp "$(VMLINUX_H)" $@
2048c2ecf20Sopenharmony_ciendif
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci$(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids	\
2078c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
2088c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/lib/rbtree.c			\
2098c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/lib/zalloc.c			\
2108c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/lib/string.c			\
2118c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/lib/ctype.c			\
2128c2ecf20Sopenharmony_ci		       $(TOOLSDIR)/lib/str_error_r.c
2138c2ecf20Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
2148c2ecf20Sopenharmony_ci		OUTPUT=$(BUILD_DIR)/resolve_btfids/ BPFOBJ=$(BPFOBJ)
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci# Get Clang's default includes on this system, as opposed to those seen by
2178c2ecf20Sopenharmony_ci# '-target bpf'. This fixes "missing" files on some architectures/distros,
2188c2ecf20Sopenharmony_ci# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
2198c2ecf20Sopenharmony_ci#
2208c2ecf20Sopenharmony_ci# Use '-idirafter': Don't interfere with include mechanics except where the
2218c2ecf20Sopenharmony_ci# build would have failed anyways.
2228c2ecf20Sopenharmony_cidefine get_sys_includes
2238c2ecf20Sopenharmony_ci$(shell $(1) -v -E - </dev/null 2>&1 \
2248c2ecf20Sopenharmony_ci	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
2258c2ecf20Sopenharmony_ci$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
2268c2ecf20Sopenharmony_ciendef
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci# Determine target endianness.
2298c2ecf20Sopenharmony_ciIS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
2308c2ecf20Sopenharmony_ci			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
2318c2ecf20Sopenharmony_ciMENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ciCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
2348c2ecf20Sopenharmony_ciBPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) 			\
2358c2ecf20Sopenharmony_ci	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
2368c2ecf20Sopenharmony_ci	     -I$(abspath $(OUTPUT)/../usr/include)
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ciCLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
2398c2ecf20Sopenharmony_ci	       -Wno-compare-distinct-pointer-types
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
2428c2ecf20Sopenharmony_ci$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci# Build BPF object using Clang
2478c2ecf20Sopenharmony_ci# $1 - input .c file
2488c2ecf20Sopenharmony_ci# $2 - output .o file
2498c2ecf20Sopenharmony_ci# $3 - CFLAGS
2508c2ecf20Sopenharmony_ci# $4 - LDFLAGS
2518c2ecf20Sopenharmony_cidefine CLANG_BPF_BUILD_RULE
2528c2ecf20Sopenharmony_ci	$(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
2538c2ecf20Sopenharmony_ci	$(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm			\
2548c2ecf20Sopenharmony_ci		-c $1 -o - || echo "BPF obj compilation failed") | 	\
2558c2ecf20Sopenharmony_ci	$(LLC) -mattr=dwarfris -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
2568c2ecf20Sopenharmony_ciendef
2578c2ecf20Sopenharmony_ci# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
2588c2ecf20Sopenharmony_cidefine CLANG_NOALU32_BPF_BUILD_RULE
2598c2ecf20Sopenharmony_ci	$(call msg,CLNG-LLC,$(TRUNNER_BINARY),$2)
2608c2ecf20Sopenharmony_ci	$(Q)($(CLANG) $3 -O2 -target bpf -emit-llvm			\
2618c2ecf20Sopenharmony_ci		-c $1 -o - || echo "BPF obj compilation failed") | 	\
2628c2ecf20Sopenharmony_ci	$(LLC) -march=bpf -mcpu=v2 $4 -filetype=obj -o $2
2638c2ecf20Sopenharmony_ciendef
2648c2ecf20Sopenharmony_ci# Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC
2658c2ecf20Sopenharmony_cidefine CLANG_NATIVE_BPF_BUILD_RULE
2668c2ecf20Sopenharmony_ci	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
2678c2ecf20Sopenharmony_ci	$(Q)($(CLANG) $3 -O2 -emit-llvm					\
2688c2ecf20Sopenharmony_ci		-c $1 -o - || echo "BPF obj compilation failed") | 	\
2698c2ecf20Sopenharmony_ci	$(LLC) -march=bpf -mcpu=v3 $4 -filetype=obj -o $2
2708c2ecf20Sopenharmony_ciendef
2718c2ecf20Sopenharmony_ci# Build BPF object using GCC
2728c2ecf20Sopenharmony_cidefine GCC_BPF_BUILD_RULE
2738c2ecf20Sopenharmony_ci	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
2748c2ecf20Sopenharmony_ci	$(Q)$(BPF_GCC) $3 $4 -O2 -c $1 -o $2
2758c2ecf20Sopenharmony_ciendef
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciSKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
2808c2ecf20Sopenharmony_ci# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
2818c2ecf20Sopenharmony_ci# Parameters:
2828c2ecf20Sopenharmony_ci# $1 - test runner base binary name (e.g., test_progs)
2838c2ecf20Sopenharmony_ci# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
2848c2ecf20Sopenharmony_cidefine DEFINE_TEST_RUNNER
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ciTRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
2878c2ecf20Sopenharmony_ciTRUNNER_BINARY := $1$(if $2,-)$2
2888c2ecf20Sopenharmony_ciTRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
2898c2ecf20Sopenharmony_ci				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
2908c2ecf20Sopenharmony_ciTRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
2918c2ecf20Sopenharmony_ci				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
2928c2ecf20Sopenharmony_ciTRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
2938c2ecf20Sopenharmony_ciTRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
2948c2ecf20Sopenharmony_ciTRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
2958c2ecf20Sopenharmony_ciTRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, $$(TRUNNER_BPF_SRCS))
2968c2ecf20Sopenharmony_ciTRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
2978c2ecf20Sopenharmony_ci				 $$(filter-out $(SKEL_BLACKLIST),	\
2988c2ecf20Sopenharmony_ci					       $$(TRUNNER_BPF_SRCS)))
2998c2ecf20Sopenharmony_ciTEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci# Evaluate rules now with extra TRUNNER_XXX variables above already defined
3028c2ecf20Sopenharmony_ci$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciendef
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
3078c2ecf20Sopenharmony_ci# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
3088c2ecf20Sopenharmony_ci# $1 - test runner base binary name (e.g., test_progs)
3098c2ecf20Sopenharmony_ci# $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc)
3108c2ecf20Sopenharmony_cidefine DEFINE_TEST_RUNNER_RULES
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ciifeq ($($(TRUNNER_OUTPUT)-dir),)
3138c2ecf20Sopenharmony_ci$(TRUNNER_OUTPUT)-dir := y
3148c2ecf20Sopenharmony_ci$(TRUNNER_OUTPUT):
3158c2ecf20Sopenharmony_ci	$$(call msg,MKDIR,,$$@)
3168c2ecf20Sopenharmony_ci	$(Q)mkdir -p $$@
3178c2ecf20Sopenharmony_ciendif
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci# ensure we set up BPF objects generation rule just once for a given
3208c2ecf20Sopenharmony_ci# input/output directory combination
3218c2ecf20Sopenharmony_ciifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
3228c2ecf20Sopenharmony_ci$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
3238c2ecf20Sopenharmony_ci$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
3248c2ecf20Sopenharmony_ci		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
3258c2ecf20Sopenharmony_ci		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
3268c2ecf20Sopenharmony_ci		     $$(INCLUDE_DIR)/vmlinux.h				\
3278c2ecf20Sopenharmony_ci		     $(wildcard $(BPFDIR)/bpf_*.h)			\
3288c2ecf20Sopenharmony_ci		     | $(TRUNNER_OUTPUT) $$(BPFOBJ)
3298c2ecf20Sopenharmony_ci	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
3308c2ecf20Sopenharmony_ci					  $(TRUNNER_BPF_CFLAGS),	\
3318c2ecf20Sopenharmony_ci					  $(TRUNNER_BPF_LDFLAGS))
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci$(TRUNNER_BPF_SKELS): $(TRUNNER_OUTPUT)/%.skel.h:			\
3348c2ecf20Sopenharmony_ci		      $(TRUNNER_OUTPUT)/%.o				\
3358c2ecf20Sopenharmony_ci		      $(BPFTOOL)					\
3368c2ecf20Sopenharmony_ci		      | $(TRUNNER_OUTPUT)
3378c2ecf20Sopenharmony_ci	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
3388c2ecf20Sopenharmony_ci	$(Q)$$(BPFTOOL) gen skeleton $$< > $$@
3398c2ecf20Sopenharmony_ciendif
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci# ensure we set up tests.h header generation rule just once
3428c2ecf20Sopenharmony_ciifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
3438c2ecf20Sopenharmony_ci$(TRUNNER_TESTS_DIR)-tests-hdr := y
3448c2ecf20Sopenharmony_ci$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
3458c2ecf20Sopenharmony_ci	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
3468c2ecf20Sopenharmony_ci	$$(shell ( cd $(TRUNNER_TESTS_DIR);				\
3478c2ecf20Sopenharmony_ci		  echo '/* Generated header, do not edit */';		\
3488c2ecf20Sopenharmony_ci		  ls *.c 2> /dev/null |					\
3498c2ecf20Sopenharmony_ci			sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@';	\
3508c2ecf20Sopenharmony_ci		 ) > $$@)
3518c2ecf20Sopenharmony_ciendif
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci# compile individual test files
3548c2ecf20Sopenharmony_ci# Note: we cd into output directory to ensure embedded BPF object is found
3558c2ecf20Sopenharmony_ci$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
3568c2ecf20Sopenharmony_ci		      $(TRUNNER_TESTS_DIR)/%.c				\
3578c2ecf20Sopenharmony_ci		      $(TRUNNER_EXTRA_HDRS)				\
3588c2ecf20Sopenharmony_ci		      $(TRUNNER_BPF_OBJS)				\
3598c2ecf20Sopenharmony_ci		      $(TRUNNER_BPF_SKELS)				\
3608c2ecf20Sopenharmony_ci		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
3618c2ecf20Sopenharmony_ci	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
3628c2ecf20Sopenharmony_ci	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
3658c2ecf20Sopenharmony_ci		       %.c						\
3668c2ecf20Sopenharmony_ci		       $(TRUNNER_EXTRA_HDRS)				\
3678c2ecf20Sopenharmony_ci		       $(TRUNNER_TESTS_HDR)				\
3688c2ecf20Sopenharmony_ci		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
3698c2ecf20Sopenharmony_ci	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
3708c2ecf20Sopenharmony_ci	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci# only copy extra resources if in flavored build
3738c2ecf20Sopenharmony_ci$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
3748c2ecf20Sopenharmony_ciifneq ($2,)
3758c2ecf20Sopenharmony_ci	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
3768c2ecf20Sopenharmony_ci	$(Q)cp -a $$^ $(TRUNNER_OUTPUT)/
3778c2ecf20Sopenharmony_ciendif
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
3808c2ecf20Sopenharmony_ci			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
3818c2ecf20Sopenharmony_ci			     $(RESOLVE_BTFIDS)				\
3828c2ecf20Sopenharmony_ci			     | $(TRUNNER_BINARY)-extras
3838c2ecf20Sopenharmony_ci	$$(call msg,BINARY,,$$@)
3848c2ecf20Sopenharmony_ci	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
3858c2ecf20Sopenharmony_ci	$(Q)$(RESOLVE_BTFIDS) --no-fail --btf btf_data.o $$@
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ciendef
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci# Define test_progs test runner.
3908c2ecf20Sopenharmony_ciTRUNNER_TESTS_DIR := prog_tests
3918c2ecf20Sopenharmony_ciTRUNNER_BPF_PROGS_DIR := progs
3928c2ecf20Sopenharmony_ciTRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
3938c2ecf20Sopenharmony_ci			 network_helpers.c testing_helpers.c		\
3948c2ecf20Sopenharmony_ci			 flow_dissector_load.h
3958c2ecf20Sopenharmony_ciTRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read				\
3968c2ecf20Sopenharmony_ci		       $(wildcard progs/btf_dump_test_case_*.c)
3978c2ecf20Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
3988c2ecf20Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
3998c2ecf20Sopenharmony_ciTRUNNER_BPF_LDFLAGS := -mattr=+alu32
4008c2ecf20Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs))
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci# Define test_progs-no_alu32 test runner.
4038c2ecf20Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
4048c2ecf20Sopenharmony_ciTRUNNER_BPF_LDFLAGS :=
4058c2ecf20Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci# Define test_progs BPF-GCC-flavored test runner.
4088c2ecf20Sopenharmony_ciifneq ($(BPF_GCC),)
4098c2ecf20Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
4108c2ecf20Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc)
4118c2ecf20Sopenharmony_ciTRUNNER_BPF_LDFLAGS :=
4128c2ecf20Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
4138c2ecf20Sopenharmony_ciendif
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci# Define test_maps test runner.
4168c2ecf20Sopenharmony_ciTRUNNER_TESTS_DIR := map_tests
4178c2ecf20Sopenharmony_ciTRUNNER_BPF_PROGS_DIR := progs
4188c2ecf20Sopenharmony_ciTRUNNER_EXTRA_SOURCES := test_maps.c
4198c2ecf20Sopenharmony_ciTRUNNER_EXTRA_FILES :=
4208c2ecf20Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
4218c2ecf20Sopenharmony_ciTRUNNER_BPF_CFLAGS :=
4228c2ecf20Sopenharmony_ciTRUNNER_BPF_LDFLAGS :=
4238c2ecf20Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_maps))
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci# Define test_verifier test runner.
4268c2ecf20Sopenharmony_ci# It is much simpler than test_maps/test_progs and sufficiently different from
4278c2ecf20Sopenharmony_ci# them (e.g., test.h is using completely pattern), that it's worth just
4288c2ecf20Sopenharmony_ci# explicitly defining all the rules explicitly.
4298c2ecf20Sopenharmony_civerifier/tests.h: verifier/*.c
4308c2ecf20Sopenharmony_ci	$(shell ( cd verifier/; \
4318c2ecf20Sopenharmony_ci		  echo '/* Generated header, do not edit */'; \
4328c2ecf20Sopenharmony_ci		  echo '#ifdef FILL_ARRAY'; \
4338c2ecf20Sopenharmony_ci		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
4348c2ecf20Sopenharmony_ci		  echo '#endif' \
4358c2ecf20Sopenharmony_ci		) > verifier/tests.h)
4368c2ecf20Sopenharmony_ci$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
4378c2ecf20Sopenharmony_ci	$(call msg,BINARY,,$@)
4388c2ecf20Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci# Make sure we are able to include and link libbpf against c++.
4418c2ecf20Sopenharmony_ci$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
4428c2ecf20Sopenharmony_ci	$(call msg,CXX,,$@)
4438c2ecf20Sopenharmony_ci	$(Q)$(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci# Benchmark runner
4468c2ecf20Sopenharmony_ci$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h
4478c2ecf20Sopenharmony_ci	$(call msg,CC,,$@)
4488c2ecf20Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
4498c2ecf20Sopenharmony_ci$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
4508c2ecf20Sopenharmony_ci$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
4518c2ecf20Sopenharmony_ci$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
4528c2ecf20Sopenharmony_ci			    $(OUTPUT)/perfbuf_bench.skel.h
4538c2ecf20Sopenharmony_ci$(OUTPUT)/bench.o: bench.h testing_helpers.h
4548c2ecf20Sopenharmony_ci$(OUTPUT)/bench: LDLIBS += -lm
4558c2ecf20Sopenharmony_ci$(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \
4568c2ecf20Sopenharmony_ci		 $(OUTPUT)/bench_count.o \
4578c2ecf20Sopenharmony_ci		 $(OUTPUT)/bench_rename.o \
4588c2ecf20Sopenharmony_ci		 $(OUTPUT)/bench_trigger.o \
4598c2ecf20Sopenharmony_ci		 $(OUTPUT)/bench_ringbufs.o
4608c2ecf20Sopenharmony_ci	$(call msg,BINARY,,$@)
4618c2ecf20Sopenharmony_ci	$(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ciEXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR)			\
4648c2ecf20Sopenharmony_ci	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
4658c2ecf20Sopenharmony_ci	feature								\
4668c2ecf20Sopenharmony_ci	$(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)
467