ClangTidy.cmake 1.8 KB

12345678910111213141516171819202122232425262728293031323334
  1. get_filename_component(CXX_COMPILER_DIR ${CMAKE_CXX_COMPILER} DIRECTORY CACHE)
  2. find_program(CLANG_TIDY clang-tidy NAMES clang-tidy clang-tidy-3.9 clang-tidy-3.8 clang-tidy-3.7 HINTS ${CXX_COMPILER_DIR})
  3. find_program(CLANG_REPLACE clang-apply-replacements NAMES clang-apply-replacements clang-apply-replacements-3.9 clang-apply-replacements-3.8 clang-apply-replacements-3.7 HINTS ${CXX_COMPILER_DIR})
  4. if(CMAKE_EXPORT_COMPILE_COMMANDS AND NOT CLANG_TIDY STREQUAL CLANG_TIDY-NOTFOUND)
  5. macro(clang_tidy target)
  6. get_target_property(TARGET_SOURCES ${target} SOURCES)
  7. get_target_property(TARGET_SOURCE_DIR ${target} SOURCE_DIR)
  8. get_target_property(TARGET_BINARY_DIR ${target} BINARY_DIR)
  9. foreach(s ${TARGET_SOURCES})
  10. get_filename_component(FILE_PATH ${s} ABSOLUTE BASE_DIR ${TARGET_SOURCE_DIR})
  11. get_filename_component(FILE_NAME ${FILE_PATH} NAME)
  12. get_source_file_property(FILE_LANG ${FILE_PATH} LANGUAGE)
  13. get_source_file_property(FILE_GENERATED ${FILE_PATH} GENERATED)
  14. if("${FILE_LANG}" STREQUAL "CXX" OR "${FILE_LANG}" STREQUAL "C" AND NOT FILE_GENERATED)
  15. add_custom_command(OUTPUT ${TARGET_BINARY_DIR}/_tidy/${FILE_NAME}
  16. COMMAND ${CLANG_TIDY} -p "${CMAKE_BINARY_DIR}" ${FILE_PATH}
  17. COMMENT "clang-tidy ${FILE_NAME} ..."
  18. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  19. )
  20. list(APPEND TIDY_TARGETS ${TARGET_BINARY_DIR}/_tidy/${FILE_NAME})
  21. endif()
  22. endforeach()
  23. add_custom_target(${target}_tidy DEPENDS ${TIDY_TARGETS})
  24. endmacro()
  25. else()
  26. macro(clang_tidy target)
  27. message(STATUS "clang-tidy not enabled, pass -DCMAKE_EXPORT_COMPILE_COMMANDS=on to cmake to enable it")
  28. endmacro()
  29. endif()
  30. add_custom_target(tidy_all DEPENDS ${MAIN_TARGET}_tidy ${HELPER_TARGET}_tidy shared_tidy)