Skip to content

Instantly share code, notes, and snippets.

@MiCurry
Last active May 2, 2025 20:02
Show Gist options
  • Save MiCurry/25f3ea3e8720c2e4ff02c2358ae22b32 to your computer and use it in GitHub Desktop.
Save MiCurry/25f3ea3e8720c2e4ff02c2358ae22b32 to your computer and use it in GitHub Desktop.
Configure
#!/bin/bash
# Convert string to lowercase
to_lower() {
echo $1 | tr '[:upper:]' '[:lower:]'
}
usage() {
printf "${bold}Usage: $0 with the following options:\
\n${RED}Compiler: ${NC}Choose from supported compilers: ${RED}[ gfortran | ifort ]${NC} \
\n${Orange}Makefile: ${NC}Provide a name for your output Makefile name. \
\n${RED}[Debug or Release]:${NC} Choose whether you want to compile the Debug or Release version.\
\n${GREEN}[MPI or Serial]: ${NC} Choose whether you want to compile the parallel (MPI) or serial version. \
\n${BLUE}[MF or SP or SP2]: ${NC} Choose between the Matrix Free (MF), or the Modified System of Eqs 1 (SP), or the Modified System of Eqs 2 (SP2) of the code. \
\nOptional: Enviornment variables: 'FC' 'FFLAGS' 'CPPFLAGS' 'LDFLAGS' 'LDLIBS' are respected\n"
exit 127
}
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
PURPLE='\033[0;35m'
Orange='\033[0;36m' bold=$(tput bold)
normal=$(tput sgr0)
NC='\033[0m' # No Color
# Argument check
if [ $# -le 1 ] || [ $# -lt 5 ] ; then
usage
fi
COMPILER=$(to_lower $1)
makefile_name=$2
debug_or_release=$(to_lower $3)
mpi_or_serial=$(to_lower $4)
forward_version=$(to_lower $5)
case $COMPILER in
gfortran | gnu | ifort | intel)
;;
*)
printf "${YELLOW}WARNING: Did not recognize the compiler '${compiler}'... falling back onto GNU ${NC}\n"
;;
esac
# Common Options
DEFAULT_OPT="-O3"
DEBUG_OPT="-O0"
case $COMPILER in
ifort | intel)
CPPFLAGS="-fpp"
FFLAGS="-std03 -mavx -ffree-line-length-none"
FFLAGS_DEBUG="-w -g -fp-stack-check -traceback"
LDFLAGS="-L$(MKLROOT)/lib/intel64"
LDLIBS="-lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -lpthread -qopenmp -parallel"
;;
gfortran | gnu | *)
CPPFLAGS="-cpp"
FFLAGS="-ffree-line-length-none -dI -fallow-argument-mismatch"
FFLAGS_DEBUG="-g -fbacktrace -fbounds-check"
LDFLAGS=""
LDLIBS="-lblas -llapack"
;;
esac
case $debug_or_release in
"Debug" | "debug")
FFLAGS="$DEBUG_OPT $FFLAGS $FFLAGS_DEBUG"
;;
"Release" | "release")
FFLAGS="$DEFAULT_OPT $FFLAGS"
esac
# Decide the name of the compiler, but take FC if it's present
case $mpi_or_serial in
"MPI" | "mpi" )
FC=${FC:-mpifort}
CPPFLAGS="$CPPFLAGS -DMPI"
;;
"Serial" | "serial")
CPPFLAGS="$CPPFLAGS"
case $COMPILER in
ifort | intel)
FC=${FC:-ifort}
;;
gfortran | gnu)
FC=${FC:-gfortran}
;;
esac
;;
*)
printf "${RED}ERROR: Did not regocnize '$mpi_or_serial' for MPI or Serial selection. Choose from: [ MPI | Serial ]${NC}\n" >&2
exit 1
esac
if [ -e $makefile_name ]; then
rm $makefile_name
fi
DEFAULT_DIRS=(
"."
"MPI"
"INV"
"SENS"
"UTILS"
"FIELDS"
"FIELDS/FiniteDiff3D"
"3D_MT"
"3D_MT/DICT"
"3D_MT/ioMod"
"3D_MT/modelParam"
"3D_MT/modelParam/modelCov"
"3D_MT/modelParam/modelParamIO"
)
MF_DIRS=(
"3D_MT/FWD"
"3D_MT/FWD/Mod2d"
)
SP_DIRS=(
"3D_MT/FWD_SP"
"3D_MT/SP_Topology"
"3D_MT/FWD"
"3D_MT/FWD/Mod2d"
)
SP2_DIRS=(
"3D_MT/FWD_SP2"
"3D_MT/SP_Topology"
"3D_MT/FWD"
"3D_MT/FWD/Mod2d"
)
source_dirs=${DEFAULT_DIRS[@]}
case $forward_version in
MF | mf)
source_dirs=( ${source_dirs[@]} ${MF_DIRS[@]} )
;;
SP | sp)
source_dirs=( ${source_dirs[@]} ${SP_DIRS[@]} )
;;
SP2 | sp2)
source_dirs=( ${source_dirs[@]} ${SP2_DIRS[@]} )
;;
*)
printf "${RED}ERROR: Do not recognize Forward Version ${forward_version}. Choose from: [ MF | SP | SP2 ] ${NC}\n"
exit 1
;;
esac
# Combine the source_dirs into a colon sepeareted list: 'dir1:dir2:dir3'
source_dirs=$(IFS=':' ; echo "${source_dirs[*]}")
perl fmkmf.pl -f90 $FC \
-opt "$CPPFLAGS $FFLAGS" \
-mpi "$CPPFLAGS" \
-o "./objs/3D_MT/csemBuild" \
-l "$LDLIBS" \
-lp "$LDFLAGS" \
-p "$source_dirs" \
Mod3DMT.f90 > $makefile_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment