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