# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=openmp
pkgver=18.1.8
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="LLVM OpenMP Runtime Library"
url="https://openmp.llvm.org/"
# s390x: LIBOMP_ARCH = UnknownArchitecture
# armhf: doesn't match arm baseline
arch="all !armhf !s390x"
license="Apache-2.0"
depends_dev="
	$pkgname=$pkgver-r$pkgrel
	"
makedepends="
	clang
	cmake
	elfutils-dev
	libffi-dev
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	perl
	samurai
	"
checkdepends="llvm$_llvmver-test-utils"
subpackages="$pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/openmp-${pkgver//_/}.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/cmake-${pkgver//_/}.src.tar.xz
	"
builddir="$srcdir/$pkgname-${pkgver//_/}.src"
options="!check" # todo

case "$CARCH" in
aarch64|ppc64le|x86_64)
	depends_dev="
		$depends_dev
		$pkgname-bitcode=$pkgver-r$pkgrel
		"
	subpackages="
		$subpackages
		$pkgname-bitcode
		libomptarget
		libomptarget-rtl-cuda
		libomptarget-rtl-amdgpu
		libomptarget-rtl
		"
	;;
riscv64)
	depends_dev="
		$depends_dev
		$pkgname-bitcode=$pkgver-r$pkgrel
		"
	subpackages="
		$subpackages
		$pkgname-bitcode
		libomptarget
	"
esac

prepare() {
	default_prepare
	mv "$srcdir"/cmake-${pkgver//_/}.src "$srcdir"/cmake
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	CC=clang \
	CXX=clang++ \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIBOMP_INSTALL_ALIASES=OFF \
		-DCLANG_TOOL=/usr/bin/clang \
		-DLINK_TOOL=/usr/bin/llvm-link \
		-DOPT_TOOL=/usr/bin/opt \
		-DPACKAGER_TOOL=/usr/bin/clang-offload-packager \
		-DOPENMP_LLVM_TOOLS_DIR=/usr/lib/llvm$_llvmver/bin \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	cmake --build build --target check-openmp
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	rm -f "$pkgdir"/usr/lib/libarcher_static.a
}

libomptarget() {
	amove usr/lib/libomptarget.so.*
}

rtl() {
	amove usr/lib/libomptarget.rtl.*.so.*
}

amdgpu() {
	amove usr/lib/libomptarget.rtl.amdgpu.so.*
}

cuda() {
	amove usr/lib/libomptarget.rtl.cuda.so.*
}

bitcode() {
	amove usr/lib/libomptarget*.bc
}

sha512sums="
7c2ca736524fb741112be247ac6be39cfe1dc92381c5e2997d97044ab9705c224ae5eabcf43b59cdec9a715a14227c6fb02cb2d1829ebc47b82d3af6e4d197d3  openmp-18.1.8.src.tar.xz
e02243b491f9e688db28d7b53270fcf87debf09d3c95b136a7c7b96e26890de68712c60a1e85f5a448a95ad8c81f2d8ae77047780822443bbe39f1a9e6211007  cmake-18.1.8.src.tar.xz
"
