blob: 05307cd8873ce46e0c1114d570259d7098bae316 [file] [log] [blame]
Zhenyao Mo72712b02018-08-23 15:55:12 -07001# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Generate BUILD.gn to define all data required to run telemetry test.
6
7If a file/folder of large size is added to catapult but is not needed to run
8telemetry test, then it should be added to the EXCLUDED_PATHS below and rerun
9this script.
10
11This script can also run with --check to see if it needs to rerun to update
12BUILD.gn.
13
14This script can also run with --chromium and rewrite the chromium file
15 //tools/perf/chrome_telemetry_build/BUILD.gn
16This is for the purpose of running try jobs in chromium.
17
18"""
19
20import difflib
21import logging
22import os
23import optparse
24import sys
25
26LICENSE = """# Copyright 2018 The Chromium Authors. All rights reserved.
27# Use of this source code is governed by a BSD-style license that can be
28# found in the LICENSE file.
29
30"""
31
32DO_NOT_EDIT_WARNING = """# This file is auto-generated from
33# //third_party/catapult/generated_telemetry_build.py
34# DO NOT EDIT!
35
36"""
37
38TELEMETRY_SUPPORT_GROUP_NAME = 'telemetry_chrome_test_support'
39
40EXCLUDED_PATHS = [
41 {
42 # needed for --chromium option; can remove once this CL lands.
43 "path": "BUILD.gn",
44 },
45 {
Zhenyao Mobc2c0a92018-08-24 14:26:04 -070046 "path": "common/node_runner/node_runner/bin",
47 },
48 {
49 "path": "common/node_runner/node_runner/node_modules",
50 },
51 {
52 "path": "docs",
53 },
54 {
55 "path": "experimental",
56 },
57 {
Zhenyao Mo72712b02018-08-23 15:55:12 -070058 # needed for --chromium option; can remove once this CL lands.
59 "path": "generate_telemetry_build.py",
60 },
61 {
62 "path": "telemetry/telemetry/bin",
63 },
64 {
65 "path": "telemetry/telemetry/internal/bin",
66 },
67 {
68 # needed for --check option
69 "path": "TEMP.gn",
70 },
71 {
72 "path": "third_party/google-endpoints",
73 },
74 {
75 "path": "third_party/Paste",
76 },
77 {
78 "path": "third_party/polymer2",
79 },
80 {
81 "path": "third_party/vinn/third_party/v8/linux/arm",
82 "condition": "is_chromeos",
83 },
84 {
85 "path": "third_party/vinn/third_party/v8/linux/mips",
86 "condition": "is_chromeos",
87 },
88 {
89 "path": "third_party/vinn/third_party/v8/linux/mips64",
90 "condition": "is_chromeos",
91 },
92 {
93 "path": "third_party/vinn/third_party/v8/linux/x86_64",
94 "condition": "is_linux || is_android",
95 },
96 {
97 "path": "third_party/vinn/third_party/v8/mac",
98 "condition": "is_mac",
99 },
100 {
101 "path": "third_party/vinn/third_party/v8/win",
102 "condition": "is_win",
103 },
104 {
105 "path": "tracing/test_data",
106 },
107]
108
109def GetFileCondition(rel_path):
110 # Return 'true' if the file should be included; return 'false' if it should
111 # be excluded; return a condition string if it should only be included if
112 # the condition is true.
113 processed_rel_path = rel_path.replace('\\', '/')
114 for exclusion in EXCLUDED_PATHS:
115 assert 'path' in exclusion
116 if exclusion['path'] == processed_rel_path:
117 if 'condition' in exclusion:
118 return exclusion['condition']
119 else:
120 return 'false'
121 return 'true'
122
123def GetDirCondition(rel_path):
124 # Return 'true' if the dir should be included; return 'false' if it should
125 # be excluded; return a condition string if it should only be included if
126 # the condition is true; return 'expand' if some files or sub-dirs under it
127 # are excluded or conditionally included, so the parser needs to go inside
128 # the dir and process further.
129 processed_rel_path = rel_path.replace('\\', '/')
130 for exclusion in EXCLUDED_PATHS:
131 assert 'path' in exclusion
132 if exclusion['path'] == processed_rel_path:
133 if 'condition' in exclusion:
134 return exclusion['condition']
135 else:
136 return 'false'
137 elif exclusion['path'].startswith(processed_rel_path + '/'):
138 return 'expand'
139 return 'true'
140
141def WriteLists(lists, conditional_lists, build_file, path_prefix):
142 first_entry = True
143 for path_list in lists:
144 for path in path_list:
145 path = path.replace('\\', '/')
146 if path_prefix:
147 path = path_prefix + path
148 if first_entry:
149 build_file.write(' data += [\n')
150 first_entry = False
151 build_file.write(' "%s",\n' % path)
152 if not first_entry:
153 build_file.write(' ]\n\n')
154 for conditional_list in conditional_lists:
155 for entry in conditional_list:
156 assert 'path' in entry
157 assert 'condition' in entry
158 path = entry['path'].replace('\\', '/')
159 if path_prefix:
160 path = path_prefix + path
161 build_file.write(""" if (%s) {
162 data += [ "%s" ]
163 }
164
165""" % (entry['condition'], path))
166
167def ProcessDir(root_path, path, build_file, path_prefix):
168 # Write all dirs and files directly under |path| unless they are excluded
169 # or need to be processed further because some of their children are excldued
170 # or conditionally included.
171 # Return a list of dirs that needs to processed further.
172 logging.debug('GenerateList for ' + path)
173 entry_list = os.listdir(path)
174 file_list = []
175 dir_list = []
176 conditional_list = []
177 expand_list = []
178 for entry in entry_list:
179 full_path = os.path.join(path, entry)
180 rel_path = os.path.relpath(full_path, root_path)
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700181 if (entry.startswith('.') or entry.endswith('~') or
182 entry.endswith('.pyc') or entry.endswith('#')):
Zhenyao Mo72712b02018-08-23 15:55:12 -0700183 logging.debug('ignored ' + rel_path)
184 continue
185 if os.path.isfile(full_path):
186 condition = GetFileCondition(rel_path)
187 if condition == 'true':
188 file_list.append(rel_path)
189 elif condition == 'false':
190 logging.debug('excluded ' + rel_path)
191 continue
192 else:
193 conditional_list.append({
194 "condition": condition,
195 "path": rel_path,
196 });
197 elif os.path.isdir(full_path):
198 condition = GetDirCondition(rel_path)
199 if condition == 'true':
200 dir_list.append(rel_path + '/')
201 elif condition == 'false':
202 logging.debug('excluded ' + rel_path)
203 elif condition == 'expand':
204 expand_list.append(full_path)
205 else:
206 conditional_list.append({
207 "condition": condition,
208 "path": rel_path + '/',
209 });
210 else:
211 assert False
212 file_list.sort()
213 dir_list.sort()
214 WriteLists([file_list, dir_list], [conditional_list],
215 build_file, path_prefix)
216 return expand_list
217
218def WriteBuildFileHeader(build_file):
219 build_file.write(LICENSE)
220 build_file.write(DO_NOT_EDIT_WARNING)
221 build_file.write('import("//build/config/compiler/compiler.gni")\n\n')
222
223def WriteBuildFileBody(build_file, root_path, path_prefix):
224 build_file.write("""group("%s") {
225 testonly = true
226 data = []
227
228""" % TELEMETRY_SUPPORT_GROUP_NAME)
229
230 candidates = [root_path]
231 while len(candidates) > 0:
232 candidate = candidates.pop(0)
233 more = ProcessDir(root_path, candidate, build_file, path_prefix)
234 candidates.extend(more)
235
236 build_file.write("}")
237
238def GenerateBuildFile(root_path, output_path, chromium):
239 CHROMIUM_GROUP = 'group("telemetry_chrome_test_without_chrome")'
240 CATAPULT_PREFIX = '//third_party/catapult'
241 CATAPULT_GROUP_NAME = CATAPULT_PREFIX + ':' + TELEMETRY_SUPPORT_GROUP_NAME
242 TELEMETRY_SUPPORT_GROUP = 'group("%s")' % TELEMETRY_SUPPORT_GROUP_NAME
243 if chromium:
244 build_file = open(output_path, 'r+')
245 contents = build_file.readlines()
246 build_file.seek(0)
247 remove_telemetry_support_group = False
248 for line in contents:
249 if TELEMETRY_SUPPORT_GROUP in line:
250 # --chromium has already run once, so remove the previously inserted
251 # TELEMETRY_SUPPORT_GROUP so we could add an updated one.
252 remove_telemetry_support_group = True
253 continue
254 if remove_telemetry_support_group:
255 if line == '}\n':
256 remove_telemetry_support_group = False
257 continue
258 if CHROMIUM_GROUP in line:
259 WriteBuildFileBody(build_file, root_path, CATAPULT_PREFIX + '/')
260 build_file.write('\n')
261 elif CATAPULT_GROUP_NAME in line:
262 line = line.replace(CATAPULT_GROUP_NAME,
263 ':' + TELEMETRY_SUPPORT_GROUP_NAME)
264 build_file.write(line)
265 build_file.close()
266 else:
267 build_file = open(output_path, 'w')
268 WriteBuildFileHeader(build_file)
269 WriteBuildFileBody(build_file, root_path, None)
270 build_file.close()
271
272def CheckForChanges():
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700273 # Return 0 if no changes are detected; return 1 otherwise.
Zhenyao Mo72712b02018-08-23 15:55:12 -0700274 root_path = os.path.dirname(os.path.realpath(__file__))
275 temp_path = os.path.join(root_path, "TEMP.gn")
276 GenerateBuildFile(root_path, temp_path, chromium=False)
277
278 ref_path = os.path.join(root_path, "BUILD.gn")
279 if not os.path.exists(ref_path):
280 logging.error("Can't localte BUILD.gn!")
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700281 return 1
Zhenyao Mo72712b02018-08-23 15:55:12 -0700282
283 temp_file = open(temp_path, 'r')
284 temp_content = temp_file.readlines()
285 temp_file.close()
286 os.remove(temp_path)
287 ref_file = open(ref_path, 'r')
288 ref_content = ref_file.readlines()
289 ref_file.close()
290
291 diff = difflib.unified_diff(temp_content, ref_content, fromfile=temp_path,
292 tofile=ref_path, lineterm='')
293 diff_data = []
294 for line in diff:
295 diff_data.append(line)
296 if len(diff_data) > 0:
297 logging.error('Diff found. Please rerun generate_telemetry_build.py.')
298 logging.debug('\n' + ''.join(diff_data))
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700299 return 1
Zhenyao Mo72712b02018-08-23 15:55:12 -0700300 logging.debug('No diff found. Everything is good.')
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700301 return 0
Zhenyao Mo72712b02018-08-23 15:55:12 -0700302
303
304def main(argv):
305 parser = optparse.OptionParser()
306 parser.add_option("-v", "--verbose", action="store_true", default=False,
307 help="print out debug information")
308 parser.add_option("-c", "--check", action="store_true", default=False,
309 help="generate a temporary build file and compare if it "
310 "is the same as the current BUILD.gn")
311 parser.add_option("--chromium", action="store_true", default=False,
312 help="generate the build file into chromium workspace. "
313 "This is for the purpose of running try jobs in Chrome.")
314 (options, _) = parser.parse_args(args=argv)
315 if options.verbose:
316 logging.basicConfig(level=logging.DEBUG)
317
318 if options.check:
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700319 return CheckForChanges()
320 if options.chromium:
Zhenyao Mo72712b02018-08-23 15:55:12 -0700321 root_path = os.path.dirname(os.path.realpath(__file__))
322 output_path = os.path.join(
323 root_path, "../../tools/perf/chrome_telemetry_build/BUILD.gn")
324 GenerateBuildFile(root_path, output_path, chromium=True)
325 else:
326 root_path = os.path.dirname(os.path.realpath(__file__))
327 output_path = os.path.join(root_path, "BUILD.gn")
328 GenerateBuildFile(root_path, output_path, chromium=False)
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700329 return 0
Zhenyao Mo72712b02018-08-23 15:55:12 -0700330
331if __name__ == '__main__':
332 sys.exit(main(sys.argv[1:]))