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="2016, The Rust Project Developers"
LICENSE="MIT"
REVISION="1"
srcGitRev="b495ea57163d858facb956a601d78c641149631f"
SOURCE_URI="https://github.com/nielx/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="cd5012f390567e6da592eec525e2373f50a5541a51f9c0540c24be0aad8d4f05"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="8598065bd965d9713bfafb6c1e766d63a7b17b89"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="19cab27b6c6906982a6213c745b49f37c75834af1106f2acafc788ffadd84ff7"
srcGitRev_3="d66270786797c98a4aa43dc0e5247e2942bb8c46"
SOURCE_URI_3="https://github.com/nielx/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="6d1e18819a61c6cef25c58d50033eb160d32187e755558a486aae73ce2c9e852"
srcGitRev_4="a3736a0a1907cbc8bf619708738815a5fd789c80"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="29da85607df523a165c1b01a378f929a0b37b75969119b6ec373d95576978b13"
srcGitRev_5="6c83cf726a3b6b2d1a10ecf932005ba15cf84f23"
SOURCE_URI_5="https://github.com/nielx/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="e7bdfce7e463c9984d43555951944f9f764190934676e0dbbcdad1f5175986dc"
srcGitRev_6="ac1c94226e9fa17005ce7e2dd52dd6d1875f3137"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="57638407e7713018e5cd4c0de29b52e976ddb248db122c13d6ded89f5f3243ce"

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

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:rust_gdb = $portVersion
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	binutils${secondaryArchSuffix} == 2.26.1_2016_07_22 # version 2.28.1 will crash when building llvm
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:file
	cmd:find
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:python
	cmd:rustc == 1.11.0
	cmd:sed
	cmd:tar
	cmd:which
	"

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/ \
	            --release-channel=stable

	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

	# clean out unneccesary files created by the rust installer
	rm $developLibDir/rustlib/components
	rm $developLibDir/rustlib/install.log
	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
}
