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="2017 The Rust Project Developers"
LICENSE="MIT"
REVISION="2"
cargoVersion="0.22.0"
rlsVersion="0.121.0"
srcGitRev="$portVersion"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="a0e674cce176a5c90804d394e57887b278acfcdbda4fe0f2ae5876531a94faa4"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="c8a8767c56ad3d3f4eb45c87b95026936fb9aa35"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="ac595c03dd5dcd3ad66eee76bec2e95770259e4aa4db5df875c2bbcf7781a441"
srcGitRev_3="b4ff403041f17957f735ad750c3241a3a428b9b7"
SOURCE_URI_3="https://github.com/rust-lang/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="dfcce5a555e0aff5f7ba7681968b77529c806c3109ede78eb1b45a0c4455db4b"
srcGitRev_4="da282f1bb7277b4d30fa1599ee29ad8eb4dd2a92"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="623889a326754e93a4a7a8a3487699ea786f4ca9b118f7468c12f8b9966905e6"
srcGitRev_5="4d1b322cff63c47e7ed649fae048e680cfeda077"
SOURCE_URI_5="https://github.com/rust-lang/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="e40ca6485049911c319b717404181675e88c2d56d2d8d27395cc85b0e4f428fb"
srcGitRev_6="d9e7d2696e41983b6b5a0b4fac604b4e548a84d3"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="e5481733ae30cc985674b7494e48ba8717748af9265514dd8516ceceb32ae8f0"
SOURCE_URI_7="https://github.com/rust-lang/cargo/archive/$cargoVersion.tar.gz"
CHECKSUM_SHA256_7="6740e2f8d035a5a5246a12e84a141d37ad4c2b826355aff497e6f1fcfb637aef"
srcGitRev_8="a4322ccb289a43cc238d4536982f184a3eec9ba7"
SOURCE_URI_8="https://github.com/rust-lang-nursery/nomicon/archive/$srcGitRev_8.tar.gz"
CHECKSUM_SHA256_8="d3e255dfe63cabd342229a53605cd94ae6df912e4771f88a1a4de7af3a59d8f2"
srcGitRev_9="266d429a48468371d2d90669f6a30dd659bb4bdb"
SOURCE_URI_9="https://github.com/rust-lang-nursery/reference/archive/$srcGitRev_9.tar.gz"
CHECKSUM_SHA256_9="7e0fb24af053ac9415568b01442a5ab491a9ed5b841e9441808100d63175cb68"
srcGitRev_10="d09c9e8144ed32170b7596abb145ade8b097acaf"
SOURCE_URI_10="https://github.com/rust-lang/book/archive/$srcGitRev_10.tar.gz"
CHECKSUM_SHA256_10="e2059881a339efff63e94d363b75bf34dfad2a0297bc46c568ba40dcc71f2d6f"
srcGitRev_11="86ff0b1413ef45aa05ccca968242ac1f09c12817"
SOURCE_URI_11="https://github.com/rust-lang-nursery/rls/archive/$srcGitRev_11.tar.gz"
CHECKSUM_SHA256_11="fa74c46f239ebd1557dea184de3caf7d76c003c6de64aa7fc07c2c9d9f0543f3"
srcGitRev_12="6b9281d2b2f0ebb94838814b1e8ace2de4b7035b"
SOURCE_URI_12="https://github.com/rust-lang-nursery/compiler-builtins/archive/$srcGitRev_12.tar.gz"
CHECKSUM_SHA256_12="82fa7ada1bdadbba9f780847dc4f9340f15e5f137d838c98ca7e30ec3dda6684"
PATCHES="rust-$portVersion.patchset"

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

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:cargo$secondaryArchSuffix = $cargoVersion
	cmd:rls = $rlsVersion
	cmd:rust_gdb = $portVersion
	cmd:rust_lldb = $portVersion
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libcrypto$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libcurl$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cargo$secondaryArchSuffix == $cargoVersion
	cmd:cmake
	cmd:cmp
	cmd:file
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:git
	cmd:grep
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:python
	cmd:rustc == $portVersion
	cmd:sed
	cmd:tar
	cmd:which
	cmd:xargs
	"

BUILD()
{
	# With the rustbuild system, Rust always makes a tarball with the source
	# In older recipes we symlinked the other git submodules, now we have to
	# copy them
	rm -rf src/tools/cargo src/libcompiler_builtins src/llvm \
	       src/tools/rust-installer src/liblibc src/rt/hoedown \
	       src/doc/nomicon src/doc/reference src/doc/book \
	       src/tools/rls
	cp -r $sourceDir3/rust-installer-$srcGitRev_3 src/tools/rust-installer
	cp -r $sourceDir4/hoedown-$srcGitRev_4 src/rt/hoedown
	cp -r $sourceDir5/libc-$srcGitRev_5 src/liblibc
	cp -r $sourceDir6/llvm-$srcGitRev_6 src/llvm
	cp -r $sourceDir7/cargo-$cargoVersion src/tools/cargo
	cp -r $sourceDir8/nomicon-$srcGitRev_8 src/doc/nomicon
	cp -r $sourceDir9/reference-$srcGitRev_9 src/doc/reference
	cp -r $sourceDir10/book-$srcGitRev_10 src/doc/book
	cp -r $sourceDir11/rls-$srcGitRev_11 src/tools/rls
	cp -r $sourceDir12/compiler-builtins-$srcGitRev_12 src/libcompiler_builtins
	rm -rf src/libcompiler_builtins/compiler-rt
	cp -r $sourceDir2/compiler-rt-$srcGitRev_2 src/libcompiler_builtins/compiler-rt

	# write the build configuration
	tr -d '\t' >config.toml <<- EOL
	[llvm]
	targets = "X86"

	[build]
	cargo = "/$relativeBinDir/cargo"
	rustc = "/boot/system/bin/rustc"
	submodules = false
	extended = true

	[install]
	prefix = "$prefix"
	libdir = "$relativeLibDir"
	mandir = "$relativeManDir"
	docdir = "$relativeDevelopDocDir"
	sysconfdir = "$relativeDataDir"

	[rust]
	channel = "stable"
	use-jemalloc = false
	rpath = false

	[dist]
	src-tarball = false
EOL
	# Disable ASLR: compiling stage 1 rustc requires a lot of RAM (about 1.5
	# GB). Haiku has a per-process limit of 2GB on 32 bit systems. ASLR makes
	# the available space even smaller. Disabling it will give us the space to
	# compile Rust
	export DISABLE_ASLR=1

	# now build rust and cargo
	./x.py dist
}

INSTALL()
{
	# we will manually invoke the install scripts
	if [ $effectiveTargetArchitecture = x86 ]; then
		architecture="i686-unknown-haiku"
	fi
	if [ $effectiveTargetArchitecture = x86_64 ]; then
		architecture="x86_64-unknown-haiku"
	fi

	# let's install the packages one by one
	cd $sourceDir/build/tmp/dist/
	for module in "rust-docs-$srcGitRev-$architecture"     \
	              "rust-std-$srcGitRev-$architecture"      \
	              "rustc-$srcGitRev-$architecture"         \
	              "rust-analysis-$srcGitRev-$architecture" \
	              "cargo-$cargoVersion-$architecture"
	do
		./$module/install.sh                               \
				--prefix=$prefix                           \
				--docdir=$developDocDir                    \
				--libdir=$libDir                           \
				--mandir=$manDir                           \
				--sysconfdir=$dataDir                      \
				--disable-ldconfig
	done

	# move the cargo and binaries (in case of a secondary arch)
	if [ -n "$secondaryArchSuffix" ]; then
		mkdir -p $binDir
		mv $prefix/bin/cargo $binDir/cargo
	fi

	# remove zsh data, it is not used on Haiku anyway
	rm -rf $prefix/share

	# 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
	# Note; this actually seems to be a bug in the Rust build system. The path
	# to rustlib is hardcoded in the rustc binary, but it does allow it to be
	# set to libdir_relative (see config.rs in the bootstrap tool). This variable
	# is only set when the configure script is used to generate the config, not
	# with config.toml
	mkdir -p $developLibDir
	mv $libDir/rustlib $developLibDir
	cd $prefix/lib
	ln -s $developLibDir/rustlib rustlib

	# clean out unneccesary files created by the rust installer
	rm $developLibDir/rustlib/components
	rm $developLibDir/rustlib/install.log
	rm $developLibDir/rustlib/manifest-*
	rm $developLibDir/rustlib/rust-installer-version
	rm $developLibDir/rustlib/uninstall.sh
}

TEST()
{
	./x.py test
}
