blob: ab789aa23f69f57ac320fccc657ddc242889adf7 [file] [log] [blame]
Ulya Trafimovicheb268862020-10-20 15:16:38 +01001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
18 "fmt"
Ulya Trafimovichc9f2b942020-12-23 15:41:29 +000019 "sort"
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000020 "strconv"
Ulya Trafimovicheb268862020-10-20 15:16:38 +010021 "strings"
22
23 "android/soong/android"
24)
25
Ulya Trafimovich480d1742020-11-20 15:30:03 +000026// This comment describes the following:
27// 1. the concept of class loader context (CLC) and its relation to classpath
28// 2. how PackageManager constructs CLC from shared libraries and their dependencies
29// 3. build-time vs. run-time CLC and why this matters for dexpreopt
30// 4. manifest fixer: a tool that adds missing <uses-library> tags to the manifests
31// 5. build system support for CLC
32//
33// 1. Class loader context
34// -----------------------
35//
36// Java libraries and apps that have run-time dependency on other libraries should list the used
37// libraries in their manifest (AndroidManifest.xml file). Each used library should be specified in
38// a <uses-library> tag that has the library name and an optional attribute specifying if the
39// library is optional or required. Required libraries are necessary for the library/app to run (it
40// will fail at runtime if the library cannot be loaded), and optional libraries are used only if
41// they are present (if not, the library/app can run without them).
42//
43// The libraries listed in <uses-library> tags are in the classpath of a library/app.
44//
45// Besides libraries, an app may also use another APK (for example in the case of split APKs), or
46// anything that gets added by the app dynamically. In general, it is impossible to know at build
47// time what the app may use at runtime. In the build system we focus on the known part: libraries.
48//
49// Class loader context (CLC) is a tree-like structure that describes class loader hierarchy. The
50// build system uses CLC in a more narrow sense: it is a tree of libraries that represents
51// transitive closure of all <uses-library> dependencies of a library/app. The top-level elements of
52// a CLC are the direct <uses-library> dependencies specified in the manifest (aka. classpath). Each
53// node of a CLC tree is a <uses-library> which may have its own <uses-library> sub-nodes.
54//
55// Because <uses-library> dependencies are, in general, a graph and not necessarily a tree, CLC may
56// contain subtrees for the same library multiple times. In other words, CLC is the dependency graph
57// "unfolded" to a tree. The duplication is only on a logical level, and the actual underlying class
58// loaders are not duplicated (at runtime there is a single class loader instance for each library).
59//
60// Example: A has <uses-library> tags B, C and D; C has <uses-library tags> B and D;
61// D has <uses-library> E; B and E have no <uses-library> dependencies. The CLC is:
62// A
63// ├── B
64// ├── C
65// │ ├── B
66// │ └── D
67// │ └── E
68// └── D
69// └── E
70//
71// CLC defines the lookup order of libraries when resolving Java classes used by the library/app.
72// The lookup order is important because libraries may contain duplicate classes, and the class is
73// resolved to the first match.
74//
75// 2. PackageManager and "shared" libraries
76// ----------------------------------------
77//
78// In order to load an APK at runtime, PackageManager (in frameworks/base) creates a CLC. It adds
79// the libraries listed in the <uses-library> tags in the app's manifest as top-level CLC elements.
80// For each of the used libraries PackageManager gets all its <uses-library> dependencies (specified
81// as tags in the manifest of that library) and adds a nested CLC for each dependency. This process
82// continues recursively until all leaf nodes of the constructed CLC tree are libraries that have no
83// <uses-library> dependencies.
84//
85// PackageManager is aware only of "shared" libraries. The definition of "shared" here differs from
86// its usual meaning (as in shared vs. static). In Android, Java "shared" libraries are those listed
87// in /system/etc/permissions/platform.xml file. This file is installed on device. Each entry in it
88// contains the name of a "shared" library, a path to its DEX jar file and a list of dependencies
89// (other "shared" libraries that this one uses at runtime and specifies them in <uses-library> tags
90// in its manifest).
91//
92// In other words, there are two sources of information that allow PackageManager to construct CLC
93// at runtime: <uses-library> tags in the manifests and "shared" library dependencies in
94// /system/etc/permissions/platform.xml.
95//
96// 3. Build-time and run-time CLC and dexpreopt
97// --------------------------------------------
98//
99// CLC is needed not only when loading a library/app, but also when compiling it. Compilation may
100// happen either on device (known as "dexopt") or during the build (known as "dexpreopt"). Since
101// dexopt takes place on device, it has the same information as PackageManager (manifests and
102// shared library dependencies). Dexpreopt, on the other hand, takes place on host and in a totally
103// different environment, and it has to get the same information from the build system (see the
104// section about build system support below).
105//
106// Thus, the build-time CLC used by dexpreopt and the run-time CLC used by PackageManager are
107// the same thing, but computed in two different ways.
108//
109// It is important that build-time and run-time CLCs coincide, otherwise the AOT-compiled code
110// created by dexpreopt will be rejected. In order to check the equality of build-time and
111// run-time CLCs, the dex2oat compiler records build-time CLC in the *.odex files (in the
112// "classpath" field of the OAT file header). To find the stored CLC, use the following command:
113// `oatdump --oat-file=<FILE> | grep '^classpath = '`.
114//
115// Mismatch between build-time and run-time CLC is reported in logcat during boot (search with
116// `logcat | grep -E 'ClassLoaderContext [a-z ]+ mismatch'`. Mismatch is bad for performance, as it
117// forces the library/app to either be dexopted, or to run without any optimizations (e.g. the app's
118// code may need to be extracted in memory from the APK, a very expensive operation).
119//
120// A <uses-library> can be either optional or required. From dexpreopt standpoint, required library
121// must be present at build time (its absence is a build error). An optional library may be either
122// present or absent at build time: if present, it will be added to the CLC, passed to dex2oat and
123// recorded in the *.odex file; otherwise, if the library is absent, it will be skipped and not
124// added to CLC. If there is a mismatch between built-time and run-time status (optional library is
125// present in one case, but not the other), then the build-time and run-time CLCs won't match and
126// the compiled code will be rejected. It is unknown at build time if the library will be present at
127// runtime, therefore either including or excluding it may cause CLC mismatch.
128//
129// 4. Manifest fixer
130// -----------------
131//
132// Sometimes <uses-library> tags are missing from the source manifest of a library/app. This may
133// happen for example if one of the transitive dependencies of the library/app starts using another
134// <uses-library>, and the library/app's manifest isn't updated to include it.
135//
136// Soong can compute some of the missing <uses-library> tags for a given library/app automatically
137// as SDK libraries in the transitive dependency closure of the library/app. The closure is needed
138// because a library/app may depend on a static library that may in turn depend on an SDK library,
139// (possibly transitively via another library).
140//
141// Not all <uses-library> tags can be computed in this way, because some of the <uses-library>
142// dependencies are not SDK libraries, or they are not reachable via transitive dependency closure.
143// But when possible, allowing Soong to calculate the manifest entries is less prone to errors and
144// simplifies maintenance. For example, consider a situation when many apps use some static library
145// that adds a new <uses-library> dependency -- all the apps will have to be updated. That is
146// difficult to maintain.
147//
148// Soong computes the libraries that need to be in the manifest as the top-level libraries in CLC.
149// These libraries are passed to the manifest_fixer.
150//
151// All libraries added to the manifest should be "shared" libraries, so that PackageManager can look
152// up their dependencies and reconstruct the nested subcontexts at runtime. There is no build check
153// to ensure this, it is an assumption.
154//
155// 5. Build system support
156// -----------------------
157//
158// In order to construct CLC for dexpreopt and manifest_fixer, the build system needs to know all
159// <uses-library> dependencies of the dexpreopted library/app (including transitive dependencies).
160// For each <uses-librarry> dependency it needs to know the following information:
161//
162// - the real name of the <uses-library> (it may be different from the module name)
163// - build-time (on host) and run-time (on device) paths to the DEX jar file of the library
164// - whether this library is optional or required
165// - all <uses-library> dependencies
166//
167// Since the build system doesn't have access to the manifest contents (it cannot read manifests at
168// the time of build rule generation), it is necessary to copy this information to the Android.bp
169// and Android.mk files. For blueprints, the relevant properties are `uses_libs` and
170// `optional_uses_libs`. For makefiles, relevant variables are `LOCAL_USES_LIBRARIES` and
171// `LOCAL_OPTIONAL_USES_LIBRARIES`. It is preferable to avoid specifying these properties explicilty
172// when they can be computed automatically by Soong (as the transitive closure of SDK library
173// dependencies).
174//
175// Some of the Java libraries that are used as <uses-library> are not SDK libraries (they are
176// defined as `java_library` rather than `java_sdk_library` in the Android.bp files). In order for
177// the build system to handle them automatically like SDK libraries, it is possible to set a
178// property `provides_uses_lib` or variable `LOCAL_PROVIDES_USES_LIBRARY` on the blueprint/makefile
179// module of such library. This property can also be used to specify real library name in cases
180// when it differs from the module name.
181//
182// Because the information from the manifests has to be duplicated in the Android.bp/Android.mk
183// files, there is a danger that it may get out of sync. To guard against that, the build system
184// generates a rule that checks the metadata in the build files against the contents of a manifest
185// (verify_uses_libraries). The manifest can be available as a source file, or as part of a prebuilt
186// APK. Note that reading the manifests at the Ninja stage of the build is fine, unlike the build
187// rule generation phase.
188//
189// ClassLoaderContext is a structure that represents CLC.
190//
191type ClassLoaderContext struct {
192 // The name of the library.
193 Name string
194
195 // On-host build path to the library dex file (used in dex2oat argument --class-loader-context).
196 Host android.Path
197
198 // On-device install path (used in dex2oat argument --stored-class-loader-context).
199 Device string
200
201 // Nested sub-CLC for dependencies.
202 Subcontexts []*ClassLoaderContext
203}
204
205// ClassLoaderContextMap is a map from SDK version to CLC. There is a special entry with key
206// AnySdkVersion that stores unconditional CLC that is added regardless of the target SDK version.
207//
208// Conditional CLC is for compatibility libraries which didn't exist prior to a certain SDK version
209// (say, N), but classes in them were in the bootclasspath jars, etc., and in version N they have
210// been separated into a standalone <uses-library>. Compatibility libraries should only be in the
211// CLC if the library/app that uses them has `targetSdkVersion` less than N in the manifest.
212//
213// Currently only apps (but not libraries) use conditional CLC.
214//
215// Target SDK version information is unavailable to the build system at rule generation time, so
216// the build system doesn't know whether conditional CLC is needed for a given app or not. So it
217// generates a build rule that includes conditional CLC for all versions, extracts the target SDK
218// version from the manifest, and filters the CLCs based on that version. Exact final CLC that is
219// passed to dex2oat is unknown to the build system, and gets known only at Ninja stage.
220//
221type ClassLoaderContextMap map[int][]*ClassLoaderContext
222
223// Compatibility libraries. Some are optional, and some are required: this is the default that
224// affects how they are handled by the Soong logic that automatically adds implicit SDK libraries
225// to the manifest_fixer, but an explicit `uses_libs`/`optional_uses_libs` can override this.
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100226var OrgApacheHttpLegacy = "org.apache.http.legacy"
227var AndroidTestBase = "android.test.base"
228var AndroidTestMock = "android.test.mock"
229var AndroidHidlBase = "android.hidl.base-V1.0-java"
230var AndroidHidlManager = "android.hidl.manager-V1.0-java"
231
Ulya Trafimovich480d1742020-11-20 15:30:03 +0000232// Compatibility libraries grouped by version/optionality (for convenience, to avoid repeating the
233// same lists in multiple places).
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100234var OptionalCompatUsesLibs28 = []string{
235 OrgApacheHttpLegacy,
236}
237var OptionalCompatUsesLibs30 = []string{
238 AndroidTestBase,
239 AndroidTestMock,
240}
241var CompatUsesLibs29 = []string{
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100242 AndroidHidlManager,
Ulya Trafimovichc9f2b942020-12-23 15:41:29 +0000243 AndroidHidlBase,
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100244}
245var OptionalCompatUsesLibs = append(android.CopyOf(OptionalCompatUsesLibs28), OptionalCompatUsesLibs30...)
246var CompatUsesLibs = android.CopyOf(CompatUsesLibs29)
247
248const UnknownInstallLibraryPath = "error"
249
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000250// AnySdkVersion means that the class loader context is needed regardless of the targetSdkVersion
251// of the app. The numeric value affects the key order in the map and, as a result, the order of
252// arguments passed to construct_context.py (high value means that the unconditional context goes
253// last). We use the converntional "current" SDK level (10000), but any big number would do as well.
254const AnySdkVersion int = android.FutureApiLevelInt
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100255
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000256// Add class loader context for the given library to the map entry for the given SDK version.
257func (clcMap ClassLoaderContextMap) addContext(ctx android.ModuleInstallPathContext, sdkVer int, lib string,
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000258 hostPath, installPath android.Path, strict bool, nestedClcMap ClassLoaderContextMap) error {
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100259
260 // If missing dependencies are allowed, the build shouldn't fail when a <uses-library> is
261 // not found. However, this is likely to result is disabling dexpreopt, as it won't be
262 // possible to construct class loader context without on-host and on-device library paths.
263 strict = strict && !ctx.Config().AllowMissingDependencies()
264
265 if hostPath == nil && strict {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000266 return fmt.Errorf("unknown build path to <uses-library> \"%s\"", lib)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100267 }
268
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000269 devicePath := UnknownInstallLibraryPath
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100270 if installPath == nil {
271 if android.InList(lib, CompatUsesLibs) || android.InList(lib, OptionalCompatUsesLibs) {
272 // Assume that compatibility libraries are installed in /system/framework.
273 installPath = android.PathForModuleInstall(ctx, "framework", lib+".jar")
274 } else if strict {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000275 return fmt.Errorf("unknown install path to <uses-library> \"%s\"", lib)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100276 } else {
277 // For some stub libraries the only known thing is the name of their implementation
278 // library, but the library itself is unavailable (missing or part of a prebuilt). In
279 // such cases we still need to add the library to <uses-library> tags in the manifest,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000280 // but we cannot use it for dexpreopt.
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100281 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100282 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000283 if installPath != nil {
284 devicePath = android.InstallPathToOnDevicePath(ctx, installPath.(android.InstallPath))
285 }
286
Ulya Trafimovich5e13a732020-11-03 15:33:03 +0000287 // Nested class loader context shouldn't have conditional part (it is allowed only at the top level).
288 for ver, _ := range nestedClcMap {
289 if ver != AnySdkVersion {
290 clcStr, _ := ComputeClassLoaderContext(nestedClcMap)
291 return fmt.Errorf("nested class loader context shouldn't have conditional part: %s", clcStr)
292 }
293 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000294 subcontexts := nestedClcMap[AnySdkVersion]
295
296 // If the library with this name is already present as one of the unconditional top-level
297 // components, do not re-add it.
298 for _, clc := range clcMap[sdkVer] {
299 if clc.Name == lib {
300 return nil
301 }
302 }
303
304 clcMap[sdkVer] = append(clcMap[sdkVer], &ClassLoaderContext{
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000305 Name: lib,
306 Host: hostPath,
307 Device: devicePath,
308 Subcontexts: subcontexts,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000309 })
Ulya Trafimovich69612672020-10-20 17:41:54 +0100310 return nil
311}
312
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000313// Wrapper around addContext that reports errors.
314func (clcMap ClassLoaderContextMap) addContextOrReportError(ctx android.ModuleInstallPathContext, sdkVer int, lib string,
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000315 hostPath, installPath android.Path, strict bool, nestedClcMap ClassLoaderContextMap) {
Ulya Trafimovich69612672020-10-20 17:41:54 +0100316
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000317 err := clcMap.addContext(ctx, sdkVer, lib, hostPath, installPath, strict, nestedClcMap)
Ulya Trafimovich69612672020-10-20 17:41:54 +0100318 if err != nil {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000319 ctx.ModuleErrorf(err.Error())
Ulya Trafimovich69612672020-10-20 17:41:54 +0100320 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100321}
322
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000323// Add class loader context. Fail on unknown build/install paths.
324func (clcMap ClassLoaderContextMap) AddContext(ctx android.ModuleInstallPathContext, lib string,
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000325 hostPath, installPath android.Path) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000326
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000327 clcMap.addContextOrReportError(ctx, AnySdkVersion, lib, hostPath, installPath, true, nil)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100328}
329
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000330// Add class loader context if the library exists. Don't fail on unknown build/install paths.
331func (clcMap ClassLoaderContextMap) MaybeAddContext(ctx android.ModuleInstallPathContext, lib *string,
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000332 hostPath, installPath android.Path) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000333
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100334 if lib != nil {
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000335 clcMap.addContextOrReportError(ctx, AnySdkVersion, *lib, hostPath, installPath, false, nil)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100336 }
337}
338
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000339// Add class loader context for the given SDK version. Don't fail on unknown build/install paths, as
340// libraries with unknown paths still need to be processed by manifest_fixer (which doesn't care
341// about paths). For the subset of libraries that are used in dexpreopt, their build/install paths
342// are validated later before CLC is used (in validateClassLoaderContext).
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000343func (clcMap ClassLoaderContextMap) AddContextForSdk(ctx android.ModuleInstallPathContext, sdkVer int,
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000344 lib string, hostPath, installPath android.Path, nestedClcMap ClassLoaderContextMap) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000345
Ulya Trafimovich88bb6f62020-12-16 16:16:11 +0000346 clcMap.addContextOrReportError(ctx, sdkVer, lib, hostPath, installPath, false, nestedClcMap)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100347}
348
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000349// Merge the other class loader context map into this one, do not override existing entries.
Ulya Trafimovich18554242020-11-03 15:55:11 +0000350// The implicitRootLib parameter is the name of the library for which the other class loader
351// context map was constructed. If the implicitRootLib is itself a <uses-library>, it should be
352// already present in the class loader context (with the other context as its subcontext) -- in
353// that case do not re-add the other context. Otherwise add the other context at the top-level.
354func (clcMap ClassLoaderContextMap) AddContextMap(otherClcMap ClassLoaderContextMap, implicitRootLib string) {
355 if otherClcMap == nil {
356 return
357 }
358
359 // If the implicit root of the merged map is already present as one of top-level subtrees, do
360 // not merge it second time.
361 for _, clc := range clcMap[AnySdkVersion] {
362 if clc.Name == implicitRootLib {
363 return
364 }
365 }
366
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000367 for sdkVer, otherClcs := range otherClcMap {
368 for _, otherClc := range otherClcs {
369 alreadyHave := false
370 for _, clc := range clcMap[sdkVer] {
371 if clc.Name == otherClc.Name {
372 alreadyHave = true
373 break
374 }
375 }
376 if !alreadyHave {
377 clcMap[sdkVer] = append(clcMap[sdkVer], otherClc)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100378 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100379 }
380 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100381}
382
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000383// Returns top-level libraries in the CLC (conditional CLC, i.e. compatibility libraries are not
384// included). This is the list of libraries that should be in the <uses-library> tags in the
385// manifest. Some of them may be present in the source manifest, others are added by manifest_fixer.
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000386func (clcMap ClassLoaderContextMap) UsesLibs() (ulibs []string) {
387 if clcMap != nil {
Ulya Trafimovich78a71552020-11-25 14:20:52 +0000388 clcs := clcMap[AnySdkVersion]
389 ulibs = make([]string, 0, len(clcs))
390 for _, clc := range clcs {
391 ulibs = append(ulibs, clc.Name)
Ulya Trafimovicha8c28e22020-10-06 17:24:19 +0100392 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100393 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000394 return ulibs
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100395}
396
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100397// Now that the full unconditional context is known, reconstruct conditional context.
398// Apply filters for individual libraries, mirroring what the PackageManager does when it
399// constructs class loader context on device.
400//
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000401// TODO(b/132357300): remove "android.hidl.manager" and "android.hidl.base" for non-system apps.
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100402//
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000403func fixClassLoaderContext(clcMap ClassLoaderContextMap) {
404 usesLibs := clcMap.UsesLibs()
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100405
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000406 for sdkVer, clcs := range clcMap {
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100407 if sdkVer == AnySdkVersion {
408 continue
409 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000410 fixedClcs := []*ClassLoaderContext{}
411 for _, clc := range clcs {
412 if android.InList(clc.Name, usesLibs) {
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100413 // skip compatibility libraries that are already included in unconditional context
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000414 } else if clc.Name == AndroidTestMock && !android.InList("android.test.runner", usesLibs) {
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100415 // android.test.mock is only needed as a compatibility library (in conditional class
416 // loader context) if android.test.runner is used, otherwise skip it
417 } else {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000418 fixedClcs = append(fixedClcs, clc)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100419 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000420 clcMap[sdkVer] = fixedClcs
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100421 }
422 }
423}
424
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000425// Return true if all build/install library paths are valid (including recursive subcontexts),
426// otherwise return false. A build path is valid if it's not nil. An install path is valid if it's
427// not equal to a special "error" value.
428func validateClassLoaderContext(clcMap ClassLoaderContextMap) (bool, error) {
429 for sdkVer, clcs := range clcMap {
430 if valid, err := validateClassLoaderContextRec(sdkVer, clcs); !valid || err != nil {
431 return valid, err
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100432 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000433 }
434 return true, nil
435}
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100436
Ulya Trafimovich480d1742020-11-20 15:30:03 +0000437// Helper function for validateClassLoaderContext() that handles recursion.
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000438func validateClassLoaderContextRec(sdkVer int, clcs []*ClassLoaderContext) (bool, error) {
439 for _, clc := range clcs {
440 if clc.Host == nil || clc.Device == UnknownInstallLibraryPath {
441 if sdkVer == AnySdkVersion {
442 // Return error if dexpreopt doesn't know paths to one of the <uses-library>
443 // dependencies. In the future we may need to relax this and just disable dexpreopt.
Ulya Trafimovich78210f62020-12-02 13:06:47 +0000444 if clc.Host == nil {
445 return false, fmt.Errorf("invalid build path for <uses-library> \"%s\"", clc.Name)
446 } else {
447 return false, fmt.Errorf("invalid install path for <uses-library> \"%s\"", clc.Name)
448 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000449 } else {
450 // No error for compatibility libraries, as Soong doesn't know if they are needed
451 // (this depends on the targetSdkVersion in the manifest), but the CLC is invalid.
452 return false, nil
453 }
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100454 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000455 if valid, err := validateClassLoaderContextRec(sdkVer, clc.Subcontexts); !valid || err != nil {
456 return valid, err
457 }
458 }
459 return true, nil
460}
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100461
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000462// Return the class loader context as a string, and a slice of build paths for all dependencies.
463// Perform a depth-first preorder traversal of the class loader context tree for each SDK version.
464// Return the resulting string and a slice of on-host build paths to all library dependencies.
465func ComputeClassLoaderContext(clcMap ClassLoaderContextMap) (clcStr string, paths android.Paths) {
Ulya Trafimovichc9f2b942020-12-23 15:41:29 +0000466 // CLC for different SDK versions should come in specific order that agrees with PackageManager.
467 // Since PackageManager processes SDK versions in ascending order and prepends compatibility
468 // libraries at the front, the required order is descending, except for AnySdkVersion that has
469 // numerically the largest order, but must be the last one. Example of correct order: [30, 29,
470 // 28, AnySdkVersion]. There are Soong tests to ensure that someone doesn't change this by
471 // accident, but there is no way to guard against changes in the PackageManager, except for
472 // grepping logcat on the first boot for absence of the following messages:
473 //
474 // `logcat | grep -E 'ClassLoaderContext [a-z ]+ mismatch`
475 //
476 versions := make([]int, 0, len(clcMap))
477 for ver, _ := range clcMap {
478 if ver != AnySdkVersion {
479 versions = append(versions, ver)
480 }
481 }
482 sort.Sort(sort.Reverse(sort.IntSlice(versions))) // descending order
483 versions = append(versions, AnySdkVersion)
484
485 for _, sdkVer := range versions {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000486 sdkVerStr := fmt.Sprintf("%d", sdkVer)
487 if sdkVer == AnySdkVersion {
488 sdkVerStr = "any" // a special keyword that means any SDK version
489 }
490 hostClc, targetClc, hostPaths := computeClassLoaderContextRec(clcMap[sdkVer])
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100491 if hostPaths != nil {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000492 clcStr += fmt.Sprintf(" --host-context-for-sdk %s %s", sdkVerStr, hostClc)
493 clcStr += fmt.Sprintf(" --target-context-for-sdk %s %s", sdkVerStr, targetClc)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100494 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000495 paths = append(paths, hostPaths...)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100496 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000497 return clcStr, android.FirstUniquePaths(paths)
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100498}
499
Ulya Trafimovich480d1742020-11-20 15:30:03 +0000500// Helper function for ComputeClassLoaderContext() that handles recursion.
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000501func computeClassLoaderContextRec(clcs []*ClassLoaderContext) (string, string, android.Paths) {
502 var paths android.Paths
503 var clcsHost, clcsTarget []string
504
505 for _, clc := range clcs {
506 subClcHost, subClcTarget, subPaths := computeClassLoaderContextRec(clc.Subcontexts)
507 if subPaths != nil {
508 subClcHost = "{" + subClcHost + "}"
509 subClcTarget = "{" + subClcTarget + "}"
510 }
511
512 clcsHost = append(clcsHost, "PCL["+clc.Host.String()+"]"+subClcHost)
513 clcsTarget = append(clcsTarget, "PCL["+clc.Device+"]"+subClcTarget)
514
515 paths = append(paths, clc.Host)
516 paths = append(paths, subPaths...)
517 }
518
519 clcHost := strings.Join(clcsHost, "#")
520 clcTarget := strings.Join(clcsTarget, "#")
521
522 return clcHost, clcTarget, paths
523}
524
Ulya Trafimovich480d1742020-11-20 15:30:03 +0000525// JSON representation of <uses-library> paths on host and on device.
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100526type jsonLibraryPath struct {
527 Host string
528 Device string
529}
530
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000531// Class loader contexts that come from Make (via JSON dexpreopt.config) files have simpler
532// structure than Soong class loader contexts: they are flat maps from a <uses-library> name to its
533// on-host and on-device paths. There are no nested subcontexts. It is a limitation of the current
534// Make implementation.
535type jsonClassLoaderContext map[string]jsonLibraryPath
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100536
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000537// A map from SDK version (represented with a JSON string) to JSON class loader context.
538type jsonClassLoaderContextMap map[string]jsonClassLoaderContext
539
540// Convert JSON class loader context map to ClassLoaderContextMap.
541func fromJsonClassLoaderContext(ctx android.PathContext, jClcMap jsonClassLoaderContextMap) ClassLoaderContextMap {
542 clcMap := make(ClassLoaderContextMap)
543 for sdkVerStr, clc := range jClcMap {
544 sdkVer, ok := strconv.Atoi(sdkVerStr)
545 if ok != nil {
546 if sdkVerStr == "any" {
547 sdkVer = AnySdkVersion
548 } else {
549 android.ReportPathErrorf(ctx, "failed to parse SDK version in dexpreopt.config: '%s'", sdkVerStr)
550 }
551 }
552 for lib, path := range clc {
553 clcMap[sdkVer] = append(clcMap[sdkVer], &ClassLoaderContext{
554 Name: lib,
555 Host: constructPath(ctx, path.Host),
556 Device: path.Device,
557 Subcontexts: nil,
558 })
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100559 }
560 }
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000561 return clcMap
Ulya Trafimovicheb268862020-10-20 15:16:38 +0100562}