#!/bin/bash
#set -x

set -e

# This is a helper script that compiles .out files for the cRIO. It gets called
# by the gcc and g++ wrapper scripts if they detect that the tool is being used
# as a shared linker.

# All the flags except -shared.
INPUTS_FLAGS=`echo "$@" | sed 's/-shared//g'`
# The arguments after any -o flags.
OUTPUT=`echo ${INPUTS_FLAGS} | awk \
  'BEGIN { RS=" " }; output { print ; output = 0 }; /-o/ { output = 1 }'`
# All arguments that don't start with a - and aren't ${OUTPUT}.
INPUTS=`echo ${INPUTS_FLAGS} | awk \
  'BEGIN { RS=" " }; /-Wl,--no-whole-archive/ { output = 0 }; \
  output { print }; \
  /-Wl,--whole-archive/ { output = 1 }'`
TEMPDIR=`dirname ${OUTPUT}`
AOS=`dirname $0`/..
powerpc-wrs-vxworks-nm ${INPUTS} | \
  tclsh ${WIND_BASE}/host/resource/hutils/tcl/munch.tcl -c ppc > \
    ${TEMPDIR}/ctdt.c
powerpc-wrs-vxworks-gcc -I${AOS}/.. -c ${TEMPDIR}/ctdt.c -o ${TEMPDIR}/ctdt.o
powerpc-wrs-vxworks-g++ ${INPUTS_FLAGS} ${TEMPDIR}/ctdt.o
ln -f ${OUTPUT} `echo ${OUTPUT} | sed 's/lib\([A-Za-z0-9_]*\)\.so$/\1.out/'`
