Skip to content

Commit

Permalink
[GH-574] ci: Enforce CMake formatting
Browse files Browse the repository at this point in the history
This ensures we can always run cmake-format on our CMake files.
  • Loading branch information
rettichschnidi committed Feb 8, 2022
1 parent a9eefea commit 6eaeea2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compliance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Check CMakeLists.txt files with cmake-lint
run: |
tools/ci/run_ci.sh --run-cmake-lint
tools/ci/run_ci.sh --run-cmake-format
- name: Check Python code with pylint
run: |
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ On Ubuntu 20.04, used in CI, the dependencies can be installed as such:
- `pip3 install -r tools/requirements-compliance.txt`

### Code formatting
New code must be formatted with [clang-format](https://clang.llvm.org/docs/ClangFormat.html).
#### C
New C code must be formatted with [clang-format](https://clang.llvm.org/docs/ClangFormat.html).

The style is based on the LLVM style, but with 4 instead of 2 spaces indentation and allowing for 120 instead of 80
characters per line.
Expand All @@ -109,6 +110,13 @@ To check if your code matches the expected style, the following commands are hel
If existing code gets reformatted, this must be done in a separate commit. Its commit id has to be added to the file
`.git-blame-ignore-revs` and committed in yet another commit.

#### CMake
All CMake code must be formatted with [cmake-format](https://github.com/cheshirekow/cmake_format).

To check if your code matches the expected style, the following commands are helpful:
- `tools/ci/run_ci.sh --run-cmake-format`: Test all CMake files, print offending ones
- `cmake-format --in-place <unformatted-file>`: Apply all needed changes directly to <unformatted-file>

### Running CI tests locally
To avoid unneeded load on the GitHub infrastructure, please consider running `tools/ci/run_ci.sh --all` before pushing.

Expand Down
27 changes: 18 additions & 9 deletions tools/ci/run_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OPT_WRAPPER_CMD=""
RUN_BUILD=0
RUN_CLANG_FORMAT=0
RUN_CLEAN=0
RUN_CMAKE_LINT=0
RUN_CMAKE_FORMAT=0
RUN_GITLINT=0
RUN_GIT_BLAME_IGNORE=0
RUN_TESTS=0
Expand Down Expand Up @@ -76,7 +76,7 @@ Available steps (executed by --all):
--run-clang-format Check C code formatting
--run-git-blame-ignore Validate .git-blame-ignore-revs
--run-clean Remove all build artifacts
--run-cmake-lint Check CMake files formatting
--run-cmake-format Check CMake files formatting
--run-build Build all targets
--run-tests Execute tests (works only for top level project)
"
Expand Down Expand Up @@ -113,8 +113,17 @@ function run_clean() {
rm -rf build-wakaama
}

function run_cmake_lint() {
function run_cmake_format() {
# Ensure formatting respects the defined style. Running cmake-lint before
# cmake-format gives a nicer message if formatting does not match the
# specified format.
git ls-files '*CMakeLists.txt' '*.cmake' | xargs cmake-lint

# Ensure formatting is exactly what cmake-format produces. Insisting on this
# ensures we can run cmake-format on all CMake files at any time.
if ! git ls-files '*CMakeLists.txt' '*.cmake' | xargs cmake-format --check; then
echo 'Please run cmake-format on all (changed) CMake files.'
fi
}

function run_gitlint() {
Expand Down Expand Up @@ -204,7 +213,7 @@ if ! PARSED_OPTS=$(getopt -o vah \
-l run-build \
-l run-clang-format \
-l run-clean \
-l run-cmake-lint \
-l run-cmake-format \
-l run-gitlint \
-l run-git-blame-ignore \
-l run-tests \
Expand Down Expand Up @@ -251,8 +260,8 @@ while true; do
RUN_CLEAN=1
shift
;;
--run-cmake-lint)
RUN_CMAKE_LINT=1
--run-cmake-format)
RUN_CMAKE_FORMAT=1
shift
;;
--run-build)
Expand Down Expand Up @@ -306,7 +315,7 @@ while true; do
-a|--all)
RUN_CLANG_FORMAT=1
RUN_CLEAN=1
RUN_CMAKE_LINT=1
RUN_CMAKE_FORMAT=1
RUN_GITLINT=1
RUN_GIT_BLAME_IGNORE=1
RUN_BUILD=1
Expand Down Expand Up @@ -382,8 +391,8 @@ if [ "${RUN_CLEAN}" -eq 1 ]; then
run_clean
fi

if [ "${RUN_CMAKE_LINT}" -eq 1 ]; then
run_cmake_lint
if [ "${RUN_CMAKE_FORMAT}" -eq 1 ]; then
run_cmake_format
fi

if [ "${RUN_BUILD}" -eq 1 ]; then
Expand Down

0 comments on commit 6eaeea2

Please sign in to comment.