################################################################################
#
# perf
#
################################################################################

LINUX_TOOLS += perf

PERF_DEPENDENCIES = host-flex host-bison

ifeq ($(BR2_PACKAGE_LINUX_TOOLS_PERF_NEEDS_HOST_PYTHON3),y)
PERF_DEPENDENCIES += host-python3
endif

ifeq ($(NORMALIZED_ARCH),x86_64)
PERF_ARCH=x86
else
PERF_ARCH=$(NORMALIZED_ARCH)
endif

PERF_MAKE_FLAGS = \
	$(LINUX_MAKE_FLAGS) \
	JOBS=$(PARALLEL_JOBS) \
	ARCH=$(PERF_ARCH) \
	DESTDIR=$(TARGET_DIR) \
	prefix=/usr \
	NO_GTK2=1 \
	NO_LIBPERL=1 \
	NO_LIBPYTHON=1 \
	NO_LIBBIONIC=1

ifeq ($(BR2_PACKAGE_LINUX_TOOLS_PERF_STATIC),y)
PERF_MAKE_FLAGS += LDFLAGS="$(TARGET_LDFLAGS) -static"
endif

# We need to pass an argument to ld for setting the emulation when
# building for MIPS architecture, otherwise the default one will always
# be used and the compilation for most variants will fail.
ifeq ($(BR2_mips),y)
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf32btsmip"
else ifeq ($(BR2_mipsel),y)
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf32ltsmip"
else ifeq ($(BR2_mips64),y)
ifeq ($(BR2_MIPS_NABI32),y)
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf32btsmipn32"
else
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf64btsmip"
endif
else ifeq ($(BR2_mips64el),y)
ifeq ($(BR2_MIPS_NABI32),y)
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf32ltsmipn32"
else
PERF_MAKE_FLAGS += LD="$(TARGET_LD) -m elf64ltsmip"
endif
endif

# The call to backtrace() function fails for ARC, because for some
# reason the unwinder from libgcc returns early. Thus the usage of
# backtrace() should be disabled in perf explicitly: at build time
# backtrace() appears to be available, but it fails at runtime: the
# backtrace will contain only several functions from the top of stack,
# instead of the complete backtrace.
ifeq ($(BR2_arc),y)
PERF_MAKE_FLAGS += NO_BACKTRACE=1
endif

ifeq ($(BR2_PACKAGE_LINUX_TOOLS_PERF_TUI),y)
PERF_DEPENDENCIES += slang
else
PERF_MAKE_FLAGS += NO_NEWT=1 NO_SLANG=1
endif

ifeq ($(BR2_PACKAGE_AUDIT),y)
PERF_DEPENDENCIES += audit
else
PERF_MAKE_FLAGS += NO_LIBAUDIT=1
endif

ifeq ($(BR2_PACKAGE_ZSTD),y)
PERF_DEPENDENCIES += zstd
endif

ifeq ($(BR2_PACKAGE_LIBUNWIND),y)
PERF_DEPENDENCIES += libunwind

ifeq ($(BR2_aarch64)$(BR2_PACKAGE_LINUX_TOOLS_PERF_STATIC)$(BR2_PACKAGE_XZ),yyy)
# libunwind-aarch64.a requires symbols from liblzma.a
PERF_MAKE_FLAGS += LIBUNWIND_LDFLAGS="-lunwind-aarch64 -llzma"
endif

else
PERF_MAKE_FLAGS += NO_LIBUNWIND=1
endif

ifeq ($(BR2_PACKAGE_NUMACTL),y)
PERF_DEPENDENCIES += numactl
else
PERF_MAKE_FLAGS += NO_LIBNUMA=1
endif

ifeq ($(BR2_PACKAGE_ELFUTILS),y)
PERF_DEPENDENCIES += elfutils
else
PERF_MAKE_FLAGS += NO_LIBELF=1 NO_DWARF=1
endif

ifeq ($(BR2_PACKAGE_BINUTILS),y)
PERF_DEPENDENCIES += binutils
else
PERF_MAKE_FLAGS += NO_DEMANGLE=1
endif

ifeq ($(BR2_PACKAGE_OPENSSL),y)
PERF_DEPENDENCIES += openssl
else
PERF_MAKE_FLAGS += NO_LIBCRYPTO=1
endif

ifeq ($(BR2_PACKAGE_ZLIB),y)
PERF_DEPENDENCIES += zlib
else
PERF_MAKE_FLAGS += NO_ZLIB=1
endif

# lzma is provided by xz
ifeq ($(BR2_PACKAGE_XZ),y)
PERF_DEPENDENCIES += xz
else
PERF_MAKE_FLAGS += NO_LZMA=1
endif

# We really do not want to build the perf documentation, because it
# has stringent requirement on the documentation generation tools,
# like xmlto and asciidoc), which may be lagging behind on some
# distributions.
# We name it 'GNUmakefile' so that GNU make will use it instead of
# the existing 'Makefile'.
define PERF_DISABLE_DOCUMENTATION
	if [ -f $(LINUX_DIR)/tools/perf/Documentation/Makefile ]; then \
		printf "%%:\n\t@:\n" >$(LINUX_DIR)/tools/perf/Documentation/GNUmakefile; \
	fi
endef
LINUX_TOOLS_POST_PATCH_HOOKS += PERF_DISABLE_DOCUMENTATION

# O must be redefined here to overwrite the one used by Buildroot for
# out of tree build. We build perf in $(LINUX_DIR)/tools/perf/ and not just
# $(LINUX_DIR) so that it isn't built in the root directory of the kernel
# sources.
define PERF_BUILD_CMDS
	$(Q)if test ! -f $(LINUX_DIR)/tools/perf/Makefile ; then \
		echo "Your kernel version is too old and does not have the perf tool." ; \
		echo "At least kernel 2.6.31 must be used." ; \
		exit 1 ; \
	fi
	$(Q)if test "$(BR2_PACKAGE_ELFUTILS)" = "" ; then \
		if ! grep -q NO_LIBELF $(LINUX_DIR)/tools/perf/Makefile* ; then \
			if ! test -r $(LINUX_DIR)/tools/perf/config/Makefile ; then \
				echo "The perf tool in your kernel cannot be built without libelf." ; \
				echo "Either upgrade your kernel to >= 3.7, or enable the elfutils package." ; \
				exit 1 ; \
			fi \
		fi \
	fi
	$(Q)if test "$(BR2_PACKAGE_LINUX_TOOLS_PERF_TUI)" = "y" ; then \
		if ! grep -q NO_SLANG $(LINUX_DIR)/tools/perf/Makefile* ; then \
			echo "The perf tool in your kernel cannot be build with the TUI." ; \
			echo "Either upgrade your kernel to >= 3.10, or disable the TUI." ; \
			exit 1 ; \
		fi \
	fi
	$(TARGET_MAKE_ENV) $(MAKE1) $(PERF_MAKE_FLAGS) \
		-C $(LINUX_DIR)/tools/perf O=$(LINUX_DIR)/tools/perf/
endef

# After installation, we remove the Perl and Python scripts from the
# target unless BR2_PACKAGE_LINUX_TOOLS_PERF_SCRIPTS is enabled
ifeq ($(BR2_PACKAGE_LINUX_TOOLS_PERF_SCRIPTS),)
define PERF_INSTALL_REMOVE_SCRIPTS
	$(RM) -r $(TARGET_DIR)/usr/libexec/perf-core/scripts/
	$(RM) -r $(TARGET_DIR)/usr/libexec/perf-core/tests/
endef
endif

define PERF_INSTALL_TARGET_CMDS
	$(TARGET_MAKE_ENV) $(MAKE1) $(PERF_MAKE_FLAGS) \
		-C $(LINUX_DIR)/tools/perf O=$(LINUX_DIR)/tools/perf/ install
	$(PERF_INSTALL_REMOVE_SCRIPTS)
endef

define PERF_LINUX_CONFIG_FIXUPS
	$(call KCONFIG_ENABLE_OPT,CONFIG_PERF_EVENTS)
endef
