1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 2*4882a593Smuzhiyuninclude ../../scripts/Makefile.include 3*4882a593Smuzhiyun 4*4882a593Smuzhiyunifeq ($(srctree),) 5*4882a593Smuzhiyunsrctree := $(patsubst %/,%,$(dir $(CURDIR))) 6*4882a593Smuzhiyunsrctree := $(patsubst %/,%,$(dir $(srctree))) 7*4882a593Smuzhiyunsrctree := $(patsubst %/,%,$(dir $(srctree))) 8*4882a593Smuzhiyunendif 9*4882a593Smuzhiyun 10*4882a593Smuzhiyunifeq ($(V),1) 11*4882a593Smuzhiyun Q = 12*4882a593Smuzhiyunelse 13*4882a593Smuzhiyun Q = @ 14*4882a593Smuzhiyunendif 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunBPF_DIR = $(srctree)/tools/lib/bpf/ 17*4882a593Smuzhiyun 18*4882a593Smuzhiyunifneq ($(OUTPUT),) 19*4882a593Smuzhiyun LIBBPF_OUTPUT = $(OUTPUT)/libbpf/ 20*4882a593Smuzhiyun LIBBPF_PATH = $(LIBBPF_OUTPUT) 21*4882a593Smuzhiyunelse 22*4882a593Smuzhiyun LIBBPF_PATH = $(BPF_DIR) 23*4882a593Smuzhiyunendif 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunLIBBPF = $(LIBBPF_PATH)libbpf.a 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunBPFTOOL_VERSION ?= $(shell make -rR --no-print-directory -sC ../../.. kernelversion) 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun$(LIBBPF): FORCE 30*4882a593Smuzhiyun $(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT)) 31*4882a593Smuzhiyun $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun$(LIBBPF)-clean: 34*4882a593Smuzhiyun $(call QUIET_CLEAN, libbpf) 35*4882a593Smuzhiyun $(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null 36*4882a593Smuzhiyun 37*4882a593Smuzhiyunprefix ?= /usr/local 38*4882a593Smuzhiyunbash_compdir ?= /usr/share/bash-completion/completions 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunCFLAGS += -O2 41*4882a593SmuzhiyunCFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers 42*4882a593SmuzhiyunCFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS)) 43*4882a593SmuzhiyunCFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ 44*4882a593Smuzhiyun -I$(if $(OUTPUT),$(OUTPUT),.) \ 45*4882a593Smuzhiyun -I$(srctree)/kernel/bpf/ \ 46*4882a593Smuzhiyun -I$(srctree)/tools/include \ 47*4882a593Smuzhiyun -I$(srctree)/tools/include/uapi \ 48*4882a593Smuzhiyun -I$(srctree)/tools/lib \ 49*4882a593Smuzhiyun -I$(srctree)/tools/perf 50*4882a593SmuzhiyunCFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' 51*4882a593Smuzhiyunifneq ($(EXTRA_CFLAGS),) 52*4882a593SmuzhiyunCFLAGS += $(EXTRA_CFLAGS) 53*4882a593Smuzhiyunendif 54*4882a593Smuzhiyunifneq ($(EXTRA_LDFLAGS),) 55*4882a593SmuzhiyunLDFLAGS += $(EXTRA_LDFLAGS) 56*4882a593Smuzhiyunendif 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunINSTALL ?= install 59*4882a593SmuzhiyunRM ?= rm -f 60*4882a593SmuzhiyunCLANG ?= clang 61*4882a593SmuzhiyunLLVM_STRIP ?= llvm-strip 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunFEATURE_USER = .bpftool 64*4882a593SmuzhiyunFEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \ 65*4882a593Smuzhiyun clang-bpf-co-re 66*4882a593SmuzhiyunFEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \ 67*4882a593Smuzhiyun clang-bpf-co-re 68*4882a593Smuzhiyun 69*4882a593Smuzhiyuncheck_feat := 1 70*4882a593SmuzhiyunNON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall 71*4882a593Smuzhiyunifdef MAKECMDGOALS 72*4882a593Smuzhiyunifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),) 73*4882a593Smuzhiyun check_feat := 0 74*4882a593Smuzhiyunendif 75*4882a593Smuzhiyunendif 76*4882a593Smuzhiyun 77*4882a593Smuzhiyunifeq ($(check_feat),1) 78*4882a593Smuzhiyunifeq ($(FEATURES_DUMP),) 79*4882a593Smuzhiyuninclude $(srctree)/tools/build/Makefile.feature 80*4882a593Smuzhiyunelse 81*4882a593Smuzhiyuninclude $(FEATURES_DUMP) 82*4882a593Smuzhiyunendif 83*4882a593Smuzhiyunendif 84*4882a593Smuzhiyun 85*4882a593Smuzhiyunifeq ($(feature-disassembler-four-args), 1) 86*4882a593SmuzhiyunCFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE 87*4882a593Smuzhiyunendif 88*4882a593Smuzhiyun 89*4882a593Smuzhiyunifeq ($(feature-reallocarray), 0) 90*4882a593SmuzhiyunCFLAGS += -DCOMPAT_NEED_REALLOCARRAY 91*4882a593Smuzhiyunendif 92*4882a593Smuzhiyun 93*4882a593SmuzhiyunLIBS = $(LIBBPF) -lelf -lz 94*4882a593Smuzhiyunifeq ($(feature-libcap), 1) 95*4882a593SmuzhiyunCFLAGS += -DUSE_LIBCAP 96*4882a593SmuzhiyunLIBS += -lcap 97*4882a593Smuzhiyunendif 98*4882a593Smuzhiyun 99*4882a593Smuzhiyuninclude $(wildcard $(OUTPUT)*.d) 100*4882a593Smuzhiyun 101*4882a593Smuzhiyunall: $(OUTPUT)bpftool 102*4882a593Smuzhiyun 103*4882a593SmuzhiyunBFD_SRCS = jit_disasm.c 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunSRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c)) 106*4882a593Smuzhiyun 107*4882a593Smuzhiyunifeq ($(feature-libbfd),1) 108*4882a593Smuzhiyun LIBS += -lbfd -ldl -lopcodes 109*4882a593Smuzhiyunelse ifeq ($(feature-libbfd-liberty),1) 110*4882a593Smuzhiyun LIBS += -lbfd -ldl -lopcodes -liberty 111*4882a593Smuzhiyunelse ifeq ($(feature-libbfd-liberty-z),1) 112*4882a593Smuzhiyun LIBS += -lbfd -ldl -lopcodes -liberty -lz 113*4882a593Smuzhiyunendif 114*4882a593Smuzhiyun 115*4882a593Smuzhiyunifneq ($(filter -lbfd,$(LIBS)),) 116*4882a593SmuzhiyunCFLAGS += -DHAVE_LIBBFD_SUPPORT 117*4882a593SmuzhiyunSRCS += $(BFD_SRCS) 118*4882a593Smuzhiyunendif 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunBPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap) 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunBOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o) 123*4882a593SmuzhiyunOBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunVMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \ 126*4882a593Smuzhiyun $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux) \ 127*4882a593Smuzhiyun ../../../vmlinux \ 128*4882a593Smuzhiyun /sys/kernel/btf/vmlinux \ 129*4882a593Smuzhiyun /boot/vmlinux-$(shell uname -r) 130*4882a593SmuzhiyunVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) 131*4882a593Smuzhiyun 132*4882a593Smuzhiyunifneq ($(VMLINUX_BTF)$(VMLINUX_H),) 133*4882a593Smuzhiyunifeq ($(feature-clang-bpf-co-re),1) 134*4882a593Smuzhiyun 135*4882a593SmuzhiyunBUILD_BPF_SKELS := 1 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP) 138*4882a593Smuzhiyunifeq ($(VMLINUX_H),) 139*4882a593Smuzhiyun $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@ 140*4882a593Smuzhiyunelse 141*4882a593Smuzhiyun $(Q)cp "$(VMLINUX_H)" $@ 142*4882a593Smuzhiyunendif 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF) 145*4882a593Smuzhiyun $(QUIET_CLANG)$(CLANG) \ 146*4882a593Smuzhiyun -I$(if $(OUTPUT),$(OUTPUT),.) \ 147*4882a593Smuzhiyun -I$(srctree)/tools/include/uapi/ \ 148*4882a593Smuzhiyun -I$(LIBBPF_PATH) \ 149*4882a593Smuzhiyun -I$(srctree)/tools/lib \ 150*4882a593Smuzhiyun -g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@ 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP) 153*4882a593Smuzhiyun $(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@ 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h 158*4882a593Smuzhiyun 159*4882a593Smuzhiyunendif 160*4882a593Smuzhiyunendif 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunCFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS) 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c 165*4882a593Smuzhiyun $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $< 166*4882a593Smuzhiyun 167*4882a593Smuzhiyun$(OUTPUT)feature.o: | zdep 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF) 170*4882a593Smuzhiyun $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS) 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun$(OUTPUT)bpftool: $(OBJS) $(LIBBPF) 173*4882a593Smuzhiyun $(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun$(OUTPUT)%.o: %.c 176*4882a593Smuzhiyun $(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $< 177*4882a593Smuzhiyun 178*4882a593Smuzhiyunfeature-detect-clean: 179*4882a593Smuzhiyun $(call QUIET_CLEAN, feature-detect) 180*4882a593Smuzhiyun $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 181*4882a593Smuzhiyun 182*4882a593Smuzhiyunclean: $(LIBBPF)-clean feature-detect-clean 183*4882a593Smuzhiyun $(call QUIET_CLEAN, bpftool) 184*4882a593Smuzhiyun $(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d 185*4882a593Smuzhiyun $(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h 186*4882a593Smuzhiyun $(Q)$(RM) -r -- $(OUTPUT)libbpf/ 187*4882a593Smuzhiyun $(call QUIET_CLEAN, core-gen) 188*4882a593Smuzhiyun $(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool 189*4882a593Smuzhiyun $(Q)$(RM) -r -- $(OUTPUT)feature/ 190*4882a593Smuzhiyun 191*4882a593Smuzhiyuninstall: $(OUTPUT)bpftool 192*4882a593Smuzhiyun $(call QUIET_INSTALL, bpftool) 193*4882a593Smuzhiyun $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin 194*4882a593Smuzhiyun $(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool 195*4882a593Smuzhiyun $(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir) 196*4882a593Smuzhiyun $(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir) 197*4882a593Smuzhiyun 198*4882a593Smuzhiyununinstall: 199*4882a593Smuzhiyun $(call QUIET_UNINST, bpftool) 200*4882a593Smuzhiyun $(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool 201*4882a593Smuzhiyun $(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool 202*4882a593Smuzhiyun 203*4882a593Smuzhiyundoc: 204*4882a593Smuzhiyun $(call descend,Documentation) 205*4882a593Smuzhiyun 206*4882a593Smuzhiyundoc-clean: 207*4882a593Smuzhiyun $(call descend,Documentation,clean) 208*4882a593Smuzhiyun 209*4882a593Smuzhiyundoc-install: 210*4882a593Smuzhiyun $(call descend,Documentation,install) 211*4882a593Smuzhiyun 212*4882a593Smuzhiyundoc-uninstall: 213*4882a593Smuzhiyun $(call descend,Documentation,uninstall) 214*4882a593Smuzhiyun 215*4882a593SmuzhiyunFORCE: 216*4882a593Smuzhiyun 217*4882a593Smuzhiyunzdep: 218*4882a593Smuzhiyun @if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun.SECONDARY: 221*4882a593Smuzhiyun.PHONY: all FORCE clean install uninstall zdep 222*4882a593Smuzhiyun.PHONY: doc doc-clean doc-install doc-uninstall 223*4882a593Smuzhiyun.DEFAULT_GOAL := all 224