ClangTidy.cmake 1.7 KB

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