add_custom_target似乎没有在cmake中正确扩展变量

时间:2016-12-22 20:11:22

标签: cmake

我试图整合clang-tidy并试图动态建立包含目录。我的cmake看起来像:

if(UNIX) 
  file(GLOB_RECURSE ALL_CXX_SOURCE_FILES *.cpp *.hpp) 
  file(GLOB_RECURSE ALL_HEADERS *.hpp)   
  set(ALL_INCLUDE_DIRECTORIES "") 
  foreach (_headerFile ${ALL_HEADERS}) 
    get_filename_component(_dir ${_headerFile} PATH)
    if(ALL_INCLUDE_DIRECTORIES STREQUAL "") 
      set(ALL_INCLUDE_DIRECTORIES "-I${_dir}")
    else()
      set(ALL_INCLUDE_DIRECTORIES "-I${_dir} ${ALL_INCLUDE_DIRECTORIES}")
    endif() 
  endforeach()
  message(${ALL_INCLUDE_DIRECTORIES}) 
  add_custom_target(clang-tidy COMMAND /usr/bin/clang-tidy ${ALL_CXX_SOURCE_FILES} -checks=* -- -std=c++11 ${ALL_INCLUDE_DIRECTORIES}) 
endif()

$ {ALL_INCLUDE_DIRECTORIES}似乎无法在add_custom_target中正确展开,因为' make clang-tidy'报告错误,因为未在包含路径中找到标头。但是,如果我使用消息($ {ALL_INCLUDE_DIRECTORIES})中的输出替换add_custom_target中的$ {ALL_INCLUDE_DIRECTORIES},我不会收到这些错误,并且clang-tidy似乎找到了标题。

1 个答案:

答案 0 :(得分:0)

这很有效。使用清单:

if(UNIX) 
  file(GLOB_RECURSE ALL_CXX_SOURCE_FILES *.cpp *.hpp) 
  file(GLOB_RECURSE ALL_HEADERS *.hpp)   
  set(ALL_INCLUDE_DIRECTORIES "") 
  foreach (_headerFile ${ALL_HEADERS}) 
    get_filename_component(_dir ${_headerFile} PATH)
    list(APPEND ALL_INCLUDE_DIRECTORIES -I${_dir}) 
  endforeach() 
  add_custom_target(clang-tidy COMMAND /usr/bin/clang-tidy ${ALL_CXX_SOURCE_FILES} -checks=* -- -std=c++11 ${ALL_INCLUDE_DIRECTORIES}) 
endif()