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) 22else 23 LIBBPF_PATH = $(BPF_DIR) 24endif 25 26LIBBPF = $(LIBBPF_PATH)libbpf.a 27 28BPFTOOL_VERSION := $(shell make -rR --no-print-directory -sC ../../.. kernelversion) 29 30$(LIBBPF): FORCE 31 $(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT)) 32 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a 33 34$(LIBBPF)-clean: 35 $(call QUIET_CLEAN, libbpf) 36 $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null 37 38prefix ?= /usr/local 39bash_compdir ?= /usr/share/bash-completion/completions 40 41CFLAGS += -O2 42CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers 43CFLAGS += $(filter-out -Wswitch-enum,$(EXTRA_WARNINGS)) 44CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ 45 -I$(srctree)/kernel/bpf/ \ 46 -I$(srctree)/tools/include \ 47 -I$(srctree)/tools/include/uapi \ 48 -I$(srctree)/tools/lib/bpf \ 49 -I$(srctree)/tools/perf 50CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' 51ifneq ($(EXTRA_CFLAGS),) 52CFLAGS += $(EXTRA_CFLAGS) 53endif 54ifneq ($(EXTRA_LDFLAGS),) 55LDFLAGS += $(EXTRA_LDFLAGS) 56endif 57 58LIBS = $(LIBBPF) -lelf -lz 59 60INSTALL ?= install 61RM ?= rm -f 62 63FEATURE_USER = .bpftool 64FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib 65FEATURE_DISPLAY = libbfd disassembler-four-args zlib 66 67check_feat := 1 68NON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall 69ifdef MAKECMDGOALS 70ifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 71 check_feat := 0 72endif 73endif 74 75ifeq ($(check_feat),1) 76ifeq ($(FEATURES_DUMP),) 77include $(srctree)/tools/build/Makefile.feature 78else 79include $(FEATURES_DUMP) 80endif 81endif 82 83ifeq ($(feature-disassembler-four-args), 1) 84CFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 85endif 86 87ifeq ($(feature-reallocarray), 0) 88CFLAGS += -DCOMPAT_NEED_REALLOCARRAY 89endif 90 91include $(wildcard $(OUTPUT)*.d) 92 93all: $(OUTPUT)bpftool 94 95BFD_SRCS = jit_disasm.c 96 97SRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) 98 99ifeq ($(feature-libbfd),1) 100 LIBS += -lbfd -ldl -lopcodes 101else ifeq ($(feature-libbfd-liberty),1) 102 LIBS += -lbfd -ldl -lopcodes -liberty 103else ifeq ($(feature-libbfd-liberty-z),1) 104 LIBS += -lbfd -ldl -lopcodes -liberty -lz 105endif 106 107ifneq ($(filter -lbfd,$(LIBS)),) 108CFLAGS += -DHAVE_LIBBFD_SUPPORT 109SRCS += $(BFD_SRCS) 110endif 111 112OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o 113 114$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c 115 $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $< 116 117$(OUTPUT)feature.o: | zdep 118 119$(OUTPUT)bpftool: $(OBJS) $(LIBBPF) 120 $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) 121 122$(OUTPUT)%.o: %.c 123 $(QUIET_CC)$(COMPILE.c) -MMD -o $@ $< 124 125clean: $(LIBBPF)-clean 126 $(call QUIET_CLEAN, bpftool) 127 $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d 128 $(Q)$(RM) -r -- $(OUTPUT)libbpf/ 129 $(call QUIET_CLEAN, core-gen) 130 $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool 131 $(Q)$(RM) -r -- $(OUTPUT)feature/ 132 133install: $(OUTPUT)bpftool 134 $(call QUIET_INSTALL, bpftool) 135 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin 136 $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool 137 $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir) 138 $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir) 139 140uninstall: 141 $(call QUIET_UNINST, bpftool) 142 $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool 143 $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool 144 145doc: 146 $(call descend,Documentation) 147 148doc-clean: 149 $(call descend,Documentation,clean) 150 151doc-install: 152 $(call descend,Documentation,install) 153 154doc-uninstall: 155 $(call descend,Documentation,uninstall) 156 157FORCE: 158 159zdep: 160 @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi 161 162.PHONY: all FORCE clean install uninstall zdep 163.PHONY: doc doc-clean doc-install doc-uninstall 164.DEFAULT_GOAL := all 165