ClangFormat.cmake 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # additional target to perform clang-format run, requires clang-format
  2. # get all project files
  3. file(GLOB_RECURSE ALL_SOURCE_FILES
  4. RELATIVE ${CMAKE_CURRENT_BINARY_DIR}
  5. ${CMAKE_SOURCE_DIR}/src/*.cpp ${CMAKE_SOURCE_DIR}/src/*.h ${CMAKE_SOURCE_DIR}/src/*.hpp
  6. ${CMAKE_SOURCE_DIR}/tests/*.cpp ${CMAKE_SOURCE_DIR}/tests/*.h ${CMAKE_SOURCE_DIR}/tests/*.hpp
  7. ${CMAKE_SOURCE_DIR}/perf/*.cpp ${CMAKE_SOURCE_DIR}/perf/*.h ${CMAKE_SOURCE_DIR}/perf/*.hpp
  8. ${CMAKE_SOURCE_DIR}/tools/*.cpp ${CMAKE_SOURCE_DIR}/tools/*.h ${CMAKE_SOURCE_DIR}/tools/*.hpp
  9. ${CMAKE_SOURCE_DIR}/include/*.h
  10. )
  11. if("${CLANG_FORMAT}" STREQUAL "")
  12. set(CLANG_FORMAT "clang-format")
  13. endif()
  14. add_custom_target(
  15. clang-format
  16. COMMAND ${CLANG_FORMAT} -style=file -i ${ALL_SOURCE_FILES}
  17. )
  18. function(JOIN VALUES GLUE OUTPUT)
  19. string (REPLACE ";" "${GLUE}" _TMP_STR "${VALUES}")
  20. set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
  21. endfunction()
  22. configure_file(builds/cmake/clang-format-check.sh.in clang-format-check.sh @ONLY)
  23. add_custom_target(
  24. clang-format-check
  25. COMMAND chmod +x clang-format-check.sh
  26. COMMAND ./clang-format-check.sh
  27. COMMENT "Checking correct formatting according to .clang-format file using ${CLANG_FORMAT}"
  28. )
  29. add_custom_target(
  30. clang-format-diff
  31. COMMAND ${CLANG_FORMAT} -style=file -i ${ALL_SOURCE_FILES}
  32. COMMAND git diff ${ALL_SOURCE_FILES}
  33. COMMENT "Formatting with clang-format (using ${CLANG_FORMAT}) and showing differences with latest commit"
  34. )