# Description: The Rust language with Cargo included.
# URL: https://www.rust-lang.org/
# Maintainer: Danny Rawlins, crux at romster dot me
# Depends on: compiler-rt
# Optional: ccache libgit2 lld polly sccache

name=rust
version=1.64.0
_date=2022-08-11
_rustc=1.63.0
_cargo=$_rustc
release=1

source=(https://static.rust-lang.org/dist/${name}c-$version-src.tar.xz
	https://static.rust-lang.org/dist/$_date/rust-std-$_rustc-x86_64-unknown-linux-gnu.tar.xz
	https://static.rust-lang.org/dist/$_date/rustc-$_rustc-x86_64-unknown-linux-gnu.tar.xz
	https://static.rust-lang.org/dist/$_date/cargo-$_cargo-x86_64-unknown-linux-gnu.tar.xz
	0001-bootstrap-Change-libexec-dir.patch
	0001-cargo-Change-libexec-dir.patch
	0002-compiler-Change-LLVM-targets.patch)

unpack_source() {
	for file in ${source[@]}; do
		case ${file##*/} in
			rustc-${version}-src.tar.xz)
			echo "Unpacking $(get_filename $file)";
			bsdtar -p -o -C ${SRC} -xf $(get_filename $file) ;;
		*.tar.xz)
			echo "Copying $(get_filename $file)";
			mkdir -p ${SRC}/${name}c-$version-src/build/cache/${_date} || true
			cp $(get_filename $file) ${SRC}/${name}c-$version-src/build/cache/${_date} ;;
		*)
			cp $(get_filename $file) ${SRC} ;;
		esac
	done
}

_check_version() {
	local ERROR
	local DATE="$(awk '/^.*"date": / {gsub(/"|,/, "", $2); print $2 }' $SRC/${name}c-$version-src/src/stage0.json)"
	local RUSTC="$(awk '/^.*"version": / {gsub(/"|,/, "", $2); print $2 }' $SRC/${name}c-$version-src/src/stage0.json)"

	printf '\033[0;32m%s\033[0m\n' "Checking versions for bootstrap compiler.."
	printf '\033[0;32m%s\033[0m\n' "Got _date = $_date && _rustc = $_rustc"

	if [ "$DATE" != "$_date" ]; then
		printf '\e[1;31m%-6s\e[m\n' "Error: _date $_date != $DATE"
		ERROR=1
	fi
	if [ "$RUSTC" != "$_rustc" ]; then
		printf '\e[1;31m%-6s\e[m\n' "Error: _rustc $_rustc != $RUSTC"
		ERROR=1
	fi

	[ $ERROR ] && exit 1
	printf '\033[0;32m%s\033[0m\n' "Versions check out! Proceeding.."
	unset DATE RUSTC ERROR
}

build() {
	_check_version

	cd "${name}c-$version-src"

	# Patch cargo so credential helpers are in /usr/lib instead of /usr/libexec
	patch -p1 -i $SRC/0001-bootstrap-Change-libexec-dir.patch
	patch -p1 -d src/tools/cargo -i $SRC/0001-cargo-Change-libexec-dir.patch

	sed -e 's/libexec/lib/g' -i src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs

	# Use our *-pc-linux-gnu targets, making LTO with clang simpler
	patch -p1 -i $SRC/0002-compiler-Change-LLVM-targets.patch

cat <<- EOF > $SRC/config.toml
	changelog-seen = 2

	[llvm]
	ninja = true
	link-shared = true
	@POLLY@
	@CCACHE@

	[install]
	prefix = "/usr"

	[rust]
	# LLVM crashes when passing an object through ThinLTO twice.  This is triggered
	# when using rust code in cross-language LTO if libstd was built using ThinLTO.
	# http://blog.llvm.org/2019/09/closing-gap-cross-language-lto-between.html
	# https://github.com/rust-lang/rust/issues/54872
	codegen-units-std = 1
	debuginfo-level-std = 2
	codegen-tests = false

	description = "CRUX"
	channel = "stable"
	parallel-compiler = true
	rpath = false
	optimize = true
	@LLD@

	[build]
	target = ["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"]
	tools = ["cargo", "clippy", "rustfmt", "analysis", "src", "rust-demangler", "rust-analyzer"]
	docs = false
	extended = true
	sanitizers = true
	profiler = true
	vendor = true
	python = "/usr/bin/python3"
	@CARGO@
	@RUST@
	@RUSTFMT@

	[target.x86_64-unknown-linux-gnu]
	llvm-config = "/usr/bin/llvm-config"
	llvm-filecheck = "/usr/bin/FileCheck"
	cc = "/usr/bin/gcc"
	cxx = "/usr/bin/g++"
	ar = "/usr/bin/gcc-ar"
	ranlib = "/usr/bin/gcc-ranlib"

	[target.i686-unknown-linux-gnu]
	cc = "/usr/bin/gcc"
	cxx = "/usr/bin/g++"
	ar = "/usr/bin/gcc-ar"
	ranlib = "/usr/bin/gcc-ranlib"
EOF

	if [ -e '/usr/bin/rustc' ]; then
		if [[ -z "$(/usr/bin/ldd /usr/bin/rustc | egrep '.*libLLVM.*.so => not found')" && \
			-z "$(/usr/bin/ldd /usr/bin/cargo | egrep 'libgit2.so.* => not found')" ]]; then

			sed	-e 's|@CARGO@|cargo = "/usr/bin/cargo"|' \
				-e 's|@RUST@|rustc = "/usr/bin/rustc"|' \
				-e 's|@RUSTFMT@|rustfmt = "/usr/bin/rustfmt"|' \
				-i $SRC/config.toml
		else
			sed -e 's|@CARGO@||' -e 's|@RUST@||' -e 's|@RUSTFMT@||' -i $SRC/config.toml
			printf "\e[031mShared library missing, not using system rust to bootstrap.\033[0m\n"
		fi
	else
		sed -e 's|@CARGO@||' -e 's|@RUST@||' -e 's|@RUSTFMT@||' -i $SRC/config.toml
	fi

	if [ -e '/usr/bin/ccache' ]; then
		sed -e 's|@CCACHE@|ccache = "/usr/bin/ccache"|' -i $SRC/config.toml
		PATH=$(echo ${PATH} | awk -v RS=: -v ORS=: '/ccache/ {next} {print}' | sed 's/:*$//')
	else
		sed -e 's|@CCACHE@||' -i $SRC/config.toml
	fi

	prt-get isinst lld && sed -e 's|@LLD@|use-lld = true|' -i $SRC/config.toml || sed -e 's|@LLD@||' -i $SRC/config.toml
	prt-get isinst polly && sed -e 's|@POLLY@|polly = true|' -i $SRC/config.toml || sed -e 's|@POLLY@||' -i $SRC/config.toml

	cat $SRC/config.toml

	mkdir "$PKGMK_SOURCE_DIR/rust" || true
	export CARGO_HOME="$PKGMK_SOURCE_DIR/rust"

	if [ -e '/usr/bin/sccache' ]; then
		export RUSTC_WRAPPER='/usr/bin/sccache'
		export SCCACHE_IDLE_TIMEOUT='1500'
	fi

	#error: field is never read: `id`
	#   --> src/bootstrap/lib.rs:280:5
	#   = note: `-D dead-code` implied by `-D warnings`
	export RUSTFLAGS+=' -A dead_code'

	export LIBGIT2_NO_PKG_CONFIG=1
	export RUST_BACKTRACE=full
	/usr/bin/python3 ./x.py build --config="${SRC}"/config.toml -j ${JOBS-1}
	DESTDIR=$PKG /usr/bin/python3 ./x.py --config="${SRC}"/config.toml install

	[ -e '/usr/bin/zsh' ] || rm -r $PKG/usr/share/zsh

	# cleanup
	rm -r $PKG/usr/share/doc
	rm -r $PKG/etc
	rm $PKG/usr/lib/rustlib/{components,manifest-rustc,rust-installer-version,uninstall.sh}

	# Remove analysis data for libs that weren't installed
	local file lib
	while read -rd '' file; do
		lib="${file%.json}.rlib"
		lib="${lib/\/analysis\///lib/}"
		if [[ ! -e $lib ]]; then
			echo "missing '$lib'"
			rm -v "$file"
		fi
	done < <(find "$PKG/usr/lib/rustlib"  -path '*/analysis/*.json' -print0)

	install -d $PKG/etc/revdep.d
	echo "/usr/lib/rustlib/i686-unknown-linux-gnu/lib" > $PKG/etc/revdep.d/$name
}
