xref: /linux/samples/bpf/Makefile (revision b1992c37)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2787360f8SJakub Kicinski
3*b1992c37SMasahiro YamadaBPF_SAMPLES_PATH ?= $(abspath $(src))
4787360f8SJakub KicinskiTOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
5787360f8SJakub Kicinski
6929bef46SQuentin Monnetpound := \#
7929bef46SQuentin Monnet
83c731ebaSAlexei Starovoitov# List of programs to build
91d97c6c2SIvan Khoronzhuktprogs-y := test_lru_dist
101d97c6c2SIvan Khoronzhuktprogs-y += sock_example
111d97c6c2SIvan Khoronzhuktprogs-y += fds_example
121d97c6c2SIvan Khoronzhuktprogs-y += sockex1
131d97c6c2SIvan Khoronzhuktprogs-y += sockex2
141d97c6c2SIvan Khoronzhuktprogs-y += sockex3
151d97c6c2SIvan Khoronzhuktprogs-y += tracex1
161d97c6c2SIvan Khoronzhuktprogs-y += tracex2
171d97c6c2SIvan Khoronzhuktprogs-y += tracex3
181d97c6c2SIvan Khoronzhuktprogs-y += tracex4
191d97c6c2SIvan Khoronzhuktprogs-y += tracex5
201d97c6c2SIvan Khoronzhuktprogs-y += tracex6
211d97c6c2SIvan Khoronzhuktprogs-y += tracex7
221d97c6c2SIvan Khoronzhuktprogs-y += test_probe_write_user
231d97c6c2SIvan Khoronzhuktprogs-y += trace_output
241d97c6c2SIvan Khoronzhuktprogs-y += lathist
251d97c6c2SIvan Khoronzhuktprogs-y += offwaketime
261d97c6c2SIvan Khoronzhuktprogs-y += spintest
271d97c6c2SIvan Khoronzhuktprogs-y += map_perf_test
281d97c6c2SIvan Khoronzhuktprogs-y += test_overhead
291d97c6c2SIvan Khoronzhuktprogs-y += test_cgrp2_array_pin
301d97c6c2SIvan Khoronzhuktprogs-y += test_cgrp2_attach
311d97c6c2SIvan Khoronzhuktprogs-y += test_cgrp2_sock
321d97c6c2SIvan Khoronzhuktprogs-y += test_cgrp2_sock2
331d97c6c2SIvan Khoronzhuktprogs-y += xdp_router_ipv4
341d97c6c2SIvan Khoronzhuktprogs-y += test_current_task_under_cgroup
351d97c6c2SIvan Khoronzhuktprogs-y += trace_event
361d97c6c2SIvan Khoronzhuktprogs-y += sampleip
371d97c6c2SIvan Khoronzhuktprogs-y += tc_l2_redirect
381d97c6c2SIvan Khoronzhuktprogs-y += lwt_len_hist
391d97c6c2SIvan Khoronzhuktprogs-y += xdp_tx_iptunnel
401d97c6c2SIvan Khoronzhuktprogs-y += test_map_in_map
415984dc6cSPrashant Bholetprogs-y += per_socket_stats_example
421d97c6c2SIvan Khoronzhuktprogs-y += syscall_tp
431d97c6c2SIvan Khoronzhuktprogs-y += cpustat
441d97c6c2SIvan Khoronzhuktprogs-y += xdp_adjust_tail
451d97c6c2SIvan Khoronzhuktprogs-y += xdp_fwd
461d97c6c2SIvan Khoronzhuktprogs-y += task_fd_query
471d97c6c2SIvan Khoronzhuktprogs-y += ibumad
481d97c6c2SIvan Khoronzhuktprogs-y += hbm
493c731ebaSAlexei Starovoitov
5043371c83SJoe Stringer# Libbpf dependencies
513f7a3318SQuentin MonnetLIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
523f7a3318SQuentin MonnetLIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
533f7a3318SQuentin MonnetLIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
543f7a3318SQuentin MonnetLIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
553f7a3318SQuentin MonnetLIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
56787360f8SJakub Kicinski
579d1f1594SRoman GushchinCGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
5828dbf861SYonghong SongTRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
596e1051a5SKumar Kartikeya DwivediXDP_SAMPLE := xdp_sample_user.o
6043371c83SJoe Stringer
611a9b268cSJakub Kicinskifds_example-objs := fds_example.o
621a9b268cSJakub Kicinskisockex1-objs := sockex1_user.o
631a9b268cSJakub Kicinskisockex2-objs := sockex2_user.o
64bc1a8597SDaniel T. Leesockex3-objs := sockex3_user.o
6563841bc0SDaniel T. Leetracex1-objs := tracex1_user.o $(TRACE_HELPERS)
6663841bc0SDaniel T. Leetracex2-objs := tracex2_user.o
6763841bc0SDaniel T. Leetracex3-objs := tracex3_user.o
6863841bc0SDaniel T. Leetracex4-objs := tracex4_user.o
69bc1a8597SDaniel T. Leetracex5-objs := tracex5_user.o $(TRACE_HELPERS)
7063841bc0SDaniel T. Leetracex6-objs := tracex6_user.o
7163841bc0SDaniel T. Leetracex7-objs := tracex7_user.o
723677d0a1SDaniel T. Leetest_probe_write_user-objs := test_probe_write_user_user.o
73ceb5dea5SDaniel T. Leetrace_output-objs := trace_output_user.o
743677d0a1SDaniel T. Leelathist-objs := lathist_user.o
75f0c328f8SDaniel T. Leeoffwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
763677d0a1SDaniel T. Leespintest-objs := spintest_user.o $(TRACE_HELPERS)
7735a8b6ddSDaniel T. Leemap_perf_test-objs := map_perf_test_user.o
78c6497df0SDaniel T. Leetest_overhead-objs := test_overhead_user.o
790cc54db1SJakub Kicinskitest_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
800cc54db1SJakub Kicinskitest_cgrp2_attach-objs := test_cgrp2_attach.o
810cc54db1SJakub Kicinskitest_cgrp2_sock-objs := test_cgrp2_sock.o
82d89af13cSDaniel T. Leetest_cgrp2_sock2-objs := test_cgrp2_sock2.o
833677d0a1SDaniel T. Leetest_current_task_under_cgroup-objs := $(CGROUP_HELPERS) \
849e6e60ecSSargun Dhillon				       test_current_task_under_cgroup_user.o
85aa5e2af6SDaniel T. Leetrace_event-objs := trace_event_user.o $(TRACE_HELPERS)
86aa5e2af6SDaniel T. Leesampleip-objs := sampleip_user.o $(TRACE_HELPERS)
87ceb5dea5SDaniel T. Leetc_l2_redirect-objs := tc_l2_redirect_user.o
88ceb5dea5SDaniel T. Leelwt_len_hist-objs := lwt_len_hist_user.o
89bbaf6029SMaciej Fijalkowskixdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
9088795b4aSDaniel T. Leetest_map_in_map-objs := test_map_in_map_user.o
910cc54db1SJakub Kicinskiper_socket_stats_example-objs := cookie_uid_helper_example.o
92f0c328f8SDaniel T. Leesyscall_tp-objs := syscall_tp_user.o
93f0c328f8SDaniel T. Leecpustat-objs := cpustat_user.o
940cc54db1SJakub Kicinskixdp_adjust_tail-objs := xdp_adjust_tail_user.o
95e1a40ef4SJakub Kicinskixdp_fwd-objs := xdp_fwd_user.o
964fe66415SDaniel T. Leetask_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
97763af200SDaniel T. Leeibumad-objs := ibumad_user.o
98c5815ac7SDaniel T. Leehbm-objs := hbm.o $(CGROUP_HELPERS)
993c731ebaSAlexei Starovoitov
10085bf1f51SLorenzo Bianconixdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE)
1016e1051a5SKumar Kartikeya Dwivedi
1023c731ebaSAlexei Starovoitov# Tell kbuild to always build the programs
1035f2fb52fSMasahiro Yamadaalways-y := $(tprogs-y)
1045f2fb52fSMasahiro Yamadaalways-y += sockex1_kern.o
1055f2fb52fSMasahiro Yamadaalways-y += sockex2_kern.o
1065f2fb52fSMasahiro Yamadaalways-y += sockex3_kern.o
1074a0ee788SDaniel T. Leealways-y += tracex1.bpf.o
108d4fffba4SDaniel T. Leealways-y += tracex2.bpf.o
1094a0ee788SDaniel T. Leealways-y += tracex3.bpf.o
1104a0ee788SDaniel T. Leealways-y += tracex4.bpf.o
1114a0ee788SDaniel T. Leealways-y += tracex5.bpf.o
1124a0ee788SDaniel T. Leealways-y += tracex6.bpf.o
1134a0ee788SDaniel T. Leealways-y += tracex7.bpf.o
114e04946f5SDaniel T. Leealways-y += sock_flags.bpf.o
115d4fffba4SDaniel T. Leealways-y += test_probe_write_user.bpf.o
116d4fffba4SDaniel T. Leealways-y += trace_output.bpf.o
1175f2fb52fSMasahiro Yamadaalways-y += tcbpf1_kern.o
1185f2fb52fSMasahiro Yamadaalways-y += tc_l2_redirect_kern.o
1195f2fb52fSMasahiro Yamadaalways-y += lathist_kern.o
1204a0ee788SDaniel T. Leealways-y += offwaketime.bpf.o
1214a0ee788SDaniel T. Leealways-y += spintest.bpf.o
122d4fffba4SDaniel T. Leealways-y += map_perf_test.bpf.o
123e04946f5SDaniel T. Leealways-y += test_overhead_tp.bpf.o
124e04946f5SDaniel T. Leealways-y += test_overhead_raw_tp.bpf.o
125e04946f5SDaniel T. Leealways-y += test_overhead_kprobe.bpf.o
1265f2fb52fSMasahiro Yamadaalways-y += parse_varlen.o parse_simple.o parse_ldabs.o
127e04946f5SDaniel T. Leealways-y += test_cgrp2_tc.bpf.o
128d4fffba4SDaniel T. Leealways-y += test_current_task_under_cgroup.bpf.o
1295f2fb52fSMasahiro Yamadaalways-y += trace_event_kern.o
1305f2fb52fSMasahiro Yamadaalways-y += sampleip_kern.o
131e04946f5SDaniel T. Leealways-y += lwt_len_hist.bpf.o
1325f2fb52fSMasahiro Yamadaalways-y += xdp_tx_iptunnel_kern.o
133e04946f5SDaniel T. Leealways-y += test_map_in_map.bpf.o
1345f2fb52fSMasahiro Yamadaalways-y += tcp_synrto_kern.o
1355f2fb52fSMasahiro Yamadaalways-y += tcp_rwnd_kern.o
1365f2fb52fSMasahiro Yamadaalways-y += tcp_bufs_kern.o
1375f2fb52fSMasahiro Yamadaalways-y += tcp_cong_kern.o
1385f2fb52fSMasahiro Yamadaalways-y += tcp_iw_kern.o
1395f2fb52fSMasahiro Yamadaalways-y += tcp_clamp_kern.o
1405f2fb52fSMasahiro Yamadaalways-y += tcp_basertt_kern.o
1415f2fb52fSMasahiro Yamadaalways-y += tcp_tos_reflect_kern.o
1425f2fb52fSMasahiro Yamadaalways-y += tcp_dumpstats_kern.o
1435f2fb52fSMasahiro Yamadaalways-y += xdp2skb_meta_kern.o
1445f2fb52fSMasahiro Yamadaalways-y += syscall_tp_kern.o
1455f2fb52fSMasahiro Yamadaalways-y += cpustat_kern.o
1465f2fb52fSMasahiro Yamadaalways-y += xdp_adjust_tail_kern.o
1475f2fb52fSMasahiro Yamadaalways-y += xdp_fwd_kern.o
1485f2fb52fSMasahiro Yamadaalways-y += task_fd_query_kern.o
1495f2fb52fSMasahiro Yamadaalways-y += ibumad_kern.o
1505f2fb52fSMasahiro Yamadaalways-y += hbm_out_kern.o
1515f2fb52fSMasahiro Yamadaalways-y += hbm_edt_kern.o
1523c731ebaSAlexei Starovoitov
153870f09f1SViktor MalikTPROGS_CFLAGS = $(TPROGS_USER_CFLAGS)
154870f09f1SViktor MalikTPROGS_LDFLAGS = $(TPROGS_USER_LDFLAGS)
155870f09f1SViktor Malik
1560e865aedSIvan Khoronzhukifeq ($(ARCH), arm)
1570e865aedSIvan Khoronzhuk# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
1580e865aedSIvan Khoronzhuk# headers when arm instruction set identification is requested.
1590e865aedSIvan KhoronzhukARM_ARCH_SELECTOR := $(filter -D__LINUX_ARM_ARCH__%, $(KBUILD_CFLAGS))
1600e865aedSIvan KhoronzhukBPF_EXTRA_CFLAGS := $(ARM_ARCH_SELECTOR)
1611d97c6c2SIvan KhoronzhukTPROGS_CFLAGS += $(ARM_ARCH_SELECTOR)
1620e865aedSIvan Khoronzhukendif
1630e865aedSIvan Khoronzhuk
164190d1c92STiezhu Yangifeq ($(ARCH), mips)
165190d1c92STiezhu YangTPROGS_CFLAGS += -D__SANE_USERSPACE_TYPES__
166058107abSTiezhu Yangifdef CONFIG_MACH_LOONGSON64
167058107abSTiezhu YangBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-loongson64
168058107abSTiezhu YangBPF_EXTRA_CFLAGS += -I$(srctree)/arch/mips/include/asm/mach-generic
169058107abSTiezhu Yangendif
170190d1c92STiezhu Yangendif
171190d1c92STiezhu Yang
17210cb3d87SIvan KhoronzhukTPROGS_CFLAGS += -Wall -O2
17310cb3d87SIvan KhoronzhukTPROGS_CFLAGS += -Wmissing-prototypes
17410cb3d87SIvan KhoronzhukTPROGS_CFLAGS += -Wstrict-prototypes
1759e09b750SRuowen QinTPROGS_CFLAGS += $(call try-run,\
1769e09b750SRuowen Qin	printf "int main() { return 0; }" |\
1779e09b750SRuowen Qin	$(CC) -Werror -fsanitize=bounds -x c - -o "$$TMP",-fsanitize=bounds,)
17810cb3d87SIvan Khoronzhuk
1791d97c6c2SIvan KhoronzhukTPROGS_CFLAGS += -I$(objtree)/usr/include
1801d97c6c2SIvan KhoronzhukTPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
1813f7a3318SQuentin MonnetTPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
1821d97c6c2SIvan KhoronzhukTPROGS_CFLAGS += -I$(srctree)/tools/include
1831d97c6c2SIvan KhoronzhukTPROGS_CFLAGS += -I$(srctree)/tools/perf
184c698eaebSRong TaoTPROGS_CFLAGS += -I$(srctree)/tools/lib
18514684b93SDavid S. MillerTPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
1861d97c6c2SIvan Khoronzhuk
187b2327c10SIvan Khoronzhukifdef SYSROOT
188b2327c10SIvan KhoronzhukTPROGS_CFLAGS += --sysroot=$(SYSROOT)
189b2327c10SIvan KhoronzhukTPROGS_LDFLAGS := -L$(SYSROOT)/usr/lib
190b2327c10SIvan Khoronzhukendif
191b2327c10SIvan Khoronzhuk
1925615ed47SToke Høiland-JørgensenTPROGS_LDLIBS			+= $(LIBBPF) -lelf -lz
193587323cfSLorenzo BianconiTPROGLDLIBS_xdp_router_ipv4	+= -lm -pthread
1941d97c6c2SIvan KhoronzhukTPROGLDLIBS_tracex4		+= -lrt
1951d97c6c2SIvan KhoronzhukTPROGLDLIBS_trace_output	+= -lrt
1961d97c6c2SIvan KhoronzhukTPROGLDLIBS_map_perf_test	+= -lrt
1971d97c6c2SIvan KhoronzhukTPROGLDLIBS_test_overhead	+= -lrt
1981d97c6c2SIvan Khoronzhuk
199bdefbbf2SJesper Dangaard Brouer# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
200628add78STiezhu Yang# make M=samples/bpf LLC=~/git/llvm-project/llvm/build/bin/llc CLANG=~/git/llvm-project/llvm/build/bin/clang
2016ccfba75SJesper Dangaard BrouerLLC ?= llc
202bdefbbf2SJesper Dangaard BrouerCLANG ?= clang
2039618bde4SYonghong SongOPT ?= opt
2049618bde4SYonghong SongLLVM_DIS ?= llvm-dis
2059778cfdfSTaeung SongLLVM_OBJCOPY ?= llvm-objcopy
20644ce0ac1SPu LehuiLLVM_READELF ?= llvm-readelf
2079778cfdfSTaeung SongBTF_PAHOLE ?= pahole
2086ccfba75SJesper Dangaard Brouer
209876e88e3SJoel Fernandes# Detect that we're cross compiling and use the cross compiler
210876e88e3SJoel Fernandesifdef CROSS_COMPILE
211518c1340SIvan KhoronzhukCLANG_ARCH_ARGS = --target=$(notdir $(CROSS_COMPILE:%-=%))
212876e88e3SJoel Fernandesendif
213876e88e3SJoel Fernandes
2140ed3cc4aSJakub Kicinski# Don't evaluate probes and warnings if we need to run make recursively
2150ed3cc4aSJakub Kicinskiifneq ($(src),)
216929bef46SQuentin MonnetHDR_PROBE := $(shell printf "$(pound)include <linux/types.h>\n struct list_head { int a; }; int main() { return 0; }" | \
217a833effaSIvan Khoronzhuk	$(CC) $(TPROGS_CFLAGS) $(TPROGS_LDFLAGS) -x c - \
218a833effaSIvan Khoronzhuk	-o /dev/null 2>/dev/null && echo okay)
21907c3bbdbSJakub Kicinski
22007c3bbdbSJakub Kicinskiifeq ($(HDR_PROBE),)
22107c3bbdbSJakub Kicinski$(warning WARNING: Detected possible issues with include path.)
22207c3bbdbSJakub Kicinski$(warning WARNING: Please install kernel headers locally (make headers_install).)
22307c3bbdbSJakub Kicinskiendif
22407c3bbdbSJakub Kicinski
2259778cfdfSTaeung SongBTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris)
2269778cfdfSTaeung SongBTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF)
2279778cfdfSTaeung SongBTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
2289ce6ae22SYonghong SongBTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \
229bbaf1ff0SFangrui Song			  $(CLANG) --target=bpf -O2 -g -c -x c - -o ./llvm_btf_verify.o; \
23044ce0ac1SPu Lehui			  $(LLVM_READELF) -S ./llvm_btf_verify.o | grep BTF; \
2319ce6ae22SYonghong Song			  /bin/rm -f ./llvm_btf_verify.o)
2329778cfdfSTaeung Song
23345027897SToke Høiland-JørgensenBPF_EXTRA_CFLAGS += -fno-stack-protector
2349ce6ae22SYonghong Songifneq ($(BTF_LLVM_PROBE),)
2352a560df7SIvan Khoronzhuk	BPF_EXTRA_CFLAGS += -g
2369ce6ae22SYonghong Songelse
2379778cfdfSTaeung Songifneq ($(and $(BTF_LLC_PROBE),$(BTF_PAHOLE_PROBE),$(BTF_OBJCOPY_PROBE)),)
2382a560df7SIvan Khoronzhuk	BPF_EXTRA_CFLAGS += -g
2399778cfdfSTaeung Song	LLC_FLAGS += -mattr=dwarfris
2409778cfdfSTaeung Song	DWARF2BTF = y
2419778cfdfSTaeung Songendif
2429ce6ae22SYonghong Songendif
2430ed3cc4aSJakub Kicinskiendif
2449778cfdfSTaeung Song
245b62a796cSJesper Dangaard Brouer# Trick to allow make to be run from this directory
246787360f8SJakub Kicinskiall:
2476cc2c876SJesper Dangaard Brouer	$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
248b62a796cSJesper Dangaard Brouer
249b62a796cSJesper Dangaard Brouerclean:
250e19b7ceeSUwe Kleine-König	$(MAKE) -C ../../ M=$(CURDIR) clean
251b2e5e93aSToke Høiland-Jørgensen	@find $(CURDIR) -type f -name '*~' -delete
2523f7a3318SQuentin Monnet	@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
253b62a796cSJesper Dangaard Brouer
254a60d24e7SQuentin Monnet$(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OUTPUT)
255787360f8SJakub Kicinski# Fix up variables inherited from Kbuild that tools/ build system won't like
2563f7a3318SQuentin Monnet	$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
257f56bcfadSViktor Malik		LDFLAGS="$(TPROGS_LDFLAGS)" srctree=$(BPF_SAMPLES_PATH)/../../ \
2583f7a3318SQuentin Monnet		O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
2593f7a3318SQuentin Monnet		$@ install_headers
260c25ef6a5SMickaël Salaün
261384b6b3bSKumar Kartikeya DwivediBPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
2623f7a3318SQuentin MonnetBPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
26337db10bcSViktor MalikDEFAULT_BPFTOOL := $(BPFTOOL_OUTPUT)/bootstrap/bpftool
26437db10bcSViktor MalikBPFTOOL ?= $(DEFAULT_BPFTOOL)
26537db10bcSViktor Malik$(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
2663f7a3318SQuentin Monnet	$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ 		\
2672e496628SPu Lehui		OUTPUT=$(BPFTOOL_OUTPUT)/ bootstrap
2683f7a3318SQuentin Monnet
2693f7a3318SQuentin Monnet$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
2703f7a3318SQuentin Monnet	$(call msg,MKDIR,$@)
2713f7a3318SQuentin Monnet	$(Q)mkdir -p $@
272384b6b3bSKumar Kartikeya Dwivedi
2734b7190e8SDavid Daney$(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE
2744b7190e8SDavid Daney	$(call filechk,offsets,__SYSCALL_NRS_H__)
2754b7190e8SDavid Daney
2762c667d77SMasahiro Yamadatargets += syscall_nrs.s
2774b7190e8SDavid Daneyclean-files += syscall_nrs.h
2784b7190e8SDavid Daney
2794b7190e8SDavid DaneyFORCE:
2804b7190e8SDavid Daney
2814b7190e8SDavid Daney
282bdefbbf2SJesper Dangaard Brouer# Verify LLVM compiler tools are available and bpf target is supported by llc
283bdefbbf2SJesper Dangaard Brouer.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
2847b01dd57SJesper Dangaard Brouer
285bdefbbf2SJesper Dangaard Brouerverify_cmds: $(CLANG) $(LLC)
286bdefbbf2SJesper Dangaard Brouer	@for TOOL in $^ ; do \
287bdefbbf2SJesper Dangaard Brouer		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
288bdefbbf2SJesper Dangaard Brouer			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
2897b01dd57SJesper Dangaard Brouer			exit 1; \
290bdefbbf2SJesper Dangaard Brouer		else true; fi; \
291bdefbbf2SJesper Dangaard Brouer	done
2927b01dd57SJesper Dangaard Brouer
293bdefbbf2SJesper Dangaard Brouerverify_target_bpf: verify_cmds
2947b01dd57SJesper Dangaard Brouer	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
2957b01dd57SJesper Dangaard Brouer		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
2967b01dd57SJesper Dangaard Brouer		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
2977b01dd57SJesper Dangaard Brouer		exit 2; \
2987b01dd57SJesper Dangaard Brouer	else true; fi
2997b01dd57SJesper Dangaard Brouer
300787360f8SJakub Kicinski$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
301787360f8SJakub Kicinski$(src)/*.c: verify_target_bpf $(LIBBPF)
3027b01dd57SJesper Dangaard Brouer
3033f7a3318SQuentin Monnetlibbpf_hdrs: $(LIBBPF)
304527024f7SAndrii Nakryiko$(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_hdrs
3053f7a3318SQuentin Monnet
3063f7a3318SQuentin Monnet.PHONY: libbpf_hdrs
3073f7a3318SQuentin Monnet
30885bf1f51SLorenzo Bianconi$(obj)/xdp_router_ipv4_user.o: $(obj)/xdp_router_ipv4.skel.h
3096e1051a5SKumar Kartikeya Dwivedi
3104a0ee788SDaniel T. Lee$(obj)/tracex5.bpf.o: $(obj)/syscall_nrs.h
311187d0738Sbrakmo$(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
312a1270fe9Sbrakmo$(obj)/hbm.o: $(src)/hbm.h
31371634d7fSbrakmo$(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
3144b7190e8SDavid Daney
315527024f7SAndrii Nakryiko# Override includes for xdp_sample_user.o because $(srctree)/usr/include in
316527024f7SAndrii Nakryiko# TPROGS_CFLAGS causes conflicts
317e64fbcaaSAlexander LobakinXDP_SAMPLE_CFLAGS += -Wall -O2 \
318527024f7SAndrii Nakryiko		     -I$(src)/../../tools/include \
319527024f7SAndrii Nakryiko		     -I$(src)/../../tools/include/uapi \
320527024f7SAndrii Nakryiko		     -I$(LIBBPF_INCLUDE) \
321527024f7SAndrii Nakryiko		     -I$(src)/../../tools/testing/selftests/bpf
322527024f7SAndrii Nakryiko
323870f09f1SViktor Malik$(obj)/$(XDP_SAMPLE): TPROGS_CFLAGS = $(XDP_SAMPLE_CFLAGS) $(TPROGS_USER_CFLAGS)
324527024f7SAndrii Nakryiko$(obj)/$(XDP_SAMPLE): $(src)/xdp_sample_user.h $(src)/xdp_sample_shared.h
325c698eaebSRong Tao# Override includes for trace_helpers.o because __must_check won't be defined
326c698eaebSRong Tao# in our include path.
327c698eaebSRong Tao$(obj)/$(TRACE_HELPERS): TPROGS_CFLAGS := $(TPROGS_CFLAGS) -D__must_check=
328527024f7SAndrii Nakryiko
3291d97c6c2SIvan Khoronzhuk-include $(BPF_SAMPLES_PATH)/Makefile.target
3301d97c6c2SIvan Khoronzhuk
331571fa247SKumar Kartikeya DwivediVMLINUX_BTF_PATHS ?= $(abspath $(if $(O),$(O)/vmlinux))				\
332571fa247SKumar Kartikeya Dwivedi		     $(abspath $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux))	\
333571fa247SKumar Kartikeya Dwivedi		     $(abspath ./vmlinux)
334384b6b3bSKumar Kartikeya DwivediVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
335384b6b3bSKumar Kartikeya Dwivedi
336384b6b3bSKumar Kartikeya Dwivedi$(obj)/vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL)
337384b6b3bSKumar Kartikeya Dwivediifeq ($(VMLINUX_H),)
338ec247044SJerome Marchandifeq ($(VMLINUX_BTF),)
339ec247044SJerome Marchand	$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)",\
340ec247044SJerome Marchand		build the kernel or set VMLINUX_BTF like "VMLINUX_BTF=/sys/kernel/btf/vmlinux" or VMLINUX_H variable)
341ec247044SJerome Marchandendif
342384b6b3bSKumar Kartikeya Dwivedi	$(Q)$(BPFTOOL) btf dump file $(VMLINUX_BTF) format c > $@
343384b6b3bSKumar Kartikeya Dwivedielse
344384b6b3bSKumar Kartikeya Dwivedi	$(Q)cp "$(VMLINUX_H)" $@
345384b6b3bSKumar Kartikeya Dwivediendif
346384b6b3bSKumar Kartikeya Dwivedi
347384b6b3bSKumar Kartikeya Dwivediclean-files += vmlinux.h
348384b6b3bSKumar Kartikeya Dwivedi
3493f199560SKumar Kartikeya Dwivedi# Get Clang's default includes on this system, as opposed to those seen by
350bbaf1ff0SFangrui Song# '--target=bpf'. This fixes "missing" files on some architectures/distros,
3513f199560SKumar Kartikeya Dwivedi# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
3523f199560SKumar Kartikeya Dwivedi#
3533f199560SKumar Kartikeya Dwivedi# Use '-idirafter': Don't interfere with include mechanics except where the
3543f199560SKumar Kartikeya Dwivedi# build would have failed anyways.
3553f199560SKumar Kartikeya Dwivedidefine get_sys_includes
3563f199560SKumar Kartikeya Dwivedi$(shell $(1) -v -E - </dev/null 2>&1 \
3573f199560SKumar Kartikeya Dwivedi        | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
3583f199560SKumar Kartikeya Dwivedi$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' | sed 's/#define /-D/' | sed 's/ /=/')
3593f199560SKumar Kartikeya Dwivediendef
3603f199560SKumar Kartikeya Dwivedi
3613f199560SKumar Kartikeya DwivediCLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
3623f199560SKumar Kartikeya Dwivedi
36385bf1f51SLorenzo Bianconi$(obj)/xdp_router_ipv4.bpf.o: $(obj)/xdp_sample.bpf.o
3643f199560SKumar Kartikeya Dwivedi
3653f199560SKumar Kartikeya Dwivedi$(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/xdp_sample_shared.h
3663f199560SKumar Kartikeya Dwivedi	@echo "  CLANG-BPF " $@
367bbaf1ff0SFangrui Song	$(Q)$(CLANG) -g -O2 --target=bpf -D__TARGET_ARCH_$(SRCARCH) \
3683f199560SKumar Kartikeya Dwivedi		-Wno-compare-distinct-pointer-types -I$(srctree)/include \
3693f199560SKumar Kartikeya Dwivedi		-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
3703f7a3318SQuentin Monnet		-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
3713f199560SKumar Kartikeya Dwivedi		-c $(filter %.bpf.c,$^) -o $@
3723f199560SKumar Kartikeya Dwivedi
37391dda69bSToke Høiland-JørgensenLINKED_SKELS := xdp_router_ipv4.skel.h
3743f199560SKumar Kartikeya Dwivediclean-files += $(LINKED_SKELS)
3753f199560SKumar Kartikeya Dwivedi
37685bf1f51SLorenzo Bianconixdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o
3773f199560SKumar Kartikeya Dwivedi
3783f199560SKumar Kartikeya DwivediLINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
3793f199560SKumar Kartikeya Dwivedi
3803f199560SKumar Kartikeya DwivediBPF_SRCS_LINKED := $(notdir $(wildcard $(src)/*.bpf.c))
3813f199560SKumar Kartikeya DwivediBPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(BPF_SRCS_LINKED))
3823f199560SKumar Kartikeya DwivediBPF_SKELS_LINKED := $(addprefix $(obj)/,$(LINKED_SKELS))
3833f199560SKumar Kartikeya Dwivedi
3843f199560SKumar Kartikeya Dwivedi$(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
3853f199560SKumar Kartikeya Dwivedi	@echo "  BPF GEN-OBJ " $(@:.skel.h=)
3863f199560SKumar Kartikeya Dwivedi	$(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps))
3873f199560SKumar Kartikeya Dwivedi	@echo "  BPF GEN-SKEL" $(@:.skel.h=)
3883f199560SKumar Kartikeya Dwivedi	$(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@
3893f199560SKumar Kartikeya Dwivedi
390128d1514SNaveen N. Rao# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
391128d1514SNaveen N. Rao# But, there is no easy way to fix it, so just exclude it since it is
39230b50aa6SYang Shi# useless for BPF samples.
3939618bde4SYonghong Song# below we use long chain of commands, clang | opt | llvm-dis | llc,
3949618bde4SYonghong Song# to generate final object file. 'clang' compiles the source into IR
3959618bde4SYonghong Song# with native target, e.g., x64, arm64, etc. 'opt' does bpf CORE IR builtin
3969618bde4SYonghong Song# processing (llvm12) and IR optimizations. 'llvm-dis' converts
3979618bde4SYonghong Song# 'opt' output to IR, and finally 'llc' generates bpf byte code.
398b88c06e3SBrenden Blanco$(obj)/%.o: $(src)/%.c
399768759edSJakub Kicinski	@echo "  CLANG-bpf " $@
4002a560df7SIvan Khoronzhuk	$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
4012a560df7SIvan Khoronzhuk		-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
4023f7a3318SQuentin Monnet		-I$(LIBBPF_INCLUDE) \
403b1ae32dbSAlexei Starovoitov		-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
40481f522f9SIlya Leoshkevich		-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
40569a9d09bSAlexei Starovoitov		-Wno-gnu-variable-sized-type-not-at-end \
40669a9d09bSAlexei Starovoitov		-Wno-address-of-packed-member -Wno-tautological-compare \
4079db95838SAbhijit Ayarekar		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
40834f6e38fSDaniel T. Lee		-fno-asynchronous-unwind-tables -fcf-protection \
4096bf3bbe1SYonghong Song		-I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
4109618bde4SYonghong Song		-O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
4119618bde4SYonghong Song		$(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
4129618bde4SYonghong Song		$(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@
4139778cfdfSTaeung Songifeq ($(DWARF2BTF),y)
4149778cfdfSTaeung Song	$(BTF_PAHOLE) -J $@
4159778cfdfSTaeung Songendif
416