1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
# pylint: disable=too-many-statements
import logging
from cmakelang.lex import TokenType
from cmakelang.parse.additional_nodes import (
FlagGroupNode, ShellCommandNode, TupleParser)
from cmakelang.parse.argument_nodes import (
ArgGroupNode,
KeywordGroupNode,
PositionalGroupNode,
PositionalParser,
StandardArgTree)
from cmakelang.parse.common import KwargBreaker, NodeType, TreeNode
from cmakelang.parse.util import (
WHITESPACE_TOKENS,
get_first_semantic_token,
get_normalized_kwarg,
should_break)
logger = logging.getLogger(__name__)
def parse_add_custom_command_events(ctx, tokens, breakstack):
"""
::
add_custom_command(TARGET <target>
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[BYPRODUCTS [files...]]
[WORKING_DIRECTORY dir]
[COMMENT comment]
[VERBATIM] [USES_TERMINAL]
[COMMAND_EXPAND_LISTS])
:see: https://cmake.org/cmake/help/latest/command/add_custom_command.html
"""
subtree = StandardArgTree.parse(
ctx, tokens,
npargs='*',
kwargs={
"BYPRODUCTS": PositionalParser('*'),
"COMMAND": ShellCommandNode.parse,
"COMMENT": PositionalParser('*'),
"TARGET": PositionalParser(1),
"WORKING_DIRECTORY": PositionalParser(1)
},
flags=[
"APPEND", "VERBATIM", "USES_TERMINAL", "COMMAND_EXPAND_LISTS",
"PRE_BUILD", "PRE_LINK", "POST_BUILD"],
breakstack=breakstack)
subtree.check_required_kwargs(ctx.lint_ctx, {
# Truly required keyword arguments
"COMMAND": "E1125",
# Required by convention
"COMMENT": "C0113"
})
return subtree
def parse_add_custom_command_standard(ctx, tokens, breakstack):
"""
::
add_custom_command(OUTPUT output1 [output2 ...]
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[MAIN_DEPENDENCY depend]
[DEPENDS [depends...]]
[BYPRODUCTS [files...]]
[IMPLICIT_DEPENDS <lang1> depend1
[<lang2> depend2] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment]
[DEPFILE depfile]
[JOB_POOL job_pool]
[VERBATIM] [APPEND] [USES_TERMINAL]
[COMMAND_EXPAND_LISTS])
:see: https://cmake.org/cmake/help/latest/command/add_custom_command.html
"""
subtree = StandardArgTree.parse(
ctx, tokens,
npargs='*',
kwargs={
"BYPRODUCTS": PositionalParser('*'),
"COMMAND": ShellCommandNode.parse,
"COMMENT": PositionalParser('*'),
"DEPENDS": PositionalParser('*'),
"DEPFILE": PositionalParser(1),
"JOB_POOL": PositionalParser(1),
"IMPLICIT_DEPENDS": TupleParser(2, '+'),
"MAIN_DEPENDENCY": PositionalParser(1),
"OUTPUT": PositionalParser('+'),
"WORKING_DIRECTORY": PositionalParser(1)
},
flags=["APPEND", "VERBATIM", "USES_TERMINAL", "COMMAND_EXPAND_LISTS"],
breakstack=breakstack)
subtree.check_required_kwargs(ctx.lint_ctx, {
# Truly required keyword arguments
"COMMAND": "E1125",
# Required by convention
"COMMENT": "C0113"
})
return subtree
def parse_add_custom_command(ctx, tokens, breakstack):
"""
There are two forms of `add_custom_command`. This is the dispatcher between
the two forms.
"""
descriminator_token = get_first_semantic_token(tokens)
if (descriminator_token is None or
descriminator_token.type is TokenType.RIGHT_PAREN):
location = ()
if tokens:
location = tokens[0].get_location()
# logger.warning("Invalid empty file() command at %s", location)
ctx.lint_ctx.record_lint("E1120", location=location)
return StandardArgTree.parse(ctx, tokens, npargs='*', kwargs={}, flags=[],
breakstack=breakstack)
if descriminator_token.type is TokenType.DEREF:
ctx.lint_ctx.record_lint(
"C0114", location=descriminator_token.get_location())
return parse_add_custom_command_standard(ctx, tokens, breakstack)
descriminator_word = get_normalized_kwarg(descriminator_token)
if descriminator_word == "TARGET":
return parse_add_custom_command_events(ctx, tokens, breakstack)
if descriminator_word == "OUTPUT":
return parse_add_custom_command_standard(ctx, tokens, breakstack)
# logger.warning(
# "Indeterminate form of add_custom_command \"%s\" at %s",
# descriminator_word, descriminator_token.location())
ctx.lint_ctx.record_lint("E1126", location=descriminator_token.get_location())
return parse_add_custom_command_standard(ctx, tokens, breakstack)
def parse_add_custom_target(ctx, tokens, breakstack):
"""
::
add_custom_target(Name [ALL] [command1 [args1...]]
[COMMAND command2 [args2...] ...]
[DEPENDS depend depend depend ... ]
[BYPRODUCTS [files...]]
[WORKING_DIRECTORY dir]
[COMMENT comment]
[JOB_POOL job_pool]
[VERBATIM] [USES_TERMINAL]
[COMMAND_EXPAND_LISTS]
[SOURCES src1 [src2...]])
:see: https://cmake.org/cmake/help/latest/command/add_custom_target.html
"""
kwargs = {
"BYPRODUCTS": PositionalParser("+"),
"COMMAND": ShellCommandNode.parse,
"COMMENT": PositionalParser(1),
"DEPENDS": PositionalParser("+"),
"JOB_POOL": PositionalParser(1),
"SOURCES": PositionalParser("+"),
"WORKING_DIRECTORY": PositionalParser(1),
}
required_kwargs = {
# Required by convention
"COMMENT": "C0113"
}
flags = ("VERBATIM", "USES_TERMINAL", "COMMAND_EXPAND_LISTS")
tree = ArgGroupNode()
# If it is a whitespace token then put it directly in the parse tree at
# the current depth
while tokens and tokens[0].type in WHITESPACE_TOKENS:
tree.children.append(tokens.pop(0))
continue
breaker = KwargBreaker(list(kwargs.keys()) + list(flags))
child_breakstack = breakstack + [breaker]
nametree = None
state = "name"
while tokens:
# Break if the next token belongs to a parent parser, i.e. if it
# matches a keyword argument of something higher in the stack, or if
# it closes a parent group.
if should_break(tokens[0], breakstack):
break
# If it is a whitespace token then put it directly in the parse tree at
# the current depth
if tokens[0].type in WHITESPACE_TOKENS:
tree.children.append(tokens.pop(0))
continue
# If it's a comment, then add it at the current depth
if tokens[0].type in (TokenType.COMMENT,
TokenType.BRACKET_COMMENT):
child = TreeNode(NodeType.COMMENT)
tree.children.append(child)
child.children.append(tokens.pop(0))
continue
ntokens = len(tokens)
if state == "name":
next_semantic = get_first_semantic_token(tokens[1:])
if (next_semantic is not None and
get_normalized_kwarg(next_semantic) == "ALL"):
npargs = 2
else:
npargs = 1
nametree = PositionalGroupNode.parse(
ctx, tokens, npargs, ["ALL"], child_breakstack)
assert len(tokens) < ntokens
tree.children.append(nametree)
state = "first-command"
continue
word = get_normalized_kwarg(tokens[0])
if state == "first-command":
if not(word in kwargs or word in flags):
subtree = PositionalGroupNode.parse(
ctx, tokens, '+', [], child_breakstack)
tree.children.append(subtree)
assert len(tokens) < ntokens
state = "kwargs"
continue
if word in flags:
subtree = FlagGroupNode.parse(
ctx, tokens, flags, breakstack + [KwargBreaker(list(kwargs.keys()))])
assert len(tokens) < ntokens
tree.children.append(subtree)
continue
if word in kwargs:
required_kwargs.pop(word, None)
subtree = KeywordGroupNode.parse(
ctx, tokens, word, kwargs[word], child_breakstack)
assert len(tokens) < ntokens
tree.children.append(subtree)
continue
ctx.lint_ctx.record_lint("E1122", location=tokens[0].get_location())
# logger.warning(
# "Unexpected positional argument %s at %s",
# tokens[0].spelling, tokens[0].location())
subtree = PositionalGroupNode.parse(ctx, tokens, '+', [], child_breakstack)
assert len(tokens) < ntokens
tree.children.append(subtree)
continue
if required_kwargs:
location = ()
for token in tree.get_semantic_tokens():
location = token.get_location()
break
missing_kwargs = sorted(
(lintid, word) for word, lintid in sorted(required_kwargs.items()))
for lintid, word in missing_kwargs:
ctx.lint_ctx.record_lint(lintid, word, location=location)
return tree
def parse_add_compile_options(ctx, tokens, breakstack):
"""
::
add_compile_options(<option> ...)
:see: https://cmake.org/cmake/help/latest/command/add_compile_options.html
"""
return StandardArgTree.parse(ctx, tokens, "+", {}, [], breakstack)
def parse_add_definitions(ctx, tokens, breakstack):
"""
::
add_definitions(-DFOO -DBAR ...)
:see: https://cmake.org/cmake/help/latest/command/add_definitions.html
"""
return StandardArgTree.parse(ctx, tokens, "+", {}, [], breakstack)
def parse_add_dependencies(ctx, tokens, breakstack):
"""
::
add_dependencies(<target> [<target-dependency>]...)
:see: https://cmake.org/cmake/help/latest/command/add_dependencies.html
"""
return StandardArgTree.parse(ctx, tokens, "2+", {}, [], breakstack)
def parse_add_subdirectory(ctx, tokens, breakstack):
"""
::
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
:see: https://cmake.org/cmake/help/latest/command/add_subdirectory.html
"""
return StandardArgTree.parse(
ctx, tokens, "+", {}, ["EXCLUDE_FROM_ALL"], breakstack)
def parse_aux_source_directory(ctx, tokens, breakstack):
"""
::
aux_source_directory(<dir> <variable>)
:see: https://cmake.org/cmake/help/latest/command/aux_source_directory.html
"""
return StandardArgTree.parse(
ctx, tokens, "2", {}, [], breakstack)
def populate_db(parse_db):
parse_db["add_custom_command"] = parse_add_custom_command
parse_db["add_custom_target"] = parse_add_custom_target
parse_db["add_compile_options"] = parse_add_compile_options
parse_db["add_definitions"] = parse_add_definitions
parse_db["add_dependencies"] = parse_add_dependencies
parse_db["add_subdirectory"] = parse_add_subdirectory
parse_db["aux_source_directory"] = parse_aux_source_directory
|