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