xref: /linux/tools/bpf/bpftool/Makefile (revision 48f5aef4c458c19ab337eed8c95a6486cc014aa3)
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/
24LIBBPF_OUTPUT := $(_OUTPUT)/libbpf/
25LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
26LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)/include
27LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf
28
29LIBBPF = $(LIBBPF_OUTPUT)libbpf.a
30LIBBPF_BOOTSTRAP_OUTPUT = $(BOOTSTRAP_OUTPUT)libbpf/
31LIBBPF_BOOTSTRAP = $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
32
33# We need to copy hashmap.h and nlattr.h which is not otherwise exported by
34# libbpf, but still required by bpftool.
35LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h)
36
37ifeq ($(BPFTOOL_VERSION),)
38BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
39endif
40
41$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR):
42	$(QUIET_MKDIR)mkdir -p $@
43
44$(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT)
45	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \
46		DESTDIR=$(LIBBPF_DESTDIR) prefix= $(LIBBPF) install_headers
47
48$(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR)
49	$(call QUIET_INSTALL, $@)
50	$(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $<
51
52$(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
53	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
54		ARCH= CC=$(HOSTCC) LD=$(HOSTLD) $@
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 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
69CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
70CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
71	-I$(if $(OUTPUT),$(OUTPUT),.) \
72	-I$(LIBBPF_INCLUDE) \
73	-I$(srctree)/kernel/bpf/ \
74	-I$(srctree)/tools/include \
75	-I$(srctree)/tools/include/uapi
76CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
77ifneq ($(EXTRA_CFLAGS),)
78CFLAGS += $(EXTRA_CFLAGS)
79endif
80ifneq ($(EXTRA_LDFLAGS),)
81LDFLAGS += $(EXTRA_LDFLAGS)
82endif
83
84INSTALL ?= install
85RM ?= rm -f
86
87FEATURE_USER = .bpftool
88FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
89	clang-bpf-co-re
90FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
91	clang-bpf-co-re
92
93check_feat := 1
94NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
95ifdef MAKECMDGOALS
96ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
97  check_feat := 0
98endif
99endif
100
101ifeq ($(check_feat),1)
102ifeq ($(FEATURES_DUMP),)
103include $(srctree)/tools/build/Makefile.feature
104else
105include $(FEATURES_DUMP)
106endif
107endif
108
109ifeq ($(feature-disassembler-four-args), 1)
110CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
111endif
112
113ifeq ($(feature-reallocarray), 0)
114CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
115endif
116
117LIBS = $(LIBBPF) -lelf -lz
118LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
119ifeq ($(feature-libcap), 1)
120CFLAGS += -DUSE_LIBCAP
121LIBS += -lcap
122endif
123
124include $(wildcard $(OUTPUT)*.d)
125
126all: $(OUTPUT)bpftool
127
128BFD_SRCS = jit_disasm.c
129
130SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
131
132ifeq ($(feature-libbfd),1)
133  LIBS += -lbfd -ldl -lopcodes
134else ifeq ($(feature-libbfd-liberty),1)
135  LIBS += -lbfd -ldl -lopcodes -liberty
136else ifeq ($(feature-libbfd-liberty-z),1)
137  LIBS += -lbfd -ldl -lopcodes -liberty -lz
138endif
139
140ifneq ($(filter -lbfd,$(LIBS)),)
141CFLAGS += -DHAVE_LIBBFD_SUPPORT
142SRCS += $(BFD_SRCS)
143endif
144
145BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
146
147BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
148$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
149
150OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
151$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
152
153VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
154		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
155		     ../../../vmlinux					\
156		     /sys/kernel/btf/vmlinux				\
157		     /boot/vmlinux-$(shell uname -r)
158VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
159
160bootstrap: $(BPFTOOL_BOOTSTRAP)
161
162ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
163ifeq ($(feature-clang-bpf-co-re),1)
164
165BUILD_BPF_SKELS := 1
166
167$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
168ifeq ($(VMLINUX_H),)
169	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
170else
171	$(Q)cp "$(VMLINUX_H)" $@
172endif
173
174$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
175	$(QUIET_CLANG)$(CLANG) \
176		-I$(if $(OUTPUT),$(OUTPUT),.) \
177		-I$(srctree)/tools/include/uapi/ \
178		-I$(LIBBPF_INCLUDE) \
179		-g -O2 -Wall -target bpf -c $< -o $@
180	$(Q)$(LLVM_STRIP) -g $@
181
182$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
183	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
184
185$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
186
187$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
188
189endif
190endif
191
192CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
193
194$(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
195	$(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD $< -o $@
196
197$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
198	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
199
200$(OUTPUT)feature.o:
201ifneq ($(feature-zlib), 1)
202	$(error "No zlib found")
203endif
204
205$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
206	$(QUIET_LINK)$(HOSTCC) $(CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
207
208$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
209	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
210
211$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
212	$(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD $< -o $@
213
214$(OUTPUT)%.o: %.c
215	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
216
217feature-detect-clean:
218	$(call QUIET_CLEAN, feature-detect)
219	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
220
221clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
222	$(call QUIET_CLEAN, bpftool)
223	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
224	$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
225	$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
226	$(call QUIET_CLEAN, core-gen)
227	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
228	$(Q)$(RM) -r -- $(OUTPUT)feature/
229
230install-bin: $(OUTPUT)bpftool
231	$(call QUIET_INSTALL, bpftool)
232	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
233	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
234
235install: install-bin
236	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
237	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
238
239uninstall:
240	$(call QUIET_UNINST, bpftool)
241	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
242	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
243
244doc:
245	$(call descend,Documentation)
246
247doc-clean:
248	$(call descend,Documentation,clean)
249
250doc-install:
251	$(call descend,Documentation,install)
252
253doc-uninstall:
254	$(call descend,Documentation,uninstall)
255
256FORCE:
257
258.SECONDARY:
259.PHONY: all FORCE clean install-bin install uninstall
260.PHONY: doc doc-clean doc-install doc-uninstall
261.DEFAULT_GOAL := all
262