xref: /openbmc/linux/tools/bpf/bpftool/Makefile (revision 8931ddd8)
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= CROSS_COMPILE= 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
155HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
156		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
157
158BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
159
160BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
161$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
162
163OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
164$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
165
166VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
167		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
168		     ../../../vmlinux					\
169		     /sys/kernel/btf/vmlinux				\
170		     /boot/vmlinux-$(shell uname -r)
171VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
172
173bootstrap: $(BPFTOOL_BOOTSTRAP)
174
175ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
176ifeq ($(feature-clang-bpf-co-re),1)
177
178BUILD_BPF_SKELS := 1
179
180$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
181ifeq ($(VMLINUX_H),)
182	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
183else
184	$(Q)cp "$(VMLINUX_H)" $@
185endif
186
187$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
188	$(QUIET_CLANG)$(CLANG) \
189		-I$(if $(OUTPUT),$(OUTPUT),.) \
190		-I$(srctree)/tools/include/uapi/ \
191		-I$(LIBBPF_BOOTSTRAP_INCLUDE) \
192		-g -O2 -Wall -target bpf -c $< -o $@
193	$(Q)$(LLVM_STRIP) -g $@
194
195$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
196	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
197
198$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
199
200$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
201
202endif
203endif
204
205CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
206
207$(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
208	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
209
210$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
211	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
212
213$(OUTPUT)feature.o:
214ifneq ($(feature-zlib), 1)
215	$(error "No zlib found")
216endif
217
218$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
219	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
220
221$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
222	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
223
224$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
225	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
226
227$(OUTPUT)%.o: %.c
228	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
229
230feature-detect-clean:
231	$(call QUIET_CLEAN, feature-detect)
232	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
233
234clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
235	$(call QUIET_CLEAN, bpftool)
236	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
237	$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
238	$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
239	$(call QUIET_CLEAN, core-gen)
240	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
241	$(Q)$(RM) -r -- $(OUTPUT)feature/
242
243install-bin: $(OUTPUT)bpftool
244	$(call QUIET_INSTALL, bpftool)
245	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
246	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
247
248install: install-bin
249	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
250	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
251
252uninstall:
253	$(call QUIET_UNINST, bpftool)
254	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
255	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
256
257doc:
258	$(call descend,Documentation)
259
260doc-clean:
261	$(call descend,Documentation,clean)
262
263doc-install:
264	$(call descend,Documentation,install)
265
266doc-uninstall:
267	$(call descend,Documentation,uninstall)
268
269FORCE:
270
271.SECONDARY:
272.PHONY: all FORCE bootstrap clean install-bin install uninstall
273.PHONY: doc doc-clean doc-install doc-uninstall
274.DEFAULT_GOAL := all
275