首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CMake和嵌入式目标的clang

使用CMake和嵌入式目标的clang
EN

Stack Overflow用户
提问于 2019-09-09 09:52:24
回答 2查看 2.5K关注 0票数 5

这里的目标是帮助人们在用C++开发时在他们的嵌入式系统上发现一些bug。作为这部分工作的一部分,我正在努力让自己变得整洁起来,为小型嵌入式目标工作。

我正在尝试设置CMake来执行以下操作:

  1. 在构建时运行clang整洁;然后
  2. 与GCC 8.2合编

然而,clang在“未知目标CPU的armv6 6-m”中失败。

--

我使用的CMake脚本如下所示:

ClangTidy.cmake

代码语言:javascript
复制
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 6-m是CMake脚本的一部分,但它只用于GCC,下面的代码用于安装GCC旗子:

代码语言:javascript
复制
set(TARGET STM32F070x6)
set(ARCH armv6-m)
set(CORE cortex-m0)
set(CMAKE_CXX_FLAGS "-march=${ARCH} -mcpu=${CORE} -D${TARGET}")

然后进行以下构建:

代码语言:javascript
复制
#
# 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的生成消息是:

代码语言:javascript
复制
[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

在发生上述错误后,立即发生以下错误:

代码语言:javascript
复制
[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 6-m)场似乎与GCC使用的目标场相同。但我不知道为什么会用目标字段调用clang。

EN

回答 2

Stack Overflow用户

发布于 2019-09-09 19:18:55

在源代码中我们可以看到:

代码语言:javascript
复制
static int HandleTidy(const std::string& runCmd, const std::string& sourceFile,
                      const std::vector<std::string>& orig_cmd)
{
  // Construct the clang-tidy command line by taking what was given
  // and adding our compiler command line.  The clang-tidy tool will
  // automatically skip over the compiler itself and extract the
  // options.
  int ret;
  std::vector<std::string> tidy_cmd = cmExpandedList(runCmd, true);
  tidy_cmd.push_back(sourceFile);
  tidy_cmd.emplace_back("--");
  cmAppend(tidy_cmd, orig_cmd);

src:https://github.com/Kitware/CMake/blob/09032f09f8d2b4f7af658060ef434083f9d6a0d4/Source/cmcmd.cxx#L196-L207

所以我想说的是,所有的编译器选项,即CMAKE_CXX_FLAGS,在默认情况下也被传递给clang.

票数 2
EN

Stack Overflow用户

发布于 2022-01-20 13:19:50

作为一种解决方法,您可以编写一个小脚本来过滤接收到的参数,然后使用过滤后的参数列表调用clang-tidy。然后,可以将此脚本传递给CMAKE_CXX_CLANG_TIDY,而不是实际的可执行文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57851621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档