# Template file for 'rust'
# partially adapted from Alpine/Adélie's Rust APKBUILD
#
# Permission to use rust and cargo trademark is granted.
# See: https://github.com/rust-lang/core-team/issues/4
#
# Always make sure custom distfiles used for bootstrap are
# uploaded to https://repo-default.voidlinux.org/distfiles/
#
pkgname=rust
version=1.85.0
revision=1
_llvmver=19
hostmakedepends="curl pkg-config python3 tar cargo-bootstrap"
makedepends="libffi-devel ncurses-devel libxml2-devel zlib-devel llvm${_llvmver}-devel"
depends="rust-std gcc"
short_desc="Safe, concurrent, practical systems language"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="Apache-2.0 OR MIT"
homepage="https://www.rust-lang.org/"
distfiles="https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz"
checksum=2f4f3142ffb7c8402139cfa0796e24baaac8b9fd3f96b2deec3b94b4045c6a8a
lib32disabled=yes
make_check=no # CBA for now
python_version=3 # needed for python files in rust-src

build_helper="qemu"
build_options="bindist"
desc_option_bindist="Generate tarballs for bootstrap"

if [ "$XBPS_LIBC" = "musl" ]; then
	hostmakedepends+=" libexecinfo-devel"
fi
if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" libexecinfo-devel"
fi

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" rust llvm${_llvmver}-devel"
	# These are required for building the buildhost's stage0/1
	hostmakedepends+=" libffi-devel libxml2-devel ncurses-devel zlib-devel"
else
	hostmakedepends+=" rust-bootstrap"
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() {
	if [ "$build_option_bindist" ]; 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-0.2.169

	# pre-configure 01-override-config overwrites vendor files
	_clear_vendor_checksums onig_sys-69.8.1
}

do_configure() {
	local _local_rebuild _debug _debug_rustc _rpath _docs _llvm_shared
	local _cross_args

	if [ "$CROSS_BUILD" ]; then
		_local_rebuild="enable"
	else
		_local_rebuild="disable"
	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)
	_debug_rustc="0"
	# just disable debug for now
	_debug="0"

	# 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
	if [ "$build_option_bindist" ]; then
		_rpath="enable"
		_docs="disable"
		_llvm_shared="disable"
	else
		_rpath="disable"
		_docs="enable"
		_llvm_shared="enable"
	fi

	if [ "$CROSS_BUILD" ]; then
		_cross_args="--set=target.${RUST_TARGET}.llvm-config=${XBPS_WRAPPERDIR}/llvm-config-qemu
		 --set=target.${RUST_TARGET}.crt-static=false
		 --set=target.${RUST_TARGET}.cc=${CC}
		 --set=target.${RUST_TARGET}.cxx=${CXX}
		 --set=target.${RUST_TARGET}.ar=${AR}
		 --set=target.${RUST_TARGET}.linker=${CC}"
	fi

	./configure \
		--build="${RUST_BUILD}" \
		--host="${RUST_TARGET}" \
		--target="${RUST_TARGET}" \
		--prefix=/usr \
		--python=python3 \
		--enable-vendor \
		--enable-locked-deps \
		--enable-verbose-tests \
		--disable-full-bootstrap \
		--enable-extended \
		--enable-profiler \
		--disable-codegen-tests \
		--disable-dist-src \
		--${_llvm_shared}-llvm-link-shared \
		--${_rpath}-rpath \
		--${_local_rebuild}-local-rebuild \
		--${_docs}-docs \
		--debuginfo-level="${_debug}" \
		--debuginfo-level-rustc="${_debug_rustc}" \
		--debuginfo-level-tests=0 \
		--release-channel=stable \
		--release-description="Void Linux" \
		--llvm-libunwind=no \
		--llvm-config=/usr/bin/llvm-config \
		--disable-lld \
		--disable-llvm-bitcode-linker \
		--tools=clippy,rustdoc,rustfmt,rust-analyzer-proc-macro-srv \
		--set="rust.optimize=true" \
		--set="rust.debug=false" \
		--set="rust.codegen-units=1" \
		--set="rust.codegen-units-std=1" \
		--set="rust.incremental=false" \
		--set="rust.jemalloc=false" \
		--set="rust.llvm-tools=false" \
		--set="build.submodules=false" \
		--set="build.rustc=/usr/bin/rustc" \
		--set="build.cargo=/usr/bin/cargo" \
		--set="build.optimized-compiler-builtins=false" \
		--set="target.${RUST_BUILD}.crt-static=false" \
		--set="target.${RUST_BUILD}.cc=${CC_host:-$CC}" \
		--set="target.${RUST_BUILD}.cxx=${CXX_host:-$CXX}" \
		--set="target.${RUST_BUILD}.ar=${AR_host:-$AR}" \
		--set="target.${RUST_BUILD}.linker=${CC_host:-$CC}" \
		--set="dist.src-tarball=false" \
		${_cross_args}
}

# 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() {
	export RUST_BACKTRACE=1
	export JEMALLOC_SYS_WITH_LG_PAGE=16
	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}" \
		CXXFLAGS_${RUST_BUILD}="${CXXFLAGS_host}" \
		CFLAGS_${RUST_TARGET}="${CFLAGS}" \
		CXXFLAGS_${RUST_TARGET}="${CXXFLAGS}" \
		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/incremental \
		src/test/mir-opt \
		src/test/pretty \
		src/test/run-make \
		src/test/run-make-fulldeps \
		src/test/ui \
		src/test/ui-fulldeps
}

do_install() {
	if [ "$build_option_bindist" ]; then
		mkdir -p ${XBPS_SRCDISTDIR}/distfiles
		install -m 0644 \
			build/dist/rustc-${version}-${RUST_TARGET}.tar.xz \
			build/dist/rust-std-${version}-${RUST_TARGET}.tar.xz \
			${XBPS_SRCDISTDIR}/distfiles
		exit 1
	fi

	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
	tar xf build/dist/rust-docs-${version}-${RUST_TARGET}.tar.gz \
	 -C "$DESTDIR/usr" --strip-components=2 --exclude=manifest.in
	tar xf build/dist/rust-src-${version}.tar.gz \
	 -C "$DESTDIR/usr" --strip-components=2 --exclude=manifest.in
	tar xf build/dist/rustfmt-${version}-${RUST_TARGET}.tar.gz \
	 -C "$DESTDIR/usr" --strip-components=2 --exclude=manifest.in
	tar xf build/dist/clippy-${version}-${RUST_TARGET}.tar.gz \
	 -C "$DESTDIR/usr" --strip-components=2 --exclude=manifest.in

	vlicense COPYRIGHT
	vlicense LICENSE-APACHE
	vlicense LICENSE-MIT

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

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

rust-std_package() {
	short_desc+=" - standard library"
	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		depends="libexecinfo-devel"
	fi
	pkg_install() {
		vmove usr/lib/rustlib/${RUST_TARGET}
		vmove usr/lib/rustlib/rustc-src
	}
}

rust-src_package() {
	short_desc+=" - source code"
	pkg_install() {
		vmove usr/lib/rustlib/src
	}
}
