SUMMARY="Modern and safe systems programming language"
DESCRIPTION="Rust is a systems programming language that runs blazingly fast, prevents almost all crashes*, and eliminates data races."
HOMEPAGE="https://www.rust-lang.org/"
COPYRIGHT="2017, The Rust Project Developers"
LICENSE="MIT"
REVISION="2"
cargoVersion="0.17.0"
srcGitRev="1.16.0"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="b7ec2d24d41ef2c27b1883d54f44a3c0a2eeecaf8c7abc1855f3171f3e0392cb"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="a8fc4c169fac43a5dc204d4fd56ddb1739f8c178"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="7d143e0025eb634de5dd2c4ceff22f632cfa065ebbf213068ccacff124732034"
srcGitRev_3="4f994850808a572e2cc8d43f968893c8e942e9bf"
SOURCE_URI_3="https://github.com/rust-lang/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="dc7240d60a869fa24a68c8734fb7c810c27cca0a6dad52df6279865e4e8e7fae"
srcGitRev_4="a3736a0a1907cbc8bf619708738815a5fd789c80"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="29da85607df523a165c1b01a378f929a0b37b75969119b6ec373d95576978b13"
srcGitRev_5="7d57bdcdbb56540f37afe5a934ce12d33a6ca7fc"
SOURCE_URI_5="https://github.com/rust-lang/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="669d4db37e59ea83449e9ddb8bcccda7c22050763602a7e6d818986f345457b7"
srcGitRev_6="ceb177eeefa7d67ca29230d2e7e8584f97d4fdad"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="0414d14ba98eb1659180a00e78d002b50a6494e6b596145d6fb4262cbb76019b"
SOURCE_URI_7="https://github.com/rust-lang/cargo/archive/$cargoVersion.tar.gz"
CHECKSUM_SHA256_7="fc689ca7a09f1e6a1c31e69f0e2616a93576a68e0e1d7f0ae4c0d3301ff21ff8"
PATCHES="rust-$portVersion.patchset"
PATCHES_3="rust-installer-$portVersion.patchset"
PATCHES_5="rust-libc-$portVersion.patchset"
PATCHES_7="cargo-$cargoVersion.patchset"

ARCHITECTURES="all !x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:cargo$secondaryArchSuffix = $cargoVersion
	cmd:rust_gdb = $portVersion
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	binutils${secondaryArchSuffix} == 2.26.1_2016_07_22 # version 2.28.1 will crash when building llvm
	devel:libcurl$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cargo$secondaryArchSuffix == 0.16.0
	cmd:cmake
	cmd:cmp
	cmd:file
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python
	cmd:rustc == 1.15.1
	cmd:sed
	cmd:tar
	cmd:which
	cmd:xargs
	"

BUILD()
{
	rm -rf src/compiler-rt src/llvm src/rust-installer src/liblibc src/rt/hoedown
	ln -sf /sources-2/compiler-rt-$srcGitRev_2 src/compiler-rt
	ln -sf /sources-3/rust-installer-$srcGitRev_3 src/rust-installer
	ln -sf /sources-4/hoedown-$srcGitRev_4 src/rt/hoedown
	ln -sf /sources-5/libc-$srcGitRev_5 src/liblibc
	ln -sf /sources-6/llvm-$srcGitRev_6 src/llvm
	# Do not use runConfigure: it generates too much noise, and it creates a totally impossible directory structure
	#runConfigure --omit-dirs "docDir libExecDir dataRootDir includeDir sharedStateDir sbinDir binDir oldIncludeDir" ./configure --disable-jemalloc --build=i686-unknown-haiku --host=i686-unknown-haiku --target=i686-unknown-haiku
	./configure --prefix=$prefix --mandir=$manDir --disable-jemalloc \
	            --enable-local-rust --local-rust-root=/boot/system/ \
	            --disable-rustbuild --release-channel=stable

	make

	# Build cargo
	# This requires some manual interventions. From 1.17.0 the cargo source can be
	# built from inside the tree. In this version we will still have to do it as a
	# separate step. We use the just-generated rustc binary to compile the new
	# version.
	if [ $effectiveTargetArchitecture = x86 ]; then
		architecture="i686-unknown-haiku"
	fi
	if [ $effectiveTargetArchitecture = x86_64 ]; then
		architecture="x86_64-unknown-haiku"
	fi

	cd /sources-7/cargo-$cargoVersion
	rm -rf src/rust-installer
	ln -sf /sources-3/rust-installer-$srcGitRev_3 src/rust-installer
	LIBRARY_PATH=/sources/rust-$portVersion/$architecture/stage2/lib:$LIBRARY_PATH \
		./configure --prefix=$prefix --datadir=$dataDir --mandir=$manDir \
            --docdir=$docDir --sysconfdir=$dataDir \
            --rustc=/sources/rust-$portVersion/$architecture/stage2/bin/rustc \
             --release-channel=stable
    LIBRARY_PATH=/sources/rust-$portVersion/$architecture/stage2/lib:$LIBRARY_PATH make
}

INSTALL()
{
	make install

	# move documentation to the proper place
	mkdir -p $developDocDir
	mv $prefix/share/doc/rust/* $developDocDir
	rm -rf $prefix/share

	if [ -n "$secondaryArchSuffix" ]; then
		# move the libs to the x86 subdir
		mkdir -p $libDir
		mv $prefix/lib/*.so $libDir
	fi

	# move the `rustlib` folder to the developLibDirs (as it is a framework of sorts)
	# do create a link in $prefix/lib as that is where rustc expects things to live
	mkdir -p $developLibDir
	mv $prefix/lib/rustlib $developLibDir
	cd $prefix/lib
	ln -s ../$relativeDevelopLibDir/rustlib rustlib

	# install cargo
	if [ $effectiveTargetArchitecture = x86 ]; then
		architecture="i686-unknown-haiku"
	fi
	if [ $effectiveTargetArchitecture = x86_64 ]; then
		architecture="x86_64-unknown-haiku"
	fi

	cd /sources-7/cargo-$cargoVersion
	LIBRARY_PATH=/sources/rust-$portVersion/$architecture/stage2/lib:$LIBRARY_PATH make install

	# move the cargo binary (in case of a secondary arch)
	if [ -n "$secondaryArchSuffix" ]; then
		mkdir -p $binDir
		mv $prefix/bin/cargo $binDir/cargo
	fi

	# clean out unneccesary files created by the rust installer
	rm $developLibDir/rustlib/components
	rm $developLibDir/rustlib/install.log
	rm $developLibDir/rustlib/manifest-cargo
	rm $developLibDir/rustlib/manifest-rust-docs
	rm $developLibDir/rustlib/manifest-rust-std-*-haiku
	rm $developLibDir/rustlib/manifest-rustc
	rm $developLibDir/rustlib/rust-installer-version
	rm $developLibDir/rustlib/uninstall.sh
}

TEST()
{
	make check
}
