# Build script for adjust the toolchain
#
# Copyright (C) 2014-2017 Matias Fonzo, <selk@dragora.org>
#
# This script is free software: you have unlimited permission
# to copy, distribute and modify it.

# This will be handled by libs/musl (post-install script):

cat << EOF > /tools/bin/adjust-toolchain
#! /bin/sh -

if test -r /tools/bin/ld-new
then
    set -e
    echo "\${0}: Adjusting toolchain ..."
    set -x

    CC=cc
    TRIPLET="\$(\$CC -dumpmachine)"
    libSuffix=$libSuffix

    mv /tools/bin/ld /tools/bin/ld-old
    mv /tools/\${TRIPLET}/bin/ld /tools/\${TRIPLET}/bin/ld-old
    mv /tools/bin/ld-new /tools/bin/ld
    ln -sf /tools/bin/ld /tools/\${TRIPLET}/bin/ld

    \$CC -dumpspecs | sed \\
     -e 's@/tools@@g' \\
     -e "/\*startfile_prefix_spec:/{n;s@.*@/usr/lib\${libSuffix}/ @}" \\
     -e '/\*cpp:/{n;s@\$@ -isystem /usr/include@}' \\
      > "\$(dirname -- "\$(\$CC --print-libgcc-file-name)")"/specs
fi

EOF

chmod 700 /tools/bin/adjust-toolchain

