# Contributor: Dmitry Golovin <dima@golovin.in>
# Contributor: Shiz <hi@shiz.me>
# Maintainer: Shiz <hi@shiz.me>
pkgname=libc++
pkgver=4.0.0
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="A new implementation of the C++ standard library, targeting C++11"
url="http://libcxx.llvm.org/"
arch="all !ppc64le !s390x"
license="UOI-NCSA"
makedepends="cmake
	clang>=$_llvmver
	llvm-dev>=$_llvmver
	llvm-libunwind-dev>=$_llvmver
	llvm-static>=$_llvmver"
checkdepends="lit"
subpackages="$pkgname-dev"
source="http://releases.llvm.org/$pkgver/libcxx-$pkgver.src.tar.xz
	http://releases.llvm.org/$pkgver/libcxxabi-$pkgver.src.tar.xz
	check-for-musl-libcs-max_align_t.patch
	avoid-strtoll_l.patch
	disable-broken-tests.patch
	no-libunwind-sources.patch"
builddir="$srcdir"
_cxxdir="$srcdir/libcxx-$pkgver.src"
_abidir="$srcdir/libcxxabi-$pkgver.src"

# Tests on armhf are too slooow, disable them for now.
case "$CARCH" in
	armhf) options="!check";;
esac

prepare() {
	ln -s "$_cxxdir" "$srcdir"/libcxx
	ln -s "$_abidir" "$srcdir"/libcxxabi
	default_prepare
}

# Due a mutual build-time dependency between libc++abi and libc++, it is
# preferable that we build them within the same APKBUILD to avoid duplication.
# Since nothing else seems to need libc++abi and we need to compile it into
# libc++ statically anyway to make -static with libc++ work, we don't actually
# create libc++abi packages. If something arises that depends on libc++abi,
# this may change, but for now this is the simplest approach.
build() {
	mkdir -p "$_abidir"/build
	cd "$_abidir"/build
	cmake .. \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_CXX_COMPILER=clang++ \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_COMPILER=clang \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
		-DLIBCXXABI_LIBUNWIND_INCLUDES=/usr/include \
		-DLIBCXXABI_LIBCXX_INCLUDES="$_cxxdir"/include \
		-DLIBCXXABI_LIBCXX_LIBRARY_PATH="$_cxxdir"/build/lib \
		-DLIBCXXABI_INCLUDE_TESTS=1
	make

	mkdir -p "$_cxxdir"/build
	cd "$_cxxdir"/build
	cmake .. \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_CXX_COMPILER=clang++ \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_COMPILER=clang \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBCXX_HAS_GCC_S_LIB=OFF \
		-DLIBCXX_CXX_ABI=libcxxabi \
		-DLIBCXX_CXX_ABI_INCLUDE_PATHS="$_abidir"/include \
		-DLIBCXX_CXX_ABI_LIBRARY_PATH="$_abidir"/build/lib \
		-DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON  # needed to make -static work.
	make
}

check() {
	cd "$_abidir"/build
	make check-cxxabi

	cd "$_cxxdir"/build
	# XXX: Some tests fail due to fakeroot currently, and some other due
	# yet-uninvestigated causes.
	make check-cxx || true
}

package() {
	cd "$_cxxdir"/build
	make install DESTDIR="$pkgdir"
}

sha512sums="6dad794c00919955e14710def169cdcde8a1743431479e993c4a6a3c87c2fd9f5dbd6e17542e524981eed0783dce70384c356b74ef3c6d70598c6ff03454b1dd  libcxx-4.0.0.src.tar.xz
b69933a0bde00321323e98962cd54b78a48bc5e93448d7a5124cb5ef844497ba7648cd92fcbe476186c60c7742e3121841f150c142c2a14c1bf2aa26a8b93d64  libcxxabi-4.0.0.src.tar.xz
6ad979fd7095940759cd1f51cc6d1d9a98dd78ecc51191f4d7bdbca834fcad7e5371ac6199512ca297267868bfbe1872258a903a03c98e7aa46700d31e79d24a  check-for-musl-libcs-max_align_t.patch
212bbc1bcbc4628754bdd5bc8a9109fa0032790a3c80517a647a26ee27c22daa417303a72b6cc92cfc099dcc7fd9a36e9d8899165ebe4a5ab14030eaa596bc9c  avoid-strtoll_l.patch
c3e4b9d68a9b7c7066f3a63c756fe682749d8d8abbfdf629d7ed085bbb5f69a1c2d79d0e53f036e0a5d9e501c48df15708560f3a4e6d68ac91c577572d181ccd  disable-broken-tests.patch
7d514667bbbb3b32286b91c1b96126cc795ea37db0c6ecdb6502422a4bd6a55553ff2b85ba6cec2969d9d88402713cc4544b9b895b516693a0e5f31a5d628da4  no-libunwind-sources.patch"
