#!/bin/bash

#
# Installation script for cM packages.
# Part of Collective Mind Infrastructure (cM).
#
# See cM LICENSE.txt for licensing details.
# See cM Copyright.txt for copyright details.
#
# Developer(s): Grigori Fursin, started on 2011.09
#

# ${CM_LOCAL_SRC_DIR}  - src directory
# ${CM_INSTALL_OBJ_DIR}  - obj directory
# ${CM_INSTALL_DIR}  - install dir
# ${CM_CODE_ENV_FILE}  - file that sets environment
# ${CM_CODE_UID}  - cM code UID
# ${CM_PROCESSOR_BITS}  - number of bits for the OS (32 or 64)
# ${CM_OS_LIB_DIR} - lib extension for this OS
# ${CM_PARALLEL_JOB_NUMBER} - parallel jobs if supported

# Prepare
mkdir -p ${CM_INSTALL_OBJ_DIR}
mkdir -p ${CM_INSTALL_DIR}

if [ "${CM_PARALLEL_JOB_NUMBER}" != "" ] ; then
  pj="-j ${CM_PARALLEL_JOB_NUMBER}"
fi

# Configure
cd ${CM_INSTALL_OBJ_DIR}
dep_gmp_i=CM_${CM_CODE_DEP_GMP}_INCLUDE
dep_gmp_l=CM_${CM_CODE_DEP_GMP}_LIB
dep_mpfr_i=CM_${CM_CODE_DEP_MPFR}_INCLUDE
dep_mpfr_l=CM_${CM_CODE_DEP_MPFR}_LIB
dep_mpc_i=CM_${CM_CODE_DEP_MPC}_INCLUDE
dep_mpc_l=CM_${CM_CODE_DEP_MPC}_LIB
dep_ppl_i=CM_${CM_CODE_DEP_PPL}_INCLUDE
dep_ppl_l=CM_${CM_CODE_DEP_PPL}_LIB
dep_cloog_i=CM_${CM_CODE_DEP_CLOOG}_INCLUDE
dep_cloog_l=CM_${CM_CODE_DEP_CLOOG}_LIB
dep_isl_i=CM_${CM_CODE_DEP_ISL}_INCLUDE
dep_isl_l=CM_${CM_CODE_DEP_ISL}_LIB

#export CFLAGS=-I${!dep_cloog_i}
#export LDFLAGS=-L${!dep_cloog_l}
#export LD_LIBRARY_PATH=${!dep_cloog_l}:$LD_LIBRARY_PATH
if [ "${CM_SKIP_BUILD}" != "yes" ] ; then

 ${CM_LOCAL_SRC_DIR}/configure  ${CM_CONFIGURE_EXTRA_PARAMS} \
                              --prefix=${CM_INSTALL_DIR} \
                              --with-gmp-include=${!dep_gmp_i} \
                              --with-gmp-lib=${!dep_gmp_l} \
                              --with-mpfr-include=${!dep_mpfr_i} \
                              --with-mpfr-lib=${!dep_mpfr_l} \
                              --with-mpc-include=${!dep_mpc_i} \
                              --with-mpc-lib=${!dep_mpc_l} \
                              --with-ppl-include=${!dep_ppl_i} \
                              --with-ppl-lib=${!dep_ppl_l} \
                              --with-cloog-include=${!dep_cloog_i} \
                              --with-cloog-lib=${!dep_cloog_l} \
                              --enable-cloog-backend=isl \
                              --disable-cloog-version-check \
                              --enable-shared \
                              --enable-static \
                              --enable-threads=posix \
                              --enable-libgomp \
                              --enable-lto \
                              --enable-graphite
 if [ "${?}" != "0" ] ; then
   echo "Error: Configuration failed at $PWD!" 
   exit 1
 fi

 # Make
 cd ${CM_INSTALL_OBJ_DIR}
 make $pj
  if [ "${?}" != "0" ] ; then
    echo "Error: Compilation failed at $PWD!" 
    exit 1
  fi
 make install $pj
  if [ "${?}" != "0" ] ; then
    echo "Error: Compilation failed at $PWD!" 
    exit 1
  fi

fi

# Prepare environment
echo "" >> ${CM_CODE_ENV_FILE}
echo "# Environment variables" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_INSTALL=${CM_INSTALL_DIR}" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_BIN=\$CM_${CM_CODE_UID}_INSTALL/bin" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_SRC=${CM_LOCAL_SRC_DIR}" >> ${CM_CODE_ENV_FILE}
if [ "${CM_PROCESSOR_BITS}" == "64" ] ; then
  # On some 64-bit OS GMP still installs itself to lib instead of lib64 - check that
  if [ -d ${CM_INSTALL_DIR}/lib64 ]; then
     echo "export CM_${CM_CODE_UID}_LIB=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}" >> ${CM_CODE_ENV_FILE}
     echo "export CM_${CM_CODE_UID}_LIBS=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}:\$CM_${CM_CODE_UID}_INSTALL/lib" >> ${CM_CODE_ENV_FILE}
  else
     echo "export CM_${CM_CODE_UID}_LIB=\$CM_${CM_CODE_UID}_INSTALL/lib" >> ${CM_CODE_ENV_FILE}
     echo "export CM_${CM_CODE_UID}_LIBS=\$CM_${CM_CODE_UID}_INSTALL/lib" >> ${CM_CODE_ENV_FILE}
  fi
else
  echo "export CM_${CM_CODE_UID}_LIB=\$CM_${CM_CODE_UID}_INSTALL/lib" >> ${CM_CODE_ENV_FILE}
  echo "export CM_${CM_CODE_UID}_LIBS=\$CM_${CM_CODE_UID}_INSTALL/${CM_OS_LIB_DIR}" >> ${CM_CODE_ENV_FILE}
fi
echo "export CM_${CM_CODE_UID}_INCLUDE=\$CM_${CM_CODE_UID}_INSTALL/include" >> ${CM_CODE_ENV_FILE}
GCC_INSTALL_PLUGINS_DIR=$(${CM_INSTALL_DIR}/bin/gcc -print-file-name=plugin)
echo "export CM_${CM_CODE_UID}_PLUGIN_DIR=${GCC_INSTALL_PLUGINS_DIR}" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_PLUGIN_DIR_INCLUDE=\$CM_${CM_CODE_UID}_PLUGIN_DIR/include" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_SRC_INCLUDE=${CM_LOCAL_SRC_DIR}/gcc" >> ${CM_CODE_ENV_FILE}

# GCC plugins
echo "export CM_${CM_CODE_UID}_GMP_INCLUDE=${!dep_gmp_i}" >> ${CM_CODE_ENV_FILE}
echo "export CM_${CM_CODE_UID}_GMP_LIB=${!dep_gmp_l}" >> ${CM_CODE_ENV_FILE}
#echo "" >> $CM_CODE_ENV_FILE
#echo "export CM_${CM_CODE_UID}_LIB_GCC_PLUGIN_CC=-lcc1" >> $CM_CODE_ENV_FILE
#echo "export CM_${CM_CODE_UID}_LIB_GCC_PLUGIN_FC=-lf951" >> $CM_CODE_ENV_FILE
#echo "export CM_${CM_CODE_UID}_LIB_GCC_PLUGIN_CPP=-lcc1plus" >> $CM_CODE_ENV_FILE

echo "" >> ${CM_CODE_ENV_FILE}
echo "export PATH=\$CM_${CM_CODE_UID}_BIN:\$PATH" >> ${CM_CODE_ENV_FILE}
echo "export LD_LIBRARY_PATH=\$CM_${CM_CODE_UID}_LIB:\$LD_LIBRARY_PATH" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_CXX=g++" >> ${CM_CODE_ENV_FILE}
echo "export CM_OBJ_EXT=.o" >> ${CM_CODE_ENV_FILE}
echo "export CM_CC=gcc" >> ${CM_CODE_ENV_FILE}
echo "export CM_FC=gfortran" >> ${CM_CODE_ENV_FILE}
echo "export CM_F90=gfortran" >> ${CM_CODE_ENV_FILE}
echo "export CM_F95=gfortran" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DLL=\"-shared -fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DLL_EXTRA=-ldl" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_OUTPUT=-o " >> ${CM_CODE_ENV_FILE}
echo "export CM_DLL_EXT=.so" >> ${CM_CODE_ENV_FILE}
echo "export CM_LIB_EXT=.a" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_CREATE_OBJ=-c" >> ${CM_CODE_ENV_FILE}
echo "export CM_LB=\"ar rcs\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_AR=ar" >> ${CM_CODE_ENV_FILE}
echo "export CM_LB_OUTPUT=" >> ${CM_CODE_ENV_FILE}
echo "export CM_EXE_EXT=.out" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_STATIC_BIN=\"-static -fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_STATIC_LIB=\"-static -fPIC\"" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_DYNAMIC_BIN=" >> ${CM_CODE_ENV_FILE}
echo "export CM_FLAGS_CREATE_ASM=-S" >> ${CM_CODE_ENV_FILE}
echo "export CM_ASM_EXT=.s" >> ${CM_CODE_ENV_FILE}
echo "export CM_PLUGIN_FLAG=-fplugin=" >> ${CM_CODE_ENV_FILE}
echo "export CM_LD_FLAGS_EXTRA=\"-lm -ldl\"" >> ${CM_CODE_ENV_FILE}

echo "" >> ${CM_CODE_ENV_FILE}
echo "export CM_MAKE=make" >> ${CM_CODE_ENV_FILE%}
echo "export CM_OBJDUMP=\"objdump -d\"" >> ${CM_CODE_ENV_FILE}

chmod 755 ${CM_CODE_ENV_FILE}

# Cleaning directories if needed
echo ""
echo "Cleaning directories if needed ..."
echo ""

if [ "${CM_INSTALL_DELETE_SRC_DIR}" == "yes" ] ; then rm -rf ${CM_LOCAL_SRC_DIR}; fi
if [ "${CM_INSTALL_DELETE_OBJ_DIR}" == "yes" ] ; then rm -rf ${CM_INSTALL_OBJ_DIR}; fi
