Skip to content

Commit

Permalink
[GH-574] ci: Configure and integrate clang-format
Browse files Browse the repository at this point in the history
git-clang-format ensures that all *new* code being submitted adheres to
the format dictated by clang-format.

The code style is based on LLVM (the creator of clang-format), but with
4 instead of 2 spaces indent width and a maximum of 120 instead of 80
characters per line in order to blend in well with existing code.

Please note:
- The er-coap-13 code gets exempt, as at some point, we probably want to
  to interact with whatever upstream there might be. For those files,
  just do what existing code does (which varies every few lines).
- Default is clang-format-10, which is available in the Ubuntu 20.04
  GitHub runner.
  • Loading branch information
rettichschnidi committed Apr 23, 2021
1 parent 6e19254 commit 100c732
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 120
19 changes: 19 additions & 0 deletions .github/workflows/compliance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,22 @@ jobs:
- name: Check commits with gitlint
run: |
tools/ci/run_ci.sh --branch-target origin/${{ github.base_ref }} --run-gitlint
check-clang-format:
name: Run clang-format
runs-on: ubuntu-20.04

steps:
- name: Checkout code including full history
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Install clang-format
run: |
sudo apt update
sudo apt -qy --no-install-recommends install clang-format-10
- name: Check commits with clang-format
run: |
tools/ci/run_ci.sh --branch-target origin/${{ github.base_ref }} --run-clang-format
4 changes: 4 additions & 0 deletions coap/er-coap-13/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
DisableFormat: true
SortIncludes: false
...
38 changes: 38 additions & 0 deletions tools/ci/run_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ OPT_BRANCH_SOURCE=
OPT_BRANCH_TARGET=master
OPT_C_EXTENSIONS=""
OPT_C_STANDARD=""
OPT_CLANG_FORMAT="clang-format-10"
OPT_SANITIZER=""
OPT_SCAN_BUILD=""
OPT_SONARQUBE=""
OPT_TEST_COVERAGE_REPORT=""
OPT_VERBOSE=0
OPT_WRAPPER_CMD=""
RUN_BUILD=0
RUN_CLANG_FORMAT=0
RUN_CLEAN=0
RUN_GITLINT=0
RUN_TESTS=0
Expand All @@ -49,6 +51,8 @@ Options:
(ENABLE: ON or OFF)
--c-standard VERSION Explicitly specify C VERSION to be used
(VERSION: 99, 11)
--clang-format BINARY Set specific clang-format binary
(BINARY: defaults to ${OPT_CLANG_FORMAT})
--sanitizer TYPE Enable sanitizer
(TYPE: address leak thread undefined)
--scan-build BINARY Enable Clang code analyzer using specified
Expand All @@ -64,6 +68,7 @@ Options:
Available steps (executed by --all):
--run-gitlint Check git commits with gitlint
--run-clang-format Check code formatting with clang-format
--run-clean Remove all build artifacts
--run-build Build all targets
--run-tests Build and execute tests
Expand All @@ -76,6 +81,24 @@ function usage() {
exit "${exit_code}"
}

function run_clang_format() {
local patch_file

patch_file="$(mktemp -t clang-format-patch.XXX)"
# shellcheck disable=SC2064
trap "{ rm -f -- '${patch_file}'; }" EXIT TERM INT

"git-${OPT_CLANG_FORMAT}" --diff "${OPT_BRANCH_TARGET}" |
{ grep -v 'no modified files to format' || true; } > "${patch_file}"

if [ -s "${patch_file}" ]; then
cat "${patch_file}"
exit 1
fi

echo "No code formatting errors found"
}

function run_clean() {
rm -rf build-wakaama
}
Expand Down Expand Up @@ -153,8 +176,10 @@ if ! PARSED_OPTS=$(getopt -o vah \
-l branch-target: \
-l c-extensions: \
-l c-standard: \
-l clang-format: \
-l help \
-l run-build \
-l run-clang-format \
-l run-clean \
-l run-gitlint \
-l run-tests \
Expand Down Expand Up @@ -188,6 +213,14 @@ while true; do
OPT_C_STANDARD=$2
shift 2
;;
--clang-format)
OPT_CLANG_FORMAT=$2
shift 2
;;
--run-clang-format)
RUN_CLANG_FORMAT=1
shift
;;
--run-clean)
RUN_CLEAN=1
shift
Expand Down Expand Up @@ -233,6 +266,7 @@ while true; do
shift
;;
-a|--all)
RUN_CLANG_FORMAT=1
RUN_CLEAN=1
RUN_GITLINT=1
RUN_BUILD=1
Expand Down Expand Up @@ -296,6 +330,10 @@ if [ "${RUN_GITLINT}" -eq 1 ]; then
run_gitlint
fi

if [ "${RUN_CLANG_FORMAT}" -eq 1 ]; then
run_clang_format
fi

if [ "${RUN_CLEAN}" -eq 1 ]; then
run_clean
fi
Expand Down

0 comments on commit 100c732

Please sign in to comment.