@@ -0,0 +1,26 | |||
|
1 | --- | |
|
2 | # See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for a definition | |
|
3 | # of the options | |
|
4 | Language: Cpp | |
|
5 | BasedOnStyle: LLVM | |
|
6 | ||
|
7 | # Line length | |
|
8 | ColumnLimit: 100 | |
|
9 | ||
|
10 | # Indent with 4 spaces | |
|
11 | IndentWidth: 4 | |
|
12 | AccessModifierOffset: -4 # -IndentWidth | |
|
13 | ConstructorInitializerIndentWidth: 8 # 2 * IndentWidth | |
|
14 | ||
|
15 | # Break only before function braces | |
|
16 | BreakBeforeBraces: Stroustrup | |
|
17 | ||
|
18 | AllowShortFunctionsOnASingleLine: Inline | |
|
19 | AlwaysBreakTemplateDeclarations: true | |
|
20 | AlwaysBreakBeforeMultilineStrings: true | |
|
21 | BreakBeforeBinaryOperators: true | |
|
22 | ConstructorInitializerAllOnOneLineOrOnePerLine: true | |
|
23 | IndentCaseLabels: true | |
|
24 | MaxEmptyLinesToKeep: 2 | |
|
25 | Standard: Cpp03 | |
|
26 |
@@ -0,0 +1,26 | |||
|
1 | --- | |
|
2 | # See http://clang.llvm.org/docs/ClangFormatStyleOptions.html for a definition | |
|
3 | # of the options | |
|
4 | Language: Cpp | |
|
5 | BasedOnStyle: LLVM | |
|
6 | ||
|
7 | # Line length | |
|
8 | ColumnLimit: 100 | |
|
9 | ||
|
10 | # Indent with 4 spaces | |
|
11 | IndentWidth: 4 | |
|
12 | AccessModifierOffset: -4 # -IndentWidth | |
|
13 | ConstructorInitializerIndentWidth: 8 # 2 * IndentWidth | |
|
14 | ||
|
15 | # Break only before function braces | |
|
16 | BreakBeforeBraces: Stroustrup | |
|
17 | ||
|
18 | AllowShortFunctionsOnASingleLine: Inline | |
|
19 | AlwaysBreakTemplateDeclarations: true | |
|
20 | AlwaysBreakBeforeMultilineStrings: true | |
|
21 | BreakBeforeBinaryOperators: true | |
|
22 | ConstructorInitializerAllOnOneLineOrOnePerLine: true | |
|
23 | IndentCaseLabels: true | |
|
24 | MaxEmptyLinesToKeep: 2 | |
|
25 | Standard: Cpp03 | |
|
26 |
@@ -0,0 +1,52 | |||
|
1 | # - try to find clang-format tool | |
|
2 | # | |
|
3 | # Cache Variables: | |
|
4 | # CLANGFORMAT_ROOT_DIR | |
|
5 | # CLANGFORMAT_EXECUTABLE | |
|
6 | # CLANGFORMAT_USE_FILE | |
|
7 | # | |
|
8 | # Non-cache variables you might use in your CMakeLists.txt: | |
|
9 | # CLANGFORMAT_FOUND | |
|
10 | # | |
|
11 | # Requires these CMake modules: | |
|
12 | # FindPackageHandleStandardArgs (known included with CMake >=2.6.2) | |
|
13 | ||
|
14 | file(TO_CMAKE_PATH "${CLANGFORMAT_ROOT_DIR}" CLANGFORMAT_ROOT_DIR) | |
|
15 | set(CLANGFORMAT_ROOT_DIR | |
|
16 | "${CLANGFORMAT_ROOT_DIR}" | |
|
17 | CACHE | |
|
18 | PATH | |
|
19 | "Path to search for clang-format") | |
|
20 | ||
|
21 | if(CLANGFORMAT_EXECUTABLE AND NOT EXISTS "${CLANGFORMAT_EXECUTABLE}") | |
|
22 | set(CLANGFORMAT_EXECUTABLE "notfound" CACHE PATH FORCE "") | |
|
23 | endif() | |
|
24 | ||
|
25 | # If we have a custom path, look there first. | |
|
26 | if(CLANGFORMAT_ROOT_DIR) | |
|
27 | find_program(CLANGFORMAT_EXECUTABLE | |
|
28 | NAMES | |
|
29 | clang-format | |
|
30 | PATHS | |
|
31 | "${CLANGFORMAT_ROOT_DIR}" | |
|
32 | PATH_SUFFIXES | |
|
33 | bin | |
|
34 | NO_DEFAULT_PATH) | |
|
35 | endif() | |
|
36 | ||
|
37 | find_program(CLANGFORMAT_EXECUTABLE NAMES clang-format) | |
|
38 | ||
|
39 | # Find the use file for clang-format | |
|
40 | GET_FILENAME_COMPONENT(CLANGFORMAT_MODULE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH) | |
|
41 | SET(CLANGFORMAT_USE_FILE "${CLANGFORMAT_MODULE_DIR}/use_clangformat.cmake") | |
|
42 | ||
|
43 | SET(CLANGFORMAT_ALL ${CLANGFORMAT_EXECUTABLE} ${CLANGFORMAT_USE_FILE}) | |
|
44 | ||
|
45 | include(FindPackageHandleStandardArgs) | |
|
46 | find_package_handle_standard_args(ClangFormat | |
|
47 | DEFAULT_MSG | |
|
48 | CLANGFORMAT_ALL | |
|
49 | CLANGFORMAT_EXECUTABLE | |
|
50 | CLANGFORMAT_USE_FILE) | |
|
51 | ||
|
52 | mark_as_advanced(CLANGFORMAT_EXECUTABLE) |
@@ -0,0 +1,128 | |||
|
1 | # | |
|
2 | # use_clangformat.cmake | |
|
3 | # | |
|
4 | # The following functions are defined in this document: | |
|
5 | # | |
|
6 | ||
|
7 | # ADD_CLANGFORMAT_TARGETS(<globExpression> ... | |
|
8 | # [ADD_TO_ALL] | |
|
9 | # [NAME <name>] | |
|
10 | # [NAME_ALL <nameall>] | |
|
11 | # [STYLE style] | |
|
12 | # [RECURSE]) | |
|
13 | # | |
|
14 | # Two custom targets will be created: | |
|
15 | # * format_reports is run as part of the build, and is not rerun unless one of | |
|
16 | # the file formatted is modified (only created if ADD_TO_ALL is provided); | |
|
17 | # * format must be explicitely called (make format) and is rerun even if the | |
|
18 | # files to format have not been modified. To achieve this behavior, the commands | |
|
19 | # used in this target pretend to produce a file without actually producing it. | |
|
20 | # Because the output file is not there after the run, the command will be rerun | |
|
21 | # again at the next target build. | |
|
22 | # | |
|
23 | # If ADD_TO_ALL is provided then a target will be added to the default build | |
|
24 | # targets so that each time a source file is compiled, it is formatted with | |
|
25 | # clang-format. | |
|
26 | # | |
|
27 | # NAME and NAME_ALL customize the name of the targets (format and format_reports | |
|
28 | # by default respectively). | |
|
29 | # | |
|
30 | # STYLE sets the style used by clang-format (default to "file"). | |
|
31 | # | |
|
32 | # RECURSE selects if the glob expressions should be applied recursively or not. | |
|
33 | FUNCTION(ADD_CLANGFORMAT_TARGETS) | |
|
34 | # Default values | |
|
35 | SET(target "format") | |
|
36 | SET(target_all "format_all") | |
|
37 | SET(style "file") | |
|
38 | SET(recurse OFF) | |
|
39 | SET(addToAll OFF) | |
|
40 | SET(globs) | |
|
41 | ||
|
42 | # Parse the options | |
|
43 | MATH(EXPR lastIdx "${ARGC} - 1") | |
|
44 | SET(i 0) | |
|
45 | WHILE(i LESS ${ARGC}) | |
|
46 | SET(arg "${ARGV${i}}") | |
|
47 | IF("${arg}" STREQUAL "ADD_TO_ALL") | |
|
48 | SET(addToAll ON) | |
|
49 | ELSEIF("${arg}" STREQUAL "NAME") | |
|
50 | clangformat_incr(i) | |
|
51 | SET(target "${ARGV${i}}") | |
|
52 | ELSEIF("${arg}" STREQUAL "NAME_ALL") | |
|
53 | clangformat_incr(i) | |
|
54 | SET(target_all "${ARGV${i}}") | |
|
55 | ELSEIF("${arg}" STREQUAL "STYLE") | |
|
56 | clangformat_incr(i) | |
|
57 | SET(style "${ARGV${i}}") | |
|
58 | ELSEIF("${arg}" STREQUAL "RECURSE") | |
|
59 | SET(recurse ON) | |
|
60 | ELSE() | |
|
61 | LIST(APPEND globs ${arg}) | |
|
62 | ENDIF() | |
|
63 | clangformat_incr(i) | |
|
64 | ENDWHILE() | |
|
65 | ||
|
66 | # Retrieve source files to format | |
|
67 | IF(recurse) | |
|
68 | FILE(GLOB_RECURSE srcs ${globs}) | |
|
69 | ELSE() | |
|
70 | FILE(GLOB srcs ${globs}) | |
|
71 | ENDIF() | |
|
72 | ||
|
73 | IF(NOT CLANGFORMAT_EXECUTABLE) | |
|
74 | MESSAGE(FATAL_ERROR "Unable to find clang-format executable") | |
|
75 | ENDIF() | |
|
76 | ||
|
77 | # Format each source file with clang-format | |
|
78 | SET(touchedFiles) | |
|
79 | SET(fakedTouchedFiles) | |
|
80 | SET(reportNb 0) | |
|
81 | # Create the directory where the touched files will be saved | |
|
82 | SET(formatDirectory "${CMAKE_CURRENT_BINARY_DIR}/format") | |
|
83 | FILE(MAKE_DIRECTORY ${formatDirectory}) | |
|
84 | FOREACH (s ${srcs}) | |
|
85 | SET(touchedFile ${formatDirectory}/format_touchedfile_${reportNb}) | |
|
86 | IF(addToAll) | |
|
87 | ADD_CUSTOM_COMMAND( | |
|
88 | OUTPUT ${touchedFile} | |
|
89 | COMMAND ${CLANGFORMAT_EXECUTABLE} | |
|
90 | -style="${style}" | |
|
91 | -i | |
|
92 | ${s} | |
|
93 | # Create a file so that this command is executed only if the source | |
|
94 | # file is modified | |
|
95 | COMMAND ${CMAKE_COMMAND} -E touch ${touchedFile} | |
|
96 | DEPENDS ${s} | |
|
97 | COMMENT "Formatting code with clang-format of ${s}" | |
|
98 | ) | |
|
99 | ENDIF() | |
|
100 | ||
|
101 | SET(fakedTouchedFile ${formatDirectory}/format_fakedtouchedfile_${reportNb}) | |
|
102 | ADD_CUSTOM_COMMAND( | |
|
103 | OUTPUT ${fakedTouchedFile} | |
|
104 | COMMAND ${CLANGFORMAT_EXECUTABLE} | |
|
105 | -style="${style}" | |
|
106 | -i | |
|
107 | ${s} | |
|
108 | DEPENDS ${s} | |
|
109 | COMMENT "Formatting code with clang-format of ${s}" | |
|
110 | ) | |
|
111 | ||
|
112 | LIST(APPEND touchedFiles ${touchedFile}) | |
|
113 | LIST(APPEND fakedTouchedFiles ${fakedTouchedFile}) | |
|
114 | clangformat_incr(reportNb) | |
|
115 | ENDFOREACH() | |
|
116 | ||
|
117 | # Create the custom targets that will trigger the custom command created | |
|
118 | # previously | |
|
119 | IF(addToAll) | |
|
120 | ADD_CUSTOM_TARGET(${target_all} ALL DEPENDS ${touchedFiles}) | |
|
121 | ENDIF() | |
|
122 | ADD_CUSTOM_TARGET(${target} DEPENDS ${fakedTouchedFiles}) | |
|
123 | ||
|
124 | ENDFUNCTION(ADD_CLANGFORMAT_TARGETS) | |
|
125 | ||
|
126 | macro(clangformat_incr var_name) | |
|
127 | math(EXPR ${var_name} "${${var_name}} + 1") | |
|
128 | endmacro() |
General Comments 0
You need to be logged in to leave comments.
Login now