diff --git a/.github/workflows/ci-code_analysis-clang_tidy.yml b/.github/workflows/ci-code_analysis-clang_tidy.yml index 2ddde77868..8fc34bce48 100644 --- a/.github/workflows/ci-code_analysis-clang_tidy.yml +++ b/.github/workflows/ci-code_analysis-clang_tidy.yml @@ -73,4 +73,4 @@ jobs: | grep -E -v "mocks" \ | grep -E -v "stubs" \ | grep -E "\.h$|\.cpp$" \ - | xargs --no-run-if-empty clang-tidy-13 -p=_build_cmake_tools --quiet + | xargs --no-run-if-empty ruby tools/run-clang-tidy.rb _build_cmake_tools diff --git a/CMakeLists.txt b/CMakeLists.txt index 64072c5005..0a0a118f41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,9 +16,6 @@ endif(CCACHE) # 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() # # MARK: - Compilation flags/options diff --git a/Makefile b/Makefile index 69aa024034..30695a406b 100644 --- a/Makefile +++ b/Makefile @@ -232,22 +232,11 @@ clang_tidy_diff: @echo "๐Ÿƒโ€โ™‚๏ธ Running clang-tidy on modified files ๐Ÿงน" @echo "" @git diff --name-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" + | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" || echo "No files added nor modified!" @echo "" @git diff --name-status develop \ | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | 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-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" - @echo "" - @git diff --name-status develop \ - | grep -E -v "_test" | grep -E "^A|^M" | sed "s/^[AM]\t//g" | grep -E "\.h\$$|\.cpp\$$" \ - | xargs /usr/local/opt/llvm/bin/clang-tidy -p=. --quiet --fix --fix-errors + | xargs --no-run-if-empty ruby tools/run-clang-tidy.rb $(CMAKE_TOOLS_BUILD_DIR) # # MARK: - Mbed targets diff --git a/cmake/ToolchainGCCArmBuild.cmake b/cmake/ToolchainGCCArmBuild.cmake index e94684fd36..3bc9ef4d1c 100644 --- a/cmake/ToolchainGCCArmBuild.cmake +++ b/cmake/ToolchainGCCArmBuild.cmake @@ -66,3 +66,10 @@ target_link_libraries(mbed-os INTERFACE -T\"${PREPROCESSED_LINKER_SCRIPT}\" ${MBED_LINK_OPTIONS} ) + +# Export cross-compilation include for clang-tidy +if(CMAKE_EXPORT_COMPILE_COMMANDS) + set(CMAKE_ASM_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}) + set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) + set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) +endif() diff --git a/tools/run-clang-tidy.rb b/tools/run-clang-tidy.rb new file mode 100644 index 0000000000..0494c6878b --- /dev/null +++ b/tools/run-clang-tidy.rb @@ -0,0 +1,97 @@ +#!/usr/bin/env ruby + +# Leka - LekaOS +# Copyright 2022 APF France handicap +# SPDX-License-Identifier: Apache-2.0 + +SIS="#include <...> search starts here:" +SIE="End of search list." + +$DEBUG = false + +def log_debug(message) + if $DEBUG + puts message + end +end + +$tmp_search_dirs = [] + +# Previous trials, for posterity +# search_dirs_cmds = ["arm-none-eabi-g++ -E -Wp,-v -xc++ /dev/null 2>&1", "arm-none-eabi-gcc -E -Wp,-v -xc /dev/null 2>&1"] +search_dirs_cmds = ["arm-none-eabi-g++ -E -Wp,-v -xc++ /dev/null 2>&1"] + +search_dirs_cmds.each do |cmd| + log_debug "Find search dirs for #{cmd}" + raw_search_dirs = `#{cmd}` + search_dirs = raw_search_dirs.match(/#{SIS}(.*?)#{SIE}/m)[1] + + log_debug "Remove starting empty space" + search_dirs = search_dirs.gsub(/^\s*/, '') + + log_debug "Appending -extra-arg" + search_dirs = search_dirs.gsub!(/^/, '-extra-arg=-isystem\&') + + $tmp_search_dirs += search_dirs.split("\n") +end + +# At the moment this is not needed but might in the future, so we keep it here +# +# print_search_dirs_cmds = ["arm-none-eabi-gcc -print-search-dirs", "arm-none-eabi-g++ -print-search-dirs"] +# print_search_dirs_cmds.each do |cmd| +# log_debug "Find search dirs for #{cmd}" +# raw_search_dirs = `#{cmd}` + +# log_debug "Remove prefixes" +# search_dirs = raw_search_dirs.gsub('install: ', '').gsub('programs: =', '').gsub('libraries: =', '') + +# log_debug "Split lines" +# search_dirs = search_dirs.gsub('/:/', "/\n/") + +# log_debug "Appending -extra-arg" +# search_dirs = search_dirs.gsub!(/^/, '-extra-arg=-isystem\&') + +# $tmp_search_dirs += search_dirs.split("\n") +# end + +log_debug "Remove duplicates" +$tmp_search_dirs = $tmp_search_dirs.uniq + +log_debug "Find clang-tidy" +$CLANG_TIDY_EXEC = "" + +if system("which clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "clang-tidy" +elsif system("which clang-tidy-13 > /dev/null") + $CLANG_TIDY_EXEC = "clang-tidy-13" +elsif system("which /opt/homebrew/opt/llvm/bin/clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "/opt/homebrew/opt/llvm/bin/clang-tidy" +elsif system("which /usr/local/opt/llvm/bin/clang-tidy > /dev/null") + $CLANG_TIDY_EXEC = "/usr/local/opt/llvm/bin/clang-tidy" +end + +puts "Clang-tidy found at: #{$CLANG_TIDY_EXEC}" + +puts "Standard headers search dirs:" +puts $tmp_search_dirs.map { |d| " #{d}" }.join("\n") +puts "" + +$BUILD_DIR = ARGV.shift +$SEARCH_DIRS = $tmp_search_dirs.join("\s") +$FILES = ARGV.join(" ") +$FILES_ARR = ARGV + +puts "Running clang-tidy on:" +puts $FILES_ARR.map { |f| " #{f}" }.join("\n") + +RUN_CLANG_TIDY_CMD = <