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