# Template file for 'cargo'
pkgname=cargo
version=0.30.0
revision=1
hostmakedepends="rust python curl cmake pkg-config"
makedepends="libcurl-devel libgit2-devel"
depends="rust"
short_desc="Rust package manager"
maintainer="Enno Boland <gottox@voidlinux.eu>"
license="MIT, Apache-2.0"
homepage="https://crates.io/"
distfiles="https://github.com/rust-lang/cargo/archive/${version}.tar.gz"
checksum=82f3ba49192b74b115b8aeca9db24deca022ee5e6351292611ab5b50e4455251
_cargo_dist_version=0.30.0

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" cargo"
	makedepends+=" rust"
else
	case "$XBPS_MACHINE" in
	x86_64-musl)
		distfiles+="
		 https://alpha.de.repo.voidlinux.org/distfiles/cargo-${_cargo_dist_version}-x86_64-unknown-linux-musl.tar.gz"
		checksum+="
		 e9344cbf3db669383cb3492d12bcd42d9130665f577be9ad4d2028011c7b1fae"
		;;
	x86_64)
		distfiles+="
		 https://static.rust-lang.org/dist/cargo-${_cargo_dist_version}-x86_64-unknown-linux-gnu.tar.gz"
		checksum+="
		 b6012ef67923b67b9b558628cc41dd90a688742bf23e9b46ad8839d898f1b0b1"
		;;
	i686)
		distfiles+="
		 https://static.rust-lang.org/dist/cargo-${_cargo_dist_version}-i686-unknown-linux-gnu.tar.gz"
		checksum+="
		 2fc5f7f5b128de456a9e1e3f5f4ef2c6d3169dbfe078931d9fcf38c9aa9b45ba"
		;;
	esac
fi

case $XBPS_TARGET_MACHINE in
	i686) _host_triplet=i686-unknown-linux-gnu;;
	x86_64) _host_triplet=x86_64-unknown-linux-gnu;;
	x86_64-musl) _host_triplet=x86_64-unknown-linux-musl;;
	armv6l) _host_triplet=arm-unknown-linux-gnueabihf;;
	armv6l-musl) _host_triplet=arm-unknown-linux-musleabihf;;
	armv7l) _host_triplet=armv7-unknown-linux-gnueabihf;;
	armv7l-musl) _host_triplet=armv7-unknown-linux-musleabihf;;
	aarch64) _host_triplet=aarch64-unknown-linux-gnu;;
	aarch64-musl) _host_triplet=aarch64-unknown-linux-musl;;
	*) broken="Please add your triplet to the cargo template!";;
esac

post_extract() {
	if [ -z "$CROSS_BUILD" ]; then
		mkdir -p target/snapshot
		case "$XBPS_MACHINE" in
			x86_64-musl) cp ../cargo cargo;;
			*) cp ../cargo-${_cargo_dist_version}-${_host_triplet}/cargo/bin/cargo cargo;;
		esac
	fi
}
do_configure() {
	# Actually use the config we set below
	export CARGO_HOME="$wrksrc/.cargo"

	mkdir -p .cargo

	cat >> .cargo/config <<EOF
[build]
jobs = ${makejobs#*j}
target = "${_host_triplet}"
[target.${_host_triplet}]
linker = "${CC}"
EOF
}

do_build() {
	export LIBGIT2_SYS_USE_PKG_CONFIG=yes
	export RUST_BACKTRACE=1
	export PKG_CONFIG_ALLOW_CROSS=1
	export RUSTFLAGS="--sysroot=${XBPS_CROSS_BASE}/usr"
	if [ "$CROSS_BUILD" ]; then
		cargo build --release
	else
		./cargo build --release
	fi
}

do_install() {
	vbin target/${_host_triplet}/release/cargo
	for f in src/etc/man/*.?; do
		vman $f
	done
	vinstall src/etc/cargo.bashcomp.sh 0644 usr/share/bash-completion/completions cargo
	vinstall src/etc/_cargo 0644 usr/share/zsh/site-functions

	vlicense LICENSE-APACHE
	vlicense LICENSE-MIT
	vlicense LICENSE-THIRD-PARTY
}
