xref: /linux/samples/bpf/Makefile (revision 2a171788ba7bb61995e98e8163204fc7880f63b2)
1# SPDX-License-Identifier: GPL-2.0
2# kbuild trick to avoid linker error. Can be omitted if a module is built.
3obj- := dummy.o
4
5# List of programs to build
6hostprogs-y := test_lru_dist
7hostprogs-y += sock_example
8hostprogs-y += fds_example
9hostprogs-y += sockex1
10hostprogs-y += sockex2
11hostprogs-y += sockex3
12hostprogs-y += tracex1
13hostprogs-y += tracex2
14hostprogs-y += tracex3
15hostprogs-y += tracex4
16hostprogs-y += tracex5
17hostprogs-y += tracex6
18hostprogs-y += test_probe_write_user
19hostprogs-y += trace_output
20hostprogs-y += lathist
21hostprogs-y += offwaketime
22hostprogs-y += spintest
23hostprogs-y += map_perf_test
24hostprogs-y += test_overhead
25hostprogs-y += test_cgrp2_array_pin
26hostprogs-y += test_cgrp2_attach
27hostprogs-y += test_cgrp2_attach2
28hostprogs-y += test_cgrp2_sock
29hostprogs-y += test_cgrp2_sock2
30hostprogs-y += xdp1
31hostprogs-y += xdp2
32hostprogs-y += test_current_task_under_cgroup
33hostprogs-y += trace_event
34hostprogs-y += sampleip
35hostprogs-y += tc_l2_redirect
36hostprogs-y += lwt_len_hist
37hostprogs-y += xdp_tx_iptunnel
38hostprogs-y += test_map_in_map
39hostprogs-y += per_socket_stats_example
40hostprogs-y += load_sock_ops
41hostprogs-y += xdp_redirect
42hostprogs-y += xdp_redirect_map
43hostprogs-y += xdp_redirect_cpu
44hostprogs-y += xdp_monitor
45hostprogs-y += syscall_tp
46
47# Libbpf dependencies
48LIBBPF := ../../tools/lib/bpf/bpf.o
49
50test_lru_dist-objs := test_lru_dist.o $(LIBBPF)
51sock_example-objs := sock_example.o $(LIBBPF)
52fds_example-objs := bpf_load.o $(LIBBPF) fds_example.o
53sockex1-objs := bpf_load.o $(LIBBPF) sockex1_user.o
54sockex2-objs := bpf_load.o $(LIBBPF) sockex2_user.o
55sockex3-objs := bpf_load.o $(LIBBPF) sockex3_user.o
56tracex1-objs := bpf_load.o $(LIBBPF) tracex1_user.o
57tracex2-objs := bpf_load.o $(LIBBPF) tracex2_user.o
58tracex3-objs := bpf_load.o $(LIBBPF) tracex3_user.o
59tracex4-objs := bpf_load.o $(LIBBPF) tracex4_user.o
60tracex5-objs := bpf_load.o $(LIBBPF) tracex5_user.o
61tracex6-objs := bpf_load.o $(LIBBPF) tracex6_user.o
62load_sock_ops-objs := bpf_load.o $(LIBBPF) load_sock_ops.o
63test_probe_write_user-objs := bpf_load.o $(LIBBPF) test_probe_write_user_user.o
64trace_output-objs := bpf_load.o $(LIBBPF) trace_output_user.o
65lathist-objs := bpf_load.o $(LIBBPF) lathist_user.o
66offwaketime-objs := bpf_load.o $(LIBBPF) offwaketime_user.o
67spintest-objs := bpf_load.o $(LIBBPF) spintest_user.o
68map_perf_test-objs := bpf_load.o $(LIBBPF) map_perf_test_user.o
69test_overhead-objs := bpf_load.o $(LIBBPF) test_overhead_user.o
70test_cgrp2_array_pin-objs := $(LIBBPF) test_cgrp2_array_pin.o
71test_cgrp2_attach-objs := $(LIBBPF) test_cgrp2_attach.o
72test_cgrp2_attach2-objs := $(LIBBPF) test_cgrp2_attach2.o cgroup_helpers.o
73test_cgrp2_sock-objs := $(LIBBPF) test_cgrp2_sock.o
74test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o
75xdp1-objs := bpf_load.o $(LIBBPF) xdp1_user.o
76# reuse xdp1 source intentionally
77xdp2-objs := bpf_load.o $(LIBBPF) xdp1_user.o
78test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) cgroup_helpers.o \
79				       test_current_task_under_cgroup_user.o
80trace_event-objs := bpf_load.o $(LIBBPF) trace_event_user.o
81sampleip-objs := bpf_load.o $(LIBBPF) sampleip_user.o
82tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o
83lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o
84xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o
85test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o
86per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o
87xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o
88xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o
89xdp_redirect_cpu-objs := bpf_load.o $(LIBBPF) xdp_redirect_cpu_user.o
90xdp_monitor-objs := bpf_load.o $(LIBBPF) xdp_monitor_user.o
91syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
92
93# Tell kbuild to always build the programs
94always := $(hostprogs-y)
95always += sockex1_kern.o
96always += sockex2_kern.o
97always += sockex3_kern.o
98always += tracex1_kern.o
99always += tracex2_kern.o
100always += tracex3_kern.o
101always += tracex4_kern.o
102always += tracex5_kern.o
103always += tracex6_kern.o
104always += sock_flags_kern.o
105always += test_probe_write_user_kern.o
106always += trace_output_kern.o
107always += tcbpf1_kern.o
108always += tcbpf2_kern.o
109always += tc_l2_redirect_kern.o
110always += lathist_kern.o
111always += offwaketime_kern.o
112always += spintest_kern.o
113always += map_perf_test_kern.o
114always += test_overhead_tp_kern.o
115always += test_overhead_kprobe_kern.o
116always += parse_varlen.o parse_simple.o parse_ldabs.o
117always += test_cgrp2_tc_kern.o
118always += xdp1_kern.o
119always += xdp2_kern.o
120always += test_current_task_under_cgroup_kern.o
121always += trace_event_kern.o
122always += sampleip_kern.o
123always += lwt_len_hist_kern.o
124always += xdp_tx_iptunnel_kern.o
125always += test_map_in_map_kern.o
126always += cookie_uid_helper_example.o
127always += tcp_synrto_kern.o
128always += tcp_rwnd_kern.o
129always += tcp_bufs_kern.o
130always += tcp_cong_kern.o
131always += tcp_iw_kern.o
132always += tcp_clamp_kern.o
133always += tcp_basertt_kern.o
134always += xdp_redirect_kern.o
135always += xdp_redirect_map_kern.o
136always += xdp_redirect_cpu_kern.o
137always += xdp_monitor_kern.o
138always += syscall_tp_kern.o
139
140HOSTCFLAGS += -I$(objtree)/usr/include
141HOSTCFLAGS += -I$(srctree)/tools/lib/
142HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
143HOSTCFLAGS += -I$(srctree)/tools/lib/ -I$(srctree)/tools/include
144HOSTCFLAGS += -I$(srctree)/tools/perf
145
146HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable
147HOSTLOADLIBES_fds_example += -lelf
148HOSTLOADLIBES_sockex1 += -lelf
149HOSTLOADLIBES_sockex2 += -lelf
150HOSTLOADLIBES_sockex3 += -lelf
151HOSTLOADLIBES_tracex1 += -lelf
152HOSTLOADLIBES_tracex2 += -lelf
153HOSTLOADLIBES_tracex3 += -lelf
154HOSTLOADLIBES_tracex4 += -lelf -lrt
155HOSTLOADLIBES_tracex5 += -lelf
156HOSTLOADLIBES_tracex6 += -lelf
157HOSTLOADLIBES_test_cgrp2_sock2 += -lelf
158HOSTLOADLIBES_load_sock_ops += -lelf
159HOSTLOADLIBES_test_probe_write_user += -lelf
160HOSTLOADLIBES_trace_output += -lelf -lrt
161HOSTLOADLIBES_lathist += -lelf
162HOSTLOADLIBES_offwaketime += -lelf
163HOSTLOADLIBES_spintest += -lelf
164HOSTLOADLIBES_map_perf_test += -lelf -lrt
165HOSTLOADLIBES_test_overhead += -lelf -lrt
166HOSTLOADLIBES_xdp1 += -lelf
167HOSTLOADLIBES_xdp2 += -lelf
168HOSTLOADLIBES_test_current_task_under_cgroup += -lelf
169HOSTLOADLIBES_trace_event += -lelf
170HOSTLOADLIBES_sampleip += -lelf
171HOSTLOADLIBES_tc_l2_redirect += -l elf
172HOSTLOADLIBES_lwt_len_hist += -l elf
173HOSTLOADLIBES_xdp_tx_iptunnel += -lelf
174HOSTLOADLIBES_test_map_in_map += -lelf
175HOSTLOADLIBES_xdp_redirect += -lelf
176HOSTLOADLIBES_xdp_redirect_map += -lelf
177HOSTLOADLIBES_xdp_redirect_cpu += -lelf
178HOSTLOADLIBES_xdp_monitor += -lelf
179HOSTLOADLIBES_syscall_tp += -lelf
180
181# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
182#  make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
183LLC ?= llc
184CLANG ?= clang
185
186# Detect that we're cross compiling and use the cross compiler
187ifdef CROSS_COMPILE
188HOSTCC = $(CROSS_COMPILE)gcc
189CLANG_ARCH_ARGS = -target $(ARCH)
190endif
191
192# Trick to allow make to be run from this directory
193all:
194	$(MAKE) -C ../../ $(CURDIR)/
195
196clean:
197	$(MAKE) -C ../../ M=$(CURDIR) clean
198	@rm -f *~
199
200$(obj)/syscall_nrs.s:	$(src)/syscall_nrs.c
201	$(call if_changed_dep,cc_s_c)
202
203$(obj)/syscall_nrs.h:	$(obj)/syscall_nrs.s FORCE
204	$(call filechk,offsets,__SYSCALL_NRS_H__)
205
206clean-files += syscall_nrs.h
207
208FORCE:
209
210
211# Verify LLVM compiler tools are available and bpf target is supported by llc
212.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC)
213
214verify_cmds: $(CLANG) $(LLC)
215	@for TOOL in $^ ; do \
216		if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \
217			echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\
218			exit 1; \
219		else true; fi; \
220	done
221
222verify_target_bpf: verify_cmds
223	@if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \
224		echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\
225		echo "   NOTICE: LLVM version >= 3.7.1 required" ;\
226		exit 2; \
227	else true; fi
228
229$(src)/*.c: verify_target_bpf
230
231$(obj)/tracex5_kern.o: $(obj)/syscall_nrs.h
232
233# asm/sysreg.h - inline assembly used by it is incompatible with llvm.
234# But, there is no easy way to fix it, so just exclude it since it is
235# useless for BPF samples.
236$(obj)/%.o: $(src)/%.c
237	$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \
238		-I$(srctree)/tools/testing/selftests/bpf/ \
239		-D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \
240		-D__TARGET_ARCH_$(ARCH) -Wno-compare-distinct-pointer-types \
241		-Wno-gnu-variable-sized-type-not-at-end \
242		-Wno-address-of-packed-member -Wno-tautological-compare \
243		-Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
244		-O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf -filetype=obj -o $@
245