blob: 7eb281aa9a07005428833c794cd8283393786564 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 09:01:12 +01001#!/usr/bin/env vpython3
Byoungchan Lee0d6d9482021-07-16 03:13:37 +09002
3# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010010"""Script for building and testing WebRTC AAR."""
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090011
12import argparse
13import logging
14import os
15import re
16import shutil
17import subprocess
18import sys
19import tempfile
20
21SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
22CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
23
24sys.path.append(os.path.join(CHECKOUT_ROOT, 'tools_webrtc'))
25from android.build_aar import BuildAar
26
27ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
28ARTIFACT_ID = 'google-webrtc'
29COMMIT_POSITION_REGEX = r'^Cr-Commit-Position: refs/heads/master@{#(\d+)}$'
30GRADLEW_BIN = os.path.join(CHECKOUT_ROOT,
31 'examples/androidtests/third_party/gradle/gradlew')
32ADB_BIN = os.path.join(CHECKOUT_ROOT,
33 'third_party/android_sdk/public/platform-tools/adb')
34AAR_PROJECT_DIR = os.path.join(CHECKOUT_ROOT, 'examples/aarproject')
35
36
37def _ParseArgs():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010038 parser = argparse.ArgumentParser(description='Releases WebRTC on Bintray.')
39 parser.add_argument('--use-goma',
40 action='store_true',
41 default=False,
42 help='Use goma.')
43 parser.add_argument('--skip-tests',
44 action='store_true',
45 default=False,
46 help='Skips running the tests.')
47 parser.add_argument(
48 '--build-dir',
49 default=None,
50 help='Temporary directory to store the build files. If not specified, '
51 'a new directory will be created.')
52 parser.add_argument('--verbose',
53 action='store_true',
54 default=False,
55 help='Debug logging.')
56 return parser.parse_args()
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090057
58
59def _GetCommitHash():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010060 commit_hash = subprocess.check_output(
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090061 ['git', 'rev-parse', 'HEAD'], cwd=CHECKOUT_ROOT).decode('UTF-8').strip()
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010062 return commit_hash
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090063
64
65def _GetCommitPos():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010066 commit_message = subprocess.check_output(
67 ['git', 'rev-list', '--format=%B', '--max-count=1', 'HEAD'],
68 cwd=CHECKOUT_ROOT).decode('UTF-8')
69 commit_pos_match = re.search(COMMIT_POSITION_REGEX, commit_message,
70 re.MULTILINE)
71 if not commit_pos_match:
72 raise Exception('Commit position not found in the commit message: %s' %
73 commit_message)
74 return commit_pos_match.group(1)
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090075
76
77def _TestAAR(build_dir):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010078 """Runs AppRTCMobile tests using the AAR. Returns true if the tests pass."""
79 logging.info('Testing library.')
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090080
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010081 # Uninstall any existing version of AppRTCMobile.
82 logging.info('Uninstalling previous AppRTCMobile versions. It is okay for '
83 'these commands to fail if AppRTCMobile is not installed.')
84 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc'])
85 subprocess.call([ADB_BIN, 'uninstall', 'org.appspot.apprtc.test'])
Byoungchan Lee0d6d9482021-07-16 03:13:37 +090086
Christoffer Jansson4e8a7732022-02-08 09:01:12 +010087 # Run tests.
88 try:
89 # First clean the project.
90 subprocess.check_call([GRADLEW_BIN, 'clean'], cwd=AAR_PROJECT_DIR)
91 # Then run the tests.
92 subprocess.check_call([
93 GRADLEW_BIN, 'connectedDebugAndroidTest',
94 '-PaarDir=' + os.path.abspath(build_dir)
95 ],
96 cwd=AAR_PROJECT_DIR)
97 except subprocess.CalledProcessError:
98 logging.exception('Test failure.')
99 return False # Clean or tests failed
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900100
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100101 return True # Tests pass
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900102
103
104def BuildAndTestAar(use_goma, skip_tests, build_dir):
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100105 version = '1.0.' + _GetCommitPos()
106 commit = _GetCommitHash()
107 logging.info('Building and Testing AAR version %s with hash %s', version,
108 commit)
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900109
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100110 # If build directory is not specified, create a temporary directory.
111 use_tmp_dir = not build_dir
112 if use_tmp_dir:
113 build_dir = tempfile.mkdtemp()
114
115 try:
116 base_name = ARTIFACT_ID + '-' + version
117 aar_file = os.path.join(build_dir, base_name + '.aar')
118
119 logging.info('Building at %s', build_dir)
120 BuildAar(ARCHS,
121 aar_file,
122 use_goma=use_goma,
123 ext_build_dir=os.path.join(build_dir, 'aar-build'))
124
125 tests_pass = skip_tests or _TestAAR(build_dir)
126 if not tests_pass:
127 raise Exception('Test failure.')
128
129 logging.info('Test success.')
130
131 finally:
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900132 if use_tmp_dir:
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100133 shutil.rmtree(build_dir, True)
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900134
135
136def main():
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100137 args = _ParseArgs()
138 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
139 BuildAndTestAar(args.use_goma, args.skip_tests, args.build_dir)
Byoungchan Lee0d6d9482021-07-16 03:13:37 +0900140
141
142if __name__ == '__main__':
Christoffer Jansson4e8a7732022-02-08 09:01:12 +0100143 sys.exit(main())