xref: /linux/tools/bpf/bpftool/Makefile (revision 561add0da6d3d07c9bccb0832fb6ed5619167d26)
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
10ifeq ($(V),1)
11  Q =
12else
13  Q = @
14endif
15
16BPF_DIR = $(srctree)/tools/lib/bpf
17
18ifneq ($(OUTPUT),)
19  _OUTPUT := $(OUTPUT)
20else
21  _OUTPUT := $(CURDIR)/
22endif
23BOOTSTRAP_OUTPUT := $(_OUTPUT)bootstrap/
24
25LIBBPF_OUTPUT := $(_OUTPUT)libbpf/
26LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
27LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
28LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf
29LIBBPF := $(LIBBPF_OUTPUT)libbpf.a
30
31LIBBPF_BOOTSTRAP_OUTPUT := $(BOOTSTRAP_OUTPUT)libbpf/
32LIBBPF_BOOTSTRAP_DESTDIR := $(LIBBPF_BOOTSTRAP_OUTPUT)
33LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include
34LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf
35LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
36
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.
39LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h)
40LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h)
41
42$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR):
43	$(QUIET_MKDIR)mkdir -p $@
44
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
48
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) $<
52
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
57
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) $<
61
62$(LIBBPF)-clean: FORCE | $(LIBBPF_OUTPUT)
63	$(call QUIET_CLEAN, libbpf)
64	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
65
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
69
70prefix ?= /usr/local
71bash_compdir ?= /usr/share/bash-completion/completions
72
73CFLAGS += -O2
74CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
75CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
76CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
77	-I$(or $(OUTPUT),.) \
78	-I$(LIBBPF_INCLUDE) \
79	-I$(srctree)/kernel/bpf/ \
80	-I$(srctree)/tools/include \
81	-I$(srctree)/tools/include/uapi
82ifneq ($(BPFTOOL_VERSION),)
83CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
84endif
85ifneq ($(EXTRA_CFLAGS),)
86CFLAGS += $(EXTRA_CFLAGS)
87endif
88ifneq ($(EXTRA_LDFLAGS),)
89LDFLAGS += $(EXTRA_LDFLAGS)
90endif
91
92INSTALL ?= install
93RM ?= rm -f
94
95FEATURE_USER = .bpftool
96
97FEATURE_TESTS := clang-bpf-co-re
98FEATURE_TESTS += llvm
99FEATURE_TESTS += libcap
100FEATURE_TESTS += libbfd
101FEATURE_TESTS += libbfd-liberty
102FEATURE_TESTS += libbfd-liberty-z
103FEATURE_TESTS += disassembler-four-args
104FEATURE_TESTS += disassembler-init-styled
105
106FEATURE_DISPLAY := clang-bpf-co-re
107FEATURE_DISPLAY += llvm
108FEATURE_DISPLAY += libcap
109FEATURE_DISPLAY += libbfd
110FEATURE_DISPLAY += libbfd-liberty
111FEATURE_DISPLAY += libbfd-liberty-z
112
113check_feat := 1
114NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
115ifdef MAKECMDGOALS
116ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
117  check_feat := 0
118endif
119endif
120
121ifeq ($(check_feat),1)
122ifeq ($(FEATURES_DUMP),)
123include $(srctree)/tools/build/Makefile.feature
124else
125include $(FEATURES_DUMP)
126endif
127endif
128
129LIBS = $(LIBBPF) -lelf -lz
130LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
131ifeq ($(feature-libcap), 1)
132CFLAGS += -DUSE_LIBCAP
133LIBS += -lcap
134endif
135
136include $(wildcard $(OUTPUT)*.d)
137
138all: $(OUTPUT)bpftool
139
140SRCS := $(wildcard *.c)
141
142ifeq ($(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))
150    LIBS += -lstdc++
151  endif
152  LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
153else
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
161  endif
162
163  # If one of the above feature combinations is set, we support libbfd
164  ifneq ($(filter -lbfd,$(LIBS)),)
165    CFLAGS += -DHAVE_LIBBFD_SUPPORT
166
167    # Libbfd interface changed over time, figure out what we need
168    ifeq ($(feature-disassembler-four-args), 1)
169      CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
170    endif
171    ifeq ($(feature-disassembler-init-styled), 1)
172      CFLAGS += -DDISASM_INIT_STYLED
173    endif
174  endif
175endif
176ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
177  # No support for JIT disassembly
178  SRCS := $(filter-out jit_disasm.c,$(SRCS))
179endif
180
181HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
182		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
183
184BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
185
186BOOTSTRAP_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)
188
189OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
190$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
191
192VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
193		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
194		     ../../../vmlinux					\
195		     /sys/kernel/btf/vmlinux				\
196		     /boot/vmlinux-$(shell uname -r)
197VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
198
199bootstrap: $(BPFTOOL_BOOTSTRAP)
200
201ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
202ifeq ($(feature-clang-bpf-co-re),1)
203
204BUILD_BPF_SKELS := 1
205
206$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
207ifeq ($(VMLINUX_H),)
208	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
209else
210	$(Q)cp "$(VMLINUX_H)" $@
211endif
212
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 -fno-stack-protector \
219		--target=bpf -c $< -o $@
220	$(Q)$(LLVM_STRIP) -g $@
221
222$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
223	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
224
225$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
226
227$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
228
229endif
230endif
231
232CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
233
234$(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
235	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
236
237$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
238	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
239
240$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
241	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
242
243$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
244	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
245
246$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
247	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
248
249$(OUTPUT)%.o: %.c
250	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
251
252feature-detect-clean:
253	$(call QUIET_CLEAN, feature-detect)
254	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
255
256clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
257	$(call QUIET_CLEAN, bpftool)
258	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
259	$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
260	$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
261	$(call QUIET_CLEAN, core-gen)
262	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
263	$(Q)$(RM) -r -- $(OUTPUT)feature/
264
265install-bin: $(OUTPUT)bpftool
266	$(call QUIET_INSTALL, bpftool)
267	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
268	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
269
270install: install-bin
271	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
272	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
273
274uninstall:
275	$(call QUIET_UNINST, bpftool)
276	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
277	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
278
279doc:
280	$(call descend,Documentation)
281
282doc-clean:
283	$(call descend,Documentation,clean)
284
285doc-install:
286	$(call descend,Documentation,install)
287
288doc-uninstall:
289	$(call descend,Documentation,uninstall)
290
291FORCE:
292
293.SECONDARY:
294.PHONY: all FORCE bootstrap clean install-bin install uninstall
295.PHONY: doc doc-clean doc-install doc-uninstall
296.DEFAULT_GOAL := all
297
298# Delete partially updated (corrupted) files on error
299.DELETE_ON_ERROR:
300