#!/usr/bin/env bash

#
# This shell script is called from the Makefile since it is much simpler
#   to implement environment modifications in Bash than Make.
#

if [ "$DEBUG" == "1" ]; then
  echo "Building Mason in debug mode"
else
  echo "Building Mason in release mode"
fi

export CHPL_COMM=none
export CHPL_RE2=bundled

if ! $CHPL_MAKE_HOME/util/printchplenv --diagnose-lib=runtime >/dev/null 2>/dev/null; then
  cat<<EOF
  Cannot build mason with this configuration.
  Check that you have built a Chapel configuration that supports:
  CHPL_COMM=none
  CHPL_RE2=bundled
EOF
  exit 1
fi

if command -v module > /dev/null 2>&1 ; then
  HUGEPAGES=$(module list 2>&1 | grep craype-hugepage | sed -e"s:[0-9]*)::g")
  if [ ! -z ${HUGEPAGES} ]; then
      echo "Unloading hugepages..."
      module unload "${HUGEPAGES}"
  fi
fi

COMPOPTS=""
PRE_COMPOPTS="--launcher=none"
POST_COMPOPTS="--checks"
if [ "$DEBUG" == "1" ]; then
  # TODO add --debug-safe-optimizations-only once https://github.com/chapel-lang/chapel/issues/28165 is resolved
  # for now, use a limited subset of what --debug-safe-optimizations-only
  # would do
  COMPOPTS="$COMPOPTS -g --no-devel --no-inline --no-copy-propagation"
else
  # avoid specialization to prevent micro-architectural optimizations that
  # could break mason in cross platform scenarios
  # this is probably overcautious
  COMPOPTS="$COMPOPTS --fast --no-specialize"
fi
ALL_COMPOPTS="$PRE_COMPOPTS $COMPOPTS $POST_COMPOPTS"

COMMAND="$chplBinDir/chpl $ALL_COMPOPTS Mason.chpl -o $chplBinDir/mason"
echo "Building Mason: $COMMAND"

if ! $COMMAND; then
  cat<<EOF
  Error building mason.
EOF
  exit 1
fi
