#!/bin/bash
# OpenHPC build script/utilities
#
#-----------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#-----------------------------------------------------------------------
# Sets up build environment for supported compiler families
#-----------------------------------------------------------------------

if [ "$#" = "1" ]; then
    OHPC_COMPILER_FAMILY=$1
fi

if [ -z "$OHPC_COMPILER_FAMILY" ]; then
    echo "OHPC_COMPILER_FAMILY not defined"
    exit 1
fi

export toolset=gcc
if [ "$OHPC_COMPILER_FAMILY" = "gnu" ]; then
    export CC=gcc
    export CXX=g++
    export FC=gfortran
    export F77=gfortran
    module purge
    module load gnu
elif [ "$OHPC_COMPILER_FAMILY" = "gnu12" ]; then
    export CC=gcc
    export CXX=g++
    export FC=gfortran
    export F77=gfortran
    module purge
    module load gnu12
elif [ "$OHPC_COMPILER_FAMILY" = "gnu9" ]; then
    export CC=gcc
    export CXX=g++
    export FC=gfortran
    export F77=gfortran
    module purge
    module load gnu9
elif [ "$OHPC_COMPILER_FAMILY" = "intel" ]; then
    export toolset=intel
    export CC=icc
    export CXX=icpc
    export FC=ifort
    export F77=ifort
    module purge
    module load intel
elif [ "$OHPC_COMPILER_FAMILY" = "arm1" ]; then
    export toolset=arm
    export CC=armclang
    export CXX=armclang++
    export FC=armflang
    export F77=armflang
    module purge
    module load arm1
elif [ "$OHPC_COMPILER_FAMILY" = "llvm" ]; then
    export toolset=clang
    export CC=clang
    export CXX=clang++
    export FC=flang
    export F77=flang
    module purge
    module load llvm
else
    echo "Unsupported OHPC_COMPILER_FAMILY -> $OHPC_COMPILER_FAMILY"
    exit 1
fi

echo " "
echo "--"
echo "OpenHPC compiler/build flags":
echo "--> CC       = $CC"
echo "--> CXX      = $CXX"
echo "--> FC       = $FC"
echo "--> F77      = $F77"
echo " "

#-----------------------------
# Set default compiler flags 
# and check for user override
#-----------------------------

arch=`uname -m`

if [ "$toolset" == "gcc" ];then
    DEFAULT_OPTS="-O3 -g -pipe -Wall -fexceptions -fstack-protector-strong -grecord-gcc-switches -mtune=generic"
    if [ "$arch" == "x86_64" ];then
	DEFAULT_OPTS="$DEFAULT_OPTS -m64"
    fi
elif [ "${toolset}" == "intel" ];then
    DEFAULT_OPTS="-O3 -g -m64 -fexceptions -fstack-protector-strong -mtune=generic"
elif [ "${toolset}" == "arm" ];then
    DEFAULT_OPTS="-O3 -g -Wall -fexceptions -fstack-protector-strong -mtune=generic"
fi

if [ -z "$OHPC_CFLAGS" ];then
    export CFLAGS=$DEFAULT_OPTS
else
    export CFLAGS=$OHPC_CFLAGS
    echo "--> CFLAGS   (user override) = $CFLAGS"
fi

if [ -z "$OHPC_CXXFLAGS" ];then
    export CXXFLAGS=$DEFAULT_OPTS
    echo "--> CXXFLAGS = $CXXFLAGS"
else
    export CXXFLAGS=$OHPC_CXXFLAGS
    echo "--> CXXFLAGS (user override) = $CXXFLAGS"
fi

if [ -z "$OHPC_FCFLAGS" ];then
    export FCFLAGS=$DEFAULT_OPTS
    echo "--> FCFLAGS  = $FCFLAGS"
else
    export FCFLAGS=$OHPC_FCFLAGS
    echo "--> FCFLAGS  (user override) = $FCFLAGS"
fi

if [ -z "$OHPC_F77FLAGS" ];then
    export F77FLAGS=$DEFAULT_OPTS
    echo "--> F77FLAGS = $F77FLAGS"
else
    export F77FLAGS=$OHPC_F77FLAGS
    echo "--> F77FLAGS (user override) = $F77FLAGS"
fi

echo "--"
echo " "


