-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for compilation target 'format'
* check clang-format 6.0 or above * skip files in doc/ in cmake >3.6 * search all possible clang-format names * might work in cmake 3.5 * added support for windows style paths * added FindClangFormat.cmake to make it all system compatible
- Loading branch information
1 parent
0d78358
commit a103ae5
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# | ||
# .rst: FindClangFormat | ||
# --------------- | ||
# | ||
# The module defines the following variables | ||
# | ||
# ``CLANG_FORMAT_EXECUTABLE`` Path to clang-format executable | ||
# ``CLANG_FORMAT_FOUND`` True if the clang-format executable was found. | ||
# ``CLANG_FORMAT_VERSION`` The version of clang-format found | ||
# ``CLANG_FORMAT_VERSION_MAJOR`` The clang-format major version if specified, 0 | ||
# otherwise ``CLANG_FORMAT_VERSION_MINOR`` The clang-format minor version if | ||
# specified, 0 otherwise ``CLANG_FORMAT_VERSION_PATCH`` The clang-format patch | ||
# version if specified, 0 otherwise ``CLANG_FORMAT_VERSION_COUNT`` Number of | ||
# version components reported by clang-format | ||
# | ||
# Example usage: | ||
# | ||
# .. code-block:: cmake | ||
# | ||
# find_package(ClangFormat) if(CLANG_FORMAT_FOUND) message("clang-format | ||
# executable found: ${CLANG_FORMAT_EXECUTABLE}\n" "version: | ||
# ${CLANG_FORMAT_VERSION}") endif() | ||
|
||
find_program(CLANG_FORMAT_EXECUTABLE | ||
NAMES clang-format | ||
clang-format-9 | ||
clang-format-9.0 | ||
clang-format-8 | ||
clang-format-8.0 | ||
clang-format-7 | ||
clang-format-7.0 | ||
clang-format-6.0 | ||
clang-format-5.0 | ||
clang-format-4.0 | ||
clang-format-3.9 | ||
clang-format-3.8 | ||
clang-format-3.7 | ||
clang-format-3.6 | ||
clang-format-3.5 | ||
clang-format-3.4 | ||
clang-format-3.3 | ||
DOC "clang-format executable") | ||
mark_as_advanced(CLANG_FORMAT_EXECUTABLE) | ||
|
||
# Extract version from command "clang-format -version" | ||
if(CLANG_FORMAT_EXECUTABLE) | ||
execute_process(COMMAND ${CLANG_FORMAT_EXECUTABLE} -version | ||
OUTPUT_VARIABLE clang_format_version | ||
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
||
if(clang_format_version MATCHES "^.*clang-format version .*") | ||
# clang_format_version samples: | ||
# * clang-format version 3.9.1-4ubuntu3~16.04.1 (tags/RELEASE_391/rc2) | ||
# * Alpine clang-format version 8.0.0 (tags/RELEASE_800/final) (based on LLVM 8.0.0) | ||
string(REGEX | ||
REPLACE ".*clang-format version ([.0-9]+).*" | ||
"\\1" | ||
CLANG_FORMAT_VERSION | ||
"${clang_format_version}") | ||
# CLANG_FORMAT_VERSION sample: "3.9.1" | ||
|
||
# Extract version components | ||
string(REPLACE "." | ||
";" | ||
clang_format_version | ||
"${CLANG_FORMAT_VERSION}") | ||
list(LENGTH clang_format_version CLANG_FORMAT_VERSION_COUNT) | ||
if(CLANG_FORMAT_VERSION_COUNT GREATER 0) | ||
list(GET clang_format_version 0 CLANG_FORMAT_VERSION_MAJOR) | ||
else() | ||
set(CLANG_FORMAT_VERSION_MAJOR 0) | ||
endif() | ||
if(CLANG_FORMAT_VERSION_COUNT GREATER 1) | ||
list(GET clang_format_version 1 CLANG_FORMAT_VERSION_MINOR) | ||
else() | ||
set(CLANG_FORMAT_VERSION_MINOR 0) | ||
endif() | ||
if(CLANG_FORMAT_VERSION_COUNT GREATER 2) | ||
list(GET clang_format_version 2 CLANG_FORMAT_VERSION_PATCH) | ||
else() | ||
set(CLANG_FORMAT_VERSION_PATCH 0) | ||
endif() | ||
endif() | ||
unset(clang_format_version) | ||
endif() | ||
|
||
if(CLANG_FORMAT_EXECUTABLE) | ||
set(CLANG_FORMAT_FOUND TRUE) | ||
else() | ||
set(CLANG_FORMAT_FOUND FALSE) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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_package(ClangFormat) | ||
# search for version number in clang-format without version number | ||
if(CLANG_FORMAT_FOUND) | ||
if(${CLANG_FORMAT_VERSION} VERSION_LESS "6.0.0") | ||
message(WARNING "No suitable clang-format (version >= 6.0) found") | ||
else() | ||
set(CLANG_FORMAT_FOUND_VERSION TRUE) | ||
endif() | ||
endif() | ||
|
||
if(CLANG_FORMAT_FOUND_VERSION) | ||
message(STATUS "Using ${CLANG_FORMAT_EXECUTABLE} for target 'format'") | ||
add_custom_target( | ||
format | ||
COMMAND ${CLANG_FORMAT_EXECUTABLE} | ||
-i | ||
-style=file | ||
${ALL_CXX_SOURCE_FILES} | ||
) | ||
endif() |