xref: /openbmc/linux/tools/bpf/bpftool/Makefile (revision b8265621)
1# SPDX-License-Identifier: GPL-2.0-only
2include ../../scripts/Makefile.include
3include ../../scripts/utilities.mak
4
5ifeq ($(srctree),)
6srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8srctree := $(patsubst %/,%,$(dir $(srctree)))
9endif
10
11ifeq ($(V),1)
12  Q =
13else
14  Q = @
15endif
16
17BPF_DIR = $(srctree)/tools/lib/bpf/
18
19ifneq ($(OUTPUT),)
20  LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
21  LIBBPF_PATH = $(LIBBPF_OUTPUT)
22else
23  LIBBPF_PATH = $(BPF_DIR)
24endif
25
26LIBBPF = $(LIBBPF_PATH)libbpf.a
27
28BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
29
30$(LIBBPF): FORCE
31	$(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT))
32	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
33
34$(LIBBPF)-clean:
35	$(call QUIET_CLEAN, libbpf)
36	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
37
38prefix ?= /usr/local
39bash_compdir ?= /usr/share/bash-completion/completions
40
41CFLAGS += -O2
42CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
43CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
44CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
45	-I$(if $(OUTPUT),$(OUTPUT),.) \
46	-I$(srctree)/kernel/bpf/ \
47	-I$(srctree)/tools/include \
48	-I$(srctree)/tools/include/uapi \
49	-I$(srctree)/tools/lib \
50	-I$(srctree)/tools/perf
51CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
52ifneq ($(EXTRA_CFLAGS),)
53CFLAGS += $(EXTRA_CFLAGS)
54endif
55ifneq ($(EXTRA_LDFLAGS),)
56LDFLAGS += $(EXTRA_LDFLAGS)
57endif
58
59INSTALL ?= install
60RM ?= rm -f
61CLANG ?= clang
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 $@
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
178clean: $(LIBBPF)-clean
179	$(call QUIET_CLEAN, bpftool)
180	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
181	$(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
182	$(Q)$(RM) -r -- $(OUTPUT)libbpf/
183	$(call QUIET_CLEAN, core-gen)
184	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
185	$(Q)$(RM) -r -- $(OUTPUT)feature/
186
187install: $(OUTPUT)bpftool
188	$(call QUIET_INSTALL, bpftool)
189	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
190	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
191	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
192	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
193
194uninstall:
195	$(call QUIET_UNINST, bpftool)
196	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
197	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
198
199doc:
200	$(call descend,Documentation)
201
202doc-clean:
203	$(call descend,Documentation,clean)
204
205doc-install:
206	$(call descend,Documentation,install)
207
208doc-uninstall:
209	$(call descend,Documentation,uninstall)
210
211FORCE:
212
213zdep:
214	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
215
216.SECONDARY:
217.PHONY: all FORCE clean install uninstall zdep
218.PHONY: doc doc-clean doc-install doc-uninstall
219.DEFAULT_GOAL := all
220