Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / bpf / bpftool / Makefile
bloba4263dfb5e03a60d22fd77450df026b9ae1d992a
1 # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 include ../../scripts/Makefile.include
4 ifeq ($(srctree),)
5 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
6 srctree := $(patsubst %/,%,$(dir $(srctree)))
7 srctree := $(patsubst %/,%,$(dir $(srctree)))
8 endif
10 ifeq ($(V),1)
11 Q =
12 else
13 Q = @
14 endif
16 BPF_DIR = $(srctree)/tools/lib/bpf
18 ifneq ($(OUTPUT),)
19 _OUTPUT := $(OUTPUT)
20 else
21 _OUTPUT := $(CURDIR)/
22 endif
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
70 prefix ?= /usr/local
71 bash_compdir ?= /usr/share/bash-completion/completions
73 CFLAGS += -O2
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__ \
77 -I$(or $(OUTPUT),.) \
78 -I$(LIBBPF_INCLUDE) \
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)"'
84 endif
85 ifneq ($(EXTRA_CFLAGS),)
86 CFLAGS += $(EXTRA_CFLAGS)
87 endif
88 ifneq ($(EXTRA_LDFLAGS),)
89 LDFLAGS += $(EXTRA_LDFLAGS)
90 endif
92 HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
93 $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
94 HOST_LDFLAGS := $(LDFLAGS)
96 INSTALL ?= install
97 RM ?= rm -f
99 FEATURE_USER = .bpftool
101 FEATURE_TESTS := clang-bpf-co-re
102 FEATURE_TESTS += llvm
103 FEATURE_TESTS += libcap
104 FEATURE_TESTS += libbfd
105 FEATURE_TESTS += libbfd-liberty
106 FEATURE_TESTS += libbfd-liberty-z
107 FEATURE_TESTS += disassembler-four-args
108 FEATURE_TESTS += disassembler-init-styled
110 FEATURE_DISPLAY := clang-bpf-co-re
111 FEATURE_DISPLAY += llvm
112 FEATURE_DISPLAY += libcap
113 FEATURE_DISPLAY += libbfd
114 FEATURE_DISPLAY += libbfd-liberty
115 FEATURE_DISPLAY += libbfd-liberty-z
117 check_feat := 1
118 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
119 ifdef MAKECMDGOALS
120 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
121 check_feat := 0
122 endif
123 endif
125 ifeq ($(check_feat),1)
126 ifeq ($(FEATURES_DUMP),)
127 include $(srctree)/tools/build/Makefile.feature
128 else
129 include $(FEATURES_DUMP)
130 endif
131 endif
133 LIBS = $(LIBBPF) -lelf -lz
134 LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
135 ifeq ($(feature-libcap), 1)
136 CFLAGS += -DUSE_LIBCAP
137 LIBS += -lcap
138 endif
140 include $(wildcard $(OUTPUT)*.d)
142 all: $(OUTPUT)bpftool
144 SRCS := $(wildcard *.c)
146 ifeq ($(feature-llvm),1)
147 # If LLVM is available, use it for JIT disassembly
148 CFLAGS += -DHAVE_LLVM_SUPPORT
149 LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
150 # llvm-config always adds -D_GNU_SOURCE, however, it may already be in CFLAGS
151 # (e.g. when bpftool build is called from selftests build as selftests
152 # Makefile includes lib.mk which sets -D_GNU_SOURCE) which would cause
153 # compilation error due to redefinition. Let's filter it out here.
154 CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
155 LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS))
156 ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static)
157 LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))
158 LIBS += -lstdc++
159 endif
160 LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
161 else
162 # Fall back on libbfd
163 ifeq ($(feature-libbfd),1)
164 LIBS += -lbfd -ldl -lopcodes
165 else ifeq ($(feature-libbfd-liberty),1)
166 LIBS += -lbfd -ldl -lopcodes -liberty
167 else ifeq ($(feature-libbfd-liberty-z),1)
168 LIBS += -lbfd -ldl -lopcodes -liberty -lz
169 endif
171 # If one of the above feature combinations is set, we support libbfd
172 ifneq ($(filter -lbfd,$(LIBS)),)
173 CFLAGS += -DHAVE_LIBBFD_SUPPORT
175 # Libbfd interface changed over time, figure out what we need
176 ifeq ($(feature-disassembler-four-args), 1)
177 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
178 endif
179 ifeq ($(feature-disassembler-init-styled), 1)
180 CFLAGS += -DDISASM_INIT_STYLED
181 endif
182 endif
183 endif
184 ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),)
185 # No support for JIT disassembly
186 SRCS := $(filter-out jit_disasm.c,$(SRCS))
187 endif
189 BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
191 BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o)
192 $(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
194 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
195 $(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
197 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
198 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
199 ../../../vmlinux \
200 /sys/kernel/btf/vmlinux \
201 /boot/vmlinux-$(shell uname -r)
202 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
204 bootstrap: $(BPFTOOL_BOOTSTRAP)
206 ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
207 ifeq ($(feature-clang-bpf-co-re),1)
209 BUILD_BPF_SKELS := 1
211 ifeq ($(VMLINUX_H),)
212 $(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
213 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
214 else
215 $(OUTPUT)vmlinux.h: $(VMLINUX_H)
216 $(Q)cp "$(VMLINUX_H)" $@
217 endif
219 $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
220 $(QUIET_CLANG)$(CLANG) \
221 -I$(or $(OUTPUT),.) \
222 -I$(srctree)/tools/include/uapi/ \
223 -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
224 -g -O2 -Wall -fno-stack-protector \
225 --target=bpf -c $< -o $@
226 $(Q)$(LLVM_STRIP) -g $@
228 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
229 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
231 $(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
233 $(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
235 endif
236 endif
238 CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
240 $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
241 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
243 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
244 $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
246 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
247 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
249 $(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
250 $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
252 $(OUTPUT)%.o: %.c
253 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
255 feature-detect-clean:
256 $(call QUIET_CLEAN, feature-detect)
257 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
259 clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
260 $(call QUIET_CLEAN, bpftool)
261 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
262 $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
263 $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
264 $(call QUIET_CLEAN, core-gen)
265 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
266 $(Q)$(RM) -r -- $(OUTPUT)feature/
268 install-bin: $(OUTPUT)bpftool
269 $(call QUIET_INSTALL, bpftool)
270 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
271 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
273 install: install-bin
274 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
275 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
277 uninstall:
278 $(call QUIET_UNINST, bpftool)
279 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
280 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
282 doc:
283 $(call descend,Documentation)
285 doc-clean:
286 $(call descend,Documentation,clean)
288 doc-install:
289 $(call descend,Documentation,install)
291 doc-uninstall:
292 $(call descend,Documentation,uninstall)
294 FORCE:
296 .SECONDARY:
297 .PHONY: all FORCE bootstrap clean install-bin install uninstall
298 .PHONY: doc doc-clean doc-install doc-uninstall
299 .DEFAULT_GOAL := all
301 # Delete partially updated (corrupted) files on error
302 .DELETE_ON_ERROR: