#!/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}" = "gnu13" ]; then
    export CC=gcc
    export CXX=g++
    export FC=gfortran
    export F77=gfortran
    module purge
    module load gnu13
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=icx
    export CXX=icpx
    export FC=ifx
    export F77=ifx
    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 [ "${arch}" == "x86_64" ];then
    ARCH_OPTS="-m64"
    if [ -e /etc/os-release ]; then
        # shellcheck disable=SC1091
        . /etc/os-release
        if [ -n "${PLATFORM_ID}" ] && [ "${PLATFORM_ID}" == "platform:el9" ]; then
            # Only RHEL 9 has switched to the optimization baseline 'x86-64-v2'
            ARCH_OPTS="${ARCH_OPTS} -march=x86-64-v2"
        fi
    fi
fi

if [ "${toolset}" == "gcc" ];then
    DEFAULT_OPTS="-O3 -g -pipe -Wall -fexceptions -fstack-protector-strong -grecord-gcc-switches -mtune=generic ${ARCH_OPTS}"
elif [ "${toolset}" == "intel" ];then
    DEFAULT_OPTS="-O3 -g -fexceptions -fstack-protector-strong -mtune=generic ${ARCH_OPTS}"
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 " "
