Files
chipyard/scripts/utils.sh
JL102 6b70dd6d39 Added "try-catch" to all build-setup steps
This was the only way I knew how to display the step at which the build-setup process failed.
I've personally experienced failures at multiple of the build steps, and before I got used to Chipyard,
it was hard to figure out which step was the culprit. With this, users should have a bit more info to
troubleshoot their issues. For some of the build steps that required multiple lines, I figured it made
more sense to put them into a sub-script, rather than putting a && at the end of each line. But for the
firesim one for example, since it was two .sh calls, I just put a && after the first one, inside of the
try block, to make sure both lines run.
2023-10-05 02:17:25 -04:00

77 lines
1.6 KiB
Bash
Executable File

#/usr/bin/env bash
#######################################
# Common setup. Init MacOS compatibility
# variables.
# Globals:
# READLINK
#######################################
function common_setup
{
# On macOS, use GNU readlink from 'coreutils' package in Homebrew/MacPorts
if [ "$(uname -s)" = "Darwin" ] ; then
READLINK=greadlink
else
READLINK=readlink
fi
}
#######################################
# Error echo wrapper
#######################################
function error
{
echo "${0##*/}: ${1}" >&2
}
#######################################
# Error then exit wrapper
# Arguments:
# string to print before exit
# (optional) int error code
#######################################
function die
{
error "$1"
exit "${2:--1}"
}
#######################################
# Save bash options. Must be called
# before a corresponding `restore_bash_options`.
#######################################
function save_bash_options
{
OLDSTATE=$(set +o)
}
#######################################
# Restore bash options. Must be called
# after a corresponding `save_bash_options`.
#######################################
function restore_bash_options
{
set +vx; eval "$OLDSTATE"
}
#######################################
# Basic try-catch block implementation
# for bash scripts.
# Usage: try; ( run commands )
# catch || { handle error }
# Source: https://stackoverflow.com/a/25180186/5121242
#######################################
function try()
{
[[ $- = *e* ]]; SAVED_OPT_E=$?
set +e
}
function catch()
{
export ex_code=$?
(( $SAVED_OPT_E )) && set +e
return $ex_code
}