# Template file for 'mercury'
pkgname=mercury
version=22.01.8
revision=1
hostmakedepends="flex bison gettext texinfo"
short_desc="Logic/Functional Programming Language"
maintainer="Emily McDonough <emily@alaskanemily.net>"
license="GPL-2.0-only, custom:LGPL-2.0-only-linking-exception"
homepage="https://mercurylang.org/"
distfiles="https://dl.mercurylang.org/release/mercury-srcdist-${version}.tar.xz"
checksum=2adb69072caad1356d3757dea88f6440468123e5121f80a06b6d7029fc27e649
nocross="Mercury cannot be cross-compiled to different architectures"

CFLAGS="-Wno-array-bounds -fno-tree-slp-vectorize"

# asm_fast is preferable, but does not work on all architectures.
case "$XBPS_MACHINE" in
	*x86_64*|*arm*)
		_LLDS_GRADE=asm_fast ;;
	*i686*)
		_LLDS_GRADE=reg ;;
	ppc*)
		broken="i don't have time for this nonsense" ;;
	*)
		_LLDS_GRADE=none ;;
esac

_LIBGRADES="hlc.gc,hlc.gc.ssdebug,hlc.gc.memprof,${_LLDS_GRADE}.gc,${_LLDS_GRADE}.gc.stseg,${_LLDS_GRADE}.gc.decldebug.stseg,${_LLDS_GRADE}.gc.profdeep.stseg"

# The parallel grades have some issues on musl systems.
if [ "$XBPS_LIBC" != musl ] ; then
	_LIBGRADES="${_LIBGRADES},hlc.par.gc,${_LLDS_GRADE}.par.gc.stseg"
fi

do_configure() {
	# Prefer the low-level C grade as default, unless it is none.
	if [ "${_LLDS_GRADE}" != "none" ] ; then
		DEFAULT_GRADES="${_LLDS_GRADE}.gc.stseg"
	else
		DEFAULT_GRADES=hlc.gc
	fi

	./configure --prefix=/usr/ \
		--enable-new-mercuryfile-struct \
		--enable-gc-mmap --enable-gc-munmap \
		--without-readline --with-editline \
		--enable-libgrades="${_LIBGRADES}" --with-llds-base-grade="${_LLDS_GRADE}" --with-default-grade="${DEFAULT_GRADES}"
}

do_build() {
	# The Mercury makefiles do most of the actual compilation in the 'install'
	# rule. The 'DESTDIR' makefile variable is used to have the makefile
	# install to the build directory instead, and then copy the files in
	# do_install().
	make PARALLEL="${makejobs}" DESTDIR=$(pwd)
	make PARALLEL="${makejobs}" DESTDIR=$(pwd) install
}

do_install() {
	vmkdir usr/lib/mercury/lib
	vmkdir usr/lib/mercury/conf
	vman usr/share/man/man1/c2init.1
	vman usr/share/man/man1/mdb.1
	vman usr/share/man/man1/mercury_config.1
	vman usr/share/man/man1/mgnuc.1
	vman usr/share/man/man1/ml.1
	vman usr/share/man/man1/mmake.1
	vman usr/share/man/man1/mmc.1
	vman usr/share/man/man1/mtags.1
	for BIN in c2init canonical_grade mdb mdemangle mercury_compile mercury_config mfiltercc mfilterjavac mgnuc mkinit ml mmake mmc mtags ; do
		vbin usr/bin/$BIN
	done
	vcopy usr/lib/mercury/conf/Mercury.config usr/lib/mercury/conf/Mercury.config
	vcopy usr/lib/mercury/conf/mercury_conf.h usr/lib/mercury/conf/mercury_conf.h
	vcopy usr/lib/mercury/ints usr/lib/mercury/ints
	vcopy usr/lib/mercury/inc usr/lib/mercury/inc
	vcopy usr/lib/mercury/ints usr/lib/mercury/ints
	vcopy usr/lib/mercury/mdb usr/lib/mercury/mdb
	vcopy usr/lib/mercury/mmake usr/lib/mercury/mmake
	vcopy usr/lib/mercury/lib/lib*gc*.a usr/lib/mercury/lib
	for GRADE in $(echo $_LIBGRADES | sed 's/,/ /g') ; do
		# Skip pregen, we don't need that after bootstrapping.
		if test "$GRADE" != hlc.gc.pregen ; then
			vmkdir usr/lib/mercury/lib/$GRADE
			vmkdir usr/lib/mercury/modules/$GRADE
			vcopy usr/lib/mercury/lib/$GRADE/inc usr/lib/mercury/lib/$GRADE
			vcopy usr/lib/mercury/lib/$GRADE/lib*.a usr/lib/mercury/lib/$GRADE
			vcopy usr/lib/mercury/modules/$GRADE/*.init usr/lib/mercury/modules/$GRADE
			# Copy the shared libraries for profiling and debug grades.
			case $GRADE in
				*prof*|*debug*)
					vcopy usr/lib/mercury/lib/$GRADE/lib*.so usr/lib/mercury/lib/$GRADE
				;;
			esac
		fi
	done
}

mercury-libs_package() {
	short_desc+=" - runtime libraries"
	pkg_install() {
		vmkdir usr/lib/mercury/lib
		vcopy usr/lib/mercury/lib/lib*gc*.so usr/lib/mercury/lib
		vlicense COPYING.LIB
		for GRADE in $(echo $_LIBGRADES | sed 's/,/ /g') ; do
			# Ignore profiling and debug grades for the runtime libraries.
			# Also skip pregen, we don't need that after bootstrapping.
			case $GRADE in
				*prof*|*debug*|hlc.gc.pregen)
					;;
				*)
					vmkdir usr/lib/mercury/lib/$GRADE
					vcopy usr/lib/mercury/lib/$GRADE/lib*.so usr/lib/mercury/lib/$GRADE
					;;
			esac
		done
	}
}

mercury-tools_package() {
	depends="mercury-libs>=${version}_${revision}"
	short_desc+=" - extra tools"
	pkg_install() {
		vman usr/share/man/man1/mprof.1
		vman usr/share/man/man1/mprof_merge_runs.1
		for BIN in mcov mdprof mercury_profile mprof mprof_merge_runs mtc mtc_diff mtc_union ; do
			vbin usr/bin/$BIN
		done
	}
}
