# Template file for 'rust'
# partially adapted from Alpine/Adélie's Rust APKBUILD
pkgname=rust
version=1.40.0
revision=1
_rust_dist_version=1.39.0
_cargo_dist_version=0.40.0
# Always make sure custom distfiles used for bootstrap are
# uploaded to https://alpha.de.repo.voidlinux.org/distfiles/
wrksrc="rustc-${version}-src"
hostmakedepends="cmake curl pkg-config python3 tar"
makedepends="libffi-devel ncurses-devel libxml2-devel zlib-devel llvm"
depends="rust-std"
short_desc="Safe, concurrent, practical systems language"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="MIT, Apache-2.0"
homepage="https://www.rust-lang.org/"
distfiles="https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz"
checksum=dd97005578defc10a482bff3e4e728350d2099c60ffcf1f5e189540c39a549ad
lib32disabled=yes
patch_args="-Np1"

build_options="static_llvm"

_bootstrap_dir="stage0-bootstrap"

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" cargo llvm"
	# These are required for building the buildhost's stage0/1
	hostmakedepends+=" libffi-devel libxml2-devel ncurses-devel zlib-devel"
else
	_bootstrap_url="https://static.rust-lang.org/dist"

	# 32-bit ppc needs custom bootstrap for now because of patches
	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}/rustc-${_rust_dist_version}-${RUST_BUILD}.tar.xz
	 ${_bootstrap_url}/rust-std-${_rust_dist_version}-${RUST_BUILD}.tar.xz
	 ${_bootstrap_url}/cargo-${_cargo_dist_version}-${RUST_BUILD}.tar.xz"

	# checksums must be specified per arch
	case "$XBPS_MACHINE" in
		i686)
			checksum+="
			 cb2e8afccfdc7e32fdbb1e106e3cedb0bd4ba1a6f060417f41c1e35a9ac8968d
			 e108d2f3ba7f5986e08cd7f2b295110243d1b715d5722d276aad7b0469418626
			 d2a7ab28f0ff82a482ff24425f844b917a74059dbce550a96f0ab9fe1c131ef9"
			;;
		x86_64)
			checksum+="
			 5b2a435a4c034615d70cfc383abe9924cbd1ffd4669caa55ce6539f22ed979ed
			 b629952ec9e3e750137d7b8a922de145c97c1dc372dd616e86e4e501da13910b
			 7b9ba52c252964724f49aab49e42bec62fca929297ef058412c7e727b0794620"
			;;
		x86_64-musl)
			checksum+="
			 bd3e2964d171eafc95668f675b521669b89abc32129214bb4c3b2509e4c66247
			 4ac886aab08eae7c67edc0be3ffd808a0a6fa599b9aa52f52f0a48fdb8b56435
			 9c17f3f43c77e3cf002b3b54f871718b0a4fca30e83dca0a76a037d4943806c7"
			;;
		ppc64le)
			checksum+="
			 542d2bd00f1af6b2108e052b8f9cfd7afdf787d47b313d7b67d8dcd3bfafe96c
			 e4ad2ed0a6de954788d27355a1f178000735bf2b7120f2aa3af49839ab405f16
			 e420f7078262bd0735c6d368e0e46b064ea39fdbc86690932b86274616e838e6"
			;;
		ppc64le-musl)
			checksum+="
			 aeb989553eb6b1e8a7473369d491c1ac4ff27a553743f19a042d759b1cbfbad9
			 d7bf5bab6cd80d387fa1b9d41dd5a5825fc5de520048cc89a8d621760c44e6df
			 e930b7da1f3ae1e0d3568c06cd4cc5f68a61813a8c53adf0e97d9592f3e3425e"
			;;
		ppc64)
			checksum+="
			 fa5fe636f502cd5a6af97281656dba59295d76e91158c19b59d6a970987fce8f
			 74f5deadb1052abf5475b3d7b204101a52337a89867daf3efde2adcd8dee78b2
			 d516be75b7cde33aa69db1e4fd99b222b868df3f642dada3bfed34d1db585380"
			;;
		ppc64-musl)
			checksum+="
			 f1fd45b8570bff03e22c70dcfc8cfa6e7657ab41ce082687f4bd294e08dad152
			 c3455841abfb9e59ae0709fe5f0fa171212e8628bf464214734eafb46f97c4c2
			 af226341d50cebc5af608375b6b200a1c10e14139812dc77a928140fe5120be9"
			;;
		ppc)
			checksum+="
			 ed91f1e60b56e6bd09b003fda0aef5b99e014663033f3b2ede44a51e5b574b13
			 2cb595dd95d5f557b3d36006ce6fe283f324e721ad8f92e1f7772cc261787cae
			 a2a64bba73102caadf819dd268f5710c978ece0efa05aaa89cbae45a5443e880"
			;;
		ppc-musl)
			checksum+="
			 3a83a55b11774af98f199ca47bf7429338a2f1e1df5fdbffbdee6bc5ffcae5c9
			 4b16d538a5a3ff8d85bd0a2bb28896ab170a340f40e40e06a8f59028285d57f6
			 bbbf7037590fd38c5d4b2e2ac7ddddcc13213d05885d7c1fc2eacbffa4d920f5"
			;;
	esac
fi

post_extract() {
	if [ -z "$CROSS_BUILD" ]; then
		mkdir -p "${wrksrc}/${_bootstrap_dir}"
		../rust-std-${_rust_dist_version}-${RUST_BUILD}/install.sh \
		 --destdir="${wrksrc}/${_bootstrap_dir}" --prefix=/ --disable-ldconfig
		../rustc-${_rust_dist_version}-${RUST_BUILD}/install.sh \
		 --destdir="${wrksrc}/${_bootstrap_dir}" --prefix=/ --disable-ldconfig
		# cargo may not be packaged like the rest
		cp ../cargo-${_cargo_dist_version}-${RUST_BUILD}/cargo/bin/cargo \
		 "${wrksrc}/${_bootstrap_dir}/bin"
	fi
}

# we need this because cargo verifies checksums of all files in vendor
# crates when it builds and gives us no way to override or update the
# file sanely... so just clear out the file list
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' vendor/$1/.cargo-checksum.json
}

post_patch() {
	rm -rf src/llvm-project
	if [ "$build_option_static_llvm" ]; then
		for x in ${FILESDIR}/patches/static-llvm/*.patch; do
			msg_normal "Applying patch $x\n"
			patch -sNp1 -i ${x}
		done
	fi

	# clear out all the checksum nonsense of patched vendor crates
	_clear_vendor_checksums libc
	_clear_vendor_checksums typenum
	_clear_vendor_checksums backtrace-sys
	_clear_vendor_checksums lzma-sys
}

do_configure() {
	local _rust_root _local_rebuild _use_debug _use_debug_rustc _use_rpath

	if [ "$CROSS_BUILD" ]; then
		_rust_root="/usr"
		_local_rebuild="true"
	else
		_rust_root="${wrksrc}/${_bootstrap_dir}"
		_local_rebuild="false"
	fi

	# disable rustc debug info as it bloats up rust-std significantly
	# set to 1 or 2 if you wish to have debug info for rustc, it won't
	# link on 32-bit platforms though (too big)
	_use_debug_rustc="0"

	# use stdlib debug level 2 for all platforms when generating debug
	if [ -n "$XBPS_DEBUG_PKGS" ]; then
		_use_debug="2"
	else
		_use_debug="0"
	fi

	# if true, the binaries will be built with rpath, so the binaries will be
	# usable directly from the build directory, this is not desirable for
	# packaging but is useful when building bootstrap binaries
	_use_rpath="false"

	cat > config.toml <<- EOF
		[build]
		build = "${RUST_BUILD}"
		host = [ "${RUST_TARGET}" ]
		target = [ "${RUST_TARGET}" ]
		cargo = "${_rust_root}/bin/cargo"
		rustc = "${_rust_root}/bin/rustc"
		submodules = false
		python = "python3"
		locked-deps = true
		vendor = true
		full-bootstrap = false
		local-rebuild = ${_local_rebuild}
		extended = false
		docs = false
		tools = []
		[install]
		prefix = "/usr"
		[rust]
		codegen-units = 1
		codegen-units-std = 1
		debug-assertions = false
		debuginfo-level = ${_use_debug}
		debuginfo-level-rustc = ${_use_debug_rustc}
		debuginfo-level-tests = 0
		backtrace = true
		incremental = false
		parallel-compiler = false
		channel = "stable"
		rpath = ${_use_rpath}
		verbose-tests = true
		dist-src = false
		jemalloc = false
		llvm-libunwind = false
		codegen-tests = false
		[dist]
		src-tarball = false
		[target.${RUST_BUILD}]
		llvm-config = "/usr/bin/llvm-config"
		crt-static = false
		cc = "${CC_host:-$CC}"
		cxx = "${CXX_host:-$CXX}"
		ar = "${AR_host:-$AR}"
		linker = "${CC_host:-$CC}"
	EOF

	if [ "$CROSS_BUILD" ]; then
		cat >> config.toml <<- EOF
			[target.${RUST_TARGET}]
			llvm-config = "/usr/bin/llvm-config"
			crt-static = false
			cc = "${CC}"
			cxx = "${CXX}"
			ar = "${AR}"
			linker = "${CC}"
		EOF
	fi
}

# Set the correct CFLAGS for the build host, we have to compile libbacktrace
# for it during stage1. Otherwise it attemps to use CFLAGS, which are the CFLAGS
# of the cross host.
do_build() {
	if [ ! "$build_option_static_llvm" ]; then
		export LLVM_LINK_SHARED=1
	fi
	export RUST_BACKTRACE=1
	export CARGO_HOME="$wrksrc/.cargo"
	export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
	# prevent sysroot from leaking in
	export RUSTFLAGS=""

	env CFLAGS_${RUST_BUILD}="${CFLAGS_host}" \
	 python3 x.py dist --jobs $XBPS_MAKEJOBS
}

do_check() {
	export RUST_BACKTRACE=1

	python3 x.py test ${makejobs} --no-doc --no-fail-fast \
		src/test/codegen \
		src/test/codegen-units \
		src/test/compile-fail \
		src/test/incremental \
		src/test/mir-opt \
		src/test/pretty \
		src/test/run-fail \
		src/test/run-fail/pretty \
		src/test/run-make \
		src/test/run-make-fulldeps \
		src/test/run-pass \
		src/test/run-pass/pretty \
		src/test/run-pass-fulldeps \
		src/test/run-pass-fulldeps/pretty \
		src/test/ui \
		src/test/ui-fulldeps
}

do_install() {
	vmkdir usr
	tar xf build/dist/rustc-${version}-${RUST_TARGET}.tar.gz \
	 -C "$DESTDIR/usr" --strip-components=2 --exclude=manifest.in
	tar xf build/dist/rust-std-${version}-${RUST_TARGET}.tar.gz \
	 -C "$DESTDIR/usr/lib" --strip-components=3 --exclude=manifest.in
	tar xf build/dist/rustc-dev-${version}-${RUST_TARGET}.tar.gz \
         -C "$DESTDIR/usr/lib" --strip-components=3 --exclude=manifest.in

	vlicense COPYRIGHT
	vlicense LICENSE-APACHE
	vlicense LICENSE-MIT

	cd ${DESTDIR}/usr/lib
	# symlinks instead of copies
	mv *.so rustlib/${RUST_TARGET}/lib
	ln -sf rustlib/${RUST_TARGET}/lib/*.so .
}

rust-doc_package() {
	short_desc+=" - documentation"
	archs=noarch
	pkg_install() {
		vmove usr/share/doc
	}
}

rust-std_package() {
	short_desc+=" - standard library"
	pkg_install() {
		vmove usr/lib/rustlib
	}
}
