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="e4511258789ee6cf16b23105cf6d691ef1c4a9ca"
SOURCE_URI="https://github.com/nielx/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="71a22a71378d441c00cc17dfb76b32bbc69469a24d460b5e63ed918015f463a0"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="ac3d1cda612edccb6f1da53cbf7716e248405f3b"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="b1c6086509231af13ad8293dd98695e0ac17d1a76808e3fdd30c0ac070abe0aa"
srcGitRev_3="f96680f5a059069a320f62568d5afc88db9a7ec0"
SOURCE_URI_3="https://github.com/nielx/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="d5f79186cd4b9221c07cf89b3f038aa073674e13ddd35f2e7e22486b8eb16020"
srcGitRev_4="4638c60dedfa581fd5fa7c6420d8f32274c9ca0b"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="a5a0a957544a5f8cf08c0b67cb503f294b0e1bb20c65766abf9dc3c683d3b9c5"
srcGitRev_5="9b1459b6237748b525e0a4ee33f63ee3e1735593"
SOURCE_URI_5="https://github.com/nielx/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="602d0875cd11eaeac39c1c4e4edab26bc6f99cfefebac7a5bd00066c0962faed"
srcGitRev_6="0bd1123d8236f2587866b786479bef0aef3f6fe9"
SOURCE_URI_6="https://github.com/nielx/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="920e505cc75b8332cbbcb7337f8e5596bdf629a1b03be487926fcfbfb6009224"

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.10.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
}
