From 100c732952897542f626cb98149407cc0156eed6 Mon Sep 17 00:00:00 2001 From: Reto Schneider Date: Fri, 2 Apr 2021 21:20:32 +0200 Subject: [PATCH] [GH-574] ci: Configure and integrate clang-format 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. --- .clang-format | 5 ++++ .github/workflows/compliance.yaml | 19 ++++++++++++++++ coap/er-coap-13/.clang-format | 4 ++++ tools/ci/run_ci.sh | 38 +++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 .clang-format create mode 100644 coap/er-coap-13/.clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..8d41d7a4b --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +IndentWidth: 4 +ColumnLimit: 120 diff --git a/.github/workflows/compliance.yaml b/.github/workflows/compliance.yaml index 1c0b45dfb..006dce845 100644 --- a/.github/workflows/compliance.yaml +++ b/.github/workflows/compliance.yaml @@ -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 diff --git a/coap/er-coap-13/.clang-format b/coap/er-coap-13/.clang-format new file mode 100644 index 000000000..ef2ae21fa --- /dev/null +++ b/coap/er-coap-13/.clang-format @@ -0,0 +1,4 @@ +--- +DisableFormat: true +SortIncludes: false +... diff --git a/tools/ci/run_ci.sh b/tools/ci/run_ci.sh index 6b7d21af3..61e925674 100755 --- a/tools/ci/run_ci.sh +++ b/tools/ci/run_ci.sh @@ -24,6 +24,7 @@ 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="" @@ -31,6 +32,7 @@ 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 @@ -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 @@ -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 @@ -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 } @@ -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 \ @@ -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 @@ -233,6 +266,7 @@ while true; do shift ;; -a|--all) + RUN_CLANG_FORMAT=1 RUN_CLEAN=1 RUN_GITLINT=1 RUN_BUILD=1 @@ -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