blob: cc30283a44247ca32e9f30540443c4e874a2bd39 [file] [log] [blame]
Yabin Cuideb7b5f2021-04-15 16:57:01 -07001#!/usr/bin/env python3
Yabin Cuib3a66f12017-01-05 12:01:26 -08002#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17"""Downloads simpleperf prebuilts from the build server."""
18import argparse
19import logging
20import os
21import shutil
22import stat
23import textwrap
24
Yabin Cuib3a66f12017-01-05 12:01:26 -080025THIS_DIR = os.path.realpath(os.path.dirname(__file__))
26
27
28class InstallEntry(object):
29 def __init__(self, target, name, install_path, need_strip=False):
30 self.target = target
31 self.name = name
32 self.install_path = install_path
33 self.need_strip = need_strip
34
35
Elliott Hughes5746ce42019-04-04 14:04:51 -070036MINGW = 'local:../../../../prebuilts/gcc/linux-x86/host/x86_64-w64-mingw32-4.8/x86_64-w64-mingw32/'
Yabin Cui370c3412018-06-12 17:11:37 -070037INSTALL_LIST = [
Elliott Hughes5746ce42019-04-04 14:04:51 -070038 # simpleperf on device.
39 InstallEntry('MODULES-IN-system-extras-simpleperf',
40 'simpleperf/android/arm64/simpleperf_ndk64',
41 'android/arm64/simpleperf'),
Yabin Cui0d6380a2021-08-17 12:09:13 -070042 InstallEntry('MODULES-IN-system-extras-simpleperf_arm',
Elliott Hughes5746ce42019-04-04 14:04:51 -070043 'simpleperf/android/arm/simpleperf_ndk',
44 'android/arm/simpleperf'),
45 InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
46 'simpleperf/android/x86_64/simpleperf_ndk64',
47 'android/x86_64/simpleperf'),
48 InstallEntry('MODULES-IN-system-extras-simpleperf_x86',
49 'simpleperf/android/x86/simpleperf_ndk',
50 'android/x86/simpleperf'),
Yabin Cuib3a66f12017-01-05 12:01:26 -080051
Yabin Cuidb303522021-03-17 14:09:52 -070052 # simpleperf on host.
Elliott Hughes5746ce42019-04-04 14:04:51 -070053 InstallEntry('MODULES-IN-system-extras-simpleperf',
54 'simpleperf/linux/x86_64/simpleperf_ndk64',
55 'linux/x86_64/simpleperf', True),
56 InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
57 'simpleperf/darwin/x86_64/simpleperf_ndk64',
58 'darwin/x86_64/simpleperf'),
Elliott Hughes5746ce42019-04-04 14:04:51 -070059 InstallEntry('MODULES-IN-system-extras-simpleperf',
Yabin Cuiaf31dd82019-05-29 10:35:46 -070060 'simpleperf/windows/x86_64/simpleperf_ndk64.exe',
Elliott Hughes5746ce42019-04-04 14:04:51 -070061 'windows/x86_64/simpleperf.exe', True),
Yabin Cuib3a66f12017-01-05 12:01:26 -080062
63 # libsimpleperf_report.so on host
Elliott Hughes5746ce42019-04-04 14:04:51 -070064 InstallEntry('MODULES-IN-system-extras-simpleperf',
65 'simpleperf/linux/x86_64/libsimpleperf_report.so',
66 'linux/x86_64/libsimpleperf_report.so', True),
67 InstallEntry('MODULES-IN-system-extras-simpleperf_mac',
68 'simpleperf/darwin/x86_64/libsimpleperf_report.dylib',
Yabin Cui370c3412018-06-12 17:11:37 -070069 'darwin/x86_64/libsimpleperf_report.dylib'),
Elliott Hughes5746ce42019-04-04 14:04:51 -070070 InstallEntry('MODULES-IN-system-extras-simpleperf',
71 'simpleperf/windows/x86_64/libsimpleperf_report.dll',
72 'windows/x86_64/libsimpleperf_report.dll', True),
Yabin Cuicbddd382017-05-03 14:46:55 -070073
74 # libwinpthread-1.dll on windows host
Elliott Hughes5746ce42019-04-04 14:04:51 -070075 InstallEntry(MINGW + '/bin/libwinpthread-1.dll', 'libwinpthread-1.dll',
Yabin Cui370c3412018-06-12 17:11:37 -070076 'windows/x86_64/libwinpthread-1.dll', False),
Yabin Cuib3a66f12017-01-05 12:01:26 -080077]
78
79
80def logger():
81 """Returns the main logger for this module."""
82 return logging.getLogger(__name__)
83
84
85def check_call(cmd):
86 """Proxy for subprocess.check_call with logging."""
87 import subprocess
88 logger().debug('check_call `%s`', ' '.join(cmd))
89 subprocess.check_call(cmd)
90
91
Yabin Cuib0d308d2017-05-08 11:29:12 -070092def fetch_artifact(branch, build, target, name):
Yabin Cuib3a66f12017-01-05 12:01:26 -080093 """Fetches and artifact from the build server."""
Yabin Cuib0d308d2017-05-08 11:29:12 -070094 if target.startswith('local:'):
95 shutil.copyfile(target[6:], name)
Yabin Cuicbddd382017-05-03 14:46:55 -070096 return
Yabin Cuib3a66f12017-01-05 12:01:26 -080097 logger().info('Fetching %s from %s %s (artifacts matching %s)', build,
Yabin Cuib0d308d2017-05-08 11:29:12 -070098 target, branch, name)
Yabin Cuib3a66f12017-01-05 12:01:26 -080099 fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact'
100 cmd = [fetch_artifact_path, '--branch', branch, '--target', target,
Yabin Cuib0d308d2017-05-08 11:29:12 -0700101 '--bid', build, name]
Yabin Cuib3a66f12017-01-05 12:01:26 -0800102 check_call(cmd)
103
104
105def start_branch(build):
106 """Creates a new branch in the project."""
107 branch_name = 'update-' + (build or 'latest')
108 logger().info('Creating branch %s', branch_name)
109 check_call(['repo', 'start', branch_name, '.'])
110
111
112def commit(branch, build, add_paths):
113 """Commits the new prebuilts."""
114 logger().info('Making commit')
115 check_call(['git', 'add'] + add_paths)
116 message = textwrap.dedent("""\
117 simpleperf: update simpleperf prebuilts to build {build}.
118
119 Taken from branch {branch}.""").format(branch=branch, build=build)
120 check_call(['git', 'commit', '-m', message])
121
122
123def remove_old_release(install_dir):
124 """Removes the old prebuilts."""
125 if os.path.exists(install_dir):
126 logger().info('Removing old install directory "%s"', install_dir)
127 check_call(['git', 'rm', '-rf', '--ignore-unmatch', install_dir])
128
129 # Need to check again because git won't remove directories if they have
130 # non-git files in them.
131 if os.path.exists(install_dir):
132 shutil.rmtree(install_dir)
133
134
135def install_new_release(branch, build, install_dir):
136 """Installs the new release."""
Yabin Cui370c3412018-06-12 17:11:37 -0700137 for entry in INSTALL_LIST:
Yabin Cuib3a66f12017-01-05 12:01:26 -0800138 install_entry(branch, build, install_dir, entry)
139
140
141def install_entry(branch, build, install_dir, entry):
142 """Installs the device specific components of the release."""
143 target = entry.target
144 name = entry.name
145 install_path = os.path.join(install_dir, entry.install_path)
146 need_strip = entry.need_strip
147
148 fetch_artifact(branch, build, target, name)
Elliott Hughes5746ce42019-04-04 14:04:51 -0700149 name = os.path.basename(name)
Yabin Cuib3a66f12017-01-05 12:01:26 -0800150 exe_stat = os.stat(name)
151 os.chmod(name, exe_stat.st_mode | stat.S_IEXEC)
152 if need_strip:
153 check_call(['strip', name])
Yabin Cui370c3412018-06-12 17:11:37 -0700154 dirname = os.path.dirname(install_path)
155 if not os.path.isdir(dirname):
156 os.makedirs(dirname)
Yabin Cuib3a66f12017-01-05 12:01:26 -0800157 shutil.move(name, install_path)
158
159
160def get_args():
161 """Parses and returns command line arguments."""
162 parser = argparse.ArgumentParser()
163
164 parser.add_argument(
Elliott Hughes5746ce42019-04-04 14:04:51 -0700165 '-b', '--branch', default='aosp-simpleperf-release',
Yabin Cuib3a66f12017-01-05 12:01:26 -0800166 help='Branch to pull build from.')
167 parser.add_argument('--build', required=True, help='Build number to pull.')
168 parser.add_argument(
169 '--use-current-branch', action='store_true',
170 help='Perform the update in the current branch. Do not repo start.')
171 parser.add_argument(
172 '-v', '--verbose', action='count', default=0,
173 help='Increase output verbosity.')
174
175 return parser.parse_args()
176
177
178def main():
179 """Program entry point."""
180 os.chdir(THIS_DIR)
181
182 args = get_args()
183 verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG)
184 verbosity = args.verbose
185 if verbosity > 2:
186 verbosity = 2
187 logging.basicConfig(level=verbose_map[verbosity])
188
189 install_dir = 'bin'
190
191 if not args.use_current_branch:
192 start_branch(args.build)
193 remove_old_release(install_dir)
194 install_new_release(args.branch, args.build, install_dir)
195 artifacts = [install_dir]
196 commit(args.branch, args.build, artifacts)
197
198
199if __name__ == '__main__':
Yabin Cui370c3412018-06-12 17:11:37 -0700200 main()