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