Advertisement
Guest User

Integrate clang-format into cmake

a guest
Feb 7th, 2017
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CMake 1.55 KB | None | 0 0
  1. find_package(clangformat QUIET)
  2.  
  3. function(add_clangformat _targetname)
  4.     if (clangformat_FOUND)
  5.         if (NOT TARGET ${_targetname})
  6.             message(FATAL_ERROR "add_clangformat should only be called on targets (got " ${_targetname} ")")
  7.         endif ()
  8.  
  9.         # figure out which sources this should be applied to
  10.         get_target_property(_clang_sources ${_targetname} SOURCES)
  11.         get_target_property(_builddir ${_targetname} BINARY_DIR)
  12.  
  13.  
  14.         set(_sources "")
  15.         foreach (_source ${_clang_sources})
  16.             if (NOT TARGET ${_source})
  17.                 get_filename_component(_source_file ${_source} NAME)
  18.                 get_source_file_property(_clang_loc "${_source}" LOCATION)
  19.  
  20.                 set(_format_file ${_targetname}_${_source_file}.format)
  21.  
  22.                 add_custom_command(OUTPUT ${_format_file}
  23.                         DEPENDS ${_source}
  24.                         COMMENT "Clang-Format ${_source}"
  25.                         COMMAND ${clangformat_EXECUTABLE} -style=file -fallback-style=WebKit -sort-includes -i ${_clang_loc}
  26.                         COMMAND ${CMAKE_COMMAND} -E touch ${_format_file})
  27.  
  28.                 list(APPEND _sources ${_format_file})
  29.             endif ()
  30.         endforeach ()
  31.  
  32.         if (_sources)
  33.             add_custom_target(${_targetname}_clangformat
  34.                     SOURCES ${_sources}
  35.                     COMMENT "Clang-Format for target ${_target}")
  36.  
  37.             add_dependencies(${_targetname} ${_targetname}_clangformat)
  38.         endif ()
  39.  
  40.     endif ()
  41. endfunction()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement