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.21.0"
srcGitRev="1.20.0"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="ca9307bf18a422029a1fca19ae1fa6c22e3047ff066fb041f9d0a9c886937860"
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="2015cf17a6a2a2280e93d9c57214ba92dbbaf42f"
SOURCE_URI_5="https://github.com/rust-lang/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="a68699c8947466360bf10a5687c7155eb295de6da4a43b7613dcca29a40db53e"
srcGitRev_6="a5ef0696d7c103006d3cb1f4263bad0ca13adca8"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="9c05c8ba8feb45940b9d646dff26b479add03ec6852edd522e75f0e517155375"
SOURCE_URI_7="https://github.com/rust-lang/cargo/archive/$cargoVersion.tar.gz"
CHECKSUM_SHA256_7="7c36cfb515c37959f06f9e6e7de28fd40eac70b53079f8abdd55eba6ae49bbff"
srcGitRev_8="81134a4dff811403b3b2f349b0c59a819f0fe0c1"
SOURCE_URI_8="https://github.com/rust-lang-nursery/nomicon/archive/$srcGitRev_8.tar.gz"
CHECKSUM_SHA256_8="eaa5028dfb4cb6c1722da244d054e2e2bbae3d7463264262b40fe735705799dc"
srcGitRev_9="1abfbaa70313fdf527cf799ffd9b9a096a62105c"
SOURCE_URI_9="https://github.com/rust-lang-nursery/reference/archive/$srcGitRev_9.tar.gz"
CHECKSUM_SHA256_9="0651328ad6f837370a0b7fb1eee49093f41936e0f0bcfbc4dec596f71280015d"
srcGitRev_10="4ee596df22f8ecaa9a0b2ddc0624b0104540dbf7"
SOURCE_URI_10="https://github.com/rust-lang/book/archive/$srcGitRev_10.tar.gz"
CHECKSUM_SHA256_10="75fe86002ce362a9a405be58ee4a30a487f7c4a0bdca5c40804e8bcabc50f3d2"
srcGitRev_11="618f802f0dcebc9f23a527afd3cd228c1fa468f8"
SOURCE_URI_11="https://github.com/rust-lang-nursery/rls/archive/$srcGitRev_11.tar.gz"
CHECKSUM_SHA256_11="6f82ceee79379efb37a2623b7a360ab65a894db9848f57643d1342c36dfac2ac"
srcGitRev_12="5e49856003f33aa5781a0edca148be21025e18e7"
SOURCE_URI_12="https://github.com/rust-lang-nursery/compiler-builtins/archive/$srcGitRev_12.tar.gz"
CHECKSUM_SHA256_12="025e3944bc30161a5d26cb77fddfe6827d1c5e14941bdf9531c11c29bca3e1f4"
PATCHES="rust-$portVersion.patchset"
PATCHES_5="rust-libc-$portVersion.patchset"

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

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:cargo$secondaryArchSuffix = $cargoVersion
	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 == 0.20.0
	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 == 1.19.0
	cmd:sed
	cmd:tar
	cmd:which
	cmd:xargs
	"

BUILD()
{
	# With the new build 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
	cat >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-cargo
	rm $developLibDir/rustlib/manifest-rust-analysis-*haiku
	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()
{
	./x.py test
}
