Skip to content

Commit

Permalink
ci: add shellcheck gh action, lint fixes
Browse files Browse the repository at this point in the history
This commit made with the assistance of github copilot

Signed-off-by: Morgan Rockett <[email protected]>
  • Loading branch information
rockett-m committed Aug 6, 2024
1 parent 4c91a6d commit 7bc17ac
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 127 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ jobs:
run: sudo ./scripts/install-build-tools.sh
- name: Lint with Pylint
run: ./scripts/pylint.sh
shellcheck:
name: Shellcheck
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Install Shellcheck
run: |
sudo apt-get update
sudo apt-get install -y shellcheck
- name: Lint with Shellcheck
run: |
NUM_CORES=$(nproc)
find . -name '*.sh' -print0 | xargs -0 -n 1 -P $NUM_CORES shellcheck
unit-and-integration-test:
name: Unit and Integration Tests
runs-on: ubuntu-22.04
Expand All @@ -84,7 +99,7 @@ jobs:
run: ./scripts/test.sh
- name: Shorten SHA
id: vars
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- uses: actions/upload-artifact@v4
if: ${{ !env.ACT }}
name: Archive Test Results
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CMakeFiles/
plots/
.deps/
.libs/
.cache/

# Database
blocks.dat
Expand Down
16 changes: 8 additions & 8 deletions benchmarks/transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ void reset_wallets(cbdc::transaction::wallet& w1,
/// @brief Time an N-in, 1-out transaction.
/// @brief Note: handles benchmark timing, do not time outside function.
/// @param sender
/// @param reciever
/// @param receiver
/// @param n_in
/// @param state
/// @return
inline bool generate_Nto1_tx(cbdc::transaction::wallet& sender,
cbdc::transaction::wallet& reciever,
cbdc::transaction::wallet& receiver,
uint32_t n_in,
benchmark::State& state) {
std::optional<cbdc::transaction::full_tx> maybe_tx{};
state.ResumeTiming();
maybe_tx = sender.send_to(n_in * 2, reciever.generate_key(), true).value();
maybe_tx = sender.send_to(n_in * 2, receiver.generate_key(), true).value();
state.PauseTiming();
if(maybe_tx.has_value()) {
sender.confirm_transaction(*maybe_tx);
reciever.confirm_transaction(*maybe_tx);
receiver.confirm_transaction(*maybe_tx);
return true;
}
return false;
Expand All @@ -64,22 +64,22 @@ inline bool generate_Nto1_tx(cbdc::transaction::wallet& sender,
/// @brief Time an N-in, 2-out transaction.
/// @brief Note: handles benchmark timing, do not time outside function.
/// @param sender
/// @param reciever
/// @param receiver
/// @param n_in
/// @param state
/// @return
inline bool generate_Nto2_tx(cbdc::transaction::wallet& sender,
cbdc::transaction::wallet& reciever,
cbdc::transaction::wallet& receiver,
uint32_t n_in,
benchmark::State& state) {
std::optional<cbdc::transaction::full_tx> maybe_tx{};
state.ResumeTiming();
maybe_tx
= sender.send_to(n_in * 2 - 1, reciever.generate_key(), true).value();
= sender.send_to(n_in * 2 - 1, receiver.generate_key(), true).value();
state.PauseTiming();
if(maybe_tx.has_value()) {
sender.confirm_transaction(*maybe_tx);
reciever.confirm_transaction(*maybe_tx);
receiver.confirm_transaction(*maybe_tx);
return true;
}
return false;
Expand Down
27 changes: 14 additions & 13 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

help() {
if [ $# -gt 0 ]; then
if [[ $# -gt 0 ]]; then
printf 'Unexpected Argument (%s)\n' "$1"
fi
printf 'HELP: Usage: %s [Debug|Release|Profiling]\n' "$0"
Expand All @@ -13,15 +13,15 @@ help() {
# Note:
# CMAKE_BUILD_TYPE="Debug" adds "-O0 -g" flags by default
# CMAKE_BUILD_TYPE="Release" adds "-O3 -DNDEBUG" by default
if [[ "$BUILD_DEBUG" == "1" ]]; then
if [[ "${BUILD_DEBUG}" == "1" ]]; then
CMAKE_BUILD_TYPE="Debug"
elif [[ "$BUILD_RELEASE" == "1" ]]; then
elif [[ "${BUILD_RELEASE}" == "1" ]]; then
CMAKE_BUILD_TYPE="Release"
elif [[ "$BUILD_PROFILING" == "1" ]]; then
elif [[ "${BUILD_PROFILING}" == "1" ]]; then
CMAKE_BUILD_TYPE="Profiling"
fi

if [ $# -gt 0 ]; then
if [[ $# -gt 0 ]]; then
case "$1" in
Release|--release|-r) CMAKE_BUILD_TYPE="Release";;
Profiling|--profiling|-p) CMAKE_BUILD_TYPE="Profiling";;
Expand All @@ -36,29 +36,30 @@ echo "Building..."
# see PREFIX in ./scripts/setup-dependencies.sh
PREFIX="$(cd "$(dirname "$0")"/.. && pwd)/prefix"

if [ -z ${BUILD_DIR+x} ]; then
if [[ -z ${BUILD_DIR+x} ]]; then
export BUILD_DIR=build
fi

mkdir -p $BUILD_DIR
cd $BUILD_DIR
mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

CMAKE_FLAGS=-DCMAKE_PREFIX_PATH="${PREFIX}"
CPUS=1
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
CPUS=$(grep -c ^processor /proc/cpuinfo)
elif [[ "$OSTYPE" == "darwin"* ]]; then
elif [[ "${OSTYPE}" == "darwin"* ]]; then
CPUS=$(sysctl -n hw.ncpu)
XCODE_CMDLINE_DIR=$(xcode-select -p)
CMAKE_FLAGS+=" -DCMAKE_C_COMPILER=${XCODE_CMDLINE_DIR}/usr/bin/clang -DCMAKE_CXX_COMPILER=${XCODE_CMDLINE_DIR}/usr/bin/clang++ -DCMAKE_CXX_FLAGS=-isystem\ /usr/local/include -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
fi

if [[ -z $CMAKE_BUILD_TYPE ]]; then
if [[ -z "${CMAKE_BUILD_TYPE}" ]]; then
echo "CMAKE_BUILD_TYPE not set, defaulting to debug"
CMAKE_BUILD_TYPE="Debug"
fi

echo "Building $CMAKE_BUILD_TYPE"
echo "Building ${CMAKE_BUILD_TYPE}"
eval "cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${CMAKE_FLAGS} .."
make -j$CPUS
make "-j${CPUS}"

echo; echo "Build complete"; echo
71 changes: 36 additions & 35 deletions scripts/install-build-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ENV_NAME=".py_venv"
# make a virtual environement to install python packages
create_venv_install_python() {
PY_LOC=$1
if [[ -z "$PY_LOC" ]]; then
if [[ -z "${PY_LOC}" ]]; then
echo "Python path not provided"
exit 1
fi
Expand All @@ -57,22 +57,22 @@ create_venv_install_python() {
fi
# install pip for linux
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if ! $SUDO apt install -y python3-pip; then
if ! ${SUDO} apt install -y python3-pip; then
echo "Failed to install python3-pip"
wget https://bootstrap.pypa.io/get-pip.py
$SUDO python${PY_VERSION} get-pip.py
${SUDO} python${PY_VERSION} get-pip.py
rm get-pip.py
fi
# add deadsnakes to download the python venv module
$SUDO add-apt-repository -y ppa:deadsnakes/ppa
${SUDO} add-apt-repository -y ppa:deadsnakes/ppa
# make sure deadsnakes is available
DEADSNAKES_AVAIL=$(wget -q --spider http://ppa.launchpad.net/deadsnakes/ppa/ubuntu/dists/focal/Release; echo $?)
if [[ $DEADSNAKES_AVAIL -ne 0 ]]; then
echo "Failed to add deadsnakes which is needed to install python3"
exit 1
fi
# install python3 venv module for linux
if ! $SUDO apt install -y "python${PY_VERSION}-venv"; then
if ! ${SUDO} apt install -y "python${PY_VERSION}-venv"; then
echo "Failed to install python${PY_VERSION}-venv"
exit 1
else
Expand Down Expand Up @@ -102,19 +102,20 @@ create_venv_install_python() {
deactivate
}

echo "OS Type: $OSTYPE"
echo "OS Type: ${OSTYPE}"
# macOS install with homebrew
if [[ "$OSTYPE" == "darwin"* ]]; then
if [[ "${OSTYPE}" == "darwin"* ]]; then

# macOS does not support running shell scripts as root with homebrew
if [[ $EUID -eq 0 ]]; then
echo -e "Mac users should run this script without 'sudo'. Exiting..."
exit 1
fi

# either use $CPUS for parallelization or delete from this script
CPUS=$(sysctl -n hw.ncpu)
# ensure development environment is set correctly for clang
$SUDO xcode-select -switch /Library/Developer/CommandLineTools
${SUDO} xcode-select -switch /Library/Developer/CommandLineTools

if ! brew --version &>/dev/null; then
echo -e "${cyan}Homebrew is required to install dependencies.${end}"
Expand All @@ -127,21 +128,21 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
BREW_ROOT=$(brew --prefix)

CLANG_TIDY=/usr/local/bin/clang-tidy
if [[ ! -L "$CLANG_TIDY" ]]; then
$SUDO ln -s "${BREW_ROOT}/opt/llvm@14/bin/clang-tidy" /usr/local/bin/clang-tidy
if [[ ! -L "${CLANG_TIDY}" ]]; then
${SUDO} ln -s "${BREW_ROOT}/opt/llvm@14/bin/clang-tidy" /usr/local/bin/clang-tidy
fi
GMAKE=/usr/local/bin/gmake
if [[ ! -L "$GMAKE" ]]; then
$SUDO ln -s $(xcode-select -p)/usr/bin/gnumake /usr/local/bin/gmake
if [[ ! -L "${GMAKE}" ]]; then
${SUDO} ln -s $(xcode-select -p)/usr/bin/gnumake /usr/local/bin/gmake
fi

# install valid python version if not installed yet
if [[ -z "$PY_INSTALLED" ]]; then
if [[ -z "${PY_INSTALLED}" ]]; then
PY_VERS=${PYTHON_VERSIONS[0]}
FULL_PY="python${PY_VERS}"

MAX_RETRIES=2
while [[ $MAX_RETRIES -gt 0 ]]; do
while [[ ${MAX_RETRIES} -gt 0 ]]; do
# try to install python version from homebrew and verify installation
if brew install "${FULL_PY}"; then
echo "${FULL_PY} installed successfully"
Expand All @@ -151,58 +152,58 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
MAX_RETRIES=$((MAX_RETRIES - 1))
sleep 1
done
if [[ $MAX_RETRIES -eq 0 ]]; then
if [[ ${MAX_RETRIES} -eq 0 ]]; then
echo "Python3 install with homebrew failed, attempted on ${FULL_PY}"
exit 1
fi
fi
# Linux install with apt
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
elif [[ "${OSTYPE}" == "linux-gnu"* ]]; then
# avoids getting stuck on interactive prompts which is essential for CI/CD
export DEBIAN_FRONTEND=noninteractive
$SUDO apt update -y
$SUDO apt install -y build-essential wget cmake libgtest-dev libbenchmark-dev \
${SUDO} apt update -y
${SUDO} apt install -y build-essential wget cmake libgtest-dev libbenchmark-dev \
lcov git software-properties-common rsync unzip bc

# Add LLVM GPG key (apt-key is deprecated in Ubuntu 21.04+ so using gpg)
wget -qO - https://apt.llvm.org/llvm-snapshot.gpg.key | \
gpg --dearmor -o /usr/share/keyrings/llvm-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/llvm-archive-keyring.gpg] http://apt.llvm.org/focal/ llvm-toolchain-focal-14 main" | \
$SUDO tee /etc/apt/sources.list.d/llvm.list
${SUDO} tee /etc/apt/sources.list.d/llvm.list

$SUDO apt update -y
$SUDO apt install -y clang-format-14 clang-tidy-14
$SUDO ln -sf $(which clang-format-14) /usr/local/bin/clang-format
$SUDO ln -sf $(which clang-tidy-14) /usr/local/bin/clang-tidy
${SUDO} apt update -y
${SUDO} apt install -y clang-format-14 clang-tidy-14
${SUDO} ln -sf "$(which clang-format-14)" /usr/local/bin/clang-format
${SUDO} ln -sf "$(which clang-tidy-14)" /usr/local/bin/clang-tidy

# install valid python version if not installed yet
if [[ -z "$PY_INSTALLED" ]]; then
if [[ -z "${PY_INSTALLED}" ]]; then
PY_VERS=${PYTHON_VERSIONS[0]}
FULL_PY="python${PY_VERS}"

# try to install python version from apt and verify installation
$SUDO apt install -y software-properties-common
$SUDO add-apt-repository -y ppa:deadsnakes/ppa
$SUDO apt update -y
${SUDO} apt install -y software-properties-common
${SUDO} add-apt-repository -y ppa:deadsnakes/ppa
${SUDO} apt update -y

DEADSNAKES_AVAIL=$(wget -q --spider http://ppa.launchpad.net/deadsnakes/ppa/ubuntu/dists/focal/Release; echo $?)
if [[ $DEADSNAKES_AVAIL -ne 0 ]]; then
if [[ ${DEADSNAKES_AVAIL} -ne 0 ]]; then
echo "Failed to add deadsnakes which is needed to install python3"
exit 1
fi

MAX_RETRIES=2
while [[ $MAX_RETRIES -gt 0 ]]; do
while [[ ${MAX_RETRIES} -gt 0 ]]; do
# install python3 valid version and venv module
if $SUDO apt install -y ${FULL_PY}; then
if ${SUDO} apt install -y "${FULL_PY}"; then
echo "${FULL_PY} installed successfully"
PY_INSTALLED=${PY_VERS}
break
fi
MAX_RETRIES=$((MAX_RETRIES - 1))
sleep 1
done
if [[ $MAX_RETRIES -eq 0 ]]; then
if [[ ${MAX_RETRIES} -eq 0 ]]; then
echo "Python3 install with apt and deadsnakes failed, attempted on ${FULL_PY}"
exit 1
fi
Expand All @@ -216,16 +217,16 @@ if ! which "python${PY_INSTALLED}" &> /dev/null; then
else
# create virtual environment and install python packages for the valid python version
PYTHON_PATH=$(which "python${PY_INSTALLED}")
create_venv_install_python "${PYTHON_PATH}" ${PY_INSTALLED}
create_venv_install_python "${PYTHON_PATH}" "${PY_INSTALLED}"
fi
echo "To activate the virtual env to run python, run 'source ./scripts/activate-venv.sh'"

PYTHON_TIDY=/usr/local/bin/run-clang-tidy.py
if [[ ! -f "${PYTHON_TIDY}" ]]; then
echo -e "${green}Copying run-clang-tidy to /usr/local/bin${end}"
wget https://raw.githubusercontent.com/llvm/llvm-project/e837ce2a32369b2e9e8e5d60270c072c7dd63827/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
$SUDO mv run-clang-tidy.py /usr/local/bin
${SUDO} mv run-clang-tidy.py /usr/local/bin
fi

echo "Build environment setup complete."
echo "Next run './scripts/setup-dependencies.sh'."
echo; echo "Build environment setup complete."
echo "Next run './scripts/setup-dependencies.sh'"; echo
Loading

0 comments on commit 7bc17ac

Please sign in to comment.