Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[platform/kernel/linux-starfive.git] / tools / bpf / bpftool / Makefile
1 # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 include ../../scripts/Makefile.include
3
4 ifeq ($(srctree),)
5 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
6 srctree := $(patsubst %/,%,$(dir $(srctree)))
7 srctree := $(patsubst %/,%,$(dir $(srctree)))
8 endif
9
10 ifeq ($(V),1)
11   Q =
12 else
13   Q = @
14 endif
15
16 BPF_DIR = $(srctree)/tools/lib/bpf
17
18 ifneq ($(OUTPUT),)
19   _OUTPUT := $(OUTPUT)
20 else
21   _OUTPUT := $(CURDIR)/
22 endif
23 BOOTSTRAP_OUTPUT := $(_OUTPUT)bootstrap/
24
25 LIBBPF_OUTPUT := $(_OUTPUT)libbpf/
26 LIBBPF_DESTDIR := $(LIBBPF_OUTPUT)
27 LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include
28 LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf
29 LIBBPF := $(LIBBPF_OUTPUT)libbpf.a
30
31 LIBBPF_BOOTSTRAP_OUTPUT := $(BOOTSTRAP_OUTPUT)libbpf/
32 LIBBPF_BOOTSTRAP_DESTDIR := $(LIBBPF_BOOTSTRAP_OUTPUT)
33 LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include
34 LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf
35 LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
36
37 # We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h
38 # which are not otherwise exported by libbpf, but still required by bpftool.
39 LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h)
40 LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h)
41
42 $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR):
43         $(QUIET_MKDIR)mkdir -p $@
44
45 $(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT)
46         $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \
47                 DESTDIR=$(LIBBPF_DESTDIR:/=) prefix= $(LIBBPF) install_headers
48
49 $(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR)
50         $(call QUIET_INSTALL, $@)
51         $(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $<
52
53 $(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
54         $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
55                 DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \
56                 ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) AR=$(HOSTAR) $@ install_headers
57
58 $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
59         $(call QUIET_INSTALL, $@)
60         $(Q)install -m 644 -t $(LIBBPF_BOOTSTRAP_HDRS_DIR) $<
61
62 $(LIBBPF)-clean: FORCE | $(LIBBPF_OUTPUT)
63         $(call QUIET_CLEAN, libbpf)
64         $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
65
66 $(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
67         $(call QUIET_CLEAN, libbpf-bootstrap)
68         $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null
69
70 prefix ?= /usr/local
71 bash_compdir ?= /usr/share/bash-completion/completions
72
73 CFLAGS += -O2
74 CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
75 CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
76 CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
77         -I$(or $(OUTPUT),.) \
78         -I$(LIBBPF_INCLUDE) \
79         -I$(srctree)/kernel/bpf/ \
80         -I$(srctree)/tools/include \
81         -I$(srctree)/tools/include/uapi
82 ifneq ($(BPFTOOL_VERSION),)
83 CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
84 endif
85 ifneq ($(EXTRA_CFLAGS),)
86 CFLAGS += $(EXTRA_CFLAGS)
87 endif
88 ifneq ($(EXTRA_LDFLAGS),)
89 LDFLAGS += $(EXTRA_LDFLAGS)
90 endif
91
92 INSTALL ?= install
93 RM ?= rm -f
94
95 FEATURE_USER = .bpftool
96 FEATURE_TESTS = libbfd libbfd-liberty libbfd-liberty-z \
97         disassembler-four-args disassembler-init-styled libcap \
98         clang-bpf-co-re
99 FEATURE_DISPLAY = libbfd libbfd-liberty libbfd-liberty-z \
100         libcap clang-bpf-co-re
101
102 check_feat := 1
103 NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
104 ifdef MAKECMDGOALS
105 ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
106   check_feat := 0
107 endif
108 endif
109
110 ifeq ($(check_feat),1)
111 ifeq ($(FEATURES_DUMP),)
112 include $(srctree)/tools/build/Makefile.feature
113 else
114 include $(FEATURES_DUMP)
115 endif
116 endif
117
118 ifeq ($(feature-disassembler-four-args), 1)
119 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
120 endif
121 ifeq ($(feature-disassembler-init-styled), 1)
122     CFLAGS += -DDISASM_INIT_STYLED
123 endif
124
125 LIBS = $(LIBBPF) -lelf -lz
126 LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
127 ifeq ($(feature-libcap), 1)
128 CFLAGS += -DUSE_LIBCAP
129 LIBS += -lcap
130 endif
131
132 include $(wildcard $(OUTPUT)*.d)
133
134 all: $(OUTPUT)bpftool
135
136 BFD_SRCS = jit_disasm.c
137
138 SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
139
140 ifeq ($(feature-libbfd),1)
141   LIBS += -lbfd -ldl -lopcodes
142 else ifeq ($(feature-libbfd-liberty),1)
143   LIBS += -lbfd -ldl -lopcodes -liberty
144 else ifeq ($(feature-libbfd-liberty-z),1)
145   LIBS += -lbfd -ldl -lopcodes -liberty -lz
146 endif
147
148 ifneq ($(filter -lbfd,$(LIBS)),)
149 CFLAGS += -DHAVE_LIBBFD_SUPPORT
150 SRCS += $(BFD_SRCS)
151 endif
152
153 HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
154                 $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
155
156 BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
157
158 BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
159 $(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP)
160
161 OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
162 $(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS)
163
164 VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)                            \
165                      $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)    \
166                      ../../../vmlinux                                   \
167                      /sys/kernel/btf/vmlinux                            \
168                      /boot/vmlinux-$(shell uname -r)
169 VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
170
171 bootstrap: $(BPFTOOL_BOOTSTRAP)
172
173 ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
174 ifeq ($(feature-clang-bpf-co-re),1)
175
176 BUILD_BPF_SKELS := 1
177
178 $(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
179 ifeq ($(VMLINUX_H),)
180         $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
181 else
182         $(Q)cp "$(VMLINUX_H)" $@
183 endif
184
185 $(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP)
186         $(QUIET_CLANG)$(CLANG) \
187                 -I$(or $(OUTPUT),.) \
188                 -I$(srctree)/tools/include/uapi/ \
189                 -I$(LIBBPF_BOOTSTRAP_INCLUDE) \
190                 -g -O2 -Wall -target bpf -c $< -o $@
191         $(Q)$(LLVM_STRIP) -g $@
192
193 $(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
194         $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
195
196 $(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
197
198 $(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
199
200 endif
201 endif
202
203 CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
204
205 $(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
206         $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
207
208 $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
209         $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
210
211 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
212         $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
213
214 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
215         $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
216
217 $(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
218         $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
219
220 $(OUTPUT)%.o: %.c
221         $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
222
223 feature-detect-clean:
224         $(call QUIET_CLEAN, feature-detect)
225         $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
226
227 clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
228         $(call QUIET_CLEAN, bpftool)
229         $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
230         $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
231         $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
232         $(call QUIET_CLEAN, core-gen)
233         $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
234         $(Q)$(RM) -r -- $(OUTPUT)feature/
235
236 install-bin: $(OUTPUT)bpftool
237         $(call QUIET_INSTALL, bpftool)
238         $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
239         $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
240
241 install: install-bin
242         $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
243         $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
244
245 uninstall:
246         $(call QUIET_UNINST, bpftool)
247         $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
248         $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
249
250 doc:
251         $(call descend,Documentation)
252
253 doc-clean:
254         $(call descend,Documentation,clean)
255
256 doc-install:
257         $(call descend,Documentation,install)
258
259 doc-uninstall:
260         $(call descend,Documentation,uninstall)
261
262 FORCE:
263
264 .SECONDARY:
265 .PHONY: all FORCE bootstrap clean install-bin install uninstall
266 .PHONY: doc doc-clean doc-install doc-uninstall
267 .DEFAULT_GOAL := all