Skip to content

Commit 0bc1472

Browse files
committed
Added automatic formatting specification for CMake files
While there are formatting description files for C/C++ (i.e., `.clang-format`), no equivalent formatting specification exists for CMake (to automatically format CMake files (e.g., `CMakeLists.txt`) consistently with `cmake-format`). This commit adds such a format description to ensure all CMake files are formatted consistently.
1 parent 96c021b commit 0bc1472

File tree

1 file changed

+354
-0
lines changed

1 file changed

+354
-0
lines changed

.cmake-format.py

Lines changed: 354 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
# ----------------------------------
2+
# Options affecting listfile parsing
3+
# ----------------------------------
4+
with section("parse"): # type: ignore
5+
6+
# Specify structure for custom cmake functions
7+
additional_commands = {
8+
"target_link_options": {
9+
"pargs": 1,
10+
"flags": [
11+
"BEFORE",
12+
],
13+
"kwargs": {
14+
"PUBLIC": "+",
15+
"PRIVATE": "+",
16+
"INTERFACE": "+",
17+
},
18+
},
19+
"target_sources": {
20+
"pargs": 1,
21+
"flags": [],
22+
"kwargs": {
23+
"PUBLIC": {
24+
"flags": [],
25+
"pargs": "*",
26+
"kwargs": {
27+
"FILE_SET": {
28+
"pargs": 1,
29+
"kwargs": {
30+
"TYPE": 1,
31+
"FILES": "+",
32+
"BASE_DIRS": "+",
33+
},
34+
},
35+
},
36+
},
37+
"PRIVATE": {
38+
"flags": [],
39+
"pargs": "*",
40+
"kwargs": {
41+
"FILE_SET": {
42+
"pargs": 1,
43+
"kwargs": {
44+
"TYPE": 1,
45+
"FILES": "+",
46+
"BASE_DIRS": "+",
47+
},
48+
},
49+
},
50+
},
51+
"INTERFACE": {
52+
"flags": [],
53+
"pargs": "*",
54+
"kwargs": {
55+
"FILE_SET": {
56+
"pargs": 1,
57+
"kwargs": {
58+
"TYPE": 1,
59+
"FILES": "+",
60+
"BASE_DIRS": "+",
61+
},
62+
},
63+
},
64+
},
65+
},
66+
},
67+
"find_package": {
68+
"pargs": "+",
69+
"flags": [
70+
"EXACT",
71+
"QUIET",
72+
"CONFIG",
73+
"GLOBAL",
74+
"REQUIRED",
75+
"NO_MODULE",
76+
"NO_CMAKE_PATH",
77+
"BYPASS_PROVIDER",
78+
"NO_POLICY_SCOPE",
79+
"NO_DEFAULT_PATH",
80+
"NO_CMAKE_BUILDS_PATH",
81+
"NO_CMAKE_SYSTEM_PATH",
82+
"NO_PACKAGE_ROOT_PATH",
83+
"NO_CMAKE_FIND_ROOT_PATH",
84+
"NO_CMAKE_INSTALL_PREFIX",
85+
"NO_CMAKE_ENVIRONMENT_PATH",
86+
"CMAKE_FIND_ROOT_PATH_BOTH",
87+
"NO_CMAKE_PACKAGE_REGISTRY",
88+
"ONLY_CMAKE_FIND_ROOT_PATH",
89+
"NO_SYSTEM_ENVIRONMENT_PATH",
90+
"NO_CMAKE_SYSTEM_PACKAGE_REGISTRY",
91+
],
92+
"kwargs": {
93+
"HINTS": "+",
94+
"NAMES": "+",
95+
"PATHS": "+",
96+
"CONFIGS": "+",
97+
"COMPONENTS": "*",
98+
"PATH_SUFFIXES": "+",
99+
"REGISTRY_VIEW": "1",
100+
"OPTIONAL_COMPONENTS": "*",
101+
},
102+
},
103+
"write_basic_package_version_file": {
104+
"pargs": 1,
105+
"flags": [
106+
"ARCH_INDEPENDENT",
107+
],
108+
"kwargs": {
109+
"COMPATIBILITY": 1,
110+
"VERSION": 1,
111+
},
112+
},
113+
"configure_package_config_file": {
114+
"pargs": "2",
115+
"kwargs": {
116+
"PATH_VARS": "*",
117+
"INSTALL_PREFIX": "1",
118+
"INSTALL_DESTINATION": "1",
119+
},
120+
"flags": ["NO_SET_AND_CHECK_MACRO", "NO_CHECK_REQUIRED_COMPONENTS_MACRO"],
121+
},
122+
}
123+
124+
# Override configurations per-command where available
125+
override_spec = {}
126+
127+
# Specify variable tags.
128+
vartags = []
129+
130+
# Specify property tags.
131+
proptags = []
132+
133+
# -----------------------------
134+
# Options affecting formatting.
135+
# -----------------------------
136+
with section("format"): # type: ignore
137+
138+
# Disable formatting entirely, making cmake-format a no-op
139+
disable = False
140+
141+
# How wide to allow formatted cmake files
142+
line_width = 120
143+
144+
# How many spaces to tab for indent
145+
tab_size = 4
146+
147+
# If true, lines are indented using tab characters (utf-8 0x09) instead of
148+
# <tab_size> space characters (utf-8 0x20). In cases where the layout would
149+
# require a fractional tab character, the behavior of the fractional
150+
# indentation is governed by <fractional_tab_policy>
151+
use_tabchars = False
152+
153+
# If <use_tabchars> is True, then the value of this variable indicates how
154+
# fractional indentions are handled during whitespace replacement. If set to
155+
# 'use-space', fractional indentation is left as spaces (utf-8 0x20). If set
156+
# to `round-up` fractional indentation is replaced with a single tab character
157+
# (utf-8 0x09) effectively shifting the column to the next tabstop
158+
fractional_tab_policy = "use-space"
159+
160+
# If an argument group contains more than this many sub-groups (parg or kwarg
161+
# groups) then force it to a vertical layout.
162+
max_subgroups_hwrap = 2
163+
164+
# If a positional argument group contains more than this many arguments, then
165+
# force it to a vertical layout.
166+
max_pargs_hwrap = 5
167+
168+
# If a cmdline positional group consumes more than this many lines without
169+
# nesting, then invalidate the layout (and nest)
170+
max_rows_cmdline = 2
171+
172+
# If true, separate flow control names from their parentheses with a space
173+
separate_ctrl_name_with_space = False
174+
175+
# If true, separate function names from parentheses with a space
176+
separate_fn_name_with_space = False
177+
178+
# If a statement is wrapped to more than one line, than dangle the closing
179+
# parenthesis on its own line.
180+
dangle_parens = True # Default: False
181+
182+
# If the trailing parenthesis must be 'dangled' on its on line, then align it
183+
# to this reference: `prefix`: the start of the statement, `prefix-indent`:
184+
# the start of the statement, plus one indentation level, `child`: align to
185+
# the column of the arguments
186+
dangle_align = "prefix-indent" # Default: prefix
187+
188+
# If the statement spelling length (including space and parenthesis) is
189+
# smaller than this amount, then force reject nested layouts.
190+
min_prefix_chars = 4
191+
192+
# If the statement spelling length (including space and parenthesis) is larger
193+
# than the tab width by more than this amount, then force reject un-nested
194+
# layouts.
195+
max_prefix_chars = 20 # Default: 10
196+
197+
# If a candidate layout is wrapped horizontally but it exceeds this many
198+
# lines, then reject the layout.
199+
max_lines_hwrap = 3 # Default: 2
200+
201+
# What style line endings to use in the output.
202+
line_ending = "unix"
203+
204+
# Format command names consistently as 'lower' or 'upper' case
205+
command_case = "lower" # Default: canonical
206+
207+
# Format keywords consistently as 'lower' or 'upper' case
208+
keyword_case = "upper" # Default: unchanged
209+
210+
# A list of command names which should always be wrapped
211+
always_wrap = []
212+
213+
# If true, the argument lists which are known to be sortable will be sorted
214+
# lexicographicall
215+
enable_sort = True
216+
217+
# If true, the parsers may infer whether or not an argument list is sortable
218+
# (without annotation).
219+
autosort = True # Default: False
220+
221+
# By default, if cmake-format cannot successfully fit everything into the
222+
# desired linewidth it will apply the last, most aggressive attempt that it
223+
# made. If this flag is True, however, cmake-format will print error, exit
224+
# with non-zero status code, and write-out nothing
225+
require_valid_layout = False
226+
227+
# A dictionary mapping layout nodes to a list of wrap decisions. See the
228+
# documentation for more information.
229+
layout_passes = {}
230+
231+
# ------------------------------------------------
232+
# Options affecting comment reflow and formatting.
233+
# ------------------------------------------------
234+
with section("markup"): # type: ignore
235+
236+
# What character to use for bulleted lists
237+
bullet_char = "*"
238+
239+
# What character to use as punctuation after numerals in an enumerated list
240+
enum_char = "."
241+
242+
# If comment markup is enabled, don't reflow the first comment block in each
243+
# listfile. Use this to preserve formatting of your copyright/license
244+
# statements.
245+
first_comment_is_literal = False
246+
247+
# If comment markup is enabled, don't reflow any comment block which matches
248+
# this (regex) pattern. Default is `None` (disabled).
249+
literal_comment_pattern = None
250+
251+
# Regular expression to match preformat fences in comments default=
252+
# ``r'^\s*([`~]{3}[`~]*)(.*)$'``
253+
fence_pattern = "^\\s*([`~]{3}[`~]*)(.*)$"
254+
255+
# Regular expression to match rulers in comments default=
256+
# ``r'^\s*[^\w\s]{3}.*[^\w\s]{3}$'``
257+
ruler_pattern = "^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$"
258+
259+
# If a comment line matches starts with this pattern then it is explicitly a
260+
# trailing comment for the preceding argument. Default is '#<'
261+
explicit_trailing_pattern = "#<"
262+
263+
# If a comment line starts with at least this many consecutive hash
264+
# characters, then don't lstrip() them off. This allows for lazy hash rulers
265+
# where the first hash char is not separated by space
266+
hashruler_min_length = 10
267+
268+
# If true, then insert a space between the first hash char and remaining hash
269+
# chars in a hash ruler, and normalize its length to fill the column
270+
canonicalize_hashrulers = True
271+
272+
# enable comment markup parsing and reflow
273+
enable_markup = False
274+
275+
# ----------------------------
276+
# Options affecting the linter
277+
# ----------------------------
278+
with section("lint"): # type: ignore
279+
280+
# a list of lint codes to disable
281+
disabled_codes = []
282+
283+
# regular expression pattern describing valid function names
284+
function_pattern = "[0-9a-z_]+"
285+
286+
# regular expression pattern describing valid macro names
287+
macro_pattern = "[0-9A-Z_]+"
288+
289+
# regular expression pattern describing valid names for variables with global
290+
# (cache) scope
291+
global_var_pattern = "[A-Z][0-9A-Z_]+"
292+
293+
# regular expression pattern describing valid names for variables with global
294+
# scope (but internal semantic)
295+
internal_var_pattern = "_[A-Z][0-9A-Z_]+"
296+
297+
# regular expression pattern describing valid names for variables with local
298+
# scope
299+
local_var_pattern = "[a-z][a-z0-9_]+"
300+
301+
# regular expression pattern describing valid names for privatedirectory
302+
# variables
303+
private_var_pattern = "_[0-9a-z_]+"
304+
305+
# regular expression pattern describing valid names for public directory
306+
# variables
307+
public_var_pattern = "[A-Z][0-9A-Z_]+"
308+
309+
# regular expression pattern describing valid names for function/macro
310+
# arguments and loop variables.
311+
argument_var_pattern = "[a-z][a-z0-9_]+"
312+
313+
# regular expression pattern describing valid names for keywords used in
314+
# functions or macros
315+
keyword_pattern = "[A-Z][0-9A-Z_]+"
316+
317+
# In the heuristic for C0201, how many conditionals to match within a loop in
318+
# before considering the loop a parser.
319+
max_conditionals_custom_parser = 2
320+
321+
# Require at least this many newlines between statements
322+
min_statement_spacing = 1
323+
324+
# Require no more than this many newlines between statements
325+
max_statement_spacing = 2
326+
max_returns = 6
327+
max_branches = 12
328+
max_arguments = 5
329+
max_localvars = 15
330+
max_statements = 50
331+
332+
# -------------------------------
333+
# Options affecting file encoding
334+
# -------------------------------
335+
with section("encode"): # type: ignore
336+
337+
# If true, emit the unicode byte-order mark (BOM) at the start of the file
338+
emit_byteorder_mark = False
339+
340+
# Specify the encoding of the input file. Defaults to utf-8
341+
input_encoding = "utf-8"
342+
343+
# Specify the encoding of the output file. Defaults to utf-8. Note that cmake
344+
# only claims to support utf-8 so be careful when using anything else
345+
output_encoding = "utf-8"
346+
347+
# -------------------------------------
348+
# Miscellaneous configurations options.
349+
# -------------------------------------
350+
with section("misc"): # type: ignore
351+
352+
# A dictionary containing any per-command configuration overrides. Currently
353+
# only `command_case` is supported.
354+
per_command = {}

0 commit comments

Comments
 (0)