#! /usr/bin/env bash
# Copyright (c) 2011-2015, ARM Limited
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Arm nor the names of its contributors may be used
#       to endorse or promote products derived from this software without
#       specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

CC_PREFIX=arm-none-eabi
CGTOOLS=/db/vendors/linaro/gcc-arm-none-eabi-7-2017-q4-major/bin

set -e
set -x
set -u
set -o pipefail

umask 022

exec < /dev/null

script_path=`cd $(dirname $0) && pwd -P`
. $script_path/build-common.sh

MULTILIB_LIST="--with-multilib-list=rmprofile"
NEWLIB_CONFIG_OPTS=" --build=$BUILD --host=$HOST_NATIVE "

export PATH=$CGTOOLS:$PATH

mkdir -p $BUILDDIR_NATIVE
rm -rf $INSTALLDIR_NATIVE && mkdir -p $INSTALLDIR_NATIVE/$TARGET/lib
rm -rf $PACKAGEDIR && mkdir -p $PACKAGEDIR

cd $SRCDIR

echo Task [III-2] /$HOST_NATIVE/newlib/
saveenv
prepend_path PATH $INSTALLDIR_NATIVE/bin
saveenvvar CFLAGS_FOR_TARGET '-g -O2 -ffunction-sections -fdata-sections -D__tirtos__'
saveenvvar CC_FOR_TARGET '$CGTOOLS/$CC_PREFIX-gcc'
saveenvvar CXX_FOR_TARGET '$CGTOOLS/$CC_PREFIX-cpp'
saveenvvar GCC_FOR_TARGET '$CGTOOLS/$CC_PREFIX-gcc'
saveenvvar AR_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ar'
saveenvvar AS_FOR_TARGET '$CGTOOLS/$CC_PREFIX-as'
saveenvvar LD_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ld'
saveenvvar NM_FOR_TARGET '$CGTOOLS/$CC_PREFIX-nm'
saveenvvar OBJDUMP_FOR_TARGET '$CGTOOLS/$CC_PREFIX-objdump'
saveenvvar RANLIB_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ranlib'
saveenvvar READELF_FOR_TARGET '$CGTOOLS/$CC_PREFIX-readelf'
saveenvvar STRIP_FOR_TARGET '$CGTOOLS/$CC_PREFIX-strip'
rm -rf $BUILDDIR_NATIVE/newlib && mkdir -p $BUILDDIR_NATIVE/newlib
pushd $BUILDDIR_NATIVE/newlib

$SRCDIR/$NEWLIB/configure  \
    $NEWLIB_CONFIG_OPTS \
    --target=$TARGET \
    --prefix=$INSTALLDIR_NATIVE \
    --infodir=$INSTALLDIR_NATIVE_DOC/info \
    --mandir=$INSTALLDIR_NATIVE_DOC/man \
    --htmldir=$INSTALLDIR_NATIVE_DOC/html \
    --pdfdir=$INSTALLDIR_NATIVE_DOC/pdf \
    --enable-newlib-io-long-long \
    --enable-newlib-register-fini \
    --enable-newlib-retargetable-locking \
    --disable-newlib-supplied-syscalls \
    --disable-nls

make -j$JOBS

make install

popd
restoreenv

echo Task [III-3] /$HOST_NATIVE/newlib-nano/
saveenv
prepend_path PATH $INSTALLDIR_NATIVE/bin
saveenvvar CFLAGS_FOR_TARGET '-g -Os -ffunction-sections -fdata-sections -D__tirtos__'
saveenvvar CC_FOR_TARGET '$CGTOOLS/$CC_PREFIX-gcc'
saveenvvar CXX_FOR_TARGET '$CGTOOLS/$CC_PREFIX-cpp'
saveenvvar GCC_FOR_TARGET '$CGTOOLS/$CC_PREFIX-gcc'
saveenvvar AR_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ar'
saveenvvar AS_FOR_TARGET '$CGTOOLS/$CC_PREFIX-as'
saveenvvar LD_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ld'
saveenvvar NM_FOR_TARGET '$CGTOOLS/$CC_PREFIX-nm'
saveenvvar OBJDUMP_FOR_TARGET '$CGTOOLS/$CC_PREFIX-objdump'
saveenvvar RANLIB_FOR_TARGET '$CGTOOLS/$CC_PREFIX-ranlib'
saveenvvar READELF_FOR_TARGET '$CGTOOLS/$CC_PREFIX-readelf'
saveenvvar STRIP_FOR_TARGET '$CGTOOLS/$CC_PREFIX-strip'
rm -rf $BUILDDIR_NATIVE/newlib-nano && mkdir -p $BUILDDIR_NATIVE/newlib-nano
pushd $BUILDDIR_NATIVE/newlib-nano

$SRCDIR/$NEWLIB_NANO/configure  \
    $NEWLIB_CONFIG_OPTS \
    --target=$TARGET \
    --prefix=$BUILDDIR_NATIVE/target-libs \
    --disable-newlib-supplied-syscalls    \
    --enable-newlib-reent-small           \
    --disable-newlib-fvwrite-in-streamio  \
    --disable-newlib-fseek-optimization   \
    --disable-newlib-wide-orient          \
    --enable-newlib-nano-malloc           \
    --disable-newlib-unbuf-stream-opt     \
    --enable-lite-exit                    \
    --enable-newlib-global-atexit         \
    --enable-newlib-nano-formatted-io     \
    --disable-nls

make -j$JOBS
make install

popd
restoreenv

copy_multi_libs src_prefix="$BUILDDIR_NATIVE/target-libs/$TARGET/lib" \
                dst_prefix="$INSTALLDIR_NATIVE/$TARGET/lib"           \
                target_gcc="$CGTOOLS/$CC_PREFIX-gcc"

# Copy the nano configured newlib.h file into the location that nano.specs
# expects it to be.
mkdir -p $INSTALLDIR_NATIVE/$TARGET/include/newlib-nano
cp -f $BUILDDIR_NATIVE/target-libs/$TARGET/include/newlib.h \
      $INSTALLDIR_NATIVE/$TARGET/include/newlib-nano/newlib.h
