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