# Contributor: Eric Molitor <eric@molitor.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=llvm-runtimes
# Note: Update together with llvm.
pkgver=20.1.8
_llvmver=${pkgver%%.*}
pkgrel=0
pkgdesc="LLVM Runtimes"
url="https://llvm.org/"
arch="all"
license="Apache-2.0"
makedepends="
	clang
	cmake
	linux-headers
	llvm$_llvmver-dev
	llvm$_llvmver-static
	python3
	samurai
	"
subpackages="
	libc++:libcxx
	libc++-static:libcxx_static
	libc++-dev:libcxx_dev
	compiler-rt:rt
	llvm-libunwind:libunwind
	llvm-libunwind-static:libunwind_static
	llvm-libunwind-dev:libunwind_dev
	"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/llvm-project-${pkgver//_/}.src.tar.xz
	armv6-arch.patch.noauto
	compiler-rt-lsan-dtp-offset.patch
	compiler-rt-ppc-fixes.patch
	compiler-rt-sanitizer-supported-arch.patch
	libunwind-link-libssp.patch
	fix-msan-with-musl.patch
	"
builddir="$srcdir/llvm-project-${pkgver//_/}.src"
options="!check"

case "$CARCH" in
# Sanitizers are broken on other arches.
# Keep in sync with compiler-rt-sanitizer-supported-arch.patch.
aarch64|ppc64le|x86_64|riscv64|loongarch64)
	_build_sanitizers='ON'
	;;
*)
	_build_sanitizers='OFF'
	;;
esac

prepare() {
	default_prepare

	case "$CARCH" in
	armhf)
		patch -Np1 < "$srcdir"/armv6-arch.patch.noauto
		;;
	esac
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="
			-DCMAKE_SYSTEM_NAME=Linux
			-DCMAKE_HOST_SYSTEM_NAME=Linux
			-DLIBUNWIND_SYSROOT=$CBUILDROOT
			"
	fi

	CC=clang \
	CXX=clang++ \
	CFLAGS="$CFLAGS -DNDEBUG -DSANITIZER_CAN_USE_PREINIT_ARRAY=0" \
	CXXFLAGS="$CXXFLAGS -DNDEBUG -DSANITIZER_CAN_USE_PREINIT_ARRAY=0" \
	cmake -B build -G Ninja -Wno-dev -S runtimes \
		-DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind;libcxx;libcxxabi" \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBUNWIND_HAS_NODEFAULTLIBS_FLAG=OFF \
		-DCOMPILER_RT_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DCOMPILER_RT_BUILD_SANITIZERS=$_build_sanitizers \
		-DCOMPILER_RT_INSTALL_PATH="/usr/lib/llvm$_llvmver/lib/clang/$_llvmver" \
		-DCOMPILER_RT_BUILD_GWP_ASAN=OFF \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		$crossopts
	cmake --build build
}

package() {
	# create target triple dir and a relative symlink to it
	local clangruntimedir="$(clang -print-runtime-dir)"
	install -dm0755 "$pkgdir"/"$clangruntimedir"
	ln -s "${clangruntimedir##*/}" "$pkgdir"/"${clangruntimedir%/*}"/linux

	DESTDIR="$pkgdir" cmake --install build

	install -dm0755 "$pkgdir"/usr/include/mach-o
	cp libunwind/include/*.h "$pkgdir"/usr/include/
	cp libunwind/include/mach-o/*.h "$pkgdir"/usr/include/mach-o/
}

libunwind() {
	pkgdesc="LLVM libunwind library"
	depends="!libunwind-dev"

	amove usr/lib/libunwind.so.*
}

libunwind_static() {
	pkgdesc="LLVM libunwind library (static)"

	amove usr/lib/libunwind.a
}

libunwind_dev() {
	pkgdesc="LLVM libunwind library (development files)"

	amove usr/lib/libunwind.so
	amove usr/include
}

rt() {
	pkgdesc="LLVM compiler-rt runtime libraries"
	# prefix with 0.0 for easy migration to clang-rtlib when this ver no longer tracks default Clang
	provides="clang$_llvmver-rtlib=0.0.$pkgver-r$pkgrel"

	amove usr/lib/llvm$_llvmver/lib/clang/$_llvmver
}

libcxx() {
	pkgdesc="LLVM libc++ library"

	amove usr/lib/libc++*.so.*
}

libcxx_static() {
	pkgdesc="LLVM libc++ library (static libs)"

	amove usr/lib/libc++*.a
}

libcxx_dev() {
	pkgdesc="LLVM libc++ library (development files)"

	amove usr/lib/libc++*.so
	amove usr/include/c++
}

sha512sums="
f330e72e6a1da468569049437cc0ba7a41abb816ccece7367189344f7ebfef730f4788ac7af2bef0aa8a49341c15ab1d31e941ffa782f264d11fe0dc05470773  llvm-project-20.1.8.src.tar.xz
ed30960bc5dea6d611a691e12949ddff1346fb5ba0ff36741496bf36442468f3724bb98526d230a97e58848bef703d6b793db27e254d927004f1903e3f2816ed  armv6-arch.patch.noauto
5830e0738e817aba515b2d5600a11a45f8e93ad9b39aae2b1561d7dd4dcea9361c66d7295aef0ae0728b23092643dc0affdfd4215f173ca5552c28ca072c731a  compiler-rt-lsan-dtp-offset.patch
248f845f46897403fef5ab503d2ffae5c727a716b4353373548384bf5b5d90f350537291d36bfb62ca0cfee40d373b8d5306856f20dac5b6f5a27e6256498c42  compiler-rt-ppc-fixes.patch
7754a0b6d5d65bc7bcc35d8d16d43c21e202a068ae729508d2d00a3e32b88483763666a9ec3130f8be4cefd59aee30f2bd46f07e6bef0519084c05a96342fdcc  compiler-rt-sanitizer-supported-arch.patch
98eeabdeb08d6c9ed767cfd8a9ed29dc6732cd87235395b51245081299c7367194fb533e1c064249a32a98da68ae396f64e304a97a2d2b7dbdb874f77da160df  libunwind-link-libssp.patch
0a4f0b5ae82f93387e8880c6e293eef9234f0cb4dadf7c52846f1a45612b931d2b460579d08d48548de9a7e6372b75f95e05e32683a60911a3d48f567cd4808b  fix-msan-with-musl.patch
"
