# Template file for 'choosenim'
pkgname=choosenim
version=0.7.4
revision=3
hostmakedepends="git nim openssl-devel pkg-config"
makedepends="openssl-devel zlib-devel libarchive-devel"
depends="gcc"
short_desc="Nim toolchain installer"
maintainer="SolitudeSF <solitudesf@protonmail.com>"
license="MIT"
homepage="https://github.com/dom96/choosenim"
distfiles="${homepage}/archive/v${version}.tar.gz>choosenim-${version}.tar.gz
 https://github.com/genotrance/nimarchive/archive/v0.5.4.tar.gz>nimarchive-0.5.4.tar.gz"
checksum="0e78a59cfb3cf7926f8dff3a4215949ccfaecd6c6dddc3b2e062098b5da03edb
 c38e8c66c2eac2bc021d450f2b60d0e45c5355df963d0133930448639c0bd8d4"

case "$XBPS_TARGET_MACHINE" in
	ppc-musl) broken="ftbfs" ;;
esac

post_extract() {
	mv ../nimarchive-0.5.4 nimarchive
}

do_build() {
	local arch args
	case "$XBPS_TARGET_MACHINE" in
		aarch64*) arch=arm64;;
		arm*)     arch=arm;;
		mips*)    arch=mips;;
		ppc64le*) arch=powerpc64el;;
		ppc64*)   arch=powerpc64;;
		ppc*)     arch=powerpc;;
	esac
	LDFLAGS+=" $($PKG_CONFIG --libs-only-l --static openssl)"
	LDFLAGS+=" $($PKG_CONFIG --libs-only-l --static libarchive)"
	LDFLAGS+=" -lrt -ldl -lpthread"
	: >nim.cfg
	if [ -z "$CROSS_BUILD" ]; then
		cat >>nim.cfg <<-EOF
		gcc.options.always = "$CPPFLAGS $CFLAGS -pthread"
		gcc.options.linker = "$LDFLAGS"
		EOF
	fi
	if [ -n "$arch" ]; then
		cat >>nim.cfg <<-EOF
		$arch.linux.gcc.exe = "$CC"
		$arch.linux.gcc.linkerexe = "$CC"
		$arch.linux.gcc.options.always = "$CPPFLAGS $CFLAGS -pthread"
		$arch.linux.gcc.options.linker = "$LDFLAGS"
		EOF
		args="--cpu:$arch"
	fi
	ln -sf $XBPS_CROSS_BASE/usr/include/archive.h src
	ln -sf $XBPS_CROSS_BASE/usr/include/archive_entry.h src
	nimble -y build -d:release --passC:-flto "$args" --path:$wrksrc/nimarchive
}

do_install() {
	vbin bin/choosenim
	vlicense LICENSE
}
