blob: 5325a9767984590cc76f3b2457fcf9c19f59b966 [file] [log] [blame]
Alan Viverettef5cb0e62017-11-17 15:15:23 -05001#!/usr/bin/python3
Alan Viverette95f6d362017-04-06 09:40:50 -04002
Alan Viverettecd3de262017-08-14 09:51:30 -04003# This script is used to update platform SDK prebuilts, Support Library, and a variety of other
4# prebuilt libraries used by Android's Makefile builds. For details on how to use this script,
5# visit go/update-prebuilts.
Alan Viverette95f6d362017-04-06 09:40:50 -04006import os, sys, getopt, zipfile, re
Alan Viveretted4527e62017-05-11 15:03:25 -04007import argparse
Jiyong Park1d1c9632018-05-29 17:45:27 +09008import glob
Alan Viveretted4527e62017-05-11 15:03:25 -04009import subprocess
shepshaparde4127cf2019-09-03 16:08:44 -070010from shutil import copyfile, rmtree, which, move
Alan Viveretted4527e62017-05-11 15:03:25 -040011from distutils.version import LooseVersion
Alan Viverettef5cb0e62017-11-17 15:15:23 -050012from functools import reduce
Alan Viverette95f6d362017-04-06 09:40:50 -040013
Alan Viverette45837092017-05-12 14:50:53 -040014current_path = 'current'
Andy Wickhamc3c99af2020-10-26 20:47:58 +000015framework_sdk_target = 'sdk_phone_armv7-win_sdk'
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -040016androidx_dir = os.path.join(current_path, 'androidx')
Alan Viverette3e57a4a2017-08-11 15:49:47 -040017extras_dir = os.path.join(current_path, 'extras')
Alan Viverettec1c32b62017-12-20 09:40:36 -050018buildtools_dir = 'tools'
Jeff Gaston553a4372018-03-29 18:34:22 -040019jetifier_dir = os.path.join(buildtools_dir, 'jetifier', 'jetifier-standalone')
Alan Viverette95f6d362017-04-06 09:40:50 -040020
Jeff Gastona68a8d42018-03-23 14:00:13 -040021temp_dir = os.path.join(os.getcwd(), "support_tmp")
22os.chdir(os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))
23git_dir = os.getcwd()
24
Alan Viverettecd3de262017-08-14 09:51:30 -040025# See go/fetch_artifact for details on this script.
Alan Viveretted4527e62017-05-11 15:03:25 -040026FETCH_ARTIFACT = '/google/data/ro/projects/android/fetch_artifact'
Jeff Sharkeyabe16df2021-05-25 10:09:03 -060027FETCH_ARTIFACT_BEYOND_CORP = '/usr/bin/fetch_artifact'
Alan Viveretted4527e62017-05-11 15:03:25 -040028
Alan Viverette0723aff2021-08-31 17:07:47 +000029# Leave map blank to automatically populate name and path:
30# - Name format is MAVEN.replaceAll(':','_')
31# - Path format is MAVEN.replaceAll(':','/').replaceAll('.','/')
Alan Viverette95f6d362017-04-06 09:40:50 -040032maven_to_make = {
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -040033 # AndroidX
Alan Viverettef3c12722021-08-30 19:50:33 +000034 'androidx.benchmark:benchmark-macro': { },
35 'androidx.benchmark:benchmark-macro-junit4': { },
36 'androidx.benchmark:benchmark-common': { },
37 'androidx.benchmark:benchmark-junit4': { },
38 'androidx.tracing:tracing': { },
39 'androidx.tracing:tracing-ktx': { },
40 'androidx.slice:slice-builders': { },
41 'androidx.slice:slice-core': { },
42 'androidx.slice:slice-view': { },
43 'androidx.remotecallback:remotecallback': { },
Alan Viverette0723aff2021-08-31 17:07:47 +000044 'androidx.remotecallback:remotecallback-processor': {'host':True},
45 'androidx.versionedparcelable:versionedparcelable': { },
Alan Viverettef3c12722021-08-30 19:50:33 +000046 'androidx.vectordrawable:vectordrawable-animated': { },
47 'androidx.activity:activity': { },
48 'androidx.activity:activity-ktx': { },
Alan Viverette0723aff2021-08-31 17:07:47 +000049 'androidx.annotation:annotation': {'host_and_device':True},
Alan Viverettef3c12722021-08-30 19:50:33 +000050 'androidx.annotation:annotation-experimental': { },
51 'androidx.asynclayoutinflater:asynclayoutinflater': { },
52 'androidx.collection:collection': { },
53 'androidx.collection:collection-ktx': { },
54 'androidx.concurrent:concurrent-futures': { },
55 'androidx.concurrent:concurrent-listenablefuture-callback': { },
56 'androidx.concurrent:concurrent-listenablefuture': { },
57 'androidx.core:core': { },
58 'androidx.core:core-animation': { },
59 'androidx.core:core-ktx': { },
60 'androidx.contentpaging:contentpaging': { },
61 'androidx.coordinatorlayout:coordinatorlayout': { },
62 'androidx.legacy:legacy-support-core-ui': { },
63 'androidx.legacy:legacy-support-core-utils': { },
64 'androidx.cursoradapter:cursoradapter': { },
65 'androidx.browser:browser': { },
66 'androidx.customview:customview': { },
67 'androidx.documentfile:documentfile': { },
68 'androidx.drawerlayout:drawerlayout': { },
69 'androidx.dynamicanimation:dynamicanimation': { },
70 'androidx.emoji:emoji': { },
71 'androidx.emoji:emoji-appcompat': { },
72 'androidx.emoji:emoji-bundled': { },
73 'androidx.emoji2:emoji2': { },
74 'androidx.emoji2:emoji2-views-helper': { },
75 'androidx.exifinterface:exifinterface': { },
76 'androidx.fragment:fragment': { },
77 'androidx.fragment:fragment-ktx': { },
78 'androidx.heifwriter:heifwriter': { },
79 'androidx.interpolator:interpolator': { },
80 'androidx.loader:loader': { },
81 'androidx.localbroadcastmanager:localbroadcastmanager': { },
82 'androidx.media:media': { },
83 'androidx.media2:media2-player': { },
84 'androidx.media2:media2-session': { },
85 'androidx.media2:media2-common': { },
86 'androidx.media2:media2-exoplayer': { },
87 'androidx.media2:media2-widget': { },
88 'androidx.navigation:navigation-common': { },
89 'androidx.navigation:navigation-common-ktx': { },
90 'androidx.navigation:navigation-fragment': { },
91 'androidx.navigation:navigation-fragment-ktx': { },
92 'androidx.navigation:navigation-runtime': { },
93 'androidx.navigation:navigation-runtime-ktx': { },
94 'androidx.navigation:navigation-ui': { },
95 'androidx.navigation:navigation-ui-ktx': { },
96 'androidx.percentlayout:percentlayout': { },
97 'androidx.print:print': { },
98 'androidx.recommendation:recommendation': { },
99 'androidx.recyclerview:recyclerview-selection': { },
100 'androidx.savedstate:savedstate': { },
101 'androidx.savedstate:savedstate-ktx': { },
102 'androidx.slidingpanelayout:slidingpanelayout': { },
103 'androidx.swiperefreshlayout:swiperefreshlayout': { },
104 'androidx.textclassifier:textclassifier': { },
105 'androidx.transition:transition': { },
106 'androidx.tvprovider:tvprovider': { },
107 'androidx.legacy:legacy-support-v13': { },
108 'androidx.legacy:legacy-preference-v14': { },
109 'androidx.leanback:leanback': { },
110 'androidx.leanback:leanback-preference': { },
111 'androidx.legacy:legacy-support-v4': { },
112 'androidx.appcompat:appcompat': { },
113 'androidx.appcompat:appcompat-resources': { },
114 'androidx.cardview:cardview': { },
115 'androidx.gridlayout:gridlayout': { },
116 'androidx.mediarouter:mediarouter': { },
117 'androidx.palette:palette': { },
118 'androidx.preference:preference': { },
119 'androidx.recyclerview:recyclerview': { },
120 'androidx.vectordrawable:vectordrawable': { },
121 'androidx.viewpager:viewpager': { },
122 'androidx.viewpager2:viewpager2': { },
123 'androidx.wear:wear': { },
124 'androidx.wear:wear-ongoing': { },
125 'androidx.webkit:webkit': { },
126 'androidx.biometric:biometric': { },
127 'androidx.autofill:autofill': { },
128 'androidx.appsearch:appsearch': { },
Alan Viverette0723aff2021-08-31 17:07:47 +0000129 'androidx.appsearch:appsearch-local-storage': {'name':'androidx.appsearch_appsearch_local_storage'},
130 'androidx.appsearch:appsearch-platform-storage': { },
131 'androidx.appsearch:appsearch-compiler': {'name':'androidx.appsearch_appsearch-compiler', 'host':True},
Alan Viverettef3c12722021-08-30 19:50:33 +0000132 'androidx.car.app:app': { },
133 'androidx.car.app:app-automotive': { },
134 'androidx.car.app:app-testing': { },
135 'androidx.startup:startup-runtime': { },
Alan Viverettecdfc9892021-08-31 19:35:58 +0000136 'androidx.window:window': {'optional-uses-libs':{'androidx.window.extensions', 'androidx.window.sidecar'}},
Alan Viverettef3c12722021-08-30 19:50:33 +0000137 'androidx.resourceinspection:resourceinspection-annotation': { },
138 'androidx.profileinstaller:profileinstaller': { },
Aurimas Liutikas6e87bbf2021-07-30 10:04:05 -0700139
Alan Viverettef44e2f92021-08-31 17:26:26 +0000140 # AndroidX for Compose
141 'androidx.compose.compiler:compiler-hosted': { },
142 'androidx.compose.runtime:runtime': { },
143 'androidx.compose.runtime:runtime-saveable': { },
144 'androidx.compose.foundation:foundation': { },
145 'androidx.compose.foundation:foundation-layout': { },
146 'androidx.compose.foundation:foundation-text': { },
147 'androidx.compose.ui:ui': { },
148 'androidx.compose.ui:ui-geometry': { },
149 'androidx.compose.ui:ui-graphics': { },
150 'androidx.compose.ui:ui-text': { },
151 'androidx.compose.ui:ui-unit': { },
152 'androidx.compose.ui:ui-util': { },
153 'androidx.compose.animation:animation-core': { },
154 'androidx.compose.animation:animation': { },
155 'androidx.compose.material:material-icons-core': { },
156 'androidx.compose.material:material-ripple': { },
157 'androidx.compose.material:material': { },
158 'androidx.activity:activity-compose': { },
159
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400160 # AndroidX for Multidex
Alan Viverette0723aff2021-08-31 17:07:47 +0000161 'androidx.multidex:multidex': { },
162 'androidx.multidex:multidex-instrumentation': { },
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400163
164 # AndroidX for Constraint Layout
Alan Viverette0723aff2021-08-31 17:07:47 +0000165 'androidx.constraintlayout:constraintlayout': {'name':'androidx-constraintlayout_constraintlayout'},
166 'androidx.constraintlayout:constraintlayout-solver': {'name':'androidx-constraintlayout_constraintlayout-solver'},
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400167
168 # AndroidX for Architecture Components
Alan Viverettef3c12722021-08-30 19:50:33 +0000169 'androidx.arch.core:core-common': { },
170 'androidx.arch.core:core-runtime': { },
171 'androidx.lifecycle:lifecycle-common': { },
172 'androidx.lifecycle:lifecycle-common-java8': { },
173 'androidx.lifecycle:lifecycle-extensions': { },
174 'androidx.lifecycle:lifecycle-livedata': { },
175 'androidx.lifecycle:lifecycle-livedata-ktx': { },
176 'androidx.lifecycle:lifecycle-livedata-core': { },
177 'androidx.lifecycle:lifecycle-livedata-core-ktx': { },
178 'androidx.lifecycle:lifecycle-process': { },
179 'androidx.lifecycle:lifecycle-runtime': { },
180 'androidx.lifecycle:lifecycle-runtime-ktx': { },
181 'androidx.lifecycle:lifecycle-service': { },
182 'androidx.lifecycle:lifecycle-viewmodel': { },
183 'androidx.lifecycle:lifecycle-viewmodel-ktx': { },
184 'androidx.lifecycle:lifecycle-viewmodel-savedstate': { },
185 'androidx.paging:paging-common': { },
186 'androidx.paging:paging-common-ktx': { },
187 'androidx.paging:paging-runtime': { },
188 'androidx.sqlite:sqlite': { },
189 'androidx.sqlite:sqlite-framework': { },
Alan Viverette0723aff2021-08-31 17:07:47 +0000190 'androidx.room:room-common': {'host_and_device':True},
Alan Viverettecdfc9892021-08-31 19:35:58 +0000191 'androidx.room:room-compiler': {'host':True, 'extra-static-libs':{'guava-21.0'}},
Alan Viverette0723aff2021-08-31 17:07:47 +0000192 'androidx.room:room-migration': {'host_and_device':True},
Alan Viverettef3c12722021-08-30 19:50:33 +0000193 'androidx.room:room-runtime': { },
194 'androidx.room:room-testing': { },
Alan Viverette0723aff2021-08-31 17:07:47 +0000195 'androidx.room:room-compiler-processing': {'host':True},
Alan Viverettef3c12722021-08-30 19:50:33 +0000196 'androidx.work:work-runtime': { },
197 'androidx.work:work-runtime-ktx': { },
198 'androidx.work:work-testing': { },
Allenab72f012018-01-29 18:10:23 -0800199
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400200 # Third-party dependencies
Jeff Gastona0bce602018-08-24 14:05:03 -0400201 'com.google.android:flexbox': {'name':'flexbox', 'path':'flexbox'},
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400202
Jeff Gastonfa7c7e82018-04-06 13:35:48 -0400203 # Androidx Material Design Components
Alan Viverettef3c12722021-08-30 19:50:33 +0000204 'com.google.android.material:material': { },
Alan Viverette95f6d362017-04-06 09:40:50 -0400205}
206
Alan Viverettecdfc9892021-08-31 19:35:58 +0000207# Mapping of POM dependencies to Soong build targets
208deps_rewrite = {
209 'auto-common':'auto_common',
210 'auto-value-annotations':'auto_value_annotations',
211 'com.google.auto.value:auto-value':'auto_value_plugin',
212 'monitor':'androidx.test.monitor',
213 'rules':'androidx.test.rules',
214 'runner':'androidx.test.runner',
215 'androidx.test:core':'androidx.test.core',
216 'com.squareup:javapoet':'javapoet',
217 'com.google.guava:listenablefuture':'guava-listenablefuture-prebuilt-jar',
218 'sqlite-jdbc':'xerial-sqlite-jdbc',
219 'gson':'gson-prebuilt-jar',
220 'com.intellij:annotations':'jetbrains-annotations',
221 'javax.annotation:javax.annotation-api':'javax-annotation-api-prebuilt-host-jar',
222 'org.robolectric:robolectric':'Robolectric_all-target',
223 'org.jetbrains.kotlin:kotlin-stdlib-common':'kotlin-stdlib',
224 'org.jetbrains.kotlinx:kotlinx-coroutines-core':'kotlinx_coroutines',
225 'org.jetbrains.kotlinx:kotlinx-coroutines-android':'kotlinx_coroutines_android',
226 'org.jetbrains.kotlinx:kotlinx-metadata-jvm':'kotlinx_metadata_jvm',
227}
228
Alan Viverettef3c12722021-08-30 19:50:33 +0000229
230def name_for_artifact(group_artifact):
231 return group_artifact.replace(':','_')
232
233
234def path_for_artifact(group_artifact):
235 return group_artifact.replace('.','/').replace(':','/')
236
237
238# Add automatic entries to maven_to_make.
239for key in maven_to_make:
240 if ('name' not in maven_to_make[key]):
241 maven_to_make[key]['name'] = name_for_artifact(key)
242 if ('path' not in maven_to_make[key]):
243 maven_to_make[key]['path'] = path_for_artifact(key)
244
Alan Viverette95f6d362017-04-06 09:40:50 -0400245# Always remove these files.
246blacklist_files = [
247 'annotations.zip',
248 'public.txt',
249 'R.txt',
Alan Viverette44ad4e42017-08-09 17:45:00 -0400250 'AndroidManifest.xml',
Alan Viverettef48d9b42017-08-17 14:45:46 -0400251 os.path.join('libs','noto-emoji-compat-java.jar')
Alan Viverette95f6d362017-04-06 09:40:50 -0400252]
253
Alan Viverette44ad4e42017-08-09 17:45:00 -0400254artifact_pattern = re.compile(r"^(.+?)-(\d+\.\d+\.\d+(?:-\w+\d+)?(?:-[\d.]+)*)\.(jar|aar)$")
Alan Viverette95f6d362017-04-06 09:40:50 -0400255
Alan Viverettec960cfb2017-12-04 13:09:22 -0500256
257class MavenLibraryInfo:
Jeff Gastonc302bb92018-03-23 13:53:48 -0400258 def __init__(self, key, group_id, artifact_id, version, dir, repo_dir, file):
Alan Viverettec960cfb2017-12-04 13:09:22 -0500259 self.key = key
260 self.group_id = group_id
261 self.artifact_id = artifact_id
262 self.version = version
Jeff Gastonc302bb92018-03-23 13:53:48 -0400263 self.dir = dir
Alan Viverettec960cfb2017-12-04 13:09:22 -0500264 self.repo_dir = repo_dir
265 self.file = file
266
267
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500268def print_e(*args, **kwargs):
269 print(*args, file=sys.stderr, **kwargs)
270
Alan Viverette95f6d362017-04-06 09:40:50 -0400271
272def touch(fname, times=None):
273 with open(fname, 'a'):
274 os.utime(fname, times)
275
276
Alan Viverette45837092017-05-12 14:50:53 -0400277def path(*path_parts):
278 return reduce((lambda x, y: os.path.join(x, y)), path_parts)
279
280
Alan Viverettecd3de262017-08-14 09:51:30 -0400281def flatten(list):
282 return reduce((lambda x, y: "%s %s" % (x, y)), list)
283
284
Alan Viverette45837092017-05-12 14:50:53 -0400285def rm(path):
286 if os.path.isdir(path):
287 rmtree(path)
288 elif os.path.exists(path):
289 os.remove(path)
290
291
292def mv(src_path, dst_path):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400293 if os.path.exists(dst_path):
Alan Viverettecd3de262017-08-14 09:51:30 -0400294 rm(dst_path)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400295 if not os.path.exists(os.path.dirname(dst_path)):
296 os.makedirs(os.path.dirname(dst_path))
Jiyong Park1d1c9632018-05-29 17:45:27 +0900297 for f in (glob.glob(src_path)):
298 if '*' in dst_path:
299 dst = os.path.join(os.path.dirname(dst_path), os.path.basename(f))
300 else:
301 dst = dst_path
shepshaparde4127cf2019-09-03 16:08:44 -0700302 move(f, dst)
Alan Viverette45837092017-05-12 14:50:53 -0400303
304
Jeff Gastonc302bb92018-03-23 13:53:48 -0400305def detect_artifacts(maven_repo_dirs):
Alan Viveretted4527e62017-05-11 15:03:25 -0400306 maven_lib_info = {}
307
Dan Willemsen814152e2017-11-06 13:22:11 -0800308 # Find the latest revision for each artifact, remove others
Jeff Gastonc302bb92018-03-23 13:53:48 -0400309 for repo_dir in maven_repo_dirs:
Dan Willemsen814152e2017-11-06 13:22:11 -0800310 for root, dirs, files in os.walk(repo_dir):
311 for file in files:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500312 if file[-4:] == ".pom":
313 # Read the POM (hack hack hack).
314 group_id = ''
315 artifact_id = ''
316 version = ''
317 file = os.path.join(root, file)
318 with open(file) as pom_file:
319 for line in pom_file:
320 if line[:11] == ' <groupId>':
321 group_id = line[11:-11]
322 elif line[:14] == ' <artifactId>':
323 artifact_id = line[14:-14]
324 elif line[:11] == ' <version>':
325 version = line[11:-11]
326 if group_id == '' or artifact_id == '' or version == '':
327 print_e('Failed to find Maven artifact data in ' + file)
328 continue
Alan Viverette95f6d362017-04-06 09:40:50 -0400329
Alan Viverettec960cfb2017-12-04 13:09:22 -0500330 # Locate the artifact.
331 artifact_file = file[:-4]
332 if os.path.exists(artifact_file + '.jar'):
333 artifact_file = artifact_file + '.jar'
334 elif os.path.exists(artifact_file + '.aar'):
335 artifact_file = artifact_file + '.aar'
336 else:
337 print_e('Failed to find artifact for ' + artifact_file)
338 continue
339
340 # Make relative to root.
341 artifact_file = artifact_file[len(root) + 1:]
342
343 # Find the mapping.
344 group_artifact = group_id + ':' + artifact_id
Jeff Gaston3e622ba2018-03-26 23:24:46 -0400345 if group_artifact in maven_to_make:
Alan Viverettec960cfb2017-12-04 13:09:22 -0500346 key = group_artifact
Jeff Gaston3e622ba2018-03-26 23:24:46 -0400347 elif artifact_id in maven_to_make:
348 key = artifact_id
Alan Viverettec960cfb2017-12-04 13:09:22 -0500349 else:
Alan Viverettecdfc9892021-08-31 19:35:58 +0000350 # No mapping entry, skip this library.
Alan Viverettec960cfb2017-12-04 13:09:22 -0500351 continue
352
353 # Store the latest version.
354 version = LooseVersion(version)
355 if key not in maven_lib_info \
356 or version > maven_lib_info[key].version:
357 maven_lib_info[key] = MavenLibraryInfo(key, group_id, artifact_id, version,
358 root, repo_dir, artifact_file)
Alan Viverette95f6d362017-04-06 09:40:50 -0400359
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400360 return maven_lib_info
361
362
Colin Cross59b59db2018-04-24 13:58:05 -0700363def transform_maven_repos(maven_repo_dirs, transformed_dir, extract_res=True, include_static_deps=True):
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400364 cwd = os.getcwd()
365
366 # Use a temporary working directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400367 maven_lib_info = detect_artifacts(maven_repo_dirs)
Jeff Gastona68a8d42018-03-23 14:00:13 -0400368 working_dir = temp_dir
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400369
Alan Viverettec960cfb2017-12-04 13:09:22 -0500370 if not maven_lib_info:
371 print_e('Failed to detect artifacts')
372 return False
373
Jeff Gastonc302bb92018-03-23 13:53:48 -0400374 # extract some files (for example, AndroidManifest.xml) from any relevant artifacts
Alan Viveretted4527e62017-05-11 15:03:25 -0400375 for info in maven_lib_info.values():
Alan Viveretted8ce7222017-12-11 17:24:43 -0500376 transform_maven_lib(working_dir, info, extract_res)
Dan Willemsen814152e2017-11-06 13:22:11 -0800377
Colin Cross74683bc2018-04-11 17:36:18 -0700378 # generate a single Android.bp that specifies to use all of the above artifacts
379 makefile = os.path.join(working_dir, 'Android.bp')
Alan Viverette4ec9a172018-02-20 16:19:31 -0500380 with open(makefile, 'w') as f:
Anton Hanssonbd2021c2021-04-06 19:21:34 +0000381 args = ["pom2bp"]
Aurimas Liutikas6e87bbf2021-07-30 10:04:05 -0700382 args.extend(["-sdk-version", "31"])
Anton Hanssonbd2021c2021-04-06 19:21:34 +0000383 args.extend(["-default-min-sdk-version", "24"])
Jeff Gaston1cc06092018-03-28 15:51:02 -0400384 if include_static_deps:
385 args.append("-static-deps")
Jeff Gaston54868ee2018-04-25 19:43:35 -0400386 rewriteNames = sorted([name for name in maven_to_make if ":" in name] + [name for name in maven_to_make if ":" not in name])
Jeff Gastona0bce602018-08-24 14:05:03 -0400387 args.extend(["-rewrite=^" + name + "$=" + maven_to_make[name]['name'] for name in rewriteNames])
Alan Viverettecdfc9892021-08-31 19:35:58 +0000388 args.extend(["-rewrite=^" + key + "$=" + value for key, value in deps_rewrite.items()])
389 args.extend(["-extra-static-libs=" + maven_to_make[name]['name'] + "=" + ",".join(sorted(maven_to_make[name]['extra-static-libs'])) for name in maven_to_make if 'extra-static-libs' in maven_to_make[name]])
390 args.extend(["-optional-uses-libs=" + maven_to_make[name]['name'] + "=" + ",".join(sorted(maven_to_make[name]['optional-uses-libs'])) for name in maven_to_make if 'optional-uses-libs' in maven_to_make[name]])
Jeff Gaston07a3cf72018-08-24 14:16:34 -0400391 args.extend(["-host=" + name for name in maven_to_make if maven_to_make[name].get('host')])
Tony Mak0f658752019-07-19 11:11:05 +0100392 args.extend(["-host-and-device=" + name for name in maven_to_make if maven_to_make[name].get('host_and_device')])
Jeff Gaston7ec1d8f2018-03-27 00:25:12 -0400393 args.extend(["."])
Dan Willemsen814152e2017-11-06 13:22:11 -0800394 subprocess.check_call(args, stdout=f, cwd=working_dir)
Alan Viverette95f6d362017-04-06 09:40:50 -0400395
396 # Replace the old directory.
Jeff Gastonc302bb92018-03-23 13:53:48 -0400397 output_dir = os.path.join(cwd, transformed_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400398 mv(working_dir, output_dir)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500399 return True
Alan Viverette95f6d362017-04-06 09:40:50 -0400400
Jeff Gastonc302bb92018-03-23 13:53:48 -0400401# moves <artifact_info> (of type MavenLibraryInfo) into the appropriate part of <working_dir> , and possibly unpacks any necessary included files
Alan Viveretted8ce7222017-12-11 17:24:43 -0500402def transform_maven_lib(working_dir, artifact_info, extract_res):
Dan Willemsen814152e2017-11-06 13:22:11 -0800403 # Move library into working dir
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400404 new_dir = os.path.normpath(os.path.join(working_dir, os.path.relpath(artifact_info.dir, artifact_info.repo_dir)))
Jeff Gastonc302bb92018-03-23 13:53:48 -0400405 mv(artifact_info.dir, new_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800406
Alan Viverettec960cfb2017-12-04 13:09:22 -0500407 matcher = artifact_pattern.match(artifact_info.file)
408 maven_lib_name = artifact_info.key
Alan Viveretted4527e62017-05-11 15:03:25 -0400409 maven_lib_vers = matcher.group(2)
Alan Viverettec960cfb2017-12-04 13:09:22 -0500410 maven_lib_type = artifact_info.file[-3:]
Alan Viveretted4527e62017-05-11 15:03:25 -0400411
Alan Viverettef3c12722021-08-30 19:50:33 +0000412 group_artifact = artifact_info.key
413 make_lib_name = maven_to_make[group_artifact]['name']
414 make_dir_name = maven_to_make[group_artifact]['path']
Alan Viveretted4527e62017-05-11 15:03:25 -0400415
Alan Viverette129555e2018-01-30 09:57:57 -0500416 artifact_file = os.path.join(new_dir, artifact_info.file)
417
Colin Cross59b59db2018-04-24 13:58:05 -0700418 if maven_lib_type == "aar":
419 if extract_res:
420 target_dir = os.path.join(working_dir, make_dir_name)
421 if not os.path.exists(target_dir):
422 os.makedirs(target_dir)
Dan Willemsen814152e2017-11-06 13:22:11 -0800423
Alan Viverettec960cfb2017-12-04 13:09:22 -0500424 process_aar(artifact_file, target_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400425
Colin Cross59b59db2018-04-24 13:58:05 -0700426 with zipfile.ZipFile(artifact_file) as zip:
427 manifests_dir = os.path.join(working_dir, "manifests")
428 zip.extract("AndroidManifest.xml", os.path.join(manifests_dir, make_lib_name))
Dan Willemsend677b602017-11-08 22:13:17 -0800429
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500430 print(maven_lib_vers, ":", maven_lib_name, "->", make_lib_name)
Alan Viveretted4527e62017-05-11 15:03:25 -0400431
Alan Viverette95f6d362017-04-06 09:40:50 -0400432
Alan Viverettec960cfb2017-12-04 13:09:22 -0500433def process_aar(artifact_file, target_dir):
Alan Viverette95f6d362017-04-06 09:40:50 -0400434 # Extract AAR file to target_dir.
435 with zipfile.ZipFile(artifact_file) as zip:
436 zip.extractall(target_dir)
437
Dan Willemsen814152e2017-11-06 13:22:11 -0800438 # Remove classes.jar
Alan Viverette95f6d362017-04-06 09:40:50 -0400439 classes_jar = os.path.join(target_dir, "classes.jar")
440 if os.path.exists(classes_jar):
Dan Willemsen814152e2017-11-06 13:22:11 -0800441 os.remove(classes_jar)
Alan Viverette95f6d362017-04-06 09:40:50 -0400442
443 # Remove or preserve empty dirs.
444 for root, dirs, files in os.walk(target_dir):
445 for dir in dirs:
446 dir_path = os.path.join(root, dir)
447 if not os.listdir(dir_path):
448 os.rmdir(dir_path)
449
450 # Remove top-level cruft.
451 for file in blacklist_files:
452 file_path = os.path.join(target_dir, file)
453 if os.path.exists(file_path):
454 os.remove(file_path)
455
Alan Viverettef17c9402017-07-19 12:57:40 -0400456
Alan Viverettecd3de262017-08-14 09:51:30 -0400457def fetch_artifact(target, build_id, artifact_path):
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600458 global args
Jiyong Park1d1c9632018-05-29 17:45:27 +0900459 download_to = os.path.join('.', os.path.dirname(artifact_path))
460 print('Fetching %s from %s ...' % (artifact_path, target))
461 if not os.path.exists(download_to):
462 os.makedirs(download_to)
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600463 if args.beyond_corp:
464 fetch_cmd = [FETCH_ARTIFACT_BEYOND_CORP, '--use_oauth2',
465 '--bid', str(build_id), '--target', target, artifact_path, download_to]
466 else:
467 fetch_cmd = [FETCH_ARTIFACT,
468 '--bid', str(build_id), '--target', target, artifact_path, download_to]
Jeff Gaston97a9b762018-10-02 15:15:12 -0400469 print("Running: " + ' '.join(fetch_cmd))
Alan Viverette45837092017-05-12 14:50:53 -0400470 try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400471 subprocess.check_output(fetch_cmd, stderr=subprocess.STDOUT)
Alan Viverette45837092017-05-12 14:50:53 -0400472 except subprocess.CalledProcessError:
Jeff Gaston13e38412018-02-06 14:45:36 -0500473 print_e('FAIL: Unable to retrieve %s artifact for build ID %s' % (artifact_path, build_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500474 print_e('Please make sure you are authenticated for build server access!')
Alan Viverette45837092017-05-12 14:50:53 -0400475 return None
476 return artifact_path
477
478
Anton Hansson1d01a032018-04-09 10:29:37 +0100479def fetch_artifacts(target, build_id, artifact_dict):
480 for artifact, target_path in artifact_dict.items():
481 artifact_path = fetch_artifact(target, build_id.url_id, artifact)
482 if not artifact_path:
483 return False
484 mv(artifact_path, target_path)
485 return True
486
487
Alan Viverette129555e2018-01-30 09:57:57 -0500488def extract_artifact(artifact_path):
Alan Viverette45837092017-05-12 14:50:53 -0400489 # Unzip the repo archive into a separate directory.
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400490 repo_dir = os.path.basename(artifact_path)[:-4]
Alan Viverette45837092017-05-12 14:50:53 -0400491 with zipfile.ZipFile(artifact_path) as zipFile:
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400492 zipFile.extractall(repo_dir)
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400493 return repo_dir
494
495
Alan Viverette7e897e22018-03-09 15:24:10 -0500496def fetch_and_extract(target, build_id, file, artifact_path=None):
497 if not artifact_path:
498 artifact_path = fetch_artifact(target, build_id, file)
Alan Viverette129555e2018-01-30 09:57:57 -0500499 if not artifact_path:
500 return None
501 return extract_artifact(artifact_path)
502
503
Jeff Gaston08a2ee42018-05-07 18:08:41 -0400504def update_androidx(target, build_id, local_file):
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400505 if build_id:
Anthony Chenacbb4872018-07-02 11:22:48 -0700506 repo_file = 'top-of-tree-m2repository-all-%s.zip' % build_id.fs_id
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400507 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file, None)
508 else:
509 repo_dir = fetch_and_extract(target, None, None, local_file)
510 if not repo_dir:
511 print_e('Failed to extract AndroidX repository')
512 return False
513
Tony Mak0f658752019-07-19 11:11:05 +0100514 # Keep JavaPlugins.bp file untounched.
515 java_plugins_bp_path = os.path.join(androidx_dir, 'JavaPlugins.bp')
516 tmp_java_plugins_bp_path = os.path.join('/tmp', 'JavaPlugins.bp')
517 mv(java_plugins_bp_path, tmp_java_plugins_bp_path)
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400518
Tony Mak0f658752019-07-19 11:11:05 +0100519 # Transform the repo archive into a Makefile-compatible format.
520 if not transform_maven_repos([repo_dir], androidx_dir, extract_res=False):
521 return False
522
523 # Import JavaPlugins.bp in Android.bp.
524 makefile = os.path.join(androidx_dir, 'Android.bp')
525 with open(makefile, "a+") as f:
526 f.write('\nbuild = ["JavaPlugins.bp"]\n')
527 mv(tmp_java_plugins_bp_path, java_plugins_bp_path)
528
529 return True
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400530
Alan Viverette6dc45752020-04-16 14:56:20 +0000531
Jeff Gaston782c3e32018-02-06 14:36:17 -0500532def update_jetifier(target, build_id):
533 repo_file = 'jetifier-standalone.zip'
534 repo_dir = fetch_and_extract(target, build_id.url_id, repo_file)
535 if not repo_dir:
536 print_e('Failed to extract Jetifier')
537 return False
538
539 rm(jetifier_dir)
Jeff Gaston553a4372018-03-29 18:34:22 -0400540 mv(os.path.join(repo_dir, 'jetifier-standalone'), jetifier_dir)
541 os.chmod(os.path.join(jetifier_dir, 'bin', 'jetifier-standalone'), 0o755)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500542 return True
543
Alan Viverette7e897e22018-03-09 15:24:10 -0500544
Alan Viverette6dc45752020-04-16 14:56:20 +0000545def update_constraint(local_file):
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400546 repo_dir = extract_artifact(local_file)
547 if not repo_dir:
Alan Viverette6dc45752020-04-16 14:56:20 +0000548 print_e('Failed to extract Constraint Layout')
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400549 return False
550 return transform_maven_repos([repo_dir], os.path.join(extras_dir, 'constraint-layout-x'), extract_res=False)
551
Alan Viverette45837092017-05-12 14:50:53 -0400552
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400553def update_material(file):
554 design_dir = extract_artifact(file)
555 if not design_dir:
Alan Viverette6dc45752020-04-16 14:56:20 +0000556 print_e('Failed to extract Material Design Components')
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400557 return False
558
559 # Don't bother extracting resources -- this should only be used with AAPT2.
560 return transform_maven_repos([design_dir],
561 os.path.join(extras_dir, 'material-design-x'), extract_res=False)
562
563
Anton Hansson1d01a032018-04-09 10:29:37 +0100564def update_framework(build_id, sdk_dir):
Anton Hansson83f92172020-03-26 11:12:03 +0000565 api_scope_list = ['public', 'system', 'test', 'module-lib', 'system-server']
Sundong Ahn66091f22018-08-29 18:54:28 +0900566 if sdk_dir == 'current':
567 api_scope_list.append('core')
568
569 for api_scope in api_scope_list:
Jiyong Park1d1c9632018-05-29 17:45:27 +0900570 target_dir = path(sdk_dir, api_scope)
Sundong Ahn66091f22018-08-29 18:54:28 +0900571 if api_scope == 'core':
572 artifact_to_path = {'core.current.stubs.jar': path(target_dir, 'android.jar')}
573 else:
574 artifact_to_path = {'apistubs/android/' + api_scope + '/*.jar': path(target_dir, '*')}
Anton Hansson1d01a032018-04-09 10:29:37 +0100575
576 if not fetch_artifacts(framework_sdk_target, build_id, artifact_to_path):
577 return False
578
Jiyong Park1d1c9632018-05-29 17:45:27 +0900579 if api_scope == 'public':
Anton Hansson1d01a032018-04-09 10:29:37 +0100580 # Fetch a few artifacts from the public sdk.
Sundong Ahn66091f22018-08-29 18:54:28 +0900581 artifact = 'sdk-repo-linux-platforms-%s.zip' % build_id.fs_id
Anton Hansson1d01a032018-04-09 10:29:37 +0100582 artifact_path = fetch_artifact(framework_sdk_target, build_id.url_id, artifact)
Anton Hansson9709fd42018-04-03 16:52:13 +0100583 if not artifact_path:
584 return False
Anton Hansson1d01a032018-04-09 10:29:37 +0100585
586 with zipfile.ZipFile(artifact_path) as zipFile:
Colin Crossd338d702020-07-10 19:17:54 -0700587 extra_files = [
588 'android.jar',
589 'framework.aidl',
590 'uiautomator.jar',
591 'core-for-system-modules.jar',
592 'data/annotations.zip',
593 'data/api-versions.xml']
594 for filename in extra_files:
Anton Hanssona26e4812020-03-25 12:54:39 +0000595 matches = list(filter(lambda path: filename in path, zipFile.namelist()))
596 if len(matches) != 1:
597 print_e('Expected 1 file named \'%s\' in zip %s, found %d' %
598 (filename, zipFile.filename, len(matches)))
599 return False
600 zip_path = matches[0]
601 src_path = zipFile.extract(zip_path)
602 dst_path = path(target_dir, filename)
603 mv(src_path, dst_path)
Anton Hansson9709fd42018-04-03 16:52:13 +0100604
Anton Hansson73be1512021-06-24 10:56:05 +0100605 # Filtered API DB is currently only available for "public"
606 fetch_artifacts(framework_sdk_target, build_id, {'api-versions-public-filtered.xml': path(target_dir, 'data/api-versions-filtered.xml')})
607
Alan Viverettecd3de262017-08-14 09:51:30 -0400608 return True
Alan Viverette45837092017-05-12 14:50:53 -0400609
Sundong Ahncc34cc32018-07-11 15:18:47 +0900610def update_makefile(build_id):
611 template = '"%s",\n\
612 "current"'
613 makefile = os.path.join(git_dir, 'Android.bp')
614
615 with open(makefile, 'r+') as f:
616 contents = f.read().replace('"current"', template % build_id)
617 f.seek(0)
618 f.write(contents)
619
620 return True
Alan Viverette45837092017-05-12 14:50:53 -0400621
Anton Hansson1d01a032018-04-09 10:29:37 +0100622def finalize_sdk(build_id, sdk_version):
623 target_finalize_dir = '%d' % sdk_version
Anton Hansson9709fd42018-04-03 16:52:13 +0100624
Anton Hanssonaa554c02020-04-30 14:06:20 +0100625 for api_scope in ['public', 'system', 'test', 'module-lib', 'system-server']:
Sundong Ahn66091f22018-08-29 18:54:28 +0900626 artifact_to_path = {'apistubs/android/' + api_scope + '/api/*.txt':
627 path(target_finalize_dir, api_scope, 'api', '*')}
628
629 if not fetch_artifacts(framework_sdk_target, build_id, artifact_to_path):
630 return False
631
632 return update_framework(build_id, target_finalize_dir) \
Sundong Ahncc34cc32018-07-11 15:18:47 +0900633 and update_makefile(target_finalize_dir)
Anton Hansson9709fd42018-04-03 16:52:13 +0100634
Anton Hansson9709fd42018-04-03 16:52:13 +0100635
Anton Hansson1d01a032018-04-09 10:29:37 +0100636def update_framework_current(build_id):
637 return update_framework(build_id, current_path)
Anton Hansson9709fd42018-04-03 16:52:13 +0100638
639
Alan Viverettec1c32b62017-12-20 09:40:36 -0500640def update_buildtools(target, arch, build_id):
Jeff Gaston13e38412018-02-06 14:45:36 -0500641 artifact_path = fetch_and_extract(target, build_id.url_id,
642 "sdk-repo-%s-build-tools-%s.zip" % (arch, build_id.fs_id))
Alan Viverettec1c32b62017-12-20 09:40:36 -0500643 if not artifact_path:
644 return False
645
646 top_level_dir = os.listdir(artifact_path)[0]
647 src_path = os.path.join(artifact_path, top_level_dir)
648 dst_path = path(buildtools_dir, arch)
Colin Cross20648d32021-02-12 13:30:51 -0800649
650 # There are a few libraries that have been manually added to the
651 # build tools, copy them from the destination back to the source
652 # before the destination is overwritten.
653 files_to_save = (
654 'lib64/libconscrypt_openjdk_jni.dylib',
655 'lib64/libconscrypt_openjdk_jni.so',
656 'bin/lib64/libwinpthread-1.dll',
657 )
658 for file in files_to_save:
659 src_file = os.path.join(dst_path, file)
660 dst_file = os.path.join(src_path, file)
661 if os.path.exists(dst_path):
662 mv(src_file, dst_file)
663
Alan Viverettec1c32b62017-12-20 09:40:36 -0500664 mv(src_path, dst_path)
665
666 # Move all top-level files to /bin and make them executable
667 bin_path = path(dst_path, 'bin')
668 top_level_files = filter(lambda e: os.path.isfile(path(dst_path, e)), os.listdir(dst_path))
669 for file in top_level_files:
670 src_file = path(dst_path, file)
671 dst_file = path(bin_path, file)
672 mv(src_file, dst_file)
673 os.chmod(dst_file, 0o755)
674
Colin Cross6673ceb2021-02-12 13:14:14 -0800675 # Make the files under lld-bin executable
676 lld_bin_files = os.listdir(os.path.join(dst_path, 'lld-bin'))
677 for file in lld_bin_files:
678 os.chmod(os.path.join(dst_path, 'lld-bin', file), 0o755)
679
Alan Viverettec1c32b62017-12-20 09:40:36 -0500680 # Remove renderscript
681 rm(path(dst_path, 'renderscript'))
682
683 return True
684
685
Alan Viverettecd3de262017-08-14 09:51:30 -0400686def append(text, more_text):
687 if text:
688 return "%s, %s" % (text, more_text)
689 return more_text
Alan Viverette45837092017-05-12 14:50:53 -0400690
Alan Viverette95f6d362017-04-06 09:40:50 -0400691
Jeff Gaston13e38412018-02-06 14:45:36 -0500692class buildId(object):
693 def __init__(self, url_id, fs_id):
694 # id when used in build server urls
695 self.url_id = url_id
696 # id when used in build commands
697 self.fs_id = fs_id
698
Jeff Gastonb9b09052018-02-05 14:16:05 -0500699def getBuildId(args):
Jeff Gaston13e38412018-02-06 14:45:36 -0500700 # must be in the format 12345 or P12345
Jeff Gastonb9b09052018-02-05 14:16:05 -0500701 source = args.source
Jeff Gaston13e38412018-02-06 14:45:36 -0500702 number_text = source[:]
703 presubmit = False
704 if number_text.startswith("P"):
705 presubmit = True
706 number_text = number_text[1:]
707 if not number_text.isnumeric():
Alan Viverette7e897e22018-03-09 15:24:10 -0500708 return None
Jeff Gaston13e38412018-02-06 14:45:36 -0500709 url_id = source
710 fs_id = url_id
711 if presubmit:
712 fs_id = "0"
713 args.file = False
714 return buildId(url_id, fs_id)
Jeff Gastonb9b09052018-02-05 14:16:05 -0500715
Colin Cross573e1212018-02-12 14:59:52 -0800716def getFile(args):
717 source = args.source
718 if not source.isnumeric():
719 return args.source
Alan Viverette7e897e22018-03-09 15:24:10 -0500720 return None
Colin Cross573e1212018-02-12 14:59:52 -0800721
Alan Viverette4ec9a172018-02-20 16:19:31 -0500722
723def script_relative(rel_path):
Jeff Gastona68a8d42018-03-23 14:00:13 -0400724 return os.path.join(script_dir, rel_path)
Alan Viverette4ec9a172018-02-20 16:19:31 -0500725
726
Jeff Gastoncc296a82018-03-23 14:33:24 -0400727def uncommittedChangesExist():
728 try:
729 # Make sure we don't overwrite any pending changes.
730 diffCommand = "cd " + git_dir + " && git diff --quiet"
731 subprocess.check_call(diffCommand, shell=True)
732 subprocess.check_call(diffCommand + " --cached", shell=True)
733 return False
734 except subprocess.CalledProcessError:
735 return True
736
737
Jeff Gastona7fba9b2018-03-26 23:48:51 -0400738rm(temp_dir)
Alan Viveretted4527e62017-05-11 15:03:25 -0400739parser = argparse.ArgumentParser(
Alan Viverette45837092017-05-12 14:50:53 -0400740 description=('Update current prebuilts'))
Alan Viveretted4527e62017-05-11 15:03:25 -0400741parser.add_argument(
Alan Viverette129555e2018-01-30 09:57:57 -0500742 'source',
743 help='Build server build ID or local Maven ZIP file')
744parser.add_argument(
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400745 '-m', '--material', action="store_true",
Alan Viverette6dc45752020-04-16 14:56:20 +0000746 help='If specified, updates only Material Design Components')
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400747parser.add_argument(
Alan Viverette3e57a4a2017-08-11 15:49:47 -0400748 '-c', '--constraint', action="store_true",
749 help='If specified, updates only Constraint Layout')
750parser.add_argument(
Jeff Gaston782c3e32018-02-06 14:36:17 -0500751 '-j', '--jetifier', action="store_true",
752 help='If specified, updates only Jetifier')
753parser.add_argument(
Alan Viverette5bfe05b2017-06-06 14:21:29 -0400754 '-p', '--platform', action="store_true",
755 help='If specified, updates only the Android Platform')
Alan Viverettec1c32b62017-12-20 09:40:36 -0500756parser.add_argument(
Anton Hansson9709fd42018-04-03 16:52:13 +0100757 '-f', '--finalize_sdk', type=int,
758 help='If specified, imports the source build as the specified finalized SDK version')
759parser.add_argument(
Alan Viverettec1c32b62017-12-20 09:40:36 -0500760 '-b', '--buildtools', action="store_true",
761 help='If specified, updates only the Build Tools')
Jeff Gastoncc296a82018-03-23 14:33:24 -0400762parser.add_argument(
Alan Viverette6dc45752020-04-16 14:56:20 +0000763 '-x', '--androidx', action="store_true",
764 help='If specified, updates only the Jetpack (androidx) libraries excluding those covered by other arguments')
Jeff Gaston41fd5e62018-04-25 17:02:02 -0400765parser.add_argument(
Jeff Gastoncc296a82018-03-23 14:33:24 -0400766 '--commit-first', action="store_true",
767 help='If specified, then if uncommited changes exist, commit before continuing')
Jeff Sharkeyabe16df2021-05-25 10:09:03 -0600768parser.add_argument(
769 '--beyond-corp', action="store_true",
770 help='If specified, then fetch artifacts with tooling that works on BeyondCorp devices')
Alan Viveretted4527e62017-05-11 15:03:25 -0400771args = parser.parse_args()
Jeff Gaston56fe2762018-02-06 14:54:37 -0500772args.file = True
Alan Viverette129555e2018-01-30 09:57:57 -0500773if not args.source:
774 parser.error("You must specify a build ID or local Maven ZIP file")
Alan Viveretted4527e62017-05-11 15:03:25 -0400775 sys.exit(1)
Alan Viverette6dc45752020-04-16 14:56:20 +0000776if not (args.platform or args.buildtools \
777 or args.jetifier or args.androidx or args.material \
778 or args.finalize_sdk or args.constraint):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500779 parser.error("You must specify at least one target to update")
Alan Viverettecd3de262017-08-14 09:51:30 -0400780 sys.exit(1)
Alan Viverette6dc45752020-04-16 14:56:20 +0000781if (args.constraint or args.material or args.androidx) \
Colin Cross74683bc2018-04-11 17:36:18 -0700782 and which('pom2bp') is None:
Louis Pullen-Freilich61bb70e2019-04-09 16:15:32 +0100783 parser.error("Cannot find pom2bp in path; please run lunch to set up build environment. You may also need to run 'm pom2bp' if it hasn't been built already.")
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500784 sys.exit(1)
Alan Viveretted4527e62017-05-11 15:03:25 -0400785
Jeff Gastoncc296a82018-03-23 14:33:24 -0400786if uncommittedChangesExist():
787 if args.commit_first:
788 subprocess.check_call("cd " + git_dir + " && git add -u", shell=True)
789 subprocess.check_call("cd " + git_dir + " && git commit -m 'save working state'", shell=True)
790
791if uncommittedChangesExist():
Anton Hansson10861a02018-04-06 13:57:08 +0100792 print_e('FAIL: There are uncommitted changes here. Please commit or stash before continuing, because %s will run "git reset --hard" if execution fails' % os.path.basename(__file__))
Alan Viveretted4527e62017-05-11 15:03:25 -0400793 sys.exit(1)
794
795try:
Alan Viverettecd3de262017-08-14 09:51:30 -0400796 components = None
797 if args.constraint:
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400798 if update_constraint_x(getFile(args)):
799 components = append(components, 'Constraint Layout X')
800 else:
801 print_e('Failed to update Constraint Layout X, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400802 sys.exit(1)
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400803 if args.androidx:
Jeff Gaston17ccf8f2018-08-29 11:33:19 -0400804 if update_androidx('androidx', \
Jeff Gastonbbd6bfc2018-03-27 17:35:49 -0400805 getBuildId(args), getFile(args)):
806 components = append(components, 'AndroidX')
807 else:
808 print_e('Failed to update AndroidX, aborting...')
809 sys.exit(1)
Jeff Gaston782c3e32018-02-06 14:36:17 -0500810 if args.jetifier:
Jeff Gaston17ccf8f2018-08-29 11:33:19 -0400811 if update_jetifier('androidx', getBuildId(args)):
Jeff Gaston782c3e32018-02-06 14:36:17 -0500812 components = append(components, 'Jetifier')
813 else:
814 print_e('Failed to update Jetifier, aborting...')
815 sys.exit(1)
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100816 if args.platform or args.finalize_sdk:
Anton Hansson87654c42018-04-26 15:19:42 +0100817 if update_framework_current(getBuildId(args)):
Alan Viverettecd3de262017-08-14 09:51:30 -0400818 components = append(components, 'platform SDK')
819 else:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500820 print_e('Failed to update platform SDK, aborting...')
Alan Viverettecd3de262017-08-14 09:51:30 -0400821 sys.exit(1)
Anton Hansson9709fd42018-04-03 16:52:13 +0100822 if args.finalize_sdk:
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100823 n = args.finalize_sdk
824 if finalize_sdk(getBuildId(args), n):
825 # We commit the finalized dir separately from the current sdk update.
826 msg = "Import final sdk version %d from build %s" % (n, getBuildId(args).url_id)
827 subprocess.check_call(['git', 'add', '%d' % n])
Sundong Ahncc34cc32018-07-11 15:18:47 +0900828 subprocess.check_call(['git', 'add', 'Android.bp'])
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100829 subprocess.check_call(['git', 'commit', '-m', msg])
Anton Hansson9709fd42018-04-03 16:52:13 +0100830 else:
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100831 print_e('Failed to finalize SDK %d, aborting...' % n)
Anton Hansson9709fd42018-04-03 16:52:13 +0100832 sys.exit(1)
Jeff Gaston6e7e7502018-04-04 00:22:45 -0400833 if args.material:
834 if update_material(getFile(args)):
835 components = append(components, 'intermediate-AndroidX Design Library')
836 else:
837 print_e('Failed to update intermediate-AndroidX Design Library, aborting...')
Alan Viverette129555e2018-01-30 09:57:57 -0500838 sys.exit(1)
Alan Viverettec1c32b62017-12-20 09:40:36 -0500839 if args.buildtools:
Jeff Gastonb9b09052018-02-05 14:16:05 -0500840 if update_buildtools('sdk_phone_armv7-sdk_mac', 'darwin', getBuildId(args)) \
841 and update_buildtools('sdk_phone_x86_64-sdk', 'linux', getBuildId(args)) \
842 and update_buildtools('sdk_phone_armv7-win_sdk', 'windows', getBuildId(args)):
Alan Viverettec1c32b62017-12-20 09:40:36 -0500843 components = append(components, 'build tools')
844 else:
845 print_e('Failed to update build tools, aborting...')
846 sys.exit(1)
Jeff Gastonb70daa92018-03-29 20:10:22 -0400847
Jeff Gastonf12f3ce2018-03-23 16:41:24 -0400848
Alan Viveretted4527e62017-05-11 15:03:25 -0400849
Colin Cross941d9802018-05-03 21:37:57 -0700850 subprocess.check_call(['git', 'add', current_path, buildtools_dir])
Colin Cross573e1212018-02-12 14:59:52 -0800851 if not args.source.isnumeric():
Alan Viverette129555e2018-01-30 09:57:57 -0500852 src_msg = "local Maven ZIP"
853 else:
Jeff Gaston13e38412018-02-06 14:45:36 -0500854 src_msg = "build %s" % (getBuildId(args).url_id)
Alan Viverette129555e2018-01-30 09:57:57 -0500855 msg = "Import %s from %s\n\n%s" % (components, src_msg, flatten(sys.argv))
Alan Viveretted4527e62017-05-11 15:03:25 -0400856 subprocess.check_call(['git', 'commit', '-m', msg])
Anton Hanssonbe15e8c2018-04-26 17:35:45 +0100857 if args.finalize_sdk:
858 print('NOTE: Created two commits:')
859 subprocess.check_call(['git', 'log', '-2', '--oneline'])
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500860 print('Remember to test this change before uploading it to Gerrit!')
Alan Viveretted4527e62017-05-11 15:03:25 -0400861
862finally:
863 # Revert all stray files, including the downloaded zip.
864 try:
865 with open(os.devnull, 'w') as bitbucket:
866 subprocess.check_call(['git', 'add', '-Af', '.'], stdout=bitbucket)
867 subprocess.check_call(
Jeff Gaston9edf86c2018-04-18 13:45:23 -0400868 ['git', 'commit', '-m', 'COMMIT TO REVERT - RESET ME!!!', '--allow-empty'], stdout=bitbucket)
Alan Viveretted4527e62017-05-11 15:03:25 -0400869 subprocess.check_call(['git', 'reset', '--hard', 'HEAD~1'], stdout=bitbucket)
870 except subprocess.CalledProcessError:
Alan Viverettef5cb0e62017-11-17 15:15:23 -0500871 print_e('ERROR: Failed cleaning up, manual cleanup required!!!')