blob: f60e6ad3a1dffaffe040655315ad735df6adeea2 [file] [log] [blame]
Thomas Gleixnerec8f24b2019-05-19 13:07:45 +01001# SPDX-License-Identifier: GPL-2.0-only
Jakub Kicinski71bb4282017-10-04 20:10:04 -07002include ../../scripts/Makefile.include
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003include ../../scripts/utilities.mak
4
5ifeq ($(srctree),)
6srctree := $(patsubst %/,%,$(dir $(CURDIR)))
7srctree := $(patsubst %/,%,$(dir $(srctree)))
8srctree := $(patsubst %/,%,$(dir $(srctree)))
Jakub Kicinski71bb4282017-10-04 20:10:04 -07009endif
10
11ifeq ($(V),1)
12 Q =
13else
14 Q = @
15endif
16
Quentin Monnet658e85a2017-12-07 15:00:17 -080017BPF_DIR = $(srctree)/tools/lib/bpf/
Jakub Kicinski71bb4282017-10-04 20:10:04 -070018
19ifneq ($(OUTPUT),)
Quentin Monnet45c5589d2019-08-30 12:00:38 +010020 LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
21 LIBBPF_PATH = $(LIBBPF_OUTPUT)
Jakub Kicinski71bb4282017-10-04 20:10:04 -070022else
Quentin Monnet45c5589d2019-08-30 12:00:38 +010023 LIBBPF_PATH = $(BPF_DIR)
Jakub Kicinski71bb4282017-10-04 20:10:04 -070024endif
25
Quentin Monnet45c5589d2019-08-30 12:00:38 +010026LIBBPF = $(LIBBPF_PATH)libbpf.a
Jakub Kicinski71bb4282017-10-04 20:10:04 -070027
Tony Ambardarba2fd562020-09-17 04:58:33 -070028BPFTOOL_VERSION ?= $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +000029
Jakub Kicinski71bb4282017-10-04 20:10:04 -070030$(LIBBPF): FORCE
Quentin Monnet45c5589d2019-08-30 12:00:38 +010031 $(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT))
32 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
Jakub Kicinski71bb4282017-10-04 20:10:04 -070033
34$(LIBBPF)-clean:
35 $(call QUIET_CLEAN, libbpf)
Quentin Monnet45c5589d2019-08-30 12:00:38 +010036 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
Jakub Kicinski71bb4282017-10-04 20:10:04 -070037
Quentin Monnet658e85a2017-12-07 15:00:17 -080038prefix ?= /usr/local
Quentin Monnet507e5902017-11-28 17:44:32 -080039bash_compdir ?= /usr/share/bash-completion/completions
Jakub Kicinski71bb4282017-10-04 20:10:04 -070040
Jakub Kicinski71bb4282017-10-04 20:10:04 -070041CFLAGS += -O2
Quentin Monneta9436dc2019-08-14 12:37:24 +010042CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
Andrii Nakryiko17bbf922020-07-01 14:28:16 -070043CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
Jakub Kicinskif412eed2018-05-03 18:37:16 -070044CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -070045 -I$(if $(OUTPUT),$(OUTPUT),.) \
Jakub Kicinskif412eed2018-05-03 18:37:16 -070046 -I$(srctree)/kernel/bpf/ \
47 -I$(srctree)/tools/include \
48 -I$(srctree)/tools/include/uapi \
Toke Høiland-Jørgensen229c3b42020-01-20 14:06:46 +010049 -I$(srctree)/tools/lib \
Jakub Kicinskif412eed2018-05-03 18:37:16 -070050 -I$(srctree)/tools/perf
Roman Gushchin4bfe3bd2017-12-27 19:16:28 +000051CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
Jiri Olsa0ef6bf32018-10-08 10:22:58 +020052ifneq ($(EXTRA_CFLAGS),)
53CFLAGS += $(EXTRA_CFLAGS)
54endif
Jiri Olsac8506162018-10-08 10:22:59 +020055ifneq ($(EXTRA_LDFLAGS),)
56LDFLAGS += $(EXTRA_LDFLAGS)
57endif
Jiri Olsa0ef6bf32018-10-08 10:22:58 +020058
Quentin Monnet658e85a2017-12-07 15:00:17 -080059INSTALL ?= install
60RM ?= rm -f
Song Liu47c09d62020-03-09 10:32:15 -070061CLANG ?= clang
Andrii Nakryikof3c93a92020-07-21 21:38:04 -070062LLVM_STRIP ?= llvm-strip
Quentin Monnet658e85a2017-12-07 15:00:17 -080063
Roman Gushchinfb982662017-12-27 19:16:29 +000064FEATURE_USER = .bpftool
Quentin Monnet0b3b9ca2020-04-29 15:45:06 +010065FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -070066 clang-bpf-co-re
Quentin Monnet0b3b9ca2020-04-29 15:45:06 +010067FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -070068 clang-bpf-co-re
Roman Gushchinfb982662017-12-27 19:16:29 +000069
70check_feat := 1
71NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
72ifdef MAKECMDGOALS
73ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
74 check_feat := 0
75endif
76endif
77
78ifeq ($(check_feat),1)
79ifeq ($(FEATURES_DUMP),)
80include $(srctree)/tools/build/Makefile.feature
81else
82include $(FEATURES_DUMP)
83endif
84endif
85
86ifeq ($(feature-disassembler-four-args), 1)
87CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
88endif
89
Jakub Kicinski531b0142018-07-10 14:43:05 -070090ifeq ($(feature-reallocarray), 0)
91CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
92endif
93
Quentin Monnet0b3b9ca2020-04-29 15:45:06 +010094LIBS = $(LIBBPF) -lelf -lz
95ifeq ($(feature-libcap), 1)
96CFLAGS += -DUSE_LIBCAP
97LIBS += -lcap
98endif
99
Jakub Kicinski90126e32018-03-15 23:26:14 -0700100include $(wildcard $(OUTPUT)*.d)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700101
102all: $(OUTPUT)bpftool
103
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800104BFD_SRCS = jit_disasm.c
105
106SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
107
108ifeq ($(feature-libbfd),1)
Stanislav Fomichev6e8ccb42019-01-15 14:03:27 -0800109 LIBS += -lbfd -ldl -lopcodes
110else ifeq ($(feature-libbfd-liberty),1)
111 LIBS += -lbfd -ldl -lopcodes -liberty
112else ifeq ($(feature-libbfd-liberty-z),1)
113 LIBS += -lbfd -ldl -lopcodes -liberty -lz
114endif
115
116ifneq ($(filter -lbfd,$(LIBS)),)
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800117CFLAGS += -DHAVE_LIBBFD_SUPPORT
118SRCS += $(BFD_SRCS)
Stanislav Fomichev29a9c102018-11-12 13:44:10 -0800119endif
120
Andrii Nakryiko16e9b182020-06-19 16:16:59 -0700121BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
122
123BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
Quentin Monnet658e85a2017-12-07 15:00:17 -0800124OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
Song Liu47c09d62020-03-09 10:32:15 -0700125
Andrii Nakryikoec23eb72020-06-29 17:47:58 -0700126VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700127 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \
128 ../../../vmlinux \
129 /sys/kernel/btf/vmlinux \
130 /boot/vmlinux-$(shell uname -r)
Andrii Nakryikoec23eb72020-06-29 17:47:58 -0700131VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
Song Liu14e57282020-03-12 11:23:30 -0700132
Andrii Nakryikoec23eb72020-06-29 17:47:58 -0700133ifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700134ifeq ($(feature-clang-bpf-co-re),1)
135
136BUILD_BPF_SKELS := 1
137
138$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
Andrii Nakryikoec23eb72020-06-29 17:47:58 -0700139ifeq ($(VMLINUX_H),)
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700140 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
Andrii Nakryikoec23eb72020-06-29 17:47:58 -0700141else
142 $(Q)cp "$(VMLINUX_H)" $@
143endif
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700144
145$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
Tobias Klauserfe4eb062020-03-12 14:03:30 +0100146 $(QUIET_CLANG)$(CLANG) \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700147 -I$(if $(OUTPUT),$(OUTPUT),.) \
Tobias Klauserfe4eb062020-03-12 14:03:30 +0100148 -I$(srctree)/tools/include/uapi/ \
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700149 -I$(LIBBPF_PATH) \
150 -I$(srctree)/tools/lib \
Andrii Nakryikof3c93a92020-07-21 21:38:04 -0700151 -g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
Song Liu47c09d62020-03-09 10:32:15 -0700152
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700153$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
154 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
Song Liu47c09d62020-03-09 10:32:15 -0700155
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700156$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
157
Andrii Nakryikod53dee32020-06-19 16:17:02 -0700158$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
159
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700160endif
161endif
162
Tobias Klauser9d9d8cc2020-06-23 12:37:10 +0200163CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
Jakub Kicinskic9c35992017-10-09 10:30:13 -0700164
165$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
Ilya Leoshkevichd70a6be2020-06-02 19:56:48 +0200166 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700167
Peter Wud66fa3c2019-08-13 01:38:33 +0100168$(OUTPUT)feature.o: | zdep
169
Andrii Nakryiko16e9b182020-06-19 16:16:59 -0700170$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
171 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)
172
173$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
174 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700175
176$(OUTPUT)%.o: %.c
Ilya Leoshkevichd70a6be2020-06-02 19:56:48 +0200177 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700178
Jesper Dangaard Brouer661b37c2020-08-27 10:53:36 +0200179feature-detect-clean:
180 $(call QUIET_CLEAN, feature-detect)
181 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
182
183clean: $(LIBBPF)-clean feature-detect-clean
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700184 $(call QUIET_CLEAN, bpftool)
Quentin Monnetfbdb6202019-08-30 12:00:39 +0100185 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700186 $(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
Quentin Monnetfbdb6202019-08-30 12:00:39 +0100187 $(Q)$(RM) -r -- $(OUTPUT)libbpf/
Jakub Kicinskicc5b3402018-03-15 23:26:17 -0700188 $(call QUIET_CLEAN, core-gen)
Quentin Monnetfbdb6202019-08-30 12:00:39 +0100189 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
190 $(Q)$(RM) -r -- $(OUTPUT)feature/
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700191
Quentin Monnet658e85a2017-12-07 15:00:17 -0800192install: $(OUTPUT)bpftool
193 $(call QUIET_INSTALL, bpftool)
194 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
195 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
196 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
197 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700198
Quentin Monnetd3244242017-12-07 15:00:18 -0800199uninstall:
200 $(call QUIET_UNINST, bpftool)
Quentin Monnetfbdb6202019-08-30 12:00:39 +0100201 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
202 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
Quentin Monnetd3244242017-12-07 15:00:18 -0800203
Jakub Kicinskiff69c212017-10-04 20:10:05 -0700204doc:
Quentin Monnet658e85a2017-12-07 15:00:17 -0800205 $(call descend,Documentation)
206
207doc-clean:
208 $(call descend,Documentation,clean)
Jakub Kicinskiff69c212017-10-04 20:10:05 -0700209
210doc-install:
Quentin Monnet658e85a2017-12-07 15:00:17 -0800211 $(call descend,Documentation,install)
Jakub Kicinskiff69c212017-10-04 20:10:05 -0700212
Quentin Monnetd3244242017-12-07 15:00:18 -0800213doc-uninstall:
214 $(call descend,Documentation,uninstall)
215
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700216FORCE:
217
Peter Wud66fa3c2019-08-13 01:38:33 +0100218zdep:
219 @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
220
Andrii Nakryiko05aca6d2020-06-19 16:17:00 -0700221.SECONDARY:
Peter Wud66fa3c2019-08-13 01:38:33 +0100222.PHONY: all FORCE clean install uninstall zdep
Quentin Monnetd3244242017-12-07 15:00:18 -0800223.PHONY: doc doc-clean doc-install doc-uninstall
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700224.DEFAULT_GOAL := all