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="3"
cargoVersion="0.18.0"
srcGitRev="1.17.0"
SOURCE_URI="https://github.com/rust-lang/rust/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="91c59dfc727dd56114b11badf60ada8338d6afd2a5fb797b1e1886ef60dd3fbe"
SOURCE_FILENAME="rust-$srcGitRev.tar.gz"
SOURCE_DIR="rust-$srcGitRev"
srcGitRev_2="d812b1641f798e38de9fa372476d42cdb6908c01"
SOURCE_URI_2="https://github.com/rust-lang/compiler-rt/archive/$srcGitRev_2.tar.gz"
CHECKSUM_SHA256_2="a1d7a2c248c01499518f54735038030651c62fe8f0d580987ac52be20a13e93b"
srcGitRev_3="4f994850808a572e2cc8d43f968893c8e942e9bf"
SOURCE_URI_3="https://github.com/rust-lang/rust-installer/archive/$srcGitRev_3.tar.gz"
CHECKSUM_SHA256_3="dc7240d60a869fa24a68c8734fb7c810c27cca0a6dad52df6279865e4e8e7fae"
srcGitRev_4="da282f1bb7277b4d30fa1599ee29ad8eb4dd2a92"
SOURCE_URI_4="https://github.com/rust-lang/hoedown/archive/$srcGitRev_4.tar.gz"
CHECKSUM_SHA256_4="623889a326754e93a4a7a8a3487699ea786f4ca9b118f7468c12f8b9966905e6"
srcGitRev_5="64d954c6a76e896fbf7ed5c17e77c40e388abe84"
SOURCE_URI_5="https://github.com/rust-lang/libc/archive/$srcGitRev_5.tar.gz"
CHECKSUM_SHA256_5="62f91d74cffe78b9e18707fd8b02dbd8dca731486d6f5cd9ceca92034d1149a3"
srcGitRev_6="2e951c3ae354bcbd2e50b30798e232949a926b75"
SOURCE_URI_6="https://github.com/rust-lang/llvm/archive/$srcGitRev_6.tar.gz"
CHECKSUM_SHA256_6="a85cc4847370573d194af95d6794304479d1024863c754d9b4e2499478c41393"
SOURCE_URI_7="https://github.com/rust-lang/cargo/archive/$cargoVersion.tar.gz"
CHECKSUM_SHA256_7="19b5c142c194fc2f940d93b4679850980ed077db0ff7c558122c7ef865e24983"
srcGitRev_8="d08fe97d12b41c1ed8cc7701e545864132783941"
SOURCE_URI_8="https://github.com/rust-lang-nursery/nomicon/archive/$srcGitRev_8.tar.gz"
CHECKSUM_SHA256_8="39060572afca31c02f2f5eafc5d94c7b1930af7d3c8b4d8b81bd904d640dc37a"
srcGitRev_9="2d23ea601f017c106a2303094ee1c57ba856d246"
SOURCE_URI_9="https://github.com/rust-lang-nursery/reference/archive/$srcGitRev_9.tar.gz"
CHECKSUM_SHA256_9="8acfa0b31fa2e0780d4de5ec1ba76154f16db8f02e3d23c04d34dcecae6189a7"
srcGitRev_10="0.1.6"
SOURCE_URI_10="https://github.com/alexcrichton/cargo-vendor/archive/$srcGitRev_10.tar.gz"
CHECKSUM_SHA256_10="927f6b782f0be32c6027335bbd49fb40cffa922095ffacc9a0aca78e45023f1a"
PATCHES="rust-$portVersion.patchset"
PATCHES_3="rust-installer-$portVersion.patchset"
PATCHES_7="cargo-$cargoVersion.patchset"
PATCHES_10="cargo-vendor-$srcGitRev_10.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.17.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.16.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 cargo src/compiler-rt src/llvm src/rust-installer \
	       src/liblibc src/rt/hoedown src/doc/nomicon src/doc/reference
	cp -r /sources-2/compiler-rt-$srcGitRev_2 src/compiler-rt
	cp -r /sources-3/rust-installer-$srcGitRev_3 src/rust-installer
	cp -r /sources-4/hoedown-$srcGitRev_4 src/rt/hoedown
	cp -r /sources-5/libc-$srcGitRev_5 src/liblibc
	cp -r /sources-6/llvm-$srcGitRev_6 src/llvm
	cp -r /sources-7/cargo-$cargoVersion cargo
	cp -r /sources-8/nomicon-$srcGitRev_8 src/doc/nomicon
	cp -r /sources-9/reference-$srcGitRev_9 src/doc/reference

	# 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"

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

	# the install step of rust depends on the cargo vendor command
	# build it separately here
	cd /sources-10/cargo-vendor-$srcGitRev_10
	cargo install --force

	# now build rust and cargo
	cd /sources/rust-$srcGitRev
	./x.py dist --verbose
}

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 /sources/rust-$srcGitRev/build/tmp/dist/
	for module in "rust-docs-$srcGitRev-$architecture" \
	              "rust-std-$srcGitRev-$architecture"  \
	              "rustc-$srcGitRev-$architecture"     \
	              "cargo-$cargoVersion-$architecture"
	do
		./$module/install.sh                           \
				--prefix=$prefix                       \
				--docdir=$developDocDir                \
				--libdir=$libDir                       \
				--mandir=$manDir                       \
				--disable-ldconfig
	done

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

	# move other cargo artifacts
	mkdir -p $dataDir
	mv $prefix/share/zsh $dataDir
	mv $prefix/etc/bash_completions.d $dataDir
	rm -rf $prefix/share $prefix/etc

	# 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-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()
{
	make check
}
