1#!/usr/bin/env python3 2# Copyright 2019 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Different build variants of Chrome for Android have different version codes. 6 7For targets that have the same package name (e.g. Chrome, Chrome Modern, 8Monochrome, Trichrome), Play Store considers them the same app and will push the 9supported app with the highest version code to devices. Note that Play Store 10does not support hosting two different apps with same version code and package 11name. 12 13Each version code generated by this script will be used by one or more APKs. 14 15Webview channels must have unique version codes for a couple reasons: 16a) Play Store does not support having the same version code for different 17 versions of a package. Without unique codes, promoting a beta apk to stable 18 would require first removing the beta version. 19b) Firebase project support (used by official builders) requires unique 20 [version code + package name]. 21 We cannot add new webview package names for new channels because webview 22 packages are allowlisted by Android as webview providers. 23 24WEBVIEW_STABLE, WEBVIEW_BETA, WEBVIEW_DEV are all used for standalone webview, 25whereas the others are used for various chrome APKs. 26 27TRICHROME_BETA is used for TrichromeChrome, TrichromeWebView, and 28TrichromeLibrary when these are compiled to use the stable package name. Similar 29to how WEBVIEW_STABLE/WEBVIEW_BETA work, this allows users to opt into the open 30Beta Track for the stable package. When Trichrome is configured to use a 31distinct package name for the Beta package, the version code will use TRICHROME 32instead of TRICHROME_BETA. 33 34Note that a package digit of '3' for Webview is reserved for Trichrome Webview. 35The same versionCode is used for both Trichrome Chrome and Trichrome Webview. 36 37Version code values are constructed like this: 38 39 {full BUILD number}{3 digits: PATCH}{1 digit: package}{1 digit: ABIs}. 40 41For example: 42 43 Build 3721, patch 0, ChromeModern (1), on ARM64 (5): 372100015 44 Build 3721, patch 9, Monochrome (2), on ARM (0): 372100920 45 46""" 47 48import argparse 49from collections import namedtuple 50 51# Package name version bits. 52_PACKAGE_NAMES = { 53 'CHROME': 0, 54 'CHROME_MODERN': 10, 55 'MONOCHROME': 20, 56 'TRICHROME': 30, 57 'TRICHROME_BETA': 40, 58 'TRICHROME_AUTO': 50, 59 'WEBVIEW_STABLE': 0, 60 'WEBVIEW_BETA': 10, 61 'WEBVIEW_DEV': 20, 62} 63""" "Next" builds get +500 on their patch number. 64 65This ensures that they are considered "newer" than any non-next build of the 66same branch number; this is a workaround for Android requiring a total ordering 67of versions when we only really have a partial ordering. This assumes that the 68actual patch number will never reach 500, which has never even come close in 69the past. 70""" 71_NEXT_BUILD_VERSION_CODE_DIFF = 50000 72"""List of version numbers to be created for each build configuration. 73Tuple format: 74 75 (version code name), (package name), (supported ABIs) 76 77Here, (supported ABIs) is referring to the combination of browser ABI and 78webview library ABI present in a particular APK. For example, 64_32 implies a 7964-bit browser with an extra 32-bit Webview library. See also 80_ABIS_TO_DIGIT_MASK. 81""" 82_APKS = { 83 '32': [ 84 ('CHROME', 'CHROME', '32'), 85 ('CHROME_MODERN', 'CHROME_MODERN', '32'), 86 ('MONOCHROME', 'MONOCHROME', '32'), 87 ('TRICHROME', 'TRICHROME', '32'), 88 ('TRICHROME_AUTO', 'TRICHROME_AUTO', '32'), 89 ('TRICHROME_BETA', 'TRICHROME_BETA', '32'), 90 ('WEBVIEW_STABLE', 'WEBVIEW_STABLE', '32'), 91 ('WEBVIEW_BETA', 'WEBVIEW_BETA', '32'), 92 ('WEBVIEW_DEV', 'WEBVIEW_DEV', '32'), 93 ], 94 '64': [ 95 ('CHROME', 'CHROME', '64'), 96 ('CHROME_MODERN', 'CHROME_MODERN', '64'), 97 ('MONOCHROME', 'MONOCHROME', '64'), 98 ('TRICHROME', 'TRICHROME', '64'), 99 ('TRICHROME_AUTO', 'TRICHROME_AUTO', '64'), 100 ('TRICHROME_BETA', 'TRICHROME_BETA', '64'), 101 ('WEBVIEW_STABLE', 'WEBVIEW_STABLE', '64'), 102 ('WEBVIEW_BETA', 'WEBVIEW_BETA', '64'), 103 ('WEBVIEW_DEV', 'WEBVIEW_DEV', '64'), 104 ], 105 'hybrid': [ 106 ('CHROME', 'CHROME', '64'), 107 ('CHROME_MODERN', 'CHROME_MODERN', '64'), 108 ('MONOCHROME', 'MONOCHROME', '32_64'), 109 ('MONOCHROME_32', 'MONOCHROME', '32'), 110 ('MONOCHROME_32_64', 'MONOCHROME', '32_64'), 111 ('MONOCHROME_64_32', 'MONOCHROME', '64_32'), 112 ('MONOCHROME_64', 'MONOCHROME', '64'), 113 ('TRICHROME', 'TRICHROME', '32_64'), 114 ('TRICHROME_32', 'TRICHROME', '32'), 115 ('TRICHROME_32_64', 'TRICHROME', '32_64'), 116 ('TRICHROME_64_32', 'TRICHROME', '64_32'), 117 ('TRICHROME_64_32_HIGH', 'TRICHROME', '64_32_high'), 118 ('TRICHROME_64', 'TRICHROME', '64'), 119 ('TRICHROME_AUTO', 'TRICHROME_AUTO', '32_64'), 120 ('TRICHROME_AUTO_32', 'TRICHROME_AUTO', '32'), 121 ('TRICHROME_AUTO_32_64', 'TRICHROME_AUTO', '32_64'), 122 ('TRICHROME_AUTO_64', 'TRICHROME_AUTO', '64'), 123 ('TRICHROME_AUTO_64_32', 'TRICHROME_AUTO', '64_32'), 124 ('TRICHROME_AUTO_64_32_HIGH', 'TRICHROME_AUTO', '64_32_high'), 125 ('TRICHROME_BETA', 'TRICHROME_BETA', '32_64'), 126 ('TRICHROME_32_BETA', 'TRICHROME_BETA', '32'), 127 ('TRICHROME_32_64_BETA', 'TRICHROME_BETA', '32_64'), 128 ('TRICHROME_64_32_BETA', 'TRICHROME_BETA', '64_32'), 129 ('TRICHROME_64_32_HIGH_BETA', 'TRICHROME_BETA', '64_32_high'), 130 ('TRICHROME_64_BETA', 'TRICHROME_BETA', '64'), 131 ('WEBVIEW_STABLE', 'WEBVIEW_STABLE', '32_64'), 132 ('WEBVIEW_BETA', 'WEBVIEW_BETA', '32_64'), 133 ('WEBVIEW_DEV', 'WEBVIEW_DEV', '32_64'), 134 ('WEBVIEW_32_STABLE', 'WEBVIEW_STABLE', '32'), 135 ('WEBVIEW_32_BETA', 'WEBVIEW_BETA', '32'), 136 ('WEBVIEW_32_DEV', 'WEBVIEW_DEV', '32'), 137 ('WEBVIEW_64_STABLE', 'WEBVIEW_STABLE', '64'), 138 ('WEBVIEW_64_BETA', 'WEBVIEW_BETA', '64'), 139 ('WEBVIEW_64_DEV', 'WEBVIEW_DEV', '64'), 140 ] 141} 142 143# Splits input build config architecture to manufacturer and bitness. 144_ARCH_TO_MFG_AND_BITNESS = { 145 'arm': ('arm', '32'), 146 'arm64': ('arm', 'hybrid'), 147 # Until riscv64 needs a unique version code to ship APKs to the store, 148 # point to the 'arm' bitmask. 149 'riscv64': ('arm', '64'), 150 'x86': ('intel', '32'), 151 'x64': ('intel', 'hybrid'), 152} 153 154# Expose the available choices to other scripts. 155ARCH_CHOICES = _ARCH_TO_MFG_AND_BITNESS.keys() 156""" 157The architecture preference is encoded into the version_code for devices 158that support multiple architectures. (exploiting play store logic that pushes 159apk with highest version code) 160 161Detail: 162Many Android devices support multiple architectures, and can run applications 163built for any of them; the Play Store considers all of the supported 164architectures compatible and does not, itself, have any preference for which 165is "better". The common cases here: 166 167- All production arm64 devices can also run arm 168- All production x64 devices can also run x86 169- Pretty much all production x86/x64 devices can also run arm (via a binary 170 translator) 171 172Since the Play Store has no particular preferences, you have to encode your own 173preferences into the ordering of the version codes. There's a few relevant 174things here: 175 176- For any android app, it's theoretically preferable to ship a 64-bit version to 177 64-bit devices if it exists, because the 64-bit architectures are supposed to 178 be "better" than their 32-bit predecessors (unfortunately this is not always 179 true due to the effect on memory usage, but we currently deal with this by 180 simply not shipping a 64-bit version *at all* on the configurations where we 181 want the 32-bit version to be used). 182- For any android app, it's definitely preferable to ship an x86 version to x86 183 devices if it exists instead of an arm version, because running things through 184 the binary translator is a performance hit. 185- For WebView, Monochrome, and Trichrome specifically, they are a special class 186 of APK called "multiarch" which means that they actually need to *use* more 187 than one architecture at runtime (rather than simply being compatible with 188 more than one). The 64-bit builds of these multiarch APKs contain both 32-bit 189 and 64-bit code, so that Webview is available for both ABIs. If you're 190 multiarch you *must* have a version that supports both 32-bit and 64-bit 191 version on a 64-bit device, otherwise it won't work properly. So, the 64-bit 192 version needs to be a higher versionCode, as otherwise a 64-bit device would 193 prefer the 32-bit version that does not include any 64-bit code, and fail. 194""" 195 196 197def _GetAbisToDigitMask(build_number, patch_number): 198 """Return the correct digit mask based on build number. 199 200 Updated from build 5750: Some intel devices advertise support for arm, 201 so arm codes must be lower than x86 codes to prevent providing an 202 arm-optimized build to intel devices. 203 204 Returns: 205 A dictionary of architecture mapped to bitness 206 mapped to version code suffix. 207 """ 208 # Scheme change was made directly to M113 and M114 branches. 209 use_new_scheme = (build_number >= 5750 210 or (build_number == 5672 and patch_number >= 176) 211 or (build_number == 5735 and patch_number >= 53)) 212 if use_new_scheme: 213 return { 214 'arm': { 215 '32': 0, 216 '32_64': 1, 217 '64_32': 2, 218 '64_32_high': 3, 219 '64': 4, 220 }, 221 'intel': { 222 '32': 6, 223 '32_64': 7, 224 '64_32': 8, 225 '64': 9, 226 }, 227 } 228 return { 229 'arm': { 230 '32': 0, 231 '32_64': 3, 232 '64_32': 4, 233 '64': 5, 234 '64_32_high': 9, 235 }, 236 'intel': { 237 '32': 1, 238 '32_64': 6, 239 '64_32': 7, 240 '64': 8, 241 }, 242 } 243 244 245VersionCodeComponents = namedtuple('VersionCodeComponents', [ 246 'build_number', 247 'patch_number', 248 'package_name', 249 'abi', 250 'is_next_build', 251]) 252 253 254def TranslateVersionCode(version_code, is_webview=False): 255 """Translates a version code to its component parts. 256 257 Returns: 258 A 5-tuple (VersionCodeComponents) with the form: 259 - Build number - integer 260 - Patch number - integer 261 - Package name - string 262 - ABI - string : if the build is 32_64 or 64_32 or 64, that is just 263 appended to 'arm' or 'x86' with an underscore 264 - Whether the build is a "next" build - boolean 265 266 So, for build 100.0.5678.99, built for Monochrome on arm 64_32, not a next 267 build, you should get: 268 5678, 99, 'MONOCHROME', 'arm_64_32', False 269 """ 270 if len(version_code) == 9: 271 build_number = int(version_code[:4]) 272 else: 273 # At one branch per day, we'll hit 5 digits in the year 2035. 274 build_number = int(version_code[:5]) 275 276 is_next_build = False 277 patch_number_plus_extra = int(version_code[-5:]) 278 if patch_number_plus_extra >= _NEXT_BUILD_VERSION_CODE_DIFF: 279 is_next_build = True 280 patch_number_plus_extra -= _NEXT_BUILD_VERSION_CODE_DIFF 281 patch_number = patch_number_plus_extra // 100 282 283 # From branch 3992 the name and abi bits in the version code are swapped. 284 if build_number >= 3992: 285 abi_digit = int(version_code[-1]) 286 package_digit = int(version_code[-2]) 287 else: 288 abi_digit = int(version_code[-2]) 289 package_digit = int(version_code[-1]) 290 291 # Before branch 4844 we added 5 to the package digit to indicate a 'next' 292 # build. 293 if build_number < 4844 and package_digit >= 5: 294 is_next_build = True 295 package_digit -= 5 296 297 for package, number in _PACKAGE_NAMES.items(): 298 if number == package_digit * 10: 299 if is_webview == ('WEBVIEW' in package): 300 package_name = package 301 break 302 303 for arch, bitness_to_number in (_GetAbisToDigitMask(build_number, 304 patch_number).items()): 305 for bitness, number in bitness_to_number.items(): 306 if abi_digit == number: 307 abi = arch if arch != 'intel' else 'x86' 308 if bitness != '32': 309 abi += '_' + bitness 310 break 311 312 return VersionCodeComponents(build_number, patch_number, package_name, abi, 313 is_next_build) 314 315 316def GenerateVersionCodes(version_values, arch, is_next_build): 317 """Build dict of version codes for the specified build architecture. Eg: 318 319 { 320 'CHROME_VERSION_CODE': '378100010', 321 'MONOCHROME_VERSION_CODE': '378100013', 322 ... 323 } 324 325 versionCode values are built like this: 326 {full BUILD int}{3 digits: PATCH}{1 digit: package}{1 digit: ABIs}. 327 328 MAJOR and MINOR values are not used for generating versionCode. 329 - MINOR is always 0. It was used for something long ago in Chrome's history 330 but has not been used since, and has never been nonzero on Android. 331 - MAJOR is cosmetic and controlled by the release managers. MAJOR and BUILD 332 always have reasonable sort ordering: for two version codes A and B, it's 333 always the case that (A.MAJOR < B.MAJOR) implies (A.BUILD < B.BUILD), and 334 that (A.MAJOR > B.MAJOR) implies (A.BUILD > B.BUILD). This property is just 335 maintained by the humans who set MAJOR. 336 337 Thus, this method is responsible for the final two digits of versionCode. 338 """ 339 340 build_number = int(version_values['BUILD']) 341 patch_number = int(version_values['PATCH']) 342 base_version_code = (build_number * 1000 + patch_number) * 100 343 344 if is_next_build: 345 base_version_code += _NEXT_BUILD_VERSION_CODE_DIFF 346 347 mfg, bitness = _ARCH_TO_MFG_AND_BITNESS[arch] 348 349 version_codes = {} 350 351 abi_to_digit_mask = _GetAbisToDigitMask(build_number, patch_number) 352 for apk, package, abis in _APKS[bitness]: 353 if abis == '64_32_high' and arch != 'arm64': 354 continue 355 abi_part = abi_to_digit_mask[mfg][abis] 356 package_part = _PACKAGE_NAMES[package] 357 358 version_code_name = apk + '_VERSION_CODE' 359 version_code_val = base_version_code + package_part + abi_part 360 version_codes[version_code_name] = str(version_code_val) 361 362 return version_codes 363 364 365def main(): 366 parser = argparse.ArgumentParser(description='Parses version codes.') 367 parser.add_argument('version_code', help='Version code (e.g. 529700010).') 368 parser.add_argument('--webview', 369 action='store_true', 370 help='Whether this is a webview version code.') 371 args = parser.parse_args() 372 print(TranslateVersionCode(args.version_code, is_webview=args.webview)) 373 374 375if __name__ == '__main__': 376 main() 377