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