Skip to content

Commit

Permalink
Adding support for compilation target 'format'
Browse files Browse the repository at this point in the history
* clang-format 6.0 or above
* skip files in doc/
* search all possible clang-format names
* might work in cmake 3.5
* added support for windows style paths
  • Loading branch information
kunaltyagi committed Jun 23, 2019
1 parent 0d78358 commit 72c0000
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
AccessModifierOffset: '0'
AlwaysBreakAfterReturnType : All
AlwaysBreakTemplateDeclarations: 'true'
BreakBeforeBraces: Linux
Language: Cpp
NamespaceIndentation: All
SpaceBeforeParens: Always
Standard: Cpp11
TabWidth: 2
UseTab: Never
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ endif()
include("${PCL_SOURCE_DIR}/cmake/pcl_verbosity.cmake")
include("${PCL_SOURCE_DIR}/cmake/pcl_targets.cmake")
include("${PCL_SOURCE_DIR}/cmake/pcl_options.cmake")
include("${PCL_SOURCE_DIR}/cmake/clang-format.cmake")

# Enable verbose timing display?
if(CMAKE_TIMING_VERBOSE AND UNIX)
Expand Down
50 changes: 50 additions & 0 deletions cmake/clang-format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# select all files with correct c++ extensions
file(GLOB_RECURSE
ALL_CXX_SOURCE_FILES
*.[ch] *.[chi]pp *.[chi]xx *.cppm *.cc *.hh *.ii *.[CHI]
)

# exclude the docs **for now**
set(EXCLUDE_REGEX ".*doc[\\|/].*")
if(${CMAKE_VERSION} VERSION_LESS "3.6.0")
foreach(PROPOSED_FILE ${ALL_CXX_SOURCE_FILES})
string(REGEX MATCH ${EXCLUDE_REGEX} regex_found ${PROPOSED_FILE})
if(regex_found)
list(REMOVE_ITEM ${ALL_CXX_SOURCE_FILES} ${PROPOSED_FILE})
endif()
endforeach()
else()
list(FILTER ALL_CXX_SOURCE_FILES EXCLUDE REGEX ${EXCLUDE_REGEX})
endif()

find_program(CLANG_FORMAT
NAMES clang-format
# backup names
clang-format-9.0 clang-format-8.0 clang-format-7.0 clang-format-6.0
clang-format-9 clang-format-8 clang-format-7 clang-format-6
)

# search for version number in clang-format without version number
if(CLANG_FORMAT)
execute_process(
COMMAND sh "-c"
"${CLANG_FORMAT} --version | grep -oE '[0-9]' | head -n 1"
OUTPUT_VARIABLE CLANG_FORMAT_VERSION
)
if(${CLANG_FORMAT_VERSION} GREATER_EQUAL 6)
set(CLANG_FORMAT_CORRECT ${CLANG_FORMAT})
endif()
endif()

if(CLANG_FORMAT_CORRECT)
message(STATUS "Using ${CLANG_FORMAT_CORRECT} for target 'format'")
add_custom_target(
format
COMMAND ${CLANG_FORMAT_CORRECT}
-i
-style=file
${ALL_CXX_SOURCE_FILES}
)
else()
message(WARNING "No suitable clang-format (version >= 6.0) found")
endif()

0 comments on commit 72c0000

Please sign in to comment.