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