8c6563e56ffc8ab12d3e6295ad03775cee9465c1
[platform/kernel/linux-starfive.git] / tools / bpf / bpftool / Makefile
1 # SPDX-License-Identifier: GPL-2.0-only
2 include ../../scripts/Makefile.include
3 include ../../scripts/utilities.mak
4
5 ifeq ($(srctree),)
6 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7 srctree := $(patsubst %/,%,$(dir $(srctree)))
8 srctree := $(patsubst %/,%,$(dir $(srctree)))
9 endif
10
11 ifeq ($(V),1)
12   Q =
13 else
14   Q = @
15 endif
16
17 BPF_DIR = $(srctree)/tools/lib/bpf/
18
19 ifneq ($(OUTPUT),)
20   LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
21   LIBBPF_PATH = $(LIBBPF_OUTPUT)
22 else
23   LIBBPF_PATH = $(BPF_DIR)
24 endif
25
26 LIBBPF = $(LIBBPF_PATH)libbpf.a
27
28 BPFTOOL_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
38 prefix ?= /usr/local
39 bash_compdir ?= /usr/share/bash-completion/completions
40
41 CFLAGS += -O2
42 CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
43 CFLAGS += $(filter-out -Wswitch-enum,$(EXTRA_WARNINGS))
44 CFLAGS += -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
51 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
52 ifneq ($(EXTRA_CFLAGS),)
53 CFLAGS += $(EXTRA_CFLAGS)
54 endif
55 ifneq ($(EXTRA_LDFLAGS),)
56 LDFLAGS += $(EXTRA_LDFLAGS)
57 endif
58
59 INSTALL ?= install
60 RM ?= rm -f
61 CLANG ?= clang
62
63 FEATURE_USER = .bpftool
64 FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
65         clang-bpf-co-re
66 FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
67         clang-bpf-co-re
68
69 check_feat := 1
70 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
71 ifdef MAKECMDGOALS
72 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
73   check_feat := 0
74 endif
75 endif
76
77 ifeq ($(check_feat),1)
78 ifeq ($(FEATURES_DUMP),)
79 include $(srctree)/tools/build/Makefile.feature
80 else
81 include $(FEATURES_DUMP)
82 endif
83 endif
84
85 ifeq ($(feature-disassembler-four-args), 1)
86 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
87 endif
88
89 ifeq ($(feature-reallocarray), 0)
90 CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
91 endif
92
93 LIBS = $(LIBBPF) -lelf -lz
94 ifeq ($(feature-libcap), 1)
95 CFLAGS += -DUSE_LIBCAP
96 LIBS += -lcap
97 endif
98
99 include $(wildcard $(OUTPUT)*.d)
100
101 all: $(OUTPUT)bpftool
102
103 BFD_SRCS = jit_disasm.c
104
105 SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
106
107 ifeq ($(feature-libbfd),1)
108   LIBS += -lbfd -ldl -lopcodes
109 else ifeq ($(feature-libbfd-liberty),1)
110   LIBS += -lbfd -ldl -lopcodes -liberty
111 else ifeq ($(feature-libbfd-liberty-z),1)
112   LIBS += -lbfd -ldl -lopcodes -liberty -lz
113 endif
114
115 ifneq ($(filter -lbfd,$(LIBS)),)
116 CFLAGS += -DHAVE_LIBBFD_SUPPORT
117 SRCS += $(BFD_SRCS)
118 endif
119
120 BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
121
122 BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
123 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
124
125 VMLINUX_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)
130 VMLINUX_BTF := $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
131
132 ifneq ($(VMLINUX_BTF),)
133 ifeq ($(feature-clang-bpf-co-re),1)
134
135 BUILD_BPF_SKELS := 1
136
137 $(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
138         $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
139
140 $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
141         $(QUIET_CLANG)$(CLANG) \
142                 -I$(if $(OUTPUT),$(OUTPUT),.) \
143                 -I$(srctree)/tools/include/uapi/ \
144                 -I$(LIBBPF_PATH) \
145                 -I$(srctree)/tools/lib \
146                 -g -O2 -target bpf -c $< -o $@
147
148 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
149         $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
150
151 $(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
152
153 $(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
154
155 endif
156 endif
157
158 CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
159
160 $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
161         $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
162
163 $(OUTPUT)feature.o: | zdep
164
165 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
166         $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)
167
168 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
169         $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
170
171 $(OUTPUT)%.o: %.c
172         $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
173
174 clean: $(LIBBPF)-clean
175         $(call QUIET_CLEAN, bpftool)
176         $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
177         $(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
178         $(Q)$(RM) -r -- $(OUTPUT)libbpf/
179         $(call QUIET_CLEAN, core-gen)
180         $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
181         $(Q)$(RM) -r -- $(OUTPUT)feature/
182
183 install: $(OUTPUT)bpftool
184         $(call QUIET_INSTALL, bpftool)
185         $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
186         $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
187         $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
188         $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
189
190 uninstall:
191         $(call QUIET_UNINST, bpftool)
192         $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
193         $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
194
195 doc:
196         $(call descend,Documentation)
197
198 doc-clean:
199         $(call descend,Documentation,clean)
200
201 doc-install:
202         $(call descend,Documentation,install)
203
204 doc-uninstall:
205         $(call descend,Documentation,uninstall)
206
207 FORCE:
208
209 zdep:
210         @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
211
212 .SECONDARY:
213 .PHONY: all FORCE clean install uninstall zdep
214 .PHONY: doc doc-clean doc-install doc-uninstall
215 .DEFAULT_GOAL := all