1# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2include ../../scripts/Makefile.include 3 4ifeq ($(srctree),) 5srctree := $(patsubst %/,%,$(dir $(CURDIR))) 6srctree := $(patsubst %/,%,$(dir $(srctree))) 7srctree := $(patsubst %/,%,$(dir $(srctree))) 8endif 9 10ifeq ($(V),1) 11 Q = 12else 13 Q = @ 14endif 15 16BPF_DIR = $(srctree)/tools/lib/bpf 17 18ifneq ($(OUTPUT),) 19 _OUTPUT := $(OUTPUT) 20else 21 _OUTPUT := $(CURDIR)/ 22endif 23BOOTSTRAP_OUTPUT := $(_OUTPUT)bootstrap/ 24 25LIBBPF_OUTPUT := $(_OUTPUT)libbpf/ 26LIBBPF_DESTDIR := $(LIBBPF_OUTPUT) 27LIBBPF_INCLUDE := $(LIBBPF_DESTDIR)include 28LIBBPF_HDRS_DIR := $(LIBBPF_INCLUDE)/bpf 29LIBBPF := $(LIBBPF_OUTPUT)libbpf.a 30 31LIBBPF_BOOTSTRAP_OUTPUT := $(BOOTSTRAP_OUTPUT)libbpf/ 32LIBBPF_BOOTSTRAP_DESTDIR := $(LIBBPF_BOOTSTRAP_OUTPUT) 33LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include 34LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf 35LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a 36 37# We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h 38# which are not otherwise exported by libbpf, but still required by bpftool. 39LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h) 40LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h) 41 42$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR): 43 $(QUIET_MKDIR)mkdir -p $@ 44 45$(LIBBPF): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_OUTPUT) 46 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) \ 47 DESTDIR=$(LIBBPF_DESTDIR:/=) prefix= $(LIBBPF) install_headers 48 49$(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_DIR) 50 $(call QUIET_INSTALL, $@) 51 $(Q)install -m 644 -t $(LIBBPF_HDRS_DIR) $< 52 53$(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT) 54 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \ 55 DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR:/=) prefix= \ 56 ARCH= CROSS_COMPILE= CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)" $@ install_headers 57 58$(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR) 59 $(call QUIET_INSTALL, $@) 60 $(Q)install -m 644 -t $(LIBBPF_BOOTSTRAP_HDRS_DIR) $< 61 62$(LIBBPF)-clean: FORCE | $(LIBBPF_OUTPUT) 63 $(call QUIET_CLEAN, libbpf) 64 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null 65 66$(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT) 67 $(call QUIET_CLEAN, libbpf-bootstrap) 68 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null 69 70prefix ?= /usr/local 71bash_compdir ?= /usr/share/bash-completion/completions 72 73CFLAGS += -O2 74CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers 75CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS)) 76CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ 77 -I$(or $(OUTPUT),.) \ 78 -I$(LIBBPF_INCLUDE) \ 79 -I$(srctree)/kernel/bpf/ \ 80 -I$(srctree)/tools/include \ 81 -I$(srctree)/tools/include/uapi 82ifneq ($(BPFTOOL_VERSION),) 83CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' 84endif 85ifneq ($(EXTRA_CFLAGS),) 86CFLAGS += $(EXTRA_CFLAGS) 87endif 88ifneq ($(EXTRA_LDFLAGS),) 89LDFLAGS += $(EXTRA_LDFLAGS) 90endif 91 92HOST_CFLAGS := $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\ 93 $(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS))) 94HOST_LDFLAGS := $(LDFLAGS) 95 96INSTALL ?= install 97RM ?= rm -f 98 99FEATURE_USER = .bpftool 100 101FEATURE_TESTS := clang-bpf-co-re 102FEATURE_TESTS += llvm 103FEATURE_TESTS += libcap 104FEATURE_TESTS += libbfd 105FEATURE_TESTS += libbfd-liberty 106FEATURE_TESTS += libbfd-liberty-z 107FEATURE_TESTS += disassembler-four-args 108FEATURE_TESTS += disassembler-init-styled 109 110FEATURE_DISPLAY := clang-bpf-co-re 111FEATURE_DISPLAY += llvm 112FEATURE_DISPLAY += libcap 113FEATURE_DISPLAY += libbfd 114FEATURE_DISPLAY += libbfd-liberty 115FEATURE_DISPLAY += libbfd-liberty-z 116 117check_feat := 1 118NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall 119ifdef MAKECMDGOALS 120ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 121 check_feat := 0 122endif 123endif 124 125ifeq ($(check_feat),1) 126ifeq ($(FEATURES_DUMP),) 127include $(srctree)/tools/build/Makefile.feature 128else 129include $(FEATURES_DUMP) 130endif 131endif 132 133LIBS = $(LIBBPF) -lelf -lz 134LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz 135ifeq ($(feature-libcap), 1) 136CFLAGS += -DUSE_LIBCAP 137LIBS += -lcap 138endif 139 140include $(wildcard $(OUTPUT)*.d) 141 142all: $(OUTPUT)bpftool 143 144SRCS := $(wildcard *.c) 145 146ifeq ($(feature-llvm),1) 147 # If LLVM is available, use it for JIT disassembly 148 CFLAGS += -DHAVE_LLVM_SUPPORT 149 LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets 150 CFLAGS += $(shell $(LLVM_CONFIG) --cflags) 151 LIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS)) 152 ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static) 153 LIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS)) 154 LIBS += -lstdc++ 155 endif 156 LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags) 157else 158 # Fall back on libbfd 159 ifeq ($(feature-libbfd),1) 160 LIBS += -lbfd -ldl -lopcodes 161 else ifeq ($(feature-libbfd-liberty),1) 162 LIBS += -lbfd -ldl -lopcodes -liberty 163 else ifeq ($(feature-libbfd-liberty-z),1) 164 LIBS += -lbfd -ldl -lopcodes -liberty -lz 165 endif 166 167 # If one of the above feature combinations is set, we support libbfd 168 ifneq ($(filter -lbfd,$(LIBS)),) 169 CFLAGS += -DHAVE_LIBBFD_SUPPORT 170 171 # Libbfd interface changed over time, figure out what we need 172 ifeq ($(feature-disassembler-four-args), 1) 173 CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 174 endif 175 ifeq ($(feature-disassembler-init-styled), 1) 176 CFLAGS += -DDISASM_INIT_STYLED 177 endif 178 endif 179endif 180ifeq ($(filter -DHAVE_LLVM_SUPPORT -DHAVE_LIBBFD_SUPPORT,$(CFLAGS)),) 181 # No support for JIT disassembly 182 SRCS := $(filter-out jit_disasm.c,$(SRCS)) 183endif 184 185BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool 186 187BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o) 188$(BOOTSTRAP_OBJS): $(LIBBPF_BOOTSTRAP) 189 190OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o 191$(OBJS): $(LIBBPF) $(LIBBPF_INTERNAL_HDRS) 192 193VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ 194 $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ 195 ../../../vmlinux \ 196 /sys/kernel/btf/vmlinux \ 197 /boot/vmlinux-$(shell uname -r) 198VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 199 200bootstrap: $(BPFTOOL_BOOTSTRAP) 201 202ifneq ($(VMLINUX_BTF)$(VMLINUX_H),) 203ifeq ($(feature-clang-bpf-co-re),1) 204 205BUILD_BPF_SKELS := 1 206 207$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP) 208ifeq ($(VMLINUX_H),) 209 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@ 210else 211 $(Q)cp "$(VMLINUX_H)" $@ 212endif 213 214$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF_BOOTSTRAP) 215 $(QUIET_CLANG)$(CLANG) \ 216 -I$(or $(OUTPUT),.) \ 217 -I$(srctree)/tools/include/uapi/ \ 218 -I$(LIBBPF_BOOTSTRAP_INCLUDE) \ 219 -g -O2 -Wall -fno-stack-protector \ 220 --target=bpf -c $< -o $@ 221 $(Q)$(LLVM_STRIP) -g $@ 222 223$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP) 224 $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@ 225 226$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h 227 228$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h 229 230endif 231endif 232 233CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS) 234 235$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c 236 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@ 237 238$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP) 239 $(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@ 240 241$(OUTPUT)bpftool: $(OBJS) $(LIBBPF) 242 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ 243 244$(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT) 245 $(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@ 246 247$(OUTPUT)%.o: %.c 248 $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@ 249 250feature-detect-clean: 251 $(call QUIET_CLEAN, feature-detect) 252 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 253 254clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean 255 $(call QUIET_CLEAN, bpftool) 256 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d 257 $(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h 258 $(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) 259 $(call QUIET_CLEAN, core-gen) 260 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool 261 $(Q)$(RM) -r -- $(OUTPUT)feature/ 262 263install-bin: $(OUTPUT)bpftool 264 $(call QUIET_INSTALL, bpftool) 265 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin 266 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool 267 268install: install-bin 269 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir) 270 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir) 271 272uninstall: 273 $(call QUIET_UNINST, bpftool) 274 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool 275 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool 276 277doc: 278 $(call descend,Documentation) 279 280doc-clean: 281 $(call descend,Documentation,clean) 282 283doc-install: 284 $(call descend,Documentation,install) 285 286doc-uninstall: 287 $(call descend,Documentation,uninstall) 288 289FORCE: 290 291.SECONDARY: 292.PHONY: all FORCE bootstrap clean install-bin install uninstall 293.PHONY: doc doc-clean doc-install doc-uninstall 294.DEFAULT_GOAL := all 295 296# Delete partially updated (corrupted) files on error 297.DELETE_ON_ERROR: 298