# Template file for 'llvm15'
pkgname=llvm15
version=15.0.7
revision=4
build_wrksrc=llvm
build_style=cmake
_ext_suffix=".cpython-${py3_ver/./}-linux-${XBPS_TARGET_LIBC/glibc/gnu}.so"
configure_args="
 -DCMAKE_BUILD_TYPE=Release -Wno-dev
 -DENABLE_LINKER_BUILD_ID=YES
 -DLLDB_USE_SYSTEM_SIX=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
 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=NO
 -DLLDB_PYTHON_RELATIVE_PATH=lib/python${py3_ver}/site-packages
 -DLLDB_PYTHON_EXE_RELATIVE_PATH=bin/python${py3_ver}
 -DLLDB_PYTHON_EXT_SUFFIX=$_ext_suffix"
hostmakedepends="perl python3 zlib-devel libffi-devel swig"
makedepends="python3-devel zlib-devel elfutils-devel libffi-devel libedit-devel
 libxml2-devel binutils-devel"
depends="libllvm15>=${version}_${revision}"
short_desc="LLVM Compiler Infrastructure Project - Version 15"
maintainer="Daniel Martinez <danielmartinez@cock.li>"
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=8b5fcb24b4128cf04df1b0b9410ce8b1a729cb3c544e6da885d234280dedeac6
lib32disabled=yes
python_version=3

conflicts="llvm17"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	configure_args+=" -DLIBCXX_HAS_MUSL_LIBC=YES
	 -DCOMPILER_RT_BUILD_GWP_ASAN=OFF"
fi

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

_enabled_projects="clang;clang-tools-extra;compiler-rt;lld"

_lldb_enable=yes

case "$XBPS_TARGET_MACHINE" in
	ppc64le*) ;;
	ppc*|i686*|riscv64*) _lldb_enable=no ;;
esac


subpackages="clang-tools-extra15 clang15 clang-analyzer15 libclang15 libclang-cpp15
 libllvm15"

if [ "$_lldb_enable" = "yes" ]; then
	# XXX fails to cross compile due to python
	_enabled_projects+=";lldb"
	subpackages+=" lldb15 lldb15-devel"
fi

subpackages+=" lld15 lld15-devel"

configure_args+=" -DLLVM_ENABLE_PROJECTS=${_enabled_projects}"

post_patch() {
	if [ "$_lldb_enable" = "yes" ]; then
		if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
			vsed -i 's|__ptrace_request|int|g' \
				${wrksrc}/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
		fi
	fi

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

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" ;;
		armv*) configure_args+=" -DVOID_GCC_BUG_109180_WORKAROUND=ON" ;;
	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 \
			-DLLVM_ENABLE_PROJECTS=${_enabled_projects}
		make ${makejobs} -C utils/TableGen
		make ${makejobs} -C tools/clang/utils/TableGen
		make ${makejobs} -C tools/clang/tools/extra/clang-tidy/misc/ConfusableTable
		make ${makejobs} -C tools/clang/tools/extra/pseudo/gen
		[ "$_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"
		configure_args+=" -DCLANG_TIDY_CONFUSABLE_CHARS_GEN=${wrksrc}/llvm/build/HOST/bin/clang-tidy-confusable-chars-gen"
		configure_args+=" -DCLANG_PSEUDO_GEN=${wrksrc}/llvm/build/HOST/bin/clang-pseudo-gen"
		[ "$_lldb_enable" = "yes" ] && configure_args+=" -DLLDB_TABLEGEN=${wrksrc}/llvm/build/HOST/bin/lldb-tblgen"
		cd ../..
	fi

	case "$XBPS_TARGET_MACHINE" in
	arm*-musl|i686-musl)
		# sanitizer code is broken since it duplicates some libc bits
		configure_args+=" -DCOMPILER_RT_BUILD_SANITIZERS=OFF"
		;;
	esac

	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
}

clang-analyzer15_package() {
	pycompile_dirs="usr/share/scan-view"
	depends="clang15-${version}_${revision} python3 perl"
	short_desc+=" - A source code analysis framework"
	homepage="https://clang-analyzer.llvm.org/"
	conflicts="clang-analyzer17"
	pkg_install() {
		vmove "usr/bin/scan-*"
		vmove "usr/libexec/*analyzer"
		vmove "usr/share/scan-*"
		vmove usr/share/man/man1/scan-build.1
	}
}

clang-tools-extra15_package() {
	lib32disabled=yes
	depends="clang15-${version}_${revision} python3"
	short_desc+=" - Extra Clang tools"
	homepage="https://clang.llvm.org/extra/"
	conflicts="clang-tools-extra17"
	pkg_install() {
		vmove usr/bin/clang-apply-replacements
		vmove usr/bin/clang-change-namespace
		vmove usr/bin/clang-doc
		vmove usr/bin/clang-include-fixer
		vmove usr/bin/clang-move
		vmove usr/bin/clang-pseudo
		vmove usr/bin/clang-query
		vmove usr/bin/clang-reorder-fields
		vmove usr/bin/clang-tidy
		vmove usr/bin/clangd
		vmove usr/bin/find-all-symbols
		vmove usr/bin/hmaptool
		vmove usr/bin/modularize
		vmove usr/bin/pp-trace
		vmove usr/include/clang-tidy
		vmove "usr/lib/libclangApplyReplacements*"
		vmove "usr/lib/libclangChangeNamespace*"
		vmove "usr/lib/libclangDaemon*"
		vmove "usr/lib/libclangDoc*"
		vmove "usr/lib/libclangIncludeCleaner*"
		vmove "usr/lib/libclangIncludeFixer*"
		vmove "usr/lib/libclangMove*"
		vmove "usr/lib/libclangPseudo*"
		vmove "usr/lib/libclangReorderFields*"
		vmove "usr/lib/libclangQuery*"
		vmove "usr/lib/libclangTidy*"
		vmove "usr/lib/libclangd*"
		vmove "usr/lib/libfindAllSymbols*"
		vmove "usr/share/clang/*find-all-symbols*"
		vmove "usr/share/clang/*include-fixer*"
		vmove "usr/share/clang/*tidy*"
	}
}

clang15_package() {
	lib32disabled=yes
	depends="libstdc++-devel libgcc-devel  binutils ${XBPS_TARGET_LIBC}-devel
	 libclang15-${version}_${revision}"
	short_desc+=" - C language family frontend"
	homepage="https://clang.llvm.org/"
	conflicts="clang17"
	pkg_install() {
		vmove "usr/bin/*clang*"
		vmove usr/bin/c-index-test
		vmove usr/bin/diagtool
		vmove usr/include/clang
		vmove usr/include/clang-c
		vmove usr/lib/clang
		vmove usr/lib/cmake/clang
		vmove "usr/lib/libclang*.a"
		vmove "usr/lib/libclang*.so"
		vmove usr/share/clang
	}
}

libclang15_package() {
	short_desc+=" - C frontend runtime library"
	pkg_install() {
		vmove "usr/lib/libclang.so.*"
	}
}

libclang-cpp15_package() {
	short_desc+=" - C frontend runtime library (C++ interface)"
	pkg_install() {
		vmove "usr/lib/libclang-cpp.so.*"
	}
}

lld15_package() {
	lib32disabled=yes
	short_desc+=" - linker"
	homepage="https://lld.llvm.org"
	conflicts="lld17"
	pkg_install() {
		vmove "usr/bin/ld.lld*"
		vmove "usr/bin/ld64.lld*"
		vmove "usr/bin/lld*"
		vmove usr/bin/wasm-ld
	}
}

lld15-devel_package() {
	lib32disabled=yes
	short_desc+=" - linker - development files"
	homepage="https://lld.llvm.org"
	depends="lld15>=${version}_${revision}"
	conflicts="lld17-devel"
	pkg_install() {
		vmove usr/include/lld
		vmove usr/lib/cmake/lld
		vmove "usr/lib/liblld*a"
	}
}

lldb15_package() {
	lib32disabled=yes
	depends+=" python3-six"
	short_desc+=" - LLDB debugger"
	homepage="https://lldb.llvm.org/"
	conflicts="lldb17"
	pkg_install() {
		vmove "usr/bin/*lldb*"
		vmove "usr/lib/liblldb*so.*"
		vmove usr/lib/python*
	}
}

lldb15-devel_package() {
	lib32disabled=yes
	depends="lldb15>=${version}_${revision}"
	short_desc+=" - LLDB debugger - development files"
	conflicts="lldb17-devel"
	pkg_install() {
		vmove usr/include/lldb
		vmove "usr/lib/liblldb*.so"
	}
}

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