162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ciinclude ../../../build/Build.include
362306a36Sopenharmony_ciinclude ../../../scripts/Makefile.arch
462306a36Sopenharmony_ciinclude ../../../scripts/Makefile.include
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciCXX ?= $(CROSS_COMPILE)g++
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciCURDIR := $(abspath .)
962306a36Sopenharmony_ciTOOLSDIR := $(abspath ../../..)
1062306a36Sopenharmony_ciLIBDIR := $(TOOLSDIR)/lib
1162306a36Sopenharmony_ciBPFDIR := $(LIBDIR)/bpf
1262306a36Sopenharmony_ciTOOLSINCDIR := $(TOOLSDIR)/include
1362306a36Sopenharmony_ciBPFTOOLDIR := $(TOOLSDIR)/bpf/bpftool
1462306a36Sopenharmony_ciAPIDIR := $(TOOLSINCDIR)/uapi
1562306a36Sopenharmony_ciifneq ($(O),)
1662306a36Sopenharmony_ciGENDIR := $(O)/include/generated
1762306a36Sopenharmony_cielse
1862306a36Sopenharmony_ciGENDIR := $(abspath ../../../../include/generated)
1962306a36Sopenharmony_ciendif
2062306a36Sopenharmony_ciGENHDR := $(GENDIR)/autoconf.h
2162306a36Sopenharmony_ciHOSTPKG_CONFIG := pkg-config
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciifneq ($(wildcard $(GENHDR)),)
2462306a36Sopenharmony_ci  GENFLAGS := -DHAVE_GENHDR
2562306a36Sopenharmony_ciendif
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciBPF_GCC		?= $(shell command -v bpf-gcc;)
2862306a36Sopenharmony_ciSAN_CFLAGS	?=
2962306a36Sopenharmony_ciSAN_LDFLAGS	?= $(SAN_CFLAGS)
3062306a36Sopenharmony_ciCFLAGS += -g -O0 -rdynamic -Wall -Werror $(GENFLAGS) $(SAN_CFLAGS)	\
3162306a36Sopenharmony_ci	  -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR)		\
3262306a36Sopenharmony_ci	  -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
3362306a36Sopenharmony_ciLDFLAGS += $(SAN_LDFLAGS)
3462306a36Sopenharmony_ciLDLIBS += -lelf -lz -lrt -lpthread
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciifneq ($(LLVM),)
3762306a36Sopenharmony_ci# Silence some warnings when compiled with clang
3862306a36Sopenharmony_ciCFLAGS += -Wno-unused-command-line-argument
3962306a36Sopenharmony_ciendif
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci# Check whether bpf cpu=v4 is supported or not by clang
4262306a36Sopenharmony_ciifneq ($(shell $(CLANG) --target=bpf -mcpu=help 2>&1 | grep 'v4'),)
4362306a36Sopenharmony_ciCLANG_CPUV4 := 1
4462306a36Sopenharmony_ciendif
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci# Order correspond to 'make run_tests' order
4762306a36Sopenharmony_ciTEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
4862306a36Sopenharmony_ci	test_dev_cgroup \
4962306a36Sopenharmony_ci	test_sock test_sockmap get_cgroup_id_user \
5062306a36Sopenharmony_ci	test_cgroup_storage \
5162306a36Sopenharmony_ci	test_tcpnotify_user test_sysctl \
5262306a36Sopenharmony_ci	test_progs-no_alu32
5362306a36Sopenharmony_ciTEST_INST_SUBDIRS := no_alu32
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci# Also test bpf-gcc, if present
5662306a36Sopenharmony_ciifneq ($(BPF_GCC),)
5762306a36Sopenharmony_ciTEST_GEN_PROGS += test_progs-bpf_gcc
5862306a36Sopenharmony_ciTEST_INST_SUBDIRS += bpf_gcc
5962306a36Sopenharmony_ciendif
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciifneq ($(CLANG_CPUV4),)
6262306a36Sopenharmony_ciTEST_GEN_PROGS += test_progs-cpuv4
6362306a36Sopenharmony_ciTEST_INST_SUBDIRS += cpuv4
6462306a36Sopenharmony_ciendif
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciTEST_GEN_FILES = test_lwt_ip_encap.bpf.o test_tc_edt.bpf.o
6762306a36Sopenharmony_ciTEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci# Order correspond to 'make run_tests' order
7062306a36Sopenharmony_ciTEST_PROGS := test_kmod.sh \
7162306a36Sopenharmony_ci	test_xdp_redirect.sh \
7262306a36Sopenharmony_ci	test_xdp_redirect_multi.sh \
7362306a36Sopenharmony_ci	test_xdp_meta.sh \
7462306a36Sopenharmony_ci	test_xdp_veth.sh \
7562306a36Sopenharmony_ci	test_offload.py \
7662306a36Sopenharmony_ci	test_sock_addr.sh \
7762306a36Sopenharmony_ci	test_tunnel.sh \
7862306a36Sopenharmony_ci	test_lwt_seg6local.sh \
7962306a36Sopenharmony_ci	test_lirc_mode2.sh \
8062306a36Sopenharmony_ci	test_skb_cgroup_id.sh \
8162306a36Sopenharmony_ci	test_flow_dissector.sh \
8262306a36Sopenharmony_ci	test_xdp_vlan_mode_generic.sh \
8362306a36Sopenharmony_ci	test_xdp_vlan_mode_native.sh \
8462306a36Sopenharmony_ci	test_lwt_ip_encap.sh \
8562306a36Sopenharmony_ci	test_tcp_check_syncookie.sh \
8662306a36Sopenharmony_ci	test_tc_tunnel.sh \
8762306a36Sopenharmony_ci	test_tc_edt.sh \
8862306a36Sopenharmony_ci	test_xdping.sh \
8962306a36Sopenharmony_ci	test_bpftool_build.sh \
9062306a36Sopenharmony_ci	test_bpftool.sh \
9162306a36Sopenharmony_ci	test_bpftool_metadata.sh \
9262306a36Sopenharmony_ci	test_doc_build.sh \
9362306a36Sopenharmony_ci	test_xsk.sh \
9462306a36Sopenharmony_ci	test_xdp_features.sh
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciTEST_PROGS_EXTENDED := with_addr.sh \
9762306a36Sopenharmony_ci	with_tunnels.sh ima_setup.sh verify_sig_setup.sh \
9862306a36Sopenharmony_ci	test_xdp_vlan.sh test_bpftool.py
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci# Compile but not part of 'make run_tests'
10162306a36Sopenharmony_ciTEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
10262306a36Sopenharmony_ci	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
10362306a36Sopenharmony_ci	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
10462306a36Sopenharmony_ci	xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
10562306a36Sopenharmony_ci	xdp_features
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciTEST_GEN_FILES += liburandom_read.so urandom_read sign-file
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci# Emit succinct information message describing current building step
11062306a36Sopenharmony_ci# $1 - generic step name (e.g., CC, LINK, etc);
11162306a36Sopenharmony_ci# $2 - optional "flavor" specifier; if provided, will be emitted as [flavor];
11262306a36Sopenharmony_ci# $3 - target (assumed to be file); only file name will be emitted;
11362306a36Sopenharmony_ci# $4 - optional extra arg, emitted as-is, if provided.
11462306a36Sopenharmony_ciifeq ($(V),1)
11562306a36Sopenharmony_ciQ =
11662306a36Sopenharmony_cimsg =
11762306a36Sopenharmony_cielse
11862306a36Sopenharmony_ciQ = @
11962306a36Sopenharmony_cimsg = @printf '  %-8s%s %s%s\n' "$(1)" "$(if $(2), [$(2)])" "$(notdir $(3))" "$(if $(4), $(4))";
12062306a36Sopenharmony_ciMAKEFLAGS += --no-print-directory
12162306a36Sopenharmony_cisubmake_extras := feature_display=0
12262306a36Sopenharmony_ciendif
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci# override lib.mk's default rules
12562306a36Sopenharmony_ciOVERRIDE_TARGETS := 1
12662306a36Sopenharmony_cioverride define CLEAN
12762306a36Sopenharmony_ci	$(call msg,CLEAN)
12862306a36Sopenharmony_ci	$(Q)$(RM) -r $(TEST_GEN_PROGS)
12962306a36Sopenharmony_ci	$(Q)$(RM) -r $(TEST_GEN_PROGS_EXTENDED)
13062306a36Sopenharmony_ci	$(Q)$(RM) -r $(TEST_GEN_FILES)
13162306a36Sopenharmony_ci	$(Q)$(RM) -r $(EXTRA_CLEAN)
13262306a36Sopenharmony_ci	$(Q)$(MAKE) -C bpf_testmod clean
13362306a36Sopenharmony_ci	$(Q)$(MAKE) docs-clean
13462306a36Sopenharmony_ciendef
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciinclude ../lib.mk
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciSCRATCH_DIR := $(OUTPUT)/tools
13962306a36Sopenharmony_ciBUILD_DIR := $(SCRATCH_DIR)/build
14062306a36Sopenharmony_ciINCLUDE_DIR := $(SCRATCH_DIR)/include
14162306a36Sopenharmony_ciBPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
14262306a36Sopenharmony_ciifneq ($(CROSS_COMPILE),)
14362306a36Sopenharmony_ciHOST_BUILD_DIR		:= $(BUILD_DIR)/host
14462306a36Sopenharmony_ciHOST_SCRATCH_DIR	:= $(OUTPUT)/host-tools
14562306a36Sopenharmony_ciHOST_INCLUDE_DIR	:= $(HOST_SCRATCH_DIR)/include
14662306a36Sopenharmony_cielse
14762306a36Sopenharmony_ciHOST_BUILD_DIR		:= $(BUILD_DIR)
14862306a36Sopenharmony_ciHOST_SCRATCH_DIR	:= $(SCRATCH_DIR)
14962306a36Sopenharmony_ciHOST_INCLUDE_DIR	:= $(INCLUDE_DIR)
15062306a36Sopenharmony_ciendif
15162306a36Sopenharmony_ciHOST_BPFOBJ := $(HOST_BUILD_DIR)/libbpf/libbpf.a
15262306a36Sopenharmony_ciRESOLVE_BTFIDS := $(HOST_BUILD_DIR)/resolve_btfids/resolve_btfids
15362306a36Sopenharmony_ciRUNQSLOWER_OUTPUT := $(BUILD_DIR)/runqslower/
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciVMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
15662306a36Sopenharmony_ci		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
15762306a36Sopenharmony_ci		     ../../../../vmlinux				\
15862306a36Sopenharmony_ci		     /sys/kernel/btf/vmlinux				\
15962306a36Sopenharmony_ci		     /boot/vmlinux-$(shell uname -r)
16062306a36Sopenharmony_ciVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
16162306a36Sopenharmony_ciifeq ($(VMLINUX_BTF),)
16262306a36Sopenharmony_ci$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)")
16362306a36Sopenharmony_ciendif
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci# Define simple and short `make test_progs`, `make test_sysctl`, etc targets
16662306a36Sopenharmony_ci# to build individual tests.
16762306a36Sopenharmony_ci# NOTE: Semicolon at the end is critical to override lib.mk's default static
16862306a36Sopenharmony_ci# rule for binaries.
16962306a36Sopenharmony_ci$(notdir $(TEST_GEN_PROGS)						\
17062306a36Sopenharmony_ci	 $(TEST_GEN_PROGS_EXTENDED)					\
17162306a36Sopenharmony_ci	 $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci# sort removes libbpf duplicates when not cross-building
17462306a36Sopenharmony_ciMAKE_DIRS := $(sort $(BUILD_DIR)/libbpf $(HOST_BUILD_DIR)/libbpf	\
17562306a36Sopenharmony_ci	       $(BUILD_DIR)/bpftool $(HOST_BUILD_DIR)/bpftool		\
17662306a36Sopenharmony_ci	       $(HOST_BUILD_DIR)/resolve_btfids				\
17762306a36Sopenharmony_ci	       $(RUNQSLOWER_OUTPUT) $(INCLUDE_DIR))
17862306a36Sopenharmony_ci$(MAKE_DIRS):
17962306a36Sopenharmony_ci	$(call msg,MKDIR,,$@)
18062306a36Sopenharmony_ci	$(Q)mkdir -p $@
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci$(OUTPUT)/%.o: %.c
18362306a36Sopenharmony_ci	$(call msg,CC,,$@)
18462306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) -c $(filter %.c,$^) $(LDLIBS) -o $@
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci$(OUTPUT)/%:%.c
18762306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
18862306a36Sopenharmony_ci	$(Q)$(LINK.c) $^ $(LDLIBS) -o $@
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci# LLVM's ld.lld doesn't support all the architectures, so use it only on x86
19162306a36Sopenharmony_ciifeq ($(SRCARCH),x86)
19262306a36Sopenharmony_ciLLD := lld
19362306a36Sopenharmony_cielse
19462306a36Sopenharmony_ciLLD := ld
19562306a36Sopenharmony_ciendif
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci# Filter out -static for liburandom_read.so and its dependent targets so that static builds
19862306a36Sopenharmony_ci# do not fail. Static builds leave urandom_read relying on system-wide shared libraries.
19962306a36Sopenharmony_ci$(OUTPUT)/liburandom_read.so: urandom_read_lib1.c urandom_read_lib2.c
20062306a36Sopenharmony_ci	$(call msg,LIB,,$@)
20162306a36Sopenharmony_ci	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS))   \
20262306a36Sopenharmony_ci		     $^ $(filter-out -static,$(LDLIBS))	     \
20362306a36Sopenharmony_ci		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
20462306a36Sopenharmony_ci		     -fPIC -shared -o $@
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci$(OUTPUT)/urandom_read: urandom_read.c urandom_read_aux.c $(OUTPUT)/liburandom_read.so
20762306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
20862306a36Sopenharmony_ci	$(Q)$(CLANG) $(filter-out -static,$(CFLAGS) $(LDFLAGS)) $(filter %.c,$^) \
20962306a36Sopenharmony_ci		     -lurandom_read $(filter-out -static,$(LDLIBS)) -L$(OUTPUT)  \
21062306a36Sopenharmony_ci		     -fuse-ld=$(LLD) -Wl,-znoseparate-code -Wl,--build-id=sha1 \
21162306a36Sopenharmony_ci		     -Wl,-rpath=. -o $@
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci$(OUTPUT)/sign-file: ../../../../scripts/sign-file.c
21462306a36Sopenharmony_ci	$(call msg,SIGN-FILE,,$@)
21562306a36Sopenharmony_ci	$(Q)$(CC) $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) \
21662306a36Sopenharmony_ci		  $< -o $@ \
21762306a36Sopenharmony_ci		  $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci$(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
22062306a36Sopenharmony_ci	$(call msg,MOD,,$@)
22162306a36Sopenharmony_ci	$(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation
22262306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod
22362306a36Sopenharmony_ci	$(Q)cp bpf_testmod/bpf_testmod.ko $@
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ciDEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
22662306a36Sopenharmony_ciifneq ($(CROSS_COMPILE),)
22762306a36Sopenharmony_ciCROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
22862306a36Sopenharmony_ciTRUNNER_BPFTOOL := $(CROSS_BPFTOOL)
22962306a36Sopenharmony_ciUSE_BOOTSTRAP := ""
23062306a36Sopenharmony_cielse
23162306a36Sopenharmony_ciTRUNNER_BPFTOOL := $(DEFAULT_BPFTOOL)
23262306a36Sopenharmony_ciUSE_BOOTSTRAP := "bootstrap/"
23362306a36Sopenharmony_ciendif
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci$(OUTPUT)/runqslower: $(BPFOBJ) | $(DEFAULT_BPFTOOL) $(RUNQSLOWER_OUTPUT)
23662306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower	       \
23762306a36Sopenharmony_ci		    OUTPUT=$(RUNQSLOWER_OUTPUT) VMLINUX_BTF=$(VMLINUX_BTF)     \
23862306a36Sopenharmony_ci		    BPFTOOL_OUTPUT=$(HOST_BUILD_DIR)/bpftool/		       \
23962306a36Sopenharmony_ci		    BPFOBJ_OUTPUT=$(BUILD_DIR)/libbpf			       \
24062306a36Sopenharmony_ci		    BPFOBJ=$(BPFOBJ) BPF_INCLUDE=$(INCLUDE_DIR)		       \
24162306a36Sopenharmony_ci		    EXTRA_CFLAGS='-g -O0 $(SAN_CFLAGS)'			       \
24262306a36Sopenharmony_ci		    EXTRA_LDFLAGS='$(SAN_LDFLAGS)' &&			       \
24362306a36Sopenharmony_ci		    cp $(RUNQSLOWER_OUTPUT)runqslower $@
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ciTEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL)
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ)
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ciTESTING_HELPERS	:= $(OUTPUT)/testing_helpers.o
25062306a36Sopenharmony_ciCGROUP_HELPERS	:= $(OUTPUT)/cgroup_helpers.o
25162306a36Sopenharmony_ciUNPRIV_HELPERS  := $(OUTPUT)/unpriv_helpers.o
25262306a36Sopenharmony_ciTRACE_HELPERS	:= $(OUTPUT)/trace_helpers.o
25362306a36Sopenharmony_ciJSON_WRITER		:= $(OUTPUT)/json_writer.o
25462306a36Sopenharmony_ciCAP_HELPERS	:= $(OUTPUT)/cap_helpers.o
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci$(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS)
25762306a36Sopenharmony_ci$(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
25862306a36Sopenharmony_ci$(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS)
25962306a36Sopenharmony_ci$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26062306a36Sopenharmony_ci$(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26162306a36Sopenharmony_ci$(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS)
26262306a36Sopenharmony_ci$(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26362306a36Sopenharmony_ci$(OUTPUT)/test_cgroup_storage: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26462306a36Sopenharmony_ci$(OUTPUT)/test_sock_fields: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26562306a36Sopenharmony_ci$(OUTPUT)/test_sysctl: $(CGROUP_HELPERS) $(TESTING_HELPERS)
26662306a36Sopenharmony_ci$(OUTPUT)/test_tag: $(TESTING_HELPERS)
26762306a36Sopenharmony_ci$(OUTPUT)/test_lirc_mode2_user: $(TESTING_HELPERS)
26862306a36Sopenharmony_ci$(OUTPUT)/xdping: $(TESTING_HELPERS)
26962306a36Sopenharmony_ci$(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
27062306a36Sopenharmony_ci$(OUTPUT)/test_maps: $(TESTING_HELPERS)
27162306a36Sopenharmony_ci$(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
27262306a36Sopenharmony_ci$(OUTPUT)/xsk.o: $(BPFOBJ)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ciBPFTOOL ?= $(DEFAULT_BPFTOOL)
27562306a36Sopenharmony_ci$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
27662306a36Sopenharmony_ci		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
27762306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
27862306a36Sopenharmony_ci		    ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" 	       \
27962306a36Sopenharmony_ci		    EXTRA_CFLAGS='-g -O0'				       \
28062306a36Sopenharmony_ci		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
28162306a36Sopenharmony_ci		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
28262306a36Sopenharmony_ci		    LIBBPF_DESTDIR=$(HOST_SCRATCH_DIR)/			       \
28362306a36Sopenharmony_ci		    prefix= DESTDIR=$(HOST_SCRATCH_DIR)/ install-bin
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciifneq ($(CROSS_COMPILE),)
28662306a36Sopenharmony_ci$(CROSS_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)	\
28762306a36Sopenharmony_ci		    $(BPFOBJ) | $(BUILD_DIR)/bpftool
28862306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)				\
28962306a36Sopenharmony_ci		    ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE)			\
29062306a36Sopenharmony_ci		    EXTRA_CFLAGS='-g -O0'					\
29162306a36Sopenharmony_ci		    OUTPUT=$(BUILD_DIR)/bpftool/				\
29262306a36Sopenharmony_ci		    LIBBPF_OUTPUT=$(BUILD_DIR)/libbpf/				\
29362306a36Sopenharmony_ci		    LIBBPF_DESTDIR=$(SCRATCH_DIR)/				\
29462306a36Sopenharmony_ci		    prefix= DESTDIR=$(SCRATCH_DIR)/ install-bin
29562306a36Sopenharmony_ciendif
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ciall: docs
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_cidocs:
30062306a36Sopenharmony_ci	$(Q)RST2MAN_OPTS="--exit-status=1" $(MAKE) $(submake_extras)	\
30162306a36Sopenharmony_ci	            -f Makefile.docs					\
30262306a36Sopenharmony_ci	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_cidocs-clean:
30562306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras)					\
30662306a36Sopenharmony_ci	            -f Makefile.docs					\
30762306a36Sopenharmony_ci	            prefix= OUTPUT=$(OUTPUT)/ DESTDIR=$(OUTPUT)/ $@
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
31062306a36Sopenharmony_ci	   $(APIDIR)/linux/bpf.h					       \
31162306a36Sopenharmony_ci	   | $(BUILD_DIR)/libbpf
31262306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
31362306a36Sopenharmony_ci		    EXTRA_CFLAGS='-g -O0 $(SAN_CFLAGS)'			       \
31462306a36Sopenharmony_ci		    EXTRA_LDFLAGS='$(SAN_LDFLAGS)'			       \
31562306a36Sopenharmony_ci		    DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ciifneq ($(BPFOBJ),$(HOST_BPFOBJ))
31862306a36Sopenharmony_ci$(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
31962306a36Sopenharmony_ci		$(APIDIR)/linux/bpf.h					       \
32062306a36Sopenharmony_ci		| $(HOST_BUILD_DIR)/libbpf
32162306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
32262306a36Sopenharmony_ci		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
32362306a36Sopenharmony_ci		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/			       \
32462306a36Sopenharmony_ci		    CC="$(HOSTCC)" LD="$(HOSTLD)"			       \
32562306a36Sopenharmony_ci		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
32662306a36Sopenharmony_ciendif
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci$(INCLUDE_DIR)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL) | $(INCLUDE_DIR)
32962306a36Sopenharmony_ciifeq ($(VMLINUX_H),)
33062306a36Sopenharmony_ci	$(call msg,GEN,,$@)
33162306a36Sopenharmony_ci	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
33262306a36Sopenharmony_cielse
33362306a36Sopenharmony_ci	$(call msg,CP,,$@)
33462306a36Sopenharmony_ci	$(Q)cp "$(VMLINUX_H)" $@
33562306a36Sopenharmony_ciendif
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci$(RESOLVE_BTFIDS): $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/resolve_btfids	\
33862306a36Sopenharmony_ci		       $(TOOLSDIR)/bpf/resolve_btfids/main.c	\
33962306a36Sopenharmony_ci		       $(TOOLSDIR)/lib/rbtree.c			\
34062306a36Sopenharmony_ci		       $(TOOLSDIR)/lib/zalloc.c			\
34162306a36Sopenharmony_ci		       $(TOOLSDIR)/lib/string.c			\
34262306a36Sopenharmony_ci		       $(TOOLSDIR)/lib/ctype.c			\
34362306a36Sopenharmony_ci		       $(TOOLSDIR)/lib/str_error_r.c
34462306a36Sopenharmony_ci	$(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/resolve_btfids	\
34562306a36Sopenharmony_ci		CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" \
34662306a36Sopenharmony_ci		LIBBPF_INCLUDE=$(HOST_INCLUDE_DIR) \
34762306a36Sopenharmony_ci		OUTPUT=$(HOST_BUILD_DIR)/resolve_btfids/ BPFOBJ=$(HOST_BPFOBJ)
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci# Get Clang's default includes on this system, as opposed to those seen by
35062306a36Sopenharmony_ci# '--target=bpf'. This fixes "missing" files on some architectures/distros,
35162306a36Sopenharmony_ci# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
35262306a36Sopenharmony_ci#
35362306a36Sopenharmony_ci# Use '-idirafter': Don't interfere with include mechanics except where the
35462306a36Sopenharmony_ci# build would have failed anyways.
35562306a36Sopenharmony_cidefine get_sys_includes
35662306a36Sopenharmony_ci$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
35762306a36Sopenharmony_ci	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
35862306a36Sopenharmony_ci$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}') \
35962306a36Sopenharmony_ci$(shell $(1) $(2) -dM -E - </dev/null | grep '__loongarch_grlen ' | awk '{printf("-D__BITS_PER_LONG=%d", $$3)}')
36062306a36Sopenharmony_ciendef
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci# Determine target endianness.
36362306a36Sopenharmony_ciIS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \
36462306a36Sopenharmony_ci			grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__')
36562306a36Sopenharmony_ciMENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ciifneq ($(CROSS_COMPILE),)
36862306a36Sopenharmony_ciCLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
36962306a36Sopenharmony_ciendif
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
37262306a36Sopenharmony_ciBPF_CFLAGS = -g -Wall -Werror -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN)	\
37362306a36Sopenharmony_ci	     -I$(INCLUDE_DIR) -I$(CURDIR) -I$(APIDIR)			\
37462306a36Sopenharmony_ci	     -I$(abspath $(OUTPUT)/../usr/include)
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ciCLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
37762306a36Sopenharmony_ci	       -Wno-compare-distinct-pointer-types
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci$(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline
38062306a36Sopenharmony_ci$(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci$(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h
38362306a36Sopenharmony_ci$(OUTPUT)/cgroup_getset_retval_hooks.o: cgroup_getset_retval_hooks.h
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci# Build BPF object using Clang
38662306a36Sopenharmony_ci# $1 - input .c file
38762306a36Sopenharmony_ci# $2 - output .o file
38862306a36Sopenharmony_ci# $3 - CFLAGS
38962306a36Sopenharmony_cidefine CLANG_BPF_BUILD_RULE
39062306a36Sopenharmony_ci	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
39162306a36Sopenharmony_ci	$(Q)$(CLANG) $3 -O2 --target=bpf -c $1 -mcpu=v3 -o $2
39262306a36Sopenharmony_ciendef
39362306a36Sopenharmony_ci# Similar to CLANG_BPF_BUILD_RULE, but with disabled alu32
39462306a36Sopenharmony_cidefine CLANG_NOALU32_BPF_BUILD_RULE
39562306a36Sopenharmony_ci	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
39662306a36Sopenharmony_ci	$(Q)$(CLANG) $3 -O2 --target=bpf -c $1 -mcpu=v2 -o $2
39762306a36Sopenharmony_ciendef
39862306a36Sopenharmony_ci# Similar to CLANG_BPF_BUILD_RULE, but with cpu-v4
39962306a36Sopenharmony_cidefine CLANG_CPUV4_BPF_BUILD_RULE
40062306a36Sopenharmony_ci	$(call msg,CLNG-BPF,$(TRUNNER_BINARY),$2)
40162306a36Sopenharmony_ci	$(Q)$(CLANG) $3 -O2 --target=bpf -c $1 -mcpu=v4 -o $2
40262306a36Sopenharmony_ciendef
40362306a36Sopenharmony_ci# Build BPF object using GCC
40462306a36Sopenharmony_cidefine GCC_BPF_BUILD_RULE
40562306a36Sopenharmony_ci	$(call msg,GCC-BPF,$(TRUNNER_BINARY),$2)
40662306a36Sopenharmony_ci	$(Q)$(BPF_GCC) $3 -O2 -c $1 -o $2
40762306a36Sopenharmony_ciendef
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ciSKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ciLINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
41262306a36Sopenharmony_ci		linked_vars.skel.h linked_maps.skel.h 			\
41362306a36Sopenharmony_ci		test_subskeleton.skel.h test_subskeleton_lib.skel.h	\
41462306a36Sopenharmony_ci		test_usdt.skel.h
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ciLSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c 		\
41762306a36Sopenharmony_ci	trace_printk.c trace_vprintk.c map_ptr_kern.c 			\
41862306a36Sopenharmony_ci	core_kern.c core_kern_overflow.c test_ringbuf.c			\
41962306a36Sopenharmony_ci	test_ringbuf_map_key.c
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci# Generate both light skeleton and libbpf skeleton for these
42262306a36Sopenharmony_ciLSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \
42362306a36Sopenharmony_ci	kfunc_call_test_subprog.c
42462306a36Sopenharmony_ciSKEL_BLACKLIST += $$(LSKELS)
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_citest_static_linked.skel.h-deps := test_static_linked1.bpf.o test_static_linked2.bpf.o
42762306a36Sopenharmony_cilinked_funcs.skel.h-deps := linked_funcs1.bpf.o linked_funcs2.bpf.o
42862306a36Sopenharmony_cilinked_vars.skel.h-deps := linked_vars1.bpf.o linked_vars2.bpf.o
42962306a36Sopenharmony_cilinked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o
43062306a36Sopenharmony_ci# In the subskeleton case, we want the test_subskeleton_lib.subskel.h file
43162306a36Sopenharmony_ci# but that's created as a side-effect of the skel.h generation.
43262306a36Sopenharmony_citest_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o
43362306a36Sopenharmony_citest_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o
43462306a36Sopenharmony_citest_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
43562306a36Sopenharmony_cixsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
43662306a36Sopenharmony_cixdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
43762306a36Sopenharmony_cixdp_features.skel.h-deps := xdp_features.bpf.o
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ciLINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci# Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on
44262306a36Sopenharmony_ci# $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
44362306a36Sopenharmony_ci# Parameters:
44462306a36Sopenharmony_ci# $1 - test runner base binary name (e.g., test_progs)
44562306a36Sopenharmony_ci# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, gcc-bpf, etc)
44662306a36Sopenharmony_cidefine DEFINE_TEST_RUNNER
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ciTRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2
44962306a36Sopenharmony_ciTRUNNER_BINARY := $1$(if $2,-)$2
45062306a36Sopenharmony_ciTRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o,	\
45162306a36Sopenharmony_ci				 $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c)))
45262306a36Sopenharmony_ciTRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o,		\
45362306a36Sopenharmony_ci				 $$(filter %.c,$(TRUNNER_EXTRA_SOURCES)))
45462306a36Sopenharmony_ciTRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES))
45562306a36Sopenharmony_ciTRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h
45662306a36Sopenharmony_ciTRUNNER_BPF_SRCS := $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))
45762306a36Sopenharmony_ciTRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.bpf.o, $$(TRUNNER_BPF_SRCS))
45862306a36Sopenharmony_ciTRUNNER_BPF_SKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.skel.h,	\
45962306a36Sopenharmony_ci				 $$(filter-out $(SKEL_BLACKLIST) $(LINKED_BPF_SRCS),\
46062306a36Sopenharmony_ci					       $$(TRUNNER_BPF_SRCS)))
46162306a36Sopenharmony_ciTRUNNER_BPF_LSKELS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.lskel.h, $$(LSKELS) $$(LSKELS_EXTRA))
46262306a36Sopenharmony_ciTRUNNER_BPF_SKELS_LINKED := $$(addprefix $$(TRUNNER_OUTPUT)/,$(LINKED_SKELS))
46362306a36Sopenharmony_ciTEST_GEN_FILES += $$(TRUNNER_BPF_OBJS)
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci# Evaluate rules now with extra TRUNNER_XXX variables above already defined
46662306a36Sopenharmony_ci$$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2))
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ciendef
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci# Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and
47162306a36Sopenharmony_ci# set up by DEFINE_TEST_RUNNER itself, create test runner build rules with:
47262306a36Sopenharmony_ci# $1 - test runner base binary name (e.g., test_progs)
47362306a36Sopenharmony_ci# $2 - test runner extra "flavor" (e.g., no_alu32, cpuv4, gcc-bpf, etc)
47462306a36Sopenharmony_cidefine DEFINE_TEST_RUNNER_RULES
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ciifeq ($($(TRUNNER_OUTPUT)-dir),)
47762306a36Sopenharmony_ci$(TRUNNER_OUTPUT)-dir := y
47862306a36Sopenharmony_ci$(TRUNNER_OUTPUT):
47962306a36Sopenharmony_ci	$$(call msg,MKDIR,,$$@)
48062306a36Sopenharmony_ci	$(Q)mkdir -p $$@
48162306a36Sopenharmony_ciendif
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci# ensure we set up BPF objects generation rule just once for a given
48462306a36Sopenharmony_ci# input/output directory combination
48562306a36Sopenharmony_ciifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),)
48662306a36Sopenharmony_ci$(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y
48762306a36Sopenharmony_ci$(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.bpf.o:				\
48862306a36Sopenharmony_ci		     $(TRUNNER_BPF_PROGS_DIR)/%.c			\
48962306a36Sopenharmony_ci		     $(TRUNNER_BPF_PROGS_DIR)/*.h			\
49062306a36Sopenharmony_ci		     $$(INCLUDE_DIR)/vmlinux.h				\
49162306a36Sopenharmony_ci		     $(wildcard $(BPFDIR)/bpf_*.h)			\
49262306a36Sopenharmony_ci		     $(wildcard $(BPFDIR)/*.bpf.h)			\
49362306a36Sopenharmony_ci		     | $(TRUNNER_OUTPUT) $$(BPFOBJ)
49462306a36Sopenharmony_ci	$$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@,			\
49562306a36Sopenharmony_ci					  $(TRUNNER_BPF_CFLAGS))
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci$(TRUNNER_BPF_SKELS): %.skel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
49862306a36Sopenharmony_ci	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
49962306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked1.o) $$<
50062306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked2.o) $$(<:.o=.linked1.o)
50162306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.linked3.o) $$(<:.o=.linked2.o)
50262306a36Sopenharmony_ci	$(Q)diff $$(<:.o=.linked2.o) $$(<:.o=.linked3.o)
50362306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen skeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$@
50462306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen subskeleton $$(<:.o=.linked3.o) name $$(notdir $$(<:.bpf.o=)) > $$(@:.skel.h=.subskel.h)
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci$(TRUNNER_BPF_LSKELS): %.lskel.h: %.bpf.o $(BPFTOOL) | $(TRUNNER_OUTPUT)
50762306a36Sopenharmony_ci	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
50862306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked1.o) $$<
50962306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked2.o) $$(<:.o=.llinked1.o)
51062306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(<:.o=.llinked3.o) $$(<:.o=.llinked2.o)
51162306a36Sopenharmony_ci	$(Q)diff $$(<:.o=.llinked2.o) $$(<:.o=.llinked3.o)
51262306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen skeleton -L $$(<:.o=.llinked3.o) name $$(notdir $$(<:.bpf.o=_lskel)) > $$@
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci$(TRUNNER_BPF_SKELS_LINKED): $(TRUNNER_BPF_OBJS) $(BPFTOOL) | $(TRUNNER_OUTPUT)
51562306a36Sopenharmony_ci	$$(call msg,LINK-BPF,$(TRUNNER_BINARY),$$(@:.skel.h=.bpf.o))
51662306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked1.o) $$(addprefix $(TRUNNER_OUTPUT)/,$$($$(@F)-deps))
51762306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked1.o)
51862306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen object $$(@:.skel.h=.linked3.o) $$(@:.skel.h=.linked2.o)
51962306a36Sopenharmony_ci	$(Q)diff $$(@:.skel.h=.linked2.o) $$(@:.skel.h=.linked3.o)
52062306a36Sopenharmony_ci	$$(call msg,GEN-SKEL,$(TRUNNER_BINARY),$$@)
52162306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen skeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$@
52262306a36Sopenharmony_ci	$(Q)$$(BPFTOOL) gen subskeleton $$(@:.skel.h=.linked3.o) name $$(notdir $$(@:.skel.h=)) > $$(@:.skel.h=.subskel.h)
52362306a36Sopenharmony_ciendif
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci# ensure we set up tests.h header generation rule just once
52662306a36Sopenharmony_ciifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),)
52762306a36Sopenharmony_ci$(TRUNNER_TESTS_DIR)-tests-hdr := y
52862306a36Sopenharmony_ci$(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c
52962306a36Sopenharmony_ci	$$(call msg,TEST-HDR,$(TRUNNER_BINARY),$$@)
53062306a36Sopenharmony_ci	$$(shell (echo '/* Generated header, do not edit */';					\
53162306a36Sopenharmony_ci		  sed -n -E 's/^void (serial_)?test_([a-zA-Z0-9_]+)\((void)?\).*/DEFINE_TEST(\2)/p'	\
53262306a36Sopenharmony_ci			$(TRUNNER_TESTS_DIR)/*.c | sort ;	\
53362306a36Sopenharmony_ci		 ) > $$@)
53462306a36Sopenharmony_ciendif
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci# compile individual test files
53762306a36Sopenharmony_ci# Note: we cd into output directory to ensure embedded BPF object is found
53862306a36Sopenharmony_ci$(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o:			\
53962306a36Sopenharmony_ci		      $(TRUNNER_TESTS_DIR)/%.c				\
54062306a36Sopenharmony_ci		      $(TRUNNER_EXTRA_HDRS)				\
54162306a36Sopenharmony_ci		      $(TRUNNER_BPF_OBJS)				\
54262306a36Sopenharmony_ci		      $(TRUNNER_BPF_SKELS)				\
54362306a36Sopenharmony_ci		      $(TRUNNER_BPF_LSKELS)				\
54462306a36Sopenharmony_ci		      $(TRUNNER_BPF_SKELS_LINKED)			\
54562306a36Sopenharmony_ci		      $$(BPFOBJ) | $(TRUNNER_OUTPUT)
54662306a36Sopenharmony_ci	$$(call msg,TEST-OBJ,$(TRUNNER_BINARY),$$@)
54762306a36Sopenharmony_ci	$(Q)cd $$(@D) && $$(CC) -I. $$(CFLAGS) -c $(CURDIR)/$$< $$(LDLIBS) -o $$(@F)
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o:				\
55062306a36Sopenharmony_ci		       %.c						\
55162306a36Sopenharmony_ci		       $(TRUNNER_EXTRA_HDRS)				\
55262306a36Sopenharmony_ci		       $(TRUNNER_TESTS_HDR)				\
55362306a36Sopenharmony_ci		       $$(BPFOBJ) | $(TRUNNER_OUTPUT)
55462306a36Sopenharmony_ci	$$(call msg,EXT-OBJ,$(TRUNNER_BINARY),$$@)
55562306a36Sopenharmony_ci	$(Q)$$(CC) $$(CFLAGS) -c $$< $$(LDLIBS) -o $$@
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci# non-flavored in-srctree builds receive special treatment, in particular, we
55862306a36Sopenharmony_ci# do not need to copy extra resources (see e.g. test_btf_dump_case())
55962306a36Sopenharmony_ci$(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT)
56062306a36Sopenharmony_ciifneq ($2:$(OUTPUT),:$(shell pwd))
56162306a36Sopenharmony_ci	$$(call msg,EXT-COPY,$(TRUNNER_BINARY),$(TRUNNER_EXTRA_FILES))
56262306a36Sopenharmony_ci	$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
56362306a36Sopenharmony_ciendif
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci$(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS)			\
56662306a36Sopenharmony_ci			     $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ)		\
56762306a36Sopenharmony_ci			     $(RESOLVE_BTFIDS)				\
56862306a36Sopenharmony_ci			     $(TRUNNER_BPFTOOL)				\
56962306a36Sopenharmony_ci			     | $(TRUNNER_BINARY)-extras
57062306a36Sopenharmony_ci	$$(call msg,BINARY,,$$@)
57162306a36Sopenharmony_ci	$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
57262306a36Sopenharmony_ci	$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@
57362306a36Sopenharmony_ci	$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \
57462306a36Sopenharmony_ci		   $(OUTPUT)/$(if $2,$2/)bpftool
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ciendef
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_ci# Define test_progs test runner.
57962306a36Sopenharmony_ciTRUNNER_TESTS_DIR := prog_tests
58062306a36Sopenharmony_ciTRUNNER_BPF_PROGS_DIR := progs
58162306a36Sopenharmony_ciTRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
58262306a36Sopenharmony_ci			 network_helpers.c testing_helpers.c		\
58362306a36Sopenharmony_ci			 btf_helpers.c flow_dissector_load.h		\
58462306a36Sopenharmony_ci			 cap_helpers.c test_loader.c xsk.c disasm.c	\
58562306a36Sopenharmony_ci			 json_writer.c unpriv_helpers.c 		\
58662306a36Sopenharmony_ci			 ip_check_defrag_frags.h
58762306a36Sopenharmony_ciTRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
58862306a36Sopenharmony_ci		       $(OUTPUT)/liburandom_read.so			\
58962306a36Sopenharmony_ci		       $(OUTPUT)/xdp_synproxy				\
59062306a36Sopenharmony_ci		       $(OUTPUT)/sign-file				\
59162306a36Sopenharmony_ci		       $(OUTPUT)/uprobe_multi				\
59262306a36Sopenharmony_ci		       ima_setup.sh 					\
59362306a36Sopenharmony_ci		       verify_sig_setup.sh				\
59462306a36Sopenharmony_ci		       $(wildcard progs/btf_dump_test_case_*.c)		\
59562306a36Sopenharmony_ci		       $(wildcard progs/*.bpf.o)
59662306a36Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
59762306a36Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS) -DENABLE_ATOMICS_TESTS
59862306a36Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs))
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci# Define test_progs-no_alu32 test runner.
60162306a36Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := CLANG_NOALU32_BPF_BUILD_RULE
60262306a36Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
60362306a36Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32))
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci# Define test_progs-cpuv4 test runner.
60662306a36Sopenharmony_ciifneq ($(CLANG_CPUV4),)
60762306a36Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := CLANG_CPUV4_BPF_BUILD_RULE
60862306a36Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(CLANG_CFLAGS)
60962306a36Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs,cpuv4))
61062306a36Sopenharmony_ciendif
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci# Define test_progs BPF-GCC-flavored test runner.
61362306a36Sopenharmony_ciifneq ($(BPF_GCC),)
61462306a36Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE
61562306a36Sopenharmony_ciTRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc,)
61662306a36Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc))
61762306a36Sopenharmony_ciendif
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ci# Define test_maps test runner.
62062306a36Sopenharmony_ciTRUNNER_TESTS_DIR := map_tests
62162306a36Sopenharmony_ciTRUNNER_BPF_PROGS_DIR := progs
62262306a36Sopenharmony_ciTRUNNER_EXTRA_SOURCES := test_maps.c
62362306a36Sopenharmony_ciTRUNNER_EXTRA_FILES :=
62462306a36Sopenharmony_ciTRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built)
62562306a36Sopenharmony_ciTRUNNER_BPF_CFLAGS :=
62662306a36Sopenharmony_ci$(eval $(call DEFINE_TEST_RUNNER,test_maps))
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci# Define test_verifier test runner.
62962306a36Sopenharmony_ci# It is much simpler than test_maps/test_progs and sufficiently different from
63062306a36Sopenharmony_ci# them (e.g., test.h is using completely pattern), that it's worth just
63162306a36Sopenharmony_ci# explicitly defining all the rules explicitly.
63262306a36Sopenharmony_civerifier/tests.h: verifier/*.c
63362306a36Sopenharmony_ci	$(shell ( cd verifier/; \
63462306a36Sopenharmony_ci		  echo '/* Generated header, do not edit */'; \
63562306a36Sopenharmony_ci		  echo '#ifdef FILL_ARRAY'; \
63662306a36Sopenharmony_ci		  ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \
63762306a36Sopenharmony_ci		  echo '#endif' \
63862306a36Sopenharmony_ci		) > verifier/tests.h)
63962306a36Sopenharmony_ci$(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
64062306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
64162306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci$(OUTPUT)/xskxceiver: xskxceiver.c xskxceiver.h $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT)
64462306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
64562306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci$(OUTPUT)/xdp_hw_metadata: xdp_hw_metadata.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h | $(OUTPUT)
64862306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
64962306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci$(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp_features.skel.h | $(OUTPUT)
65262306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
65362306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci# Make sure we are able to include and link libbpf against c++.
65662306a36Sopenharmony_ci$(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
65762306a36Sopenharmony_ci	$(call msg,CXX,,$@)
65862306a36Sopenharmony_ci	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci# Benchmark runner
66162306a36Sopenharmony_ci$(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
66262306a36Sopenharmony_ci	$(call msg,CC,,$@)
66362306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) -O2 -c $(filter %.c,$^) $(LDLIBS) -o $@
66462306a36Sopenharmony_ci$(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h
66562306a36Sopenharmony_ci$(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h
66662306a36Sopenharmony_ci$(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \
66762306a36Sopenharmony_ci			    $(OUTPUT)/perfbuf_bench.skel.h
66862306a36Sopenharmony_ci$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h
66962306a36Sopenharmony_ci$(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h
67062306a36Sopenharmony_ci$(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h
67162306a36Sopenharmony_ci$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_update_bench.skel.h
67262306a36Sopenharmony_ci$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h
67362306a36Sopenharmony_ci$(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h
67462306a36Sopenharmony_ci$(OUTPUT)/bench_local_storage_create.o: $(OUTPUT)/bench_local_storage_create.skel.h
67562306a36Sopenharmony_ci$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h
67662306a36Sopenharmony_ci$(OUTPUT)/bench_htab_mem.o: $(OUTPUT)/htab_mem_bench.skel.h
67762306a36Sopenharmony_ci$(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ)
67862306a36Sopenharmony_ci$(OUTPUT)/bench: LDLIBS += -lm
67962306a36Sopenharmony_ci$(OUTPUT)/bench: $(OUTPUT)/bench.o \
68062306a36Sopenharmony_ci		 $(TESTING_HELPERS) \
68162306a36Sopenharmony_ci		 $(TRACE_HELPERS) \
68262306a36Sopenharmony_ci		 $(CGROUP_HELPERS) \
68362306a36Sopenharmony_ci		 $(OUTPUT)/bench_count.o \
68462306a36Sopenharmony_ci		 $(OUTPUT)/bench_rename.o \
68562306a36Sopenharmony_ci		 $(OUTPUT)/bench_trigger.o \
68662306a36Sopenharmony_ci		 $(OUTPUT)/bench_ringbufs.o \
68762306a36Sopenharmony_ci		 $(OUTPUT)/bench_bloom_filter_map.o \
68862306a36Sopenharmony_ci		 $(OUTPUT)/bench_bpf_loop.o \
68962306a36Sopenharmony_ci		 $(OUTPUT)/bench_strncmp.o \
69062306a36Sopenharmony_ci		 $(OUTPUT)/bench_bpf_hashmap_full_update.o \
69162306a36Sopenharmony_ci		 $(OUTPUT)/bench_local_storage.o \
69262306a36Sopenharmony_ci		 $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \
69362306a36Sopenharmony_ci		 $(OUTPUT)/bench_bpf_hashmap_lookup.o \
69462306a36Sopenharmony_ci		 $(OUTPUT)/bench_local_storage_create.o \
69562306a36Sopenharmony_ci		 $(OUTPUT)/bench_htab_mem.o \
69662306a36Sopenharmony_ci		 #
69762306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
69862306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci$(OUTPUT)/veristat.o: $(BPFOBJ)
70162306a36Sopenharmony_ci$(OUTPUT)/veristat: $(OUTPUT)/veristat.o
70262306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
70362306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci$(OUTPUT)/uprobe_multi: uprobe_multi.c
70662306a36Sopenharmony_ci	$(call msg,BINARY,,$@)
70762306a36Sopenharmony_ci	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ciEXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
71062306a36Sopenharmony_ci	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
71162306a36Sopenharmony_ci	feature bpftool							\
71262306a36Sopenharmony_ci	$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h	\
71362306a36Sopenharmony_ci			       no_alu32 cpuv4 bpf_gcc bpf_testmod.ko	\
71462306a36Sopenharmony_ci			       liburandom_read.so)
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci.PHONY: docs docs-clean
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci# Delete partially updated (corrupted) files on error
71962306a36Sopenharmony_ci.DELETE_ON_ERROR:
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ciDEFAULT_INSTALL_RULE := $(INSTALL_RULE)
72262306a36Sopenharmony_cioverride define INSTALL_RULE
72362306a36Sopenharmony_ci	$(DEFAULT_INSTALL_RULE)
72462306a36Sopenharmony_ci	@for DIR in $(TEST_INST_SUBDIRS); do		  \
72562306a36Sopenharmony_ci		mkdir -p $(INSTALL_PATH)/$$DIR;   \
72662306a36Sopenharmony_ci		rsync -a $(OUTPUT)/$$DIR/*.bpf.o $(INSTALL_PATH)/$$DIR;\
72762306a36Sopenharmony_ci	done
72862306a36Sopenharmony_ciendef
729