blob: 4b9600bc6e265cebc2c96ef1ba75be5210c0a6a2 [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]+)')
Mads Ager12a56bc2017-11-27 11:51:25 +010021BUILD = os.path.join(REPO_ROOT, 'build')
22LIBS = os.path.join(BUILD, 'libs')
23GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense')
Mads Agera4911eb2017-11-22 13:19:36 +010024SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020025
26D8 = 'd8'
27R8 = 'r8'
Mads Agerb10c07f2017-11-27 13:25:52 +010028R8_SRC = 'sourceJar'
Søren Gjessedc9d8a22017-10-12 12:40:59 +020029COMPATDX = 'compatdx'
30COMPATPROGUARD = 'compatproguard'
31
Rico Wind74fab302017-10-02 07:25:33 +020032D8_JAR = os.path.join(LIBS, 'd8.jar')
33R8_JAR = os.path.join(LIBS, 'r8.jar')
Mads Agerb10c07f2017-11-27 13:25:52 +010034R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar')
Mads Ager0bd1ebd2017-11-22 13:40:21 +010035R8_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-exclude-deps.jar')
Rico Wind74fab302017-10-02 07:25:33 +020036COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020037COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar')
Mads Ager12a56bc2017-11-27 11:51:25 +010038MAVEN_ZIP = os.path.join(LIBS, 'r8.zip')
39GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE')
Mads Ager418d1ca2017-05-22 09:35:49 +020040
41def PrintCmd(s):
42 if type(s) is list:
43 s = ' '.join(s)
44 print 'Running: %s' % s
45 # I know this will hit os on windows eventually if we don't do this.
46 sys.stdout.flush()
47
Rico Windf80f5a22017-06-16 09:15:57 +020048def IsWindows():
49 return os.name == 'nt'
50
Mads Ager418d1ca2017-05-22 09:35:49 +020051def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'):
Rico Windf80f5a22017-06-16 09:15:57 +020052 suffix = '.bat' if IsWindows() else ''
53 download_script = 'download_from_google_storage%s' % suffix
54 cmd = [download_script, '-n', '-b', bucket, '-u', '-s',
Mads Ager418d1ca2017-05-22 09:35:49 +020055 sha1_file]
56 PrintCmd(cmd)
57 subprocess.check_call(cmd)
58
59def get_sha1(filename):
60 sha1 = hashlib.sha1()
61 with open(filename, 'rb') as f:
62 while True:
63 chunk = f.read(1024*1024)
64 if not chunk:
65 break
66 sha1.update(chunk)
67 return sha1.hexdigest()
68
Tamas Kenez971eec62017-05-24 11:08:40 +020069def makedirs_if_needed(path):
70 try:
71 os.makedirs(path)
72 except OSError:
73 if not os.path.isdir(path):
74 raise
75
Rico Windb4621c12017-08-28 12:48:53 +020076def upload_dir_to_cloud_storage(directory, destination):
Rico Winda94f01c2017-06-27 10:32:34 +020077 # Upload and make the content encoding right for viewing directly
Rico Winddce9c872017-08-14 14:49:04 +020078 cmd = ['gsutil.py', 'cp', '-z', 'html', '-a',
Rico Winda94f01c2017-06-27 10:32:34 +020079 'public-read', '-R', directory, destination]
80 PrintCmd(cmd)
81 subprocess.check_call(cmd)
82
Rico Windb4621c12017-08-28 12:48:53 +020083def upload_file_to_cloud_storage(source, destination):
84 cmd = ['gsutil.py', 'cp', '-a', 'public-read', source, destination]
85 PrintCmd(cmd)
86 subprocess.check_call(cmd)
87
Mads Ager418d1ca2017-05-22 09:35:49 +020088class TempDir(object):
89 def __init__(self, prefix=''):
90 self._temp_dir = None
91 self._prefix = prefix
92
93 def __enter__(self):
94 self._temp_dir = tempfile.mkdtemp(self._prefix)
95 return self._temp_dir
96
97 def __exit__(self, *_):
98 shutil.rmtree(self._temp_dir, ignore_errors=True)
99
100class ChangedWorkingDirectory(object):
101 def __init__(self, working_directory):
102 self._working_directory = working_directory
103
104 def __enter__(self):
105 self._old_cwd = os.getcwd()
Rico Windf80f5a22017-06-16 09:15:57 +0200106 print 'Enter directory = ', self._working_directory
Mads Ager418d1ca2017-05-22 09:35:49 +0200107 os.chdir(self._working_directory)
108
109 def __exit__(self, *_):
Rico Windf80f5a22017-06-16 09:15:57 +0200110 print 'Enter directory = ', self._old_cwd
Mads Ager418d1ca2017-05-22 09:35:49 +0200111 os.chdir(self._old_cwd)
Tamas Kenez82efeb52017-06-12 13:56:22 +0200112
113# Reading Android CTS test_result.xml
114
115class CtsModule(object):
116 def __init__(self, module_name):
117 self.name = module_name
118
119class CtsTestCase(object):
120 def __init__(self, test_case_name):
121 self.name = test_case_name
122
123class CtsTest(object):
124 def __init__(self, test_name, outcome):
125 self.name = test_name
126 self.outcome = outcome
127
128# Generator yielding CtsModule, CtsTestCase or CtsTest from
129# reading through a CTS test_result.xml file.
130def read_cts_test_result(file_xml):
131 re_module = re.compile('<Module name="([^"]*)"')
132 re_test_case = re.compile('<TestCase name="([^"]*)"')
133 re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"')
134 with open(file_xml) as f:
135 for line in f:
136 m = re_module.search(line)
137 if m:
138 yield CtsModule(m.groups()[0])
139 continue
140 m = re_test_case.search(line)
141 if m:
142 yield CtsTestCase(m.groups()[0])
143 continue
144 m = re_test.search(line)
145 if m:
146 outcome = m.groups()[0]
Rico Windf80f5a22017-06-16 09:15:57 +0200147 assert outcome in ['fail', 'pass']
Tamas Kenez82efeb52017-06-12 13:56:22 +0200148 yield CtsTest(m.groups()[1], outcome == 'pass')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200149
150def grep_memoryuse(logfile):
151 re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)')
152 result = None
153 with open(logfile) as f:
154 for line in f:
155 m = re_vmhwm.search(line)
156 if m:
157 groups = m.groups()
158 s = len(groups)
159 if s >= 1:
160 result = int(groups[0])
161 if s >= 2:
162 unit = groups[1]
163 if unit == 'kB':
164 result *= 1024
165 elif unit != '':
166 raise Exception('Unrecognized unit in memory usage log: {}'
167 .format(unit))
168 if result is None:
169 raise Exception('No memory usage found in log: {}'.format(logfile))
Tamas Kenez02bff032017-07-18 12:13:58 +0200170 return result
171
172# Return a dictionary: {segment_name -> segments_size}
173def getDexSegmentSizes(dex_files):
174 assert len(dex_files) > 0
175 cmd = ['java', '-jar', DEX_SEGMENTS_JAR]
176 cmd.extend(dex_files)
177 PrintCmd(cmd)
178 output = subprocess.check_output(cmd)
179
180 matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output)
181
182 if matches is None or len(matches) == 0:
183 raise Exception('DexSegments failed to return any output for' \
184 ' these files: {}'.format(dex_files))
185
186 result = {}
187
188 for match in matches:
189 result[match[0]] = int(match[1])
190
191 return result
192
193def print_dexsegments(prefix, dex_files):
194 for segment_name, size in getDexSegmentSizes(dex_files).items():
195 print('{}-{}(CodeSize): {}'
196 .format(prefix, segment_name, size))
Tamas Kenez2cf47cf2017-07-25 10:22:52 +0200197
198# ensure that java version is 1.8.*-internal,
199# as opposed to e.g. 1.7* or 1.8.*-google-v7
200def check_java_version():
201 cmd= ['java', '-version']
202 output = subprocess.check_output(cmd, stderr = subprocess.STDOUT)
203 m = re.search('openjdk version "([^"]*)"', output)
204 if m is None:
205 raise Exception("Can't check java version: no version string in output"
206 " of 'java -version': '{}'".format(output))
207 version = m.groups(0)[0]
208 m = re.search('1[.]8[.].*-internal', version)
209 if m is None:
210 raise Exception("Incorrect java version, expected: '1.8.*-internal',"
211 " actual: {}".format(version))
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200212
213def verify_with_dex2oat(dex_file):
214
215 # dex2oat accepts non-existent dex files, check here instead
216 if not os.path.exists(dex_file):
217 raise Exception('Dex file not found: "{}"'.format(dex_file))
218
219 android_root_dir = os.path.join(TOOLS_DIR, 'linux', 'art', 'product',
220 'angler')
221 boot_art = os.path.join(android_root_dir, 'system', 'framework', 'boot.art')
222 dex2oat = os.path.join(TOOLS_DIR, 'linux', 'art', 'bin', 'dex2oat')
223
224 with TempDir() as temp:
225 oat_file = os.path.join(temp, 'all.oat')
226
227 cmd = [
228 dex2oat,
229 '--android-root=' + android_root_dir,
230 '--runtime-arg', '-Xnorelocate',
231 '--boot-image=' + boot_art,
232 '--dex-file=' + dex_file,
233 '--oat-file=' + oat_file,
234 '--instruction-set=arm64',
Sebastien Hertzae3a1212017-09-08 10:01:17 +0200235 '--compiler-filter=quicken'
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200236 ]
237
238 PrintCmd(cmd)
239 subprocess.check_call(cmd,
240 env = {"LD_LIBRARY_PATH":
241 os.path.join(TOOLS_DIR, 'linux', 'art', 'lib')}
242 )