mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-06 13:51:33 -06:00
Tools using the json-v1 format might want to trace stack frames across different `CMakeLists.txt` files, in order to, for example, provide stacktraces that span from the top-level `CMakeLists.txt` in a project. One would think that `frame` lets you do that, but it doesn't, because it tells you the depth of the stack within the current `CMakeLists.txt`, so it gets reset across calls to `add_subdirectory`. The solution involves adding a field with a "global frame". This value gets incremented on calls to `add_subdirectory`, which makes it easier for tools to reconstruct "global stacktraces". I considered changing the current "frame" value, but I didn't because it would be a breaking change. I cannot think of any use-case where "frame" is more useful to "global-frame", but maybe I'm missing something.
85 lines
2.2 KiB
Python
Executable File
85 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3:
|
|
unicode = str
|
|
|
|
trace_file = None
|
|
expand = False
|
|
|
|
for i in sys.argv[1:]:
|
|
if trace_file is None and not i.startswith('-'):
|
|
trace_file = i
|
|
continue
|
|
|
|
if i in ['-e', '--expand']:
|
|
expand = True
|
|
|
|
assert trace_file is not None
|
|
assert os.path.exists(trace_file)
|
|
|
|
if expand:
|
|
msg_args = ['STATUS', 'fff', 'fff;sss; SPACES !!! ', ' 42 space in string!', ' SPACES !!! ']
|
|
else:
|
|
msg_args = ['STATUS', 'fff', '${ASDF}', ' ${FOO} ${BAR}', ' SPACES !!! ']
|
|
|
|
required_traces = [
|
|
{
|
|
'args': ['STATUS', 'JSON-V1 str', 'spaces'],
|
|
'cmd': 'message',
|
|
},
|
|
{
|
|
'args': ['ASDF', 'fff', 'sss', ' SPACES !!! '],
|
|
'cmd': 'set',
|
|
},
|
|
{
|
|
'args': ['FOO', '42'],
|
|
'cmd': 'set',
|
|
},
|
|
{
|
|
'args': ['BAR', ' space in string!'],
|
|
'cmd': 'set',
|
|
},
|
|
{
|
|
'args': msg_args,
|
|
'cmd': 'message',
|
|
'frame': 3 if expand else 2,
|
|
'global_frame': 3 if expand else 2
|
|
},
|
|
{
|
|
'args': ['STATUS', 'nested global_frame'],
|
|
'cmd': 'message',
|
|
'frame': 3,
|
|
'global_frame': 6 if expand else 5
|
|
}
|
|
]
|
|
|
|
with open(trace_file, 'r') as fp:
|
|
# Check for version (must be the first document)
|
|
vers = json.loads(fp.readline())
|
|
assert sorted(vers.keys()) == ['version']
|
|
assert sorted(vers['version'].keys()) == ['major', 'minor']
|
|
assert vers['version']['major'] == 1
|
|
assert vers['version']['minor'] == 2
|
|
|
|
for i in fp.readlines():
|
|
line = json.loads(i)
|
|
assert sorted(line.keys()) == ['args', 'cmd', 'file', 'frame', 'global_frame','line', 'time']
|
|
assert isinstance(line['args'], list)
|
|
assert isinstance(line['cmd'], unicode)
|
|
assert isinstance(line['file'], unicode)
|
|
assert isinstance(line['frame'], int)
|
|
assert isinstance(line['global_frame'], int)
|
|
assert isinstance(line['line'], int)
|
|
assert isinstance(line['time'], float)
|
|
|
|
for j in required_traces:
|
|
# Compare the subset of required keys with line
|
|
if {k: line[k] for k in j} == j:
|
|
required_traces.remove(j)
|
|
|
|
assert not required_traces
|