Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2include ../../scripts/Makefile.include
3
4ifeq ($(srctree),)
5srctree := $(patsubst %/,%,$(dir $(CURDIR)))
6srctree := $(patsubst %/,%,$(dir $(srctree)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8endif
9
10BPF_DIR = $(srctree)/tools/lib/bpf
11
12ifneq ($(OUTPUT),)
13 _OUTPUT := $(OUTPUT)
14else
15 _OUTPUT := $(CURDIR)/
16endif
17BOOTSTRAP_OUTPUT := $(_OUTPUT)bootstrap/
18
19LIBBPF_OUTPUT := $(_OUTPUT)libbpf/
20LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
21LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
22LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf
23LIBBPF := $(LIBBPF_OUTPUT)libbpf.a
24
25LIBBPF_BOOTSTRAP_OUTPUT := $(BOOTSTRAP_OUTPUT)libbpf/
26LIBBPF_BOOTSTRAP_DESTDIR := $(LIBBPF_BOOTSTRAP_OUTPUT)
27LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include
28LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf
29LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
30
31# We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h
32# which are not otherwise exported by libbpf, but still required by bpftool.
33LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h)
34LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h)
35
36$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR):
37 $(QUIET_MKDIR)mkdir -p $@
38
39$(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT)
40 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \
41 DESTDIR=$(LIBBPF_DESTDIR:/=) prefix= $(LIBBPF) install_headers
42
43$(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR)
44 $(call QUIET_INSTALL, $@)
45 $(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $<
46
47$(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
48 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
49 DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \
50 ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" $@ install_headers
51
52$(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
53 $(call QUIET_INSTALL, $@)
54 $(Q)install -m 644 -t $(LIBBPF_BOOTSTRAP_HDRS_DIR) $<
55
56$(LIBBPF)-clean: FORCE | $(LIBBPF_OUTPUT)
57 $(call QUIET_CLEAN, libbpf)
58 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
59
60$(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
61 $(call QUIET_CLEAN, libbpf-bootstrap)
62 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null
63
64prefix ?= /usr/local
65bash_compdir ?= /usr/share/bash-completion/completions
66
67CFLAGS += -O2
68CFLAGS += -W
69CFLAGS += -Wall
70CFLAGS += -Wextra
71CFLAGS += -Wformat-signedness
72CFLAGS += -Wno-unused-parameter
73CFLAGS += -Wno-missing-field-initializers
74CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
75CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
76 -I$(or $(OUTPUT),.) \
77 -I$(LIBBPF_INCLUDE) \
78 -I$(srctree)/kernel/bpf/ \
79 -I$(srctree)/tools/include \
80 -I$(srctree)/tools/include/uapi
81ifneq ($(BPFTOOL_VERSION),)
82CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
83endif
84ifneq ($(EXTRA_CFLAGS),)
85CFLAGS += $(EXTRA_CFLAGS)
86endif
87ifneq ($(EXTRA_LDFLAGS),)
88LDFLAGS += $(EXTRA_LDFLAGS)
89endif
90
91HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
92 $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
93HOST_LDFLAGS := $(LDFLAGS)
94
95INSTALL ?= install
96RM ?= rm -f
97
98FEATURE_USER = .bpftool
99
100FEATURE_TESTS := clang-bpf-co-re
101FEATURE_TESTS += llvm
102FEATURE_TESTS += libcap
103FEATURE_TESTS += libbfd
104FEATURE_TESTS += libbfd-liberty
105FEATURE_TESTS += libbfd-liberty-z
106FEATURE_TESTS += disassembler-four-args
107FEATURE_TESTS += disassembler-init-styled
108FEATURE_TESTS += libelf-zstd
109
110FEATURE_DISPLAY := clang-bpf-co-re
111FEATURE_DISPLAY += llvm
112FEATURE_DISPLAY += libcap
113FEATURE_DISPLAY += libbfd
114FEATURE_DISPLAY += libbfd-liberty
115FEATURE_DISPLAY += libbfd-liberty-z
116
117check_feat := 1
118NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
119ifdef MAKECMDGOALS
120ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
121 check_feat := 0
122endif
123endif
124
125ifeq ($(check_feat),1)
126ifeq ($(FEATURES_DUMP),)
127include $(srctree)/tools/build/Makefile.feature
128else
129include $(FEATURES_DUMP)
130endif
131endif
132
133LIBS = $(LIBBPF) -lelf -lz
134LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
135
136ifeq ($(feature-libelf-zstd),1)
137LIBS += -lzstd
138LIBS_BOOTSTRAP += -lzstd
139endif
140
141ifeq ($(feature-libcap), 1)
142CFLAGS += -DUSE_LIBCAP
143LIBS += -lcap
144endif
145
146include $(wildcard $(OUTPUT)*.d)
147
148all: $(OUTPUT)bpftool
149
150SRCS := $(wildcard *.c)
151
152ifeq ($(feature-llvm),1)
153 # If LLVM is available, use it for JIT disassembly
154 CFLAGS += -DHAVE_LLVM_SUPPORT
155 LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
156 # llvm-config always adds -D_GNU_SOURCE, however, it may already be in CFLAGS
157 # (e.g. when bpftool build is called from selftests build as selftests
158 # Makefile includes lib.mk which sets -D_GNU_SOURCE) which would cause
159 # compilation error due to redefinition. Let's filter it out here.
160 CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
161 LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS))
162 ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static)
163 LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))
164 LIBS += -lstdc++
165 endif
166 LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
167else
168 # Fall back on libbfd
169 ifeq ($(feature-libbfd),1)
170 LIBS += -lbfd -ldl -lopcodes
171 else ifeq ($(feature-libbfd-liberty),1)
172 LIBS += -lbfd -ldl -lopcodes -liberty
173 else ifeq ($(feature-libbfd-liberty-z),1)
174 LIBS += -lbfd -ldl -lopcodes -liberty -lz
175 endif
176
177 # If one of the above feature combinations is set, we support libbfd
178 ifneq ($(filter -lbfd,$(LIBS)),)
179 CFLAGS += -DHAVE_LIBBFD_SUPPORT
180
181 # Libbfd interface changed over time, figure out what we need
182 ifeq ($(feature-disassembler-four-args), 1)
183 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
184 endif
185 ifeq ($(feature-disassembler-init-styled), 1)
186 CFLAGS += -DDISASM_INIT_STYLED
187 endif
188 endif
189endif
190ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
191 # No support for JIT disassembly
192 SRCS := $(filter-out jit_disasm.c,$(SRCS))
193endif
194
195BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
196
197BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o)
198$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
199
200OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
201$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
202
203VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
204 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
205 ../../../vmlinux \
206 /sys/kernel/btf/vmlinux \
207 /boot/vmlinux-$(shell uname -r)
208VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
209
210bootstrap: $(BPFTOOL_BOOTSTRAP)
211
212ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
213ifeq ($(feature-clang-bpf-co-re),1)
214
215BUILD_BPF_SKELS := 1
216
217ifeq ($(VMLINUX_H),)
218$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
219 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
220else
221$(OUTPUT)vmlinux.h: $(VMLINUX_H)
222 $(Q)cp "$(VMLINUX_H)" $@
223endif
224
225$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
226 $(QUIET_CLANG)$(CLANG) \
227 -I$(or $(OUTPUT),.) \
228 -I$(srctree)/tools/include/uapi/ \
229 -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
230 -g -O2 -Wall -fno-stack-protector \
231 --target=bpf -c $< -o $@
232 $(Q)$(LLVM_STRIP) -g $@
233
234$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
235 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
236
237$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
238
239$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
240
241endif
242endif
243
244CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
245
246$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
247 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
248
249$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
250 $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
251
252$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
253 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
254
255$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
256 $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
257
258$(OUTPUT)%.o: %.c
259 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
260
261feature-detect-clean:
262 $(call QUIET_CLEAN, feature-detect)
263 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
264
265clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
266 $(call QUIET_CLEAN, bpftool)
267 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
268 $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
269 $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
270 $(call QUIET_CLEAN, core-gen)
271 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
272 $(Q)$(RM) -r -- $(OUTPUT)feature/
273
274install-bin: $(OUTPUT)bpftool
275 $(call QUIET_INSTALL, bpftool)
276 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
277 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
278
279install: install-bin
280 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
281 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
282
283uninstall:
284 $(call QUIET_UNINST, bpftool)
285 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
286 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
287
288doc:
289 $(call descend,Documentation)
290
291doc-clean:
292 $(call descend,Documentation,clean)
293
294doc-install:
295 $(call descend,Documentation,install)
296
297doc-uninstall:
298 $(call descend,Documentation,uninstall)
299
300FORCE:
301
302.SECONDARY:
303.PHONY: all FORCE bootstrap clean install-bin install uninstall
304.PHONY: doc doc-clean doc-install doc-uninstall
305.DEFAULT_GOAL := all
306
307# Delete partially updated (corrupted) files on error
308.DELETE_ON_ERROR: