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