forked from pixie-io/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_vscode_tasks.py
254 lines (215 loc) · 8.37 KB
/
generate_vscode_tasks.py
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
# This file manages the generation of tasks.json (builds) and launch.json (debug commands).
# Simply run: python scripts/generate_vscode_tasks.py from the bazel workspace root.
import argparse
import json
import os
import subprocess
import sys
def getOutputBase():
return subprocess.check_output(
['bazel', 'info', 'output_base']).rstrip().decode("utf-8")
# Returns source file map. Some of these are only relevant for Mac or Linux, respectively,
# but they don't cause any negative side-effects so we leave them all in here.
def getSourceFileMap(output_base):
return {
'/proc/self/cwd/': '${workspaceFolder}/',
'{0}/execroot/pl/src/'.format(output_base): '${workspaceFolder}/src/',
'{0}/execroot/pl/src/'.format(output_base.replace('/private/', '/')):
'${workspaceFolder}/src/',
}
# Generates a path based on the Bazel test target.
def getPathFromTestTarget(target):
return target.replace('//', '').replace(':', '/')
# Generate task segment based on compilation and exec modes.
def generateTaskSegment(target, exec_mode, comp_mode, output_std, v_level):
if exec_mode not in ['build', 'test']:
raise ValueError('exec_mode must be either build or test')
if comp_mode not in ['dbg', 'opt']:
raise ValueError('comp_mode must be either dbg or opt')
args = []
args.append('{0}'.format(exec_mode))
args.append('--compilation_mode={0}'.format(comp_mode))
if output_std:
args.append("--test_output=all")
args.append("--action_env=\"GTEST_COLOR=1\"")
args.append("--action_env=\"GLOG_logtostderr=1\"")
args.append("--action_env=\"GLOG_colorlogtostderr=1\"")
args.append("--action_env=\"GLOG_log_prefix=0\"")
args.append("--action_env=\"GLOG_v={}\"".format(v_level))
args.append(target)
return {
'label': '(bazel:{0}:{1}) {2}'.format(comp_mode, exec_mode, target),
'type': 'shell',
'command': 'bazel',
'args': args,
'group': '{0}'.format(exec_mode),
}
def generateTaskSegments(target, output_std, v_level):
tasks = []
exec_mode_list = ['build', 'test']
for exec_mode in exec_mode_list:
for comp_mode in ['dbg', 'opt']:
tasks += [generateTaskSegment(target,
exec_mode, comp_mode, output_std, v_level)]
return tasks
def generateLaunchSegments(lldb_mode, target, output_base):
if lldb_mode:
return [{
"args": [],
"cwd": "${workspaceFolder}",
'name': '(lldb) Launch {0}'.format(target),
'preLaunchTask': '(bazel:dbg:build) {0}'.format(target),
'program': '{0}/bazel-bin/{1}'.format('${workspaceFolder}',
getPathFromTestTarget(target)),
"request": "launch",
"sourceMap": getSourceFileMap(output_base),
"type": "lldb"
}]
else:
return [{
'name': '(lldb-mi) Launch {0}'.format(target),
'type': 'cppdbg',
'request': 'launch',
'program': '{0}/bazel-bin/{1}'.format('${workspaceFolder}',
getPathFromTestTarget(target)),
'args': [],
'stopAtEntry': False,
'cwd': '${workspaceFolder}',
'environment': [],
'externalConsole': False,
'MIMode': 'lldb',
'preLaunchTask': '(bazel:dbg:build) {0}'.format(target),
'sourceFileMap': getSourceFileMap(output_base),
# This is required to get the debugger to work properly on macos.
'miDebuggerPath': 'lldb-mi',
}]
# Formats a directory target from a given path.
def format_directory_target(path):
if path[-1] != '/':
path += '/'
return path + "..."
# Combines path sections into a formatted directory target.
def split_path_format(path_sections):
return format_directory_target('//{}'.format('/'.join(path_sections)))
# Gets the parent directory targets for a given target path.
def get_all_parent_directories(target_path):
split_path = target_path.split('/')
if not (split_path[0] == '' and split_path[1] == ''):
return []
split_paths_content = split_path[2:]
new_paths = []
for i in range(0, len(split_paths_content) + 1):
new_paths.append(split_path_format(split_paths_content[:i]))
return new_paths
# Takes in the list of targets and adds all of the parent directories as targets.
def get_build_directories(targets):
seen_test_dirs = set()
for t in targets:
path_target_split = t.split(':')
if len(path_target_split) != 2:
continue
path, target = path_target_split
seen_test_dirs.update(get_all_parent_directories(path))
return seen_test_dirs
def main():
parser = argparse.ArgumentParser(description='Generate files for vscode.')
parser.add_argument('--lldb', action='store_true',
help='Generate for LLDB (defaults to vscode mode)', default=False)
parser.add_argument('--all_output', action='store_true',
help='Output all of the output', default=False)
parser.add_argument(
'--v', help='Verbosity level (if supported) of the test.', default=0, type=int)
parsed = parser.parse_args(sys.argv[1:])
if parsed.lldb:
print('In LLDB mode')
targets_str = subprocess.check_output(
['bazel', 'query', 'kind(\'cc_test rule\', //...)']).decode("utf-8")
output_base = getOutputBase()
task_list = []
launch_list = []
targets = targets_str.split('\n')
for target in targets:
if target != '':
task_list += generateTaskSegments(target,
parsed.all_output, parsed.v)
launch_list += generateLaunchSegments(
parsed.lldb, target, output_base)
# directory targets don't have a launch segment.
directory_targets = get_build_directories(targets)
for target in directory_targets:
if target != '':
task_list += generateTaskSegments(target,
parsed.all_output, parsed.v)
# Task to regnerate the file.
task_list += [{
'label': 'generate tasks/launch json files (lldb)',
'type': 'shell',
'command': 'python',
'args': [
'${workspaceFolder}/scripts/generate_vscode_tasks.py',
'--lldb',
'--all_output',
],
'group': 'build'
}]
task_list += [{
'label': 'generate tasks/launch json files',
'type': 'shell',
'command': 'python',
'args': [
'${workspaceFolder}/scripts/generate_vscode_tasks.py',
],
'group': 'build'
}]
task_list += [{
'label': 'generate tasks/launch json files with output, v=1',
'type': 'shell',
'command': 'python',
'args': [
'${workspaceFolder}/scripts/generate_vscode_tasks.py',
'--all_output',
'--v=1'
],
'group': 'build'
}]
task_list += [{
'label': 'generate tasks/launch json files with output, v=2',
'type': 'shell',
'command': 'python',
'args': [
'${workspaceFolder}/scripts/generate_vscode_tasks.py',
'--all_output',
'--v=2'
],
'group': 'build'
}]
# Task to generate all bazel targets.
task_list += [{
'label': '(bazel:{0}:build) all'.format(comp_mode),
'type': 'shell',
'command': 'bazel',
'args': [
'build',
'--compilation_mode={0}'.format(comp_mode),
'//...',
'--',
'-//src/ui/...',
],
'group': 'build',
} for comp_mode in ['opt', 'dbg']]
tasks = {
'version': '2.0.0',
'tasks': task_list,
}
launch = {
'version': '0.2.0',
'configurations': launch_list,
}
workspace_root = subprocess.check_output(
['bazel', 'info', 'workspace']).rstrip().decode("utf-8")
with open(os.path.join(workspace_root, '.vscode/launch.json'), 'w') as launch_file:
launch_file.write(json.dumps(launch, indent=4, sort_keys=True))
with open(os.path.join(workspace_root, '.vscode/tasks.json'), 'w') as task_file:
task_file.write(json.dumps(tasks, indent=4, sort_keys=True))
if __name__ == '__main__':
main()