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="2015, The Rust Project Developers"
LICENSE="MIT"
REVISION="1"
srcGitRev="cc3dc6313b9098cc97b0aab889b407ea2ee61674"
SOURCE_URI="https://github.com/nielx/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="8b029f873b8a4eed794b3eecb1cd589859eef11e6f0f54f587761390fc648ec0"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="50aaf12f610fd14c40fe8c17d85e27dacc091e84"
SOURCE_URI_2="https://github.com/nielx/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="adabe5ccb750361199db684ea12c25de32106e477eb186d9645787959cc6b7c2"
srcGitRev_3="70b8783064398a74c64c65e7216000dabb695bf9"
SOURCE_URI_3="https://github.com/nielx/llvm/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="f13a30c575be1f4c6ea2787a6435ea20e2a874ed28af8db96ed274ca8bdc0045"
srcGitRev_4="7911d8666bab5bb3456f15431450e719ef2cb2c9"
SOURCE_URI_4="https://github.com/nielx/rust-installer/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="0946d54f64b0dedf43ecafe463bc0370d0e1273621d187b384fc112b6de1304f"
srcGitRev_5="238c4d57cce10d33b05cf52a91fc62a09f31ffbb"
SOURCE_URI_5="https://github.com/rust-lang/hoedown/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="0b48589221adc4748b4a8f79c374006defdaadb1ecc363f395232cfad5b23b5c"
if [ $effectiveTargetArchitecture = x86_64 ]; then
	SOURCE_URI_6="http://rust-on-haiku.com/downloads/33#noarchive"
	CHECKSUM_SHA256_6="6156c67e3b4d61e330290e86ed11f645c266b7e1512737dff1da7cd7ff6dee35"
	SOURCE_FILENAME_6="rust-stage0-2015-11-13-c386da0-haiku-x86_64-d09b23db0ef31209a1ebc42e54177449db0985ef.tar.bz2"
else
	SOURCE_URI_6="http://rust-on-haiku.com/downloads/22#noarchive"
	CHECKSUM_SHA256_6="87d3f40436f5e82b095e53b884e34e47567ead5a076a0b49c29f6370d20d3643"
	SOURCE_FILENAME_6="rust-stage0-2015-08-13-8c5c388-haiku-i386-716818c2428eeb1ba418b7d6db1b7956bd38cdf8.tar.bz2"
fi
PATCHES="rust-$portVersion.patchset"

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

PROVIDES="
	rust$secondaryArchSuffix = $portVersion compat >= 1.2
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	cmd:rust_gdb = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	rust_runtime$secondaryArchSuffix == $portVersion base
	"

PROVIDES_runtime="
	rust_runtime$secondaryArchSuffix = $portVersion
	"
REQUIRES_runtime="
	haiku$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:python
	cmd:sed
	cmd:find
	cmd:grep
	cmd:file
	cmd:git
	cmd:tar
	cmd:which
	"

BUILD()
{
	rm -rf src/compiler-rt src/llvm src/rust-installer
	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
	# 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 --release-channel=stable

	# download the stage 0 compiler, and put it in the proper place
	if [ $effectiveTargetArchitecture = x86_64 ]; then
		ln -sf /sources-6/rust-stage0-2015-11-13-c386da0-haiku-x86_64-d09b23db0ef31209a1ebc42e54177449db0985ef.tar.bz2 dl/rust-stage0-2015-11-13-c386da0-haiku-x86_64-d09b23db0ef31209a1ebc42e54177449db0985ef.tar.bz2
	else
		ln -sf /sources-6/rust-stage0-2015-08-13-8c5c388-haiku-i386-716818c2428eeb1ba418b7d6db1b7956bd38cdf8.tar.bz2 dl/rust-stage0-2015-08-13-8c5c388-haiku-i386-716818c2428eeb1ba418b7d6db1b7956bd38cdf8.tar.bz2
	fi
	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-rustc
	rm $developLibDir/rustlib/rust-installer-version
	rm $developLibDir/rustlib/uninstall.sh

	# set up the runtime package
	packageEntries runtime \
		$libDir/libarena-*.so \
		$libDir/libflate-*.so \
		$libDir/libgetopts-*.so \
		$libDir/libgraphviz-*.so \
		$libDir/liblog-*.so \
		$libDir/libstd-*.so \
		$libDir/libterm-*.so \
		$libDir/libtest-*.so
}

TEST()
{
	make check
}
