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