Index: tools/toolchain.sh
===================================================================
--- tools/toolchain.sh	(revision 3e05a6914c2f7b39feb950dafa664c07aadc5ff1)
+++ tools/toolchain.sh	(revision f3a7b0df8d6e499e8d47eeef3a165498df5ca509)
@@ -31,31 +31,20 @@
 BINUTILS_GDB_GIT="https://github.com/HelenOS/binutils-gdb.git"
 
-BINUTILS_BRANCH="binutils-2_41-helenos"
-BINUTILS_VERSION="2.41"
-
-GDB_BRANCH="gdb-13.2-helenos"
-GDB_VERSION="13.2"
+BINUTILS_BRANCH="binutils-2_31_1-helenos"
+BINUTILS_VERSION="2.31.1"
+
+GDB_BRANCH="gdb-8_2-helenos"
+GDB_VERSION="8.2"
 
 GCC_GIT="https://github.com/HelenOS/gcc.git"
-GCC_BRANCH="13_2_0-helenos"
-GCC_VERSION="13.2"
+GCC_BRANCH="8_2_0-helenos"
+GCC_VERSION="8.2.0"
 
 BASEDIR="$PWD"
 SRCDIR="$(readlink -f $(dirname "$0"))"
 
-# If we install into a temporary directory and copy from there,
-# we don't have to trust the upstream makefiles respect the prefix for
-# all files, and we also have a ready-made package for distribution.
-INSTALL_DIR="${BASEDIR}/PKG"
-
 SYSTEM_INSTALL=false
-
-BUILD_GDB=false
-BUILD_BINUTILS=true
-BUILD_GCC=true
-
-if [ -z "$JOBS" ] ; then
-	JOBS="`nproc`"
-fi
+REAL_INSTALL=true
+USE_HELENOS_TARGET=true
 
 check_error() {
@@ -72,5 +61,5 @@
 	echo
 	echo "Syntax:"
-	echo " $0 [--system-wide] [--with-gdb|--only-gdb] <platform>"
+	echo " $0 [--system-wide] [--no-install] [--non-helenos-target] <platform>"
 	echo " $0 [--system-wide] --test-version [<platform>]"
 	echo
@@ -87,4 +76,5 @@
 	echo " sparc64    SPARC V9"
 	echo " all        build all targets"
+	echo " essential  build only targets currently needed for HelenOS development"
 	echo " parallel   same as 'all', but all in parallel"
 	echo " 2-way      same as 'all', but 2-way parallel"
@@ -102,4 +92,11 @@
 	echo "     variable is not set, then the default value of \$HOME/.local/share"
 	echo "     is assumed."
+	echo
+	echo "If the --no-install option is used, the toolchain still uses the"
+	echo "target installation directory as determined above, but the files"
+	echo "are actually copied into the PKG/ subdirectory during the installation"
+	echo "without affecting the actual target file system. This might be useful"
+	echo "when preparing a system-wide installation, but avoiding running this"
+	echo "script under the super user."
 	echo
 	echo "The --non-helenos-target option will build non-HelenOS-specific"
@@ -142,5 +139,5 @@
 	for PLATFORM in $PLATFORMS ; do
 		set_target_from_platform "$PLATFORM"
-		PREFIX="${CROSS_PREFIX}/bin/${TARGET}"
+		PREFIX="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
 
 		echo "== $PLATFORM =="
@@ -180,16 +177,4 @@
 }
 
-ring_bell() {
-	printf '\a'
-	sleep 0.1
-	printf '\a'
-	sleep 0.1
-	printf '\a'
-	sleep 0.1
-	printf '\a'
-	sleep 0.1
-	printf '\a'
-}
-
 show_countdown() {
 	TM="$1"
@@ -256,17 +241,17 @@
 
 check_dirs() {
-	cd "${BASEDIR}"
-	check_error $? "Unable to change directory to ${BASEDIR}."
+	OUTSIDE="$1"
+	BASE="$2"
+	ORIGINAL="$PWD"
+
+	cd "${BASE}"
+	check_error $? "Unable to change directory to ${BASE}."
 	ABS_BASE="$PWD"
-
-	if $SYSTEM_INSTALL && [ ! -d "${CROSS_PREFIX}" ]; then
-		ring_bell
-		( set -x ; sudo -k mkdir -p "${CROSS_PREFIX}" )
-	else
-		mkdir -p "${CROSS_PREFIX}"
-	fi
-
-	cd "${CROSS_PREFIX}"
-	check_error $? "Unable to change directory to ${CROSS_PREFIX}."
+	cd "${ORIGINAL}"
+	check_error $? "Unable to change directory to ${ORIGINAL}."
+
+	mkdir -p "${OUTSIDE}"
+	cd "${OUTSIDE}"
+	check_error $? "Unable to change directory to ${OUTSIDE}."
 
 	while [ "${#PWD}" -gt "${#ABS_BASE}" ]; do
@@ -281,5 +266,6 @@
 	fi
 
-	cd "${BASEDIR}"
+	cd "${ORIGINAL}"
+	check_error $? "Unable to change directory to ${ORIGINAL}."
 }
 
@@ -294,33 +280,18 @@
 	change_title "Downloading sources"
 	echo ">>> Downloading sources"
-
-	if $BUILD_BINUTILS ; then
-		git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
-		# If the directory already existed, pull upstream changes.
-		git -C "binutils-$BINUTILS_VERSION" pull
-	fi
-
-	if $BUILD_GCC ; then
-		git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
-		git -C "gcc-$GCC_VERSION" pull
-
-		change_title "Downloading GCC prerequisites"
-		echo ">>> Downloading GCC prerequisites"
-		cd "gcc-${GCC_VERSION}"
-		./contrib/download_prerequisites
-		cd ..
-	fi
-
-	if $BUILD_GDB ; then
-		git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
-		git -C "gdb-$GDB_VERSION" pull
-	fi
-
-	# This sets the CROSS_PREFIX variable
-	set_cross_prefix
-
-	DESTDIR_SPEC="DESTDIR=${INSTALL_DIR}"
-
-	check_dirs
+	git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
+	git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
+	git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
+
+	# If the directory already existed, pull upstream changes.
+	git -C "binutils-$BINUTILS_VERSION" pull
+	git -C "gdb-$GDB_VERSION" pull
+	git -C "gcc-$GCC_VERSION" pull
+
+	change_title "Downloading GCC prerequisites"
+	echo ">>> Downloading GCC prerequisites"
+	cd "gcc-${GCC_VERSION}"
+	./contrib/download_prerequisites
+	cd ..
 }
 
@@ -350,27 +321,74 @@
 	esac
 
-	TARGET="${GNU_ARCH}-helenos"
-}
-
-build_binutils() {
-	# This sets the TARGET variable
-	set_target_from_platform "$1"
+	HELENOS_TARGET="${GNU_ARCH}-helenos"
+
+	case "$1" in
+		"arm32")
+			LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
+			;;
+		*)
+			LINUX_TARGET="${GNU_ARCH}-linux-gnu"
+			;;
+	esac
+}
+
+build_target() {
+	PLATFORM="$1"
+
+	# This sets the *_TARGET variables
+	set_target_from_platform "$PLATFORM"
+	if $USE_HELENOS_TARGET ; then
+		TARGET="$HELENOS_TARGET"
+	else
+		TARGET="$LINUX_TARGET"
+	fi
 
 	WORKDIR="${BASEDIR}/${TARGET}"
+	INSTALL_DIR="${BASEDIR}/PKG"
 	BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
+	GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
+	GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
+
+	# This sets the CROSS_PREFIX variable
+	set_cross_prefix
+
+	if [ -z "$JOBS" ] ; then
+		JOBS="`nproc`"
+	fi
+
+	PREFIX="${CROSS_PREFIX}"
 
 	echo ">>> Removing previous content"
 	cleanup_dir "${WORKDIR}"
 	mkdir -p "${WORKDIR}"
-
-	echo ">>> Processing binutils (${TARGET})"
+	check_dirs "${PREFIX}" "${WORKDIR}"
+
+	if $USE_HELENOS_TARGET ; then
+		change_title "Creating build sysroot"
+		echo ">>> Creating build sysroot"
+		mkdir -p "${WORKDIR}/sysroot/include"
+		mkdir "${WORKDIR}/sysroot/lib"
+		ARCH="$PLATFORM"
+		if [ "$ARCH" = "mips32eb" ]; then
+			ARCH=mips32
+		fi
+
+		cp -r -L -t "${WORKDIR}/sysroot/include" \
+			${SRCDIR}/../abi/include/* \
+			${SRCDIR}/../uspace/lib/c/arch/${ARCH}/include/* \
+			${SRCDIR}/../uspace/lib/c/include/*
+		check_error $? "Failed to create build sysroot."
+	fi
+
+
+	echo ">>> Processing binutils (${PLATFORM})"
 	mkdir -p "${BINUTILSDIR}"
 	cd "${BINUTILSDIR}"
 	check_error $? "Change directory failed."
 
-	change_title "binutils: configure (${TARGET})"
+	change_title "binutils: configure (${PLATFORM})"
 	CFLAGS="-Wno-error -fcommon" "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
 		"--target=${TARGET}" \
-		"--prefix=${CROSS_PREFIX}" \
+		"--prefix=${PREFIX}" \
 		"--program-prefix=${TARGET}-" \
 		--disable-nls \
@@ -382,35 +400,28 @@
 	check_error $? "Error configuring binutils."
 
-	change_title "binutils: make (${TARGET})"
+	change_title "binutils: make (${PLATFORM})"
 	make all -j$JOBS
 	check_error $? "Error compiling binutils."
 
-	change_title "binutils: install (${TARGET})"
-	make install $DESTDIR_SPEC
+	change_title "binutils: install (${PLATFORM})"
+	make install "DESTDIR=${INSTALL_DIR}"
 	check_error $? "Error installing binutils."
-}
-
-build_gcc() {
-	# This sets the TARGET variable
-	set_target_from_platform "$1"
-
-	WORKDIR="${BASEDIR}/${TARGET}"
-	GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
-
-	echo ">>> Removing previous content"
-	cleanup_dir "${WORKDIR}"
-	mkdir -p "${WORKDIR}"
-
-	echo ">>> Processing GCC (${TARGET})"
+
+
+	echo ">>> Processing GCC (${PLATFORM})"
 	mkdir -p "${GCCDIR}"
 	cd "${GCCDIR}"
 	check_error $? "Change directory failed."
 
-	BUILDPATH="${CROSS_PREFIX}/bin:${PATH}"
-
-	change_title "GCC: configure (${TARGET})"
-	PATH="${BUILDPATH}" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
+	if $USE_HELENOS_TARGET ; then
+		SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
+	else
+		SYSROOT=--without-headers
+	fi
+
+	change_title "GCC: configure (${PLATFORM})"
+	PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
 		"--target=${TARGET}" \
-		"--prefix=${CROSS_PREFIX}" \
+		"--prefix=${PREFIX}" \
 		"--program-prefix=${TARGET}-" \
 		--with-gnu-as \
@@ -421,98 +432,72 @@
 		--disable-shared \
 		--disable-werror \
-		--without-headers  # TODO: Replace with proper sysroot so we can build more libs
+		$SYSROOT
 	check_error $? "Error configuring GCC."
 
-	change_title "GCC: make (${TARGET})"
-	PATH="${BUILDPATH}" make all-gcc -j$JOBS
+	change_title "GCC: make (${PLATFORM})"
+	PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
 	check_error $? "Error compiling GCC."
 
-	change_title "GCC: install (${TARGET})"
-	PATH="${BUILDPATH}" make install-gcc $DESTDIR_SPEC
+	if $USE_HELENOS_TARGET ; then
+		PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
+		check_error $? "Error compiling libgcc."
+		# TODO: libatomic and libstdc++ need some extra care
+		#    PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
+		#    check_error $? "Error compiling libatomic."
+		#    PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
+		#    check_error $? "Error compiling libstdc++."
+	fi
+
+	change_title "GCC: install (${PLATFORM})"
+	PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
+	if $USE_HELENOS_TARGET ; then
+		PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
+		#    PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
+		#    PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
+	fi
 	check_error $? "Error installing GCC."
-}
-
-build_libgcc() {
-	# This sets the TARGET variable
-	set_target_from_platform "$1"
-
-	WORKDIR="${BASEDIR}/${TARGET}"
-	GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
-
-	# No removing previous content here, we need the previous GCC build
-
-	cd "${GCCDIR}"
-	check_error $? "Change directory failed."
-
-	BUILDPATH="${CROSS_PREFIX}/bin:${PATH}"
-
-	change_title "libgcc: make (${TARGET})"
-
-	PATH="${BUILDPATH}" make all-target-libgcc -j$JOBS
-	check_error $? "Error compiling libgcc."
-	# TODO: libatomic and libstdc++ need some extra care
-	#    PATH="${BUILDPATH}" make all-target-libatomic -j$JOBS
-	#    check_error $? "Error compiling libatomic."
-	#    PATH="${BUILDPATH}" make all-target-libstdc++-v3 -j$JOBS
-	#    check_error $? "Error compiling libstdc++."
-
-	change_title "libgcc: install (${TARGET})"
-
-	PATH="${BUILDPATH}" make install-target-libgcc $DESTDIR_SPEC
-	#    PATH="${BUILDPATH}" make install-target-libatomic $DESTDIR_SPEC
-	#    PATH="${BUILDPATH}" make install-target-libstdc++-v3 $DESTDIR_SPEC
-	check_error $? "Error installing libgcc."
-}
-
-build_gdb() {
-	# This sets the TARGET variable
-	set_target_from_platform "$1"
-
-	WORKDIR="${BASEDIR}/${TARGET}"
-	GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
-
-	echo ">>> Removing previous content"
-	cleanup_dir "${WORKDIR}"
-	mkdir -p "${WORKDIR}"
-
-	echo ">>> Processing GDB (${TARGET})"
+
+
+	echo ">>> Processing GDB (${PLATFORM})"
 	mkdir -p "${GDBDIR}"
 	cd "${GDBDIR}"
 	check_error $? "Change directory failed."
 
-	change_title "GDB: configure (${TARGET})"
-	CFLAGS="-fcommon" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
+	change_title "GDB: configure (${PLATFORM})"
+	CFLAGS="-fcommon" PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
 		"--target=${TARGET}" \
-		"--prefix=${CROSS_PREFIX}" \
+		"--prefix=${PREFIX}" \
 		"--program-prefix=${TARGET}-" \
 		--enable-werror=no
 	check_error $? "Error configuring GDB."
 
-	change_title "GDB: make (${TARGET})"
-	make all-gdb -j$JOBS
+	change_title "GDB: make (${PLATFORM})"
+	PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
 	check_error $? "Error compiling GDB."
 
-	change_title "GDB: install (${TARGET})"
-	make install-gdb $DESTDIR_SPEC
+	change_title "GDB: make (${PLATFORM})"
+	PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
 	check_error $? "Error installing GDB."
-}
-
-install_pkg() {
-	echo ">>> Moving to the destination directory."
-	if $SYSTEM_INSTALL ; then
-		ring_bell
-		( set -x ; sudo -k cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
-	else
-		( set -x ; cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
-	fi
-}
-
-link_clang() {
+
 	# Symlink clang and lld to the install path.
 	CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
 	LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
 
-	ln -s $CLANG "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-clang"
-	ln -s $LLD "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-ld.lld"
+	ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
+	ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
+
+	if $REAL_INSTALL ; then
+		echo ">>> Moving to the destination directory."
+		cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
+	fi
+
+	cd "${BASEDIR}"
+	check_error $? "Change directory failed."
+
+	echo ">>> Cleaning up"
+	cleanup_dir "${WORKDIR}"
+
+	echo
+	echo ">>> Cross-compiler for ${TARGET} installed."
 }
 
@@ -527,12 +512,10 @@
 			exit
 			;;
-		--with-gdb)
-			BUILD_GDB=true
+		--no-install)
+			REAL_INSTALL=false
 			shift
 			;;
-		--only-gdb)
-			BUILD_GDB=true
-			BUILD_BINUTILS=false
-			BUILD_GCC=false
+		--non-helenos-target)
+			USE_HELENOS_TARGET=false
 			shift
 			;;
@@ -546,80 +529,4 @@
 	show_usage
 fi
-
-PLATFORMS="amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64"
-
-run_one() {
-	$1 $PLATFORM
-}
-
-run_all() {
-	for x in $PLATFORMS ; do
-		$1 $x
-	done
-}
-
-run_parallel() {
-	for x in $PLATFORMS ; do
-		$1 $x &
-	done
-	wait
-}
-
-run_2way() {
-	$1 amd64 &
-	$1 arm32 &
-	wait
-
-	$1 arm64 &
-	$1 ia32 &
-	wait
-
-	$1 ia64 &
-	$1 mips32 &
-	wait
-
-	$1 mips32eb &
-	$1 ppc32 &
-	wait
-
-	$1 riscv64 &
-	$1 sparc64 &
-	wait
-}
-
-everything() {
-	RUNNER="$1"
-
-	prepare
-
-	if $BUILD_BINUTILS ; then
-		$RUNNER build_binutils
-
-		if $BUILD_GCC ; then
-			# gcc/libgcc may fail to build correctly if binutils is not installed first
-			echo ">>> Installing binutils"
-			install_pkg
-		fi
-	fi
-
-	if $BUILD_GCC ; then
-		$RUNNER build_gcc
-
-		# libgcc may fail to build correctly if gcc is not installed first
-		echo ">>> Installing GCC"
-		install_pkg
-
-		$RUNNER build_libgcc
-	fi
-
-	if $BUILD_GDB ; then
-		$RUNNER build_gdb
-	fi
-
-	echo ">>> Installing all files"
-	install_pkg
-
-	link_clang
-}
 
 case "$1" in
@@ -629,15 +536,67 @@
 		;;
 	amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
-		PLATFORM="$1"
-		everything run_one
+		prepare
+		build_target "$1"
 		;;
 	"all")
-		everything run_all
+		prepare
+		build_target "amd64"
+		build_target "arm32"
+		build_target "arm64"
+		build_target "ia32"
+		build_target "ia64"
+		build_target "mips32"
+		build_target "mips32eb"
+		build_target "ppc32"
+		build_target "riscv64"
+		build_target "sparc64"
+		;;
+	"essential")
+		prepare
+		build_target "amd64"
+		build_target "arm32"
+		build_target "arm64"
+		build_target "ia32"
+		build_target "ia64"
+		build_target "mips32"
+		build_target "mips32eb"
+		build_target "ppc32"
+		build_target "sparc64"
 		;;
 	"parallel")
-		everything run_parallel
+		prepare
+		build_target "amd64" &
+		build_target "arm32" &
+		build_target "arm64" &
+		build_target "ia32" &
+		build_target "ia64" &
+		build_target "mips32" &
+		build_target "mips32eb" &
+		build_target "ppc32" &
+		build_target "riscv64" &
+		build_target "sparc64" &
+		wait
 		;;
 	"2-way")
-		everything run_2way
+		prepare
+		build_target "amd64" &
+		build_target "arm32" &
+		wait
+
+		build_target "arm64" &
+		build_target "ia32" &
+		wait
+
+		build_target "ia64" &
+		build_target "mips32" &
+		wait
+
+		build_target "mips32eb" &
+		build_target "ppc32" &
+		wait
+
+		build_target "riscv64" &
+		build_target "sparc64" &
+		wait
 		;;
 	*)
