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