本文旨在帮助开发使用C++的嵌入式系统的人们捕捉一些bug。作为其中的一部分,我正在尝试让clang-tidy在小型嵌入式目标上工作。
我正在尝试设置CMake以执行以下操作:
- 在构建时运行clang tidy;然后
- 使用GCC 8.2进行编译
然而,clang-tidy失败并显示“未知目标CPU'armv6-m'”
--
我使用的CMake脚本如下:
ClangTidy.cmake
set(ENABLE_CLANG_TIDY ON CACHE BOOL "Add clang-tidy automatically to builds")
if (ENABLE_CLANG_TIDY)
find_program(CLANG_TIDY_EXE NAMES "C:\\Program Files\\LLVM\\bin\\clang-tidy.exe")
if (CLANG_TIDY_EXE)
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
set(CLANG_TIDY_CHECKS "-*,modernize-*")
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-checks=${CLANG_TIDY_CHECKS};-header-filter='${CMAKE_SOURCE_DIR}/*'"
CACHE STRING "" FORCE)
else()
message(AUTHOR_WARNING "clang-tidy not found!")
set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE) # delete it
endif()
endif()
armv6-m是CMake脚本的一部分,但仅用于GCC,以下是用于设置GCC标志的内容:
set(TARGET STM32F070x6)
set(ARCH armv6-m)
set(CORE cortex-m0)
set(CMAKE_CXX_FLAGS "-march=${ARCH} -mcpu=${CORE} -D${TARGET}")
接下来进行以下构建:
#
# Including extra cmake rules
include(cmake/ClangTidy.cmake)
#Compile and link the exe :)
add_executable(${TARGET}.elf ${MAIN_SOURCE})
#Print the size of the .hex
add_custom_target(size ALL arm-none-eabi-size ${TARGET}.elf DEPENDS ${TARGET}.elf)
add_custom_target(${TARGET}.bin ALL DEPENDS ${TARGET}.elf COMMAND ${CMAKE_OBJCOPY} -Obinary ${TARGET}.elf ${TARGET}.bin)
cmake构建后的生成构建消息为:
[build] "C:\Program Files\CMake\bin\cmake.exe" -E __run_co_compile
--tidy="C:/Program Files/LLVM/bin/clang-tidy.exe;-checks=-*,modernize-*;-header-filter='G:/Files/Git/projectname/*'" --source=../src/main.cpp
-- C:\PROGRA~2\GNUTOO~1\82018-~1\bin\AR10B2~1.EXE -DHSE_VALUE=48000000 -DSTM32F070x6 -DTRACE -DUSE_STDPERIPH_DRIVER -I../include -I../system -I../library -I../library/usb -I../library/usb/HID -I../library/usb/LL -I../library/usb/Standard -I../library/usb/Types -I../library/common -I../library/common/SCPI -I../library/common/containers -I../library/peripherals -I../library/peripherals/Bitbanging -I../st-library/include -I../st-library/include/arm -I../st-library/include/cmsis -I../st-library/include/cortexm -I../st-library/include/diag -I../st-library/include/stm32f0-stdperiph -march=armv6-m -mcpu=cortex-m0 -DSTM32F070x6 -Os -mthumb -g2 -ggdb -pedantic -Wall -Wextra -Wfloat-equal -Wshadow -Wall -Wl,--gc-sections -fmessage-length=0 -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -std=c++1z -fno-rtti -fno-exceptions -fno-use-cxa-atexit -fno-threadsafe-statics -ftemplate-backtrace-limit=0 -O2 -g -DNDEBUG -MD -MT CMakeFiles/STM32F070x6.elf.dir/src/main.cpp.obj -MF CMakeFiles\STM32F070x6.elf.dir\src\main.cpp.obj.d -o CMakeFiles/STM32F070x6.elf.dir/src/main.cpp.obj -c ../src/main.cpp
在上述错误发生后立即进行以下操作:
[build] error: unknown target CPU 'armv6-m' [clang-diagnostic-error]
[build] note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cannonlake, icelake-client, icelake-server, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, x86-64
目标(armv6-m)字段似乎与GCC使用的相同。但我不确定为什么会带有target字段来调用clang-tidy。