From f8652e3a0f616975c800c3fed6a1287a50fac33c Mon Sep 17 00:00:00 2001 From: Alexandre Quesnel <131881+aquesnel@users.noreply.github.com> Date: Sat, 8 May 2021 16:53:32 +0000 Subject: [PATCH] Add checking the code formatting with astyle during CI builds --- .github/workflows/build.yml | 29 +++++ astyle_config.as | 9 +- scripts/install_astyle.sh | 108 ++++++++++++++++++ .../install_astyle_dependencies_with_apt.sh | 6 + scripts/run_astyle.sh | 74 ++++++++++++ 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100755 scripts/install_astyle.sh create mode 100755 scripts/install_astyle_dependencies_with_apt.sh create mode 100755 scripts/run_astyle.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2bc452f1..529edf59 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -146,3 +146,32 @@ jobs: - run: ./bootstrap - run: scripts/install_cppcheck.sh $CPPCHECK_REPO $CPPCHECK_VER - run: scripts/run_cppcheck.sh -v $CPPCHECK_VER + + code_formatting_check: + name: code formatting check + runs-on: ubuntu-latest + env: + CC: gcc + # This is required to use a version of astyle other than that + # supplied with the operating system + ASTYLE_VER: 3.1 + ASTYLE_REPO: https://svn.code.sf.net/p/astyle/code/tags + steps: + # This is currently the only way to get a version into + # the cache tag name - see https://github.com/actions/cache/issues/543 + - run: | + echo "OS_VERSION=`lsb_release -sr`" >> $GITHUB_ENV + - uses: actions/checkout@v2 + - name: Cache astyle + uses: actions/cache@v2 + env: + cache-name: cache-astyle + with: + path: ~/astyle.local + key: ${{ runner.os }}-${{ env.OS_VERSION }}-build-${{ env.cache-name }}-${{ env.ASTYLE_VER }} + - run: sudo scripts/install_astyle_dependencies_with_apt.sh + - run: scripts/install_astyle.sh $ASTYLE_REPO $ASTYLE_VER + - name: Format code with astyle + run: scripts/run_astyle.sh + - name: Check code formatting + run: git diff --exit-code diff --git a/astyle_config.as b/astyle_config.as index 7bcc1da4..176a8f68 100644 --- a/astyle_config.as +++ b/astyle_config.as @@ -45,6 +45,14 @@ # For each directory in the command line, process all subdirectories recursively. --recursive +# Exclude git submodule directories and generated files. +--exclude=libpainter +--exclude=librfxcodec +--exclude=xrdp_configure_options.h + +# ignore errors from generated files that do not exist +--ignore-exclude-errors + # Preserve the original file's date and time modified. --preserve-date @@ -53,4 +61,3 @@ --formatted --lineend=linux - diff --git a/scripts/install_astyle.sh b/scripts/install_astyle.sh new file mode 100755 index 00000000..2ce3d52f --- /dev/null +++ b/scripts/install_astyle.sh @@ -0,0 +1,108 @@ +#!/bin/sh + +# Script to install a version of astyle in ~/astyle.local/ +# +# Used by CI builds +# +# Currently only supports git repos as sources +# +# Usage: /path/to/install_astyle.sh + +INSTALL_ROOT=~/astyle.local + +# ---------------------------------------------------------------------------- +# U S A G E +# ---------------------------------------------------------------------------- +usage() +{ + echo "** Usage: $0 " + echo " e.g. $0 https://svn.code.sf.net/p/astyle/code/tags 3.1" +} >&2 + +# ---------------------------------------------------------------------------- +# C A L L _ M A K E +# +# Calls make with the specified parameters, but only displays the error +# log if it fails +# ---------------------------------------------------------------------------- +call_make() +{ + # Disable set -e, if active + set_entry_opts=`set +o` + set +e + + status=1 + log=`mktemp /tmp/astyle-log.XXXXXXXXXX` + if [ -n "$log" ]; then + make "$@" >$log 2>&1 + status=$? + if [ $status -ne 0 ]; then + cat $log >&2 + fi + rm $log + fi + + # Re-enable `set -e` if active before + $set_entry_opts + + return $status +} + + +# ---------------------------------------------------------------------------- +# M A I N +# ---------------------------------------------------------------------------- +if [ $# -ne 2 ]; then + usage + exit 1 +fi + +REPO_URL="$1" +ASTYLE_VER="$2" + +# Already installed? +exe=$INSTALL_ROOT/$ASTYLE_VER/usr/bin/astyle +if [ -x "$exe" ]; then + echo "astyle version $ASTYLE_VER is already installed at $exe" >&2 + exit 0 +fi + +workdir=`mktemp -d /tmp/astyle.XXXXXXXXXX` +if [ -z "$workdir" ]; then + echo "** Unable to create temporary working directory" 2>&1 + exit 1 +fi + +# Use a sub-process for the next bit to restrict the scope of 'set -e' +( + set -e ; # Exit sub-process on first error + + # Put everything in this directory + FILESDIR=$INSTALL_ROOT/$ASTYLE_VER + + svn checkout ${REPO_URL}/${ASTYLE_VER}/AStyle $workdir + + cd $workdir + + make_args="DESTDIR=$FILESDIR" + + echo "Creating Makefiles..." + cmake . + + echo "Making astyle..." + call_make $make_args + + echo "Installing astyle..." + mkdir -p $FILESDIR + call_make install $make_args + # make install DESTDIR=~/astyle.local/3.1 +) +status=$? + +if [ $status -eq 0 ]; then + rm -rf $workdir +else + "** Script failed. Work dir is $workdir" >&2 +fi + +exit $status diff --git a/scripts/install_astyle_dependencies_with_apt.sh b/scripts/install_astyle_dependencies_with_apt.sh new file mode 100755 index 00000000..4aa40ff1 --- /dev/null +++ b/scripts/install_astyle_dependencies_with_apt.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eufx + +PACKAGES="subversion cmake" + +apt-get -yq --no-install-suggests --no-install-recommends install $PACKAGES \ No newline at end of file diff --git a/scripts/run_astyle.sh b/scripts/run_astyle.sh new file mode 100755 index 00000000..abe45168 --- /dev/null +++ b/scripts/run_astyle.sh @@ -0,0 +1,74 @@ +#!/bin/sh + +# Script to run astyle on the code +# +# Usage: /path/to/run_astyle.sh +# +# Note: the script must be run from the root directory of the xrdp repository + +INSTALL_ROOT=~/astyle.local +ASTYLE_FROM_XRDP=$INSTALL_ROOT/3.1/usr/bin/astyle +MIN_ASTYLE_VER="3.1" + +# ---------------------------------------------------------------------------- +# U S A G E +# ---------------------------------------------------------------------------- +usage() +{ + echo "** Usage: $0" + echo " e.g. $0" +} >&2 + +# ---------------------------------------------------------------------------- +# M A I N +# ---------------------------------------------------------------------------- +if [ $# -ne 0 ]; then + usage + exit 1 +fi + +# check if the built-in astyle meets the minimum requrements +ASTYLE_FROM_OS_VER_OUTPUT=`astyle --version | grep "Artistic Style Version" | cut -d' ' -f4` + +ASTYLE="" +ERROR_MESSAGE="" +if [ ! -z "$ASTYLE_FROM_OS_VER_OUTPUT" ]; then + # astyle is installed, so check if it's version meets the minimum requirements + LOWEST_VERSION=`echo -e "$MIN_ASTYLE_VER\n$ASTYLE_FROM_OS_VER_OUTPUT" | sort -V | head -n1` + if [ "$MIN_ASTYLE_VER" = "$LOWEST_VERSION" ]; then + ASTYLE=astyle + else + ERROR_MESSAGE="The version of astyle installed does not meet the minimum version requirement: >= $MIN_ASTYLE_VER " + fi +else + ERROR_MESSAGE="astyle is not installed on the system path" +fi + +if [ -z "$ASTYLE" ]; then + # astyle from the os is invlid, fallback to the xrdp version if it is installed + if [ -x "$ASTYLE_FROM_XRDP" ]; then + ASTYLE="$ASTYLE_FROM_XRDP" + ERROR_MESSAGE="" + else + ERROR_MESSAGE="${ERROR_MESSAGE}\nastyle $MIN_ASTYLE_VER is not installed at the expected path: $ASTYLE_FROM_XRDP" + fi +fi + +if [ ! -z "$ERROR_MESSAGE" ]; then + echo "$ERROR_MESSAGE" + exit 1 +fi + +if [ ! -f "astyle_config.as" ]; then + echo "$0 must be run from the root xrdp repository directory which " + echo "contains the 'astyle_config.as' file." + exit 2 +fi + +ASTYLE_FLAGS="--options=astyle_config.as ./\*.c ./\*.h" + +# Display the astyle version and command for debugging +"$ASTYLE" --version && { + echo Command: $ASTYLE $ASTYLE_FLAGS + "$ASTYLE" $ASTYLE_FLAGS +}