# Template file for 'llvm12'
pkgname=llvm12
version=12.0.1
revision=4
build_wrksrc=llvm
build_style=cmake
configure_args="
 -DCMAKE_BUILD_TYPE=Release -Wno-dev
 -DENABLE_LINKER_BUILD_ID=YES
 -DLLDB_USE_SYSTEM_SIX=YES
 -DLIBCXX_CXX_ABI=libcxxabi
 -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=YES
 -DLIBCXXABI_USE_LLVM_UNWINDER=YES
 -DLIBCXXABI_ENABLE_STATIC_UNWINDER=YES
 -DLIBOMP_ENABLE_SHARED=YES
 -DLIBOMP_INSTALL_ALIASES=NO
 -DLLVM_INCLUDE_DOCS=YES
 -DLLVM_BUILD_DOCS=YES
 -DLLVM_ENABLE_SPHINX=YES
 -DSPHINX_WARNINGS_AS_ERRORS=NO
 -DLLVM_INSTALL_UTILS=YES
 -DLLVM_BUILD_LLVM_DYLIB=YES
 -DLLVM_LINK_LLVM_DYLIB=YES
 -DLLVM_ENABLE_RTTI=YES
 -DLLVM_ENABLE_FFI=YES
 -DLLVM_BINUTILS_INCDIR=/usr/include"
hostmakedepends="perl python3 zlib-devel libffi-devel swig python3-Sphinx
 python3-recommonmark python3-sphinx-automodapi"
makedepends="python3-devel zlib-devel elfutils-devel libffi-devel libedit-devel
 libxml2-devel binutils-devel"
depends="libllvm12>=${version}_${revision}"
short_desc="Low Level Virtual Machine"
maintainer="Orphaned <orphan@voidlinux.org>"
license="Apache-2.0"
homepage="https://www.llvm.org"
distfiles="https://github.com/llvm/llvm-project/releases/download/llvmorg-${version}/llvm-project-${version}.src.tar.xz"
checksum=129cb25cd13677aad951ce5c2deb0fe4afc1e9d98950f53b51bdcfb5a73afa0e
lib32disabled=yes
python_version=3

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	configure_args+=" -DLIBCXX_HAS_MUSL_LIBC=YES"
	makedepends+=" libexecinfo-devel"
	depends+=" libexecinfo-devel"
fi

# "operand out of range" assembler failures
case "$XBPS_TARGET_MACHINE" in
	ppc64*) ;;
	ppc*) configure_args+=" -DCLANG_ENABLE_CLANGD=OFF" ;;
esac

post_patch() {
	# update config.guess for better platform detection
	cp $XBPS_COMMONDIR/environment/configure/automake/config.guess \
		${wrksrc}/llvm/cmake

	# fix linker failures on some archs
	vsed -i 's,check_library_exists(gcc_s .*,set(LIBCXXABI_HAS_GCC_S_LIB ON),' \
		${wrksrc}/libcxxabi/cmake/config-ix.cmake
	vsed -i 's,check_library_exists(gcc .*,set(LIBCXXABI_HAS_GCC_LIB ON),' \
		${wrksrc}/libcxxabi/cmake/config-ix.cmake

	# need libssp_nonshared on some musl platforms (because of nodefaultlibs)
	case "$XBPS_TARGET_MACHINE" in
		ppc64*) ;;
		ppc*-musl|i686-musl|mips*-musl)
			vsed -i 's,^# Setup flags.$,add_library_flags(ssp_nonshared),' \
				${wrksrc}/libunwind/src/CMakeLists.txt
			vsed -i 's,^# Setup flags.$,add_library_flags(ssp_nonshared),' \
				${wrksrc}/libcxxabi/src/CMakeLists.txt
			vsed -i 's,#ssp,,' ${wrksrc}/libcxx/CMakeLists.txt
			;;
	esac

	case "$XBPS_TARGET_MACHINE" in
		x86_64-musl) # some sanitizer currently only on x86_64 stuff needs backtrace
			vsed -i 's,# Set common link flags.,list(APPEND SANITIZER_COMMON_LINK_LIBS execinfo),' \
				${wrksrc}/compiler-rt/CMakeLists.txt
			;;
		arm*-musl|i686-musl) # sanitizer code is broken since it duplicates some libc bits
			vsed -i 's/set(COMPILER_RT_HAS_SANITIZER_COMMON TRUE)/set(COMPILER_RT_HAS_SANITIZER_COMMON FALSE)/' \
				${wrksrc}/compiler-rt/cmake/config-ix.cmake
			;;
	esac
}

pre_configure() {
	local triplet

	# Vastly reduce size of debugging symbols:
	CFLAGS=${CFLAGS/ -g/ -g1}
	CXXFLAGS=${CXXFLAGS/ -g/ -g1}

	# since gcc9, the build likes to blow up for ppc32 apparently because
	# of clang being too large for a 24-bit relative call to the PLT, so
	# optimize for size instead
	case "$XBPS_TARGET_MACHINE" in
		ppc64*) ;;
		mips*-musl|ppc*) configure_args+=" -DVOID_CXX_OPT_FLAGS=-Os" ;;
	esac

	if [ "$CROSS_BUILD" ]; then
		msg_normal "Building host tblgen\n"
		mkdir -p build/HOST
		cd build/HOST
		CC="$BUILD_CC" CXX="$BUILD_CXX" CFLAGS="$BUILD_CFLAGS" \
			CXXFLAGS="$BUILD_CXXFLAGS" LDFLAGS="$BUILD_LDFLAGS" \
			cmake ../.. -DCMAKE_BUILD_TYPE=Release
		make ${makejobs} -C utils/TableGen
		make ${makejobs} -C tools/clang/utils/TableGen
		[ "$_lldb_enable" = "yes" ] && make ${makejobs} -C tools/lldb/utils/TableGen
		configure_args+=" -DLLVM_TABLEGEN=${wrksrc}/llvm/build/HOST/bin/llvm-tblgen"
		configure_args+=" -DCLANG_TABLEGEN=${wrksrc}/llvm/build/HOST/bin/clang-tblgen"
		cd ../..
	fi

	case "$XBPS_TARGET_MACHINE" in
		i686*) _arch="X86";;
		x86_64*) _arch="X86";;
		armv5*) _arch="Armv5te";;
		armv6*) _arch="Armv6";;
		armv7*) _arch="Armv7";;
		aarch64*) _arch="AArch64";;
		mips*) _arch="Mips";;
		ppc*) _arch="PowerPC";;
		riscv64*) _arch="RISCV64";;
	esac

	triplet=${XBPS_CROSS_TRIPLET:-$XBPS_TRIPLET}

	configure_args+=" -DLLVM_TARGET_ARCH=${_arch}"
	configure_args+=" -DLLVM_HOST_TRIPLE=${triplet}"
	configure_args+=" -DLLVM_DEFAULT_TARGET_TRIPLE=${triplet}"
}

do_install() {
	cd build
	cmake -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr -P cmake_install.cmake

	# Required for multilib.
	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		for _header in llvm-config; do
			mv ${DESTDIR}/usr/include/llvm/Config/${_header}{,-64}.h
			vinstall ${FILESDIR}/llvm-Config-${_header}.h 644 \
				usr/include/llvm/Config ${_header}.h
		done
	fi

	# Remove llvm-config-host in cross builds.
	if [ "$CROSS_BUILD" ]; then
		rm -f ${DESTDIR}/usr/bin/llvm-config-host
	fi
}

libllvm12_package() {
	short_desc+=" - runtime library"
	pkg_install() {
		vmove "usr/lib/libLLVM-*.so*"
	}
}
