blob: 97b63dbfcbe801861842af77735bd272a90e5463 [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)
181 if entry.startswith('.') or entry.endswith('~') or entry.endswith('.pyc'):
182 logging.debug('ignored ' + rel_path)
183 continue
184 if os.path.isfile(full_path):
185 condition = GetFileCondition(rel_path)
186 if condition == 'true':
187 file_list.append(rel_path)
188 elif condition == 'false':
189 logging.debug('excluded ' + rel_path)
190 continue
191 else:
192 conditional_list.append({
193 "condition": condition,
194 "path": rel_path,
195 });
196 elif os.path.isdir(full_path):
197 condition = GetDirCondition(rel_path)
198 if condition == 'true':
199 dir_list.append(rel_path + '/')
200 elif condition == 'false':
201 logging.debug('excluded ' + rel_path)
202 elif condition == 'expand':
203 expand_list.append(full_path)
204 else:
205 conditional_list.append({
206 "condition": condition,
207 "path": rel_path + '/',
208 });
209 else:
210 assert False
211 file_list.sort()
212 dir_list.sort()
213 WriteLists([file_list, dir_list], [conditional_list],
214 build_file, path_prefix)
215 return expand_list
216
217def WriteBuildFileHeader(build_file):
218 build_file.write(LICENSE)
219 build_file.write(DO_NOT_EDIT_WARNING)
220 build_file.write('import("//build/config/compiler/compiler.gni")\n\n')
221
222def WriteBuildFileBody(build_file, root_path, path_prefix):
223 build_file.write("""group("%s") {
224 testonly = true
225 data = []
226
227""" % TELEMETRY_SUPPORT_GROUP_NAME)
228
229 candidates = [root_path]
230 while len(candidates) > 0:
231 candidate = candidates.pop(0)
232 more = ProcessDir(root_path, candidate, build_file, path_prefix)
233 candidates.extend(more)
234
235 build_file.write("}")
236
237def GenerateBuildFile(root_path, output_path, chromium):
238 CHROMIUM_GROUP = 'group("telemetry_chrome_test_without_chrome")'
239 CATAPULT_PREFIX = '//third_party/catapult'
240 CATAPULT_GROUP_NAME = CATAPULT_PREFIX + ':' + TELEMETRY_SUPPORT_GROUP_NAME
241 TELEMETRY_SUPPORT_GROUP = 'group("%s")' % TELEMETRY_SUPPORT_GROUP_NAME
242 if chromium:
243 build_file = open(output_path, 'r+')
244 contents = build_file.readlines()
245 build_file.seek(0)
246 remove_telemetry_support_group = False
247 for line in contents:
248 if TELEMETRY_SUPPORT_GROUP in line:
249 # --chromium has already run once, so remove the previously inserted
250 # TELEMETRY_SUPPORT_GROUP so we could add an updated one.
251 remove_telemetry_support_group = True
252 continue
253 if remove_telemetry_support_group:
254 if line == '}\n':
255 remove_telemetry_support_group = False
256 continue
257 if CHROMIUM_GROUP in line:
258 WriteBuildFileBody(build_file, root_path, CATAPULT_PREFIX + '/')
259 build_file.write('\n')
260 elif CATAPULT_GROUP_NAME in line:
261 line = line.replace(CATAPULT_GROUP_NAME,
262 ':' + TELEMETRY_SUPPORT_GROUP_NAME)
263 build_file.write(line)
264 build_file.close()
265 else:
266 build_file = open(output_path, 'w')
267 WriteBuildFileHeader(build_file)
268 WriteBuildFileBody(build_file, root_path, None)
269 build_file.close()
270
271def CheckForChanges():
272 root_path = os.path.dirname(os.path.realpath(__file__))
273 temp_path = os.path.join(root_path, "TEMP.gn")
274 GenerateBuildFile(root_path, temp_path, chromium=False)
275
276 ref_path = os.path.join(root_path, "BUILD.gn")
277 if not os.path.exists(ref_path):
278 logging.error("Can't localte BUILD.gn!")
279 return False
280
281 temp_file = open(temp_path, 'r')
282 temp_content = temp_file.readlines()
283 temp_file.close()
284 os.remove(temp_path)
285 ref_file = open(ref_path, 'r')
286 ref_content = ref_file.readlines()
287 ref_file.close()
288
289 diff = difflib.unified_diff(temp_content, ref_content, fromfile=temp_path,
290 tofile=ref_path, lineterm='')
291 diff_data = []
292 for line in diff:
293 diff_data.append(line)
294 if len(diff_data) > 0:
295 logging.error('Diff found. Please rerun generate_telemetry_build.py.')
296 logging.debug('\n' + ''.join(diff_data))
297 return False
298 logging.debug('No diff found. Everything is good.')
299 return True
300
301
302def main(argv):
303 parser = optparse.OptionParser()
304 parser.add_option("-v", "--verbose", action="store_true", default=False,
305 help="print out debug information")
306 parser.add_option("-c", "--check", action="store_true", default=False,
307 help="generate a temporary build file and compare if it "
308 "is the same as the current BUILD.gn")
309 parser.add_option("--chromium", action="store_true", default=False,
310 help="generate the build file into chromium workspace. "
311 "This is for the purpose of running try jobs in Chrome.")
312 (options, _) = parser.parse_args(args=argv)
313 if options.verbose:
314 logging.basicConfig(level=logging.DEBUG)
315
316 if options.check:
317 CheckForChanges()
318 elif options.chromium:
319 root_path = os.path.dirname(os.path.realpath(__file__))
320 output_path = os.path.join(
321 root_path, "../../tools/perf/chrome_telemetry_build/BUILD.gn")
322 GenerateBuildFile(root_path, output_path, chromium=True)
323 else:
324 root_path = os.path.dirname(os.path.realpath(__file__))
325 output_path = os.path.join(root_path, "BUILD.gn")
326 GenerateBuildFile(root_path, output_path, chromium=False)
327
328if __name__ == '__main__':
329 sys.exit(main(sys.argv[1:]))