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/"
LICENSE="MIT"
COPYRIGHT="2017, The Rust Project Developers"
srcGitRev="1.15.1"
srcGitRev_2="a8fc4c169fac43a5dc204d4fd56ddb1739f8c178"
srcGitRev_3="4f994850808a572e2cc8d43f968893c8e942e9bf"
srcGitRev_4="a3736a0a1907cbc8bf619708738815a5fd789c80"
srcGitRev_5="0ac39c5ccf6a04395b7c40dd62321cb91f63f160"
srcGitRev_6="ceb177eeefa7d67ca29230d2e7e8584f97d4fdad"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$srcGitRev.tar.gz"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
SOURCE_URI_3="https://github.com/rust-lang/rust-installer/archive/$srcGitRev_3.tar.gz"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
SOURCE_URI_5="https://github.com/rust-lang/libc/archive/$srcGitRev_5.tar.gz"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
SOURCE_URI_7="http://rust-on-haiku.com/downloads/76#noarchive"
SOURCE_URI_8="http://rust-on-haiku.com/downloads/77#noarchive"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_FILENAME_7="cargo-0.16.0-i686.tar.gz"
SOURCE_FILENAME_8="cargo-0.16.0-x86_64.tar.gz"
CHECKSUM_SHA256="c9e880a6eb72e9ec689139844ba82e956aad6d69f11e89114a47fb7858d2cef0"
CHECKSUM_SHA256_2="7d143e0025eb634de5dd2c4ceff22f632cfa065ebbf213068ccacff124732034"
CHECKSUM_SHA256_3="dc7240d60a869fa24a68c8734fb7c810c27cca0a6dad52df6279865e4e8e7fae"
CHECKSUM_SHA256_4="29da85607df523a165c1b01a378f929a0b37b75969119b6ec373d95576978b13"
CHECKSUM_SHA256_5="17694d9a1e09153237f2ce85f9c5e1d632b97ab9a0f688ba27da31c4fecbc400"
CHECKSUM_SHA256_6="0414d14ba98eb1659180a00e78d002b50a6494e6b596145d6fb4262cbb76019b"
CHECKSUM_SHA256_7="d92d38cbcdbb491591348d302fe6d079122e12650de9f0e2e9a5a40e7e2504f1"
CHECKSUM_SHA256_8="54e9062464dbff4b11b91831520a7ecd7b539742871ed43553b2d6a8cd6b7d35"
SOURCE_DIR="rust-$srcGitRev"
SOURCE_DIR_7="cargo-i686"
SOURCE_DIR_8="cargo-x86_64"
PATCHES="rust-$portVersion.patchset"
PATCHES_3="rust-installer-$portVersion.patchset"
REVISION="2"
ARCHITECTURES="?all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

# It is an explicit choice to not provide cmd:rustc$secondaryArchSuffix
# The compiler uses relative paths to itself to find libs (in particular
# the *rlib libraries, which is why those are symlinked in the INSTALL()
# phase). As rust will never compile with gcc2, this will not conflict
# with a 'vanilla' package.
# As for cmd:cargo, cargo does not have any paths hardcoded. Putting the
# binary in a proper place in a secondary architecture, will remind the user
# to use 'setarch x86', which is required to have rust call the proper linker.

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	cmd:rust_gdb = $portVersion
	cmd:cargo$secondaryArchSuffix = 0.16.0
	"
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
	"
BUILD_PREREQUIRES="
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:python2
	cmd:sed
	cmd:find
	cmd:grep
	cmd:file
	cmd:git
	cmd:tar
	cmd:which
	cmd:rustc == 1.14.0
	cmd:cmake
	"

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
}

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

	# install the prebuilt cargo binaries
	if [ $effectiveTargetArchitecture = x86 ]; then
		cd /sources-7/cargo-i686
		tar xvfz $SOURCE_FILENAME_7
	fi
	if [ $effectiveTargetArchitecture = x86_64 ]; then
		cd /sources-8/cargo-x86_64
		tar xvfz $SOURCE_FILENAME_8
	fi

	mkdir -p $binDir
	cp cargo $binDir
}

TEST()
{
	make check
}
