#!/bin/busybox sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=libc++abi
PKGVERSION=8.0.1
PKGREVISION=1
PKGLICENSE="MIT CUSTOM"
PKGSUMMARY="low level support for the standard C++ library"
PKGDOWNLOAD="https://github.com/llvm/llvm-project/releases/download/llvmorg-$PKGVERSION/libcxxabi-$PKGVERSION.src.tar.xz"
PKGOPTIONS="COMPILER_RT LIBUNWIND"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${COMPILER_RT:=yes}
: ${LIBUNWIND:=yes}

if [ "$COMPILER_RT" = "yes" ]; then
	with_compiler_rt="-DLIBCXXABI_USE_COMPILER_RT=Off"
else
	with_compiler_rt="-DLIBCXXABI_USE_COMPILER_RT=On"
fi

if [ "$LIBUNWIND" = "yes" ]; then
	with_libunwind="-DLIBCXXABI_USE_LLVM_UNWINDER=On"
else
	with_libunwind="-DLIBCXXABI_USE_LLVM_UNWINDER=Off"
fi

build()
{
	rm -rf libcxxabi-$PKGVERSION.src
	tar xvf libcxxabi-$PKGVERSION.src.tar.xz
	cd libcxxabi-$PKGVERSION.src

	#
	# We build libc++abi against the system-wide libc++, so disable the
	# -nostdinc++ flag.
	#
	sed -i -e "/-nostdinc++/d" CMakeLists.txt
	mkdir build && cd build
	cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_CXX_COMPILER="$CXX" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_COMPILER="$CC" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DCMAKE_INSTALL_PREFIX= \
		-DLIBCXXABI_LIBCXX_INCLUDES=/include/c++/v1 \
		$with_compiler_rt \
		$with_libunwind
	make
	make install DESTDIR=$DESTDIR
	mkdir -p $DESTDIR/include
	cp ../include/* $DESTDIR/include
	cd ..

	cd ..
	rm -rf libcxxabi-$PKGVERSION.src
}
