# Template file for 'cargo'
pkgname=cargo
version=0.41.0
revision=1
wrksrc="cargo-${version}"
build_helper=rust
hostmakedepends="rust python curl cmake pkg-config"
makedepends="libcurl-devel libgit2-devel"
depends="rust"
short_desc="Rust package manager"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="MIT, Apache-2.0"
homepage="https://crates.io/"
_libgit2_ver=0.9.1
_git2_ver=0.10.1
distfiles="https://github.com/rust-lang/cargo/archive/${version}.tar.gz
 https://github.com/rust-lang/git2-rs/archive/libgit2-sys-${_libgit2_ver}.tar.gz"
checksum="77b2e5b5207ed30dd6e6af934663f0c4553968dce575f95ca7e141444b6e9704
 81e20fa9a0f6ea3e3a0e6458148f3dea4a89af87d34128d4375f2b7fd661a49c"
_cargo_dist_version=0.40.0
build_options="static"

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" cargo"
	makedepends+=" rust"
else
	_bootstrap_url="https://static.rust-lang.org/dist"

	case "$XBPS_MACHINE" in
		x86_64*|i686|ppc64le|ppc) ;;
		ppc*) _bootstrap_url="https://alpha.de.repo.voidlinux.org/distfiles" ;;
		*) broken="unsupported host: ${XBPS_MACHINE}" ;;
	esac

	distfiles+="
	 ${_bootstrap_url}/cargo-${_cargo_dist_version}-${RUST_BUILD}.tar.xz"

	case "$XBPS_MACHINE" in
		i686)
			checksum+="
			 d2a7ab28f0ff82a482ff24425f844b917a74059dbce550a96f0ab9fe1c131ef9"
			;;
		x86_64)
			checksum+="
			 7b9ba52c252964724f49aab49e42bec62fca929297ef058412c7e727b0794620"
			;;
		x86_64-musl)
			checksum+="
			 9c17f3f43c77e3cf002b3b54f871718b0a4fca30e83dca0a76a037d4943806c7"
			;;
		ppc64le)
			checksum+="
			 e420f7078262bd0735c6d368e0e46b064ea39fdbc86690932b86274616e838e6"
			;;
		ppc64le-musl)
			checksum+="
			 e930b7da1f3ae1e0d3568c06cd4cc5f68a61813a8c53adf0e97d9592f3e3425e"
			;;
		ppc64)
			checksum+="
			 d516be75b7cde33aa69db1e4fd99b222b868df3f642dada3bfed34d1db585380"
			;;
		ppc64-musl)
			checksum+="
			 af226341d50cebc5af608375b6b200a1c10e14139812dc77a928140fe5120be9"
			;;
		ppc)
			checksum+="
			 a2a64bba73102caadf819dd268f5710c978ece0efa05aaa89cbae45a5443e880"
			;;
		ppc-musl)
			checksum+="
			 bbbf7037590fd38c5d4b2e2ac7ddddcc13213d05885d7c1fc2eacbffa4d920f5"
			;;
	esac
fi

post_extract() {
	if [ -z "$CROSS_BUILD" ]; then
		mkdir -p target/snapshot
		cp ../cargo-${_cargo_dist_version}-${RUST_TARGET}/cargo/bin/cargo cargo
	fi

	mv ${XBPS_BUILDDIR}/git2-rs-libgit2-sys-${_libgit2_ver} .
	mv git2-rs-libgit2-sys-${_libgit2_ver}/libgit2-sys .
}

post_patch() {
	# but only use the patched libgit2 when not static; when static, bundled
	# libgit2 is used and this would not work (libgit2 sources are not there)
	if [ ! "$build_option_static" ]; then
		cat >> Cargo.toml <<- EOF
			[patch.crates-io]
			libgit2-sys = { path = './libgit2-sys' }
		EOF
	fi
}

do_build() {
	if [ "$build_option_static" ]; then
		unset LIBGIT2_SYS_USE_PKG_CONFIG
		unset LIBSSH2_SYS_USE_PKG_CONFIG
		export OPENSSL_STATIC=1
		# rust-openssl can not be linked static if pkg-config is used
		export OPENSSL_NO_PKG_CONFIG
		export OPENSSL_DIR="${XBPS_CROSS_BASE}/usr"
	fi

	if [ "$CROSS_BUILD" ]; then
		cargo="cargo"
	else
		cargo="./cargo"
	fi

	# gotta pin out libgit2-sys at the version
	$cargo update
	$cargo update --package git2 --precise ${_git2_ver}
	$cargo update --package libgit2-sys --precise ${_libgit2_ver}
	$cargo build --release $(vopt_if static --features="all-static")
}

do_install() {
	if [ "$CROSS_BUILD" ]; then
		vbin target/${RUST_TARGET}/release/cargo
	else
		vbin target/release/cargo
	fi
	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
}
