xref: /linux/tools/bpf/bpftool/Makefile (revision 2ea4d86a5093defcb2fc49799184ede178e64d36)
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 += libcap
99FEATURE_TESTS += libbfd
100FEATURE_TESTS += libbfd-liberty
101FEATURE_TESTS += libbfd-liberty-z
102FEATURE_TESTS += disassembler-four-args
103FEATURE_TESTS += disassembler-init-styled
104
105FEATURE_DISPLAY := clang-bpf-co-re
106FEATURE_DISPLAY += libcap
107FEATURE_DISPLAY += libbfd
108FEATURE_DISPLAY += libbfd-liberty
109FEATURE_DISPLAY += libbfd-liberty-z
110
111check_feat := 1
112NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
113ifdef MAKECMDGOALS
114ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
115  check_feat := 0
116endif
117endif
118
119ifeq ($(check_feat),1)
120ifeq ($(FEATURES_DUMP),)
121include $(srctree)/tools/build/Makefile.feature
122else
123include $(FEATURES_DUMP)
124endif
125endif
126
127LIBS = $(LIBBPF) -lelf -lz
128LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
129ifeq ($(feature-libcap), 1)
130CFLAGS += -DUSE_LIBCAP
131LIBS += -lcap
132endif
133
134include $(wildcard $(OUTPUT)*.d)
135
136all: $(OUTPUT)bpftool
137
138SRCS := $(wildcard *.c)
139
140ifeq ($(feature-libbfd),1)
141  LIBS += -lbfd -ldl -lopcodes
142else ifeq ($(feature-libbfd-liberty),1)
143  LIBS += -lbfd -ldl -lopcodes -liberty
144else ifeq ($(feature-libbfd-liberty-z),1)
145  LIBS += -lbfd -ldl -lopcodes -liberty -lz
146endif
147
148# If one of the above feature combinations is set, we support libbfd
149ifneq ($(filter -lbfd,$(LIBS)),)
150  CFLAGS += -DHAVE_LIBBFD_SUPPORT
151
152  # Libbfd interface changed over time, figure out what we need
153  ifeq ($(feature-disassembler-four-args), 1)
154    CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
155  endif
156  ifeq ($(feature-disassembler-init-styled), 1)
157    CFLAGS += -DDISASM_INIT_STYLED
158  endif
159endif
160ifeq ($(filter -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
161  # No support for JIT disassembly
162  SRCS := $(filter-out jit_disasm.c,$(SRCS))
163endif
164
165HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
166		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
167
168BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
169
170BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
171$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
172
173OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
174$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
175
176VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
177		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
178		     ../../../vmlinux					\
179		     /sys/kernel/btf/vmlinux				\
180		     /boot/vmlinux-$(shell uname -r)
181VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
182
183bootstrap: $(BPFTOOL_BOOTSTRAP)
184
185ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
186ifeq ($(feature-clang-bpf-co-re),1)
187
188BUILD_BPF_SKELS := 1
189
190$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
191ifeq ($(VMLINUX_H),)
192	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
193else
194	$(Q)cp "$(VMLINUX_H)" $@
195endif
196
197$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
198	$(QUIET_CLANG)$(CLANG) \
199		-I$(or $(OUTPUT),.) \
200		-I$(srctree)/tools/include/uapi/ \
201		-I$(LIBBPF_BOOTSTRAP_INCLUDE) \
202		-g -O2 -Wall -target bpf -c $< -o $@
203	$(Q)$(LLVM_STRIP) -g $@
204
205$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
206	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
207
208$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
209
210$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
211
212endif
213endif
214
215CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
216
217$(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
218	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
219
220$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
221	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
222
223$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
224	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
225
226$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
227	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
228
229$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
230	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
231
232$(OUTPUT)%.o: %.c
233	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
234
235feature-detect-clean:
236	$(call QUIET_CLEAN, feature-detect)
237	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
238
239clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
240	$(call QUIET_CLEAN, bpftool)
241	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
242	$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
243	$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
244	$(call QUIET_CLEAN, core-gen)
245	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
246	$(Q)$(RM) -r -- $(OUTPUT)feature/
247
248install-bin: $(OUTPUT)bpftool
249	$(call QUIET_INSTALL, bpftool)
250	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
251	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
252
253install: install-bin
254	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
255	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
256
257uninstall:
258	$(call QUIET_UNINST, bpftool)
259	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
260	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
261
262doc:
263	$(call descend,Documentation)
264
265doc-clean:
266	$(call descend,Documentation,clean)
267
268doc-install:
269	$(call descend,Documentation,install)
270
271doc-uninstall:
272	$(call descend,Documentation,uninstall)
273
274FORCE:
275
276.SECONDARY:
277.PHONY: all FORCE bootstrap clean install-bin install uninstall
278.PHONY: doc doc-clean doc-install doc-uninstall
279.DEFAULT_GOAL := all
280