@@ -1,128 +1,139 | |||||
1 | # |
|
1 | # | |
2 | # use_clangformat.cmake |
|
2 | # use_clangformat.cmake | |
3 | # |
|
3 | # | |
4 | # The following functions are defined in this document: |
|
4 | # The following functions are defined in this document: | |
5 | # |
|
5 | # | |
6 |
|
6 | |||
7 | # ADD_CLANGFORMAT_TARGETS(<globExpression> ... |
|
7 | # ADD_CLANGFORMAT_TARGETS(<globExpression> ... | |
8 | # [ADD_TO_ALL] |
|
8 | # [ADD_TO_ALL] | |
9 | # [NAME <name>] |
|
9 | # [NAME <name>] | |
10 | # [NAME_ALL <nameall>] |
|
10 | # [NAME_ALL <nameall>] | |
11 | # [STYLE style] |
|
11 | # [STYLE style] | |
12 | # [RECURSE]) |
|
12 | # [RECURSE]) | |
13 | # |
|
13 | # | |
14 | # Two custom targets will be created: |
|
14 | # Two custom targets will be created: | |
15 | # * format_reports is run as part of the build, and is not rerun unless one of |
|
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); |
|
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 |
|
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 |
|
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. |
|
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 |
|
20 | # Because the output file is not there after the run, the command will be rerun | |
21 | # again at the next target build. |
|
21 | # again at the next target build. | |
22 | # |
|
22 | # | |
23 | # If ADD_TO_ALL is provided then a target will be added to the default build |
|
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 |
|
24 | # targets so that each time a source file is compiled, it is formatted with | |
25 | # clang-format. |
|
25 | # clang-format. | |
26 | # |
|
26 | # | |
27 | # NAME and NAME_ALL customize the name of the targets (format and format_reports |
|
27 | # NAME and NAME_ALL customize the name of the targets (format and format_reports | |
28 | # by default respectively). |
|
28 | # by default respectively). | |
29 | # |
|
29 | # | |
30 | # STYLE sets the style used by clang-format (default to "file"). |
|
30 | # STYLE sets the style used by clang-format (default to "file"). | |
31 | # |
|
31 | # | |
32 | # RECURSE selects if the glob expressions should be applied recursively or not. |
|
32 | # RECURSE selects if the glob expressions should be applied recursively or not. | |
33 | FUNCTION(ADD_CLANGFORMAT_TARGETS) |
|
33 | FUNCTION(ADD_CLANGFORMAT_TARGETS) | |
34 | # Default values |
|
34 | # Default values | |
35 | SET(target "format") |
|
35 | SET(target "format") | |
36 | SET(target_all "format_all") |
|
36 | SET(target_all "format_all") | |
37 | SET(style "file") |
|
37 | SET(style "file") | |
38 | SET(recurse OFF) |
|
38 | SET(recurse OFF) | |
39 | SET(addToAll OFF) |
|
39 | SET(addToAll OFF) | |
40 | SET(globs) |
|
40 | SET(globs) | |
41 |
|
41 | |||
42 | # Parse the options |
|
42 | # Parse the options | |
43 | MATH(EXPR lastIdx "${ARGC} - 1") |
|
43 | MATH(EXPR lastIdx "${ARGC} - 1") | |
44 | SET(i 0) |
|
44 | SET(i 0) | |
45 | WHILE(i LESS ${ARGC}) |
|
45 | WHILE(i LESS ${ARGC}) | |
46 | SET(arg "${ARGV${i}}") |
|
46 | SET(arg "${ARGV${i}}") | |
47 | IF("${arg}" STREQUAL "ADD_TO_ALL") |
|
47 | IF("${arg}" STREQUAL "ADD_TO_ALL") | |
48 | SET(addToAll ON) |
|
48 | SET(addToAll ON) | |
49 | ELSEIF("${arg}" STREQUAL "NAME") |
|
49 | ELSEIF("${arg}" STREQUAL "NAME") | |
50 | clangformat_incr(i) |
|
50 | clangformat_incr(i) | |
51 | SET(target "${ARGV${i}}") |
|
51 | SET(target "${ARGV${i}}") | |
52 | ELSEIF("${arg}" STREQUAL "NAME_ALL") |
|
52 | ELSEIF("${arg}" STREQUAL "NAME_ALL") | |
53 | clangformat_incr(i) |
|
53 | clangformat_incr(i) | |
54 | SET(target_all "${ARGV${i}}") |
|
54 | SET(target_all "${ARGV${i}}") | |
55 | ELSEIF("${arg}" STREQUAL "STYLE") |
|
55 | ELSEIF("${arg}" STREQUAL "STYLE") | |
56 | clangformat_incr(i) |
|
56 | clangformat_incr(i) | |
57 | SET(style "${ARGV${i}}") |
|
57 | SET(style "${ARGV${i}}") | |
58 | ELSEIF("${arg}" STREQUAL "RECURSE") |
|
58 | ELSEIF("${arg}" STREQUAL "RECURSE") | |
59 | SET(recurse ON) |
|
59 | SET(recurse ON) | |
60 | ELSE() |
|
60 | ELSE() | |
61 | LIST(APPEND globs ${arg}) |
|
61 | LIST(APPEND globs ${arg}) | |
62 | ENDIF() |
|
62 | ENDIF() | |
63 | clangformat_incr(i) |
|
63 | clangformat_incr(i) | |
64 | ENDWHILE() |
|
64 | ENDWHILE() | |
65 |
|
65 | |||
|
66 | ||||
66 | # Retrieve source files to format |
|
67 | # Retrieve source files to format | |
67 | IF(recurse) |
|
68 | IF(recurse) | |
68 | FILE(GLOB_RECURSE srcs ${globs}) |
|
69 | FILE(GLOB_RECURSE srcs ${globs}) | |
69 | ELSE() |
|
70 | ELSE() | |
70 | FILE(GLOB srcs ${globs}) |
|
71 | FILE(GLOB srcs ${globs}) | |
71 | ENDIF() |
|
72 | ENDIF() | |
72 |
|
73 | |||
73 | IF(NOT CLANGFORMAT_EXECUTABLE) |
|
74 | IF(NOT CLANGFORMAT_EXECUTABLE) | |
74 | MESSAGE(FATAL_ERROR "Unable to find clang-format executable") |
|
75 | MESSAGE(FATAL_ERROR "Unable to find clang-format executable") | |
75 | ENDIF() |
|
76 | ENDIF() | |
76 |
|
77 | |||
77 | # Format each source file with clang-format |
|
78 | # Format each source file with clang-format | |
78 | SET(touchedFiles) |
|
79 | SET(touchedFiles) | |
79 | SET(fakedTouchedFiles) |
|
80 | SET(fakedTouchedFiles) | |
80 | SET(reportNb 0) |
|
81 | SET(reportNb 0) | |
81 | # Create the directory where the touched files will be saved |
|
82 | # Create the directory where the touched files will be saved | |
82 | SET(formatDirectory "${CMAKE_CURRENT_BINARY_DIR}/format") |
|
83 | SET(formatDirectory "${CMAKE_CURRENT_BINARY_DIR}/format") | |
83 | FILE(MAKE_DIRECTORY ${formatDirectory}) |
|
84 | FILE(MAKE_DIRECTORY ${formatDirectory}) | |
|
85 | # STRING(REPLACE "*.ui" "" srcs ${srcs}) | |||
|
86 | FOREACH(item ${globs}) | |||
|
87 | STRING(REGEX MATCH ".*\.ui$" item ${item}) | |||
|
88 | IF(item) | |||
|
89 | LIST(APPEND UIS ${item}) | |||
|
90 | ENDIF(item) | |||
|
91 | ENDFOREACH(item ${globs}) | |||
|
92 | ||||
|
93 | LIST(REMOVE_ITEM srcs ${UIS}) | |||
|
94 | message("format lang: ${srcs}" ) | |||
84 | FOREACH (s ${srcs}) |
|
95 | FOREACH (s ${srcs}) | |
85 | SET(touchedFile ${formatDirectory}/format_touchedfile_${reportNb}) |
|
96 | SET(touchedFile ${formatDirectory}/format_touchedfile_${reportNb}) | |
86 | IF(addToAll) |
|
97 | IF(addToAll) | |
87 | ADD_CUSTOM_COMMAND( |
|
98 | ADD_CUSTOM_COMMAND( | |
88 | OUTPUT ${touchedFile} |
|
99 | OUTPUT ${touchedFile} | |
89 | COMMAND ${CLANGFORMAT_EXECUTABLE} |
|
100 | COMMAND ${CLANGFORMAT_EXECUTABLE} | |
90 | -style="${style}" |
|
101 | -style="${style}" | |
91 | -i |
|
102 | -i | |
92 | ${s} |
|
103 | ${s} | |
93 | # Create a file so that this command is executed only if the source |
|
104 | # Create a file so that this command is executed only if the source | |
94 | # file is modified |
|
105 | # file is modified | |
95 | COMMAND ${CMAKE_COMMAND} -E touch ${touchedFile} |
|
106 | COMMAND ${CMAKE_COMMAND} -E touch ${touchedFile} | |
96 | DEPENDS ${s} |
|
107 | DEPENDS ${s} | |
97 | COMMENT "Formatting code with clang-format of ${s}" |
|
108 | COMMENT "Formatting code with clang-format of ${s}" | |
98 | ) |
|
109 | ) | |
99 | ENDIF() |
|
110 | ENDIF() | |
100 |
|
111 | |||
101 | SET(fakedTouchedFile ${formatDirectory}/format_fakedtouchedfile_${reportNb}) |
|
112 | SET(fakedTouchedFile ${formatDirectory}/format_fakedtouchedfile_${reportNb}) | |
102 | ADD_CUSTOM_COMMAND( |
|
113 | ADD_CUSTOM_COMMAND( | |
103 | OUTPUT ${fakedTouchedFile} |
|
114 | OUTPUT ${fakedTouchedFile} | |
104 | COMMAND ${CLANGFORMAT_EXECUTABLE} |
|
115 | COMMAND ${CLANGFORMAT_EXECUTABLE} | |
105 | -style="${style}" |
|
116 | -style="${style}" | |
106 | -i |
|
117 | -i | |
107 | ${s} |
|
118 | ${s} | |
108 | DEPENDS ${s} |
|
119 | DEPENDS ${s} | |
109 | COMMENT "Formatting code with clang-format of ${s}" |
|
120 | COMMENT "Formatting code with clang-format of ${s}" | |
110 | ) |
|
121 | ) | |
111 |
|
122 | |||
112 | LIST(APPEND touchedFiles ${touchedFile}) |
|
123 | LIST(APPEND touchedFiles ${touchedFile}) | |
113 | LIST(APPEND fakedTouchedFiles ${fakedTouchedFile}) |
|
124 | LIST(APPEND fakedTouchedFiles ${fakedTouchedFile}) | |
114 | clangformat_incr(reportNb) |
|
125 | clangformat_incr(reportNb) | |
115 | ENDFOREACH() |
|
126 | ENDFOREACH() | |
116 |
|
127 | |||
117 | # Create the custom targets that will trigger the custom command created |
|
128 | # Create the custom targets that will trigger the custom command created | |
118 | # previously |
|
129 | # previously | |
119 | IF(addToAll) |
|
130 | IF(addToAll) | |
120 | ADD_CUSTOM_TARGET(${target_all} ALL DEPENDS ${touchedFiles}) |
|
131 | ADD_CUSTOM_TARGET(${target_all} ALL DEPENDS ${touchedFiles}) | |
121 | ENDIF() |
|
132 | ENDIF() | |
122 | ADD_CUSTOM_TARGET(${target} DEPENDS ${fakedTouchedFiles}) |
|
133 | ADD_CUSTOM_TARGET(${target} DEPENDS ${fakedTouchedFiles}) | |
123 |
|
134 | |||
124 | ENDFUNCTION(ADD_CLANGFORMAT_TARGETS) |
|
135 | ENDFUNCTION(ADD_CLANGFORMAT_TARGETS) | |
125 |
|
136 | |||
126 | macro(clangformat_incr var_name) |
|
137 | macro(clangformat_incr var_name) | |
127 | math(EXPR ${var_name} "${${var_name}} + 1") |
|
138 | math(EXPR ${var_name} "${${var_name}} + 1") | |
128 | endmacro() |
|
139 | endmacro() |
General Comments 0
You need to be logged in to leave comments.
Login now