Skip to content

Commit

Permalink
Split mfc.sh into multiple files (MFlowCode#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleberre authored Jan 5, 2024
1 parent d8dec27 commit e80a3f7
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 438 deletions.
453 changes: 16 additions & 437 deletions mfc.sh

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions toolchain/bootstrap/cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

# If we downloaded our own version of CMake, let us make it accessible to
# any command we run hereafter.

if [ -f "$(pwd)/build/cmake/bin/cmake" ]; then
export PATH="$(pwd)/build/cmake/bin:$PATH"

log "Found$MAGENTA CMake$COLOR_RESET: $MAGENTA$(pwd)/build/cmake/bin/cmake$COLOR_RESET."
fi

bShouldInstallCMake=false
if ! command -v cmake > /dev/null 2>&1; then
# Not installed
bShouldInstallCMake=true

warn "$MAGENTA""CMake$COLOR_RESET is not installed."
else
cmake_verstr=$(cmake --version | tr ' ' '\n' | sed -n 3p)
cmake_major=$(echo $cmake_verstr | tr '.' '\n' | sed -n 1p)
cmake_minor=$(echo $cmake_verstr | tr '.' '\n' | sed -n 2p)
cmake_patch=$(echo $cmake_verstr | tr '.' '\n' | sed -n 3p)
cmake_version="$(printf %05d%05d%05d $cmake_major $cmake_minor $cmake_patch)"

MFC_CMAKE_MIN_VERSTR=$(cat CMakeLists.txt | grep cmake_minimum_required | head -n 1 | sed 's/[^0-9,.]*//g')
MFC_CMAKE_MIN_MAJOR=$(echo $MFC_CMAKE_MIN_VERSTR | tr '.' '\n' | head -n 1)
MFC_CMAKE_MIN_MINOR=$(echo $MFC_CMAKE_MIN_VERSTR | tr '.' '\n' | head -n 2 | tail -n 1)
MFC_CMAKE_MIN_PATCH=0
MFC_CMAKE_MIN_VERSION="$(printf %05d%05d%05d $MFC_CMAKE_MIN_MAJOR $MFC_CMAKE_MIN_MINOR $MFC_CMAKE_MIN_PATCH)"

if [ "$cmake_version" -lt "$MFC_CMAKE_MIN_VERSION" ]; then
# Out of date
bShouldInstallCMake=true

log "$MAGENTA CMake$YELLOW is out of date (current: $MAGENTA$cmake_verstr$YELLOW < minimum: $MAGENTA$MFC_CMAKE_MIN_VERSTR$YELLOW).$COLOR_RESET"
fi
fi

if [ "$bShouldInstallCMake" = true ]; then
version="3.25.2"
arch="$(uname -m)"

bErrorDoSelfDownload=false
if [[ "$OSTYPE" == "darwin"* ]]; then
error "Cannot fetch$MAGENTA CMake$COLOR_RESET for$MAGENTA macOS$COLOR_RESET."
bErrorDoSelfDownload=true
fi

if ! [[ "$arch" == "x86_64" || "$arch" == "aarch64" ]]; then
error "Cannot fetch$MAGENTA CMake$COLOR_RESET for architecture $MAGENTA$arch$COLOR_RESET."
bErrorDoSelfDownload=true
fi

if [ "$bErrorDoSelfDownload" = true ]; then
log "Please install$MAGENTA CMake$COLOR_RESET manually:"
log " - via$MAGENTA Aptitude$COLOR_RESET: $ sudo apt install cmake"
log " - via$MAGENTA Homebrew$COLOR_RESET: $ brew install cmake"
log " - via$MAGENTA Spack$COLOR_RESET: $ spack install cmake"
log " - via$MAGENTA Pacman$COLOR_RESET: $ sudo pacman -S cmake"
log " - via$MAGENTA Lmod$COLOR_RESET: $ module load cmake (if available)"
log " - via$MAGENTA cmake.org$COLOR_RESET: https://cmake.org/download/"

exit 1
fi

if [ -d "$(pwd)/build/cmake" ]; then
rm -rf "$(pwd)/build/cmake"
fi

mkdir -p "$(pwd)/build/cmake"

filename="cmake-$version-linux-$arch.sh"
repository="https://github.com/Kitware/CMake"
url="$repository/releases/download/v$version/$filename"

log "Downloading$MAGENTA CMake v$version$COLOR_RESET for $MAGENTA$arch$COLOR_RESET from $CYAN$repository$COLOR_RESET."

cmake_fatal_error() {
log "$MAGENTA""CMake$COLOR_RESET is not discoverable or is an older release, incompatible with$MAGENTA MFC$COLOR_RESET. Please download"
log "or install a recent version of$MAGENTA CMake$COLOR_RESET to get past this step. If you are currently on a"
log "managed system like a cluster, provided there is no suitable environment module, you can"
log "either build it from source, or get it via$MAGENTA Spack$COLOR_RESET."
log "- The minimum required version is currently$MAGENTA CMake$COLOR_RESET v$MFC_CMAKE_MIN_MAJOR.$MFC_CMAKE_MIN_MINOR.$MFC_CMAKE_MIN_PATCH."
log "- We attempted to download$MAGENTA CMake$COLOR_RESET v$version from $url."

exit 1
}

if ! command -v wget > /dev/null 2>&1; then
error "$MAGENTA""wget$COLOR_RESET is not installed but is necessary to download$MAGENTA CMake$COLOR_RESET."

cmake_fatal_error
fi

if ! wget -P "$(pwd)/build/cmake" "$url"; then
error "Failed to download a compatible version of$MAGENTA CMake$COLOR_RESET."

cmake_fatal_error
fi

log "Installing$MAGENTA CMake$COLOR_RESET into $MAGENTA$(pwd)/build/cmake$COLOR_RESET."

if ! $SHELL "$(pwd)/build/cmake/$filename" "--skip-license" "--prefix=$(pwd)/build/cmake"; then
error "Failed to install a compatible version of CMake."

cmake_fatal_error
fi

rm "$(pwd)/build/cmake/$filename"

ok "Found$MAGENTA CMake$COLOR_RESET: $MAGENTA$(pwd)/build/cmake/bin/cmake$COLOR_RESET."
export PATH="$(pwd)/build/cmake/bin:$PATH"
fi
33 changes: 33 additions & 0 deletions toolchain/bootstrap/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

if ! command -v docker > /dev/null 2>&1; then
error "$MAGENTA""Docker$COLOR_RESET is not installed."

exit 1
fi

log "Running in$MAGENTA Docker$COLOR_RESET mode."

if [ -t 1 ]; then
dockerintopts='--interactive --tty'
fi

__docker_run() {
docker run $dockerintopts --rm --workdir /home/me/MFC \
--mount type=bind,source="$(pwd)",target=/home/me/MFC \
sbryngelson/mfc:latest $@
}

__docker_run sudo chown -R me:me /home/me/MFC
if (($?)); then
error "Docker: Failed to set directory permissions on MFC mount.."

exit 1
fi

__docker_run $@
if (($?)); then
error "Error running Docker container with $@."

exit 1
fi
18 changes: 18 additions & 0 deletions toolchain/bootstrap/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

log "Formatting MFC with fprettify..."

fprettify ${@:-src} --exclude "src/*/autogen" --recursive --silent \
--indent 4 --c-relations --enable-replacements --enable-decl \
--whitespace-comma 1 --whitespace-multdiv 1 --whitespace-plusminus 1 \
--case 1 1 1 1 --strict-indent
ret="$?"

if [ "$ret" != '0' ]; then
error "failed to execute fprettify."
error "MFC has not been fprettify'ied."

exit 1
fi

ok "MFC has been fprettify'ied."
130 changes: 130 additions & 0 deletions toolchain/bootstrap/modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash

unset u_c
unset u_cg

# Reset u_computer & u_cg to known values since this script is run with "source"
# Therefore, values of variables defined here are kept when the script runs again.
u_computer=""; u_cg=""

# If there are command-line arguments, parse them:
while [[ $# -gt 0 ]]; do
case $1 in
-c|--computer) u_c="$2"; shift; shift; ;;
-m|--mode) u_cg="$2"; shift; shift; ;;
-*|--*) echo "Unknown option $1"; return; ;;
esac
done

# Get computer (if not supplied in command-line)
if [ -v $u_c ]; then
log "Select a system:"
log "$G""ORNL$W: Ascent (a) | Crusher (c) | Summit (s) | Wombat (w)"
log "$C""ACCESS$W: Bridges2 (b) | Expanse (e) | Delta (d)"
log "$Y""GaTech$W: Phoenix (p)"
log "$R""CALTECH$W: Richardson (r)"
log_n "($G""a$W/$G""c$W/$G""s$W/$G""w$W/$C""b$W/$C""e$CR/$C""d$CR/$Y""p$CR/$R""r$CR): "
read u_c
log
fi

# Get CPU/GPU (if not supplied in command-line)
if [ -v $u_cg ]; then
log "Select configuration:"
log " - CPU (c | cpu)"
log " - GPU (g | gpu)"
log_n "(c/cpu/g/gpu): "
read u_cg
log
fi

# User input to lowercase
u_c=$(echo "$u_c" | tr '[:upper:]' '[:lower:]')
u_cg=$(echo "$u_cg" | tr '[:upper:]' '[:lower:]')

if [ "$u_cg" == 'c' ] || [ "$u_cg" == 'cpu' ]; then
CG='CPU'; cg='cpu'
elif [ "$u_cg" == "g" ] || [ "$u_cg" == 'gpu' ]; then
CG='GPU'; cg='gpu'
fi

__combine() {
echo -n $@ | sed 's/\[\n\r\s\]\+/\ /'
}

__extract() {
__combine "$(grep -E "^$1\s+" toolchain/modules | sed "s/^$1\s\+//")"
}

COMPUTER="$(__extract "$u_c")"

if [[ -z "$COMPUTER" ]]; then
error "Computer $M$u_cg$CR not recognized."
return
fi

MODULES=($(__extract "$u_c-$cg") $(__combine $(__extract "$u_c-all")))

log "Loading modules for $M$COMPUTER$CR on $M$CG$CR"'s:'

# Reset modules to default system configuration
if [ "$u_c" != 'p' ]; then
module reset > /dev/null 2>&1
code="$?"

# Purge if reset is not available
if [ "$code" -ne '0' ]; then
module purge > /dev/null 2>&1
fi
else
module purge > /dev/null 2>&1
fi

# Find length of longest module_name in $MODULES for $COMPUTER
max_module_length="0"
for module_name in ${MODULES[@]}; do
module_length="${#module_name}"

if [ "$module_length" -gt "$max_module_length" ]; then
max_module_length="$module_length"
fi
done

# Load modules ($MODULES)
for module_name in ${MODULES[@]}; do
log_n " - $CYAN$module_name$COLOR_RESET "

# Add padding spaces
module_length="${#module_name}"
delta="$((max_module_length-module_length-1))"
if [ "$delta" -ge "0" ]; then
printf "%0.s " $(seq 0 $delta)
fi

# Load the module
module load "$module_name" > /dev/null 2>&1

# Handle Success / Failure
code=$?
if [ "$code" == '0' ]; then
echo -e "[$G""SUCCESS$W]"
else
echo -e "[$R""FAILURE$W]"

# Run load again to show error message
module load "$module_name"

return
fi
done

if [ "$cg" == 'gpu' ]; then
isnv=$($FC --version | grep NVIDIA | wc -l)
if [ $isnv -eq 0 ]; then
export CC=nvc CXX=nvc++ FC=nvfortran
fi
fi
ok 'All modules and environment variables have been loaded.'


return
Loading

0 comments on commit e80a3f7

Please sign in to comment.