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 -Wnested-externs,$(EXTRA_WARNINGS)) 44CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ 45 -I$(if $(OUTPUT),$(OUTPUT),.) \ 46 -I$(srctree)/kernel/bpf/ \ 47 -I$(srctree)/tools/include \ 48 -I$(srctree)/tools/include/uapi \ 49 -I$(srctree)/tools/lib \ 50 -I$(srctree)/tools/perf 51CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' 52ifneq ($(EXTRA_CFLAGS),) 53CFLAGS += $(EXTRA_CFLAGS) 54endif 55ifneq ($(EXTRA_LDFLAGS),) 56LDFLAGS += $(EXTRA_LDFLAGS) 57endif 58 59INSTALL ?= install 60RM ?= rm -f 61CLANG ?= clang 62LLVM_STRIP ?= llvm-strip 63 64FEATURE_USER = .bpftool 65FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \ 66 clang-bpf-co-re 67FEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \ 68 clang-bpf-co-re 69 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 90ifeq ($(feature-reallocarray), 0) 91CFLAGS += -DCOMPAT_NEED_REALLOCARRAY 92endif 93 94LIBS = $(LIBBPF) -lelf -lz 95ifeq ($(feature-libcap), 1) 96CFLAGS += -DUSE_LIBCAP 97LIBS += -lcap 98endif 99 100include $(wildcard $(OUTPUT)*.d) 101 102all: $(OUTPUT)bpftool 103 104BFD_SRCS = jit_disasm.c 105 106SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) 107 108ifeq ($(feature-libbfd),1) 109 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)),) 117CFLAGS += -DHAVE_LIBBFD_SUPPORT 118SRCS += $(BFD_SRCS) 119endif 120 121BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap) 122 123BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o) 124OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o 125 126VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ 127 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ 128 ../../../vmlinux \ 129 /sys/kernel/btf/vmlinux \ 130 /boot/vmlinux-$(shell uname -r) 131VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 132 133ifneq ($(VMLINUX_BTF)$(VMLINUX_H),) 134ifeq ($(feature-clang-bpf-co-re),1) 135 136BUILD_BPF_SKELS := 1 137 138$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP) 139ifeq ($(VMLINUX_H),) 140 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@ 141else 142 $(Q)cp "$(VMLINUX_H)" $@ 143endif 144 145$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF) 146 $(QUIET_CLANG)$(CLANG) \ 147 -I$(if $(OUTPUT),$(OUTPUT),.) \ 148 -I$(srctree)/tools/include/uapi/ \ 149 -I$(LIBBPF_PATH) \ 150 -I$(srctree)/tools/lib \ 151 -g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@ 152 153$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP) 154 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@ 155 156$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h 157 158$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h 159 160endif 161endif 162 163CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS) 164 165$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c 166 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $< 167 168$(OUTPUT)feature.o: | zdep 169 170$(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) 175 176$(OUTPUT)%.o: %.c 177 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $< 178 179clean: $(LIBBPF)-clean 180 $(call QUIET_CLEAN, bpftool) 181 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d 182 $(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h 183 $(Q)$(RM) -r -- $(OUTPUT)libbpf/ 184 $(call QUIET_CLEAN, core-gen) 185 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool 186 $(Q)$(RM) -r -- $(OUTPUT)feature/ 187 188install: $(OUTPUT)bpftool 189 $(call QUIET_INSTALL, bpftool) 190 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin 191 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool 192 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir) 193 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir) 194 195uninstall: 196 $(call QUIET_UNINST, bpftool) 197 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool 198 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool 199 200doc: 201 $(call descend,Documentation) 202 203doc-clean: 204 $(call descend,Documentation,clean) 205 206doc-install: 207 $(call descend,Documentation,install) 208 209doc-uninstall: 210 $(call descend,Documentation,uninstall) 211 212FORCE: 213 214zdep: 215 @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi 216 217.SECONDARY: 218.PHONY: all FORCE clean install uninstall zdep 219.PHONY: doc doc-clean doc-install doc-uninstall 220.DEFAULT_GOAL := all 221