# Contributor Travis Tilley <ttilley@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=compiler-rt
# Note: Update together with llvm.
pkgver=5.0.1
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="LLVM compiler-rt runtime libraries"
arch="all !ppc64le !s390x"
url="http://llvm.org/"
license="UOI-NCSA"
makedepends="
	cmake
	linux-headers
	clang
	llvm-dev>=$_llvmver
	llvm-static>=$_llvmver
	python2
	"
checkdepends="llvm-test-utils>=$_llvmver"
source="https://llvm.org/releases/$pkgver/$pkgname-$pkgver.src.tar.xz"
builddir="$srcdir/$pkgname-$pkgver.src"

build() {
	mkdir -p "$builddir"/build
	cd "$builddir"/build

	# compiler-rt uses llvm intrinsic types for testing
	# and therefore requires clang
	cmake .. \
		-DCMAKE_C_COMPILER=clang \
		-DCMAKE_CXX_COMPILER=clang++ \
		\
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_C_FLAGS_MINSIZEREL_INIT="$CFLAGS" \
		-DCMAKE_CXX_FLAGS_MINSIZEREL_INIT="$CXXFLAGS" \
		-DCMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT="$LDFLAGS" \
		-DCMAKE_INSTALL_PREFIX=/usr \
		\
		-DCOMPILER_RT_INCLUDE_TESTS=ON \
		-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
		-DCOMPILER_RT_BUILD_XRAY=OFF \
		-DLIT_COMMAND="/usr/bin/lit"
	make
}

check() {
	cd "$builddir"/build
	# remove test that does not work with PaX kernel
	rm ../test/builtins/Unit/enable_execute_stack_test.c \
		../test/builtins/Unit/clear_cache_test.c
	make check-compiler-rt
}

package() {
	cd "$builddir"/build

	make DESTDIR="$pkgdir" install

	cd "$pkgdir"

	mkdir -p usr/lib/clang
	case "$CARCH" in
		ppc64le | s390x) ;;
		*) mv usr/lib/linux usr/lib/clang/$pkgver;;
	esac
}

sha512sums="61e23775db78bdeed3003a509b45ae609287db31302fed186f3210d481ecf062d1828571e63482215ab00ff732df8f3fa6c77334dfb677e4b76eb555d476b8c4  compiler-rt-5.0.1.src.tar.xz"
