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