#!@@CT_bash@@
# Copyright 2007 Yann E. MORIN
# Licensed under the GPL v2. See COPYING in the root of this package.

# This is the main entry point to crosstool
# This will:
#   - download, extract and patch the toolchain components
#   - build and install each components in turn
#   - and eventually test the resulting toolchain

# What this file does is prepare the environment, based upon the user-choosen
# options. It also checks the existing environment for un-friendly variables,
# and builds the tools.

# Parse the common functions
# Note: some initialisation and sanitizing is done while parsing this file,
# most notably:
#  - set trap handler on errors,
#  - don't hash commands lookups,
#  - initialise logging.
. "${CT_LIB_DIR}/scripts/functions"

# Parse the configuration file
# It has some info about the logging facility, so include it early
. .config.2
# Yes! We can do full logging from now on!

# Override the locale early, in case we ever translate crosstool-NG messages
if [ -z "${CT_NO_OVERIDE_LC_MESSAGES}" ]; then
    export LC_ALL=C
    export LANG=C
fi

# remove . from PATH since it can cause gcc build failures
CT_SanitizePath

# Some sanity checks in the environment and needed tools
CT_DoLog INFO "Performing some trivial sanity checks"
CT_TestAndAbort "Don't set LD_LIBRARY_PATH. It screws up the build." -n "${LD_LIBRARY_PATH}"
CT_TestAndAbort "Don't set CFLAGS. It screws up the build." -n "${CFLAGS}"
CT_TestAndAbort "Don't set CXXFLAGS. It screws up the build." -n "${CXXFLAGS}"
CT_Test "GREP_OPTIONS screws up the build. Resetting." -n "${GREP_OPTIONS}"
export GREP_OPTIONS=

# Some sanity checks on paths content
for d in            \
    LOCAL_TARBALLS  \
    WORK            \
    PREFIX          \
    INSTALL         \
    ; do
        eval dir="\${CT_${d}_DIR}"
        case "${dir}" in
            *" "*)
                CT_Abort "'CT_${d}_DIR'='${dir}' contains a space in it.\nDon't use spaces in paths, it breaks things."
                ;;
        esac
done

# Where will we work?
CT_WORK_DIR="${CT_WORK_DIR:-${CT_TOP_DIR}/targets}"
CT_DoExecLog ALL mkdir -p "${CT_WORK_DIR}"

# Check build file system case-sensitiveness
CT_DoExecLog DEBUG touch "${CT_WORK_DIR}/foo"
CT_TestAndAbort "Your file system in '${CT_WORK_DIR}' is *not* case-sensitive!" -f "${CT_WORK_DIR}/FOO"
CT_DoExecLog DEBUG rm -f "${CT_WORK_DIR}/foo"

# Check the user is using an existing SHELL to be used by ./configure and Makefiles
CT_TestOrAbort "The CONFIG_SHELL '${CT_CONFIG_SHELL}' is not valid" -f "${CT_CONFIG_SHELL}" -a -x "${CT_CONFIG_SHELL}"

# Create the bin-override early
# Contains symlinks to the tools found by ./configure
# Note: CT_DoLog and CT_DoExecLog do not use any of those tool, so
# they can be safely used
CT_TOOLS_OVERIDE_DIR="${CT_WORK_DIR}/tools"
CT_DoLog DEBUG "Creating bin-override for tools in '${CT_TOOLS_OVERIDE_DIR}'"
CT_DoExecLog DEBUG mkdir -p "${CT_TOOLS_OVERIDE_DIR}/bin"
cat "${CT_LIB_DIR}/paths.mk" |while read trash line; do
    tool="${line%%=*}"
    path="${line#*=}"
    CT_DoLog DEBUG "Creating script-override for '${tool}' -> '${path}'"
    printf "#${BANG}${CT_SHELL}\nexec '${path}' \"\${@}\"\n" >"${CT_TOOLS_OVERIDE_DIR}/bin/${tool}"
    CT_DoExecLog ALL chmod 700 "${CT_TOOLS_OVERIDE_DIR}/bin/${tool}"
done
export PATH="${CT_TOOLS_OVERIDE_DIR}/bin:${PATH}"

# Start date. Can't be done until we know the locale
# Also requires the bin-override tools
CT_STAR_DATE=$(CT_DoDate +%s%N)
CT_STAR_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)

# Log real begining of build, now
CT_DoLog INFO "Build started ${CT_STAR_DATE_HUMAN}"

# We really need to extract from ,config and not .config.2, as we
# do want the kconfig's values, not our mangled config with arrays.
CT_DoStep DEBUG "Dumping user-supplied crosstool-NG configuration"
CT_DoExecLog DEBUG grep -E '^(# |)CT_' .config
CT_EndStep

CT_DoLog DEBUG "Unsetting and unexporting MAKEFLAGS"
unset MAKEFLAGS
export MAKEFLAGS

CT_DoLog INFO "Building environment variables"

# Include sub-scripts instead of calling them: that way, we do not have to
# export any variable, nor re-parse the configuration and functions files.
. "${CT_LIB_DIR}/scripts/build/internals.sh"
. "${CT_LIB_DIR}/scripts/build/arch/${CT_ARCH}.sh"
. "${CT_LIB_DIR}/scripts/build/companion_tools.sh"
. "${CT_LIB_DIR}/scripts/build/kernel/${CT_KERNEL}.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/gmp.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/mpfr.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/ppl.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/cloog.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/mpc.sh"
. "${CT_LIB_DIR}/scripts/build/companion_libs/libelf.sh"
. "${CT_LIB_DIR}/scripts/build/binutils/binutils.sh"
. "${CT_LIB_DIR}/scripts/build/binutils/elf2flt.sh"
. "${CT_LIB_DIR}/scripts/build/binutils/sstrip.sh"
. "${CT_LIB_DIR}/scripts/build/libc/${CT_LIBC}.sh"
. "${CT_LIB_DIR}/scripts/build/cc/${CT_CC}.sh"
. "${CT_LIB_DIR}/scripts/build/debug.sh"
. "${CT_LIB_DIR}/scripts/build/test_suite.sh"

# Target tuple: CT_TARGET needs a little love:
CT_DoBuildTargetTuple

# Kludge: If any of the configured options needs CT_TARGET,
# then rescan the options file now:
. .config.2

# Sanity check some directories
CT_TestAndAbort "'CT_PREFIX_DIR' is not set: where should I install?" -z "${CT_PREFIX_DIR}"

# Second kludge: merge user-supplied target CFLAGS with architecture-provided
# target CFLAGS. Do the same for LDFLAGS in case it happens in the future.
# Put user-supplied flags at the end, so that they take precedence.
CT_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_TARGET_CFLAGS}"
CT_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_TARGET_LDFLAGS}"
CT_CC_CORE_EXTRA_CONFIG_ARRAY=( ${CT_ARCH_CC_CORE_EXTRA_CONFIG} "${CT_CC_CORE_EXTRA_CONFIG_ARRAY[@]}" )
CT_CC_EXTRA_CONFIG_ARRAY=( ${CT_ARCH_CC_EXTRA_CONFIG} "${CT_CC_EXTRA_CONFIG_ARRAY[@]}" )

# Compute the package version string
CT_PKGVERSION="crosstool-NG ${CT_VERSION}${CT_TOOLCHAIN_PKGVERSION:+ - ${CT_TOOLCHAIN_PKGVERSION}}"

# Compute the working directories names
CT_TARBALLS_DIR="${CT_WORK_DIR}/tarballs"
CT_SRC_DIR="${CT_WORK_DIR}/src"
CT_BUILD_DIR="${CT_WORK_DIR}/${CT_TARGET}/build"
CT_BUILDTOOLS_PREFIX_DIR="${CT_WORK_DIR}/${CT_TARGET}/buildtools"
CT_STATE_DIR="${CT_WORK_DIR}/${CT_TARGET}/state"
CT_CONFIG_DIR="${CT_BUILD_DIR}/configs"
CT_COMPLIBS_DIR="${CT_BUILD_DIR}/static"

# Compute test suite install directory
CT_TEST_SUITE_DIR=${CT_INSTALL_DIR}/test-suite

# Note: we'll always install the core compiler in its own directory, so as to
# not mix the two builds: core and final.
CT_CC_CORE_STATIC_PREFIX_DIR="${CT_BUILD_DIR}/${CT_CC}-core-static"
CT_CC_CORE_SHARED_PREFIX_DIR="${CT_BUILD_DIR}/${CT_CC}-core-shared"

# We must ensure that we can restart if asked for!
if [ -n "${CT_RESTART}" -a ! -d "${CT_STATE_DIR}"  ]; then
    CT_DoLog ERROR "You asked to restart a non-restartable build"
    CT_DoLog ERROR "This happened because you didn't set CT_DEBUG_CT_SAVE_STEPS"
    CT_DoLog ERROR "in the config options for the previous build, or the state"
    CT_DoLog ERROR "directory for the previous build was deleted."
    CT_Abort "I will stop here to avoid any carnage"
fi

# If the local tarball directory does not exist, say so, and don't try to save there!
if [    "${CT_SAVE_TARBALLS}" = "y"     \
     -a ! -d "${CT_LOCAL_TARBALLS_DIR}" ]; then
    CT_DoLog WARN "Directory '${CT_LOCAL_TARBALLS_DIR}' does not exist."
    CT_DoLog WARN "Will not save downloaded tarballs to local storage."
    CT_SAVE_TARBALLS=
fi

# Check now if we can write to the destination directory:
if [ -d "${CT_INSTALL_DIR}" ]; then
    CT_TestAndAbort "Destination directory '${CT_INSTALL_DIR}' is not removable" ! -w $(dirname "${CT_INSTALL_DIR}")
fi

# Good, now grab a bit of informations on the system we're being run on,
# just in case something goes awok, and it's not our fault:
CT_SYS_USER=$(id -un)
CT_SYS_HOSTNAME=$(hostname -f 2>/dev/null || true)
# Hmmm. Some non-DHCP-enabled machines do not have an FQDN... Fall back to node name.
CT_SYS_HOSTNAME="${CT_SYS_HOSTNAME:-$(uname -n)}"
CT_SYS_KERNEL=$(uname -s)
CT_SYS_REVISION=$(uname -r)
CT_SYS_OS=$(uname -s)
CT_SYS_MACHINE=$(uname -m)
CT_SYS_PROCESSOR=$(uname -p)
CT_SYS_GCC=$(gcc -dumpversion)
CT_SYS_TARGET=$(CT_DoConfigGuess)
CT_TOOLCHAIN_ID="crosstool-${CT_VERSION} build ${CT_STAR_DATE_HUMAN} by ${CT_SYS_USER}@${CT_SYS_HOSTNAME}"

CT_DoLog EXTRA "Preparing working directories"

# Ah! The build directory shall be eradicated, even if we restart!
# Ditto for the build tools install dir
CT_DoForceRmdir "${CT_BUILD_DIR}" "${CT_BUILDTOOLS_PREFIX_DIR}"

# Don't eradicate directories if we need to restart
if [ -z "${CT_RESTART}" ]; then
    # Get rid of pre-existing installed toolchain and previous build directories.
    if [ "${CT_FORCE_DOWNLOAD}" = "y" -a -d "${CT_TARBALLS_DIR}" ]; then
        CT_DoForceRmdir "${CT_TARBALLS_DIR}"
    fi
    if [ "${CT_FORCE_EXTRACT}" = "y" -a -d "${CT_SRC_DIR}" ]; then
        CT_DoForceRmdir "${CT_SRC_DIR}"
    fi
    if [ -d "${CT_INSTALL_DIR}" -a "${CT_RM_RF_PREFIX_DIR}" = "y" ]; then
        CT_DoForceRmdir "${CT_INSTALL_DIR}"
    fi
    # In case we start anew, get rid of the previously saved state directory
    if [ -d "${CT_STATE_DIR}" ]; then
        CT_DoForceRmdir "${CT_STATE_DIR}"
    fi
fi

# Create the directories we'll use, even if restarting: it does no harm to
# create already existent directories, and CT_BUILD_DIR needs to be created
# anyway
CT_DoExecLog ALL mkdir -p "${CT_TARBALLS_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_SRC_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_BUILD_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_BUILDTOOLS_PREFIX_DIR}/bin"
CT_DoExecLog ALL mkdir -p "${CT_CONFIG_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_INSTALL_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_PREFIX_DIR}"
CT_DoExecLog ALL mkdir -p "${CT_COMPLIBS_DIR}"
if [ -z "${CT_CANADIAN}" ]; then
    CT_DoExecLog ALL mkdir -p "${CT_CC_CORE_STATIC_PREFIX_DIR}"
    CT_DoExecLog ALL mkdir -p "${CT_CC_CORE_SHARED_PREFIX_DIR}"
fi

# Only create the state dir if asked for a restartable build
[ -n "${CT_DEBUG_CT_SAVE_STEPS}" ] && CT_DoExecLog ALL mkdir -p "${CT_STATE_DIR}"

# Check install file system case-sensitiveness
CT_DoExecLog DEBUG touch "${CT_PREFIX_DIR}/foo"
CT_TestAndAbort "Your file system in '${CT_PREFIX_DIR}' is *not* case-sensitive!" -f "${CT_PREFIX_DIR}/FOO"
CT_DoExecLog DEBUG rm -f "${CT_PREFIX_DIR}/foo"

# Kludge: CT_INSTALL_DIR and CT_PREFIX_DIR might have grown read-only if
# the previous build was successful.
CT_DoExecLog ALL chmod -R u+w "${CT_INSTALL_DIR}" "${CT_PREFIX_DIR}"

# Setting up the rest of the environment only if not restarting
if [ -z "${CT_RESTART}" ]; then
    case "${CT_SYSROOT_NAME}" in
        "")     CT_SYSROOT_NAME="sysroot";;
        .)      CT_Abort "Sysroot name is set to '.' which is forbidden";;
        *' '*)  CT_Abort "Sysroot name contains forbidden space(s): '${CT_SYSROOT_NAME}'";;
        */*)    CT_Abort "Sysroot name contains forbidden slash(es): '${CT_SYSROOT_NAME}'";;
    esac

    # Arrange paths depending on wether we use sysroot or not.
    if [ "${CT_USE_SYSROOT}" = "y" ]; then
        CT_SYSROOT_REL_DIR="${CT_SYSROOT_DIR_PREFIX:+${CT_SYSROOT_DIR_PREFIX}/}${CT_SYSROOT_NAME}"
        CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}/${CT_SYSROOT_REL_DIR}"
        CT_DEBUGROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}/${CT_SYSROOT_DIR_PREFIX}/debug-root"
        CT_HEADERS_DIR="${CT_SYSROOT_DIR}/usr/include"
        CT_SanitiseVarDir CT_SYSROOT_REL_DIR CT_SYSROOT_DIR CT_DEBUGROOT_DIR CT_HEADERS_DIR 
        BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
        CC_CORE_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
        CC_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
        LIBC_SYSROOT_ARG=""
        # glibc's prefix must be exactly /usr, else --with-sysroot'd gcc will get
        # confused when $sysroot/usr/include is not present.
        # Note: --prefix=/usr is magic!
        # See http://www.gnu.org/software/libc/FAQ.html#s-2.2
    else
        # plain old way. All libraries in prefix/target/lib
        CT_SYSROOT_DIR="${CT_PREFIX_DIR}/${CT_TARGET}"
        CT_DEBUGROOT_DIR="${CT_SYSROOT_DIR}"
        CT_HEADERS_DIR="${CT_SYSROOT_DIR}/include"
        CT_SanitiseVarDir CT_SYSROOT_DIR CT_DEBUGROOT_DIR CT_HEADERS_DIR 
        # hack!  Always use --with-sysroot for binutils.
        # binutils 2.14 and later obey it, older binutils ignore it.
        # Lets you build a working 32->64 bit cross gcc
        BINUTILS_SYSROOT_ARG="--with-sysroot=${CT_SYSROOT_DIR}"
        # Use --with-headers, else final gcc will define disable_glibc while
        # building libgcc, and you'll have no profiling
        CC_CORE_SYSROOT_ARG="--without-headers"
        CC_SYSROOT_ARG="--with-headers=${CT_HEADERS_DIR}"
        LIBC_SYSROOT_ARG="prefix="
    fi
    CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}"
    CT_DoExecLog ALL mkdir -p "${CT_DEBUGROOT_DIR}"

    # Prepare the 'lib' directories in sysroot, else the ../lib64 hack used by
    # 32 -> 64 bit crosscompilers won't work, and build of final gcc will fail
    # with: "ld: cannot open crti.o: No such file or directory"
    # Also prepare the lib directory in the install dir, else some 64 bit archs
    # won't build
    CT_DoExecLog ALL mkdir -p "${CT_PREFIX_DIR}/lib"
    CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}/lib"
    CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}/usr/lib"
    CT_DoExecLog ALL mkdir -p "${CT_SYSROOT_DIR}/usr/include"

    if [ "${CT_USE_SYSROOT}" = "y" ]; then
        # Prevent gcc from installing its libraries outside of the sysroot
        CT_Pushd "${CT_PREFIX_DIR}/${CT_TARGET}"
        CT_DoExecLog ALL ln -sf "${CT_SYSROOT_REL_DIR}/lib" "lib"
        CT_Popd
    fi

    # Since we're *not* multilib on the target side, we want all the
    # libraries to end up in "lib".  We create "lib64" (for 64-bit
    # build or host architectures) and "lib32" (for 32-bit emulation
    # on 64-bit) as symlinks to "lib".
    #
    # Not all of these symlinks are necessary, but better safe than
    # sorry. They are summarily removed by build/internals.sh:do_finish.
    for d in                            \
        "${CT_PREFIX_DIR}"              \
        "${CT_SYSROOT_DIR}"             \
        "${CT_SYSROOT_DIR}/usr"         \
        "${CT_PREFIX_DIR}/${CT_TARGET}" \
    ; do
        CT_DoExecLog ALL ln -sf "lib" "${d}/lib32"
        CT_DoExecLog ALL ln -sf "lib" "${d}/lib64"
    done

    # Determine build system if not set by the user
    if [ -z "${CT_BUILD}" ]; then
        CT_BUILD=$(CT_DoConfigGuess)
    fi

    # Prepare mangling patterns to later modify BUILD and HOST (see below)
    case "${CT_TOOLCHAIN_TYPE}" in
        cross)
            # A cross-compiler runs on the same machine it is built on
            CT_HOST="${CT_BUILD}"
            build_mangle="build_"
            host_mangle="build_"
            target_mangle=""
            install_build_tools_for="BUILD HOST"
            ;;
        canadian)
            build_mangle="build_"
            host_mangle="host_"
            target_mangle=""
            install_build_tools_for="BUILD HOST TARGET"
            ;;
        *)  CT_Abort "No code for '${CT_TOOLCHAIN_TYPE}' toolchain type!"
            ;;
    esac

    # Save the real tuples to generate shell-wrappers to the real tools
    CT_REAL_BUILD="${CT_BUILD}"
    CT_REAL_HOST="${CT_HOST}"
    CT_REAL_TARGET="${CT_TARGET}"

    # Canonicalise CT_BUILD and CT_HOST
    # Not only will it give us full-qualified tuples, but it will also ensure
    # that they are valid tuples (in case of typo with user-provided tuples)
    # That's way better than trying to rewrite config.sub ourselves...
    # CT_TARGET is already made canonical in CT_DoBuildTargetTuple
    CT_BUILD=$(CT_DoConfigSub "${CT_BUILD}")
    CT_HOST=$(CT_DoConfigSub "${CT_HOST}")

    # Modify BUILD and HOST so that gcc always generate a cross-compiler
    # even if any of the build, host or target machines are the same.
    # NOTE: we'll have to mangle the (BUILD|HOST)->TARGET x-compiler to
    #       support canadain build, later...
    CT_BUILD="${CT_BUILD/-/-${build_mangle}}"
    CT_HOST="${CT_HOST/-/-${host_mangle}}"
    CT_TARGET="${CT_TARGET/-/-${target_mangle}}"

    # Now we have mangled our BUILD and HOST tuples, we must fake the new
    # cross-tools for those mangled tuples.
    CT_DoLog DEBUG "Making build system tools available"
    for m in ${install_build_tools_for}; do
        r="CT_REAL_${m}"
        v="CT_${m}"
        p="CT_${m}_PREFIX"
        s="CT_${m}_SUFFIX"
        if [ -n "${!p}" ]; then
            t="${!p}"
        else
            t="${!r}-"
        fi

        for tool in ar as dlltool gcc g++ gcj gnatbind gnatmake ld nm objcopy objdump ranlib strip windres; do
            # First try with prefix + suffix
            # Then try with prefix only
            # Then try with suffix only, but only for BUILD, and HOST iff REAL_BUILD == REAL_HOST
            # Finally try with neither prefix nor suffix, but only for BUILD, and HOST iff REAL_BUILD == REAL_HOST
            # This is needed, because some tools have a prefix and
            # a suffix (eg. gcc), while others may have only one,
            # or even none (eg. binutils)
            where=$(CT_Which "${t}${tool}${!s}")
            [ -z "${where}" ] && where=$(CT_Which "${t}${tool}")
            if [    -z "${where}"                         \
                 -a \(    "${m}" = "BUILD"                \
                       -o "${CT_REAL_BUILD}" = "${!r}" \) ]; then
                where=$(CT_Which "${tool}${!s}")
            fi
            if [ -z "${where}"                            \
                 -a \(    "${m}" = "BUILD"                \
                       -o "${CT_REAL_BUILD}" = "${!r}" \) ]; then
                where=$(CT_Which "${tool}")
            fi

            # Not all tools are available for all platforms, but some are really,
            # bally needed
            if [ -n "${where}" ]; then
                CT_DoLog DEBUG "  '${!v}-${tool}' -> '${where}'"
                printf "#${BANG}${CT_SHELL}\nexec '${where}' \"\${@}\"\n" >"${CT_BUILDTOOLS_PREFIX_DIR}/bin/${!v}-${tool}"
                CT_DoExecLog ALL chmod 700 "${CT_BUILDTOOLS_PREFIX_DIR}/bin/${!v}-${tool}"
            else
                case "${tool}" in
                    # We'll at least need some of them...
                    ar|as|gcc|ld|nm|objcopy|objdump|ranlib)
                        CT_Abort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!"
                        ;;
                    # Some are conditionnally required
                    # Add them in alphabetical (C locale) ordering
                    g++)
                        # g++ (needed for companion lib), only needed for HOST
                        CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${m}" = "HOST"
                        ;;
                    gcj)
                        CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${CT_CC_LANG_JAVA}" = "y"
                        ;;
                    strip)
                        CT_TestAndAbort "Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : either needed!" "${CT_STRIP_ALL_TOOLCHAIN_EXECUTABLES}" = "y"
                        ;;
                    # If any other is missing, only warn at low level
                    *)
                        # It does not deserve a WARN level.
                        CT_DoLog DEBUG "  Missing: '${t}${tool}${!s}' or '${t}${tool}' or '${tool}' : not required."
                        ;;
                esac
            fi
        done
    done

    # Some makeinfo versions are a pain in [put your most sensible body part here].
    # Go ahead with those, by creating a wrapper that keeps partial files, and that
    # never fails:
    CT_DoLog DEBUG "  'makeinfo' -> '$(CT_Which makeinfo)'"
    printf "#${BANG}${CT_SHELL}\n$(CT_Which makeinfo) --force \"\${@}\"\ntrue\n" >"${CT_BUILDTOOLS_PREFIX_DIR}/bin/makeinfo"
    CT_DoExecLog ALL chmod 700 "${CT_BUILDTOOLS_PREFIX_DIR}/bin/makeinfo"

    # Carefully add paths in the order we want them:
    #  - first try in ${CT_PREFIX_DIR}/bin
    #  - then try in ${CT_CC_CORE_SHARED_PREFIX_DIR}/bin
    #  - then try in ${CT_CC_CORE_STATIC_PREFIX_DIR}/bin
    #  - fall back to searching user's PATH
    # Of course, neither cross-native nor canadian can run on BUILD,
    # so don't add those PATHs in this case...
    case "${CT_TOOLCHAIN_TYPE}" in
        cross)  export PATH="${CT_BUILDTOOLS_PREFIX_DIR}/bin:${CT_PREFIX_DIR}/bin:${CT_CC_CORE_SHARED_PREFIX_DIR}/bin:${CT_CC_CORE_STATIC_PREFIX_DIR}/bin:${PATH}";;
        canadian) export PATH="${CT_BUILDTOOLS_PREFIX_DIR}/bin:${PATH}";;
        *)  ;;
    esac

    # Now we know our host and where to find the host tools, we can check
    # if static link was requested, but only if it was requested
    if [ "${CT_WANTS_STATIC_LINK}" = "y" ]; then
        tmp="${CT_BUILD_DIR}/.static-test"
        if ! "${CT_HOST}-gcc" -xc - -static -o "${tmp}" >/dev/null 2>&1 <<-_EOF_
				int main() { return 0; }
			_EOF_
        then
            CT_Abort "Static linking impossible on the host system '${CT_HOST}'"
        fi
        rm -f "${tmp}"
    fi

    # Help gcc
    CT_CFLAGS_FOR_HOST=
    [ "${CT_USE_PIPES}" = "y" ] && CT_CFLAGS_FOR_HOST+=" -pipe"
    CT_CFLAGS_FOR_HOST+=" ${CT_EXTRA_FLAGS_FOR_HOST}"

    # Set the shell to be used by ./configure scripts and by Makefiles (those
    # that support it!).
    export CONFIG_SHELL="${CT_CONFIG_SHELL}"    # for ./configure
    export SHELL="${CT_CONFIG_SHELL}"           # for Makefiles

    # And help make go faster
    JOBSFLAGS=
    # Override the configured jobs with what's been given on the command line
    [ -n "${CT_JOBS}" ] && CT_PARALLEL_JOBS="${CT_JOBS}"
    # Use the number of processors+1 when automatically setting the number of
    # parallel jobs.  Fall back to 1 if the host doesn't use GLIBC.
    AUTO_JOBS=$((`getconf _NPROCESSORS_ONLN 2> /dev/null || echo 0` + 1))
    [ ${CT_PARALLEL_JOBS} -eq 0 ] && JOBSFLAGS="${JOBSFLAGS} -j${AUTO_JOBS}"
    [ ${CT_PARALLEL_JOBS} -gt 0 ] && JOBSFLAGS="${JOBSFLAGS} -j${CT_PARALLEL_JOBS}"
    [ ${CT_LOAD} -ne 0 ] && JOBSFLAGS="${JOBSFLAGS} -l${CT_LOAD}"

    # We need to save the real .config with kconfig's value,
    # not our mangled .config.2 with arrays.
    CT_DoLog EXTRA "Installing user-supplied crosstool-NG configuration"
    CT_DoExecLog ALL mkdir -p "${CT_PREFIX_DIR}/bin"
    CT_DoExecLog DEBUG install -m 0755 "${CT_LIB_DIR}/scripts/toolchain-config.in" "${CT_PREFIX_DIR}/bin/${CT_TARGET}-ct-ng.config"
    CT_DoExecLog DEBUG sed -i -e 's,@@grep@@,"'"${grep}"'",;' "${CT_PREFIX_DIR}/bin/${CT_TARGET}-ct-ng.config"
    bzip2 -c -9 .config >>"${CT_PREFIX_DIR}/bin/${CT_TARGET}-ct-ng.config"

    CT_DoStep EXTRA "Dumping internal crosstool-NG configuration"
    CT_DoLog EXTRA "Building a toolchain for:"
    CT_DoLog EXTRA "  build  = ${CT_REAL_BUILD}"
    CT_DoLog EXTRA "  host   = ${CT_REAL_HOST}"
    CT_DoLog EXTRA "  target = ${CT_TARGET}"
    set |grep -E '^CT_.+=' |sort |CT_DoLog DEBUG
    CT_DoLog DEBUG "Other environment:"
    printenv |grep -v -E '^CT_.+=' |CT_DoLog DEBUG
    CT_EndStep
fi

if [ -z "${CT_RESTART}" ]; then
    CT_DoStep INFO "Retrieving needed toolchain components' tarballs"
    do_companion_tools_get
    do_kernel_get
    do_gmp_get
    do_mpfr_get
    do_ppl_get
    do_cloog_get
    do_mpc_get
    do_libelf_get
    do_binutils_get
    do_elf2flt_get
    do_sstrip_get
    do_cc_get
    do_libc_get
    do_debug_get
    do_test_suite_get
    CT_EndStep

    if [ "${CT_ONLY_DOWNLOAD}" != "y" ]; then
        if [ "${CT_FORCE_EXTRACT}" = "y" ]; then
            CT_DoForceRmdir "${CT_SRC_DIR}"
            CT_DoExecLog ALL mkdir -p "${CT_SRC_DIR}"
        fi

        if [ "${CT_COMP_TOOLS}" = "y" ]; then
          CT_DoStep INFO "Extracting, patching and installing companion tools"
          do_companion_tools_extract
          do_companion_tools
          CT_EndStep
        fi

        CT_DoStep INFO "Extracting and patching toolchain components"
        do_kernel_extract
        do_gmp_extract
        do_mpfr_extract
        do_ppl_extract
        do_cloog_extract
        do_mpc_extract
        do_libelf_extract
        do_binutils_extract
        do_elf2flt_extract
        do_sstrip_extract
        do_cc_extract
        do_libc_extract
        do_debug_extract
        do_test_suite_extract
        CT_EndStep
    fi
fi

# Now for the job by itself. Go have a coffee!
if [ "${CT_ONLY_DOWNLOAD}" != "y" -a "${CT_ONLY_EXTRACT}" != "y" ]; then
    # Because of CT_RESTART, this becomes quite complex
    do_stop=0
    prev_step=
    [ -n "${CT_RESTART}" ] && do_it=0 || do_it=1
    # Aha! CT_STEPS comes from steps.mk!
    for step in ${CT_STEPS}; do
        if [ ${do_it} -eq 0 ]; then
            if [ "${CT_RESTART}" = "${step}" ]; then
                CT_DoLoadState "${step}"
                do_it=1
                do_stop=0
            fi
        else
            CT_DoSaveState ${step}
            if [ ${do_stop} -eq 1 ]; then
                CT_DoLog ERROR "Stopping just after step '${prev_step}', as requested."
                exit 0
            fi
        fi
        if [ ${do_it} -eq 1 ]; then
            ( do_${step} )
            if [ "${CT_STOP}" = "${step}" ]; then
                do_stop=1
            fi
            if [ "${CT_DEBUG_PAUSE_STEPS}" = "y" ]; then
                CT_DoPause "Step '${step}' finished"
            fi
        fi
        prev_step="${step}"
    done
fi

CT_DoEnd INFO

# From now-on, it can become impossible to log any time, because
# either we're compressing the log file, or it can become RO any
# moment... Consign all ouptut to oblivion...
CT_DoLog INFO "Finishing installation (may take a few seconds)..."
exec >/dev/null 2>&1
rm -f ${CT_PREFIX_DIR}/build.log.bz2
if [ "${CT_LOG_TO_FILE}" = "y" ]; then
    cp "${tmp_log_file}" "${CT_PREFIX_DIR}/build.log"
    if [ "${CT_LOG_FILE_COMPRESS}" = y ]; then
        bzip2 -9 "${CT_PREFIX_DIR}/build.log"
    fi
fi
[ "${CT_INSTALL_DIR_RO}" = "y"  ] && chmod -R a-w "${CT_INSTALL_DIR}"
[ "${CT_TEST_SUITE}" = "y" ] && chmod -R u+w "${CT_TEST_SUITE_DIR}"

trap - EXIT
