Index: tools/toolchain.sh
===================================================================
--- tools/toolchain.sh	(revision ce52c333dd9013cd4e9c22121ab41c36aee9c7d4)
+++ tools/toolchain.sh	(revision 252d03cdce4fb131247f5dd8f472481f4b7c3417)
@@ -41,7 +41,8 @@
 GCC_VERSION="8.2.0"
 
-BASEDIR="`pwd`"
+BASEDIR="$PWD"
 SRCDIR="$(readlink -f $(dirname "$0"))"
 
+SYSTEM_INSTALL=false
 REAL_INSTALL=true
 USE_HELENOS_TARGET=true
@@ -57,9 +58,9 @@
 
 show_usage() {
-	echo "Cross-compiler toolchain build script"
+	echo "HelenOS cross-compiler toolchain build script"
 	echo
 	echo "Syntax:"
-	echo " $0 [--no-install] [--non-helenos-target] <platform>"
-	echo " $0 --test-version [<platform>]"
+	echo " $0 [--system-wide] [--no-install] [--non-helenos-target] <platform>"
+	echo " $0 [--system-wide] --test-version [<platform>]"
 	echo
 	echo "Possible target platforms are:"
@@ -79,28 +80,55 @@
 	echo " 2-way      same as 'all', but 2-way parallel"
 	echo
-	echo "The toolchain is installed into directory specified by the"
-	echo "CROSS_PREFIX environment variable. If the variable is not"
-	echo "defined, /usr/local/cross/ is used as default."
-	echo
-	echo "If --no-install is present, the toolchain still uses the"
-	echo "CROSS_PREFIX as the target directory but the installation"
-	echo "copies the files into PKG/ subdirectory without affecting"
-	echo "the actual root file system. That is only useful if you do"
-	echo "not want to run the script under the super user."
-	echo
-	echo "The --non-helenos-target will build non-HelenOS-specific toolchain"
-	echo "(i.e. it will use *-linux-* triplet instead of *-helenos)."
+	echo "The toolchain target installation directory is determined by matching"
+	echo "the first of the following conditions:"
+	echo
+	echo " (1) If the \$CROSS_PREFIX environment variable is set, then it is"
+	echo "     used as the target installation directory."
+	echo " (2) If the --system-wide option is used, then /opt/HelenOS/cross"
+	echo "     is used as the target installation directory. This usually"
+	echo "     requires running this script with super user privileges."
+	echo " (3) In other cases, \$XDG_DATA_HOME/HelenOS/cross is used as the"
+	echo "     target installation directory. If the \$XDG_DATA_HOME environment"
+	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"
+	echo "toolchain (i.e. it will use *-linux-* triplet instead of *-helenos)."
 	echo "Using this toolchain for building HelenOS is not supported."
 	echo
+	echo "The --test-version mode tests the currently installed version of the"
+	echo "toolchain."
 
 	exit 3
 }
 
+set_cross_prefix() {
+	if [ -z "$CROSS_PREFIX" ] ; then
+		if $SYSTEM_INSTALL ; then
+			CROSS_PREFIX="/opt/HelenOS/cross"
+		else
+			if [ -z "$XDG_DATA_HOME" ] ; then
+				XDG_DATA_HOME="$HOME/.local/share"
+			fi
+			CROSS_PREFIX="$XDG_DATA_HOME/HelenOS/cross"
+		fi
+	fi
+}
+
 test_version() {
-	echo "Cross-compiler toolchain build script"
-	echo
-	echo "Start testing the version of the installed software" 
-	echo
-	
+	set_cross_prefix
+
+	echo "HelenOS cross-compiler toolchain build script"
+	echo
+	echo "Testing the version of the installed software in $CROSS_PREFIX"
+	echo
+
 	if [ -z "$1" ] || [ "$1" = "all" ] ; then
 		PLATFORMS='amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64'
@@ -108,13 +136,6 @@
 		PLATFORMS="$1"
 	fi
-	
-	
-	if [ -z "${CROSS_PREFIX}" ] ; then
-		CROSS_PREFIX="/usr/local/cross"
-	fi
-
-	for i in $PLATFORMS
-	do
-		PLATFORM="$i"
+
+	for PLATFORM in $PLATFORMS ; do
 		set_target_from_platform "$PLATFORM"
 		PREFIX="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
@@ -125,6 +146,4 @@
 		test_app_version "GDB" "gdb" "GNU gdb (.*)[[:space:]]\+\([.0-9]*\)" "$GDB_VERSION"
 	done
-
-	exit
 }
 
@@ -135,5 +154,4 @@
 	INS_VERSION="$4"
 
-
 	APP="${PREFIX}-${APPNAME}"
 	if [ ! -e $APP ]; then
@@ -148,5 +166,5 @@
 
 		if [ "$INS_VERSION" = "$VERSION" ]; then
-			echo "+ $PKGNAME is uptodate ($INS_VERSION)"
+			echo "+ $PKGNAME is up-to-date ($INS_VERSION)"
 		else
 			echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
@@ -155,6 +173,4 @@
 }
 
-
-
 change_title() {
 	printf "\e]0;$1\a"
@@ -178,4 +194,11 @@
 
 show_dependencies() {
+	set_cross_prefix
+
+	echo "HelenOS cross-compiler toolchain build script"
+	echo
+	echo "Installing software to $CROSS_PREFIX"
+	echo
+	echo
 	echo "IMPORTANT NOTICE:"
 	echo
@@ -255,4 +278,5 @@
 	check_error $? "Change directory failed."
 
+	change_title "Downloading sources"
 	echo ">>> Downloading sources"
 	git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
@@ -265,4 +289,5 @@
 	git -C "gcc-$GCC_VERSION" pull
 
+	change_title "Downloading GCC prerequisites"
 	echo ">>> Downloading GCC prerequisites"
 	cd "gcc-${GCC_VERSION}"
@@ -325,10 +350,9 @@
 	GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
 
-	if [ -z "${CROSS_PREFIX}" ] ; then
-		CROSS_PREFIX="/usr/local/cross"
-	fi
+	# This sets the CROSS_PREFIX variable
+	set_cross_prefix
 
 	if [ -z "$JOBS" ] ; then
-		JOBS=`nproc`
+		JOBS="`nproc`"
 	fi
 
@@ -341,4 +365,5 @@
 
 	if $USE_HELENOS_TARGET ; then
+		change_title "Creating build sysroot"
 		echo ">>> Creating build sysroot"
 		mkdir -p "${WORKDIR}/sysroot/include"
@@ -355,4 +380,5 @@
 		check_error $? "Failed to create build sysroot."
 	fi
+
 
 	echo ">>> Processing binutils (${PLATFORM})"
@@ -416,9 +442,9 @@
 		PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
 		check_error $? "Error compiling libgcc."
-		# TODO: needs 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++."
+		# 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
 
@@ -427,6 +453,6 @@
 	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}"
+		#    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."
@@ -463,5 +489,4 @@
 	if $REAL_INSTALL ; then
 		echo ">>> Moving to the destination directory."
-		echo cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
 		cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
 	fi
@@ -479,4 +504,8 @@
 while [ "$#" -gt 1 ] ; do
 	case "$1" in
+		--system-wide)
+			SYSTEM_INSTALL=true
+			shift
+			;;
 		--test-version)
 			test_version "$2"
@@ -504,4 +533,5 @@
 	--test-version)
 		test_version
+		exit
 		;;
 	amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
