blob: 1510555b1f89194a31e5be63339974b14465702a [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 Moc54a6eb2018-08-29 10:06:31 -070046 "path": "common/node_runner",
Zhenyao Mobc2c0a92018-08-24 14:26:04 -070047 },
48 {
49 "path": "docs",
50 },
51 {
52 "path": "experimental",
53 },
54 {
Zhenyao Mo72712b02018-08-23 15:55:12 -070055 # needed for --chromium option; can remove once this CL lands.
56 "path": "generate_telemetry_build.py",
57 },
58 {
Zhenyao Mob18e8eb2018-08-28 16:43:56 -070059 "path": "telemetry/telemetry/data",
60 },
61 {
Zhenyao Mo72712b02018-08-23 15:55:12 -070062 "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)
Zhenyao Mob18e8eb2018-08-28 16:43:56 -0700174 entry_list.sort()
Zhenyao Mo72712b02018-08-23 15:55:12 -0700175 file_list = []
176 dir_list = []
177 conditional_list = []
178 expand_list = []
179 for entry in entry_list:
180 full_path = os.path.join(path, entry)
181 rel_path = os.path.relpath(full_path, root_path)
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700182 if (entry.startswith('.') or entry.endswith('~') or
183 entry.endswith('.pyc') or entry.endswith('#')):
Zhenyao Mo72712b02018-08-23 15:55:12 -0700184 logging.debug('ignored ' + rel_path)
185 continue
186 if os.path.isfile(full_path):
187 condition = GetFileCondition(rel_path)
188 if condition == 'true':
189 file_list.append(rel_path)
190 elif condition == 'false':
191 logging.debug('excluded ' + rel_path)
192 continue
193 else:
194 conditional_list.append({
195 "condition": condition,
196 "path": rel_path,
197 });
198 elif os.path.isdir(full_path):
199 condition = GetDirCondition(rel_path)
200 if condition == 'true':
201 dir_list.append(rel_path + '/')
202 elif condition == 'false':
203 logging.debug('excluded ' + rel_path)
204 elif condition == 'expand':
205 expand_list.append(full_path)
206 else:
207 conditional_list.append({
208 "condition": condition,
209 "path": rel_path + '/',
210 });
211 else:
212 assert False
Zhenyao Mo72712b02018-08-23 15:55:12 -0700213 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():
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700272 # Return 0 if no changes are detected; return 1 otherwise.
Zhenyao Mo72712b02018-08-23 15:55:12 -0700273 root_path = os.path.dirname(os.path.realpath(__file__))
274 temp_path = os.path.join(root_path, "TEMP.gn")
275 GenerateBuildFile(root_path, temp_path, chromium=False)
276
277 ref_path = os.path.join(root_path, "BUILD.gn")
278 if not os.path.exists(ref_path):
279 logging.error("Can't localte BUILD.gn!")
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700280 return 1
Zhenyao Mo72712b02018-08-23 15:55:12 -0700281
282 temp_file = open(temp_path, 'r')
283 temp_content = temp_file.readlines()
284 temp_file.close()
285 os.remove(temp_path)
286 ref_file = open(ref_path, 'r')
287 ref_content = ref_file.readlines()
288 ref_file.close()
289
290 diff = difflib.unified_diff(temp_content, ref_content, fromfile=temp_path,
291 tofile=ref_path, lineterm='')
292 diff_data = []
293 for line in diff:
294 diff_data.append(line)
295 if len(diff_data) > 0:
296 logging.error('Diff found. Please rerun generate_telemetry_build.py.')
297 logging.debug('\n' + ''.join(diff_data))
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700298 return 1
Zhenyao Mo72712b02018-08-23 15:55:12 -0700299 logging.debug('No diff found. Everything is good.')
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700300 return 0
Zhenyao Mo72712b02018-08-23 15:55:12 -0700301
302
303def main(argv):
304 parser = optparse.OptionParser()
305 parser.add_option("-v", "--verbose", action="store_true", default=False,
306 help="print out debug information")
307 parser.add_option("-c", "--check", action="store_true", default=False,
308 help="generate a temporary build file and compare if it "
309 "is the same as the current BUILD.gn")
310 parser.add_option("--chromium", action="store_true", default=False,
311 help="generate the build file into chromium workspace. "
312 "This is for the purpose of running try jobs in Chrome.")
313 (options, _) = parser.parse_args(args=argv)
314 if options.verbose:
315 logging.basicConfig(level=logging.DEBUG)
316
317 if options.check:
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700318 return CheckForChanges()
319 if options.chromium:
Zhenyao Mo72712b02018-08-23 15:55:12 -0700320 root_path = os.path.dirname(os.path.realpath(__file__))
321 output_path = os.path.join(
322 root_path, "../../tools/perf/chrome_telemetry_build/BUILD.gn")
323 GenerateBuildFile(root_path, output_path, chromium=True)
324 else:
325 root_path = os.path.dirname(os.path.realpath(__file__))
326 output_path = os.path.join(root_path, "BUILD.gn")
327 GenerateBuildFile(root_path, output_path, chromium=False)
Zhenyao Mo074cebc2018-08-27 11:31:05 -0700328 return 0
Zhenyao Mo72712b02018-08-23 15:55:12 -0700329
330if __name__ == '__main__':
331 sys.exit(main(sys.argv[1:]))