#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=rust
PKGVERSION=1.38.0
PKGREVISION=1
PKGLICENSE="APACHEv20 MIT"
PKGSUMMARY="the Rust programming language"
PKGDOWNLOAD="https://static.rust-lang.org/dist/rustc-$PKGVERSION-src.tar.gz"
PKGDEPENDS="bash:build rust:build python:build"
PKGTAGS="CUSTOM"
PKGOPTIONS="COMPONENTS"

: ${CC:=clang}
: ${CFLAGS:=-O2}
: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${LIBS:=}
: ${COMPONENTS:=yes}

case $COMPONENTS in
"yes")
	with_components="cargo clippy rustfmt" ;;
"no")
	with_components="" ;;
*)
	with_components="$COMPONENTS"
esac

build()
{
	rm -rf rustc-$PKGVERSION-src
	tar xvpf rustc-$PKGVERSION-src.tar.gz
	cd rustc-$PKGVERSION-src

	if [ "$CC" = "clang" ]; then
		patch -p0 < ../patch-clang.patch
	fi

	# http://cvsweb.openbsd.org/cgi-bin/cvsweb/ports/lang/rust/patches
	patch -p0 < ../patch-libressl.patch
	patch -p0 < ../patch-rust.patch

	sed \
		-e "s|@PWD@|$cwd|" \
		-e "s|@ARCH@|$(uname -m)|" \
		-e "s|@CC@|$CC|" \
		-e "s|@CXX@|$CXX|" \
		-e "s|@VERSION@|$PKGVERSION|" ../config.toml > config.toml

	./x.py build -v -j 2 src/libstd src/librustc $with_components
	DESTDIR=$DESTDIR ./x.py install -v src/libstd src/librustc $with_components

	# Remove useless bits.
	rm -f $DESTDIR/lib/rustlib/uninstall.sh
	rm -f $DESTDIR/lib/rustlib/install.log
	rm -f $DESTDIR/lib/rustlib/components
	rm -f $DESTDIR/lib/rustlib/manifest*

	cd ..
	rm -rf rustc-$PKGVERSION-src
}
