blob: 40a62b8836a202b30855136508c162adc18e1baa [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
13import tempfile
14
15TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..')))
16REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..'))
Tamas Kenezfc34cd82017-07-13 12:43:57 +020017MEMORY_USE_TMP_FILE = 'memory_use.tmp'
Tamas Kenez02bff032017-07-18 12:13:58 +020018DEX_SEGMENTS_JAR = os.path.join(REPO_ROOT, 'build', 'libs',
19 'dexsegments.jar')
20DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)')
Rico Wind74fab302017-10-02 07:25:33 +020021LIBS = os.path.join(REPO_ROOT, 'build', 'libs')
Mads Agerac794132017-11-09 11:38:45 +010022MAVEN_ZIP = os.path.join(LIBS, 'r8.zip')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020023
24D8 = 'd8'
25R8 = 'r8'
26COMPATDX = 'compatdx'
27COMPATPROGUARD = 'compatproguard'
28
Rico Wind74fab302017-10-02 07:25:33 +020029D8_JAR = os.path.join(LIBS, 'd8.jar')
30R8_JAR = os.path.join(LIBS, 'r8.jar')
31COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020032COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar')
Mads Ager418d1ca2017-05-22 09:35:49 +020033
34def PrintCmd(s):
35 if type(s) is list:
36 s = ' '.join(s)
37 print 'Running: %s' % s
38 # I know this will hit os on windows eventually if we don't do this.
39 sys.stdout.flush()
40
Rico Windf80f5a22017-06-16 09:15:57 +020041def IsWindows():
42 return os.name == 'nt'
43
Mads Ager418d1ca2017-05-22 09:35:49 +020044def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'):
Rico Windf80f5a22017-06-16 09:15:57 +020045 suffix = '.bat' if IsWindows() else ''
46 download_script = 'download_from_google_storage%s' % suffix
47 cmd = [download_script, '-n', '-b', bucket, '-u', '-s',
Mads Ager418d1ca2017-05-22 09:35:49 +020048 sha1_file]
49 PrintCmd(cmd)
50 subprocess.check_call(cmd)
51
52def get_sha1(filename):
53 sha1 = hashlib.sha1()
54 with open(filename, 'rb') as f:
55 while True:
56 chunk = f.read(1024*1024)
57 if not chunk:
58 break
59 sha1.update(chunk)
60 return sha1.hexdigest()
61
Tamas Kenez971eec62017-05-24 11:08:40 +020062def makedirs_if_needed(path):
63 try:
64 os.makedirs(path)
65 except OSError:
66 if not os.path.isdir(path):
67 raise
68
Rico Windb4621c12017-08-28 12:48:53 +020069def upload_dir_to_cloud_storage(directory, destination):
Rico Winda94f01c2017-06-27 10:32:34 +020070 # Upload and make the content encoding right for viewing directly
Rico Winddce9c872017-08-14 14:49:04 +020071 cmd = ['gsutil.py', 'cp', '-z', 'html', '-a',
Rico Winda94f01c2017-06-27 10:32:34 +020072 'public-read', '-R', directory, destination]
73 PrintCmd(cmd)
74 subprocess.check_call(cmd)
75
Rico Windb4621c12017-08-28 12:48:53 +020076def upload_file_to_cloud_storage(source, destination):
77 cmd = ['gsutil.py', 'cp', '-a', 'public-read', source, destination]
78 PrintCmd(cmd)
79 subprocess.check_call(cmd)
80
Mads Ager418d1ca2017-05-22 09:35:49 +020081class TempDir(object):
82 def __init__(self, prefix=''):
83 self._temp_dir = None
84 self._prefix = prefix
85
86 def __enter__(self):
87 self._temp_dir = tempfile.mkdtemp(self._prefix)
88 return self._temp_dir
89
90 def __exit__(self, *_):
91 shutil.rmtree(self._temp_dir, ignore_errors=True)
92
93class ChangedWorkingDirectory(object):
94 def __init__(self, working_directory):
95 self._working_directory = working_directory
96
97 def __enter__(self):
98 self._old_cwd = os.getcwd()
Rico Windf80f5a22017-06-16 09:15:57 +020099 print 'Enter directory = ', self._working_directory
Mads Ager418d1ca2017-05-22 09:35:49 +0200100 os.chdir(self._working_directory)
101
102 def __exit__(self, *_):
Rico Windf80f5a22017-06-16 09:15:57 +0200103 print 'Enter directory = ', self._old_cwd
Mads Ager418d1ca2017-05-22 09:35:49 +0200104 os.chdir(self._old_cwd)
Tamas Kenez82efeb52017-06-12 13:56:22 +0200105
106# Reading Android CTS test_result.xml
107
108class CtsModule(object):
109 def __init__(self, module_name):
110 self.name = module_name
111
112class CtsTestCase(object):
113 def __init__(self, test_case_name):
114 self.name = test_case_name
115
116class CtsTest(object):
117 def __init__(self, test_name, outcome):
118 self.name = test_name
119 self.outcome = outcome
120
121# Generator yielding CtsModule, CtsTestCase or CtsTest from
122# reading through a CTS test_result.xml file.
123def read_cts_test_result(file_xml):
124 re_module = re.compile('<Module name="([^"]*)"')
125 re_test_case = re.compile('<TestCase name="([^"]*)"')
126 re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"')
127 with open(file_xml) as f:
128 for line in f:
129 m = re_module.search(line)
130 if m:
131 yield CtsModule(m.groups()[0])
132 continue
133 m = re_test_case.search(line)
134 if m:
135 yield CtsTestCase(m.groups()[0])
136 continue
137 m = re_test.search(line)
138 if m:
139 outcome = m.groups()[0]
Rico Windf80f5a22017-06-16 09:15:57 +0200140 assert outcome in ['fail', 'pass']
Tamas Kenez82efeb52017-06-12 13:56:22 +0200141 yield CtsTest(m.groups()[1], outcome == 'pass')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200142
143def grep_memoryuse(logfile):
144 re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)')
145 result = None
146 with open(logfile) as f:
147 for line in f:
148 m = re_vmhwm.search(line)
149 if m:
150 groups = m.groups()
151 s = len(groups)
152 if s >= 1:
153 result = int(groups[0])
154 if s >= 2:
155 unit = groups[1]
156 if unit == 'kB':
157 result *= 1024
158 elif unit != '':
159 raise Exception('Unrecognized unit in memory usage log: {}'
160 .format(unit))
161 if result is None:
162 raise Exception('No memory usage found in log: {}'.format(logfile))
Tamas Kenez02bff032017-07-18 12:13:58 +0200163 return result
164
165# Return a dictionary: {segment_name -> segments_size}
166def getDexSegmentSizes(dex_files):
167 assert len(dex_files) > 0
168 cmd = ['java', '-jar', DEX_SEGMENTS_JAR]
169 cmd.extend(dex_files)
170 PrintCmd(cmd)
171 output = subprocess.check_output(cmd)
172
173 matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output)
174
175 if matches is None or len(matches) == 0:
176 raise Exception('DexSegments failed to return any output for' \
177 ' these files: {}'.format(dex_files))
178
179 result = {}
180
181 for match in matches:
182 result[match[0]] = int(match[1])
183
184 return result
185
186def print_dexsegments(prefix, dex_files):
187 for segment_name, size in getDexSegmentSizes(dex_files).items():
188 print('{}-{}(CodeSize): {}'
189 .format(prefix, segment_name, size))
Tamas Kenez2cf47cf2017-07-25 10:22:52 +0200190
191# ensure that java version is 1.8.*-internal,
192# as opposed to e.g. 1.7* or 1.8.*-google-v7
193def check_java_version():
194 cmd= ['java', '-version']
195 output = subprocess.check_output(cmd, stderr = subprocess.STDOUT)
196 m = re.search('openjdk version "([^"]*)"', output)
197 if m is None:
198 raise Exception("Can't check java version: no version string in output"
199 " of 'java -version': '{}'".format(output))
200 version = m.groups(0)[0]
201 m = re.search('1[.]8[.].*-internal', version)
202 if m is None:
203 raise Exception("Incorrect java version, expected: '1.8.*-internal',"
204 " actual: {}".format(version))
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200205
206def verify_with_dex2oat(dex_file):
207
208 # dex2oat accepts non-existent dex files, check here instead
209 if not os.path.exists(dex_file):
210 raise Exception('Dex file not found: "{}"'.format(dex_file))
211
212 android_root_dir = os.path.join(TOOLS_DIR, 'linux', 'art', 'product',
213 'angler')
214 boot_art = os.path.join(android_root_dir, 'system', 'framework', 'boot.art')
215 dex2oat = os.path.join(TOOLS_DIR, 'linux', 'art', 'bin', 'dex2oat')
216
217 with TempDir() as temp:
218 oat_file = os.path.join(temp, 'all.oat')
219
220 cmd = [
221 dex2oat,
222 '--android-root=' + android_root_dir,
223 '--runtime-arg', '-Xnorelocate',
224 '--boot-image=' + boot_art,
225 '--dex-file=' + dex_file,
226 '--oat-file=' + oat_file,
227 '--instruction-set=arm64',
Sebastien Hertzae3a1212017-09-08 10:01:17 +0200228 '--compiler-filter=quicken'
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200229 ]
230
231 PrintCmd(cmd)
232 subprocess.check_call(cmd,
233 env = {"LD_LIBRARY_PATH":
234 os.path.join(TOOLS_DIR, 'linux', 'art', 'lib')}
235 )