#!/bin/bash

BASEDIR="$PWD"

test -z "$NUMBER_OF_PROCESSORS" && NUMBER_OF_PROCESSORS=1
export MAKE_FLAGS="-j $NUMBER_OF_PROCESSORS"

##################### OPTIONS AND PATHS ############################

REBUILD_NEWLIB=true
RECONFIG_RTEMS=true
REMAKE_RTEMS=true
REMAKE_BSP=true

RTEMS_VARIANT=OPTIMIZE
RTEMS_VARIANT=DEBUG

# Which BSP?
BSP=nios2_eb2_1

TARGET=nios2-rtems

QUARTUS_ROOTDIR=/opt/altera/quartus2_5.0
SOPC_KIT_NIOS2=/opt/altera/nios2_5.1

# What is the base directory containing Altera's nios2 gnutoools?
NIOS2_GNUTOOLS="$SOPC_KIT_NIOS2/bin/nios2-gnutools/H-i686-pc-linux-gnu"

# Where is the target-gcc and accompanying libraries and header files?
GCCVER="3.4.1"
GCCDIR="$NIOS2_GNUTOOLS/lib/gcc/nios2-elf/$GCCVER"
  
# Where to create b-* directories for building
BUILD="$BASEDIR/b"

# Where to install gnutools wrapper, newlib and RTEMS libraries
# PREFIX="/usr/local/$TARGET".
# Warning: There is a rm -rf $PREFIX below!!
PREFIX="$BASEDIR/install"

# Where is the newlib source tree?
NEWLIB=$BASEDIR/newlib-20060117_1116

# Where is the RTEMS source tree?
RTEMS=$BASEDIR/rtems-cvs

##################### CLEAN UP #####################################

rm -rf $PREFIX $BUILD-*

##################### WRAP NIOS2-ELF TOOLS #########################

# Workaround for not having a nios2-rtems toolchain yet

mkdir -p $PREFIX/bin
for TOOL in ar as nm objdump objcopy ranlib size strip ld
do
  ln -sf $NIOS2_GNUTOOLS/bin/nios2-elf-$TOOL $PREFIX/bin/nios2-rtems-$TOOL
done

# Following are the paths to libraries and include files of our RTEMS newlib.
# The -msys-lib and -msys-crt0 settings are required only during configure to
# allow the compiler to create executables (which aren't actually executed) for
# testing some features. When building actual RTEMS applications, specs are
# overridden and the sys-lib/crt0 specifications on the command line are
# ignored.

# If you get problems with the range of (gp), do add a -G0 here
# (and probably you'd also have to add it in newlib/newlib/configure.host and
# rebuild newlib, so the configurations match!)

# Library directories must be specified with -B, not with -L, otherwise the
# compiler wouldn't look for multilib variants.
# Avoid any space before -nostdinc, otherwise rtems/config-ml.in would
# match it as a single '--norecursive' option, preventing a multilib build
# If you don't enable multilib-ing, you'll have to specify
# -mno-hw-mul and/or -mno-hw-div depending on your CPU selection

cat <<EOT > $PREFIX/bin/nios2-rtems-gcc
#!/bin/sh
$NIOS2_GNUTOOLS/bin/nios2-elf-gcc -nostdinc \
 -isystem "$PREFIX/$TARGET/include" -isystem "$GCCDIR/include" \
 "-B$PREFIX/$TARGET/lib/" "-B$PREFIX/$TARGET/lib/$TARGET/$GCCVER" \
 -msys-lib=c "-msys-crt0=$PREFIX/$TARGET/lib/crt0.o" \
 -DSYSTEM_BUS_WIDTH=32 -D__rtems__ -D__USE_INIT_FINI__ \
 "\$@"
EOT
chmod +x $PREFIX/bin/nios2-rtems-gcc

ln -sf nios2-rtems-gcc $PREFIX/bin/nios2-rtems-cc
ln -sf nios2-rtems-gcc $PREFIX/bin/nios2-rtems-c++

##################### BUILD RTEMS NEWLIB ###########################

OLDPATH="$PATH"
export PATH=$PREFIX/bin:$PATH

$REBUILD_NEWLIB && (
  rm -rf   "$BUILD-newlib"
  mkdir -p "$BUILD-newlib"
  cd       "$BUILD-newlib"
  $NEWLIB/configure --target=$TARGET --prefix=$PREFIX
  make all
  test $? -eq 0 && make install
  test $? -eq 0 || ( RECONFIG_RTEMS=false ; REMAKE_RTEMS=false; REMAKE_BSP=false)
  cd $BASEDIR
)

##################### CONFIGURE RTEMS ##############################

$RECONFIG_RTEMS && (
  echo Configuring RTEMS in rtems from $RTEMS
  rm -rf $BUILD-rtems/*
  mkdir -p $BUILD-rtems
  cd $BUILD-rtems
  $RTEMS/configure  \
    --target=$TARGET  \
    --prefix=$PREFIX  \
    --disable-rdbg \
    --disable-rtems-debug \
    --disable-itron \
    --disable-cxx  \
    --enable-posix  \
    --enable-networking  \
    --enable-tests \
    --enable-multilib
  test $? -eq 0 || ( REMAKE_RTEMS=false; REMAKE_BSP=false )
  cd $BASEDIR
)

##################### BUILD RTEMS ##################################

$REMAKE_RTEMS && (
  echo Making RTEMS in b-rtems
  cd $BUILD-rtems
  make RTEMS_BSP="" VARIANT="$RTEMS_VARIANT" all
  test $? -eq 0 && make RTEMS_BSP="" VARIANT="$RTEMS_VARIANT" install
  test $? -eq 0 || REMAKE_BSP=false
  cd $BASEDIR
)

##################### BUILD BSP AND TESTS ##########################

$REMAKE_BSP && (
  echo Making RTEMS BSP in b-rtems
  cd $BUILD-rtems
  # rm -rf $BUILD/b-rtems/$TARGET/c/$BSP/testsuites/tmtests/tm0?/*.?xe
  rm -rf $BUILD-rtems/$TARGET/c/$BSP/testsuites/*/*/*.?xe
  rm -rf $BUILD-rtems/$TARGET/c/$BSP/testsuites/*/*/o-optimize/*.?xe
  make RTEMS_BSP="nios2_eb2_1" VARIANT="$RTEMS_VARIANT" all \
  && make RTEMS_BSP="nios2_eb2_1" VARIANT="$RTEMS_VARIANT" install
  cd $BASEDIR
)

##################### BUILD BSP AND TESTS ##########################

export PATH=$OLDPATH 



