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="2018 The Rust Project Developers"
LICENSE="MIT"
REVISION="2"
cargoVersion="0.28.0"
rlsVersion="0.127.0"
rustfmtVersion="0.6.1"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$portVersion.tar.gz"
CHECKSUM_SHA256="0bbca54761be5302efc6e6fdb5f4dab4e0dec24458ca7c13538b7259885f9457"
SOURCE_FILENAME="rust-$portVersion.tar.gz"
srcGitRev_2="7243155b1c3da0a980c868a87adebf00e0b33989"
SOURCE_URI_2="https://github.com/rust-lang/llvm/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="ba1578c156e8f10a9330f8d2b8514987c74a2babaea845ac4c4a1d2d5ef379ec"
SOURCE_FILENAME_2="rust-$portVersion-llvm-$srcGitRev_2.tar.gz"
srcGitRev_3="118e078c5badd520d18b92813fd88789c8d341ab"
SOURCE_URI_3="https://github.com/rust-lang/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="34fd6dd8f8e5943be3ab022cd7d0ca792ea2cfe6fb21e06d6054134fc6441892"
SOURCE_FILENAME_3="rust-$portVersion-rust-installer-$srcGitRev_3.tar.gz"
srcGitRev_4="c5fb6dbe8154732b2af8367c75a9b079b2951154"
SOURCE_URI_4="https://github.com/rust-lang/libc/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="aa083022606be28922dfd94606dc27da11f9f397ed732d93fd70fd6a8fa82594"
SOURCE_FILENAME_4="rust-$portVersion-libc-$srcGitRev_4.tar.gz"
srcGitRev_5="748a5e6742db4a21c4c630a58087f818828e8a0a"
SOURCE_URI_5="https://github.com/rust-lang-nursery/nomicon/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="abf8866307d51259e55fef461e7954d05cf3169001069dc9ed4ab5eb40a73abd"
SOURCE_FILENAME_5="rust-$portVersion-nomicon-$srcGitRev_5.tar.gz"
SOURCE_URI_6="https://github.com/rust-lang/cargo/archive/$cargoVersion.tar.gz"
CHECKSUM_SHA256_6="62f4269af6cd168c91a6b10ef845e6dc0f1c211605bd13d155a6a001ade026a1"
SOURCE_FILENAME_6="rust-$portVersion-cargo-$cargoVersion.tar.gz"
srcGitRev_7="134f419ee62714590b04712fe6072253bc2a7822"
SOURCE_URI_7="https://github.com/rust-lang-nursery/reference/archive/$srcGitRev_7.tar.gz"
CHECKSUM_SHA256_7="9e54405f4a6c487cba04d2e25e7026b86581038a88741ce5757b38ddfab4cb07"
SOURCE_FILENAME_7="rust-$portVersion-reference-$srcGitRev_7.tar.gz"
srcGitRev_8="f51127530d46b9acbf4747c859da185e771cfcf3"
SOURCE_URI_8="https://github.com/rust-lang/book/archive/$srcGitRev_8.tar.gz"
CHECKSUM_SHA256_8="a18b090a3a29313a9e68dbef0ca4a5e0ecb36d9eb0f244a1e6cbd33989546989"
SOURCE_FILENAME_8="rust-$portVersion-book-$srcGitRev_8.tar.gz"
srcGitRev_9="d2ade31a52a417257742de72c5936a8a342a34b5"
SOURCE_URI_9="https://github.com/rust-lang-nursery/rls/archive/$srcGitRev_9.tar.gz"
CHECKSUM_SHA256_9="5ca89b25cf636b018cb1e9862cf2272e1295874c712db23178e36a3b2dded892"
SOURCE_FILENAME_9="rust-$portVersion-rls-$rlsVersion.tar.gz"
srcGitRev_10="2a2f6d96c8dc578d2474742f14c9bab0b36b0408"
SOURCE_URI_10="https://github.com/rust-lang-nursery/compiler-builtins/archive/$srcGitRev_10.tar.gz"
CHECKSUM_SHA256_10="cebece5392d3990a4ed06af378f1e44daea0c30d1d348d1cefb7dae9a4f56bbb"
SOURCE_FILENAME_10="rust-$portVersion-compiler-builtins-$srcGitRev_10.tar.gz"
srcGitRev_11="dfd960b5f1a1751b22738fa34fd27b583f4618db"
SOURCE_URI_11="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_11.tar.gz"
CHECKSUM_SHA256_11="7384b3592a4b05857fbf4e54ff5f0ac17bb8bd982b428f348ae2cc4afd9a552c"
SOURCE_FILENAME_11="rust-$portVersion-compiler-rt-$srcGitRev_11.tar.gz"
srcGitRev_12="49279d715bc9bc979313e7c1056ada821ddc3ee2"
SOURCE_URI_12="https://github.com/rust-lang-nursery/rustfmt/archive/$srcGitRev_12.tar.gz"
CHECKSUM_SHA256_12="9708789d175ce1dd43e1623c1373629d26d583022e9e826d768d9270c4304f36"
SOURCE_FILENAME_12="rust-$portVersion-rustfmt-$rustfmtVersion.tar.gz"
srcGitRev_13="f48fed70d4447445b586a35c4ae88683542ffc72"
SOURCE_URI_13="https://github.com/solson/miri/archive/$srcGitRev_13.tar.gz"
CHECKSUM_SHA256_13="7ab3a7345cc98e96a223b92653374fe1cae2c81f90afb745e933f02eacbf7390"
SOURCE_FILENAME_13="rust-$portVersion-miri-$srcGitRev_13.tar.gz"
srcGitRev_14="eebda16e4b45f2eed4310cf7b9872cc752278163"
SOURCE_URI_14="https://github.com/rust-lang/rust-by-example/archive/$srcGitRev_14.tar.gz"
CHECKSUM_SHA256_14="be12b7ba065470a046f7e2ba3f76abd3fd6028b24393eba292d024de8164b630"
SOURCE_FILENAME_14="rust-$portVersion-rust-by-example-$srcGitRev_14.tar.gz"
srcGitRev_15="a19ca1cd91cf97777af8268a6136bd2e4648e189"
SOURCE_URI_15="https://github.com/rust-lang-nursery/stdsimd/archive/$srcGitRev_15.tar.gz"
CHECKSUM_SHA256_15="ddfb9831e8cb439545ce8180a958e57877a0cd9fc3f54305b38679f2b0dd2f68"
SOURCE_FILENAME_15="rust-$portVersion-stdsimd-$srcGitRev_15.tar.gz"
srcGitRev_16="1742229ebb7843a65c05ee495d8de5366fcc5567"
SOURCE_URI_16="https://github.com/rust-lang-nursery/rust-clippy/archive/$srcGitRev_16.tar.gz"
CHECKSUM_SHA256_16="e5b04a1dfeab2d89fc5ee4509711c381bcdca53586b73a999102e14ac8748c23"
SOURCE_FILENAME_16="rust-$portVersion-clippy-$srcGitRev_16.tar.gz"
PATCHES="rust-$portVersion.patchset"
PATCHES_2="rust-llvm-$portVersion.patchset"

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

PROVIDES="
	rust$secondaryArchSuffix = $portVersion
	cmd:cargo$secondaryArchSuffix = $cargoVersion
	cmd:cargo_fmt = $cargoVersion
	cmd:rls = $rlsVersion
	cmd:rust_gdb = $portVersion
	cmd:rust_lldb = $portVersion
	cmd:rustc = $portVersion
	cmd:rustdoc = $portVersion
	cmd:rustfmt = $rustfmtVersion
	"
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()
{
	# Set up the source tree with all the proper submodules
	rm -rf src/llvm src/tools/rust-installer src/liblibc src/doc/nomicon \
	       src/tools/cargo src/doc/reference src/doc/book src/tools/rls \
	       src/libcompiler_builtins src/tools/rustfmt src/tools/miri \
	       src/doc/rust-by-example src/stdsimd src/tools/clippy

	ln -s $sourceDir2/llvm-$srcGitRev_2 src/llvm
	ln -s $sourceDir3/rust-installer-$srcGitRev_3 src/tools/rust-installer
	ln -s $sourceDir4/libc-$srcGitRev_4 src/liblibc
	ln -s $sourceDir5/nomicon-$srcGitRev_5 src/doc/nomicon
	ln -s $sourceDir6/cargo-$cargoVersion src/tools/cargo
	ln -s $sourceDir7/reference-$srcGitRev_7 src/doc/reference
	ln -s $sourceDir8/book-$srcGitRev_8 src/doc/book
	ln -s $sourceDir9/rls-$srcGitRev_9 src/tools/rls
	ln -s $sourceDir10/compiler-builtins-$srcGitRev_10 src/libcompiler_builtins
	rm -rf src/libcompiler_builtins/compiler-rt
	ln -s $sourceDir11/compiler-rt-$srcGitRev_11 src/libcompiler_builtins/compiler-rt
	ln -s $sourceDir12/rustfmt-$srcGitRev_12 src/tools/rustfmt
	ln -s $sourceDir13/miri-$srcGitRev_13 src/tools/miri
	ln -s $sourceDir14/rust-by-example-$srcGitRev_14 src/doc/rust-by-example
	ln -s $sourceDir15/stdsimd-$srcGitRev_15 src/stdsimd
	ln -s $sourceDir16/rust-clippy-$srcGitRev_16 src/tools/clippy

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

	[build]
	cargo = "/$relativeBinDir/cargo"
	rustc = "/boot/system/bin/rustc"
	submodules = false
	extended = true
	tools = ["cargo", "rls", "rustfmt", "analysis"]

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

	[rust]
	channel = "stable"
	use-jemalloc = false
	rpath = false
	deny-warnings = false
	dist-src = 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"      \
	              "rls-$rlsVersion-$architecture"          \
	              "rustfmt-$rustfmtVersion-$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
	ln -r -s $developLibDir/rustlib $prefix/lib/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
}
