# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Shiz <hi@shiz.me>
# Contributor: Jeizsm <jeizsm@gmail.com>
# Maintainer: Samuel Holland <samuel@sholland.org>
pkgname=rust
pkgver=1.29.2
_bootcargover=0.30.0
_bootver=1.29.1
_llvmver=6
pkgrel=0
pkgdesc="The Rust Programming Language"
url="https://www.rust-lang.org"
arch="all"
license="(Apache-2.0 OR MIT) AND (NCSA OR MIT) AND BSD-2-Clause AND BSD-3-Clause"
depends="$pkgname-stdlib=$pkgver-r$pkgrel gcc musl-dev"
makedepends="
	curl-dev
	libgit2-dev
	libssh2-dev
	llvm$_llvmver-dev
	llvm$_llvmver-test-utils
	openssl-dev
	python3
	zlib-dev
	"
subpackages="
	$pkgname-dbg
	$pkgname-stdlib
	$pkgname-analysis
	$pkgname-doc
	$pkgname-gdb::noarch
	$pkgname-lldb::noarch
	$pkgname-src::noarch
	cargo
	cargo-clippy:_cargo_clippy
	cargo-fmt:_cargo_fmt
	cargo-doc:_cargo_doc:noarch
	cargo-bash-completion:_cargo_bashcomp:noarch
	cargo-zsh-completion:_cargo_zshcomp:noarch
	rls
	rustfmt
	"
source="https://static.rust-lang.org/dist/rustc-$pkgver-src.tar.xz
	https://distfiles.adelielinux.org/source/rust/cargo-$_bootcargover-$CBUILD.tar.xz
	https://distfiles.adelielinux.org/source/rust/rust-std-$_bootver-$CBUILD.tar.xz
	https://distfiles.adelielinux.org/source/rust/rustc-$_bootver-$CBUILD.tar.xz
	0001-Require-static-native-libraries-when-linking-static-.patch
	0002-Don-t-pass-CFLAGS-to-the-C-compiler.patch
	0003-Fix-LLVM-build.patch
	0004-test-sysroot-crates-are-unstable-Fix-test-when-rpath.patch
	0005-Remove-nostdlib-and-musl_root-from-musl-targets.patch
	0006-Prefer-libgcc_eh-over-libunwind-for-musl.patch
	0007-runtest-Fix-proc-macro-tests-on-musl-hosts.patch
	0008-Allow-rustdoc-to-work-when-cross-compiling-on-musl.patch
	0009-Add-missing-OpenSSL-configurations-for-musl-targets.patch
	0010-test-linkage-visibility-dlsym-only-sees-exported-sym.patch
	0011-test-invalid_const_promotion-Accept-SIGTRAP-as-a-val.patch
	0015-flock-Fix-F_SETLK-F_SETLKW-on-32-bit-O_LARGEFILE.patch
	0016-Add-powerpc-unknown-linux-musl-target.patch
	0017-Use-the-ELFv2-ABI-on-powerpc64-musl.patch
	0018-Fix-powerpc64-ELFv2-big-endian-struct-passing-ABI.patch
	0019-Add-powerpc64-unknown-linux-musl-target.patch
	0020-run-pass-const-endianness-negate-before-to_le.patch
	0021-Fix-double_check-tests-on-big-endian-targets.patch
	0022-x.py-Use-python3-instead-of-python.patch
	0023-test-target-feature-gate-Only-run-on-relevant-target.patch
	0024-test-use-extern-for-plugins-Don-t-assume-multilib.patch
	0025-Ignore-broken-and-non-applicable-tests.patch
	0026-Link-stage-2-tools-dynamically-to-libstd.patch
	0027-Move-debugger-scripts-to-usr-share-rust.patch
	0028-Add-foxkit-target-specs.patch
	0030-liblibc-linkage.patch
	0031-liblibc-0163a7ce20b5.patch
	0035-rand-5b9e5c39d857.patch
	0036-rand-checksums.patch
	0040-rls-atomics.patch
	"
builddir="$srcdir/rustc-$pkgver-src"
_rlibdir="/usr/lib/rustlib/$CTARGET/lib"

prepare() {
	cd "$builddir"

	default_prepare

	$srcdir/cargo-$_bootcargover-$CBUILD/install.sh \
		--destdir=$srcdir/prebuilt \
		--prefix=/ \
		--disable-ldconfig
	$srcdir/rust-std-$_bootver-$CBUILD/install.sh \
		--destdir=$srcdir/prebuilt \
		--prefix=/ \
		--disable-ldconfig
	$srcdir/rustc-$_bootver-$CBUILD/install.sh \
		--destdir=$srcdir/prebuilt \
		--prefix=/ \
		--disable-ldconfig
}

build() {
	cd "$builddir"

	cat > config.toml <<- EOF
		[build]
		build = "$CBUILD"
		host = [ "$CHOST" ]
		target = [ "$CTARGET" ]
		cargo = "$srcdir/prebuilt/bin/cargo"
		rustc = "$srcdir/prebuilt/bin/rustc"
		submodules = false
		python = "python3"
		locked-deps = true
		vendor = true
		extended = true
		tools = [ "analysis", "cargo", "clippy", "rls", "rustfmt", "src" ]
		[install]
		prefix = "/usr"
		[rust]
		codegen-units = 1
		debuginfo = true
		debuginfo-lines = true
		debuginfo-only-std = false
		debuginfo-tools = true
		use-jemalloc = false
		backtrace = true
		channel = "stable"
		rpath = false
		[target.$CTARGET]
		cc = "$CTARGET-gcc"
		cxx = "$CTARGET-g++"
		linker = "$CTARGET-gcc"
		llvm-config = "/usr/lib/llvm$_llvmver/bin/llvm-config"
		crt-static = false
	EOF

	LIBGIT2_SYS_USE_PKG_CONFIG=1 \
	LLVM_LINK_SHARED=1 \
	RUST_BACKTRACE=1 \
	./x.py build -j ${JOBS:-2}
}

check() {
	cd "$builddir"

	./x.py test -j ${JOBS:-2} --no-doc --no-fail-fast \
		src/test/codegen \
		src/test/codegen-units \
		src/test/compile-fail \
		src/test/compile-fail-fulldeps \
		src/test/incremental \
		src/test/incremental-fulldeps \
		src/test/mir-opt \
		src/test/parse-fail \
		src/test/pretty \
		src/test/run-fail \
		src/test/run-fail/pretty \
		src/test/run-fail-fulldeps \
		src/test/run-fail-fulldeps/pretty \
		src/test/run-make \
		src/test/run-make-fulldeps \
		src/test/run-pass \
		src/test/run-pass/pretty \
		src/test/run-pass-fulldeps \
		src/test/run-pass-fulldeps/pretty \
		src/test/ui \
		src/test/ui-fulldeps
}

package() {
	cd "$builddir"

	DESTDIR="$pkgdir" ./x.py install

	rm "$pkgdir"/usr/lib/*.so \
	   "$pkgdir"/usr/lib/rustlib/components \
	   "$pkgdir"/usr/lib/rustlib/install.log \
	   "$pkgdir"/usr/lib/rustlib/manifest-* \
	   "$pkgdir"/usr/lib/rustlib/rust-installer-version \
	   "$pkgdir"/usr/lib/rustlib/uninstall.sh \
	   "$pkgdir"/usr/share/doc/rust/*.old
}

stdlib() {
	pkgdesc="Standard library for Rust"
	depends="musl-utils"

	_mv "$pkgdir"$_rlibdir/*.so "$subpkgdir"$_rlibdir

	mkdir -p "$subpkgdir"/etc/ld.so.conf.d
	echo "$_rlibdir" > "$subpkgdir"/etc/ld.so.conf.d/$pkgname.conf
}

analysis() {
	pkgdesc="Compiler analysis data for the Rust standard library"
	depends="$pkgname=$pkgver-r$pkgrel $pkgname-stdlib=$pkgver-r$pkgrel"

	_mv "$pkgdir"${_rlibdir%/*}/analysis "$subpkgdir"${_rlibdir%/*}
}

gdb() {
	pkgdesc="GDB pretty printers for Rust"
	license="Apache-2.0 OR MIT"
	depends="$pkgname gdb"
	install_if="$pkgname=$pkgver-r$pkgrel gdb"

	_mv "$pkgdir"/usr/bin/rust-gdb "$subpkgdir"/usr/bin
	_mv "$pkgdir"/usr/share/rust/gdb_*.py "$subpkgdir"/usr/share/rust || true
}

lldb() {
	pkgdesc="LLDB pretty printers for Rust"
	license="Apache-2.0 OR MIT"
	depends="$pkgname lldb py3-lldb"
	install_if="$pkgname=$pkgver-r$pkgrel lldb"

	_mv "$pkgdir"/usr/bin/rust-lldb "$subpkgdir"/usr/bin
	_mv "$pkgdir"/usr/share/rust/lldb_*.py "$subpkgdir"/usr/share/rust || true
}

src() {
	pkgdesc="$pkgdesc (source code)"
	depends=""

	_mv "$pkgdir"/usr/lib/rustlib/src/rust "$subpkgdir"/usr/src
	rmdir -p "$pkgdir"/usr/lib/rustlib/src 2>/dev/null || true

	mkdir -p "$subpkgdir"/usr/lib/rustlib/src
	ln -s ../../../src/rust "$subpkgdir"/usr/lib/rustlib/src/rust
}

cargo() {
	pkgdesc="The Rust package manager"
	depends="$pkgname-stdlib=$pkgver-r$pkgrel $pkgname"

	_mv "$pkgdir"/usr/bin/cargo "$subpkgdir"/usr/bin
}

_cargo_clippy() {
	pkgdesc="A collection of Rust lints (cargo plugin)"
	depends="$pkgname-stdlib=$pkgver-r$pkgrel cargo"

	_mv "$pkgdir"/usr/bin/cargo-clippy \
	    "$pkgdir"/usr/bin/clippy-driver \
	    "$subpkgdir"/usr/bin
}

_cargo_fmt() {
	pkgdesc="Format Rust code (cargo plugin)"
	depends="$pkgname-stdlib=$pkgver-r$pkgrel cargo rustfmt"
	install_if="cargo=$pkgver-r$pkgrel rustfmt=$pkgver-r$pkgrel"

	_mv "$pkgdir"/usr/bin/cargo-fmt "$subpkgdir"/usr/bin
}

_cargo_bashcomp() {
	pkgdesc="Bash completion for cargo"
	license="Apache-2.0 OR MIT"
	depends=""
	install_if="cargo=$pkgver-r$pkgrel bash-completion"

	_mv "$pkgdir"/etc/bash_completion.d/cargo \
	    "$subpkgdir"/usr/share/bash-completion/completions
	rmdir -p "$pkgdir"/etc/bash_completion.d 2>/dev/null || true
}

_cargo_zshcomp() {
	pkgdesc="ZSH completion for cargo"
	license="Apache-2.0 OR MIT"
	depends=""
	install_if="cargo=$pkgver-r$pkgrel zsh"

	_mv "$pkgdir"/usr/share/zsh/site-functions/_cargo \
	    "$subpkgdir"/usr/share/zsh/site-functions/_cargo
	rmdir -p "$pkgdir"/usr/share/zsh/site-functions 2>/dev/null || true
}

_cargo_doc() {
	pkgdesc="The Rust package manager (documentation)"
	license="Apache-2.0 OR MIT"
	depends=""
	install_if="cargo=$pkgver-r$pkgrel docs"

	# XXX: This is hackish!
	_mv "$pkgdir"/../$pkgname-doc/usr/share/man/man1/cargo* \
	    "$subpkgdir"/usr/share/man/man1
}

rls() {
	pkgdesc="The Rust language server"
	license="Apache-2.0 OR MIT"
	depends="$pkgname-stdlib=$pkgver-r$pkgrel"

	_mv "$pkgdir"/usr/bin/rls "$subpkgdir"/usr/bin
}

rustfmt() {
	pkgdesc="Format Rust code"
	depends="$pkgname-stdlib=$pkgver-r$pkgrel"

	_mv "$pkgdir"/usr/bin/rustfmt "$subpkgdir"/usr/bin
}

_mv() {
	local dest; for dest; do true; done  # get last argument
	mkdir -p "$dest"
	mv "$@"
}
sha512sums="d8fa9501764348ace8f60f2069bcd016925abe56c8bbc2b87fb52ff796e4bc7284c1fccbb1671416437bb82fde7d9240eeae875d6099914e27e75cfe050e2d38  rustc-1.29.2-src.tar.xz
bd1fd3694947fc192419dab1e1e314fb5a9d7266715b5765dd1766507a234b6ad93bd097f531d560adc8b9f71534f53c440e6986e63b322319fcd0c24325d979  cargo-0.30.0-powerpc64-foxkit-linux-musl.tar.xz
d6ce24de73131702b623e8a146435526457b6d766fe6192df7471f1942119a6396b85121ce2250da3c3ec21b44aaff316e5c0387796654129d074d29d60a25b2  rust-std-1.29.1-powerpc64-foxkit-linux-musl.tar.xz
b649fc30dee7510f656c53f3a5737a66d59726e24be7085134504f0202a899e549274df417c814487e5fbd8bbb69470411c9ed770dbc37af15a99a63f913a508  rustc-1.29.1-powerpc64-foxkit-linux-musl.tar.xz
a242a6cdc95ecfa402e1457bc9e3b98305b8a5bcb5f54904807382cc98720e2a8fd16cee311edba84eb64129c591a58ed1f70798269832ccf19f1af79efe8b70  0001-Require-static-native-libraries-when-linking-static-.patch
2b2dc29d31351e21e10808a0223b8b43d3804f0fbef6a97d6016958bb982850d0a63b73f1133ca4e70ad5c5901a3357c279e40a5c60379eb8ac070a9fb78a203  0002-Don-t-pass-CFLAGS-to-the-C-compiler.patch
6f291642e2ce0cbe57e5354d4eff2745b651b3308771fd122edcb75ff7b809a34ee98c5ab2b64699b60e6be1007a383b29e0f991a9539f85a4c6c19e239c7654  0003-Fix-LLVM-build.patch
4f1a716e22126d65f4b32be6210293a32e54c3b4c8655097962e2d0d44c1b976cb923fc99dbf8d6487541518e9c8ed73d3d2efb6da3b881513d8243808de7adf  0004-test-sysroot-crates-are-unstable-Fix-test-when-rpath.patch
7ff0c4cfc040f47b9dcf28d64bbcdf45d674bbd3a05e7d3646474044e0713d80d878d8cc39dd5029cd6f7c2c6d53c08ea445eacf147ccf14de1d729bf6a83734  0005-Remove-nostdlib-and-musl_root-from-musl-targets.patch
f1b9d68d4685db660b6c09cfa5ce0f818a3146e458973b942ac7991cfdd849976da7bc4e7245b9aa0ace498b51c9dbe9def3f2ffc277f52d2883f8b8fd0c47ef  0006-Prefer-libgcc_eh-over-libunwind-for-musl.patch
14cd3ff8d3e1d41014008ec8da958b72383c5fab6066d4dd21f4c02043fbea9ef159a1a76b718ec54c135fe971c7269d88b9703692706d23ed409022b0e57221  0007-runtest-Fix-proc-macro-tests-on-musl-hosts.patch
e7de9eec874e08d3aaaa1389ed827ccb201659f792d4b64bf430a1b972b1d1f793fb14131d60df92d84af35da01eee4d59f6b8f631acfef95284688cde45fe04  0008-Allow-rustdoc-to-work-when-cross-compiling-on-musl.patch
8c2cbe4c0a99c38e4c7f05b30d396acfbe0511c06b433a468c35477cda73e77a4a0d61f809465d29e92bf3960dc25d5d860d28c79be42ac25f336aea40a24881  0009-Add-missing-OpenSSL-configurations-for-musl-targets.patch
38085c8f79a3fda0f31cc733044c50fdfb1bff82da4f4cb40f280bf0924c53cfb1fab31a8345e40ae2960f1c1b495570d8219c9c9958db4da184f7120e07a4ad  0010-test-linkage-visibility-dlsym-only-sees-exported-sym.patch
93faeccf86b7920d48aba11e3345459e828a47a2b709ac65bc1ed71ef928ae18539040390f394c58fadbe0f44459248018c3cf0b0ac64aedd0f27a5ae866d69e  0011-test-invalid_const_promotion-Accept-SIGTRAP-as-a-val.patch
c2208400ee5a9f0f7b7897effe536f5937e05c0c4214f5974194ef8c41fc3013ca5224594add1b9fb83f50c9372bd659b7a34442fc3462d43fe8fe6dbdceb1ad  0015-flock-Fix-F_SETLK-F_SETLKW-on-32-bit-O_LARGEFILE.patch
157b6e658ad39d01a7bcee16de5a7b4cb0c8a95a55ace9ca24ac499dee343db7de5d7455443f868cd02f612ef9f6787001ee8e53d8b046304d451507e25ca7ce  0016-Add-powerpc-unknown-linux-musl-target.patch
2f353c5e29e00f9e8f28b6c9010b204138a6913565674afd40ab79f2b434ed8af8296d2fe92459c103eb6262579466c44be2280e5ec46e129855fcb06f314e32  0017-Use-the-ELFv2-ABI-on-powerpc64-musl.patch
c63ce49e7041991e78921d4b6f36e037fbcf478eb30a8590fbb0349bc55ead4639ebf95f8d90d7d572244dbf14f74471c192cc2c7dd020fb8f274e885f130c29  0018-Fix-powerpc64-ELFv2-big-endian-struct-passing-ABI.patch
96d6473a9a1aed1df0911da4583ff8377ecf9cea920aaeafdbf4f4db652027e2937e1108eb21aafc044c0b45a6cd1f152393c5fa527cdabd4ce4e70f69fe15c6  0019-Add-powerpc64-unknown-linux-musl-target.patch
133f2b57e351dd6a75ac3abe3d980c3e1e40af4cdfe7fc4a05ed3e45029ae69ab979db7edb2b890742c107120f436404fc720e20df52fd9d5ff14d22a7d82aa4  0020-run-pass-const-endianness-negate-before-to_le.patch
652420d7bdc9be4e6136577e375e9b43f7c4086b3f0c1b4739c7eb6303c7826df84f40da7811618f296edcf712357e4b046e477b748605d066ab6d8015c6a619  0021-Fix-double_check-tests-on-big-endian-targets.patch
7eb0c8eb095ca0201b26444b275005a89921731e87fb7a82d3d46215f14acc8b987134288955fc90f9d419ebe0cb26e09589906aa954bcbe229e25b737c64c7d  0022-x.py-Use-python3-instead-of-python.patch
231d37dcd04787300a38122f1482b616970e070a4a87349aeaf5ec9f7d15563feeaa88be9cfc0863640b22596f5c2e2acfe7f9b22e43e6f1ee90f3b4730e746b  0023-test-target-feature-gate-Only-run-on-relevant-target.patch
b6c34f9982d6f5b59acc716391a63f922accf69bdcf2631ab57ce3279a62e1874bceb7d0707fdd5290a17ec88e948d803c567476f904f96b7e5d7a240a4ee53c  0024-test-use-extern-for-plugins-Don-t-assume-multilib.patch
cf8d642afcfd27fbc8c982a27d7d09f6097f1d0bfb67251d3a51323bd86743f584ebbd1cb4a8e872bd7711610929aa4bd514a107956fb89dd14f6dc50e9bdd26  0025-Ignore-broken-and-non-applicable-tests.patch
9b9f4625d0a56417fa72d58982e55449b8fb5ed103799327a9017976ddd8ec431e81f592d3f843927068bd0e68e8e5a589650f18fb37bd275100f17bb65a1560  0026-Link-stage-2-tools-dynamically-to-libstd.patch
35fc80eb99d818f8d4c67a5dc72c1acc56a26b529b5a24cad7aa0d5ea4e872384c4eb07084caa3436140cdf377ed0bbcb920e40473a65f2ff90e3ddc1e20e390  0027-Move-debugger-scripts-to-usr-share-rust.patch
b53bc78603b9953a7aecf947b32e209e56dc20f5a8d2d64b5a10c4c3a98c66500119851e49775f35e2ff9697ed7815894c39837f04dc59616f37c8d4be8448fe  0028-Add-foxkit-target-specs.patch
f94abe390798b968bccfba1c908ab5895220ed0d167314b790a9c28b10cac81fb2e3b08760b4e8551ad5a5d02cd052d3d0d647ad8f8634e97f4d3b0470a21c42  0030-liblibc-linkage.patch
9b761388e3e7d4ac7ced81bf2572439724f4ce3190b49429b92249f4b858cc9bbb79d72e25365825cbd25cb9b3352c627dcc88cc643e5abeb917cf14755cab0b  0031-liblibc-0163a7ce20b5.patch
94ee746f50ba3d88e409cd6b840a0995fa40d38e6a3c4a7e9fa32430242c492f35f6fb95ee22e0da6dba84f39dc83f0196a0921c5ec2bc602ea220b017c78be2  0035-rand-5b9e5c39d857.patch
5e409a7e45afd2104e1bbbcedb0cc2a04b2c3838d820bb3dc6e879a3d4619f0cce562d2bf2b0c55b780375bf26f0d4d4d6b0acdac199e1c39b2d94543d35e017  0036-rand-checksums.patch
9182408b7a87fc2f01dc12a36440e43a721f7693da00310806f6306e711c04b34d43b6ab348ae523656937f61389adb6a5a2b8ff46dec5424ad20c29652d4edf  0040-rls-atomics.patch"