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/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 000000000..13b51583d --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,4 @@ +# This file contains all commits which are purely cosmetics + +# Cleaning up Eclipse headers +6a458fbc7a0e96a9fdd7d2b429c6924c5de7b35a diff --git a/.github/workflows/compliance.yaml b/.github/workflows/compliance.yaml index 739e43f5f..006dce845 100644 --- a/.github/workflows/compliance.yaml +++ b/.github/workflows/compliance.yaml @@ -17,6 +17,25 @@ jobs: run: | pip3 install gitlint - - name: Check commits with checkpatch + - 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/README.md b/README.md index 8bf5a849c..c6bf17d89 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,39 @@ Several compilation switches are used: Depending on your platform, you need to define LWM2M_BIG_ENDIAN or LWM2M_LITTLE_ENDIAN. LWM2M_CLIENT_MODE and LWM2M_SERVER_MODE can be defined at the same time. +## Development + +### Dependencies and Tools +- Mandatory: + - Compiler: GCC and/or Clang +- Optional (but strongly recommended): + - Build system generator: CMake 3.13+ + - Version control system: Git (and a GitHub account) + - Git commit message linter: gitlint + - Build system: ninja + - C code formatting: clang-format, version 10 + - Unit testing: CUnit + +On Ubuntu 20.04, used in CI, the dependencies can be installed as such: +- `apt install build-essential clang-format clang-format-10 clang-tools-10 cmake gcovr git libcunit1-dev ninja-build python3-pip` +- `pip3 install gitlint` + +### Code formatting +New 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. + +To check if your code matches the expected style, the following commands are helpful: + - `git clang-format-10 --diff`: Show what needs to be changed to match the expected code style + - `git clang-format-10`: Apply all needed changes directly + - `git clang-format-10 --commit master`: Fix code style for all changes since master + +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. + +### Running CI tests locally +To avoid unneeded load on the GitHub infrastructure, please consider running `tools/ci/run_ci.sh --all` before pushing. ## Examples 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/core/discover.c b/core/discover.c index 2d469454f..143a3d1df 100644 --- a/core/discover.c +++ b/core/discover.c @@ -1,21 +1,20 @@ /******************************************************************************* -* -* Copyright (c) 2015 Intel Corporation and others. -* All rights reserved. This program and the accompanying materials -* are made available under the terms of the Eclipse Public License v2.0 -* and Eclipse Distribution License v1.0 which accompany this distribution. -* -* The Eclipse Public License is available at -* http://www.eclipse.org/legal/epl-v20.html -* The Eclipse Distribution License is available at -* http://www.eclipse.org/org/documents/edl-v10.php. -* -* Contributors: -* David Navarro, Intel Corporation - initial API and implementation -* Scott Bertin, AMETEK, Inc. - Please refer to git log -* -*******************************************************************************/ - + * + * Copyright (c) 2015 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * David Navarro, Intel Corporation - initial API and implementation + * Scott Bertin, AMETEK, Inc. - Please refer to git log + * + *******************************************************************************/ #include "internals.h" diff --git a/core/internals.h b/core/internals.h index a99e26d3f..157b0c3e0 100644 --- a/core/internals.h +++ b/core/internals.h @@ -18,7 +18,7 @@ * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log * Tuve Nordius, Husqvarna Group - Please refer to git log - * + * *******************************************************************************/ /* Copyright (c) 2013, 2014 Intel Corporation diff --git a/core/liblwm2m.c b/core/liblwm2m.c index 5e223bc1f..8a2d69ec1 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -17,7 +17,7 @@ * Toby Jaffey - Please refer to git log * Pascal Rieux - Please refer to git log * Tuve Nordius, Husqvarna Group - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/core/list.c b/core/list.c index 5f5160699..324329bc5 100644 --- a/core/list.c +++ b/core/list.c @@ -12,7 +12,7 @@ * * Contributors: * David Navarro, Intel Corporation - initial API and implementation - * + * *******************************************************************************/ #include "internals.h" diff --git a/core/management.c b/core/management.c index c98ffd59f..faeaf598a 100644 --- a/core/management.c +++ b/core/management.c @@ -17,7 +17,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Tuve Nordius, Husqvarna Group - Please refer to git log - * + * *******************************************************************************/ /* Copyright (c) 2013, 2014 Intel Corporation diff --git a/core/uri.c b/core/uri.c index 730b709b8..7e28def2b 100644 --- a/core/uri.c +++ b/core/uri.c @@ -16,7 +16,7 @@ * Toby Jaffey - Please refer to git log * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/core/utils.c b/core/utils.c index 81293e4cb..6b1399cf7 100644 --- a/core/utils.c +++ b/core/utils.c @@ -15,7 +15,7 @@ * Toby Jaffey - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log * Tuve Nordius, Husqvarna Group - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/data/data.c b/data/data.c index cc65bfddb..814794345 100644 --- a/data/data.c +++ b/data/data.c @@ -1,22 +1,22 @@ /******************************************************************************* -* -* Copyright (c) 2013, 2014 Intel Corporation and others. -* All rights reserved. This program and the accompanying materials -* are made available under the terms of the Eclipse Public License v2.0 -* and Eclipse Distribution License v1.0 which accompany this distribution. -* -* The Eclipse Public License is available at -* http://www.eclipse.org/legal/epl-v20.html -* The Eclipse Distribution License is available at -* http://www.eclipse.org/org/documents/edl-v10.php. -* -* Contributors: -* David Navarro, Intel Corporation - initial API and implementation -* Fabien Fleutot - Please refer to git log -* Bosch Software Innovations GmbH - Please refer to git log -* Scott Bertin, AMETEK, Inc. - Please refer to git log -* -*******************************************************************************/ + * + * Copyright (c) 2013, 2014 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * David Navarro, Intel Corporation - initial API and implementation + * Fabien Fleutot - Please refer to git log + * Bosch Software Innovations GmbH - Please refer to git log + * Scott Bertin, AMETEK, Inc. - Please refer to git log + * + *******************************************************************************/ #include "internals.h" #include diff --git a/data/json.c b/data/json.c index 750187d88..1d407bc81 100644 --- a/data/json.c +++ b/data/json.c @@ -16,7 +16,6 @@ * *******************************************************************************/ - #include "internals.h" #include #include diff --git a/data/json_common.c b/data/json_common.c index 4ad6d8ae4..a77f56705 100644 --- a/data/json_common.c +++ b/data/json_common.c @@ -16,7 +16,6 @@ * *******************************************************************************/ - #include "internals.h" #include diff --git a/data/senml_json.c b/data/senml_json.c index 66bceed53..a6b40ba59 100644 --- a/data/senml_json.c +++ b/data/senml_json.c @@ -16,7 +16,6 @@ * *******************************************************************************/ - #include "internals.h" #include #include diff --git a/data/tlv.c b/data/tlv.c index 708637922..7efeb3f57 100644 --- a/data/tlv.c +++ b/data/tlv.c @@ -15,7 +15,7 @@ * Fabien Fleutot - Please refer to git log * Bosch Software Innovations GmbH - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ #include "internals.h" diff --git a/examples/bootstrap_server/bootstrap_server.c b/examples/bootstrap_server/bootstrap_server.c index 1053df35f..2eb41f940 100644 --- a/examples/bootstrap_server/bootstrap_server.c +++ b/examples/bootstrap_server/bootstrap_server.c @@ -17,7 +17,6 @@ * *******************************************************************************/ - #include "liblwm2m.h" #include diff --git a/examples/client/object_access_control.c b/examples/client/object_access_control.c index b92e3048d..90546d9f9 100644 --- a/examples/client/object_access_control.c +++ b/examples/client/object_access_control.c @@ -14,7 +14,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * ******************************************************************************/ /* diff --git a/examples/client/object_connectivity_moni.c b/examples/client/object_connectivity_moni.c index 6a894aa37..403b4e2b3 100644 --- a/examples/client/object_connectivity_moni.c +++ b/examples/client/object_connectivity_moni.c @@ -1,6 +1,6 @@ /******************************************************************************* * - * Copyright (c) 2014 Bosch Software Innovations GmbH Germany. + * Copyright (c) 2014 Bosch Software Innovations GmbH Germany. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * and Eclipse Distribution License v1.0 which accompany this distribution. @@ -14,7 +14,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_connectivity_stat.c b/examples/client/object_connectivity_stat.c index 989a58d6d..ccd3234b0 100644 --- a/examples/client/object_connectivity_stat.c +++ b/examples/client/object_connectivity_stat.c @@ -13,7 +13,7 @@ * Contributors: * Bosch Software Innovations GmbH - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_device.c b/examples/client/object_device.c index 5ee967aba..9ccbe083d 100644 --- a/examples/client/object_device.c +++ b/examples/client/object_device.c @@ -18,7 +18,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_firmware.c b/examples/client/object_firmware.c index 30e9efa3e..35f0647ef 100644 --- a/examples/client/object_firmware.c +++ b/examples/client/object_firmware.c @@ -18,7 +18,7 @@ * Pascal Rieux - Please refer to git log * Gregory Lemercier - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_location.c b/examples/client/object_location.c index 4d32e2cec..13dc5b894 100644 --- a/examples/client/object_location.c +++ b/examples/client/object_location.c @@ -1,5 +1,5 @@ /******************************************************************************* - * + * * Copyright (c) 2014 Bosch Software Innovations GmbH, Germany. * * All rights reserved. This program and the accompanying materials @@ -15,7 +15,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * ******************************************************************************/ /*! \file LWM2M object "Location" implementation diff --git a/examples/client/object_security.c b/examples/client/object_security.c index 453546686..54d7d3266 100644 --- a/examples/client/object_security.c +++ b/examples/client/object_security.c @@ -16,7 +16,7 @@ * Pascal Rieux - Please refer to git log * Ville Skyttä - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_server.c b/examples/client/object_server.c index 6d48c6aee..a1e9098c6 100644 --- a/examples/client/object_server.c +++ b/examples/client/object_server.c @@ -16,7 +16,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/client/object_test.c b/examples/client/object_test.c index 5ae52d510..3563b211c 100644 --- a/examples/client/object_test.c +++ b/examples/client/object_test.c @@ -20,7 +20,7 @@ * Ville Skyttä - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log * Tuve Nordius, Husqvarna Group - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/lightclient/object_device.c b/examples/lightclient/object_device.c index 9f73448c4..0f69228a9 100644 --- a/examples/lightclient/object_device.c +++ b/examples/lightclient/object_device.c @@ -18,7 +18,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/lightclient/object_security.c b/examples/lightclient/object_security.c index d736bf681..5ed39876e 100644 --- a/examples/lightclient/object_security.c +++ b/examples/lightclient/object_security.c @@ -15,7 +15,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/lightclient/object_server.c b/examples/lightclient/object_server.c index 1e4bc5fb0..0a6c1624e 100644 --- a/examples/lightclient/object_server.c +++ b/examples/lightclient/object_server.c @@ -16,7 +16,7 @@ * Bosch Software Innovations GmbH - Please refer to git log * Pascal Rieux - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/lightclient/object_test.c b/examples/lightclient/object_test.c index 3f5c51335..1a003a6ae 100644 --- a/examples/lightclient/object_test.c +++ b/examples/lightclient/object_test.c @@ -19,7 +19,7 @@ * Pascal Rieux - Please refer to git log * Ville Skyttä - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ /* diff --git a/examples/shared/commandline.c b/examples/shared/commandline.c index f321eaa30..613425bb3 100644 --- a/examples/shared/commandline.c +++ b/examples/shared/commandline.c @@ -14,7 +14,7 @@ * David Navarro, Intel Corporation - initial API and implementation * Fabien Fleutot - Please refer to git log * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ #include diff --git a/examples/shared/commandline.h b/examples/shared/commandline.h index a78b7104c..fdb5a51f1 100644 --- a/examples/shared/commandline.h +++ b/examples/shared/commandline.h @@ -12,7 +12,7 @@ * * Contributors: * David Navarro, Intel Corporation - initial API and implementation - * + * *******************************************************************************/ #include diff --git a/examples/shared/connection.c b/examples/shared/connection.c index 5dd911f5f..39b305458 100644 --- a/examples/shared/connection.c +++ b/examples/shared/connection.c @@ -13,7 +13,7 @@ * Contributors: * David Navarro, Intel Corporation - initial API and implementation * Pascal Rieux - Please refer to git log - * + * *******************************************************************************/ #include diff --git a/examples/shared/connection.h b/examples/shared/connection.h index faf00eec0..6e8d02f75 100644 --- a/examples/shared/connection.h +++ b/examples/shared/connection.h @@ -12,7 +12,7 @@ * * Contributors: * David Navarro, Intel Corporation - initial API and implementation - * + * *******************************************************************************/ #ifndef CONNECTION_H_ diff --git a/tests/convert_numbers_test.c b/tests/convert_numbers_test.c index ae5996a5d..f54d1a8b7 100644 --- a/tests/convert_numbers_test.c +++ b/tests/convert_numbers_test.c @@ -14,7 +14,7 @@ * David Navarro, Intel Corporation - initial API and implementation * David Graeff - Make this a test suite * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ #include "internals.h" diff --git a/tests/senml_json_tests.c b/tests/senml_json_tests.c index 35ba76e86..a584b702b 100644 --- a/tests/senml_json_tests.c +++ b/tests/senml_json_tests.c @@ -14,7 +14,7 @@ * David Navarro, Intel Corporation - initial API and implementation * David Gräff - Convert to test case * Scott Bertin, AMETEK, Inc. - Please refer to git log - * + * *******************************************************************************/ #include "liblwm2m.h" diff --git a/tests/tlv_json_lwm2m_data_test.c b/tests/tlv_json_lwm2m_data_test.c index 6fc16a8c2..e89c9e793 100644 --- a/tests/tlv_json_lwm2m_data_test.c +++ b/tests/tlv_json_lwm2m_data_test.c @@ -13,7 +13,7 @@ * Contributors: * David Navarro, Intel Corporation - initial API and implementation * David Gräff - Convert to test case - * + * *******************************************************************************/ #include "liblwm2m.h" diff --git a/tests/unittests.c b/tests/unittests.c index df5a8dd78..0df4cd371 100644 --- a/tests/unittests.c +++ b/tests/unittests.c @@ -15,7 +15,6 @@ * *******************************************************************************/ - #include #include diff --git a/tools/ci/run_ci.sh b/tools/ci/run_ci.sh index 4825a2ea1..cb23fb8fa 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,8 +32,10 @@ OPT_TEST_COVERAGE_REPORT="" OPT_VERBOSE=0 OPT_WRAPPER_CMD="" RUN_BUILD=0 +RUN_CLANG_FORMAT=0 RUN_CLEAN=0 RUN_GITLINT=0 +RUN_GIT_BLAME_IGNORE=0 RUN_TESTS=0 HELP_MSG="usage: ${SCRIPT_NAME} ... @@ -49,6 +52,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 +69,8 @@ Options: Available steps (executed by --all): --run-gitlint Check git commits with gitlint + --run-clang-format Check code formatting with clang-format + --run-git-blame-ignore Validate .git-blame-ignore-revs --run-clean Remove all build artifacts --run-build Build all targets --run-tests Build and execute tests @@ -76,6 +83,27 @@ 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}" 2>&1 | + { grep -v \ + -e 'no modified files to format' \ + -e 'clang-format did not modify any files' || 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 } @@ -86,6 +114,15 @@ function run_gitlint() { gitlint --commits "${commits}" } +function run_git_blame_ignore() { + for commit in $(grep -E '^[A-Za-z0-9]{40}$' .git-blame-ignore-revs); do + if ! git merge-base --is-ancestor "${commit}" HEAD; then + echo ".git-blame-ignore-revs: Commit ${commit} is not an ancestor." + exit + fi + done +} + function run_build() { # Existing directory needed by SonarQube build-wrapper mkdir -p build-wakaama @@ -153,10 +190,13 @@ 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-git-blame-ignore \ -l run-tests \ -l sanitizer: \ -l scan-build: \ @@ -188,6 +228,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 @@ -200,6 +248,10 @@ while true; do RUN_GITLINT=1 shift ;; + --run-git-blame-ignore) + RUN_GIT_BLAME_IGNORE=1 + shift + ;; --run-tests) RUN_TESTS=1 shift @@ -233,8 +285,10 @@ while true; do shift ;; -a|--all) + RUN_CLANG_FORMAT=1 RUN_CLEAN=1 RUN_GITLINT=1 + RUN_GIT_BLAME_IGNORE=1 RUN_BUILD=1 RUN_TESTS=1 shift @@ -292,10 +346,18 @@ fi # Run Steps -if [[ $RUN_GITLINT == 1 ]]; then +if [ "${RUN_GITLINT}" -eq 1 ]; then run_gitlint fi +if [ "${RUN_CLANG_FORMAT}" -eq 1 ]; then + run_clang_format +fi + +if [ "${RUN_GIT_BLAME_IGNORE}" -eq 1 ]; then + run_git_blame_ignore +fi + if [ "${RUN_CLEAN}" -eq 1 ]; then run_clean fi