1 # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 include ../../scripts/Makefile.include
5 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
6 srctree := $(patsubst %/,%,$(dir $(srctree)))
7 srctree := $(patsubst %/,%,$(dir $(srctree)))
16 BPF_DIR = $(srctree)/tools/lib/bpf
23 BOOTSTRAP_OUTPUT := $(_OUTPUT)bootstrap/
25 LIBBPF_OUTPUT := $(_OUTPUT)libbpf/
26 LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
27 LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
28 LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf
29 LIBBPF := $(LIBBPF_OUTPUT)libbpf.a
31 LIBBPF_BOOTSTRAP_OUTPUT := $(BOOTSTRAP_OUTPUT)libbpf/
32 LIBBPF_BOOTSTRAP_DESTDIR := $(LIBBPF_BOOTSTRAP_OUTPUT)
33 LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include
34 LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf
35 LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
37 # We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h
38 # which are not otherwise exported by libbpf, but still required by bpftool.
39 LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h)
40 LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h)
42 $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR):
43 $(QUIET_MKDIR)mkdir -p $@
45 $(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT)
46 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \
47 DESTDIR=$(LIBBPF_DESTDIR:/=) prefix= $(LIBBPF) install_headers
49 $(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR)
50 $(call QUIET_INSTALL, $@)
51 $(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $<
53 $(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
54 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
55 DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \
56 ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) $@ install_headers
58 $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
59 $(call QUIET_INSTALL, $@)
60 $(Q)install -m 644 -t $(LIBBPF_BOOTSTRAP_HDRS_DIR) $<
62 $(LIBBPF)-clean: FORCE | $(LIBBPF_OUTPUT)
63 $(call QUIET_CLEAN, libbpf)
64 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
66 $(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
67 $(call QUIET_CLEAN, libbpf-bootstrap)
68 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null
71 bash_compdir ?= /usr/share/bash-completion/completions
74 CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
75 CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
76 CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
79 -I$(srctree)/kernel/bpf/ \
80 -I$(srctree)/tools/include \
81 -I$(srctree)/tools/include/uapi
82 ifneq ($(BPFTOOL_VERSION),)
83 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
85 ifneq ($(EXTRA_CFLAGS),)
86 CFLAGS += $(EXTRA_CFLAGS)
88 ifneq ($(EXTRA_LDFLAGS),)
89 LDFLAGS += $(EXTRA_LDFLAGS)
95 FEATURE_USER = .bpftool
97 FEATURE_TESTS := clang-bpf-co-re
99 FEATURE_TESTS += libcap
100 FEATURE_TESTS += libbfd
101 FEATURE_TESTS += libbfd-liberty
102 FEATURE_TESTS += libbfd-liberty-z
103 FEATURE_TESTS += disassembler-four-args
104 FEATURE_TESTS += disassembler-init-styled
106 FEATURE_DISPLAY := clang-bpf-co-re
107 FEATURE_DISPLAY += llvm
108 FEATURE_DISPLAY += libcap
109 FEATURE_DISPLAY += libbfd
110 FEATURE_DISPLAY += libbfd-liberty
111 FEATURE_DISPLAY += libbfd-liberty-z
114 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
116 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
121 ifeq ($(check_feat),1)
122 ifeq ($(FEATURES_DUMP),)
123 include $(srctree)/tools/build/Makefile.feature
125 include $(FEATURES_DUMP)
129 LIBS = $(LIBBPF) -lelf -lz
130 LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
131 ifeq ($(feature-libcap), 1)
132 CFLAGS += -DUSE_LIBCAP
136 include $(wildcard $(OUTPUT)*.d)
138 all: $(OUTPUT)bpftool
140 SRCS := $(wildcard *.c)
142 ifeq ($(feature-llvm),1)
143 # If LLVM is available, use it for JIT disassembly
144 CFLAGS += -DHAVE_LLVM_SUPPORT
145 LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
146 CFLAGS += $(shell $(LLVM_CONFIG) --cflags --libs $(LLVM_CONFIG_LIB_COMPONENTS))
147 LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS))
148 ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static)
149 LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))
152 LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
154 # Fall back on libbfd
155 ifeq ($(feature-libbfd),1)
156 LIBS += -lbfd -ldl -lopcodes
157 else ifeq ($(feature-libbfd-liberty),1)
158 LIBS += -lbfd -ldl -lopcodes -liberty
159 else ifeq ($(feature-libbfd-liberty-z),1)
160 LIBS += -lbfd -ldl -lopcodes -liberty -lz
163 # If one of the above feature combinations is set, we support libbfd
164 ifneq ($(filter -lbfd,$(LIBS)),)
165 CFLAGS += -DHAVE_LIBBFD_SUPPORT
167 # Libbfd interface changed over time, figure out what we need
168 ifeq ($(feature-disassembler-four-args), 1)
169 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
171 ifeq ($(feature-disassembler-init-styled), 1)
172 CFLAGS += -DDISASM_INIT_STYLED
176 ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
177 # No support for JIT disassembly
178 SRCS := $(filter-out jit_disasm.c,$(SRCS))
181 HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
182 $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
184 BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
186 BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
187 $(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
189 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
190 $(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
192 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
193 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
195 /sys/kernel/btf/vmlinux \
196 /boot/vmlinux-$(shell uname -r)
197 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
199 bootstrap: $(BPFTOOL_BOOTSTRAP)
201 ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
202 ifeq ($(feature-clang-bpf-co-re),1)
206 $(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
208 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
210 $(Q)cp "$(VMLINUX_H)" $@
213 $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
214 $(QUIET_CLANG)$(CLANG) \
215 -I$(or $(OUTPUT),.) \
216 -I$(srctree)/tools/include/uapi/ \
217 -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
218 -g -O2 -Wall -target bpf -c $< -o $@
219 $(Q)$(LLVM_STRIP) -g $@
221 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
222 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
224 $(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
226 $(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
231 CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
233 $(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
234 $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
236 $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
237 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
239 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
240 $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
242 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
243 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
245 $(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
246 $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
249 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
251 feature-detect-clean:
252 $(call QUIET_CLEAN, feature-detect)
253 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
255 clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
256 $(call QUIET_CLEAN, bpftool)
257 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
258 $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
259 $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
260 $(call QUIET_CLEAN, core-gen)
261 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
262 $(Q)$(RM) -r -- $(OUTPUT)feature/
264 install-bin: $(OUTPUT)bpftool
265 $(call QUIET_INSTALL, bpftool)
266 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
267 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
270 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
271 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
274 $(call QUIET_UNINST, bpftool)
275 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
276 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
279 $(call descend,Documentation)
282 $(call descend,Documentation,clean)
285 $(call descend,Documentation,install)
288 $(call descend,Documentation,uninstall)
293 .PHONY: all FORCE bootstrap clean install-bin install uninstall
294 .PHONY: doc doc-clean doc-install doc-uninstall
297 # Delete partially updated (corrupted) files on error