xref: /openbmc/linux/tools/bpf/bpftool/Makefile (revision 165f2d28)
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,$(EXTRA_WARNINGS))
44CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
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
58LIBS = $(LIBBPF) -lelf -lz
59
60INSTALL ?= install
61RM ?= rm -f
62CLANG ?= clang
63
64FEATURE_USER = .bpftool
65FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib \
66	clang-bpf-global-var
67FEATURE_DISPLAY = libbfd disassembler-four-args zlib clang-bpf-global-var
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
93include $(wildcard $(OUTPUT)*.d)
94
95all: $(OUTPUT)bpftool
96
97BFD_SRCS = jit_disasm.c
98
99SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
100
101ifeq ($(feature-libbfd),1)
102  LIBS += -lbfd -ldl -lopcodes
103else ifeq ($(feature-libbfd-liberty),1)
104  LIBS += -lbfd -ldl -lopcodes -liberty
105else ifeq ($(feature-libbfd-liberty-z),1)
106  LIBS += -lbfd -ldl -lopcodes -liberty -lz
107endif
108
109ifneq ($(filter -lbfd,$(LIBS)),)
110CFLAGS += -DHAVE_LIBBFD_SUPPORT
111SRCS += $(BFD_SRCS)
112endif
113
114OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
115_OBJS = $(filter-out $(OUTPUT)prog.o,$(OBJS)) $(OUTPUT)_prog.o
116
117ifeq ($(feature-clang-bpf-global-var),1)
118	__OBJS = $(OBJS)
119else
120	__OBJS = $(_OBJS)
121endif
122
123$(OUTPUT)_prog.o: prog.c
124	$(QUIET_CC)$(COMPILE.c) -MMD -DBPFTOOL_WITHOUT_SKELETONS -o $@ $<
125
126$(OUTPUT)_bpftool: $(_OBJS) $(LIBBPF)
127	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(_OBJS) $(LIBS)
128
129skeleton/profiler.bpf.o: skeleton/profiler.bpf.c $(LIBBPF)
130	$(QUIET_CLANG)$(CLANG) \
131		-I$(srctree)/tools/include/uapi/ \
132		-I$(LIBBPF_PATH) -I$(srctree)/tools/lib \
133		-g -O2 -target bpf -c $< -o $@
134
135profiler.skel.h: $(OUTPUT)_bpftool skeleton/profiler.bpf.o
136	$(QUIET_GEN)$(OUTPUT)./_bpftool gen skeleton skeleton/profiler.bpf.o > $@
137
138$(OUTPUT)prog.o: prog.c profiler.skel.h
139	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
140
141$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
142	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
143
144$(OUTPUT)feature.o: | zdep
145
146$(OUTPUT)bpftool: $(__OBJS) $(LIBBPF)
147	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(__OBJS) $(LIBS)
148
149$(OUTPUT)%.o: %.c
150	$(QUIET_CC)$(COMPILE.c) -MMD -o $@ $<
151
152clean: $(LIBBPF)-clean
153	$(call QUIET_CLEAN, bpftool)
154	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
155	$(Q)$(RM) -- $(OUTPUT)_bpftool profiler.skel.h skeleton/profiler.bpf.o
156	$(Q)$(RM) -r -- $(OUTPUT)libbpf/
157	$(call QUIET_CLEAN, core-gen)
158	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
159	$(Q)$(RM) -r -- $(OUTPUT)feature/
160
161install: $(OUTPUT)bpftool
162	$(call QUIET_INSTALL, bpftool)
163	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
164	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
165	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
166	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
167
168uninstall:
169	$(call QUIET_UNINST, bpftool)
170	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
171	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
172
173doc:
174	$(call descend,Documentation)
175
176doc-clean:
177	$(call descend,Documentation,clean)
178
179doc-install:
180	$(call descend,Documentation,install)
181
182doc-uninstall:
183	$(call descend,Documentation,uninstall)
184
185FORCE:
186
187zdep:
188	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
189
190.PHONY: all FORCE clean install uninstall zdep
191.PHONY: doc doc-clean doc-install doc-uninstall
192.DEFAULT_GOAL := all
193