blob: e0ffa5c928069884ae48d1b7c15879ab765a3de8 [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001# Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
2# for details. All rights reserved. Use of this source code is governed by a
3# BSD-style license that can be found in the LICENSE file.
4
5# Different utility functions used accross scripts
6
7import hashlib
8import os
Tamas Kenez82efeb52017-06-12 13:56:22 +02009import re
Mads Ager418d1ca2017-05-22 09:35:49 +020010import shutil
11import subprocess
12import sys
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +020013import tarfile
Mads Ager418d1ca2017-05-22 09:35:49 +020014import tempfile
15
Rico Wind9d70f612018-08-31 09:17:43 +020016ANDROID_JAR = 'third_party/android_jar/lib-v{api}/android.jar'
Mads Ager418d1ca2017-05-22 09:35:49 +020017TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..')))
18REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..'))
Rico Wind6238f222018-10-03 10:36:10 +020019THIRD_PARTY = os.path.join(REPO_ROOT, 'third_party')
Tamas Kenezfc34cd82017-07-13 12:43:57 +020020MEMORY_USE_TMP_FILE = 'memory_use.tmp'
Tamas Kenez02bff032017-07-18 12:13:58 +020021DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)')
Mads Ager12a56bc2017-11-27 11:51:25 +010022BUILD = os.path.join(REPO_ROOT, 'build')
Ian Zerny0f5fc732018-11-15 14:34:41 +010023BUILD_DEPS_DIR = os.path.join(BUILD, 'deps')
24BUILD_MAIN_DIR = os.path.join(BUILD, 'classes', 'main')
25BUILD_TEST_DIR = os.path.join(BUILD, 'classes', 'test')
Mads Ager12a56bc2017-11-27 11:51:25 +010026LIBS = os.path.join(BUILD, 'libs')
27GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense')
Mads Agera4911eb2017-11-22 13:19:36 +010028SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020029
30D8 = 'd8'
31R8 = 'r8'
Tamas Kenez03ab76f2018-12-07 14:33:25 +010032R8LIB = 'r8lib'
Morten Krogh-Jespersene28db462019-01-09 13:32:15 +010033R8LIB_NO_DEPS = 'r8LibNoDeps'
Mads Agerb10c07f2017-11-27 13:25:52 +010034R8_SRC = 'sourceJar'
Søren Gjessedc9d8a22017-10-12 12:40:59 +020035COMPATDX = 'compatdx'
Tamas Kenez03ab76f2018-12-07 14:33:25 +010036COMPATDXLIB = 'compatdxlib'
Søren Gjessedc9d8a22017-10-12 12:40:59 +020037COMPATPROGUARD = 'compatproguard'
Tamas Kenez03ab76f2018-12-07 14:33:25 +010038COMPATPROGUARDLIB = 'compatproguardlib'
Søren Gjessedc9d8a22017-10-12 12:40:59 +020039
Rico Wind74fab302017-10-02 07:25:33 +020040D8_JAR = os.path.join(LIBS, 'd8.jar')
41R8_JAR = os.path.join(LIBS, 'r8.jar')
Tamas Kenez03ab76f2018-12-07 14:33:25 +010042R8LIB_JAR = os.path.join(LIBS, 'r8lib.jar')
Mads Agerb10c07f2017-11-27 13:25:52 +010043R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar')
Tamas Kenez03ab76f2018-12-07 14:33:25 +010044R8LIB_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8lib-exclude-deps.jar')
Tamas Kenez180be092018-12-05 15:23:06 +010045R8_FULL_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-full-exclude-deps.jar')
Rico Wind74fab302017-10-02 07:25:33 +020046COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar')
Tamas Kenez03ab76f2018-12-07 14:33:25 +010047COMPATDXLIB_JAR = os.path.join(LIBS, 'compatdxlib.jar')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020048COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar')
Tamas Kenez03ab76f2018-12-07 14:33:25 +010049COMPATPROGUARDLIB_JAR = os.path.join(LIBS, 'compatproguardlib.jar')
Mads Ager12a56bc2017-11-27 11:51:25 +010050MAVEN_ZIP = os.path.join(LIBS, 'r8.zip')
51GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE')
Mathias Rav3fb4a3a2018-05-29 15:41:36 +020052RT_JAR = os.path.join(REPO_ROOT, 'third_party/openjdk/openjdk-rt-1.8/rt.jar')
Mathias Ravb46dc002018-06-06 09:37:11 +020053R8LIB_KEEP_RULES = os.path.join(REPO_ROOT, 'src/main/keep.txt')
Mads Ager418d1ca2017-05-22 09:35:49 +020054
55def PrintCmd(s):
56 if type(s) is list:
57 s = ' '.join(s)
58 print 'Running: %s' % s
59 # I know this will hit os on windows eventually if we don't do this.
60 sys.stdout.flush()
61
Rico Windf80f5a22017-06-16 09:15:57 +020062def IsWindows():
63 return os.name == 'nt'
64
Jean-Marie Henaffe4e36d12018-04-05 10:33:50 +020065def DownloadFromX20(sha1_file):
66 download_script = os.path.join(REPO_ROOT, 'tools', 'download_from_x20.py')
67 cmd = [download_script, sha1_file]
68 PrintCmd(cmd)
69 subprocess.check_call(cmd)
70
Mads Ager418d1ca2017-05-22 09:35:49 +020071def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'):
Rico Windf80f5a22017-06-16 09:15:57 +020072 suffix = '.bat' if IsWindows() else ''
73 download_script = 'download_from_google_storage%s' % suffix
74 cmd = [download_script, '-n', '-b', bucket, '-u', '-s',
Mads Ager418d1ca2017-05-22 09:35:49 +020075 sha1_file]
76 PrintCmd(cmd)
77 subprocess.check_call(cmd)
78
79def get_sha1(filename):
80 sha1 = hashlib.sha1()
81 with open(filename, 'rb') as f:
82 while True:
83 chunk = f.read(1024*1024)
84 if not chunk:
85 break
86 sha1.update(chunk)
87 return sha1.hexdigest()
88
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +020089def get_HEAD_sha1():
90 cmd = ['git', 'rev-parse', 'HEAD']
91 PrintCmd(cmd)
92 with ChangedWorkingDirectory(REPO_ROOT):
93 return subprocess.check_output(cmd).strip()
94
Tamas Kenez971eec62017-05-24 11:08:40 +020095def makedirs_if_needed(path):
96 try:
97 os.makedirs(path)
98 except OSError:
99 if not os.path.isdir(path):
100 raise
101
Rico Wind800fd712018-09-24 11:29:33 +0200102def upload_dir_to_cloud_storage(directory, destination, is_html=False, public_read=True):
Rico Winda94f01c2017-06-27 10:32:34 +0200103 # Upload and make the content encoding right for viewing directly
Rico Windd0d88cf2018-02-09 09:46:11 +0100104 cmd = ['gsutil.py', 'cp']
105 if is_html:
106 cmd += ['-z', 'html']
Rico Wind800fd712018-09-24 11:29:33 +0200107 if public_read:
108 cmd += ['-a', 'public-read']
109 cmd += ['-R', directory, destination]
Rico Winda94f01c2017-06-27 10:32:34 +0200110 PrintCmd(cmd)
111 subprocess.check_call(cmd)
112
Rico Wind800fd712018-09-24 11:29:33 +0200113def upload_file_to_cloud_storage(source, destination, public_read=True):
114 cmd = ['gsutil.py', 'cp']
115 if public_read:
116 cmd += ['-a', 'public-read']
117 cmd += [source, destination]
Rico Windb4621c12017-08-28 12:48:53 +0200118 PrintCmd(cmd)
119 subprocess.check_call(cmd)
120
Rico Wind139eece2018-09-25 09:42:09 +0200121def delete_file_from_cloud_storage(destination):
122 cmd = ['gsutil.py', 'rm', destination]
123 PrintCmd(cmd)
124 subprocess.check_call(cmd)
125
Rico Wind4fd2dda2018-09-26 17:41:45 +0200126def ls_files_on_cloud_storage(destination):
127 cmd = ['gsutil.py', 'ls', destination]
128 PrintCmd(cmd)
129 return subprocess.check_output(cmd)
130
Rico Wind139eece2018-09-25 09:42:09 +0200131def cat_file_on_cloud_storage(destination, ignore_errors=False):
132 cmd = ['gsutil.py', 'cat', destination]
133 PrintCmd(cmd)
134 try:
135 return subprocess.check_output(cmd)
136 except subprocess.CalledProcessError as e:
137 if ignore_errors:
138 return ''
139 else:
140 raise e
141
142def file_exists_on_cloud_storage(destination):
143 cmd = ['gsutil.py', 'ls', destination]
144 PrintCmd(cmd)
145 return subprocess.call(cmd) == 0
146
Jean-Marie Henaff7a64eec2018-05-31 15:30:35 +0200147def download_file_from_cloud_storage(source, destination):
148 cmd = ['gsutil.py', 'cp', source, destination]
149 PrintCmd(cmd)
150 subprocess.check_call(cmd)
151
152def create_archive(name):
153 tarname = '%s.tar.gz' % name
154 with tarfile.open(tarname, 'w:gz') as tar:
155 tar.add(name)
156 return tarname
157
158def extract_dir(filename):
159 return filename[0:len(filename) - len('.tar.gz')]
160
161def unpack_archive(filename):
162 dest_dir = extract_dir(filename)
163 if os.path.exists(dest_dir):
164 print 'Deleting existing dir %s' % dest_dir
165 shutil.rmtree(dest_dir)
166 dirname = os.path.dirname(os.path.abspath(filename))
167 with tarfile.open(filename, 'r:gz') as tar:
168 tar.extractall(path=dirname)
169
Rico Wind1a29c4f2018-01-25 08:43:08 +0100170# Note that gcs is eventually consistent with regards to list operations.
171# This is not a problem in our case, but don't ever use this method
172# for synchronization.
173def cloud_storage_exists(destination):
174 cmd = ['gsutil.py', 'ls', destination]
175 PrintCmd(cmd)
176 exit_code = subprocess.call(cmd)
177 return exit_code == 0
178
Mads Ager418d1ca2017-05-22 09:35:49 +0200179class TempDir(object):
180 def __init__(self, prefix=''):
181 self._temp_dir = None
182 self._prefix = prefix
183
184 def __enter__(self):
185 self._temp_dir = tempfile.mkdtemp(self._prefix)
186 return self._temp_dir
187
188 def __exit__(self, *_):
189 shutil.rmtree(self._temp_dir, ignore_errors=True)
190
191class ChangedWorkingDirectory(object):
192 def __init__(self, working_directory):
193 self._working_directory = working_directory
194
195 def __enter__(self):
196 self._old_cwd = os.getcwd()
Rico Windf80f5a22017-06-16 09:15:57 +0200197 print 'Enter directory = ', self._working_directory
Mads Ager418d1ca2017-05-22 09:35:49 +0200198 os.chdir(self._working_directory)
199
200 def __exit__(self, *_):
Rico Windf80f5a22017-06-16 09:15:57 +0200201 print 'Enter directory = ', self._old_cwd
Mads Ager418d1ca2017-05-22 09:35:49 +0200202 os.chdir(self._old_cwd)
Tamas Kenez82efeb52017-06-12 13:56:22 +0200203
204# Reading Android CTS test_result.xml
205
206class CtsModule(object):
207 def __init__(self, module_name):
208 self.name = module_name
209
210class CtsTestCase(object):
211 def __init__(self, test_case_name):
212 self.name = test_case_name
213
214class CtsTest(object):
215 def __init__(self, test_name, outcome):
216 self.name = test_name
217 self.outcome = outcome
218
219# Generator yielding CtsModule, CtsTestCase or CtsTest from
220# reading through a CTS test_result.xml file.
221def read_cts_test_result(file_xml):
222 re_module = re.compile('<Module name="([^"]*)"')
223 re_test_case = re.compile('<TestCase name="([^"]*)"')
224 re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"')
225 with open(file_xml) as f:
226 for line in f:
227 m = re_module.search(line)
228 if m:
229 yield CtsModule(m.groups()[0])
230 continue
231 m = re_test_case.search(line)
232 if m:
233 yield CtsTestCase(m.groups()[0])
234 continue
235 m = re_test.search(line)
236 if m:
237 outcome = m.groups()[0]
Rico Windf80f5a22017-06-16 09:15:57 +0200238 assert outcome in ['fail', 'pass']
Tamas Kenez82efeb52017-06-12 13:56:22 +0200239 yield CtsTest(m.groups()[1], outcome == 'pass')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200240
241def grep_memoryuse(logfile):
242 re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)')
243 result = None
244 with open(logfile) as f:
245 for line in f:
246 m = re_vmhwm.search(line)
247 if m:
248 groups = m.groups()
249 s = len(groups)
250 if s >= 1:
251 result = int(groups[0])
252 if s >= 2:
253 unit = groups[1]
254 if unit == 'kB':
255 result *= 1024
256 elif unit != '':
257 raise Exception('Unrecognized unit in memory usage log: {}'
258 .format(unit))
259 if result is None:
260 raise Exception('No memory usage found in log: {}'.format(logfile))
Tamas Kenez02bff032017-07-18 12:13:58 +0200261 return result
262
263# Return a dictionary: {segment_name -> segments_size}
264def getDexSegmentSizes(dex_files):
265 assert len(dex_files) > 0
Mathias Rav9cf6a742018-05-22 09:34:45 +0200266 cmd = ['java', '-jar', R8_JAR, 'dexsegments']
Tamas Kenez02bff032017-07-18 12:13:58 +0200267 cmd.extend(dex_files)
268 PrintCmd(cmd)
269 output = subprocess.check_output(cmd)
270
271 matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output)
272
273 if matches is None or len(matches) == 0:
274 raise Exception('DexSegments failed to return any output for' \
275 ' these files: {}'.format(dex_files))
276
277 result = {}
278
279 for match in matches:
280 result[match[0]] = int(match[1])
281
282 return result
283
Rico Windc0b16382018-05-17 13:23:43 +0200284def get_maven_path(version):
285 return os.path.join('com', 'android', 'tools', 'r8', version)
286
Tamas Kenez02bff032017-07-18 12:13:58 +0200287def print_dexsegments(prefix, dex_files):
288 for segment_name, size in getDexSegmentSizes(dex_files).items():
289 print('{}-{}(CodeSize): {}'
290 .format(prefix, segment_name, size))
Tamas Kenez2cf47cf2017-07-25 10:22:52 +0200291
Mads Agerbc7b2ce2018-02-05 11:28:47 +0100292# Ensure that we are not benchmarking with a google jvm.
Tamas Kenez2cf47cf2017-07-25 10:22:52 +0200293def check_java_version():
294 cmd= ['java', '-version']
295 output = subprocess.check_output(cmd, stderr = subprocess.STDOUT)
296 m = re.search('openjdk version "([^"]*)"', output)
297 if m is None:
298 raise Exception("Can't check java version: no version string in output"
299 " of 'java -version': '{}'".format(output))
300 version = m.groups(0)[0]
Mads Agerbc7b2ce2018-02-05 11:28:47 +0100301 m = re.search('google', version)
302 if m is not None:
303 raise Exception("Do not use google JVM for benchmarking: " + version)
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200304
Rico Wind9d70f612018-08-31 09:17:43 +0200305def get_android_jar(api):
306 return os.path.join(REPO_ROOT, ANDROID_JAR.format(api=api))
Rico Windda6836e2018-12-07 12:32:03 +0100307
308def is_bot():
309 return 'BUILDBOT_BUILDERNAME' in os.environ