# Template file for 'cargo'
pkgname=cargo
version=0.45.0
revision=2
wrksrc="cargo-${version}"
build_helper=rust
hostmakedepends="rust python curl cmake pkg-config"
makedepends="libcurl-devel"
depends="rust"
short_desc="Rust package manager"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="MIT, Apache-2.0"
homepage="https://crates.io/"
distfiles="https://github.com/rust-lang/cargo/archive/${version}.tar.gz"
checksum=304b2abb13ca0219a3485466625f2a38e6011f898199272ee76ad03d4c8c68b0
replaces="cargo-tree>=0"
_cargo_dist_version=0.44.0
_curl_sys_ver=0.4.35
_curl_ver=7.71.1

build_options="static bindist"
desc_option_bindist="Generate a tarball for bootstrap"

if [ -n "$build_option_static" -o -n "$build_option_bindist" ]; then
	_build_static=yes
fi

if [ -z "$_build_static" ]; then
	makedepends+=" libgit2-devel"
else
	distfiles+="
	 https://github.com/alexcrichton/curl-rust/archive/curl-sys-${_curl_sys_ver}.tar.gz
	 https://github.com/curl/curl/releases/download/curl-${_curl_ver//./_}/curl-${_curl_ver}.tar.gz"
	checksum+="
	 305eba92fcc5005864b429f7b7801a96af2cb8cfeb57701c3047f679e33970d2
	 59ef1f73070de67b87032c72ee6037cedae71dcb1d7ef2d7f59487704aec069d"
fi

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*) _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+="
			 c7b4045b2001be2e97b0b08dfaee02097aea0e8a9983b42585d733cfa1e69683"
			;;
		x86_64)
			checksum+="
			 84ba23058542691dae8157beec6a7679a420643eb6a708134bb1dfb01cbdebea"
			;;
		x86_64-musl)
			checksum+="
			 653cb60214d6f1000c9a7775111dccd093c0118e7f7bf5f2f1f53a7e5e2420b3"
			;;
		ppc64le)
			checksum+="
			 4f90cd6cbcf9e9d6ba11830e093092e4e6c3d156ffde2e729933869b7a039ff9"
			;;
		ppc64le-musl)
			checksum+="
			 a85e8c9714a8bb7c17cd23993c677f51d9e0d6f5e85ab10f53ec1639b19bbbd4"
			;;
		ppc64)
			checksum+="
			 983e2fbec9c4bfe66bab37ad64cdd5b47918378e421d9bf639e938f120650199"
			;;
		ppc64-musl)
			checksum+="
			 f974893d1cac6c799345bece08c7f796eb50e1a77c4b9bc2a6451d161a57c660"
			;;
		ppc)
			checksum+="
			 48f4419300fe758ebe024e6449a5ca1dff07803047a815b3d3909170baa7adcc"
			;;
		ppc-musl)
			checksum+="
			 bbb93a757e75d09cb5f6d6dd837e32212acd8888426406352eccf52f329386f1"
			;;
	esac
fi

# since curl-sys is somehow totally broken, we need to work around it
#
# the problem is basically that at least with curl-sys 0.4.35, the static-curl
# option will make the build.rs copy curl source files from inside curl/ in the
# source tree, but the directory is empty; there is logic in the build.rs that
# checks for existence of '.git' inside curl/, and if it doesn't exist, it
# runs 'git submodule update', which is supposed to clone a copy of curl into
# the source tree from git, but a curl-sys release tarball is not a git repo
# and does not contain .git or .gitmodules at all, so that just fails anyway
#
# so work around the whole mess by fetching our own curl-sys, our own copy of
# curl itself, then arrange things appropriately, create the .git inside curl/
# to fool build.rs into not trying to run git, and then patch Cargo.toml inside
# cargo to use our patched tree...
post_extract() {
	if [ -z "$CROSS_BUILD" ]; then
		mkdir -p target/snapshot
		cp ../cargo-${_cargo_dist_version}-${RUST_TARGET}/cargo/bin/cargo cargo
	fi
	if [ -n "$_build_static" ]; then
		mv ${XBPS_BUILDDIR}/curl-rust-curl-sys-${_curl_sys_ver} .
		mv curl-rust-curl-sys-${_curl_sys_ver}/curl-sys .
		rm -rf curl-sys/curl
		mv ${XBPS_BUILDDIR}/curl-${_curl_ver} curl-sys/curl
		mkdir curl-sys/curl/.git
	fi
}

# see above
post_patch() {
	[ -z "$_build_static" ] && return 0
	cat >> Cargo.toml <<- EOF
		[patch.crates-io]
		curl-sys = { path = './curl-sys' }
	EOF
}

do_build() {
	local cargo cargs

	if [ -n "$_build_static" ]; then
		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

	if [ -n "$_build_static" ]; then
		cargs+=" --features=all-static"
		$cargo update
		$cargo update --package curl-sys --precise ${_curl_sys_ver}
	fi

	$cargo build --release ${cargs}
}

do_install() {
	local cbin="target/release/cargo"
	if [ "$CROSS_BUILD" ]; then
		cbin="target/${RUST_TARGET}/release/cargo"
	fi

	if [ "$build_option_bindist" ]; then
		local dest="cargo-${version}-${RUST_TARGET}"
		mkdir -p ${XBPS_SRCDISTDIR}/distfiles ${dest}/cargo/bin
		install -m 0755 ${cbin} ${dest}/cargo/bin
		install -m 0644 LICENSE-APACHE LICENSE-MIT LICENSE-THIRD-PARTY ${dest}
		bsdtar cvJf ${dest}.tar.xz ${dest}
		install -m 0644 ${dest}.tar.xz ${XBPS_SRCDISTDIR}/distfiles
		exit 1
	fi

	vbin ${cbin}
	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
}
