Commit d54fa670 authored by David Frank's avatar David Frank Committed by Tobias Lasser
Browse files

#90 Add formatting and linting using cmake-format

The cmakelang package (see https://github.com/cheshirekow/cmake_format)
provides tools for linting and formatting CMake files. This commit
provides a basic infrastructure to make use of it.

First, the necessary formatting style file is added
('.cmake-format.py'). For many cases, the CMake files don't fulfill
these requirements yet, therefore, many linting checks are still
disabled.

Second, scripts and CI stages (only for linting) are added, which are
run alongside clang-format and clang-tidy. Currently, the formatting
job is not run, as the files are not yet formatted correctly.

Future MRs should work on slowly reducing the disabled codes.

Close #90
parent ffc8d708
Pipeline #522873 failed with stages
in 10 minutes and 38 seconds
......@@ -202,7 +202,7 @@ with section("markup"):
with section("lint"):
# a list of lint codes to disable
disabled_codes = ["C0103", "C0111", "C0304", "C0306", "E1120", "R0912", "C0113", "C0301", "C0303", "C0305", "C0307"]
disabled_codes = ["C0103", "C0111", "C0304", "C0306", "E1120", "R0912", "C0113", "C0301", "C0303", "C0305", "C0307", "W0105"]
# regular expression pattern describing valid function names
function_pattern = '[0-9a-z_]+'
......
......@@ -76,21 +76,21 @@ cache:
- build/**/tests/test_*tests-*.cmake
- build/Testing
# Add elsa install to artifact
# Add elsa install to artifact
- install-elsa/include/elsa/**/*.h
- install-elsa/include/elsa/**/*.hpp
- install-elsa/include/elsa/**/*.cuh
- install-elsa/lib/cmake/elsa/**/*.cmake
- install-elsa/lib/libelsa*.a
# Add quickvec install to artifact
# Add quickvec install to artifact
- install-elsa/include/Quickvec/**/*.h
- install-elsa/include/Quickvec/**/*.hpp
- install-elsa/lib/cmake/Quickvec/*.cmake
- install-elsa/lib/libquickvec.a
# Add doctest install to artifact
# Add doctest install to artifact
- install-elsa/include/doctest/**/*.h
- install-elsa/lib/cmake/doctest/**/*.cmake
# Add spdlog install to artifact
# Add spdlog install to artifact
- install-elsa/include/spdlog/**/*.h
- install-elsa/lib/cmake/spdlog/**/*.cmake
- install-elsa/lib/libspdlog.a
......@@ -193,6 +193,28 @@ comment-formating:
- elsa
- clang
.cmake-format:
<<: *run_always
stage: static-test
image: $CLANG_FORMAT_IMAGE
script:
./tools/ci_scripts/cmake-format-test.sh
tags:
- linux
- elsa
- clang
cmake-lint:
<<: *run_always
stage: static-test
image: $CLANG_FORMAT_IMAGE
script:
./tools/ci_scripts/cmake-lint-test.sh
tags:
- linux
- elsa
- clang
### compile jobs ###
build-ubuntu:
......@@ -248,10 +270,10 @@ build-gpu-only:
- cd build
- cmake .. -GNinja -DELSA_BENCHMARKS=ON -DELSA_CUDA_VECTOR=ON -DELSA_CREATE_JUNIT_REPORTS=ON -DCMAKE_INSTALL_PREFIX="../install-elsa" -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_EXE_LINKER_FLAGS="-lc++abi"
- ninja
- ninja build-tests
- ninja build-tests
- ninja build-benchmarks
- ninja build-examples
- ninja install
- ninja install
tags:
- linux
- elsa
......
......@@ -92,6 +92,16 @@ currently use version 10.0.0, different versions might produce errors.
We use `clang-tidy` with the enabled checks specified in [the configuration file](.clang-tidy). Note
that currently all `readability-*` checks have to pass, otherwise the CI will fail. We encourage
developers to check their code with `clang-tidy` and remove all warnings if applicable.
#### CMake
We use [cmakelang](https://cmake-format.readthedocs.io/en/latest/index.html) to enforce
certain style guide and reduce the changes of error in our CMake code, please check the guide to install it.
Currently, only the `cmake-lint` is used, but sooner rather than later, we'll also start
using `cmake-format` of the same package.
Please check the link above on how to install the package.
## Code Coverage
We use `lcov` with `gcov` for test coverage information. If you want to run this locally you have to
......
#!/bin/bash
# Format all CMake-like files with cmake-format
# Determine the applied differences with git,
# return 1 when changes had to be made, so the CI step fails.
# set exit on error
set -e
# preference list
cmake_format_tool_candiates=(cmake-format)
for candidate in ${cmake_format_tool_candiates[@]}; do
if command -v "$candidate" >/dev/null; then
echo "Formatting check with $candidate --version:"
$candidate --version
cmake_format_tool=$candidate
break
fi
done
if [[ -z "$cmake_format_tool" ]]; then
echo "$cmake_format_tool not correctly installed"
exit 1
fi
echo
# run cmake-format
format_call="find elsa benchmarks examples tools cmake -name '*.cmake' -o -name 'CMakeLists.txt' | xargs $cmake_format_tool -l error"
exit_code=0
eval $format_call --check || exit_code=$?
# if exit code 0, all is good
if [[ -z $exit_code ]]; then
echo "Excellent. You passed the formatting check!"
exit 0;
else
echo
echo "The above files have cmake-format problems"
echo "Inside the repo, please run"
echo
echo "$format_call -i"
echo
echo "to solve the issue."
fi
exit 1
#!/bin/bash
# Format all CMake-like files with cmake-format
# Determine the applied differences with git,
# return 1 when changes had to be made, so the CI step fails.
# set exit on error
set -e
# preference list
cmake_lint_tool_candiates=(cmake-lint)
for candidate in ${cmake_lint_tool_candiates[@]}; do
if command -v "$candidate" >/dev/null; then
echo "Linting check with $candidate --version:"
$candidate --version
cmake_lint_tool=$candidate
break
fi
done
if [[ -z "$cmake_lint_tool" ]]; then
echo "$cmake_lint_tool not correctly installed"
exit 1
fi
echo
# run cmake-lint
lint_call="find elsa benchmarks examples tools cmake -name '*.cmake' -o -name 'CMakeLists.txt' | xargs $cmake_lint_tool -l error"
exit_code=0
eval $lint_call || exit_code=$?
# if exit code 0, all is good
if [[ $exit_code -eq 0 ]]; then
echo "Excellent. You passed the linting check!"
exit 0;
else
echo
echo "The above files have cmake-lint problems"
echo "Inside the repo, please run"
echo
echo "$lint_call"
echo
echo "to see the issue. Please check the output"
fi
exit 1
......@@ -5,12 +5,12 @@ MAINTAINER David Frank <frankd@in.tum.de>
LABEL Description="Image only containing clang-format"
ARG CLANG_FORMAT_VERSION=10
RUN apt update && apt upgrade -qqy && apt install git clang-format-${CLANG_FORMAT_VERSION} -qqy \
&& rm -rf /var/lib/apt/lists/*
RUN apt update && apt upgrade -qqy && apt install git clang-format-${CLANG_FORMAT_VERSION} python3-pip -qqy \
&& pip install cmakelang && rm -rf /var/lib/apt/lists/*
# Append llvm directory to path, that we can use clang-format instead of clang-format-<some-version>
ENV PATH="$PATH:/usr/lib/llvm-${CLANG_FORMAT_VERSION}/bin/"
# Add Test script
ADD tests/test_clang-format.sh /tmp/tests/test_clang-format.sh
......
......@@ -18,4 +18,20 @@ if [ "$?" != "0" ] ; then
echo "Error clang tidy version mismatch, expected $1"
exit 1
fi
echo "==="
echo "=== cmake-format ==="
cmake-format --version
if [ "$?" != "0" ] ; then
echo "Error cmake-format can't be executed"
exit 1
fi
echo "==="
echo "=== cmake-lint ==="
cmake-lint --version
if [ "$?" != "0" ] ; then
echo "Error cmake-lint can't be executed"
exit 1
fi
echo "==="
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment