diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000..9810d46665 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,104 @@ +# vim: set filetype=yaml: +--- +HeaderFilterRegex: "" +UseColor: true +FormatStyle: file + +Checks: " \ + -* \ + , bugprone-assert-side-effect \ + , bugprone-bool-pointer-implicit-conversion \ + , bugprone-branch-clone \ + , bugprone-copy-constructor-init \ + , bugprone-dangling-handle \ + , bugprone-dynamic-static-initializers \ + , bugprone-exception-escape \ + , bugprone-forward-declaration-namespace \ + , bugprone-implicit-widening-of-multiplication-result \ + , bugprone-incorrect-roundings \ + , bugprone-infinite-loop \ + , bugprone-integer-division \ + , bugprone-lambda-function-name \ + , bugprone-macro-parentheses \ + , bugprone-macro-repeated-side-effects \ + , bugprone-misplaced-operator-in-strlen-in-alloc \ + , bugprone-misplaced-pointer-arithmetic-in-alloc \ + , bugprone-misplaced-widening-cast \ + , bugprone-move-forwarding-reference \ + , bugprone-multiple-statement-macro \ + , bugprone-not-null-terminated-result \ + , bugprone-parent-virtual-call \ + , bugprone-redundant-branch-condition \ + , bugprone-signed-char-misuse \ + , bugprone-sizeof-container \ + , bugprone-sizeof-expression \ + , bugprone-string-constructor \ + , bugprone-string-integer-assignment \ + , bugprone-string-literal-with-embedded-nul \ + , bugprone-suspicious-enum-usage \ + , bugprone-suspicious-include \ + , bugprone-suspicious-memset-usage \ + , bugprone-suspicious-missing-comma \ + , bugprone-suspicious-semicolon \ + , bugprone-suspicious-string-compare \ + , bugprone-swapped-arguments \ + , bugprone-terminating-continue \ + , bugprone-too-small-loop-variable \ + , bugprone-undefined-memory-manipulation \ + , bugprone-undelegated-constructor \ + , bugprone-unused-raii \ + , bugprone-unused-return-value \ + , bugprone-use-after-move \ + , bugprone-virtual-near-miss \ + , cppcoreguidelines-avoid-goto \ + , cppcoreguidelines-avoid-non-const-global-variables \ + , cppcoreguidelines-init-variables \ + , cppcoreguidelines-interfaces-global-init \ + , cppcoreguidelines-macro-usage \ + , cppcoreguidelines-narrowing-conversions \ + , cppcoreguidelines-no-malloc \ + , cppcoreguidelines-prefer-member-initializer \ + , cppcoreguidelines-pro-bounds-constant-array-index \ + , cppcoreguidelines-pro-type-const-cast \ + , cppcoreguidelines-pro-type-cstyle-cast \ + , cppcoreguidelines-pro-type-member-init \ + , cppcoreguidelines-pro-type-reinterpret-cast \ + , cppcoreguidelines-pro-type-static-cast-downcast \ + , cppcoreguidelines-pro-type-union-access \ + , cppcoreguidelines-slicing \ + , cppcoreguidelines-special-member-functions \ + , google-readability-avoid-underscore-in-googletest-name \ + , google-readability-casting \ + , google-readability-todo \ + , google-upgrade-googletest-case \ + , hicpp-multiway-paths-covered \ + , misc-definitions-in-headers \ + , misc-misplaced-const \ + , misc-non-copyable-objects \ + , misc-non-private-member-variables-in-classes \ + , misc-redundant-expression \ + , misc-static-assert \ + , misc-unconventional-assign-operator \ + , misc-unused-alias-decls \ + , misc-unused-using-decls \ + , modernize-* \ + , readability-* \ + , -readability-convert-member-functions-to-static \ + " + +CheckOptions: + - key: bugprone-misplaced-widening-cast.CheckImplicitCasts + value: true + - key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor + value: true + - key: readability-magic-numbers.IgnorePowersOf2IntegerValues + value: true + - key: readability-function-cognitive-complexity.IgnoreMacros + value: true + - key: readability-magic-numbers.IgnoredIntegerValues + value: '1;2;3;4;10;100;1000;60;3600;9600;115200' + + +WarningsAsErrors: " \ + modernize-* + " diff --git a/.github/workflows/ci-code_analysis.yml b/.github/workflows/ci-code_analysis.yml index c352cb4cdf..5f6065ee18 100644 --- a/.github/workflows/ci-code_analysis.yml +++ b/.github/workflows/ci-code_analysis.yml @@ -11,30 +11,32 @@ on: pull_request: types: [opened, synchronize, reopened] -jobs: - sonarcloud: - name: sonarcloud - runs-on: ubuntu-latest +env: + CC: /usr/bin/gcc + CXX: /usr/bin/g++ - env: - CC: /usr/bin/gcc - CXX: /usr/bin/g++ + CLICOLOR_FORCE: true # ninja - CCACHE_DIR: /home/runner/work/ccache - CCACHE_COMPRESS: true - CCACHE_COMPRESSLEVEL: 6 + CCACHE_DIR: /home/runner/work/ccache + CCACHE_COMPRESS: true + CCACHE_COMPRESSLEVEL: 6 - ARM_TOOLCHAIN_URL: "https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2" - ARM_TOOLCHAIN_FILENAME: "gcc-arm-none-eabi-*-x86_64-linux.tar.bz2" - ARM_TOOLCHAIN_EXTRACT_DIRECTORY: "gcc-arm-none-eabi-*" + ARM_TOOLCHAIN_URL: "https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2" + ARM_TOOLCHAIN_FILENAME: "gcc-arm-none-eabi-*-x86_64-linux.tar.bz2" + ARM_TOOLCHAIN_EXTRACT_DIRECTORY: "gcc-arm-none-eabi-*" - SONARCLOUD_CLI_URL: "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472.zip" - SONARCLOUD_CLI_FILENAME: "sonar-scanner-cli-*.zip" - SONARCLOUD_CLI_EXTRACT_DIRECTORY: "sonar-scanner-*" + SONARCLOUD_CLI_URL: "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472.zip" + SONARCLOUD_CLI_FILENAME: "sonar-scanner-cli-*.zip" + SONARCLOUD_CLI_EXTRACT_DIRECTORY: "sonar-scanner-*" - SONARCLOUD_BUILD_WRAPPER_URL: "https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip" - SONARCLOUD_BUILD_WRAPPER_FILENAME: "build-wrapper-linux-x86.zip" - SONARCLOUD_BUILD_WRAPPER_EXTRACT_DIRECTORY: "build-wrapper-linux-x86" + SONARCLOUD_BUILD_WRAPPER_URL: "https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip" + SONARCLOUD_BUILD_WRAPPER_FILENAME: "build-wrapper-linux-x86.zip" + SONARCLOUD_BUILD_WRAPPER_EXTRACT_DIRECTORY: "build-wrapper-linux-x86" + +jobs: + sonarcloud: + name: sonarcloud + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -170,7 +172,7 @@ jobs: - name: Config, build LekaOS & al. run: | - make config + make config config_tools build-wrapper-linux-x86-64 --out-dir bw-output make - name: Ccache post build @@ -193,9 +195,136 @@ jobs: make ccache_postbuild ccache -z + - name: Upload compilation database + uses: actions/upload-artifact@v2 + with: + name: compile_commands.json + path: ./_build_cmake_tools/compile_commands.json + - name: Run sonar-scanner run: | sonar-scanner -X -Dsonar.login=${{ secrets.SONAR_TOKEN }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + clang_tidy: + name: clang-tidy + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 2 + + # + # Mark: - Install ARM GCC Toolchain + # + + - name: Cache ARM GCC Toolchain + id: cache_arm_toolchain + uses: actions/cache@v2 + with: + path: ~/gcc-arm-none-eabi + key: ${{ runner.os }}-global_cache-arm_toolchain-${{ steps.cache_variables.outputs.arm_toolchain_url_sha }} + + - name: Install ARM GCC Toolchain + if: steps.cache_arm_toolchain.outputs.cache-hit != 'true' + run: | + wget ${{ env.ARM_TOOLCHAIN_URL }} + tar -xjf ${{ env.ARM_TOOLCHAIN_FILENAME }} && rm -rf ${{ env.ARM_TOOLCHAIN_FILENAME }} + mv ${{ env.ARM_TOOLCHAIN_EXTRACT_DIRECTORY }} ~/gcc-arm-none-eabi + + - name: Add ARM GCC Toolchain to path + run: | + echo "~/gcc-arm-none-eabi/bin" >> $GITHUB_PATH + + - name: Test ARM GCC Toolchain + run: | + ls -al ~/gcc-arm-none-eabi/bin + arm-none-eabi-gcc -v + + # + # Mark: - Install clang-tidy + # + + - name: Install clang-tidy 12 + run: | + sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-12 main" + wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add - + sudo apt update -y + sudo apt install -y --no-install-recommends clang-tidy-12 + clang-tidy-12 --version + + # + # Mark: - Install misc. tools + # + + - name: Install tools + run: | + sudo apt-get install -y --no-install-recommends ninja-build + + # + # Mark: - Download mbed-os & ccache + # + + - name: Cache Mbed OS + id: cache_mbed_os + uses: actions/cache@v2 + with: + path: extern/mbed-os + key: ${{ runner.os }}-global_cache-mbed_os-${{ hashFiles('extern/mbed-os/platform/include/platform/mbed_version.h', '.mbed_version') }} + + - name: Curl Mbed OS + if: steps.cache_mbed_os.outputs.cache-hit != 'true' + run: | + make mbed_curl + + # + # Mark: - Install python/pip dependencies + # + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + + - name: Cache pip dependencies + id: cache_pip_dependencies + uses: actions/cache@v2 + with: + path: | + ~/.cache/pip + ${{ env.pythonLocation }} + key: ${{ runner.os }}-global_cache-pip_dependencies-${{ env.pythonLocation }}-${{ hashFiles('extern/mbed-os/requirements.txt', '.mbed_version') }} + + - name: Install pip packages + run: | + pip install --upgrade --upgrade-strategy eager mbed-cli + pip install --upgrade --upgrade-strategy eager -r ./extern/mbed-os/requirements.txt + + - name: Test pip packages + run: | + pip list -v + mbed-cli --help + + # + # Mark: - Analysze + # + + - name: Generate compile_commands.json + run: | + make config_tools + + - name: Get modified files + id: modified_files + uses: Ana06/get-changed-files@v1.2 + with: + format: "space-delimited" + filter: "*" + + - name: Run clang-tidy + run: | + echo ${{ steps.modified_files.outputs.all }} + echo ${{ steps.modified_files.outputs.all }} | xargs -n1 | grep -E '\.h$|\.cpp$' | xargs --no-run-if-empty clang-tidy-12 -p=. --quiet diff --git a/.github/workflows/ci-cross_compilation.yml b/.github/workflows/ci-cross_compilation.yml index bb0a7b11a0..dd49f4bfd2 100644 --- a/.github/workflows/ci-cross_compilation.yml +++ b/.github/workflows/ci-cross_compilation.yml @@ -20,6 +20,8 @@ jobs: runs-on: ubuntu-latest env: + CLICOLOR_FORCE: true # ninja + CCACHE_DIR: /home/runner/work/ccache CCACHE_COMPRESS: true CCACHE_COMPRESSLEVEL: 6 diff --git a/.github/workflows/ci-unit_tests.yml b/.github/workflows/ci-unit_tests.yml index 010a799a9b..cad87ad993 100644 --- a/.github/workflows/ci-unit_tests.yml +++ b/.github/workflows/ci-unit_tests.yml @@ -14,6 +14,13 @@ on: - "feature/**" - "bugfix/**" +env: + CLICOLOR_FORCE: true # ninja + + CCACHE_DIR: /home/runner/work/ccache + CCACHE_COMPRESS: true + CCACHE_COMPRESSLEVEL: 6 + jobs: gcc: name: gcc @@ -23,10 +30,6 @@ jobs: CC: /usr/bin/gcc CXX: /usr/bin/g++ - CCACHE_DIR: /home/runner/work/ccache - CCACHE_COMPRESS: true - CCACHE_COMPRESSLEVEL: 6 - steps: - uses: actions/checkout@v2 with: @@ -108,10 +111,6 @@ jobs: CC: /usr/bin/clang CXX: /usr/bin/clang++ - CCACHE_DIR: /home/runner/work/ccache - CCACHE_COMPRESS: true - CCACHE_COMPRESSLEVEL: 6 - steps: - uses: actions/checkout@v2 with: diff --git a/.vscode/settings.json b/.vscode/settings.json index 46f4031888..e996221ba3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,8 @@ { "files.associations": { - "*.orig": "cpp", ".clang-format": "yaml", + ".clang-tidy": "yaml", + "*.orig": "cpp", "any": "cpp", "array": "cpp", "atomic": "cpp", @@ -96,6 +97,7 @@ "--log=verbose", "--completion-style=detailed", "--clang-tidy", - "--clang-tidy-checks=-*,modernize*" + "--header-insertion=never", + "--suggest-missing-includes" ] } diff --git a/CMakeLists.txt b/CMakeLists.txt index 545e1ae53f..d6e510eac2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ set(CMAKE_C_EXTENSIONS TRUE) # Add custom flags add_compile_options( - -fdiagnostics-color + -fdiagnostics-color=always $<$:-Wno-register> ) @@ -70,6 +70,10 @@ project(LekaOS LANGUAGES C CXX ASM) # Generate compile commands database set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") +if(CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) +endif() + # For convenience you can define useful variables set(OS_DIR ${ROOT_DIR}/src) set(LIBS_DIR ${ROOT_DIR}/libs) diff --git a/Makefile b/Makefile index 0aa7807ec3..281744583d 100644 --- a/Makefile +++ b/Makefile @@ -69,8 +69,6 @@ all: @echo "" @echo "๐Ÿ—๏ธ Building everything! ๐ŸŒˆ" cmake --build $(TARGET_BUILD_DIR) - @rm -rf $(ROOT_DIR)/compile_commands.json - @cp $(TARGET_BUILD_DIR)/compile_commands.json ./ os: @echo "" @@ -126,6 +124,7 @@ config_tools_build: mkdir_tools_config @echo "" @echo "๐Ÿƒ Running cmake configuration script for target $(TARGET_BOARD) ๐Ÿ“" @cmake -S . -B $(CMAKE_TOOLS_BUILD_DIR) -GNinja -DCMAKE_CONFIG_DIR="$(CMAKE_TOOLS_CONFIG_DIR)" -DTARGET_BOARD="$(TARGET_BOARD)" -DCMAKE_BUILD_TYPE=Debug -DENABLE_CODE_ANALYSIS=$(ENABLE_CODE_ANALYSIS) + @ln -sf $(CMAKE_TOOLS_BUILD_DIR)/compile_commands.json ./ # # MARK: - Tests targets @@ -189,7 +188,7 @@ build_unit_tests: run_unit_tests: @echo "" @echo "๐Ÿƒโ€โ™‚๏ธ Running unit tests ๐Ÿงช" - @$(UNIT_TESTS_BUILD_DIR)/LekaOSUnitTestsExec --gtest_output=xml:$(UNIT_TESTS_COVERAGE_DIR)/google_test_detail.xml + @$(UNIT_TESTS_BUILD_DIR)/LekaOSUnitTestsExec --gtest_color=yes --gtest_output=xml:$(UNIT_TESTS_COVERAGE_DIR)/google_test_detail.xml config_unit_tests: mkdir_build_unit_tests @echo "" @@ -218,6 +217,22 @@ clang_format_fix: @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-format & fixing files ๐ŸŽจ" python3 tools/run-clang-format.py -r --extension=h,c,cpp --color=always --style=file . -i +clang_tidy_diff: + @echo "" + @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-tidy on modified files ๐Ÿงน" + @echo "" + @git diff --name-only develop | grep -E "\.h\$$|\.cpp\$$" + @echo "" + @git diff --name-only develop | grep -E "\.h\$$|\.cpp\$$" | xargs /usr/local/opt/llvm/bin/clang-tidy -p=. --quiet + +clang_tidy_diff_fix: + @echo "" + @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-tidy on modified files ๐Ÿงน" + @echo "" + @git diff --name-only develop | grep -E "\.h\$$|\.cpp\$$" + @echo "" + @git diff --name-only develop | grep -E "\.h\$$|\.cpp\$$" | xargs /usr/local/opt/llvm/bin/clang-tidy -p=. --quiet --fix --fix-errors + code_analysis: mkdir_build @echo "" @echo "๐Ÿƒโ€โ™‚๏ธ Running cppcheck code analysis ๐Ÿ”ฌ" @@ -276,6 +291,7 @@ rm_build_all: @echo "โš ๏ธ Cleaning up all build directories ๐Ÿงน" rm -rf $(PROJECT_BUILD_DIR) rm -rf $(CMAKE_TOOLS_BUILD_DIR) + rm -rf ./compile_commands.json rm_config: @echo "" diff --git a/src/main.cpp b/src/main.cpp index 90b3dd87e4..4a6cdc4cb6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,34 +2,35 @@ // Copyright 2020 APF France handicap // SPDX-License-Identifier: Apache-2.0 -#include "mbed.h" +#include "drivers/BufferedSerial.h" +#include "rtos/Kernel.h" +#include "rtos/ThisThread.h" +#include "rtos/Thread.h" #include "HelloWorld.h" +#include "LogKit.h" using namespace leka; +using namespace std::chrono; -HelloWorld hello; - -static BufferedSerial serial(USBTX, USBRX, 9600); - -constexpr uint8_t buff_size = 128; -char buff[buff_size] {}; - -int main(void) +auto main() -> int { - auto start = Kernel::Clock::now(); + static auto serial = mbed::BufferedSerial(USBTX, USBRX); + leka::logger::set_print_function([](const char *str, size_t size) { serial.write(str, size); }); + + rtos::ThisThread::sleep_for(1s); - printf("\nHello, Investigation Day!\n\n"); + log_info("\n\n"); + log_info("Hello, LekaOS!\n"); rtos::ThisThread::sleep_for(2s); + auto hello = HelloWorld(); hello.start(); while (true) { - auto t = Kernel::Clock::now() - start; - int length = sprintf(buff, "A message from your board %s --> \"%s\" at %i s\n", MBED_CONF_APP_TARGET_NAME, - hello.world, int(t.count() / 1000)); - serial.write(buff, length); + log_debug("A message from your board %s --> \"%s\" at %ims", MBED_CONF_APP_TARGET_NAME, hello.world, + int(rtos::Kernel::Clock::now().time_since_epoch().count())); rtos::ThisThread::sleep_for(1s); } } diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index df425eb0d9..c2e1d5b47e 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -36,8 +36,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Colored output for compilation if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + message(STATUS "GCC found! Adding -fdiagnostics-color=always compile option") add_compile_options("-fdiagnostics-color=always") -elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang$") +elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") + message(STATUS "Clang found! Adding -fcolor-diagnostics compile option") add_compile_options("-fcolor-diagnostics") endif()