0

I'm trying to integrate clang-tidy and am attempting to dynamically establish the include directories. My cmake looks like:

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()

The ${ALL_INCLUDE_DIRECTORIES} does not appear to expand correctly in the add_custom_target because the 'make clang-tidy' reports errors for not finding the headers at the include paths. However, if I use the output from message(${ALL_INCLUDE_DIRECTORIES}) to replace the ${ALL_INCLUDE_DIRECTORIES} in the add_custom_target I do not get these errors and clang-tidy appears to find the headers.

Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26
  • Works if you use a list as detailed here: http://stackoverflow.com/questions/33696412/cmake-add-custom-command-argument-based-on-variable-content – Jibbity jobby Dec 22 '16 at 20:31

1 Answers1

0

This works. Use a list:

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()
Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26