18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ciinclude ../../scripts/Makefile.include
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciifeq ($(srctree),)
58c2ecf20Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(CURDIR)))
68c2ecf20Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(srctree)))
78c2ecf20Sopenharmony_cisrctree := $(patsubst %/,%,$(dir $(srctree)))
88c2ecf20Sopenharmony_ciendif
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciifeq ($(V),1)
118c2ecf20Sopenharmony_ci  Q =
128c2ecf20Sopenharmony_cielse
138c2ecf20Sopenharmony_ci  Q = @
148c2ecf20Sopenharmony_ciendif
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciBPF_DIR = $(srctree)/tools/lib/bpf/
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciifneq ($(OUTPUT),)
198c2ecf20Sopenharmony_ci  LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
208c2ecf20Sopenharmony_ci  LIBBPF_PATH = $(LIBBPF_OUTPUT)
218c2ecf20Sopenharmony_cielse
228c2ecf20Sopenharmony_ci  LIBBPF_PATH = $(BPF_DIR)
238c2ecf20Sopenharmony_ciendif
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciLIBBPF = $(LIBBPF_PATH)libbpf.a
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciBPFTOOL_VERSION ?= $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci$(LIBBPF): FORCE
308c2ecf20Sopenharmony_ci	$(if $(LIBBPF_OUTPUT),@mkdir -p $(LIBBPF_OUTPUT))
318c2ecf20Sopenharmony_ci	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci$(LIBBPF)-clean:
348c2ecf20Sopenharmony_ci	$(call QUIET_CLEAN, libbpf)
358c2ecf20Sopenharmony_ci	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciprefix ?= /usr/local
388c2ecf20Sopenharmony_cibash_compdir ?= /usr/share/bash-completion/completions
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciCFLAGS += -O2
418c2ecf20Sopenharmony_ciCFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
428c2ecf20Sopenharmony_ciCFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
438c2ecf20Sopenharmony_ciCFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
448c2ecf20Sopenharmony_ci	-I$(if $(OUTPUT),$(OUTPUT),.) \
458c2ecf20Sopenharmony_ci	-I$(srctree)/kernel/bpf/ \
468c2ecf20Sopenharmony_ci	-I$(srctree)/tools/include \
478c2ecf20Sopenharmony_ci	-I$(srctree)/tools/include/uapi \
488c2ecf20Sopenharmony_ci	-I$(srctree)/tools/lib \
498c2ecf20Sopenharmony_ci	-I$(srctree)/tools/perf
508c2ecf20Sopenharmony_ciCFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
518c2ecf20Sopenharmony_ciifneq ($(EXTRA_CFLAGS),)
528c2ecf20Sopenharmony_ciCFLAGS += $(EXTRA_CFLAGS)
538c2ecf20Sopenharmony_ciendif
548c2ecf20Sopenharmony_ciifneq ($(EXTRA_LDFLAGS),)
558c2ecf20Sopenharmony_ciLDFLAGS += $(EXTRA_LDFLAGS)
568c2ecf20Sopenharmony_ciendif
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciINSTALL ?= install
598c2ecf20Sopenharmony_ciRM ?= rm -f
608c2ecf20Sopenharmony_ciCLANG ?= clang
618c2ecf20Sopenharmony_ciLLVM_STRIP ?= llvm-strip
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciFEATURE_USER = .bpftool
648c2ecf20Sopenharmony_ciFEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib libcap \
658c2ecf20Sopenharmony_ci	clang-bpf-co-re
668c2ecf20Sopenharmony_ciFEATURE_DISPLAY = libbfd disassembler-four-args zlib libcap \
678c2ecf20Sopenharmony_ci	clang-bpf-co-re
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cicheck_feat := 1
708c2ecf20Sopenharmony_ciNON_CHECK_FEAT_TARGETS := clean uninstall doc doc-clean doc-install doc-uninstall
718c2ecf20Sopenharmony_ciifdef MAKECMDGOALS
728c2ecf20Sopenharmony_ciifeq ($(filter-out $(NON_CHECK_FEAT_TARGETS),$(MAKECMDGOALS)),)
738c2ecf20Sopenharmony_ci  check_feat := 0
748c2ecf20Sopenharmony_ciendif
758c2ecf20Sopenharmony_ciendif
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciifeq ($(check_feat),1)
788c2ecf20Sopenharmony_ciifeq ($(FEATURES_DUMP),)
798c2ecf20Sopenharmony_ciinclude $(srctree)/tools/build/Makefile.feature
808c2ecf20Sopenharmony_cielse
818c2ecf20Sopenharmony_ciinclude $(FEATURES_DUMP)
828c2ecf20Sopenharmony_ciendif
838c2ecf20Sopenharmony_ciendif
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciifeq ($(feature-disassembler-four-args), 1)
868c2ecf20Sopenharmony_ciCFLAGS += -DDISASM_FOUR_ARGS_SIGNATURE
878c2ecf20Sopenharmony_ciendif
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ciifeq ($(feature-reallocarray), 0)
908c2ecf20Sopenharmony_ciCFLAGS += -DCOMPAT_NEED_REALLOCARRAY
918c2ecf20Sopenharmony_ciendif
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciLIBS = $(LIBBPF) -lelf -lz
948c2ecf20Sopenharmony_ciifeq ($(feature-libcap), 1)
958c2ecf20Sopenharmony_ciCFLAGS += -DUSE_LIBCAP
968c2ecf20Sopenharmony_ciLIBS += -lcap
978c2ecf20Sopenharmony_ciendif
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciinclude $(wildcard $(OUTPUT)*.d)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciall: $(OUTPUT)bpftool
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciBFD_SRCS = jit_disasm.c
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciSRCS = $(filter-out $(BFD_SRCS),$(wildcard *.c))
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciifeq ($(feature-libbfd),1)
1088c2ecf20Sopenharmony_ci  LIBS += -lbfd -ldl -lopcodes
1098c2ecf20Sopenharmony_cielse ifeq ($(feature-libbfd-liberty),1)
1108c2ecf20Sopenharmony_ci  LIBS += -lbfd -ldl -lopcodes -liberty
1118c2ecf20Sopenharmony_cielse ifeq ($(feature-libbfd-liberty-z),1)
1128c2ecf20Sopenharmony_ci  LIBS += -lbfd -ldl -lopcodes -liberty -lz
1138c2ecf20Sopenharmony_ciendif
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciifneq ($(filter -lbfd,$(LIBS)),)
1168c2ecf20Sopenharmony_ciCFLAGS += -DHAVE_LIBBFD_SUPPORT
1178c2ecf20Sopenharmony_ciSRCS += $(BFD_SRCS)
1188c2ecf20Sopenharmony_ciendif
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciBPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciBOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
1238c2ecf20Sopenharmony_ciOBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ciVMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux)				\
1268c2ecf20Sopenharmony_ci		     $(if $(KBUILD_OUTPUT),$(KBUILD_OUTPUT)/vmlinux)	\
1278c2ecf20Sopenharmony_ci		     ../../../vmlinux					\
1288c2ecf20Sopenharmony_ci		     /sys/kernel/btf/vmlinux				\
1298c2ecf20Sopenharmony_ci		     /boot/vmlinux-$(shell uname -r)
1308c2ecf20Sopenharmony_ciVMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS))))
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciifneq ($(VMLINUX_BTF)$(VMLINUX_H),)
1338c2ecf20Sopenharmony_ciifeq ($(feature-clang-bpf-co-re),1)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciBUILD_BPF_SKELS := 1
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci$(OUTPUT)vmlinux.h: $(VMLINUX_BTF) $(BPFTOOL_BOOTSTRAP)
1388c2ecf20Sopenharmony_ciifeq ($(VMLINUX_H),)
1398c2ecf20Sopenharmony_ci	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) btf dump file $< format c > $@
1408c2ecf20Sopenharmony_cielse
1418c2ecf20Sopenharmony_ci	$(Q)cp "$(VMLINUX_H)" $@
1428c2ecf20Sopenharmony_ciendif
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci$(OUTPUT)%.bpf.o: skeleton/%.bpf.c $(OUTPUT)vmlinux.h $(LIBBPF)
1458c2ecf20Sopenharmony_ci	$(QUIET_CLANG)$(CLANG) \
1468c2ecf20Sopenharmony_ci		-I$(if $(OUTPUT),$(OUTPUT),.) \
1478c2ecf20Sopenharmony_ci		-I$(srctree)/tools/include/uapi/ \
1488c2ecf20Sopenharmony_ci		-I$(LIBBPF_PATH) \
1498c2ecf20Sopenharmony_ci		-I$(srctree)/tools/lib \
1508c2ecf20Sopenharmony_ci		-g -O2 -target bpf -c $< -o $@ && $(LLVM_STRIP) -g $@
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci$(OUTPUT)%.skel.h: $(OUTPUT)%.bpf.o $(BPFTOOL_BOOTSTRAP)
1538c2ecf20Sopenharmony_ci	$(QUIET_GEN)$(BPFTOOL_BOOTSTRAP) gen skeleton $< > $@
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci$(OUTPUT)prog.o: $(OUTPUT)profiler.skel.h
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci$(OUTPUT)pids.o: $(OUTPUT)pid_iter.skel.h
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ciendif
1608c2ecf20Sopenharmony_ciendif
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ciCFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci$(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
1658c2ecf20Sopenharmony_ci	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci$(OUTPUT)feature.o: | zdep
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
1708c2ecf20Sopenharmony_ci	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
1738c2ecf20Sopenharmony_ci	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci$(OUTPUT)%.o: %.c
1768c2ecf20Sopenharmony_ci	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cifeature-detect-clean:
1798c2ecf20Sopenharmony_ci	$(call QUIET_CLEAN, feature-detect)
1808c2ecf20Sopenharmony_ci	$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciclean: $(LIBBPF)-clean feature-detect-clean
1838c2ecf20Sopenharmony_ci	$(call QUIET_CLEAN, bpftool)
1848c2ecf20Sopenharmony_ci	$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
1858c2ecf20Sopenharmony_ci	$(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
1868c2ecf20Sopenharmony_ci	$(Q)$(RM) -r -- $(OUTPUT)libbpf/
1878c2ecf20Sopenharmony_ci	$(call QUIET_CLEAN, core-gen)
1888c2ecf20Sopenharmony_ci	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
1898c2ecf20Sopenharmony_ci	$(Q)$(RM) -r -- $(OUTPUT)feature/
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciinstall: $(OUTPUT)bpftool
1928c2ecf20Sopenharmony_ci	$(call QUIET_INSTALL, bpftool)
1938c2ecf20Sopenharmony_ci	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(prefix)/sbin
1948c2ecf20Sopenharmony_ci	$(Q)$(INSTALL) $(OUTPUT)bpftool $(DESTDIR)$(prefix)/sbin/bpftool
1958c2ecf20Sopenharmony_ci	$(Q)$(INSTALL) -m 0755 -d $(DESTDIR)$(bash_compdir)
1968c2ecf20Sopenharmony_ci	$(Q)$(INSTALL) -m 0644 bash-completion/bpftool $(DESTDIR)$(bash_compdir)
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciuninstall:
1998c2ecf20Sopenharmony_ci	$(call QUIET_UNINST, bpftool)
2008c2ecf20Sopenharmony_ci	$(Q)$(RM) -- $(DESTDIR)$(prefix)/sbin/bpftool
2018c2ecf20Sopenharmony_ci	$(Q)$(RM) -- $(DESTDIR)$(bash_compdir)/bpftool
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cidoc:
2048c2ecf20Sopenharmony_ci	$(call descend,Documentation)
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cidoc-clean:
2078c2ecf20Sopenharmony_ci	$(call descend,Documentation,clean)
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cidoc-install:
2108c2ecf20Sopenharmony_ci	$(call descend,Documentation,install)
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cidoc-uninstall:
2138c2ecf20Sopenharmony_ci	$(call descend,Documentation,uninstall)
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciFORCE:
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cizdep:
2188c2ecf20Sopenharmony_ci	@if [ "$(feature-zlib)" != "1" ]; then echo "No zlib found"; exit 1 ; fi
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci.SECONDARY:
2218c2ecf20Sopenharmony_ci.PHONY: all FORCE clean install uninstall zdep
2228c2ecf20Sopenharmony_ci.PHONY: doc doc-clean doc-install doc-uninstall
2238c2ecf20Sopenharmony_ci.DEFAULT_GOAL := all
224