#!/bin/sh
set -x
set -e
if [ X${SOURCED} = X ]; then
  if [ -f sourcem.cfg ]; then
    echo "Environment not setup, using `pwd`/.sourceme.cfg"
    source sourceme.cfg
  else
    echo "Please copy, configure and source config/sourceme.cfg.example then rerun."
    exit 1
  fi
fi

# The following test cases will not currently compile with TI tools.
BROKEN="
testcases/kernel/syscalls/ipc/semctl/semctl01
testcases/kernel/syscalls/ipc/semctl/semctl03
testcases/kernel/syscalls/ipc/semctl/semctl05
testcases/kernel/syscalls/sysinfo/sysinfo01
testcases/misc/math/float/float_bessel
testcases/misc/math/float/float_exp_log
testcases/misc/math/float/float_iperb
testcases/misc/math/float/float_power
testcases/misc/math/float/float_trigo
"

echo "Removing -pipe from config.mk.in to prevent wrapper conflicts"
for i in include/mk/config.mk*; do
  perl -i -pe 's/ -pipe//g'  $i
done

echo "Removing -fpic from Makefiles"
for i in testcases/commands/ade/*/Makefile; do
  perl -i -pe 's/ -fpic//g'  $i
  perl -i -pe 's/ -fPIC//g'  $i
done

if [ "X${BE}" != X ]; then
 BEFLAG="-me"
 EB="eb"
else
 BEFLAG=""
 EB=""
fi

echo "Compiling lib/write_log.c with cl6x directly"
pushd lib
if [ ${FMT} = coff ]; then
    "`dirname ${PFX}`/cgtools/bin/cl6x" $BEFLAG \
    --abi=coffabi -mv6400+ --mem_model:data=far \
    --symdebug:dwarf --no_compress -O2 -ms0 -q \
    -pdsr=552 -pdsr=112 -pdsr=193 -pdsr=190 -pdsr=179 -pden -pdv \
    -I="`dirname ${PFX}`/lib/gcc-lib/c6x-coff/3.2.2/include" \
    -I=../include -eo .o write_log.c
else
    "`dirname ${PFX}`/cgtools/bin/cl6x" $BEFLAG \
    --abi=eabi --dsbt --pic  --visibility=default \
     -mv6400+ --mem_model:data=far \
    --symdebug:dwarf --no_compress -O2 -ms0 -q \
    -pdsr=552 -pdsr=112 -pdsr=193 -pdsr=190 -pdsr=179 -pden -pdv \
    --diag_suppress=1219 --diag_suppress=824 \
    -I="`dirname ${PFX}`/c6x${EB}-elf-sysroot/usr/include" \
    -I="`dirname ${PFX}`/lib/gcc-lib/c6x${EB}-elf/3.2.2/include" \
    -I=../include -eo .o write_log.c
fi
popd

echo "Inhibiting broken testcases from building."
for i in ${BROKEN}; do
  echo "touch ${i}"
  touch ${i}
done
