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="b515c198d30b5d5e18a15bd167949032659f81dc"
SOURCE_URI="https://github.com/nielx/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="ced1585902f9ad107726345ef8873effb03fda72b1d897fd885cd815fd5a3cdf"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="57315f7e07d09b6f0341ebbcd50dded6c20d782f"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="244e97dbad67f752a4c394407e0929dbf44ed520a2dd9f19ebf86f2919112c3e"
srcGitRev_3="eee85639922229007a63fc17ba9d1144ef84d53d"
SOURCE_URI_3="https://github.com/nielx/llvm/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="0a7a666426467e761bbd240d3f6e6e59ad2286f0e52edd2bac8570051c76a324"
srcGitRev_4="f96680f5a059069a320f62568d5afc88db9a7ec0"
SOURCE_URI_4="https://github.com/nielx/rust-installer/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="d5f79186cd4b9221c07cf89b3f038aa073674e13ddd35f2e7e22486b8eb16020"
srcGitRev_5="4638c60dedfa581fd5fa7c6420d8f32274c9ca0b"
SOURCE_URI_5="https://github.com/rust-lang/hoedown/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="a5a0a957544a5f8cf08c0b67cb503f294b0e1bb20c65766abf9dc3c683d3b9c5"
srcGitRev_6="e1a938af495e2ba7477204fb4f24ccc902605e75"
SOURCE_URI_6="https://github.com/nielx/libc/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="5996751d47d363932fb130dba61257e6ed7efc439bcac0244e4d9768c6b3142f"

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
	"
BUILD_PREREQUIRES="
	cmd:file
	cmd:find
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:python
	cmd:rustc == 1.9.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/llvm-$srcGitRev_3 src/llvm
	ln -sf /sources-4/rust-installer-$srcGitRev_4 src/rust-installer
	ln -sf /sources-5/hoedown-$srcGitRev_5 src/rt/hoedown
	ln -sf /sources-6/libc-$srcGitRev_6 src/liblibc
	# 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
}
