#!/bin/bash

SRC=random_tree
SRCEXE=random_tree_test


#*******************
#**** Linux x64 ****
#*******************

OPT="-O3 -ffast-math"


#*** C ****

DEST=gcc_Linux_x64

echo "* Building C source of Linux x64 dynamic library and example ..."
gcc $OPT -shared -o $DEST"_dynamic/lib"$SRC.so $SRC.c
gcc $OPT $SRCEXE.c -o $DEST"_dynamic/"$SRCEXE -L $DEST"_dynamic" -l$SRC

echo "* Building C source of Linux x64 static library and example ..."
gcc $OPT -c $SRC.c
ar rcs $DEST"_static/lib"$SRC.a $SRC.o
rm -f $SRC.o
gcc $OPT $SRCEXE.c -o $DEST"_static/"$SRCEXE -L $DEST"_static" -l$SRC


#**** C++ ****

DEST=G++_Linux_x64

echo "* Building C++ source of Linux x64 static library and example ..."
g++ $OPT -c $SRC"_cpp.cpp"
ar rcs $DEST"_static/lib"$SRC.a $SRC"_cpp.o"
rm -f $SRC"_cpp.o"
g++ $OPT -s $SRCEXE.cpp -o $DEST"_static/"$SRCEXE -L $DEST"_static/" -l$SRC


#**** Fortran 90 ****

DEST=Gfortran_Linux_x64

echo "* Building Fortran 90 source of Linux x64 static library and example ..."
cd $DEST"_static"
gfortran $OPT -c ../$SRC.f90
ar rcs lib$SRC.a $SRC.o
rm -f $SRC.o
gfortran $OPT -s -o $SRCEXE ../$SRCEXE.f90 -L. -l$SRC
cd ..


#*******************
#**** Linux x86 ****
#*******************

OPT="-O3 -ffast-math -m32"


#*** C ****

DEST=gcc_Linux_x86

echo "* Building C source of Linux x86 dynamic library and example ..."
gcc $OPT -shared -o $DEST"_dynamic/lib"$SRC.so $SRC.c
gcc $OPT $SRCEXE.c -o $DEST"_dynamic/"$SRCEXE -L $DEST"_dynamic" -l$SRC

echo "* Building C source of Linux x86 static library and example ..."
gcc $OPT -c $SRC.c
ar rcs $DEST"_static/lib"$SRC.a $SRC.o
rm -f $SRC.o
gcc $OPT $SRCEXE.c -o $DEST"_static/"$SRCEXE -L $DEST"_static" -l$SRC


#**** C++ ****

DEST=G++_Linux_x86

echo "* Building C++ source of Linux x86 static library and example ..."
g++ $OPT -c $SRC"_cpp.cpp"
ar rcs $DEST"_static/lib"$SRC.a $SRC"_cpp.o"
rm -f $SRC"_cpp.o"
g++ $OPT -s $SRCEXE.cpp -o $DEST"_static/"$SRCEXE -L $DEST"_static/" -l$SRC


#**** Fortran 90 ****

DEST=Gfortran_Linux_x86

echo "* Building Fortran 90 source of Linux x86 static library and example ..."
cd $DEST"_static"
gfortran $OPT -c ../$SRC.f90
ar rcs lib$SRC.a $SRC.o
rm -f $SRC.o
gfortran $OPT -s -o $SRCEXE ../$SRCEXE.f90 -L. -l$SRC
cd ..
