# Template file for 'giac'
pkgname=giac
version=2.0.0.21
revision=1
build_style=gnu-configure
configure_args="--disable-micropy --disable-quickjs"
hostmakedepends="automake gettext-devel libtool"
makedepends="fltk-devel gmp-devel gsl-devel libjpeg-turbo-devel
 libpng-devel readline-devel mpfr-devel pari-devel
 $(vopt_if openblas 'openblas-devel' 'lapack-devel')"
depends="desktop-file-utils hicolor-icon-theme"
short_desc="Free computer algebra system"
maintainer="Gonzalo Tornaría <tornaria@cmat.edu.uy>"
license="GPL-3.0-or-later"
homepage="https://www-fourier.univ-grenoble-alpes.fr/~parisse/giac.html"
changelog="https://www-fourier.univ-grenoble-alpes.fr/~parisse/install_en#new"
distfiles="https://www-fourier.univ-grenoble-alpes.fr/~parisse/debian/dists/stable/main/source/giac_${version%.*}-${version##*.}.tar.gz"
checksum=3e7fa1c281a21ab74ed66ef247faffe5a105bc11be1f7715f31b4368ce8dcbc8

# need more than 4*65536 stack, see try_parse() in gen.cc line 11812
LDFLAGS="-Wl,-z,stack-size=2097152"

build_options="openblas"

case $XBPS_TARGET_MACHINE in
	# avoid numerical noise caused by extended-precision of registers
	i686*) CXXFLAGS="-mfpmath=sse -msse2" ;;
esac

case "$XBPS_TARGET_MACHINE" in
	x86_64*|i686*|aarch64*|armv[67]*|ppc64*)
		# Prefer accelerated routines where available
		build_options_default="openblas"
		;;
	*) ;;
esac

pre_configure() {
	# use full version in banner -- upstream hides last component
	vsed -i configure.ac \
		-e '/m4_define(\[giac_micro_version]/ s/\[0]/[0-'${version##*.}']/'
	# add missing source file
	vsed -e 's/^libgiac_la_SOURCES = /&sha256.cc /' -i src/Makefile.am
	autoreconf -fi
	if [ "$build_option_openblas" ]; then
		export BLAS_LIBS=-lopenblas
		export BLAS_CFLAGS=-I/usr/include/openblas
		export LAPACK_LIBS=-lopenblas
		export LAPACK_CFLAGS=-I/usr/include/openblas
	fi
}

post_install() {
	# remove duplicates
	rm -r ${DESTDIR}/usr/share/doc

	echo "giac: check that '#include <giac/giac.h>' works (see #45708)"
	echo '#include <giac/giac.h>' |
		${CC} -c -isystem "${DESTDIR}/usr/include" -xc++ - -o /dev/null
}

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

giac-devel_package() {
	short_desc+=" - development files"
	depends="${sourcepkg}>=${version}_${revision} gmp-devel"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
	}
}

giac-doc_package() {
	short_desc+=" - Documentation and Examples"
	pkg_install() {
		vmove "usr/share/giac/doc"
		vmove "usr/share/giac/examples"
		find ${PKGDESTDIR} -name "Makefile*" -delete
	}
}
