# Copyright 2005-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit meson-multilib

DESCRIPTION="Portable and efficient API to determine the call-chain of a program"
HOMEPAGE="https://savannah.nongnu.org/projects/libunwind"

if [[ ${PV} == 9999 ]] ; then
	EGIT_REPO_URI="https://github.com/libunwind/libunwind"
	inherit git-r3
else
	MY_PV="${PV/_rc/-rc}"
	SRC_URI="https://github.com/libunwind/libunwind/releases/download/v${MY_PV}/${PN}-${MY_PV}.tar.xz"
	S="${WORKDIR}/${PN}-${MY_PV}"

	KEYWORDS="~amd64 ~arm ~arm64 ~hppa ~loong ~ppc ~ppc64 ~riscv ~s390 -sparc ~x86"
fi

LICENSE="MIT"
SLOT="0/11" # libunwind.so.11
IUSE="debug debug-frame +doc lzma static-libs test zlib"
RESTRICT="!test? ( test )"

BDEPEND="
	doc? (
		app-text/texlive-core
		dev-texlive/texlive-binextra
	)
"
RDEPEND="
	lzma? ( app-arch/xz-utils[static-libs?,${MULTILIB_USEDEP}] )
	zlib? ( virtual/zlib:=[static-libs?,${MULTILIB_USEDEP}] )
"
DEPEND="${RDEPEND}"

MULTILIB_WRAPPED_HEADERS=(
	/usr/include/libunwind.h

	# see libunwind.h for the full list of arch-specific headers
	/usr/include/libunwind-aarch64.h
	/usr/include/libunwind-alpha.h
	/usr/include/libunwind-arm.h
	/usr/include/libunwind-hppa.h
	/usr/include/libunwind-ia64.h
	/usr/include/libunwind-loongarch64.h
	/usr/include/libunwind-mips.h
	/usr/include/libunwind-ppc32.h
	/usr/include/libunwind-ppc64.h
	/usr/include/libunwind-riscv.h
	/usr/include/libunwind-s390x.h
	/usr/include/libunwind-sh.h
	/usr/include/libunwind-sparc64.h
	/usr/include/libunwind-x86.h
	/usr/include/libunwind-x86_64.h
)

multilib_src_configure() {
	local emesonargs=(
		# cxx_exceptions: always enable it, headers provide the interface
		# and on some archs it is disabled by default causing a mismatch between the
		# API and the ABI, bug #418253
		-Dcxx_exceptions=enabled
		-Dcoredump=enabled
		-Dptrace=enabled
		-Dsetjmp=enabled
		-Ddefault_library=$(usex static-libs both shared)
		$(meson_feature debug-frame debug_frame)
		$(meson_native_use_feature doc documentation)
		$(meson_feature lzma minidebuginfo)
		$(meson_feature zlib zlibdebuginfo)
		# Leave conservative_checks at its upstream default (enabled): it makes
		# the unwinder validate addresses before dereferencing them, so bad CFI
		# or a corrupt stack ends the unwind instead of faulting inside
		# libunwind, which typically happens in a crash handler.
		$(meson_use debug debug_logs)
	)

	meson_src_configure
}
