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